FieldDataInstaller.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 <algorithm>
  16. #include <fstream>
  17. #include <iostream>
  18. #include <boost/filesystem.hpp>
  19. #include <QtCore/QDir>
  20. #include "common/StringUtil.h"
  21. #include "formats/field/LZSFLevelFileManager.h"
  22. #include "formats/field/MapListFile.h"
  23. #include "FieldDataInstaller.h"
  24. #include "FieldScriptFormatters.h"
  25. FieldDataInstaller::FieldDataInstaller(
  26. const DiskImage& disk_image, const std::string output_dir,
  27. std::function<void(std::string)> log_output_line
  28. ):
  29. output_dir_(output_dir), data_dir_(output_dir + "game/"), write_output_line_(log_output_line),
  30. script_installer_(data_dir_, map_list_, spawn_points_, log_output_line),
  31. map_writer_(
  32. data_dir_, map_list_, spawn_points_, scale_factors_, used_models_and_anims_, log_output_line
  33. ),
  34. background_installer_(data_dir_),
  35. model_installer_(disk_image, output_dir_, data_dir_, used_models_and_anims_, log_output_line)
  36. {}
  37. FieldDataInstaller::~FieldDataInstaller(){}
  38. void FieldDataInstaller::LoadInstalledFields(){
  39. converted_map_list_.clear();
  40. for (const std::string& name : map_list_){
  41. if (boost::filesystem::exists(data_dir_ + FieldData::MAPS_DIR + "/" + name + "/map.xml")){
  42. converted_map_list_.push_back(name);
  43. }
  44. }
  45. }
  46. int FieldDataInstaller::CollectSpawnAndScaleFactorsInit(Ogre::ResourceGroupManager* res_mgr){
  47. // List what's in the LGP archive.
  48. flevel_file_list_ = res_mgr->listResourceNames("FFVIIFields", "*");
  49. // Load the map list field.
  50. VGears::MapListFilePtr map_list = VGears::MapListFileManager::GetSingleton().load(
  51. "maplist", "FFVIIFields"
  52. ).staticCast<VGears::MapListFile>();
  53. map_list_ = map_list->GetMapList();
  54. return flevel_file_list_->size();
  55. }
  56. void FieldDataInstaller::CollectSpawnAndScaleFactors(int field_index){
  57. // TODO: Show invalid index.
  58. if (field_index >= flevel_file_list_->size()) return;
  59. const auto resource_name = (*flevel_file_list_)[field_index];
  60. // Exclude things that are not fields.
  61. if (!IsAFieldFile(resource_name)) return;
  62. try{
  63. VGears::FLevelFilePtr field = VGears::LZSFLevelFileManager::GetSingleton()
  64. .load(resource_name, "FFVIIFields").staticCast<VGears::FLevelFile>();
  65. if (!CollectSpawnPoints(field)){
  66. failed_fields_.push_back(resource_name);
  67. return;
  68. }
  69. scale_factors_[FieldData::FieldId(field->getName(), map_list_)]
  70. = FieldDecompiler::ScaleFactor(field->GetRawScript());
  71. }
  72. catch (const std::exception& exception){
  73. failed_fields_.push_back(resource_name);
  74. std::cerr << "[WARNING] CollectSpawnAndScaleFactors: skipped " << resource_name
  75. << " after error: " << exception.what() << std::endl;
  76. }
  77. }
  78. void FieldDataInstaller::WriteWorldArrivals(){
  79. // The exits to the world map ("wm<N>") have no field to write the spawn points into, so the
  80. // coordinates that each exit gives (world units) are written for the world map scripts, by
  81. // spawn point name.
  82. std::string output = "-- Generated by the installer: where each field exit puts the player in the world map.\n"
  83. "WorldArrivals = {\n";
  84. for (const auto& entry : spawn_points_){
  85. if (entry.first >= map_list_.size()) continue;
  86. const std::string& target = map_list_[entry.first];
  87. if (target.size() < 3 || target.compare(0, 2, "wm") != 0
  88. || target.find_first_not_of("0123456789", 2) != std::string::npos) continue;
  89. for (const SpawnPointDb::Record& record : entry.second.gateways_to_this_field){
  90. std::string name;
  91. if (record.from_script){
  92. name = map_list_.at(record.field_id) + "_" + record.entity_name + "_" + record.script_function_name
  93. + "_addr_" + std::to_string(record.gateway_index_or_map_jump_address);
  94. }
  95. else{
  96. name = "Spawn_" + map_list_.at(record.field_id) + "_"
  97. + std::to_string(record.gateway_index_or_map_jump_address);
  98. }
  99. output += " [\"" + name + "\"] = {x = " + std::to_string(record.gateway.destination.x)
  100. + ", y = " + std::to_string(record.gateway.destination.y)
  101. + ", dir = " + std::to_string(record.gateway.dir) + "},\n";
  102. }
  103. }
  104. output += "}\n";
  105. boost::filesystem::create_directories(data_dir_ + "world/scripts");
  106. std::ofstream file(data_dir_ + "world/scripts/world_arrivals.lua");
  107. file << output;
  108. }
  109. void FieldDataInstaller::Convert(int field_index){
  110. if (field_index == 0) WriteWorldArrivals();
  111. // TODO: Show invalid index.
  112. if (field_index >= flevel_file_list_->size()) return;
  113. const auto resource_name = (*flevel_file_list_)[field_index];
  114. // Exclude things that are not fields.
  115. if (!IsAFieldFile(resource_name)) return;
  116. if (std::find(failed_fields_.begin(), failed_fields_.end(), resource_name) != failed_fields_.end()){
  117. std::cerr << "[ERROR] Skip field: " << resource_name
  118. << " because its scripts or scale factor could not be collected." << std::endl;
  119. return;
  120. }
  121. const QString dir = QString::fromStdString(data_dir_ + FieldData::MAPS_DIR + "/" + resource_name);
  122. if (!QDir(dir).mkpath(".")) throw std::runtime_error("Failed to mkpath");
  123. VGears::FLevelFilePtr field = VGears::LZSFLevelFileManager::GetSingleton()
  124. .load(resource_name, "FFVIIFields").staticCast<VGears::FLevelFile>();
  125. PcFieldToVGearsField(field);
  126. }
  127. int FieldDataInstaller::WriteInit(){
  128. doc_ = std::make_unique<TiXmlDocument>();
  129. element_ = std::make_unique<TiXmlElement>("maps");
  130. return converted_map_list_.size();
  131. }
  132. void FieldDataInstaller::Write(int field_index){
  133. // TODO: Probably need to inject "empty" and "test" fields.
  134. // TODO: Show invalid index.
  135. if (field_index >= converted_map_list_.size()) return;
  136. const auto map = converted_map_list_[field_index];
  137. std::unique_ptr<TiXmlElement> xml_element(new TiXmlElement("map"));
  138. xml_element->SetAttribute("name", map);
  139. xml_element->SetAttribute("file_name", map + "/map.xml");
  140. element_->LinkEndChild(xml_element.release());
  141. }
  142. void FieldDataInstaller::WriteEnd(){
  143. doc_->LinkEndChild(element_.release());
  144. doc_->SaveFile(data_dir_ + "/field/maps/_fields.xml");
  145. }
  146. std::vector<std::string> FieldDataInstaller::ConvertModelsInit(){return model_installer_.Initialize();}
  147. void FieldDataInstaller::ConvertModels(std::string model_name){model_installer_.ConvertModel(model_name);}
  148. bool FieldDataInstaller::IsAFieldFile(const Ogre::String& resource_name){
  149. return (
  150. !VGears::StringUtil::endsWith(resource_name, ".tex")
  151. && !VGears::StringUtil::endsWith(resource_name, ".tut")
  152. && !VGears::StringUtil::endsWith(resource_name, ".siz")
  153. && resource_name != "maplist"
  154. );
  155. }
  156. bool FieldDataInstaller::CollectSpawnPoints(const VGears::FLevelFilePtr& field){
  157. const size_t field_id = FieldData::FieldId(field->getName(), map_list_);
  158. // Decompile the script just so the MAPJUMPs can be collected. This is needed to construct the
  159. // full list of entries to each field.
  160. try{
  161. FF7FieldScriptGatewayCollector formatter(field->getName(), map_list_, spawn_points_, field_id);
  162. FieldDecompiler::Decompile(
  163. field->getName(), field->GetRawScript(), formatter, "", "EntityContainer = {}\n\n"
  164. );
  165. }
  166. catch (const ::DecompilerException& ex){
  167. std::cerr << "[WARNING] CollectSpawnPoints: decompiler failed for " << field->getName()
  168. << ": " << ex.what() << std::endl;
  169. return false;
  170. }
  171. const VGears::TriggersFilePtr& triggers = field->GetTriggers();
  172. if (triggers.isNull()){
  173. std::cerr << "CollectSpawnPoints: missing trigger data for " << field->getName() << std::endl;
  174. return false;
  175. }
  176. const auto& gateways = triggers->GetGateways();
  177. for (size_t i = 0; i < gateways.size(); i ++){
  178. if (gateways[i].destination_field_id == FieldData::INACTIVE_GATEWAY_ID) continue;
  179. SpawnPointDb::Record record;
  180. record.field_id = static_cast<u16>(field_id);
  181. record.gateway = gateways[i];
  182. record.gateway_index_or_map_jump_address = static_cast<u32>(i);
  183. FieldData::AddSpawnPoint(spawn_points_, record);
  184. }
  185. return true;
  186. }
  187. void FieldDataInstaller::PcFieldToVGearsField(const VGears::FLevelFilePtr& field){
  188. const FieldDecompiler::DecompiledScript decompiled = script_installer_.Install(field);
  189. map_writer_.WriteMap(field, decompiled);
  190. const float scale = FieldData::ScaleFactor(
  191. scale_factors_, FieldData::FieldId(field->getName(), map_list_)
  192. );
  193. background_installer_.Install(field, decompiled, scale);
  194. map_writer_.WriteWalkmesh(field);
  195. converted_map_list_.push_back(field->getName());
  196. }