Asset loading structure complete.

Loads information from loader.
information creates useable.
Minor folder restructuring.
Untested.
This commit is contained in:
Harrison Deng 2020-04-17 22:09:13 -05:00
parent 9c19b21ffb
commit d50ede989c
8 changed files with 123 additions and 10 deletions

View File

@ -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<T>(string path) {
throw new System.NotImplementedException();
//TODO Implement a method of loading.
private readonly Dictionary<Type, ILoader> typeLoaders;
public AssetLoader() {
typeLoaders = new Dictionary<Type, ILoader>();
}
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);
}
}
}

View File

@ -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<ContentData> queue;
Dictionary<string, IDisposable> assets;
/// <summary>
@ -45,6 +45,7 @@ namespace RecrownedGTK.AssetsSystem
queue = new Queue<ContentData>();
contentPathModifier = new Dictionary<Type, IAssetPathResolver>();
}
/// <summary>
/// Adds a <see cref="IContentPathResolver"/> to this handler.
/// </summary>
@ -53,6 +54,7 @@ namespace RecrownedGTK.AssetsSystem
public void AddContentPathResolver(Type assetType, IAssetPathResolver contentResolver) {
contentPathModifier.Add(assetType, contentResolver);
}
/// <summary>
/// Removes the <see cref="IContentPathResolver"/> for the key.
/// </summary>
@ -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<IDisposable>(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,13 +126,16 @@ namespace RecrownedGTK.AssetsSystem
/// </summary>
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()
{
running = true;
@ -146,6 +153,7 @@ namespace RecrownedGTK.AssetsSystem
}
running = false;
}
/// <summary>
/// Removes the asset from the list of assets in the system.
/// Cannot remove from queue.

View File

@ -0,0 +1,9 @@
using System;
namespace RecrownedGTK.AssetsSystem.Information {
public interface IInfo
{
Type type {get;}
IDisposable CreateUseable();
}
}

View File

@ -1,4 +1,4 @@
namespace RecrownedGTK.Pipeline.Information {
namespace RecrownedGTK.AssetsSystem.Information {
public struct NinePatchInfo {
public int leftBound, rightBound, bottomBound, topBound;
public string name;

View File

@ -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;
}
}
}

View File

@ -1,6 +1,6 @@
using RecrownedGTK.Types;
namespace RecrownedGTK.Pipeline.Information {
namespace RecrownedGTK.AssetsSystem.Information {
public struct TextureMapInfo {
public string pathToMap;
public MapRegionInfo[] regionInfos;

View File

@ -0,0 +1,8 @@
using System;
using RecrownedGTK.AssetsSystem.Information;
namespace RecrownedGTK.AssetsSystem.Loaders {
public interface ILoader
{
IInfo load(string path);
}
}

View File

@ -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;
}
/// <summary>
///Load PNG data to this texture representation.
///
/// Decodes the PNG into a byte array to be stored.
/// </summary>
/// <param name="path">The path to the PNG file to be represented.</param>
/// <param name="width">Outputs the width of the PNG file.</param>
/// <param name="height">Outputs the height of the PNG file.</param>
/// <returns>A byte array representing the PNG.</returns>
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;
}
}
}