2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-09-03 23:37:26 +00:00

refactor: make use of IOutputPath in ObjWriting

This commit is contained in:
Jan
2025-01-11 13:06:48 +01:00
parent b584cd7423
commit 2d58054ffc
25 changed files with 143 additions and 171 deletions

View File

@@ -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);
}

View File

@@ -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>
@@ -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(const Zone& zone, const std::string& basePath, IOutputPath& outputPath, ISearchPath& objSearchPath);
AssetDumpingContext();
_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()
{
@@ -40,4 +33,13 @@ public:
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;
};

View File

@@ -1,4 +1,5 @@
#pragma once
#include "Zone/Zone.h"
class IZoneAssetDumperState
@@ -13,7 +14,7 @@ public:
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
}

View File

@@ -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"),
@@ -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";

View File

@@ -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 @@ public:
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;
};