From b77bddddbccdc02a4da92dad1e3e1eadac2cc007 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Sun, 1 Aug 2010 21:00:17 +1200 Subject: [PATCH] fix maploader to be backwards compatible. InitDict-based format is now 3. Formats 1,2 are translated on load, and will be saved back as Format 3 if saved in the editor. --- OpenRA.Game/Map.cs | 101 ++++++++++++++++++++++----------- mods/ra/maps/map/map.yaml | 2 +- mods/ra/maps/shellmap/map.yaml | 2 +- 3 files changed, 69 insertions(+), 36 deletions(-) diff --git a/OpenRA.Game/Map.cs b/OpenRA.Game/Map.cs index c54a6914d7..5787581936 100755 --- a/OpenRA.Game/Map.cs +++ b/OpenRA.Game/Map.cs @@ -88,6 +88,14 @@ namespace OpenRA Author = "Your name here"; } + class Format2ActorReference + { + public string Id; + public string Type; + public int2 Location; + public string Owner; + } + public Map(IFolder package) { Package = package; @@ -101,41 +109,66 @@ namespace OpenRA { string[] loc = wp.Value.Value.Split(','); Waypoints.Add(wp.Key, new int2(int.Parse(loc[0]), int.Parse(loc[1]))); - } - - // Players - if (MapFormat == 1) - { - Players.Add("Neutral", new PlayerReference("Neutral", "allies", true, true)); - } - else - { - foreach (var kv in yaml["Players"].Nodes) - { - var player = new PlayerReference(kv.Value); - Players.Add(player.Name, player); - } - } - - // Actors - if (MapFormat == 1) + } + + // Players & Actors -- this has changed several times. + // - Be backwards compatible wherever possible. + // - Loading a map then saving it out upgrades to latest. + // Minimum criteria for dropping a format: + // - There are no maps of this format left in tree + + switch (MapFormat) { - int actors = 0; - foreach (var kv in yaml["Actors"].Nodes) - { - string[] vals = kv.Value.Value.Split(' '); - string[] loc = vals[2].Split(','); - Actors.Add( "Actor" + actors++, new ActorReference( vals[ 0 ] ) + case 1: { - new LocationInit( new int2( int.Parse( loc[ 0 ] ), int.Parse( loc[ 1 ] ) ) ), - new OwnerInit( "Neutral" ), - } ); - } - } - else - { - foreach( var kv in yaml[ "Actors" ].Nodes ) - Actors.Add( kv.Key, new ActorReference( kv.Value.Value, kv.Value.Nodes ) ); + Players.Add("Neutral", new PlayerReference("Neutral", "allies", true, true)); + + int actors = 0; + foreach (var kv in yaml["Actors"].Nodes) + { + string[] vals = kv.Value.Value.Split(' '); + string[] loc = vals[2].Split(','); + Actors.Add("Actor" + actors++, new ActorReference(vals[0]) + { + new LocationInit( new int2( int.Parse( loc[ 0 ] ), int.Parse( loc[ 1 ] ) ) ), + new OwnerInit( "Neutral" ), + }); + } + } break; + + case 2: + { + foreach (var kv in yaml["Players"].Nodes) + { + var player = new PlayerReference(kv.Value); + Players.Add(player.Name, player); + } + + foreach (var kv in yaml["Actors"].Nodes) + { + var oldActorReference = FieldLoader.Load(kv.Value); + Actors.Add(oldActorReference.Id, new ActorReference(oldActorReference.Type) + { + new LocationInit( oldActorReference.Location ), + new OwnerInit( oldActorReference.Owner ) + }); + } + } break; + + case 3: + { + foreach (var kv in yaml["Players"].Nodes) + { + var player = new PlayerReference(kv.Value); + Players.Add(player.Name, player); + } + + foreach (var kv in yaml["Actors"].Nodes) + Actors.Add(kv.Key, new ActorReference(kv.Value.Value, kv.Value.Nodes)); + } break; + + default: + throw new InvalidDataException("Map format {0} is not supported.".F(MapFormat)); } // Smudges @@ -156,7 +189,7 @@ namespace OpenRA public void Save(string filepath) { - MapFormat = 2; + MapFormat = 3; var root = new Dictionary(); foreach (var field in SimpleFields) diff --git a/mods/ra/maps/map/map.yaml b/mods/ra/maps/map/map.yaml index 58f44a663b..a6734a115b 100644 --- a/mods/ra/maps/map/map.yaml +++ b/mods/ra/maps/map/map.yaml @@ -1,6 +1,6 @@ Selectable: True -MapFormat: 2 +MapFormat: 3 Title: Test diff --git a/mods/ra/maps/shellmap/map.yaml b/mods/ra/maps/shellmap/map.yaml index e224a2dc79..219fc78619 100644 --- a/mods/ra/maps/shellmap/map.yaml +++ b/mods/ra/maps/shellmap/map.yaml @@ -1,6 +1,6 @@ Selectable: False -MapFormat: 2 +MapFormat: 3 Title: