progress on automated asynchronous content management; ready for testing; began working on font generation system.

This commit is contained in:
2018-09-12 16:34:44 -05:00
parent 419fb6ab6b
commit e45d998c3a
10 changed files with 268 additions and 39 deletions

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
{
class ContentLoad
{
internal Type type;
internal string assetName;
public ContentLoad(string assetName, Type type)
{
this.type = type;
this.assetName = assetName;
}
}
}

View File

@@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
{
class ContentLoader
{
}
}

View File

@@ -3,38 +3,88 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
{
class ContentSystem
{
private readonly ContentManager contentManager;
private readonly Queue<Dictionary<Type, string>> queue;
private Dictionary<string, IDisposable> assets;
Thread thread;
internal volatile bool loading;
readonly ContentManager contentManager;
readonly Queue<ContentLoad> queue;
Dictionary<string, IDisposable> assets;
readonly Dictionary<Type, IContentResolver> contentResolver;
public ContentSystem(ContentManager contentManager)
{
this.contentManager = contentManager;
assets = new Dictionary<string, IDisposable>();
queue = new Queue<Dictionary<Type, string>>();
queue = new Queue<ContentLoad>();
contentResolver = new Dictionary<Type, IContentResolver>();
}
void Load(string assetName, Type type)
private void Load(string assetName, Type type)
{
if (assets.ContainsKey(assetName))
IContentResolver handler = contentResolver[type];
string path = handler.Load(assetName);
assets.Add(assetName, contentManager.Load<IDisposable>(path));
}
private void LoadBatch()
{
while (queue.Count != 0)
{
IContentResolver handler = contentResolver[type];
string path = handler.Load(assetName);
assets.Add(assetName, contentManager.Load<IDisposable>(path));
lock (queue)
{
ContentLoad content = queue.Dequeue();
Load(content.assetName, content.type);
}
}
}
T get<T> (string assetName)
T get<T>(string assetName)
{
return (T)assets[assetName];
lock(queue)
{
return (T)assets[assetName];
}
}
void Queue(string assetName, Type type)
{
lock (queue)
{
if (!assets.ContainsKey(assetName))
{
queue.Enqueue(new ContentLoad(assetName, type));
}
}
}
/// <summary>
/// Called whenever a batch of assets should be loaded from the queue. Safe to call once every frame.
/// </summary>
void Update()
{
if (queue.Count > 0)
{
if (thread == null || !thread.IsAlive)
{
ThreadStart threadStart = new ThreadStart(LoadBatch);
thread = new Thread(threadStart);
}
}
}
void UnloadAll()
{
foreach (KeyValuePair<string, IDisposable> asset in assets)
{
asset.Value.Dispose();
assets.Remove(asset.Key);
}
}
}
}