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
|
#include "main.h"
#include "assets/aif.h"
#include "assets/animation.h"
#include "assets/exitlist.h"
#include "assets/frameobjlists.h"
#include "assets/gfx.h"
#include "assets/midi.h"
#include "assets/palette.h"
#include "assets/spriteframe.h"
#include "assets/tileset.h"
#include "offsets.h"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include <fmt/format.h>
using nlohmann::json;
// Does json array contain element? https://github.com/nlohmann/json/issues/399#issuecomment-844212174
#define CONTAINS(list, value) (std::find(list.begin(), list.end(), value) != list.end())
void usage() {
std::cout << "Usage: asset_processor [options] MODE VARIANT BUILD_PATH\n"
<< "\n"
<< "MODE Mode to execute\n"
<< "VARIANT Variant to build. One of USA, JP, EU, DEMO_USA, DEMO_JP\n"
<< "BUILD_PATH Path to the build folder\n"
<< "\n"
<< "Modes:\n"
<< "extract Extract binary data from baserom\n"
<< "convert Convert data to human readable format\n"
<< "build Build binary data from assets\n"
<< "\n"
<< "Options:\n"
<< "-v Print verbose output\n"
<< "-h Show this help\n"
<< "-f Force extraction of all assets (even if unmodified)\n";
std::exit(1);
}
bool gVerbose = false;
bool gForceExtraction = false;
Mode gMode;
std::string gVariant;
std::string gAssetsFolder;
std::string gBaseromPath;
static std::map<std::string, std::string> baseroms = { { "USA", "baserom.gba" },
{ "EU", "baserom_eu.gba" },
{ "JP", "baserom_jp.gba" },
{ "DEMO_USA", "baserom_demo.gba" },
{ "DEMO_JP", "baserom_demo_jp.gba" } };
int main(int argc, char** argv) {
// Parse options.
int argCount = argc - 1; // Skip program name
char** args = &argv[1];
while (argCount > 0 && args[0][0] == '-') {
if (strcmp(args[0], "-v") == 0) {
gVerbose = true;
argCount--;
args++;
} else if (strcmp(args[0], "-h") == 0) {
argCount--;
args++;
usage();
} else if (strcmp(args[0], "-f") == 0) {
gForceExtraction = true;
argCount--;
args++;
} else {
std::cerr << "Error: Unrecognized argument `" << args[0] << "`" << std::endl;
std::exit(1);
}
}
if (argCount != 3) {
std::cerr << "Error: Not enough arguments. expected: 3, actual: " << argCount << std::endl;
usage();
}
if (strcmp(args[0], "extract") == 0) {
gMode = EXTRACT;
} else if (strcmp(args[0], "convert") == 0) {
gMode = CONVERT;
} else if (strcmp(args[0], "build") == 0) {
gMode = BUILD;
} else {
std::cerr << "Error: Unsupported mode `" << args[0] << "`" << std::endl;
std::exit(1);
}
gVariant = args[1];
if (gVariant != "USA" && gVariant != "EU" && gVariant != "JP" && gVariant != "DEMO_USA" && gVariant != "DEMO_JP") {
std::cerr << "Error: Unsupported variant `" << gVariant << "`" << std::endl;
std::exit(1);
}
gAssetsFolder = args[2];
gBaseromPath = baseroms[gVariant];
if (!std::filesystem::exists(gBaseromPath)) {
std::cerr << "Error: You need to provide a " << gVariant << " ROM as " << gBaseromPath << std::endl;
std::exit(1);
}
// Read baserom.
std::ifstream file(gBaseromPath, std::ios::binary | std::ios::ate);
auto size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> baserom(static_cast<size_t>(size));
if (!file.read(baserom.data(), size)) {
fmt::print(stderr, "Could not read baserom {}\n", gBaseromPath);
std::exit(1);
}
file.close();
// Gather all json configs from assets folder.
std::vector<std::filesystem::path> configs;
std::string configFolder = "assets";
for (const auto& entry : std::filesystem::directory_iterator(configFolder)) {
const auto& path = entry.path();
if (path.extension() == ".json") {
configs.push_back(path);
}
}
std::sort(configs.begin(), configs.end());
for (const auto& config : configs) {
if (gVerbose) {
std::cout << "Parsing " << config << "..." << std::endl;
}
std::ifstream input(config);
json assets;
input >> assets;
std::filesystem::file_time_type configModified = std::filesystem::last_write_time(config);
std::unique_ptr<OffsetCalculator> offsetCalculator;
int currentOffset = 0;
for (const auto& asset : assets) {
if (asset.contains("offsets")) { // Offset definition
if (asset["offsets"].contains(gVariant)) {
currentOffset = asset["offsets"][gVariant];
}
} else if (asset.contains("calculateOffsets")) { // Start offset calculation
std::filesystem::path path = gAssetsFolder;
path = path / asset["calculateOffsets"];
int baseOffset = asset["start"].get<int>() + currentOffset;
offsetCalculator = std::make_unique<OffsetCalculator>(path, baseOffset);
} else if (asset.contains("path")) { // Asset definition
if (asset.contains("variants")) {
if (!CONTAINS(asset["variants"], gVariant)) {
// This asset is not used in the current variant
continue;
}
}
std::filesystem::path path = gAssetsFolder;
path = path / asset["path"];
switch (gMode) {
case EXTRACT: {
std::unique_ptr<BaseAsset> assetHandler = getAssetHandlerByType(path, asset, currentOffset);
if (shouldExtractAsset(path, configModified)) {
if (gVerbose) {
std::cout << "Extracting " << path << "..." << std::endl;
}
extractAsset(assetHandler, baserom);
}
if (offsetCalculator != nullptr) {
offsetCalculator->addAsset(assetHandler->getStart(), assetHandler->getSymbol());
}
break;
}
case CONVERT: {
std::unique_ptr<BaseAsset> assetHandler = getAssetHandlerByType(path, asset, currentOffset);
if (shouldConvertAsset(assetHandler)) {
if (gVerbose) {
std::cout << "Converting " << assetHandler->getAssetPath() << "..." << std::endl;
}
convertAsset(assetHandler, baserom);
}
break;
}
case BUILD: {
std::unique_ptr<BaseAsset> assetHandler = getAssetHandlerByType(path, asset, currentOffset);
if (shouldBuildAsset(assetHandler)) {
if (gVerbose) {
std::cout << "Building " << assetHandler->getAssetPath() << "..." << std::endl;
}
buildAsset(assetHandler);
}
break;
}
}
}
}
}
if (gVerbose) {
std::cout << "done" << std::endl;
}
}
std::unique_ptr<BaseAsset> getAssetHandlerByType(const std::filesystem::path& path, const json& asset,
const int& currentOffset) {
int start = 0;
if (asset.contains("start")) {
// Apply offset to the start of the USA variant
start = asset["start"].get<int>() + currentOffset;
} else if (asset.contains("starts")) {
// Use start for the current variant
start = asset["starts"][gVariant];
}
std::string type;
if (asset.contains("type")) {
type = asset["type"];
}
int size = 0;
if (asset.contains("size")) { // The asset has a size and want to be extracted first.
size = asset["size"]; // TODO can different sizes for the different variants ever occur?
}
std::unique_ptr<BaseAsset> assetHandler;
if (type == "tileset") {
assetHandler = std::make_unique<TilesetAsset>(path, start, size, asset);
} else if (type == "animation") {
assetHandler = std::make_unique<AnimationAsset>(path, start, size, asset);
} else if (type == "sprite_frame") {
assetHandler = std::make_unique<SpriteFrameAsset>(path, start, size, asset);
} else if (type == "exit_list") {
assetHandler = std::make_unique<ExitListAsset>(path, start, size, asset);
} else if (type == "frame_obj_lists") {
assetHandler = std::make_unique<FrameObjListsAsset>(path, start, size, asset);
} else if (type == "midi") {
assetHandler = std::make_unique<MidiAsset>(path, start, size, asset);
} else if (type == "aif") {
assetHandler = std::make_unique<AifAsset>(path, start, size, asset);
} else if (type == "gfx") {
assetHandler = std::make_unique<GfxAsset>(path, start, size, asset);
} else if (type == "palette") {
assetHandler = std::make_unique<PaletteAsset>(path, start, size, asset);
} else if (type == "map_gfx" || type == "map_layer1" || type == "map_layer2" || type == "metatiles_tile_types1" ||
type == "metatiles_tile_types2" || type == "metatiles_tileset1" || type == "metatiles_tileset2" ||
type == "map_mapping1" || type == "map_mapping2" || type == "tileset_mapping3" ||
type == "map_collision" || type == "unknown") {
// TODO implement conversions
assetHandler = std::make_unique<BaseAsset>(path, start, size, asset);
} else if (type.empty()) {
// Unknown binary asset
assetHandler = std::make_unique<BaseAsset>(path, start, size, asset);
} else {
fmt::print(stderr, "Error: Unimplemented asset type \"{}\"", type);
std::exit(1);
}
assetHandler->setup();
return assetHandler;
}
bool shouldExtractAsset(const std::filesystem::path& path, const std::filesystem::file_time_type& configModified) {
if (gForceExtraction) {
return true;
}
if (std::filesystem::is_regular_file(path)) {
std::filesystem::file_time_type fileModified = std::filesystem::last_write_time(path);
if (fileModified < configModified) {
// File was created before the config was modified, so it needs to be renewed.
return true;
} else {
return false;
}
} else {
// Target file does not yet exist -> extract
return true;
}
}
void extractAsset(std::unique_ptr<BaseAsset>& assetHandler, const std::vector<char>& baserom) {
// Create the parent directory
std::filesystem::path parentDir = assetHandler->getPath();
parentDir.remove_filename();
std::filesystem::create_directories(parentDir);
assetHandler->extractBinary(baserom);
}
bool shouldConvertAsset(const std::unique_ptr<BaseAsset>& assetHandler) {
(void)assetHandler;
return true; // TODO
}
void convertAsset(std::unique_ptr<BaseAsset>& assetHandler, const std::vector<char>& baserom) {
assetHandler->convertToHumanReadable(baserom);
}
bool shouldBuildAsset(const std::unique_ptr<BaseAsset>& assetHandler) {
(void)assetHandler;
return true; // TODO
}
void buildAsset(std::unique_ptr<BaseAsset>& assetHandler) {
assetHandler->buildToBinary();
}
|