-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathact_actfile.cpp
More file actions
217 lines (194 loc) · 3.88 KB
/
act_actfile.cpp
File metadata and controls
217 lines (194 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "act_actfile.h"
#include "act_utils.h"
std::unique_ptr<Act::Object> Act::ReadActFromFile(AbstractInputStream s)
{
static const std::uint32_t magic_act1 = 0x31544341; //"ACT1"
try
{
Task task("ReadActFromFile");
if (s->StrictMode())
{
std::uint32_t magic = (std::uint32_t) s->ReadInt32();
if (magic != magic_act1)
{
task.Error("incorrect magic number");
}
std::int32_t unknown = s->ReadInt32();
if (unknown != 1)
{
task.Error("incorrect file type");
}
std::int32_t offset = s->ReadInt32();
if (offset != 0)
{
s->SkipBytes(offset);
}
}
return Act::Object::ReadObject(s, ".?AVCAct@@");
}
catch (std::exception&)
{
return nullptr;
}
}
std::unique_ptr<Act::McdFile> Act::ReadMcdFromFile(AbstractInputStream s)
{
static const std::uint32_t magic_2dmc = 0x434D4432; //"2DMC"
try
{
Task task("ReadMcdFromFile");
if (s->StrictMode())
{
std::uint32_t magic = (std::uint32_t) s->ReadInt32();
if (magic != magic_2dmc)
{
task.Error("incorrect magic number");
}
std::int32_t unknown = s->ReadInt32();
if (unknown != 2)
{
task.Error("incorrect file type");
}
std::int32_t offset = s->ReadInt32();
if (offset != 0)
{
task.Error("non-zero offset is not supported");
}
}
std::unique_ptr<McdFile> ret = std::make_unique<McdFile>();
ret->Read(s);
return ret;
}
catch (int&)
{
return nullptr;
}
}
void Act::WriteMcdToFile(std::shared_ptr<ISerializable> obj, AbstractOutputStream s)
{
static const std::uint32_t magic_2dmc = 0x434D4432; //"2DMC"
//header
if (s->StrictMode())
{
s->WriteInt32("magic", magic_2dmc);
s->WriteInt32("type", 2);
s->WriteInt32("offset", 0);
}
obj->Write(s);
}
void Act::WriteActToFile(std::shared_ptr<ISerializable> obj, AbstractOutputStream s)
{
static const std::uint32_t magic_act1 = 0x31544341; //"ACT1"
//header
if (s->StrictMode())
{
s->WriteInt32("magic", magic_act1);
s->WriteInt32("type", 1);
s->WriteInt32("offset", 0);
}
obj->Write(s);
}
void Act::McdFile::Read(Act::AbstractInputStream s)
{
Task task("ReadMcd");
std::int32_t count_data;
std::int32_t size_data;
if (s->StrictMode())
{
count_data = s->ReadInt32();
size_data = s->ReadInt32();
UnknownData.reserve(count_data);
}
else
{
size_data = 56;
}
if (size_data != 56)
{
task.Error("incorrect segment length");
}
s->ReadArray(std::string(), [&](){
if (s->StrictMode())
{
for (int i = 0; i < count_data; ++i)
{
s->ReadObject(std::string(), [&](){
McdFile::Segment seg;
seg.Read(s);
UnknownData.push_back(seg);
s->ReadInt32();
});
}
}
else
{
while (!s->EndOfBlock())
{
s->ReadObject(std::string(), [&](){
McdFile::Segment seg;
seg.Read(s);
UnknownData.push_back(seg);
});
}
}
});
std::int32_t count_res;
if (s->StrictMode())
{
count_res = s->ReadInt32();
Resources.reserve(count_res);
}
s->ReadArray(std::string(), [&](){
if (s->StrictMode())
{
for (int i = 0; i < count_res; ++i)
{
s->ReadObject(std::string(), [&](){
Resources.emplace_back(Object::ReadObject(s));
});
}
}
else
{
while (!s->EndOfBlock())
{
s->ReadObject(std::string(), [&](){
Resources.emplace_back(Object::ReadObject(s));
});
}
}
});
}
void Act::McdFile::Write(Act::AbstractOutputStream s)
{
if (s->StrictMode())
{
s->WriteInt32("unknown_count", UnknownData.size());
s->WriteInt32("unknown_size", 56);
}
s->WriteArray("chips", [&](){
for (auto&& seg : UnknownData)
{
s->WriteObject("", [&](){
seg.Write(s);
});
if (s->StrictMode())
{
s->WriteNextPosition();
}
}
});
if (s->StrictMode())
{
s->WriteInt32("resouce_count", Resources.size());
}
s->WriteArray("resources", [&](){
for (auto&& res : Resources)
{
s->WriteObject("", [&](){
s->WriteInt32("classid", res->GetClassID());
res->Write(s);
});
}
});
}