From 2d58054ffceea3649410fa1cb7e01f3cc2066f3a Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 11 Jan 2025 13:06:48 +0100 Subject: [PATCH] refactor: make use of IOutputPath in ObjWriting --- .../SearchPath/OutputPathFilesystem.cpp | 2 +- .../Dumping/AssetDumpingContext.cpp | 32 ++++--------------- src/ObjWriting/Dumping/AssetDumpingContext.h | 22 +++++++------ .../Dumping/IZoneAssetDumperState.h | 3 +- .../Dumping/Localize/StringFileDumper.cpp | 4 +-- .../Dumping/Localize/StringFileDumper.h | 25 ++++++++------- .../IW3/AssetDumpers/AssetDumperGfxImage.cpp | 18 +++++------ .../AssetDumpers/AssetDumperLocalizeEntry.cpp | 12 +++---- .../IW3/AssetDumpers/AssetDumperXModel.cpp | 13 ++++---- src/ObjWriting/Game/IW3/ObjWriterIW3.cpp | 2 +- .../IW4/AssetDumpers/AssetDumperGfxImage.cpp | 20 ++++++------ .../AssetDumpers/AssetDumperLocalizeEntry.cpp | 10 +++--- .../IW4/AssetDumpers/AssetDumperXModel.cpp | 13 ++++---- src/ObjWriting/Game/IW4/ObjWriterIW4.cpp | 2 +- .../IW5/AssetDumpers/AssetDumperGfxImage.cpp | 20 ++++++------ .../AssetDumpers/AssetDumperLocalizeEntry.cpp | 12 +++---- src/ObjWriting/Game/IW5/ObjWriterIW5.cpp | 2 +- .../T5/AssetDumpers/AssetDumperGfxImage.cpp | 20 ++++++------ .../AssetDumpers/AssetDumperLocalizeEntry.cpp | 12 +++---- src/ObjWriting/Game/T5/ObjWriterT5.cpp | 2 +- .../T6/AssetDumpers/AssetDumperGfxImage.cpp | 24 +++++++------- .../AssetDumpers/AssetDumperLocalizeEntry.cpp | 12 +++---- src/ObjWriting/Game/T6/ObjWriterT6.cpp | 2 +- .../XModel/XModelDumper.cpp.template | 12 +++---- src/Unlinker/Unlinker.cpp | 18 +++++------ 25 files changed, 143 insertions(+), 171 deletions(-) diff --git a/src/ObjCommon/SearchPath/OutputPathFilesystem.cpp b/src/ObjCommon/SearchPath/OutputPathFilesystem.cpp index 41fb008c6..038c56668 100644 --- a/src/ObjCommon/SearchPath/OutputPathFilesystem.cpp +++ b/src/ObjCommon/SearchPath/OutputPathFilesystem.cpp @@ -24,7 +24,7 @@ std::unique_ptr OutputPathFilesystem::Open(const std::string& file fs::create_directories(containingDirectory, ec); if (ec) { - std::cerr << std::format("Failed to create folder '{}' when try to open file '{}'\n", containingDirectory, fileName); + std::cerr << std::format("Failed to create folder '{}' when try to open file '{}'\n", containingDirectory.string(), fileName); return nullptr; } diff --git a/src/ObjWriting/Dumping/AssetDumpingContext.cpp b/src/ObjWriting/Dumping/AssetDumpingContext.cpp index b29842742..ea3041e12 100644 --- a/src/ObjWriting/Dumping/AssetDumpingContext.cpp +++ b/src/ObjWriting/Dumping/AssetDumpingContext.cpp @@ -4,35 +4,15 @@ #include #include -AssetDumpingContext::AssetDumpingContext() - : m_zone(nullptr) +AssetDumpingContext::AssetDumpingContext(const Zone& zone, const std::string& basePath, IOutputPath& outputPath, ISearchPath& objSearchPath) + : m_zone(zone), + m_base_path(basePath), + m_output_path(outputPath), + m_obj_search_path(objSearchPath) { } std::unique_ptr AssetDumpingContext::OpenAssetFile(const std::string& fileName) const { - std::filesystem::path assetFilePath(m_base_path); - assetFilePath.append(fileName); - - auto assetFileFolder(assetFilePath); - assetFileFolder.replace_filename(""); - - std::error_code ec; - std::filesystem::create_directories(assetFileFolder, ec); - - if (ec) - { - std::cerr << std::format("Failed to create folder '{}'. Asset '{}' won't be dumped\n", assetFilePath.string(), fileName); - return nullptr; - } - - auto file = std::make_unique(assetFilePath, std::fstream::out | std::fstream::binary); - - if (!file->is_open()) - { - std::cerr << std::format("Failed to open file '{}' to dump asset '{}'\n", assetFilePath.string(), fileName); - return nullptr; - } - - return std::move(file); + return m_output_path.Open(fileName); } diff --git a/src/ObjWriting/Dumping/AssetDumpingContext.h b/src/ObjWriting/Dumping/AssetDumpingContext.h index d4e78735b..bfc384de6 100644 --- a/src/ObjWriting/Dumping/AssetDumpingContext.h +++ b/src/ObjWriting/Dumping/AssetDumpingContext.h @@ -2,8 +2,8 @@ #include "IZoneAssetDumperState.h" #include "Obj/Gdt/GdtStream.h" +#include "SearchPath/IOutputPath.h" #include "SearchPath/ISearchPath.h" -#include "Utils/ClassUtils.h" #include "Zone/Zone.h" #include @@ -13,17 +13,10 @@ class AssetDumpingContext { - std::unordered_map> m_zone_asset_dumper_states; - public: - Zone* m_zone; - std::string m_base_path; - std::unique_ptr m_gdt; - ISearchPath* m_obj_search_path; - - AssetDumpingContext(); + AssetDumpingContext(const Zone& zone, const std::string& basePath, IOutputPath& outputPath, ISearchPath& objSearchPath); - _NODISCARD std::unique_ptr OpenAssetFile(const std::string& fileName) const; + [[nodiscard]] std::unique_ptr OpenAssetFile(const std::string& fileName) const; template T* GetZoneAssetDumperState() { @@ -40,4 +33,13 @@ class AssetDumpingContext m_zone_asset_dumper_states.emplace(std::make_pair>(typeid(T), std::move(newState))); return newStatePtr; } + + const Zone& m_zone; + const std::string& m_base_path; + IOutputPath& m_output_path; + ISearchPath& m_obj_search_path; + std::unique_ptr m_gdt; + +private: + std::unordered_map> m_zone_asset_dumper_states; }; diff --git a/src/ObjWriting/Dumping/IZoneAssetDumperState.h b/src/ObjWriting/Dumping/IZoneAssetDumperState.h index ad12d5e51..19403b5ae 100644 --- a/src/ObjWriting/Dumping/IZoneAssetDumperState.h +++ b/src/ObjWriting/Dumping/IZoneAssetDumperState.h @@ -1,4 +1,5 @@ #pragma once + #include "Zone/Zone.h" class IZoneAssetDumperState @@ -13,7 +14,7 @@ class IZoneAssetDumperState IZoneAssetDumperState& operator=(const IZoneAssetDumperState& other) = default; IZoneAssetDumperState& operator=(IZoneAssetDumperState&& other) noexcept = default; - virtual void SetZone(Zone* zone) + virtual void SetZone(const Zone& zone) { // Do nothing by default } diff --git a/src/ObjWriting/Dumping/Localize/StringFileDumper.cpp b/src/ObjWriting/Dumping/Localize/StringFileDumper.cpp index 2dc25be8a..b36af710a 100644 --- a/src/ObjWriting/Dumping/Localize/StringFileDumper.cpp +++ b/src/ObjWriting/Dumping/Localize/StringFileDumper.cpp @@ -4,7 +4,7 @@ #include -StringFileDumper::StringFileDumper(Zone* zone, std::ostream& stream) +StringFileDumper::StringFileDumper(const Zone& zone, std::ostream& stream) : AbstractTextDumper(stream), m_zone(zone), m_language_caps("ENGLISH"), @@ -31,7 +31,7 @@ void StringFileDumper::SetNotes(std::string notes) void StringFileDumper::WriteHeader() { - m_stream << "// Dumped from fastfile \"" << m_zone->m_name << "\".\n"; + m_stream << "// Dumped from fastfile \"" << m_zone.m_name << "\".\n"; m_stream << "// In their original format the strings might have been separated in multiple files.\n"; m_stream << "VERSION \"1\"\n"; m_stream << "CONFIG \"" << m_config_file << "\"\n"; diff --git a/src/ObjWriting/Dumping/Localize/StringFileDumper.h b/src/ObjWriting/Dumping/Localize/StringFileDumper.h index a78464c48..404107982 100644 --- a/src/ObjWriting/Dumping/Localize/StringFileDumper.h +++ b/src/ObjWriting/Dumping/Localize/StringFileDumper.h @@ -5,19 +5,8 @@ class StringFileDumper : AbstractTextDumper { - Zone* m_zone; - - std::string m_config_file; - std::string m_notes; - std::string m_language_caps; - - bool m_wrote_header; - - void WriteHeader(); - void WriteReference(const std::string& reference) const; - public: - StringFileDumper(Zone* zone, std::ostream& stream); + StringFileDumper(const Zone& zone, std::ostream& stream); void SetConfigFile(std::string configFile); void SetNotes(std::string notes); @@ -26,4 +15,16 @@ class StringFileDumper : AbstractTextDumper void WriteLocalizeEntry(const std::string& reference, const std::string& value); void Finalize(); + +private: + void WriteHeader(); + void WriteReference(const std::string& reference) const; + + const Zone& m_zone; + + std::string m_config_file; + std::string m_notes; + std::string m_language_caps; + + bool m_wrote_header; }; diff --git a/src/ObjWriting/Game/IW3/AssetDumpers/AssetDumperGfxImage.cpp b/src/ObjWriting/Game/IW3/AssetDumpers/AssetDumperGfxImage.cpp index b482f8fbc..5f76f1c45 100644 --- a/src/ObjWriting/Game/IW3/AssetDumpers/AssetDumperGfxImage.cpp +++ b/src/ObjWriting/Game/IW3/AssetDumpers/AssetDumperGfxImage.cpp @@ -17,11 +17,11 @@ using namespace IW3; namespace { - std::unique_ptr LoadImageFromLoadDef(const GfxImage* image) + std::unique_ptr LoadImageFromLoadDef(const GfxImage& image) { Dx9TextureLoader textureLoader; - const auto& loadDef = *image->texture.loadDef; + const auto& loadDef = *image.texture.loadDef; textureLoader.Width(loadDef.dimensions[0]).Height(loadDef.dimensions[1]).Depth(loadDef.dimensions[2]); if (loadDef.flags & iwi6::IMG_FLAG_VOLMAP) @@ -36,22 +36,22 @@ namespace return textureLoader.LoadTexture(loadDef.data); } - std::unique_ptr LoadImageFromIwi(const GfxImage* image, ISearchPath* searchPath) + std::unique_ptr LoadImageFromIwi(const GfxImage& image, ISearchPath& searchPath) { - const auto imageFileName = std::format("images/{}.iwi", image->name); - const auto filePathImage = searchPath->Open(imageFileName); + const auto imageFileName = std::format("images/{}.iwi", image.name); + const auto filePathImage = searchPath.Open(imageFileName); if (!filePathImage.IsOpen()) { - std::cerr << std::format("Could not find data for image \"{}\"\n", image->name); + std::cerr << std::format("Could not find data for image \"{}\"\n", image.name); return nullptr; } return iwi::LoadIwi(*filePathImage.m_stream); } - std::unique_ptr LoadImageData(ISearchPath* searchPath, const GfxImage* image) + std::unique_ptr LoadImageData(ISearchPath& searchPath, const GfxImage& image) { - if (image->texture.loadDef && image->texture.loadDef->resourceSize > 0) + if (image.texture.loadDef && image.texture.loadDef->resourceSize > 0) return LoadImageFromLoadDef(image); return LoadImageFromIwi(image, searchPath); @@ -91,7 +91,7 @@ std::string AssetDumperGfxImage::GetAssetFileName(const XAssetInfo& as void AssetDumperGfxImage::DumpAsset(AssetDumpingContext& context, XAssetInfo* asset) { const auto* image = asset->Asset(); - const auto texture = LoadImageData(context.m_obj_search_path, image); + const auto texture = LoadImageData(context.m_obj_search_path, *image); if (!texture) return; diff --git a/src/ObjWriting/Game/IW3/AssetDumpers/AssetDumperLocalizeEntry.cpp b/src/ObjWriting/Game/IW3/AssetDumpers/AssetDumperLocalizeEntry.cpp index d56accd10..d95a03b75 100644 --- a/src/ObjWriting/Game/IW3/AssetDumpers/AssetDumperLocalizeEntry.cpp +++ b/src/ObjWriting/Game/IW3/AssetDumpers/AssetDumperLocalizeEntry.cpp @@ -3,7 +3,8 @@ #include "Dumping/Localize/StringFileDumper.h" #include "Localize/LocalizeCommon.h" -#include +#include +#include using namespace IW3; @@ -12,11 +13,8 @@ void AssetDumperLocalizeEntry::DumpPool(AssetDumpingContext& context, AssetPool< if (pool->m_asset_lookup.empty()) return; - const auto language = LocalizeCommon::GetNameOfLanguage(context.m_zone->m_language); - - std::ostringstream ss; - ss << language << "/localizedstrings/" << context.m_zone->m_name << ".str"; - const auto assetFile = context.OpenAssetFile(ss.str()); + const auto language = LocalizeCommon::GetNameOfLanguage(context.m_zone.m_language); + const auto assetFile = context.OpenAssetFile(std::format("{}/localizedstrings/{}.str", language, context.m_zone.m_name)); if (assetFile) { @@ -38,6 +36,6 @@ void AssetDumperLocalizeEntry::DumpPool(AssetDumpingContext& context, AssetPool< } else { - printf("Could not create string file for dumping localized strings of zone '%s'\n", context.m_zone->m_name.c_str()); + std::cerr << std::format("Could not create string file for dumping localized strings of zone '{}'\n", context.m_zone.m_name); } } diff --git a/src/ObjWriting/Game/IW3/AssetDumpers/AssetDumperXModel.cpp b/src/ObjWriting/Game/IW3/AssetDumpers/AssetDumperXModel.cpp index 0775772f1..f48934777 100644 --- a/src/ObjWriting/Game/IW3/AssetDumpers/AssetDumperXModel.cpp +++ b/src/ObjWriting/Game/IW3/AssetDumpers/AssetDumperXModel.cpp @@ -107,8 +107,8 @@ namespace for (auto boneNum = 0u; boneNum < model->numBones; boneNum++) { XModelBone bone; - if (model->boneNames[boneNum] < context.m_zone->m_script_strings.Count()) - bone.name = context.m_zone->m_script_strings[model->boneNames[boneNum]]; + if (model->boneNames[boneNum] < context.m_zone.m_script_strings.Count()) + bone.name = context.m_zone.m_script_strings[model->boneNames[boneNum]]; else bone.name = "INVALID_BONE_NAME"; @@ -426,7 +426,7 @@ namespace if (!mtlFile) return; - const auto writer = obj::CreateMtlWriter(*mtlFile, context.m_zone->m_game->GetShortName(), context.m_zone->m_name); + const auto writer = obj::CreateMtlWriter(*mtlFile, context.m_zone.m_game->GetShortName(), context.m_zone.m_name); DistinctMapper materialMapper(model->numsurfs); writer->Write(common); @@ -440,8 +440,7 @@ namespace if (!assetFile) return; - const auto writer = - obj::CreateObjWriter(*assetFile, std::format("{}.mtl", model->name), context.m_zone->m_game->GetShortName(), context.m_zone->m_name); + const auto writer = obj::CreateObjWriter(*assetFile, std::format("{}.mtl", model->name), context.m_zone.m_game->GetShortName(), context.m_zone.m_name); DistinctMapper materialMapper(model->numsurfs); writer->Write(common); @@ -455,7 +454,7 @@ namespace if (!assetFile) return; - const auto writer = xmodel_export::CreateWriterForVersion6(*assetFile, context.m_zone->m_game->GetShortName(), context.m_zone->m_name); + const auto writer = xmodel_export::CreateWriterForVersion6(*assetFile, context.m_zone.m_game->GetShortName(), context.m_zone.m_name); writer->Write(common); } @@ -470,7 +469,7 @@ namespace return; const auto output = std::make_unique(*assetFile); - const auto writer = gltf::Writer::CreateWriter(output.get(), context.m_zone->m_game->GetShortName(), context.m_zone->m_name); + const auto writer = gltf::Writer::CreateWriter(output.get(), context.m_zone.m_game->GetShortName(), context.m_zone.m_name); writer->Write(common); } diff --git a/src/ObjWriting/Game/IW3/ObjWriterIW3.cpp b/src/ObjWriting/Game/IW3/ObjWriterIW3.cpp index 997f5e72c..017856121 100644 --- a/src/ObjWriting/Game/IW3/ObjWriterIW3.cpp +++ b/src/ObjWriting/Game/IW3/ObjWriterIW3.cpp @@ -23,7 +23,7 @@ bool ObjWriter::DumpZone(AssetDumpingContext& context) const dumper.DumpPool(context, assetPools->poolName.get()); \ } - const auto* assetPools = dynamic_cast(context.m_zone->m_pools.get()); + const auto* assetPools = dynamic_cast(context.m_zone.m_pools.get()); // DUMP_ASSET_POOL(AssetDumperPhysPreset, m_phys_preset, ASSET_TYPE_PHYSPRESET) // DUMP_ASSET_POOL(AssetDumperXAnimParts, m_xanim_parts, ASSET_TYPE_XANIMPARTS) diff --git a/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperGfxImage.cpp b/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperGfxImage.cpp index fe311b935..4490e5202 100644 --- a/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperGfxImage.cpp +++ b/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperGfxImage.cpp @@ -14,12 +14,12 @@ using namespace IW4; namespace { - std::unique_ptr LoadImageFromLoadDef(const GfxImage* image) + std::unique_ptr LoadImageFromLoadDef(const GfxImage& image) { Dx9TextureLoader textureLoader; - const auto& loadDef = *image->texture.loadDef; - textureLoader.Width(image->width).Height(image->height).Depth(image->depth); + const auto& loadDef = *image.texture.loadDef; + textureLoader.Width(image.width).Height(image.height).Depth(image.depth); if ((loadDef.flags & iwi8::IMG_FLAG_MAPTYPE_MASK) == iwi8::IMG_FLAG_MAPTYPE_3D) textureLoader.Type(TextureType::T_3D); @@ -33,22 +33,22 @@ namespace return textureLoader.LoadTexture(loadDef.data); } - std::unique_ptr LoadImageFromIwi(const GfxImage* image, ISearchPath* searchPath) + std::unique_ptr LoadImageFromIwi(const GfxImage& image, ISearchPath& searchPath) { - const auto imageFileName = std::format("images/{}.iwi", image->name); - const auto filePathImage = searchPath->Open(imageFileName); + const auto imageFileName = std::format("images/{}.iwi", image.name); + const auto filePathImage = searchPath.Open(imageFileName); if (!filePathImage.IsOpen()) { - std::cerr << std::format("Could not find data for image \"{}\"\n", image->name); + std::cerr << std::format("Could not find data for image \"{}\"\n", image.name); return nullptr; } return iwi::LoadIwi(*filePathImage.m_stream); } - std::unique_ptr LoadImageData(ISearchPath* searchPath, const GfxImage* image) + std::unique_ptr LoadImageData(ISearchPath& searchPath, const GfxImage& image) { - if (image->texture.loadDef && image->texture.loadDef->resourceSize > 0) + if (image.texture.loadDef && image.texture.loadDef->resourceSize > 0) return LoadImageFromLoadDef(image); return LoadImageFromIwi(image, searchPath); @@ -88,7 +88,7 @@ std::string AssetDumperGfxImage::GetAssetFileName(const XAssetInfo& as void AssetDumperGfxImage::DumpAsset(AssetDumpingContext& context, XAssetInfo* asset) { const auto* image = asset->Asset(); - const auto texture = LoadImageData(context.m_obj_search_path, image); + const auto texture = LoadImageData(context.m_obj_search_path, *image); if (!texture) return; diff --git a/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperLocalizeEntry.cpp b/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperLocalizeEntry.cpp index 33a682d42..ef1799ac9 100644 --- a/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperLocalizeEntry.cpp +++ b/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperLocalizeEntry.cpp @@ -3,6 +3,7 @@ #include "Dumping/Localize/StringFileDumper.h" #include "Localize/LocalizeCommon.h" +#include #include using namespace IW4; @@ -12,11 +13,8 @@ void AssetDumperLocalizeEntry::DumpPool(AssetDumpingContext& context, AssetPool< if (pool->m_asset_lookup.empty()) return; - const auto language = LocalizeCommon::GetNameOfLanguage(context.m_zone->m_language); - - std::ostringstream ss; - ss << language << "/localizedstrings/" << context.m_zone->m_name << ".str"; - const auto assetFile = context.OpenAssetFile(ss.str()); + const auto language = LocalizeCommon::GetNameOfLanguage(context.m_zone.m_language); + const auto assetFile = context.OpenAssetFile(std::format("{}/localizedstrings/{}.str", language, context.m_zone.m_name)); if (assetFile) { @@ -38,6 +36,6 @@ void AssetDumperLocalizeEntry::DumpPool(AssetDumpingContext& context, AssetPool< } else { - printf("Could not create string file for dumping localized strings of zone '%s'\n", context.m_zone->m_name.c_str()); + std::cerr << std::format("Could not create string file for dumping localized strings of zone '{}'\n", context.m_zone.m_name); } } diff --git a/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperXModel.cpp b/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperXModel.cpp index cee8cf252..f74919412 100644 --- a/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperXModel.cpp +++ b/src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperXModel.cpp @@ -102,8 +102,8 @@ namespace for (auto boneNum = 0u; boneNum < model->numBones; boneNum++) { XModelBone bone; - if (model->boneNames[boneNum] < context.m_zone->m_script_strings.Count()) - bone.name = context.m_zone->m_script_strings[model->boneNames[boneNum]]; + if (model->boneNames[boneNum] < context.m_zone.m_script_strings.Count()) + bone.name = context.m_zone.m_script_strings[model->boneNames[boneNum]]; else bone.name = "INVALID_BONE_NAME"; @@ -408,7 +408,7 @@ namespace if (!mtlFile) return; - const auto writer = obj::CreateMtlWriter(*mtlFile, context.m_zone->m_game->GetShortName(), context.m_zone->m_name); + const auto writer = obj::CreateMtlWriter(*mtlFile, context.m_zone.m_game->GetShortName(), context.m_zone.m_name); DistinctMapper materialMapper(model->numsurfs); writer->Write(common); @@ -427,8 +427,7 @@ namespace if (!assetFile) return; - const auto writer = - obj::CreateObjWriter(*assetFile, std::format("{}.mtl", model->name), context.m_zone->m_game->GetShortName(), context.m_zone->m_name); + const auto writer = obj::CreateObjWriter(*assetFile, std::format("{}.mtl", model->name), context.m_zone.m_game->GetShortName(), context.m_zone.m_name); DistinctMapper materialMapper(model->numsurfs); writer->Write(common); @@ -443,7 +442,7 @@ namespace if (!assetFile) return; - const auto writer = xmodel_export::CreateWriterForVersion6(*assetFile, context.m_zone->m_game->GetShortName(), context.m_zone->m_name); + const auto writer = xmodel_export::CreateWriterForVersion6(*assetFile, context.m_zone.m_game->GetShortName(), context.m_zone.m_name); writer->Write(common); } @@ -459,7 +458,7 @@ namespace return; const auto output = std::make_unique(*assetFile); - const auto writer = gltf::Writer::CreateWriter(output.get(), context.m_zone->m_game->GetShortName(), context.m_zone->m_name); + const auto writer = gltf::Writer::CreateWriter(output.get(), context.m_zone.m_game->GetShortName(), context.m_zone.m_name); writer->Write(common); } diff --git a/src/ObjWriting/Game/IW4/ObjWriterIW4.cpp b/src/ObjWriting/Game/IW4/ObjWriterIW4.cpp index 9e996b970..43a890ad4 100644 --- a/src/ObjWriting/Game/IW4/ObjWriterIW4.cpp +++ b/src/ObjWriting/Game/IW4/ObjWriterIW4.cpp @@ -36,7 +36,7 @@ bool ObjWriter::DumpZone(AssetDumpingContext& context) const dumper.DumpPool(context, assetPools->poolName.get()); \ } - const auto* assetPools = dynamic_cast(context.m_zone->m_pools.get()); + const auto* assetPools = dynamic_cast(context.m_zone.m_pools.get()); DUMP_ASSET_POOL(AssetDumperPhysPreset, m_phys_preset, ASSET_TYPE_PHYSPRESET) DUMP_ASSET_POOL(AssetDumperPhysCollmap, m_phys_collmap, ASSET_TYPE_PHYSCOLLMAP) diff --git a/src/ObjWriting/Game/IW5/AssetDumpers/AssetDumperGfxImage.cpp b/src/ObjWriting/Game/IW5/AssetDumpers/AssetDumperGfxImage.cpp index 8e3e76d12..82d3cca13 100644 --- a/src/ObjWriting/Game/IW5/AssetDumpers/AssetDumperGfxImage.cpp +++ b/src/ObjWriting/Game/IW5/AssetDumpers/AssetDumperGfxImage.cpp @@ -14,13 +14,13 @@ using namespace IW5; namespace { - std::unique_ptr LoadImageFromLoadDef(const GfxImage* image) + std::unique_ptr LoadImageFromLoadDef(const GfxImage& image) { Dx9TextureLoader textureLoader; - const auto& loadDef = *image->texture.loadDef; + const auto& loadDef = *image.texture.loadDef; - textureLoader.Width(image->width).Height(image->height).Depth(image->depth); + textureLoader.Width(image.width).Height(image.height).Depth(image.depth); if ((loadDef.flags & iwi8::IMG_FLAG_MAPTYPE_MASK) == iwi8::IMG_FLAG_MAPTYPE_3D) textureLoader.Type(TextureType::T_3D); @@ -34,22 +34,22 @@ namespace return textureLoader.LoadTexture(loadDef.data); } - std::unique_ptr LoadImageFromIwi(const GfxImage* image, ISearchPath* searchPath) + std::unique_ptr LoadImageFromIwi(const GfxImage& image, ISearchPath& searchPath) { - const auto imageFileName = std::format("images/{}.iwi", image->name); - const auto filePathImage = searchPath->Open(imageFileName); + const auto imageFileName = std::format("images/{}.iwi", image.name); + const auto filePathImage = searchPath.Open(imageFileName); if (!filePathImage.IsOpen()) { - std::cerr << std::format("Could not find data for image \"{}\"\n", image->name); + std::cerr << std::format("Could not find data for image \"{}\"\n", image.name); return nullptr; } return iwi::LoadIwi(*filePathImage.m_stream); } - std::unique_ptr LoadImageData(ISearchPath* searchPath, const GfxImage* image) + std::unique_ptr LoadImageData(ISearchPath& searchPath, const GfxImage& image) { - if (image->texture.loadDef && image->texture.loadDef->resourceSize > 0) + if (image.texture.loadDef && image.texture.loadDef->resourceSize > 0) return LoadImageFromLoadDef(image); return LoadImageFromIwi(image, searchPath); @@ -89,7 +89,7 @@ std::string AssetDumperGfxImage::GetAssetFileName(const XAssetInfo& as void AssetDumperGfxImage::DumpAsset(AssetDumpingContext& context, XAssetInfo* asset) { const auto* image = asset->Asset(); - const auto texture = LoadImageData(context.m_obj_search_path, image); + const auto texture = LoadImageData(context.m_obj_search_path, *image); if (!texture) return; diff --git a/src/ObjWriting/Game/IW5/AssetDumpers/AssetDumperLocalizeEntry.cpp b/src/ObjWriting/Game/IW5/AssetDumpers/AssetDumperLocalizeEntry.cpp index c46dd95b0..600e41b88 100644 --- a/src/ObjWriting/Game/IW5/AssetDumpers/AssetDumperLocalizeEntry.cpp +++ b/src/ObjWriting/Game/IW5/AssetDumpers/AssetDumperLocalizeEntry.cpp @@ -3,7 +3,8 @@ #include "Dumping/Localize/StringFileDumper.h" #include "Localize/LocalizeCommon.h" -#include +#include +#include using namespace IW5; @@ -12,11 +13,8 @@ void AssetDumperLocalizeEntry::DumpPool(AssetDumpingContext& context, AssetPool< if (pool->m_asset_lookup.empty()) return; - const auto language = LocalizeCommon::GetNameOfLanguage(context.m_zone->m_language); - - std::ostringstream ss; - ss << language << "/localizedstrings/" << context.m_zone->m_name << ".str"; - const auto assetFile = context.OpenAssetFile(ss.str()); + const auto language = LocalizeCommon::GetNameOfLanguage(context.m_zone.m_language); + const auto assetFile = context.OpenAssetFile(std::format("{}/localizedstrings/{}.str", language, context.m_zone.m_name)); if (assetFile) { @@ -38,6 +36,6 @@ void AssetDumperLocalizeEntry::DumpPool(AssetDumpingContext& context, AssetPool< } else { - printf("Could not create string file for dumping localized strings of zone '%s'\n", context.m_zone->m_name.c_str()); + std::cerr << std::format("Could not create string file for dumping localized strings of zone '{}'\n", context.m_zone.m_name); } } diff --git a/src/ObjWriting/Game/IW5/ObjWriterIW5.cpp b/src/ObjWriting/Game/IW5/ObjWriterIW5.cpp index e618dbccb..bbbca5ceb 100644 --- a/src/ObjWriting/Game/IW5/ObjWriterIW5.cpp +++ b/src/ObjWriting/Game/IW5/ObjWriterIW5.cpp @@ -28,7 +28,7 @@ bool ObjWriter::DumpZone(AssetDumpingContext& context) const dumper.DumpPool(context, assetPools->poolName.get()); \ } - const auto* assetPools = dynamic_cast(context.m_zone->m_pools.get()); + const auto* assetPools = dynamic_cast(context.m_zone.m_pools.get()); // DUMP_ASSET_POOL(AssetDumperPhysPreset, m_phys_preset, ASSET_TYPE_PHYSPRESET) // DUMP_ASSET_POOL(AssetDumperPhysCollmap, m_phys_collmap, ASSET_TYPE_PHYSCOLLMAP) // DUMP_ASSET_POOL(AssetDumperXAnimParts, m_xanim_parts, ASSET_TYPE_XANIMPARTS) diff --git a/src/ObjWriting/Game/T5/AssetDumpers/AssetDumperGfxImage.cpp b/src/ObjWriting/Game/T5/AssetDumpers/AssetDumperGfxImage.cpp index c150e2d9c..6bb99fcf9 100644 --- a/src/ObjWriting/Game/T5/AssetDumpers/AssetDumperGfxImage.cpp +++ b/src/ObjWriting/Game/T5/AssetDumpers/AssetDumperGfxImage.cpp @@ -14,12 +14,12 @@ using namespace T5; namespace { - std::unique_ptr LoadImageFromLoadDef(const GfxImage* image) + std::unique_ptr LoadImageFromLoadDef(const GfxImage& image) { Dx9TextureLoader textureLoader; - const auto& loadDef = *image->texture.loadDef; - textureLoader.Width(image->width).Height(image->height).Depth(image->depth); + const auto& loadDef = *image.texture.loadDef; + textureLoader.Width(image.width).Height(image.height).Depth(image.depth); if (loadDef.flags & iwi13::IMG_FLAG_VOLMAP) textureLoader.Type(TextureType::T_3D); @@ -33,22 +33,22 @@ namespace return textureLoader.LoadTexture(loadDef.data); } - std::unique_ptr LoadImageFromIwi(const GfxImage* image, ISearchPath* searchPath) + std::unique_ptr LoadImageFromIwi(const GfxImage& image, ISearchPath& searchPath) { - const auto imageFileName = std::format("images/{}.iwi", image->name); - const auto filePathImage = searchPath->Open(imageFileName); + const auto imageFileName = std::format("images/{}.iwi", image.name); + const auto filePathImage = searchPath.Open(imageFileName); if (!filePathImage.IsOpen()) { - std::cerr << std::format("Could not find data for image \"{}\"\n", image->name); + std::cerr << std::format("Could not find data for image \"{}\"\n", image.name); return nullptr; } return iwi::LoadIwi(*filePathImage.m_stream); } - std::unique_ptr LoadImageData(ISearchPath* searchPath, const GfxImage* image) + std::unique_ptr LoadImageData(ISearchPath& searchPath, const GfxImage& image) { - if (image->texture.loadDef && image->texture.loadDef->resourceSize > 0) + if (image.texture.loadDef && image.texture.loadDef->resourceSize > 0) return LoadImageFromLoadDef(image); return LoadImageFromIwi(image, searchPath); @@ -88,7 +88,7 @@ std::string AssetDumperGfxImage::GetAssetFileName(const XAssetInfo& as void AssetDumperGfxImage::DumpAsset(AssetDumpingContext& context, XAssetInfo* asset) { const auto* image = asset->Asset(); - const auto texture = LoadImageData(context.m_obj_search_path, image); + const auto texture = LoadImageData(context.m_obj_search_path, *image); if (!texture) return; diff --git a/src/ObjWriting/Game/T5/AssetDumpers/AssetDumperLocalizeEntry.cpp b/src/ObjWriting/Game/T5/AssetDumpers/AssetDumperLocalizeEntry.cpp index f78ff324d..0377f5cf6 100644 --- a/src/ObjWriting/Game/T5/AssetDumpers/AssetDumperLocalizeEntry.cpp +++ b/src/ObjWriting/Game/T5/AssetDumpers/AssetDumperLocalizeEntry.cpp @@ -3,7 +3,8 @@ #include "Dumping/Localize/StringFileDumper.h" #include "Localize/LocalizeCommon.h" -#include +#include +#include using namespace T5; @@ -12,11 +13,8 @@ void AssetDumperLocalizeEntry::DumpPool(AssetDumpingContext& context, AssetPool< if (pool->m_asset_lookup.empty()) return; - const auto language = LocalizeCommon::GetNameOfLanguage(context.m_zone->m_language); - - std::ostringstream ss; - ss << language << "/localizedstrings/" << context.m_zone->m_name << ".str"; - const auto assetFile = context.OpenAssetFile(ss.str()); + const auto language = LocalizeCommon::GetNameOfLanguage(context.m_zone.m_language); + const auto assetFile = context.OpenAssetFile(std::format("{}/localizedstrings/{}.str", language, context.m_zone.m_name)); if (assetFile) { @@ -38,6 +36,6 @@ void AssetDumperLocalizeEntry::DumpPool(AssetDumpingContext& context, AssetPool< } else { - printf("Could not create string file for dumping localized strings of zone '%s'\n", context.m_zone->m_name.c_str()); + std::cerr << std::format("Could not create string file for dumping localized strings of zone '{}'\n", context.m_zone.m_name); } } diff --git a/src/ObjWriting/Game/T5/ObjWriterT5.cpp b/src/ObjWriting/Game/T5/ObjWriterT5.cpp index be552546f..568263de7 100644 --- a/src/ObjWriting/Game/T5/ObjWriterT5.cpp +++ b/src/ObjWriting/Game/T5/ObjWriterT5.cpp @@ -23,7 +23,7 @@ bool ObjWriter::DumpZone(AssetDumpingContext& context) const dumper.DumpPool(context, assetPools->poolName.get()); \ } - const auto* assetPools = dynamic_cast(context.m_zone->m_pools.get()); + const auto* assetPools = dynamic_cast(context.m_zone.m_pools.get()); // DUMP_ASSET_POOL(AssetDumperPhysPreset, m_phys_preset, ASSET_TYPE_PHYSPRESET) // DUMP_ASSET_POOL(AssetDumperPhysConstraints, m_phys_constraints, ASSET_TYPE_PHYSCONSTRAINTS) diff --git a/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperGfxImage.cpp b/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperGfxImage.cpp index 60eb52c29..f529cca11 100644 --- a/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperGfxImage.cpp +++ b/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperGfxImage.cpp @@ -15,12 +15,12 @@ using namespace T6; namespace { - std::unique_ptr LoadImageFromLoadDef(const GfxImage* image) + std::unique_ptr LoadImageFromLoadDef(const GfxImage& image) { Dx12TextureLoader textureLoader; - const auto& loadDef = *image->texture.loadDef; - textureLoader.Width(image->width).Height(image->height).Depth(image->depth); + const auto& loadDef = *image.texture.loadDef; + textureLoader.Width(image.width).Height(image.height).Depth(image.depth); if (loadDef.flags & iwi27::IMG_FLAG_VOLMAP) textureLoader.Type(TextureType::T_3D); @@ -34,13 +34,13 @@ namespace return textureLoader.LoadTexture(loadDef.data); } - std::unique_ptr LoadImageFromIwi(const GfxImage* image, ISearchPath* searchPath) + std::unique_ptr LoadImageFromIwi(const GfxImage& image, ISearchPath& searchPath) { - if (image->streamedPartCount > 0) + if (image.streamedPartCount > 0) { for (auto* ipak : IIPak::Repository) { - auto ipakStream = ipak->GetEntryStream(image->hash, image->streamedParts[0].hash); + auto ipakStream = ipak->GetEntryStream(image.hash, image.streamedParts[0].hash); if (ipakStream) { @@ -53,20 +53,20 @@ namespace } } - const auto imageFileName = std::format("images/{}.iwi", image->name); - const auto filePathImage = searchPath->Open(imageFileName); + const auto imageFileName = std::format("images/{}.iwi", image.name); + const auto filePathImage = searchPath.Open(imageFileName); if (!filePathImage.IsOpen()) { - std::cerr << std::format("Could not find data for image \"{}\"\n", image->name); + std::cerr << std::format("Could not find data for image \"{}\"\n", image.name); return nullptr; } return iwi::LoadIwi(*filePathImage.m_stream); } - std::unique_ptr LoadImageData(ISearchPath* searchPath, const GfxImage* image) + std::unique_ptr LoadImageData(ISearchPath& searchPath, const GfxImage& image) { - if (image->texture.loadDef && image->texture.loadDef->resourceSize > 0) + if (image.texture.loadDef && image.texture.loadDef->resourceSize > 0) return LoadImageFromLoadDef(image); return LoadImageFromIwi(image, searchPath); @@ -106,7 +106,7 @@ std::string AssetDumperGfxImage::GetAssetFileName(const XAssetInfo& as void AssetDumperGfxImage::DumpAsset(AssetDumpingContext& context, XAssetInfo* asset) { const auto* image = asset->Asset(); - const auto texture = LoadImageData(context.m_obj_search_path, image); + const auto texture = LoadImageData(context.m_obj_search_path, *image); if (!texture) return; diff --git a/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperLocalizeEntry.cpp b/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperLocalizeEntry.cpp index 0f5257c32..e9ebf7359 100644 --- a/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperLocalizeEntry.cpp +++ b/src/ObjWriting/Game/T6/AssetDumpers/AssetDumperLocalizeEntry.cpp @@ -3,7 +3,8 @@ #include "Dumping/Localize/StringFileDumper.h" #include "Localize/LocalizeCommon.h" -#include +#include +#include using namespace T6; @@ -12,11 +13,8 @@ void AssetDumperLocalizeEntry::DumpPool(AssetDumpingContext& context, AssetPool< if (pool->m_asset_lookup.empty()) return; - const auto language = LocalizeCommon::GetNameOfLanguage(context.m_zone->m_language); - - std::ostringstream ss; - ss << language << "/localizedstrings/" << context.m_zone->m_name << ".str"; - const auto assetFile = context.OpenAssetFile(ss.str()); + const auto language = LocalizeCommon::GetNameOfLanguage(context.m_zone.m_language); + const auto assetFile = context.OpenAssetFile(std::format("{}/localizedstrings/{}.str", language, context.m_zone.m_name)); if (assetFile) { @@ -38,6 +36,6 @@ void AssetDumperLocalizeEntry::DumpPool(AssetDumpingContext& context, AssetPool< } else { - printf("Could not create string file for dumping localized strings of zone '%s'\n", context.m_zone->m_name.c_str()); + std::cerr << std::format("Could not create string file for dumping localized strings of zone '{}'\n", context.m_zone.m_name); } } diff --git a/src/ObjWriting/Game/T6/ObjWriterT6.cpp b/src/ObjWriting/Game/T6/ObjWriterT6.cpp index 7f344eed6..0682c53ea 100644 --- a/src/ObjWriting/Game/T6/ObjWriterT6.cpp +++ b/src/ObjWriting/Game/T6/ObjWriterT6.cpp @@ -37,7 +37,7 @@ bool ObjWriter::DumpZone(AssetDumpingContext& context) const dumper.DumpPool(context, assetPools->poolName.get()); \ } - const auto* assetPools = dynamic_cast(context.m_zone->m_pools.get()); + const auto* assetPools = dynamic_cast(context.m_zone.m_pools.get()); DUMP_ASSET_POOL(AssetDumperPhysPreset, m_phys_preset, ASSET_TYPE_PHYSPRESET) DUMP_ASSET_POOL(AssetDumperPhysConstraints, m_phys_constraints, ASSET_TYPE_PHYSCONSTRAINTS) diff --git a/src/ObjWriting/XModel/XModelDumper.cpp.template b/src/ObjWriting/XModel/XModelDumper.cpp.template index f3644849a..971e5149c 100644 --- a/src/ObjWriting/XModel/XModelDumper.cpp.template +++ b/src/ObjWriting/XModel/XModelDumper.cpp.template @@ -204,8 +204,8 @@ namespace GAME for (auto boneNum = 0u; boneNum < model->numBones; boneNum++) { XModelBone bone; - if (model->boneNames[boneNum] < context.m_zone->m_script_strings.Count()) - bone.name = context.m_zone->m_script_strings[model->boneNames[boneNum]]; + if (model->boneNames[boneNum] < context.m_zone.m_script_strings.Count()) + bone.name = context.m_zone.m_script_strings[model->boneNames[boneNum]]; else bone.name = "INVALID_BONE_NAME"; @@ -543,7 +543,7 @@ namespace GAME if (!mtlFile) return; - const auto writer = obj::CreateMtlWriter(*mtlFile, context.m_zone->m_game->GetShortName(), context.m_zone->m_name); + const auto writer = obj::CreateMtlWriter(*mtlFile, context.m_zone.m_game->GetShortName(), context.m_zone.m_name); DistinctMapper materialMapper(model->numsurfs); writer->Write(common); @@ -558,7 +558,7 @@ namespace GAME return; const auto writer = - obj::CreateObjWriter(*assetFile, std::format("{}.mtl", model->name), context.m_zone->m_game->GetShortName(), context.m_zone->m_name); + obj::CreateObjWriter(*assetFile, std::format("{}.mtl", model->name), context.m_zone.m_game->GetShortName(), context.m_zone.m_name); DistinctMapper materialMapper(model->numsurfs); writer->Write(common); @@ -572,7 +572,7 @@ namespace GAME if (!assetFile) return; - const auto writer = xmodel_export::CreateWriterForVersion6(*assetFile, context.m_zone->m_game->GetShortName(), context.m_zone->m_name); + const auto writer = xmodel_export::CreateWriterForVersion6(*assetFile, context.m_zone.m_game->GetShortName(), context.m_zone.m_name); writer->Write(common); } @@ -587,7 +587,7 @@ namespace GAME return; const auto output = std::make_unique(*assetFile); - const auto writer = gltf::Writer::CreateWriter(output.get(), context.m_zone->m_game->GetShortName(), context.m_zone->m_name); + const auto writer = gltf::Writer::CreateWriter(output.get(), context.m_zone.m_game->GetShortName(), context.m_zone.m_name); writer->Write(common); } diff --git a/src/Unlinker/Unlinker.cpp b/src/Unlinker/Unlinker.cpp index 5eae8e335..d21c1e2fa 100644 --- a/src/Unlinker/Unlinker.cpp +++ b/src/Unlinker/Unlinker.cpp @@ -6,6 +6,7 @@ #include "IObjWriter.h" #include "ObjWriting.h" #include "SearchPath/IWD.h" +#include "SearchPath/OutputPathFilesystem.h" #include "SearchPath/SearchPathFilesystem.h" #include "SearchPath/SearchPaths.h" #include "UnlinkerArgs.h" @@ -100,14 +101,14 @@ class Unlinker::Impl void UpdateAssetIncludesAndExcludes(const AssetDumpingContext& context) const { - const auto assetTypeCount = context.m_zone->m_pools->GetAssetTypeCount(); + const auto assetTypeCount = context.m_zone.m_pools->GetAssetTypeCount(); ObjWriting::Configuration.AssetTypesToHandleBitfield = std::vector(assetTypeCount); std::vector handledSpecifiedAssets(m_args.m_specified_asset_types.size()); for (auto i = 0; i < assetTypeCount; i++) { - const auto assetTypeName = std::string(*context.m_zone->m_pools->GetAssetTypeName(i)); + const auto assetTypeName = std::string(*context.m_zone.m_pools->GetAssetTypeName(i)); const auto foundSpecifiedEntry = m_args.m_specified_asset_type_map.find(assetTypeName); if (foundSpecifiedEntry != m_args.m_specified_asset_type_map.end()) @@ -137,7 +138,7 @@ class Unlinker::Impl auto first = true; for (auto i = 0; i < assetTypeCount; i++) { - const auto assetTypeName = std::string(*context.m_zone->m_pools->GetAssetTypeName(i)); + const auto assetTypeName = std::string(*context.m_zone.m_pools->GetAssetTypeName(i)); if (first) first = false; @@ -164,7 +165,8 @@ class Unlinker::Impl } else if (m_args.m_task == UnlinkerArgs::ProcessingTask::DUMP) { - const auto outputFolderPath = m_args.GetOutputFolderPathForZone(zone); + const auto outputFolderPathStr = m_args.GetOutputFolderPathForZone(zone); + const fs::path outputFolderPath(outputFolderPathStr); fs::create_directories(outputFolderPath); fs::path zoneDefinitionFileFolder(outputFolderPath); @@ -174,12 +176,10 @@ class Unlinker::Impl if (!WriteZoneDefinitionFile(zone, zoneDefinitionFileFolder)) return false; - std::ofstream gdtStream; - AssetDumpingContext context; - context.m_zone = &zone; - context.m_base_path = outputFolderPath; - context.m_obj_search_path = &searchPath; + OutputPathFilesystem outputFolderOutputPath(outputFolderPath); + AssetDumpingContext context(zone, outputFolderPathStr, outputFolderOutputPath, searchPath); + std::ofstream gdtStream; if (m_args.m_use_gdt) { if (!OpenGdtFile(zone, outputFolderPath, gdtStream))