From f2d751a4c90c64721ae9b5b61b68f1776c2bf277 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 24 Apr 2022 22:28:42 -0500 Subject: [PATCH] Corrected UniqueId implementation. Thanks @Anaminus! --- BinaryFormat/Chunks/PROP.cs | 13 ++++++++----- DataTypes/UniqueId.cs | 16 ++++++++++++++++ Tokens/UniqueId.cs | 24 +++++++++++++++++++++--- 3 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 DataTypes/UniqueId.cs diff --git a/BinaryFormat/Chunks/PROP.cs b/BinaryFormat/Chunks/PROP.cs index 0e6f332..3de5c1a 100644 --- a/BinaryFormat/Chunks/PROP.cs +++ b/BinaryFormat/Chunks/PROP.cs @@ -656,8 +656,10 @@ namespace RobloxFiles.BinaryFormat.Chunks { readProperties(i => { - var buffer = reader.ReadBytes(16); - return new Guid(buffer); + var index = reader.ReadUInt32(); + var time = reader.ReadUInt32(); + var random = reader.ReadUInt64(); + return new UniqueId(index, time, random); }); break; @@ -1293,9 +1295,10 @@ namespace RobloxFiles.BinaryFormat.Chunks { props.ForEach(prop => { - var guid = prop.CastValue(); - byte[] buffer = guid.ToByteArray(); - writer.Write(buffer); + var uniqueId = prop.CastValue(); + writer.Write(uniqueId.Index); + writer.Write(uniqueId.Time); + writer.Write(uniqueId.Random); }); break; diff --git a/DataTypes/UniqueId.cs b/DataTypes/UniqueId.cs new file mode 100644 index 0000000..98ced8c --- /dev/null +++ b/DataTypes/UniqueId.cs @@ -0,0 +1,16 @@ +namespace RobloxFiles.DataTypes +{ + public struct UniqueId + { + public readonly uint Time; + public readonly uint Index; + public readonly ulong Random; + + public UniqueId(uint time, uint index, ulong random) + { + Time = time; + Index = index; + Random = random; + } + } +} diff --git a/Tokens/UniqueId.cs b/Tokens/UniqueId.cs index d25f31a..aa8c197 100644 --- a/Tokens/UniqueId.cs +++ b/Tokens/UniqueId.cs @@ -1,9 +1,10 @@ using System; using System.Xml; +using RobloxFiles.DataTypes; namespace RobloxFiles.Tokens { - public class UniqueId : IXmlPropertyToken + public class UniqueIdToken : IXmlPropertyToken { public string XmlPropertyToken => "UniqueId"; @@ -13,7 +14,13 @@ namespace RobloxFiles.Tokens if (Guid.TryParse(hex, out var guid)) { - prop.Value = guid; + var bytes = guid.ToByteArray(); + var random = BitConverter.ToUInt64(bytes, 0); + + var time = BitConverter.ToUInt32(bytes, 8); + var index = BitConverter.ToUInt32(bytes, 12); + + prop.Value = new UniqueId(time, index, random); return true; } @@ -22,7 +29,18 @@ namespace RobloxFiles.Tokens public void WriteProperty(Property prop, XmlDocument doc, XmlNode node) { - var guid = prop.CastValue(); + var uniqueId = prop.CastValue(); + var random = BitConverter.GetBytes(uniqueId.Random); + + var time = BitConverter.GetBytes(uniqueId.Time); + var index = BitConverter.GetBytes(uniqueId.Index); + + var bytes = new byte[16]; + random.CopyTo(bytes, 0); + time.CopyTo(bytes, 8); + index.CopyTo(bytes, 12); + + var guid = new Guid(bytes); node.InnerText = guid.ToString("N"); } }