diff --git a/OpenRA.Editor/OpenRA.Editor.csproj b/OpenRA.Editor/OpenRA.Editor.csproj
index 166adf6e68..be2dd5e5fa 100644
--- a/OpenRA.Editor/OpenRA.Editor.csproj
+++ b/OpenRA.Editor/OpenRA.Editor.csproj
@@ -165,6 +165,7 @@
Component
+
diff --git a/OpenRA.FileFormats/Graphics/TileSetRenderer.cs b/OpenRA.Editor/TileSetRenderer.cs
similarity index 69%
rename from OpenRA.FileFormats/Graphics/TileSetRenderer.cs
rename to OpenRA.Editor/TileSetRenderer.cs
index abb93b1cca..c1b1482b1f 100644
--- a/OpenRA.FileFormats/Graphics/TileSetRenderer.cs
+++ b/OpenRA.Editor/TileSetRenderer.cs
@@ -13,8 +13,9 @@ using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
+using OpenRA.FileFormats;
-namespace OpenRA.FileFormats
+namespace OpenRA.Editor
{
public class TileSetRenderer
{
@@ -22,34 +23,31 @@ namespace OpenRA.FileFormats
Dictionary> templates;
public Size TileSize;
- List LoadTemplate(string filename, string[] exts, Cache r8cache, int[] frames)
+ List LoadTemplate(string filename, string[] exts, Dictionary sourceCache, int[] frames)
{
- if (exts.Contains(".R8") && FileSystem.Exists(filename + ".R8"))
+ ISpriteSource source;
+ if (!sourceCache.ContainsKey(filename))
{
- var data = new List();
- foreach (var f in frames)
- data.Add(f >= 0 ? r8cache[filename][f].Data : null);
+ using (var s = FileSystem.OpenWithExts(filename, exts))
+ source = SpriteSource.LoadSpriteSource(s, filename);
- return data;
+ if (source.CacheWhenLoadingTileset)
+ sourceCache.Add(filename, source);
+ }
+ else
+ source = sourceCache[filename];
+
+ if (frames != null)
+ {
+ var ret = new List();
+ var srcFrames = source.Frames.ToArray();
+ foreach (var i in frames)
+ ret.Add(srcFrames[i].Data);
+
+ return ret;
}
- using (var s = FileSystem.OpenWithExts(filename, exts))
- {
- var type = SpriteSource.DetectSpriteType(s);
- ISpriteSource source;
- switch (type)
- {
- case SpriteType.TmpTD:
- source = new TmpTDReader(s);
- break;
- case SpriteType.TmpRA:
- source = new TmpRAReader(s);
- break;
- default:
- throw new InvalidDataException(filename + " is not a valid terrain tile");
- }
- return source.Frames.Select(f => f.Data).ToList();
- }
+ return source.Frames.Select(f => f.Data).ToList();
}
public TileSetRenderer(TileSet tileset, Size tileSize)
@@ -58,9 +56,9 @@ namespace OpenRA.FileFormats
this.TileSize = tileSize;
templates = new Dictionary>();
- var r8cache = new Cache(s => new R8Reader(FileSystem.OpenWithExts(s, ".R8")).Frames.ToArray());
+ var sourceCache = new Dictionary();
foreach (var t in TileSet.Templates)
- templates.Add(t.Key, LoadTemplate(t.Value.Image, tileset.Extensions, r8cache, t.Value.Frames));
+ templates.Add(t.Key, LoadTemplate(t.Value.Image, tileset.Extensions, sourceCache, t.Value.Frames));
}
public Bitmap RenderTemplate(ushort id, Palette p)
diff --git a/OpenRA.FileFormats/Graphics/R8Reader.cs b/OpenRA.FileFormats/Graphics/R8Reader.cs
index de0cb8eaf3..edc95b2cf2 100644
--- a/OpenRA.FileFormats/Graphics/R8Reader.cs
+++ b/OpenRA.FileFormats/Graphics/R8Reader.cs
@@ -63,6 +63,7 @@ namespace OpenRA.FileFormats
{
readonly List frames = new List();
public IEnumerable Frames { get { return frames.Cast(); } }
+ public bool CacheWhenLoadingTileset { get { return true; } }
public readonly int ImageCount;
public R8Reader(Stream stream)
diff --git a/OpenRA.FileFormats/Graphics/ShpD2Reader.cs b/OpenRA.FileFormats/Graphics/ShpD2Reader.cs
index 622d7d8d47..aa21efc521 100644
--- a/OpenRA.FileFormats/Graphics/ShpD2Reader.cs
+++ b/OpenRA.FileFormats/Graphics/ShpD2Reader.cs
@@ -88,6 +88,7 @@ namespace OpenRA.FileFormats
{
List headers = new List();
public IEnumerable Frames { get { return headers.Cast(); } }
+ public bool CacheWhenLoadingTileset { get { return false; } }
public ShpD2Reader(Stream s)
{
diff --git a/OpenRA.FileFormats/Graphics/ShpReader.cs b/OpenRA.FileFormats/Graphics/ShpReader.cs
index c10d61d8af..b211b94751 100644
--- a/OpenRA.FileFormats/Graphics/ShpReader.cs
+++ b/OpenRA.FileFormats/Graphics/ShpReader.cs
@@ -58,6 +58,7 @@ namespace OpenRA.FileFormats
{
readonly List headers = new List();
public IEnumerable Frames { get { return headers.Cast(); } }
+ public bool CacheWhenLoadingTileset { get { return false; } }
public readonly Size Size;
int recurseDepth = 0;
diff --git a/OpenRA.FileFormats/Graphics/ShpTSReader.cs b/OpenRA.FileFormats/Graphics/ShpTSReader.cs
index 7fc4a463bf..90c45aa85a 100644
--- a/OpenRA.FileFormats/Graphics/ShpTSReader.cs
+++ b/OpenRA.FileFormats/Graphics/ShpTSReader.cs
@@ -46,6 +46,7 @@ namespace OpenRA.FileFormats
{
readonly List frames = new List();
public IEnumerable Frames { get { return frames.Cast(); } }
+ public bool CacheWhenLoadingTileset { get { return false; } }
public ShpTSReader(Stream stream)
{
diff --git a/OpenRA.FileFormats/Graphics/SpriteSource.cs b/OpenRA.FileFormats/Graphics/SpriteSource.cs
index b1af05257d..9c7f25a672 100644
--- a/OpenRA.FileFormats/Graphics/SpriteSource.cs
+++ b/OpenRA.FileFormats/Graphics/SpriteSource.cs
@@ -26,6 +26,7 @@ namespace OpenRA.FileFormats
public interface ISpriteSource
{
IEnumerable Frames { get; }
+ bool CacheWhenLoadingTileset { get; }
}
public enum SpriteType { Unknown, ShpTD, ShpTS, ShpD2, TmpTD, TmpRA, R8 }
diff --git a/OpenRA.FileFormats/Graphics/TmpRAReader.cs b/OpenRA.FileFormats/Graphics/TmpRAReader.cs
index c126554e6b..ce627eacac 100644
--- a/OpenRA.FileFormats/Graphics/TmpRAReader.cs
+++ b/OpenRA.FileFormats/Graphics/TmpRAReader.cs
@@ -19,6 +19,7 @@ namespace OpenRA.FileFormats
{
readonly List tiles = new List();
public IEnumerable Frames { get { return tiles.Cast(); } }
+ public bool CacheWhenLoadingTileset { get { return false; } }
public TmpRAReader(Stream s)
{
diff --git a/OpenRA.FileFormats/Graphics/TmpTDReader.cs b/OpenRA.FileFormats/Graphics/TmpTDReader.cs
index 04f6e9faf8..fe73203966 100644
--- a/OpenRA.FileFormats/Graphics/TmpTDReader.cs
+++ b/OpenRA.FileFormats/Graphics/TmpTDReader.cs
@@ -38,6 +38,7 @@ namespace OpenRA.FileFormats
{
readonly List tiles = new List();
public IEnumerable Frames { get { return tiles.Cast(); } }
+ public bool CacheWhenLoadingTileset { get { return false; } }
public TmpTDReader(Stream s)
{
diff --git a/OpenRA.FileFormats/OpenRA.FileFormats.csproj b/OpenRA.FileFormats/OpenRA.FileFormats.csproj
index ada09fc867..e867a1dc03 100644
--- a/OpenRA.FileFormats/OpenRA.FileFormats.csproj
+++ b/OpenRA.FileFormats/OpenRA.FileFormats.csproj
@@ -141,7 +141,6 @@
-
diff --git a/OpenRA.Game/Graphics/Theater.cs b/OpenRA.Game/Graphics/Theater.cs
index 078e4c1883..fdbb5908b8 100644
--- a/OpenRA.Game/Graphics/Theater.cs
+++ b/OpenRA.Game/Graphics/Theater.cs
@@ -23,38 +23,31 @@ namespace OpenRA.Graphics
Dictionary templates;
Sprite missingTile;
- Sprite[] LoadTemplate(string filename, string[] exts, Cache r8Cache, int[] frames)
+ Sprite[] LoadTemplate(string filename, string[] exts, Dictionary sourceCache, int[] frames)
{
- if (exts.Contains(".R8") && FileSystem.Exists(filename+".R8"))
+ ISpriteSource source;
+ if (!sourceCache.ContainsKey(filename))
{
- return frames.Select(f =>
- {
- if (f < 0)
- return null;
+ using (var s = FileSystem.OpenWithExts(filename, exts))
+ source = SpriteSource.LoadSpriteSource(s, filename);
- var image = r8Cache[filename][f];
- return sheetBuilder.Add(image.Data, new Size(image.Size.Width, image.Size.Height));
- }).ToArray();
+ if (source.CacheWhenLoadingTileset)
+ sourceCache.Add(filename, source);
+ }
+ else
+ source = sourceCache[filename];
+
+ if (frames != null)
+ {
+ var ret = new List();
+ var srcFrames = source.Frames.ToArray();
+ foreach (var i in frames)
+ ret.Add(sheetBuilder.Add(srcFrames[i]));
+
+ return ret.ToArray();
}
- using (var s = FileSystem.OpenWithExts(filename, exts))
- {
- var type = SpriteSource.DetectSpriteType(s);
- ISpriteSource source;
- switch (type)
- {
- case SpriteType.TmpTD:
- source = new TmpTDReader(s);
- break;
- case SpriteType.TmpRA:
- source = new TmpRAReader(s);
- break;
- default:
- throw new InvalidDataException(filename + " is not a valid terrain tile");
- }
-
- return source.Frames.Select(f => sheetBuilder.Add(f)).ToArray();
- }
+ return source.Frames.Select(f => sheetBuilder.Add(f)).ToArray();
}
public Theater(TileSet tileset)
@@ -69,11 +62,11 @@ namespace OpenRA.Graphics
return new Sheet(new Size(tileset.SheetSize, tileset.SheetSize));
};
- var r8Cache = new Cache(s => new R8Reader(FileSystem.OpenWithExts(s, ".R8")).Frames.ToArray());
+ var sourceCache = new Dictionary();
templates = new Dictionary();
sheetBuilder = new SheetBuilder(SheetType.Indexed, allocate);
foreach (var t in tileset.Templates)
- templates.Add(t.Value.Id, LoadTemplate(t.Value.Image, tileset.Extensions, r8Cache, t.Value.Frames));
+ templates.Add(t.Value.Id, LoadTemplate(t.Value.Image, tileset.Extensions, sourceCache, t.Value.Frames));
// 1x1px transparent tile
missingTile = sheetBuilder.Add(new byte[1], new Size(1, 1));