ZoneLoading: Extract all obj dumping relevant parts to ObjWriting component

This commit is contained in:
Jan
2019-12-25 14:07:24 +01:00
parent e8de3a3f39
commit 00b3322cb2
28 changed files with 143 additions and 37 deletions

View File

@ -0,0 +1,39 @@
#pragma once
#include "IAssetDumper.h"
#include "Utils/FileAPI.h"
#include "Utils/PathUtils.h"
#include <cstdio>
template<class T>
class AbstractAssetDumper : public IAssetDumper<T>
{
protected:
virtual std::string GetFileNameForAsset(Zone* zone, T* asset) = 0;
virtual void DumpAsset(Zone* zone, T* asset, FileAPI::File* out) = 0;
public:
void DumpPool(Zone* zone, AssetPool<T>* pool, const std::string& basePath) override
{
for(auto assetInfo : *pool)
{
std::string assetFilePath = utils::Path::Combine(basePath, GetFileNameForAsset(zone, assetInfo->m_asset));
FileAPI::DirectoryCreate(utils::Path::GetDirectory(assetFilePath));
auto file = FileAPI::Open(assetFilePath, FileAPI::Mode::MODE_WRITE);
if(file.IsOpen())
{
DumpAsset(zone, assetInfo->m_asset, &file);
file.Close();
}
else
{
printf("Failed to open file '%s' to dump asset '%s'\n", assetFilePath.c_str(), assetInfo->m_name.c_str());
}
}
}
};

View File

@ -0,0 +1,13 @@
#pragma once
#include "Zone/Zone.h"
#include "Pool/AssetPool.h"
template<class T>
class IAssetDumper
{
public:
virtual ~IAssetDumper() = default;
virtual void DumpPool(Zone* zone, AssetPool<T>* pool, const std::string& basePath) = 0;
};

View File

@ -0,0 +1,14 @@
#pragma once
#include "Zone/Zone.h"
#include "Utils/FileAPI.h"
class IZoneDumper
{
public:
virtual ~IZoneDumper() = default;
virtual bool CanHandleZone(Zone* zone) = 0;
virtual bool DumpZone(Zone* zone, const std::string& basePath) = 0;
virtual bool WriteZoneDefinition(Zone* zone, FileAPI::File* file, bool minimalistic) = 0;
};

View File

@ -0,0 +1,63 @@
#include "StringFileDumper.h"
#include <regex>
StringFileDumper::StringFileDumper(Zone* zone, FileAPI::File* file)
{
m_zone = zone;
m_file = file;
m_config_file = "";
m_notes = "";
m_language_caps = "ENGLISH";
m_wrote_header = false;
}
void StringFileDumper::SetLanguageName(std::string language)
{
m_language_caps = std::move(language);
for (auto& c : m_language_caps) c = toupper(c);
}
void StringFileDumper::SetConfigFile(std::string configFile)
{
m_config_file = std::move(configFile);
}
void StringFileDumper::SetNotes(std::string notes)
{
m_notes = std::move(notes);
}
void StringFileDumper::WriteHeader()
{
m_file->Printf("// Dumped from fastfile \"%s\".\n", m_zone->m_name.c_str());
m_file->Printf("// In their original format the strings might have been separated in multiple files.\n");
m_file->Printf("VERSION \"1\"\n");
m_file->Printf("CONFIG \"%s\"\n", m_config_file.c_str());
m_file->Printf("FILENOTES \"%s\"\n", m_notes.c_str());
m_wrote_header = true;
}
void StringFileDumper::WriteLocalizeEntry(const std::string& reference, const std::string& value)
{
if (!m_wrote_header)
WriteHeader();
m_file->Printf("\n");
m_file->Printf("REFERENCE %s\n", reference.c_str());
const std::string escapedValue = std::regex_replace(value, std::regex("\n"), "\\n");
const std::string valueSpacing = std::string(15 - m_language_caps.length(), ' ');
m_file->Printf("LANG_%s%s\"%s\"\n", m_language_caps.c_str(), valueSpacing.c_str(), escapedValue.c_str());
}
void StringFileDumper::Finalize()
{
if (!m_wrote_header)
WriteHeader();
m_file->Printf("\nENDMARKER");
}

View File

@ -0,0 +1,29 @@
#pragma once
#include "Zone/Zone.h"
#include "Utils/FileAPI.h"
class StringFileDumper
{
Zone* m_zone;
FileAPI::File* m_file;
std::string m_config_file;
std::string m_notes;
std::string m_language_caps;
bool m_wrote_header;
void WriteHeader();
public:
StringFileDumper(Zone* zone, FileAPI::File* file);
void SetConfigFile(std::string configFile);
void SetNotes(std::string notes);
void SetLanguageName(std::string language);
void WriteLocalizeEntry(const std::string& reference, const std::string& value);
void Finalize();
};