diff --git a/OpenRA.Game/GameRules/RulesetCache.cs b/OpenRA.Game/GameRules/RulesetCache.cs index f61320f845..545fc01edc 100644 --- a/OpenRA.Game/GameRules/RulesetCache.cs +++ b/OpenRA.Game/GameRules/RulesetCache.cs @@ -18,7 +18,7 @@ using OpenRA.Support; namespace OpenRA { - public sealed class RulesetCache : IDisposable + public sealed class RulesetCache { static readonly List NoMapRules = new List(); @@ -30,7 +30,6 @@ namespace OpenRA readonly Dictionary notificationCache = new Dictionary(); readonly Dictionary musicCache = new Dictionary(); readonly Dictionary tileSetCache = new Dictionary(); - readonly Dictionary sequenceCaches = new Dictionary(); public event EventHandler LoadingProgress; void RaiseProgress() @@ -85,9 +84,10 @@ namespace OpenRA k => new MusicInfo(k.Key, k.Value)); using (new PerfTimer("TileSets")) - tileSets = LoadTileSets(fileSystem, tileSetCache, sequenceCaches, m.TileSets); + tileSets = LoadTileSets(fileSystem, tileSetCache, m.TileSets); - var sequences = sequenceCaches.ToDictionary(kvp => kvp.Key, kvp => new SequenceProvider(fileSystem, kvp.Value, map)); + // TODO: only initialize, and then cache, the provider for the given map + var sequences = tileSets.ToDictionary(t => t.Key, t => new SequenceProvider(fileSystem, modData, t.Value, map)); return new Ruleset(actors, weapons, voices, notifications, music, tileSets, sequences); } @@ -122,8 +122,7 @@ namespace OpenRA return itemSet; } - Dictionary LoadTileSets(IReadOnlyFileSystem fileSystem, Dictionary itemCache, - Dictionary sequenceCaches, string[] files) + Dictionary LoadTileSets(IReadOnlyFileSystem fileSystem, Dictionary itemCache, string[] files) { var items = new Dictionary(); @@ -136,23 +135,11 @@ namespace OpenRA { t = new TileSet(fileSystem, file); itemCache.Add(file, t); - - // every time we load a tile set, we create a sequence cache for it - var sc = new SequenceCache(modData, fileSystem, t); - sequenceCaches.Add(t.Id, sc); - items.Add(t.Id, t); } } return items; } - - public void Dispose() - { - foreach (var cache in sequenceCaches.Values) - cache.Dispose(); - sequenceCaches.Clear(); - } } } diff --git a/OpenRA.Game/Graphics/SequenceProvider.cs b/OpenRA.Game/Graphics/SequenceProvider.cs index 21d62b9311..bb63316735 100644 --- a/OpenRA.Game/Graphics/SequenceProvider.cs +++ b/OpenRA.Game/Graphics/SequenceProvider.cs @@ -42,15 +42,22 @@ namespace OpenRA.Graphics IReadOnlyDictionary ParseSequences(ModData modData, TileSet tileSet, SpriteCache cache, MiniYamlNode node); } - public class SequenceProvider + public class SequenceProvider : IDisposable { + readonly ModData modData; + readonly TileSet tileSet; readonly Lazy sequences; - public readonly SpriteCache SpriteCache; + readonly Lazy spriteCache; + public SpriteCache SpriteCache { get { return spriteCache.Value; } } - public SequenceProvider(IReadOnlyFileSystem fileSystem, SequenceCache cache, Map map) + readonly Dictionary sequenceCache = new Dictionary(); + + public SequenceProvider(IReadOnlyFileSystem fileSystem, ModData modData, TileSet tileSet, Map map) { - sequences = Exts.Lazy(() => cache.LoadSequences(fileSystem, map)); - SpriteCache = cache.SpriteCache; + this.modData = modData; + this.tileSet = tileSet; + sequences = Exts.Lazy(() => LoadSequences(fileSystem, map)); + spriteCache = Exts.Lazy(() => new SpriteCache(fileSystem, modData.SpriteLoaders, new SheetBuilder(SheetType.Indexed))); } public ISpriteSequence GetSequence(string unitName, string sequenceName) @@ -89,33 +96,6 @@ namespace OpenRA.Graphics return unitSeq.Value.Keys; } - public void Preload() - { - SpriteCache.SheetBuilder.Current.CreateBuffer(); - foreach (var unitSeq in sequences.Value.Values) - foreach (var seq in unitSeq.Value.Values) { } - SpriteCache.SheetBuilder.Current.ReleaseBuffer(); - } - } - - public sealed class SequenceCache : IDisposable - { - readonly ModData modData; - readonly TileSet tileSet; - readonly Lazy spriteCache; - public SpriteCache SpriteCache { get { return spriteCache.Value; } } - - readonly Dictionary sequenceCache = new Dictionary(); - - public SequenceCache(ModData modData, IReadOnlyFileSystem fileSystem, TileSet tileSet) - { - this.modData = modData; - this.tileSet = tileSet; - - // Every time we load a tile set, we create a sequence cache for it - spriteCache = Exts.Lazy(() => new SpriteCache(fileSystem, modData.SpriteLoaders, new SheetBuilder(SheetType.Indexed))); - } - public Sequences LoadSequences(IReadOnlyFileSystem fileSystem, Map map) { using (new Support.PerfTimer("LoadSequences")) @@ -150,6 +130,14 @@ namespace OpenRA.Graphics return new ReadOnlyDictionary(items); } + public void Preload() + { + SpriteCache.SheetBuilder.Current.CreateBuffer(); + foreach (var unitSeq in sequences.Value.Values) + foreach (var seq in unitSeq.Value.Values) { } + SpriteCache.SheetBuilder.Current.ReleaseBuffer(); + } + public void Dispose() { if (spriteCache.IsValueCreated) diff --git a/OpenRA.Game/ModData.cs b/OpenRA.Game/ModData.cs index 6689c8b94e..59f57b9a61 100644 --- a/OpenRA.Game/ModData.cs +++ b/OpenRA.Game/ModData.cs @@ -192,7 +192,6 @@ namespace OpenRA { if (LoadScreen != null) LoadScreen.Dispose(); - RulesetCache.Dispose(); MapCache.Dispose(); if (VoxelLoader != null) VoxelLoader.Dispose();