2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-03-18 19:03:03 +00:00

refactor: streamline IW4 asset loading

This commit is contained in:
Jan Laupetin
2025-08-05 01:13:58 +02:00
parent 81a67151b5
commit 6806337f46
49 changed files with 398 additions and 397 deletions

View File

@@ -10,26 +10,43 @@
using namespace IW4;
GdtLoaderPhysPreset::GdtLoaderPhysPreset(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone)
: m_memory(memory),
m_gdt(gdt),
m_zone(zone)
namespace
{
}
AssetCreationResult GdtLoaderPhysPreset::CreateAsset(const std::string& assetName, AssetCreationContext& context)
{
auto* gdtEntry = m_gdt.GetGdtEntryByGdfAndName(ObjConstants::GDF_FILENAME_PHYS_PRESET, assetName);
if (gdtEntry == nullptr)
return AssetCreationResult::NoAction();
InfoString infoString;
if (!infoString.FromGdtProperties(*gdtEntry))
class GdtLoaderPhysPreset final : public AssetCreator<AssetPhysPreset>
{
std::cerr << std::format("Failed to read phys preset gdt entry: \"{}\"\n", assetName);
return AssetCreationResult::Failure();
}
public:
GdtLoaderPhysPreset(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone)
: m_gdt(gdt),
m_info_string_loader(memory, zone)
{
}
InfoStringLoaderPhysPreset infoStringLoader(m_memory, m_zone);
return infoStringLoader.CreateAsset(assetName, infoString, context);
}
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override
{
const auto* gdtEntry = m_gdt.GetGdtEntryByGdfAndName(ObjConstants::GDF_FILENAME_PHYS_PRESET, assetName);
if (gdtEntry == nullptr)
return AssetCreationResult::NoAction();
InfoString infoString;
if (!infoString.FromGdtProperties(*gdtEntry))
{
std::cerr << std::format("Failed to read phys preset gdt entry: \"{}\"\n", assetName);
return AssetCreationResult::Failure();
}
return m_info_string_loader.CreateAsset(assetName, infoString, context);
}
private:
IGdtQueryable& m_gdt;
IW4::phys_preset::InfoStringLoader m_info_string_loader;
};
} // namespace
namespace IW4::phys_preset
{
std::unique_ptr<AssetCreator<AssetPhysPreset>> CreateGdtLoader(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone)
{
return std::make_unique<GdtLoaderPhysPreset>(memory, gdt, zone);
}
} // namespace IW4::phys_preset

View File

@@ -3,20 +3,12 @@
#include "Asset/IAssetCreator.h"
#include "Game/IW4/IW4.h"
#include "Gdt/IGdtQueryable.h"
#include "SearchPath/ISearchPath.h"
#include "Utils/MemoryManager.h"
namespace IW4
#include <memory>
namespace IW4::phys_preset
{
class GdtLoaderPhysPreset final : public AssetCreator<AssetPhysPreset>
{
public:
GdtLoaderPhysPreset(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone);
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override;
private:
MemoryManager& m_memory;
IGdtQueryable& m_gdt;
Zone& m_zone;
};
} // namespace IW4
std::unique_ptr<AssetCreator<AssetPhysPreset>> CreateGdtLoader(MemoryManager& memory, IGdtQueryable& gdt, Zone& zone);
} // namespace IW4::phys_preset

View File

@@ -58,30 +58,33 @@ namespace
}
} // namespace
InfoStringLoaderPhysPreset::InfoStringLoaderPhysPreset(MemoryManager& memory, Zone& zone)
: m_memory(memory),
m_zone(zone)
namespace IW4::phys_preset
{
}
AssetCreationResult InfoStringLoaderPhysPreset::CreateAsset(const std::string& assetName, const InfoString& infoString, AssetCreationContext& context)
{
PhysPresetInfo presetInfo;
std::memset(&presetInfo, 0, sizeof(presetInfo));
auto* physPreset = m_memory.Alloc<PhysPreset>();
AssetRegistration<AssetPhysPreset> registration(assetName, physPreset);
InfoStringToPhysPresetConverter converter(
infoString, &presetInfo, m_zone.m_script_strings, m_memory, context, registration, phys_preset_fields, std::extent_v<decltype(phys_preset_fields)>);
if (!converter.Convert())
InfoStringLoader::InfoStringLoader(MemoryManager& memory, Zone& zone)
: m_memory(memory),
m_zone(zone)
{
std::cerr << std::format("Failed to parse phys preset: \"{}\"\n", assetName);
return AssetCreationResult::Failure();
}
CopyFromPhysPresetInfo(presetInfo, *physPreset);
physPreset->name = m_memory.Dup(assetName.c_str());
AssetCreationResult InfoStringLoader::CreateAsset(const std::string& assetName, const InfoString& infoString, AssetCreationContext& context)
{
PhysPresetInfo presetInfo;
std::memset(&presetInfo, 0, sizeof(presetInfo));
return AssetCreationResult::Success(context.AddAsset(std::move(registration)));
}
auto* physPreset = m_memory.Alloc<PhysPreset>();
AssetRegistration<AssetPhysPreset> registration(assetName, physPreset);
InfoStringToPhysPresetConverter converter(
infoString, &presetInfo, m_zone.m_script_strings, m_memory, context, registration, phys_preset_fields, std::extent_v<decltype(phys_preset_fields)>);
if (!converter.Convert())
{
std::cerr << std::format("Failed to parse phys preset: \"{}\"\n", assetName);
return AssetCreationResult::Failure();
}
CopyFromPhysPresetInfo(presetInfo, *physPreset);
physPreset->name = m_memory.Dup(assetName.c_str());
return AssetCreationResult::Success(context.AddAsset(std::move(registration)));
}
} // namespace IW4::phys_preset

