2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-05-17 07:21:43 +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
@@ -0,0 +1,54 @@
#include "GdtLoaderPhysConstraintsT6.h"
#include "Game/T6/ObjConstantsT6.h"
#include "Game/T6/T6.h"
#include "InfoString/InfoString.h"
#include "InfoStringLoaderPhysConstraintsT6.h"
#include <cstring>
#include <format>
#include <iostream>
using namespace T6;
namespace
{
class GdtLoaderPhysConstraints final : public AssetCreator<AssetPhysConstraints>
{
public:
GdtLoaderPhysConstraints(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 constraints gdt entry: \"{}\"\n", assetName);
return AssetCreationResult::Failure();
}
return m_info_string_loader.CreateAsset(assetName, infoString, context);
}
private:
IGdtQueryable& m_gdt;
InfoStringLoaderPhysConstraints m_info_string_loader;
};
} // namespace
namespace T6
{
std::unique_ptr<AssetCreator<AssetPhysConstraints>>
CreateGdtPhysConstraintsLoader(MemoryManager& memory, ISearchPath& searchPath, IGdtQueryable& gdt, Zone& zone)
{
return std::make_unique<GdtLoaderPhysConstraints>(memory, searchPath, gdt, zone);
}
} // namespace T6
@@ -0,0 +1,15 @@
#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<AssetPhysConstraints>>
CreateGdtPhysConstraintsLoader(MemoryManager& memory, ISearchPath& searchPath, IGdtQueryable& gdt, Zone& zone);
} // namespace T6
@@ -0,0 +1,110 @@
#include "InfoStringLoaderPhysConstraintsT6.h"
#include "Game/T6/InfoString/InfoStringToStructConverter.h"
#include "Game/T6/PhysConstraints/PhysConstraintsFields.h"
#include "Game/T6/T6.h"
#include <cassert>
#include <cstring>
#include <format>
#include <iostream>
#include <limits>
using namespace T6;
namespace
{
class InfoStringToPhysConstraintsConverter final : public InfoStringToStructConverter
{
protected:
bool ConvertExtensionField(const cspField_t& field, const std::string& value) override
{
switch (static_cast<constraintsFieldType_t>(field.iFieldType))
{
case CFT_TYPE:
return ConvertEnumInt(field.szName, value, field.iOffset, s_constraintTypeNames, std::extent_v<decltype(s_constraintTypeNames)>);
default:
assert(false);
return false;
}
}
public:
InfoStringToPhysConstraintsConverter(const InfoString& infoString,
PhysConstraints& physConstraints,
ZoneScriptStrings& zoneScriptStrings,
MemoryManager& memory,
AssetCreationContext& context,
AssetRegistration<AssetPhysConstraints>& registration,
const cspField_t* fields,
const size_t fieldCount)
: InfoStringToStructConverter(infoString, &physConstraints, zoneScriptStrings, memory, context, registration, fields, fieldCount)
{
}
};
void CalculatePhysConstraintsFields(PhysConstraints& physConstraints, ZoneScriptStrings& scriptStrings)
{
for (auto& constraint : physConstraints.data)
{
constraint.targetname = scriptStrings.AddOrGetScriptString("");
constraint.target_ent1 = scriptStrings.AddOrGetScriptString("");
constraint.target_ent2 = scriptStrings.AddOrGetScriptString("");
constraint.attach_point_type1 = ATTACH_POINT_BONE;
constraint.attach_point_type2 = ATTACH_POINT_BONE;
constraint.target_bone1 = "";
constraint.target_bone2 = "";
}
// count
{
auto foundEnd = false;
for (auto i = 0u; i < std::extent_v<decltype(PhysConstraints::data)>; i++)
{
if (physConstraints.data[i].type == CONSTRAINT_NONE)
{
foundEnd = true;
physConstraints.count = i;
break;
}
}
if (!foundEnd)
physConstraints.count = std::extent_v<decltype(PhysConstraints::data)>;
}
}
} // namespace
InfoStringLoaderPhysConstraints::InfoStringLoaderPhysConstraints(MemoryManager& memory, ISearchPath& searchPath, Zone& zone)
: m_memory(memory),
m_search_path(searchPath),
m_zone(zone)
{
}
AssetCreationResult InfoStringLoaderPhysConstraints::CreateAsset(const std::string& assetName, const InfoString& infoString, AssetCreationContext& context)
{
auto* physConstraints = m_memory.Alloc<PhysConstraints>();
physConstraints->name = m_memory.Dup(assetName.c_str());
AssetRegistration<AssetPhysConstraints> registration(assetName, physConstraints);
InfoStringToPhysConstraintsConverter converter(infoString,
*physConstraints,
m_zone.m_script_strings,
m_memory,
context,
registration,
phys_constraints_fields,
std::extent_v<decltype(phys_constraints_fields)>);
if (!converter.Convert())
{
std::cerr << std::format("Failed to parse phys constraints: \"{}\"\n", assetName);
return AssetCreationResult::Failure();
}
CalculatePhysConstraintsFields(*physConstraints, m_zone.m_script_strings);
registration.AddScriptString(m_zone.m_script_strings.AddOrGetScriptString(""));
return AssetCreationResult::Success(context.AddAsset(std::move(registration)));
}
@@ -0,0 +1,21 @@
#pragma once
#include "Asset/AssetCreationContext.h"
#include "Asset/AssetCreationResult.h"
#include "InfoString/InfoString.h"
namespace T6
{
class InfoStringLoaderPhysConstraints
{
public:
InfoStringLoaderPhysConstraints(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
@@ -0,0 +1,54 @@
#include "RawLoaderPhysConstraintsT6.h"
#include "Game/T6/ObjConstantsT6.h"
#include "Game/T6/T6.h"
#include "InfoString/InfoString.h"
#include "InfoStringLoaderPhysConstraintsT6.h"
#include <cstring>
#include <format>
#include <iostream>
using namespace T6;
namespace
{
class RawLoaderPhysConstraints final : public AssetCreator<AssetPhysConstraints>
{
public:
RawLoaderPhysConstraints(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("physconstraints/{}", 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_CONSTRAINTS, *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;
InfoStringLoaderPhysConstraints m_info_string_loader;
};
} // namespace
namespace T6
{
std::unique_ptr<AssetCreator<AssetPhysConstraints>> CreateRawPhysConstraintsLoader(MemoryManager& memory, ISearchPath& searchPath, Zone& zone)
{
return std::make_unique<RawLoaderPhysConstraints>(memory, searchPath, zone);
}
} // namespace T6
@@ -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<AssetPhysConstraints>> CreateRawPhysConstraintsLoader(MemoryManager& memory, ISearchPath& searchPath, Zone& zone);
} // namespace T6