2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-05-17 07:21:43 +00:00

Merge pull request #753 from pagingred/iw5_physpreset_dumper

feat: phys presets dumping and loading for IW3, IW5, T5
This commit is contained in:
Paging Red
2026-05-02 06:01:59 -04:00
committed by GitHub
parent 704b191b97
commit 759a3ccf0e
41 changed files with 1698 additions and 12 deletions
+2 -1
View File
@@ -11,6 +11,7 @@
#include "Maps/AddonMapEntsDumperIW5.h"
#include "Menu/MenuDumperIW5.h"
#include "Menu/MenuListDumperIW5.h"
#include "PhysPreset/PhysPresetInfoStringDumperIW5.h"
#include "RawFile/RawFileDumperIW5.h"
#include "Script/ScriptDumperIW5.h"
#include "Sound/LoadedSoundDumperIW5.h"
@@ -22,7 +23,7 @@ using namespace IW5;
void ObjWriter::RegisterAssetDumpers(AssetDumpingContext& context)
{
// REGISTER_DUMPER(AssetDumperPhysPreset)
RegisterAssetDumper(std::make_unique<phys_preset::InfoStringDumperIW5>());
// REGISTER_DUMPER(AssetDumperPhysCollmap)
// REGISTER_DUMPER(AssetDumperXAnimParts)
// REGISTER_DUMPER(AssetDumperXModelSurfs)
@@ -0,0 +1,112 @@
#include "PhysPresetInfoStringDumperIW5.h"
#include "Game/IW5/InfoString/EnumStrings.h"
#include "Game/IW5/InfoString/InfoStringFromStructConverter.h"
#include "Game/IW5/ObjConstantsIW5.h"
#include "Game/IW5/PhysPreset/PhysPresetFields.h"
#include "PhysPreset/PhysPresetCommon.h"
#include <algorithm>
#include <cassert>
#include <cmath>
#include <type_traits>
using namespace IW5;
namespace
{
class InfoStringFromPhysPresetConverter final : public InfoStringFromStructConverter
{
protected:
void FillFromExtensionField(const cspField_t& field) override
{
switch (static_cast<physPresetFieldType_t>(field.iFieldType))
{
case PPFT_SCALING:
FillFromEnumInt(std::string(field.szName), field.iOffset, szPhysPresetScalingNames, std::extent_v<decltype(szPhysPresetScalingNames)>);
break;
case PPFT_NUM_FIELD_TYPES:
default:
assert(false);
break;
}
}
public:
InfoStringFromPhysPresetConverter(const PhysPresetInfo* structure,
const cspField_t* fields,
const size_t fieldCount,
std::function<std::string(scr_string_t)> scriptStringValueCallback)
: InfoStringFromStructConverter(structure, fields, fieldCount, std::move(scriptStringValueCallback))
{
}
};
void CopyToPhysPresetInfo(const PhysPreset* physPreset, PhysPresetInfo* physPresetInfo)
{
physPresetInfo->mass = physPreset->mass;
physPresetInfo->bounce = physPreset->bounce;
physPresetInfo->friction = physPreset->friction;
physPresetInfo->bulletForceScale = physPreset->bulletForceScale;
physPresetInfo->explosiveForceScale = physPreset->explosiveForceScale;
physPresetInfo->sndAliasPrefix = physPreset->sndAliasPrefix;
physPresetInfo->piecesSpreadFraction = physPreset->piecesSpreadFraction;
physPresetInfo->piecesUpwardVelocity = physPreset->piecesUpwardVelocity;
physPresetInfo->minMomentum = physPreset->minMomentum;
physPresetInfo->maxMomentum = physPreset->maxMomentum;
physPresetInfo->minPitch = physPreset->minPitch;
physPresetInfo->maxPitch = physPreset->maxPitch;
physPresetInfo->volumeType = physPreset->volumeType;
physPresetInfo->pitchType = physPreset->pitchType;
physPresetInfo->tempDefaultToCylinder = physPreset->tempDefaultToCylinder ? 1 : 0;
physPresetInfo->perSurfaceSndAlias = physPreset->perSurfaceSndAlias ? 1 : 0;
}
InfoString CreateInfoString(const XAssetInfo<PhysPreset>& asset)
{
auto* physPresetInfo = new PhysPresetInfo;
CopyToPhysPresetInfo(asset.Asset(), physPresetInfo);
InfoStringFromPhysPresetConverter converter(physPresetInfo,
phys_preset_fields,
std::extent_v<decltype(phys_preset_fields)>,
[asset](const scr_string_t scrStr) -> std::string
{
assert(scrStr < asset.m_zone->m_script_strings.Count());
if (scrStr >= asset.m_zone->m_script_strings.Count())
return "";
return asset.m_zone->m_script_strings[scrStr];
});
return converter.Convert();
}
} // namespace
namespace phys_preset
{
void InfoStringDumperIW5::DumpAsset(AssetDumpingContext& context, const XAssetInfo<AssetPhysPreset::Type>& asset)
{
// Only dump raw when no gdt available
if (context.m_gdt)
{
const auto infoString = CreateInfoString(asset);
GdtEntry gdtEntry(asset.m_name, GDF_FILENAME_PHYS_PRESET);
infoString.ToGdtProperties(INFO_STRING_PREFIX_PHYS_PRESET, gdtEntry);
context.m_gdt->WriteEntry(gdtEntry);
}
else
{
const auto assetFile = context.OpenAssetFile(GetFileNameForAssetName(asset.m_name));
if (!assetFile)
return;
auto& stream = *assetFile;
const auto infoString = CreateInfoString(asset);
const auto stringValue = infoString.ToString(INFO_STRING_PREFIX_PHYS_PRESET);
stream.write(stringValue.c_str(), stringValue.size());
}
}
} // namespace phys_preset
@@ -0,0 +1,13 @@
#pragma once
#include "Dumping/AbstractAssetDumper.h"
#include "Game/IW5/IW5.h"
namespace phys_preset
{
class InfoStringDumperIW5 final : public AbstractAssetDumper<IW5::AssetPhysPreset>
{
protected:
void DumpAsset(AssetDumpingContext& context, const XAssetInfo<IW5::AssetPhysPreset::Type>& asset) override;
};
} // namespace phys_preset
@@ -0,0 +1,133 @@
#include "InfoStringFromStructConverter.h"
#include <cassert>
#include <sstream>
using namespace T5;
void InfoStringFromStructConverter::FillFromBaseField(const cspField_t& field)
{
switch (static_cast<csParseFieldType_t>(field.iFieldType))
{
case CSPFT_STRING:
FillFromString(std::string(field.szName), field.iOffset);
break;
case CSPFT_STRING_MAX_STRING_CHARS:
FillFromStringBuffer(std::string(field.szName), field.iOffset, 1024);
break;
case CSPFT_STRING_MAX_QPATH:
FillFromStringBuffer(std::string(field.szName), field.iOffset, 64);
break;
case CSPFT_STRING_MAX_OSPATH:
FillFromStringBuffer(std::string(field.szName), field.iOffset, 256);
break;
case CSPFT_INT:
FillFromInt(std::string(field.szName), field.iOffset);
break;
case CSPFT_BOOL:
FillFromBool(std::string(field.szName), field.iOffset);
break;
case CSPFT_QBOOLEAN:
FillFromQBoolean(std::string(field.szName), field.iOffset);
break;
case CSPFT_FLOAT:
FillFromFloat(std::string(field.szName), field.iOffset);
break;
case CSPFT_MILLISECONDS:
FillFromMilliseconds(std::string(field.szName), field.iOffset);
break;
case CSPFT_FX:
{
const auto* fx = *reinterpret_cast<FxEffectDef**>(reinterpret_cast<uintptr_t>(m_structure) + field.iOffset);
if (fx)
m_info_string.SetValueForKey(std::string(field.szName), std::string(AssetName(fx->name)));
else
m_info_string.SetValueForKey(std::string(field.szName), "");
break;
}
case CSPFT_XMODEL:
{
const auto* model = *reinterpret_cast<XModel**>(reinterpret_cast<uintptr_t>(m_structure) + field.iOffset);
if (model)
m_info_string.SetValueForKey(std::string(field.szName), std::string(AssetName(model->name)));
else
m_info_string.SetValueForKey(std::string(field.szName), "");
break;
}
case CSPFT_MATERIAL:
case CSPFT_MATERIAL_STREAM:
{
const auto* material = *reinterpret_cast<Material**>(reinterpret_cast<uintptr_t>(m_structure) + field.iOffset);
if (material)
m_info_string.SetValueForKey(std::string(field.szName), std::string(AssetName(material->info.name)));
else
m_info_string.SetValueForKey(std::string(field.szName), "");
break;
}
case CSPFT_PHYS_PRESET:
{
const auto* physPreset = *reinterpret_cast<PhysPreset**>(reinterpret_cast<uintptr_t>(m_structure) + field.iOffset);
if (physPreset)
m_info_string.SetValueForKey(std::string(field.szName), std::string(AssetName(physPreset->name)));
else
m_info_string.SetValueForKey(std::string(field.szName), "");
break;
}
case CSPFT_SCRIPT_STRING:
FillFromScriptString(std::string(field.szName), field.iOffset);
break;
case CSPFT_NUM_BASE_FIELD_TYPES:
default:
assert(false);
break;
}
}
void InfoStringFromStructConverter::FillInfoString()
{
for (auto fieldIndex = 0u; fieldIndex < m_field_count; fieldIndex++)
{
const auto& field = m_fields[fieldIndex];
assert(field.iFieldType >= 0);
if (field.iFieldType < CSPFT_NUM_BASE_FIELD_TYPES)
FillFromBaseField(field);
else
FillFromExtensionField(field);
}
}
InfoStringFromStructConverter::InfoStringFromStructConverter(const void* structure, const cspField_t* fields, const size_t fieldCount)
: InfoStringFromStructConverterBase(structure),
m_fields(fields),
m_field_count(fieldCount)
{
}
InfoStringFromStructConverter::InfoStringFromStructConverter(const void* structure,
const cspField_t* fields,
const size_t fieldCount,
std::function<std::string(scr_string_t)> scriptStringValueCallback)
: InfoStringFromStructConverterBase(structure, std::move(scriptStringValueCallback)),
m_fields(fields),
m_field_count(fieldCount)
{
}
@@ -0,0 +1,24 @@
#pragma once
#include "Game/T5/T5.h"
#include "InfoString/InfoStringFromStructConverterBase.h"
namespace T5
{
class InfoStringFromStructConverter : public InfoStringFromStructConverterBase
{
protected:
const cspField_t* m_fields;
size_t m_field_count;
virtual void FillFromExtensionField(const cspField_t& field) = 0;
void FillFromBaseField(const cspField_t& field);
void FillInfoString() override;
public:
InfoStringFromStructConverter(const void* structure, const cspField_t* fields, size_t fieldCount);
InfoStringFromStructConverter(const void* structure,
const cspField_t* fields,
size_t fieldCount,
std::function<std::string(scr_string_t)> scriptStringValueCallback);
};
} // namespace T5
+2 -1
View File
@@ -5,6 +5,7 @@
#include "Game/T5/XModel/XModelDumperT5.h"
#include "Image/ImageDumperT5.h"
#include "Localize/LocalizeDumperT5.h"
#include "PhysPreset/PhysPresetInfoStringDumperT5.h"
#include "RawFile/RawFileDumperT5.h"
#include "StringTable/StringTableDumperT5.h"
@@ -12,7 +13,7 @@ using namespace T5;
void ObjWriter::RegisterAssetDumpers(AssetDumpingContext& context)
{
// REGISTER_DUMPER(AssetDumperPhysPreset, m_phys_preset)
RegisterAssetDumper(std::make_unique<phys_preset::InfoStringDumperT5>());
// REGISTER_DUMPER(AssetDumperPhysConstraints, m_phys_constraints)
// REGISTER_DUMPER(AssetDumperDestructibleDef, m_destructible_def)
// REGISTER_DUMPER(AssetDumperXAnimParts, m_xanim_parts)
@@ -0,0 +1,108 @@
#include "PhysPresetInfoStringDumperT5.h"
#include "Game/T5/InfoString/InfoStringFromStructConverter.h"
#include "Game/T5/ObjConstantsT5.h"
#include "Game/T5/PhysPreset/PhysPresetFields.h"
#include "PhysPreset/PhysPresetCommon.h"
#include <algorithm>
#include <cassert>
#include <cmath>
#include <type_traits>
using namespace T5;
namespace
{
class InfoStringFromPhysPresetConverter final : public InfoStringFromStructConverter
{
protected:
void FillFromExtensionField(const cspField_t& field) override
{
assert(false);
}
public:
InfoStringFromPhysPresetConverter(const PhysPresetInfo* structure,
const cspField_t* fields,
const size_t fieldCount,
std::function<std::string(scr_string_t)> scriptStringValueCallback)
: InfoStringFromStructConverter(structure, fields, fieldCount, std::move(scriptStringValueCallback))
{
}
};
void CopyToPhysPresetInfo(const PhysPreset* physPreset, PhysPresetInfo* physPresetInfo)
{
physPresetInfo->mass = std::clamp(physPreset->mass * 1000.0f, 1.0f, 2000.0f);
physPresetInfo->bounce = physPreset->bounce;
if (physPreset->friction >= MAX_FRICTION)
{
physPresetInfo->isFrictionInfinity = 1;
physPresetInfo->friction = 0;
}
else
{
physPresetInfo->isFrictionInfinity = 0;
physPresetInfo->friction = physPreset->friction;
}
physPresetInfo->bulletForceScale = physPreset->bulletForceScale;
physPresetInfo->explosiveForceScale = physPreset->explosiveForceScale;
physPresetInfo->piecesSpreadFraction = physPreset->piecesSpreadFraction;
physPresetInfo->piecesUpwardVelocity = physPreset->piecesUpwardVelocity;
physPresetInfo->canFloat = physPreset->canFloat;
physPresetInfo->gravityScale = std::clamp(physPreset->gravityScale, 0.01f, 10.0f);
physPresetInfo->centerOfMassOffset = physPreset->centerOfMassOffset;
physPresetInfo->buoyancyBoxMin = physPreset->buoyancyBoxMin;
physPresetInfo->buoyancyBoxMax = physPreset->buoyancyBoxMax;
}
InfoString CreateInfoString(const XAssetInfo<PhysPreset>& asset)
{
auto* physPresetInfo = new PhysPresetInfo;
CopyToPhysPresetInfo(asset.Asset(), physPresetInfo);
InfoStringFromPhysPresetConverter converter(physPresetInfo,
phys_preset_fields,
std::extent_v<decltype(phys_preset_fields)>,
[asset](const scr_string_t scrStr) -> std::string
{
assert(scrStr < asset.m_zone->m_script_strings.Count());
if (scrStr >= asset.m_zone->m_script_strings.Count())
return "";
return asset.m_zone->m_script_strings[scrStr];
});
return converter.Convert();
}
} // namespace
namespace phys_preset
{
void InfoStringDumperT5::DumpAsset(AssetDumpingContext& context, const XAssetInfo<AssetPhysPreset::Type>& asset)
{
// Only dump raw when no gdt available
if (context.m_gdt)
{
const auto infoString = CreateInfoString(asset);
GdtEntry gdtEntry(asset.m_name, GDF_FILENAME_PHYS_PRESET);
infoString.ToGdtProperties(INFO_STRING_PREFIX_PHYS_PRESET, gdtEntry);
context.m_gdt->WriteEntry(gdtEntry);
}
else
{
const auto assetFile = context.OpenAssetFile(GetFileNameForAssetName(asset.m_name));
if (!assetFile)
return;
auto& stream = *assetFile;
const auto infoString = CreateInfoString(asset);
const auto stringValue = infoString.ToString(INFO_STRING_PREFIX_PHYS_PRESET);
stream.write(stringValue.c_str(), stringValue.size());
}
}
} // namespace phys_preset
@@ -0,0 +1,13 @@
#pragma once
#include "Dumping/AbstractAssetDumper.h"
#include "Game/T5/T5.h"
namespace phys_preset
{
class InfoStringDumperT5 final : public AbstractAssetDumper<T5::AssetPhysPreset>
{
protected:
void DumpAsset(AssetDumpingContext& context, const XAssetInfo<T5::AssetPhysPreset::Type>& asset) override;
};
} // namespace phys_preset