From d50ede989c5c67fc05ae14954b57d0e660dca3bb Mon Sep 17 00:00:00 2001 From: Harrison Date: Fri, 17 Apr 2020 22:09:13 -0500 Subject: [PATCH] Asset loading structure complete. Loads information from loader. information creates useable. Minor folder restructuring. Untested. --- RecrownedGTK/AssetsSystem/AssetLoader.cs | 29 +++++++++++-- RecrownedGTK/AssetsSystem/AssetManager.cs | 18 +++++--- .../AssetsSystem/Information/IInfo.cs | 9 ++++ .../Information/NinePatchInfo.cs | 2 +- .../AssetsSystem/Information/TextureInfo.cs | 24 +++++++++++ .../Information/TextureMapInfo.cs | 2 +- RecrownedGTK/AssetsSystem/Loaders/ILoader.cs | 8 ++++ .../AssetsSystem/Loaders/TextureLoader.cs | 41 +++++++++++++++++++ 8 files changed, 123 insertions(+), 10 deletions(-) create mode 100644 RecrownedGTK/AssetsSystem/Information/IInfo.cs rename RecrownedGTK/{Pipeline => AssetsSystem}/Information/NinePatchInfo.cs (89%) create mode 100644 RecrownedGTK/AssetsSystem/Information/TextureInfo.cs rename RecrownedGTK/{Pipeline => AssetsSystem}/Information/TextureMapInfo.cs (95%) create mode 100644 RecrownedGTK/AssetsSystem/Loaders/ILoader.cs create mode 100644 RecrownedGTK/AssetsSystem/Loaders/TextureLoader.cs diff --git a/RecrownedGTK/AssetsSystem/AssetLoader.cs b/RecrownedGTK/AssetsSystem/AssetLoader.cs index b14e620..491c7fc 100644 --- a/RecrownedGTK/AssetsSystem/AssetLoader.cs +++ b/RecrownedGTK/AssetsSystem/AssetLoader.cs @@ -1,8 +1,31 @@ +using System; +using System.Collections.Generic; +using RecrownedGTK.AssetsSystem.Information; +using RecrownedGTK.AssetsSystem.Loaders; + namespace RecrownedGTK.AssetsSystem { public class AssetLoader { - public T Load(string path) { - throw new System.NotImplementedException(); - //TODO Implement a method of loading. + private readonly Dictionary typeLoaders; + public AssetLoader() { + typeLoaders = new Dictionary(); + } + public void AddLoaderResolver(Type type, ILoader loader) { + if (typeLoaders.ContainsKey(type)) { + throw new InvalidOperationException(String.Format("The type {0} already exists in this resolver.", type)); + } + typeLoaders.Add(type, loader); + } + public void RemoveLoaderResolver(Type type) { + if (!typeLoaders.ContainsKey(type)) { + throw new InvalidOperationException(String.Format("The type {0} doesn't exist in this resolver.", type)); + } + typeLoaders.Remove(type); + } + public IInfo Load(string path, Type type) { + if (!typeLoaders.ContainsKey(type)) { + throw new InvalidOperationException(String.Format("The type {0} doesn't exist in this resolver.", type)); + } + return typeLoaders[type].load(path); } } } \ No newline at end of file diff --git a/RecrownedGTK/AssetsSystem/AssetManager.cs b/RecrownedGTK/AssetsSystem/AssetManager.cs index 831cc04..45b7bca 100644 --- a/RecrownedGTK/AssetsSystem/AssetManager.cs +++ b/RecrownedGTK/AssetsSystem/AssetManager.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Threading; +using RecrownedGTK.AssetsSystem.Information; namespace RecrownedGTK.AssetsSystem { @@ -11,7 +12,6 @@ namespace RecrownedGTK.AssetsSystem public class AssetManager { private AssetLoader assetLoader; - Thread thread; readonly Queue queue; Dictionary assets; /// @@ -45,6 +45,7 @@ namespace RecrownedGTK.AssetsSystem queue = new Queue(); contentPathModifier = new Dictionary(); } + /// /// Adds a to this handler. /// @@ -53,6 +54,7 @@ namespace RecrownedGTK.AssetsSystem public void AddContentPathResolver(Type assetType, IAssetPathResolver contentResolver) { contentPathModifier.Add(assetType, contentResolver); } + /// /// Removes the for the key. /// @@ -60,6 +62,7 @@ namespace RecrownedGTK.AssetsSystem public void RemoveContentResolver(Type assetType) { contentPathModifier.Remove(assetType); } + private void Load(string assetName, Type type, bool usePathModifier) { Debug.WriteLine("Loading asset: " + assetName); @@ -77,7 +80,7 @@ namespace RecrownedGTK.AssetsSystem } path = handler.Modify(assetName); } - assets.Add(assetName, assetLoader.Load(path)); + assets.Add(assetName, assetLoader.Load(path, type).CreateUseable()); } @@ -109,6 +112,7 @@ namespace RecrownedGTK.AssetsSystem { queue.Enqueue(new ContentData(assetName, typeof(T), usePathModifier)); Debug.WriteLine("Queued asset: " + assetName); + Update(); } else { @@ -122,12 +126,15 @@ namespace RecrownedGTK.AssetsSystem /// public void Update() { - if (queue.Count > 0 && (thread == null || !thread.IsAlive)) + if (queue.Count > 0 && !running) { - thread = new Thread(LoadBatch); - thread.Start(); + ThreadPool.QueueUserWorkItem(LoadBatchCallback); } } + + private void LoadBatchCallback(object info) { + LoadBatch(); + } private void LoadBatch() { @@ -146,6 +153,7 @@ namespace RecrownedGTK.AssetsSystem } running = false; } + /// /// Removes the asset from the list of assets in the system. /// Cannot remove from queue. diff --git a/RecrownedGTK/AssetsSystem/Information/IInfo.cs b/RecrownedGTK/AssetsSystem/Information/IInfo.cs new file mode 100644 index 0000000..31d20fc --- /dev/null +++ b/RecrownedGTK/AssetsSystem/Information/IInfo.cs @@ -0,0 +1,9 @@ +using System; + +namespace RecrownedGTK.AssetsSystem.Information { + public interface IInfo + { + Type type {get;} + IDisposable CreateUseable(); + } +} \ No newline at end of file diff --git a/RecrownedGTK/Pipeline/Information/NinePatchInfo.cs b/RecrownedGTK/AssetsSystem/Information/NinePatchInfo.cs similarity index 89% rename from RecrownedGTK/Pipeline/Information/NinePatchInfo.cs rename to RecrownedGTK/AssetsSystem/Information/NinePatchInfo.cs index 6922dc9..d0b7dd4 100644 --- a/RecrownedGTK/Pipeline/Information/NinePatchInfo.cs +++ b/RecrownedGTK/AssetsSystem/Information/NinePatchInfo.cs @@ -1,4 +1,4 @@ -namespace RecrownedGTK.Pipeline.Information { +namespace RecrownedGTK.AssetsSystem.Information { public struct NinePatchInfo { public int leftBound, rightBound, bottomBound, topBound; public string name; diff --git a/RecrownedGTK/AssetsSystem/Information/TextureInfo.cs b/RecrownedGTK/AssetsSystem/Information/TextureInfo.cs new file mode 100644 index 0000000..0eeaffa --- /dev/null +++ b/RecrownedGTK/AssetsSystem/Information/TextureInfo.cs @@ -0,0 +1,24 @@ +using System; +using RecrownedGTK.Graphics; + +namespace RecrownedGTK.AssetsSystem.Information { + public struct TextureInfo : IInfo { + public string name; + public byte[] textureData; + int width, height; + public Type type => typeof(TextureData); + public TextureInfo(string name, int width, int height, byte[] textureData) { + this.name = name; + this.width = width; + this.height = height; + this.textureData = textureData; + } + + + public IDisposable CreateUseable() + { + TextureData data = new TextureData(textureData, width, height); + return data; + } + } +} \ No newline at end of file diff --git a/RecrownedGTK/Pipeline/Information/TextureMapInfo.cs b/RecrownedGTK/AssetsSystem/Information/TextureMapInfo.cs similarity index 95% rename from RecrownedGTK/Pipeline/Information/TextureMapInfo.cs rename to RecrownedGTK/AssetsSystem/Information/TextureMapInfo.cs index 9258ad8..cf1fffa 100644 --- a/RecrownedGTK/Pipeline/Information/TextureMapInfo.cs +++ b/RecrownedGTK/AssetsSystem/Information/TextureMapInfo.cs @@ -1,6 +1,6 @@ using RecrownedGTK.Types; -namespace RecrownedGTK.Pipeline.Information { +namespace RecrownedGTK.AssetsSystem.Information { public struct TextureMapInfo { public string pathToMap; public MapRegionInfo[] regionInfos; diff --git a/RecrownedGTK/AssetsSystem/Loaders/ILoader.cs b/RecrownedGTK/AssetsSystem/Loaders/ILoader.cs new file mode 100644 index 0000000..a22cd01 --- /dev/null +++ b/RecrownedGTK/AssetsSystem/Loaders/ILoader.cs @@ -0,0 +1,8 @@ +using System; +using RecrownedGTK.AssetsSystem.Information; +namespace RecrownedGTK.AssetsSystem.Loaders { + public interface ILoader + { + IInfo load(string path); + } +} \ No newline at end of file diff --git a/RecrownedGTK/AssetsSystem/Loaders/TextureLoader.cs b/RecrownedGTK/AssetsSystem/Loaders/TextureLoader.cs new file mode 100644 index 0000000..cecd18e --- /dev/null +++ b/RecrownedGTK/AssetsSystem/Loaders/TextureLoader.cs @@ -0,0 +1,41 @@ +using System; +using System.Drawing; +using System.IO; +using RecrownedGTK.AssetsSystem.Information; +using RecrownedGTK.Graphics; + +namespace RecrownedGTK.AssetsSystem.Loaders { + public class TextureLoader : ILoader + { + public IInfo load(string path) + { + int width; + int height; + byte[] textureData = LoadPNG(path, out width, out height); + TextureInfo info = new TextureInfo(Path.GetFileName(path), width, height, textureData); + return info; + } + + /// + ///Load PNG data to this texture representation. + /// + /// Decodes the PNG into a byte array to be stored. + /// + /// The path to the PNG file to be represented. + /// Outputs the width of the PNG file. + /// Outputs the height of the PNG file. + /// A byte array representing the PNG. + private byte[] LoadPNG(string path, out int width, out int height) { + byte[] textureData = null; + using (FileStream file = new FileStream(path, FileMode.Open)) { + using (Bitmap bitmap = new Bitmap(file)) { + ImageConverter converter = new ImageConverter(); + textureData = (byte[]) converter.ConvertTo(bitmap, typeof(byte[])); + width = bitmap.Width; + height = bitmap.Height; + } + } + return textureData; + } + } +} \ No newline at end of file