From 23653e306747067af28a835be37283eae433caf3 Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 27 Mar 2021 10:49:49 +0100 Subject: [PATCH] Add ZBarrier loading --- .../T6/AssetLoaders/AssetLoaderZBarrier.cpp | 98 +++++++++++++++++++ .../T6/AssetLoaders/AssetLoaderZBarrier.h | 18 ++++ src/ObjLoading/Game/T6/ObjLoaderT6.cpp | 3 +- 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderZBarrier.cpp create mode 100644 src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderZBarrier.h diff --git a/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderZBarrier.cpp b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderZBarrier.cpp new file mode 100644 index 00000000..b8336f03 --- /dev/null +++ b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderZBarrier.cpp @@ -0,0 +1,98 @@ +#include "AssetLoaderZBarrier.h" + +#include +#include +#include + +#include "Game/T6/ObjConstantsT6.h" +#include "Game/T6/T6.h" +#include "Game/T6/InfoString/InfoStringToStructConverter.h" +#include "Game/T6/InfoString/ZBarrierFields.h" +#include "InfoString/InfoString.h" + +using namespace T6; + +namespace T6 +{ + class InfoStringToZBarrierConverter final : public InfoStringToStructConverter + { + protected: + bool ConvertExtensionField(const cspField_t& field, const std::string& value) override + { + assert(false); + return false; + } + + public: + InfoStringToZBarrierConverter(const InfoString& infoString, ZBarrierDef* zbarrier, ZoneScriptStrings& zoneScriptStrings, MemoryManager* memory, IAssetLoadingManager* manager, + const cspField_t* fields, const size_t fieldCount) + : InfoStringToStructConverter(infoString, zbarrier, zoneScriptStrings, memory, manager, fields, fieldCount) + { + } + }; +} + +void AssetLoaderZBarrier::CalculateZBarrierFields(ZBarrierDef* zbarrier) +{ + // numBoardsInBarrier + { + auto foundEnd = false; + for (auto i = 0u; i < std::extent::value; i++) + { + if (zbarrier->boards[i].pBoardModel == nullptr) + { + foundEnd = true; + zbarrier->numBoardsInBarrier = i; + } + } + + if (!foundEnd) + zbarrier->numBoardsInBarrier = std::extent::value; + } +} + +void* AssetLoaderZBarrier::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) +{ + auto* zbarrier = memory->Create(); + memset(zbarrier, 0, sizeof(ZBarrierDef)); + CalculateZBarrierFields(zbarrier); + zbarrier->name = memory->Dup(assetName.c_str()); + return zbarrier; +} + +bool AssetLoaderZBarrier::CanLoadFromRaw() const +{ + return true; +} + +bool AssetLoaderZBarrier::LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const +{ + const auto fileName = "zbarrier/" + assetName; + const auto file = searchPath->Open(fileName); + if (!file.IsOpen()) + return false; + + InfoString infoString; + if (!infoString.FromStream(ObjConstants::INFO_STRING_PREFIX_ZBARRIER, *file.m_stream)) + { + std::cout << "Failed to read zbarrier raw file: \"" << fileName << "\"" << std::endl; + return true; + } + + auto* zbarrier = memory->Create(); + memset(zbarrier, 0, sizeof(ZBarrierDef)); + + InfoStringToZBarrierConverter converter(infoString, zbarrier, zone->m_script_strings, memory, manager, zbarrier_fields, std::extent::value); + if (!converter.Convert()) + { + std::cout << "Failed to parse zbarrier raw file: \"" << fileName << "\"" << std::endl; + return true; + } + + CalculateZBarrierFields(zbarrier); + zbarrier->name = memory->Dup(assetName.c_str()); + + manager->AddAsset(ASSET_TYPE_ZBARRIER, assetName, zbarrier, converter.GetDependencies(), converter.GetUsedScriptStrings()); + + return true; +} diff --git a/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderZBarrier.h b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderZBarrier.h new file mode 100644 index 00000000..929f7b83 --- /dev/null +++ b/src/ObjLoading/Game/T6/AssetLoaders/AssetLoaderZBarrier.h @@ -0,0 +1,18 @@ +#pragma once +#include "Game/T6/T6.h" +#include "AssetLoading/BasicAssetLoader.h" +#include "AssetLoading/IAssetLoadingManager.h" +#include "SearchPath/ISearchPath.h" + +namespace T6 +{ + class AssetLoaderZBarrier final : public BasicAssetLoader + { + static void CalculateZBarrierFields(ZBarrierDef* zbarrier); + + 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 c7b85959..4c3749de 100644 --- a/src/ObjLoading/Game/T6/ObjLoaderT6.cpp +++ b/src/ObjLoading/Game/T6/ObjLoaderT6.cpp @@ -15,6 +15,7 @@ #include "AssetLoaders/AssetLoaderWeapon.h" #include "AssetLoaders/AssetLoaderWeaponAttachment.h" #include "AssetLoaders/AssetLoaderWeaponAttachmentUnique.h" +#include "AssetLoaders/AssetLoaderZBarrier.h" #include "AssetLoading/AssetLoadingManager.h" #include "Image/Texture.h" #include "Image/IwiLoader.h" @@ -77,7 +78,7 @@ namespace T6 REGISTER_ASSET_LOADER(AssetLoaderSlug) REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_FOOTSTEP_TABLE, FootstepTableDef)) REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_FOOTSTEPFX_TABLE, FootstepFXTableDef)) - REGISTER_ASSET_LOADER(BASIC_LOADER(ASSET_TYPE_ZBARRIER, ZBarrierDef)) + REGISTER_ASSET_LOADER(AssetLoaderZBarrier) #undef BASIC_LOADER #undef REGISTER_ASSET_LOADER