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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
#include "ZFile.h"
#include "ZTexture.h"
#include "ZBlob.h"
#include "ZAnimation.h"
#include "HighLevel/HLModelIntermediette.h"
#include "HighLevel/HLAnimationIntermediette.h"
#include "Overlays/ZOverlay.h"
#include "Path.h"
#include "File.h"
#include "Globals.h"
#if !defined(_MSC_VER) && !defined(__CYGWIN__)
#include <execinfo.h>
#include <unistd.h>
#include <signal.h>
#endif
#include <string>
#include "tinyxml2.h"
using namespace tinyxml2;
using namespace std;
bool Parse(string xmlFilePath, string basePath, string outPath, ZFileMode fileMode);
void BuildAssetTexture(string pngFilePath, TextureType texType, string outPath);
void BuildAssetBlob(string blobFilePath, string outPath);
void BuildAssetModelIntermediette(string mdlPath, string outPath);
void BuildAssetAnimationIntermediette(string animPath, string outPath);
int NewMain(int argc, char* argv[]);
#if !defined(_MSC_VER) && !defined(__CYGWIN__)
void ErrorHandler(int sig)
{
void* array[4096];
char** symbols;
size_t size;
size = backtrace(array, 4096);
symbols = backtrace_symbols(array, 4096);
for (int i = 1; i < size; i++)
{
size_t len = strlen(symbols[i]);
cout << symbols[i] << "\n";
}
//cout << "Error: signal " << sig << ":\n";
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}
#endif
int main(int argc, char* argv[])
{
Globals* g = new Globals();
return NewMain(argc, argv);
}
int NewMain(int argc, char* argv[])
{
// Syntax: ZAPD.exe [mode (b/btex/bovl/e)] (Arbritrary Number of Arguments)
printf("ZAPD: Zelda Asset Processor For Decomp\n");
if (argc < 2)
{
printf("ZAPD.exe [mode (b/btex/bovl/bsf/bblb/bmdlintr/bamnintr/e)] ...\n");
return 1;
}
// Parse File Mode
string buildMode = argv[1];
ZFileMode fileMode = ZFileMode::Invalid;
if (buildMode == "b")
fileMode = ZFileMode::Build;
else if (buildMode == "btex")
fileMode = ZFileMode::BuildTexture;
else if (buildMode == "bovl")
fileMode = ZFileMode::BuildOverlay;
else if (buildMode == "bsf")
fileMode = ZFileMode::BuildSourceFile;
else if (buildMode == "bblb")
fileMode = ZFileMode::BuildBlob;
else if (buildMode == "bmdlintr")
fileMode = ZFileMode::BuildModelIntermediette;
else if (buildMode == "bamnintr")
fileMode = ZFileMode::BuildAnimationIntermediette;
else if (buildMode == "e")
fileMode = ZFileMode::Extract;
if (fileMode == ZFileMode::Invalid)
{
printf("Error: Invalid file mode '%s'\n", buildMode.c_str());
return 1;
}
// Parse other "commands"
for (int i = 2; i < argc; i++)
{
string arg = argv[i];
if (arg == "-o" || arg == "--outputpath") // Set output path
{
Globals::Instance->outputPath = argv[i + 1];
i++;
}
else if (arg == "-i" || arg == "--inputpath") // Set input path
{
Globals::Instance->inputPath = argv[i + 1];
i++;
}
else if (arg == "-b" || arg == "--baserompath") // Set baserom path
{
Globals::Instance->baseRomPath = argv[i + 1];
i++;
}
else if (arg == "-gsf") // Generate source file during extraction
{
Globals::Instance->genSourceFile = string(argv[i + 1]) == "1";
i++;
}
else if (arg == "-ifp") // Include file prefix in generated symbols
{
Globals::Instance->includeFilePrefix = string(argv[i + 1]) == "1";
i++;
}
else if (arg == "-tm") // Test Mode
{
Globals::Instance->testMode = string(argv[i + 1]) == "1";
i++;
}
else if (arg == "-dm") // Debug Messages
{
Globals::Instance->debugMessages = string(argv[i + 1]) == "1";
i++;
}
else if (arg == "-profile") // Profile
{
Globals::Instance->profile = string(argv[i + 1]) == "1";
i++;
}
else if (arg == "-uer") // Split resources into their individual components (enabled by default)
{
Globals::Instance->useExternalResources = string(argv[i + 1]) == "1";
i++;
}
else if (arg == "-tt") // Set texture type
{
Globals::Instance->texType = ZTexture::GetTextureTypeFromString(argv[i + 1]);
i++;
}
else if (arg == "-cfg") // Set cfg path
{
Globals::Instance->cfgPath = argv[i + 1];
i++;
}
else if (arg == "-sm") // Set symbol map path
{
Globals::Instance->GenSymbolMap(argv[i + 1]);
i++;
}
else if (arg == "-rconf") // Read Config File
{
Globals::Instance->ReadConfigFile(argv[i + 1]);
i++;
}
else if (arg == "-al") // Set actor list
{
i++;
}
else if (arg == "-ol") // Set object list
{
i++;
}
else if (arg == "-eh") // Enable Error Handler
{
#if !defined(_MSC_VER) && !defined(__CYGWIN__)
signal(SIGSEGV, ErrorHandler);
#endif
}
}
if (fileMode == ZFileMode::Build || fileMode == ZFileMode::Extract || fileMode == ZFileMode::BuildSourceFile)
{
Parse(Globals::Instance->inputPath, Globals::Instance->baseRomPath, Globals::Instance->outputPath, fileMode);
}
else if (fileMode == ZFileMode::BuildTexture)
{
TextureType texType = Globals::Instance->texType;
string pngFilePath = Globals::Instance->inputPath;
string outFilePath = Globals::Instance->outputPath;
BuildAssetTexture(pngFilePath, texType, outFilePath);
}
else if (fileMode == ZFileMode::BuildBlob)
{
string blobFilePath = Globals::Instance->inputPath;
string outFilePath = Globals::Instance->outputPath;
BuildAssetBlob(blobFilePath, outFilePath);
}
else if (fileMode == ZFileMode::BuildModelIntermediette)
{
BuildAssetModelIntermediette(Globals::Instance->inputPath, Globals::Instance->outputPath);
}
else if (fileMode == ZFileMode::BuildAnimationIntermediette)
{
BuildAssetAnimationIntermediette(Globals::Instance->inputPath, Globals::Instance->outputPath);
}
else if (fileMode == ZFileMode::BuildOverlay)
{
ZOverlay* overlay = ZOverlay::FromBuild(Path::GetDirectoryName(Globals::Instance->inputPath), Path::GetDirectoryName(Globals::Instance->cfgPath));
if (overlay)
File::WriteAllText(Globals::Instance->outputPath, overlay->GetSourceOutputCode(""));
}
return 0;
}
bool Parse(string xmlFilePath, string basePath, string outPath, ZFileMode fileMode)
{
XMLDocument doc;
XMLError eResult = doc.LoadFile(xmlFilePath.c_str());
if (eResult != tinyxml2::XML_SUCCESS)
return false;
XMLNode* root = doc.FirstChild();
if (root == nullptr)
return false;
//vector<ZFile*> files = vector<ZFile*>();
for (XMLElement* child = root->FirstChildElement(); child != NULL; child = child->NextSiblingElement())
{
if (string(child->Name()) == "File")
{
ZFile* file = new ZFile(fileMode, child, basePath, outPath, false);
Globals::Instance->files.push_back(file);
}
}
for (ZFile* file : Globals::Instance->files)
{
if (fileMode == ZFileMode::Build)
file->BuildResources();
else if (fileMode == ZFileMode::BuildSourceFile)
file->BuildSourceFile(outPath);
else
file->ExtractResources(outPath);
}
XMLElement* element = root->FirstChildElement("File");
if (element == nullptr)
return false;
return true;
}
void BuildAssetTexture(string pngFilePath, TextureType texType, string outPath)
{
vector<string> split = StringHelper::Split(outPath, "/");
string name = StringHelper::Split(split[split.size() - 1], ".")[0];
ZTexture* tex = ZTexture::FromPNG(pngFilePath, texType);
string cfgPath = StringHelper::Split(pngFilePath, ".")[0] + ".cfg";
if (File::Exists(cfgPath))
name = File::ReadAllText(cfgPath);
//string src = StringHelper::Sprintf("u64 %s[] = \n{\n", name.c_str()) + tex->GetSourceOutputCode(name) + "};\n";
string src = tex->GetSourceOutputCode(name);
File::WriteAllText(outPath, src);
delete tex;
}
void BuildAssetBlob(string blobFilePath, string outPath)
{
vector<string> split = StringHelper::Split(outPath, "/");
ZBlob* blob = ZBlob::FromFile(blobFilePath);
string name = StringHelper::Split(split[split.size() - 1], ".")[0];
//string src = StringHelper::Sprintf("u8 %s[] = \n{\n", name.c_str()) + blob->GetSourceOutputCode(name) + "};\n";
string src = blob->GetSourceOutputCode(name);
File::WriteAllText(outPath, src);
delete blob;
}
void BuildAssetModelIntermediette(string mdlPath, string outPath)
{
XMLDocument doc;
XMLError eResult = doc.LoadFile(mdlPath.c_str());
vector<string> split = StringHelper::Split(outPath, "/");
HLModelIntermediette* mdl = HLModelIntermediette::FromXML(doc.RootElement());
string output = mdl->OutputCode();
File::WriteAllText(outPath, output);
delete mdl;
}
void BuildAssetAnimationIntermediette(string animPath, string outPath)
{
vector<string> split = StringHelper::Split(outPath, "/");
ZFile* file = new ZFile("", split[split.size() - 2]);
HLAnimationIntermediette* anim = HLAnimationIntermediette::FromXML(animPath);
ZAnimation* zAnim = anim->ToZAnimation();
zAnim->SetName(Path::GetFileNameWithoutExtension(split[split.size() - 1]));
zAnim->parent = file;
zAnim->rotationIndicesSeg = 1;
zAnim->rotationValuesSeg = 2;
zAnim->GetSourceOutputCode(split[split.size() - 2]);
string output = "";
output += file->declarations[2]->text + "\n";
output += file->declarations[1]->text + "\n";
output += file->declarations[0]->text + "\n";
File::WriteAllText(outPath, output);
delete zAnim;
delete file;
}
|