KernelAttacks.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 <fstream>
  16. #include <iostream>
  17. #include <memory>
  18. #include "installer/extractors/KernelDataInstaller.h"
  19. #include "installer/extractors/KernelDataParts.h"
  20. #include "installer/extractors/KernelTextList.h"
  21. using namespace KernelData;
  22. int KernelDataInstaller::ReadCommands(){
  23. // Empty the command list
  24. commands_.clear();
  25. File command_file = kernel_.ExtractGZip(KERNEL_COMMAND_DATA);
  26. KernelTextList names(kernel_.ExtractGZip(KERNEL_COMMAND_NAMES));
  27. KernelTextList descriptions(kernel_.ExtractGZip(KERNEL_COMMAND_DESCRIPTIONS));
  28. int command_count = 0;
  29. // There are exactly 32 commands. Some of them are blank, so there is not really a
  30. // reliable exit condition.
  31. for (int i = 0; i < 32; i ++){
  32. CommandData data;
  33. data.name = names.NextName();
  34. data.description = descriptions.NextDescription();
  35. // Read data from the command m, data section.
  36. data.cursor_action = command_file.readU8();
  37. data.target_raw = command_file.readU8();
  38. data.unknown = command_file.readU16LE();
  39. data.camera_single = command_file.readU16LE();
  40. data.camera_multiple = command_file.readU16LE();
  41. // Derive data from the read data.
  42. data.id = i;
  43. if (data.target_raw == (data.target_raw | 0x01)) data.target.selection_enabled = true;
  44. else data.target.selection_enabled = false;
  45. if (data.target_raw == (data.target_raw | 0x02)) data.target.default_enemy = true;
  46. else data.target.default_enemy = false;
  47. // Default multiple is inverted
  48. if (data.target_raw == (data.target_raw | 0x04)) data.target.default_multiple = false;
  49. else data.target.default_multiple = true;
  50. if (data.target_raw == (data.target_raw | 0x08)) data.target.toggle_multiple = true;
  51. else data.target.toggle_multiple = false;
  52. if (data.target_raw == (data.target_raw | 0x10)) data.target.fixed_row = true;
  53. else data.target.fixed_row = false;
  54. if (data.target_raw == (data.target_raw | 0x20)) data.target.short_range = true;
  55. else data.target.short_range = false;
  56. if (data.target_raw == (data.target_raw | 0x40)) data.target.all_rows = true;
  57. else data.target.all_rows = false;
  58. if (data.target_raw == (data.target_raw | 0x80)) data.target.random = true;
  59. else data.target.random = false;
  60. switch (data.cursor_action){
  61. case NONE: data.menu = "Battle.Menu.NONE"; break;
  62. case MAGIC: data.menu = "Battle.Menu.MAGIC"; break;
  63. case SUMMON: data.menu = "Battle.Menu.SUMMON"; break;
  64. case ITEM: data.menu = "Battle.Menu.ITEM"; break;
  65. case E_SKILL: data.menu = "Battle.Menu.E_SKILL"; break;
  66. case THROW: data.menu = "Battle.Menu.THROW"; break;
  67. case LIMIT: data.menu = "Battle.Menu.LIMIT"; break;
  68. case TARGET: data.menu = "Battle.Menu.TARGET"; break;
  69. case W_MAGIC: data.menu = "Battle.Menu.W_MAGIC"; break;
  70. case W_SUMMON: data.menu = "Battle.Menu.W_SUMMON"; break;
  71. case W_ITEM: data.menu = "Battle.Menu.W_ITEM"; break;
  72. case COIN: data.menu = "Battle.Menu.COIN"; break;
  73. default: data.menu = "Battle.Menu.NONE";
  74. }
  75. // Add to the list of commands.
  76. if (data.name != ""){
  77. command_names_[i] = data.name;
  78. commands_.push_back(data);
  79. command_count ++;
  80. }
  81. }
  82. return command_count;
  83. }
  84. void KernelDataInstaller::WriteCommands(std::string file_name){
  85. std::fstream file;
  86. file.open(file_name, std::ios::out);
  87. if (!file) return;
  88. for (CommandData command : commands_){
  89. file << "Game.Commands[" << command.id << "] = {\n"
  90. << " name = \"" << command.name << "\",\n"
  91. << " description = \"" << command.description << "\",\n"
  92. << " menu = " << command.menu << ",\n"
  93. << " camera = {single = " << command.camera_single << ", "
  94. << "multiple = " << command.camera_multiple << "}\n}\n";
  95. }
  96. file.close();
  97. }
  98. int KernelDataInstaller::ReadAttacks(){
  99. // Empty the item list
  100. attacks_.clear();
  101. File attacks_file = kernel_.ExtractGZip(KERNEL_ATTACK_DATA);
  102. KernelTextList names(kernel_.ExtractGZip(KERNEL_ATTACK_NAMES));
  103. KernelTextList descriptions(kernel_.ExtractGZip(KERNEL_ATTACK_DESCRIPTIONS));
  104. int attack_count = 0;
  105. limit_attacks_.clear();
  106. // Limit data, from ff7.exe (file offset of the table at 0x910d94, in the .data section).
  107. const int LIMIT_TABLE_OFFSET = 0x910d94 - 0x7b0000 + 0x3ae000;
  108. std::unique_ptr<File> exe_file;
  109. try{
  110. exe_file.reset(new File(exe_path_));
  111. }
  112. catch (...){
  113. std::cerr << "[WARNING] Can't read the data of the limits from ff7.exe." << std::endl;
  114. }
  115. // The kernel has data for 128 attacks, but names and descriptions for 256. The last 128 are the
  116. // limit break techniques, which the characters refer to by ID (128 and up).
  117. for (int i = 0; i < 256; i ++){
  118. AttackData data;
  119. data.name = names.NextName();
  120. data.description = descriptions.NextDescription();
  121. // Limit techniques only have name and description. Both start with control codes, drop
  122. // everything before the first letter.
  123. // The data of the limits that the characters use (128 to 183) is not in the kernel: it is a table of
  124. // attack records, with the same format, in the data of ff7.exe (0x910d94, indexed by the limit ID minus
  125. // 128; found from the code that ff7.exe uses to load the limits of a character, 0x5c9524).
  126. File* source = &attacks_file;
  127. bool limit_from_exe = false;
  128. if (i >= 128 && i <= 183 && exe_file != nullptr){
  129. exe_file->SetOffset(LIMIT_TABLE_OFFSET + (i - 128) * 0x1c);
  130. source = exe_file.get();
  131. limit_from_exe = true;
  132. }
  133. if (i >= 128){
  134. for (std::string* text : {&data.name, &data.description}){
  135. size_t first = text->find_first_of(
  136. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
  137. if (first == std::string::npos) text->clear();
  138. else text->erase(0, first);
  139. }
  140. data.id = i;
  141. if (!limit_from_exe){
  142. limit_attacks_.push_back(data);
  143. continue;
  144. }
  145. }
  146. // Read data from the item data section.
  147. data.accuracy = source->readU8();
  148. data.impact_effect = source->readU8();
  149. data.hurt_anim = source->readU8();
  150. data.unknown = source->readU8();
  151. data.mp = source->readU16LE();
  152. data.sound = source->readU16LE();
  153. data.camera_single = source->readU16LE();
  154. data.camera_multiple = source->readU16LE();
  155. data.target_raw = source->readU8();
  156. data.effect = source->readU8();
  157. data.damage_raw = source->readU8();
  158. data.power = source->readU8();
  159. data.condition_raw = source->readU8();
  160. data.status_change_raw = source->readU8();
  161. data.additional_effects_raw = source->readU8();
  162. data.additional_effects_mod_raw = source->readU8();
  163. data.status_raw = source->readU32LE();
  164. data.element_raw = source->readU16LE();
  165. data.special_raw = source->readU16LE();
  166. // Derive data from the read data.
  167. data.id = i;
  168. ReadTarget(data.target_raw, data.target);
  169. data.damage_formula = data.damage_raw >> 4; // Upper nybble.
  170. data.damage_modifier = data.damage_raw & 15; // Lower nybble.
  171. data.restore_type = ReadRestoreType(data.condition_raw);
  172. ReadStatusEffect(data.status_change_raw, data.status_raw, data.status);
  173. ReadElements(data.element_raw, data.elements);
  174. // Add to the list of items.
  175. attacks_.push_back(data);
  176. attack_names_[i] = data.name;
  177. attack_count ++;
  178. }
  179. return attack_count;
  180. }
  181. void KernelDataInstaller::WriteAttacks(std::string file_name){
  182. std::fstream file;
  183. file.open(file_name, std::ios::out);
  184. if (!file) return;
  185. for (AttackData attack : attacks_){
  186. file << "Game.Attacks[" << attack.id << "] = {\n"
  187. << " name = \"" << attack.name << "\",\n"
  188. << " description = \"" << attack.description << "\",\n"
  189. << " accuracy = " << static_cast<int>(attack.accuracy) << ",\n"
  190. << " impact_effect = " << static_cast<int>(attack.impact_effect) << ",\n"
  191. << " hurt_anim = " << static_cast<int>(attack.hurt_anim) << ",\n"
  192. << " mp = " << attack.mp << ",\n"
  193. << " sound = " << attack.sound << ",\n"
  194. << " camera = {single = " << attack.camera_single << ", "
  195. << "multiple = " << attack.camera_multiple << "},\n"
  196. << " effect = " << static_cast<int>(attack.effect) << ",\n"
  197. << " dmg_formula = " << static_cast<int>(attack.damage_formula) << ",\n"
  198. << " dmg_modifier = " << static_cast<int>(attack.damage_modifier) << ",\n"
  199. << " power = " << static_cast<int>(attack.power) << ",\n"
  200. << " additional = {effect = " << static_cast<int>(attack.additional_effects_raw)
  201. << ", modifier = " << static_cast<int>(attack.additional_effects_mod_raw) << "},\n"
  202. << " restore = " << attack.restore_type << ",\n";
  203. WriteTarget(file, attack.target);
  204. file << " elements = {";
  205. for (int element : attack.elements) file << element << ", ";
  206. file << "},\n status = {";
  207. // Status effects: ID (bit position), mode (0 inflict, 1 cure, 2 toggle) and chance out of 63.
  208. for (int status : attack.status.status){
  209. file << "{status = " << status << ", mode = " << attack.status.mode
  210. << ", chance = " << attack.status.chance << "}, ";
  211. }
  212. file << "}\n}\n";
  213. }
  214. // Limit techniques: only name and description are known.
  215. for (AttackData attack : limit_attacks_){
  216. file << "Game.Attacks[" << attack.id << "] = {\n"
  217. << " name = \"" << attack.name << "\",\n"
  218. << " description = \"" << attack.description << "\"\n"
  219. << "}\n";
  220. }
  221. file.close();
  222. }
  223. int KernelDataInstaller::ReadSummonNames(){
  224. // Empty the summon list
  225. summon_names_.clear();
  226. KernelTextList names(kernel_.ExtractGZip(KERNEL_SUMMON_ATTACK_NAMES));
  227. int summon_count = 0;
  228. for (int i = 0; i < 16; i ++){ // There are exactly 16.
  229. SummonNameData data;
  230. data.name = names.NextName();
  231. // If the key item doesn't have a name, it means all key items have been read.
  232. // break the loop.
  233. if ("" == data.name) break;
  234. // Add to the list of items.
  235. data.id = i;
  236. summon_names_.push_back(data);
  237. summon_count ++;
  238. }
  239. return summon_count;
  240. }
  241. void KernelDataInstaller::WriteSummonNames(std::string file_name){
  242. std::fstream file;
  243. file.open(file_name, std::ios::out);
  244. if (!file) return;
  245. for (SummonNameData summon : summon_names_)
  246. file << "Game.Summon_Names[" << summon.id << "] = {\"" << summon.name << "\"}\n";
  247. file.close();
  248. }