diff --git a/EOLib.Graphics.Test/LibraryGraphicPairTest.cs b/EOLib.Graphics.Test/LibraryGraphicPairTest.cs deleted file mode 100644 index 10214ea66..000000000 --- a/EOLib.Graphics.Test/LibraryGraphicPairTest.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using NUnit.Framework; - -namespace EOLib.Graphics.Test -{ - [TestFixture, ExcludeFromCodeCoverage] - public class LibraryGraphicPairTest - { - [Test] - public void Equals_ReturnsFalse_WhenOtherObjectIsNotLibraryGraphicPair() - { - var pair = new LibraryGraphicPair(1, 1); - Assert.IsFalse(pair.Equals(new object())); - } - - [Test] - public void CompareTo_ReturnsNeg1_WhenOtherObjectIsNotLibraryGraphicPair() - { - var pair = new LibraryGraphicPair(1, 1); - Assert.AreEqual(-1, pair.CompareTo(new object())); - } - - [Test] - public void CompareTo_ReturnsNeg1_WhenOtherObjectHasDifferentLibraryNumber() - { - var pair = new LibraryGraphicPair(1, 1); - var other = new LibraryGraphicPair(2, 1); - - Assert.AreEqual(-1, pair.CompareTo(other)); - } - - [Test] - public void CompareTo_ReturnsNeg1_WhenOtherObjectHasDifferentGFXNumber() - { - var pair = new LibraryGraphicPair(1, 1); - var other = new LibraryGraphicPair(1, 2); - - Assert.AreEqual(-1, pair.CompareTo(other)); - } - - [Test] - public void CompareTo_Returns0_WhenOtherObjectHasSameValues() - { - var pair = new LibraryGraphicPair(1, 1); - var other = new LibraryGraphicPair(1, 1); - - Assert.AreEqual(0, pair.CompareTo(other)); - } - } -} diff --git a/EOLib.Graphics/LibraryGraphicPair.cs b/EOLib.Graphics/LibraryGraphicPair.cs deleted file mode 100644 index e4a3d3555..000000000 --- a/EOLib.Graphics/LibraryGraphicPair.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; - -namespace EOLib.Graphics -{ - public struct LibraryGraphicPair : IComparable - { - private readonly int LibraryNumber; - private readonly int GraphicNumber; - - public LibraryGraphicPair(int lib, int gfx) - { - LibraryNumber = lib; - GraphicNumber = gfx; - } - - public int CompareTo(object other) - { - if (!(other is LibraryGraphicPair)) - return -1; - - LibraryGraphicPair rhs = (LibraryGraphicPair)other; - - if (rhs.LibraryNumber == LibraryNumber && rhs.GraphicNumber == GraphicNumber) - return 0; - - return -1; - } - - public override bool Equals(object obj) - { - if (!(obj is LibraryGraphicPair)) return false; - LibraryGraphicPair other = (LibraryGraphicPair)obj; - return other.GraphicNumber == GraphicNumber && other.LibraryNumber == LibraryNumber; - } - - public override int GetHashCode() - { - return (LibraryNumber << 16) | GraphicNumber; - } - } -} diff --git a/EOLib.Graphics/NativeGraphicsManager.cs b/EOLib.Graphics/NativeGraphicsManager.cs index d2383ef99..de92be5de 100644 --- a/EOLib.Graphics/NativeGraphicsManager.cs +++ b/EOLib.Graphics/NativeGraphicsManager.cs @@ -6,21 +6,23 @@ using SixLabors.ImageSharp.Processing; using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.IO; +using System.Linq; namespace EOLib.Graphics { [MappedType(BaseType = typeof(INativeGraphicsManager), IsSingleton = true)] public sealed class NativeGraphicsManager : INativeGraphicsManager { - private readonly ConcurrentDictionary _cache; + private readonly ConcurrentDictionary> _cache; private readonly INativeGraphicsLoader _gfxLoader; private readonly IGraphicsDeviceProvider _graphicsDeviceProvider; public NativeGraphicsManager(INativeGraphicsLoader gfxLoader, IGraphicsDeviceProvider graphicsDeviceProvider) { - _cache = new ConcurrentDictionary(); + _cache = new ConcurrentDictionary>(); _gfxLoader = gfxLoader; _graphicsDeviceProvider = graphicsDeviceProvider; } @@ -29,18 +31,16 @@ public Texture2D TextureFromResource(GFXTypes file, int resourceVal, bool transp { Texture2D ret; - var key = new LibraryGraphicPair((int)file, 100 + resourceVal); - - if (_cache.ContainsKey(key)) + if (_cache.ContainsKey(file) && _cache[file].ContainsKey(resourceVal)) { if (reloadFromFile) { - _cache[key]?.Dispose(); - _cache.TryRemove(key, out var _); + _cache[file][resourceVal]?.Dispose(); + _cache[file].Remove(resourceVal, out _); } else { - return _cache[key]; + return _cache[file][resourceVal]; } } @@ -61,7 +61,12 @@ public Texture2D TextureFromResource(GFXTypes file, int resourceVal, bool transp } } - _cache.TryAdd(key, ret); + if (_cache.ContainsKey(file) || + _cache.TryAdd(file, new ConcurrentDictionary())) + { + _cache[file].TryAdd(resourceVal, ret); + } + return ret; } @@ -96,8 +101,9 @@ private static void CrossPlatformMakeTransparent(Image bmp, Color transparentCol public void Dispose() { - foreach (var text in _cache.Values) + foreach (var text in _cache.SelectMany(x => x.Value.Values)) text.Dispose(); + _cache.Clear(); } }