/* * Copyright (C) 2022 The V-Gears Team * * This file is part of V-Gears * * V-Gears is free software: you can redistribute it and/or modify it under * terms of the GNU General Public License as published by the Free Software * Foundation, version 3.0 (GPLv3) of the License. * * V-Gears is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include #include #include #include #include #include "common/StringUtil.h" #include "formats/field/LZSFLevelFileManager.h" #include "formats/field/MapListFile.h" #include "FieldDataInstaller.h" #include "FieldScriptFormatters.h" FieldDataInstaller::FieldDataInstaller( const DiskImage& disk_image, const std::string output_dir, std::function log_output_line ): output_dir_(output_dir), data_dir_(output_dir + "game/"), write_output_line_(log_output_line), script_installer_(data_dir_, map_list_, spawn_points_, log_output_line), map_writer_( data_dir_, map_list_, spawn_points_, scale_factors_, used_models_and_anims_, log_output_line ), background_installer_(data_dir_), model_installer_(disk_image, output_dir_, data_dir_, used_models_and_anims_, log_output_line) {} FieldDataInstaller::~FieldDataInstaller(){} void FieldDataInstaller::LoadInstalledFields(){ converted_map_list_.clear(); for (const std::string& name : map_list_){ if (boost::filesystem::exists(data_dir_ + FieldData::MAPS_DIR + "/" + name + "/map.xml")){ converted_map_list_.push_back(name); } } } int FieldDataInstaller::CollectSpawnAndScaleFactorsInit(Ogre::ResourceGroupManager* res_mgr){ // List what's in the LGP archive. flevel_file_list_ = res_mgr->listResourceNames("FFVIIFields", "*"); // Load the map list field. VGears::MapListFilePtr map_list = VGears::MapListFileManager::GetSingleton().load( "maplist", "FFVIIFields" ).staticCast(); map_list_ = map_list->GetMapList(); return flevel_file_list_->size(); } void FieldDataInstaller::CollectSpawnAndScaleFactors(int field_index){ // TODO: Show invalid index. if (field_index >= flevel_file_list_->size()) return; const auto resource_name = (*flevel_file_list_)[field_index]; // Exclude things that are not fields. if (!IsAFieldFile(resource_name)) return; try{ VGears::FLevelFilePtr field = VGears::LZSFLevelFileManager::GetSingleton() .load(resource_name, "FFVIIFields").staticCast(); if (!CollectSpawnPoints(field)){ failed_fields_.push_back(resource_name); return; } scale_factors_[FieldData::FieldId(field->getName(), map_list_)] = FieldDecompiler::ScaleFactor(field->GetRawScript()); } catch (const std::exception& exception){ failed_fields_.push_back(resource_name); std::cerr << "[WARNING] CollectSpawnAndScaleFactors: skipped " << resource_name << " after error: " << exception.what() << std::endl; } } void FieldDataInstaller::WriteWorldArrivals(){ // The exits to the world map ("wm") have no field to write the spawn points into, so the // coordinates that each exit gives (world units) are written for the world map scripts, by // spawn point name. std::string output = "-- Generated by the installer: where each field exit puts the player in the world map.\n" "WorldArrivals = {\n"; for (const auto& entry : spawn_points_){ if (entry.first >= map_list_.size()) continue; const std::string& target = map_list_[entry.first]; if (target.size() < 3 || target.compare(0, 2, "wm") != 0 || target.find_first_not_of("0123456789", 2) != std::string::npos) continue; for (const SpawnPointDb::Record& record : entry.second.gateways_to_this_field){ std::string name; if (record.from_script){ name = map_list_.at(record.field_id) + "_" + record.entity_name + "_" + record.script_function_name + "_addr_" + std::to_string(record.gateway_index_or_map_jump_address); } else{ name = "Spawn_" + map_list_.at(record.field_id) + "_" + std::to_string(record.gateway_index_or_map_jump_address); } output += " [\"" + name + "\"] = {x = " + std::to_string(record.gateway.destination.x) + ", y = " + std::to_string(record.gateway.destination.y) + ", dir = " + std::to_string(record.gateway.dir) + "},\n"; } } output += "}\n"; boost::filesystem::create_directories(data_dir_ + "world/scripts"); std::ofstream file(data_dir_ + "world/scripts/world_arrivals.lua"); file << output; } void FieldDataInstaller::Convert(int field_index){ if (field_index == 0) WriteWorldArrivals(); // TODO: Show invalid index. if (field_index >= flevel_file_list_->size()) return; const auto resource_name = (*flevel_file_list_)[field_index]; // Exclude things that are not fields. if (!IsAFieldFile(resource_name)) return; if (std::find(failed_fields_.begin(), failed_fields_.end(), resource_name) != failed_fields_.end()){ std::cerr << "[ERROR] Skip field: " << resource_name << " because its scripts or scale factor could not be collected." << std::endl; return; } const QString dir = QString::fromStdString(data_dir_ + FieldData::MAPS_DIR + "/" + resource_name); if (!QDir(dir).mkpath(".")) throw std::runtime_error("Failed to mkpath"); VGears::FLevelFilePtr field = VGears::LZSFLevelFileManager::GetSingleton() .load(resource_name, "FFVIIFields").staticCast(); PcFieldToVGearsField(field); } int FieldDataInstaller::WriteInit(){ doc_ = std::make_unique(); element_ = std::make_unique("maps"); return converted_map_list_.size(); } void FieldDataInstaller::Write(int field_index){ // TODO: Probably need to inject "empty" and "test" fields. // TODO: Show invalid index. if (field_index >= converted_map_list_.size()) return; const auto map = converted_map_list_[field_index]; std::unique_ptr xml_element(new TiXmlElement("map")); xml_element->SetAttribute("name", map); xml_element->SetAttribute("file_name", map + "/map.xml"); element_->LinkEndChild(xml_element.release()); } void FieldDataInstaller::WriteEnd(){ doc_->LinkEndChild(element_.release()); doc_->SaveFile(data_dir_ + "/field/maps/_fields.xml"); } std::vector FieldDataInstaller::ConvertModelsInit(){return model_installer_.Initialize();} void FieldDataInstaller::ConvertModels(std::string model_name){model_installer_.ConvertModel(model_name);} bool FieldDataInstaller::IsAFieldFile(const Ogre::String& resource_name){ return ( !VGears::StringUtil::endsWith(resource_name, ".tex") && !VGears::StringUtil::endsWith(resource_name, ".tut") && !VGears::StringUtil::endsWith(resource_name, ".siz") && resource_name != "maplist" ); } bool FieldDataInstaller::CollectSpawnPoints(const VGears::FLevelFilePtr& field){ const size_t field_id = FieldData::FieldId(field->getName(), map_list_); // Decompile the script just so the MAPJUMPs can be collected. This is needed to construct the // full list of entries to each field. try{ FF7FieldScriptGatewayCollector formatter(field->getName(), map_list_, spawn_points_, field_id); FieldDecompiler::Decompile( field->getName(), field->GetRawScript(), formatter, "", "EntityContainer = {}\n\n" ); } catch (const ::DecompilerException& ex){ std::cerr << "[WARNING] CollectSpawnPoints: decompiler failed for " << field->getName() << ": " << ex.what() << std::endl; return false; } const VGears::TriggersFilePtr& triggers = field->GetTriggers(); if (triggers.isNull()){ std::cerr << "CollectSpawnPoints: missing trigger data for " << field->getName() << std::endl; return false; } const auto& gateways = triggers->GetGateways(); for (size_t i = 0; i < gateways.size(); i ++){ if (gateways[i].destination_field_id == FieldData::INACTIVE_GATEWAY_ID) continue; SpawnPointDb::Record record; record.field_id = static_cast(field_id); record.gateway = gateways[i]; record.gateway_index_or_map_jump_address = static_cast(i); FieldData::AddSpawnPoint(spawn_points_, record); } return true; } void FieldDataInstaller::PcFieldToVGearsField(const VGears::FLevelFilePtr& field){ const FieldDecompiler::DecompiledScript decompiled = script_installer_.Install(field); map_writer_.WriteMap(field, decompiled); const float scale = FieldData::ScaleFactor( scale_factors_, FieldData::FieldId(field->getName(), map_list_) ); background_installer_.Install(field, decompiled, scale); map_writer_.WriteWalkmesh(field); converted_map_list_.push_back(field->getName()); }