KernelDataInstaller.cpp 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294
  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. #include <tinyxml.h>
  16. #include <fstream>
  17. #include <iostream>
  18. #include "DiskImage.h"
  19. #include "KernelDataInstaller.h"
  20. #include "common/FinalFantasy7/FF7NameLookup.h"
  21. #include <boost/algorithm/string/replace.hpp>
  22. KernelDataInstaller::KernelDataInstaller(DiskImage& disk_image):
  23. kernel_(*disk_image.filePath("data/kernel/KERNEL.BIN")), exe_path_(*disk_image.filePath("ff7.exe")
  24. ){
  25. for (int i = 0; i < 416; i ++) prices_[i] = 50;
  26. }
  27. KernelDataInstaller::~KernelDataInstaller(){}
  28. void KernelDataInstaller::ReadPrices(){
  29. File exe_file(exe_path_);
  30. exe_file.SetOffset(0x00514518); // Items, weapons, armors, accessories start here
  31. for (int i = 0; i < 320; i ++) prices_[i] = exe_file.readU32LE();
  32. exe_file.SetOffset(0x00514B18); // Materias start here
  33. for (int i = 320; i < 416; i ++) prices_[i] = exe_file.readU32LE();
  34. }
  35. int KernelDataInstaller::ReadCommands(){
  36. // Empty the command list
  37. commands_.clear();
  38. File command_file = kernel_.ExtractGZip(KERNEL_COMMAND_DATA);
  39. File command_name_file = kernel_.ExtractGZip(KERNEL_COMMAND_NAMES);
  40. File command_desc_file = kernel_.ExtractGZip(KERNEL_COMMAND_DESCRIPTIONS);
  41. int command_count = 0;
  42. // There are exactly 32 commands. Some of them are blank, so there is not really a
  43. // reliable exit condition.
  44. for (int i = 0; i < 32; i ++){
  45. CommandData data;
  46. int name_offset;
  47. int desc_offset;
  48. // From the current name file offset, read 16 bytes.
  49. // This is the pointer to the offset for the item name.
  50. // Start reading there until 0xFF is found.
  51. data.name = "";
  52. u8 ch = 1;
  53. name_offset = command_name_file.readU16LE();
  54. while (ch != 0xFF){
  55. ch = command_name_file.GetU8(name_offset);
  56. if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
  57. name_offset ++;
  58. }
  59. boost::replace_all(data.name, "\"", "'");
  60. // For descriptions, do the same as for names.
  61. data.description = "";
  62. ch = 1;
  63. desc_offset = command_desc_file.readU16LE();
  64. while (ch != 0xFF){
  65. ch = command_desc_file.GetU8(desc_offset);
  66. if (ch != 0xFF){
  67. // Warning: If the char is 0xF9, special function!
  68. // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
  69. // This is to compress information in the kernel.
  70. if (ch == 0xF9){
  71. // Get how much to read, two upper bits of the next byte.
  72. // Multiply by 2 and add 4. That's just how it is.
  73. u8 comp_size = (command_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
  74. // Get new offset, lower six bits of the next byte.
  75. // The offset goes backwards from the position of the 0xF9 byte.
  76. u8 comp_offset = command_desc_file.GetU8(desc_offset + 1) & 63;
  77. for (int c = 0; c < comp_size; c ++){
  78. data.description += ENGLISH_CHARS[command_desc_file.GetU8(desc_offset - 1 - comp_offset + c)];
  79. }
  80. desc_offset ++;
  81. }
  82. else data.description += ENGLISH_CHARS[ch];
  83. }
  84. desc_offset ++;
  85. }
  86. boost::replace_all(data.description, "\"", "'");
  87. // Read data from the command m, data section.
  88. data.cursor_action = command_file.readU8();
  89. data.target_raw = command_file.readU8();
  90. data.unknown = command_file.readU16LE();
  91. data.camera_single = command_file.readU16LE();
  92. data.camera_multiple = command_file.readU16LE();
  93. // Derive data from the read data.
  94. data.id = i;
  95. if (data.target_raw == (data.target_raw | 0x01)) data.target.selection_enabled = true;
  96. else data.target.selection_enabled = false;
  97. if (data.target_raw == (data.target_raw | 0x02)) data.target.default_enemy = true;
  98. else data.target.default_enemy = false;
  99. // Default multiple is inverted
  100. if (data.target_raw == (data.target_raw | 0x04)) data.target.default_multiple = false;
  101. else data.target.default_multiple = true;
  102. if (data.target_raw == (data.target_raw | 0x08)) data.target.toggle_multiple = true;
  103. else data.target.toggle_multiple = false;
  104. if (data.target_raw == (data.target_raw | 0x10)) data.target.fixed_row = true;
  105. else data.target.fixed_row = false;
  106. if (data.target_raw == (data.target_raw | 0x20)) data.target.short_range = true;
  107. else data.target.short_range = false;
  108. if (data.target_raw == (data.target_raw | 0x40)) data.target.all_rows = true;
  109. else data.target.all_rows = false;
  110. if (data.target_raw == (data.target_raw | 0x80)) data.target.random = true;
  111. else data.target.random = false;
  112. switch (data.cursor_action){
  113. case NONE: data.menu = "Battle.Menu.NONE"; break;
  114. case MAGIC: data.menu = "Battle.Menu.MAGIC"; break;
  115. case SUMMON: data.menu = "Battle.Menu.SUMMON"; break;
  116. case ITEM: data.menu = "Battle.Menu.ITEM"; break;
  117. case E_SKILL: data.menu = "Battle.Menu.E_SKILL"; break;
  118. case THROW: data.menu = "Battle.Menu.THROW"; break;
  119. case LIMIT: data.menu = "Battle.Menu.LIMIT"; break;
  120. case TARGET: data.menu = "Battle.Menu.TARGET"; break;
  121. case W_MAGIC: data.menu = "Battle.Menu.W_MAGIC"; break;
  122. case W_SUMMON: data.menu = "Battle.Menu.W_SUMMON"; break;
  123. case W_ITEM: data.menu = "Battle.Menu.W_ITEM"; break;
  124. case COIN: data.menu = "Battle.Menu.COIN"; break;
  125. default: data.menu = "Battle.Menu.NONE";
  126. }
  127. // Add to the list of commands.
  128. if (data.name != ""){
  129. command_names_[i] = data.name;
  130. commands_.push_back(data);
  131. command_count ++;
  132. }
  133. }
  134. return command_count;
  135. }
  136. void KernelDataInstaller::WriteCommands(std::string file_name){
  137. std::fstream file;
  138. file.open(file_name, std::ios::out);
  139. if (!file) return;
  140. for (CommandData command : commands_){
  141. file << "Game.Commands[" << command.id << "] = {\n"
  142. << " name = \"" << command.name << "\",\n"
  143. << " description = \"" << command.description << "\",\n"
  144. << " menu = " << command.menu << ",\n"
  145. << " camera = {single = " << command.camera_single << ", "
  146. << "multiple = " << command.camera_multiple << "}\n}\n";
  147. }
  148. file.close();
  149. }
  150. int KernelDataInstaller::ReadAttacks(){
  151. // Empty the item list
  152. attacks_.clear();
  153. File attacks_file = kernel_.ExtractGZip(KERNEL_ATTACK_DATA);
  154. File attacks_name_file = kernel_.ExtractGZip(KERNEL_ATTACK_NAMES);
  155. File attacks_desc_file = kernel_.ExtractGZip(KERNEL_ATTACK_DESCRIPTIONS);
  156. int attack_count = 0;
  157. limit_attacks_.clear();
  158. // The kernel has data for 128 attacks, but names and descriptions for 256. The last 128 are the
  159. // limit break techniques, which the characters refer to by ID (128 and up).
  160. for (int i = 0; i < 256; i ++){
  161. AttackData data;
  162. int name_offset;
  163. int desc_offset;
  164. // From the current name file offset, read 16 bytes.
  165. // This is the pointer to the offset for the item name.
  166. // Start reading there until 0xFF is found.
  167. data.name = "";
  168. u8 ch = 1;
  169. name_offset = attacks_name_file.readU16LE();
  170. while (ch != 0xFF){
  171. ch = attacks_name_file.GetU8(name_offset);
  172. if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
  173. name_offset ++;
  174. }
  175. boost::replace_all(data.name, "\"", "'");
  176. // For descriptions, do the same as for names.
  177. data.description = "";
  178. ch = 1;
  179. desc_offset = attacks_desc_file.readU16LE();
  180. while (ch != 0xFF){
  181. ch = attacks_desc_file.GetU8(desc_offset);
  182. if (ch != 0xFF){
  183. // Warning: If the char is 0xF9, special function!
  184. // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
  185. // This is to compress information in the kernel.
  186. if (ch == 0xF9){
  187. // Get how much to read, two upper bits of the next byte.
  188. // Multiply by 2 and add 4. That's just how it is.
  189. u8 comp_size = (attacks_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
  190. // Get new offset, lower six bits of the next byte.
  191. // The offset goes backwards from the position of the 0xF9 byte.
  192. u8 comp_offset = attacks_desc_file.GetU8(desc_offset + 1) & 63;
  193. for (int c = 0; c < comp_size; c ++){
  194. data.description += ENGLISH_CHARS[
  195. attacks_desc_file.GetU8(desc_offset - 1 - comp_offset + c)
  196. ];
  197. }
  198. desc_offset ++;
  199. }
  200. else data.description += ENGLISH_CHARS[ch];
  201. }
  202. desc_offset ++;
  203. }
  204. boost::replace_all(data.description, "\"", "'");
  205. // Limit techniques only have name and description. Both start with control codes, drop
  206. // everything before the first letter.
  207. if (i >= 128){
  208. for (std::string* text : {&data.name, &data.description}){
  209. size_t first = text->find_first_of(
  210. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
  211. if (first == std::string::npos) text->clear();
  212. else text->erase(0, first);
  213. }
  214. data.id = i;
  215. limit_attacks_.push_back(data);
  216. continue;
  217. }
  218. // Read data from the item data section.
  219. data.accuracy = attacks_file.readU8();
  220. data.impact_effect = attacks_file.readU8();
  221. data.hurt_anim = attacks_file.readU8();
  222. data.unknown = attacks_file.readU8();
  223. data.mp = attacks_file.readU16LE();
  224. data.sound = attacks_file.readU16LE();
  225. data.camera_single = attacks_file.readU16LE();
  226. data.camera_multiple = attacks_file.readU16LE();
  227. data.target_raw = attacks_file.readU8();
  228. data.effect = attacks_file.readU8();
  229. data.damage_raw = attacks_file.readU8();
  230. data.power = attacks_file.readU8();
  231. data.condition_raw = attacks_file.readU8();
  232. data.status_change_raw = attacks_file.readU8();
  233. data.additional_effects_raw = attacks_file.readU8();
  234. data.additional_effects_mod_raw = attacks_file.readU8();
  235. data.status_raw = attacks_file.readU32LE();
  236. data.element_raw = attacks_file.readU16LE();
  237. data.special_raw = attacks_file.readU16LE();
  238. // Derive data from the read data.
  239. data.id = i;
  240. if (data.target_raw == (data.target_raw | 0x01)) data.target.selection_enabled = true;
  241. else data.target.selection_enabled = false;
  242. if (data.target_raw == (data.target_raw | 0x02)) data.target.default_enemy = true;
  243. else data.target.default_enemy = false;
  244. if (data.target_raw == (data.target_raw | 0x04)) data.target.default_multiple = true;
  245. else data.target.default_multiple = false;
  246. if (data.target_raw == (data.target_raw | 0x08)) data.target.toggle_multiple = true;
  247. else data.target.toggle_multiple = false;
  248. if (data.target_raw == (data.target_raw | 0x10)) data.target.fixed_row = true;
  249. else data.target.fixed_row = false;
  250. if (data.target_raw == (data.target_raw | 0x20)) data.target.short_range = true;
  251. else data.target.short_range = false;
  252. if (data.target_raw == (data.target_raw | 0x40)) data.target.default_enemy = true;
  253. else data.target.all_rows = false;
  254. if (data.target_raw == (data.target_raw | 0x80)) data.target.random = true;
  255. else data.target.random = false;
  256. data.damage_formula = data.damage_raw >> 4; // Upper nybble.
  257. data.damage_modifier = data.damage_raw & 15; // Lower nybble.
  258. // Status attack byte: the lower 6 bits are the chance out of 63, and the upper bits the
  259. // mode (0x80 toggles the statuses, 0x40 cures them, otherwise they are inflicted).
  260. data.status.chance = data.status_change_raw & 0x3F;
  261. if ((data.status_change_raw & 0x80) != 0) data.status.mode = TOGGLE;
  262. else if ((data.status_change_raw & 0x40) != 0) data.status.mode = CURE;
  263. else data.status.mode = INFLICT;
  264. switch (data.condition_raw){
  265. case 0: data.restore_type = RESTORE_HP; break;
  266. case 1: data.restore_type = RESTORE_MP; break;
  267. case 2: data.restore_type = RESTORE_STATUS; break;
  268. default: data.restore_type = RESTORE_NONE;
  269. }
  270. // In KERNEL.BIN, items that don't induce statuses actually have all statuses marked, and
  271. // a chance of 255. If it's the case, skip statuses altogether. The status IDs are the bit
  272. // positions of the original game: 0 death, 1 near death, 2 sleep, 3 poison, 4 fury,
  273. // 5 sadness, 6 confusion, 7 silence, 8 haste, 9 slow, 10 stop...
  274. if (data.status_change_raw != 0xFF && data.status_raw != 0xFFFFFFFF){
  275. for (int bit = 0; bit < 32; bit ++){
  276. if ((data.status_raw & (1u << bit)) != 0) data.status.status.push_back(bit);
  277. }
  278. }
  279. // Get elements
  280. if (data.element_raw == (data.element_raw | 0x0001)) data.elements.push_back(FIRE);
  281. if (data.element_raw == (data.element_raw | 0x0002)) data.elements.push_back(ICE);
  282. if (data.element_raw == (data.element_raw | 0x0004)) data.elements.push_back(BOLT);
  283. if (data.element_raw == (data.element_raw | 0x0008)) data.elements.push_back(EARTH);
  284. if (data.element_raw == (data.element_raw | 0x0010)) data.elements.push_back(POISON);
  285. if (data.element_raw == (data.element_raw | 0x0020)) data.elements.push_back(GRAVITY);
  286. if (data.element_raw == (data.element_raw | 0x0040)) data.elements.push_back(WATER);
  287. if (data.element_raw == (data.element_raw | 0x0080)) data.elements.push_back(WIND);
  288. if (data.element_raw == (data.element_raw | 0x0100)) data.elements.push_back(HOLY);
  289. if (data.element_raw == (data.element_raw | 0x0200)) data.elements.push_back(RESTORATIVE);
  290. if (data.element_raw == (data.element_raw | 0x0400)) data.elements.push_back(CUT);
  291. if (data.element_raw == (data.element_raw | 0x0800)) data.elements.push_back(HIT);
  292. if (data.element_raw == (data.element_raw | 0x1000)) data.elements.push_back(PUNCH);
  293. if (data.element_raw == (data.element_raw | 0x2000)) data.elements.push_back(SHOOT);
  294. if (data.element_raw == (data.element_raw | 0x4000)) data.elements.push_back(SHOUT);
  295. if (data.element_raw == (data.element_raw | 0x8000)) data.elements.push_back(HIDDEN);
  296. // In KERNEL.BIN, Non elemental item actually have all elements marked.
  297. // If that happens, clear them all.
  298. if (data.elements.size() == 16) data.elements.clear();
  299. // Add to the list of items.
  300. attacks_.push_back(data);
  301. attack_names_[i] = data.name;
  302. attack_count ++;
  303. }
  304. return attack_count;
  305. }
  306. void KernelDataInstaller::WriteAttacks(std::string file_name){
  307. std::fstream file;
  308. file.open(file_name, std::ios::out);
  309. if (!file) return;
  310. for (AttackData attack : attacks_){
  311. file << "Game.Attacks[" << attack.id << "] = {\n"
  312. << " name = \"" << attack.name << "\",\n"
  313. << " description = \"" << attack.description << "\",\n"
  314. << " accuracy = " << static_cast<int>(attack.accuracy) << ",\n"
  315. << " impact_effect = " << static_cast<int>(attack.impact_effect) << ",\n"
  316. << " hurt_anim = " << static_cast<int>(attack.hurt_anim) << ",\n"
  317. << " mp = " << attack.mp << ",\n"
  318. << " sound = " << attack.sound << ",\n"
  319. << " camera = {single = " << attack.camera_single << ", "
  320. << "multiple = " << attack.camera_multiple << "},\n"
  321. << " effect = " << static_cast<int>(attack.effect) << ",\n"
  322. << " dmg_formula = " << static_cast<int>(attack.damage_formula) << ",\n"
  323. << " dmg_modifier = " << static_cast<int>(attack.damage_modifier) << ",\n"
  324. << " power = " << static_cast<int>(attack.power) << ",\n"
  325. << " restore = " << attack.restore_type << ",\n"
  326. << " target = {selectable = " << attack.target.selection_enabled
  327. << ", default_enemy = " << attack.target.default_enemy
  328. << ", default_multiple = " << attack.target.default_multiple
  329. << ", toggle_multiple = " << attack.target.toggle_multiple
  330. << ", fixed_row = " << attack.target.fixed_row
  331. << ", short_range = " << attack.target.short_range
  332. << ", all_rows = " << attack.target.all_rows
  333. << ", random = " << attack.target.random << "},\n"
  334. << " elements = {";
  335. for (int element : attack.elements) file << element << ", ";
  336. file << "},\n status = {";
  337. // Status effects: ID (bit position), mode (0 inflict, 1 cure, 2 toggle) and chance out of 63.
  338. for (int status : attack.status.status){
  339. file << "{status = " << status << ", mode = " << attack.status.mode
  340. << ", chance = " << attack.status.chance << "}, ";
  341. }
  342. file << "}\n}\n";
  343. }
  344. // Limit techniques: only name and description are known.
  345. for (AttackData attack : limit_attacks_){
  346. file << "Game.Attacks[" << attack.id << "] = {\n"
  347. << " name = \"" << attack.name << "\",\n"
  348. << " description = \"" << attack.description << "\"\n"
  349. << "}\n";
  350. }
  351. file.close();
  352. }
  353. int KernelDataInstaller::ReadCharacters(){
  354. // TODO: Do something about young Cloud and Sephiroth.
  355. // Empty the item list
  356. characters_.clear();
  357. File characters_file = kernel_.ExtractGZip(KERNEL_BATTLE_AND_GROWTH_DATA);
  358. int character_count = 0;
  359. for (int i = 0; i < 11; i ++){ // 11 characters
  360. CharacterData data;
  361. // Read data from the item data section.
  362. data.curve_str = characters_file.readU8();
  363. data.curve_vit = characters_file.readU8();
  364. data.curve_mag = characters_file.readU8();
  365. data.curve_spr = characters_file.readU8();
  366. data.curve_dex = characters_file.readU8();
  367. data.curve_lck = characters_file.readU8();
  368. data.curve_hp = characters_file.readU8();
  369. data.curve_mp = characters_file.readU8();
  370. data.curve_exp = characters_file.readU8();
  371. data.unused_0 = characters_file.readU8();
  372. data.initial_level = characters_file.readU8();
  373. data.unused_1 = characters_file.readU8();
  374. data.limit_1_1 = characters_file.readU8();
  375. data.limit_1_2 = characters_file.readU8();
  376. data.limit_1_3 = characters_file.readU8();
  377. data.limit_2_1 = characters_file.readU8();
  378. data.limit_2_2 = characters_file.readU8();
  379. data.limit_2_3 = characters_file.readU8();
  380. data.limit_3_1 = characters_file.readU8();
  381. data.limit_3_2 = characters_file.readU8();
  382. data.limit_3_3 = characters_file.readU8();
  383. data.limit_4_1 = characters_file.readU8();
  384. data.limit_4_2 = characters_file.readU8();
  385. data.limit_4_3 = characters_file.readU8();
  386. data.limit_2_kills = characters_file.readU16LE();
  387. data.limit_3_kills = characters_file.readU16LE();
  388. data.limit_1_2_uses = characters_file.readU16LE();
  389. data.limit_1_3_uses = characters_file.readU16LE();
  390. data.limit_2_2_uses = characters_file.readU16LE();
  391. data.limit_2_3_uses = characters_file.readU16LE();
  392. data.limit_3_2_uses = characters_file.readU16LE();
  393. data.limit_3_3_uses = characters_file.readU16LE();
  394. data.limit_1_div = characters_file.readU32LE();
  395. data.limit_2_div = characters_file.readU32LE();
  396. data.limit_3_div = characters_file.readU32LE();
  397. data.limit_4_div = characters_file.readU32LE();
  398. // Derive data from the read data.
  399. data.id = i;
  400. data.name = VGears::NameLookup::CharName(i);
  401. // Add to the list of characters.
  402. characters_.push_back(data);
  403. character_count ++;
  404. }
  405. return character_count;
  406. }
  407. void KernelDataInstaller::WriteCharacters(std::string file_name){
  408. std::fstream file;
  409. file.open(file_name, std::ios::out);
  410. if (!file) return;
  411. for (CharacterData character : characters_){
  412. file
  413. << "Game.Characters[" << character.id << "] = {\n"
  414. << " id = " << character.id << ",\n"
  415. << " name = \"" << character.name << "\",\n"
  416. << " initial_level = \"" << static_cast<int>(character.initial_level) << "\",\n"
  417. << " curves = {\n"
  418. << " str = " << static_cast<int>(character.curve_str) << ",\n"
  419. << " vit = " << static_cast<int>(character.curve_vit) << ",\n"
  420. << " mag = " << static_cast<int>(character.curve_mag) << ",\n"
  421. << " spr = " << static_cast<int>(character.curve_spr) << ",\n"
  422. << " dex = " << static_cast<int>(character.curve_dex) << ",\n"
  423. << " lck = " << static_cast<int>(character.curve_lck) << ",\n"
  424. << " hp = " << static_cast<int>(character.curve_hp) << ",\n"
  425. << " mp = " << static_cast<int>(character.curve_mp) << ",\n"
  426. << " exp = " << static_cast<int>(character.curve_exp) << ",\n"
  427. << " },\n"
  428. << " limits = {\n"
  429. << " [1] = {\n"
  430. << " kills = 0,\n"
  431. << " techniques = {\n"
  432. << " [1] = {\n"
  433. << " command = " << static_cast<int>(character.limit_1_1) << ",\n"
  434. << " uses = 0\n"
  435. << " },\n";
  436. if (character.limit_1_2 < 255){ // Some characters only have 1 limit per level.
  437. file
  438. << " [2] = {\n"
  439. << " command = " << static_cast<int>(character.limit_1_2) << ",\n"
  440. << " uses = " << character.limit_1_2_uses << "\n"
  441. << " },\n";
  442. }
  443. file
  444. << " }\n"
  445. << " },\n";
  446. // Limit level 2.
  447. if (character.limit_2_kills < 65535){
  448. file
  449. << " [2] = {\n"
  450. << " kills = " << character.limit_2_kills << ",\n"
  451. << " techniques = {\n"
  452. << " [1] = {\n"
  453. << " command = " << static_cast<int>(character.limit_2_1) << ",\n"
  454. << " uses = 0\n"
  455. << " },\n";
  456. if (character.limit_2_2 < 255){ // Some characters only have 1 limit per level.
  457. file
  458. << " [2] = {\n"
  459. << " command = "
  460. << static_cast<int>(character.limit_2_2) << ",\n"
  461. << " uses = " << character.limit_2_2_uses << "\n"
  462. << " },\n";
  463. }
  464. file
  465. << " }\n"
  466. << " },\n";
  467. }
  468. // Limit level 3.
  469. if (character.limit_3_kills < 65535){
  470. file
  471. << " [3] = {\n"
  472. << " kills = " << character.limit_3_kills << ",\n"
  473. << " techniques = {\n"
  474. << " [1] = {\n"
  475. << " command = " << static_cast<int>(character.limit_3_1) << ",\n"
  476. << " uses = 0\n"
  477. << " },\n";
  478. if (character.limit_3_2 < 255){ // Some characters only have 1 limit per level.
  479. file
  480. << " [2] = {\n"
  481. << " command = "
  482. << static_cast<int>(character.limit_3_2) << ",\n"
  483. << " uses = " << character.limit_3_2_uses << "\n"
  484. << " },\n";
  485. }
  486. file
  487. << " }\n"
  488. << " },\n";
  489. }
  490. // Limit level 4.
  491. if (character.limit_4_1 < 255){
  492. file
  493. << " [4] = {\n"
  494. << " kills = 0,\n"
  495. << " techniques = {\n"
  496. << " [1] = {\n"
  497. << " command = " << static_cast<int>(character.limit_4_1) << ",\n"
  498. << " uses = 0\n"
  499. << " },\n"
  500. << " }\n"
  501. << " },\n";
  502. }
  503. file
  504. << " }\n}\n";
  505. }
  506. file.close();
  507. }
  508. void KernelDataInstaller::ReadGrowth(){
  509. File growth_file = kernel_.ExtractGZip(KERNEL_BATTLE_AND_GROWTH_DATA);
  510. growth_file.SetOffset(0x01F8); //Skip character data.
  511. for (int i = 0; i < 12; i ++) growth_.bonus_stat[i] = growth_file.readU8();
  512. for (int i = 0; i < 12; i ++) growth_.bonus_hp[i] = growth_file.readU8();
  513. for (int i = 0; i < 12; i ++) growth_.bonus_mp[i] = growth_file.readU8();
  514. for (int i = 0; i < 64; i ++){
  515. growth_.curves[i].id = i;
  516. if (i < 37) growth_.curves[i].type = CURVE_PRIMARY;
  517. else if (i >=37 && i < 46) growth_.curves[i].type = CURVE_HP;
  518. else if (i >=46 && i < 55) growth_.curves[i].type = CURVE_MP;
  519. else growth_.curves[i].type = CURVE_EXP;
  520. for (int j = 0; j < 8; j ++){
  521. growth_.curves[i].gradient[j] = growth_file.readU8();
  522. growth_.curves[i].base[j] = growth_file.readU8();
  523. }
  524. }
  525. }
  526. void KernelDataInstaller::WriteGrowth(std::string file_name){
  527. std::fstream file;
  528. file.open(file_name, std::ios::out);
  529. if (!file) return;
  530. file << "Game.Growth.bonus.primary = {";
  531. for (int i = 0; i < 12; i ++) file << "[" << i << "] = " << static_cast<int>(growth_.bonus_stat[i]) << ", ";
  532. file << "}\nGame.Growth.bonus.hp = {";
  533. for (int i = 0; i < 12; i ++) file << "[" << i << "] = " << static_cast<int>(growth_.bonus_hp[i]) << ", ";
  534. file << "}\nGame.Growth.bonus.mp = {";
  535. for (int i = 0; i < 12; i ++) file << "[" << i << "] = " << static_cast<int>(growth_.bonus_mp[i]) << ", ";
  536. file << "}\n";
  537. for (int i = 0; i < 64; i ++){
  538. file << "Game.Growth.curves[" << i << "] = {\n";
  539. for (int j = 0; j < 8; j ++){
  540. file << " [" << j << "] = {gradient = " << static_cast<int>(growth_.curves[i].gradient[j])
  541. << ", base = " << static_cast<int>(growth_.curves[i].gradient[j]) << "},\n";
  542. }
  543. file << "}\n";
  544. }
  545. file.close();
  546. }
  547. int KernelDataInstaller::ReadItems(){
  548. // Empty the item list
  549. items_.clear();
  550. File items_file = kernel_.ExtractGZip(KERNEL_ITEM_DATA);
  551. File items_name_file = kernel_.ExtractGZip(KERNEL_ITEM_NAMES);
  552. File items_desc_file = kernel_.ExtractGZip(KERNEL_ITEM_DESCRIPTIONS);
  553. int item_count = 0;
  554. for (int i = 0; i < 128; i ++){
  555. ItemData data;
  556. int name_offset;
  557. int desc_offset;
  558. // From the current name file offset, read 16 bytes.
  559. // This is the pointer to the offset for the item name.
  560. // Start reading there until 0xFF is found.
  561. data.name = "";
  562. u8 ch = 1;
  563. name_offset = items_name_file.readU16LE();
  564. while (ch != 0xFF){
  565. ch = items_name_file.GetU8(name_offset);
  566. if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
  567. name_offset ++;
  568. }
  569. boost::replace_all(data.name, "\"", "'");
  570. // For descriptions, do the same as for names.
  571. data.description = "";
  572. ch = 1;
  573. desc_offset = items_desc_file.readU16LE();
  574. while (ch != 0xFF){
  575. ch = items_desc_file.GetU8(desc_offset);
  576. if (ch != 0xFF){
  577. // Warning: If the char is 0xF9, special function!
  578. // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
  579. // This is to compress information in the kernel.
  580. if (ch == 0xF9){
  581. // Get how much to read, two upper bits of the next byte.
  582. // Multiply by 2 and add 4. That's just how it is.
  583. u8 comp_size = (items_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
  584. // Get new offset, lower six bits of the next byte.
  585. // The offset goes backwards from the position of the 0xF9 byte.
  586. u8 comp_offset = items_desc_file.GetU8(desc_offset + 1) & 63;
  587. for (int c = 0; c < comp_size; c ++){
  588. data.description += ENGLISH_CHARS[items_desc_file.GetU8(desc_offset - 1 - comp_offset + c)];
  589. }
  590. desc_offset ++;
  591. }
  592. else data.description += ENGLISH_CHARS[ch];
  593. }
  594. desc_offset ++;
  595. }
  596. boost::replace_all(data.description, "\"", "'");
  597. // If the item doesn't have a name, it means all items have been read.
  598. // break the loop.
  599. if ("" == data.name) break;
  600. // Read data from the item data section.
  601. data.unknown_0 = items_file.readU32LE();
  602. data.unknown_1 = items_file.readU32LE();
  603. data.camera = items_file.readU16LE();
  604. data.restrict_raw = items_file.readU16LE();
  605. data.target_raw = items_file.readU8();
  606. data.effect = items_file.readU8();
  607. data.damage_raw = items_file.readU8();
  608. data.power = items_file.readU8();
  609. data.condition_raw = items_file.readU8();
  610. data.status_change_raw = items_file.readU8();
  611. data.additional_effects_raw = items_file.readU8();
  612. data.additional_effects_mod_raw = items_file.readU8();
  613. data.status_raw = items_file.readU32LE();
  614. data.element_raw = items_file.readU16LE();
  615. data.special_raw = items_file.readU16LE();
  616. // Derive data from the read data.
  617. data.id = i;
  618. data.sellable = !(data.restrict_raw & (1 << 0));
  619. data.useable_battle = !(data.restrict_raw & (2 << 0));
  620. data.useable_menu = !(data.restrict_raw & (4 << 0));
  621. if (data.target_raw == (data.target_raw | 0x01)) data.target.selection_enabled = true;
  622. else data.target.selection_enabled = false;
  623. if (data.target_raw == (data.target_raw | 0x02)) data.target.default_enemy = true;
  624. else data.target.default_enemy = false;
  625. if (data.target_raw == (data.target_raw | 0x04)) data.target.default_multiple = true;
  626. else data.target.default_multiple = false;
  627. if (data.target_raw == (data.target_raw | 0x08)) data.target.toggle_multiple = true;
  628. else data.target.toggle_multiple = false;
  629. if (data.target_raw == (data.target_raw | 0x10)) data.target.fixed_row = true;
  630. else data.target.fixed_row = false;
  631. if (data.target_raw == (data.target_raw | 0x20)) data.target.short_range = true;
  632. else data.target.short_range = false;
  633. if (data.target_raw == (data.target_raw | 0x40)) data.target.default_enemy = true;
  634. else data.target.all_rows = false;
  635. if (data.target_raw == (data.target_raw | 0x80)) data.target.random = true;
  636. else data.target.random = false;
  637. data.damage_formula = data.damage_raw >> 4; // Upper nybble.
  638. data.damage_modifier = data.damage_raw & 15; // Lower nybble.
  639. data.status.chance = 63 - (data.status_change_raw ^ 63); // Out of 63;
  640. if (data.status_change_raw == (data.status_change_raw | 0x00000080)) data.status.mode = TOGGLE;
  641. else if (data.status_change_raw == (data.status_change_raw | 0x00000040)) data.status.mode = CURE;
  642. else data.status.mode = INFLICT;
  643. switch (data.condition_raw){
  644. case 0: data.restore_type = RESTORE_HP; break;
  645. case 1: data.restore_type = RESTORE_MP; break;
  646. case 2: data.restore_type = RESTORE_STATUS; break;
  647. default: data.restore_type = RESTORE_NONE;
  648. }
  649. // In KERNEL.BIN, items that don't induce statuses actually have all statuses marked, and
  650. // a chance of 255. If it's the case, skip statuses altogether.
  651. if (data.status.chance <= 64){
  652. // TODO: Im fairly sure that this can be done with a single-line loop...
  653. // ... but I don't know how
  654. if (data.status_raw == (data.status_raw | 0x00000001)) data.status.status.push_back(DEATH);
  655. if (data.status_raw == (data.status_raw | 0x00000002)) data.status.status.push_back(NEAR_DEATH);
  656. if (data.status_raw == (data.status_raw | 0x00000004)) data.status.status.push_back(SLEEP);
  657. if (data.status_raw == (data.status_raw | 0x00000008)) data.status.status.push_back(POISONED);
  658. if (data.status_raw == (data.status_raw | 0x00000010)) data.status.status.push_back(SADNESS);
  659. if (data.status_raw == (data.status_raw | 0x00000020)) data.status.status.push_back(FURY);
  660. if (data.status_raw == (data.status_raw | 0x00000040)) data.status.status.push_back(CONFU);
  661. if (data.status_raw == (data.status_raw | 0x00000080)) data.status.status.push_back(SILENCE);
  662. if (data.status_raw == (data.status_raw | 0x00000100)) data.status.status.push_back(HASTE);
  663. if (data.status_raw == (data.status_raw | 0x00000200)) data.status.status.push_back(SLOW);
  664. if (data.status_raw == (data.status_raw | 0x00000400)) data.status.status.push_back(STOP);
  665. if (data.status_raw == (data.status_raw | 0x00000800)) data.status.status.push_back(FROG);
  666. if (data.status_raw == (data.status_raw | 0x00001000)) data.status.status.push_back(SMALL);
  667. if (data.status_raw == (data.status_raw | 0x00002000)) data.status.status.push_back(SLOW_NUMB);
  668. if (data.status_raw == (data.status_raw | 0x00004000)) data.status.status.push_back(PETRIFY);
  669. if (data.status_raw == (data.status_raw | 0x00008000)) data.status.status.push_back(REGEN);
  670. if (data.status_raw == (data.status_raw | 0x00010000)) data.status.status.push_back(BARRIER);
  671. if (data.status_raw == (data.status_raw | 0x00020000)) data.status.status.push_back(M_BARRIER);
  672. if (data.status_raw == (data.status_raw | 0x00040000)) data.status.status.push_back(REFLECT);
  673. if (data.status_raw == (data.status_raw | 0x00080000)) data.status.status.push_back(DUAL);
  674. if (data.status_raw == (data.status_raw | 0x00100000)) data.status.status.push_back(SHIELD);
  675. if (data.status_raw == (data.status_raw | 0x00200000)) data.status.status.push_back(D_SENTENCE);
  676. if (data.status_raw == (data.status_raw | 0x00400000)) data.status.status.push_back(MANIPULATE);
  677. if (data.status_raw == (data.status_raw | 0x00800000)) data.status.status.push_back(BERSERK);
  678. if (data.status_raw == (data.status_raw | 0x01000000)) data.status.status.push_back(PEERLESS);
  679. if (data.status_raw == (data.status_raw | 0x02000000)) data.status.status.push_back(PARALYSIS);
  680. if (data.status_raw == (data.status_raw | 0x04000000)) data.status.status.push_back(DARKNESS);
  681. if (data.status_raw == (data.status_raw | 0x08000000)) data.status.status.push_back(DUAL_DRAIN);
  682. if (data.status_raw == (data.status_raw | 0x10000000)) data.status.status.push_back(DEATH_FORCE);
  683. if (data.status_raw == (data.status_raw | 0x20000000)) data.status.status.push_back(RESIST);
  684. if (data.status_raw == (data.status_raw | 0x40000000)) data.status.status.push_back(LUCKY_GIRL);
  685. if (data.status_raw == (data.status_raw | 0x80000000)) data.status.status.push_back(IMPRISONED);
  686. }
  687. // Get elements
  688. if (data.element_raw == (data.element_raw | 0x0001)) data.elements.push_back(FIRE);
  689. if (data.element_raw == (data.element_raw | 0x0002)) data.elements.push_back(ICE);
  690. if (data.element_raw == (data.element_raw | 0x0004)) data.elements.push_back(BOLT);
  691. if (data.element_raw == (data.element_raw | 0x0008)) data.elements.push_back(EARTH);
  692. if (data.element_raw == (data.element_raw | 0x0010)) data.elements.push_back(POISON);
  693. if (data.element_raw == (data.element_raw | 0x0020)) data.elements.push_back(GRAVITY);
  694. if (data.element_raw == (data.element_raw | 0x0040)) data.elements.push_back(WATER);
  695. if (data.element_raw == (data.element_raw | 0x0080)) data.elements.push_back(WIND);
  696. if (data.element_raw == (data.element_raw | 0x0100)) data.elements.push_back(HOLY);
  697. if (data.element_raw == (data.element_raw | 0x0200)) data.elements.push_back(RESTORATIVE);
  698. if (data.element_raw == (data.element_raw | 0x0400)) data.elements.push_back(CUT);
  699. if (data.element_raw == (data.element_raw | 0x0800)) data.elements.push_back(HIT);
  700. if (data.element_raw == (data.element_raw | 0x1000)) data.elements.push_back(PUNCH);
  701. if (data.element_raw == (data.element_raw | 0x2000)) data.elements.push_back(SHOOT);
  702. if (data.element_raw == (data.element_raw | 0x4000)) data.elements.push_back(SHOUT);
  703. if (data.element_raw == (data.element_raw | 0x8000)) data.elements.push_back(HIDDEN);
  704. // In KERNEL.BIN, Non elemental item actually have all elements marked.
  705. // If that happens, clear them all.
  706. if (data.elements.size() == 16) data.elements.clear();
  707. // Add to the list of items.
  708. items_.push_back(data);
  709. item_count ++;
  710. }
  711. return item_count;
  712. }
  713. void KernelDataInstaller::WriteItems(std::string file_name){
  714. std::fstream file;
  715. file.open(file_name, std::ios::out);
  716. if (!file) return;
  717. for (ItemData item : items_){
  718. file << "Game.Items[" << item.id << "] = {\n"
  719. << " type = Inventory.ITEM_TYPE.ITEM,\n"
  720. << " id = " << item.id << ",\n"
  721. << " price = " << prices_[item.id] << ",\n"
  722. << " name = \"" << item.name << "\",\n"
  723. << " description = \"" << item.description << "\",\n"
  724. << " camera = " << item.camera << ",\n"
  725. << " sell = " << item.sellable << ",\n"
  726. << " battle = " << item.useable_battle << ",\n"
  727. << " menu = " << item.useable_menu << ",\n"
  728. << " throw = 0,\n"
  729. << " effect = " << static_cast<int>(item.effect) << ",\n"
  730. << " dmg_formula = " << static_cast<int>(item.damage_formula) << ",\n"
  731. << " dmg_modifier = " << static_cast<int>(item.damage_modifier) << ",\n"
  732. << " power = " << static_cast<int>(item.power) << ",\n"
  733. << " restore = " << item.restore_type << ",\n"
  734. << " target = {selectable = " << item.target.selection_enabled
  735. << ", default_enemy = " << item.target.default_enemy
  736. << ", default_multiple = " << item.target.default_multiple
  737. << ", toggle_multiple = " << item.target.toggle_multiple
  738. << ", fixed_row = " << item.target.fixed_row
  739. << ", short_range = " << item.target.short_range
  740. << ", all_rows = " << item.target.all_rows
  741. << ", random = " << item.target.random << "},\n"
  742. << " status = {";
  743. for (int s : item.status.status){
  744. file << "\n {status = " << s << ", mode = " << item.status.mode
  745. << ", chance = " << item.status.chance << "},";
  746. }
  747. if (item.status.status.size() > 0) file << "\n },\n";
  748. else file << "},\n";
  749. file << " elements = {";
  750. for (int e : item.elements) file << e << ", ";
  751. file << "},\n}\n";
  752. }
  753. file.close();
  754. }
  755. int KernelDataInstaller::ReadWeapons(){
  756. // Empty the item list
  757. weapons_.clear();
  758. File weapons_file = kernel_.ExtractGZip(KERNEL_WEAPON_DATA);
  759. File weapons_name_file = kernel_.ExtractGZip(KERNEL_WEAPON_NAMES);
  760. File weapons_desc_file = kernel_.ExtractGZip(KERNEL_WEAPON_DESCRIPTIONS);
  761. int weapon_count = 0;
  762. for (int i = 0; i < 128; i ++){
  763. WeaponData data;
  764. int name_offset;
  765. int desc_offset;
  766. // From the current name file offset, read 16 bytes.
  767. // This is the pointer to the offset for the item name.
  768. // Start reading there until 0xFF is found.
  769. data.name = "";
  770. u8 ch = 1;
  771. name_offset = weapons_name_file.readU16LE();
  772. while (ch != 0xFF){
  773. ch = weapons_name_file.GetU8(name_offset);
  774. if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
  775. name_offset ++;
  776. }
  777. boost::replace_all(data.name, "\"", "'");
  778. // For descriptions, do the same as for names.
  779. data.description = "";
  780. ch = 1;
  781. desc_offset = weapons_desc_file.readU16LE();
  782. while (ch != 0xFF){
  783. ch = weapons_desc_file.GetU8(desc_offset);
  784. if (ch != 0xFF){
  785. // Warning: If the char is 0xF9, special function!
  786. // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
  787. // This is to compress information in the kernel.
  788. if (ch == 0xF9){
  789. // Get how much to read, two upper bits of the next byte.
  790. // Multiply by 2 and add 4. That's just how it is.
  791. u8 comp_size = (weapons_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
  792. // Get new offset, lower six bits of the next byte.
  793. // The offset goes backwards from the position of the 0xF9 byte.
  794. u8 comp_offset = weapons_desc_file.GetU8(desc_offset + 1) & 63;
  795. for (int c = 0; c < comp_size; c ++){
  796. data.description += ENGLISH_CHARS[weapons_desc_file.GetU8(desc_offset - 1 - comp_offset + c)];
  797. }
  798. desc_offset ++;
  799. }
  800. else data.description += ENGLISH_CHARS[ch];
  801. }
  802. desc_offset ++;
  803. }
  804. boost::replace_all(data.description, "\"", "'");
  805. // If the item doesn't have a name, it means all items have been read.
  806. // break the loop.
  807. if ("" == data.name) break;
  808. // Read data from the weapon data section.
  809. data.target_raw = weapons_file.readU8();
  810. data.unused_0 = weapons_file.readU8();
  811. data.damage_raw = weapons_file.readU8();
  812. data.unused_1 = weapons_file.readU8();
  813. data.power = weapons_file.readU8();
  814. data.status = weapons_file.readU8();
  815. data.growth = weapons_file.readU8();
  816. data.critical = weapons_file.readU8();
  817. data.accuracy = weapons_file.readU8();
  818. data.model_raw = weapons_file.readU8();
  819. data.unused_2 = weapons_file.readU8();
  820. data.high_sound = weapons_file.readU8();
  821. data.camera = weapons_file.readU16LE();
  822. data.equip_raw = weapons_file.readU16LE();
  823. data.element_raw = weapons_file.readU16LE();
  824. data.unused_3 = weapons_file.readU16LE();
  825. for (int s = 0; s < 4; s ++) data.stat_raw[s] = weapons_file.readU8();
  826. for (int s = 0; s < 4; s ++) data.stat_bonus_raw[s] = weapons_file.readU8();
  827. for (int s = 0; s < 8; s ++) data.slots_raw[s] = weapons_file.readU8();
  828. data.sound = weapons_file.readU8();
  829. data.sound_critical = weapons_file.readU8();
  830. data.sound_miss = weapons_file.readU8();
  831. data.effect = weapons_file.readU8();
  832. data.special_raw = weapons_file.readU16LE();
  833. data.restrict_raw = weapons_file.readU16LE();
  834. // Derive data from the read data.
  835. data.id = i;
  836. if (data.growth != 0 && data.growth != 2 && data.growth != 3) data.growth = 1;
  837. data.sellable = !(data.restrict_raw & (1 << 0));
  838. data.useable_battle = !(data.restrict_raw & (2 << 0));
  839. data.useable_menu = !(data.restrict_raw & (4 << 0));
  840. data.throwable = !(data.restrict_raw & (8 << 0));
  841. if (data.target_raw == (data.target_raw | 0x01)) data.target.selection_enabled = true;
  842. else data.target.selection_enabled = false;
  843. if (data.target_raw == (data.target_raw | 0x02)) data.target.default_enemy = true;
  844. else data.target.default_enemy = false;
  845. if (data.target_raw == (data.target_raw | 0x04)) data.target.default_multiple = true;
  846. else data.target.default_multiple = false;
  847. if (data.target_raw == (data.target_raw | 0x08)) data.target.toggle_multiple = true;
  848. else data.target.toggle_multiple = false;
  849. if (data.target_raw == (data.target_raw | 0x10)) data.target.fixed_row = true;
  850. else data.target.fixed_row = false;
  851. if (data.target_raw == (data.target_raw | 0x20)) data.target.short_range = true;
  852. else data.target.short_range = false;
  853. if (data.target_raw == (data.target_raw | 0x40)) data.target.default_enemy = true;
  854. else data.target.all_rows = false;
  855. if (data.target_raw == (data.target_raw | 0x80)) data.target.random = true;
  856. else data.target.random = false;
  857. data.damage_formula = data.damage_raw >> 4; // Upper nybble.
  858. data.damage_modifier = data.damage_raw & 15; // Lower nybble.
  859. data.animation_index = data.model_raw >> 4; // Upper nybble.
  860. data.model = data.model_raw & 15; // Lower nybble.
  861. if (data.equip_raw == (data.equip_raw | 0x0001)) data.equip.push_back(0);
  862. if (data.equip_raw == (data.equip_raw | 0x0002)) data.equip.push_back(1);
  863. if (data.equip_raw == (data.equip_raw | 0x0004)) data.equip.push_back(2);
  864. if (data.equip_raw == (data.equip_raw | 0x0008)) data.equip.push_back(3);
  865. if (data.equip_raw == (data.equip_raw | 0x0010)) data.equip.push_back(4);
  866. if (data.equip_raw == (data.equip_raw | 0x0020)) data.equip.push_back(5);
  867. if (data.equip_raw == (data.equip_raw | 0x0040)) data.equip.push_back(6);
  868. if (data.equip_raw == (data.equip_raw | 0x0080)) data.equip.push_back(7);
  869. if (data.equip_raw == (data.equip_raw | 0x0100)) data.equip.push_back(8);
  870. if (data.equip_raw == (data.equip_raw | 0x0200)) data.equip.push_back(9);
  871. if (data.equip_raw == (data.equip_raw | 0x0400)) data.equip.push_back(10);
  872. for (int s = 0; s < 4; s ++){
  873. if (data.stat_raw[s] < 0XFF && data.stat_bonus_raw[s] > 0){
  874. StatBonus stat_bonus;
  875. stat_bonus.stat = data.stat_raw[s];
  876. stat_bonus.bonus = data.stat_bonus_raw[s];
  877. data.stat_bonus.push_back(stat_bonus);
  878. }
  879. }
  880. for (int s = 0; s < 8; s ++){
  881. if (data.slots_raw[s] == 1 || data.slots_raw[s] == 5) data.materia_slots.push_back(SLOT_UNLINKED);
  882. else if (
  883. data.slots_raw[s] == 2 || data.slots_raw[s] == 3 || data.slots_raw[s] == 6 || data.slots_raw[s] == 7
  884. ) data.materia_slots.push_back(SLOT_LINKED);
  885. }
  886. if (data.element_raw == (data.element_raw | 0x0001)) data.elements.push_back(FIRE);
  887. if (data.element_raw == (data.element_raw | 0x0002)) data.elements.push_back(ICE);
  888. if (data.element_raw == (data.element_raw | 0x0004)) data.elements.push_back(BOLT);
  889. if (data.element_raw == (data.element_raw | 0x0008)) data.elements.push_back(EARTH);
  890. if (data.element_raw == (data.element_raw | 0x0010)) data.elements.push_back(POISON);
  891. if (data.element_raw == (data.element_raw | 0x0020)) data.elements.push_back(GRAVITY);
  892. if (data.element_raw == (data.element_raw | 0x0040)) data.elements.push_back(WATER);
  893. if (data.element_raw == (data.element_raw | 0x0080)) data.elements.push_back(WIND);
  894. if (data.element_raw == (data.element_raw | 0x0100)) data.elements.push_back(HOLY);
  895. if (data.element_raw == (data.element_raw | 0x0200)) data.elements.push_back(RESTORATIVE);
  896. if (data.element_raw == (data.element_raw | 0x0400)) data.elements.push_back(CUT);
  897. if (data.element_raw == (data.element_raw | 0x0800)) data.elements.push_back(HIT);
  898. if (data.element_raw == (data.element_raw | 0x1000)) data.elements.push_back(PUNCH);
  899. if (data.element_raw == (data.element_raw | 0x2000)) data.elements.push_back(SHOOT);
  900. if (data.element_raw == (data.element_raw | 0x4000)) data.elements.push_back(SHOUT);
  901. if (data.element_raw == (data.element_raw | 0x8000)) data.elements.push_back(HIDDEN);
  902. // In KERNEL.BIN, Non elemental item actually have all elements marked.
  903. // If that happens, clear them all.
  904. if (data.elements.size() == 16) data.elements.clear();
  905. // Add to the list of weapons.
  906. weapons_.push_back(data);
  907. weapon_count ++;
  908. }
  909. return weapon_count;
  910. }
  911. void KernelDataInstaller::WriteWeapons(std::string file_name){
  912. std::fstream file;
  913. file.open(file_name, std::ios::out);
  914. if (!file) return;
  915. for (WeaponData weapon : weapons_){
  916. file << "Game.Items[" << weapon.id + 128 << "] = {\n"
  917. << " type = Inventory.ITEM_TYPE.WEAPON,\n"
  918. << " id = " << weapon.id + 128 << ",\n"
  919. << " price = " << prices_[weapon.id + 128] << ",\n"
  920. << " name = \"" << weapon.name << "\",\n"
  921. << " description = \"" << weapon.description << "\",\n"
  922. << " camera = " << weapon.camera << ",\n"
  923. << " sell = " << weapon.sellable << ",\n"
  924. << " battle = " << weapon.useable_battle << ",\n"
  925. << " menu = " << weapon.useable_menu << ",\n"
  926. << " throw = " << weapon.throwable << ",\n"
  927. << " effect = " << static_cast<int>(weapon.effect) << ",\n"
  928. << " dmg_formula = " << static_cast<int>(weapon.damage_formula) << ",\n"
  929. << " dmg_modifier = " << static_cast<int>(weapon.damage_modifier) << ",\n"
  930. << " power = " << static_cast<int>(weapon.power) << ",\n"
  931. << " growth = " << static_cast<int>(weapon.growth) << ",\n"
  932. << " accuracy = " << static_cast<int>(weapon.accuracy) << ",\n"
  933. << " critical = " << static_cast<int>(weapon.critical) << ",\n"
  934. << " model = " << static_cast<int>(weapon.model) << ",\n"
  935. << " animation = " << static_cast<int>(weapon.animation_index) << ",\n"
  936. << " target = {selectable = " << weapon.target.selection_enabled
  937. << ", default_enemy = " << weapon.target.default_enemy
  938. << ", default_multiple = " << weapon.target.default_multiple
  939. << ", toggle_multiple = " << weapon.target.toggle_multiple
  940. << ", fixed_row = " << weapon.target.fixed_row
  941. << ", short_range = " << weapon.target.short_range
  942. << ", all_rows = " << weapon.target.all_rows
  943. << ", random = " << weapon.target.random << "},\n"
  944. << " elements = {";
  945. for (int s : weapon.elements) file << s << ", ";
  946. file << "},\n";
  947. file << " slots = {";
  948. for (u8 slot : weapon.materia_slots) file << static_cast<int>(slot) << ", ";
  949. file << "},\n stats = {";
  950. for (StatBonus s : weapon.stat_bonus){
  951. file << "\n " << GetBasicStatName(s.stat) << " = "
  952. << static_cast<int>(s.bonus) << ",";
  953. }
  954. if (weapon.stat_bonus.size() > 0) file << "\n },\n";
  955. else file << "},\n";
  956. file << " users = {";
  957. for (int s : weapon.equip) file << s << ", ";
  958. file << "}\n}\n";
  959. }
  960. file.close();
  961. }
  962. int KernelDataInstaller::ReadArmors(){
  963. // Empty the item list
  964. armors_.clear();
  965. File armor_file = kernel_.ExtractGZip(KERNEL_ARMOR_DATA);
  966. File armor_name_file = kernel_.ExtractGZip(KERNEL_ARMOR_NAMES);
  967. File armor_desc_file = kernel_.ExtractGZip(KERNEL_ARMOR_DESCRIPTIONS);
  968. int armor_count = 0;
  969. for (int i = 0; i < 32; i ++){ // There are exactly 32.
  970. ArmorData data;
  971. int name_offset;
  972. int desc_offset;
  973. // From the current name file offset, read 16 bytes.
  974. // This is the pointer to the offset for the item name.
  975. // Start reading there until 0xFF is found.
  976. data.name = "";
  977. u8 ch = 1;
  978. name_offset = armor_name_file.readU16LE();
  979. while (ch != 0xFF){
  980. ch = armor_name_file.GetU8(name_offset);
  981. if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
  982. name_offset ++;
  983. }
  984. boost::replace_all(data.name, "\"", "'");
  985. // For descriptions, do the same as for names.
  986. data.description = "";
  987. ch = 1;
  988. desc_offset = armor_desc_file.readU16LE();
  989. while (ch != 0xFF){
  990. ch = armor_desc_file.GetU8(desc_offset);
  991. if (ch != 0xFF){
  992. // Warning: If the char is 0xF9, special function!
  993. // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
  994. // This is to compress information in the kernel.
  995. if (ch == 0xF9){
  996. // Get how much to read, two upper bits of the next byte.
  997. // Multiply by 2 and add 4. That's just how it is.
  998. u8 comp_size = (armor_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
  999. // Get new offset, lower six bits of the next byte.
  1000. // The offset goes backwards from the position of the 0xF9 byte.
  1001. u8 comp_offset = armor_desc_file.GetU8(desc_offset + 1) & 63;
  1002. for (int c = 0; c < comp_size; c ++){
  1003. data.description += ENGLISH_CHARS[armor_desc_file.GetU8(desc_offset - 1 - comp_offset + c)];
  1004. }
  1005. desc_offset ++;
  1006. }
  1007. else data.description += ENGLISH_CHARS[ch];
  1008. }
  1009. desc_offset ++;
  1010. }
  1011. boost::replace_all(data.description, "\"", "'");
  1012. // If the armor doesn't have a name, it means all items have been read.
  1013. // break the loop.
  1014. if ("" == data.name) break;
  1015. // Read data from the armor data section.
  1016. data.unknown_0 = armor_file.readU8();
  1017. data.element_defense_mode = armor_file.readU8();
  1018. data.defense = armor_file.readU8();
  1019. data.m_defense = armor_file.readU8();
  1020. data.evasion = armor_file.readU8();
  1021. data.m_evasion = armor_file.readU8();
  1022. data.status = armor_file.readU8();
  1023. data.unknown_1 = armor_file.readU16LE();
  1024. for (int s = 0; s < 8; s ++) data.slots_raw[s] = armor_file.readU8();
  1025. data.growth = armor_file.readU8();
  1026. data.equip_raw = armor_file.readU16LE();
  1027. data.element_raw = armor_file.readU16LE();
  1028. data.unknown_2 = armor_file.readU16LE();
  1029. for (int s = 0; s < 4; s ++) data.stat_raw[s] = armor_file.readU8();
  1030. for (int s = 0; s < 4; s ++) data.stat_bonus_raw[s] = armor_file.readU8();
  1031. data.restrict_raw = armor_file.readU16LE();
  1032. data.unknown_3 = armor_file.readU16LE();
  1033. // Derive data from the read data.
  1034. data.id = i;
  1035. if (data.growth != 0 && data.growth != 2 && data.growth != 3) data.growth = 1;
  1036. data.sellable = !(data.restrict_raw & (1 << 0));
  1037. data.useable_battle = !(data.restrict_raw & (2 << 0));
  1038. data.useable_menu = !(data.restrict_raw & (4 << 0));
  1039. if (data.equip_raw == (data.equip_raw | 0x0001)) data.equip.push_back(0);
  1040. if (data.equip_raw == (data.equip_raw | 0x0002)) data.equip.push_back(1);
  1041. if (data.equip_raw == (data.equip_raw | 0x0004)) data.equip.push_back(2);
  1042. if (data.equip_raw == (data.equip_raw | 0x0008)) data.equip.push_back(3);
  1043. if (data.equip_raw == (data.equip_raw | 0x0010)) data.equip.push_back(4);
  1044. if (data.equip_raw == (data.equip_raw | 0x0020)) data.equip.push_back(5);
  1045. if (data.equip_raw == (data.equip_raw | 0x0040)) data.equip.push_back(6);
  1046. if (data.equip_raw == (data.equip_raw | 0x0080)) data.equip.push_back(7);
  1047. if (data.equip_raw == (data.equip_raw | 0x0100)) data.equip.push_back(8);
  1048. if (data.equip_raw == (data.equip_raw | 0x0200)) data.equip.push_back(9);
  1049. if (data.equip_raw == (data.equip_raw | 0x0400)) data.equip.push_back(10);
  1050. for (int s = 0; s < 4; s ++){
  1051. if (data.stat_raw[s] < 0XFF && data.stat_bonus_raw[s] > 0){
  1052. StatBonus stat_bonus;
  1053. stat_bonus.stat = data.stat_raw[s];
  1054. stat_bonus.bonus = data.stat_bonus_raw[s];
  1055. data.stat_bonus.push_back(stat_bonus);
  1056. }
  1057. }
  1058. for (int i = 0; i < 8; i ++){
  1059. if (data.slots_raw[i] == 1 || data.slots_raw[i] == 5){
  1060. data.materia_slots.push_back(SLOT_UNLINKED);
  1061. }
  1062. else if (
  1063. data.slots_raw[i] == 2 || data.slots_raw[i] == 3 || data.slots_raw[i] == 6 || data.slots_raw[i] == 7
  1064. ) data.materia_slots.push_back(SLOT_LINKED);
  1065. }
  1066. // In KERNEL.BIN, items with no elemental defense actually do have all elements marked,
  1067. // with mode 255. If, that's the case, consider that there are no elements.
  1068. if (data.element_defense_mode != 0xFF){
  1069. if (data.element_raw == (data.element_raw | 0x0001)) data.elements.push_back(FIRE);
  1070. if (data.element_raw == (data.element_raw | 0x0002)) data.elements.push_back(ICE);
  1071. if (data.element_raw == (data.element_raw | 0x0004)) data.elements.push_back(BOLT);
  1072. if (data.element_raw == (data.element_raw | 0x0008)) data.elements.push_back(EARTH);
  1073. if (data.element_raw == (data.element_raw | 0x0010)) data.elements.push_back(POISON);
  1074. if (data.element_raw == (data.element_raw | 0x0020)) data.elements.push_back(GRAVITY);
  1075. if (data.element_raw == (data.element_raw | 0x0040)) data.elements.push_back(WATER);
  1076. if (data.element_raw == (data.element_raw | 0x0080)) data.elements.push_back(WIND);
  1077. if (data.element_raw == (data.element_raw | 0x0100)) data.elements.push_back(HOLY);
  1078. if (data.element_raw == (data.element_raw | 0x0200)) data.elements.push_back(RESTORATIVE);
  1079. if (data.element_raw == (data.element_raw | 0x0400)) data.elements.push_back(CUT);
  1080. if (data.element_raw == (data.element_raw | 0x0800)) data.elements.push_back(HIT);
  1081. if (data.element_raw == (data.element_raw | 0x1000)) data.elements.push_back(PUNCH);
  1082. if (data.element_raw == (data.element_raw | 0x2000)) data.elements.push_back(SHOOT);
  1083. if (data.element_raw == (data.element_raw | 0x4000)) data.elements.push_back(SHOUT);
  1084. if (data.element_raw == (data.element_raw | 0x8000)) data.elements.push_back(HIDDEN);
  1085. }
  1086. // In KERNEL.BIN, Non elemental item actually have all elements marked.
  1087. // If that happens, clear them all.
  1088. if (data.elements.size() == 16) data.elements.clear();
  1089. // Add to the list of weapons.
  1090. armors_.push_back(data);
  1091. armor_count ++;
  1092. }
  1093. return armor_count;
  1094. }
  1095. void KernelDataInstaller::WriteArmors(std::string file_name){
  1096. std::fstream file;
  1097. file.open(file_name, std::ios::out);
  1098. if (!file) return;
  1099. for (ArmorData armor : armors_){
  1100. file << "Game.Items[" << armor.id + 256 << "] = {\n"
  1101. << " type = Inventory.ITEM_TYPE.ARMOR,\n"
  1102. << " id = " << armor.id + 256 << ",\n"
  1103. << " price = " << prices_[armor.id + 256] << ",\n"
  1104. << " name = \"" << armor.name << "\",\n"
  1105. << " description = \"" << armor.description << "\",\n"
  1106. << " sell = " << armor.sellable << ",\n"
  1107. << " battle = " << armor.useable_battle << ",\n"
  1108. << " menu = " << armor.useable_menu << ",\n"
  1109. << " throw = 0,\n"
  1110. << " defense = {physical = " << static_cast<int>(armor.defense) << ", magical = "
  1111. << static_cast<int>(armor.m_defense) << "},\n"
  1112. << " evasion = {physical = " << static_cast<int>(armor.evasion) << ", magical = "
  1113. << static_cast<int>(armor.m_evasion) << "},\n"
  1114. << " growth = " << static_cast<int>(armor.growth) << ",\n"
  1115. << " status = {";
  1116. if (armor.status != 0XFF) file << armor.status;
  1117. file
  1118. << "},\n"
  1119. << " elements = {";
  1120. for (int s : armor.elements){
  1121. file << "\n {id = " << s << ", mode = "
  1122. << static_cast<int>(armor.element_defense_mode) << "},";
  1123. }
  1124. if (armor.elements.size() > 0) file << "\n },\n";
  1125. else file << "},\n";
  1126. file << " slots = {";
  1127. for (u8 slot : armor.materia_slots) file << static_cast<int>(slot) << ", ";
  1128. file << "},\n stats = {";
  1129. for (StatBonus s : armor.stat_bonus){
  1130. file << "\n " << GetBasicStatName(s.stat) << " = "
  1131. << static_cast<int>(s.bonus) << ",";
  1132. }
  1133. if (armor.stat_bonus.size() > 0) file << "\n },\n";
  1134. else file << "},\n";
  1135. file << " users = {";
  1136. for (int s : armor.equip) file << s << ", ";
  1137. file << "}\n}\n";
  1138. }
  1139. file.close();
  1140. }
  1141. int KernelDataInstaller::ReadAccessories(){
  1142. // Empty the item list
  1143. accessories_.clear();
  1144. File accessory_file = kernel_.ExtractGZip(KERNEL_ACCESSORY_DATA);
  1145. File accessory_name_file = kernel_.ExtractGZip(KERNEL_ACCESSORY_NAMES);
  1146. File accessory_desc_file = kernel_.ExtractGZip(KERNEL_ACCESSORY_DESCRIPTIONS);
  1147. int accessory_count = 0;
  1148. for (int i = 0; i < 32; i ++){ // There are exactly 32.
  1149. AccessoryData data;
  1150. int name_offset;
  1151. int desc_offset;
  1152. // From the current name file offset, read 16 bytes.
  1153. // This is the pointer to the offset for the item name.
  1154. // Start reading there until 0xFF is found.
  1155. data.name = "";
  1156. u8 ch = 1;
  1157. name_offset = accessory_name_file.readU16LE();
  1158. while (ch != 0xFF){
  1159. ch = accessory_name_file.GetU8(name_offset);
  1160. if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
  1161. name_offset ++;
  1162. }
  1163. boost::replace_all(data.name, "\"", "'");
  1164. // For descriptions, do the same as for names.
  1165. data.description = "";
  1166. ch = 1;
  1167. desc_offset = accessory_desc_file.readU16LE();
  1168. while (ch != 0xFF){
  1169. ch = accessory_desc_file.GetU8(desc_offset);
  1170. if (ch != 0xFF){
  1171. // Warning: If the char is 0xF9, special function!
  1172. // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
  1173. // This is to compress information in the kernel.
  1174. if (ch == 0xF9){
  1175. // Get how much to read, two upper bits of the next byte.
  1176. // Multiply by 2 and add 4. That's just how it is.
  1177. u8 comp_size = (accessory_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
  1178. // Get new offset, lower six bits of the next byte.
  1179. // The offset goes backwards from the position of the 0xF9 byte.
  1180. u8 comp_offset = accessory_desc_file.GetU8(desc_offset + 1) & 63;
  1181. for (int c = 0; c < comp_size; c ++){
  1182. data.description += ENGLISH_CHARS[accessory_desc_file.GetU8(desc_offset - 1 - comp_offset + c)];
  1183. }
  1184. desc_offset ++;
  1185. }
  1186. else data.description += ENGLISH_CHARS[ch];
  1187. }
  1188. desc_offset ++;
  1189. }
  1190. boost::replace_all(data.description, "\"", "'");
  1191. // If the item doesn't have a name, it means all items have been read.
  1192. // break the loop.
  1193. if ("" == data.name) break;
  1194. // Read data from the accessory data section.
  1195. for (int s = 0; s < 2; s ++) data.stat_raw[s] = accessory_file.readU8();
  1196. for (int s = 0; s < 2; s ++) data.stat_bonus_raw[s] = accessory_file.readU8();
  1197. data.element_defense_mode = accessory_file.readU8();
  1198. data.effect = accessory_file.readU8();
  1199. data.element_raw = accessory_file.readU16LE();
  1200. data.status_raw = accessory_file.readU32LE();
  1201. data.equip_raw = accessory_file.readU16LE();
  1202. data.restrict_raw = accessory_file.readU16LE();
  1203. // Derive data from the read data.
  1204. data.id = i;
  1205. data.sellable = !(data.restrict_raw & (1 << 0));
  1206. data.useable_battle = !(data.restrict_raw & (2 << 0));
  1207. data.useable_menu = !(data.restrict_raw & (4 << 0));
  1208. if (data.status_raw == (data.status_raw | 0x00000001)) data.status.push_back(DEATH);
  1209. if (data.status_raw == (data.status_raw | 0x00000002)) data.status.push_back(NEAR_DEATH);
  1210. if (data.status_raw == (data.status_raw | 0x00000004)) data.status.push_back(SLEEP);
  1211. if (data.status_raw == (data.status_raw | 0x00000008)) data.status.push_back(POISONED);
  1212. if (data.status_raw == (data.status_raw | 0x00000010)) data.status.push_back(SADNESS);
  1213. if (data.status_raw == (data.status_raw | 0x00000020)) data.status.push_back(FURY);
  1214. if (data.status_raw == (data.status_raw | 0x00000040)) data.status.push_back(CONFU);
  1215. if (data.status_raw == (data.status_raw | 0x00000080)) data.status.push_back(SILENCE);
  1216. if (data.status_raw == (data.status_raw | 0x00000100)) data.status.push_back(HASTE);
  1217. if (data.status_raw == (data.status_raw | 0x00000200)) data.status.push_back(SLOW);
  1218. if (data.status_raw == (data.status_raw | 0x00000400)) data.status.push_back(STOP);
  1219. if (data.status_raw == (data.status_raw | 0x00000800)) data.status.push_back(FROG);
  1220. if (data.status_raw == (data.status_raw | 0x00001000)) data.status.push_back(SMALL);
  1221. if (data.status_raw == (data.status_raw | 0x00002000)) data.status.push_back(SLOW_NUMB);
  1222. if (data.status_raw == (data.status_raw | 0x00004000)) data.status.push_back(PETRIFY);
  1223. if (data.status_raw == (data.status_raw | 0x00008000)) data.status.push_back(REGEN);
  1224. if (data.status_raw == (data.status_raw | 0x00010000)) data.status.push_back(BARRIER);
  1225. if (data.status_raw == (data.status_raw | 0x00020000)) data.status.push_back(M_BARRIER);
  1226. if (data.status_raw == (data.status_raw | 0x00040000)) data.status.push_back(REFLECT);
  1227. if (data.status_raw == (data.status_raw | 0x00080000)) data.status.push_back(DUAL);
  1228. if (data.status_raw == (data.status_raw | 0x00100000)) data.status.push_back(SHIELD);
  1229. if (data.status_raw == (data.status_raw | 0x00200000)) data.status.push_back(D_SENTENCE);
  1230. if (data.status_raw == (data.status_raw | 0x00400000)) data.status.push_back(MANIPULATE);
  1231. if (data.status_raw == (data.status_raw | 0x00800000)) data.status.push_back(BERSERK);
  1232. if (data.status_raw == (data.status_raw | 0x01000000)) data.status.push_back(PEERLESS);
  1233. if (data.status_raw == (data.status_raw | 0x02000000)) data.status.push_back(PARALYSIS);
  1234. if (data.status_raw == (data.status_raw | 0x04000000)) data.status.push_back(DARKNESS);
  1235. if (data.status_raw == (data.status_raw | 0x08000000)) data.status.push_back(DUAL_DRAIN);
  1236. if (data.status_raw == (data.status_raw | 0x10000000)) data.status.push_back(DEATH_FORCE);
  1237. if (data.status_raw == (data.status_raw | 0x20000000)) data.status.push_back(RESIST);
  1238. if (data.status_raw == (data.status_raw | 0x40000000)) data.status.push_back(LUCKY_GIRL);
  1239. if (data.status_raw == (data.status_raw | 0x80000000)) data.status.push_back(IMPRISONED);
  1240. if (data.equip_raw == (data.equip_raw | 0x0001)) data.equip.push_back(0);
  1241. if (data.equip_raw == (data.equip_raw | 0x0002)) data.equip.push_back(1);
  1242. if (data.equip_raw == (data.equip_raw | 0x0004)) data.equip.push_back(2);
  1243. if (data.equip_raw == (data.equip_raw | 0x0008)) data.equip.push_back(3);
  1244. if (data.equip_raw == (data.equip_raw | 0x0010)) data.equip.push_back(4);
  1245. if (data.equip_raw == (data.equip_raw | 0x0020)) data.equip.push_back(5);
  1246. if (data.equip_raw == (data.equip_raw | 0x0040)) data.equip.push_back(6);
  1247. if (data.equip_raw == (data.equip_raw | 0x0080)) data.equip.push_back(7);
  1248. if (data.equip_raw == (data.equip_raw | 0x0100)) data.equip.push_back(8);
  1249. if (data.equip_raw == (data.equip_raw | 0x0200)) data.equip.push_back(9);
  1250. if (data.equip_raw == (data.equip_raw | 0x0400)) data.equip.push_back(10);
  1251. for (int s = 0; s < 2; s ++){
  1252. if (data.stat_raw[s] < 0XFF && data.stat_bonus_raw[s] > 0){
  1253. StatBonus stat_bonus;
  1254. stat_bonus.stat = data.stat_raw[s];
  1255. stat_bonus.bonus = data.stat_bonus_raw[s];
  1256. data.stat_bonus.push_back(stat_bonus);
  1257. }
  1258. }
  1259. // In KERNEL.BIN, items with no elemental defense actually do have all elements marked,
  1260. // with mode 255. If that's the case, consider that there are no elements.
  1261. if (data.element_defense_mode != 0xFF){
  1262. if (data.element_raw == (data.element_raw | 0x0001)) data.elements.push_back(FIRE);
  1263. if (data.element_raw == (data.element_raw | 0x0002)) data.elements.push_back(ICE);
  1264. if (data.element_raw == (data.element_raw | 0x0004)) data.elements.push_back(BOLT);
  1265. if (data.element_raw == (data.element_raw | 0x0008)) data.elements.push_back(EARTH);
  1266. if (data.element_raw == (data.element_raw | 0x0010)) data.elements.push_back(POISON);
  1267. if (data.element_raw == (data.element_raw | 0x0020)) data.elements.push_back(GRAVITY);
  1268. if (data.element_raw == (data.element_raw | 0x0040)) data.elements.push_back(WATER);
  1269. if (data.element_raw == (data.element_raw | 0x0080)) data.elements.push_back(WIND);
  1270. if (data.element_raw == (data.element_raw | 0x0100)) data.elements.push_back(HOLY);
  1271. if (data.element_raw == (data.element_raw | 0x0200)) data.elements.push_back(RESTORATIVE);
  1272. if (data.element_raw == (data.element_raw | 0x0400)) data.elements.push_back(CUT);
  1273. if (data.element_raw == (data.element_raw | 0x0800)) data.elements.push_back(HIT);
  1274. if (data.element_raw == (data.element_raw | 0x1000)) data.elements.push_back(PUNCH);
  1275. if (data.element_raw == (data.element_raw | 0x2000)) data.elements.push_back(SHOOT);
  1276. if (data.element_raw == (data.element_raw | 0x4000)) data.elements.push_back(SHOUT);
  1277. if (data.element_raw == (data.element_raw | 0x8000)) data.elements.push_back(HIDDEN);
  1278. }
  1279. // In KERNEL.BIN, Non elemental item actually have all elements marked.
  1280. // If that happens, clear them all.
  1281. if (data.elements.size() == 16) data.elements.clear();
  1282. // Add to the list of weapons.
  1283. accessories_.push_back(data);
  1284. accessory_count ++;
  1285. }
  1286. return accessory_count;
  1287. }
  1288. void KernelDataInstaller::WriteAccessories(std::string file_name){
  1289. std::fstream file;
  1290. file.open(file_name, std::ios::out);
  1291. if (!file) return;
  1292. for (AccessoryData accessory : accessories_){
  1293. file << "Game.Items[" << accessory.id + 287 << "] = {\n"
  1294. << " type = Inventory.ITEM_TYPE.ACCESSORY,\n"
  1295. << " id = " << accessory.id + 287 << ",\n"
  1296. << " price = " << prices_[accessory.id + 287] << ",\n"
  1297. << " name = \"" << accessory.name << "\",\n"
  1298. << " description = \"" << accessory.description << "\",\n"
  1299. << " sell = " << accessory.sellable << ",\n"
  1300. << " battle = " << accessory.useable_battle << ",\n"
  1301. << " menu = " << accessory.useable_menu << ",\n"
  1302. << " throw = 0,\n"
  1303. << " special_effect = {";
  1304. if (accessory.effect != 0XFF) file << static_cast<int>(accessory.effect);
  1305. file
  1306. << "},\n"
  1307. << " status = {";
  1308. for (int s : accessory.status) file << s << ", ";
  1309. file
  1310. << "},\n"
  1311. << " elements = {";
  1312. // Elements and their resistance mode (0 absorbs, 1 nullifies, 2 halves), as in armors.
  1313. for (int s : accessory.elements){
  1314. file << "\n {id = " << s << ", mode = "
  1315. << static_cast<int>(accessory.element_defense_mode) << "},";
  1316. }
  1317. if (accessory.elements.size() > 0) file << "\n ";
  1318. file << "},\n stats = {";
  1319. for (StatBonus s : accessory.stat_bonus){
  1320. file << "\n " << GetBasicStatName(s.stat) << " = "
  1321. << static_cast<int>(s.bonus) << ",";
  1322. }
  1323. if (accessory.stat_bonus.size() > 0) file << "\n },\n";
  1324. else file << "},\n";
  1325. file << " users = {";
  1326. for (int s : accessory.equip) file << s << ", ";
  1327. file << "}\n}\n";
  1328. }
  1329. file.close();
  1330. }
  1331. int KernelDataInstaller::ReadMateria(){
  1332. // Empty the item list
  1333. materia_.clear();
  1334. File materia_file = kernel_.ExtractGZip(KERNEL_MATERIA_DATA);
  1335. File materia_name_file = kernel_.ExtractGZip(KERNEL_MATERIA_NAMES);
  1336. File materia_desc_file = kernel_.ExtractGZip(KERNEL_MATERIA_DESCRIPTIONS);
  1337. int materia_count = 0;
  1338. for (int i = 0; i < 96; i ++){ // There are exactly 96.
  1339. MateriaData data;
  1340. int name_offset;
  1341. int desc_offset;
  1342. // From the current name file offset, read 16 bytes.
  1343. // This is the pointer to the offset for the item name.
  1344. // Start reading there until 0xFF is found.
  1345. data.name = "";
  1346. u8 ch = 1;
  1347. name_offset = materia_name_file.readU16LE();
  1348. while (ch != 0xFF){
  1349. ch = materia_name_file.GetU8(name_offset);
  1350. if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
  1351. name_offset ++;
  1352. }
  1353. boost::replace_all(data.name, "\"", "'");
  1354. // For descriptions, do the same as for names.
  1355. data.description = "";
  1356. ch = 1;
  1357. desc_offset = materia_desc_file.readU16LE();
  1358. while (ch != 0xFF){
  1359. ch = materia_desc_file.GetU8(desc_offset);
  1360. if (ch != 0xFF){
  1361. // Warning: If the char is 0xF9, special function!
  1362. // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
  1363. // This is to compress information in the kernel.
  1364. if (ch == 0xF9){
  1365. // Get how much to read, two upper bits of the next byte.
  1366. // Multiply by 2 and add 4. That's just how it is.
  1367. u8 comp_size = (materia_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
  1368. // Get new offset, lower six bits of the next byte.
  1369. // The offset goes backwards from the position of the 0xF9 byte.
  1370. u8 comp_offset = materia_desc_file.GetU8(desc_offset + 1) & 63;
  1371. for (int c = 0; c < comp_size; c ++){
  1372. data.description += ENGLISH_CHARS[materia_desc_file.GetU8(desc_offset - 1 - comp_offset + c)];
  1373. }
  1374. desc_offset ++;
  1375. }
  1376. else data.description += ENGLISH_CHARS[ch];
  1377. }
  1378. desc_offset ++;
  1379. }
  1380. boost::replace_all(data.description, "\"", "'");
  1381. // Read data from the materia data section.
  1382. for (int i = 0; i < 4; i ++) data.level_up_ap[i] = materia_file.readU16LE();
  1383. data.stat_raw = materia_file.readU8();
  1384. data.status_raw // WARNING: Status has only three bytes.
  1385. = materia_file.readU8() + (materia_file.readU8() << 8) + (materia_file.readU8() << 16);
  1386. data.element = materia_file.readU8();
  1387. data.type_raw = materia_file.readU8();
  1388. for (int i = 0; i < 6; i ++) data.attribute[i] = materia_file.readU8();
  1389. // If the materia doesn't have a name, don't bother processing the rest of the data,
  1390. // it's not going to be added to the list.
  1391. if ("" == data.name) continue;
  1392. // Derive data from the read data.
  1393. data.id = i;
  1394. // Status. 3 bytes, so only the first 24 can be present.
  1395. if (data.status_raw == (data.status_raw | 0x000001)) data.status.push_back(DEATH);
  1396. if (data.status_raw == (data.status_raw | 0x000002)) data.status.push_back(NEAR_DEATH);
  1397. if (data.status_raw == (data.status_raw | 0x000004)) data.status.push_back(SLEEP);
  1398. if (data.status_raw == (data.status_raw | 0x000008)) data.status.push_back(POISONED);
  1399. if (data.status_raw == (data.status_raw | 0x000010)) data.status.push_back(SADNESS);
  1400. if (data.status_raw == (data.status_raw | 0x000020)) data.status.push_back(FURY);
  1401. if (data.status_raw == (data.status_raw | 0x000040)) data.status.push_back(CONFU);
  1402. if (data.status_raw == (data.status_raw | 0x000080)) data.status.push_back(SILENCE);
  1403. if (data.status_raw == (data.status_raw | 0x000100)) data.status.push_back(HASTE);
  1404. if (data.status_raw == (data.status_raw | 0x000200)) data.status.push_back(SLOW);
  1405. if (data.status_raw == (data.status_raw | 0x000400)) data.status.push_back(STOP);
  1406. if (data.status_raw == (data.status_raw | 0x000800)) data.status.push_back(FROG);
  1407. if (data.status_raw == (data.status_raw | 0x001000)) data.status.push_back(SMALL);
  1408. if (data.status_raw == (data.status_raw | 0x002000)) data.status.push_back(SLOW_NUMB);
  1409. if (data.status_raw == (data.status_raw | 0x004000)) data.status.push_back(PETRIFY);
  1410. if (data.status_raw == (data.status_raw | 0x008000)) data.status.push_back(REGEN);
  1411. if (data.status_raw == (data.status_raw | 0x010000)) data.status.push_back(BARRIER);
  1412. if (data.status_raw == (data.status_raw | 0x020000)) data.status.push_back(M_BARRIER);
  1413. if (data.status_raw == (data.status_raw | 0x040000)) data.status.push_back(REFLECT);
  1414. if (data.status_raw == (data.status_raw | 0x080000)) data.status.push_back(DUAL);
  1415. if (data.status_raw == (data.status_raw | 0x100000)) data.status.push_back(SHIELD);
  1416. if (data.status_raw == (data.status_raw | 0x200000)) data.status.push_back(D_SENTENCE);
  1417. if (data.status_raw == (data.status_raw | 0x400000)) data.status.push_back(MANIPULATE);
  1418. if (data.status_raw == (data.status_raw | 0x800000)) data.status.push_back(BERSERK);
  1419. // Limit type to materia color.
  1420. data.type = data.type_raw & 15; // Lower nybble.
  1421. data.sub_type = data.type_raw & 15; // Upper nyble.
  1422. switch (data.type){
  1423. case 0x09:
  1424. case 0x0A:
  1425. data.type = static_cast<int>(MATERIA_TYPE::MAGIC);
  1426. break;
  1427. case 0x05:
  1428. data.type = static_cast<int>(MATERIA_TYPE::SUPPORT);
  1429. break;
  1430. case 0x02:
  1431. case 0x03:
  1432. case 0x06:
  1433. case 0x07:
  1434. case 0x08:
  1435. data.type = static_cast<int>(MATERIA_TYPE::COMMAND);
  1436. break;
  1437. case 0x0B:
  1438. case 0x0C:
  1439. data.type = static_cast<int>(MATERIA_TYPE::SUMMON);
  1440. break;
  1441. default:
  1442. data.type = static_cast<int>(MATERIA_TYPE::INDEPENDENT);
  1443. }
  1444. // Stat bonuses. Manually done.
  1445. data.stats.str = 0;
  1446. data.stats.vit = 0;
  1447. data.stats.mag = 0;
  1448. data.stats.spr = 0;
  1449. data.stats.dex = 0;
  1450. data.stats.lck = 0;
  1451. data.stats.hp = 0;
  1452. data.stats.mp = 0;
  1453. data.stats.change = false;
  1454. switch (data.stat_raw){
  1455. case 0x01:
  1456. data.stats.change = true;
  1457. data.stats.str = -2;
  1458. data.stats.vit = -1;
  1459. data.stats.mag = 2;
  1460. data.stats.spr = 1;
  1461. data.stats.dex = 0;
  1462. data.stats.lck = 0;
  1463. data.stats.hp = -5;
  1464. data.stats.mp = 5;
  1465. break;
  1466. case 0x02:
  1467. data.stats.change = true;
  1468. data.stats.str = -4;
  1469. data.stats.vit = -2;
  1470. data.stats.mag = 4;
  1471. data.stats.spr = 2;
  1472. data.stats.hp = -10;
  1473. data.stats.mp = 10;
  1474. break;
  1475. case 0x03:
  1476. data.stats.change = true;
  1477. data.stats.dex = 2;
  1478. data.stats.lck = -2;
  1479. break;
  1480. case 0x04:
  1481. data.stats.change = true;
  1482. data.stats.str = -1;
  1483. data.stats.vit = -1;
  1484. data.stats.mag = 1;
  1485. data.stats.spr = 1;
  1486. break;
  1487. case 0x05:
  1488. data.stats.change = true;
  1489. data.stats.str = 1;
  1490. data.stats.vit = 1;
  1491. data.stats.mag = -1;
  1492. data.stats.spr = -1;
  1493. break;
  1494. case 0x06:
  1495. data.stats.change = true;
  1496. data.stats.vit = 1;
  1497. break;
  1498. case 0x07:
  1499. data.stats.change = true;
  1500. data.stats.lck = 1;
  1501. break;
  1502. case 0x08:
  1503. data.stats.change = true;
  1504. data.stats.lck = -1;
  1505. break;
  1506. case 0x09:
  1507. data.stats.change = true;
  1508. data.stats.dex = -2;
  1509. break;
  1510. case 0x0A:
  1511. data.stats.change = true;
  1512. data.stats.dex = 2;
  1513. break;
  1514. case 0x0B:
  1515. data.stats.change = true;
  1516. data.stats.str = -1;
  1517. data.stats.mag = 1;
  1518. data.stats.hp = -2;
  1519. data.stats.mp = 2;
  1520. break;
  1521. case 0x0C:
  1522. data.stats.change = true;
  1523. data.stats.mag = 1;
  1524. data.stats.hp = -2;
  1525. data.stats.mp = 2;
  1526. break;
  1527. case 0x0D:
  1528. data.stats.change = true;
  1529. data.stats.mag = 1;
  1530. data.stats.spr = 1;
  1531. data.stats.hp = -5;
  1532. data.stats.mp = 5;
  1533. break;
  1534. case 0x0E:
  1535. data.stats.change = true;
  1536. data.stats.mag = 2;
  1537. data.stats.spr = 2;
  1538. data.stats.hp = -10;
  1539. data.stats.mp = 10;
  1540. break;
  1541. case 0x0F:
  1542. data.stats.change = true;
  1543. data.stats.mag = 4;
  1544. data.stats.spr = 4;
  1545. data.stats.hp = -10;
  1546. data.stats.mp = 15;
  1547. break;
  1548. case 0x10:
  1549. data.stats.change = true;
  1550. data.stats.mag = 8;
  1551. data.stats.spr = 8;
  1552. data.stats.hp = -10;
  1553. data.stats.mp = 20;
  1554. break;
  1555. }
  1556. // Attributtes. Very hardcoded!
  1557. /*u8 sub_type = data.type_raw >> 4;
  1558. u8 type = data.type_raw & 15;
  1559. switch (type){
  1560. case 0x09: // Magic, normal.
  1561. case 0x0A: // Magic, high tier.
  1562. // Always enable magic command.
  1563. // Each attribute is an attack unlocked each level.
  1564. data.command.push_back(2);
  1565. for (int a = 0; a < 6; a ++){
  1566. if (data.attribute[i] == 0xFF) data.attack[i] = -1;
  1567. else data.attack[i] = data.attribute[i];
  1568. data.attack_times[i] = -1;
  1569. }
  1570. break;
  1571. case 0x05: //Support materia.
  1572. switch (data.attribute[i]){
  1573. case 0x54: data.ability = "Materia.ABILITY.COMMAND_COUNTER"; break;
  1574. case 0x55: data.ability = "Materia.ABILITY.MAGIC_COUNTER"; break;
  1575. case 0x56: data.ability = "Materia.ABILITY.SNEAK_ATTACK"; break;
  1576. case 0x58: data.ability = "Materia.ABILITY.MP_TURBO"; break;
  1577. case 0x59: data.ability = "Materia.ABILITY.HP_ABSORB"; break;
  1578. case 0x5A: data.ability = "Materia.ABILITY.MP_ABSORB"; break;
  1579. case 0x5C: data.ability = "Materia.ABILITY.ADDED_CUT"; break;
  1580. case 0x5D: data.ability = "Materia.ABILITY.STEAL_AS_WELL"; break;
  1581. case 0x5E: data.ability = "Materia.ABILITY.ELEMENTAL"; break;
  1582. case 0x5F: data.ability = "Materia.ABILITY.ADDED_EFFECT"; break;
  1583. }
  1584. for (int a = 1; a < 6; a ++){
  1585. if (data.attribute[i] != 0xFF)
  1586. data.ability_parameters[a - 1] = data.attribute[i];
  1587. }
  1588. break;
  1589. case 0x02: // Command, non-existent.
  1590. for (int a = 0; a < 6; a ++)
  1591. if (data.attribute[i] != 0xFF) data.command.push_back(data.attribute[i]);
  1592. break;
  1593. case 0x03: // Command, W commands.
  1594. for (int a = 0; a < 6; a ++)
  1595. if (data.attribute[i] != 0xFF) data.command.push_back(data.attribute[i]);
  1596. break;
  1597. case 0x06: // Command, replace attack, different by level.
  1598. for (int a = 0; a < 6; a ++)
  1599. if (data.attribute[i] != 0xFF) data.command.push_back(data.attribute[i]);
  1600. break;
  1601. case 0x07: // E. Skill
  1602. data.command.push_back(13); break; // E.Skill.
  1603. break;
  1604. case 0x08: // Adds commands. Can be different by level.
  1605. for (int a = 0; a < 6; a ++)
  1606. if (data.attribute[i] != 0xFF) data.command.push_back(data.attribute[i]);
  1607. break;
  1608. case 0x0B: // Summon, normal.
  1609. case 0x0C: // Summon, high tier.
  1610. // Always enable Summon command (3), always enable attack in attribute 0.
  1611. // Attributes 1-5 are the times the summon can be used per battle. They are always
  1612. // 1-5.
  1613. data.command.push_back(3);
  1614. //data.attack.push_back(data.attribute[i]);
  1615. for (int a = 1; a <= 6; a ++){
  1616. data.attack_times[a - 1] = a;
  1617. }
  1618. break;
  1619. //default:
  1620. //data.type = static_cast<int>(MATERIA_TYPE::INDEPENDENT);
  1621. }*/
  1622. // Add to the list of materia.
  1623. materia_.push_back(data);
  1624. materia_count ++;
  1625. }
  1626. return materia_count;
  1627. }
  1628. void KernelDataInstaller::WriteMateria(std::string file_name){
  1629. std::fstream file;
  1630. file.open(file_name, std::ios::out);
  1631. if (!file) return;
  1632. for (MateriaData materia : materia_){
  1633. file << "Game.Materia[" << materia.id << "] = {\n"
  1634. << " price = " << prices_[materia.id + 320] << ",\n"
  1635. << " name = \"" << materia.name << "\",\n"
  1636. << " description = \"" << materia.description << "\",\n"
  1637. << " type = " << materia.type << ",\n"
  1638. // TODO Remove from here...
  1639. << " sub_type = " << (int)materia.sub_type << ",\n"
  1640. << " type_raw = " << (int)materia.type_raw << ",\n"
  1641. << " type_raw_upp = " << (int)(materia.type_raw >> 4) << ",\n"
  1642. << " type_raw_low = " << (int)(materia.type_raw & 15) << ",\n"
  1643. << " attributtes = {";
  1644. for (int i = 0; i < 6; i ++)
  1645. file << (int) materia.attribute[i] << ", ";
  1646. file << "},\n"
  1647. // TODO ... until here
  1648. << " levels_ap = {[1] = 0, ";
  1649. for (int i = 0; i < 4; i ++){
  1650. if (materia.level_up_ap[i] < 65535)
  1651. file << "[" << (i + 2) << "] = " << (materia.level_up_ap[i] * 100) << ", ";
  1652. }
  1653. file
  1654. << "},\n"
  1655. << " status = {";
  1656. for (int s : materia.status) file << s << ", ";
  1657. file
  1658. << "},\n"
  1659. << " elements = {";
  1660. if (materia.element != 0xFF) file << static_cast<int>(materia.element);
  1661. file << "},\n stats = {";
  1662. if (materia.stats.str != 0) file << "\n str = " << materia.stats.str << ",";
  1663. if (materia.stats.vit != 0) file << "\n vit = " << materia.stats.vit << ",";
  1664. if (materia.stats.mag != 0) file << "\n mag = " << materia.stats.mag << ",";
  1665. if (materia.stats.spr != 0) file << "\n spr = " << materia.stats.spr << ",";
  1666. if (materia.stats.dex != 0) file << "\n dex = " << materia.stats.dex << ",";
  1667. if (materia.stats.lck != 0) file << "\n lck = " << materia.stats.lck << ",";
  1668. if (materia.stats.hp != 0) file << "\n hp = " << materia.stats.hp << ",";
  1669. if (materia.stats.mp != 0) file << "\n mp = " << materia.stats.mp << ",";
  1670. if (materia.stats.change) file << "\n },\n";
  1671. else file << "},\n";
  1672. // Type-dependant
  1673. if (materia.type == static_cast<int>(MATERIA_TYPE::MAGIC)){
  1674. file << " magic = {\n";
  1675. for (int a = 0; a < 6; a ++){
  1676. if (materia.attribute[a] != 0xFF){
  1677. file << " [" << a + 1 << "] = " << static_cast<int>(materia.attribute[a])
  1678. << ", -- " << attack_names_[static_cast<int>(materia.attribute[a])] << "\n";
  1679. }
  1680. }
  1681. file << " }\n";
  1682. }
  1683. else if (materia.type == static_cast<int>(MATERIA_TYPE::SUMMON)){
  1684. file << " summon = " << static_cast<int>(materia.attribute[0]) << ", -- "
  1685. << attack_names_[static_cast<int>(materia.attribute[0])] << "\n";
  1686. file << " times = {";
  1687. for (int a = 1; a < 6; a ++)
  1688. file << "[" << a << "] = " << static_cast<int>(materia.attribute[a]) << ", ";
  1689. file << "}\n";
  1690. }
  1691. else if (materia.type == static_cast<int>(MATERIA_TYPE::COMMAND)){
  1692. switch (materia.sub_type){
  1693. case 0x02: // Command, replace attack.
  1694. file << " command = {\n";
  1695. for (int a = 0; a < 6; a ++){
  1696. if (materia.attribute[a] != 0xFF){
  1697. file << " [" << a + 1 << "] = "
  1698. << static_cast<int>(materia.attribute[a]) << ", -- "
  1699. << command_names_[static_cast<int>(materia.attribute[a])] << "\n";
  1700. }
  1701. }
  1702. file << " },\n command_replace = 1 -- Attack\n";
  1703. break;
  1704. case 0x03: // Command, W commands.
  1705. file << " command = {\n";
  1706. for (int a = 0; a < 6; a ++){
  1707. if (materia.attribute[a] != 0xFF){
  1708. file << " [" << a + 1 << "] = "
  1709. << static_cast<int>(materia.attribute[a]) << ", -- "
  1710. << command_names_[static_cast<int>(materia.attribute[a])] << "\n";
  1711. }
  1712. }
  1713. file << " },\n command_replace = "
  1714. << static_cast<int>(materia.attribute[0] - 19) << " -- "
  1715. << command_names_[static_cast<int>(materia.attribute[0] - 19)] << "\n";
  1716. break;
  1717. case 0x06: // Normal command.
  1718. case 0x07: // E.Skill.
  1719. file << " command = {\n";
  1720. for (int a = 0; a < 6; a ++){
  1721. if (materia.attribute[a] != 0xFF){
  1722. file << " [" << a + 1 << "] = "
  1723. << static_cast<int>(materia.attribute[a]) << ", -- "
  1724. << command_names_[static_cast<int>(materia.attribute[a])] << "\n";
  1725. }
  1726. }
  1727. file << " }\n";
  1728. break;
  1729. case 0x08: // Master command.
  1730. // TODO
  1731. break;
  1732. }
  1733. }
  1734. else if (materia.type == static_cast<int>(MATERIA_TYPE::SUPPORT)){
  1735. file << " link = {\n id = ";
  1736. switch (materia.attribute[0]){
  1737. case 0x51: file << "Materia.LINK.ALL,\n"; break;
  1738. case 0x54: file << "Materia.LINK.COMMAND_COUNTER,\n"; break;
  1739. case 0x55: file << "Materia.LINK.MAGIC_COUNTER,\n"; break;
  1740. case 0x56: file << "Materia.LINK.SNEAK_ATTACK,\n"; break;
  1741. case 0x57: file << "Materia.LINK.FINAL_ATTACK,\n"; break;
  1742. case 0x58: file << "Materia.LINK.MP_TURBO,\n"; break;
  1743. case 0x59: file << "Materia.LINK.MP_ABSORB,\n"; break;
  1744. case 0x5A: file << "Materia.LINK.HP_ABSORB,\n"; break;
  1745. case 0x5C: file << "Materia.LINK.ADDED_CUT,\n"; break;
  1746. case 0x5D: file << "Materia.LINK.STEAL_AS_WELL,\n"; break;
  1747. case 0x5E: file << "Materia.LINK.ELEMENTAL,\n"; break;
  1748. case 0x5F: file << "Materia.LINK.ADDED_EFFECT,\n"; break;
  1749. case 0x63: file << "Materia.LINK.QUADRA_MAGIC,\n"; break;
  1750. default: file << "nil,\n"; break;
  1751. }
  1752. file << " param = {";
  1753. for (int a = 1; a < 6; a ++){
  1754. if (materia.attribute[a] != 0xFF){
  1755. file << "[" << a << "] = " << static_cast<int>(materia.attribute[a]) << ", ";
  1756. }
  1757. }
  1758. file << "}\n }\n";
  1759. }
  1760. else if (materia.type == static_cast<int>(MATERIA_TYPE::INDEPENDENT)){
  1761. bool invert_attrs = false;
  1762. file << " ability = {\n id = ";
  1763. switch (materia.sub_type){
  1764. case 0x00: // Character effects
  1765. switch (materia.attribute[0]){
  1766. case 0x02: file << "Materia.ABILITY.MAG_PLUS,\n"; break;
  1767. case 0x04: file << "Materia.ABILITY.DEX_PLUS,\n"; break;
  1768. case 0x05: file << "Materia.ABILITY.LCK_PLUS,\n"; break;
  1769. case 0x08: file << "Materia.ABILITY.HP_PLUS,\n"; break;
  1770. case 0x09: file << "Materia.ABILITY.MP_PLUS,\n"; break;
  1771. case 0x0A: file << "Materia.ABILITY.EXP_PLUS,\n"; break;
  1772. case 0x0B: file << "Materia.ABILITY.COVER,\n"; break;
  1773. case 0x0C: file << "Materia.ABILITY.UNDERWATER,\n"; break;
  1774. case 0x50: file << "Materia.ABILITY.LONG_RANGE,\n"; break;
  1775. case 0x53: file << "Materia.ABILITY.COUNTER_ATTACK,\n"; break;
  1776. case 0x62: file << "Materia.ABILITY.HP_MP,\n"; break;
  1777. default: file << "nil,\n"; break;
  1778. }
  1779. break;
  1780. case 0x01: // Party effects
  1781. switch (materia.attribute[0]){
  1782. case 0x00: file << "Materia.ABILITY.MONEY_PLUS,\n"; break;
  1783. case 0x01: // Enemy Away / Enemy lure / Chocobo lure
  1784. if (materia.id == 7) file << "Materia.ABILITY.ENCOUNTER_DOWN,\n";
  1785. else if (materia.id == 8) file << "Materia.ABILITY.ENCOUNTER_UP,\n";
  1786. else if (materia.id == 9) file << "Materia.ABILITY.ENCOUNTER_CHOCOBO,\n";
  1787. else file << "nil,\n";
  1788. break;
  1789. case 0x03: file << "Materia.ABILITY.PREEMPTIVE,\n"; break;
  1790. default: file << "nil,\n"; break;
  1791. }
  1792. break;
  1793. case 0x04:
  1794. file << "Materia.ABILITY.MEGA_ALL,\n";
  1795. break;
  1796. default: file << "nil,\n"; break;
  1797. }
  1798. file << " param = {";
  1799. for (int a = 1; a < 6; a ++){
  1800. if (materia.attribute[a] != 0xFF){
  1801. file << "[" << a << "] = "
  1802. << static_cast<int>(materia.attribute[a]) << ", ";
  1803. }
  1804. }
  1805. file << "}\n }\n";
  1806. }
  1807. file << "}\n";
  1808. }
  1809. file.close();
  1810. }
  1811. int KernelDataInstaller::ReadKeyItems(){
  1812. // Empty the item list
  1813. key_items_.clear();
  1814. File key_items_name_file = kernel_.ExtractGZip(KERNEL_KEY_ITEM_NAMES);
  1815. File key_items_desc_file = kernel_.ExtractGZip(KERNEL_KEY_ITEM_DESCRIPTIONS);
  1816. int key_item_count = 0;
  1817. for (int i = 0; i < 128; i ++){
  1818. KeyItemData data;
  1819. int name_offset;
  1820. int desc_offset;
  1821. // From the current name file offset, read 16 bytes.
  1822. // This is the pointer to the offset for the item name.
  1823. // Start reading there until 0xFF is found.
  1824. data.name = "";
  1825. u8 ch = 1;
  1826. name_offset = key_items_name_file.readU16LE();
  1827. while (ch != 0xFF){
  1828. ch = key_items_name_file.GetU8(name_offset);
  1829. if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
  1830. name_offset ++;
  1831. }
  1832. boost::replace_all(data.name, "\"", "'");
  1833. // For descriptions, do the same as for names.
  1834. data.description = "";
  1835. ch = 1;
  1836. desc_offset = key_items_desc_file.readU16LE();
  1837. while (ch != 0xFF){
  1838. ch = key_items_desc_file.GetU8(desc_offset);
  1839. if (ch != 0xFF){
  1840. // Warning: If the char is 0xF9, special function!
  1841. // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
  1842. // This is to compress information in the kernel.
  1843. if (ch == 0xF9){
  1844. // Get how much to read, two upper bits of the next byte.
  1845. // Multiply by 2 and add 4. That's just how it is.
  1846. u8 comp_size = (key_items_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
  1847. // Get new offset, lower six bits of the next byte.
  1848. // The offset goes backwards from the position of the 0xF9 byte.
  1849. u8 comp_offset = key_items_desc_file.GetU8(desc_offset + 1) & 63;
  1850. for (int i = 0; i < comp_size; i ++){
  1851. data.description += ENGLISH_CHARS[key_items_desc_file.GetU8(desc_offset - 1 - comp_offset + i)];
  1852. }
  1853. desc_offset ++;
  1854. }
  1855. else data.description += ENGLISH_CHARS[ch];
  1856. }
  1857. desc_offset ++;
  1858. }
  1859. boost::replace_all(data.description, "\"", "'");
  1860. // If the key item doesn't have a name, it means all key items have been read.
  1861. // break the loop.
  1862. if ("" == data.name) break;
  1863. // Add to the list of items.
  1864. data.id = i;
  1865. key_items_.push_back(data);
  1866. key_item_count ++;
  1867. }
  1868. return key_item_count;
  1869. }
  1870. void KernelDataInstaller::WriteKeyItems(std::string file_name){
  1871. std::fstream file;
  1872. file.open(file_name, std::ios::out);
  1873. if (!file) return;
  1874. for (KeyItemData key_item : key_items_){
  1875. file << "Game.Key_Items[" << key_item.id << "] = {\n"
  1876. << " name = \"" << key_item.name << "\",\n description = \"" << key_item.description << "\"\n}\n";
  1877. }
  1878. file.close();
  1879. }
  1880. int KernelDataInstaller::ReadSummonNames(){
  1881. // Empty the summon list
  1882. key_items_.clear();
  1883. File summon_name_file = kernel_.ExtractGZip(KERNEL_SUMMON_ATTACK_NAMES);
  1884. int summon_count = 0;
  1885. for (int i = 0; i < 16; i ++){ // There are exactly 16.
  1886. SummonNameData data;
  1887. int name_offset;
  1888. // From the current name file offset, read 16 bytes.
  1889. // This is the pointer to the offset for the item name.
  1890. // Start reading there until 0xFF is found.
  1891. data.name = "";
  1892. u8 ch = 1;
  1893. name_offset = summon_name_file.readU16LE();
  1894. while (ch != 0xFF){
  1895. ch = summon_name_file.GetU8(name_offset);
  1896. if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
  1897. name_offset ++;
  1898. }
  1899. boost::replace_all(data.name, "\"", "'");
  1900. // If the key item doesn't have a name, it means all key items have been read.
  1901. // break the loop.
  1902. if ("" == data.name) break;
  1903. // Add to the list of items.
  1904. data.id = i;
  1905. summon_names_.push_back(data);
  1906. summon_count ++;
  1907. }
  1908. return summon_count;
  1909. }
  1910. void KernelDataInstaller::WriteSummonNames(std::string file_name){
  1911. std::fstream file;
  1912. file.open(file_name, std::ios::out);
  1913. if (!file) return;
  1914. for (SummonNameData summon : summon_names_)
  1915. file << "Game.Summon_Names[" << summon.id << "] = {\"" << summon.name << "\"}\n";
  1916. file.close();
  1917. }
  1918. void KernelDataInstaller::ReadInitialSaveMap(){
  1919. // Empty the item list
  1920. attacks_.clear();
  1921. File savemap_file = kernel_.ExtractGZip(KERNEL_INITIALIZATION_DATA);
  1922. savemap_file.WriteFile("/home/ivalentin/initialsavemap");
  1923. // The initial savemap in KERNEL.BIN startas at 0x0054, directly into Cloud's level.
  1924. // It skips the header (PC and PSX, checksum, preview data and window colour).
  1925. for (int i = 0; i < 9; i ++){
  1926. savemap_.characters[i].identifier = savemap_file.readU8();
  1927. savemap_.characters[i].level = savemap_file.readU8();
  1928. savemap_.characters[i].str = savemap_file.readU8();
  1929. savemap_.characters[i].vit = savemap_file.readU8();
  1930. savemap_.characters[i].mag = savemap_file.readU8();
  1931. savemap_.characters[i].spr = savemap_file.readU8();
  1932. savemap_.characters[i].dex = savemap_file.readU8();
  1933. savemap_.characters[i].lck = savemap_file.readU8();
  1934. savemap_.characters[i].str_bonus = savemap_file.readU8();
  1935. savemap_.characters[i].vit_bonus = savemap_file.readU8();
  1936. savemap_.characters[i].mag_bonus = savemap_file.readU8();
  1937. savemap_.characters[i].spr_bonus = savemap_file.readU8();
  1938. savemap_.characters[i].dex_bonus = savemap_file.readU8();
  1939. savemap_.characters[i].lck_bonus = savemap_file.readU8();
  1940. savemap_.characters[i].limit_level = savemap_file.readU8();
  1941. savemap_.characters[i].limit_bar = savemap_file.readU8();
  1942. bool name_end = false;
  1943. savemap_.characters[i].name = "";
  1944. for (int j = 0; j < 12; j ++){
  1945. savemap_.characters[i].name_raw[i] = savemap_file.readU8();
  1946. if (name_end == false){
  1947. if (savemap_.characters[i].name_raw[i] != 0XFF){
  1948. savemap_.characters[i].name += ENGLISH_CHARS[savemap_.characters[i].name_raw[i]];
  1949. }
  1950. else name_end = true;
  1951. }
  1952. }
  1953. savemap_.characters[i].weapon = savemap_file.readU8();
  1954. savemap_.characters[i].armor = savemap_file.readU8();
  1955. savemap_.characters[i].accessory = savemap_file.readU8();
  1956. savemap_.characters[i].status_raw = savemap_file.readU8();
  1957. if (savemap_.characters[i].status_raw == (savemap_.characters[i].status_raw | 0x10)){
  1958. savemap_.characters[i].sadness = true;
  1959. savemap_.characters[i].fury = false;
  1960. }
  1961. else if (savemap_.characters[i].status_raw == (savemap_.characters[i].status_raw | 0x20)){
  1962. savemap_.characters[i].sadness = false;
  1963. savemap_.characters[i].fury = true;
  1964. }
  1965. else{
  1966. savemap_.characters[i].sadness = false;
  1967. savemap_.characters[i].fury = false;
  1968. }
  1969. savemap_.characters[i].row_raw = savemap_file.readU8();
  1970. if (savemap_.characters[i].row_raw == 0xFE) savemap_.characters[i].back_row = true;
  1971. else savemap_.characters[i].back_row = false;
  1972. savemap_.characters[i].level_progress = savemap_file.readU8();
  1973. savemap_.characters[i].learned_limits_raw = savemap_file.readU16LE();
  1974. for (int j = 0; j < 4; j ++){
  1975. savemap_.characters[i].limits_learned[j][0] = false;
  1976. savemap_.characters[i].limits_learned[j][1] = false;
  1977. }
  1978. if (savemap_.characters[i].learned_limits_raw == (savemap_.characters[i].learned_limits_raw | 0x0001))
  1979. savemap_.characters[i].limits_learned[0][0] = true;
  1980. if (savemap_.characters[i].learned_limits_raw == (savemap_.characters[i].learned_limits_raw | 0x0002))
  1981. savemap_.characters[i].limits_learned[0][1] = true;
  1982. if (savemap_.characters[i].learned_limits_raw == (savemap_.characters[i].learned_limits_raw | 0x0008))
  1983. savemap_.characters[i].limits_learned[1][0] = true;
  1984. if (savemap_.characters[i].learned_limits_raw == (savemap_.characters[i].learned_limits_raw | 0x0010))
  1985. savemap_.characters[i].limits_learned[1][1] = true;
  1986. if (savemap_.characters[i].learned_limits_raw == (savemap_.characters[i].learned_limits_raw | 0x0040))
  1987. savemap_.characters[i].limits_learned[2][0] = true;
  1988. if (savemap_.characters[i].learned_limits_raw == (savemap_.characters[i].learned_limits_raw | 0x0080))
  1989. savemap_.characters[i].limits_learned[2][1] = true;
  1990. if (savemap_.characters[i].learned_limits_raw == (savemap_.characters[i].learned_limits_raw | 0x0200))
  1991. savemap_.characters[i].limits_learned[3][0] = true;
  1992. savemap_.characters[i].kills = savemap_file.readU16LE();
  1993. savemap_.characters[i].limit_uses[0] = savemap_file.readU16LE();
  1994. savemap_.characters[i].limit_uses[1] = savemap_file.readU16LE();
  1995. savemap_.characters[i].limit_uses[2] = savemap_file.readU16LE();
  1996. savemap_.characters[i].hp = savemap_file.readU16LE();
  1997. savemap_.characters[i].base_hp = savemap_file.readU16LE();
  1998. savemap_.characters[i].mp = savemap_file.readU16LE();
  1999. savemap_.characters[i].base_mp = savemap_file.readU16LE();
  2000. savemap_.characters[i].unknown = savemap_file.readU32LE();
  2001. savemap_.characters[i].max_hp = savemap_file.readU16LE();
  2002. savemap_.characters[i].max_mp = savemap_file.readU16LE();
  2003. savemap_.characters[i].exp = savemap_file.readU32LE();
  2004. for (int m = 0; m < 8; m ++){
  2005. savemap_.characters[i].weapon_materia[m].id = savemap_file.readU8();
  2006. // TODO: Identify Enemy Skill, do special treatment
  2007. savemap_.characters[i].weapon_materia[m].ap // WARNING: AP has only three bytes.
  2008. = savemap_file.readU8() + (savemap_file.readU8() << 8) + (savemap_file.readU8() << 16);
  2009. }
  2010. for (int m = 0; m < 8; m ++){
  2011. savemap_.characters[i].armor_materia[m].id = savemap_file.readU8();
  2012. // TODO: Identify Enemy Skill, do special treatment
  2013. savemap_.characters[i].armor_materia[m].ap // WARNING: AP has only three bytes.
  2014. = savemap_file.readU8() + (savemap_file.readU8() << 8) + (savemap_file.readU8() << 16);
  2015. }
  2016. savemap_.characters[i].exp_to_next = savemap_file.readU32LE();
  2017. }
  2018. // The initial savemap ends up after character data, end now.
  2019. }
  2020. void KernelDataInstaller::WriteInitialSaveMap(std::string file_name){
  2021. // The initial save map only contains character data.
  2022. std::fstream file;
  2023. file.open(file_name, std::ios::out);
  2024. if (!file) return;
  2025. for (int c = 0; c < 9; c ++){
  2026. int total_count; // Utility to count in loops.
  2027. SaveMap::Character character = savemap_.characters[c];
  2028. file
  2029. << "Characters[" << c << "] = {\n"
  2030. << " char_id = " << static_cast<int>(character.identifier) << ",\n"
  2031. << " name = \"" << character.name << "\",\n"
  2032. << " enabled = false,\n"
  2033. << " locked = false,\n"
  2034. << " level = " << static_cast<int>(character.level) << ",\n"
  2035. << " kills = " << character.kills << ",\n"
  2036. << " back_row = " << (character.back_row ? "true" : "false") << ",\n"
  2037. << " exp = " << character.exp << ",\n"
  2038. << " exp_to_next = " << character.exp_to_next << ",\n"
  2039. << " stats = {\n"
  2040. << " str = {base = " << static_cast<int>(character.str)
  2041. << ", bonus = " << static_cast<int>(character.str_bonus)
  2042. << ", total = " << static_cast<int>(character.str + character.str_bonus) << "},\n"
  2043. << " vit = {base = " << static_cast<int>(character.vit)
  2044. << ", bonus = " << static_cast<int>(character.vit_bonus)
  2045. << ", total = " << static_cast<int>(character.vit + character.vit_bonus) << "},\n"
  2046. << " mag = {base = " << static_cast<int>(character.mag)
  2047. << ", bonus = " << static_cast<int>(character.mag_bonus)
  2048. << ", total = " << static_cast<int>(character.mag + character.mag_bonus) << "},\n"
  2049. << " spr = {base = " << static_cast<int>(character.spr)
  2050. << ", bonus = " << static_cast<int>(character.spr_bonus)
  2051. << ", total = " << static_cast<int>(character.spr + character.spr_bonus) << "},\n"
  2052. << " dex = {base = " << static_cast<int>(character.dex)
  2053. << ", bonus = " << static_cast<int>(character.dex_bonus)
  2054. << ", total = " << static_cast<int>(character.dex + character.dex_bonus) << "},\n"
  2055. << " lck = {base = " << static_cast<int>(character.lck)
  2056. << ", bonus = " << static_cast<int>(character.lck_bonus)
  2057. << ", total = " << static_cast<int>(character.lck + character.lck_bonus) << "},\n"
  2058. << " hp = {current = " << character.hp << ", base = " << character.base_hp
  2059. << ", max = " << character.max_hp << "},\n"
  2060. << " mp = {current = " << character.mp << ", base = " << character.base_mp
  2061. << ", max = " << character.max_mp << "},\n"
  2062. << " atk = 0,\n"
  2063. << " acc = 0,\n"
  2064. << " def = 0,\n"
  2065. << " eva = 0,\n"
  2066. << " matk = 0,\n"
  2067. << " mdef = 0,\n"
  2068. << " meva = 0\n"
  2069. << " },\n"
  2070. << " limit = {\n"
  2071. << " current = " << static_cast<int>(character.limit_level) << ",\n"
  2072. << " bar = " << static_cast<int>(character.limit_bar) << ",\n"
  2073. << " learned = {\n"
  2074. << " [1] = {[1] = " << (character.limits_learned[0][0] == 1 ? "true" : "false")
  2075. << ", [2] = " << (character.limits_learned[0][1] == 1 ? "true" : "false") << "},\n"
  2076. << " [2] = {[1] = " << (character.limits_learned[1][0] == 1 ? "true" : "false")
  2077. << ", [2] = " << (character.limits_learned[1][1] == 1 ? "true" : "false") << "},\n"
  2078. << " [3] = {[1] = " << (character.limits_learned[2][0] == 1 ? "true" : "false")
  2079. << ", [2] = " << (character.limits_learned[2][1] == 1 ? "true" : "false") << "},\n"
  2080. << " [4] = {[1] = " << (character.limits_learned[3][0] == 1 ? "true" : "false")
  2081. << ", [2] = " << (character.limits_learned[3][1] == 1 ? "true" : "false") << "}\n"
  2082. << " },\n"
  2083. << " uses = {[1] = " << character.limit_uses[0] << ", [2] = "
  2084. << character.limit_uses[1] << ", [3] = " << character.limit_uses[2] << ", [4] = 0}\n"
  2085. << " },\n"
  2086. << " weapon = {\n"
  2087. << " id = " << static_cast<int>(character.weapon) + 128 << ",\n"
  2088. << " materia = {";
  2089. total_count = 0;
  2090. for (int m = 0; m < 8; m ++){
  2091. if (character.weapon_materia[m].id < 255){
  2092. total_count ++;
  2093. file << "\n [" << m << "] = {id = "
  2094. << static_cast<int>(character.weapon_materia[m].id) << ", ap = "
  2095. << character.weapon_materia[m].ap << "},";
  2096. }
  2097. }
  2098. if (total_count > 0) file << "\n }\n },\n";
  2099. else file << "}\n },\n";
  2100. file
  2101. << " armor = {\n"
  2102. << " id = " << static_cast<int>(character.armor) + 256 << ",\n"
  2103. << " materia = {";
  2104. total_count = 0;
  2105. for (int m = 0; m < 8; m ++){
  2106. if (character.armor_materia[m].id < 255){
  2107. total_count ++;
  2108. file << "\n [" << m << "] = {id = "
  2109. << static_cast<int>(character.armor_materia[m].id) << ", ap = "
  2110. << character.armor_materia[m].ap << "},";
  2111. }
  2112. }
  2113. if (total_count > 0) file << "\n }\n },\n";
  2114. else file << "}\n },\n";
  2115. file << " accessory = ";
  2116. if (character.accessory < 0xFF) file << static_cast<int>(character.accessory) + 287;
  2117. else file << "nil";
  2118. file << ",\n status = {";
  2119. if (character.fury) file << FURY;
  2120. if (character.sadness) file << SADNESS;
  2121. file << "}\n}\nCharacters.recalculate_stats(" << c << ")\n\n";
  2122. }
  2123. file.close();
  2124. }
  2125. std::string KernelDataInstaller::GetBasicStatName(int id){
  2126. std::string name = "stat";
  2127. switch (id){
  2128. case 0: name = "str"; break;
  2129. case 1: name = "vit"; break;
  2130. case 2: name = "mag"; break;
  2131. case 3: name = "spr"; break;
  2132. case 4: name = "dex"; break;
  2133. case 5: name = "lck"; break;
  2134. }
  2135. return name;
  2136. }