From c2db81683718d0ebac1ec56cf16a7736304dc33d Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 11 Feb 2011 21:21:02 +1300 Subject: [PATCH] Kill MapStub --- OpenRA.Game/Map.cs | 73 +++++++++++++-- OpenRA.Game/MapStub.cs | 93 ------------------- OpenRA.Game/ModData.cs | 8 +- OpenRA.Game/OpenRA.Game.csproj | 1 - OpenRA.Game/Widgets/MapPreviewWidget.cs | 8 +- .../Widgets/Delegates/MapChooserDelegate.cs | 2 +- .../Delegates/ReplayBrowserDelegate.cs | 4 +- .../Delegates/ServerBrowserDelegate.cs | 2 +- 8 files changed, 79 insertions(+), 112 deletions(-) delete mode 100644 OpenRA.Game/MapStub.cs diff --git a/OpenRA.Game/Map.cs b/OpenRA.Game/Map.cs index 5166422999..fff055989b 100644 --- a/OpenRA.Game/Map.cs +++ b/OpenRA.Game/Map.cs @@ -20,8 +20,32 @@ using System.Text; namespace OpenRA { - public class Map : MapStub + public class Map { + protected IFolder Container; + public string Path {get; protected set;} + + // Yaml map data + public string Uid { get; protected set; } + [FieldLoader.Load] public int MapFormat; + [FieldLoader.Load] public bool Selectable; + [FieldLoader.Load] public bool UseAsShellmap; + [FieldLoader.Load] public string RequiresMod; + + [FieldLoader.Load] public string Title; + [FieldLoader.Load] public string Type = "Conquest"; + [FieldLoader.Load] public string Description; + [FieldLoader.Load] public string Author; + [FieldLoader.Load] public string Tileset; + + public Dictionary Actors = new Dictionary(); + + public int PlayerCount { get { return SpawnPoints.Count(); } } + public IEnumerable SpawnPoints { get { return Actors.Values.Where(a => a.Type == "mpspawn").Select(a => a.InitDict.Get().value); } } + + [FieldLoader.Load] public Rectangle Bounds; + + // Yaml map data public Dictionary Players = new Dictionary(); public List Smudges = new List(); @@ -81,10 +105,13 @@ namespace OpenRA } public Map(string path) - : base(path) { - var yaml = new MiniYaml( null, MiniYaml.FromStream( Container.GetContent( "map.yaml" ) ) ); - + Path = path; + Container = FileSystem.OpenPackage(path, int.MaxValue); + var yaml = new MiniYaml( null, MiniYaml.FromStream(Container.GetContent("map.yaml")) ); + FieldLoader.Load(this, yaml); + Uid = ComputeHash(); + // 'Simple' metadata FieldLoader.Load( this, yaml ); @@ -92,6 +119,11 @@ namespace OpenRA // Use release-20110207 to convert older maps to format 4 if (MapFormat < 4) throw new InvalidDataException("Map format {0} is not supported.\n File: {1}".F(MapFormat, path)); + + // Load actors + foreach (var kv in yaml.NodesDict["Actors"].NodesDict) + Actors.Add(kv.Key, new ActorReference(kv.Value.Value, kv.Value.NodesDict)); + // Load players foreach (var kv in yaml.NodesDict["Players"].NodesDict) @@ -100,9 +132,26 @@ namespace OpenRA Players.Add(player.Name, player); } - // Creep player + // Upgrade map to format 5 if (MapFormat < 5) - { + { + // Define RequiresMod for map installer + RequiresMod = Game.CurrentMods.Keys.First(); + + // Add waypoint actors + foreach( var wp in yaml.NodesDict[ "Waypoints" ].NodesDict ) + { + string[] loc = wp.Value.Value.Split( ',' ); + var a = new ActorReference("mpspawn"); + a.Add(new LocationInit(new int2( int.Parse( loc[ 0 ] ), int.Parse( loc[ 1 ] ) ))); + Actors.Add(wp.Key, a); + } + + var TopLeft = (int2)FieldLoader.GetValue( "", typeof(int2), yaml.NodesDict["TopLeft"].Value); + var BottomRight = (int2)FieldLoader.GetValue( "", typeof(int2), yaml.NodesDict["BottomRight"].Value); + Bounds = Rectangle.FromLTRB(TopLeft.X, TopLeft.Y, BottomRight.X, BottomRight.Y); + + // Creep player foreach (var mp in Players.Where(p => !p.Value.NonCombatant && !p.Value.Enemies.Contains("Creeps"))) mp.Value.Enemies = mp.Value.Enemies.Concat(new[] {"Creeps"}).ToArray(); @@ -361,5 +410,17 @@ namespace OpenRA { Bounds = Rectangle.FromLTRB(left, top, right, bottom); } + + string ComputeHash() + { + // UID is calculated by taking an SHA1 of the yaml and binary data + // Read the relevant data into a buffer + var data = Container.GetContent("map.yaml").ReadAllBytes() + .Concat(Container.GetContent("map.bin").ReadAllBytes()).ToArray(); + + // Take the SHA1 + using (var csp = SHA1.Create()) + return new string(csp.ComputeHash(data).SelectMany(a => a.ToString("x2")).ToArray()); + } } } diff --git a/OpenRA.Game/MapStub.cs b/OpenRA.Game/MapStub.cs deleted file mode 100644 index a7ffa0a936..0000000000 --- a/OpenRA.Game/MapStub.cs +++ /dev/null @@ -1,93 +0,0 @@ -#region Copyright & License Information -/* - * Copyright 2007-2010 The OpenRA Developers (see AUTHORS) - * This file is part of OpenRA, which is free software. It is made - * available to you under the terms of the GNU General Public License - * as published by the Free Software Foundation. For more information, - * see LICENSE. - */ -#endregion - -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Security.Cryptography; -using OpenRA.FileFormats; -using OpenRA.Traits; - -namespace OpenRA -{ - public class MapStub - { - protected IFolder Container; - public string Path {get; protected set;} - - // Yaml map data - public string Uid { get; protected set; } - [FieldLoader.Load] public int MapFormat; - [FieldLoader.Load] public bool Selectable; - [FieldLoader.Load] public bool UseAsShellmap; - [FieldLoader.Load] public string RequiresMod; - - [FieldLoader.Load] public string Title; - [FieldLoader.Load] public string Type = "Conquest"; - [FieldLoader.Load] public string Description; - [FieldLoader.Load] public string Author; - [FieldLoader.Load] public string Tileset; - - public Dictionary Actors = new Dictionary(); - - public int PlayerCount { get { return SpawnPoints.Count(); } } - public IEnumerable SpawnPoints { get { return Actors.Values.Where(a => a.Type == "mpspawn").Select(a => a.InitDict.Get().value); } } - - [FieldLoader.Load] public Rectangle Bounds; - - public MapStub() {} // Hack for the editor - not used for anything important - - public MapStub(string path) - { - Path = path; - Container = FileSystem.OpenPackage(path, int.MaxValue); - var yaml = new MiniYaml( null, MiniYaml.FromStream(Container.GetContent("map.yaml")) ); - FieldLoader.Load(this, yaml); - Uid = ComputeHash(); - - // Load actors - foreach (var kv in yaml.NodesDict["Actors"].NodesDict) - Actors.Add(kv.Key, new ActorReference(kv.Value.Value, kv.Value.NodesDict)); - - // Upgrade map to format 5 - if (MapFormat < 5) - { - // Define RequiresMod for map installer - RequiresMod = Game.CurrentMods.Keys.First(); - - // Add waypoint actors - foreach( var wp in yaml.NodesDict[ "Waypoints" ].NodesDict ) - { - string[] loc = wp.Value.Value.Split( ',' ); - var a = new ActorReference("mpspawn"); - a.Add(new LocationInit(new int2( int.Parse( loc[ 0 ] ), int.Parse( loc[ 1 ] ) ))); - Actors.Add(wp.Key, a); - } - - var TopLeft = (int2)FieldLoader.GetValue( "", typeof(int2), yaml.NodesDict["TopLeft"].Value); - var BottomRight = (int2)FieldLoader.GetValue( "", typeof(int2), yaml.NodesDict["BottomRight"].Value); - Bounds = Rectangle.FromLTRB(TopLeft.X, TopLeft.Y, BottomRight.X, BottomRight.Y); - } - } - - string ComputeHash() - { - // UID is calculated by taking an SHA1 of the yaml and binary data - // Read the relevant data into a buffer - var data = Container.GetContent("map.yaml").ReadAllBytes() - .Concat(Container.GetContent("map.bin").ReadAllBytes()).ToArray(); - - // Take the SHA1 - using (var csp = SHA1.Create()) - return new string(csp.ComputeHash(data).SelectMany(a => a.ToString("x2")).ToArray()); - } - } -} diff --git a/OpenRA.Game/ModData.cs b/OpenRA.Game/ModData.cs index 0820b0896b..2b3af578ca 100755 --- a/OpenRA.Game/ModData.cs +++ b/OpenRA.Game/ModData.cs @@ -21,7 +21,7 @@ namespace OpenRA { public readonly Manifest Manifest; public readonly ObjectCreator ObjectCreator; - public Dictionary AvailableMaps {get; private set;} + public Dictionary AvailableMaps {get; private set;} public readonly WidgetLoader WidgetLoader; public ILoadScreen LoadScreen = null; public SheetBuilder SheetBuilder; @@ -99,15 +99,15 @@ namespace OpenRA .Concat(Directory.GetFiles(dir, "*.oramap")); } - Dictionary FindMaps(string[] mods) + Dictionary FindMaps(string[] mods) { var paths = mods.SelectMany(p => FindMapsIn("mods{0}{1}{0}maps{0}".F(Path.DirectorySeparatorChar, p))) .Concat(mods.SelectMany(p => FindMapsIn("{1}maps{0}{2}{0}".F(Path.DirectorySeparatorChar, Game.SupportDir, p)))); - Dictionary ret = new Dictionary(); + Dictionary ret = new Dictionary(); foreach (var path in paths) { - var map = new MapStub(path); + var map = new Map(path); if (ret.ContainsKey(map.Uid)) System.Console.WriteLine("Ignoring duplicate map: {0}", path); else diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index db770c5472..5f48fe50f4 100755 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -184,7 +184,6 @@ - diff --git a/OpenRA.Game/Widgets/MapPreviewWidget.cs b/OpenRA.Game/Widgets/MapPreviewWidget.cs index 5e17f49b4a..2afd6ef9ad 100644 --- a/OpenRA.Game/Widgets/MapPreviewWidget.cs +++ b/OpenRA.Game/Widgets/MapPreviewWidget.cs @@ -19,9 +19,9 @@ namespace OpenRA.Widgets { public class MapPreviewWidget : Widget { - public Func Map = () => null; + public Func Map = () => null; public Func> SpawnColors = () => new Dictionary(); - static Cache PreviewCache = new Cache(stub => Minimap.RenderMapPreview( new Map( stub.Path ))); + static Cache PreviewCache = new Cache(stub => Minimap.RenderMapPreview( new Map( stub.Path ))); public MapPreviewWidget() : base() { } protected MapPreviewWidget(MapPreviewWidget other) @@ -33,14 +33,14 @@ namespace OpenRA.Widgets } public override Widget Clone() { return new MapPreviewWidget(this); } - public int2 ConvertToPreview(MapStub map, int2 point) + public int2 ConvertToPreview(Map map, int2 point) { return new int2(MapRect.X + (int)(PreviewScale*(point.X - map.Bounds.Left)) , MapRect.Y + (int)(PreviewScale*(point.Y - map.Bounds.Top))); } Sheet mapChooserSheet; Sprite mapChooserSprite; - MapStub lastMap; + Map lastMap; Rectangle MapRect; float PreviewScale = 0; static Sprite UnownedSpawn = null; diff --git a/OpenRA.Mods.RA/Widgets/Delegates/MapChooserDelegate.cs b/OpenRA.Mods.RA/Widgets/Delegates/MapChooserDelegate.cs index a193011147..41ac16ed24 100644 --- a/OpenRA.Mods.RA/Widgets/Delegates/MapChooserDelegate.cs +++ b/OpenRA.Mods.RA/Widgets/Delegates/MapChooserDelegate.cs @@ -19,7 +19,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates { public class MapChooserDelegate : IWidgetDelegate { - MapStub Map = null; + Map Map = null; Widget scrollpanel; Widget itemTemplate; diff --git a/OpenRA.Mods.RA/Widgets/Delegates/ReplayBrowserDelegate.cs b/OpenRA.Mods.RA/Widgets/Delegates/ReplayBrowserDelegate.cs index c74fc62d8a..9416d748be 100644 --- a/OpenRA.Mods.RA/Widgets/Delegates/ReplayBrowserDelegate.cs +++ b/OpenRA.Mods.RA/Widgets/Delegates/ReplayBrowserDelegate.cs @@ -57,7 +57,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates widget.GetWidget("REPLAY_INFO").IsVisible = () => currentReplay != null; } - MapStub MapStubFromSummary(ReplaySummary rs) + Map MapFromSummary(ReplaySummary rs) { if (rs.LobbyInfo == null) return null; @@ -81,7 +81,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates try { var summary = new ReplaySummary(currentReplay); - var mapStub = MapStubFromSummary(summary); + var mapStub = MapFromSummary(summary); widget.GetWidget("DURATION").GetText = () => WidgetUtils.FormatTime(summary.Duration * 3 /* todo: 3:1 ratio isnt always true. */); diff --git a/OpenRA.Mods.RA/Widgets/Delegates/ServerBrowserDelegate.cs b/OpenRA.Mods.RA/Widgets/Delegates/ServerBrowserDelegate.cs index 03fd85828c..94fe4dbe7f 100644 --- a/OpenRA.Mods.RA/Widgets/Delegates/ServerBrowserDelegate.cs +++ b/OpenRA.Mods.RA/Widgets/Delegates/ServerBrowserDelegate.cs @@ -98,7 +98,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates }; } - MapStub CurrentMap() + Map CurrentMap() { return (currentServer == null || !Game.modData.AvailableMaps.ContainsKey(currentServer.Map)) ? null : Game.modData.AvailableMaps[currentServer.Map];