Kill MapStub

This commit is contained in:
Paul Chote
2011-02-11 21:21:02 +13:00
parent 73020a9419
commit c2db816837
8 changed files with 79 additions and 112 deletions

View File

@@ -20,8 +20,32 @@ using System.Text;
namespace OpenRA 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<string, ActorReference> Actors = new Dictionary<string, ActorReference>();
public int PlayerCount { get { return SpawnPoints.Count(); } }
public IEnumerable<int2> SpawnPoints { get { return Actors.Values.Where(a => a.Type == "mpspawn").Select(a => a.InitDict.Get<LocationInit>().value); } }
[FieldLoader.Load] public Rectangle Bounds;
// Yaml map data // Yaml map data
public Dictionary<string, PlayerReference> Players = new Dictionary<string, PlayerReference>(); public Dictionary<string, PlayerReference> Players = new Dictionary<string, PlayerReference>();
public List<SmudgeReference> Smudges = new List<SmudgeReference>(); public List<SmudgeReference> Smudges = new List<SmudgeReference>();
@@ -81,10 +105,13 @@ namespace OpenRA
} }
public Map(string path) 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 // 'Simple' metadata
FieldLoader.Load( this, yaml ); FieldLoader.Load( this, yaml );
@@ -92,6 +119,11 @@ namespace OpenRA
// Use release-20110207 to convert older maps to format 4 // Use release-20110207 to convert older maps to format 4
if (MapFormat < 4) if (MapFormat < 4)
throw new InvalidDataException("Map format {0} is not supported.\n File: {1}".F(MapFormat, path)); 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 // Load players
foreach (var kv in yaml.NodesDict["Players"].NodesDict) foreach (var kv in yaml.NodesDict["Players"].NodesDict)
@@ -100,9 +132,26 @@ namespace OpenRA
Players.Add(player.Name, player); Players.Add(player.Name, player);
} }
// Creep player // Upgrade map to format 5
if (MapFormat < 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"))) 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(); mp.Value.Enemies = mp.Value.Enemies.Concat(new[] {"Creeps"}).ToArray();
@@ -361,5 +410,17 @@ namespace OpenRA
{ {
Bounds = Rectangle.FromLTRB(left, top, right, bottom); 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());
}
} }
} }

View File

@@ -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<string, ActorReference> Actors = new Dictionary<string, ActorReference>();
public int PlayerCount { get { return SpawnPoints.Count(); } }
public IEnumerable<int2> SpawnPoints { get { return Actors.Values.Where(a => a.Type == "mpspawn").Select(a => a.InitDict.Get<LocationInit>().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());
}
}
}

View File

