BattleDataInstaller.cpp 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. /*
  2. * V-Gears
  3. * Copyright (C) 2022 V-Gears Team
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <iostream>
  19. #include <fstream>
  20. #include <sstream>
  21. #include <map>
  22. #include <set>
  23. #include <cctype>
  24. #include <cstdio>
  25. #include <cstdlib>
  26. #include <algorithm>
  27. #include <zlib.h>
  28. #include <tinyxml.h>
  29. #include <boost/filesystem.hpp>
  30. #include <OgreMesh.h>
  31. #include "BattleDataInstaller.h"
  32. #include "BattleCameraScripts.h"
  33. #include "ExeEffectCameras.h"
  34. #include "ExeEffectMeshes.h"
  35. #include "ModelMaterials.h"
  36. #include "TexFile.h"
  37. #include "data/VGearsHRCFileManager.h"
  38. #include "data/VGearsAFileManager.h"
  39. #include "data/AbFile.h"
  40. #include "data/DaFile.h"
  41. #include "data/FF7Data.h"
  42. #include "common/VGearsStringUtil.h"
  43. #include "common/FinalFantasy7/FF7NameLookup.h"
  44. BattleDataInstaller::BattleDataInstaller(
  45. const DiskImage& disk_image, const std::string output_dir, Ogre::ResourceGroupManager* res_mgr
  46. ):
  47. disk_image_(disk_image), output_dir_(output_dir), data_dir_(output_dir + "data/"),
  48. scene_bin_(*const_cast<DiskImage&>(disk_image).filePath("data/battle/scene.bin")), total_scenes_(0), next_scene_(0),
  49. res_mgr_(res_mgr)
  50. {}
  51. BattleDataInstaller::~BattleDataInstaller(){}
  52. unsigned int BattleDataInstaller::InitializeScenes(){
  53. next_scene_ = 0;
  54. scenes_.clear();
  55. enemies_.clear();
  56. attacks_.clear();
  57. scenes_uncompressed_.clear();
  58. total_scenes_ = 0;
  59. // scene.bin is a sequence of 0x2000 bytes blocks (33 in the original game). Each block starts
  60. // with up to 16 pointers (in 4-byte units) to gzipped scenes, terminated by 0xFFFFFFFF. A scene
  61. // ends where the next one starts, or at the end of the block. There are 256 scenes in total.
  62. // Scene indexes are positional: every slot must be kept, even if it can't be extracted,
  63. // because battle IDs (scene * 4 + setup) are derived from them.
  64. const u32 block_size = 0x2000;
  65. const u32 blocks = scene_bin_.GetFileSize() / block_size;
  66. for (u32 i = 0; i < blocks; i ++){
  67. File scene_bin_block(&scene_bin_, i * block_size, block_size);
  68. u32 offsets[16];
  69. u32 count = 0;
  70. for (; count < 16; count ++){
  71. scene_bin_block.SetOffset(4 * count);
  72. u32 pointer = scene_bin_block.readU32LE();
  73. if (pointer == 0xFFFFFFFF || pointer * 4 >= block_size) break;
  74. offsets[count] = pointer * 4;
  75. }
  76. for (u32 o = 0; o < count; o ++){
  77. u32 start_offset = offsets[o];
  78. u32 end_offset = (o + 1 < count) ? offsets[o + 1] : block_size;
  79. if (end_offset <= start_offset) end_offset = block_size;
  80. File scene_bin_fragment(&scene_bin_block, start_offset, end_offset - start_offset);
  81. // NULL if it can't be extracted. Kept to preserve scene indexes.
  82. scenes_uncompressed_.push_back(ExtractGZipScene(scene_bin_fragment));
  83. total_scenes_ ++;
  84. }
  85. }
  86. return total_scenes_;
  87. }
  88. unsigned int BattleDataInstaller::InitializeBattleModels(){
  89. next_model_to_process_ = 0;
  90. next_model_to_convert_ = 0;
  91. battle_lgp_files_.clear();
  92. battle_lgp_file_names_.clear();
  93. battle_models_.clear();
  94. // Open battle.lgp
  95. File battle_lgp_file(*disk_image_.filePath("data/battle/battle.lgp"));
  96. // Also, open it as a LGP archive.
  97. VGears::LGPArchive battle_lgp(*disk_image_.filePath("data/battle/battle.lgp"), "LGP");
  98. battle_lgp.open(*disk_image_.filePath("data/battle/battle.lgp"), true);
  99. battle_lgp.load();
  100. VGears::LGPArchive::FileList file_list = battle_lgp.GetFiles();
  101. for (int i = 0; i < file_list.size(); i ++){
  102. VGears::LGPArchive::FileEntry f = file_list.at(i);
  103. if (f.data_offset + f.data_size <= battle_lgp_file.GetFileSize()){
  104. File b_lgp_file(&battle_lgp_file, f.data_offset, f.data_size);
  105. battle_lgp_files_.push_back(b_lgp_file);
  106. battle_lgp_file_names_.push_back(f.file_name);
  107. }
  108. }
  109. return battle_lgp_files_.size();
  110. }
  111. unsigned int BattleDataInstaller::InitializeSpellModels(){
  112. next_model_to_process_ = 0;
  113. next_model_to_convert_ = 0;
  114. battle_lgp_files_.clear();
  115. battle_lgp_file_names_.clear();
  116. battle_models_.clear();
  117. magic_lgp_files_.clear();
  118. magic_lgp_file_names_.clear();
  119. magic_lgp_file_folders_.clear();
  120. // Open magic.lgp
  121. File magic_lgp_file(*disk_image_.filePath("data/battle/magic.lgp"));
  122. // Also, open it as a LGP archive.
  123. VGears::LGPArchive magic_lgp(*disk_image_.filePath("data/battle/magic.lgp"), "LGP");
  124. magic_lgp.open(*disk_image_.filePath("data/battle/magic.lgp"), true);
  125. magic_lgp.load();
  126. VGears::LGPArchive::FileList file_list = magic_lgp.GetFiles();
  127. for (int i = 0; i < file_list.size(); i ++){
  128. VGears::LGPArchive::FileEntry f = file_list.at(i);
  129. if (f.data_offset + f.data_size <= magic_lgp_file.GetFileSize()){
  130. File m_lgp_file(&magic_lgp_file, f.data_offset, f.data_size);
  131. magic_lgp_files_.push_back(m_lgp_file);
  132. magic_lgp_file_names_.push_back(f.file_name);
  133. magic_lgp_file_folders_.push_back(f.folder);
  134. }
  135. }
  136. return magic_lgp_files_.size();
  137. }
  138. unsigned int BattleDataInstaller::ProcessScene(){
  139. if (next_scene_ >= scenes_uncompressed_.size()) return total_scenes_;
  140. File* scene_file = scenes_uncompressed_[next_scene_];
  141. if (scene_file == NULL){
  142. // Keep battle IDs stable: an unreadable scene still takes its 4 battle slots.
  143. std::cout << "Warning: scene " << next_scene_ << " could not be extracted." << std::endl;
  144. for (int f = 0; f < 4; f ++){
  145. Formation empty;
  146. empty.id = next_scene_ * 4 + f;
  147. formations_.push_back(empty);
  148. }
  149. next_scene_ ++;
  150. return next_scene_;
  151. }
  152. BattleSceneFile scene(next_scene_, *scene_file);
  153. scenes_.push_back(scene);
  154. // Add new enemies to the list.
  155. std::vector<Enemy> enemies = scene.GetEnemies();
  156. for (int e = 0; e < enemies.size(); e ++){
  157. if (std::any_of(
  158. enemies_.begin(), enemies_.end(), [&](const Enemy& existing){ return existing.id == enemies[e].id; }
  159. )) continue;
  160. enemies_.push_back(enemies[e]);
  161. }
  162. // Add new attacks to the list.
  163. std::vector<Attack> attacks = scene.GetAttacks();
  164. for (int a = 0; a < attacks.size(); a ++){
  165. if (std::any_of(
  166. attacks_.begin(), attacks_.end(), [&](const Attack& existing){ return existing.id == attacks[a].id; }
  167. )) continue;
  168. attacks_.push_back(attacks[a]);
  169. }
  170. // Battle ID = scene index * 4 + setup index, as referenced by field scripts and encounters.
  171. std::vector<Formation> formations = scene.GetFormations();
  172. for (int f = 0; f < formations.size(); f ++){
  173. formations[f].id = next_scene_ * 4 + f;
  174. formations_.push_back(formations[f]);
  175. }
  176. next_scene_ ++;
  177. return next_scene_;
  178. }
  179. unsigned int BattleDataInstaller::ProcessBattleModel(){
  180. if (next_model_to_process_ >= battle_lgp_files_.size()) return battle_lgp_files_.size();
  181. // TODO: Do something with the files.
  182. // A little explanation. Each file has a 4 letter name, for example: 1234
  183. // 12 is the model identifier.
  184. // 34 if the type of file:
  185. // - aa: Skeleton file (.hrc)
  186. // - ab: Animation scripts
  187. // - ac - al: Textures (.tex)
  188. // - am - cz: Polygon files (.p)
  189. // - da: Animations (.anim)
  190. std::string id = battle_lgp_file_names_[next_model_to_process_].substr(0, 2);
  191. /*if (id != "rt"){ // TODO DEBUG REMOVE
  192. next_model_to_process_ ++;
  193. return next_model_to_process_;
  194. }*/
  195. std::string type = battle_lgp_file_names_[next_model_to_process_].substr(2, 2);
  196. FF7Data::BattleModelInfo info = FF7Data::GetBattleModelInfo(id);
  197. if (type == "aa"){
  198. // HRC file.
  199. battle_lgp_files_[next_model_to_process_].WriteFile(
  200. output_dir_ + "temp/battle_models/"
  201. + battle_lgp_file_names_[next_model_to_process_] + ".hrcbin"
  202. );
  203. bool found = false;
  204. for (int m = 0; m < battle_models_.size(); m ++){
  205. if (battle_models_[m].id == id){
  206. battle_models_[m].hrc = battle_lgp_file_names_[next_model_to_process_] + ".hrc";
  207. found = true;
  208. break;
  209. }
  210. }
  211. if (found == false){
  212. BattleModel model;
  213. model.id = id;
  214. model.hrc = battle_lgp_file_names_[next_model_to_process_] + ".hrc";
  215. battle_models_.push_back(model);
  216. }
  217. }
  218. else if (type == "ab"){
  219. battle_lgp_files_[next_model_to_process_].WriteFile(
  220. output_dir_ + "temp/battle_models/"
  221. + battle_lgp_file_names_[next_model_to_process_] + ".script"
  222. );
  223. auto model_it = std::find_if(
  224. battle_models_.begin(), battle_models_.end(),
  225. [&](const BattleModel& model){ return model.id == id; }
  226. );
  227. if (model_it != battle_models_.end()){
  228. model_it->script = battle_lgp_file_names_[next_model_to_process_] + ".script";
  229. }
  230. else{
  231. BattleModel model;
  232. model.id = id;
  233. model.script = battle_lgp_file_names_[next_model_to_process_] + ".script";
  234. battle_models_.push_back(model);
  235. }
  236. }
  237. else if (type == "da"){
  238. // .da Animation file.
  239. battle_lgp_files_[next_model_to_process_].WriteFile(
  240. output_dir_ + "temp/battle_models/" + battle_lgp_file_names_[next_model_to_process_] + ".anim"
  241. );
  242. bool found = false;
  243. for (int m = 0; m < battle_models_.size(); m ++){
  244. if (battle_models_[m].id == id){
  245. battle_models_[m].anim = battle_lgp_file_names_[next_model_to_process_] + ".anim";
  246. found = true;
  247. break;
  248. }
  249. }
  250. if (found == false){
  251. BattleModel model;
  252. model.id = id;
  253. model.anim = battle_lgp_file_names_[next_model_to_process_] + ".anim";
  254. battle_models_.push_back(model);
  255. }
  256. }
  257. else if (
  258. type == "ac" || type == "ad" || type == "ae" || type == "af" || type == "ag"
  259. || type == "ah"|| type == "ai" || type == "aj" || type == "ak" || type == "al"
  260. ){
  261. // .tex texture files. Save directly to their directory.
  262. battle_lgp_files_[next_model_to_process_].WriteFile(
  263. output_dir_ + "temp/battle_models/" + battle_lgp_file_names_[next_model_to_process_] + ".tex"
  264. );
  265. TexFile tex(battle_lgp_files_[next_model_to_process_]);
  266. std::string path = data_dir_ + "models/battle/";
  267. if (info.is_player){
  268. path += "characters/";
  269. // Add or update on character_data_
  270. bool found = false;
  271. for (auto data: character_data_){
  272. if (data.id == info.numeric_id){
  273. // Check if the model already exists for this character
  274. bool model_found = false;
  275. for (auto mod: data.models){
  276. if (mod.model == info.alphanumeric_id + "_" + info.name_normal + ".mesh"){
  277. model_found = true;
  278. break;
  279. }
  280. }
  281. if (model_found) break;
  282. CharacterModel model;
  283. model.name = info.name;
  284. model.model = info.alphanumeric_id + "_" + info.name_normal + ".mesh";
  285. data.models.push_back(model);
  286. character_data_.push_back(data);
  287. found = true;
  288. break;
  289. }
  290. }
  291. if (!found){
  292. CharacterData character_data;
  293. character_data.id = info.numeric_id;
  294. CharacterModel model;
  295. model.name = info.name;
  296. model.model = info.alphanumeric_id + "_" + info.name_normal + ".mesh";
  297. character_data.models.push_back(model);
  298. character_data_.push_back(character_data);
  299. }
  300. }
  301. else if (info.is_scene){
  302. SceneModel scene_data;
  303. scene_data.id = info.numeric_id;
  304. scene_data.model = info.alphanumeric_id + "_" + info.name_normal + ".mesh";
  305. scene_data.description = info.name;
  306. // Every texture of a scene model reaches this point, but the map needs one entry.
  307. if (std::none_of(scene_data_.begin(), scene_data_.end(), [&](const SceneModel& s){
  308. return s.id == scene_data.id && s.model == scene_data.model;
  309. })) scene_data_.push_back(scene_data);
  310. path += "scenes/";
  311. }
  312. else path += "enemies/";
  313. path += (id + type + "_" + info.name_normal + ".png");
  314. tex.SavePng(path);
  315. res_mgr_->declareResource(
  316. id + type + "_" + info.name_normal + ".png", "Texture", "FFVIITextures"
  317. );
  318. bool found = false;
  319. for (int m = 0; m < battle_models_.size(); m ++){
  320. if (battle_models_[m].id == id){
  321. battle_models_[m].tex.push_back(id + type+ "_" + info.name_normal + ".png");
  322. found = true;
  323. break;
  324. }
  325. }
  326. if (found == false){
  327. BattleModel model;
  328. model.id = id;
  329. model.tex.push_back(id + type + "_" + info.name_normal + ".png");
  330. battle_models_.push_back(model);
  331. }
  332. }
  333. else{
  334. // .p polygon file.
  335. battle_lgp_files_[next_model_to_process_].WriteFile(
  336. output_dir_ + "temp/battle_models/" + battle_lgp_file_names_[next_model_to_process_] + ".p"
  337. );
  338. bool found = false;
  339. for (int m = 0; m < battle_models_.size(); m ++){
  340. if (battle_models_[m].id == id){
  341. battle_models_[m].p.push_back(battle_lgp_file_names_[next_model_to_process_] + ".p");
  342. found = true;
  343. break;
  344. }
  345. }
  346. if (found == false){
  347. BattleModel model;
  348. model.id = id;
  349. model.p.push_back(battle_lgp_file_names_[next_model_to_process_] + ".p");
  350. battle_models_.push_back(model);
  351. }
  352. }
  353. next_model_to_process_ ++;
  354. return next_model_to_process_;
  355. }
  356. unsigned int BattleDataInstaller::ProcessSpellModel(){
  357. if (next_model_to_process_ >= magic_lgp_files_.size()) return magic_lgp_files_.size();
  358. // TODO: Do something with the files.
  359. // A little explanation. There are no patterns on file names, and there are multiple extensions.
  360. // - d: Skeleton file, similar to aa from battle models.
  361. // - b: Unknown.
  362. // - p*: Polygon files, for each bone.
  363. // - tex: Texture files, TIM format.
  364. // - rsd: Resource files
  365. // - s: Unknown.
  366. // - a00: Animation files, similar to da from battle models.
  367. //std::cout << "SPELL MODEL FILE: " << magic_lgp_file_names_[next_model_to_process_] << std::endl;
  368. std::string fname = magic_lgp_file_names_[next_model_to_process_];
  369. std::string id = fname.substr(0, fname.find_last_of("."));
  370. std::string ext = fname.substr(fname.find_last_of(".") + 1);
  371. FF7Data::SpellModelInfo info = FF7Data::GetSpellModelInfo(id);
  372. //magic_lgp_files_[next_model_to_process_].WriteFile(output_dir_ + "temp/spell_models/" + fname);
  373. if (ext == "d"){
  374. // Skeleton file.
  375. magic_lgp_files_[next_model_to_process_].WriteFile(output_dir_ + "temp/spell_models/" + fname);
  376. bool found = false;
  377. for (int m = 0; m < spell_models_.size(); m ++){
  378. if (spell_models_[m].id == id){
  379. spell_models_[m].hrc = fname;
  380. found = true;
  381. break;
  382. }
  383. }
  384. if (found == false){
  385. SpellModel model;
  386. model.id = id;
  387. model.hrc = fname;
  388. spell_models_.push_back(model);
  389. }
  390. }
  391. else if (ext.at(0) == 'p'){
  392. // Bone file.
  393. // If the extension is just ".p", use the file name.
  394. std::string p_name;
  395. if (ext == "p") p_name = fname;
  396. // If the extension is ".pXX", save it with altered name.
  397. else p_name =
  398. fname.substr(0, fname.length() - 4) + "_" + fname.substr(fname.length() - 2, 2) + ".p";
  399. magic_lgp_files_[next_model_to_process_].WriteFile(
  400. output_dir_ + "temp/spell_models/" + p_name
  401. );
  402. std::cout << "[BONE RENAMED] " << fname << " --> " << p_name << std::endl;
  403. bool found = false;
  404. for (int m = 0; m < spell_models_.size(); m ++){
  405. if (spell_models_[m].id == id){
  406. spell_models_[m].p.push_back(p_name);
  407. found = true;
  408. break;
  409. }
  410. }
  411. if (found == false){
  412. SpellModel model;
  413. model.id = id;
  414. model.p.push_back(p_name);
  415. spell_models_.push_back(model);
  416. }
  417. }
  418. if (ext == "tex"){
  419. // Texture files. They can be shared among various spells, so just convert them and don't
  420. // add them to a particular model.
  421. magic_lgp_files_[next_model_to_process_].WriteFile(output_dir_ + "temp/spell_models/" + fname);
  422. TexFile tex(output_dir_ + "temp/spell_models/" + fname);
  423. std::string path = data_dir_ + "models/battle/attacks/" + id + ".png";
  424. // Many of these tex files are wrong and dummied out in the OG, so try/catch.
  425. try{
  426. tex.SavePng(path);
  427. }
  428. catch(const std::exception& e){
  429. std::cerr << "[WARNING] Error converting texture " << (output_dir_ + "temp/spell_models/" + fname)
  430. << " to png: " << e.what() << std::endl;
  431. }
  432. }
  433. if (ext == "a00"){
  434. // Animation file.
  435. magic_lgp_files_[next_model_to_process_].WriteFile(output_dir_ + "temp/spell_models/" + fname);
  436. bool found = false;
  437. for (int m = 0; m < spell_models_.size(); m ++){
  438. if (spell_models_[m].id == id){
  439. spell_models_[m].anim = fname;
  440. found = true;
  441. break;
  442. }
  443. }
  444. if (found == false){
  445. SpellModel model;
  446. model.id = id;
  447. model.anim = fname;
  448. spell_models_.push_back(model);
  449. }
  450. }
  451. // TODO: b files? a files? s files?
  452. next_model_to_process_ ++;
  453. return next_model_to_process_;
  454. }
  455. namespace{
  456. /**
  457. * Files of magic.lgp (without extension) with the animations of the limit breaks of each model of the party.
  458. * The animation files have the format of the animations of the models, with 8 animations for the skeleton of
  459. * the character (the animations 44 to 51 of the model, which are empty in battle.lgp) and 8 for its weapon.
  460. * The files are in the order of the techniques of the character (limit level 1 first and second, level 2, ...):
  461. * the table that ff7.exe uses to load them (at 0x7b7b58) has them in groups of 7 by character.
  462. */
  463. const std::map<std::string, std::vector<std::string>>& LimitAnimationFiles(){
  464. static const std::vector<std::string> cloud = {"blaver", "kyou", "limcl2", "limcl6", "limcl3", "limcl7", "limcl4"};
  465. static const std::vector<std::string> barret = {"hvshot", "limbr5", "limbr2", "limbr6", "limbr3", "limbr7", "limbr4"};
  466. static const std::map<std::string, std::vector<std::string>> files = {
  467. {"rt", cloud}, {"si", cloud},
  468. {"ru", {"limfast"}}, // Tifa.
  469. {"rv", {"iyash", "limea5", "limea2", "limea6", "limea3", "kodo", "limea4"}}, // Aerith.
  470. {"rw", {"limsled", "limrd5", "limsled", "limrd6", "limrd3", "limrd7", "limrd4"}}, // Red XIII.
  471. {"rx", {"limyf1", "limyf5", "limyf2", "limyf6", "limyf3", "limyf7", "limyf4"}}, // Yuffie.
  472. {"ry", {"dice"}}, // Cait Sith.
  473. {"rz", {"limcd1", "limcd5", "limcd2", "limcd6", "limcd3", "limcd7", "limcd4"}}, // Cid.
  474. {"sb", barret}, {"sc", barret}, {"sd", barret}, {"se", barret},
  475. };
  476. return files;
  477. }
  478. }
  479. unsigned int BattleDataInstaller::ConvertBattleModelsInit(){
  480. next_model_to_convert_ = 0;
  481. // The animations of the limit breaks are in magic.lgp. Take the ones of the party to the folder of the battle
  482. // models, so they are converted with them.
  483. try{
  484. std::set<std::string> wanted;
  485. for (const auto& entry : LimitAnimationFiles()){
  486. for (const std::string& tag : entry.second) wanted.insert(tag + ".a00");
  487. }
  488. File magic_lgp_file(*disk_image_.filePath("data/battle/magic.lgp"));
  489. VGears::LGPArchive magic_lgp(*disk_image_.filePath("data/battle/magic.lgp"), "LGP");
  490. magic_lgp.open(*disk_image_.filePath("data/battle/magic.lgp"), true);
  491. magic_lgp.load();
  492. VGears::LGPArchive::FileList file_list = magic_lgp.GetFiles();
  493. for (int i = 0; i < file_list.size(); i ++){
  494. VGears::LGPArchive::FileEntry f = file_list.at(i);
  495. std::string name = f.file_name;
  496. std::transform(name.begin(), name.end(), name.begin(), ::tolower);
  497. if (wanted.count(name) == 0) continue;
  498. if (f.data_offset + f.data_size > magic_lgp_file.GetFileSize()) continue;
  499. File limit_file(&magic_lgp_file, f.data_offset, f.data_size);
  500. limit_file.WriteFile(output_dir_ + "temp/battle_models/limit_" + name);
  501. }
  502. }
  503. catch (const std::exception& ex){
  504. std::cerr << "[ERROR] Exception reading the limit animations: " << ex.what() << std::endl;
  505. }
  506. return battle_models_.size();
  507. }
  508. unsigned int BattleDataInstaller::ConvertSpellModelsInit(){
  509. next_model_to_convert_ = 0;
  510. return spell_models_.size();
  511. }
  512. unsigned int BattleDataInstaller::ConvertBattleModel(){
  513. if (next_model_to_convert_ >= battle_models_.size()) return battle_models_.size();
  514. BattleModel model = battle_models_[next_model_to_convert_];
  515. try{
  516. std::string path = "";
  517. FF7Data::BattleModelInfo info = FF7Data::GetBattleModelInfo(model.id);
  518. if (info.is_player) path += "characters/";
  519. else if (info.is_scene) path += "scenes/";
  520. else path += "enemies/";
  521. if (info.is_scene){
  522. DecompileSceneModelHrc(
  523. File(output_dir_ + "temp/battle_models/" + model.hrc + "bin"), model,
  524. output_dir_ + "temp/battle_models/" + model.id + "_" + info.name_normal + ".hrc",
  525. model.id + "_" + info.name_normal
  526. );
  527. }
  528. else{
  529. DecompileBattleModelHrc(
  530. File(output_dir_ + "temp/battle_models/" + model.hrc + "bin"), model,
  531. output_dir_ + "temp/battle_models/" + model.id + "_" + info.name_normal + ".hrc",
  532. model.id + "_" + info.name_normal
  533. );
  534. }
  535. GenerateRsdFiles(model, output_dir_ + "temp/battle_models/");
  536. if ("" != model.anim){ // Skip for non-animated, ie battle backgrounds
  537. DaFile da(File(output_dir_ + "temp/battle_models/" + model.anim));
  538. std::vector<std::string> a_files = da.GenerateAFiles(model.id, output_dir_ + "temp/battle_models/");
  539. for (std::string file_name : a_files) model.a.push_back(file_name);
  540. }
  541. // Limit breaks of the party: their animations are in files of magic.lgp. Each one is added to the model with
  542. // the names <model>_<file>_00 to _07, which replace the empty animations 44 to 51 for that limit.
  543. if (info.is_player){
  544. auto limits = LimitAnimationFiles().find(model.id);
  545. if (limits != LimitAnimationFiles().end()){
  546. std::set<std::string> converted;
  547. for (const std::string& tag : limits->second){
  548. if (!converted.insert(tag).second) continue;
  549. std::string limit_path = output_dir_ + "temp/battle_models/limit_" + tag + ".a00";
  550. if (!std::ifstream(limit_path).good()) continue;
  551. try{
  552. File limit_file(limit_path);
  553. DaFile limit_da(limit_file);
  554. std::vector<std::string> limit_files = limit_da.GenerateAFiles(
  555. model.id + "_" + tag, output_dir_ + "temp/battle_models/"
  556. );
  557. for (const std::string& file_name : limit_files){
  558. // Only the animations of the skeleton (0 to 7), not the ones of the weapon.
  559. size_t dot = file_name.find_last_of('.');
  560. int number = std::atoi(file_name.substr(dot - 2, 2).c_str());
  561. if (number < 8) model.a.push_back(file_name);
  562. }
  563. }
  564. catch (const std::exception& ex){
  565. std::cerr << "[ERROR] Exception reading the limit animation " << tag << ": " << ex.what() << std::endl;
  566. }
  567. }
  568. }
  569. }
  570. if ("" != model.script){ // Skip non scripted models
  571. // Only the models of the party have the 74 scripts (the limits are the ones from 60 up). The other
  572. // ones (enemies and the rest of the models with scripts) have 32.
  573. AbFile ab(File(output_dir_ + "temp/battle_models/" + model.script), !info.is_player);
  574. ab.GenerateScripts(model.id + "_" + info.name_normal, data_dir_ + "scripts/battle_models/");
  575. }
  576. // Playable characters hold their weapons in battle (but Red XIII, who has none). Each one is a
  577. // polygon file by itself, named <id>c<k..z>, so make a model for each with a single bone.
  578. std::vector<std::pair<std::string, std::string>> weapons; // Polygon file, mesh name.
  579. if (info.is_player && model.id != "rw"){
  580. for (std::string p : model.p){
  581. if (p.length() < 4 || p[2] != 'c' || p[3] < 'k' || p[3] > 'z') continue;
  582. char number[8];
  583. std::snprintf(number, sizeof(number), "%02d", p[3] - 'k');
  584. const std::string weapon_name = model.id + "_" + info.name_normal + "_weapon_" + number;
  585. std::ofstream hrc(output_dir_ + "temp/battle_models/" + weapon_name + ".hrc");
  586. hrc << ":HEADER_BLOCK 2\n:SKELETON " << weapon_name << "\n:BONES 1\n\nbone0\nroot\n0.000000\n1 "
  587. << p.substr(0, 4) << "\n\n";
  588. hrc.close();
  589. weapons.push_back(std::make_pair(p, weapon_name));
  590. }
  591. }
  592. // Reload the resources for the newly created hrc and rsd.
  593. // TODO: Could this be done with res_mgr_->declareResource() ? Probably faster
  594. res_mgr_->removeResourceLocation(output_dir_ + "temp/battle_models/", "FFVII");
  595. res_mgr_->addResourceLocation(output_dir_ + "temp/battle_models/", "FileSystem", "FFVII", true, true);
  596. Ogre::ResourcePtr hrc = VGears::HRCFileManager::GetSingleton().load(
  597. model.id + "_" + info.name_normal + ".hrc", "FFVII"
  598. );
  599. Ogre::String base_name;
  600. VGears::StringUtil::splitBase(model.hrc, base_name);
  601. auto mesh_name = model.id + "_" + info.name_normal + ".mesh";
  602. Ogre::MeshPtr mesh(Ogre::MeshManager::getSingleton().load(mesh_name, "FFVII"));
  603. Ogre::SkeletonPtr skeleton(mesh->getSkeleton());
  604. for (std::string anim : model.a){
  605. VGears::AFileManager &afl_mgr(VGears::AFileManager::GetSingleton());
  606. Ogre::String a_base_name;
  607. VGears::StringUtil::splitBase(anim, a_base_name);
  608. VGears::AFilePtr a = afl_mgr.load(a_base_name + ".a", "FFVII").staticCast<VGears::AFile>();
  609. // Convert the FF7 name to a more readable name set in the meta data.
  610. VGears::StringUtil::splitBase(anim, base_name);
  611. a->AddTo(skeleton, VGears::NameLookup::Animation(base_name));
  612. }
  613. ExportMesh(path, mesh);
  614. for (const auto& weapon : weapons){
  615. try{
  616. VGears::HRCFileManager::GetSingleton().load(weapon.second + ".hrc", "FFVII");
  617. Ogre::MeshPtr weapon_mesh(Ogre::MeshManager::getSingleton().load(weapon.second + ".mesh", "FFVII"));
  618. ExportMesh(path, weapon_mesh);
  619. }
  620. catch (const Ogre::Exception& ex){
  621. std::cerr << "[ERROR] Ogre exception converting weapon " << weapon.second << ": " << ex.what() << std::endl;
  622. }
  623. }
  624. }
  625. catch (const Ogre::Exception& ex){
  626. std::cerr << "[ERROR] Ogre exception converting battle model " << model.hrc <<": " << ex.what() << std::endl;
  627. }
  628. catch (const std::exception& ex){
  629. std::cerr << "[ERROR] Exception converting battle model " << model.hrc << ": " << ex.what() << std::endl;
  630. }
  631. next_model_to_convert_ ++;
  632. return next_model_to_convert_;
  633. }
  634. unsigned int BattleDataInstaller::ConvertSpellModel(){
  635. if (next_model_to_convert_ >= spell_models_.size()) return spell_models_.size();
  636. SpellModel model = spell_models_[next_model_to_convert_];
  637. try{
  638. std::string path = "attacks/";
  639. FF7Data::SpellModelInfo info = FF7Data::GetSpellModelInfo(model.id);
  640. // Decompile the .hrc file (if any)
  641. if (model.hrc != ""){
  642. std::cout << "[DECOMPILE] " << output_dir_ + "temp/spell_models/" + model.hrc << std::endl;
  643. DecompileSpellModelHrc(
  644. File(output_dir_ + "temp/spell_models/" + model.hrc), model,
  645. output_dir_ + "temp/spell_models/" + info.name_normal + ".hrc", model.id
  646. );
  647. model.hrc = info.name_normal + ".hrc";
  648. std::cout << " TO: " << (output_dir_ + "temp/spell_models/" + info.name_normal + ".hrc") << std::endl;
  649. }
  650. GenerateRsdFiles(model, output_dir_ + "temp/spell_models/");
  651. // Decompile the .a00 file (if any) into a .a file.
  652. if ("" != model.anim){ // Skip for non-animated.
  653. DaFile da(File(output_dir_ + "temp/spell_models/" + model.anim));
  654. std::vector<std::string> a_files = da.GenerateAFiles(model.id, output_dir_ + "temp/spell_models/");
  655. for (std::string file_name : a_files) model.a.push_back(file_name);
  656. }
  657. // Reload the resources for the newly created hrc and rsd.
  658. // TODO: Could this be done with res_mgr_->declareResource() ? Probably faster
  659. res_mgr_->removeResourceLocation(output_dir_ + "temp/spell_models/", "FFVII");
  660. res_mgr_->addResourceLocation(output_dir_ + "temp/spell_models/", "FileSystem", "FFVII", true, true);
  661. // Export meshes thet have an .hrc skeleton.
  662. if (model.hrc != ""){
  663. std::cout << "[EXPORT MESH] " << model.hrc << std::endl;
  664. Ogre::ResourcePtr hrc = VGears::HRCFileManager::GetSingleton().load(model.id + ".hrc", "FFVII");
  665. Ogre::String base_name;
  666. VGears::StringUtil::splitBase(model.hrc, base_name);
  667. auto mesh_name = model.id + ".mesh";
  668. Ogre::MeshPtr mesh(Ogre::MeshManager::getSingleton().load(mesh_name, "FFVII"));
  669. Ogre::SkeletonPtr skeleton(mesh->getSkeleton());
  670. for (std::string anim : model.a){
  671. VGears::AFileManager &afl_mgr(VGears::AFileManager::GetSingleton());
  672. Ogre::String a_base_name;
  673. VGears::StringUtil::splitBase(anim, a_base_name);
  674. VGears::AFilePtr a = afl_mgr.load(a_base_name + ".a", "FFVII").staticCast<VGears::AFile>();
  675. // Convert the FF7 name to a more readable name set in the meta data.
  676. VGears::StringUtil::splitBase(anim, base_name);
  677. a->AddTo(skeleton, VGears::NameLookup::Animation(base_name));
  678. }
  679. ExportMesh(path, mesh);
  680. }
  681. }
  682. catch (const Ogre::Exception& ex){
  683. std::cerr << "[ERROR] Ogre exception converting spell model " << model.hrc <<": " << ex.what() << std::endl;
  684. }
  685. catch (const std::exception& ex){
  686. std::cerr << "[ERROR] Exception converting spell model " << model.hrc << ": " << ex.what() << std::endl;
  687. }
  688. next_model_to_convert_ ++;
  689. return next_model_to_convert_;
  690. }
  691. namespace{
  692. /**
  693. * Makes a name that is safe to use in files and Ogre resources: lower case letters, numbers and
  694. * underscores.
  695. */
  696. std::string EffectSafeName(std::string name){
  697. for (char& c : name){
  698. c = std::tolower(static_cast<unsigned char>(c));
  699. if (!std::isalnum(static_cast<unsigned char>(c)) && c != '_') c = '_';
  700. }
  701. return name;
  702. }
  703. /**
  704. * Gets the name without the extension, in lower case.
  705. */
  706. std::string EffectStem(const std::string& file_name){
  707. std::string stem = file_name.substr(0, file_name.find_last_of('.'));
  708. std::transform(stem.begin(), stem.end(), stem.begin(), [](unsigned char c){return std::tolower(c);});
  709. return stem;
  710. }
  711. /**
  712. * Gets the name of a texture in a rsd file, without the extension, in lower case.
  713. */
  714. std::string EffectRsdValue(const std::string& line){
  715. size_t equal = line.find('=');
  716. if (equal == std::string::npos) return "";
  717. std::string value = line.substr(equal + 1);
  718. while (!value.empty() && std::isspace(static_cast<unsigned char>(value.back()))) value.pop_back();
  719. while (!value.empty() && std::isspace(static_cast<unsigned char>(value.front()))) value.erase(0, 1);
  720. return EffectStem(value);
  721. }
  722. }
  723. unsigned int BattleDataInstaller::InitializeEffectModels(){
  724. effect_models_.clear();
  725. next_effect_to_convert_ = 0;
  726. const std::string dir = output_dir_ + "temp/effects/";
  727. // Index the files by folder and name.
  728. std::map<std::pair<std::string, std::string>, size_t> index;
  729. for (size_t f = 0; f < magic_lgp_files_.size(); f ++){
  730. std::string name = magic_lgp_file_names_[f];
  731. std::transform(name.begin(), name.end(), name.begin(), [](unsigned char c){return std::tolower(c);});
  732. index[{magic_lgp_file_folders_[f], name}] = f;
  733. }
  734. // Folder key: the effect folder, "common" for the root.
  735. auto key_of = [](const std::string& folder){
  736. if (folder.empty()) return std::string("common");
  737. std::string key = folder;
  738. const std::string prefix = "ff7/data/battle/";
  739. if (key.compare(0, prefix.size(), prefix) == 0) key = key.substr(prefix.size());
  740. return EffectSafeName(key);
  741. };
  742. // Finds a file in the folder of the model, or in the archive root.
  743. auto find = [&](const std::string& folder, const std::string& name, std::string& found_folder){
  744. auto it = index.find({folder, name});
  745. if (it != index.end()){found_folder = folder; return it->second;}
  746. it = index.find({"", name});
  747. if (it != index.end()){found_folder = ""; return it->second;}
  748. return static_cast<size_t>(-1);
  749. };
  750. std::set<std::string> written;
  751. auto write = [&](size_t f, const std::string& name){
  752. if (written.insert(name).second) magic_lgp_files_[f].WriteFile(dir + name);
  753. };
  754. for (size_t f = 0; f < magic_lgp_files_.size(); f ++){
  755. std::string rsd_name = magic_lgp_file_names_[f];
  756. if (rsd_name.size() < 4 || EffectSafeName(rsd_name.substr(rsd_name.size() - 4)) != "_rsd") continue;
  757. const std::string& folder = magic_lgp_file_folders_[f];
  758. // Parse the rsd.
  759. File& rsd_file = magic_lgp_files_[f];
  760. rsd_file.SetOffset(0);
  761. std::string content;
  762. for (size_t b = 0; b < rsd_file.GetFileSize(); b ++) content += static_cast<char>(rsd_file.readU8());
  763. std::string ply;
  764. std::vector<std::string> tex;
  765. std::istringstream lines(content);
  766. std::string line;
  767. while (std::getline(lines, line)){
  768. std::string upper = line;
  769. std::transform(upper.begin(), upper.end(), upper.begin(), [](unsigned char c){return std::toupper(c);});
  770. if (upper.compare(0, 4, "PLY=") == 0) ply = EffectRsdValue(line);
  771. else if (upper.compare(0, 4, "TEX[") == 0) tex.push_back(EffectRsdValue(line));
  772. }
  773. if (ply.empty()) continue;
  774. std::string p_folder;
  775. size_t p_index = find(folder, ply + ".p", p_folder);
  776. if (p_index == static_cast<size_t>(-1)) continue;
  777. const std::string key = key_of(folder);
  778. EffectModel model;
  779. model.name = key + "_" + EffectSafeName(EffectStem(rsd_name));
  780. const std::string p_name = key_of(p_folder) + "_" + EffectSafeName(ply) + ".p";
  781. write(p_index, p_name);
  782. std::string rsd = "@RSD940102\nPLY=" + p_name.substr(0, p_name.size() - 2) + ".PLY\n";
  783. rsd += "MAT=" + model.name + ".MAT\nGRP=" + model.name + ".GRP\n";
  784. std::vector<std::string> tex_names;
  785. for (const std::string& t : tex){
  786. std::string t_folder;
  787. size_t t_index = find(folder, t + ".tex", t_folder);
  788. if (t_index == static_cast<size_t>(-1)) continue;
  789. const std::string t_name = key_of(t_folder) + "_" + EffectSafeName(t) + ".tex";
  790. write(t_index, t_name);
  791. tex_names.push_back(t_name);
  792. }
  793. rsd += "NTEX=" + std::to_string(tex_names.size()) + "\n";
  794. for (size_t t = 0; t < tex_names.size(); t ++){
  795. rsd += "TEX[" + std::to_string(t) + "]=" + tex_names[t].substr(0, tex_names[t].size() - 4) + ".TIM\n";
  796. }
  797. model.textures = tex_names;
  798. std::ofstream(dir + model.name + ".rsd") << rsd;
  799. // A model with a single bone, with the mesh.
  800. std::ofstream(dir + model.name + ".hrc")
  801. << ":HEADER_BLOCK 2\n:SKELETON " << model.name << "\n:BONES 1\n\nbone0\nroot\n0.000000\n1 "
  802. << model.name << "\n\n";
  803. effect_models_.push_back(model);
  804. }
  805. res_mgr_->removeResourceLocation(dir, "FFVII");
  806. res_mgr_->addResourceLocation(dir, "FileSystem", "FFVII", true, true);
  807. return effect_models_.size();
  808. }
  809. unsigned int BattleDataInstaller::ConvertEffectModel(){
  810. if (next_effect_to_convert_ >= effect_models_.size()) return effect_models_.size();
  811. EffectModel& model = effect_models_[next_effect_to_convert_];
  812. try{
  813. VGears::HRCFileManager::GetSingleton().load(model.name + ".hrc", "FFVII");
  814. Ogre::MeshPtr mesh(Ogre::MeshManager::getSingleton().load(model.name + ".mesh", "FFVII"));
  815. ExportMesh("effects/", mesh);
  816. for (const std::string& texture : model.textures){
  817. const std::string path = data_dir_ + "models/battle/effects/"
  818. + texture.substr(0, texture.size() - 4) + ".png";
  819. if (boost::filesystem::exists(path)) continue;
  820. try{
  821. TexFile tex(output_dir_ + "temp/effects/" + texture);
  822. tex.SavePng(path);
  823. }
  824. catch (const std::exception& ex){
  825. // Many of the original textures are dummies.
  826. std::cerr << "[WARNING] Error converting effect texture " << texture << ": " << ex.what() << std::endl;
  827. }
  828. }
  829. model.converted = true;
  830. }
  831. catch (const Ogre::Exception& ex){
  832. std::cerr << "[WARNING] Could not convert effect model " << model.name << ": " << ex.what() << std::endl;
  833. }
  834. catch (const std::exception& ex){
  835. std::cerr << "[WARNING] Could not convert effect model " << model.name << ": " << ex.what() << std::endl;
  836. }
  837. next_effect_to_convert_ ++;
  838. return next_effect_to_convert_;
  839. }
  840. void BattleDataInstaller::WriteEffectModels(){
  841. std::ofstream file(data_dir_ + "gamedata/effects.lua");
  842. file << "-- Meshes of the battle effects, extracted from magic.lgp. Names are the folder of the effect\n"
  843. << "-- and the original file, for example magic_faira_1. Meshes are in models/battle/effects.\n"
  844. << "Game.EffectModels = {\n";
  845. for (const EffectModel& model : effect_models_){
  846. if (model.converted) file << " \"" << model.name << "\",\n";
  847. }
  848. file << "}\n";
  849. WriteEffectSprites();
  850. WriteEffectCameras();
  851. WriteEffectMeshes();
  852. WriteBattleCameras();
  853. }
  854. void BattleDataInstaller::WriteBattleCameras(){
  855. // The scripts of the cameras of the battles (see BattleCameraScripts.h).
  856. std::map<int, BattleCameraScripts::Set> sets;
  857. for (int i = 0; i < 3; ++ i){
  858. const std::string name = "data/battle/camdat" + std::to_string(i) + ".bin";
  859. auto path = const_cast<DiskImage&>(disk_image_).filePath(name);
  860. BattleCameraScripts::Set set;
  861. if (path != nullptr && BattleCameraScripts::ReadFile(*path, set)) sets[i] = set;
  862. else std::cerr << "[WARNING] Can't read " << name << "." << std::endl;
  863. }
  864. std::ofstream file(data_dir_ + "gamedata/battle_cameras.lua");
  865. BattleCameraScripts::WriteLua(file, sets);
  866. }
  867. void BattleDataInstaller::WriteEffectMeshes(){
  868. // The meshes of the effects that are in the data of ff7.exe, like the ground of Ifrit and the
  869. // mountain of Ramuh (see ExeEffectMeshes.h). Names are the address of the block in the executable.
  870. std::map<uint32_t, ExeEffectMeshes::Mesh> meshes;
  871. auto exe_path = const_cast<DiskImage&>(disk_image_).filePath("ff7.exe");
  872. if (exe_path == nullptr || !ExeEffectMeshes::Extract(*exe_path, meshes)){
  873. std::cerr << "[WARNING] Can't read the meshes of the effects from ff7.exe." << std::endl;
  874. }
  875. std::ofstream file(data_dir_ + "gamedata/effect_meshes.lua");
  876. ExeEffectMeshes::WriteLua(file, meshes);
  877. }
  878. void BattleDataInstaller::WriteEffectCameras(){
  879. // The scripts of the cameras are in the data of ff7.exe, and are found from the code that
  880. // uses them. Names are the address of the script in memory.
  881. std::map<uint32_t, std::vector<int16_t>> scripts;
  882. auto exe_path = const_cast<DiskImage&>(disk_image_).filePath("ff7.exe");
  883. if (exe_path == nullptr || !ExeEffectCameras::Extract(*exe_path, scripts)){
  884. std::cerr << "[WARNING] Can't read the camera scripts of the effects from ff7.exe." << std::endl;
  885. }
  886. std::ofstream file(data_dir_ + "gamedata/effect_cameras.lua");
  887. file << "-- Camera scripts of the battle effects, read from ff7.exe. Game.EffectCameras[name] is a list\n"
  888. << "-- of 16 bit words, with the address of the script in the executable as the name (like\n"
  889. << "-- 0x8ae268). See scripts/battle/effect_camera.lua for the format.\n"
  890. << "Game.EffectCameras = Game.EffectCameras or {}\n";
  891. for (const auto& script : scripts){
  892. file << "Game.EffectCameras[\"0x" << std::hex << script.first << std::dec << "\"] = {";
  893. for (size_t i = 0; i < script.second.size(); ++ i){
  894. if (i % 16 == 0) file << "\n ";
  895. file << script.second[i] << ", ";
  896. }
  897. file << "\n}\n";
  898. }
  899. }
  900. void BattleDataInstaller::WriteEffectSprites(){
  901. // The sprite files (.s) are lists of frames, and every frame a list of quads of a sprite sheet (see
  902. // the layout below). They are written twice: all the quads one after the other (the first format,
  903. // used by the Cure effect) and the frames with their quads (effect_animations.lua).
  904. // Textures are converted too, with the name of the effect folder as a prefix (common_ for the
  905. // archive root), to images/battle_fx, so they can be used as images of sprites.
  906. const std::string key_prefix = "ff7/data/battle/";
  907. auto key_of = [&](std::string folder){
  908. if (folder.empty()) return std::string("common");
  909. if (folder.compare(0, key_prefix.size(), key_prefix) == 0) folder = folder.substr(key_prefix.size());
  910. return EffectSafeName(folder);
  911. };
  912. std::ofstream file(data_dir_ + "gamedata/effect_sprites.lua");
  913. file << "-- Frames of the sprite files of magic.lgp. Game.EffectSprites[name] is a list of frames with\n"
  914. << "-- the position (u, v) and size (w, h) in the texture, in pixels, the offset to draw it\n"
  915. << "-- (ox, oy) and the flags. The names are the folder and the file, like common_iyashi.\n"
  916. << "-- Textures are in images/battle_fx, like common_iyashi00.png.\n"
  917. << "Game.EffectSprites = Game.EffectSprites or {}\n";
  918. // (The list has the quads of all the frames in order, with the position of the corner as ox, oy.)
  919. std::ofstream animations(data_dir_ + "gamedata/effect_animations.lua");
  920. animations << "-- Frames of the sprite files (.SPT) of magic.lgp. Game.EffectAnimations[name] is a list of frames and\n"
  921. << "-- every frame a list of quads: {x, y, u, v, w, h}, with the position of the corner and the size of the\n"
  922. << "-- quad in units of 8 of the effect, and the place in the texture in pixels. The names are the folder\n"
  923. << "-- and the file, like common_houden_2.\n"
  924. << "Game.EffectAnimations = Game.EffectAnimations or {}\n";
  925. const std::string dir = output_dir_ + "temp/effects/";
  926. boost::filesystem::create_directories(data_dir_ + "images/battle_fx/");
  927. for (size_t f = 0; f < magic_lgp_files_.size(); f ++){
  928. const std::string& name = magic_lgp_file_names_[f];
  929. if (name.size() < 3) continue;
  930. std::string ext = name.substr(name.find_last_of('.') + 1);
  931. std::transform(ext.begin(), ext.end(), ext.begin(), [](unsigned char c){return std::tolower(c);});
  932. const std::string key = key_of(magic_lgp_file_folders_[f]);
  933. if (ext == "tex"){
  934. const std::string stem = EffectSafeName(EffectStem(name));
  935. const std::string path = data_dir_ + "images/battle_fx/" + key + "_" + stem + ".png";
  936. if (boost::filesystem::exists(path)) continue;
  937. try{
  938. magic_lgp_files_[f].WriteFile(dir + key + "_" + stem + ".tex");
  939. TexFile tex(dir + key + "_" + stem + ".tex");
  940. tex.SavePng(path);
  941. }
  942. catch (const std::exception& ex){
  943. // Many of the original textures are dummies.
  944. }
  945. }
  946. else if (ext == "s"){
  947. File& sprite_file = magic_lgp_files_[f];
  948. const size_t size = sprite_file.GetFileSize();
  949. if (size < 8) continue;
  950. sprite_file.SetOffset(0);
  951. std::vector<unsigned char> bytes(size);
  952. for (size_t b = 0; b < size; b ++) bytes[b] = sprite_file.readU8();
  953. // Only the files with the header of sprite lists.
  954. if (bytes[0] != 0x23 || bytes[1] != 0x20) continue;
  955. // After the header (the two characters, and the number of frames as a 32 bit value, at 4)
  956. // there are the frames. Each one has two 16 bit values, the second is the number of quads,
  957. // and then 20 bytes for every quad: flags (4 bytes), the position of the corner (x and y,
  958. // 16 bits), the place in the texture (u and v, 16 bits), 4 more bytes, and the width
  959. // (8 bits, twice) and the height (8 bits, twice). Positions and sizes are in units of 8 of
  960. // the effect. Layout found in the code that draws them (0x598a7a), and all the files of
  961. // magic.lgp end exactly where their last frame ends.
  962. struct Quad{int x, y, u, v, w, h;};
  963. std::vector<std::vector<Quad>> frames;
  964. const uint32_t frame_count = bytes[4] | (bytes[5] << 8) | (bytes[6] << 16) | (static_cast<uint32_t>(bytes[7]) << 24);
  965. size_t p = 8;
  966. bool valid = true;
  967. for (uint32_t fr = 0; fr < frame_count && valid; ++ fr){
  968. if (p + 4 > size){valid = false; break;}
  969. const int count = static_cast<int16_t>(bytes[p + 2] | (bytes[p + 3] << 8));
  970. p += 4;
  971. if (count < 0 || p + static_cast<size_t>(count) * 20 > size){valid = false; break;}
  972. std::vector<Quad> quads;
  973. for (int q = 0; q < count; ++ q, p += 20){
  974. Quad quad;
  975. quad.x = static_cast<int16_t>(bytes[p + 4] | (bytes[p + 5] << 8));
  976. quad.y = static_cast<int16_t>(bytes[p + 6] | (bytes[p + 7] << 8));
  977. quad.u = static_cast<int16_t>(bytes[p + 8] | (bytes[p + 9] << 8));
  978. quad.v = static_cast<int16_t>(bytes[p + 10] | (bytes[p + 11] << 8));
  979. quad.w = bytes[p + 16];
  980. quad.h = bytes[p + 18];
  981. quads.push_back(quad);
  982. }
  983. frames.push_back(quads);
  984. }
  985. if (!valid || p != size || frames.empty()) continue;
  986. const std::string sprite_name = key + "_" + EffectSafeName(EffectStem(name));
  987. // All the quads one after the other, for the effects that pick them by number.
  988. file << "Game.EffectSprites[\"" << sprite_name << "\"] = {\n";
  989. for (const std::vector<Quad>& quads : frames){
  990. for (const Quad& quad : quads){
  991. file << " {u = " << quad.u << ", v = " << quad.v << ", w = " << quad.w << ", h = " << quad.h
  992. << ", ox = " << quad.x << ", oy = " << quad.y << "},\n";
  993. }
  994. }
  995. file << "}\n";
  996. // The frames, with their quads.
  997. animations << "Game.EffectAnimations[\"" << sprite_name << "\"] = {\n";
  998. for (const std::vector<Quad>& quads : frames){
  999. animations << " {";
  1000. for (size_t q = 0; q < quads.size(); ++ q){
  1001. animations << (q > 0 ? ", " : "") << "{" << quads[q].x << ", " << quads[q].y << ", " << quads[q].u << ", "
  1002. << quads[q].v << ", " << quads[q].w << ", " << quads[q].h << "}";
  1003. }
  1004. animations << "},\n";
  1005. }
  1006. animations << "}\n";
  1007. }
  1008. }
  1009. }
  1010. void BattleDataInstaller::WriteEnemies(){
  1011. for (Enemy enemy : enemies_){
  1012. TiXmlDocument xml;
  1013. std::unique_ptr<TiXmlElement> container(new TiXmlElement("enemy"));
  1014. container->SetAttribute("id", enemy.id);
  1015. container->SetAttribute("model", enemy.model);
  1016. container->SetAttribute("name", enemy.name);
  1017. container->SetAttribute("level", enemy.level);
  1018. container->SetAttribute("exp", enemy.exp);
  1019. container->SetAttribute("ap", enemy.ap);
  1020. container->SetAttribute("money", enemy.money);
  1021. container->SetAttribute("morph", enemy.morph);
  1022. container->SetDoubleAttribute("back_damage", enemy.back_damage);
  1023. std::unique_ptr<TiXmlElement> stats(new TiXmlElement("stats"));
  1024. std::unique_ptr<TiXmlElement> stat_str(new TiXmlElement("stat"));
  1025. stat_str->SetAttribute("id", "str");
  1026. stat_str->SetAttribute("value", enemy.str);
  1027. stats->LinkEndChild(stat_str.release());
  1028. std::unique_ptr<TiXmlElement> stat_mag(new TiXmlElement("stat"));
  1029. stat_mag->SetAttribute("id", "mag");
  1030. stat_mag->SetAttribute("value", enemy.mag);
  1031. stats->LinkEndChild(stat_mag.release());
  1032. std::unique_ptr<TiXmlElement> stat_def(new TiXmlElement("stat"));
  1033. stat_def->SetAttribute("id", "def");
  1034. stat_def->SetAttribute("value", enemy.def);
  1035. stats->LinkEndChild(stat_def.release());
  1036. std::unique_ptr<TiXmlElement> stat_mdef(new TiXmlElement("stat"));
  1037. stat_mdef->SetAttribute("id", "mdef");
  1038. stat_mdef->SetAttribute("value", enemy.mdef);
  1039. stats->LinkEndChild(stat_mdef.release());
  1040. std::unique_ptr<TiXmlElement> stat_spd(new TiXmlElement("stat"));
  1041. stat_spd->SetAttribute("id", "spd");
  1042. stat_spd->SetAttribute("value", enemy.spd);
  1043. stats->LinkEndChild(stat_spd.release());
  1044. std::unique_ptr<TiXmlElement> stat_lck(new TiXmlElement("stat"));
  1045. stat_lck->SetAttribute("id", "lck");
  1046. stat_lck->SetAttribute("value", enemy.lck);
  1047. stats->LinkEndChild(stat_lck.release());
  1048. std::unique_ptr<TiXmlElement> stat_eva(new TiXmlElement("stat"));
  1049. stat_eva->SetAttribute("id", "eva");
  1050. stat_eva->SetAttribute("value", enemy.eva);
  1051. stats->LinkEndChild(stat_eva.release());
  1052. container->LinkEndChild(stats.release());
  1053. std::unique_ptr<TiXmlElement> elements(new TiXmlElement("elements"));
  1054. for (Enemy::Element element : enemy.elements){
  1055. std::unique_ptr<TiXmlElement> xml_element(new TiXmlElement("element"));
  1056. xml_element->SetAttribute("id", element.id);
  1057. xml_element->SetDoubleAttribute("factor", element.factor);
  1058. elements->LinkEndChild(xml_element.release());
  1059. }
  1060. container->LinkEndChild(elements.release());
  1061. std::unique_ptr<TiXmlElement> immunities(new TiXmlElement("immunities"));
  1062. for (Enemy::Immunity immunity : enemy.immunities){
  1063. std::unique_ptr<TiXmlElement> xml_immunity(new TiXmlElement("immunity"));
  1064. xml_immunity->SetAttribute("status", immunity.status);
  1065. xml_immunity->SetDoubleAttribute("rate", immunity.rate);
  1066. immunities->LinkEndChild(xml_immunity.release());
  1067. }
  1068. container->LinkEndChild(immunities.release());
  1069. std::unique_ptr<TiXmlElement> attacks(new TiXmlElement("attacks"));
  1070. for (Enemy::Attack attack : enemy.attacks){
  1071. std::unique_ptr<TiXmlElement> xml_attack(new TiXmlElement("attack"));
  1072. xml_attack->SetAttribute("id", attack.id);
  1073. xml_attack->SetAttribute("animation", attack.animation);
  1074. xml_attack->SetAttribute("camera", attack.camera);
  1075. attacks->LinkEndChild(xml_attack.release());
  1076. }
  1077. container->LinkEndChild(attacks.release());
  1078. std::unique_ptr<TiXmlElement> manip_attacks(new TiXmlElement("manipulate"));
  1079. manip_attacks->SetAttribute("manipulable", enemy.manipulate_attacks.size() > 0 ? 1 : 0);
  1080. for (u16 attack : enemy.manipulate_attacks){
  1081. std::unique_ptr<TiXmlElement> xml_manip_attack(new TiXmlElement("attack"));
  1082. xml_manip_attack->SetAttribute("id", attack);
  1083. manip_attacks->LinkEndChild(xml_manip_attack.release());
  1084. }
  1085. container->LinkEndChild(manip_attacks.release());
  1086. std::unique_ptr<TiXmlElement> steals(new TiXmlElement("steal"));
  1087. for (Enemy::Item item : enemy.steal){
  1088. std::unique_ptr<TiXmlElement> xml_steal(new TiXmlElement("item"));
  1089. xml_steal->SetAttribute("id", item.id);
  1090. xml_steal->SetDoubleAttribute("rate", item.rate);
  1091. steals->LinkEndChild(xml_steal.release());
  1092. }
  1093. container->LinkEndChild(steals.release());
  1094. std::unique_ptr<TiXmlElement> drops(new TiXmlElement("drop"));
  1095. for (Enemy::Item item : enemy.drop){
  1096. std::unique_ptr<TiXmlElement> xml_drop(new TiXmlElement("item"));
  1097. xml_drop->SetAttribute("id", item.id);
  1098. xml_drop->SetDoubleAttribute("rate", item.rate);
  1099. drops->LinkEndChild(xml_drop.release());
  1100. }
  1101. container->LinkEndChild(drops.release());
  1102. std::unique_ptr<TiXmlElement> animations(new TiXmlElement("animations"));
  1103. for (unsigned int animation : enemy.animations){
  1104. std::unique_ptr<TiXmlElement> xml_animation(new TiXmlElement("animation"));
  1105. xml_animation->SetAttribute("id", animation);
  1106. animations->LinkEndChild(xml_animation.release());
  1107. }
  1108. container->LinkEndChild(animations.release());
  1109. // AI scripts, as hexadecimal bytecode.
  1110. {
  1111. static const char* AI_SLOT_NAMES[16] = {
  1112. "init", "main", "counter_general", "counter_death", "counter_physical", "counter_magic",
  1113. "battle_end", "pre_turn", "event_1", "event_2", "event_3", "event_4", "event_5",
  1114. "event_6", "event_7", "event_8"
  1115. };
  1116. std::unique_ptr<TiXmlElement> ai(new TiXmlElement("ai"));
  1117. for (size_t slot = 0; slot < enemy.ai_scripts.size() && slot < 16; slot ++){
  1118. if (enemy.ai_scripts[slot].empty()) continue;
  1119. std::string hex;
  1120. char digits[3];
  1121. for (u8 byte : enemy.ai_scripts[slot]){
  1122. snprintf(digits, sizeof(digits), "%02x", byte);
  1123. hex += digits;
  1124. }
  1125. std::unique_ptr<TiXmlElement> xml_script(new TiXmlElement("script"));
  1126. xml_script->SetAttribute("slot", static_cast<int>(slot));
  1127. xml_script->SetAttribute("name", AI_SLOT_NAMES[slot]);
  1128. xml_script->SetAttribute("data", hex);
  1129. ai->LinkEndChild(xml_script.release());
  1130. }
  1131. container->LinkEndChild(ai.release());
  1132. }
  1133. xml.LinkEndChild(container.release());
  1134. std::string file_name = BuildEnemyFileName(enemy);
  1135. xml.SaveFile(data_dir_ + file_name);
  1136. enemy_map_[enemy.id] = file_name;
  1137. }
  1138. }
  1139. void BattleDataInstaller::WriteAttacks(){
  1140. for (Attack attack : attacks_){
  1141. TiXmlDocument xml;
  1142. std::unique_ptr<TiXmlElement> container(new TiXmlElement("Attack"));
  1143. container->SetAttribute("id", attack.id);
  1144. container->SetAttribute("name", attack.name);
  1145. container->SetAttribute("mp", attack.mp);
  1146. container->SetAttribute("reflectable", attack.reflectable);
  1147. std::unique_ptr<TiXmlElement> damage(new TiXmlElement("Damage"));
  1148. std::unique_ptr<TiXmlElement> damage_power(new TiXmlElement("Parameter"));
  1149. damage_power->SetAttribute("id", "power");
  1150. damage_power->SetAttribute("value", attack.damage.power);
  1151. damage->LinkEndChild(damage_power.release());
  1152. std::unique_ptr<TiXmlElement> damage_formula(new TiXmlElement("Parameter"));
  1153. damage_formula->SetAttribute("id", "formula");
  1154. damage_formula->SetAttribute("value", attack.damage.formula);
  1155. damage->LinkEndChild(damage_formula.release());
  1156. std::unique_ptr<TiXmlElement> damage_modifier(new TiXmlElement("Parameter"));
  1157. damage_modifier->SetAttribute("id", "modifier");
  1158. damage_modifier->SetAttribute("value", attack.damage.modifier);
  1159. damage->LinkEndChild(damage_modifier.release());
  1160. std::unique_ptr<TiXmlElement> damage_acc(new TiXmlElement("Parameter"));
  1161. damage_acc->SetAttribute("id", "accuracy");
  1162. // Accuracy is a fraction between 0 and 1, it can't be written as an integer.
  1163. damage_acc->SetDoubleAttribute("value", attack.damage.accuracy);
  1164. damage->LinkEndChild(damage_acc.release());
  1165. std::unique_ptr<TiXmlElement> damage_darkness(new TiXmlElement("Parameter"));
  1166. damage_darkness->SetAttribute("id", "affected_by_darkness");
  1167. damage_darkness->SetAttribute("value", attack.damage.affected_by_darkness);
  1168. damage->LinkEndChild(damage_darkness.release());
  1169. std::unique_ptr<TiXmlElement> damage_restore(new TiXmlElement("Parameter"));
  1170. damage_restore->SetAttribute("id", "restore");
  1171. damage_restore->SetAttribute("value", static_cast<int>(attack.damage.restore));
  1172. damage->LinkEndChild(damage_restore.release());
  1173. std::unique_ptr<TiXmlElement> damage_mp(new TiXmlElement("Parameter"));
  1174. damage_mp->SetAttribute("id", "damage_mp");
  1175. damage_mp->SetAttribute("value", attack.damage.damage_mp);
  1176. damage->LinkEndChild(damage_mp.release());
  1177. std::unique_ptr<TiXmlElement> damage_drain_hp(new TiXmlElement("Parameter"));
  1178. damage_drain_hp->SetAttribute("id", "drain_hp");
  1179. damage_drain_hp->SetAttribute("value", attack.damage.drain_hp);
  1180. damage->LinkEndChild(damage_drain_hp.release());
  1181. std::unique_ptr<TiXmlElement> damage_drain_mp(new TiXmlElement("Parameter"));
  1182. damage_drain_mp->SetAttribute("id", "drain_mp");
  1183. damage_drain_mp->SetAttribute("value", attack.damage.drain_mp);
  1184. damage->LinkEndChild(damage_drain_mp.release());
  1185. std::unique_ptr<TiXmlElement> damage_ignore(new TiXmlElement("Parameter"));
  1186. damage_ignore->SetAttribute("id", "ignore_status_defense");
  1187. damage_ignore->SetAttribute("value", attack.damage.ignore_status_defense);
  1188. damage->LinkEndChild(damage_ignore.release());
  1189. std::unique_ptr<TiXmlElement> damage_critical(new TiXmlElement("Parameter"));
  1190. damage_critical->SetAttribute("id", "always_critical");
  1191. damage_critical->SetAttribute("value", attack.damage.critical);
  1192. damage->LinkEndChild(damage_critical.release());
  1193. container->LinkEndChild(damage.release());
  1194. std::unique_ptr<TiXmlElement> target_opts(new TiXmlElement("Target"));
  1195. std::unique_ptr<TiXmlElement> target_se(new TiXmlElement("Parameter"));
  1196. target_se->SetAttribute("id", "selection_enabled");
  1197. target_se->SetAttribute("value", attack.target.selection_enabled);
  1198. target_opts->LinkEndChild(target_se.release());
  1199. std::unique_ptr<TiXmlElement> target_de(new TiXmlElement("Parameter"));
  1200. target_de->SetAttribute("id", "default_enemy");
  1201. target_de->SetAttribute("value", attack.target.default_enemy);
  1202. target_opts->LinkEndChild(target_de.release());
  1203. std::unique_ptr<TiXmlElement> target_dm(new TiXmlElement("Parameter"));
  1204. target_dm->SetAttribute("id", "default_multiple");
  1205. target_dm->SetAttribute("value", attack.target.default_multiple);
  1206. target_opts->LinkEndChild(target_dm.release());
  1207. std::unique_ptr<TiXmlElement> target_tm(new TiXmlElement("Parameter"));
  1208. target_tm->SetAttribute("id", "toggle_multiple");
  1209. target_tm->SetAttribute("value", attack.target.toggle_multiple);
  1210. target_opts->LinkEndChild(target_tm.release());
  1211. std::unique_ptr<TiXmlElement> target_fr(new TiXmlElement("Parameter"));
  1212. target_fr->SetAttribute("id", "fixed_row");
  1213. target_fr->SetAttribute("value", attack.target.fixed_row);
  1214. target_opts->LinkEndChild(target_fr.release());
  1215. std::unique_ptr<TiXmlElement> target_sr(new TiXmlElement("Parameter"));
  1216. target_sr->SetAttribute("id", "short_range");
  1217. target_sr->SetAttribute("value", attack.target.short_range);
  1218. target_opts->LinkEndChild(target_sr.release());
  1219. std::unique_ptr<TiXmlElement> target_ar(new TiXmlElement("Parameter"));
  1220. target_ar->SetAttribute("id", "all_rows");
  1221. target_ar->SetAttribute("value", attack.target.all_rows);
  1222. target_opts->LinkEndChild(target_ar.release());
  1223. std::unique_ptr<TiXmlElement> target_rd(new TiXmlElement("Parameter"));
  1224. target_rd->SetAttribute("id", "random");
  1225. target_rd->SetAttribute("value", attack.target.random);
  1226. target_opts->LinkEndChild(target_rd.release());
  1227. std::unique_ptr<TiXmlElement> target_od(new TiXmlElement("Parameter"));
  1228. target_od->SetAttribute("id", "only_for_dead");
  1229. target_od->SetAttribute("value", attack.target.only_for_dead);
  1230. target_opts->LinkEndChild(target_od.release());
  1231. std::unique_ptr<TiXmlElement> target_nr(new TiXmlElement("Parameter"));
  1232. target_nr->SetAttribute("id", "no_retarget");
  1233. target_nr->SetAttribute("value", attack.target.no_re_target);
  1234. target_opts->LinkEndChild(target_nr.release());
  1235. container->LinkEndChild(target_opts.release());
  1236. std::unique_ptr<TiXmlElement> effects(new TiXmlElement("Effects"));
  1237. for (Attack::StatusEffect effect : attack.status_effects){
  1238. std::unique_ptr<TiXmlElement> xml_effect(new TiXmlElement("Effect"));
  1239. xml_effect->SetAttribute("id", effect.id);
  1240. xml_effect->SetDoubleAttribute("chance", effect.chance);
  1241. xml_effect->SetAttribute("mode", GameData::STATUS_EFFECT_MODE::to_string(effect.type));
  1242. effects->LinkEndChild(xml_effect.release());
  1243. }
  1244. container->LinkEndChild(effects.release());
  1245. std::unique_ptr<TiXmlElement> elements(new TiXmlElement("Elements"));
  1246. for (unsigned int element : attack.elements){
  1247. std::unique_ptr<TiXmlElement> xml_element(new TiXmlElement("Element"));
  1248. xml_element->SetAttribute("id", element);
  1249. elements->LinkEndChild(xml_element.release());
  1250. }
  1251. container->LinkEndChild(elements.release());
  1252. std::unique_ptr<TiXmlElement> anim(new TiXmlElement("Animation"));
  1253. std::unique_ptr<TiXmlElement> anim_e(new TiXmlElement("Parameter"));
  1254. anim_e->SetAttribute("id", "effect");
  1255. anim_e->SetAttribute("value", attack.animation.effect);
  1256. anim->LinkEndChild(anim_e.release());
  1257. std::unique_ptr<TiXmlElement> anim_ie(new TiXmlElement("Parameter"));
  1258. anim_ie->SetAttribute("id", "impact_effect");
  1259. anim_ie->SetAttribute("value", attack.animation.impact_effect);
  1260. anim->LinkEndChild(anim_ie.release());
  1261. std::unique_ptr<TiXmlElement> anim_s(new TiXmlElement("Parameter"));
  1262. anim_s->SetAttribute("id", "sound");
  1263. anim_s->SetAttribute("value", attack.animation.sound);
  1264. anim->LinkEndChild(anim_s.release());
  1265. std::unique_ptr<TiXmlElement> anim_ha(new TiXmlElement("Parameter"));
  1266. anim_ha->SetAttribute("id", "hurt_anim");
  1267. anim_ha->SetAttribute("value", attack.animation.hurt_anim);
  1268. anim->LinkEndChild(anim_ha.release());
  1269. std::unique_ptr<TiXmlElement> anim_c(new TiXmlElement("Parameter"));
  1270. anim_c->SetAttribute("id", "camera_single");
  1271. anim_c->SetAttribute("value", attack.animation.camera_single);
  1272. anim->LinkEndChild(anim_c.release());
  1273. std::unique_ptr<TiXmlElement> anim_cm(new TiXmlElement("Parameter"));
  1274. anim_cm->SetAttribute("id", "camera_multiple");
  1275. anim_cm->SetAttribute("value", attack.animation.camera_multiple);
  1276. anim->LinkEndChild(anim_cm.release());
  1277. container->LinkEndChild(anim.release());
  1278. xml.LinkEndChild(container.release());
  1279. std::string file_name = BuildAttackFileName(attack);
  1280. xml.SaveFile(data_dir_ + file_name);
  1281. attack_map_[attack.id] = file_name;
  1282. }
  1283. // Index of the attack files by ID, as file names include the attack name.
  1284. TiXmlDocument index;
  1285. std::unique_ptr<TiXmlElement> attacks(new TiXmlElement("attacks"));
  1286. for (const auto& entry : attack_map_){
  1287. std::unique_ptr<TiXmlElement> xml_attack(new TiXmlElement("attack"));
  1288. xml_attack->SetAttribute("id", entry.first);
  1289. // The file name in the map has the gamedata/attack/ path.
  1290. xml_attack->SetAttribute("file", entry.second.substr(entry.second.find_last_of('/') + 1));
  1291. attacks->LinkEndChild(xml_attack.release());
  1292. }
  1293. index.LinkEndChild(attacks.release());
  1294. index.SaveFile(data_dir_ + "gamedata/attack/_index.xml");
  1295. }
  1296. void BattleDataInstaller::WriteFormations(){
  1297. for (Formation formation : formations_){
  1298. TiXmlDocument xml;
  1299. std::unique_ptr<TiXmlElement> container(new TiXmlElement("formation"));
  1300. container->SetAttribute("id", formation.id);
  1301. container->SetAttribute("next", formation.next_formation);
  1302. container->SetDoubleAttribute("escape_difficulty", formation.escape_counter);
  1303. container->SetAttribute("escapable", formation.escapable);
  1304. container->SetAttribute("skip_victory_pose", formation.skip_victory_pose);
  1305. container->SetAttribute("skip_spoils", formation.skip_spoils);
  1306. container->SetAttribute("skip_victory_pose", formation.skip_victory_pose);
  1307. container->SetAttribute("preemptive_disabled", formation.preemptive_disabled);
  1308. container->SetAttribute("layout", static_cast<int>(formation.layout));
  1309. std::unique_ptr<TiXmlElement> location(new TiXmlElement("location"));
  1310. location->SetAttribute("id", formation.location);
  1311. location->SetAttribute("name", formation.location_name);
  1312. container->LinkEndChild(location.release());
  1313. std::unique_ptr<TiXmlElement> enemies(new TiXmlElement("enemies"));
  1314. for (Formation::Enemy enemy : formation.enemies){
  1315. std::unique_ptr<TiXmlElement> enemy_xml(new TiXmlElement("enemy"));
  1316. enemy_xml->SetAttribute("id", enemy.id);
  1317. enemy_xml->SetAttribute("x", enemy.x);
  1318. enemy_xml->SetAttribute("y", enemy.y);
  1319. enemy_xml->SetAttribute("z", enemy.z);
  1320. enemy_xml->SetAttribute("row", enemy.row);
  1321. std::string cover = "";
  1322. for (int c = 0; c < 5; c ++) cover += enemy.cover[c];
  1323. enemy_xml->SetAttribute("cover", cover);
  1324. enemy_xml->SetAttribute("visible", enemy.visible);
  1325. enemy_xml->SetAttribute("direction", enemy.direction);
  1326. enemy_xml->SetAttribute("targeteable", enemy.targeteable);
  1327. enemy_xml->SetAttribute("main_script_active", enemy.main_script_active);
  1328. enemies->LinkEndChild(enemy_xml.release());
  1329. }
  1330. container->LinkEndChild(enemies.release());
  1331. std::unique_ptr<TiXmlElement> cameras(new TiXmlElement("camera"));
  1332. cameras->SetAttribute("initial", formation.initial_camera_position);
  1333. int c = 0;
  1334. for (Formation::Camera camera : formation.camera_positions){
  1335. std::unique_ptr<TiXmlElement> camera_xml(new TiXmlElement("position"));
  1336. camera_xml->SetAttribute("id", c);
  1337. camera_xml->SetAttribute("x", camera.x);
  1338. camera_xml->SetAttribute("y", camera.y);
  1339. camera_xml->SetAttribute("z", camera.z);
  1340. camera_xml->SetAttribute("direction_x", camera.d_x);
  1341. camera_xml->SetAttribute("direction_y", camera.d_y);
  1342. camera_xml->SetAttribute("direction_z", camera.d_z);
  1343. cameras->LinkEndChild(camera_xml.release());
  1344. c ++;
  1345. }
  1346. container->LinkEndChild(cameras.release());
  1347. std::unique_ptr<TiXmlElement> arena(new TiXmlElement("arena"));
  1348. arena->SetAttribute("is_arena", formation.is_arena_battle);
  1349. for (int next : formation.next_arena_formation_candidates){
  1350. std::unique_ptr<TiXmlElement> next_xml(new TiXmlElement("next_candidate"));
  1351. next_xml->SetAttribute("id", next);
  1352. arena->LinkEndChild(next_xml.release());
  1353. }
  1354. container->LinkEndChild(arena.release());
  1355. xml.LinkEndChild(container.release());
  1356. std::string file_name = std::to_string(formation.id);
  1357. while (file_name.length() < 4) file_name = "0" + file_name;
  1358. file_name = "gamedata/formation/" + file_name + ".xml";
  1359. xml.SaveFile(data_dir_ + file_name);
  1360. formation_map_[formation.id] = file_name;
  1361. }
  1362. }
  1363. void BattleDataInstaller::WriteCharacterData(){
  1364. TiXmlDocument xml;
  1365. std::unique_ptr<TiXmlElement> container(new TiXmlElement("characters"));
  1366. for (CharacterData data : character_data_){
  1367. std::unique_ptr<TiXmlElement> xml_character(new TiXmlElement("character"));
  1368. xml_character->SetAttribute("id", data.id);
  1369. for (CharacterModel model : data.models){
  1370. std::unique_ptr<TiXmlElement> xml_model(new TiXmlElement("model"));
  1371. xml_model->SetAttribute("name", model.name);
  1372. xml_model->SetAttribute("file", model.model);
  1373. xml_character->LinkEndChild(xml_model.release());
  1374. }
  1375. container->LinkEndChild(xml_character.release());
  1376. }
  1377. xml.LinkEndChild(container.release());
  1378. xml.SaveFile(data_dir_ + "models/battle/characters.xml");
  1379. }
  1380. void BattleDataInstaller::WriteSceneData(){
  1381. TiXmlDocument xml;
  1382. std::unique_ptr<TiXmlElement> container(new TiXmlElement("scenes"));
  1383. for (SceneModel data : scene_data_){
  1384. std::unique_ptr<TiXmlElement> xml_scene(new TiXmlElement("scene"));
  1385. xml_scene->SetAttribute("id", data.id);
  1386. xml_scene->SetAttribute("file", data.model);
  1387. xml_scene->SetAttribute("description", data.description);
  1388. container->LinkEndChild(xml_scene.release());
  1389. }
  1390. xml.LinkEndChild(container.release());
  1391. xml.SaveFile(data_dir_ + "models/battle/scenes.xml");
  1392. }
  1393. std::string BattleDataInstaller::BuildEnemyFileName(Enemy enemy){
  1394. std::string file_name = "gamedata/enemy/";
  1395. std::string id = std::to_string(enemy.id);
  1396. while (id.size() < 4) id = "0" + id;
  1397. file_name += id + ".xml";
  1398. return file_name;
  1399. }
  1400. std::string BattleDataInstaller::BuildAttackFileName(Attack attack){
  1401. std::string file_name = "gamedata/attack/";
  1402. std::string id = std::to_string(attack.id);
  1403. while (id.size() < 4) id = "0" + id;
  1404. file_name += (id + "_");
  1405. for (int n = 0; n < attack.name.size(); n ++){
  1406. if (
  1407. (attack.name[n] >= '0' && attack.name[n] <= '9')
  1408. || (attack.name[n] >= 'a' && attack.name[n] <= 'z')
  1409. || (attack.name[n] >= 'A' && attack.name[n] <= 'Z')
  1410. ) file_name += attack.name[n];
  1411. }
  1412. file_name += ".xml";
  1413. return file_name;
  1414. }
  1415. File* BattleDataInstaller::ExtractGZipScene(File file){
  1416. u32 offset = 0;
  1417. u8* file_buffer = static_cast<u8*>(malloc(file.GetFileSize()));
  1418. file.GetFileBuffer(file_buffer, 0, file.GetFileSize());
  1419. u32 extract_size = 256 * 1024;
  1420. u8* extract_buffer = static_cast<u8*>(malloc(extract_size));
  1421. int ret;
  1422. z_stream strm;
  1423. strm.zalloc = Z_NULL; // Used to allocate the internal state.
  1424. strm.zfree = Z_NULL; // Used to free the internal state.
  1425. strm.opaque = Z_NULL; // Private data object passed to zalloc and zfree.
  1426. strm.next_in = file_buffer + offset; //Next input byte.
  1427. strm.avail_in = file.GetFileSize() - offset; // Number of bytes available at next_in.
  1428. strm.next_out = extract_buffer; // Next output byte should be put there.
  1429. strm.avail_out = extract_size; // Remaining free space at next_out.
  1430. ret = inflateInit2(&strm, 15 + 32);
  1431. if (ret != Z_OK){
  1432. switch (ret){
  1433. case Z_MEM_ERROR:
  1434. inflateEnd(&strm);
  1435. std::cout << "Warning: inflateInit2 - Z_MEM_ERROR in file " << file.GetFileName() << std::endl;
  1436. break;
  1437. case Z_STREAM_ERROR:
  1438. inflateEnd(&strm);
  1439. std::cout << "Warning: inflateInit2 - Z_STREAM_ERROR in file " << file.GetFileName() << std::endl;
  1440. break;
  1441. default:
  1442. inflateEnd(&strm);
  1443. std::cout << "Warning: inflateInit2 - unknown error in file " << file.GetFileName() << std::endl;
  1444. }
  1445. return NULL;
  1446. }
  1447. do{
  1448. if (strm.next_out == NULL){
  1449. inflateEnd(&strm);
  1450. std::cout << "Warning: strm.next_out == NULL in file " << file.GetFileName() << std::endl;
  1451. return NULL;
  1452. }
  1453. ret = inflate(&strm, Z_NO_FLUSH);
  1454. assert(ret != Z_STREAM_ERROR);
  1455. switch (ret){
  1456. case Z_NEED_DICT:
  1457. inflateEnd(&strm);
  1458. std::cout << "Warning: inflate - Z_NEED_DICT in file " << file.GetFileName() << std::endl;
  1459. return NULL;
  1460. case Z_DATA_ERROR:
  1461. inflateEnd(&strm);
  1462. std::cout << "Warning: inflate - Z_DATA_ERROR in file " << file.GetFileName() << std::endl;
  1463. return NULL;
  1464. case Z_MEM_ERROR:
  1465. inflateEnd(&strm);
  1466. std::cout << "Warning: inflate - Z_MEM_ERROR in file " << file.GetFileName() << std::endl;
  1467. return NULL;
  1468. }
  1469. if (ret != Z_STREAM_END){
  1470. extract_buffer = static_cast<u8*>(realloc(extract_buffer, extract_size * 2));
  1471. if (extract_buffer == NULL){
  1472. inflateEnd(&strm);
  1473. //std::cout << "Warning: extract_buffer == NULL in file "
  1474. // << file.GetFileName() << std::endl;
  1475. return NULL;
  1476. }
  1477. strm.next_out = static_cast<u8*>(extract_buffer + extract_size);
  1478. strm.avail_out = extract_size;
  1479. extract_size *= 2;
  1480. }
  1481. } while (ret != Z_STREAM_END);
  1482. extract_size = extract_size - strm.avail_out;
  1483. (void)inflateEnd(&strm);
  1484. if (ret != Z_STREAM_END){
  1485. std::cout << "Warning: ret != Z_STREAM_END in file " << file.GetFileName() << std::endl;
  1486. return NULL;
  1487. }
  1488. File* out_file = new File(extract_buffer, 0, extract_size);
  1489. return out_file;
  1490. }
  1491. void BattleDataInstaller::ExportMesh(const std::string outdir, const Ogre::MeshPtr &mesh){
  1492. VGears::String base_mesh_name;
  1493. VGears::StringUtil::splitFull(mesh->getName(), base_mesh_name);
  1494. MakeMaterialsUnique(mesh, outdir + base_mesh_name);
  1495. Ogre::MeshSerializer mesh_serializer;
  1496. Ogre::SkeletonPtr skeleton(mesh->getSkeleton());
  1497. Ogre::SkeletonSerializer skeleton_serializer;
  1498. skeleton_serializer.exportSkeleton(
  1499. skeleton.get(), data_dir_ + "models/battle/" + outdir + base_mesh_name + ".skeleton"
  1500. );
  1501. mesh->setSkeletonName(outdir + base_mesh_name + ".skeleton");
  1502. mesh_serializer.exportMesh(mesh.get(), data_dir_ + "models/battle/" + outdir + mesh->getName());
  1503. Ogre::MaterialSerializer mat_ser;
  1504. std::set<std::string> textures;
  1505. Ogre::Mesh::SubMeshList sub_meshes = mesh->getSubMeshes();
  1506. for (Ogre::SubMesh* sub_mesh : sub_meshes){
  1507. Ogre::MaterialPtr mat(Ogre::MaterialManager::getSingleton().getByName(sub_mesh->getMaterialName()));
  1508. if (mat == nullptr) continue;
  1509. for (size_t techs = 0; techs < mat->getNumTechniques(); techs ++){
  1510. Ogre::Technique* tech = mat->getTechnique(techs);
  1511. if (tech == nullptr) continue;
  1512. for (size_t pass_num = 0; pass_num < tech->getNumPasses(); pass_num ++){
  1513. Ogre::Pass* pass = tech->getPass(pass_num);
  1514. if (pass == nullptr) continue;
  1515. for (
  1516. size_t texture_unit_num = 0; texture_unit_num < pass->getNumTextureUnitStates(); texture_unit_num ++
  1517. ){
  1518. Ogre::TextureUnitState* unit = pass->getTextureUnitState(texture_unit_num);
  1519. if (unit && unit->getTextureName().empty() == false){
  1520. // Ensure the output material script references png files, no tex files.
  1521. Ogre::String base_name;
  1522. VGears::StringUtil::splitBase(unit->getTextureName(), base_name);
  1523. unit->setTextureName(outdir + base_name + ".png");
  1524. textures.insert(unit->getTextureName());
  1525. }
  1526. }
  1527. }
  1528. }
  1529. if (std::count(battle_materials_.begin(), battle_materials_.end(), sub_mesh->getMaterialName()) == 0){
  1530. mat_ser.queueForExport(mat);
  1531. battle_materials_.push_back(sub_mesh->getMaterialName());
  1532. }
  1533. }
  1534. mat_ser.exportQueued(data_dir_ + "models/battle/" + outdir + base_mesh_name + VGears::EXT_MATERIAL);
  1535. }
  1536. void BattleDataInstaller::GenerateRsdFiles(BattleModel model, std::string path){
  1537. std::sort(model.tex.begin(), model.tex.end());
  1538. // For each p file, generate a rsd file.
  1539. for (std::string p : model.p){
  1540. std::string base_name = p.substr(0, 4);
  1541. std::string content = "@RSD940102\n";
  1542. content += "PLY=" + base_name + ".PLY\n";
  1543. content += "MAT=" + base_name + ".MAT\n";
  1544. content += "GRP=" + base_name + ".GRP\n";
  1545. content += "NTEX=" + std::to_string(model.tex.size()) + "\n";
  1546. for (int t = 0; t < model.tex.size(); t ++) content += "TEX[" + std::to_string(t) + "]=" + model.tex[t] + "\n";
  1547. content += "\n";
  1548. std::ofstream out(path + base_name + ".rsd");
  1549. out << content;
  1550. out.close();
  1551. }
  1552. }
  1553. void BattleDataInstaller::GenerateRsdFiles(SpellModel model, std::string path){
  1554. //std::sort(model.tex.begin(), model.tex.end());
  1555. // For each p file, generate a rsd file.
  1556. for (std::string p : model.p){
  1557. std::string base_name = p.substr(0, p.length() - 2);
  1558. std::string content = "@RSD940102\n";
  1559. content += "PLY=" + base_name + ".PLY\n";
  1560. content += "MAT=" + base_name + ".MAT\n";
  1561. content += "GRP=" + base_name + ".GRP\n";
  1562. content += "NTEX=0\n";
  1563. //for (int t = 0; t < model.tex.size(); t ++)
  1564. // content += "TEX[" + std::to_string(t) + "]=" + model.tex[t] + "\n";
  1565. content += "\n";
  1566. std::ofstream out(path + base_name + ".rsd");
  1567. out << content;
  1568. out.close();
  1569. }
  1570. }
  1571. void BattleDataInstaller::DecompileBattleModelHrc(
  1572. File compiled, BattleModel model, std::string path, std::string skeleton_name
  1573. ){
  1574. compiled.SetOffset(0);
  1575. compiled.readU32LE();
  1576. compiled.readU32LE();
  1577. compiled.readU32LE();
  1578. std::sort(model.p.begin(), model.p.end());
  1579. std::string decompiled(":HEADER_BLOCK 2\n:SKELETON " + skeleton_name + "\n");
  1580. int bones = compiled.readU32LE();
  1581. compiled.SetOffset(compiled.GetFileSize() - (bones * 12));
  1582. decompiled += ":BONES " + std::to_string(bones) + "\n\n";
  1583. int p_file_index = 0;
  1584. for (int b = 0; b < bones; b ++){
  1585. decompiled += "bone" + std::to_string(b) + "\n";
  1586. int parent = compiled.readU32LE();
  1587. if (parent == 0xFFFFFFFF) decompiled += "root\n";
  1588. else decompiled += "bone" + std::to_string(parent) + "\n";
  1589. union {
  1590. float f;
  1591. unsigned long ul;
  1592. unsigned char b[4];
  1593. } u;
  1594. u.b[0] = compiled.readU8();
  1595. u.b[1] = compiled.readU8();
  1596. u.b[2] = compiled.readU8();
  1597. u.b[3] = compiled.readU8();
  1598. decompiled += std::to_string(u.f * -1) + "\n";
  1599. int offset = compiled.readU32LE();
  1600. if (offset == 0) decompiled += "0\n\n";
  1601. else {
  1602. decompiled += std::to_string(offset) + " ";
  1603. if (p_file_index < model.p.size()) decompiled += model.p[p_file_index].substr(0, 4);
  1604. p_file_index ++;
  1605. decompiled += "\n\n";
  1606. }
  1607. }
  1608. std::ofstream out(path);
  1609. out << decompiled;
  1610. out.close();
  1611. }
  1612. void BattleDataInstaller::DecompileSpellModelHrc(
  1613. File compiled, SpellModel model, std::string path, std::string skeleton_name
  1614. ){
  1615. compiled.SetOffset(0);
  1616. compiled.readU32LE();
  1617. compiled.readU32LE();
  1618. compiled.readU32LE();
  1619. std::sort(model.p.begin(), model.p.end());
  1620. std::string decompiled(":HEADER_BLOCK 2\n:SKELETON " + skeleton_name + "\n");
  1621. int bones = compiled.readU32LE();
  1622. compiled.SetOffset(compiled.GetFileSize() - (bones * 12));
  1623. decompiled += ":BONES " + std::to_string(bones) + "\n\n";
  1624. int p_index = 0;
  1625. for (int b = 0; b < bones; b ++){
  1626. decompiled += "bone" + std::to_string(b) + "\n";
  1627. int parent = compiled.readU32LE();
  1628. if (parent == 0xFFFFFFFF) decompiled += "root\n";
  1629. else decompiled += "bone" + std::to_string(parent) + "\n";
  1630. union {
  1631. float f;
  1632. unsigned long ul;
  1633. unsigned char b[4];
  1634. } u;
  1635. u.b[0] = compiled.readU8();
  1636. u.b[1] = compiled.readU8();
  1637. u.b[2] = compiled.readU8();
  1638. u.b[3] = compiled.readU8();
  1639. decompiled += std::to_string(u.f * -1) + "\n";
  1640. int offset = compiled.readU32LE();
  1641. if (offset == 0) decompiled += "0\n\n";
  1642. else {
  1643. decompiled += std::to_string(offset) + " ";
  1644. if (p_index < model.p.size()) decompiled += model.p[p_index].substr(0, model.p[p_index].length() - 2);
  1645. p_index ++;
  1646. decompiled += "\n\n";
  1647. }
  1648. }
  1649. std::ofstream out(path);
  1650. out << decompiled;
  1651. out.close();
  1652. }
  1653. void BattleDataInstaller::DecompileSceneModelHrc(
  1654. File compiled, BattleModel model, std::string path, std::string skeleton_name
  1655. ){
  1656. compiled.SetOffset(0);
  1657. compiled.readU32LE(); // Skip 4 bytes
  1658. compiled.readU32LE(); // Skip 4 bytes
  1659. compiled.readU32LE(); // Skip 4 bytes
  1660. compiled.readU32LE(); // Supposed to be the number of bones, but for scenes is always 0.
  1661. std::sort(model.p.begin(), model.p.end());
  1662. std::string decompiled(":HEADER_BLOCK 2\n:SKELETON " + skeleton_name + "\n");
  1663. int bones = model.p.size();
  1664. decompiled += ":BONES " + std::to_string(bones) + "\n\n";
  1665. int p_file_index = 0;
  1666. for (int b = 0; b < bones; b ++){
  1667. decompiled += "bone" + std::to_string(b) + "\n";
  1668. if (b == 0) decompiled += "root\n";
  1669. else decompiled += ("bone" + std::to_string(b - 1) + "\n");
  1670. decompiled += "100.000000\n";
  1671. decompiled += "1 " + model.p[b].substr(0, 4) + "\n\n";
  1672. }
  1673. std::ofstream out(path);
  1674. out << decompiled;
  1675. out.close();
  1676. }