No Description

project.cpp 53KB

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