Added default path modifier, fixed how it references assets.

This commit is contained in:
Harrison Deng 2020-07-10 18:04:56 -05:00
parent 68c506670b
commit 98869c2d38

View File

@ -23,6 +23,11 @@ namespace SlatedGameToolkit.Framework.AssetSystem {
/// <returns>A loaded, useable asset.</returns>
public delegate T LoadAsset<T>(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 {
/// <value>A dictionary of file extension associated with respective paths.</value>
public ConcurrentDictionary<string, ModifyPath> PathModifiers { get; private set; }
/// <summary>
/// If this value is not null, the asset manager will modify paths according to this delegate if their is not a more appropriate modifier.
/// </summary>
/// <value>The delegate to use to modify the path.</value>
public ModifyPath DefaultPathModifier {get; set;}
/// <summary>
/// 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 {
/// <param name="glContext">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.</param>
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);
}
/// <summary>