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.

This commit is contained in:
Chris Forbes
2010-08-01 21:00:17 +12:00
parent 3521319095
commit b77bddddbc
3 changed files with 69 additions and 36 deletions

View File

@@ -88,6 +88,14 @@ namespace OpenRA
Author = "Your name here"; Author = "Your name here";
} }
class Format2ActorReference
{
public string Id;
public string Type;
public int2 Location;
public string Owner;
}
public Map(IFolder package) public Map(IFolder package)
{ {
Package = package; Package = package;
@@ -103,39 +111,64 @@ namespace OpenRA
Waypoints.Add(wp.Key, new int2(int.Parse(loc[0]), int.Parse(loc[1]))); Waypoints.Add(wp.Key, new int2(int.Parse(loc[0]), int.Parse(loc[1])));
} }
// Players // Players & Actors -- this has changed several times.
if (MapFormat == 1) // - 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)
{
case 1:
{ {
Players.Add("Neutral", new PlayerReference("Neutral", "allies", true, true)); 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" ),
});
} }
else } break;
case 2:
{ {
foreach (var kv in yaml["Players"].Nodes) foreach (var kv in yaml["Players"].Nodes)
{ {
var player = new PlayerReference(kv.Value); var player = new PlayerReference(kv.Value);
Players.Add(player.Name, player); Players.Add(player.Name, player);
} }
}
// Actors
if (MapFormat == 1)
{
int actors = 0;
foreach (var kv in yaml["Actors"].Nodes) foreach (var kv in yaml["Actors"].Nodes)
{ {
string[] vals = kv.Value.Value.Split(' '); var oldActorReference = FieldLoader.Load<Format2ActorReference>(kv.Value);
string[] loc = vals[2].Split(','); Actors.Add(oldActorReference.Id, new ActorReference(oldActorReference.Type)
Actors.Add( "Actor" + actors++, new ActorReference( vals[ 0 ] )
{ {
new LocationInit( new int2( int.Parse( loc[ 0 ] ), int.Parse( loc[ 1 ] ) ) ), new LocationInit( oldActorReference.Location ),
new OwnerInit( "Neutral" ), new OwnerInit( oldActorReference.Owner )
} ); });
} }
} } break;
else
case 3:
{ {
foreach( var kv in yaml[ "Actors" ].Nodes ) foreach (var kv in yaml["Players"].Nodes)
Actors.Add( kv.Key, new ActorReference( kv.Value.Value, kv.Value.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 // Smudges
@@ -156,7 +189,7 @@ namespace OpenRA
public void Save(string filepath) public void Save(string filepath)
{ {
MapFormat = 2; MapFormat = 3;
var root = new Dictionary<string, MiniYaml>(); var root = new Dictionary<string, MiniYaml>();
foreach (var field in SimpleFields) foreach (var field in SimpleFields)

View File

@@ -1,6 +1,6 @@
Selectable: True Selectable: True
MapFormat: 2 MapFormat: 3
Title: Test Title: Test

View File

@@ -1,6 +1,6 @@
Selectable: False Selectable: False
MapFormat: 2 MapFormat: 3
Title: <none> Title: <none>