From 3b74dae09f639b98404dd55832efdfd96dc60e90 Mon Sep 17 00:00:00 2001 From: morkt Date: Sat, 8 Apr 2017 18:38:23 +0400 Subject: [PATCH] (ResourceInstance): lazily initialized wrapper for formats instances. --- GameRes/FormatCatalog.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/GameRes/FormatCatalog.cs b/GameRes/FormatCatalog.cs index 6b674b74..b41bc017 100644 --- a/GameRes/FormatCatalog.cs +++ b/GameRes/FormatCatalog.cs @@ -32,6 +32,7 @@ using System.Linq; using GameRes.Collections; using System.Runtime.Serialization.Formatters.Binary; using GameRes.Compression; +using System.Threading; namespace GameRes { @@ -314,6 +315,32 @@ namespace GameRes } } + /// + /// Lazily initialized wrapper for resource instances. + /// + public class ResourceInstance where T : IResource + { + T m_format; + Func m_resolver; + + public ResourceInstance (string tag) + { + var t = typeof(T); + if (typeof(ImageFormat) == t || t.IsSubclassOf (typeof(ImageFormat))) + m_resolver = () => ImageFormat.FindByTag (tag) as T; + else if (typeof(ArchiveFormat) == t || t.IsSubclassOf (typeof(ArchiveFormat))) + m_resolver = () => FormatCatalog.Instance.ArcFormats.FirstOrDefault (f => f.Tag == tag) as T; + else if (typeof(AudioFormat) == t || t.IsSubclassOf (typeof(AudioFormat))) + m_resolver = () => FormatCatalog.Instance.AudioFormats.FirstOrDefault (f => f.Tag == tag) as T; + else if (typeof(ScriptFormat) == t || t. IsSubclassOf (typeof(ScriptFormat))) + m_resolver = () => FormatCatalog.Instance.ScriptFormats.FirstOrDefault (f => f.Tag == tag) as T; + else + throw new ApplicationException ("Invalid resource type specified for ResourceInstance"); + } + + public T Value { get { return LazyInitializer.EnsureInitialized (ref m_format, m_resolver); } } + } + [Serializable] public class SchemeDataBase {