Нет описания

project.cpp 47KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. #include "asm.h"
  2. #include "project.h"
  3. #include "tile.h"
  4. #include "tileset.h"
  5. #include "metatile.h"
  6. #include "event.h"
  7. #include <QDebug>
  8. #include <QDir>
  9. #include <QFile>
  10. #include <QTextStream>
  11. #include <QStandardItem>
  12. #include <QMessageBox>
  13. #include <QRegularExpression>
  14. Project::Project()
  15. {
  16. groupNames = new QStringList;
  17. map_groups = new QMap<QString, int>;
  18. groupedMapNames = new QList<QStringList*>;
  19. mapNames = new QStringList;
  20. map_cache = new QMap<QString, Map*>;
  21. mapConstantsToMapNames = new QMap<QString, QString>;
  22. mapNamesToMapConstants = new QMap<QString, QString>;
  23. mapAttributesTable = new QMap<int, QString>;
  24. tileset_cache = new QMap<QString, Tileset*>;
  25. }
  26. QString Project::getProjectTitle() {
  27. if (!root.isNull()) {
  28. return root.section('/', -1);
  29. } else {
  30. return QString();
  31. }
  32. }
  33. Map* Project::loadMap(QString map_name) {
  34. Map *map = new Map;
  35. map->setName(map_name);
  36. readMapHeader(map);
  37. readMapAttributes(map);
  38. getTilesets(map);
  39. loadBlockdata(map);
  40. loadMapBorder(map);
  41. readMapEvents(map);
  42. loadMapConnections(map);
  43. map->commit();
  44. map->history.save();
  45. map_cache->insert(map_name, map);
  46. return map;
  47. }
  48. void Project::loadMapConnections(Map *map) {
  49. map->connections.clear();
  50. if (!map->connections_label.isNull()) {
  51. QString path = root + QString("/data/maps/%1/connections.inc").arg(map->name);
  52. QString text = readTextFile(path);
  53. if (!text.isNull()) {
  54. QList<QStringList> *commands = parse(text);
  55. QStringList *list = getLabelValues(commands, map->connections_label);
  56. //// Avoid using this value. It ought to be generated instead.
  57. //int num_connections = list->value(0).toInt(nullptr, 0);
  58. QString connections_list_label = list->value(1);
  59. QList<QStringList> *connections = getLabelMacros(commands, connections_list_label);
  60. for (QStringList command : *connections) {
  61. QString macro = command.value(0);
  62. if (macro == "connection") {
  63. Connection *connection = new Connection;
  64. connection->direction = command.value(1);
  65. connection->offset = command.value(2);
  66. QString mapConstant = command.value(3);
  67. if (mapConstantsToMapNames->contains(mapConstant)) {
  68. connection->map_name = mapConstantsToMapNames->value(mapConstant);
  69. map->connections.append(connection);
  70. } else {
  71. qDebug() << QString("Failed to find connected map for map constant '%1'").arg(mapConstant);
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. QList<QStringList>* Project::getLabelMacros(QList<QStringList> *list, QString label) {
  79. bool in_label = false;
  80. QList<QStringList> *new_list = new QList<QStringList>;
  81. for (int i = 0; i < list->length(); i++) {
  82. QStringList params = list->value(i);
  83. QString macro = params.value(0);
  84. if (macro == ".label") {
  85. if (params.value(1) == label) {
  86. in_label = true;
  87. } else if (in_label) {
  88. // If nothing has been read yet, assume the label
  89. // we're looking for is in a stack of labels.
  90. if (new_list->length() > 0) {
  91. break;
  92. }
  93. }
  94. } else if (in_label) {
  95. new_list->append(params);
  96. }
  97. }
  98. return new_list;
  99. }
  100. // For if you don't care about filtering by macro,
  101. // and just want all values associated with some label.
  102. QStringList* Project::getLabelValues(QList<QStringList> *list, QString label) {
  103. list = getLabelMacros(list, label);
  104. QStringList *values = new QStringList;
  105. for (int i = 0; i < list->length(); i++) {
  106. QStringList params = list->value(i);
  107. QString macro = params.value(0);
  108. // Ignore .align
  109. if (macro == ".align") {
  110. continue;
  111. }
  112. for (int j = 1; j < params.length(); j++) {
  113. values->append(params.value(j));
  114. }
  115. }
  116. return values;
  117. }
  118. void Project::readMapHeader(Map* map) {
  119. QString label = map->name;
  120. Asm *parser = new Asm;
  121. QString header_text = readTextFile(root + "/data/maps/" + label + "/header.inc");
  122. if (header_text.isNull()) {
  123. return;
  124. }
  125. QStringList *header = getLabelValues(parser->parse(header_text), label);
  126. map->attributes_label = header->value(0);
  127. map->events_label = header->value(1);
  128. map->scripts_label = header->value(2);
  129. map->connections_label = header->value(3);
  130. map->song = header->value(4);
  131. map->index = header->value(5);
  132. map->location = header->value(6);
  133. map->visibility = header->value(7);
  134. map->weather = header->value(8);
  135. map->type = header->value(9);
  136. map->unknown = header->value(10);
  137. map->show_location = header->value(11);
  138. map->battle_scene = header->value(12);
  139. }
  140. void Project::saveMapHeader(Map *map) {
  141. QString label = map->name;
  142. QString header_path = root + "/data/maps/" + label + "/header.inc";
  143. QString text = "";
  144. text += QString("%1::\n").arg(label);
  145. text += QString("\t.4byte %1\n").arg(map->attributes_label);
  146. text += QString("\t.4byte %1\n").arg(map->events_label);
  147. text += QString("\t.4byte %1\n").arg(map->scripts_label);
  148. text += QString("\t.4byte %1\n").arg(map->connections_label);
  149. text += QString("\t.2byte %1\n").arg(map->song);
  150. text += QString("\t.2byte %1\n").arg(map->index);
  151. text += QString("\t.byte %1\n").arg(map->location);
  152. text += QString("\t.byte %1\n").arg(map->visibility);
  153. text += QString("\t.byte %1\n").arg(map->weather);
  154. text += QString("\t.byte %1\n").arg(map->type);
  155. text += QString("\t.2byte %1\n").arg(map->unknown);
  156. text += QString("\t.byte %1\n").arg(map->show_location);
  157. text += QString("\t.byte %1\n").arg(map->battle_scene);
  158. saveTextFile(header_path, text);
  159. }
  160. void Project::readMapAttributesTable() {
  161. int curMapIndex = 1;
  162. QString attributesText = readTextFile(getMapAttributesTableFilepath());
  163. QList<QStringList>* values = parse(attributesText);
  164. bool inAttributePointers = false;
  165. for (int i = 0; i < values->length(); i++) {
  166. QStringList params = values->value(i);
  167. QString macro = params.value(0);
  168. if (macro == ".label") {
  169. if (inAttributePointers) {
  170. break;
  171. }
  172. if (params.value(1) == "gMapAttributes") {
  173. inAttributePointers = true;
  174. }
  175. } else if (macro == ".4byte" && inAttributePointers) {
  176. QString mapName = params.value(1);
  177. if (!mapName.contains("UnknownMapAttributes")) {
  178. // Strip off "_MapAttributes" from the label if it's a real map label.
  179. mapName = mapName.remove(mapName.length() - 14, 14);
  180. }
  181. mapAttributesTable->insert(curMapIndex, mapName);
  182. curMapIndex++;
  183. }
  184. }
  185. }
  186. void Project::saveMapAttributesTable() {
  187. QString text = "";
  188. text += QString("\t.align 2\n");
  189. text += QString("gMapAttributes::\n");
  190. for (int i = 0; i < mapAttributesTable->count(); i++) {
  191. int mapIndex = i + 1;
  192. QString mapName = mapAttributesTable->value(mapIndex);
  193. if (!mapName.contains("UnknownMapAttributes")) {
  194. text += QString("\t.4byte %1_MapAttributes\n").arg(mapName);
  195. } else {
  196. text += QString("\t.4byte %1\n").arg(mapName);
  197. }
  198. }
  199. saveTextFile(getMapAttributesTableFilepath(), text);
  200. }
  201. QString Project::getMapAttributesTableFilepath() {
  202. return QString("%1/data/maps/attributes_table.inc").arg(root);
  203. }
  204. void Project::readMapAttributes(Map* map) {
  205. Asm *parser = new Asm;
  206. QString assets_text = readTextFile(root + "/data/maps/_assets.inc");
  207. if (assets_text.isNull()) {
  208. return;
  209. }
  210. QStringList *attributes = getLabelValues(parser->parse(assets_text), map->attributes_label);
  211. map->width = attributes->value(0);
  212. map->height = attributes->value(1);
  213. map->border_label = attributes->value(2);
  214. map->blockdata_label = attributes->value(3);
  215. map->tileset_primary_label = attributes->value(4);
  216. map->tileset_secondary_label = attributes->value(5);
  217. }
  218. void Project::getTilesets(Map* map) {
  219. map->tileset_primary = getTileset(map->tileset_primary_label);
  220. map->tileset_secondary = getTileset(map->tileset_secondary_label);
  221. }
  222. Tileset* Project::loadTileset(QString label) {
  223. Asm *parser = new Asm;
  224. QString headers_text = readTextFile(root + "/data/tilesets/headers.inc");
  225. QStringList *values = getLabelValues(parser->parse(headers_text), label);
  226. Tileset *tileset = new Tileset;
  227. tileset->name = label;
  228. tileset->is_compressed = values->value(0);
  229. tileset->is_secondary = values->value(1);
  230. tileset->padding = values->value(2);
  231. tileset->tiles_label = values->value(3);
  232. tileset->palettes_label = values->value(4);
  233. tileset->metatiles_label = values->value(5);
  234. tileset->metatile_attrs_label = values->value(6);
  235. tileset->callback_label = values->value(7);
  236. loadTilesetAssets(tileset);
  237. tileset_cache->insert(label, tileset);
  238. return tileset;
  239. }
  240. QString Project::getBlockdataPath(Map* map) {
  241. QString text = readTextFile(root + "/data/maps/_assets.inc");
  242. QStringList *values = getLabelValues(parse(text), map->blockdata_label);
  243. QString path;
  244. if (!values->isEmpty()) {
  245. path = root + "/" + values->value(0).section('"', 1, 1);
  246. } else {
  247. path = root + "/data/maps/" + map->name + "/map.bin";
  248. }
  249. return path;
  250. }
  251. QString Project::getMapBorderPath(Map *map) {
  252. QString text = readTextFile(root + "/data/maps/_assets.inc");
  253. QStringList *values = getLabelValues(parse(text), map->border_label);
  254. QString path;
  255. if (!values->isEmpty()) {
  256. path = root + "/" + values->value(0).section('"', 1, 1);
  257. } else {
  258. path = root + "/data/maps/" + map->name + "/border.bin";
  259. }
  260. return path;
  261. }
  262. void Project::loadBlockdata(Map* map) {
  263. QString path = getBlockdataPath(map);
  264. map->blockdata = readBlockdata(path);
  265. }
  266. void Project::loadMapBorder(Map *map) {
  267. QString path = getMapBorderPath(map);
  268. map->border = readBlockdata(path);
  269. }
  270. void Project::saveBlockdata(Map* map) {
  271. QString path = getBlockdataPath(map);
  272. writeBlockdata(path, map->blockdata);
  273. map->history.save();
  274. }
  275. void Project::writeBlockdata(QString path, Blockdata *blockdata) {
  276. QFile file(path);
  277. if (file.open(QIODevice::WriteOnly)) {
  278. QByteArray data = blockdata->serialize();
  279. file.write(data);
  280. }
  281. }
  282. void Project::saveAllMaps() {
  283. QList<QString> keys = map_cache->keys();
  284. for (int i = 0; i < keys.length(); i++) {
  285. QString key = keys.value(i);
  286. Map* map = map_cache->value(key);
  287. saveMap(map);
  288. }
  289. }
  290. void Project::saveMap(Map *map) {
  291. saveBlockdata(map);
  292. saveMapHeader(map);
  293. saveMapEvents(map);
  294. }
  295. void Project::saveAllDataStructures() {
  296. saveMapAttributesTable();
  297. }
  298. void Project::loadTilesetAssets(Tileset* tileset) {
  299. Asm* parser = new Asm;
  300. QString category = (tileset->is_secondary == "TRUE") ? "secondary" : "primary";
  301. if (tileset->name.isNull()) {
  302. return;
  303. }
  304. QString dir_path = root + "/data/tilesets/" + category + "/" + tileset->name.replace("gTileset_", "").toLower();
  305. QString graphics_text = readTextFile(root + "/data/tilesets/graphics.inc");
  306. QList<QStringList> *graphics = parser->parse(graphics_text);
  307. QStringList *tiles_values = getLabelValues(graphics, tileset->tiles_label);
  308. QStringList *palettes_values = getLabelValues(graphics, tileset->palettes_label);
  309. QString tiles_path;
  310. if (!tiles_values->isEmpty()) {
  311. tiles_path = root + "/" + tiles_values->value(0).section('"', 1, 1);
  312. } else {
  313. tiles_path = dir_path + "/tiles.4bpp";
  314. if (tileset->is_compressed == "TRUE") {
  315. tiles_path += ".lz";
  316. }
  317. }
  318. QStringList *palette_paths = new QStringList;
  319. if (!palettes_values->isEmpty()) {
  320. for (int i = 0; i < palettes_values->length(); i++) {
  321. QString value = palettes_values->value(i);
  322. palette_paths->append(root + "/" + value.section('"', 1, 1));
  323. }
  324. } else {
  325. QString palettes_dir_path = dir_path + "/palettes";
  326. for (int i = 0; i < 16; i++) {
  327. palette_paths->append(palettes_dir_path + "/" + QString("%1").arg(i, 2, 10, QLatin1Char('0')) + ".gbapal");
  328. }
  329. }
  330. QString metatiles_path;
  331. QString metatile_attrs_path;
  332. QString metatiles_text = readTextFile(root + "/data/tilesets/metatiles.inc");
  333. QList<QStringList> *metatiles_macros = parser->parse(metatiles_text);
  334. QStringList *metatiles_values = getLabelValues(metatiles_macros, tileset->metatiles_label);
  335. if (!metatiles_values->isEmpty()) {
  336. metatiles_path = root + "/" + metatiles_values->value(0).section('"', 1, 1);
  337. } else {
  338. metatiles_path = dir_path + "/metatiles.bin";
  339. }
  340. QStringList *metatile_attrs_values = getLabelValues(metatiles_macros, tileset->metatile_attrs_label);
  341. if (!metatile_attrs_values->isEmpty()) {
  342. metatile_attrs_path = root + "/" + metatile_attrs_values->value(0).section('"', 1, 1);
  343. } else {
  344. metatile_attrs_path = dir_path + "/metatile_attributes.bin";
  345. }
  346. // tiles
  347. tiles_path = fixGraphicPath(tiles_path);
  348. QImage *image = new QImage(tiles_path);
  349. //image->setColor(0, qRgb(0xff, 0, 0)); // debug
  350. QList<QImage> *tiles = new QList<QImage>;
  351. int w = 8;
  352. int h = 8;
  353. for (int y = 0; y < image->height(); y += h)
  354. for (int x = 0; x < image->width(); x += w) {
  355. QImage tile = image->copy(x, y, w, h);
  356. tiles->append(tile);
  357. }
  358. tileset->tiles = tiles;
  359. // metatiles
  360. //qDebug() << metatiles_path;
  361. QFile metatiles_file(metatiles_path);
  362. if (metatiles_file.open(QIODevice::ReadOnly)) {
  363. QByteArray data = metatiles_file.readAll();
  364. int num_metatiles = data.length() / 16;
  365. int num_layers = 2;
  366. QList<Metatile*> *metatiles = new QList<Metatile*>;
  367. for (int i = 0; i < num_metatiles; i++) {
  368. Metatile *metatile = new Metatile;
  369. int index = i * (2 * 4 * num_layers);
  370. for (int j = 0; j < 4 * num_layers; j++) {
  371. uint16_t word = data[index++] & 0xff;
  372. word += (data[index++] & 0xff) << 8;
  373. Tile tile;
  374. tile.tile = word & 0x3ff;
  375. tile.xflip = (word >> 10) & 1;
  376. tile.yflip = (word >> 11) & 1;
  377. tile.palette = (word >> 12) & 0xf;
  378. metatile->tiles->append(tile);
  379. }
  380. metatiles->append(metatile);
  381. }
  382. tileset->metatiles = metatiles;
  383. } else {
  384. tileset->metatiles = new QList<Metatile*>;
  385. qDebug() << QString("Could not open '%1'").arg(metatiles_path);
  386. }
  387. QFile attrs_file(metatile_attrs_path);
  388. //qDebug() << metatile_attrs_path;
  389. if (attrs_file.open(QIODevice::ReadOnly)) {
  390. QByteArray data = attrs_file.readAll();
  391. int num_metatiles = data.length() / 2;
  392. for (int i = 0; i < num_metatiles; i++) {
  393. uint16_t word = data[i*2] & 0xff;
  394. word += (data[i*2 + 1] & 0xff) << 8;
  395. tileset->metatiles->value(i)->attr = word;
  396. }
  397. } else {
  398. qDebug() << QString("Could not open '%1'").arg(metatile_attrs_path);
  399. }
  400. // palettes
  401. QList<QList<QRgb>> *palettes = new QList<QList<QRgb>>;
  402. for (int i = 0; i < palette_paths->length(); i++) {
  403. QString path = palette_paths->value(i);
  404. // the palettes are not compressed. this should never happen. it's only a precaution.
  405. path = path.replace(QRegExp("\\.lz$"), "");
  406. // TODO default to .pal (JASC-PAL)
  407. // just use .gbapal for now
  408. QFile file(path);
  409. QList<QRgb> palette;
  410. if (file.open(QIODevice::ReadOnly)) {
  411. QByteArray data = file.readAll();
  412. for (int j = 0; j < 16; j++) {
  413. uint16_t word = data[j*2] & 0xff;
  414. word += (data[j*2 + 1] & 0xff) << 8;
  415. int red = word & 0x1f;
  416. int green = (word >> 5) & 0x1f;
  417. int blue = (word >> 10) & 0x1f;
  418. QRgb color = qRgb(red * 8, green * 8, blue * 8);
  419. palette.prepend(color);
  420. }
  421. } else {
  422. for (int j = 0; j < 16; j++) {
  423. palette.append(qRgb(j * 16, j * 16, j * 16));
  424. }
  425. qDebug() << QString("Could not open '%1'").arg(path);
  426. }
  427. //qDebug() << path;
  428. palettes->append(palette);
  429. }
  430. tileset->palettes = palettes;
  431. }
  432. Blockdata* Project::readBlockdata(QString path) {
  433. Blockdata *blockdata = new Blockdata;
  434. //qDebug() << path;
  435. QFile file(path);
  436. if (file.open(QIODevice::ReadOnly)) {
  437. QByteArray data = file.readAll();
  438. for (int i = 0; (i + 1) < data.length(); i += 2) {
  439. uint16_t word = (data[i] & 0xff) + ((data[i + 1] & 0xff) << 8);
  440. blockdata->addBlock(word);
  441. }
  442. }
  443. return blockdata;
  444. }
  445. Map* Project::getMap(QString map_name) {
  446. if (map_cache->contains(map_name)) {
  447. return map_cache->value(map_name);
  448. } else {
  449. Map *map = loadMap(map_name);
  450. return map;
  451. }
  452. }
  453. Tileset* Project::getTileset(QString label) {
  454. if (tileset_cache->contains(label)) {
  455. return tileset_cache->value(label);
  456. } else {
  457. Tileset *tileset = loadTileset(label);
  458. return tileset;
  459. }
  460. }
  461. QString Project::readTextFile(QString path) {
  462. QFile file(path);
  463. if (!file.open(QIODevice::ReadOnly)) {
  464. //QMessageBox::information(0, "Error", QString("Could not open '%1': ").arg(path) + file.errorString());
  465. qDebug() << QString("Could not open '%1': ").arg(path) + file.errorString();
  466. return QString();
  467. }
  468. QTextStream in(&file);
  469. QString text = "";
  470. while (!in.atEnd()) {
  471. text += in.readLine() + "\n";
  472. }
  473. return text;
  474. }
  475. void Project::saveTextFile(QString path, QString text) {
  476. QFile file(path);
  477. if (file.open(QIODevice::WriteOnly)) {
  478. file.write(text.toUtf8());
  479. } else {
  480. qDebug() << QString("Could not open '%1' for writing: ").arg(path) + file.errorString();
  481. }
  482. }
  483. void Project::readMapGroups() {
  484. QString text = readTextFile(root + "/data/maps/_groups.inc");
  485. if (text.isNull()) {
  486. return;
  487. }
  488. Asm *parser = new Asm;
  489. QList<QStringList> *commands = parser->parse(text);
  490. bool in_group_pointers = false;
  491. QStringList *groups = new QStringList;
  492. for (int i = 0; i < commands->length(); i++) {
  493. QStringList params = commands->value(i);
  494. QString macro = params.value(0);
  495. if (macro == ".label") {
  496. if (in_group_pointers) {
  497. break;
  498. }
  499. if (params.value(1) == "gMapGroups") {
  500. in_group_pointers = true;
  501. }
  502. } else if (macro == ".4byte") {
  503. if (in_group_pointers) {
  504. for (int j = 1; j < params.length(); j++) {
  505. groups->append(params.value(j));
  506. }
  507. }
  508. }
  509. }
  510. QList<QStringList*> *groupedMaps = new QList<QStringList*>;
  511. for (int i = 0; i < groups->length(); i++) {
  512. QStringList *list = new QStringList;
  513. groupedMaps->append(list);
  514. }
  515. QStringList *maps = new QStringList;
  516. int group = -1;
  517. for (int i = 0; i < commands->length(); i++) {
  518. QStringList params = commands->value(i);
  519. QString macro = params.value(0);
  520. if (macro == ".label") {
  521. group = groups->indexOf(params.value(1));
  522. } else if (macro == ".4byte") {
  523. if (group != -1) {
  524. for (int j = 1; j < params.length(); j++) {
  525. QString mapName = params.value(j);
  526. QStringList *list = groupedMaps->value(group);
  527. list->append(mapName);
  528. maps->append(mapName);
  529. map_groups->insert(mapName, group);
  530. // Build the mapping and reverse mapping between map constants and map names.
  531. QString mapConstant = Map::mapConstantFromName(mapName);
  532. mapConstantsToMapNames->insert(mapConstant, mapName);
  533. mapNamesToMapConstants->insert(mapName, mapConstant);
  534. }
  535. }
  536. }
  537. }
  538. groupNames = groups;
  539. groupedMapNames = groupedMaps;
  540. mapNames = maps;
  541. }
  542. void Project::addNewMapToGroup(QString mapName, int groupNum) {
  543. // Write new map to project files.
  544. // 1. Create directory data/maps/<map_name>/
  545. // 2. Create file data/maps/<map_name>/border.bin
  546. // 3. Create file data/maps/<map_name>/header.inc
  547. // 4. Create file data/maps/<map_name>/map.bin
  548. // 5. Create file data/maps/events/<map_name>.inc
  549. // 6. Create file data/scripts/maps/<map_name>.inc
  550. // 7. Create file data/text/maps/<map_name>.inc
  551. // 8. Modify data/event_scripts.s:
  552. // .include "data/scripts/maps/<map_name>.inc"
  553. // .include "data/text/maps/<map_name>.inc"
  554. // 9. Modify data/map_events.s:
  555. // .include "data/maps/events/<map_name>.inc"
  556. // 10. Modify data/maps/_assets.inc
  557. // 11. Modify data/maps/_groups.inc
  558. // 12. Modify data/maps/attributes_table.inc
  559. // 13. Modify data/maps/headers.inc
  560. int mapIndex = mapAttributesTable->count() + 1;
  561. mapAttributesTable->insert(mapIndex, mapName);
  562. QString dataDir = QString("%1/data/").arg(root);
  563. QString dataMapsDir = QString("%1maps/").arg(dataDir);
  564. QString newMapDataDir = QString("%1%2/").arg(dataMapsDir).arg(mapName);
  565. // 1. Create directory data/maps/<map_name>/
  566. if (!QDir::root().mkdir(newMapDataDir)) {
  567. qDebug() << "Error: failed to create directory for new map. " << newMapDataDir;
  568. return;
  569. }
  570. // 2. Create file data/maps/<map_name>/border.bin
  571. QFile borderFile(newMapDataDir + "border.bin");
  572. borderFile.open(QIODevice::WriteOnly);
  573. QDataStream borderStream(&borderFile);
  574. borderStream.setByteOrder(QDataStream::LittleEndian);
  575. borderStream << qint16(0x01D4) << qint16(0x01D5) << qint16(0x01DC) << qint16(0x01DD);
  576. borderFile.close();
  577. // 3. Create file data/maps/<map_name>/header.inc
  578. QFile headerFile(newMapDataDir + "header.inc");
  579. headerFile.open(QIODevice::WriteOnly);
  580. QTextStream headerStream(&headerFile);
  581. headerStream << mapName << "::" << endl
  582. << "\t.4byte " << mapName << "_MapAttributes" << endl
  583. << "\t.4byte " << mapName << "_MapEvents" << endl
  584. << "\t.4byte " << mapName << "_MapScripts" << endl
  585. << "\t.4byte 0x0" << endl
  586. << "\t.2byte BGM_DAN02" << endl
  587. << "\t.2byte " << mapIndex << endl
  588. << "\t.byte 0" << endl
  589. << "\t.byte 0" << endl
  590. << "\t.byte 11" << endl
  591. << "\t.byte 4" << endl
  592. << "\t.2byte 0" << endl
  593. << "\t.byte 1" << endl
  594. << "\t.byte 0" << endl;
  595. headerFile.close();
  596. // 4. Create file data/maps/<map_name>/map.bin
  597. QFile mapFile(newMapDataDir + "map.bin");
  598. mapFile.open(QIODevice::WriteOnly);
  599. QDataStream mapStream(&mapFile);
  600. mapStream.setByteOrder(QDataStream::LittleEndian);
  601. for (int i = 0; i < 20 * 20; i++) {
  602. mapStream << qint16(0x3001);
  603. }
  604. mapFile.close();
  605. // 5. Create file data/maps/events/<map_name>.inc
  606. QFile eventsFile(dataMapsDir + "events/" + mapName + ".inc");
  607. eventsFile.open(QIODevice::WriteOnly);
  608. QTextStream eventsStream(&eventsFile);
  609. eventsStream << mapName << "_MapEvents::" << endl
  610. << "\tmap_events 0x0, 0x0, 0x0, 0x0" << endl;
  611. eventsFile.close();
  612. // 6. Create file data/scripts/maps/<map_name>.inc
  613. QFile scriptsFile(dataDir + "scripts/maps/" + mapName + ".inc");
  614. scriptsFile.open(QIODevice::WriteOnly);
  615. QTextStream scriptsStream(&scriptsFile);
  616. scriptsStream << mapName << "_MapScripts::" << endl
  617. << "\t.byte 0" << endl;
  618. scriptsFile.close();
  619. // 7. Create file data/text/maps/<map_name>.inc
  620. QFile textFile(dataDir + "text/maps/" + mapName + ".inc");
  621. textFile.open(QIODevice::WriteOnly);
  622. QTextStream textStream(&textFile);
  623. textStream << endl;
  624. textFile.close();
  625. // 8. Modify data/event_scripts.s:
  626. QFile eventScriptsFile(dataDir + "event_scripts.s");
  627. eventScriptsFile.open(QIODevice::Append);
  628. QTextStream eventScriptsStream(&eventScriptsFile);
  629. eventScriptsStream << endl
  630. << "\t.include \"data/scripts/maps/" << mapName << ".inc\"" << endl
  631. << "\t.include \"data/text/maps/" << mapName << ".inc\"" << endl;
  632. eventScriptsFile.close();
  633. // 9. Modify data/map_events.s:
  634. QFile mapEventsFile(dataDir + "map_events.s");
  635. mapEventsFile.open(QIODevice::Append);
  636. QTextStream mapEventsStream(&mapEventsFile);
  637. mapEventsStream << endl
  638. << "\t.include \"data/maps/events/" << mapName << ".inc\"" << endl;
  639. mapEventsFile.close();
  640. // 10. Modify data/maps/_assets.inc
  641. QFile assetsFile(dataMapsDir + "_assets.inc");
  642. assetsFile.open(QIODevice::Append);
  643. QTextStream assetsStream(&assetsFile);
  644. assetsStream << endl
  645. << mapName << "_MapBorder::" << endl
  646. << "\t.incbin \"data/maps/" << mapName << "/border.bin\"" << endl
  647. << endl
  648. << mapName << "_MapBlockdata::" << endl
  649. << "\t.incbin \"data/maps/" << mapName << "/map.bin\"" << endl
  650. << endl
  651. << "\t.align 2" << endl
  652. << mapName << "_MapAttributes::" << endl
  653. << "\t.4byte 0x14" << endl
  654. << "\t.4byte 0x14" << endl
  655. << "\t.4byte " << mapName << "_MapBorder" << endl
  656. << "\t.4byte " << mapName << "_MapBlockdata" << endl
  657. << "\t.4byte gTileset_General" << endl
  658. << "\t.4byte gTileset_Pacifidlog" << endl
  659. << endl;
  660. assetsFile.close();
  661. // 11. Modify data/maps/_groups.inc
  662. // TODO:
  663. // 12. Modify data/maps/attributes_table.inc
  664. QFile attributesFile(dataMapsDir + "attributes_table.inc");
  665. attributesFile.open(QIODevice::Append);
  666. QTextStream attributesStream(&attributesFile);
  667. attributesStream << endl
  668. << "\t.4byte " << mapName << "_MapAttributes" << endl;
  669. attributesFile.close();
  670. // 13. Modify data/maps/headers.inc
  671. QFile headersFile(dataMapsDir + "headers.inc");
  672. headersFile.open(QIODevice::Append);
  673. QTextStream headersStream(&headersFile);
  674. headersStream << endl
  675. << "\t.include \"data/maps/" << mapName << "/header.inc\"" << endl;
  676. headersFile.close();
  677. mapNames->append(mapName);
  678. map_groups->insert(mapName, groupNum);
  679. groupedMapNames->value(groupNum)->append(mapName);
  680. }
  681. QString Project::getNewMapName() {
  682. // Ensure default name doesn't already exist.
  683. int i = 0;
  684. QString newMapName;
  685. do {
  686. newMapName = QString("NewMap%1").arg(++i);
  687. } while (mapNames->contains(newMapName));
  688. return newMapName;
  689. }
  690. QList<QStringList>* Project::parse(QString text) {
  691. Asm *parser = new Asm;
  692. return parser->parse(text);
  693. }
  694. QStringList Project::getLocations() {
  695. // TODO
  696. QStringList names;
  697. for (int i = 0; i < 88; i++) {
  698. names.append(QString("%1").arg(i));
  699. }
  700. return names;
  701. }
  702. QStringList Project::getVisibilities() {
  703. // TODO
  704. QStringList names;
  705. for (int i = 0; i < 16; i++) {
  706. names.append(QString("%1").arg(i));
  707. }
  708. return names;
  709. }
  710. QStringList Project::getWeathers() {
  711. // TODO
  712. QStringList names;
  713. for (int i = 0; i < 16; i++) {
  714. names.append(QString("%1").arg(i));
  715. }
  716. return names;
  717. }
  718. QStringList Project::getMapTypes() {
  719. // TODO
  720. QStringList names;
  721. for (int i = 0; i < 16; i++) {
  722. names.append(QString("%1").arg(i));
  723. }
  724. return names;
  725. }
  726. QStringList Project::getBattleScenes() {
  727. // TODO
  728. QStringList names;
  729. for (int i = 0; i < 16; i++) {
  730. names.append(QString("%1").arg(i));
  731. }
  732. return names;
  733. }
  734. QStringList Project::getSongNames() {
  735. QStringList names;
  736. QString text = readTextFile(root + "/include/constants/songs.h");
  737. if (!text.isNull()) {
  738. QStringList songDefinePrefixes;
  739. songDefinePrefixes << "SE_" << "BGM_";
  740. QMap<QString, int> songDefines = readCDefines(text, songDefinePrefixes);
  741. names = songDefines.keys();
  742. }
  743. return names;
  744. }
  745. QString Project::getSongName(int songNumber) {
  746. QStringList names;
  747. QString text = readTextFile(root + "/include/constants/songs.h");
  748. if (!text.isNull()) {
  749. QStringList songDefinePrefixes;
  750. songDefinePrefixes << "SE_" << "BGM_";
  751. QMap<QString, int> songDefines = readCDefines(text, songDefinePrefixes);
  752. // Loop through song defines, and fine the one with the matching song number.
  753. QMap<QString, int>::iterator iter = songDefines.begin();
  754. while (iter != songDefines.end()) {
  755. if (iter.value() == songNumber) {
  756. return iter.key();
  757. }
  758. iter++;
  759. }
  760. }
  761. return "";
  762. }
  763. QMap<QString, int> Project::getMapObjGfxConstants() {
  764. QMap<QString, int> constants;
  765. QString text = readTextFile(root + "/include/constants/map_objects.h");
  766. if (!text.isNull()) {
  767. QStringList mapObjGfxPrefixes;
  768. mapObjGfxPrefixes << "MAP_OBJ_GFX_";
  769. constants = readCDefines(text, mapObjGfxPrefixes);
  770. }
  771. return constants;
  772. }
  773. QString Project::fixGraphicPath(QString path) {
  774. path = path.replace(QRegExp("\\.lz$"), "");
  775. path = path.replace(QRegExp("\\.[1248]bpp$"), ".png");
  776. return path;
  777. }
  778. void Project::loadObjectPixmaps(QList<Event*> objects) {
  779. bool needs_update = false;
  780. for (Event *object : objects) {
  781. if (object->pixmap.isNull()) {
  782. needs_update = true;
  783. break;
  784. }
  785. }
  786. if (!needs_update) {
  787. return;
  788. }
  789. QMap<QString, int> constants = getMapObjGfxConstants();
  790. QString pointers_text = readTextFile(root + "/src/data/field_map_obj/map_object_graphics_info_pointers.h");
  791. QString info_text = readTextFile(root + "/src/data/field_map_obj/map_object_graphics_info.h");
  792. QString pic_text = readTextFile(root + "/src/data/field_map_obj/map_object_pic_tables.h");
  793. QString assets_text = readTextFile(root + "/src/field/field_map_obj.c");
  794. QStringList pointers = readCArray(pointers_text, "gMapObjectGraphicsInfoPointers");
  795. for (Event *object : objects) {
  796. if (!object->pixmap.isNull()) {
  797. continue;
  798. }
  799. QString event_type = object->get("event_type");
  800. if (event_type == "object") {
  801. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(0, 0, 16, 16);
  802. } else if (event_type == "warp") {
  803. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(16, 0, 16, 16);
  804. } else if (event_type == "trap" || event_type == "trap_weather") {
  805. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(32, 0, 16, 16);
  806. } else if (event_type == "sign" || event_type == "event_hidden_item" || event_type == "event_secret_base") {
  807. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(48, 0, 16, 16);
  808. }
  809. if (event_type == "object") {
  810. int sprite_id = constants.value(object->get("sprite"));
  811. QString info_label = pointers.value(sprite_id).replace("&", "");
  812. QString pic_label = readCArray(info_text, info_label).value(14);
  813. QString gfx_label = readCArray(pic_text, pic_label).value(0);
  814. gfx_label = gfx_label.section(QRegExp("[\\(\\)]"), 1, 1);
  815. QString path = readCIncbin(assets_text, gfx_label);
  816. if (!path.isNull()) {
  817. path = fixGraphicPath(path);
  818. QPixmap pixmap(root + "/" + path);
  819. if (!pixmap.isNull()) {
  820. object->pixmap = pixmap;
  821. }
  822. }
  823. }
  824. }
  825. }
  826. void Project::saveMapEvents(Map *map) {
  827. QString path = root + QString("/data/maps/events/%1.inc").arg(map->name);
  828. QString text = "";
  829. if (map->events["object"].length() > 0) {
  830. text += QString("%1::\n").arg(map->object_events_label);
  831. for (int i = 0; i < map->events["object"].length(); i++) {
  832. Event *object_event = map->events["object"].value(i);
  833. int radius_x = object_event->getInt("radius_x");
  834. int radius_y = object_event->getInt("radius_y");
  835. QString radius = QString("%1").arg((radius_x & 0xf) + ((radius_y & 0xf) << 4));
  836. uint16_t x = object_event->getInt("x");
  837. uint16_t y = object_event->getInt("y");
  838. text += QString("\tobject_event %1").arg(i + 1);
  839. text += QString(", %1").arg(object_event->get("sprite"));
  840. text += QString(", %1").arg(object_event->get("replacement"));
  841. text += QString(", %1").arg(x & 0xff);
  842. text += QString(", %1").arg((x >> 8) & 0xff);
  843. text += QString(", %1").arg(y & 0xff);
  844. text += QString(", %1").arg((y >> 8) & 0xff);
  845. text += QString(", %1").arg(object_event->get("elevation"));
  846. text += QString(", %1").arg(object_event->get("behavior"));
  847. text += QString(", %1").arg(radius);
  848. text += QString(", 0");
  849. text += QString(", %1").arg(object_event->get("property"));
  850. text += QString(", 0");
  851. text += QString(", %1").arg(object_event->get("sight_radius"));
  852. text += QString(", 0");
  853. text += QString(", %1").arg(object_event->get("script_label"));
  854. text += QString(", %1").arg(object_event->get("event_flag"));
  855. text += QString(", 0");
  856. text += QString(", 0");
  857. text += "\n";
  858. }
  859. text += "\n";
  860. }
  861. if (map->events["warp"].length() > 0) {
  862. text += QString("%1::\n").arg(map->warps_label);
  863. for (Event *warp : map->events["warp"]) {
  864. text += QString("\twarp_def %1").arg(warp->get("x"));
  865. text += QString(", %1").arg(warp->get("y"));
  866. text += QString(", %1").arg(warp->get("elevation"));
  867. text += QString(", %1").arg(warp->get("destination_warp"));
  868. text += QString(", %1").arg(mapNamesToMapConstants->value(warp->get("destination_map_name")));
  869. text += "\n";
  870. }
  871. text += "\n";
  872. }
  873. if (map->events["trap"].length() + map->events["trap_weather"].length() > 0) {
  874. text += QString("%1::\n").arg(map->coord_events_label);
  875. for (Event *coords : map->events["trap"]) {
  876. text += QString("\tcoord_event %1").arg(coords->get("x"));
  877. text += QString(", %1").arg(coords->get("y"));
  878. text += QString(", %1").arg(coords->get("elevation"));
  879. text += QString(", 0");
  880. text += QString(", %1").arg(coords->get("coord_unknown1"));
  881. text += QString(", %1").arg(coords->get("coord_unknown2"));
  882. text += QString(", 0");
  883. text += QString(", %1").arg(coords->get("script_label"));
  884. text += "\n";
  885. }
  886. for (Event *coords : map->events["trap_weather"]) {
  887. text += QString("\tcoord_weather_event %1").arg(coords->get("x"));
  888. text += QString(", %1").arg(coords->get("y"));
  889. text += QString(", %1").arg(coords->get("elevation"));
  890. text += QString(", %1").arg(coords->get("weather"));
  891. text += "\n";
  892. }
  893. text += "\n";
  894. }
  895. if (map->events["sign"].length() +
  896. map->events["event_hidden_item"].length() +
  897. map->events["event_secret_base"].length() > 0)
  898. {
  899. text += QString("%1::\n").arg(map->bg_events_label);
  900. for (Event *sign : map->events["sign"]) {
  901. text += QString("\tbg_event %1").arg(sign->get("x"));
  902. text += QString(", %1").arg(sign->get("y"));
  903. text += QString(", %1").arg(sign->get("elevation"));
  904. text += QString(", %1").arg(sign->get("type"));
  905. text += QString(", 0");
  906. text += QString(", %1").arg(sign->get("script_label"));
  907. text += "\n";
  908. }
  909. for (Event *item : map->events["event_hidden_item"]) {
  910. text += QString("\tbg_hidden_item_event %1").arg(item->get("x"));
  911. text += QString(", %1").arg(item->get("y"));
  912. text += QString(", %1").arg(item->get("elevation"));
  913. text += QString(", %1").arg(item->get("item"));
  914. text += QString(", %1").arg(item->get("flag"));
  915. text += "\n";
  916. }
  917. for (Event *item : map->events["event_secret_base"]) {
  918. text += QString("\tbg_secret_base_event %1").arg(item->get("x"));
  919. text += QString(", %1").arg(item->get("y"));
  920. text += QString(", %1").arg(item->get("elevation"));
  921. text += QString(", %1").arg(item->get("secret_base_map"));
  922. text += "\n";
  923. }
  924. text += "\n";
  925. }
  926. text += QString("%1::\n").arg(map->events_label);
  927. text += QString("\tmap_events %1, %2, %3, %4\n")
  928. .arg(map->object_events_label)
  929. .arg(map->warps_label)
  930. .arg(map->coord_events_label)
  931. .arg(map->bg_events_label);
  932. saveTextFile(path, text);
  933. }
  934. void Project::readMapEvents(Map *map) {
  935. // lazy
  936. QString path = root + QString("/data/maps/events/%1.inc").arg(map->name);
  937. QString text = readTextFile(path);
  938. if (text.isNull()) {
  939. return;
  940. }
  941. QStringList *labels = getLabelValues(parse(text), map->events_label);
  942. map->object_events_label = labels->value(0);
  943. map->warps_label = labels->value(1);
  944. map->coord_events_label = labels->value(2);
  945. map->bg_events_label = labels->value(3);
  946. QList<QStringList> *object_events = getLabelMacros(parse(text), map->object_events_label);
  947. map->events["object"].clear();
  948. for (QStringList command : *object_events) {
  949. if (command.value(0) == "object_event") {
  950. Event *object = new Event;
  951. object->put("map_name", map->name);
  952. // This macro is not fixed as of writing, but it should take fewer args.
  953. bool old_macro = false;
  954. if (command.length() >= 20) {
  955. command.removeAt(19);
  956. command.removeAt(18);
  957. command.removeAt(15);
  958. command.removeAt(13);
  959. command.removeAt(11);
  960. command.removeAt(1); // id. not 0, but is just the index in the list of objects
  961. old_macro = true;
  962. }
  963. int i = 1;
  964. object->put("sprite", command.value(i++));
  965. object->put("replacement", command.value(i++));
  966. int16_t x = command.value(i++).toInt(nullptr, 0) | (command.value(i++).toInt(nullptr, 0) << 8);
  967. int16_t y = command.value(i++).toInt(nullptr, 0) | (command.value(i++).toInt(nullptr, 0) << 8);
  968. object->put("x", x);
  969. object->put("y", y);
  970. object->put("elevation", command.value(i++));
  971. object->put("behavior", command.value(i++));
  972. if (old_macro) {
  973. int radius = command.value(i++).toInt(nullptr, 0);
  974. object->put("radius_x", radius & 0xf);
  975. object->put("radius_y", (radius >> 4) & 0xf);
  976. } else {
  977. object->put("radius_x", command.value(i++));
  978. object->put("radius_y", command.value(i++));
  979. }
  980. object->put("property", command.value(i++));
  981. object->put("sight_radius", command.value(i++));
  982. object->put("script_label", command.value(i++));
  983. object->put("event_flag", command.value(i++));
  984. object->put("event_type", "object");
  985. map->events["object"].append(object);
  986. }
  987. }
  988. QList<QStringList> *warps = getLabelMacros(parse(text), map->warps_label);
  989. map->events["warp"].clear();
  990. for (QStringList command : *warps) {
  991. if (command.value(0) == "warp_def") {
  992. Event *warp = new Event;
  993. warp->put("map_name", map->name);
  994. int i = 1;
  995. warp->put("x", command.value(i++));
  996. warp->put("y", command.value(i++));
  997. warp->put("elevation", command.value(i++));
  998. warp->put("destination_warp", command.value(i++));
  999. // Ensure the warp destination map constant is valid before adding it to the warps.
  1000. QString mapConstant = command.value(i++);
  1001. if (mapConstantsToMapNames->contains(mapConstant)) {
  1002. warp->put("destination_map_name", mapConstantsToMapNames->value(mapConstant));
  1003. warp->put("event_type", "warp");
  1004. map->events["warp"].append(warp);
  1005. } else {
  1006. qDebug() << QString("Destination map constant '%1' is invalid for warp").arg(mapConstant);
  1007. }
  1008. }
  1009. }
  1010. QList<QStringList> *coords = getLabelMacros(parse(text), map->coord_events_label);
  1011. map->events["trap"].clear();
  1012. map->events["trap_weather"].clear();
  1013. for (QStringList command : *coords) {
  1014. if (command.value(0) == "coord_event") {
  1015. Event *coord = new Event;
  1016. coord->put("map_name", map->name);
  1017. bool old_macro = false;
  1018. if (command.length() >= 9) {
  1019. command.removeAt(7);
  1020. command.removeAt(4);
  1021. old_macro = true;
  1022. }
  1023. int i = 1;
  1024. coord->put("x", command.value(i++));
  1025. coord->put("y", command.value(i++));
  1026. coord->put("elevation", command.value(i++));
  1027. coord->put("coord_unknown1", command.value(i++));
  1028. coord->put("coord_unknown2", command.value(i++));
  1029. coord->put("script_label", command.value(i++));
  1030. //coord_unknown3
  1031. //coord_unknown4
  1032. coord->put("event_type", "trap");
  1033. map->events["trap"].append(coord);
  1034. } else if (command.value(0) == "coord_weather_event") {
  1035. Event *coord = new Event;
  1036. coord->put("map_name", map->name);
  1037. int i = 1;
  1038. coord->put("x", command.value(i++));
  1039. coord->put("y", command.value(i++));
  1040. coord->put("elevation", command.value(i++));
  1041. coord->put("weather", command.value(i++));
  1042. coord->put("event_type", "trap_weather");
  1043. map->events["trap_weather"].append(coord);
  1044. }
  1045. }
  1046. QList<QStringList> *bgs = getLabelMacros(parse(text), map->bg_events_label);
  1047. map->events["sign"].clear();
  1048. map->events["event_hidden_item"].clear();
  1049. map->events["event_secret_base"].clear();
  1050. for (QStringList command : *bgs) {
  1051. if (command.value(0) == "bg_event") {
  1052. Event *bg = new Event;
  1053. bg->put("map_name", map->name);
  1054. int i = 1;
  1055. bg->put("x", command.value(i++));
  1056. bg->put("y", command.value(i++));
  1057. bg->put("elevation", command.value(i++));
  1058. bg->put("type", command.value(i++));
  1059. i++;
  1060. bg->put("script_label", command.value(i++));
  1061. //sign_unknown7
  1062. bg->put("event_type", "sign");
  1063. map->events["sign"].append(bg);
  1064. } else if (command.value(0) == "bg_hidden_item_event") {
  1065. Event *bg = new Event;
  1066. bg->put("map_name", map->name);
  1067. int i = 1;
  1068. bg->put("x", command.value(i++));
  1069. bg->put("y", command.value(i++));
  1070. bg->put("elevation", command.value(i++));
  1071. bg->put("item", command.value(i++));
  1072. bg->put("flag", command.value(i++));
  1073. bg->put("event_type", "event_hidden_item");
  1074. map->events["event_hidden_item"].append(bg);
  1075. } else if (command.value(0) == "bg_secret_base_event") {
  1076. Event *bg = new Event;
  1077. bg->put("map_name", map->name);
  1078. int i = 1;
  1079. bg->put("x", command.value(i++));
  1080. bg->put("y", command.value(i++));
  1081. bg->put("elevation", command.value(i++));
  1082. bg->put("secret_base_map", command.value(i++));
  1083. bg->put("event_type", "event_secret_base");
  1084. map->events["event_secret_base"].append(bg);
  1085. }
  1086. }
  1087. }
  1088. QStringList Project::readCArray(QString text, QString label) {
  1089. QStringList list;
  1090. if (label.isNull()) {
  1091. return list;
  1092. }
  1093. QRegExp *re = new QRegExp(QString("\\b%1\\b\\s*\\[?\\s*\\]?\\s*=\\s*\\{([^\\}]*)\\}").arg(label));
  1094. int pos = re->indexIn(text);
  1095. if (pos != -1) {
  1096. QString body = re->cap(1);
  1097. body = body.replace(QRegExp("\\s*"), "");
  1098. list = body.split(',');
  1099. /*
  1100. QRegExp *inner = new QRegExp("&?\\b([A-Za-z0-9_\\(\\)]*)\\b,");
  1101. int pos = 0;
  1102. while ((pos = inner->indexIn(body, pos)) != -1) {
  1103. list << inner->cap(1);
  1104. pos += inner->matchedLength();
  1105. }
  1106. */
  1107. }
  1108. return list;
  1109. }
  1110. QString Project::readCIncbin(QString text, QString label) {
  1111. QString path;
  1112. if (label.isNull()) {
  1113. return path;
  1114. }
  1115. QRegExp *re = new QRegExp(QString(
  1116. "\\b%1\\b"
  1117. "\\s*\\[?\\s*\\]?\\s*=\\s*"
  1118. "INCBIN_[US][0-9][0-9]?"
  1119. "\\(\"([^\"]*)\"\\)").arg(label));
  1120. int pos = re->indexIn(text);
  1121. if (pos != -1) {
  1122. path = re->cap(1);
  1123. }
  1124. return path;
  1125. }
  1126. QMap<QString, int> Project::readCDefines(QString text, QStringList prefixes) {
  1127. QMap<QString, int> defines;
  1128. QString combinedPrefixes = "[" + prefixes.join('|') + "]";
  1129. QRegularExpression re(QString("#define\\s+(?<defineName>%1\\w+)\\s(?<defineValue>\\w+)").arg(combinedPrefixes));
  1130. QRegularExpressionMatchIterator iter = re.globalMatch(text);
  1131. while (iter.hasNext()) {
  1132. QRegularExpressionMatch match = iter.next();
  1133. QString name = match.captured("defineName");
  1134. QString value = match.captured("defineValue");
  1135. bool valid;
  1136. int parsedValue = value.startsWith("0x") ? value.toInt(&valid, 16) : value.toInt(&valid, 10);
  1137. if (valid) {
  1138. if (!defines.contains(name)) {
  1139. defines.insert(name, parsedValue);
  1140. } else {
  1141. qDebug() << QString("Define '%1' is defined multiple times'").arg(name);
  1142. }
  1143. } else {
  1144. qDebug() << QString("Failed to parse define '%1' value '%2' as base 10 or hexadecimal value").arg(name, value);
  1145. }
  1146. }
  1147. return defines;
  1148. }