FieldDataInstaller.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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 <string>
  16. #include <boost/filesystem.hpp>
  17. #include <QtCore/QDir>
  18. #include "FieldDataInstaller.h"
  19. #include "TexFile.h"
  20. #include "common/File.h"
  21. #include "data/VGearsMapListFile.h"
  22. #include "data/VGearsLZSFLevelFileManager.h"
  23. #include "data/VGearsHRCFileManager.h"
  24. #include "data/VGearsLGPArchive.h"
  25. #include "data/VGearsAFileManager.h"
  26. float FieldDataInstaller::LINE_SCALE_FACTOR(0.0078124970964f);
  27. std::string FieldDataInstaller::FIELD_MODELS_DIR("models/fields/entities");
  28. std::string FieldDataInstaller::FIELD_MAPS_DIR("fields");
  29. int FieldDataInstaller::INACTIVE_GATEWAY_ID(32767);
  30. std::string FieldDataInstaller::CreateGateWayScript(
  31. const std::string& gateway_entity_name, const std::string& target_map_name,
  32. const std::string& source_spawn_point_name
  33. ){
  34. return
  35. "\nEntityContainer[ \"" + gateway_entity_name + "\" ] = {\n\n"
  36. " on_start = function(self)\n"
  37. " return 0\n"
  38. " end,\n\n"
  39. " on_approach = function(self, entity)\n"
  40. " return 0\n"
  41. " end,\n\n"
  42. " on_cross = function(self, entity)\n"
  43. " return 0\n"
  44. " end,\n\n"
  45. " on_near = function(self, entity)\n"
  46. " if entity == \"Cloud\" then\n"
  47. " if not Data.DisableGateways then\n"
  48. " load_field_map_request(\""
  49. + target_map_name + "\", \"" + source_spawn_point_name + "\")\n"
  50. " end\n"
  51. " end\n"
  52. " return 0\n"
  53. " end,\n\n"
  54. " on_leave = function(self, entity)\n"
  55. " return 0\n"
  56. " end,\n"
  57. "}\n\n";
  58. }
  59. size_t FieldDataInstaller::GetFieldId(
  60. const std::string& name, const std::vector<std::string>& field_id_to_name_lookup
  61. ){
  62. for (size_t i = 0; i < field_id_to_name_lookup.size(); i ++)
  63. if (field_id_to_name_lookup[i] == name) return i;
  64. throw std::runtime_error("No Id found for field name");
  65. }
  66. bool FieldDataInstaller::IsAFieldFile(const Ogre::String& resource_name){
  67. return (
  68. !VGears::StringUtil::endsWith(resource_name, ".tex")
  69. && !VGears::StringUtil::endsWith(resource_name, ".tut")
  70. && !VGears::StringUtil::endsWith(resource_name, ".siz")
  71. && resource_name != "maplist"
  72. );
  73. }
  74. bool FieldDataInstaller::WillCrash(const Ogre::String& resource_name){
  75. if (
  76. resource_name == "bugin1a" // crashes in back ground image generation
  77. || resource_name == "frcyo" // Hangs in decompliation
  78. || resource_name == "fr_e" // Background image crash
  79. || resource_name == "las4_2" // Background image crash
  80. || resource_name == "las4_3" // Background image crash
  81. || resource_name == "lastmap" // Background image crash
  82. || resource_name == "md_e1" // Background image crash
  83. || resource_name == "trnad_3" // Background image crash
  84. || resource_name == "bonevil2" // stof
  85. || resource_name == "coloin1" // boost::bad_format_string: format-string is ill-formed
  86. || resource_name == "del3" // Segmentation fault.
  87. || resource_name == "elmin4_2" // boost::bad_format_string: format-string is ill-formed
  88. ){return true;}
  89. return false;
  90. }
  91. bool FieldDataInstaller::IsTestField(const Ogre::String& resource_name){
  92. if (
  93. resource_name == "startmap"
  94. || resource_name == "md1stin"
  95. || resource_name == "md1_1"
  96. || resource_name == "md1_2"
  97. || resource_name == "md8_1"
  98. || resource_name == "nmkin_1"
  99. || resource_name == "nmkin_2"
  100. || resource_name == "nmkin_3"
  101. || resource_name == "nmkin_4"
  102. || resource_name == "nmkin_5"
  103. || resource_name == "nrthmk"
  104. || resource_name == "elevtr1"
  105. || resource_name == "tin_1"
  106. || resource_name == "tin_2"
  107. || resource_name == "rootmap"
  108. || resource_name == "md8_4"
  109. ){return true;}
  110. return false;
  111. }
  112. void FieldDataInstaller::CreateDir(const std::string& path){
  113. QString target = QString::fromStdString(output_dir_ + path);
  114. QDir dir(target);
  115. if (!dir.mkpath(".")) throw std::runtime_error("Failed to mkpath");
  116. }
  117. void FieldDataInstaller::CollectFieldScaleFactors(
  118. VGears::FLevelFilePtr& field, FieldScaleFactorMap& scale_factors,
  119. const std::vector<std::string>& field_id_to_name_lookup
  120. ){
  121. scale_factors[GetFieldId(field->getName(), field_id_to_name_lookup)]
  122. = FieldDecompiler::ScaleFactor(field->GetRawScript());
  123. }
  124. void FieldDataInstaller::CollectSpawnPoints(
  125. VGears::FLevelFilePtr& field, const std::vector<std::string>& field_id_to_name_lookup,
  126. FieldSpawnPointsMap& spawn_points
  127. ){
  128. const size_t this_field_id = GetFieldId(field->getName(), field_id_to_name_lookup);
  129. // Decompile the script just so the MAPJUMPs can be collected. This is needed to construct the
  130. // full list of entries to each field.
  131. try{
  132. // Get the raw script bytes.
  133. FieldDecompiler::DecompiledScript decompiled;
  134. const std::vector<u8> raw_field_data = field->GetRawScript();
  135. // Decompile to LUA.
  136. FF7FieldScriptGatewayCollector formatter(
  137. field->getName(), field_id_to_name_lookup, spawn_points, this_field_id
  138. );
  139. decompiled = FieldDecompiler::Decompile(
  140. field->getName(), raw_field_data, formatter, "", "EntityContainer = {}\n\n"
  141. );
  142. }
  143. catch (const ::DecompilerException& ex){
  144. std::cerr << "CollectSpawnPoints: DecompilerException: " << ex.what() << std::endl;
  145. }
  146. const VGears::TriggersFilePtr& triggers = field->GetTriggers();
  147. const auto& gateways = triggers->GetGateways();
  148. for (size_t i = 0; i < gateways.size(); i ++){
  149. const VGears::TriggersFile::Gateway& gateway = gateways[i];
  150. if (gateway.destination_field_id != INACTIVE_GATEWAY_ID){
  151. auto it = spawn_points.find(gateway.destination_field_id);
  152. if (it != std::end(spawn_points)){
  153. // Add to the list of gateways that link to
  154. // destination_field_id.
  155. SpawnPointDb::Record rec;
  156. rec.field_id = this_field_id;
  157. rec.gateway = gateway;
  158. rec.gateway_index_or_map_jump_address = i;
  159. it->second.gateways_to_this_field.push_back(rec);
  160. }
  161. else{
  162. // Create a new record for destination_field_id.
  163. SpawnPointDb db;
  164. db.target_field_id = gateway.destination_field_id;
  165. SpawnPointDb::Record rec;
  166. rec.field_id = this_field_id;
  167. rec.gateway = gateway;
  168. rec.gateway_index_or_map_jump_address = i;
  169. db.gateways_to_this_field.push_back(rec);
  170. spawn_points.insert(std::make_pair(db.target_field_id, db));
  171. }
  172. }
  173. }
  174. }
  175. FieldDataInstaller::FieldDataInstaller(const std::string input_dir, const std::string output_dir):
  176. input_dir_(input_dir), output_dir_(output_dir)
  177. {}
  178. FieldDataInstaller::~FieldDataInstaller(){}
  179. int FieldDataInstaller::CollectSpawnAndScaleFactorsInit(Ogre::ResourceGroupManager* res_mgr){
  180. // List what's in the LGP archive.
  181. //flevel_file_list_ = application_.ResMgr()->listResourceNames("FFVIIFields", "*");
  182. flevel_file_list_ = res_mgr->listResourceNames("FFVIIFields", "*");
  183. // Load the map list field.
  184. VGears::MapListFilePtr map_list = VGears::MapListFileManager::GetSingleton().load(
  185. "maplist", "FFVIIFields"
  186. ).staticCast<VGears::MapListFile>();
  187. map_list_ = map_list->GetMapList();
  188. return flevel_file_list_->size();
  189. }
  190. void FieldDataInstaller::CollectSpawnAndScaleFactors(int field_index){
  191. // On the first pass collate required field information.
  192. progress_step_num_elements_ = flevel_file_list_->size();
  193. if (field_index >= flevel_file_list_->size()){
  194. // TODO: Show invalid index.
  195. return;
  196. }
  197. auto resource_name = (*flevel_file_list_)[field_index];
  198. // Exclude things that are not fields.
  199. //if (IsAFieldFile(resource_name) && !WillCrash(resource_name)){
  200. // TODO: DEBUG: Only testing fields.
  201. if (IsAFieldFile(resource_name) && IsTestField(resource_name) && !WillCrash(resource_name)){
  202. VGears::FLevelFilePtr field = VGears::LZSFLevelFileManager::GetSingleton().load(
  203. resource_name, "FFVIIFields"
  204. ).staticCast<VGears::FLevelFile>();
  205. CollectSpawnPoints(field, map_list_, spawn_points_);
  206. CollectFieldScaleFactors(field, scale_factors_, map_list_);
  207. }
  208. }
  209. void FieldDataInstaller::Convert(int field_index){
  210. // Do the full conversion with the collated data.
  211. if (field_index >= flevel_file_list_->size()){
  212. // TODO: Show invalid index.
  213. return;
  214. }
  215. auto resource_name = (*flevel_file_list_)[field_index];
  216. // Exclude things that are not fields.
  217. if (IsAFieldFile(resource_name)){
  218. //if (/*IsTestField(resource_name) &&*/ !WillCrash(resource_name)){
  219. // TODO: DEBUG: Only test fields
  220. if (IsTestField(resource_name) && !WillCrash(resource_name)){
  221. //write_output_line_("Converting field " + resource_name);
  222. std::cout << " - Converting field: " << resource_name << std::endl;
  223. CreateDir(FIELD_MAPS_DIR + "/" + resource_name);
  224. VGears::FLevelFilePtr field = VGears::LZSFLevelFileManager::GetSingleton().load(
  225. resource_name, "FFVIIFields"
  226. ).staticCast<VGears::FLevelFile>();
  227. PcFieldToVGearsField(field);
  228. }
  229. else{
  230. /*write_output_line_(
  231. "[ERROR] Skip field " + resource_name + " due to crash or hang issue."
  232. );*/
  233. std::cerr << "[ERROR] Skip field: " << resource_name
  234. << " due to crash or hang issue." << std::endl;
  235. }
  236. }
  237. }
  238. int FieldDataInstaller::WriteInit(){
  239. doc_ = std::make_unique<TiXmlDocument>();
  240. element_ = std::make_unique<TiXmlElement>("maps");
  241. return converted_map_list_.size();
  242. }
  243. void FieldDataInstaller::Write(int field_index){
  244. // TODO: Probably need to inject "empty" and "test" fields.
  245. if (field_index >= flevel_file_list_->size()){
  246. // TODO: Show invalid index.
  247. return;
  248. }
  249. auto map = converted_map_list_[field_index];
  250. //write_output_line_("Writing field " + FieldName(map));
  251. std::unique_ptr<TiXmlElement> xml_element(new TiXmlElement("map"));
  252. xml_element->SetAttribute("name", map);
  253. xml_element->SetAttribute("file_name", FIELD_MAPS_DIR + "/" + map + "/map.xml");
  254. element_->LinkEndChild(xml_element.release());
  255. }
  256. void FieldDataInstaller::WriteEnd(){
  257. doc_->LinkEndChild(element_.release());
  258. doc_->SaveFile(output_dir_ + "/maps.xml");
  259. }
  260. std::vector<std::string> FieldDataInstaller::ConvertModelsInit(){
  261. std::vector<std::string> models;
  262. // Open char_lgp as a lgp archive
  263. VGears::LGPArchive char_lgp(input_dir_ + "data/field/char.lgp", "LGP");
  264. char_lgp.open(input_dir_ + "data/field/char.lgp", true);
  265. char_lgp.load();
  266. // Also, open it as a file for reading
  267. File char_file(input_dir_ + "data/field/char.lgp");
  268. //Ogre::StringVectorPtr file_list = char_lgp.list(true, true);
  269. field_model_file_list_ = char_lgp.list(true, true);
  270. VGears::LGPArchive::FileList files = char_lgp.GetFiles();
  271. for (int i = 0; i < field_model_file_list_->size(); i ++){
  272. VGears::LGPArchive::FileEntry f = files.at(i);
  273. // Save the TEX File
  274. std::fstream out;
  275. out.open(output_dir_ + "temp/char/" + f.file_name, std::ios::out);
  276. char_file.SetOffset(f.data_offset);
  277. for (int j = 0; j < f.data_size; j ++) out << char_file.readU8();
  278. out.close();
  279. //field_model_file_list_->push_back(f.file_name);
  280. }
  281. for (auto it = used_models_and_anims_.map.begin(); it != used_models_and_anims_.map.end(); it ++){
  282. models.push_back(it->first);
  283. }
  284. return models;
  285. }
  286. void FieldDataInstaller::ConvertModels(std::string model_name){
  287. auto model = used_models_and_anims_.map.find(model_name);
  288. try{
  289. Ogre::ResourcePtr hrc = VGears::HRCFileManager::GetSingleton().load(model->first, "FFVII");
  290. //Ogre::ResourcePtr hrc = VGears::HRCFile();
  291. Ogre::String base_name;
  292. VGears::StringUtil::splitBase(model->first, base_name);
  293. auto mesh_name = VGears::NameLookup::model(base_name) + ".mesh";
  294. Ogre::MeshPtr mesh(Ogre::MeshManager::getSingleton().load(mesh_name, "FFVII"));
  295. Ogre::SkeletonPtr skeleton(mesh->getSkeleton());
  296. for (auto& anim : model->second){
  297. VGears::AFileManager &afl_mgr(VGears::AFileManager::GetSingleton());
  298. VGears::AFilePtr a = afl_mgr.load(anim, "FFVII").staticCast<VGears::AFile>();
  299. // Convert the FF7 name to a more readable name set in the meta data.
  300. VGears::StringUtil::splitBase(anim, base_name);
  301. a->AddTo(skeleton, VGears::NameLookup::Animation(base_name));
  302. }
  303. ExportMesh(output_dir_ + FIELD_MODELS_DIR + "/", mesh);
  304. }
  305. catch (const Ogre::Exception& ex){
  306. write_output_line_(
  307. "[ERROR] Ogre exception converting model "
  308. + model->first + ": " + ex.what()
  309. );
  310. std::cerr << "[ERROR] Ogre exception converting model "
  311. << model->first <<": " << ex.what() << std::endl;
  312. }
  313. catch (const std::exception& ex){
  314. write_output_line_(
  315. "[ERROR] Exception converting model " + model->first + ": " + ex.what()
  316. );
  317. std::cerr << "[ERROR] Exception converting model "
  318. << model->first << ": " << ex.what() << std::endl;
  319. }
  320. }
  321. void FieldDataInstaller::ExportMesh(const std::string outdir, const Ogre::MeshPtr &mesh){
  322. // TODO: Share function with pc model exporter
  323. VGears::String base_mesh_name;
  324. VGears::StringUtil::splitFull(mesh->getName(), base_mesh_name);
  325. Ogre::MeshSerializer mesh_serializer;
  326. Ogre::SkeletonPtr skeleton(mesh->getSkeleton());
  327. Ogre::SkeletonSerializer skeleton_serializer;
  328. skeleton_serializer.exportSkeleton(
  329. skeleton.getPointer(), outdir + base_mesh_name + ".skeleton"
  330. );
  331. mesh->setSkeletonName(FIELD_MODELS_DIR + "/" + base_mesh_name + ".skeleton");
  332. mesh_serializer.exportMesh(mesh.getPointer(), outdir + mesh->getName());
  333. Ogre::Mesh::SubMeshIterator it(mesh->getSubMeshIterator());
  334. Ogre::MaterialSerializer mat_ser;
  335. size_t i(0);
  336. std::set<std::string> textures;
  337. while (it.hasMoreElements()){
  338. Ogre::SubMesh *sub_mesh(it.getNext());
  339. Ogre::MaterialPtr mat(Ogre::MaterialManager::getSingleton().getByName(
  340. sub_mesh->getMaterialName())
  341. );
  342. if (mat != nullptr){
  343. for (size_t techs = 0; techs < mat->getNumTechniques(); techs ++){
  344. Ogre::Technique* tech = mat->getTechnique(techs);
  345. if (tech){
  346. for (size_t pass_num = 0; pass_num < tech->getNumPasses(); pass_num ++){
  347. Ogre::Pass* pass = tech->getPass(pass_num);
  348. if (pass){
  349. for (
  350. size_t texture_unit_num = 0;
  351. texture_unit_num < pass->getNumTextureUnitStates();
  352. texture_unit_num ++
  353. ){
  354. Ogre::TextureUnitState* unit
  355. = pass->getTextureUnitState(texture_unit_num);
  356. if (unit && unit->getTextureName().empty() == false){
  357. // Convert the texture from .tex to .png.
  358. TexFile tex(
  359. output_dir_ + "temp/char/" + unit->getTextureName()
  360. );
  361. // Ensure the output material script references png files
  362. // rather than tex files.
  363. Ogre::String base_name;
  364. VGears::StringUtil::splitBase(
  365. unit->getTextureName(), base_name
  366. );
  367. unit->setTextureName(
  368. FIELD_MODELS_DIR + "/" + base_mesh_name
  369. + "_" + base_name + ".png"
  370. );
  371. tex.SavePng(
  372. output_dir_ + FIELD_MODELS_DIR + "/" + base_name + ".png", 0
  373. );
  374. // Copy subtexture (xxxx.png) to model_xxxx.png
  375. // TODO: obtain the "data" folder
  376. // programatically.
  377. boost::filesystem::copy_file(
  378. output_dir_ + FIELD_MODELS_DIR + "/" + base_name + ".png",
  379. output_dir_ + FIELD_MODELS_DIR + "/"
  380. + base_mesh_name + "_" + base_name + ".png",
  381. boost::filesystem::copy_option::overwrite_if_exists
  382. );
  383. textures.insert(unit->getTextureName());
  384. }
  385. }
  386. }
  387. }
  388. }
  389. }
  390. if (std::count(materials_.begin(), materials_.end(), sub_mesh->getMaterialName()) == 0){
  391. //std::cout << "[MATERIAL] Writting material "
  392. // << sub_mesh->getMaterialName() << std::endl;
  393. mat_ser.queueForExport(mat);
  394. materials_.push_back(sub_mesh->getMaterialName());
  395. }
  396. }
  397. ++ i;
  398. }
  399. mat_ser.exportQueued(outdir + base_mesh_name + VGears::EXT_MATERIAL);
  400. for (auto& texture_name : textures){
  401. std::string tex_name = texture_name.c_str();
  402. try{
  403. Ogre::TexturePtr texture_ptr
  404. = Ogre::TextureManager::getSingleton().load(tex_name, "FFVIITextures" /*"FFVII"*/);
  405. Ogre::Image image;
  406. texture_ptr->convertToImage(image);
  407. Ogre::String base_name;
  408. VGears::StringUtil::splitBase(texture_name, base_name);
  409. image.save(outdir + base_mesh_name + "_" + base_name + ".png");
  410. }
  411. catch (std::exception const& ex){
  412. std::cerr << "[ERROR] Exception: " << ex.what() << std::endl;
  413. }
  414. }
  415. }
  416. float FieldDataInstaller::GetFieldScaleFactor(size_t field_id){
  417. auto it = scale_factors_.find(field_id);
  418. if (it == std::end(scale_factors_))
  419. throw std::runtime_error("Scale factor not found for field id");
  420. return it->second;
  421. }
  422. std::vector<int> FieldDataInstaller::ExtractMusicTrackIds(VGears::FLevelFilePtr& field){
  423. // Search for the "AKAO" string in the raw field data.
  424. // The next byte is a track id.
  425. const std::vector<u8> raw = field->GetRawScript();
  426. std::vector<int> tracks;
  427. for (int i = 0; i < raw.size() - 5; i ++){
  428. if (raw[i] == 0x41){ // A
  429. if (raw[i + 1] == 0x4B){ // K
  430. if (raw[i + 2] == 0x41){ // A
  431. if (raw[i + 3] == 0x4f){ // O
  432. tracks.push_back(static_cast<int>(raw[i + 4]) - 2);
  433. }
  434. }
  435. }
  436. }
  437. }
  438. return tracks;
  439. }
  440. void FieldDataInstaller::PcFieldToVGearsField(VGears::FLevelFilePtr& field){
  441. // Generate triggers script to insert into main
  442. // decompiled FF7 field -> LUA script.
  443. std::string gateway_script_data;
  444. const VGears::TriggersFilePtr& triggers = field->GetTriggers();
  445. const auto& gateways = triggers->GetGateways();
  446. for (size_t i = 0; i < gateways.size(); i ++){
  447. const VGears::TriggersFile::Gateway& gateway = gateways[i];
  448. if (gateway.destination_field_id != INACTIVE_GATEWAY_ID){
  449. const std::string gateway_entity_name = "Gateway" + std::to_string(i);
  450. gateway_script_data += CreateGateWayScript(
  451. gateway_entity_name,
  452. map_list_.at(gateway.destination_field_id),
  453. "Spawn_" + field->getName() + "_" + std::to_string(i)
  454. );
  455. }
  456. }
  457. const VGears::ModelListFilePtr& models = field->GetModelList();
  458. FieldDecompiler::DecompiledScript decompiled;
  459. FF7FieldScriptFormatter formatter(field->getName(), models, map_list_, spawn_points_);
  460. try{
  461. // Get the raw script bytes.
  462. const std::vector<u8> raw_field_data = field->GetRawScript();
  463. // Decompile to LUA.
  464. decompiled = FieldDecompiler::Decompile(
  465. field->getName(), raw_field_data, formatter, gateway_script_data,
  466. "EntityContainer = {}\n\n"
  467. );
  468. std::ofstream script_file(
  469. output_dir_ + "/" + FIELD_MAPS_DIR + "/" + field->getName() + "/script.lua"
  470. );
  471. if (script_file.is_open()){
  472. script_file << decompiled.luaScript;
  473. field_text_writer_.Begin(
  474. output_dir_ + "/" + FIELD_MAPS_DIR + "/" + field->getName() + "/text.xml"
  475. );
  476. try{field_text_writer_.Write(raw_field_data, field->getName());}
  477. catch (const std::out_of_range& ex){
  478. write_output_line_(
  479. "[ERROR] Failed to read texts from field " + field->getName() + ": " + ex.what()
  480. );
  481. std::cerr << "[ERROR] Failed to read texts from field "
  482. << field->getName() << ": " << ex.what() << std::endl;
  483. }
  484. field_text_writer_.End();
  485. }
  486. else{
  487. write_output_line_(
  488. "[ERROR] Failed to open script file from field " + field->getName() + " for writing."
  489. );
  490. std::cerr << "[ERROR] Failed to open script file from field "
  491. << field->getName() << "for writing." << std::endl;
  492. }
  493. }
  494. catch (const ::DecompilerException& ex){
  495. write_output_line_(
  496. "[ERROR] Internal decompiler error in field " + field->getName() + ": " + ex.what()
  497. );
  498. std::cerr << "[ERROR] Internal decompiler error in field "
  499. << field->getName() << ": " << ex.what() << std::endl;
  500. }
  501. // Write out the map file which links all the other files together for a given field.
  502. {
  503. TiXmlDocument doc;
  504. std::unique_ptr<TiXmlElement> element(new TiXmlElement("map"));
  505. // Script.
  506. std::unique_ptr<TiXmlElement> xml_script_element(new TiXmlElement("script"));
  507. xml_script_element->SetAttribute(
  508. "file_name", FIELD_MAPS_DIR + "/" + field->getName() + "/script.lua"
  509. );
  510. element->LinkEndChild(xml_script_element.release());
  511. // Background.
  512. std::unique_ptr<TiXmlElement> xml_background_2d(new TiXmlElement("background2d"));
  513. xml_background_2d->SetAttribute(
  514. "file_name", FIELD_MAPS_DIR + "/" + field->getName() + "/bg.xml"
  515. );
  516. element->LinkEndChild(xml_background_2d.release());
  517. // Texts.
  518. std::unique_ptr<TiXmlElement> xml_texts(new TiXmlElement("texts"));
  519. xml_texts->SetAttribute(
  520. "file_name", FIELD_MAPS_DIR + "/" + field->getName() + "/text.xml"
  521. );
  522. element->LinkEndChild(xml_texts.release());
  523. // Walkmesh.
  524. std::unique_ptr<TiXmlElement> xml_walkmesh(new TiXmlElement("walkmesh"));
  525. xml_walkmesh->SetAttribute(
  526. "file_name", FIELD_MAPS_DIR + "/" + field->getName() + "/wm.xml"
  527. );
  528. element->LinkEndChild(xml_walkmesh.release());
  529. // Forward direction (angle player moves when "up" is pressed)
  530. std::unique_ptr<TiXmlElement> xml_forward_direction(new TiXmlElement("movement_rotation"));
  531. xml_forward_direction->SetAttribute(
  532. "degree", std::to_string(triggers->MovementRotation())
  533. );
  534. element->LinkEndChild(xml_forward_direction.release());
  535. // Get this fields Id.
  536. const size_t this_field_id = GetFieldId(field->getName(), map_list_);
  537. // Use the ID to find the pre-computed list of gateways in all other fields that link to
  538. // this field.
  539. auto spawn_iterator = spawn_points_.find(this_field_id);
  540. Ogre::Vector3 first_entity_point = Ogre::Vector3::ZERO;
  541. // If none found it probably just means no other fields have doors to this one.
  542. if (spawn_iterator != std::end(spawn_points_)){
  543. const std::vector<SpawnPointDb::Record>& spawn_point_records
  544. = spawn_iterator->second.gateways_to_this_field;
  545. for (size_t i = 0; i < spawn_point_records.size(); i ++){
  546. const VGears::TriggersFile::Gateway& gateway = spawn_point_records[i].gateway;
  547. // Entity_point:
  548. std::unique_ptr<TiXmlElement> xml_entity_point(new TiXmlElement("entity_point"));
  549. if (spawn_point_records[i].from_script){
  550. // Spawn points from map jumps have a another algorithm.
  551. xml_entity_point->SetAttribute(
  552. "name",
  553. map_list_.at(
  554. spawn_point_records[i].field_id) + "_" + spawn_point_records[i].entity_name
  555. + "_" + spawn_point_records[i].script_function_name + "_addr_"
  556. + std::to_string(spawn_point_records[i].gateway_index_or_map_jump_address)
  557. );
  558. }
  559. else{
  560. // Must also include the gateway index for the case where 2
  561. // fields have more than one door linking to each other.
  562. xml_entity_point->SetAttribute(
  563. "name",
  564. "Spawn_" + map_list_.at(spawn_point_records[i].field_id) + "_"
  565. + std::to_string(spawn_point_records[i].gateway_index_or_map_jump_address)
  566. );
  567. }
  568. const float downscaler_next = 128.0f * GetFieldScaleFactor(
  569. gateway.destination_field_id
  570. );
  571. // Position Z is actually the target walkmesh triangle ID, so this is tiny bit more
  572. // complex. Now "get the Z value of the triangle with that ID". Note that ID
  573. // actually just means index.
  574. unsigned int triangle_index = static_cast<unsigned int>(gateway.destination.z);
  575. if (
  576. triangle_index >= field->GetWalkmesh()->GetTriangles().size()
  577. ){
  578. write_output_line_(
  579. "[WARNING] In field " + field->getName() + ": Map jump triangle ("
  580. + std::to_string(triangle_index) + ") out of bounds ("
  581. + std::to_string(field->GetWalkmesh()->GetTriangles().size()) + ")"
  582. );
  583. std::cerr << "[WARNING] In field " << field->getName()
  584. << ": Map jump triangle (" << triangle_index << ") out of bounds ("
  585. << field->GetWalkmesh()->GetTriangles().size() << ")" << std::endl;
  586. triangle_index = 0;
  587. }
  588. const float z_of_triangle_with_id
  589. = field->GetWalkmesh()->GetTriangles().at(triangle_index).a.z;
  590. const Ogre::Vector3 position(
  591. gateway.destination.x / downscaler_next, gateway.destination.y / downscaler_next,
  592. z_of_triangle_with_id
  593. );
  594. if (position != Ogre::Vector3::ZERO && first_entity_point == Ogre::Vector3::ZERO)
  595. first_entity_point = position;
  596. xml_entity_point->SetAttribute(
  597. "position", Ogre::StringConverter::toString(position)
  598. );
  599. const float rotation = (360.0f * static_cast<float>(gateway.dir)) / 255.0f;
  600. xml_entity_point->SetAttribute("rotation", std::to_string(rotation));
  601. element->LinkEndChild(xml_entity_point.release());
  602. }
  603. }
  604. // Get lines. Add them to a list, so they aren't processed later as regular entities.
  605. std::vector<std::string> line_entities;
  606. for (FieldDecompiler::Line line : decompiled.lines){
  607. std::unique_ptr<TiXmlElement> xml_entity_trigger(new TiXmlElement("entity_trigger"));
  608. line_entities.push_back(line.name);
  609. xml_entity_trigger->SetAttribute("name", line.name);
  610. xml_entity_trigger->SetAttribute(
  611. "point1",
  612. std::to_string(line.point_a[0] * LINE_SCALE_FACTOR)
  613. + " " + std::to_string(line.point_a[1] * LINE_SCALE_FACTOR)
  614. + " " + std::to_string(line.point_a[2] * LINE_SCALE_FACTOR)
  615. );
  616. xml_entity_trigger->SetAttribute(
  617. "point2",
  618. std::to_string(line.point_b[0] * LINE_SCALE_FACTOR)
  619. + " " + std::to_string(line.point_b[1] * LINE_SCALE_FACTOR)
  620. + " " + std::to_string(line.point_b[2] * LINE_SCALE_FACTOR)
  621. );
  622. xml_entity_trigger->SetAttribute("enabled", "true");
  623. element->LinkEndChild(xml_entity_trigger.release());
  624. }
  625. // Get entities.
  626. for (FieldDecompiler::FieldEntity entity : decompiled.entities){
  627. // If the entity has been added as a line, skip.
  628. if (line_entities.size() > 0){
  629. if (
  630. *std::find(line_entities.begin(), line_entities.end(), entity.name) == entity.name
  631. ){
  632. continue;
  633. }
  634. }
  635. const int char_id = entity.char_id;
  636. if (char_id != -1){
  637. const VGears::ModelListFile::ModelDescription& desc = models->GetModels().at(char_id);
  638. auto& animations = used_models_and_anims_.ModelAnimations(desc.hrc_name);
  639. for (const auto& anim : desc.animations)
  640. animations.insert(used_models_and_anims_.NormalizeAnimationName(anim.name));
  641. std::unique_ptr<TiXmlElement> xml_entity_script(new TiXmlElement("entity_model"));
  642. xml_entity_script->SetAttribute("name", entity.name);
  643. // TODO: Add to list of HRC's to convert, obtain name of converted .mesh file.
  644. auto lower_case_hrc_name = desc.hrc_name;
  645. VGears::StringUtil::toLowerCase(lower_case_hrc_name);
  646. xml_entity_script->SetAttribute(
  647. "file_name",
  648. FIELD_MODELS_DIR + "/" + used_models_and_anims_.ModelMetaDataName(lower_case_hrc_name)
  649. );
  650. xml_entity_script->SetAttribute("index", entity.index);
  651. if (desc.type == VGears::ModelListFile::PLAYER){
  652. // For player models the position is set manually in the xml because if a map
  653. // is loaded manually this is where the player will end up. Hence we set the
  654. // position to the first entity_point that we have.
  655. xml_entity_script->SetAttribute(
  656. "position", Ogre::StringConverter::toString(first_entity_point)
  657. );
  658. }
  659. else{
  660. xml_entity_script->SetAttribute(
  661. "position", Ogre::StringConverter::toString(Ogre::Vector3::ZERO)
  662. );
  663. }
  664. xml_entity_script->SetAttribute("direction", "0");
  665. // TODO: This isn't quite right, the models animation
  666. // translation seems to be inverted?
  667. xml_entity_script->SetAttribute("scale", "0.03125 0.03125 0.03125");
  668. xml_entity_script->SetAttribute(
  669. "root_orientation", "0.7071067811865476 0.7071067811865476 0 0"
  670. );
  671. element->LinkEndChild(xml_entity_script.release());
  672. }
  673. else{
  674. std::unique_ptr<TiXmlElement> xml_entity_script(new TiXmlElement("entity_script"));
  675. xml_entity_script->SetAttribute("name", entity.name);//it.first);
  676. element->LinkEndChild(xml_entity_script.release());
  677. }
  678. }
  679. const float downscaler_this = 128.0f * GetFieldScaleFactor(this_field_id);
  680. for (size_t i = 0; i < gateways.size(); i ++){
  681. const VGears::TriggersFile::Gateway& gateway = gateways[i];
  682. // If non-inactive gateway:
  683. if (gateway.destination_field_id != INACTIVE_GATEWAY_ID){
  684. std::unique_ptr<TiXmlElement> xml_entity_trigger(
  685. new TiXmlElement("entity_trigger")
  686. );
  687. xml_entity_trigger->SetAttribute("name", "Gateway" + std::to_string(i));
  688. xml_entity_trigger->SetAttribute(
  689. "point1",
  690. Ogre::StringConverter::toString(
  691. Ogre::Vector3(
  692. gateway.exit_line[0].x,
  693. gateway.exit_line[0].y,
  694. gateway.exit_line[0].z) / downscaler_this
  695. )
  696. );
  697. xml_entity_trigger->SetAttribute(
  698. "point2",
  699. Ogre::StringConverter::toString(
  700. Ogre::Vector3(
  701. gateway.exit_line[1].x,
  702. gateway.exit_line[1].y,
  703. gateway.exit_line[1].z) / downscaler_this
  704. )
  705. );
  706. // Enabled is hard coded to true.
  707. xml_entity_trigger->SetAttribute("enabled", "true");
  708. element->LinkEndChild(xml_entity_trigger.release());
  709. }
  710. }
  711. // Get music tracks
  712. std::vector<int> tracks = ExtractMusicTrackIds(field);
  713. std::unique_ptr<TiXmlElement> xml_tracks(new TiXmlElement("tracks"));
  714. for (int t = 0; t < tracks.size(); t ++){
  715. std::unique_ptr<TiXmlElement> xml_track(new TiXmlElement("track"));
  716. xml_track->SetAttribute("id", t);
  717. xml_track->SetAttribute("track_id", tracks[t]);
  718. xml_tracks->LinkEndChild(xml_track.release());
  719. }
  720. element->LinkEndChild(xml_tracks.release());
  721. doc.LinkEndChild(element.release());
  722. doc.SaveFile(output_dir_ + "/" + FIELD_MAPS_DIR + "/" + field->getName() + "/map.xml");
  723. const VGears::PaletteFilePtr& pal = field->GetPalette();
  724. const VGears::BackgroundFilePtr& bg = field->GetBackground();
  725. std::unique_ptr<Ogre::Image> bg_image(bg->CreateImage(pal));
  726. bg_image->encode("png");
  727. bg_image->save(output_dir_ + "/" + FIELD_MAPS_DIR + "/" + field->getName() + "/tiles.png");
  728. {
  729. TiXmlDocument bg_doc;
  730. std::unique_ptr<TiXmlElement> bg_element(new TiXmlElement("background2d"));
  731. // Magic constants.
  732. const int BG_SCALE_UP_FACTOR = 3;
  733. const int BG_PSX_SCREEN_WIDTH = 320;
  734. const int BG_PSX_SCREEN_HEIGHT = 240;
  735. // Get texture atlas size.
  736. const int width = bg_image->getWidth();
  737. const int height = bg_image->getHeight();
  738. const VGears::CameraMatrixFilePtr& camera_matrix = field->GetCameraMatrix();
  739. const Ogre::Vector3 position
  740. = camera_matrix->GetPosition() / GetFieldScaleFactor(this_field_id);
  741. const Ogre::Quaternion orientation = camera_matrix->GetOrientation();
  742. const Ogre::Degree fov
  743. = camera_matrix->GetFov(static_cast<float>(BG_PSX_SCREEN_HEIGHT));
  744. const int min_x = triggers->GetCameraRange().left * BG_SCALE_UP_FACTOR;
  745. const int min_y = triggers->GetCameraRange().top * BG_SCALE_UP_FACTOR;
  746. const int max_x = triggers->GetCameraRange().right * BG_SCALE_UP_FACTOR;
  747. const int max_y = triggers->GetCameraRange().bottom * BG_SCALE_UP_FACTOR;
  748. bg_element->SetAttribute("image", FIELD_MAPS_DIR + "/" + field->getName() + "/tiles.png");
  749. bg_element->SetAttribute("position", Ogre::StringConverter::toString(position));
  750. bg_element->SetAttribute("orientation", Ogre::StringConverter::toString(orientation));
  751. bg_element->SetAttribute("fov", Ogre::StringConverter::toString(fov));
  752. bg_element->SetAttribute(
  753. "range",
  754. std::to_string(min_x) + " " + std::to_string(min_y) + " "
  755. + std::to_string(max_x) + " " + std::to_string(max_y)
  756. );
  757. bg_element->SetAttribute(
  758. "clip",
  759. std::to_string(BG_PSX_SCREEN_WIDTH * BG_SCALE_UP_FACTOR) + " "
  760. + std::to_string(BG_PSX_SCREEN_HEIGHT * BG_SCALE_UP_FACTOR)
  761. );
  762. // Write out the *_BG.XML data.
  763. auto& layers = bg->GetLayers();
  764. for (const auto& layer : layers){
  765. // TODO: Should only the tiles to construct enabled layers be outputted?
  766. // if (layer.enabled){
  767. for (const VGears::BackgroundFile::SpriteData& sprite : layer.sprites){
  768. std::unique_ptr<TiXmlElement> xml_element(new TiXmlElement("tile"));
  769. xml_element->SetAttribute(
  770. "destination",
  771. Ogre::StringConverter::toString(sprite.dst.x * BG_SCALE_UP_FACTOR) + " "
  772. + Ogre::StringConverter::toString(sprite.dst.y * BG_SCALE_UP_FACTOR)
  773. );
  774. xml_element->SetAttribute(
  775. "width", Ogre::StringConverter::toString(sprite.width * BG_SCALE_UP_FACTOR)
  776. );
  777. xml_element->SetAttribute(
  778. "height", Ogre::StringConverter::toString(sprite.height * BG_SCALE_UP_FACTOR)
  779. );
  780. // Each tile is added to a big texture atlas with hard coded size of 1024x1024,
  781. // convert UV's to the 0.0f to 1.0f range.
  782. const float u0 = static_cast<float>(sprite.src.x) / width;
  783. const float v0 = static_cast<float>(sprite.src.y) / height;
  784. const float u1 = static_cast<float>(sprite.src.x + sprite.width) / width;
  785. const float v1 = static_cast<float>(sprite.src.y + sprite.height) / height;
  786. xml_element->SetAttribute(
  787. "uv",
  788. Ogre::StringConverter::toString(u0) + " "
  789. + Ogre::StringConverter::toString(v0) + " "
  790. + Ogre::StringConverter::toString(u1) + " "
  791. + Ogre::StringConverter::toString(v1)
  792. );
  793. // TODO: It works (on FFVII PC), but why this number?
  794. xml_element->SetAttribute(
  795. "depth",
  796. Ogre::StringConverter::toString(static_cast<float>(sprite.depth) * (0.03125f))
  797. );
  798. // TODO: Copied from DatFile::AddTile.
  799. // Add to common method.
  800. Ogre::String blending_str = "";
  801. if (sprite.blending == 0) blending_str = "alpha";
  802. // 3 is source + 0.25 * destination:
  803. else if (sprite.blending == 1 || sprite.blending == 3) blending_str = "add";
  804. else if (sprite.blending == 2) blending_str = "subtract";
  805. // TODO: Should probably throw to fail conversion:
  806. else blending_str = "unknown";
  807. xml_element->SetAttribute("blending", blending_str);
  808. bg_element->LinkEndChild(xml_element.release());
  809. }
  810. //}
  811. }
  812. bg_doc.LinkEndChild(bg_element.release());
  813. bg_doc.SaveFile(output_dir_ + "/" + FIELD_MAPS_DIR + "/" + field->getName() + "/bg.xml");
  814. }
  815. }
  816. {
  817. // Save out the walk mesh as XML.
  818. const VGears::WalkmeshFilePtr& walkmesh = field->GetWalkmesh();
  819. TiXmlDocument doc;
  820. std::unique_ptr<TiXmlElement> wmesh_element(new TiXmlElement("walkmesh"));
  821. for (VGears::WalkmeshFile::Triangle& tri : walkmesh->GetTriangles()){
  822. std::unique_ptr<TiXmlElement> xml_element(new TiXmlElement("triangle"));
  823. xml_element->SetAttribute("a", Ogre::StringConverter::toString(tri.a));
  824. xml_element->SetAttribute("b", Ogre::StringConverter::toString(tri.b));
  825. xml_element->SetAttribute("c", Ogre::StringConverter::toString(tri.c));
  826. xml_element->SetAttribute("a_b", std::to_string(tri.access_side[0]));
  827. xml_element->SetAttribute("b_c", std::to_string(tri.access_side[1]));
  828. xml_element->SetAttribute("c_a", std::to_string(tri.access_side[2]));
  829. wmesh_element->LinkEndChild(xml_element.release());
  830. }
  831. doc.LinkEndChild(wmesh_element.release());
  832. doc.SaveFile(output_dir_ + "/" + FIELD_MAPS_DIR + "/" + field->getName() + "/wm.xml");
  833. }
  834. converted_map_list_.push_back(field->getName());
  835. }