diff --git a/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderTracer.cpp b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderTracer.cpp new file mode 100644 index 00000000..c29448f9 --- /dev/null +++ b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderTracer.cpp @@ -0,0 +1,86 @@ +#include "AssetLoaderTracer.h" + +#include +#include +#include + +#include "Game/T6/ObjConstantsT6.h" +#include "Game/T6/T6.h" +#include "Game/T6/InfoString/EnumStrings.h" +#include "Game/T6/InfoString/InfoStringToStructConverter.h" +#include "Game/T6/InfoString/TracerFields.h" +#include "InfoString/InfoString.h" + +using namespace T6; + +namespace T6 +{ + class InfoStringToTracerConverter final : public InfoStringToStructConverter + { + protected: + bool ConvertExtensionField(const cspField_t& field, const std::string& value) override + { + switch (static_cast(field.iFieldType)) + { + case TFT_TRACERTYPE: + return ConvertEnumInt(value, field.iOffset, tracerTypeNames, std::extent::value); + + case TFT_NUM_FIELD_TYPES: + default: + assert(false); + return false; + } + } + + public: + InfoStringToTracerConverter(const InfoString& infoString, TracerDef* tracer, ZoneScriptStrings& zoneScriptStrings, MemoryManager* memory, IAssetLoadingManager* manager, + const cspField_t* fields, const size_t fieldCount) + : InfoStringToStructConverter(infoString, tracer, zoneScriptStrings, memory, manager, fields, fieldCount) + { + } + }; +} + +void* AssetLoaderTracer::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) +{ + auto* tracer = memory->Create(); + memset(tracer, 0, sizeof(TracerDef)); + tracer->name = memory->Dup(assetName.c_str()); + return tracer; +} + +bool AssetLoaderTracer::CanLoadFromRaw() const +{ + return true; +} + +bool AssetLoaderTracer::LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const +{ + const auto fileName = "tracer/" + assetName; + const auto file = searchPath->Open(fileName); + if (!file.IsOpen()) + return false; + + InfoString infoString; + if (!infoString.FromStream(ObjConstants::INFO_STRING_PREFIX_TRACER, *file.m_stream)) + { + std::cout << "Failed to read tracer raw file: \"" << fileName << "\"" << std::endl; + return true; + } + + auto* tracer = memory->Create(); + memset(tracer, 0, sizeof(TracerDef)); + + InfoStringToTracerConverter converter(infoString, tracer, zone->m_script_strings, memory, manager, tracer_fields, std::extent::value); + if (!converter.Convert()) + { + std::cout << "Failed to parse tracer raw file: \"" << fileName << "\"" << std::endl; + return true; + } + + tracer->name = memory->Dup(assetName.c_str()); + + manager->AddAsset(ASSET_TYPE_TRACER, assetName, tracer, converter.GetDependencies(), converter.GetUsedScriptStrings()); + + return true; +} diff --git a/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderTracer.h b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderTracer.h new file mode 100644 index 00000000..d6430a8f --- /dev/null +++ b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderTracer.h @@ -0,0 +1,16 @@ +#pragma once +#include "Game/T6/T6.h" +#include "AssetLoading/BasicAssetLoader.h" +#include "AssetLoading/IAssetLoadingManager.h" +#include "SearchPath/ISearchPath.h" + +namespace T6 +{ + class AssetLoaderTracer final : public BasicAssetLoader + { + public: + _NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override; + _NODISCARD bool CanLoadFromRaw() const override; + bool LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const override; + }; +} diff --git a/src/ObjLoading/Game/T6/ObjLoaderT6.cpp b/src/ObjLoading/Game/T6/ObjLoaderT6.cpp index 4c3749de..af14c619 100644 --- a/src/ObjLoading/Game/T6/ObjLoaderT6.cpp +++ b/src/ObjLoading/Game/T6/ObjLoaderT6.cpp @@ -11,6 +11,7 @@ #include "AssetLoaders/AssetLoaderScriptParseTree.h" #include "AssetLoaders/AssetLoaderSlug.h" #include "AssetLoaders/AssetLoaderStringTable.h" +#include "AssetLoaders/AssetLoaderTracer.h" #include "AssetLoaders/AssetLoaderVehicle.h" #include "AssetLoaders/AssetLoaderWeapon.h" #include "AssetLoaders/AssetLoaderWeaponAttachment.h" @@ -72,7 +73,7 @@ namespace T6 REGISTER_ASSET_LOADER(AssetLoaderVehicle) REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_MEMORYBLOCK, MemoryBlock)) REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_ADDON_MAP_ENTS, AddonMapEnts)) - REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_TRACER, TracerDef)) + REGISTER_ASSET_LOADER(AssetLoaderTracer) REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_SKINNEDVERTS, SkinnedVertsDef)) REGISTER_ASSET_LOADER(AssetLoaderQdb) REGISTER_ASSET_LOADER(AssetLoaderSlug)