Без опису

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