FieldDisassembler.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include "decompiler/Disassembler.h"
  17. #include "decompiler/sudm.h"
  18. #include <array>
  19. class BinaryReader;
  20. class Function;
  21. namespace FF7{
  22. /**
  23. * Field opcodes.
  24. */
  25. enum OPCODES{
  26. RET = 0x0,
  27. REQ = 0x01,
  28. REQSW = 0x02,
  29. REQEW = 0x03,
  30. PREQ = 0x04,
  31. PRQSW = 0x05,
  32. PRQEW = 0x06,
  33. RETTO = 0x07,
  34. JOIN = 0x08,
  35. SPLIT = 0x09,
  36. SPTYE = 0x0A,
  37. GTPYE = 0x0B,
  38. DSKCG = 0x0E,
  39. SPECIAL = 0x0F,
  40. JMPF = 0x10,
  41. JMPFL = 0x11,
  42. JMPB = 0x12,
  43. JMPBL = 0x13,
  44. IFUB = 0x14,
  45. IFUBL = 0x15,
  46. IFSW = 0x16,
  47. IFSWL = 0x17,
  48. IFUW = 0x18,
  49. IFUWL = 0x19,
  50. MINIGAME = 0x20,
  51. TUTOR = 0x21,
  52. BTMD2 = 0x22,
  53. BTRLD = 0x23,
  54. WAIT = 0x24,
  55. NFADE = 0x25,
  56. BLINK = 0x26,
  57. BGMOVIE = 0x27,
  58. KAWAI = 0x28,
  59. KAWIW = 0x29,
  60. PMOVA = 0x2A,
  61. SLIP = 0x2B,
  62. BGPDH = 0x2C,
  63. BGSCR = 0x2D,
  64. WCLS = 0x2E,
  65. WSIZW = 0x2F,
  66. IFKEY = 0x30,
  67. IFKEYON = 0x31,
  68. IFKEYOFF = 0x32,
  69. UC = 0x33,
  70. PDIRA = 0x34,
  71. PTURA = 0x35,
  72. WSPCL = 0x36,
  73. WNUMB = 0x37,
  74. STTIM = 0x38,
  75. GOLDU = 0x39,
  76. GOLDD = 0x3A,
  77. CHGLD = 0x3B,
  78. HMPMAX1 = 0x3C,
  79. HMPMAX2 = 0x3D,
  80. MHMMX = 0x3E,
  81. HMPMAX3 = 0x3F,
  82. MESSAGE = 0x40,
  83. MPARA = 0x41,
  84. MPRA2 = 0x42,
  85. MPNAM = 0x43,
  86. MPU = 0x45,
  87. MPD = 0x47,
  88. ASK = 0x48,
  89. MENU = 0x49,
  90. MENU2 = 0x4A,
  91. BTLTB = 0x4B,
  92. HPU = 0x4D,
  93. HPD = 0x4F,
  94. WINDOW = 0x50,
  95. WMOVE = 0x51,
  96. WMODE = 0x52,
  97. WREST = 0x53,
  98. WCLSE = 0x54,
  99. WROW = 0x55,
  100. GWCOL = 0x56,
  101. SWCOL = 0x57,
  102. STITM = 0x58,
  103. DLITM = 0x59,
  104. CKITM = 0x5A,
  105. SMTRA = 0x5B,
  106. DMTRA = 0x5C,
  107. CMTRA = 0x5D,
  108. SHAKE = 0x5E,
  109. NOP = 0x5F,
  110. MAPJUMP = 0x60,
  111. SCRLO = 0x61,
  112. SCRLC = 0x62,
  113. SCRLA = 0x63,
  114. SCR2D = 0x64,
  115. SCRCC = 0x65,
  116. SCR2DC = 0x66,
  117. SCRLW = 0x67,
  118. SCR2DL = 0x68,
  119. MPDSP = 0x69,
  120. VWOFT = 0x6A,
  121. FADE = 0x6B,
  122. FADEW = 0x6C,
  123. IDLCK = 0x6D,
  124. LSTMP = 0x6E,
  125. SCRLP = 0x6F,
  126. BATTLE = 0x70,
  127. BTLON = 0x71,
  128. BTLMD = 0x72,
  129. PGTDR = 0x73,
  130. GETPC = 0x74,
  131. PXYZI = 0x75,
  132. PLUS_ = 0x76, // PLUS!
  133. PLUS2_ = 0x77, // PLUS2!
  134. MINUS_ = 0x78, // MINUS!
  135. MINUS2_ = 0x79, // MINUS2!
  136. INC_ = 0x7A, // INC!
  137. INC2_ = 0x7B, // INC2!
  138. DEC_ = 0x7C, // DEC!
  139. DEC2_ = 0x7D, // DEC2!
  140. TLKON = 0x7E,
  141. RDMSD = 0x7F,
  142. SETBYTE = 0x80,
  143. SETWORD = 0x81,
  144. BITON = 0x82,
  145. BITOFF = 0x83,
  146. BITXOR = 0x84,
  147. PLUS = 0x85,
  148. PLUS2 = 0x86,
  149. MINUS = 0x87,
  150. MINUS2 = 0x88,
  151. MUL = 0x89,
  152. MUL2 = 0x8A,
  153. DIV = 0x8B,
  154. DIV2 = 0x8C,
  155. MOD = 0x8D,
  156. MOD2 = 0x8E,
  157. AND = 0x8F,
  158. AND2 = 0x90,
  159. OR = 0x91,
  160. OR2 = 0x92,
  161. XOR = 0x93,
  162. XOR2 = 0x94,
  163. INC = 0x95,
  164. INC2 = 0x96,
  165. DEC = 0x97,
  166. DEC2 = 0x98,
  167. RANDOM = 0x99,
  168. LBYTE = 0x9A,
  169. HBYTE = 0x9B,
  170. TWOBYTE = 0x9C,
  171. SETX = 0x9D,
  172. GETX = 0x9E,
  173. SEARCHX = 0x9F,
  174. PC = 0xA0,
  175. opCodeCHAR = 0xA1,
  176. DFANM = 0xA2,
  177. ANIME1 = 0xA3,
  178. VISI = 0xA4,
  179. XYZI = 0xA5,
  180. XYI = 0xA6,
  181. XYZ = 0xA7,
  182. MOVE = 0xA8,
  183. CMOVE = 0xA9,
  184. MOVA = 0xAA,
  185. TURA = 0xAB,
  186. ANIMW = 0xAC,
  187. FMOVE = 0xAD,
  188. ANIME2 = 0xAE,
  189. ANIM_1 = 0xAF, // ANIM!1
  190. CANIM1 = 0xB0,
  191. CANM_1 = 0xB1, // CANM!1
  192. MSPED = 0xB2,
  193. DIR = 0xB3,
  194. TURNGEN = 0xB4,
  195. TURN = 0xB5,
  196. DIRA = 0xB6,
  197. GETDIR = 0xB7,
  198. GETAXY = 0xB8,
  199. GETAI = 0xB9,
  200. ANIM_2 = 0xBA, // ANIM!2
  201. CANIM2 = 0xBB,
  202. CANM_2 = 0xBC, // CANM!2
  203. ASPED = 0xBD,
  204. CC = 0xBF,
  205. JUMP = 0xC0,
  206. AXYZI = 0xC1,
  207. LADER = 0xC2,
  208. OFST = 0xC3,
  209. OFSTW = 0xC4,
  210. TALKR = 0xC5,
  211. SLIDR = 0xC6,
  212. SOLID = 0xC7,
  213. PRTYP = 0xC8,
  214. PRTYM = 0xC9,
  215. PRTYE = 0xCA,
  216. IFPRTYQ = 0xCB,
  217. IFMEMBQ = 0xCC,
  218. MMBUD = 0xCD,
  219. MMBLK = 0xCE,
  220. MMBUK = 0xCF,
  221. LINE = 0xD0,
  222. LINON = 0xD1,
  223. MPJPO = 0xD2,
  224. SLINE = 0xD3,
  225. SIN = 0xD4,
  226. COS = 0xD5,
  227. TLKR2 = 0xD6,
  228. SLDR2 = 0xD7,
  229. PMJMP = 0xD8,
  230. PMJMP2 = 0xD9,
  231. AKAO2 = 0xDA,
  232. FCFIX = 0xDB,
  233. CCANM = 0xDC,
  234. ANIMB = 0xDD,
  235. TURNW = 0xDE,
  236. MPPAL = 0xDF,
  237. BGON = 0xE0,
  238. BGOFF = 0xE1,
  239. BGROL = 0xE2,
  240. BGROL2 = 0xE3,
  241. BGCLR = 0xE4,
  242. STPAL = 0xE5,
  243. LDPAL = 0xE6,
  244. CPPAL = 0xE7,
  245. RTPAL = 0xE8,
  246. ADPAL = 0xE9,
  247. MPPAL2 = 0xEA,
  248. STPLS = 0xEB,
  249. LDPLS = 0xEC,
  250. CPPAL2 = 0xED,
  251. RTPAL2 = 0xEE,
  252. ADPAL2 = 0xEF,
  253. MUSIC = 0xF0,
  254. SOUND = 0xF1,
  255. AKAO = 0xF2,
  256. MUSVT = 0xF3,
  257. MUSVM = 0xF4,
  258. MULCK = 0xF5,
  259. BMUSC = 0xF6,
  260. CHMPH = 0xF7,
  261. PMVIE = 0xF8,
  262. MOVIE = 0xF9,
  263. MVIEF = 0xFA,
  264. MVCAM = 0xFB,
  265. FMUSC = 0xFC,
  266. CMUSC = 0xFD,
  267. CHMST = 0xFE,
  268. GAMEOVER = 0xFF
  269. };
  270. /**
  271. * SPECIAL opcodes.
  272. *
  273. * SPECIAL opcode is a special one. It's a multi-purpose opcode, used to
  274. * perform game-specific tasks. It has a sub-opcode, in the next address,
  275. * that determines the operation to perform. This is a list of those
  276. * sub-opcodes.
  277. */
  278. enum OPCODES_SPECIAL{
  279. ARROW = 0xF5,
  280. PNAME = 0xF6,
  281. GMSPD = 0xF7,
  282. SMSPD = 0xF8,
  283. FLMAT = 0xF9,
  284. FLITM = 0xFA,
  285. BTLCK = 0xFB,
  286. MVLCK = 0xFC,
  287. SPCNM = 0xFD,
  288. RSGLB = 0xFE,
  289. CLITM = 0xFF
  290. };
  291. /**
  292. * KAWAI opcodes.
  293. *
  294. * KAWAI opcode is a special one. It's a multi-purpose opcode, used to
  295. * perform graphics related tasks. It has a sub-opcode, in the next
  296. * address, that determines the operation to perform. This is a list of
  297. * those sub-opcodes.
  298. */
  299. enum OPCODES_KAWAI{
  300. EYETX = 0x00,
  301. TRNSP = 0x01,
  302. AMBNT = 0x02,
  303. Unknown03 = 0x03, // Makou Reactor can write these unknowns.
  304. Unknown04 = 0x04,
  305. Unknown05 = 0x05,
  306. LIGHT = 0x06,
  307. Unknown07 = 0x07,
  308. Unknown08 = 0x08,
  309. Unknown09 = 0x09,
  310. SBOBJ = 0x0A,
  311. Unknown0B = 0x0B,
  312. Unknown0C = 0x0C,
  313. SHINE = 0x0D,
  314. RESET = 0xFF
  315. };
  316. class FieldEngine;
  317. /**
  318. * A disassebler for field maps.
  319. */
  320. class FieldDisassembler : public Disassembler{
  321. public:
  322. /**
  323. * Information about an instruction.
  324. */
  325. struct InstructionRecord{
  326. /**
  327. * The size, in bytes, of the opcode.
  328. */
  329. unsigned char opcode_size;
  330. /**
  331. * The opcode code.
  332. */
  333. unsigned int opcode;
  334. /**
  335. * The opcode short name (mnemonic).
  336. */
  337. const char* opcode_name;
  338. /**
  339. * Format of the opcode arguments.
  340. */
  341. const char* argument_format;
  342. /**
  343. * @todo Document.
  344. */
  345. std::function<InstPtr()> factory_function;
  346. };
  347. /**
  348. * A list of flow control opcodes needed by the disassembler.
  349. */
  350. static InstructionRecord FLOW_OPCODES[];
  351. /**
  352. * Generates a map of control flow instructions.
  353. *
  354. * @return A map of control flow instructions, by opcode name.
  355. */
  356. static std::map<std::string, const InstructionRecord*> FieldInstructions();
  357. /**
  358. * Finds the ID of the character a function refers to.
  359. *
  360. * @param start_addr[in] The starting address of the function.
  361. * @param start_addr[in] The starting address of the function.
  362. * @param end_addr[in] The ending address of the instruction.
  363. * @param insts[in] The lsit of instructions to search.
  364. * @return The ID of the character the function refers to, or -1
  365. * if the function doesn't belong to a character.
  366. */
  367. static int FindId(uint32 start_addr, uint32 end_addr, const InstVec& insts);
  368. /**
  369. * Constructor.
  370. *
  371. * @param formatter[in] The code formatter.
  372. * @param engine[in] The engine to use to disassemble.
  373. * @param insts[in] The list of instructions.
  374. */
  375. FieldDisassembler(
  376. SUDM::IScriptFormatter& formatter, FieldEngine* engine, InstVec& insts
  377. );
  378. /**
  379. * Constructor.
  380. *
  381. * @param formatter[in] The code formatter.
  382. * @param engine[in] The engine to use to disassemble.
  383. * @param insts[in] The list of instructions.
  384. * @param raw_script_data[in] @todo Understand and document.
  385. */
  386. FieldDisassembler(
  387. SUDM::IScriptFormatter& formatter, FieldEngine* engine,
  388. InstVec& insts, const std::vector<unsigned char>& raw_script_data
  389. );
  390. /**
  391. * Destructor.
  392. */
  393. ~FieldDisassembler();
  394. /**
  395. * Retrieves the scale factor for the field.
  396. */
  397. float GetScaleFactor() const;
  398. /**
  399. * Disassembles the instructions.
  400. */
  401. virtual void DoDisassemble() override;
  402. private:
  403. /**
  404. * Field file identification magic number.
  405. */
  406. static const int MAGIC;
  407. /**
  408. * The number of sections in a field.
  409. */
  410. static const int NUM_SECTIONS;
  411. /**
  412. * The sections of a field.
  413. */
  414. enum SECTIONS{
  415. /**
  416. * The scripts section.
  417. */
  418. SCRIPT = 0,
  419. /**
  420. * The walkmesh section.
  421. */
  422. WALKMESH = 1,
  423. /**
  424. * The tiles section.
  425. */
  426. TILE_MAP = 2,
  427. /**
  428. * The camera section.
  429. */
  430. CAMERA_MATRIX = 3,
  431. /**
  432. * The triggers section.
  433. */
  434. TRIGGERS = 4,
  435. /**
  436. * Encounter data section.
  437. */
  438. ENCOUNTERS = 5,
  439. /**
  440. * Field models section.
  441. */
  442. MODELS = 6
  443. };
  444. /**
  445. * Info about a script.
  446. */
  447. struct ScriptInfo{
  448. /**
  449. * Address of the script entry point.
  450. */
  451. uint16 entry_point;
  452. /**
  453. * Address of the next script entry point.
  454. *
  455. * I.e. The last address of the script, plus one.
  456. */
  457. uint32 next_entry_point;
  458. /**
  459. * Script index.
  460. */
  461. size_t index;
  462. };
  463. /**
  464. * A script header.
  465. */
  466. struct ScriptHeader{
  467. /**
  468. * Field file identification magic number.
  469. */
  470. uint16 magic;
  471. /**
  472. * The number of entities in the field.
  473. */
  474. uint8 number_of_entities;
  475. /**
  476. * The number of models in the field.
  477. */
  478. uint8 number_of_models;
  479. /**
  480. * Offset at which the field text strings begin.
  481. */
  482. uint16 offset_to_strings;
  483. /**
  484. * The number of Akao/tuto blocks/offsets.
  485. */
  486. uint16 number_of_akao_offsets;
  487. /**
  488. * Scale of field.
  489. *
  490. * For move and talk calculation (9bit fixed point).
  491. */
  492. uint16 scale;
  493. /**
  494. * Blank data, unused.
  495. */
  496. std::array<uint16, 3> blank;
  497. /**
  498. * Field creator name.
  499. */
  500. std::array<char, 8> creator;
  501. /**
  502. * The field name.
  503. */
  504. std::array<char, 8> name;
  505. /**
  506. * The list of entity names.
  507. *
  508. * There will be {@see number_of_entities} entities.
  509. */
  510. std::vector<std::array<char, 8>> field_entity_names;
  511. /**
  512. * Akao/Tuto block offsets.
  513. *
  514. * There will be {@see number_of_akao_offsets} offsets.
  515. */
  516. std::vector<uint32> akao_offsets;
  517. /**
  518. * Entity script entry points.
  519. *
  520. * More explicitly, subroutine offsets. There will be
  521. * {@see number_of_entities} entity scripts.
  522. */
  523. std::vector<std::array<uint16, 32>> entity_scripts;
  524. /**
  525. * Reads the header.
  526. *
  527. * @param reader[in] The reader used to read the header.
  528. * @throws ScriptHeaderInvalidException If the magic number for
  529. * the field is wrong.
  530. */
  531. void Read(BinaryReader& reader){
  532. magic = reader.ReadU16();
  533. if (magic != MAGIC) throw ScriptHeaderInvalidException();
  534. number_of_entities = reader.ReadU8();
  535. number_of_models = reader.ReadU8();
  536. offset_to_strings = reader.ReadU16();
  537. number_of_akao_offsets = reader.ReadU16();
  538. scale = reader.ReadU16();
  539. for (int i = 0; i < 3; i ++) blank[i] = reader.ReadU16();
  540. for (int i = 0; i < 8; i ++) creator[i] = reader.ReadU8();
  541. for (int i = 0; i < 8; i ++) name[i] = reader.ReadU8();
  542. for (int i = 0; i < number_of_entities; i ++){
  543. std::array<char, 8> name;
  544. for (int j = 0; j < 8; j ++) name[j] = reader.ReadU8();
  545. field_entity_names.emplace_back(name);
  546. }
  547. for (int i = 0; i < number_of_akao_offsets; i ++)
  548. akao_offsets.emplace_back(reader.ReadU32());
  549. entity_scripts.reserve(number_of_entities);
  550. for (int i = 0; i < number_of_entities; i ++){
  551. std::array<uint16, 32> scripts;
  552. for (int j = 0; j < 32; j ++) scripts[j] = reader.ReadU16();
  553. entity_scripts.push_back(scripts);
  554. }
  555. }
  556. };
  557. /**
  558. * Open a file for disassembly.
  559. *
  560. * @param filename[in] The file to disassemble.
  561. * @deprecated Do not use. Load the instructions from the
  562. * constructor.
  563. */
  564. virtual void Open(const char *filename) override;
  565. /**
  566. * Disassembles a script (function).
  567. *
  568. * @param entity_name[in] Name of the entity the script belongs
  569. * to.
  570. * @param entity_index[in] Index of the entity the script belongs
  571. * to.
  572. * @param script_entry_point[in] Address of the first instruction
  573. * of the script to disassemble.
  574. * @param script index[in] Index of the script, relative to the
  575. * entity scripts.
  576. * @param next_script_entry_point[in] Address of the first
  577. * instruction of the script next to the one to disassemble (i.e.
  578. * the last address of the script to disassemble, plus one).
  579. * @param is_start[in] Indicates if the script is the first one
  580. * for it's entity.
  581. * @param is_end[in] Indicates if the script is the last one for
  582. * it's entity.
  583. * @throws DecompilerException for malformed scripts.
  584. */
  585. void DisassembleIndivdualScript(
  586. std::string entity_name, size_t entity_index, size_t script_index,
  587. size_t script_entry_point, uint32 next_script_entry_point, bool is_start, bool is_end
  588. );
  589. /**
  590. * Adds a function to the engine.
  591. *
  592. * @param entity_name[in] Name of the entity that owns the function.
  593. * @param entity_index[in] Index of the entity that owns the function.
  594. * @param script_index[in] Index of the script within the entity.
  595. * @param next_script_entry_point[in] Start position of the next
  596. * script (or the ending position of this one, plus one).
  597. * @param is_start[in] @todo Understand and document.
  598. * @param is_end[in] @todo Understand and document.
  599. * @param to_return_only[in] True to read the script only until the
  600. * first return.
  601. * @param func_name[in] Name of the function. If the entity is a line,
  602. * the provided name will be overridden and set to the standard line
  603. * entity script names.
  604. */
  605. void AddFunc(
  606. std::string entity_name, size_t entity_index, size_t script_index,
  607. uint32 next_script_entry_point, const bool is_start, const bool is_end,
  608. bool to_return_only, std::string func_name
  609. );
  610. /**
  611. * Reads opcodes from a script, and detects lines.
  612. *
  613. * It stops when it reaches the ending position or a RET opcode,
  614. * whatever comes first. It also detects if one of the read opcodes
  615. * LINE, which means that the entity is a line
  616. *
  617. * @param end_pos[in] The last position to read. Opcodes will be read
  618. * until this, or until a RET is found.
  619. * @param point_a[out] If a LINE opcode is found, the first point will
  620. * be saved here.
  621. * @param point_b[out] If a LINE opcode is found, the second point
  622. * will be saved here.
  623. * @return True if LINE opcode found, false if not.
  624. */
  625. bool ReadOpCodesToPositionOrReturn(
  626. size_t end_pos, std::vector<float>& point_a, std::vector<float>& point_b
  627. );
  628. /**
  629. * Initializes a function for an entity.
  630. *
  631. * @param script_index[in] Index of the script.
  632. */
  633. std::unique_ptr<Function> StartFunction(size_t scriptIndex);
  634. /**
  635. * Reads the header.
  636. */
  637. void ReadHeader();
  638. /**
  639. * Reads the header.
  640. *
  641. * @param reader[in] Reader to use to read.
  642. */
  643. void ReadHeader(BinaryReader& reader);
  644. /**
  645. * Retrieves the offset of the end of a script.
  646. *
  647. * @param cur_entry_point[in] @todo Understand and document.
  648. * @param entity_index[in] Index of the entity the script belongs to.
  649. * @param script_index[in] The script index.
  650. * @return The address of the end of the script.
  651. */
  652. uint32 GetEndOfScriptOffset(
  653. uint16 cur_entry_point, size_t entity_index, size_t script_index
  654. );
  655. /**
  656. * Parses an opcode.
  657. *
  658. * Adds properties to an instruction, and adds the instruction to
  659. * the list.
  660. *
  661. * @param opcode[in] The opcode code.
  662. * @param name[in] the opcode name.
  663. * @param instruction[in][out] A newly created instruction of the
  664. * type the opcode belongs to.
  665. * @param stack_change[in] Indicates how much the instruction
  666. * changes the stack pointer by.
  667. * @param argument_format[in] The opcode argument format.
  668. */
  669. template<typename T> void ParseOpcode(
  670. int opcode, std::string name, T instruction,
  671. int stack_change, const char* argument_format
  672. ){
  673. uint32 full_opcode = (full_opcode << 8) + opcode;
  674. this->insts_.push_back(instruction);
  675. this->insts_.back()->SetOpcode(full_opcode);
  676. this->insts_.back()->SetAddress(this->address_);
  677. this->insts_.back()->SetStackChange(0);
  678. this->insts_.back()->SetName(std::string(name));
  679. this->insts_.back()->SetCodeGenData("");
  680. this->ReadParams(this->insts_.back(), argument_format);
  681. }
  682. /**
  683. * The engine.
  684. */
  685. FieldEngine* engine_;
  686. /**
  687. * Last address of the header.
  688. */
  689. uint32 header_end_position_ = 0;
  690. /**
  691. * The list of sections.
  692. */
  693. std::array<uint32, 7> sections_;
  694. /**
  695. * Size of the pointers to sections.
  696. *
  697. * @todo Understand and document.
  698. */
  699. int16 section_pointers_size_;
  700. /**
  701. * The field header.
  702. */
  703. ScriptHeader header_;
  704. /**
  705. * Indicates if the field has been loaded from raw data.
  706. */
  707. bool loaded_from_raw_data_ = false;
  708. /**
  709. * The formatter.
  710. */
  711. SUDM::IScriptFormatter& formatter_;
  712. /**
  713. * The scale factor of the field.
  714. */
  715. float scale_factor_ = 1.0f;
  716. };
  717. }