Added write support for binary files!
Holy cow, this took a lot of work. I think I may need to do a few more things before I consider this a 1.0 release, but I'm glad to have finally overcome this hurdle!
This commit is contained in:
@ -1,40 +1,111 @@
|
||||
namespace RobloxFiles.BinaryFormat.Chunks
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace RobloxFiles.BinaryFormat.Chunks
|
||||
{
|
||||
public class INST
|
||||
public class INST : IBinaryFileChunk
|
||||
{
|
||||
public readonly int TypeIndex;
|
||||
public readonly string TypeName;
|
||||
public readonly bool IsService;
|
||||
public readonly int NumInstances;
|
||||
public readonly int[] InstanceIds;
|
||||
public int TypeIndex { get; internal set; }
|
||||
public string TypeName { get; internal set; }
|
||||
|
||||
public bool IsService { get; internal set; }
|
||||
public List<bool> RootedServices { get; internal set; }
|
||||
|
||||
public int NumInstances { get; internal set; }
|
||||
public List<int> InstanceIds { get; internal set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return TypeName;
|
||||
}
|
||||
|
||||
public INST(BinaryRobloxFileChunk chunk)
|
||||
public void LoadFromReader(BinaryRobloxFileReader reader)
|
||||
{
|
||||
using (BinaryRobloxFileReader reader = chunk.GetDataReader())
|
||||
{
|
||||
TypeIndex = reader.ReadInt32();
|
||||
TypeName = reader.ReadString();
|
||||
IsService = reader.ReadBoolean();
|
||||
BinaryRobloxFile file = reader.File;
|
||||
|
||||
NumInstances = reader.ReadInt32();
|
||||
InstanceIds = reader.ReadInstanceIds(NumInstances);
|
||||
TypeIndex = reader.ReadInt32();
|
||||
TypeName = reader.ReadString();
|
||||
IsService = reader.ReadBoolean();
|
||||
|
||||
NumInstances = reader.ReadInt32();
|
||||
InstanceIds = reader.ReadInstanceIds(NumInstances);
|
||||
|
||||
if (IsService)
|
||||
{
|
||||
RootedServices = new List<bool>();
|
||||
|
||||
for (int i = 0; i < NumInstances; i++)
|
||||
{
|
||||
bool isRooted = reader.ReadBoolean();
|
||||
RootedServices.Add(isRooted);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Allocate(BinaryRobloxFile file)
|
||||
{
|
||||
foreach (int instId in InstanceIds)
|
||||
for (int i = 0; i < NumInstances; i++)
|
||||
{
|
||||
Instance inst = new Instance() { ClassName = TypeName };
|
||||
int instId = InstanceIds[i];
|
||||
|
||||
var inst = new Instance()
|
||||
{
|
||||
ClassName = TypeName,
|
||||
IsService = IsService,
|
||||
Referent = instId.ToString()
|
||||
};
|
||||
|
||||
if (IsService)
|
||||
{
|
||||
bool rooted = RootedServices[i];
|
||||
inst.IsRootedService = rooted;
|
||||
}
|
||||
|
||||
file.Instances[instId] = inst;
|
||||
}
|
||||
|
||||
file.Types[TypeIndex] = this;
|
||||
}
|
||||
|
||||
public BinaryRobloxFileChunk SaveAsChunk(BinaryRobloxFileWriter writer)
|
||||
{
|
||||
writer.StartWritingChunk(this);
|
||||
|
||||
writer.Write(TypeIndex);
|
||||
writer.WriteString(TypeName);
|
||||
|
||||
writer.Write(IsService);
|
||||
writer.Write(NumInstances);
|
||||
writer.WriteInstanceIds(InstanceIds);
|
||||
|
||||
if (IsService)
|
||||
{
|
||||
BinaryRobloxFile file = writer.File;
|
||||
|
||||
foreach (int instId in InstanceIds)
|
||||
{
|
||||
Instance service = file.Instances[instId];
|
||||
writer.Write(service.IsRootedService);
|
||||
}
|
||||
}
|
||||
|
||||
return writer.FinishWritingChunk();
|
||||
}
|
||||
|
||||
internal static void ApplyTypeMap(BinaryRobloxFileWriter writer)
|
||||
{
|
||||
BinaryRobloxFile file = writer.File;
|
||||
file.Instances = writer.Instances.ToArray();
|
||||
|
||||
var types = writer.TypeMap
|
||||
.OrderBy(type => type.Key)
|
||||
.Select(type => type.Value)
|
||||
.ToArray();
|
||||
|
||||
for (int i = 0; i < types.Length; i++, file.NumTypes++)
|
||||
{
|
||||
INST type = types[i];
|
||||
type.TypeIndex = i;
|
||||
}
|
||||
|
||||
file.Types = types;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user