@@ -21,7 +21,7 @@ namespace OpenRA
{ {
public readonly Manifest Manifest; public readonly Manifest Manifest;
public readonly ObjectCreator ObjectCreator; public readonly ObjectCreator ObjectCreator;
public Dictionary<string, MapStub> AvailableMaps {get; private set;} public Dictionary<string, Map> AvailableMaps {get; private set;}
public readonly WidgetLoader WidgetLoader; public readonly WidgetLoader WidgetLoader;
public ILoadScreen LoadScreen = null; public ILoadScreen LoadScreen = null;
public SheetBuilder SheetBuilder; public SheetBuilder SheetBuilder;
@@ -99,15 +99,15 @@ namespace OpenRA
.Concat(Directory.GetFiles(dir, "*.oramap")); .Concat(Directory.GetFiles(dir, "*.oramap"));
} }
Dictionary<string, MapStub> FindMaps(string[] mods) Dictionary<string, Map> FindMaps(string[] mods)
{ {
var paths = mods.SelectMany(p => FindMapsIn("mods{0}{1}{0}maps{0}".F(Path.DirectorySeparatorChar, p))) 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)))); .Concat(mods.SelectMany(p => FindMapsIn("{1}maps{0}{2}{0}".F(Path.DirectorySeparatorChar, Game.SupportDir, p))));
Dictionary<string, MapStub> ret = new Dictionary<string, MapStub>(); Dictionary<string, Map> ret = new Dictionary<string, Map>();
foreach (var path in paths) foreach (var path in paths)
{ {
var map = new MapStub(path); var map = new Map(path);
if (ret.ContainsKey(map.Uid)) if (ret.ContainsKey(map.Uid))
System.Console.WriteLine("Ignoring duplicate map: {0}", path); System.Console.WriteLine("Ignoring duplicate map: {0}", path);
else else

View File

@@ -184,7 +184,6 @@
<Compile Include="Widgets\ProgressBarWidget.cs" /> <Compile Include="Widgets\ProgressBarWidget.cs" />
<Compile Include="Utilities.cs" /> <Compile Include="Utilities.cs" />
<Compile Include="Traits\Waypoint.cs" /> <Compile Include="Traits\Waypoint.cs" />
<Compile Include="MapStub.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj"> <ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -19,9 +19,9 @@ namespace OpenRA.Widgets
{ {
public class MapPreviewWidget : Widget public class MapPreviewWidget : Widget
{ {
public Func<MapStub> Map = () => null; public Func<Map> Map = () => null;
public Func<Dictionary<int2, Color>> SpawnColors = () => new Dictionary<int2, Color>(); public Func<Dictionary<int2, Color>> SpawnColors = () => new Dictionary<int2, Color>();
static Cache<MapStub,Bitmap> PreviewCache = new Cache<MapStub, Bitmap>(stub => Minimap.RenderMapPreview( new Map( stub.Path ))); static Cache<Map,Bitmap> PreviewCache = new Cache<Map, Bitmap>(stub => Minimap.RenderMapPreview( new Map( stub.Path )));
public MapPreviewWidget() : base() { } public MapPreviewWidget() : base() { }
protected MapPreviewWidget(MapPreviewWidget other) protected MapPreviewWidget(MapPreviewWidget other)
@@ -33,14 +33,14 @@ namespace OpenRA.Widgets
} }
public override Widget Clone() { return new MapPreviewWidget(this); } 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))); return new int2(MapRect.X + (int)(PreviewScale*(point.X - map.Bounds.Left)) , MapRect.Y + (int)(PreviewScale*(point.Y - map.Bounds.Top)));
} }
Sheet mapChooserSheet; Sheet mapChooserSheet;
Sprite mapChooserSprite; Sprite mapChooserSprite;
MapStub lastMap; Map lastMap;
Rectangle MapRect; Rectangle MapRect;
float PreviewScale = 0; float PreviewScale = 0;
static Sprite UnownedSpawn = null; static Sprite UnownedSpawn = null;

View File

@@ -19,7 +19,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
{ {
public class MapChooserDelegate : IWidgetDelegate public class MapChooserDelegate : IWidgetDelegate
{ {
MapStub Map = null; Map Map = null;
Widget scrollpanel; Widget scrollpanel;
Widget itemTemplate; Widget itemTemplate;

View File

@@ -57,7 +57,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
widget.GetWidget("REPLAY_INFO").IsVisible = () => currentReplay != null; widget.GetWidget("REPLAY_INFO").IsVisible = () => currentReplay != null;
} }
MapStub MapStubFromSummary(ReplaySummary rs) Map MapFromSummary(ReplaySummary rs)
{ {
if (rs.LobbyInfo == null) if (rs.LobbyInfo == null)
return null; return null;
@@ -81,7 +81,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
try try
{ {
var summary = new ReplaySummary(currentReplay); var summary = new ReplaySummary(currentReplay);
var mapStub = MapStubFromSummary(summary); var mapStub = MapFromSummary(summary);
widget.GetWidget<LabelWidget>("DURATION").GetText = widget.GetWidget<LabelWidget>("DURATION").GetText =
() => WidgetUtils.FormatTime(summary.Duration * 3 /* todo: 3:1 ratio isnt always true. */); () => WidgetUtils.FormatTime(summary.Duration * 3 /* todo: 3:1 ratio isnt always true. */);

View File

@@ -98,7 +98,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
}; };
} }
MapStub CurrentMap() Map CurrentMap()
{ {
return (currentServer == null || !Game.modData.AvailableMaps.ContainsKey(currentServer.Map)) return (currentServer == null || !Game.modData.AvailableMaps.ContainsKey(currentServer.Map))
? null : Game.modData.AvailableMaps[currentServer.Map]; ? null : Game.modData.AvailableMaps[currentServer.Map];