Asset loading structure complete.
Loads information from loader. information creates useable. Minor folder restructuring. Untested.
This commit is contained in:
parent
9c19b21ffb
commit
d50ede989c
@ -1,8 +1,31 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using RecrownedGTK.AssetsSystem.Information;
|
||||||
|
using RecrownedGTK.AssetsSystem.Loaders;
|
||||||
|
|
||||||
namespace RecrownedGTK.AssetsSystem {
|
namespace RecrownedGTK.AssetsSystem {
|
||||||
public class AssetLoader {
|
public class AssetLoader {
|
||||||
public T Load<T>(string path) {
|
private readonly Dictionary<Type, ILoader> typeLoaders;
|
||||||
throw new System.NotImplementedException();
|
public AssetLoader() {
|
||||||
//TODO Implement a method of loading.
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,6 +2,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using RecrownedGTK.AssetsSystem.Information;
|
||||||
|
|
||||||
namespace RecrownedGTK.AssetsSystem
|
namespace RecrownedGTK.AssetsSystem
|
||||||
{
|
{
|
||||||
@ -11,7 +12,6 @@ namespace RecrownedGTK.AssetsSystem
|
|||||||
public class AssetManager
|
public class AssetManager
|
||||||
{
|
{
|
||||||
private AssetLoader assetLoader;
|
private AssetLoader assetLoader;
|
||||||
Thread thread;
|
|
||||||
readonly Queue<ContentData> queue;
|
readonly Queue<ContentData> queue;
|
||||||
Dictionary<string, IDisposable> assets;
|
Dictionary<string, IDisposable> assets;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -45,6 +45,7 @@ namespace RecrownedGTK.AssetsSystem
|
|||||||
queue = new Queue<ContentData>();
|
queue = new Queue<ContentData>();
|
||||||
contentPathModifier = new Dictionary<Type, IAssetPathResolver>();
|
contentPathModifier = new Dictionary<Type, IAssetPathResolver>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds a <see cref="IContentPathResolver"/> to this handler.
|
/// Adds a <see cref="IContentPathResolver"/> to this handler.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -53,6 +54,7 @@ namespace RecrownedGTK.AssetsSystem
|
|||||||
public void AddContentPathResolver(Type assetType, IAssetPathResolver contentResolver) {
|
public void AddContentPathResolver(Type assetType, IAssetPathResolver contentResolver) {
|
||||||
contentPathModifier.Add(assetType, contentResolver);
|
contentPathModifier.Add(assetType, contentResolver);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes the <see cref="IContentPathResolver"/> for the key.
|
/// Removes the <see cref="IContentPathResolver"/> for the key.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -60,6 +62,7 @@ namespace RecrownedGTK.AssetsSystem
|
|||||||
public void RemoveContentResolver(Type assetType) {
|
public void RemoveContentResolver(Type assetType) {
|
||||||
contentPathModifier.Remove(assetType);
|
contentPathModifier.Remove(assetType);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Load(string assetName, Type type, bool usePathModifier)
|
private void Load(string assetName, Type type, bool usePathModifier)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("Loading asset: " + assetName);
|
Debug.WriteLine("Loading asset: " + assetName);
|
||||||
@ -77,7 +80,7 @@ namespace RecrownedGTK.AssetsSystem
|
|||||||
}
|
}
|
||||||
path = handler.Modify(assetName);
|
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));
|
queue.Enqueue(new ContentData(assetName, typeof(T), usePathModifier));
|
||||||
Debug.WriteLine("Queued asset: " + assetName);
|
Debug.WriteLine("Queued asset: " + assetName);
|
||||||
|
Update();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -122,12 +126,15 @@ namespace RecrownedGTK.AssetsSystem
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void Update()
|
public void Update()
|
||||||
{
|
{
|
||||||
if (queue.Count > 0 && (thread == null || !thread.IsAlive))
|
if (queue.Count > 0 && !running)
|
||||||
{
|
{
|
||||||
thread = new Thread(LoadBatch);
|
ThreadPool.QueueUserWorkItem(LoadBatchCallback);
|
||||||
thread.Start();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void LoadBatchCallback(object info) {
|
||||||
|
LoadBatch();
|
||||||
|
}
|
||||||
|
|
||||||
private void LoadBatch()
|
private void LoadBatch()
|
||||||
{
|
{
|
||||||
@ -146,6 +153,7 @@ namespace RecrownedGTK.AssetsSystem
|
|||||||
}
|
}
|
||||||
running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes the asset from the list of assets in the system.
|
/// Removes the asset from the list of assets in the system.
|
||||||
/// Cannot remove from queue.
|
/// Cannot remove from queue.
|
||||||
|
9
RecrownedGTK/AssetsSystem/Information/IInfo.cs
Normal file
9
RecrownedGTK/AssetsSystem/Information/IInfo.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace RecrownedGTK.AssetsSystem.Information {
|
||||||
|
public interface IInfo
|
||||||
|
{
|
||||||
|
Type type {get;}
|
||||||
|
IDisposable CreateUseable();
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
namespace RecrownedGTK.Pipeline.Information {
|
namespace RecrownedGTK.AssetsSystem.Information {
|
||||||
public struct NinePatchInfo {
|
public struct NinePatchInfo {
|
||||||
public int leftBound, rightBound, bottomBound, topBound;
|
public int leftBound, rightBound, bottomBound, topBound;
|
||||||
public string name;
|
public string name;
|
24
RecrownedGTK/AssetsSystem/Information/TextureInfo.cs
Normal file
24
RecrownedGTK/AssetsSystem/Information/TextureInfo.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
using RecrownedGTK.Types;
|
using RecrownedGTK.Types;
|
||||||
|
|
||||||
namespace RecrownedGTK.Pipeline.Information {
|
namespace RecrownedGTK.AssetsSystem.Information {
|
||||||
public struct TextureMapInfo {
|
public struct TextureMapInfo {
|
||||||
public string pathToMap;
|
public string pathToMap;
|
||||||
public MapRegionInfo[] regionInfos;
|
public MapRegionInfo[] regionInfos;
|
8
RecrownedGTK/AssetsSystem/Loaders/ILoader.cs
Normal file
8
RecrownedGTK/AssetsSystem/Loaders/ILoader.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
using System;
|
||||||
|
using RecrownedGTK.AssetsSystem.Information;
|
||||||
|
namespace RecrownedGTK.AssetsSystem.Loaders {
|
||||||
|
public interface ILoader
|
||||||
|
{
|
||||||
|
IInfo load(string path);
|
||||||
|
}
|
||||||
|
}
|
41
RecrownedGTK/AssetsSystem/Loaders/TextureLoader.cs
Normal file
41
RecrownedGTK/AssetsSystem/Loaders/TextureLoader.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user