From 3c9f589b5ec488d23e13bfd8831641d3f144d5ae Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Mon, 23 Jun 2014 22:03:45 +0100 Subject: [PATCH] Move try-catch during creation of sequences into the function rather than around the lazy initialization. This fixes an issue with VS breaking on the exception because it thinks it will go unhandled, even though it will be handled. --- OpenRA.Game/Graphics/SequenceProvider.cs | 63 ++++++++++++++---------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/OpenRA.Game/Graphics/SequenceProvider.cs b/OpenRA.Game/Graphics/SequenceProvider.cs index 085330b211..2f3947e137 100644 --- a/OpenRA.Game/Graphics/SequenceProvider.cs +++ b/OpenRA.Game/Graphics/SequenceProvider.cs @@ -15,9 +15,12 @@ using System.Linq; namespace OpenRA.Graphics { + using Sequences = IReadOnlyDictionary>>; + using UnitSequences = Lazy>; + public class SequenceProvider { - readonly Lazy>>> sequences; + readonly Lazy sequences; public readonly SpriteLoader SpriteLoader; public SequenceProvider(SequenceCache cache, Map map) @@ -28,7 +31,7 @@ namespace OpenRA.Graphics public Sequence GetSequence(string unitName, string sequenceName) { - Lazy> unitSeq; + UnitSequences unitSeq; if (!sequences.Value.TryGetValue(unitName, out unitSeq)) throw new InvalidOperationException("Unit `{0}` does not have any sequences defined.".F(unitName)); @@ -41,7 +44,7 @@ namespace OpenRA.Graphics public bool HasSequence(string unitName, string sequenceName) { - Lazy> unitSeq; + UnitSequences unitSeq; if (!sequences.Value.TryGetValue(unitName, out unitSeq)) throw new InvalidOperationException("Unit `{0}` does not have any sequences defined.".F(unitName)); @@ -50,7 +53,7 @@ namespace OpenRA.Graphics public IEnumerable Sequences(string unitName) { - Lazy> unitSeq; + UnitSequences unitSeq; if (!sequences.Value.TryGetValue(unitName, out unitSeq)) throw new InvalidOperationException("Unit `{0}` does not have any sequences defined.".F(unitName)); @@ -60,16 +63,7 @@ namespace OpenRA.Graphics public void Preload() { foreach (var unitSeq in sequences.Value.Values) - { - try - { - foreach (var seq in unitSeq.Value.Values); - } - catch (FileNotFoundException ex) - { - Log.Write("debug", ex.Message); - } - } + foreach (var seq in unitSeq.Value.Values) { } } } @@ -79,7 +73,7 @@ namespace OpenRA.Graphics readonly Lazy spriteLoader; public SpriteLoader SpriteLoader { get { return spriteLoader.Value; } } - readonly Dictionary>> sequenceCache = new Dictionary>>(); + readonly Dictionary sequenceCache = new Dictionary(); public SequenceCache(ModData modData, TileSet tileSet) { @@ -88,13 +82,13 @@ namespace OpenRA.Graphics spriteLoader = Exts.Lazy(() => new SpriteLoader(tileSet.Extensions, new SheetBuilder(SheetType.Indexed))); } - public IReadOnlyDictionary>> LoadSequences(Map map) + public Sequences LoadSequences(Map map) { using (new Support.PerfTimer("LoadSequences")) return Load(map.SequenceDefinitions); } - IReadOnlyDictionary>> Load(List sequenceNodes) + Sequences Load(List sequenceNodes) { var sequenceFiles = modData.Manifest.Sequences; @@ -102,7 +96,7 @@ namespace OpenRA.Graphics .Select(s => MiniYaml.FromFile(s)) .Aggregate(sequenceNodes, MiniYaml.MergeLiberal); - var items = new Dictionary>>(); + var items = new Dictionary(); foreach (var n in nodes) { // Work around the loop closure issue in older versions of C# @@ -110,23 +104,40 @@ namespace OpenRA.Graphics var key = node.Value.ToLines(node.Key).JoinWith("|"); - Lazy> t; + UnitSequences t; if (sequenceCache.TryGetValue(key, out t)) items.Add(node.Key, t); else { - t = Exts.Lazy(() => (IReadOnlyDictionary)new ReadOnlyDictionary( - node.Value.ToDictionary().ToDictionary(x => x.Key, x => - { - using (new Support.PerfTimer("new Sequence(\"{0}\")".F(node.Key), 20)) - return new Sequence(spriteLoader.Value, node.Key, x.Key, x.Value); - }))); + t = Exts.Lazy(() => CreateUnitSequences(node)); sequenceCache.Add(key, t); items.Add(node.Key, t); } } - return new ReadOnlyDictionary>>(items); + return new ReadOnlyDictionary(items); + } + + IReadOnlyDictionary CreateUnitSequences(MiniYamlNode node) + { + var unitSequences = new Dictionary(); + + foreach (var kvp in node.Value.ToDictionary()) + { + using (new Support.PerfTimer("new Sequence(\"{0}\")".F(node.Key), 20)) + { + try + { + unitSequences.Add(kvp.Key, new Sequence(spriteLoader.Value, node.Key, kvp.Key, kvp.Value)); + } + catch (FileNotFoundException ex) + { + Log.Write("debug", ex.Message); + } + } + } + + return new ReadOnlyDictionary(unitSequences); } } }