From 382efbcdfbe77e350f1894d1e4c349e9e776680a Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Thu, 1 Apr 2010 17:37:46 +1300 Subject: [PATCH] Map saving --- MapConverter/MapConverter.cs | 8 +++++--- OpenRA.FileFormats/FieldLoader.cs | 4 ++-- OpenRA.FileFormats/Map/ActorReference.cs | 6 ++++++ OpenRA.FileFormats/Map/NewMap.cs | 19 +++++++++++++++++-- OpenRA.FileFormats/MiniYaml.cs | 7 ++++++- OpenRA.FileFormats/OpenRA.FileFormats.csproj | 2 +- .../{Properties => Primitives}/int2.cs | 2 +- 7 files changed, 38 insertions(+), 10 deletions(-) rename OpenRA.FileFormats/{Properties => Primitives}/int2.cs (94%) diff --git a/MapConverter/MapConverter.cs b/MapConverter/MapConverter.cs index 18f31c559b..8a0ec339c2 100644 --- a/MapConverter/MapConverter.cs +++ b/MapConverter/MapConverter.cs @@ -226,7 +226,7 @@ namespace MapConverter foreach( KeyValuePair kv in terrain ) { var loc = int.Parse( kv.Key ); - Map.Actors.Add("Actor"+a++, new ActorReference(kv.Value, new int2(loc % MapSize, loc / MapSize), null ) ); + Map.Actors.Add("Actor"+a++, new ActorReference(kv.Value, new int2(loc % MapSize, loc / MapSize), "Neutral" ) ); } } @@ -268,7 +268,7 @@ namespace MapConverter foreach( KeyValuePair kv in terrain ) { var loc = int.Parse( kv.Key ); - Map.Actors.Add("Actor"+a++, new ActorReference( kv.Value.Split(',')[0], new int2(loc % MapSize, loc / MapSize),null)); + Map.Actors.Add("Actor"+a++, new ActorReference( kv.Value.Split(',')[0], new int2(loc % MapSize, loc / MapSize),"Neutral")); } } @@ -279,7 +279,9 @@ namespace MapConverter { //num=owner,type,health,location,facing,... var parts = s.Value.Split( ',' ); - var loc = int.Parse(parts[3]); + var loc = int.Parse(parts[3]); + if (parts[0] == "") + parts[0] = "Neutral"; Map.Actors.Add("Actor"+a++, new ActorReference( parts[1].ToLowerInvariant(), new int2(loc % MapSize, loc / MapSize), parts[0])); } } diff --git a/OpenRA.FileFormats/FieldLoader.cs b/OpenRA.FileFormats/FieldLoader.cs index 65038a4880..acf18c313f 100644 --- a/OpenRA.FileFormats/FieldLoader.cs +++ b/OpenRA.FileFormats/FieldLoader.cs @@ -1,4 +1,4 @@ -#region Copyright & License Information +#region Copyright & License Information /* * Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford. * This file is part of OpenRA. @@ -107,7 +107,7 @@ namespace OpenRA.FileFormats f => new MiniYaml(FormatValue(o, f)))); } - static string FormatValue(object o, FieldInfo f) + public static string FormatValue(object o, FieldInfo f) { var v = f.GetValue(o); return f.FieldType.IsArray diff --git a/OpenRA.FileFormats/Map/ActorReference.cs b/OpenRA.FileFormats/Map/ActorReference.cs index 42a2afd3d0..0422006708 100644 --- a/OpenRA.FileFormats/Map/ActorReference.cs +++ b/OpenRA.FileFormats/Map/ActorReference.cs @@ -31,5 +31,11 @@ namespace OpenRA.FileFormats Location = location; Owner = owner; } + + public override string ToString () + { + return string.Format("{0} {1} {2},{3}", Name, Owner, Location.X,Location.Y); + } + } } diff --git a/OpenRA.FileFormats/Map/NewMap.cs b/OpenRA.FileFormats/Map/NewMap.cs index c5761e37d1..ed37bd1a69 100644 --- a/OpenRA.FileFormats/Map/NewMap.cs +++ b/OpenRA.FileFormats/Map/NewMap.cs @@ -23,6 +23,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; +using System.Reflection; namespace OpenRA.FileFormats { @@ -51,7 +52,7 @@ namespace OpenRA.FileFormats List SimpleFields = new List() { - "MapFormat", "Title", "Description", "Author", "PlayerCount", "Tileset", "Tiledata", "Preview", "Bounds" + "MapFormat", "Title", "Description", "Author", "PlayerCount", "Tileset", "Tiledata", "Preview", "Size", "Bounds" }; public NewMap() {} @@ -94,9 +95,22 @@ namespace OpenRA.FileFormats public void Save(string filepath) { - // Do stuff + Dictionary root = new Dictionary(); + var d = new Dictionary(); + foreach (var field in SimpleFields) + { + FieldInfo f = this.GetType().GetField(field); + if (f.GetValue(this) == null) continue; + root.Add(field,new MiniYaml(FieldSaver.FormatValue(this,f),null)); + } + root.Add("Actors",MiniYaml.FromDictionary(Actors)); + root.Add("Waypoints",MiniYaml.FromDictionary(Waypoints)); + + // TODO: Players + root.Add("Rules",new MiniYaml(null,Rules)); SaveBinaryData(Tiledata); + root.WriteToFile(filepath); } static byte ReadByte( Stream s ) @@ -169,6 +183,7 @@ namespace OpenRA.FileFormats writer.Flush(); writer.Close(); + File.Move(filepath+".tmp",filepath); } public void DebugContents() diff --git a/OpenRA.FileFormats/MiniYaml.cs b/OpenRA.FileFormats/MiniYaml.cs index 25a0d61db7..3e2fd3cbf2 100755 --- a/OpenRA.FileFormats/MiniYaml.cs +++ b/OpenRA.FileFormats/MiniYaml.cs @@ -39,7 +39,12 @@ namespace OpenRA.FileFormats Value = value; Nodes = nodes; } - + + public static MiniYaml FromDictionary(Dictionarydict) + { + return new MiniYaml( null, dict.ToDictionary( x=>x.Key.ToString(), x=>new MiniYaml(x.Value.ToString()))); + } + static Dictionary FromLines(string[] lines) { var levels = new List>(); diff --git a/OpenRA.FileFormats/OpenRA.FileFormats.csproj b/OpenRA.FileFormats/OpenRA.FileFormats.csproj index 64d1f2114a..0f4f531478 100644 --- a/OpenRA.FileFormats/OpenRA.FileFormats.csproj +++ b/OpenRA.FileFormats/OpenRA.FileFormats.csproj @@ -85,7 +85,6 @@ - @@ -103,6 +102,7 @@ +