Refactoring to remove static Rules & SequenceProvider

This commit is contained in:
Pavlos Touboulidis
2014-05-05 02:43:08 +03:00
parent c68427eaa6
commit 63ec6d60e7
114 changed files with 914 additions and 615 deletions

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2014 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,
@@ -28,6 +28,9 @@ namespace OpenRA.Graphics
public string Name { get { return name; } }
readonly SequenceProvider sequenceProvider;
static SequenceProvider lastSequenceProvider;
public Animation(string name)
: this(name, () => 0) {}
@@ -36,6 +39,17 @@ namespace OpenRA.Graphics
this.name = name.ToLowerInvariant();
this.tickFunc = () => {};
this.facingFunc = facingFunc;
// TODO: This is wrong, don't use the static
if (Game.orderManager != null && Game.orderManager.world != null && Game.orderManager.world.Map != null)
sequenceProvider = Game.orderManager.world.Map.SequenceProvider;
// HACK: This just makes sure we have a sequence provider in between map changes for delayed actions
// It sucks but it can only be removed when we don't use the statics above but replace them with
// a possible parameter on this constructor.
if (sequenceProvider == null)
sequenceProvider = lastSequenceProvider;
else
lastSequenceProvider = sequenceProvider;
}
int CurrentFrame { get { return backwards ? CurrentSequence.Start + CurrentSequence.Length - frame - 1 : frame; } }
@@ -66,7 +80,7 @@ namespace OpenRA.Graphics
{
backwards = false;
tickAlways = false;
CurrentSequence = SequenceProvider.GetSequence(name, sequenceName);
CurrentSequence = sequenceProvider.GetSequence(name, sequenceName);
frame = 0;
tickFunc = () =>
{
@@ -81,7 +95,7 @@ namespace OpenRA.Graphics
if (!HasSequence(sequenceName))
return false;
CurrentSequence = SequenceProvider.GetSequence(name, sequenceName);
CurrentSequence = sequenceProvider.GetSequence(name, sequenceName);
frame %= CurrentSequence.Length;
return true;
}
@@ -90,7 +104,7 @@ namespace OpenRA.Graphics
{
backwards = false;
tickAlways = false;
CurrentSequence = SequenceProvider.GetSequence(name, sequenceName);
CurrentSequence = sequenceProvider.GetSequence(name, sequenceName);
frame = 0;
tickFunc = () =>
{
@@ -114,7 +128,7 @@ namespace OpenRA.Graphics
{
backwards = false;
tickAlways = true;
CurrentSequence = SequenceProvider.GetSequence(name, sequenceName);
CurrentSequence = sequenceProvider.GetSequence(name, sequenceName);
frame = func();
tickFunc = () => frame = func();
}
@@ -128,7 +142,7 @@ namespace OpenRA.Graphics
Tick(40); // tick one frame
}
public bool HasSequence(string seq) { return SequenceProvider.HasSequence(name, seq); }
public bool HasSequence(string seq) { return sequenceProvider.HasSequence(name, seq); }
public void Tick(int t)
{
@@ -159,7 +173,7 @@ namespace OpenRA.Graphics
public Sequence GetSequence(string sequenceName)
{
return SequenceProvider.GetSequence(name, sequenceName);
return sequenceProvider.GetSequence(name, sequenceName);
}
}
}

View File

@@ -16,23 +16,18 @@ using OpenRA.Primitives;
namespace OpenRA.Graphics
{
public static class CursorProvider
public class CursorProvider
{
static HardwarePalette palette;
static Dictionary<string, CursorSequence> cursors;
static Cache<string, PaletteReference> palettes;
HardwarePalette palette;
Dictionary<string, CursorSequence> cursors;
Cache<string, PaletteReference> palettes;
static PaletteReference CreatePaletteReference(string name)
public Action OnProgress = () => { if (Game.modData != null && Game.modData.LoadScreen != null) Game.modData.LoadScreen.Display(); };
public CursorProvider(ModData modData)
{
var pal = palette.GetPalette(name);
if (pal == null)
throw new InvalidOperationException("Palette `{0}` does not exist".F(name));
var sequenceFiles = modData.Manifest.Cursors;
return new PaletteReference(name, palette.GetPaletteIndex(name), pal);
}
public static void Initialize(string[] sequenceFiles)
{
cursors = new Dictionary<string, CursorSequence>();
palettes = new Cache<string, PaletteReference>(CreatePaletteReference);
var sequences = new MiniYaml(null, sequenceFiles.Select(s => MiniYaml.FromFile(s)).Aggregate(MiniYaml.MergeLiberal));
@@ -49,26 +44,37 @@ namespace OpenRA.Graphics
foreach (var p in sequences.NodesDict["Palettes"].Nodes)
palette.AddPalette(p.Key, new Palette(GlobalFileSystem.Open(p.Value.Value), shadowIndex), false);
var spriteLoader = new SpriteLoader(new string[0], new SheetBuilder(SheetType.Indexed));
foreach (var s in sequences.NodesDict["Cursors"].Nodes)
LoadSequencesForCursor(s.Key, s.Value);
LoadSequencesForCursor(spriteLoader, s.Key, s.Value);
palette.Initialize();
}
static void LoadSequencesForCursor(string cursorSrc, MiniYaml cursor)
PaletteReference CreatePaletteReference(string name)
{
Game.modData.LoadScreen.Display();
var pal = palette.GetPalette(name);
if (pal == null)
throw new InvalidOperationException("Palette `{0}` does not exist".F(name));
foreach (var sequence in cursor.Nodes)
cursors.Add(sequence.Key, new CursorSequence(cursorSrc, cursor.Value, sequence.Value));
return new PaletteReference(name, palette.GetPaletteIndex(name), pal);
}
public static bool HasCursorSequence(string cursor)
void LoadSequencesForCursor(SpriteLoader loader, string cursorSrc, MiniYaml cursor)
{
OnProgress();
foreach (var sequence in cursor.Nodes)
cursors.Add(sequence.Key, new CursorSequence(loader, cursorSrc, cursor.Value, sequence.Value));
}
public bool HasCursorSequence(string cursor)
{
return cursors.ContainsKey(cursor);
}
public static void DrawCursor(Renderer renderer, string cursorName, int2 lastMousePos, int cursorFrame)
public void DrawCursor(Renderer renderer, string cursorName, int2 lastMousePos, int cursorFrame)
{
var cursorSequence = GetCursorSequence(cursorName);
var cursorSprite = cursorSequence.GetSprite(cursorFrame);
@@ -80,13 +86,12 @@ namespace OpenRA.Graphics
cursorSprite.size);
}
public static CursorSequence GetCursorSequence(string cursor)
public CursorSequence GetCursorSequence(string cursor)
{
try { return cursors[cursor]; }
catch (KeyNotFoundException)
{
throw new InvalidOperationException(
"Cursor does not have a sequence `{0}`".F(cursor));
throw new InvalidOperationException("Cursor does not have a sequence `{0}`".F(cursor));
}
}
}

View File

@@ -25,9 +25,9 @@ namespace OpenRA.Graphics
Sprite[] sprites;
public CursorSequence(string cursorSrc, string palette, MiniYaml info)
public CursorSequence(SpriteLoader loader, string cursorSrc, string palette, MiniYaml info)
{
sprites = Game.modData.SpriteLoader.LoadAllSprites(cursorSrc);
sprites = loader.LoadAllSprites(cursorSrc);
var d = info.NodesDict;
start = Exts.ParseIntegerInvariant(d["start"].Value);

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2014 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,
@@ -19,14 +19,8 @@ namespace OpenRA.Graphics
{
public class Minimap
{
public static Bitmap TerrainBitmap(Map map)
public static Bitmap TerrainBitmap(TileSet tileset, Map map, bool actualSize = false)
{
return TerrainBitmap(map, false);
}
public static Bitmap TerrainBitmap(Map map, bool actualSize)
{
var tileset = Rules.TileSets[map.Tileset];
var width = map.Bounds.Width;
var height = map.Bounds.Height;
@@ -61,10 +55,9 @@ namespace OpenRA.Graphics
// Add the static resources defined in the map; if the map lives
// in a world use AddCustomTerrain instead
public static Bitmap AddStaticResources(Map map, Bitmap terrainBitmap)
public static Bitmap AddStaticResources(TileSet tileset, Map map, Bitmap terrainBitmap)
{
Bitmap terrain = new Bitmap(terrainBitmap);
var tileset = Rules.TileSets[map.Tileset];
var bitmapData = terrain.LockBits(terrain.Bounds(),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
@@ -81,7 +74,7 @@ namespace OpenRA.Graphics
if (map.MapResources.Value[mapX, mapY].Type == 0)
continue;
var res = Rules.Info["world"].Traits.WithInterface<ResourceTypeInfo>()
var res = map.Rules.Actors["world"].Traits.WithInterface<ResourceTypeInfo>()
.Where(t => t.ResourceType == map.MapResources.Value[mapX, mapY].Type)
.Select(t => t.TerrainType).FirstOrDefault();
if (res == null)
@@ -185,10 +178,10 @@ namespace OpenRA.Graphics
return bitmap;
}
public static Bitmap RenderMapPreview(Map map, bool actualSize)
public static Bitmap RenderMapPreview(TileSet tileset, Map map, bool actualSize)
{
Bitmap terrain = TerrainBitmap(map, actualSize);
return AddStaticResources(map, terrain);
Bitmap terrain = TerrainBitmap(tileset, map, actualSize);
return AddStaticResources(tileset, map, terrain);
}
}
}

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Graphics
public readonly int ShadowZOffset;
public readonly int[] Frames;
public Sequence(string unit, string name, MiniYaml info)
public Sequence(SpriteLoader loader, string unit, string name, MiniYaml info)
{
var srcOverride = info.Value;
Name = name;
@@ -50,7 +50,7 @@ namespace OpenRA.Graphics
// Apply offset to each sprite in the sequence
// Different sequences may apply different offsets to the same frame
sprites = Game.modData.SpriteLoader.LoadAllSprites(srcOverride ?? unit).Select(
sprites = loader.LoadAllSprites(srcOverride ?? unit).Select(
s => new Sprite(s.sheet, s.bounds, s.offset + offset, s.channel, blendMode)).ToArray();
if (!d.ContainsKey("Length"))

View File

@@ -17,102 +17,101 @@ using OpenRA.Primitives;
namespace OpenRA.Graphics
{
public static class SequenceProvider
public class SequenceProvider
{
public static Sequence GetSequence(string unitName, string sequenceName)
readonly Lazy<IReadOnlyDictionary<string, Lazy<IReadOnlyDictionary<string, Sequence>>>> sequences;
public SequenceProvider(Map map)
{
return Game.modData.SequenceProvider.GetSequence(unitName, sequenceName);
this.sequences = Exts.Lazy(() => map.Rules.TileSets[map.Tileset].Data.SequenceCache.LoadSequences(map));
}
public static bool HasSequence(string unitName, string sequenceName)
public Sequence GetSequence(string unitName, string sequenceName)
{
return Game.modData.SequenceProvider.HasSequence(unitName, sequenceName);
try
{
return sequences.Value[unitName].Value[sequenceName];
}
catch (KeyNotFoundException)
{
if (sequences.Value.ContainsKey(unitName))
throw new InvalidOperationException("Unit `{0}` does not have a sequence `{1}`".F(unitName, sequenceName));
else
throw new InvalidOperationException("Unit `{0}` does not have all sequences defined.".F(unitName));
}
}
public static IEnumerable<string> Sequences(string unitName)
public bool HasSequence(string unitName, string sequenceName)
{
return Game.modData.SequenceProvider.Sequences(unitName);
if (!sequences.Value.ContainsKey(unitName))
throw new InvalidOperationException("Unit `{0}` does not have sequence `{1}` defined.".F(unitName, sequenceName));
return sequences.Value[unitName].Value.ContainsKey(sequenceName);
}
public IEnumerable<string> Sequences(string unitName)
{
if (!sequences.Value.ContainsKey(unitName))
throw new InvalidOperationException("Unit `{0}` does not have all sequences defined.".F(unitName));
return sequences.Value[unitName].Value.Keys;
}
}
public class ModSequenceProvider
public class SequenceCache
{
readonly ModData modData;
readonly TileSet tileSet;
readonly Dictionary<string, Lazy<Dictionary<string, Sequence>>> sequenceCache = new Dictionary<string, Lazy<Dictionary<string, Sequence>>>();
Dictionary<string, Lazy<Dictionary<string, Sequence>>> sequences;
readonly Dictionary<string, Lazy<IReadOnlyDictionary<string, Sequence>>> sequenceCache = new Dictionary<string, Lazy<IReadOnlyDictionary<string, Sequence>>>();
public ModSequenceProvider(ModData modData)
public Action OnProgress = () => { if (Game.modData != null && Game.modData.LoadScreen != null) Game.modData.LoadScreen.Display(); };
public SequenceCache(ModData modData, TileSet tileSet)
{
this.modData = modData;
this.tileSet = tileSet;
}
public void ActivateMap(Map map)
public IReadOnlyDictionary<string, Lazy<IReadOnlyDictionary<string, Sequence>>> LoadSequences(Map map)
{
sequences = Load(modData.Manifest.Sequences, map.Tileset, map.Sequences);
using (new Support.PerfTimer("LoadSequences"))
return Load(map.SequenceDefinitions);
}
public Dictionary<string, Lazy<Dictionary<string, Sequence>>> Load(string[] sequenceFiles, string tileset, List<MiniYamlNode> sequenceNodes)
IReadOnlyDictionary<string, Lazy<IReadOnlyDictionary<string, Sequence>>> Load(List<MiniYamlNode> sequenceNodes)
{
Game.modData.LoadScreen.Display();
OnProgress();
var sequenceFiles = modData.Manifest.Sequences;
var nodes = sequenceFiles
.Select(s => MiniYaml.FromFile(s))
.Aggregate(sequenceNodes, MiniYaml.MergeLiberal);
var items = new Dictionary<string, Lazy<Dictionary<string, Sequence>>>();
var items = new Dictionary<string, Lazy<IReadOnlyDictionary<string, Sequence>>>();
foreach (var node in nodes)
{
// Sequence loading uses the active SpriteLoader that depends on the current map's tileset
var key = node.Value.ToLines(node.Key).JoinWith("|");
var key = tileset + node.Value.ToLines(node.Key).JoinWith("|");
Lazy<Dictionary<string, Sequence>> t;
Lazy<IReadOnlyDictionary<string, Sequence>> t;
if (sequenceCache.TryGetValue(key, out t))
{
items.Add(node.Key, t);
}
else
{
t = Exts.Lazy(() => node.Value.NodesDict.ToDictionary(x => x.Key, x => new Sequence(node.Key, x.Key, x.Value)));
t = Exts.Lazy(() => (IReadOnlyDictionary<string, Sequence>)new ReadOnlyDictionary<string, Sequence>(
node.Value.NodesDict.ToDictionary(x => x.Key, x =>
new Sequence(tileSet.Data.SpriteLoader, node.Key, x.Key, x.Value))));
sequenceCache.Add(key, t);
items.Add(node.Key, t);
}
OnProgress();
}
return items;
}
public Sequence GetSequence(string unitName, string sequenceName)
{
try { return sequences[unitName].Value[sequenceName]; }
catch (KeyNotFoundException)
{
if (sequences.ContainsKey(unitName))
throw new InvalidOperationException(
"Unit `{0}` does not have a sequence `{1}`".F(unitName, sequenceName));
else
throw new InvalidOperationException(
"Unit `{0}` does not have all sequences defined.".F(unitName));
}
}
public bool HasSequence(string unitName, string sequenceName)
{
if (!sequences.ContainsKey(unitName))
throw new InvalidOperationException(
"Unit `{0}` does not have sequence `{1}` defined.".F(unitName, sequenceName));
return sequences[unitName].Value.ContainsKey(sequenceName);
}
public IEnumerable<string> Sequences(string unitName)
{
if (!sequences.ContainsKey(unitName))
throw new InvalidOperationException(
"Unit `{0}` does not have all sequences defined.".F(unitName));
return sequences[unitName].Value.Keys;
return new ReadOnlyDictionary<string, Lazy<IReadOnlyDictionary<string, Sequence>>>(items);
}
}
}

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2014 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,
@@ -18,7 +18,7 @@ namespace OpenRA.Graphics
{
public class SpriteLoader
{
readonly SheetBuilder SheetBuilder;
public readonly SheetBuilder SheetBuilder;
readonly Cache<string, Sprite[]> sprites;
readonly string[] exts;