Clean up ImportTSMapCommand.
This commit is contained in:
@@ -23,16 +23,8 @@ namespace OpenRA.Mods.TS.UtilityCommands
|
|||||||
{
|
{
|
||||||
class ImportTSMapCommand : IUtilityCommand
|
class ImportTSMapCommand : IUtilityCommand
|
||||||
{
|
{
|
||||||
public string Name { get { return "--import-ts-map"; } }
|
string IUtilityCommand.Name { get { return "--import-ts-map"; } }
|
||||||
|
bool IUtilityCommand.ValidateArguments(string[] args) { return args.Length >= 2; }
|
||||||
public bool ValidateArguments(string[] args)
|
|
||||||
{
|
|
||||||
return args.Length >= 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
int2 fullSize;
|
|
||||||
int actorCount = 0;
|
|
||||||
int spawnCount = 0;
|
|
||||||
|
|
||||||
static readonly Dictionary<byte, string> OverlayToActor = new Dictionary<byte, string>()
|
static readonly Dictionary<byte, string> OverlayToActor = new Dictionary<byte, string>()
|
||||||
{
|
{
|
||||||
@@ -150,31 +142,46 @@ namespace OpenRA.Mods.TS.UtilityCommands
|
|||||||
{ 0x03, new byte[] { 0x7E } }
|
{ 0x03, new byte[] { 0x7E } }
|
||||||
};
|
};
|
||||||
|
|
||||||
private static readonly Dictionary<string, string> DeployableActors = new Dictionary<string, string>()
|
static readonly Dictionary<string, string> DeployableActors = new Dictionary<string, string>()
|
||||||
{
|
{
|
||||||
{ "gadpsa", "lpst" },
|
{ "gadpsa", "lpst" },
|
||||||
{ "gatick", "ttnk" }
|
{ "gatick", "ttnk" }
|
||||||
};
|
};
|
||||||
|
|
||||||
[Desc("FILENAME", "Convert a Tiberian Sun map to the OpenRA format.")]
|
[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.
|
// HACK: The engine code assumes that Game.modData is set.
|
||||||
Game.ModData = modData;
|
Game.ModData = modData;
|
||||||
|
|
||||||
var filename = args[1];
|
var filename = args[1];
|
||||||
var file = new IniFile(File.Open(args[1], FileMode.Open));
|
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);
|
var map = new Map(Game.ModData, modData.DefaultTileSets[tileset], size.Width, size.Height)
|
||||||
ReadActors(map, file, "Structures");
|
{
|
||||||
ReadActors(map, file, "Units");
|
Title = basic.GetValue("Name", Path.GetFileNameWithoutExtension(filename)),
|
||||||
ReadActors(map, file, "Infantry");
|
Author = "Westwood Studios",
|
||||||
ReadTerrainActors(map, file);
|
Bounds = new Rectangle(iniBounds[0], iniBounds[1], iniBounds[2], 2 * iniBounds[3] + 2 * iniBounds[1]),
|
||||||
ReadWaypoints(map, file);
|
RequiresMod = modData.Manifest.Mod.Id
|
||||||
ReadOverlay(map, file);
|
};
|
||||||
|
|
||||||
|
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);
|
ReadLighting(map, file);
|
||||||
|
|
||||||
|
var spawnCount = map.ActorDefinitions.Count(n => n.Value.Value == "mpspawn");
|
||||||
var mapPlayers = new MapPlayers(map.Rules, spawnCount);
|
var mapPlayers = new MapPlayers(map.Rules, spawnCount);
|
||||||
map.PlayerDefinitions = mapPlayers.ToMiniYaml();
|
map.PlayerDefinitions = mapPlayers.ToMiniYaml();
|
||||||
|
|
||||||
@@ -184,7 +191,7 @@ namespace OpenRA.Mods.TS.UtilityCommands
|
|||||||
Console.WriteLine(dest + " saved.");
|
Console.WriteLine(dest + " saved.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void UnpackLZO(byte[] src, byte[] dest)
|
static void UnpackLZO(byte[] src, byte[] dest)
|
||||||
{
|
{
|
||||||
var srcOffset = 0U;
|
var srcOffset = 0U;
|
||||||
var destOffset = 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 srcOffset = 0;
|
||||||
var destOffset = 0;
|
var destOffset = 0;
|
||||||
@@ -217,31 +224,7 @@ namespace OpenRA.Mods.TS.UtilityCommands
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Map GenerateMapHeader(string filename, IniFile file, ModData modData)
|
static void ReadTiles(Map map, IniFile file, int2 fullSize)
|
||||||
{
|
|
||||||
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<ResourceTile>(map.Grid.Type, size);
|
|
||||||
map.Tiles = new CellLayer<TerrainTile>(map.Grid.Type, size);
|
|
||||||
map.Height = new CellLayer<byte>(map.Grid.Type, size);
|
|
||||||
|
|
||||||
map.RequiresMod = modData.Manifest.Mod.Id;
|
|
||||||
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ReadTiles(Map map, IniFile file)
|
|
||||||
{
|
{
|
||||||
var tileset = Game.ModData.DefaultTileSets[map.Tileset];
|
var tileset = Game.ModData.DefaultTileSets[map.Tileset];
|
||||||
var mapSection = file.GetSection("IsoMapPack5");
|
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 overlaySection = file.GetSection("OverlayPack");
|
||||||
var overlayCompressed = Convert.FromBase64String(overlaySection.Aggregate(string.Empty, (a, b) => a + b.Value));
|
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")
|
new OwnerInit("Neutral")
|
||||||
};
|
};
|
||||||
|
|
||||||
map.ActorDefinitions.Add(new MiniYamlNode("Actor" + actorCount++, ar.Save()));
|
map.ActorDefinitions.Add(new MiniYamlNode("Actor" + map.ActorDefinitions.Count, ar.Save()));
|
||||||
continue;
|
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);
|
var waypointsSection = file.GetSection("Waypoints", true);
|
||||||
foreach (var kv in waypointsSection)
|
foreach (var kv in waypointsSection)
|
||||||
@@ -357,14 +340,11 @@ namespace OpenRA.Mods.TS.UtilityCommands
|
|||||||
ar.Add(new LocationInit(cell));
|
ar.Add(new LocationInit(cell));
|
||||||
ar.Add(new OwnerInit("Neutral"));
|
ar.Add(new OwnerInit("Neutral"));
|
||||||
|
|
||||||
map.ActorDefinitions.Add(new MiniYamlNode("Actor" + actorCount++, ar.Save()));
|
map.ActorDefinitions.Add(new MiniYamlNode("Actor" + map.ActorDefinitions.Count, ar.Save()));
|
||||||
|
|
||||||
if (ar.Type == "mpspawn")
|
|
||||||
spawnCount++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReadTerrainActors(Map map, IniFile file)
|
static void ReadTerrainActors(Map map, IniFile file, int2 fullSize)
|
||||||
{
|
{
|
||||||
var terrainSection = file.GetSection("Terrain", true);
|
var terrainSection = file.GetSection("Terrain", true);
|
||||||
foreach (var kv in terrainSection)
|
foreach (var kv in terrainSection)
|
||||||
@@ -384,11 +364,11 @@ namespace OpenRA.Mods.TS.UtilityCommands
|
|||||||
if (!map.Rules.Actors.ContainsKey(name))
|
if (!map.Rules.Actors.ContainsKey(name))
|
||||||
Console.WriteLine("Ignoring unknown actor type: `{0}`".F(name));
|
Console.WriteLine("Ignoring unknown actor type: `{0}`".F(name));
|
||||||
else
|
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);
|
var structuresSection = file.GetSection(type, true);
|
||||||
foreach (var kv in structuresSection)
|
foreach (var kv in structuresSection)
|
||||||
@@ -427,11 +407,11 @@ namespace OpenRA.Mods.TS.UtilityCommands
|
|||||||
if (!map.Rules.Actors.ContainsKey(name))
|
if (!map.Rules.Actors.ContainsKey(name))
|
||||||
Console.WriteLine("Ignoring unknown actor type: `{0}`".F(name));
|
Console.WriteLine("Ignoring unknown actor type: `{0}`".F(name));
|
||||||
else
|
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 lightingTypes = new[] { "Red", "Green", "Blue", "Ambient" };
|
||||||
var lightingSection = file.GetSection("Lighting");
|
var lightingSection = file.GetSection("Lighting");
|
||||||
|
|||||||
Reference in New Issue
Block a user