No Description

project.cpp 55KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  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"].length() > 0) {
  1052. text += QString("%1::\n").arg(map->object_events_label);
  1053. for (int i = 0; i < map->events["object"].length(); i++) {
  1054. Event *object_event = map->events["object"].value(i);
  1055. int radius_x = object_event->getInt("radius_x");
  1056. int radius_y = object_event->getInt("radius_y");
  1057. uint16_t x = object_event->getInt("x");
  1058. uint16_t y = object_event->getInt("y");
  1059. text += QString("\tobject_event %1").arg(i + 1);
  1060. text += QString(", %1").arg(object_event->get("sprite"));
  1061. text += QString(", %1").arg(object_event->get("replacement"));
  1062. text += QString(", %1").arg(x);
  1063. text += QString(", %1").arg(y);
  1064. text += QString(", %1").arg(object_event->get("elevation"));
  1065. text += QString(", %1").arg(object_event->get("behavior"));
  1066. text += QString(", %1").arg(radius_x);
  1067. text += QString(", %1").arg(radius_y);
  1068. text += QString(", %1").arg(object_event->get("property"));
  1069. text += QString(", %1").arg(object_event->get("sight_radius"));
  1070. text += QString(", %1").arg(object_event->get("script_label"));
  1071. text += QString(", %1").arg(object_event->get("event_flag"));
  1072. text += "\n";
  1073. }
  1074. text += "\n";
  1075. }
  1076. if (map->events["warp"].length() > 0) {
  1077. text += QString("%1::\n").arg(map->warps_label);
  1078. for (Event *warp : map->events["warp"]) {
  1079. text += QString("\twarp_def %1").arg(warp->get("x"));
  1080. text += QString(", %1").arg(warp->get("y"));
  1081. text += QString(", %1").arg(warp->get("elevation"));
  1082. text += QString(", %1").arg(warp->get("destination_warp"));
  1083. text += QString(", %1").arg(mapNamesToMapConstants->value(warp->get("destination_map_name")));
  1084. text += "\n";
  1085. }
  1086. text += "\n";
  1087. }
  1088. if (map->events["trap"].length() + map->events["trap_weather"].length() > 0) {
  1089. text += QString("%1::\n").arg(map->coord_events_label);
  1090. for (Event *coords : map->events["trap"]) {
  1091. text += QString("\tcoord_event %1").arg(coords->get("x"));
  1092. text += QString(", %1").arg(coords->get("y"));
  1093. text += QString(", %1").arg(coords->get("elevation"));
  1094. text += QString(", 0");
  1095. text += QString(", %1").arg(coords->get("script_var"));
  1096. text += QString(", %1").arg(coords->get("script_var_value"));
  1097. text += QString(", 0");
  1098. text += QString(", %1").arg(coords->get("script_label"));
  1099. text += "\n";
  1100. }
  1101. for (Event *coords : map->events["trap_weather"]) {
  1102. text += QString("\tcoord_weather_event %1").arg(coords->get("x"));
  1103. text += QString(", %1").arg(coords->get("y"));
  1104. text += QString(", %1").arg(coords->get("elevation"));
  1105. text += QString(", %1").arg(coords->get("weather"));
  1106. text += "\n";
  1107. }
  1108. text += "\n";
  1109. }
  1110. if (map->events["sign"].length() +
  1111. map->events["event_hidden_item"].length() +
  1112. map->events["event_secret_base"].length() > 0)
  1113. {
  1114. text += QString("%1::\n").arg(map->bg_events_label);
  1115. for (Event *sign : map->events["sign"]) {
  1116. text += QString("\tbg_event %1").arg(sign->get("x"));
  1117. text += QString(", %1").arg(sign->get("y"));
  1118. text += QString(", %1").arg(sign->get("elevation"));
  1119. text += QString(", %1").arg(sign->get("type"));
  1120. text += QString(", 0");
  1121. text += QString(", %1").arg(sign->get("script_label"));
  1122. text += "\n";
  1123. }
  1124. for (Event *item : map->events["event_hidden_item"]) {
  1125. text += QString("\tbg_hidden_item_event %1").arg(item->get("x"));
  1126. text += QString(", %1").arg(item->get("y"));
  1127. text += QString(", %1").arg(item->get("elevation"));
  1128. text += QString(", %1").arg(item->get("item"));
  1129. text += QString(", %1").arg(item->get("flag"));
  1130. text += "\n";
  1131. }
  1132. for (Event *item : map->events["event_secret_base"]) {
  1133. text += QString("\tbg_secret_base_event %1").arg(item->get("x"));
  1134. text += QString(", %1").arg(item->get("y"));
  1135. text += QString(", %1").arg(item->get("elevation"));
  1136. text += QString(", %1").arg(item->get("secret_base_map"));
  1137. text += "\n";
  1138. }
  1139. text += "\n";
  1140. }
  1141. text += QString("%1::\n").arg(map->events_label);
  1142. text += QString("\tmap_events %1, %2, %3, %4\n")
  1143. .arg(map->object_events_label)
  1144. .arg(map->warps_label)
  1145. .arg(map->coord_events_label)
  1146. .arg(map->bg_events_label);
  1147. saveTextFile(path, text);
  1148. }
  1149. void Project::readMapEvents(Map *map) {
  1150. if (!map->isPersistedToFile) {
  1151. return;
  1152. }
  1153. // lazy
  1154. QString path = root + QString("/data/maps/%1/events.inc").arg(map->name);
  1155. QString text = readTextFile(path);
  1156. if (text.isNull()) {
  1157. return;
  1158. }
  1159. QStringList *labels = getLabelValues(parseAsm(text), map->events_label);
  1160. map->object_events_label = labels->value(0);
  1161. map->warps_label = labels->value(1);
  1162. map->coord_events_label = labels->value(2);
  1163. map->bg_events_label = labels->value(3);
  1164. QList<QStringList> *object_events = getLabelMacros(parseAsm(text), map->object_events_label);
  1165. map->events["object"].clear();
  1166. for (QStringList command : *object_events) {
  1167. if (command.value(0) == "object_event") {
  1168. Event *object = new Event;
  1169. object->put("map_name", map->name);
  1170. int i = 2;
  1171. object->put("sprite", command.value(i++));
  1172. object->put("replacement", command.value(i++));
  1173. object->put("x", command.value(i++).toInt(nullptr, 0));
  1174. object->put("y", command.value(i++).toInt(nullptr, 0));
  1175. object->put("elevation", command.value(i++));
  1176. object->put("behavior", command.value(i++));
  1177. object->put("radius_x", command.value(i++).toInt(nullptr, 0));
  1178. object->put("radius_y", command.value(i++).toInt(nullptr, 0));
  1179. object->put("property", command.value(i++));
  1180. object->put("sight_radius", command.value(i++));
  1181. object->put("script_label", command.value(i++));
  1182. object->put("event_flag", command.value(i++));
  1183. object->put("event_type", EventType::Object);
  1184. map->events["object"].append(object);
  1185. }
  1186. }
  1187. QList<QStringList> *warps = getLabelMacros(parseAsm(text), map->warps_label);
  1188. map->events["warp"].clear();
  1189. for (QStringList command : *warps) {
  1190. if (command.value(0) == "warp_def") {
  1191. Event *warp = new Event;
  1192. warp->put("map_name", map->name);
  1193. int i = 1;
  1194. warp->put("x", command.value(i++));
  1195. warp->put("y", command.value(i++));
  1196. warp->put("elevation", command.value(i++));
  1197. warp->put("destination_warp", command.value(i++));
  1198. // Ensure the warp destination map constant is valid before adding it to the warps.
  1199. QString mapConstant = command.value(i++);
  1200. if (mapConstantsToMapNames->contains(mapConstant)) {
  1201. warp->put("destination_map_name", mapConstantsToMapNames->value(mapConstant));
  1202. warp->put("event_type", EventType::Warp);
  1203. map->events["warp"].append(warp);
  1204. } else {
  1205. qDebug() << QString("Destination map constant '%1' is invalid for warp").arg(mapConstant);
  1206. }
  1207. }
  1208. }
  1209. QList<QStringList> *coords = getLabelMacros(parseAsm(text), map->coord_events_label);
  1210. map->events["trap"].clear();
  1211. map->events["trap_weather"].clear();
  1212. for (QStringList command : *coords) {
  1213. if (command.value(0) == "coord_event") {
  1214. Event *coord = new Event;
  1215. coord->put("map_name", map->name);
  1216. bool old_macro = false;
  1217. if (command.length() >= 9) {
  1218. command.removeAt(7);
  1219. command.removeAt(4);
  1220. old_macro = true;
  1221. }
  1222. int i = 1;
  1223. coord->put("x", command.value(i++));
  1224. coord->put("y", command.value(i++));
  1225. coord->put("elevation", command.value(i++));
  1226. coord->put("script_var", command.value(i++));
  1227. coord->put("script_var_value", command.value(i++));
  1228. coord->put("script_label", command.value(i++));
  1229. //coord_unknown3
  1230. //coord_unknown4
  1231. coord->put("event_type", EventType::CoordScript);
  1232. map->events["trap"].append(coord);
  1233. } else if (command.value(0) == "coord_weather_event") {
  1234. Event *coord = new Event;
  1235. coord->put("map_name", map->name);
  1236. int i = 1;
  1237. coord->put("x", command.value(i++));
  1238. coord->put("y", command.value(i++));
  1239. coord->put("elevation", command.value(i++));
  1240. coord->put("weather", command.value(i++));
  1241. coord->put("event_type", EventType::CoordWeather);
  1242. map->events["trap_weather"].append(coord);
  1243. }
  1244. }
  1245. QList<QStringList> *bgs = getLabelMacros(parseAsm(text), map->bg_events_label);
  1246. map->events["sign"].clear();
  1247. map->events["event_hidden_item"].clear();
  1248. map->events["event_secret_base"].clear();
  1249. for (QStringList command : *bgs) {
  1250. if (command.value(0) == "bg_event") {
  1251. Event *bg = new Event;
  1252. bg->put("map_name", map->name);
  1253. int i = 1;
  1254. bg->put("x", command.value(i++));
  1255. bg->put("y", command.value(i++));
  1256. bg->put("elevation", command.value(i++));
  1257. bg->put("type", command.value(i++));
  1258. i++;
  1259. bg->put("script_label", command.value(i++));
  1260. //sign_unknown7
  1261. bg->put("event_type", EventType::Sign);
  1262. map->events["sign"].append(bg);
  1263. } else if (command.value(0) == "bg_hidden_item_event") {
  1264. Event *bg = new Event;
  1265. bg->put("map_name", map->name);
  1266. int i = 1;
  1267. bg->put("x", command.value(i++));
  1268. bg->put("y", command.value(i++));
  1269. bg->put("elevation", command.value(i++));
  1270. bg->put("item", command.value(i++));
  1271. bg->put("flag", command.value(i++));
  1272. bg->put("event_type", EventType::HiddenItem);
  1273. map->events["event_hidden_item"].append(bg);
  1274. } else if (command.value(0) == "bg_secret_base_event") {
  1275. Event *bg = new Event;
  1276. bg->put("map_name", map->name);
  1277. int i = 1;
  1278. bg->put("x", command.value(i++));
  1279. bg->put("y", command.value(i++));
  1280. bg->put("elevation", command.value(i++));
  1281. bg->put("secret_base_map", command.value(i++));
  1282. bg->put("event_type", EventType::SecretBase);
  1283. map->events["event_secret_base"].append(bg);
  1284. }
  1285. }
  1286. }
  1287. void Project::setNewMapEvents(Map *map) {
  1288. map->object_events_label = "0x0";
  1289. map->warps_label = "0x0";
  1290. map->coord_events_label = "0x0";
  1291. map->bg_events_label = "0x0";
  1292. map->events["object"].clear();
  1293. map->events["warp"].clear();
  1294. map->events["trap"].clear();
  1295. map->events["trap_weather"].clear();
  1296. map->events["sign"].clear();
  1297. map->events["event_hidden_item"].clear();
  1298. map->events["event_secret_base"].clear();
  1299. }
  1300. QStringList Project::readCArray(QString text, QString label) {
  1301. QStringList list;
  1302. if (label.isNull()) {
  1303. return list;
  1304. }
  1305. QRegExp *re = new QRegExp(QString("\\b%1\\b\\s*\\[?\\s*\\]?\\s*=\\s*\\{([^\\}]*)\\}").arg(label));
  1306. int pos = re->indexIn(text);
  1307. if (pos != -1) {
  1308. QString body = re->cap(1);
  1309. body = body.replace(QRegExp("\\s*"), "");
  1310. list = body.split(',');
  1311. /*
  1312. QRegExp *inner = new QRegExp("&?\\b([A-Za-z0-9_\\(\\)]*)\\b,");
  1313. int pos = 0;
  1314. while ((pos = inner->indexIn(body, pos)) != -1) {
  1315. list << inner->cap(1);
  1316. pos += inner->matchedLength();
  1317. }
  1318. */
  1319. }
  1320. return list;
  1321. }
  1322. QString Project::readCIncbin(QString text, QString label) {
  1323. QString path;
  1324. if (label.isNull()) {
  1325. return path;
  1326. }
  1327. QRegExp *re = new QRegExp(QString(
  1328. "\\b%1\\b"
  1329. "\\s*\\[?\\s*\\]?\\s*=\\s*"
  1330. "INCBIN_[US][0-9][0-9]?"
  1331. "\\(\"([^\"]*)\"\\)").arg(label));
  1332. int pos = re->indexIn(text);
  1333. if (pos != -1) {
  1334. path = re->cap(1);
  1335. }
  1336. return path;
  1337. }
  1338. QMap<QString, int> Project::readCDefines(QString text, QStringList prefixes) {
  1339. ParseUtil parser;
  1340. QMap<QString, int> allDefines;
  1341. QMap<QString, int> filteredDefines;
  1342. QRegularExpression re("#define\\s+(?<defineName>\\w+)[^\\S\\n]+(?<defineValue>.+)");
  1343. QRegularExpressionMatchIterator iter = re.globalMatch(text);
  1344. while (iter.hasNext()) {
  1345. QRegularExpressionMatch match = iter.next();
  1346. QString name = match.captured("defineName");
  1347. QString expression = match.captured("defineValue");
  1348. expression.replace(QRegularExpression("//.*"), "");
  1349. int value = parser.evaluateDefine(expression, &allDefines);
  1350. allDefines.insert(name, value);
  1351. for (QString prefix : prefixes) {
  1352. if (name.startsWith(prefix)) {
  1353. filteredDefines.insert(name, value);
  1354. }
  1355. }
  1356. }
  1357. return filteredDefines;
  1358. }