Brak opisu

project.cpp 53KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. #include "parseutil.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. mapNames = new QStringList;
  19. itemNames = new QStringList;
  20. flagNames = new QStringList;
  21. varNames = new QStringList;
  22. map_cache = new QMap<QString, Map*>;
  23. mapConstantsToMapNames = new QMap<QString, QString>;
  24. mapNamesToMapConstants = new QMap<QString, QString>;
  25. tileset_cache = new QMap<QString, Tileset*>;
  26. }
  27. QString Project::getProjectTitle() {
  28. if (!root.isNull()) {
  29. return root.section('/', -1);
  30. } else {
  31. return QString();
  32. }
  33. }
  34. Map* Project::loadMap(QString map_name) {
  35. Map *map;
  36. if (map_cache->contains(map_name)) {
  37. map = map_cache->value(map_name);
  38. // TODO: uncomment when undo/redo history is fully implemented for all actions.
  39. if (true/*map->hasUnsavedChanges()*/) {
  40. return map;
  41. }
  42. } else {
  43. map = new Map;
  44. map->setName(map_name);
  45. }
  46. readMapHeader(map);
  47. readMapLayout(map);
  48. readMapEvents(map);
  49. loadMapConnections(map);
  50. map->commit();
  51. map->history.save();
  52. map_cache->insert(map_name, map);
  53. return map;
  54. }
  55. void Project::loadMapConnections(Map *map) {
  56. if (!map->isPersistedToFile) {
  57. return;
  58. }
  59. map->connections.clear();
  60. map->connection_items.clear();
  61. if (!map->connections_label.isNull()) {
  62. QString path = root + QString("/data/maps/%1/connections.inc").arg(map->name);
  63. QString text = readTextFile(path);
  64. if (!text.isNull()) {
  65. QList<QStringList> *commands = parseAsm(text);
  66. QStringList *list = getLabelValues(commands, map->connections_label);
  67. //// Avoid using this value. It ought to be generated instead.
  68. //int num_connections = list->value(0).toInt(nullptr, 0);
  69. QString connections_list_label = list->value(1);
  70. QList<QStringList> *connections = getLabelMacros(commands, connections_list_label);
  71. for (QStringList command : *connections) {
  72. QString macro = command.value(0);
  73. if (macro == "connection") {
  74. Connection *connection = new Connection;
  75. connection->direction = command.value(1);
  76. connection->offset = command.value(2);
  77. QString mapConstant = command.value(3);
  78. if (mapConstantsToMapNames->contains(mapConstant)) {
  79. connection->map_name = mapConstantsToMapNames->value(mapConstant);
  80. map->connections.append(connection);
  81. } else {
  82. qDebug() << QString("Failed to find connected map for map constant '%1'").arg(mapConstant);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. void Project::setNewMapConnections(Map *map) {
  90. map->connections.clear();
  91. }
  92. QList<QStringList>* Project::getLabelMacros(QList<QStringList> *list, QString label) {
  93. bool in_label = false;
  94. QList<QStringList> *new_list = new QList<QStringList>;
  95. for (int i = 0; i < list->length(); i++) {
  96. QStringList params = list->value(i);
  97. QString macro = params.value(0);
  98. if (macro == ".label") {
  99. if (params.value(1) == label) {
  100. in_label = true;
  101. } else if (in_label) {
  102. // If nothing has been read yet, assume the label
  103. // we're looking for is in a stack of labels.
  104. if (new_list->length() > 0) {
  105. break;
  106. }
  107. }
  108. } else if (in_label) {
  109. new_list->append(params);
  110. }
  111. }
  112. return new_list;
  113. }
  114. // For if you don't care about filtering by macro,
  115. // and just want all values associated with some label.
  116. QStringList* Project::getLabelValues(QList<QStringList> *list, QString label) {
  117. list = getLabelMacros(list, label);
  118. QStringList *values = new QStringList;
  119. for (int i = 0; i < list->length(); i++) {
  120. QStringList params = list->value(i);
  121. QString macro = params.value(0);
  122. // Ignore .align
  123. if (macro == ".align")
  124. continue;
  125. if (macro == ".ifdef")
  126. continue;
  127. if (macro == ".ifndef")
  128. continue;
  129. for (int j = 1; j < params.length(); j++) {
  130. values->append(params.value(j));
  131. }
  132. }
  133. return values;
  134. }
  135. void Project::readMapHeader(Map* map) {
  136. if (!map->isPersistedToFile) {
  137. return;
  138. }
  139. QString label = map->name;
  140. ParseUtil *parser = new ParseUtil;
  141. QString header_text = readTextFile(root + "/data/maps/" + label + "/header.inc");
  142. if (header_text.isNull()) {
  143. return;
  144. }
  145. QStringList *header = getLabelValues(parser->parseAsm(header_text), label);
  146. map->layout_label = header->value(0);
  147. map->events_label = header->value(1);
  148. map->scripts_label = header->value(2);
  149. map->connections_label = header->value(3);
  150. map->song = header->value(4);
  151. map->layout_id = header->value(5);
  152. map->location = header->value(6);
  153. map->visibility = header->value(7);
  154. map->weather = header->value(8);
  155. map->type = header->value(9);
  156. map->unknown = header->value(10);
  157. map->show_location = header->value(11);
  158. map->battle_scene = header->value(12);
  159. }
  160. void Project::setNewMapHeader(Map* map, int mapIndex) {
  161. map->layout_label = QString("%1_Layout").arg(map->name);
  162. map->events_label = QString("%1_MapEvents").arg(map->name);;
  163. map->scripts_label = QString("%1_MapScripts").arg(map->name);;
  164. map->connections_label = "0x0";
  165. map->song = "MUS_DAN02";
  166. map->layout_id = QString("%1").arg(mapIndex);
  167. map->location = "0";
  168. map->visibility = "0";
  169. map->weather = "2";
  170. map->type = "1";
  171. map->unknown = "0";
  172. map->show_location = "1";
  173. map->battle_scene = "0";
  174. }
  175. void Project::saveMapHeader(Map *map) {
  176. QString label = map->name;
  177. QString header_path = root + "/data/maps/" + label + "/header.inc";
  178. QString text = "";
  179. text += QString("%1::\n").arg(label);
  180. text += QString("\t.4byte %1\n").arg(map->layout_label);
  181. text += QString("\t.4byte %1\n").arg(map->events_label);
  182. text += QString("\t.4byte %1\n").arg(map->scripts_label);
  183. if (map->connections.length() == 0) {
  184. map->connections_label = "0x0";
  185. } else {
  186. map->connections_label = QString("%1_MapConnections").arg(map->name);
  187. }
  188. text += QString("\t.4byte %1\n").arg(map->connections_label);
  189. text += QString("\t.2byte %1\n").arg(map->song);
  190. text += QString("\t.2byte %1\n").arg(map->layout_id);
  191. text += QString("\t.byte %1\n").arg(map->location);
  192. text += QString("\t.byte %1\n").arg(map->visibility);
  193. text += QString("\t.byte %1\n").arg(map->weather);
  194. text += QString("\t.byte %1\n").arg(map->type);
  195. text += QString("\t.2byte %1\n").arg(map->unknown);
  196. text += QString("\t.byte %1\n").arg(map->show_location);
  197. text += QString("\t.byte %1\n").arg(map->battle_scene);
  198. saveTextFile(header_path, text);
  199. }
  200. void Project::saveMapConnections(Map *map) {
  201. QString path = root + "/data/maps/" + map->name + "/connections.inc";
  202. if (map->connections.length() > 0) {
  203. QString text = "";
  204. QString connectionsListLabel = QString("%1_MapConnectionsList").arg(map->name);
  205. int numValidConnections = 0;
  206. text += QString("%1::\n").arg(connectionsListLabel);
  207. for (Connection* connection : map->connections) {
  208. if (mapNamesToMapConstants->contains(connection->map_name)) {
  209. text += QString("\tconnection %1, %2, %3\n")
  210. .arg(connection->direction)
  211. .arg(connection->offset)
  212. .arg(mapNamesToMapConstants->value(connection->map_name));
  213. numValidConnections++;
  214. } else {
  215. qDebug() << QString("Failed to write map connection. %1 not a valid map name").arg(connection->map_name);
  216. }
  217. }
  218. text += QString("\n");
  219. text += QString("%1::\n").arg(map->connections_label);
  220. text += QString("\t.4byte %1\n").arg(numValidConnections);
  221. text += QString("\t.4byte %1\n").arg(connectionsListLabel);
  222. saveTextFile(path, text);
  223. } else {
  224. deleteFile(path);
  225. }
  226. updateMapsWithConnections(map);
  227. }
  228. void Project::updateMapsWithConnections(Map *map) {
  229. if (map->connections.length() > 0) {
  230. if (!mapsWithConnections.contains(map->name)) {
  231. mapsWithConnections.append(map->name);
  232. }
  233. } else {
  234. if (mapsWithConnections.contains(map->name)) {
  235. mapsWithConnections.removeOne(map->name);
  236. }
  237. }
  238. }
  239. void Project::readMapLayoutsTable() {
  240. int curIndex = 1;
  241. QString layoutsText = readTextFile(getMapLayoutsTableFilepath());
  242. QList<QStringList>* values = parseAsm(layoutsText);
  243. bool inLayoutPointers = false;
  244. for (int i = 0; i < values->length(); i++) {
  245. QStringList params = values->value(i);
  246. QString macro = params.value(0);
  247. if (macro == ".label") {
  248. if (inLayoutPointers) {
  249. break;
  250. }
  251. if (params.value(1) == "gMapLayouts") {
  252. inLayoutPointers = true;
  253. }
  254. } else if (macro == ".4byte" && inLayoutPointers) {
  255. QString layoutName = params.value(1);
  256. mapLayoutsTable.append(layoutName);
  257. }
  258. }
  259. // Deep copy
  260. mapLayoutsTableMaster = mapLayoutsTable;
  261. mapLayoutsTableMaster.detach();
  262. }
  263. void Project::saveMapLayoutsTable() {
  264. QString text = "";
  265. text += QString("\t.align 2\n");
  266. text += QString("gMapLayouts::\n");
  267. for (QString layoutName : mapLayoutsTableMaster) {
  268. text += QString("\t.4byte %1\n").arg(layoutName);
  269. }
  270. saveTextFile(getMapLayoutsTableFilepath(), text);
  271. }
  272. QString Project::getMapLayoutsTableFilepath() {
  273. return QString("%1/data/layouts_table.inc").arg(root);
  274. }
  275. QStringList* Project::readLayoutValues(QString layoutLabel) {
  276. ParseUtil *parser = new ParseUtil;
  277. QString layoutText = readTextFile(getMapLayoutFilepath(layoutLabel));
  278. if (layoutText.isNull()) {
  279. return NULL;
  280. }
  281. QStringList *layoutValues = getLabelValues(parser->parseAsm(layoutText), layoutLabel);
  282. QString borderLabel = layoutValues->value(2);
  283. QString blockdataLabel = layoutValues->value(3);
  284. QStringList *borderValues = getLabelValues(parser->parseAsm(layoutText), borderLabel);
  285. QString borderPath = borderValues->value(0).replace("\"", "");
  286. layoutValues->append(borderPath);
  287. QStringList *blockdataValues = getLabelValues(parser->parseAsm(layoutText), blockdataLabel);
  288. QString blockdataPath = blockdataValues->value(0).replace("\"", "");
  289. layoutValues->append(blockdataPath);
  290. if (layoutValues->size() != 8) {
  291. qDebug() << "Error: Unexpected number of properties in layout '" << layoutLabel << "'";
  292. return NULL;
  293. }
  294. return layoutValues;
  295. }
  296. void Project::readMapLayout(Map* map) {
  297. if (!map->isPersistedToFile) {
  298. return;
  299. }
  300. MapLayout *layout;
  301. if (!mapLayouts.contains(map->layout_label)) {
  302. QStringList *layoutValues = readLayoutValues(map->layout->label);
  303. if (layoutValues == NULL) {
  304. return;
  305. }
  306. layout = new MapLayout();
  307. mapLayouts.insert(map->layout_label, layout);
  308. layout->name = MapLayout::getNameFromLabel(map->layout_label);
  309. layout->label = map->layout_label;
  310. layout->width = layoutValues->value(0);
  311. layout->height = layoutValues->value(1);
  312. layout->border_label = layoutValues->value(2);
  313. layout->blockdata_label = layoutValues->value(3);
  314. layout->tileset_primary_label = layoutValues->value(4);
  315. layout->tileset_secondary_label = layoutValues->value(5);
  316. layout->border_path = layoutValues->value(6);
  317. layout->blockdata_path = layoutValues->value(7);
  318. map->layout = layout;
  319. } else {
  320. map->layout = mapLayouts[map->layout_label];
  321. }
  322. getTilesets(map);
  323. loadBlockdata(map);
  324. loadMapBorder(map);
  325. }
  326. void Project::readAllMapLayouts() {
  327. mapLayouts.clear();
  328. for (int i = 0; i < mapLayoutsTable.size(); i++) {
  329. QString layoutLabel = mapLayoutsTable[i];
  330. QStringList *layoutValues = readLayoutValues(layoutLabel);
  331. if (layoutValues == NULL) {
  332. return;
  333. }
  334. MapLayout *layout = new MapLayout();
  335. layout->name = MapLayout::getNameFromLabel(layoutLabel);
  336. layout->label = layoutLabel;
  337. layout->index = i;
  338. layout->width = layoutValues->value(0);
  339. layout->height = layoutValues->value(1);
  340. layout->border_label = layoutValues->value(2);
  341. layout->blockdata_label = layoutValues->value(3);
  342. layout->tileset_primary_label = layoutValues->value(4);
  343. layout->tileset_secondary_label = layoutValues->value(5);
  344. layout->border_path = layoutValues->value(6);
  345. layout->blockdata_path = layoutValues->value(7);
  346. mapLayouts.insert(layoutLabel, layout);
  347. }
  348. // Deep copy
  349. mapLayoutsMaster = mapLayouts;
  350. mapLayoutsMaster.detach();
  351. }
  352. void Project::saveAllMapLayouts() {
  353. for (QString layoutName : mapLayoutsTableMaster) {
  354. MapLayout *layout = mapLayouts.value(layoutName);
  355. QString text = QString("%1::\n").arg(layout->border_label);
  356. text += QString("\t.incbin \"%1\"\n").arg(layout->border_path);
  357. text += QString("\n");
  358. text += QString("%1::\n").arg(layout->blockdata_label);
  359. text += QString("\t.incbin \"%1\"\n").arg(layout->blockdata_path);
  360. text += QString("\n");
  361. text += QString("\t.align 2\n");
  362. text += QString("%1::\n").arg(layoutName);
  363. text += QString("\t.4byte %1\n").arg(layout->width);
  364. text += QString("\t.4byte %1\n").arg(layout->height);
  365. text += QString("\t.4byte %1\n").arg(layout->border_label);
  366. text += QString("\t.4byte %1\n").arg(layout->blockdata_label);
  367. text += QString("\t.4byte %1\n").arg(layout->tileset_primary_label);
  368. text += QString("\t.4byte %1\n").arg(layout->tileset_secondary_label);
  369. text += QString("\n");
  370. saveTextFile(getMapLayoutFilepath(layout->label), text);
  371. }
  372. }
  373. QString Project::getMapLayoutFilepath(QString layoutLabel) {
  374. return QString("%1/data/layouts/%2/layout.inc").arg(root).arg(MapLayout::getNameFromLabel(layoutLabel));
  375. }
  376. void Project::setNewMapLayout(Map* map) {
  377. MapLayout *layout = new MapLayout();
  378. layout->label = QString("%1_Layout").arg(map->name);
  379. layout->name = MapLayout::getNameFromLabel(layout->label);
  380. layout->width = "20";
  381. layout->height = "20";
  382. layout->border_label = QString("%1_MapBorder").arg(map->name);
  383. layout->border_path = QString("data/layouts/%1/border.bin").arg(map->name);
  384. layout->blockdata_label = QString("%1_MapBlockdata").arg(map->name);
  385. layout->blockdata_path = QString("data/layouts/%1/map.bin").arg(map->name);
  386. layout->tileset_primary_label = "gTileset_General";
  387. layout->tileset_secondary_label = "gTileset_Petalburg";
  388. map->layout = layout;
  389. map->layout_label = layout->label;
  390. // Insert new entry into the global map layouts.
  391. mapLayouts.insert(layout->label, layout);
  392. mapLayoutsTable.append(layout->label);
  393. }
  394. void Project::saveMapGroupsTable() {
  395. QString text = "";
  396. int groupNum = 0;
  397. for (QStringList mapNames : groupedMapNames) {
  398. text += QString("\t.align 2\n");
  399. text += QString("gMapGroup%1::\n").arg(groupNum);
  400. for (QString mapName : mapNames) {
  401. text += QString("\t.4byte %1\n").arg(mapName);
  402. }
  403. text += QString("\n");
  404. groupNum++;
  405. }
  406. text += QString("\t.align 2\n");
  407. text += QString("gMapGroups::\n");
  408. for (int i = 0; i < groupNum; i++) {
  409. text += QString("\t.4byte gMapGroup%1\n").arg(i);
  410. }
  411. saveTextFile(root + "/data/maps/groups.inc", text);
  412. }
  413. void Project::saveMapConstantsHeader() {
  414. QString text = QString("#ifndef GUARD_CONSTANTS_MAPS_H\n");
  415. text += QString("#define GUARD_CONSTANTS_MAPS_H\n");
  416. text += QString("\n");
  417. int groupNum = 0;
  418. for (QStringList mapNames : groupedMapNames) {
  419. text += QString("// Map Group %1\n").arg(groupNum);
  420. int maxLength = 0;
  421. for (QString mapName : mapNames) {
  422. QString mapConstantName = mapNamesToMapConstants->value(mapName);
  423. if (mapConstantName.length() > maxLength)
  424. maxLength = mapConstantName.length();
  425. }
  426. int groupIndex = 0;
  427. for (QString mapName : mapNames) {
  428. QString mapConstantName = mapNamesToMapConstants->value(mapName);
  429. text += QString("#define %1%2(%3 | (%4 << 8))\n")
  430. .arg(mapConstantName)
  431. .arg(QString(" ").repeated(maxLength - mapConstantName.length() + 1))
  432. .arg(groupIndex)
  433. .arg(groupNum);
  434. groupIndex++;
  435. }
  436. text += QString("\n");
  437. groupNum++;
  438. }
  439. text += QString("\n");
  440. text += QString("#define MAP_NONE (0x7F | (0x7F << 8))\n");
  441. text += QString("#define MAP_UNDEFINED (0xFF | (0xFF << 8))\n\n\n");
  442. text += QString("#define MAP_GROUP(map) (MAP_##map >> 8)\n");
  443. text += QString("#define MAP_NUM(map) (MAP_##map & 0xFF)\n\n");
  444. text += QString("#endif // GUARD_CONSTANTS_MAPS_H\n");
  445. saveTextFile(root + "/include/constants/maps.h", text);
  446. }
  447. void Project::getTilesets(Map* map) {
  448. if (map->layout->has_unsaved_changes) {
  449. return;
  450. }
  451. map->layout->tileset_primary = getTileset(map->layout->tileset_primary_label);
  452. map->layout->tileset_secondary = getTileset(map->layout->tileset_secondary_label);
  453. }
  454. Tileset* Project::loadTileset(QString label) {
  455. ParseUtil *parser = new ParseUtil;
  456. QString headers_text = readTextFile(root + "/data/tilesets/headers.inc");
  457. QStringList *values = getLabelValues(parser->parseAsm(headers_text), label);
  458. Tileset *tileset = new Tileset;
  459. tileset->name = label;
  460. tileset->is_compressed = values->value(0);
  461. tileset->is_secondary = values->value(1);
  462. tileset->padding = values->value(2);
  463. tileset->tiles_label = values->value(3);
  464. tileset->palettes_label = values->value(4);
  465. tileset->metatiles_label = values->value(5);
  466. tileset->metatile_attrs_label = values->value(6);
  467. tileset->callback_label = values->value(7);
  468. loadTilesetAssets(tileset);
  469. tileset_cache->insert(label, tileset);
  470. return tileset;
  471. }
  472. void Project::loadBlockdata(Map* map) {
  473. if (!map->isPersistedToFile || map->layout->has_unsaved_changes) {
  474. return;
  475. }
  476. QString path = QString("%1/%2").arg(root).arg(map->layout->blockdata_path);
  477. map->layout->blockdata = readBlockdata(path);
  478. }
  479. void Project::setNewMapBlockdata(Map* map) {
  480. Blockdata *blockdata = new Blockdata;
  481. for (int i = 0; i < map->getWidth() * map->getHeight(); i++) {
  482. blockdata->addBlock(qint16(0x3001));
  483. }
  484. map->layout->blockdata = blockdata;
  485. }
  486. void Project::loadMapBorder(Map *map) {
  487. if (!map->isPersistedToFile || map->layout->has_unsaved_changes) {
  488. return;
  489. }
  490. QString path = QString("%1/%2").arg(root).arg(map->layout->border_path);
  491. map->layout->border = readBlockdata(path);
  492. }
  493. void Project::setNewMapBorder(Map *map) {
  494. Blockdata *blockdata = new Blockdata;
  495. blockdata->addBlock(qint16(0x01D4));
  496. blockdata->addBlock(qint16(0x01D5));
  497. blockdata->addBlock(qint16(0x01DC));
  498. blockdata->addBlock(qint16(0x01DD));
  499. map->layout->border = blockdata;
  500. }
  501. void Project::saveMapBorder(Map *map) {
  502. QString path = QString("%1/%2").arg(root).arg(map->layout->border_path);
  503. writeBlockdata(path, map->layout->border);
  504. }
  505. void Project::saveBlockdata(Map* map) {
  506. QString path = QString("%1/%2").arg(root).arg(map->layout->blockdata_path);
  507. writeBlockdata(path, map->layout->blockdata);
  508. map->history.save();
  509. }
  510. void Project::writeBlockdata(QString path, Blockdata *blockdata) {
  511. QFile file(path);
  512. if (file.open(QIODevice::WriteOnly)) {
  513. QByteArray data = blockdata->serialize();
  514. file.write(data);
  515. } else {
  516. qDebug() << "Failed to open blockdata file for writing: '" << path << "'";
  517. }
  518. }
  519. void Project::saveAllMaps() {
  520. QList<QString> keys = map_cache->keys();
  521. for (int i = 0; i < keys.length(); i++) {
  522. QString key = keys.value(i);
  523. Map* map = map_cache->value(key);
  524. saveMap(map);
  525. }
  526. }
  527. void Project::saveMap(Map *map) {
  528. // Create/Modify a few collateral files for brand new maps.
  529. if (!map->isPersistedToFile) {
  530. QString newMapDataDir = QString(root + "/data/maps/%1").arg(map->name);
  531. if (!QDir::root().mkdir(newMapDataDir)) {
  532. qDebug() << "Error: failed to create directory for new map. " << newMapDataDir;
  533. }
  534. QString newLayoutDir = QString(root + "/data/layouts/%1").arg(map->name);
  535. if (!QDir::root().mkdir(newLayoutDir)) {
  536. qDebug() << "Error: failed to create directory for new layout. " << newLayoutDir;
  537. }
  538. // TODO: In the future, these files needs more structure to allow for proper parsing/saving.
  539. // Create file data/maps/<map_name>/scripts.inc
  540. QString text = QString("%1_MapScripts::\n\t.byte 0\n").arg(map->name);
  541. saveTextFile(root + "/data/maps/" + map->name + "/scripts.inc", text);
  542. // Create file data/maps/<map_name>/text.inc
  543. saveTextFile(root + "/data/maps/" + map->name + "/text.inc", "\n");
  544. // Simply append to data/event_scripts.s.
  545. text = QString("\n\t.include \"data/maps/%1/scripts.inc\"\n").arg(map->name);
  546. text += QString("\t.include \"data/maps/%1/text.inc\"\n").arg(map->name);
  547. appendTextFile(root + "/data/event_scripts.s", text);
  548. // Simply append to data/map_events.s.
  549. text = QString("\n\t.include \"data/maps/%1/events.inc\"\n").arg(map->name);
  550. appendTextFile(root + "/data/map_events.s", text);
  551. // Simply append to data/maps/headers.inc.
  552. text = QString("\t.include \"data/maps/%1/header.inc\"\n").arg(map->name);
  553. appendTextFile(root + "/data/maps/headers.inc", text);
  554. // Simply append to data/layouts.inc.
  555. text = QString("\t.include \"data/layouts/%1/layout.inc\"\n").arg(map->layout->name);
  556. appendTextFile(root + "/data/layouts.inc", text);
  557. }
  558. saveMapBorder(map);
  559. saveMapHeader(map);
  560. saveMapConnections(map);
  561. saveBlockdata(map);
  562. saveMapEvents(map);
  563. // Update global data structures with current map data.
  564. updateMapLayout(map);
  565. map->isPersistedToFile = true;
  566. map->layout->has_unsaved_changes = false;
  567. }
  568. void Project::updateMapLayout(Map* map) {
  569. if (!mapLayoutsTableMaster.contains(map->layout_label)) {
  570. mapLayoutsTableMaster.append(map->layout_label);
  571. }
  572. // Deep copy
  573. MapLayout *layout = mapLayouts.value(map->layout_label);
  574. MapLayout *newLayout = new MapLayout();
  575. *newLayout = *layout;
  576. mapLayoutsMaster.insert(map->layout_label, newLayout);
  577. }
  578. void Project::saveAllDataStructures() {
  579. saveMapLayoutsTable();
  580. saveAllMapLayouts();
  581. saveMapGroupsTable();
  582. saveMapConstantsHeader();
  583. saveMapsWithConnections();
  584. }
  585. void Project::loadTilesetAssets(Tileset* tileset) {
  586. ParseUtil* parser = new ParseUtil;
  587. QString category = (tileset->is_secondary == "TRUE") ? "secondary" : "primary";
  588. if (tileset->name.isNull()) {
  589. return;
  590. }
  591. QString dir_path = root + "/data/tilesets/" + category + "/" + tileset->name.replace("gTileset_", "").toLower();
  592. QString graphics_text = readTextFile(root + "/data/tilesets/graphics.inc");
  593. QList<QStringList> *graphics = parser->parseAsm(graphics_text);
  594. QStringList *tiles_values = getLabelValues(graphics, tileset->tiles_label);
  595. QStringList *palettes_values = getLabelValues(graphics, tileset->palettes_label);
  596. QString tiles_path;
  597. if (!tiles_values->isEmpty()) {
  598. tiles_path = root + "/" + tiles_values->value(0).section('"', 1, 1);
  599. } else {
  600. tiles_path = dir_path + "/tiles.4bpp";
  601. if (tileset->is_compressed == "TRUE") {
  602. tiles_path += ".lz";
  603. }
  604. }
  605. QStringList *palette_paths = new QStringList;
  606. if (!palettes_values->isEmpty()) {
  607. for (int i = 0; i < palettes_values->length(); i++) {
  608. QString value = palettes_values->value(i);
  609. palette_paths->append(root + "/" + value.section('"', 1, 1));
  610. }
  611. } else {
  612. QString palettes_dir_path = dir_path + "/palettes";
  613. for (int i = 0; i < 16; i++) {
  614. palette_paths->append(palettes_dir_path + "/" + QString("%1").arg(i, 2, 10, QLatin1Char('0')) + ".gbapal");
  615. }
  616. }
  617. QString metatiles_path;
  618. QString metatile_attrs_path;
  619. QString metatiles_text = readTextFile(root + "/data/tilesets/metatiles.inc");
  620. QList<QStringList> *metatiles_macros = parser->parseAsm(metatiles_text);
  621. QStringList *metatiles_values = getLabelValues(metatiles_macros, tileset->metatiles_label);
  622. if (!metatiles_values->isEmpty()) {
  623. metatiles_path = root + "/" + metatiles_values->value(0).section('"', 1, 1);
  624. } else {
  625. metatiles_path = dir_path + "/metatiles.bin";
  626. }
  627. QStringList *metatile_attrs_values = getLabelValues(metatiles_macros, tileset->metatile_attrs_label);
  628. if (!metatile_attrs_values->isEmpty()) {
  629. metatile_attrs_path = root + "/" + metatile_attrs_values->value(0).section('"', 1, 1);
  630. } else {
  631. metatile_attrs_path = dir_path + "/metatile_attributes.bin";
  632. }
  633. // tiles
  634. tiles_path = fixGraphicPath(tiles_path);
  635. QImage *image = new QImage(tiles_path);
  636. //image->setColor(0, qRgb(0xff, 0, 0)); // debug
  637. QList<QImage> *tiles = new QList<QImage>;
  638. int w = 8;
  639. int h = 8;
  640. for (int y = 0; y < image->height(); y += h)
  641. for (int x = 0; x < image->width(); x += w) {
  642. QImage tile = image->copy(x, y, w, h);
  643. tiles->append(tile);
  644. }
  645. tileset->tiles = tiles;
  646. // metatiles
  647. QFile metatiles_file(metatiles_path);
  648. if (metatiles_file.open(QIODevice::ReadOnly)) {
  649. QByteArray data = metatiles_file.readAll();
  650. int num_metatiles = data.length() / 16;
  651. int num_layers = 2;
  652. QList<Metatile*> *metatiles = new QList<Metatile*>;
  653. for (int i = 0; i < num_metatiles; i++) {
  654. Metatile *metatile = new Metatile;
  655. int index = i * (2 * 4 * num_layers);
  656. for (int j = 0; j < 4 * num_layers; j++) {
  657. uint16_t word = data[index++] & 0xff;
  658. word += (data[index++] & 0xff) << 8;
  659. Tile tile;
  660. tile.tile = word & 0x3ff;
  661. tile.xflip = (word >> 10) & 1;
  662. tile.yflip = (word >> 11) & 1;
  663. tile.palette = (word >> 12) & 0xf;
  664. metatile->tiles->append(tile);
  665. }
  666. metatiles->append(metatile);
  667. }
  668. tileset->metatiles = metatiles;
  669. } else {
  670. tileset->metatiles = new QList<Metatile*>;
  671. qDebug() << QString("Could not open '%1'").arg(metatiles_path);
  672. }
  673. QFile attrs_file(metatile_attrs_path);
  674. //qDebug() << metatile_attrs_path;
  675. if (attrs_file.open(QIODevice::ReadOnly)) {
  676. QByteArray data = attrs_file.readAll();
  677. int num_metatiles = data.length() / 2;
  678. for (int i = 0; i < num_metatiles; i++) {
  679. uint16_t word = data[i*2] & 0xff;
  680. word += (data[i*2 + 1] & 0xff) << 8;
  681. tileset->metatiles->value(i)->attr = word;
  682. }
  683. } else {
  684. qDebug() << QString("Could not open '%1'").arg(metatile_attrs_path);
  685. }
  686. // palettes
  687. QList<QList<QRgb>> *palettes = new QList<QList<QRgb>>;
  688. for (int i = 0; i < palette_paths->length(); i++) {
  689. QString path = palette_paths->value(i);
  690. // the palettes are not compressed. this should never happen. it's only a precaution.
  691. path = path.replace(QRegExp("\\.lz$"), "");
  692. // TODO default to .pal (JASC-PAL)
  693. // just use .gbapal for now
  694. QFile file(path);
  695. QList<QRgb> palette;
  696. if (file.open(QIODevice::ReadOnly)) {
  697. QByteArray data = file.readAll();
  698. for (int j = 0; j < 16; j++) {
  699. uint16_t word = data[j*2] & 0xff;
  700. word += (data[j*2 + 1] & 0xff) << 8;
  701. int red = word & 0x1f;
  702. int green = (word >> 5) & 0x1f;
  703. int blue = (word >> 10) & 0x1f;
  704. QRgb color = qRgb(red * 8, green * 8, blue * 8);
  705. palette.prepend(color);
  706. }
  707. } else {
  708. for (int j = 0; j < 16; j++) {
  709. palette.append(qRgb(j * 16, j * 16, j * 16));
  710. }
  711. qDebug() << QString("Could not open palette path '%1'").arg(path);
  712. }
  713. palettes->append(palette);
  714. }
  715. tileset->palettes = palettes;
  716. }
  717. Blockdata* Project::readBlockdata(QString path) {
  718. Blockdata *blockdata = new Blockdata;
  719. QFile file(path);
  720. if (file.open(QIODevice::ReadOnly)) {
  721. QByteArray data = file.readAll();
  722. for (int i = 0; (i + 1) < data.length(); i += 2) {
  723. uint16_t word = (data[i] & 0xff) + ((data[i + 1] & 0xff) << 8);
  724. blockdata->addBlock(word);
  725. }
  726. } else {
  727. qDebug() << "Failed to open blockdata path '" << path << "'";
  728. }
  729. return blockdata;
  730. }
  731. Map* Project::getMap(QString map_name) {
  732. if (map_cache->contains(map_name)) {
  733. return map_cache->value(map_name);
  734. } else {
  735. Map *map = loadMap(map_name);
  736. return map;
  737. }
  738. }
  739. Tileset* Project::getTileset(QString label) {
  740. if (tileset_cache->contains(label)) {
  741. return tileset_cache->value(label);
  742. } else {
  743. Tileset *tileset = loadTileset(label);
  744. return tileset;
  745. }
  746. }
  747. QString Project::readTextFile(QString path) {
  748. QFile file(path);
  749. if (!file.open(QIODevice::ReadOnly)) {
  750. //QMessageBox::information(0, "Error", QString("Could not open '%1': ").arg(path) + file.errorString());
  751. qDebug() << QString("Could not open '%1': ").arg(path) + file.errorString();
  752. return QString();
  753. }
  754. QTextStream in(&file);
  755. QString text = "";
  756. while (!in.atEnd()) {
  757. text += in.readLine() + "\n";
  758. }
  759. return text;
  760. }
  761. void Project::saveTextFile(QString path, QString text) {
  762. QFile file(path);
  763. if (file.open(QIODevice::WriteOnly)) {
  764. file.write(text.toUtf8());
  765. } else {
  766. qDebug() << QString("Could not open '%1' for writing: ").arg(path) + file.errorString();
  767. }
  768. }
  769. void Project::appendTextFile(QString path, QString text) {
  770. QFile file(path);
  771. if (file.open(QIODevice::Append)) {
  772. file.write(text.toUtf8());
  773. } else {
  774. qDebug() << QString("Could not open '%1' for appending: ").arg(path) + file.errorString();
  775. }
  776. }
  777. void Project::deleteFile(QString path) {
  778. QFile file(path);
  779. if (file.exists() && !file.remove()) {
  780. qDebug() << QString("Could not delete file '%1': ").arg(path) + file.errorString();
  781. }
  782. }
  783. void Project::readMapGroups() {
  784. QString text = readTextFile(root + "/data/maps/groups.inc");
  785. if (text.isNull()) {
  786. return;
  787. }
  788. ParseUtil *parser = new ParseUtil;
  789. QList<QStringList> *commands = parser->parseAsm(text);
  790. bool in_group_pointers = false;
  791. QStringList *groups = new QStringList;
  792. for (int i = 0; i < commands->length(); i++) {
  793. QStringList params = commands->value(i);
  794. QString macro = params.value(0);
  795. if (macro == ".label") {
  796. if (in_group_pointers) {
  797. break;
  798. }
  799. if (params.value(1) == "gMapGroups") {
  800. in_group_pointers = true;
  801. }
  802. } else if (macro == ".4byte") {
  803. if (in_group_pointers) {
  804. for (int j = 1; j < params.length(); j++) {
  805. groups->append(params.value(j));
  806. }
  807. }
  808. }
  809. }
  810. QList<QStringList> groupedMaps;
  811. for (int i = 0; i < groups->length(); i++) {
  812. groupedMaps.append(QStringList());
  813. }
  814. QStringList *maps = new QStringList;
  815. int group = -1;
  816. for (int i = 0; i < commands->length(); i++) {
  817. QStringList params = commands->value(i);
  818. QString macro = params.value(0);
  819. if (macro == ".label") {
  820. group = groups->indexOf(params.value(1));
  821. } else if (macro == ".4byte") {
  822. if (group != -1) {
  823. for (int j = 1; j < params.length(); j++) {
  824. QString mapName = params.value(j);
  825. groupedMaps[group].append(mapName);
  826. maps->append(mapName);
  827. map_groups->insert(mapName, group);
  828. // Build the mapping and reverse mapping between map constants and map names.
  829. QString mapConstant = Map::mapConstantFromName(mapName);
  830. mapConstantsToMapNames->insert(mapConstant, mapName);
  831. mapNamesToMapConstants->insert(mapName, mapConstant);
  832. }
  833. }
  834. }
  835. }
  836. groupNames = groups;
  837. groupedMapNames = groupedMaps;
  838. mapNames = maps;
  839. }
  840. Map* Project::addNewMapToGroup(QString mapName, int groupNum) {
  841. // Setup new map in memory, but don't write to file until map is actually saved later.
  842. mapNames->append(mapName);
  843. map_groups->insert(mapName, groupNum);
  844. groupedMapNames[groupNum].append(mapName);
  845. Map *map = new Map;
  846. map->isPersistedToFile = false;
  847. map->setName(mapName);
  848. mapConstantsToMapNames->insert(map->constantName, map->name);
  849. mapNamesToMapConstants->insert(map->name, map->constantName);
  850. setNewMapHeader(map, mapLayoutsTable.size() + 1);
  851. setNewMapLayout(map);
  852. getTilesets(map);
  853. setNewMapBlockdata(map);
  854. setNewMapBorder(map);
  855. setNewMapEvents(map);
  856. setNewMapConnections(map);
  857. map->commit();
  858. map->history.save();
  859. map_cache->insert(mapName, map);
  860. return map;
  861. }
  862. QString Project::getNewMapName() {
  863. // Ensure default name doesn't already exist.
  864. int i = 0;
  865. QString newMapName;
  866. do {
  867. newMapName = QString("NewMap%1").arg(++i);
  868. } while (mapNames->contains(newMapName));
  869. return newMapName;
  870. }
  871. QList<QStringList>* Project::parseAsm(QString text) {
  872. ParseUtil *parser = new ParseUtil;
  873. return parser->parseAsm(text);
  874. }
  875. QStringList Project::getLocations() {
  876. // TODO
  877. QStringList names;
  878. for (int i = 0; i < 88; i++) {
  879. names.append(QString("%1").arg(i));
  880. }
  881. return names;
  882. }
  883. QStringList Project::getVisibilities() {
  884. // TODO
  885. QStringList names;
  886. for (int i = 0; i < 16; i++) {
  887. names.append(QString("%1").arg(i));
  888. }
  889. return names;
  890. }
  891. QStringList Project::getWeathers() {
  892. // TODO
  893. QStringList names;
  894. for (int i = 0; i < 16; i++) {
  895. names.append(QString("%1").arg(i));
  896. }
  897. return names;
  898. }
  899. QStringList Project::getMapTypes() {
  900. // TODO
  901. QStringList names;
  902. for (int i = 0; i < 16; i++) {
  903. names.append(QString("%1").arg(i));
  904. }
  905. return names;
  906. }
  907. QStringList Project::getBattleScenes() {
  908. // TODO
  909. QStringList names;
  910. for (int i = 0; i < 16; i++) {
  911. names.append(QString("%1").arg(i));
  912. }
  913. return names;
  914. }
  915. void Project::readItemNames() {
  916. QString filepath = root + "/include/constants/items.h";
  917. QStringList prefixes = (QStringList() << "ITEM_");
  918. readCDefinesSorted(filepath, prefixes, itemNames);
  919. }
  920. void Project::readFlagNames() {
  921. QString filepath = root + "/include/constants/flags.h";
  922. QStringList prefixes = (QStringList() << "FLAG_");
  923. readCDefinesSorted(filepath, prefixes, flagNames);
  924. }
  925. void Project::readVarNames() {
  926. QString filepath = root + "/include/constants/vars.h";
  927. QStringList prefixes = (QStringList() << "VAR_");
  928. readCDefinesSorted(filepath, prefixes, varNames);
  929. }
  930. void Project::readCDefinesSorted(QString filepath, QStringList prefixes, QStringList* definesToSet) {
  931. QString text = readTextFile(filepath);
  932. if (!text.isNull()) {
  933. QMap<QString, int> defines = readCDefines(text, prefixes);
  934. // The defines should to be sorted by their underlying value, not alphabetically.
  935. // Reverse the map and read out the resulting keys in order.
  936. QMultiMap<int, QString> definesInverse;
  937. for (QString defineName : defines.keys()) {
  938. definesInverse.insert(defines[defineName], defineName);
  939. }
  940. *definesToSet = definesInverse.values();
  941. }
  942. }
  943. void Project::readMapsWithConnections() {
  944. QString path = root + "/data/maps/connections.inc";
  945. QString text = readTextFile(path);
  946. if (text.isNull()) {
  947. return;
  948. }
  949. mapsWithConnections.clear();
  950. QRegularExpression re("data\\/maps\\/(?<mapName>\\w+)\\/connections.inc");
  951. QList<QStringList>* includes = parseAsm(text);
  952. for (QStringList values : *includes) {
  953. if (values.length() != 2)
  954. continue;
  955. QRegularExpressionMatch match = re.match(values.value(1));
  956. if (match.hasMatch()) {
  957. QString mapName = match.captured("mapName");
  958. mapsWithConnections.append(mapName);
  959. }
  960. }
  961. }
  962. void Project::saveMapsWithConnections() {
  963. QString path = root + "/data/maps/connections.inc";
  964. QString text = "";
  965. for (QString mapName : mapsWithConnections) {
  966. if (mapNamesToMapConstants->contains(mapName)) {
  967. text += QString("\t.include \"data/maps/%1/connections.inc\"\n").arg(mapName);
  968. } else {
  969. qDebug() << QString("Failed to write connection include. %1 not a valid map name").arg(mapName);
  970. }
  971. }
  972. saveTextFile(path, text);
  973. }
  974. QStringList Project::getSongNames() {
  975. QStringList names;
  976. QString text = readTextFile(root + "/include/constants/songs.h");
  977. if (!text.isNull()) {
  978. QStringList songDefinePrefixes;
  979. songDefinePrefixes << "SE_" << "MUS_";
  980. QMap<QString, int> songDefines = readCDefines(text, songDefinePrefixes);
  981. names = songDefines.keys();
  982. }
  983. return names;
  984. }
  985. QMap<QString, int> Project::getEventObjGfxConstants() {
  986. QMap<QString, int> constants;
  987. QString text = readTextFile(root + "/include/constants/event_objects.h");
  988. if (!text.isNull()) {
  989. QStringList eventObjGfxPrefixes;
  990. eventObjGfxPrefixes << "EVENT_OBJ_GFX_";
  991. constants = readCDefines(text, eventObjGfxPrefixes);
  992. }
  993. return constants;
  994. }
  995. QString Project::fixGraphicPath(QString path) {
  996. path = path.replace(QRegExp("\\.lz$"), "");
  997. path = path.replace(QRegExp("\\.[1248]bpp$"), ".png");
  998. return path;
  999. }
  1000. void Project::loadEventPixmaps(QList<Event*> objects) {
  1001. bool needs_update = false;
  1002. for (Event *object : objects) {
  1003. if (object->pixmap.isNull()) {
  1004. needs_update = true;
  1005. break;
  1006. }
  1007. }
  1008. if (!needs_update) {
  1009. return;
  1010. }
  1011. QMap<QString, int> constants = getEventObjGfxConstants();
  1012. QString pointers_text = readTextFile(root + "/src/data/field_event_obj/event_object_graphics_info_pointers.h");
  1013. QString info_text = readTextFile(root + "/src/data/field_event_obj/event_object_graphics_info.h");
  1014. QString pic_text = readTextFile(root + "/src/data/field_event_obj/event_object_pic_tables.h");
  1015. QString assets_text = readTextFile(root + "/src/data/field_event_obj/event_object_graphics.h");
  1016. QStringList pointers = readCArray(pointers_text, "gEventObjectGraphicsInfoPointers");
  1017. for (Event *object : objects) {
  1018. if (!object->pixmap.isNull()) {
  1019. continue;
  1020. }
  1021. QString event_type = object->get("event_type");
  1022. if (event_type == EventType::Object) {
  1023. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(0, 0, 16, 16);
  1024. } else if (event_type == EventType::Warp) {
  1025. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(16, 0, 16, 16);
  1026. } else if (event_type == EventType::CoordScript || event_type == EventType::CoordWeather) {
  1027. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(32, 0, 16, 16);
  1028. } else if (event_type == EventType::Sign || event_type == EventType::HiddenItem || event_type == EventType::SecretBase) {
  1029. object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(48, 0, 16, 16);
  1030. }
  1031. if (event_type == EventType::Object) {
  1032. int sprite_id = constants.value(object->get("sprite"));
  1033. QString info_label = pointers.value(sprite_id).replace("&", "");
  1034. QString pic_label = readCArray(info_text, info_label).value(14);
  1035. QString gfx_label = readCArray(pic_text, pic_label).value(0);
  1036. gfx_label = gfx_label.section(QRegExp("[\\(\\)]"), 1, 1);
  1037. QString path = readCIncbin(assets_text, gfx_label);
  1038. if (!path.isNull()) {
  1039. path = fixGraphicPath(path);
  1040. QPixmap pixmap(root + "/" + path);
  1041. if (!pixmap.isNull()) {
  1042. object->pixmap = pixmap;
  1043. }
  1044. }
  1045. }
  1046. }
  1047. }
  1048. void Project::saveMapEvents(Map *map) {
  1049. QString path = root + QString("/data/maps/%1/events.inc").arg(map->name);
  1050. QString text = "";
  1051. if (map->events["object_event_group"].length() > 0) {
  1052. text += QString("%1::\n").arg(map->object_events_label);
  1053. for (int i = 0; i < map->events["object_event_group"].length(); i++) {
  1054. Event *object_event = map->events["object_event_group"].value(i);
  1055. text += object_event->buildObjectEventMacro(i);
  1056. }
  1057. text += "\n";
  1058. }
  1059. if (map->events["warp_event_group"].length() > 0) {
  1060. text += QString("%1::\n").arg(map->warps_label);
  1061. for (Event *warp : map->events["warp_event_group"]) {
  1062. text += warp->buildWarpEventMacro(mapNamesToMapConstants);
  1063. }
  1064. text += "\n";
  1065. }
  1066. if (map->events["coord_event_group"].length() > 0) {
  1067. text += QString("%1::\n").arg(map->coord_events_label);
  1068. for (Event *event : map->events["coord_event_group"]) {
  1069. QString event_type = event->get("event_type");
  1070. if (event_type == EventType::CoordScript) {
  1071. text += event->buildCoordScriptEventMacro();
  1072. } else if (event_type == EventType::CoordWeather) {
  1073. text += event->buildCoordWeatherEventMacro();
  1074. }
  1075. }
  1076. text += "\n";
  1077. }
  1078. if (map->events["bg_event_group"].length() > 0)
  1079. {
  1080. text += QString("%1::\n").arg(map->bg_events_label);
  1081. for (Event *event : map->events["bg_event_group"]) {
  1082. QString event_type = event->get("event_type");
  1083. if (event_type == EventType::Sign) {
  1084. text += event->buildSignEventMacro();
  1085. } else if (event_type == EventType::HiddenItem) {
  1086. text += event->buildHiddenItemEventMacro();
  1087. } else if (event_type == EventType::SecretBase) {
  1088. text += event->buildSecretBaseEventMacro();
  1089. }
  1090. }
  1091. text += "\n";
  1092. }
  1093. text += QString("%1::\n").arg(map->events_label);
  1094. text += QString("\tmap_events %1, %2, %3, %4\n")
  1095. .arg(map->object_events_label)
  1096. .arg(map->warps_label)
  1097. .arg(map->coord_events_label)
  1098. .arg(map->bg_events_label);
  1099. saveTextFile(path, text);
  1100. }
  1101. void Project::readMapEvents(Map *map) {
  1102. if (!map->isPersistedToFile) {
  1103. return;
  1104. }
  1105. // lazy
  1106. QString path = root + QString("/data/maps/%1/events.inc").arg(map->name);
  1107. QString text = readTextFile(path);
  1108. if (text.isNull()) {
  1109. return;
  1110. }
  1111. QStringList *labels = getLabelValues(parseAsm(text), map->events_label);
  1112. map->object_events_label = labels->value(0);
  1113. map->warps_label = labels->value(1);
  1114. map->coord_events_label = labels->value(2);
  1115. map->bg_events_label = labels->value(3);
  1116. QList<QStringList> *object_events = getLabelMacros(parseAsm(text), map->object_events_label);
  1117. map->events["object_event_group"].clear();
  1118. for (QStringList command : *object_events) {
  1119. if (command.value(0) == "object_event") {
  1120. Event *object = new Event;
  1121. object->put("map_name", map->name);
  1122. int i = 2;
  1123. object->put("sprite", command.value(i++));
  1124. object->put("replacement", command.value(i++));
  1125. object->put("x", command.value(i++).toInt(nullptr, 0));
  1126. object->put("y", command.value(i++).toInt(nullptr, 0));
  1127. object->put("elevation", command.value(i++));
  1128. object->put("behavior", command.value(i++));
  1129. object->put("radius_x", command.value(i++).toInt(nullptr, 0));
  1130. object->put("radius_y", command.value(i++).toInt(nullptr, 0));
  1131. object->put("trainer_see_type", command.value(i++));
  1132. object->put("sight_radius_tree_id", command.value(i++));
  1133. object->put("script_label", command.value(i++));
  1134. object->put("event_flag", command.value(i++));
  1135. object->put("event_group_type", "object_event_group");
  1136. object->put("event_type", EventType::Object);
  1137. map->events["object_event_group"].append(object);
  1138. }
  1139. }
  1140. QList<QStringList> *warps = getLabelMacros(parseAsm(text), map->warps_label);
  1141. map->events["warp_event_group"].clear();
  1142. for (QStringList command : *warps) {
  1143. if (command.value(0) == "warp_def") {
  1144. Event *warp = new Event;
  1145. warp->put("map_name", map->name);
  1146. int i = 1;
  1147. warp->put("x", command.value(i++));
  1148. warp->put("y", command.value(i++));
  1149. warp->put("elevation", command.value(i++));
  1150. warp->put("destination_warp", command.value(i++));
  1151. // Ensure the warp destination map constant is valid before adding it to the warps.
  1152. QString mapConstant = command.value(i++);
  1153. if (mapConstantsToMapNames->contains(mapConstant)) {
  1154. warp->put("destination_map_name", mapConstantsToMapNames->value(mapConstant));
  1155. warp->put("event_group_type", "warp_event_group");
  1156. warp->put("event_type", EventType::Warp);
  1157. map->events["warp_event_group"].append(warp);
  1158. } else {
  1159. qDebug() << QString("Destination map constant '%1' is invalid for warp").arg(mapConstant);
  1160. }
  1161. }
  1162. }
  1163. QList<QStringList> *coords = getLabelMacros(parseAsm(text), map->coord_events_label);
  1164. map->events["coord_event_group"].clear();
  1165. for (QStringList command : *coords) {
  1166. if (command.value(0) == "coord_event") {
  1167. Event *coord = new Event;
  1168. coord->put("map_name", map->name);
  1169. bool old_macro = false;
  1170. if (command.length() >= 9) {
  1171. command.removeAt(7);
  1172. command.removeAt(4);
  1173. old_macro = true;
  1174. }
  1175. int i = 1;
  1176. coord->put("x", command.value(i++));
  1177. coord->put("y", command.value(i++));
  1178. coord->put("elevation", command.value(i++));
  1179. coord->put("script_var", command.value(i++));
  1180. coord->put("script_var_value", command.value(i++));
  1181. coord->put("script_label", command.value(i++));
  1182. //coord_unknown3
  1183. //coord_unknown4
  1184. coord->put("event_group_type", "coord_event_group");
  1185. coord->put("event_type", EventType::CoordScript);
  1186. map->events["coord_event_group"].append(coord);
  1187. } else if (command.value(0) == "coord_weather_event") {
  1188. Event *coord = new Event;
  1189. coord->put("map_name", map->name);
  1190. int i = 1;
  1191. coord->put("x", command.value(i++));
  1192. coord->put("y", command.value(i++));
  1193. coord->put("elevation", command.value(i++));
  1194. coord->put("weather", command.value(i++));
  1195. coord->put("event_group_type", "coord_event_group");
  1196. coord->put("event_type", EventType::CoordWeather);
  1197. map->events["coord_event_group"].append(coord);
  1198. }
  1199. }
  1200. QList<QStringList> *bgs = getLabelMacros(parseAsm(text), map->bg_events_label);
  1201. map->events["bg_event_group"].clear();
  1202. for (QStringList command : *bgs) {
  1203. if (command.value(0) == "bg_event") {
  1204. Event *bg = new Event;
  1205. bg->put("map_name", map->name);
  1206. int i = 1;
  1207. bg->put("x", command.value(i++));
  1208. bg->put("y", command.value(i++));
  1209. bg->put("elevation", command.value(i++));
  1210. bg->put("player_facing_direction", command.value(i++));
  1211. i++;
  1212. bg->put("script_label", command.value(i++));
  1213. //sign_unknown7
  1214. bg->put("event_group_type", "bg_event_group");
  1215. bg->put("event_type", EventType::Sign);
  1216. map->events["bg_event_group"].append(bg);
  1217. } else if (command.value(0) == "bg_hidden_item_event") {
  1218. Event *bg = new Event;
  1219. bg->put("map_name", map->name);
  1220. int i = 1;
  1221. bg->put("x", command.value(i++));
  1222. bg->put("y", command.value(i++));
  1223. bg->put("elevation", command.value(i++));
  1224. bg->put("item", command.value(i++));
  1225. bg->put("flag", command.value(i++));
  1226. bg->put("event_group_type", "bg_event_group");
  1227. bg->put("event_type", EventType::HiddenItem);
  1228. map->events["bg_event_group"].append(bg);
  1229. } else if (command.value(0) == "bg_secret_base_event") {
  1230. Event *bg = new Event;
  1231. bg->put("map_name", map->name);
  1232. int i = 1;
  1233. bg->put("x", command.value(i++));
  1234. bg->put("y", command.value(i++));
  1235. bg->put("elevation", command.value(i++));
  1236. bg->put("secret_base_map", command.value(i++));
  1237. bg->put("event_group_type", "bg_event_group");
  1238. bg->put("event_type", EventType::SecretBase);
  1239. map->events["bg_event_group"].append(bg);
  1240. }
  1241. }
  1242. }
  1243. void Project::setNewMapEvents(Map *map) {
  1244. map->object_events_label = "0x0";
  1245. map->warps_label = "0x0";
  1246. map->coord_events_label = "0x0";
  1247. map->bg_events_label = "0x0";
  1248. map->events["object_event_group"].clear();
  1249. map->events["warp_event_group"].clear();
  1250. map->events["coord_event_group"].clear();
  1251. map->events["bg_event_group"].clear();
  1252. }
  1253. QStringList Project::readCArray(QString text, QString label) {
  1254. QStringList list;
  1255. if (label.isNull()) {
  1256. return list;
  1257. }
  1258. QRegExp *re = new QRegExp(QString("\\b%1\\b\\s*\\[?\\s*\\]?\\s*=\\s*\\{([^\\}]*)\\}").arg(label));
  1259. int pos = re->indexIn(text);
  1260. if (pos != -1) {
  1261. QString body = re->cap(1);
  1262. body = body.replace(QRegExp("\\s*"), "");
  1263. list = body.split(',');
  1264. /*
  1265. QRegExp *inner = new QRegExp("&?\\b([A-Za-z0-9_\\(\\)]*)\\b,");
  1266. int pos = 0;
  1267. while ((pos = inner->indexIn(body, pos)) != -1) {
  1268. list << inner->cap(1);
  1269. pos += inner->matchedLength();
  1270. }
  1271. */
  1272. }
  1273. return list;
  1274. }
  1275. QString Project::readCIncbin(QString text, QString label) {
  1276. QString path;
  1277. if (label.isNull()) {
  1278. return path;
  1279. }
  1280. QRegExp *re = new QRegExp(QString(
  1281. "\\b%1\\b"
  1282. "\\s*\\[?\\s*\\]?\\s*=\\s*"
  1283. "INCBIN_[US][0-9][0-9]?"
  1284. "\\(\"([^\"]*)\"\\)").arg(label));
  1285. int pos = re->indexIn(text);
  1286. if (pos != -1) {
  1287. path = re->cap(1);
  1288. }
  1289. return path;
  1290. }
  1291. QMap<QString, int> Project::readCDefines(QString text, QStringList prefixes) {
  1292. ParseUtil parser;
  1293. QMap<QString, int> allDefines;
  1294. QMap<QString, int> filteredDefines;
  1295. QRegularExpression re("#define\\s+(?<defineName>\\w+)[^\\S\\n]+(?<defineValue>.+)");
  1296. QRegularExpressionMatchIterator iter = re.globalMatch(text);
  1297. while (iter.hasNext()) {
  1298. QRegularExpressionMatch match = iter.next();
  1299. QString name = match.captured("defineName");
  1300. QString expression = match.captured("defineValue");
  1301. expression.replace(QRegularExpression("//.*"), "");
  1302. int value = parser.evaluateDefine(expression, &allDefines);
  1303. allDefines.insert(name, value);
  1304. for (QString prefix : prefixes) {
  1305. if (name.startsWith(prefix)) {
  1306. filteredDefines.insert(name, value);
  1307. }
  1308. }
  1309. }
  1310. return filteredDefines;
  1311. }