private static void TraverseMap(XmlNode node, PsoBuilder pb, PsoStructureEntryInfo entry, PsoStructureInfo infos, byte[] data, PsoArrayResults arrayResults) { var mapidx1 = entry.ReferenceKey & 0x0000FFFF; var mapidx2 = (entry.ReferenceKey >> 16) & 0x0000FFFF; var mapreftype1 = infos.Entries[mapidx2]; var mapreftype2 = infos.Entries[mapidx1]; if (mapreftype2.ReferenceKey != 0) { } var xStruct = pb.AddMapNodeStructureInfo((MetaName)mapreftype2.ReferenceKey); var xName = xStruct.IndexInfo.NameHash; var kEntry = xStruct?.FindEntry(MetaName.Key); var iEntry = xStruct?.FindEntry(MetaName.Item); if (kEntry.Type != PsoDataType.String) { } List <byte[]> nodesData = new List <byte[]>(); foreach (XmlNode cnode in node.ChildNodes) { var kattr = cnode.Attributes["key"].Value; var tattr = cnode.Attributes["type"].Value;//CW invention for convenience..! var khash = (MetaName)(uint)GetHash(kattr); var thash = (MetaName)(uint)GetHash(tattr); byte[] strucBytes = Traverse(cnode, pb, thash); byte[] nodeBytes = new byte[xStruct.StructureLength]; TraverseStringRaw(kattr, pb, kEntry, nodeBytes); //write the key if (xName != (MetaName)MetaTypeName.ARRAYINFO) // (mapreftype2.ReferenceKey != 0) { //value struct embedded in ARRAYINFO node Buffer.BlockCopy(strucBytes, 0, nodeBytes, iEntry.DataOffset, strucBytes.Length); } else { //normal ARRAYINFO with pointer value var itemptr = pb.AddItemPtr(thash, strucBytes); itemptr.SwapEnd(); //big schmigg var ptrbytes = MetaTypes.ConvertToBytes(itemptr); Buffer.BlockCopy(ptrbytes, 0, nodeBytes, iEntry.DataOffset, ptrbytes.Length); } nodesData.Add(nodeBytes); } Write(0x1000000, data, entry.DataOffset); Write(0, data, entry.DataOffset + 4); arrayResults.Structures[entry.DataOffset + 8] = pb.AddItemArrayPtr(xName, nodesData.ToArray()); //pb.AddPointerArray(nodeptrs); }
private void WriteBytes <T>(T val, int offset) where T : struct { var data = MetaTypes.ConvertToBytes(val); for (int i = 0; i < data.Length; i++) { Bytes[offset + i] = data[i]; } }
private static void TraverseStringRaw(string str, PsoBuilder pb, PsoStructureEntryInfo entry, byte[] data) { switch (entry.Unk_5h) { default: break; case 0: var str0len = (int)((entry.ReferenceKey >> 16) & 0xFFFF); if (!string.IsNullOrEmpty(str)) { byte[] strdata = Encoding.ASCII.GetBytes(str); Buffer.BlockCopy(strdata, 0, data, entry.DataOffset, strdata.Length); if (strdata.Length > str0len) { } } break; case 1: case 2: if (!string.IsNullOrEmpty(str)) { var bptr = pb.AddString(str); var ptr = new PsoPOINTER(bptr.BlockID, bptr.Offset); ptr.SwapEnd(); var val = MetaTypes.ConvertToBytes(ptr); Buffer.BlockCopy(val, 0, data, entry.DataOffset, val.Length); } break; case 3: if (!string.IsNullOrEmpty(str)) { var bptr = pb.AddString(str); var ptr = new CharPointer(bptr.Pointer, str.Length); ptr.SwapEnd(); var val = MetaTypes.ConvertToBytes(ptr); Buffer.BlockCopy(val, 0, data, entry.DataOffset, val.Length); } break; case 7: //hash only? case 8: //hash with STRF entry? var hashVal = string.IsNullOrEmpty(str) ? 0 : GetHash(str); Write(hashVal, data, entry.DataOffset); if (entry.Unk_5h == 8) { pb.AddStringToSTRF(str); } break; } }
public void WriteArrays(byte[] data) { foreach (KeyValuePair <int, Array_Structure> ptr in Structures) { var _data = MetaTypes.ConvertToBytes(ptr.Value); Buffer.BlockCopy(_data, 0, data, ptr.Key, _data.Length); } foreach (KeyValuePair <int, Array_StructurePointer> ptr in StructurePointers) { var _data = MetaTypes.ConvertToBytes(ptr.Value); Buffer.BlockCopy(_data, 0, data, ptr.Key, _data.Length); } foreach (KeyValuePair <int, Array_uint> ptr in UInts) { var _data = MetaTypes.ConvertToBytes(ptr.Value); Buffer.BlockCopy(_data, 0, data, ptr.Key, _data.Length); } foreach (KeyValuePair <int, Array_ushort> ptr in UShorts) { var _data = MetaTypes.ConvertToBytes(ptr.Value); Buffer.BlockCopy(_data, 0, data, ptr.Key, _data.Length); } foreach (KeyValuePair <int, Array_byte> ptr in UBytes) { var _data = MetaTypes.ConvertToBytes(ptr.Value); Buffer.BlockCopy(_data, 0, data, ptr.Key, _data.Length); } foreach (KeyValuePair <int, Array_float> ptr in Floats) { var _data = MetaTypes.ConvertToBytes(ptr.Value); Buffer.BlockCopy(_data, 0, data, ptr.Key, _data.Length); } foreach (KeyValuePair <int, Array_Vector3> ptr in Float_XYZs) { var _data = MetaTypes.ConvertToBytes(ptr.Value); Buffer.BlockCopy(_data, 0, data, ptr.Key, _data.Length); } foreach (KeyValuePair <int, Array_uint> ptr in Hashes) { var _data = MetaTypes.ConvertToBytes(ptr.Value); Buffer.BlockCopy(_data, 0, data, ptr.Key, _data.Length); } }
public MetaBuilderPointer AddItem <T>(MetaName type, T item) where T : struct { MetaBuilderBlock block = EnsureBlock(type); byte[] data = MetaTypes.ConvertToBytes(item); int brem = data.Length % 16; if (brem > 0) { int newlen = data.Length - brem + 16; byte[] newdata = new byte[newlen]; Buffer.BlockCopy(data, 0, newdata, 0, data.Length); data = newdata; //make sure item size is multiple of 16... so pointers don't need sub offsets! } int idx = block.AddItem(data); MetaBuilderPointer r = new MetaBuilderPointer(); r.Block = block.Index + 1; r.Offset = (idx * data.Length) / 16; r.Length = data.Length; return(r); }
private static byte[] Traverse(XmlNode node, PsoBuilder pb, MetaName type = 0, bool isRoot = false) { if (type == 0) { type = (MetaName)(uint)GetHash(node.Name); } var infos = PsoTypes.GetStructureInfo(type); if (infos != null) { byte[] data = new byte[infos.StructureLength]; var arrayResults = new PsoArrayResults(); arrayResults.Structures = new Dictionary <int, Array_Structure>(); arrayResults.StructurePointers = new Dictionary <int, Array_StructurePointer>(); arrayResults.UInts = new Dictionary <int, Array_uint>(); arrayResults.UShorts = new Dictionary <int, Array_ushort>(); arrayResults.UBytes = new Dictionary <int, Array_byte>(); arrayResults.Floats = new Dictionary <int, Array_float>(); arrayResults.Float_XYZs = new Dictionary <int, Array_Vector3>(); arrayResults.Hashes = new Dictionary <int, Array_uint>(); Array.Clear(data, 0, infos.StructureLength); //shouldn't really be necessary... PsoStructureEntryInfo arrEntry = null; //if (isRoot) //{ // pb.EnsureBlock(type); //} for (int i = 0; i < infos.Entries.Length; i++) { var entry = infos.Entries[i]; var cnode = GetEntryNode(node.ChildNodes, entry.EntryNameHash); if (entry.EntryNameHash == (MetaName)MetaTypeName.ARRAYINFO) { arrEntry = entry; continue; } if (cnode == null) { //warning: node not found in XML for this entry! continue; } switch (entry.Type) { case PsoDataType.Array: { TraverseArray(cnode, pb, entry, arrEntry, arrayResults, data, infos); break; } case PsoDataType.Structure: { var stype = (MetaName)entry.ReferenceKey; if (stype == 0) { var stypestr = Xml.GetStringAttribute(cnode, "type"); if (!string.IsNullOrEmpty(stypestr)) { stype = (MetaName)(uint)GetHash(stypestr); } } var struc = Traverse(cnode, pb, stype); if (struc != null) { switch (entry.Unk_5h) { default: //ErrorXml(sb, cind, ename + ": Unexpected Structure subtype: " + entry.Unk_5h.ToString()); break; case 0: //default structure Buffer.BlockCopy(struc, 0, data, entry.DataOffset, struc.Length); break; case 3: //structure pointer... case 4: //also pointer? what's the difference? var bptr = pb.AddItem(stype, struc); var ptr = new PsoPOINTER(bptr.BlockID, bptr.Offset); ptr.SwapEnd(); var ptrb = MetaTypes.ConvertToBytes(ptr); Buffer.BlockCopy(ptrb, 0, data, entry.DataOffset, ptrb.Length); break; } } else { } break; } case PsoDataType.Map: { TraverseMap(cnode, pb, entry, infos, data, arrayResults); break; } case PsoDataType.Bool: { byte val = (cnode.Attributes["value"].Value == "false") ? (byte)0 : (byte)1; data[entry.DataOffset] = val; break; } case PsoDataType.SByte: { var val = Convert.ToSByte(cnode.Attributes["value"].Value); data[entry.DataOffset] = (byte)val; break; } case PsoDataType.UByte: { var val = Convert.ToByte(cnode.Attributes["value"].Value); data[entry.DataOffset] = val; break; } case PsoDataType.SShort: { var val = Convert.ToInt16(cnode.Attributes["value"].Value); Write(val, data, entry.DataOffset); break; } case PsoDataType.UShort: { var val = Convert.ToUInt16(cnode.Attributes["value"].Value); Write(val, data, entry.DataOffset); break; } case PsoDataType.SInt: { var val = Convert.ToInt32(cnode.Attributes["value"].Value); Write(val, data, entry.DataOffset); break; } case PsoDataType.UInt: { switch (entry.Unk_5h) { default: //ErrorXml(sb, cind, ename + ": Unexpected Integer subtype: " + entry.Unk_5h.ToString()); break; case 0: //signed int (? flags?) var sval = Convert.ToInt32(cnode.Attributes["value"].Value); Write(sval, data, entry.DataOffset); break; case 1: //unsigned int var ustr = cnode.Attributes["value"].Value; uint uval = 0; if (ustr.StartsWith("0x")) { ustr = ustr.Substring(2); uval = Convert.ToUInt32(ustr, 16); } else { uval = Convert.ToUInt32(ustr); } Write(uval, data, entry.DataOffset); break; } break; } case PsoDataType.Float: { float val = FloatUtil.Parse(cnode.Attributes["value"].Value); Write(val, data, entry.DataOffset); break; } case PsoDataType.Float2: { float x = FloatUtil.Parse(cnode.Attributes["x"].Value); float y = FloatUtil.Parse(cnode.Attributes["y"].Value); Write(x, data, entry.DataOffset); Write(y, data, entry.DataOffset + sizeof(float)); break; } case PsoDataType.Float3: { float x = FloatUtil.Parse(cnode.Attributes["x"].Value); float y = FloatUtil.Parse(cnode.Attributes["y"].Value); float z = FloatUtil.Parse(cnode.Attributes["z"].Value); Write(x, data, entry.DataOffset); Write(y, data, entry.DataOffset + sizeof(float)); Write(z, data, entry.DataOffset + sizeof(float) * 2); break; } case PsoDataType.Float4: { float x = FloatUtil.Parse(cnode.Attributes["x"].Value); float y = FloatUtil.Parse(cnode.Attributes["y"].Value); float z = FloatUtil.Parse(cnode.Attributes["z"].Value); float w = FloatUtil.Parse(cnode.Attributes["w"].Value); Write(x, data, entry.DataOffset); Write(y, data, entry.DataOffset + sizeof(float)); Write(z, data, entry.DataOffset + sizeof(float) * 2); Write(w, data, entry.DataOffset + sizeof(float) * 3); break; } case PsoDataType.String: { TraverseString(cnode, pb, entry, data); break; } case PsoDataType.Enum: { pb.AddEnumInfo((MetaName)entry.ReferenceKey); switch (entry.Unk_5h) { default: //ErrorXml(sb, cind, ename + ": Unexpected Enum subtype: " + entry.Unk_5h.ToString()); break; case 0: //int enum int ival = GetEnumInt((MetaName)entry.ReferenceKey, cnode.InnerText, entry.Type); Write(ival, data, entry.DataOffset); break; case 1: //short enum? short sval = (short)GetEnumInt((MetaName)entry.ReferenceKey, cnode.InnerText, entry.Type); Write(sval, data, entry.DataOffset); break; case 2: //byte enum byte bval = (byte)GetEnumInt((MetaName)entry.ReferenceKey, cnode.InnerText, entry.Type); data[entry.DataOffset] = bval; break; } break; } case PsoDataType.Flags: { //uint fCount = (entry.ReferenceKey >> 16) & 0x0000FFFF; uint fEntry = (entry.ReferenceKey & 0xFFF); var fEnt = (fEntry != 0xFFF) ? infos.GetEntry((int)fEntry) : null; PsoEnumInfo flagsInfo = null; MetaName fEnum = (MetaName)(fEnt?.ReferenceKey ?? 0); if ((fEnt != null) && (fEnt.EntryNameHash == (MetaName)MetaTypeName.ARRAYINFO)) { flagsInfo = PsoTypes.GetEnumInfo(fEnum); } if (flagsInfo == null) { if (fEntry != 0xFFF) { } //flagsInfo = PsoTypes.GetEnumInfo(entry.EntryNameHash); } if (flagsInfo != null) { pb.AddEnumInfo(flagsInfo.IndexInfo.NameHash); } else { } //error? switch (entry.Unk_5h) { default: //ErrorXml(sb, cind, ename + ": Unexpected Flags subtype: " + entry.Unk_5h.ToString()); break; case 0: //int flags int ival = GetEnumInt(fEnum, cnode.InnerText, entry.Type); Write(ival, data, entry.DataOffset); break; case 1: //short flags short sval = (short)GetEnumInt(fEnum, cnode.InnerText, entry.Type); Write(sval, data, entry.DataOffset); break; case 2: //byte flags byte bval = (byte)GetEnumInt(fEnum, cnode.InnerText, entry.Type); data[entry.DataOffset] = bval; break; } break; } case PsoDataType.Float3a: { float x = FloatUtil.Parse(cnode.Attributes["x"].Value); float y = FloatUtil.Parse(cnode.Attributes["y"].Value); float z = FloatUtil.Parse(cnode.Attributes["z"].Value); Write(x, data, entry.DataOffset); Write(y, data, entry.DataOffset + sizeof(float)); Write(z, data, entry.DataOffset + sizeof(float) * 2); break; } case PsoDataType.Float4a: { float x = FloatUtil.Parse(cnode.Attributes["x"].Value); float y = FloatUtil.Parse(cnode.Attributes["y"].Value); float z = FloatUtil.Parse(cnode.Attributes["z"].Value); //float w = FloatUtil.Parse(cnode.Attributes["w"].Value); Write(x, data, entry.DataOffset); Write(y, data, entry.DataOffset + sizeof(float)); Write(z, data, entry.DataOffset + sizeof(float) * 2); //Write(w, data, entry.DataOffset + sizeof(float) * 3); break; } case PsoDataType.HFloat: { var val = Convert.ToInt16(cnode.Attributes["value"].Value); Write(val, data, entry.DataOffset); break; } case PsoDataType.Long: { var uval = Convert.ToUInt64(cnode.Attributes["value"].Value); Write(uval, data, entry.DataOffset); break; } default: break; } } arrayResults.WriteArrays(data); pb.AddStructureInfo(infos.IndexInfo.NameHash); if (isRoot) { pb.RootPointer = pb.AddItem(type, data); } return(data); } else { } //info not found return(null); }
private static byte[] Traverse(XmlNode node, MetaBuilder mb, MetaName type = 0, bool isRoot = false) { if (type == 0) { type = (MetaName)(uint)GetHash(node.Name); } var infos = MetaTypes.GetStructureInfo(type); if (infos != null) { byte[] data = new byte[infos.StructureSize]; var arrayResults = new ArrayResults(); arrayResults.Structures = new Dictionary <int, Array_Structure>(); arrayResults.StructurePointers = new Dictionary <int, Array_StructurePointer>(); arrayResults.UInts = new Dictionary <int, Array_uint>(); arrayResults.UShorts = new Dictionary <int, Array_ushort>(); arrayResults.UBytes = new Dictionary <int, Array_byte>(); arrayResults.Floats = new Dictionary <int, Array_float>(); arrayResults.Float_XYZs = new Dictionary <int, Array_Vector3>(); arrayResults.Hashes = new Dictionary <int, Array_uint>(); Array.Clear(data, 0, infos.StructureSize); MetaStructureEntryInfo_s arrEntry = new MetaStructureEntryInfo_s(); if (isRoot) { mb.EnsureBlock(type); } for (int i = 0; i < infos.Entries.Length; i++) { var entry = infos.Entries[i]; var cnode = GetEntryNode(node.ChildNodes, entry); if (entry.EntryNameHash == MetaName.ARRAYINFO) { arrEntry = entry; continue; } if (cnode == null) { continue; } switch (entry.DataType) { case MetaStructureEntryDataType.Array: { TraverseArray(cnode, mb, arrEntry, entry.DataOffset, arrayResults); break; } case MetaStructureEntryDataType.ArrayOfBytes: { GetParsedArrayOfBytes(cnode, data, entry, arrEntry); break; } case MetaStructureEntryDataType.ArrayOfChars: { int offset = entry.DataOffset; var split = Split(cnode.InnerText, 2); for (int j = 0; j < split.Length; j++) { byte val = Convert.ToByte(split[j], 16); data[offset] = val; offset += sizeof(byte); } break; } case MetaStructureEntryDataType.Boolean: { byte val = (cnode.Attributes["value"].Value == "false") ? (byte)0 : (byte)1; data[entry.DataOffset] = val; break; } case MetaStructureEntryDataType.ByteEnum: { byte val = Convert.ToByte(cnode.Attributes["value"].Value); data[entry.DataOffset] = val; break; } case MetaStructureEntryDataType.CharPointer: { if (!string.IsNullOrEmpty(cnode.InnerText)) { var ptr = mb.AddStringPtr(cnode.InnerText); var val = MetaTypes.ConvertToBytes(ptr); Buffer.BlockCopy(val, 0, data, entry.DataOffset, val.Length); } break; } case MetaStructureEntryDataType.DataBlockPointer: { // TODO break; } case MetaStructureEntryDataType.Float: { float val = FloatUtil.Parse(cnode.Attributes["value"].Value); Write(val, data, entry.DataOffset); break; } case MetaStructureEntryDataType.Float_XYZ: { float x = FloatUtil.Parse(cnode.Attributes["x"].Value); float y = FloatUtil.Parse(cnode.Attributes["y"].Value); float z = FloatUtil.Parse(cnode.Attributes["z"].Value); Write(x, data, entry.DataOffset); Write(y, data, entry.DataOffset + sizeof(float)); Write(z, data, entry.DataOffset + sizeof(float) * 2); break; } case MetaStructureEntryDataType.Float_XYZW: { float x = FloatUtil.Parse(cnode.Attributes["x"].Value); float y = FloatUtil.Parse(cnode.Attributes["y"].Value); float z = FloatUtil.Parse(cnode.Attributes["z"].Value); float w = FloatUtil.Parse(cnode.Attributes["w"].Value); Write(x, data, entry.DataOffset); Write(y, data, entry.DataOffset + sizeof(float)); Write(z, data, entry.DataOffset + sizeof(float) * 2); Write(w, data, entry.DataOffset + sizeof(float) * 3); break; } case MetaStructureEntryDataType.Hash: { var hash = GetHash(cnode.InnerText); Write(hash, data, entry.DataOffset); break; } case MetaStructureEntryDataType.IntEnum: case MetaStructureEntryDataType.IntFlags1: case MetaStructureEntryDataType.IntFlags2: { var _infos = MetaTypes.GetEnumInfo(entry.ReferenceKey); mb.AddEnumInfo(_infos.EnumNameHash); int val = GetEnumInt(entry.ReferenceKey, cnode.InnerText); Write(val, data, entry.DataOffset); break; } case MetaStructureEntryDataType.ShortFlags: { var _infos = MetaTypes.GetEnumInfo(entry.ReferenceKey); mb.AddEnumInfo(_infos.EnumNameHash); int val = GetEnumInt(entry.ReferenceKey, cnode.InnerText); Write((short)val, data, entry.DataOffset); break; } case MetaStructureEntryDataType.SignedByte: { var val = Convert.ToSByte(cnode.Attributes["value"].Value); data[entry.DataOffset] = (byte)val; break; } case MetaStructureEntryDataType.SignedInt: { var val = Convert.ToInt32(cnode.Attributes["value"].Value); Write(val, data, entry.DataOffset); break; } case MetaStructureEntryDataType.SignedShort: { var val = Convert.ToInt16(cnode.Attributes["value"].Value); Write(val, data, entry.DataOffset); break; } case MetaStructureEntryDataType.Structure: { var struc = Traverse(cnode, mb, entry.ReferenceKey); if (struc != null) { Buffer.BlockCopy(struc, 0, data, entry.DataOffset, struc.Length); } break; } case MetaStructureEntryDataType.StructurePointer: { // TODO break; } case MetaStructureEntryDataType.UnsignedByte: { var val = Convert.ToByte(cnode.Attributes["value"].Value); data[entry.DataOffset] = val; break; } case MetaStructureEntryDataType.UnsignedInt: { switch (entry.EntryNameHash) { case MetaName.color: { var val = Convert.ToUInt32(cnode.Attributes["value"].Value, 16); Write(val, data, entry.DataOffset); break; } default: { var val = Convert.ToUInt32(cnode.Attributes["value"].Value); Write(val, data, entry.DataOffset); break; } } break; } case MetaStructureEntryDataType.UnsignedShort: { var val = Convert.ToUInt16(cnode.Attributes["value"].Value); Write(val, data, entry.DataOffset); break; } default: break; } } arrayResults.WriteArrays(data); mb.AddStructureInfo(infos.StructureNameHash); if (isRoot) { mb.AddItem(type, data); } return(data); } return(null); }
public PsoBuilderPointer AddItem <T>(MetaName type, T item) where T : struct { byte[] data = MetaTypes.ConvertToBytes(item); return(AddItem(type, data)); }