From 98869c2d38ecd9c1177649751ea597faea7640d2 Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Fri, 10 Jul 2020 18:04:56 -0500 Subject: [PATCH] Added default path modifier, fixed how it references assets. --- .../AssetSystem/AssetManager.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/SlatedGameToolkit.Framework/AssetSystem/AssetManager.cs b/src/SlatedGameToolkit.Framework/AssetSystem/AssetManager.cs index dd30ce2..ede5e84 100644 --- a/src/SlatedGameToolkit.Framework/AssetSystem/AssetManager.cs +++ b/src/SlatedGameToolkit.Framework/AssetSystem/AssetManager.cs @@ -23,6 +23,11 @@ namespace SlatedGameToolkit.Framework.AssetSystem { /// A loaded, useable asset. public delegate T LoadAsset(string path, GLContext glContext) where T : IAssetUseable; public class AssetManager { + public IAssetUseable this[string key] { + get { + return this.assets[key]; + } + } private readonly object assetThreadLock = new object(); private Thread thread; private ConcurrentQueue<(string, GLContext)> loadables; @@ -41,6 +46,12 @@ namespace SlatedGameToolkit.Framework.AssetSystem { /// A dictionary of file extension associated with respective paths. public ConcurrentDictionary PathModifiers { get; private set; } + /// + /// If this value is not null, the asset manager will modify paths according to this delegate if their is not a more appropriate modifier. + /// + /// The delegate to use to modify the path. + public ModifyPath DefaultPathModifier {get; set;} + /// /// A dictionary containing associations between file extensions and their loaders. /// All file extensions will be requested in lower cases, and therefore, the extension key in this dictionary should also be all lowercase. @@ -91,12 +102,15 @@ namespace SlatedGameToolkit.Framework.AssetSystem { /// The OpenGL context to associate with the loaded asset if the loaded asset requires it. May be null, in which the currently active context will be associated with it. public void Load(string name, GLContext glContext = null) { if (glContext == null) glContext = WindowContextsManager.CurrentGL; - string ext = Path.GetExtension(name).ToLower(); + string original = name; + string ext = Path.GetExtension(name).ToLower().Substring(1); if (PathModifiers.ContainsKey(ext)) { name = PathModifiers[ext](name); + } else if (DefaultPathModifier != null) { + name = DefaultPathModifier(name); } if (!Loaders.ContainsKey(ext)) throw new FrameworkUsageException(string.Format("Failed to find associated loader for file \"{0}\" with extension \"{1}\".", name, ext)); - assets[name] = Loaders[ext](name, glContext); + assets[original] = Loaders[ext](name, glContext); } ///