Record original inventory and recover all GAM level entities
This commit is contained in:
commit
d62e2ab4de
16 files changed
+3954
No files matched your search
@@ -0,0 +1,110 @@
|
||||
using System.Buffers.Binary;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
|
||||
namespace GamePort.Assets;
|
||||
|
||||
/// <summary>Original, big endian level entities. See docs/gam-format.md.</summary>
|
||||
public sealed record GamProperty(string Name, uint Type, byte[] Raw)
|
||||
{
|
||||
public string Text => Type == 1 ? Encoding.Latin1.GetString(Raw)
|
||||
: throw new InvalidDataException($"{Name} is not a string");
|
||||
public float Float => Type == 3 && Raw.Length == 4
|
||||
? BitConverter.Int32BitsToSingle(BinaryPrimitives.ReadInt32BigEndian(Raw))
|
||||
: throw new InvalidDataException($"{Name} is not a float32");
|
||||
public int Integer => Type == 6 && Raw.Length == 4
|
||||
? BinaryPrimitives.ReadInt32BigEndian(Raw)
|
||||
: throw new InvalidDataException($"{Name} is not an int32");
|
||||
}
|
||||
|
||||
public sealed record GamEntity(string Kind, int Offset,
|
||||
IReadOnlyDictionary<string, GamProperty> Properties, byte[] Trailer)
|
||||
{
|
||||
public string? Text(string name) => Properties.TryGetValue(name, out var p) ? p.Text : null;
|
||||
public float Number(string name, float fallback = 0) =>
|
||||
Properties.TryGetValue(name, out var p) ? p.Float : fallback;
|
||||
public int Integer(string name, int fallback = 0) =>
|
||||
Properties.TryGetValue(name, out var p) ? p.Integer : fallback;
|
||||
public string? Tag => Text("ObjectTag");
|
||||
public Vector3 Position => new(Number("PositionX"), Number("PositionY"), Number("PositionZ"));
|
||||
public Vector3 Rotation => new(Number("RotationX"), Number("RotationY"), Number("RotationZ"));
|
||||
public Vector3 CameraPosition => new(Number("ViewportPx"), Number("ViewportPy"), Number("ViewportPz"));
|
||||
}
|
||||
|
||||
public sealed record GamLevel(string Code, IReadOnlyList<GamEntity> Entities)
|
||||
{
|
||||
public GamEntity? Player => Entities.FirstOrDefault(e => e.Kind == "3JIM");
|
||||
|
||||
public GamEntity? FindStart(string? tag = null)
|
||||
{
|
||||
tag ??= Player?.Text("StartPoint");
|
||||
return Entities.FirstOrDefault(e => e.Kind == "STRT" &&
|
||||
string.Equals(e.Tag, tag, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
public static GamLevel Load(string path) => Parse(File.ReadAllBytes(path));
|
||||
|
||||
public static GamLevel Parse(ReadOnlySpan<byte> data)
|
||||
{
|
||||
var reader = new GamReader(data);
|
||||
var code = reader.Text(4);
|
||||
if (code.Any(c => !char.IsAsciiLetterOrDigit(c)))
|
||||
throw new InvalidDataException("Invalid level FourCC");
|
||||
var count = reader.Count(12);
|
||||
var entities = new List<GamEntity>(count);
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var offset = reader.Offset;
|
||||
var kind = reader.Text(4);
|
||||
var properties = new Dictionary<string, GamProperty>(StringComparer.Ordinal);
|
||||
var propertyCount = reader.Count(10);
|
||||
for (var j = 0; j < propertyCount; j++)
|
||||
{
|
||||
var name = reader.Pascal();
|
||||
var type = reader.U32();
|
||||
var length = reader.U32();
|
||||
if (length > int.MaxValue)
|
||||
throw new InvalidDataException($"Property too long at {reader.Offset:x}");
|
||||
if (type == 1 && reader.Byte() != length)
|
||||
throw new InvalidDataException($"String size mismatch at {reader.Offset:x}");
|
||||
if (type is 3 or 6 && length != 4)
|
||||
throw new InvalidDataException($"Scalar size mismatch at {reader.Offset:x}");
|
||||
var property = new GamProperty(name, type, reader.Take((int)length).ToArray());
|
||||
if (type == 3 && !float.IsFinite(property.Float))
|
||||
throw new InvalidDataException($"Non-finite scalar at {reader.Offset:x}");
|
||||
if (!properties.TryAdd(name, property))
|
||||
throw new InvalidDataException($"Duplicate property {name}");
|
||||
}
|
||||
entities.Add(new GamEntity(kind, offset, properties, reader.Take(4).ToArray()));
|
||||
}
|
||||
if (reader.Remaining != 0)
|
||||
throw new InvalidDataException($"Unexpected bytes at {reader.Offset:x}");
|
||||
return new GamLevel(code, entities);
|
||||
}
|
||||
|
||||
private ref struct GamReader(ReadOnlySpan<byte> data)
|
||||
{
|
||||
private readonly ReadOnlySpan<byte> data = data;
|
||||
public int Offset { get; private set; }
|
||||
public int Remaining => data.Length - Offset;
|
||||
public ReadOnlySpan<byte> Take(int length)
|
||||
{
|
||||
if (length < 0 || length > Remaining)
|
||||
throw new InvalidDataException($"Truncated GAM at {Offset:x}");
|
||||
var result = data.Slice(Offset, length);
|
||||
Offset += length;
|
||||
return result;
|
||||
}
|
||||
public byte Byte() => Take(1)[0];
|
||||
public uint U32() => BinaryPrimitives.ReadUInt32BigEndian(Take(4));
|
||||
public string Text(int length) => Encoding.Latin1.GetString(Take(length));
|
||||
public string Pascal() => Text(Byte());
|
||||
public int Count(int minimumItemBytes)
|
||||
{
|
||||
var count = U32();
|
||||
if (count > Remaining / minimumItemBytes)
|
||||
throw new InvalidDataException($"Count exceeds remaining GAM at {Offset:x}");
|
||||
return (int)count;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user