acdream/src/AcDream.Core/Vfx/PhysicsScriptTableResolver.cs

86 lines
3 KiB
C#

using DatReaderWriter.DBObjs;
using DatReaderWriter.Enums;
using DatReaderWriter.Types;
namespace AcDream.Core.Vfx;
/// <summary>
/// Exact retail PhysicsScriptTable lookup: hash by raw script type, then scan
/// that type's entries in stored DAT order for the first upper threshold.
/// </summary>
/// <remarks>
/// Port of <c>PhysicsScriptTable::GetScript</c> at <c>0x00521E40</c> and
/// <c>PhysicsScriptTableData::GetScript</c> at <c>0x00521A20</c>.
/// </remarks>
public sealed class PhysicsScriptTableResolver
{
private readonly Func<uint, PhysicsScriptTable?> _loadTable;
public PhysicsScriptTableResolver(Func<uint, PhysicsScriptTable?> loadTable)
=> _loadTable = loadTable ?? throw new ArgumentNullException(nameof(loadTable));
/// <summary>
/// Returns the selected PhysicsScript DID, or null for a missing table or
/// type, an unordered/above-range intensity, or an invalid script DID.
/// </summary>
public uint? Resolve(uint tableDid, uint rawScriptType, float intensity)
=> Resolve(tableDid, rawScriptType, intensity, out _);
/// <summary>
/// Resolves a typed play while preserving a corrupt-DAT exception for the
/// App owner to diagnose with entity context. A load failure remains a
/// retail no-play and never corrupts the caller's script FIFO.
/// </summary>
public uint? Resolve(
uint tableDid,
uint rawScriptType,
float intensity,
out Exception? loadFailure)
{
loadFailure = null;
if (!IsPhysicsScriptTableDid(tableDid))
return null;
PhysicsScriptTable? table;
try
{
table = _loadTable(tableDid);
}
catch (Exception error)
{
loadFailure = error;
return null;
}
if (table is null
|| table.Id != tableDid
|| !table.ScriptTable.TryGetValue(
unchecked((PlayScript)rawScriptType),
out PhysicsScriptTableData? data))
{
return null;
}
// Retail compares intensity <= Mod. Do not sort: equal and duplicate
// thresholds resolve to the first stored entry. IEEE comparisons make
// NaN fall through all entries, +infinity fall through finite entries,
// and -infinity select the first finite threshold.
foreach (ScriptAndModData entry in data.Scripts)
{
if (intensity <= entry.Mod)
{
uint scriptDid = entry.ScriptId.DataId;
return IsPhysicsScriptDid(scriptDid) ? scriptDid : null;
}
}
return null;
}
public static bool IsPhysicsScriptDid(uint did) =>
// AC data IDs encode the DBObj type in the high byte; the remaining
// 24 bits are the file index and must not be truncated to 16 bits.
(did & 0xFF000000u) == 0x33000000u;
public static bool IsPhysicsScriptTableDid(uint did) =>
(did & 0xFF000000u) == 0x34000000u;
}