2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-07-05 02:31:55 +00:00

chore: fix loading and writing code for T6

This commit is contained in:
Jan
2024-12-31 12:38:01 +01:00
parent d8bc156ffd
commit 83d13aa166
193 changed files with 4129 additions and 4208 deletions

View File

@ -0,0 +1,53 @@
#include "GdtLoaderPhysPresetT6.h"
#include "Game/T6/ObjConstantsT6.h"
#include "Game/T6/T6.h"
#include "InfoString/InfoString.h"
#include "InfoStringLoaderPhysPresetT6.h"
#include <cstring>
#include <format>
#include <iostream>
using namespace T6;
namespace
{
class GdtLoaderPhysPreset final : public AssetCreator<AssetPhysPreset>
{
public:
GdtLoaderPhysPreset(MemoryManager& memory, ISearchPath& searchPath, IGdtQueryable& gdt, Zone& zone)
: m_gdt(gdt),
m_info_string_loader(memory, searchPath, zone)
{
}
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override
{
const auto* gdtEntry = m_gdt.GetGdtEntryByGdfAndName(ObjConstants::GDF_FILENAME_WEAPON, 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;
InfoStringLoaderPhysPreset m_info_string_loader;
};
} // namespace
namespace T6
{
std::unique_ptr<AssetCreator<AssetPhysPreset>> CreateGdtPhysPresetLoader(MemoryManager& memory, ISearchPath& searchPath, IGdtQueryable& gdt, Zone& zone)
{
return std::make_unique<GdtLoaderPhysPreset>(memory, searchPath, gdt, zone);
}
} // namespace T6

View File

@ -0,0 +1,14 @@
#pragma once
#include "Asset/IAssetCreator.h"
#include "Game/T6/T6.h"
#include "Gdt/IGdtQueryable.h"
#include "SearchPath/ISearchPath.h"
#include "Utils/MemoryManager.h"
#include <memory>
namespace T6
{
std::unique_ptr<AssetCreator<AssetPhysPreset>> CreateGdtPhysPresetLoader(MemoryManager& memory, ISearchPath& searchPath, IGdtQueryable& gdt, Zone& zone);
} // namespace T6

View File

@ -0,0 +1,89 @@
#include "InfoStringLoaderPhysPresetT6.h"
#include "Game/T6/InfoString/InfoStringToStructConverter.h"
#include "Game/T6/PhysPreset/PhysPresetFields.h"
#include "Game/T6/T6.h"
#include <cassert>
#include <cstring>
#include <format>
#include <iostream>
#include <limits>
using namespace T6;
namespace
{
class InfoStringToPhysPresetConverter final : public InfoStringToStructConverter
{
protected:
bool ConvertExtensionField(const cspField_t& field, const std::string& value) override
{
assert(false);
return false;
}
public:
InfoStringToPhysPresetConverter(const InfoString& infoString,
PhysPresetInfo& physPreset,
ZoneScriptStrings& zoneScriptStrings,
MemoryManager& memory,
AssetCreationContext& context,
AssetRegistration<AssetPhysPreset>& registration,
const cspField_t* fields,
const size_t fieldCount)
: InfoStringToStructConverter(infoString, &physPreset, zoneScriptStrings, memory, context, registration, fields, fieldCount)
{
}
};
void CopyFromPhysPresetInfo(const PhysPresetInfo& physPresetInfo, PhysPreset& physPreset)
{
physPreset.mass = std::clamp(physPresetInfo.mass, 1.0f, 2000.0f) * 0.001f;
physPreset.bounce = physPresetInfo.bounce;
if (physPresetInfo.isFrictionInfinity != 0)
physPreset.friction = std::numeric_limits<float>::infinity();
else
physPreset.friction = physPresetInfo.friction;
physPreset.bulletForceScale = physPresetInfo.bulletForceScale;
physPreset.explosiveForceScale = physPresetInfo.explosiveForceScale;
physPreset.piecesSpreadFraction = physPresetInfo.piecesSpreadFraction;
physPreset.piecesUpwardVelocity = physPresetInfo.piecesUpwardVelocity;
physPreset.canFloat = physPresetInfo.canFloat;
physPreset.gravityScale = std::clamp(physPresetInfo.gravityScale, 0.01f, 10.0f);
physPreset.centerOfMassOffset = physPresetInfo.centerOfMassOffset;
physPreset.buoyancyBoxMin = physPresetInfo.buoyancyBoxMin;
physPreset.buoyancyBoxMax = physPresetInfo.buoyancyBoxMax;
}
} // namespace
InfoStringLoaderPhysPreset::InfoStringLoaderPhysPreset(MemoryManager& memory, ISearchPath& searchPath, Zone& zone)
: m_memory(memory),
m_search_path(searchPath),
m_zone(zone)
{
}
AssetCreationResult InfoStringLoaderPhysPreset::CreateAsset(const std::string& assetName, const InfoString& infoString, AssetCreationContext& context)
{
auto* physPreset = m_memory.Alloc<PhysPreset>();
physPreset->name = m_memory.Dup(assetName.c_str());
AssetRegistration<AssetPhysPreset> registration(assetName, physPreset);
PhysPresetInfo physPresetInfo;
memset(&physPresetInfo, 0, sizeof(physPresetInfo));
InfoStringToPhysPresetConverter converter(
infoString, physPresetInfo, 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(physPresetInfo, *physPreset);
return AssetCreationResult::Success(context.AddAsset(std::move(registration)));
}

View File

@ -0,0 +1,21 @@
#pragma once
#include "Asset/AssetCreationContext.h"
#include "Asset/AssetCreationResult.h"
#include "InfoString/InfoString.h"
namespace T6
{
class InfoStringLoaderPhysPreset
{
public:
InfoStringLoaderPhysPreset(MemoryManager& memory, ISearchPath& searchPath, Zone& zone);
AssetCreationResult CreateAsset(const std::string& assetName, const InfoString& infoString, AssetCreationContext& context);
private:
MemoryManager& m_memory;
ISearchPath& m_search_path;
Zone& m_zone;
};
} // namespace T6

View File

@ -0,0 +1,54 @@
#include "RawLoaderPhysPresetT6.h"
#include "Game/T6/ObjConstantsT6.h"
#include "Game/T6/T6.h"
#include "InfoString/InfoString.h"
#include "InfoStringLoaderPhysPresetT6.h"
#include <cstring>
#include <format>
#include <iostream>
using namespace T6;
namespace
{
class RawLoaderPhysPreset final : public AssetCreator<AssetPhysPreset>
{
public:
RawLoaderPhysPreset(MemoryManager& memory, ISearchPath& searchPath, Zone& zone)
: m_search_path(searchPath),
m_info_string_loader(memory, searchPath, zone)
{
}
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override
{
const auto fileName = std::format("physic/{}", 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;
InfoStringLoaderPhysPreset m_info_string_loader;
};
} // namespace
namespace T6
{
std::unique_ptr<AssetCreator<AssetPhysPreset>> CreateRawPhysPresetLoader(MemoryManager& memory, ISearchPath& searchPath, Zone& zone)
{
return std::make_unique<RawLoaderPhysPreset>(memory, searchPath, zone);
}
} // namespace T6

View File

@ -0,0 +1,13 @@
#pragma once
#include "Asset/IAssetCreator.h"
#include "Game/T6/T6.h"
#include "SearchPath/ISearchPath.h"
#include "Utils/MemoryManager.h"
#include <memory>
namespace T6
{
std::unique_ptr<AssetCreator<AssetPhysPreset>> CreateRawPhysPresetLoader(MemoryManager& memory, ISearchPath& searchPath, Zone& zone);
} // namespace T6