Skip to content

Commit

Permalink
Merge pull request #347 from Laupetin/refactor/outpath-search-path
Browse files Browse the repository at this point in the history
refactor: use OutputPathFilesystem for writing fastfiles
  • Loading branch information
Laupetin authored Jan 14, 2025
2 parents a364e63 + cc67f6e commit a1d3e64
Show file tree
Hide file tree
Showing 34 changed files with 220 additions and 227 deletions.
28 changes: 14 additions & 14 deletions src/Linker/Linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "LinkerPaths.h"
#include "ObjContainer/SoundBank/SoundBankWriter.h"
#include "ObjWriting.h"
#include "SearchPath/OutputPathFilesystem.h"
#include "SearchPath/SearchPaths.h"
#include "Utils/ObjFileStream.h"
#include "Zone/AssetList/AssetList.h"
Expand Down Expand Up @@ -268,42 +269,41 @@ class LinkerImpl final : public Linker
return zone_creator::CreateZoneForDefinition(zoneDefinition.m_game, context);
}

bool WriteZoneToFile(const LinkerPathManager& paths, const fs::path& outDir, const std::string& projectName, Zone* zone) const
static bool WriteZoneToFile(IOutputPath& outPath, const Zone& zone)
{
auto zoneFilePath(outDir);
zoneFilePath.append(std::format("{}.ff", zone->m_name));

fs::create_directories(outDir);

std::ofstream stream(zoneFilePath, std::fstream::out | std::fstream::binary);
if (!stream.is_open())
const auto stream = outPath.Open(std::format("{}.ff", zone.m_name));
if (!stream)
{
std::cerr << std::format("Failed to open file for zone: {}\n", zone.m_name);
return false;
}

std::cout << std::format("Building zone \"{}\"\n", zoneFilePath.string());
std::cout << std::format("Building zone \"{}\"\n", zone.m_name);

if (!ZoneWriting::WriteZone(stream, zone))
if (!ZoneWriting::WriteZone(*stream, zone))
{
std::cerr << "Writing zone failed.\n";
stream.close();
return false;
}

std::cout << std::format("Created zone \"{}\"\n", zoneFilePath.string());
std::cout << std::format("Created zone \"{}\"\n", zone.m_name);

stream.close();
return true;
}

