2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-04-29 15:09:38 +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,53 @@
#include "GdtLoaderVehicleT6.h"
#include "Game/T6/ObjConstantsT6.h"
#include "Game/T6/T6.h"
#include "InfoString/InfoString.h"
#include "InfoStringLoaderVehicleT6.h"
#include <cstring>
#include <format>
#include <iostream>
using namespace T6;
namespace
{
class GdtLoaderVehicle final : public AssetCreator<AssetVehicle>
{
public:
GdtLoaderVehicle(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_VEHICLE, assetName);
if (gdtEntry == nullptr)
return AssetCreationResult::NoAction();
InfoString infoString;
if (!infoString.FromGdtProperties(*gdtEntry))
{
std::cerr << std::format("Failed to read vehicle gdt entry: \"{}\"\n", assetName);
return AssetCreationResult::Failure();
}
return m_info_string_loader.CreateAsset(assetName, infoString, context);
}
private:
IGdtQueryable& m_gdt;
InfoStringLoaderVehicle m_info_string_loader;
};
} // namespace
namespace T6
{
std::unique_ptr<AssetCreator<AssetVehicle>> CreateGdtVehicleLoader(MemoryManager& memory, ISearchPath& searchPath, IGdtQueryable& gdt, Zone& zone)
{
return std::make_unique<GdtLoaderVehicle>(memory, searchPath, gdt, zone);
}
} // namespace T6
@@ -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<AssetVehicle>> CreateGdtVehicleLoader(MemoryManager& memory, ISearchPath& searchPath, IGdtQueryable& gdt, Zone& zone);
} // namespace T6
@@ -0,0 +1,133 @@
#include "InfoStringLoaderVehicleT6.h"
#include "Game/T6/InfoString/InfoStringToStructConverter.h"
#include "Game/T6/T6.h"
#include "Game/T6/Vehicle/VehicleFields.h"
#include <cassert>
#include <cstring>
#include <format>
#include <iostream>
#include <limits>
using namespace T6;
namespace
{
class InfoStringToVehicleConverter final : public InfoStringToStructConverter
{
protected:
bool ConvertExtensionField(const cspField_t& field, const std::string& value) override
{
switch (static_cast<VehicleFieldType>(field.iFieldType))
{
case VFT_TYPE:
return ConvertEnumInt(field.szName, value, field.iOffset, s_vehicleClassNames, std::extent_v<decltype(s_vehicleClassNames)>);
case VFT_CAMERAMODE:
return ConvertEnumInt(field.szName, value, field.iOffset, s_vehicleCameraModes, std::extent_v<decltype(s_vehicleCameraModes)>);
case VFT_TRACTION_TYPE:
return ConvertEnumInt(field.szName, value, field.iOffset, s_tractionTypeNames, std::extent_v<decltype(s_tractionTypeNames)>);
case VFT_MPH_TO_INCHES_PER_SECOND:
{
char* endPtr;
*reinterpret_cast<float*>(reinterpret_cast<uintptr_t>(m_structure) + field.iOffset) = strtof(value.c_str(), &endPtr) * 17.6f;
if (endPtr != &value[value.size()])
{
std::cerr << std::format("Failed to parse value \"{}\" as mph\n", value);
return false;
}
return true;
}
case VFT_POUNDS_TO_GAME_MASS:
{
char* endPtr;
*reinterpret_cast<float*>(reinterpret_cast<uintptr_t>(m_structure) + field.iOffset) = strtof(value.c_str(), &endPtr) * 0.001f;
if (endPtr != &value[value.size()])
{
std::cerr << std::format("Failed to parse value \"{}\" as pounds\n", value);
return false;
}
return true;
}
case VFT_TEAM:
{
if (value == "axis")
{
*reinterpret_cast<int*>(reinterpret_cast<uintptr_t>(m_structure) + field.iOffset) = TEAM_AXIS;
return true;
}
if (value == "allies")
{
*reinterpret_cast<int*>(reinterpret_cast<uintptr_t>(m_structure) + field.iOffset) = TEAM_ALLIES;
return true;
}
if (value == "neutral")
{
*reinterpret_cast<int*>(reinterpret_cast<uintptr_t>(m_structure) + field.iOffset) = TEAM_FOUR;
return true;
}
*reinterpret_cast<int*>(reinterpret_cast<uintptr_t>(m_structure) + field.iOffset) = TEAM_BAD;
std::cerr << std::format("Failed to parse value \"{}\" as team\n", value);
return false;
}
case VFT_KEY_BINDING:
case VFT_GRAPH:
case VFT_WIIUCONTROLOVERRIDE:
default:
assert(false);
return false;
}
}
public:
InfoStringToVehicleConverter(const InfoString& infoString,
VehicleDef& vehicleDef,
ZoneScriptStrings& zoneScriptStrings,
MemoryManager& memory,
AssetCreationContext& context,
AssetRegistration<AssetVehicle>& registration,
const cspField_t* fields,
const size_t fieldCount)
: InfoStringToStructConverter(infoString, &vehicleDef, zoneScriptStrings, memory, context, registration, fields, fieldCount)
{
}
};
} // namespace
InfoStringLoaderVehicle::InfoStringLoaderVehicle(MemoryManager& memory, ISearchPath& searchPath, Zone& zone)
: m_memory(memory),
m_search_path(searchPath),
m_zone(zone)
{
}
AssetCreationResult InfoStringLoaderVehicle::CreateAsset(const std::string& assetName, const InfoString& infoString, AssetCreationContext& context)
{
auto* vehicleDef = m_memory.Alloc<VehicleDef>();
vehicleDef->name = m_memory.Dup(assetName.c_str());
AssetRegistration<AssetVehicle> registration(assetName, vehicleDef);
InfoStringToVehicleConverter converter(
infoString, *vehicleDef, m_zone.m_script_strings, m_memory, context, registration, vehicle_fields, std::extent_v<decltype(vehicle_fields)>);
if (!converter.Convert())
{
std::cerr << std::format("Failed to parse vehicle: \"{}\"\n", assetName);
return AssetCreationResult::Failure();
}
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 InfoStringLoaderVehicle
{
public:
InfoStringLoaderVehicle(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 "RawLoaderVehicleT6.h"
#include "Game/T6/ObjConstantsT6.h"
#include "Game/T6/T6.h"
#include "InfoString/InfoString.h"
#include "InfoStringLoaderVehicleT6.h"
#include <cstring>
#include <format>
#include <iostream>
using namespace T6;
namespace
{
class RawLoaderVehicle final : public AssetCreator<AssetVehicle>
{
public:
RawLoaderVehicle(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("vehicles/{}", assetName);
const auto file = m_search_path.Open(fileName);
if (!file.IsOpen())
return AssetCreationResult::NoAction();
InfoString infoString;
if (!infoString.FromStream(ObjConstants::INFO_STRING_PREFIX_VEHICLE, *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;
InfoStringLoaderVehicle m_info_string_loader;
};
} // namespace
namespace T6
{
std::unique_ptr<AssetCreator<AssetVehicle>> CreateRawVehicleLoader(MemoryManager& memory, ISearchPath& searchPath, Zone& zone)
{
return std::make_unique<RawLoaderVehicle>(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<AssetVehicle>> CreateRawVehicleLoader(MemoryManager& memory, ISearchPath& searchPath, Zone& zone);
} // namespace T6