private static Array_byte TraverseRawUByteArray(XmlNode node, MetaBuilder mb) { var data = new List <byte>(); if (node.InnerText != "") { var split = Regex.Split(node.InnerText, @"[\s\r\n\t]"); for (int i = 0; i < split.Length; i++) { if (!string.IsNullOrEmpty(split[i])) { var val = Convert.ToByte(split[i]); data.Add(val); } } } return(mb.AddByteArrayPtr(data.ToArray())); }
private static Array_float TraverseRawFloatArray(XmlNode node, MetaBuilder mb) { var data = new List <float>(); if (node.InnerText != "") { var split = Regex.Split(node.InnerText, @"[\s\r\n\t]"); for (int i = 0; i < split.Length; i++) { var ts = split[i]?.Trim(); if (!string.IsNullOrEmpty(ts)) { var val = FloatUtil.Parse(ts);// Convert.ToSingle(split[i]); data.Add(val); } } } return(mb.AddFloatArrayPtr(data.ToArray())); }
private static Array_Vector3 TraverseRawVector3Array(XmlNode node, MetaBuilder mb) { var items = new List <Vector4>(); foreach (XmlNode cnode in node.ChildNodes) { var split = Regex.Split(node.InnerText, @",\s"); float x = FloatUtil.Parse(split[0]); float y = FloatUtil.Parse(split[1]); float z = FloatUtil.Parse(split[2]); float w = 0f; var val = new Vector4(x, y, z, w); items.Add(val); break; } return(mb.AddPaddedVector3ArrayPtr(items.ToArray())); }
private static Array_StructurePointer TraverseArrayStructurePointer(XmlNode node, MetaBuilder mb) { var ptrs = new List <MetaPOINTER>(); foreach (XmlNode cnode in node.ChildNodes) { var type = (MetaName)(uint)GetHash(cnode.Attributes["type"].Value); var struc = Traverse(cnode, mb, type); if (struc != null) { var ptr = mb.AddItemPtr(type, struc); ptrs.Add(ptr); } } return(mb.AddPointerArray(ptrs.ToArray())); }
private static void TraverseArray(XmlNode node, MetaBuilder mb, MetaStructureEntryInfo_s arrEntry, int offset, ArrayResults results) { switch (arrEntry.DataType) { case MetaStructureEntryDataType.Structure: { results.Structures[offset] = TraverseArrayStructure(node, mb, arrEntry.ReferenceKey); break; } case MetaStructureEntryDataType.StructurePointer: { results.StructurePointers[offset] = TraverseArrayStructurePointer(node, mb); break; } case MetaStructureEntryDataType.UnsignedInt: { results.UInts[offset] = TraverseRawUIntArray(node, mb); break; } case MetaStructureEntryDataType.UnsignedShort: { results.UShorts[offset] = TraverseRawUShortArray(node, mb); break; } case MetaStructureEntryDataType.UnsignedByte: { results.UBytes[offset] = TraverseRawUByteArray(node, mb); break; } case MetaStructureEntryDataType.Float: { results.Floats[offset] = TraverseRawFloatArray(node, mb); break; } case MetaStructureEntryDataType.Float_XYZ: { results.Float_XYZs[offset] = TraverseRawVector3Array(node, mb); break; } case MetaStructureEntryDataType.Hash: { results.Hashes[offset] = TraverseHashArray(node, mb); break; } case MetaStructureEntryDataType.CharPointer: { // TODO break; } case MetaStructureEntryDataType.DataBlockPointer: { // TODO break; } default: break; } }
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 byte[] Save() { MetaBuilder mb = new MetaBuilder(); var mdb = mb.EnsureBlock(MetaName.CMapTypes); CMapTypes mapTypes = _CMapTypes; if (Extensions == null || Extensions.Length <= 0) { mapTypes.extensions = new Array_StructurePointer(); } else { mapTypes.extensions = mb.AddWrapperArrayPtr(Extensions); } if ((AllArchetypes != null) && (AllArchetypes.Length > 0)) { for (int i = 0; i < AllArchetypes.Length; i++) { var arch = AllArchetypes[i]; //save the extensions first.. if (arch._BaseArchetypeDef.extensions.Count1 > 0) { arch._BaseArchetypeDef.extensions = mb.AddWrapperArrayPtr(arch.Extensions); } } MetaPOINTER[] ptrs = new MetaPOINTER[AllArchetypes.Length]; for (int i = 0; i < AllArchetypes.Length; i++) { var arch = AllArchetypes[i]; switch (arch) { case TimeArchetype t: ptrs[i] = mb.AddItemPtr(MetaName.CTimeArchetypeDef, t._TimeArchetypeDef); break; case MloArchetype m: try { m._MloArchetypeDef._MloArchetypeDef.entities = mb.AddWrapperArrayPtr(m.entities); m._MloArchetypeDef._MloArchetypeDef.rooms = mb.AddWrapperArray(m.rooms); m._MloArchetypeDef._MloArchetypeDef.portals = mb.AddWrapperArray(m.portals); m._MloArchetypeDef._MloArchetypeDef.entitySets = mb.AddWrapperArray(m.entitySets); m._MloArchetypeDef._MloArchetypeDef.timeCycleModifiers = mb.AddItemArrayPtr(MetaName.CMloTimeCycleModifier, m.timeCycleModifiers); } catch /* (Exception e)*/ { //todo: log save error. } ptrs[i] = mb.AddItemPtr(MetaName.CMloArchetypeDef, m._MloArchetypeDef); break; case Archetype a: ptrs[i] = mb.AddItemPtr(MetaName.CBaseArchetypeDef, a._BaseArchetypeDef); break; } } mapTypes.archetypes = mb.AddPointerArray(ptrs); } else { mapTypes.archetypes = new Array_StructurePointer(); } if (CompositeEntityTypes != null && CompositeEntityTypes.Length > 0) { var cptrs = new MetaPOINTER[CompositeEntityTypes.Length]; for (int i = 0; i < cptrs.Length; i++) { var cet = CompositeEntityTypes[i]; cptrs[i] = mb.AddItemPtr(MetaName.CCompositeEntityType, cet); } mapTypes.compositeEntityTypes = mb.AddItemArrayPtr(MetaName.CCompositeEntityType, cptrs); } mapTypes.name = NameHash; mapTypes.dependencies = new Array_uint(); // is this never used? possibly a todo? mb.AddStructureInfo(MetaName.CMapTypes); if ((AllArchetypes != null && AllArchetypes.Length > 0)) { mb.AddStructureInfo(MetaName.CBaseArchetypeDef); mb.AddEnumInfo(MetaName.rage__fwArchetypeDef__eAssetType); // ASSET_TYPE_ } if ((AllArchetypes != null) && (AllArchetypes.Any(x => x is MloArchetype))) { mb.AddStructureInfo(MetaName.CMloArchetypeDef); mb.AddStructureInfo(MetaName.CMloRoomDef); mb.AddStructureInfo(MetaName.CMloPortalDef); mb.AddStructureInfo(MetaName.CMloEntitySet); mb.AddStructureInfo(MetaName.CMloTimeCycleModifier); } if ((AllArchetypes != null) && (AllArchetypes.Any(x => x is MloArchetype m && m.entities.Length > 0))) { mb.AddStructureInfo(MetaName.CEntityDef); mb.AddEnumInfo(MetaName.rage__eLodType); //LODTYPES_ mb.AddEnumInfo(MetaName.rage__ePriorityLevel); //PRI_ } if ((AllArchetypes != null) && (AllArchetypes.Any(x => x is TimeArchetype))) { mb.AddStructureInfo(MetaName.CTimeArchetypeDef); } if (CompositeEntityTypes?.Length > 0) { mb.AddStructureInfo(MetaName.CCompositeEntityType); } mb.AddItem(MetaName.CMapTypes, mapTypes); Meta meta = mb.GetMeta(); byte[] data = ResourceBuilder.Build(meta, 2); HasChanged = false; return(data); }
private static Array_Vector3 TraverseRawVector3Array(XmlNode node, MetaBuilder mb) { var items = new List <Vector4>(); float x = 0f; float y = 0f; float z = 0f; float w = 0f; var cnodes = node.SelectNodes("Item"); if (cnodes.Count > 0) { foreach (XmlNode cnode in cnodes) { var str = cnode.InnerText; var strs = str.Split(','); if (strs.Length >= 3) { x = FloatUtil.Parse(strs[0].Trim()); y = FloatUtil.Parse(strs[1].Trim()); z = FloatUtil.Parse(strs[2].Trim()); if (strs.Length >= 4) { w = FloatUtil.Parse(strs[3].Trim()); } var val = new Vector4(x, y, z, w); items.Add(val); } } } else { var split = node.InnerText.Split('\n');// Regex.Split(node.InnerText, @"[\s\r\n\t]"); for (int i = 0; i < split.Length; i++) { var s = split[i]?.Trim(); if (string.IsNullOrEmpty(s)) { continue; } var split2 = Regex.Split(s, @"[\s\t]"); int c = 0; x = 0f; y = 0f; z = 0f; for (int n = 0; n < split2.Length; n++) { var ts = split2[n]?.Trim(); if (string.IsNullOrEmpty(ts)) { continue; } var f = FloatUtil.Parse(ts); switch (c) { case 0: x = f; break; case 1: y = f; break; case 2: z = f; break; } c++; } if (c >= 3) { var val = new Vector4(x, y, z, w); items.Add(val); } } } return(mb.AddPaddedVector3ArrayPtr(items.ToArray())); }
public byte[] Save() { MetaBuilder mb = new MetaBuilder(); var mdb = mb.EnsureBlock(MetaName.CMapTypes); CMapTypes mapTypes = CMapTypes; if ((AllArchetypes != null) && (AllArchetypes.Length > 0)) { MetaPOINTER[] archPtrs = new MetaPOINTER[AllArchetypes.Length]; for (int i = 0; i < AllArchetypes.Length; i++) { var arch = AllArchetypes[i]; arch._BaseArchetypeDef.extensions = mb.AddWrapperArrayPtr(arch.Extensions); } for (int i = 0; i < archPtrs.Length; i++) { var arch = AllArchetypes[i]; if (arch is MloArchetype) { var mloArch = arch as MloArchetype; mloArch._BaseMloArchetypeDef._MloArchetypeDef.entities = mb.AddWrapperArrayPtr(mloArch.entities); mloArch._BaseMloArchetypeDef._MloArchetypeDef.rooms = mb.AddWrapperArray(mloArch.rooms); mloArch._BaseMloArchetypeDef._MloArchetypeDef.portals = mb.AddWrapperArray(mloArch.portals); mloArch._BaseMloArchetypeDef._MloArchetypeDef.entitySets = mb.AddWrapperArray(mloArch.entitySets); mloArch._BaseMloArchetypeDef._MloArchetypeDef.timeCycleModifiers = mb.AddItemArrayPtr(MetaName.CTimeCycleModifier, mloArch.timeCycleModifiers); archPtrs[i] = mb.AddItemPtr(MetaName.CMloArchetypeDef, mloArch.BaseMloArchetypeDef); } else if (arch is TimeArchetype) { var timeArch = arch as TimeArchetype; archPtrs[i] = mb.AddItemPtr(MetaName.CTimeArchetypeDef, timeArch.TimeArchetypeDef); } else { archPtrs[i] = mb.AddItemPtr(MetaName.CBaseArchetypeDef, arch.BaseArchetypeDef); } } mapTypes.archetypes = mb.AddPointerArray(archPtrs); } if ((CompositeEntityTypes != null) && (CompositeEntityTypes.Length > 0)) { MetaPOINTER[] cetPtrs = new MetaPOINTER[CompositeEntityTypes.Length]; for (int i = 0; i < cetPtrs.Length; i++) { var cet = CompositeEntityTypes[i]; cetPtrs[i] = mb.AddItemPtr(MetaName.CCompositeEntityType, cet); } mapTypes.compositeEntityTypes = mb.AddItemArrayPtr(MetaName.CCompositeEntityType, cetPtrs); } mb.AddItem(MetaName.CMapTypes, mapTypes); mb.AddStructureInfo(MetaName.CMapTypes); mb.AddStructureInfo(MetaName.CBaseArchetypeDef); mb.AddStructureInfo(MetaName.CMloArchetypeDef); mb.AddStructureInfo(MetaName.CTimeArchetypeDef); mb.AddStructureInfo(MetaName.CMloRoomDef); mb.AddStructureInfo(MetaName.CMloPortalDef); mb.AddStructureInfo(MetaName.CMloEntitySet); mb.AddStructureInfo(MetaName.CCompositeEntityType); mb.AddEnumInfo((MetaName)1991964615); mb.AddEnumInfo((MetaName)1294270217); mb.AddEnumInfo((MetaName)1264241711); mb.AddEnumInfo((MetaName)648413703); mb.AddEnumInfo((MetaName)3573596290); mb.AddEnumInfo((MetaName)700327466); mb.AddEnumInfo((MetaName)193194928); mb.AddEnumInfo((MetaName)2266515059); Meta = mb.GetMeta(); byte[] data = ResourceBuilder.Build(Meta, 2); //ymap is version 2... return(data); }
public byte[] Save() { //direct save to a raw, compressed ymap file (openIV-compatible format) //since Ymap object contents have been modified, need to recreate the arrays which are what is saved. BuildCEntityDefs(); BuildCCarGens(); //TODO: //BuildInstances(); //BuildLodLights(); //BuildDistantLodLights(); //BuildTimecycleModifiers(); //already being saved - update them.. //BuildBoxOccluders(); //BuildOccludeModels(); //BuildContainerLods(); MetaBuilder mb = new MetaBuilder(); var mdb = mb.EnsureBlock(MetaName.CMapData); CMapData mapdata = CMapData; if (CEntityDefs != null) { for (int i = 0; i < CEntityDefs.Length; i++) { var yent = AllEntities[i]; //save the extensions.. CEntityDefs[i].extensions = mb.AddWrapperArrayPtr(yent.Extensions); } } mapdata.entities = mb.AddItemPointerArrayPtr(MetaName.CEntityDef, CEntityDefs); mapdata.timeCycleModifiers = mb.AddItemArrayPtr(MetaName.CTimeCycleModifier, CTimeCycleModifiers); mapdata.physicsDictionaries = mb.AddHashArrayPtr(physicsDictionaries); mapdata.carGenerators = mb.AddItemArrayPtr(MetaName.CCarGen, CCarGens); if (CMloInstanceDefs != null) { LogSaveWarning("CMloInstanceDefs were present, may not save properly. (TODO!)"); } //clear everything out for now - TODO: fix if (mapdata.containerLods.Count1 != 0) { LogSaveWarning("containerLods were not saved. (TODO!)"); } if (mapdata.occludeModels.Count1 != 0) { LogSaveWarning("occludeModels were not saved. (TODO!)"); } if (mapdata.boxOccluders.Count1 != 0) { LogSaveWarning("boxOccluders were not saved. (TODO!)"); } if (mapdata.instancedData.GrassInstanceList.Count1 != 0) { LogSaveWarning("instancedData.GrassInstanceList was not saved. (TODO!)"); } if (mapdata.instancedData.PropInstanceList.Count1 != 0) { LogSaveWarning("instancedData.PropInstanceList was not saved. (TODO!)"); } if (mapdata.LODLightsSOA.direction.Count1 != 0) { LogSaveWarning("LODLightsSOA was not saved. (TODO!)"); } if (mapdata.DistantLODLightsSOA.position.Count1 != 0) { LogSaveWarning("DistantLODLightsSOA was not saved. (TODO!)"); } mapdata.containerLods = new Array_Structure(); mapdata.occludeModels = new Array_Structure(); mapdata.boxOccluders = new Array_Structure(); mapdata.instancedData = new rage__fwInstancedMapData(); mapdata.LODLightsSOA = new CLODLight(); mapdata.DistantLODLightsSOA = new CDistantLODLight(); var block = new CBlockDesc(); block.name = mb.AddStringPtr(Path.GetFileNameWithoutExtension(Name)); block.exportedBy = mb.AddStringPtr("CodeWalker"); block.time = mb.AddStringPtr(DateTime.UtcNow.ToString("dd MMMM yyyy HH:mm")); mapdata.block = block; string name = Path.GetFileNameWithoutExtension(Name); uint nameHash = JenkHash.GenHash(name); mapdata.name = new MetaHash(nameHash);//make sure name is upto date... mb.AddItem(MetaName.CMapData, mapdata); //make sure all the relevant structure and enum infos are present. mb.AddStructureInfo(MetaName.rage__fwInstancedMapData); mb.AddStructureInfo(MetaName.CLODLight); mb.AddStructureInfo(MetaName.CDistantLODLight); mb.AddStructureInfo(MetaName.CBlockDesc); mb.AddStructureInfo(MetaName.CMapData); mb.AddStructureInfo(MetaName.CEntityDef); mb.AddStructureInfo(MetaName.CTimeCycleModifier); if ((CCarGens != null) && (CCarGens.Length > 0)) { mb.AddStructureInfo(MetaName.CCarGen); } mb.AddEnumInfo((MetaName)1264241711); //LODTYPES_ mb.AddEnumInfo((MetaName)648413703); //PRI_ Meta meta = mb.GetMeta(); byte[] data = ResourceBuilder.Build(meta, 2); //ymap is version 2... return(data); }