From b450d888e5dde06093d0beb4f8d63d663751d084 Mon Sep 17 00:00:00 2001 From: Naomi Date: Wed, 14 Oct 2020 02:39:51 +0100 Subject: [PATCH] [map] fix map extraction --- DataTool/SaveLogic/Map.cs | 23 ++++++------ DataTool/ToolLogic/Dbg/DebugMapDump.cs | 12 +++---- TankLib/teMapChunk.cs | 50 ++++++++++++++------------ 3 files changed, 44 insertions(+), 41 deletions(-) diff --git a/DataTool/SaveLogic/Map.cs b/DataTool/SaveLogic/Map.cs index 79d96fb26..30b1c73ac 100644 --- a/DataTool/SaveLogic/Map.cs +++ b/DataTool/SaveLogic/Map.cs @@ -38,13 +38,13 @@ public OverwatchMap(string name, FindLogic.Combo.ComboInfo info, teMapPlaceableD Name = name; Info = info; - SingleModels = singleModels; - ModelGroups = modelGroups; - Models = models; - Entities = entities; - Lights = lights; - Sounds = sounds; - Effects = effects; + SingleModels = singleModels ?? new teMapPlaceableData(); + ModelGroups = modelGroups ?? new teMapPlaceableData(); + Models = models ?? new teMapPlaceableData(); + Entities = entities ?? new teMapPlaceableData(); + Lights = lights ?? new teMapPlaceableData(); + Sounds = sounds ?? new teMapPlaceableData(); + Effects = effects ?? new teMapPlaceableData(); } public void Write(Stream output) { @@ -377,14 +377,13 @@ public static void Save(ICLIFlags flags, MapHeader mapInfo, STUMapHeader mapHead } public static teMapPlaceableData GetPlaceableData(STUMapHeader map, Enums.teMAP_PLACEABLE_TYPE modelGroup) { - return GetPlaceableData(map, (byte) modelGroup); + using (Stream stream = OpenFile(map.GetChunkKey(modelGroup))) { + return stream == null ? null : new teMapPlaceableData(stream, modelGroup); + } } public static teMapPlaceableData GetPlaceableData(STUMapHeader map, byte type) { - using (Stream stream = OpenFile(map.GetChunkKey(type))) { - if (stream == null) return null; - return new teMapPlaceableData(stream); - } + return GetPlaceableData(map, (Enums.teMAP_PLACEABLE_TYPE) type); } } } \ No newline at end of file diff --git a/DataTool/ToolLogic/Dbg/DebugMapDump.cs b/DataTool/ToolLogic/Dbg/DebugMapDump.cs index 5216dedd9..c9779adb0 100644 --- a/DataTool/ToolLogic/Dbg/DebugMapDump.cs +++ b/DataTool/ToolLogic/Dbg/DebugMapDump.cs @@ -57,16 +57,16 @@ public void Parse(ICLIFlags toolFlags) public static teMapPlaceableData GetPlaceableData(STUMapHeader map, Enums.teMAP_PLACEABLE_TYPE modelGroup) { - return GetPlaceableData(map, (byte)modelGroup); + using (Stream stream = OpenFile(map.GetChunkKey(modelGroup))) + { + if (stream == null) return null; + return new teMapPlaceableData(stream, modelGroup); + } } public static teMapPlaceableData GetPlaceableData(STUMapHeader map, byte type) { - using (Stream stream = OpenFile(map.GetChunkKey(type))) - { - if (stream == null) return null; - return new teMapPlaceableData(stream); - } + return GetPlaceableData(map, (Enums.teMAP_PLACEABLE_TYPE) type); } } } diff --git a/TankLib/teMapChunk.cs b/TankLib/teMapChunk.cs index 69b34ce41..34319c67b 100644 --- a/TankLib/teMapChunk.cs +++ b/TankLib/teMapChunk.cs @@ -16,18 +16,23 @@ namespace TankLib { /// Tank MapChunk, file type 0BC /// public abstract class teMapChunk { - protected abstract void Read(BinaryReader reader); + protected abstract void Read(BinaryReader reader, teMAP_PLACEABLE_TYPE type); + + [SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")] + protected teMapChunk() { + + } [SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")] - public teMapChunk(Stream stream) { + protected teMapChunk(Stream stream, teMAP_PLACEABLE_TYPE type) { using (BinaryReader reader = new BinaryReader(stream)) { - Read(reader); + Read(reader, type); } } [SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")] - public teMapChunk(BinaryReader reader) { - Read(reader); + protected teMapChunk(BinaryReader reader, teMAP_PLACEABLE_TYPE type) { + Read(reader, type); } } @@ -41,11 +46,7 @@ public struct CommonStructure { public ushort Unknown1; public byte Unknown2; - - /// - /// Placeable type - /// - public teMAP_PLACEABLE_TYPE Type; + public byte Unknown3; /// /// Size in bytes (including this structure) @@ -79,13 +80,16 @@ public struct PlaceableDataHeader { public static teMapPlaceableManager Manager = new teMapPlaceableManager(); public PlaceableDataHeader Header; - public IMapPlaceable[] Placeables; - public CommonStructure[] CommonStructures; + public IMapPlaceable[] Placeables = {}; + public CommonStructure[] CommonStructures = {}; + + public teMapPlaceableData() { + } - public teMapPlaceableData(Stream stream) : base(stream) { } - public teMapPlaceableData(BinaryReader reader) : base(reader) { } + public teMapPlaceableData(Stream stream, teMAP_PLACEABLE_TYPE type) : base(stream, type) { } + public teMapPlaceableData(BinaryReader reader, teMAP_PLACEABLE_TYPE type) : base(reader, type) { } - protected override void Read(BinaryReader reader) { + protected override void Read(BinaryReader reader, teMAP_PLACEABLE_TYPE type) { Header = reader.Read(); if (Header.PlaceableOffset > 0) { @@ -100,12 +104,12 @@ protected override void Read(BinaryReader reader) { CommonStructure commonStructure = reader.Read(); CommonStructures[i] = commonStructure; - Placeables[i] = Manager.CreateType(commonStructure, reader); + Placeables[i] = Manager.CreateType(commonStructure, type, reader); reader.BaseStream.Position = beforePos + CommonStructures[i].Size; } - if (CommonStructures.Length > 0 && CommonStructures[0].Type == teMAP_PLACEABLE_TYPE.ENTITY && Header.InstanceDataOffset > 0) { + if (CommonStructures.Length > 0 && type == teMAP_PLACEABLE_TYPE.ENTITY && Header.InstanceDataOffset > 0) { int execCount = 0; reader.BaseStream.Position = Header.InstanceDataOffset+16; foreach (IMapPlaceable placeable in Placeables) { @@ -118,7 +122,7 @@ protected override void Read(BinaryReader reader) { try { teStructuredData structuredData = new teStructuredData(reader); entity.InstanceData[i] = structuredData.GetInstance(); - } catch (Exception e) + } catch { execCount++; } @@ -249,7 +253,7 @@ public void Read(BinaryReader reader) { for (int i = 0; i < Header.InstanceDataCount; i++) { long disStart = reader.BaseStream.Position; - var type = reader.ReadUInt32(); + var _ = reader.ReadUInt32(); // type var relOffset = reader.ReadUInt32(); var offset = disStart + relOffset; @@ -480,12 +484,12 @@ private void AddType(Type type) { Types[instance.Type] = type; } - public IMapPlaceable CreateType(teMapPlaceableData.CommonStructure commonStructure, BinaryReader reader) { + public IMapPlaceable CreateType(teMapPlaceableData.CommonStructure commonStructure, teMAP_PLACEABLE_TYPE type, BinaryReader reader) { IMapPlaceable value = new teMapPlaceableDummy((int)commonStructure.Size); - if (Types.TryGetValue(commonStructure.Type, out Type placeableType)) { + if (Types.TryGetValue(type, out Type placeableType)) { value = (IMapPlaceable)Activator.CreateInstance(placeableType); - } else if (_misingTypes.Add(commonStructure.Type)) { - Debugger.Log(0, "teMapPlaceableManager", $"Unhandled placeable type: {commonStructure.Type}\r\n"); + } else if (_misingTypes.Add(type)) { + Debugger.Log(0, "teMapPlaceableManager", $"Unhandled placeable type: {type}\r\n"); } value.Read(reader); return value;