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

View File

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

View File

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