View File

@@ -4,12 +4,12 @@
#include "Asset/AssetCreationResult.h"
#include "InfoString/InfoString.h"
namespace IW4
namespace IW4::phys_preset
{
class InfoStringLoaderPhysPreset
class InfoStringLoader
{
public:
InfoStringLoaderPhysPreset(MemoryManager& memory, Zone& zone);
InfoStringLoader(MemoryManager& memory, Zone& zone);
AssetCreationResult CreateAsset(const std::string& assetName, const InfoString& infoString, AssetCreationContext& context);
@@ -17,4 +17,4 @@ namespace IW4
MemoryManager& m_memory;
Zone& m_zone;
};
} // namespace IW4
} // namespace IW4::phys_preset

View File

@@ -10,28 +10,46 @@
#include <iostream>
using namespace IW4;
using namespace ::phys_preset;
RawLoaderPhysPreset::RawLoaderPhysPreset(MemoryManager& memory, ISearchPath& searchPath, Zone& zone)
: m_memory(memory),
m_search_path(searchPath),
m_zone(zone)
namespace
{
}
AssetCreationResult RawLoaderPhysPreset::CreateAsset(const std::string& assetName, AssetCreationContext& context)
{
const auto fileName = phys_preset::GetFileNameForAssetName(assetName);
const auto file = m_search_path.Open(fileName);
if (!file.IsOpen())
return AssetCreationResult::NoAction();
InfoString infoString;
if (!infoString.FromStream(ObjConstants::INFO_STRING_PREFIX_PHYS_PRESET, *file.m_stream))
class RawLoaderPhysPreset final : public AssetCreator<AssetPhysPreset>
{
std::cerr << std::format("Could not parse as info string file: \"{}\"\n", fileName);
return AssetCreationResult::Failure();
}
public:
RawLoaderPhysPreset(MemoryManager& memory, ISearchPath& searchPath, Zone& zone)
: m_search_path(searchPath),
m_info_string_loader(memory, zone)
{
}
InfoStringLoaderPhysPreset infoStringLoader(m_memory, m_zone);
return infoStringLoader.CreateAsset(assetName, infoString, context);
}
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override
{
const auto fileName = GetFileNameForAssetName(assetName);
const auto file = m_search_path.Open(fileName);
if (!file.IsOpen())
return AssetCreationResult::NoAction();
InfoString infoString;
if (!infoString.FromStream(ObjConstants::INFO_STRING_PREFIX_PHYS_PRESET, *file.m_stream))
{
std::cerr << std::format("Could not parse as info string file: \"{}\"\n", fileName);
return AssetCreationResult::Failure();
}
return m_info_string_loader.CreateAsset(assetName, infoString, context);
}
private:
ISearchPath& m_search_path;
IW4::phys_preset::InfoStringLoader m_info_string_loader;
};
} // namespace
namespace IW4::phys_preset
{
std::unique_ptr<AssetCreator<AssetPhysPreset>> CreateRawLoader(MemoryManager& memory, ISearchPath& searchPath, Zone& zone)
{
return std::make_unique<RawLoaderPhysPreset>(memory, searchPath, zone);
}
} // namespace IW4::phys_preset

View File

@@ -5,18 +5,9 @@
#include "SearchPath/ISearchPath.h"
#include "Utils/MemoryManager.h"
namespace IW4
#include <memory>
namespace IW4::phys_preset
{
class RawLoaderPhysPreset final : public AssetCreator<AssetPhysPreset>
{
public:
RawLoaderPhysPreset(MemoryManager& memory, ISearchPath& searchPath, Zone& zone);
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override;
private:
MemoryManager& m_memory;
ISearchPath& m_search_path;
Zone& m_zone;
};
} // namespace IW4
std::unique_ptr<AssetCreator<AssetPhysPreset>> CreateRawLoader(MemoryManager& memory, ISearchPath& searchPath, Zone& zone);
} // namespace IW4::phys_preset