Skip to content

Commit

Permalink
Merge pull request #341 from Laupetin/refactor/obj-dumping-output-path
Browse files Browse the repository at this point in the history
refactor: obj dumping output path
  • Loading branch information
Laupetin authored Jan 11, 2025
2 parents 27c01e0 + 2d58054 commit 1d39062
Show file tree
Hide file tree
Showing 25 changed files with 155 additions and 171 deletions.
14 changes: 13 additions & 1 deletion src/ObjCommon/SearchPath/OutputPathFilesystem.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "OutputPathFilesystem.h"

#include <format>
#include <fstream>
#include <iostream>

namespace fs = std::filesystem;

Expand All @@ -17,11 +19,21 @@ std::unique_ptr<std::ostream> OutputPathFilesystem::Open(const std::string& file
return nullptr;

const auto containingDirectory = fullNewPath.parent_path();
fs::create_directories(containingDirectory);

std::error_code ec;
fs::create_directories(containingDirectory, ec);
if (ec)
{
std::cerr << std::format("Failed to create folder '{}' when try to open file '{}'\n", containingDirectory.string(), fileName);
return nullptr;
}

std::ofstream stream(fullNewPath, std::ios::binary | std::ios::out);
if (!stream.is_open())
{
std::cerr << std::format("Failed to open file '{}'\n", fileName);
return nullptr;
}

return std::make_unique<std::ofstream>(std::move(stream));
}
32 changes: 6 additions & 26 deletions src/ObjWriting/Dumping/AssetDumpingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,15 @@
#include <format>
#include <fstream>

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<std::ostream> 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<std::ofstream>(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);
}
22 changes: 12 additions & 10 deletions src/ObjWriting/Dumping/AssetDumpingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <memory>
Expand All @@ -13,17 +13,10 @@

class AssetDumpingContext
{
std::unordered_map<std::type_index, std::unique_ptr<IZoneAssetDumperState>> m_zone_asset_dumper_states;

public:
Zone* m_zone;
std::string m_base_path;
std::unique_ptr<GdtOutputStream> m_gdt;
ISearchPath* m_obj_search_path;

AssetDumpingContext();
AssetDumpingContext(const Zone& zone, const std::string& basePath, IOutputPath& outputPath, ISearchPath& objSearchPath);

_NODISCARD std::unique_ptr<std::ostream> OpenAssetFile(const std::string& fileName) const;
[[nodiscard]] std::unique_ptr<std::ostream> OpenAssetFile(const std::string& fileName) const;

template<typename T> T* GetZoneAssetDumperState()
{
Expand All @@ -40,4 +33,13 @@ class AssetDumpingContext
m_zone_asset_dumper_states.emplace(std::make_pair<std::type_index, std::unique_ptr<IZoneAssetDumperState>>(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<GdtOutputStream> m_gdt;

private:
std::unordered_map<std::type_index, std::unique_ptr<IZoneAssetDumperState>> m_zone_asset_dumper_states;
};
3 changes: 2 additions & 1 deletion src/ObjWriting/Dumping/IZoneAssetDumperState.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "Zone/Zone.h"

class IZoneAssetDumperState
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions src/ObjWriting/Dumping/Localize/StringFileDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <regex>

StringFileDumper::StringFileDumper(Zone* zone, std::ostream& stream)
StringFileDumper::StringFileDumper(const Zone& zone, std::ostream& stream)
: AbstractTextDumper(stream),
m_zone(zone),
m_language_caps("ENGLISH"),
Expand All @@ -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";
Expand Down
25 changes: 13 additions & 12 deletions src/ObjWriting/Dumping/Localize/StringFileDumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
};
18 changes: 9 additions & 9 deletions src/ObjWriting/Game/IW3/AssetDumpers/AssetDumperGfxImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ using namespace IW3;

namespace
{
std::unique_ptr<Texture> LoadImageFromLoadDef(const GfxImage* image)
std::unique_ptr<Texture> 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)
Expand All @@ -36,22 +36,22 @@ namespace
return textureLoader.LoadTexture(loadDef.data);
}

std::unique_ptr<Texture> LoadImageFromIwi(const GfxImage* image, ISearchPath* searchPath)
std::unique_ptr<Texture> 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<Texture> LoadImageData(ISearchPath* searchPath, const GfxImage* image)
std::unique_ptr<Texture> 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);
Expand Down Expand Up @@ -91,7 +91,7 @@ std::string AssetDumperGfxImage::GetAssetFileName(const XAssetInfo<GfxImage>& as
void AssetDumperGfxImage::DumpAsset(AssetDumpingContext& context, XAssetInfo<GfxImage>* 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include "Dumping/Localize/StringFileDumper.h"
#include "Localize/LocalizeCommon.h"

#include <sstream>
#include <format>
#include <iostream>

using namespace IW3;

Expand All @@ -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)
{
Expand All @@ -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);
}
}
13 changes: 6 additions & 7 deletions src/ObjWriting/Game/IW3/AssetDumpers/AssetDumperXModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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<Material*> materialMapper(model->numsurfs);

writer->Write(common);
Expand All @@ -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<Material*> materialMapper(model->numsurfs);

writer->Write(common);
Expand All @@ -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);
}

Expand All @@ -470,7 +469,7 @@ namespace
return;

const auto output = std::make_unique<T>(*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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ObjWriting/Game/IW3/ObjWriterIW3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bool ObjWriter::DumpZone(AssetDumpingContext& context) const
dumper.DumpPool(context, assetPools->poolName.get()); \
}

const auto* assetPools = dynamic_cast<GameAssetPoolIW3*>(context.m_zone->m_pools.get());
const auto* assetPools = dynamic_cast<GameAssetPoolIW3*>(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)
Expand Down
20 changes: 10 additions & 10 deletions src/ObjWriting/Game/IW4/AssetDumpers/AssetDumperGfxImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ using namespace IW4;

namespace
{
std::unique_ptr<Texture> LoadImageFromLoadDef(const GfxImage* image)
std::unique_ptr<Texture> 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);
Expand All @@ -33,22 +33,22 @@ namespace
return textureLoader.LoadTexture(loadDef.data);
}

std::unique_ptr<Texture> LoadImageFromIwi(const GfxImage* image, ISearchPath* searchPath)
std::unique_ptr<Texture> 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<Texture> LoadImageData(ISearchPath* searchPath, const GfxImage* image)
std::unique_ptr<Texture> 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);
Expand Down Expand Up @@ -88,7 +88,7 @@ std::string AssetDumperGfxImage::GetAssetFileName(const XAssetInfo<GfxImage>& as
void AssetDumperGfxImage::DumpAsset(AssetDumpingContext& context, XAssetInfo<GfxImage>* 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;

Expand Down
Loading

0 comments on commit 1d39062

Please sign in to comment.