public void Serialize(Stream stream, NomadObject _unused) { var bs = (stream as BinaryStream) ?? new BinaryStream(stream); Data.Serialize(bs); }
public override void Serialize(Stream stream, NomadObject data) { Context.Begin(); // make sure the cache is ready NomadCache.Clear(); var bs = (stream as BinaryStream) ?? new BinaryStream(stream); Context.Log("Serializing data..."); WriteHeader(bs, data); switch (Format) { case FormatType.Resource: case FormatType.Objects: WriteObject_FmtA(bs, data); break; case FormatType.Entities: DescriptorTag.GlobalFlags |= DescriptorFlags.Use24Bit; WriteObject_FmtB(bs, data); DescriptorTag.GlobalFlags &= ~DescriptorFlags.Use24Bit; break; } Context.End(); }
public override void Serialize(Stream stream, NomadObject data) { Context.Begin(); var elem = CreateXmlElement(data); var xDoc = new XDocument(elem); using (var writer = XmlWriter.Create(stream, Utils.XMLWriterSettings)) { try { xDoc.WriteTo(writer); } catch (Exception e) { Context.Log($"**** XML save error : '{e.Message}' ****"); Context.Log(e.StackTrace); } finally { writer.Flush(); } } Context.End(); }
public NomadObject ReadRmlObject(XElement rml, NomadObject parent) { var result = new NomadObject(true) { Id = rml.Name.LocalName, }; foreach (var rmlAttr in rml.Attributes()) { var value = rmlAttr.Value; var attr = new NomadValue(DataType.RML, rmlAttr.Value) { Id = rmlAttr.Name.LocalName, }; result.Attributes.Add(attr); } foreach (var rmlChild in rml.Elements()) { ReadRmlObject(rmlChild, result); } if (parent != null) { parent.Children.Add(result); } return(result); }
public void LoadXml(string filename) { var srl = new NomadXmlSerializer(); NomadObject root = null; using (var fs = File.Open(filename, FileMode.Open)) { root = srl.Deserialize(fs); } Version = int.Parse(root.GetAttributeValue("Version")); MoveCount = int.Parse(root.GetAttributeValue("Count")); var resources = new List <MoveResourceData>(); var infos = new List <MoveResourceInfo>(); var size = 0; foreach (var child in root.Children) { var resource = new MoveResourceData(); resource.Deserialize(child); resources.Add(resource); var info = resource.GetInfo(); infos.Add(info); size += info.Size; } var buffer = new byte[size]; var offset = 0; for (int i = 0; i < resources.Count; i++) { var resource = resources[i]; var info = infos[i]; foreach (var param in resource.AllParams) { var globalParam = AllParams.First((p) => p.Name == param.Name); globalParam.Values.AddRange(param.Values); foreach (var fixup in param.Fixups) { var fixupInfo = new FixupInfo(fixup.Hash, fixup.Offset + offset); globalParam.Fixups.Add(fixupInfo); } } Buffer.BlockCopy(resource.MoveData, 0, buffer, offset, info.Size); offset += info.Size; } PerMoveResourceInfo.Infos = infos; MoveData = buffer; }
protected bool WriteAttributesList_FmtB(BinaryStream stream, NomadObject obj) { var ptr = (int)stream.Position; byte[] buffer = new byte[0]; using (var bs = new BinaryStream(1024)) { WriteAttributesData_FmtB(bs, obj.Attributes, ptr); buffer = bs.ToArray(); } var length = buffer.Length; if (length > 65535) { throw new InvalidOperationException("Attribute data too large."); } stream.Write((short)length); stream.Write(buffer, 0, length); // did we write any data? return(length > 0); }
protected NomadValue ReadRmlAttribute(BinaryStream _stream, NomadObject parent = null) { Context.State = ContextStateType.Member; Context.ObjectIndex++; var unk = (byte)_stream.ReadByte(); if (unk != 0) { throw new InvalidOperationException("Invalid RML attribute data."); } var nameIdx = DescriptorTag.Read(_stream, ReferenceType.Index); var valIdx = DescriptorTag.Read(_stream, ReferenceType.Index); var buffer = Utils.GetStringBuffer(_strings[valIdx]); var result = new NomadValue(DataType.RML, buffer) { Id = _strings[nameIdx], }; if (parent != null) { parent.Attributes.Add(result); } return(result); }
void ReadInfos(NomadObject obj) { var sizesAttr = obj.GetAttribute("sizes"); var rootNodeIdsAttr = obj.GetAttribute("rootNodeIds"); var sizes = Utils.UnpackArray(sizesAttr, BitConverter.ToInt32, 4, out int nSizes); var rootNodeIds = Utils.UnpackArray(rootNodeIdsAttr, BitConverter.ToInt32, 4, out int nRootNodeIds); if (nSizes != nRootNodeIds) { throw new InvalidDataException("Yikes!"); } var count = (nSizes & nRootNodeIds); for (int i = 0; i < count; i++) { var size = sizes[i]; var rootNodeId = rootNodeIds[i]; var info = new MoveResourceInfo(rootNodeId, size); Infos.Add(info); } }
public NomadObject ToObject() { var obj = new NomadObject("MoveResource"); Serialize(obj); return(obj); }
public void Deserialize(NomadObject root) { var obj = root.GetChild("PerMoveResourceInfo"); if (obj != null) { ReadInfos(obj); } }
public NomadObject ToObject() { var obj = new NomadObject("FIXUP"); obj.SetAttributeValue("offset", DataType.BinHex, $"#{Offset:X8}"); obj.SetAttributeValue("hash", DataType.BinHex, $"#{Hash:X8}"); return(obj); }
protected NomadValue ReadAttribute_FmtA(BinaryStream stream, NomadObject parent) { Context.State = ContextStateType.Member; Context.MemberIndex++; var hash = stream.ReadInt32(); return(ReadAttributeData(stream, parent, hash)); }
public void Deserialize(NomadObject root) { RootNodeId = Utils.UnpackData(root.GetAttribute("rootNodeId"), BitConverter.ToInt32); MoveData = root.GetAttribute("data").Data.Buffer; foreach (var param in AllParams) { param.Deserialize(root); } }
FixupInfo ParseFixup(NomadObject obj) { var offsetAttr = obj.GetAttribute("offset"); var hashAttr = obj.GetAttribute("hash"); var offset = Utils.UnpackData(offsetAttr, BitConverter.ToInt32); var hash = Utils.UnpackData(hashAttr, BitConverter.ToInt32); return(new FixupInfo(hash, offset)); }
protected NomadObject ReadMapData(BinaryStream stream) { var check = stream.ReadInt32(); var result = new NomadObject() { Id = "FCXMapData" }; if (check != 0x26) { // non-editor map? stream.Position -= 4; UID = stream.Read <Guid>(); ReadPadding(stream, 1); Version = 0; IsUserMap = false; } else { var magic = stream.ReadInt32(); if (magic != MGX_MAPDATA) { throw new InvalidDataException("Invalid FCX map data -- bad data magic!"); } Version = check; IsUserMap = true; ReadPadding(stream, 3); MetaData = ReadFCBChunk(stream, result); ConfigData = ReadFCBChunk(stream, result); ReadPadding(stream, 5); var thumbSize = stream.ReadInt32(); if (thumbSize > 0) { ThumbData = stream.ReadBytes(thumbSize); } ReadPadding(stream, 1); } var mapSize = (int)(stream.Length - stream.Position); MapData = stream.ReadBytes(mapSize); return(result); }
public static ResourceFile Create(NomadObject obj, FileType type, ResourceFilter filter = null) { var result = new ResourceFile() { Root = obj, Type = type, CustomFilter = filter, }; return(result); }
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(); }
public static ResourceFilter GetFilter(NomadObject root) { foreach (var filter in _filters) { if (filter.Matches(root)) { return(filter); } } return(null); }
public void Serialize(NomadObject root) { root.SetAttributeValue("rootNodeId", DataType.BinHex, $"#{RootNodeId:X8}"); root.SetAttributeValue("data", DataType.BinHex, Utils.Bytes2HexString(MoveData)); foreach (var param in AllParams) { if (!param.IsEmpty) { param.Serialize(root); } } }
protected NomadValue ReadAttributeData(BinaryStream stream, NomadObject parent, int hash) { var ptr = (int)stream.Position; var id = StringId.Parse(hash); var type = (id == "RML_DATA") ? DataType.RML : AttributeTypes.GetBestType(id, parent.Id); var data = AttributeData.Read(stream, type); switch (type) { case DataType.BinHex: { if (data.Type == DataType.String) { StringId fullType = $"{parent.Id}.{id}"; if (!AttributeTypes.IsTypeKnown(fullType)) { AttributeTypes.RegisterGuessType(fullType, data.Type); } } } break; case DataType.String: { if (data.Type == DataType.BinHex) { StringId fullType = $"{parent.Id}.{id}"; Context.LogDebug($"**** Attribute '{fullType}' was incorrectly assumed to be a String! ****"); } } break; } var result = new NomadValue() { Id = id, Data = data, }; if (parent != null) { parent.Attributes.Add(result); } return(result); }
public void Deserialize(NomadObject root) { var values = root.GetChild(ValuesId); var fixups = root.GetChild(FixupsId); if (values != null) { Values.AddRange(values.Children); } if (fixups != null) { ReadFixups(fixups); } }
public static ResourceFile Create <TSerializer>(NomadObject obj, ResourceFilter filter = null) where TSerializer : INomadSerializer, new() { var serializer = new TSerializer(); var result = new ResourceFile() { Root = obj, Type = serializer.Type, CustomSerializer = serializer, }; return(result); }
public MapDataUnpacker(NomadObject obj) { foreach (var child in obj) { var id = child.Id; if (child.IsAttribute) { _values.Add(id.Name, child as NomadValue); } else { _objects.Add(id.Name, child as NomadObject); } } }
protected void WriteObject_FmtA(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 tag = DescriptorTag.CreateReference(Context.GetIdx(cached), ReferenceType.Index); tag.WriteTo(stream); } else { var nChildren = DescriptorTag.Create(obj.Children.Count); var nAttributes = DescriptorTag.Create(obj.Attributes.Count); Context.AddRef(obj, ptr); nChildren.WriteTo(stream); stream.Write(obj.Id.Hash); if (obj.IsRml) { WriteRmlData(stream, obj); } else { nAttributes.WriteTo(stream); Context.State = ContextStateType.Member; foreach (var attr in obj.Attributes) { WriteAttribute_FmtA(stream, attr); } foreach (var child in obj.Children) { WriteObject_FmtA(stream, child); } } } }
public XElement CreateXmlElement(NomadObject obj, XElement parent = null) { Context.State = ContextStateType.Object; Context.ObjectIndex++; var name = XName.Get(obj.Id); var elem = new XElement(name); if (parent != null) { parent.Add(elem); } foreach (var attr in obj.Attributes) { if (attr.Id == "RML_DATA") { using (var bs = new BinaryStream(attr.Data.Buffer)) { var rmlSize = bs.ReadInt32(); var rmlBuffer = bs.ReadBytes(rmlSize); using (var rs = new BinaryStream(rmlBuffer)) { var rmlData = new NomadRmlSerializer(); var rml = rmlData.Deserialize(rs); var rmlRoot = new XElement("RML_DATA"); var rmlElem = CreateXmlElement(rml, rmlRoot); elem.Add(rmlRoot); } } } else { CreateXmlAttribute(attr, elem); } } foreach (var child in obj.Children) { CreateXmlElement(child, elem); } return(elem); }
void ReadFixups(NomadObject obj) { var offsetsAttr = obj.GetAttribute("offsetsArray"); var hashesAttr = obj.GetAttribute("hashesArray"); if ((offsetsAttr != null) && (hashesAttr != null)) { var offsets = Utils.UnpackArray(offsetsAttr, BitConverter.ToInt32, 4, out int nOffsets); var hashes = Utils.UnpackArray(hashesAttr, BitConverter.ToInt32, 4, out int nHashes); if (nOffsets != nHashes) { throw new InvalidDataException("Yikes!"); } // doesn't matter which one we use // but we'll be verbose anyways :) var count = (nOffsets & nHashes); Fixups = new List <FixupInfo>(count); for (int i = 0; i < count; i++) { var offset = offsets[i]; var hash = hashes[i]; var fixup = new FixupInfo(hash, offset); Fixups.Add(fixup); } } else { foreach (var child in obj.Children) { if (child.Id != "FIXUP") { throw new InvalidDataException($"Expected a FIXUP but got '{child.Id}' instead."); } var fixup = ParseFixup(child); Fixups.Add(fixup); } } }
public void Serialize(NomadObject root) { var values = new NomadObject(ValuesId); var fixups = new NomadObject(FixupsId); values.Children.AddRange(Values); foreach (var fixup in Fixups) { var obj = fixup.ToObject(); fixups.Children.Add(obj); } root.Children.Add(values); root.Children.Add(fixups); }
public override NomadObject Deserialize(Stream stream) { Context.Begin(); var bs = (stream as BinaryStream) ?? new BinaryStream(stream); Context.Log("Deserializing data..."); var magic = bs.ReadInt32(); if (magic != Nomad.Magic) { throw new InvalidOperationException("Invalid binary data -- bad magic!"); } Format = (FormatType)bs.ReadInt16(); var flags = bs.ReadInt16() & 1; var nElems = bs.ReadInt32(); var nAttrs = bs.ReadInt32(); NomadObject result = null; switch (Format) { case FormatType.Resource: case FormatType.Objects: result = ReadObject_FmtA(bs); break; case FormatType.Entities: // ugly hacks :( DescriptorTag.GlobalFlags |= DescriptorFlags.Use24Bit; result = ReadObject_FmtB(bs); DescriptorTag.GlobalFlags &= ~DescriptorFlags.Use24Bit; break; } Context.End(); return(result); }
protected NomadObject ReadFCBChunk(BinaryStream stream, NomadObject parent) { var fcbSize = stream.ReadInt32(); var fcbData = stream.ReadBytes(fcbSize); using (var bs = new BinaryStream(fcbData)) { var serializer = new NomadResourceSerializer(); var root = serializer.Deserialize(bs); if (parent != null) { parent.Children.Add(root); } return(root); } }
protected List <NomadValue> ReadAttributes_FmtB(BinaryStream stream, NomadObject parent, IEnumerable <int> hashes) { Context.State = ContextStateType.Member; var result = new List <NomadValue>(); foreach (var hash in hashes) { Context.MemberIndex++; var attrPtr = (int)stream.Position; var attr = ReadAttributeData(stream, parent, hash); result.Add(attr); } return(result); }