public override void Serialize(Stream stream, NomadObject _unused) { Context.Begin(); var bs = (stream as BinaryStream) ?? new BinaryStream(stream); byte[] buffer = null; using (var nb = new BinaryStream(1024)) { base.Serialize(nb, Root); buffer = nb.ToArray(); } var count = Prototypes.Count; bs.Write(buffer.Length + 8); bs.Write(count); bs.Write(buffer); // todo: prototype entries Context.Log("Writing prototypes..."); foreach (var kv in Prototypes) { var uid = kv.Key; var obj = kv.Value; // fail-safe var reference = new NomadReference(obj); var cached = reference.Get(); var ptr = (int)Context.GetPtr(cached); if (ptr == -1) { throw new InvalidDataException("Couldn't get the pointer to a prototype!"); } var info = new EntityPrototypeInfo(obj, ptr); var uidBuffer = (Use64Bit) ? BitConverter.GetBytes(uid) : BitConverter.GetBytes((uint)uid); bs.Write(uidBuffer); info.Serialize(bs); } Context.End(); }
protected void WriteObject_FmtB(BinaryStream stream, NomadObject obj) { Context.State = ContextStateType.Object; Context.ObjectIndex++; var ptr = (int)stream.Position; var idx = NomadCache.Find(obj); if (idx != -1) { var cached = NomadCache.Refs[idx]; var offset = (int)Context.GetPtr(cached); var tag = DescriptorTag.CreateReference(offset, ReferenceType.Offset); tag.WriteTo(stream); } else { var reference = new NomadReference(obj); var cached = reference.Get(); Context.AddRef(cached, ptr); var count = DescriptorTag.Create(obj.Children.Count); count.WriteTo(stream); stream.Write(obj.Id.Hash); WriteAttributesList_FmtB(stream, obj); foreach (var child in obj.Children) { WriteObject_FmtB(stream, child); } } }