bool BuildFastFile(LinkerPathManager& paths, const std::string& projectName, const std::string& targetName, ZoneDefinition& zoneDefinition) const
{
const fs::path outDir(paths.m_linker_paths->BuildOutputFolderPath(projectName, zoneDefinition.m_game));

OutputPathFilesystem outputPath(outDir);

const fs::path cacheDir(paths.m_linker_paths->BuildCacheFolderPath(projectName, zoneDefinition.m_game));
SoundBankWriter::OutputPath = outDir;

const auto zone = CreateZoneForDefinition(paths, outDir, cacheDir, targetName, zoneDefinition);
auto result = zone != nullptr;
if (zone)
result = WriteZoneToFile(paths, outDir, projectName, zone.get());
result = WriteZoneToFile(outputPath, *zone);

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
#include "Internal/BaseTemplate.h"

#include <cassert>
#include <cstdint>
#include <iostream>
#include <sstream>

class ZoneWriteTemplate::Internal final : BaseTemplate
{
enum class MemberWriteType
enum class MemberWriteType : std::uint8_t
{
ARRAY_POINTER,
DYNAMIC_ARRAY,
Expand Down Expand Up @@ -86,7 +87,7 @@ class ZoneWriteTemplate::Internal final : BaseTemplate

void PrintHeaderConstructor() const
{
LINE(WriterClassName(m_env.m_asset) << "(" << m_env.m_asset->m_definition->GetFullName() << "* asset, Zone* zone, IZoneOutputStream* stream);")
LINE(WriterClassName(m_env.m_asset) << "(" << m_env.m_asset->m_definition->GetFullName() << "* asset, const Zone& zone, IZoneOutputStream& stream);")
}

void PrintVariableInitialization(const DataDefinition* def) const
Expand All @@ -112,11 +113,11 @@ class ZoneWriteTemplate::Internal final : BaseTemplate
void PrintConstructorMethod()
{
LINE(WriterClassName(m_env.m_asset) << "::" << WriterClassName(m_env.m_asset) << "(" << m_env.m_asset->m_definition->GetFullName()
<< "* asset, Zone* zone, IZoneOutputStream* stream)")
<< "* asset, const Zone& zone, IZoneOutputStream& stream)")

m_intendation++;
LINE_START(": AssetWriter(zone->m_pools->GetAssetOrAssetReference(" << m_env.m_asset->m_asset_enum_entry->m_name << ", GetAssetName(asset))"
<< ", zone, stream)")
LINE_START(": AssetWriter(zone.m_pools->GetAssetOrAssetReference(" << m_env.m_asset->m_asset_enum_entry->m_name << ", GetAssetName(asset))"
<< ", zone, stream)")
LINE_END("")
m_intendation--;

Expand Down Expand Up @@ -184,7 +185,7 @@ class ZoneWriteTemplate::Internal final : BaseTemplate
{
if (writeType == MemberWriteType::SINGLE_POINTER)
{
LINE(WriterClassName(member->m_type) << " writer(" << MakeMemberAccess(info, member, modifier) << ", m_zone, m_stream);")
LINE(WriterClassName(member->m_type) << " writer(" << MakeMemberAccess(info, member, modifier) << ", m_zone, *m_stream);")
LINE("writer.Write(&" << MakeWrittenMemberAccess(info, member, modifier) << ");")
}
else if (writeType == MemberWriteType::POINTER_ARRAY)
Expand Down Expand Up @@ -1005,7 +1006,7 @@ class ZoneWriteTemplate::Internal final : BaseTemplate

if (info && StructureComputations(info).IsAsset())
{
LINE(WriterClassName(info) << " writer(*" << MakeTypePtrVarName(def) << ", m_zone, m_stream);")
LINE(WriterClassName(info) << " writer(*" << MakeTypePtrVarName(def) << ", m_zone, *m_stream);")
LINE("writer.Write(" << MakeTypeWrittenPtrVarName(def) << ");")
}
else
Expand Down
36 changes: 17 additions & 19 deletions src/ZoneWriting/Game/IW3/ContentWriterIW3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,44 @@
#include "Writing/WritingException.h"

#include <cassert>
#include <sstream>
#include <format>

using namespace IW3;

ContentWriter::ContentWriter()
: varXAssetList(nullptr),
ContentWriter::ContentWriter(const Zone& zone)
: ContentWriterBase(zone),
varXAssetList(nullptr),
varXAsset(nullptr),
varScriptStringList(nullptr)
{
}

void ContentWriter::CreateXAssetList(XAssetList& xAssetList, MemoryManager& memory) const
{
if (!m_zone->m_script_strings.Empty())
if (!m_zone.m_script_strings.Empty())
{
assert(m_zone->m_script_strings.Count() <= SCR_STRING_MAX + 1);
xAssetList.stringList.count = static_cast<int>(m_zone->m_script_strings.Count());
xAssetList.stringList.strings = memory.Alloc<const char*>(m_zone->m_script_strings.Count());
assert(m_zone.m_script_strings.Count() <= SCR_STRING_MAX + 1);
xAssetList.stringList.count = static_cast<int>(m_zone.m_script_strings.Count());
xAssetList.stringList.strings = memory.Alloc<const char*>(m_zone.m_script_strings.Count());

for (auto i = 0u; i < m_zone->m_script_strings.Count(); i++)
xAssetList.stringList.strings[i] = m_zone->m_script_strings.CValue(i);
for (auto i = 0u; i < m_zone.m_script_strings.Count(); i++)
xAssetList.stringList.strings[i] = m_zone.m_script_strings.CValue(i);
}
else
{
xAssetList.stringList.count = 0;
xAssetList.stringList.strings = nullptr;
}

const auto assetCount = m_zone->m_pools->GetTotalAssetCount();
const auto assetCount = m_zone.m_pools->GetTotalAssetCount();
if (assetCount > 0)
{
xAssetList.assetCount = static_cast<int>(assetCount);
xAssetList.assets = memory.Alloc<XAsset>(assetCount);

const auto end = m_zone->m_pools->end();
const auto end = m_zone.m_pools->end();
auto index = 0u;
for (auto i = m_zone->m_pools->begin(); i != end; ++i)
for (auto i = m_zone.m_pools->begin(); i != end; ++i)
{
auto& asset = xAssetList.assets[index++];
asset.type = static_cast<XAssetType>((*i)->m_type);
Expand Down Expand Up @@ -102,7 +103,7 @@ void ContentWriter::WriteXAsset(const bool atStreamStart)
#define WRITE_ASSET(type_index, typeName, headerEntry) \
case type_index: \
{ \
Writer_##typeName writer(varXAsset->header.headerEntry, m_zone, m_stream); \
Writer_##typeName writer(varXAsset->header.headerEntry, m_zone, *m_stream); \
writer.Write(&varXAsset->header.headerEntry); \
break; \
}
Expand Down Expand Up @@ -147,9 +148,7 @@ void ContentWriter::WriteXAsset(const bool atStreamStart)

default:
{
std::ostringstream str;
str << "Unsupported asset type: " << varXAsset->type << ".";
throw WritingException(str.str());
throw WritingException(std::format("Unsupported asset type: {}.", static_cast<unsigned>(varXAsset->type)));
}
}

Expand All @@ -171,10 +170,9 @@ void ContentWriter::WriteXAssetArray(const bool atStreamStart, const size_t coun
}
}

void ContentWriter::WriteContent(Zone* zone, IZoneOutputStream* stream)
void ContentWriter::WriteContent(IZoneOutputStream& stream)
{
m_zone = zone;
m_stream = stream;
m_stream = &stream;

m_stream->PushBlock(XFILE_BLOCK_VIRTUAL);

Expand Down
15 changes: 8 additions & 7 deletions src/ZoneWriting/Game/IW3/ContentWriterIW3.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ namespace IW3
{
class ContentWriter final : public ContentWriterBase, public IContentWritingEntryPoint
{
XAssetList* varXAssetList;
XAsset* varXAsset;
ScriptStringList* varScriptStringList;
public:
explicit ContentWriter(const Zone& zone);

void WriteContent(IZoneOutputStream& stream) override;

private:
void CreateXAssetList(XAssetList& xAssetList, MemoryManager& memory) const;

void WriteScriptStringList(bool atStreamStart);

void WriteXAsset(bool atStreamStart);
void WriteXAssetArray(bool atStreamStart, size_t count);

public:
ContentWriter();

void WriteContent(Zone* zone, IZoneOutputStream* stream) override;
XAssetList* varXAssetList;
XAsset* varXAsset;
ScriptStringList* varScriptStringList;
};
} // namespace IW3
4 changes: 2 additions & 2 deletions src/ZoneWriting/Game/IW3/ZoneWriterFactoryIW3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ namespace
}
} // namespace

std::unique_ptr<ZoneWriter> ZoneWriterFactory::CreateWriter(Zone* zone) const
std::unique_ptr<ZoneWriter> ZoneWriterFactory::CreateWriter(const Zone& zone) const
{
auto writer = std::make_unique<ZoneWriter>();

SetupBlocks(*writer);

auto contentInMemory = std::make_unique<StepWriteZoneContentToMemory>(
std::make_unique<ContentWriter>(), zone, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK);
std::make_unique<ContentWriter>(zone), zone, ZoneConstants::OFFSET_BLOCK_BIT_COUNT, ZoneConstants::INSERT_BLOCK);
auto* contentInMemoryPtr = contentInMemory.get();
writer->AddWritingStep(std::move(contentInMemory));

Expand Down
2 changes: 1 addition & 1 deletion src/ZoneWriting/Game/IW3/ZoneWriterFactoryIW3.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ namespace IW3
class ZoneWriterFactory final : public IZoneWriterFactory
{
public:
_NODISCARD std::unique_ptr<ZoneWriter> CreateWriter(Zone* zone) const override;
[[nodiscard]] std::unique_ptr<ZoneWriter> CreateWriter(const Zone& zone) const override;
};
} // namespace IW3
36 changes: 17 additions & 19 deletions src/ZoneWriting/Game/IW4/ContentWriterIW4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,43 +38,44 @@
#include "Writing/WritingException.h"

#include <cassert>
#include <sstream>
#include <format>

using namespace IW4;

ContentWriter::ContentWriter()
: varXAssetList(nullptr),
ContentWriter::ContentWriter(const Zone& zone)
: ContentWriterBase(zone),
varXAssetList(nullptr),
varXAsset(nullptr),
varScriptStringList(nullptr)
{
}

void ContentWriter::CreateXAssetList(XAssetList& xAssetList, MemoryManager& memory) const
{
if (!m_zone->m_script_strings.Empty())
if (!m_zone.m_script_strings.Empty())
{
assert(m_zone->m_script_strings.Count() <= SCR_STRING_MAX + 1);
xAssetList.stringList.count = static_cast<int>(m_zone->m_script_strings.Count());
xAssetList.stringList.strings = memory.Alloc<const char*>(m_zone->m_script_strings.Count());
assert(m_zone.m_script_strings.Count() <= SCR_STRING_MAX + 1);
xAssetList.stringList.count = static_cast<int>(m_zone.m_script_strings.Count());
xAssetList.stringList.strings = memory.Alloc<const char*>(m_zone.m_script_strings.Count());

for (auto i = 0u; i < m_zone->m_script_strings.Count(); i++)
xAssetList.stringList.strings[i] = m_zone->m_script_strings.CValue(i);
for (auto i = 0u; i < m_zone.m_script_strings.Count(); i++)
xAssetList.stringList.strings[i] = m_zone.m_script_strings.CValue(i);
}
else
{
xAssetList.stringList.count = 0;
xAssetList.stringList.strings = nullptr;
}

const auto assetCount = m_zone->m_pools->GetTotalAssetCount();
const auto assetCount = m_zone.m_pools->GetTotalAssetCount();
if (assetCount > 0)
{
xAssetList.assetCount = static_cast<int>(assetCount);
xAssetList.assets = memory.Alloc<XAsset>(assetCount);

const auto end = m_zone->m_pools->end();
const auto end = m_zone.m_pools->end();
auto index = 0u;
for (auto i = m_zone->m_pools->begin(); i != end; ++i)
for (auto i = m_zone.m_pools->begin(); i != end; ++i)
{
auto& asset = xAssetList.assets[index++];
asset.type = static_cast<XAssetType>((*i)->m_type);
Expand Down Expand Up @@ -112,7 +113,7 @@ void ContentWriter::WriteXAsset(const bool atStreamStart)
#define WRITE_ASSET(type_index, typeName, headerEntry) \
case type_index: \
{ \
Writer_##typeName writer(varXAsset->header.headerEntry, m_zone, m_stream); \
Writer_##typeName writer(varXAsset->header.headerEntry, m_zone, *m_stream); \
writer.Write(&varXAsset->header.headerEntry); \
break; \
}
Expand Down Expand Up @@ -167,9 +168,7 @@ void ContentWriter::WriteXAsset(const bool atStreamStart)

default:
{
std::ostringstream str;
str << "Unsupported asset type: " << varXAsset->type << ".";
throw WritingException(str.str());
throw WritingException(std::format("Unsupported asset type: {}.", static_cast<unsigned>(varXAsset->type)));
}
}

Expand All @@ -191,10 +190,9 @@ void ContentWriter::WriteXAssetArray(const bool atStreamStart, const size_t coun
}
}

void ContentWriter::WriteContent(Zone* zone, IZoneOutputStream* stream)
void ContentWriter::WriteContent(IZoneOutputStream& stream)
{
m_zone = zone;
m_stream = stream;
m_stream = &stream;

m_stream->PushBlock(XFILE_BLOCK_VIRTUAL);

Expand Down
15 changes: 8 additions & 7 deletions src/ZoneWriting/Game/IW4/ContentWriterIW4.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ namespace IW4
{
class ContentWriter final : public ContentWriterBase, public IContentWritingEntryPoint
{
XAssetList* varXAssetList;
XAsset* varXAsset;
ScriptStringList* varScriptStringList;
public:
explicit ContentWriter(const Zone& zone);

void WriteContent(IZoneOutputStream& stream) override;

private:
void CreateXAssetList(XAssetList& xAssetList, MemoryManager& memory) const;

void WriteScriptStringList(bool atStreamStart);

void WriteXAsset(bool atStreamStart);
void WriteXAssetArray(bool atStreamStart, size_t count);

public:
ContentWriter();

void WriteContent(Zone* zone, IZoneOutputStream* stream) override;
XAssetList* varXAssetList;
XAsset* varXAsset;
ScriptStringList* varScriptStringList;
};
} // namespace IW4
Loading

0 comments on commit a1d3e64

Please sign in to comment.