Make defining property chunks easier

This commit is contained in:
Jarrod Doyle 2024-08-25 13:07:49 +01:00
parent 3b9ff6c852
commit df923492cf
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
3 changed files with 38 additions and 50 deletions

View File

@ -6,32 +6,31 @@ using System.Text;
namespace KeepersCompound.LGS.Database.Chunks;
public class PropertyModelName : IChunk
public class Property
{
public record Property
public int objectId;
public int length;
public virtual void Read(BinaryReader reader)
{
public int objectId;
public int length;
public string modelName;
public Property(BinaryReader reader)
{
objectId = reader.ReadInt32();
length = (int)reader.ReadUInt32();
var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(length)).Replace("\0", string.Empty);
modelName = tmpName[..Math.Min(length - 1, tmpName.Length)];
}
objectId = reader.ReadInt32();
length = (int)reader.ReadUInt32();
}
}
public class PropertyChunk<T> : IChunk where T : Property, new()
{
public ChunkHeader Header { get; set; }
public List<Property> properties;
public List<T> properties;
public void ReadData(BinaryReader reader, DbFile.TableOfContents.Entry entry)
{
properties = new List<Property>();
properties = new List<T>();
while (reader.BaseStream.Position < entry.Offset + entry.Size + 24)
{
properties.Add(new Property(reader));
var prop = new T();
prop.Read(reader);
properties.Add(prop);
}
}
@ -41,36 +40,25 @@ public class PropertyModelName : IChunk
}
}
public class PropertyScale : IChunk
public class PropModelName : Property
{
public record Property
public string modelName;
public override void Read(BinaryReader reader)
{
public int objectId;
public int length;
public Vector3 scale;
public Property(BinaryReader reader)
{
objectId = reader.ReadInt32();
length = (int)reader.ReadUInt32();
scale = reader.ReadVec3();
}
base.Read(reader);
var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(length)).Replace("\0", string.Empty);
modelName = tmpName[..Math.Min(length - 1, tmpName.Length)];
}
}
public ChunkHeader Header { get; set; }
public List<Property> properties;
public class PropScale : Property
{
public Vector3 scale;
public void ReadData(BinaryReader reader, DbFile.TableOfContents.Entry entry)
public override void Read(BinaryReader reader)
{
properties = new List<Property>();
while (reader.BaseStream.Position < entry.Offset + entry.Size + 24)
{
properties.Add(new Property(reader));
}
base.Read(reader);
scale = reader.ReadVec3();
}
public void WriteData(BinaryWriter writer)
{
throw new System.NotImplementedException();
}
}
}

View File

@ -102,8 +102,8 @@ public class DbFile
"TXLIST" => new TxList(),
"WREXT" => new WorldRep(),
"BRLIST" => new BrList(),
"P$ModelName" => new PropertyModelName(),
"P$Scale" => new PropertyScale(),
"P$ModelName" => new PropertyChunk<PropModelName>(),
"P$Scale" => new PropertyChunk<PropScale>(),
"LD$MetaProp" => new LinkDataMetaProp(),
_ when entryName.StartsWith("L$") => new LinkChunk(),
_ => new GenericChunk(),

View File

@ -111,8 +111,8 @@ public partial class Mission : Node3D
)
{
var brList = (BrList)brListRaw;
var modelNames = (PropertyModelName)modelNamesRaw;
var scales = (PropertyScale)scalesRaw;
var modelNames = (PropertyChunk<PropModelName>)modelNamesRaw;
var scales = (PropertyChunk<PropScale>)scalesRaw;
var metaPropLinks = (LinkChunk)metaPropLinksRaw;
var metaPropLinkData = (LinkDataMetaProp)metaPropLinkDataRaw;
@ -136,10 +136,10 @@ public partial class Mission : Node3D
gamFile.Chunks.TryGetValue("P$Scale", out var gamChunk4))
{
GD.Print($"Pre-Merged chunks: {modelNames.properties.Count} {metaPropLinks.links.Count} {metaPropLinkData.linkData.Count}");
modelNames.properties.AddRange(((PropertyModelName)gamChunk1).properties);
modelNames.properties.AddRange(((PropertyChunk<PropModelName>)gamChunk1).properties);
metaPropLinks.links.AddRange(((LinkChunk)gamChunk2).links);
metaPropLinkData.linkData.AddRange(((LinkDataMetaProp)gamChunk3).linkData);
scales.properties.AddRange(((PropertyScale)gamChunk4).properties);
scales.properties.AddRange(((PropertyChunk<PropScale>)gamChunk4).properties);
GD.Print($"Post-Merged chunks: {modelNames.properties.Count} {metaPropLinks.links.Count} {metaPropLinkData.linkData.Count}");
}
}
@ -163,8 +163,8 @@ public partial class Mission : Node3D
private void PlaceObjects(
BrList brList,
PropertyModelName modelNames,
PropertyScale scales,
PropertyChunk<PropModelName> modelNames,
PropertyChunk<PropScale> scales,
LinkChunk metapropLink,
LinkDataMetaProp metaPropLinkData)
{