2020-07-13 01:19:30 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-09-10 05:08:12 +00:00
|
|
|
|
using System.Text;
|
2019-06-08 03:43:28 +00:00
|
|
|
|
|
|
|
|
|
namespace RobloxFiles.BinaryFormat.Chunks
|
2019-01-26 00:39:37 +00:00
|
|
|
|
{
|
2019-06-08 03:43:28 +00:00
|
|
|
|
public class PRNT : IBinaryFileChunk
|
2019-01-26 00:39:37 +00:00
|
|
|
|
{
|
2020-07-13 01:19:30 +00:00
|
|
|
|
private const byte FORMAT = 0;
|
2020-09-10 05:08:12 +00:00
|
|
|
|
private BinaryRobloxFile File;
|
2019-01-26 00:39:37 +00:00
|
|
|
|
|
2020-07-13 01:19:30 +00:00
|
|
|
|
public void Load(BinaryRobloxFileReader reader)
|
2019-01-26 00:39:37 +00:00
|
|
|
|
{
|
2019-06-08 03:43:28 +00:00
|
|
|
|
BinaryRobloxFile file = reader.File;
|
2020-09-10 05:08:12 +00:00
|
|
|
|
File = file;
|
2019-01-26 00:39:37 +00:00
|
|
|
|
|
2020-07-13 01:19:30 +00:00
|
|
|
|
byte format = reader.ReadByte();
|
|
|
|
|
int idCount = reader.ReadInt32();
|
|
|
|
|
|
|
|
|
|
if (format != FORMAT)
|
|
|
|
|
throw new Exception($"Unexpected PRNT format: {format} (expected {FORMAT}!)");
|
2019-01-29 09:50:55 +00:00
|
|
|
|
|
2020-07-13 01:19:30 +00:00
|
|
|
|
var childIds = reader.ReadInstanceIds(idCount);
|
|
|
|
|
var parentIds = reader.ReadInstanceIds(idCount);
|
2019-06-08 03:43:28 +00:00
|
|
|
|
|
2020-07-13 01:19:30 +00:00
|
|
|
|
for (int i = 0; i < idCount; i++)
|
2019-01-29 09:50:55 +00:00
|
|
|
|
{
|
2020-07-13 01:19:30 +00:00
|
|
|
|
int childId = childIds[i];
|
|
|
|
|
int parentId = parentIds[i];
|
2019-01-29 09:50:55 +00:00
|
|
|
|
|
|
|
|
|
Instance child = file.Instances[childId];
|
2020-10-30 20:07:11 +00:00
|
|
|
|
Instance parent = (parentId >= 0 ? file.Instances[parentId] : file);
|
|
|
|
|
|
|
|
|
|
if (child == null)
|
|
|
|
|
{
|
2021-01-20 20:45:58 +00:00
|
|
|
|
RobloxFile.LogError($"PRNT: could not parent {childId} to {parentId} because child {childId} was null.");
|
2020-10-30 20:07:11 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parentId >= 0 && parent == null)
|
|
|
|
|
{
|
2021-01-20 20:45:58 +00:00
|
|
|
|
RobloxFile.LogError($"PRNT: could not parent {childId} to {parentId} because parent {parentId} was null.");
|
2020-10-30 20:07:11 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-19 04:44:51 +00:00
|
|
|
|
child.Parent = (parentId >= 0 ? file.Instances[parentId] : file);
|
2019-01-29 09:50:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-08 03:43:28 +00:00
|
|
|
|
|
2020-07-13 01:19:30 +00:00
|
|
|
|
public void Save(BinaryRobloxFileWriter writer)
|
2019-06-08 03:43:28 +00:00
|
|
|
|
{
|
2020-09-10 05:08:12 +00:00
|
|
|
|
var file = writer.File;
|
|
|
|
|
File = file;
|
|
|
|
|
|
2020-07-13 01:19:30 +00:00
|
|
|
|
var postInstances = writer.PostInstances;
|
|
|
|
|
var idCount = postInstances.Count;
|
2019-06-08 03:43:28 +00:00
|
|
|
|
|
2020-07-13 01:19:30 +00:00
|
|
|
|
var childIds = new List<int>();
|
|
|
|
|
var parentIds = new List<int>();
|
2019-06-08 03:43:28 +00:00
|
|
|
|
|
2019-06-30 22:01:19 +00:00
|
|
|
|
foreach (Instance inst in writer.PostInstances)
|
2019-06-08 03:43:28 +00:00
|
|
|
|
{
|
|
|
|
|
Instance parent = inst.Parent;
|
|
|
|
|
|
|
|
|
|
int childId = int.Parse(inst.Referent);
|
|
|
|
|
int parentId = -1;
|
|
|
|
|
|
|
|
|
|
if (parent != null)
|
|
|
|
|
parentId = int.Parse(parent.Referent);
|
|
|
|
|
|
2020-07-13 01:19:30 +00:00
|
|
|
|
childIds.Add(childId);
|
|
|
|
|
parentIds.Add(parentId);
|
2019-06-08 03:43:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 01:19:30 +00:00
|
|
|
|
writer.Write(FORMAT);
|
|
|
|
|
writer.Write(idCount);
|
2019-06-08 03:43:28 +00:00
|
|
|
|
|
2020-07-13 01:19:30 +00:00
|
|
|
|
writer.WriteInstanceIds(childIds);
|
|
|
|
|
writer.WriteInstanceIds(parentIds);
|
2019-06-08 03:43:28 +00:00
|
|
|
|
}
|
2020-09-10 05:08:12 +00:00
|
|
|
|
|
|
|
|
|
public void WriteInfo(StringBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
var childIds = new List<int>();
|
|
|
|
|
var parentIds = new List<int>();
|
|
|
|
|
|
|
|
|
|
foreach (Instance inst in File.GetDescendants())
|
|
|
|
|
{
|
|
|
|
|
Instance parent = inst.Parent;
|
|
|
|
|
|
|
|
|
|
int childId = int.Parse(inst.Referent);
|
|
|
|
|
int parentId = -1;
|
|
|
|
|
|
|
|
|
|
if (parent != null)
|
|
|
|
|
parentId = int.Parse(parent.Referent);
|
|
|
|
|
|
|
|
|
|
childIds.Add(childId);
|
|
|
|
|
parentIds.Add(parentId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.AppendLine($"- Format: {FORMAT}");
|
|
|
|
|
builder.AppendLine($"- ChildIds: {string.Join(", ", childIds)}");
|
|
|
|
|
builder.AppendLine($"- ParentIds: {string.Join(", ", parentIds)}");
|
|
|
|
|
}
|
2019-01-26 00:39:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|