FieldModelInstaller.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <boost/filesystem.hpp>
  18. #include <OgreMaterialManager.h>
  19. #include <OgreMaterialSerializer.h>
  20. #include <OgreMeshManager.h>
  21. #include <OgreMeshSerializer.h>
  22. #include <OgreSkeletonSerializer.h>
  23. #include <OgreSubMesh.h>
  24. #include <OgreTechnique.h>
  25. #include "common/File.h"
  26. #include "formats/FF7NameLookup.h"
  27. #include "formats/lgp/LGPArchive.h"
  28. #include "formats/model/AFileManager.h"
  29. #include "formats/model/HRCFileManager.h"
  30. #include "installer/formats/TexFile.h"
  31. #include "installer/tables/ModelMaterials.h"
  32. #include "FieldDataTypes.h"
  33. #include "FieldModelInstaller.h"
  34. FieldModelInstaller::FieldModelInstaller(
  35. const DiskImage& disk_image, const std::string& output_dir, const std::string& data_dir,
  36. ModelsAndAnimationsDb& used_models, std::function<void(std::string)> log_output_line
  37. ):
  38. disk_image_(disk_image), output_dir_(output_dir), data_dir_(data_dir), used_models_(used_models),
  39. write_output_line_(log_output_line)
  40. {}
  41. std::vector<std::string> FieldModelInstaller::Initialize(){
  42. ExtractModelFiles();
  43. // Ogre indexes a resource location when it is added, so files written afterwards are not
  44. // found until the location is registered again. On a clean install the folder was empty when
  45. // it was first added.
  46. Ogre::ResourceGroupManager& res_mgr = Ogre::ResourceGroupManager::getSingleton();
  47. res_mgr.removeResourceLocation(TempDir(), "FFVII");
  48. res_mgr.addResourceLocation(TempDir(), "FileSystem", "FFVII", true, true);
  49. std::vector<std::string> models;
  50. for (const auto& model : used_models_.map) models.push_back(model.first);
  51. return models;
  52. }
  53. void FieldModelInstaller::ConvertModel(const std::string& model_name){
  54. auto model = used_models_.map.find(model_name);
  55. if (model == used_models_.map.end()){
  56. write_output_line_("[ERROR] Model " + model_name + " is not used by any field.");
  57. return;
  58. }
  59. VGears::HRCFileManager& hrc_manager = VGears::HRCFileManager::GetSingleton();
  60. try{
  61. Ogre::ResourcePtr hrc = hrc_manager.load(model->first, "FFVII");
  62. Ogre::String base_name;
  63. VGears::StringUtil::splitBase(model->first, base_name);
  64. const auto mesh_name = VGears::NameLookup::model(base_name) + ".mesh";
  65. Ogre::MeshPtr mesh(Ogre::MeshManager::getSingleton().load(mesh_name, "FFVII"));
  66. Ogre::SkeletonPtr skeleton(mesh->getSkeleton());
  67. const Ogre::AxisAlignedBox bind_bounds = mesh->getBounds();
  68. Ogre::AxisAlignedBox translation_bounds(Ogre::Vector3::ZERO, Ogre::Vector3::ZERO);
  69. for (const auto& animation_name : model->second){
  70. VGears::AFilePtr animation = VGears::AFileManager::GetSingleton()
  71. .load(animation_name, "FFVII").staticCast<VGears::AFile>();
  72. // Convert the FF7 name to a more readable name set in the meta data.
  73. VGears::StringUtil::splitBase(animation_name, base_name);
  74. animation->AddTo(skeleton, VGears::NameLookup::Animation(base_name), true);
  75. translation_bounds.merge(animation->GetRootTranslationBounds(true));
  76. }
  77. ExtendBounds(mesh, bind_bounds, translation_bounds);
  78. ExportMesh(ModelsDir(), mesh);
  79. // Models can share a skeleton name with different bones. Drop the model (and its skeleton
  80. // and mesh) so the next one doesn't reuse them.
  81. hrc_manager.remove(model->first, "FFVII");
  82. }
  83. catch (const Ogre::Exception& ex){
  84. if (hrc_manager.resourceExists(model->first, "FFVII")) hrc_manager.remove(model->first, "FFVII");
  85. write_output_line_("[ERROR] Ogre exception converting model " + model->first + ": " + ex.what());
  86. }
  87. catch (const std::exception& ex){
  88. if (hrc_manager.resourceExists(model->first, "FFVII")) hrc_manager.remove(model->first, "FFVII");
  89. write_output_line_("[ERROR] Exception converting model " + model->first + ": " + ex.what());
  90. }
  91. }
  92. void FieldModelInstaller::ExtractModelFiles(){
  93. const std::string path = *disk_image_.filePath("data/field/char.lgp");
  94. // Read the list of files as an archive, and their contents as a file.
  95. VGears::LGPArchive char_lgp(path, "LGP");
  96. char_lgp.open(path, true);
  97. char_lgp.load();
  98. File char_file(path);
  99. const Ogre::StringVectorPtr file_list = char_lgp.list(true, true);
  100. const VGears::LGPArchive::FileList files = char_lgp.GetFiles();
  101. for (size_t i = 0; i < file_list->size(); i ++){
  102. const VGears::LGPArchive::FileEntry& entry = files.at(i);
  103. std::fstream out;
  104. out.open(TempDir() + entry.file_name, std::ios::out);
  105. char_file.SetOffset(entry.data_offset);
  106. for (int j = 0; j < entry.data_size; j ++) out << char_file.readU8();
  107. out.close();
  108. }
  109. }
  110. void FieldModelInstaller::ExtendBounds(
  111. const Ogre::MeshPtr& mesh, const Ogre::AxisAlignedBox& bind_bounds,
  112. const Ogre::AxisAlignedBox& translation_bounds
  113. ){
  114. if (!bind_bounds.isFinite()) return;
  115. const Ogre::AxisAlignedBox animated_bounds(
  116. bind_bounds.getMinimum() + translation_bounds.getMinimum(),
  117. bind_bounds.getMaximum() + translation_bounds.getMaximum()
  118. );
  119. mesh->_setBounds(animated_bounds, false);
  120. mesh->_setBoundingSphereRadius(std::max(
  121. mesh->getBoundingSphereRadius(),
  122. animated_bounds.getCenter().length() + animated_bounds.getHalfSize().length()
  123. ));
  124. }
  125. void FieldModelInstaller::ExportMesh(const std::string& outdir, const Ogre::MeshPtr& mesh){
  126. VGears::String base_mesh_name;
  127. VGears::StringUtil::splitFull(mesh->getName(), base_mesh_name);
  128. MakeMaterialsUnique(mesh, base_mesh_name);
  129. Ogre::SkeletonPtr skeleton(mesh->getSkeleton());
  130. Ogre::SkeletonSerializer skeleton_serializer;
  131. skeleton_serializer.exportSkeleton(skeleton.getPointer(), outdir + base_mesh_name + ".skeleton");
  132. mesh->setSkeletonName(base_mesh_name + ".skeleton");
  133. Ogre::MeshSerializer mesh_serializer;
  134. mesh_serializer.exportMesh(mesh.getPointer(), outdir + mesh->getName());
  135. Ogre::MaterialSerializer material_serializer;
  136. Ogre::Mesh::SubMeshIterator it(mesh->getSubMeshIterator());
  137. while (it.hasMoreElements()){
  138. Ogre::SubMesh* sub_mesh(it.getNext());
  139. Ogre::MaterialPtr material(
  140. Ogre::MaterialManager::getSingleton().getByName(sub_mesh->getMaterialName())
  141. );
  142. if (material == nullptr) continue;
  143. for (size_t t = 0; t < material->getNumTechniques(); t ++){
  144. Ogre::Technique* technique = material->getTechnique(t);
  145. if (technique == nullptr) continue;
  146. for (size_t p = 0; p < technique->getNumPasses(); p ++){
  147. Ogre::Pass* pass = technique->getPass(p);
  148. if (pass == nullptr) continue;
  149. for (size_t u = 0; u < pass->getNumTextureUnitStates(); u ++){
  150. Ogre::TextureUnitState* unit = pass->getTextureUnitState(u);
  151. if (unit != nullptr && !unit->getTextureName().empty())
  152. ConvertTexture(*unit, base_mesh_name);
  153. }
  154. }
  155. }
  156. if (std::count(materials_.begin(), materials_.end(), sub_mesh->getMaterialName()) == 0){
  157. material_serializer.queueForExport(material);
  158. materials_.push_back(sub_mesh->getMaterialName());
  159. }
  160. }
  161. material_serializer.exportQueued(outdir + base_mesh_name + VGears::EXT_MATERIAL);
  162. }
  163. void FieldModelInstaller::ConvertTexture(Ogre::TextureUnitState& unit, const std::string& base_mesh_name){
  164. // Materials are shared by name across all model types, so this one may have already been
  165. // converted for a battle model (its texture is then a png in another folder). There is
  166. // nothing to convert in that case.
  167. if (!boost::filesystem::exists(TempDir() + unit.getTextureName())){
  168. write_output_line_(
  169. "[WARNING] Texture " + unit.getTextureName() + " of model " + base_mesh_name
  170. + " not found; skipping it."
  171. );
  172. return;
  173. }
  174. TexFile tex(TempDir() + unit.getTextureName());
  175. // The material uses the png, under the name of the model.
  176. Ogre::String base_name;
  177. VGears::StringUtil::splitBase(unit.getTextureName(), base_name);
  178. const std::string png_name = base_mesh_name + "_" + base_name + ".png";
  179. unit.setTextureName(png_name);
  180. tex.SavePng(ModelsDir() + png_name, 0);
  181. }
  182. std::string FieldModelInstaller::TempDir() const{return output_dir_ + "temp/char/";}
  183. std::string FieldModelInstaller::ModelsDir() const{return data_dir_ + FieldData::MODELS_DIR + "/";}