From ca2b712ad9333836c7f1fe2503ea8affcafa25a1 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Mon, 21 Mar 2016 23:08:27 +0000 Subject: [PATCH] Clean up ImportTSMapCommand. --- .../UtilityCommands/ImportTSMapCommand.cs | 98 ++++++++----------- 1 file changed, 39 insertions(+), 59 deletions(-) diff --git a/OpenRA.Mods.TS/UtilityCommands/ImportTSMapCommand.cs b/OpenRA.Mods.TS/UtilityCommands/ImportTSMapCommand.cs index 7ce541f66a..356ef55d46 100644 --- a/OpenRA.Mods.TS/UtilityCommands/ImportTSMapCommand.cs +++ b/OpenRA.Mods.TS/UtilityCommands/ImportTSMapCommand.cs @@ -23,16 +23,8 @@ namespace OpenRA.Mods.TS.UtilityCommands { class ImportTSMapCommand : IUtilityCommand { - public string Name { get { return "--import-ts-map"; } } - - public bool ValidateArguments(string[] args) - { - return args.Length >= 2; - } - - int2 fullSize; - int actorCount = 0; - int spawnCount = 0; + string IUtilityCommand.Name { get { return "--import-ts-map"; } } + bool IUtilityCommand.ValidateArguments(string[] args) { return args.Length >= 2; } static readonly Dictionary OverlayToActor = new Dictionary() { @@ -150,31 +142,46 @@ namespace OpenRA.Mods.TS.UtilityCommands { 0x03, new byte[] { 0x7E } } }; - private static readonly Dictionary DeployableActors = new Dictionary() + static readonly Dictionary DeployableActors = new Dictionary() { { "gadpsa", "lpst" }, { "gatick", "ttnk" } }; [Desc("FILENAME", "Convert a Tiberian Sun map to the OpenRA format.")] - public void Run(ModData modData, string[] args) + void IUtilityCommand.Run(ModData modData, string[] args) { // HACK: The engine code assumes that Game.modData is set. Game.ModData = modData; var filename = args[1]; var file = new IniFile(File.Open(args[1], FileMode.Open)); - var map = GenerateMapHeader(filename, file, modData); + var basic = file.GetSection("Basic"); + var mapSection = file.GetSection("Map"); + var tileset = mapSection.GetValue("Theater", ""); + var iniSize = mapSection.GetValue("Size", "0, 0, 0, 0").Split(',').Select(int.Parse).ToArray(); + var iniBounds = mapSection.GetValue("LocalSize", "0, 0, 0, 0").Split(',').Select(int.Parse).ToArray(); + var size = new Size(iniSize[2], 2 * iniSize[3]); - ReadTiles(map, file); - ReadActors(map, file, "Structures"); - ReadActors(map, file, "Units"); - ReadActors(map, file, "Infantry"); - ReadTerrainActors(map, file); - ReadWaypoints(map, file); - ReadOverlay(map, file); + var map = new Map(Game.ModData, modData.DefaultTileSets[tileset], size.Width, size.Height) + { + Title = basic.GetValue("Name", Path.GetFileNameWithoutExtension(filename)), + Author = "Westwood Studios", + Bounds = new Rectangle(iniBounds[0], iniBounds[1], iniBounds[2], 2 * iniBounds[3] + 2 * iniBounds[1]), + RequiresMod = modData.Manifest.Mod.Id + }; + + var fullSize = new int2(iniSize[2], iniSize[3]); + ReadTiles(map, file, fullSize); + ReadActors(map, file, "Structures", fullSize); + ReadActors(map, file, "Units", fullSize); + ReadActors(map, file, "Infantry", fullSize); + ReadTerrainActors(map, file, fullSize); + ReadWaypoints(map, file, fullSize); + ReadOverlay(map, file, fullSize); ReadLighting(map, file); + var spawnCount = map.ActorDefinitions.Count(n => n.Value.Value == "mpspawn"); var mapPlayers = new MapPlayers(map.Rules, spawnCount); map.PlayerDefinitions = mapPlayers.ToMiniYaml(); @@ -184,7 +191,7 @@ namespace OpenRA.Mods.TS.UtilityCommands Console.WriteLine(dest + " saved."); } - void UnpackLZO(byte[] src, byte[] dest) + static void UnpackLZO(byte[] src, byte[] dest) { var srcOffset = 0U; var destOffset = 0U; @@ -200,7 +207,7 @@ namespace OpenRA.Mods.TS.UtilityCommands } } - void UnpackLCW(byte[] src, byte[] dest, byte[] temp) + static void UnpackLCW(byte[] src, byte[] dest, byte[] temp) { var srcOffset = 0; var destOffset = 0; @@ -217,31 +224,7 @@ namespace OpenRA.Mods.TS.UtilityCommands } } - Map GenerateMapHeader(string filename, IniFile file, ModData modData) - { - var basic = file.GetSection("Basic"); - var mapSection = file.GetSection("Map"); - var tileset = mapSection.GetValue("Theater", ""); - var iniSize = mapSection.GetValue("Size", "0, 0, 0, 0").Split(',').Select(int.Parse).ToArray(); - var iniBounds = mapSection.GetValue("LocalSize", "0, 0, 0, 0").Split(',').Select(int.Parse).ToArray(); - var size = new Size(iniSize[2], 2 * iniSize[3]); - - fullSize = new int2(iniSize[2], iniSize[3]); - - var map = new Map(Game.ModData, modData.DefaultTileSets[tileset], size.Width, size.Height); - map.Title = basic.GetValue("Name", Path.GetFileNameWithoutExtension(filename)); - map.Author = "Westwood Studios"; - map.Bounds = new Rectangle(iniBounds[0], iniBounds[1], iniBounds[2], 2 * iniBounds[3] + 2 * iniBounds[1]); - map.Resources = new CellLayer(map.Grid.Type, size); - map.Tiles = new CellLayer(map.Grid.Type, size); - map.Height = new CellLayer(map.Grid.Type, size); - - map.RequiresMod = modData.Manifest.Mod.Id; - - return map; - } - - void ReadTiles(Map map, IniFile file) + static void ReadTiles(Map map, IniFile file, int2 fullSize) { var tileset = Game.ModData.DefaultTileSets[map.Tileset]; var mapSection = file.GetSection("IsoMapPack5"); @@ -279,7 +262,7 @@ namespace OpenRA.Mods.TS.UtilityCommands } } - void ReadOverlay(Map map, IniFile file) + static void ReadOverlay(Map map, IniFile file, int2 fullSize) { var overlaySection = file.GetSection("OverlayPack"); var overlayCompressed = Convert.FromBase64String(overlaySection.Aggregate(string.Empty, (a, b) => a + b.Value)); @@ -320,7 +303,7 @@ namespace OpenRA.Mods.TS.UtilityCommands new OwnerInit("Neutral") }; - map.ActorDefinitions.Add(new MiniYamlNode("Actor" + actorCount++, ar.Save())); + map.ActorDefinitions.Add(new MiniYamlNode("Actor" + map.ActorDefinitions.Count, ar.Save())); continue; } @@ -340,7 +323,7 @@ namespace OpenRA.Mods.TS.UtilityCommands } } - void ReadWaypoints(Map map, IniFile file) + static void ReadWaypoints(Map map, IniFile file, int2 fullSize) { var waypointsSection = file.GetSection("Waypoints", true); foreach (var kv in waypointsSection) @@ -357,14 +340,11 @@ namespace OpenRA.Mods.TS.UtilityCommands ar.Add(new LocationInit(cell)); ar.Add(new OwnerInit("Neutral")); - map.ActorDefinitions.Add(new MiniYamlNode("Actor" + actorCount++, ar.Save())); - - if (ar.Type == "mpspawn") - spawnCount++; + map.ActorDefinitions.Add(new MiniYamlNode("Actor" + map.ActorDefinitions.Count, ar.Save())); } } - void ReadTerrainActors(Map map, IniFile file) + static void ReadTerrainActors(Map map, IniFile file, int2 fullSize) { var terrainSection = file.GetSection("Terrain", true); foreach (var kv in terrainSection) @@ -384,11 +364,11 @@ namespace OpenRA.Mods.TS.UtilityCommands if (!map.Rules.Actors.ContainsKey(name)) Console.WriteLine("Ignoring unknown actor type: `{0}`".F(name)); else - map.ActorDefinitions.Add(new MiniYamlNode("Actor" + actorCount++, ar.Save())); + map.ActorDefinitions.Add(new MiniYamlNode("Actor" + map.ActorDefinitions.Count, ar.Save())); } } - void ReadActors(Map map, IniFile file, string type) + static void ReadActors(Map map, IniFile file, string type, int2 fullSize) { var structuresSection = file.GetSection(type, true); foreach (var kv in structuresSection) @@ -427,11 +407,11 @@ namespace OpenRA.Mods.TS.UtilityCommands if (!map.Rules.Actors.ContainsKey(name)) Console.WriteLine("Ignoring unknown actor type: `{0}`".F(name)); else - map.ActorDefinitions.Add(new MiniYamlNode("Actor" + actorCount++, ar.Save())); + map.ActorDefinitions.Add(new MiniYamlNode("Actor" + map.ActorDefinitions.Count, ar.Save())); } } - void ReadLighting(Map map, IniFile file) + static void ReadLighting(Map map, IniFile file) { var lightingTypes = new[] { "Red", "Green", "Blue", "Ambient" }; var lightingSection = file.GetSection("Lighting");