2018-09-12 06:32:05 +00:00
|
|
|
|
using Microsoft.Xna.Framework.Content;
|
2018-09-13 02:39:21 +00:00
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2018-09-12 06:32:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2018-09-12 21:34:44 +00:00
|
|
|
|
using System.Threading;
|
2018-09-12 06:32:05 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
|
|
|
|
|
{
|
2018-09-15 18:15:32 +00:00
|
|
|
|
public class ContentSystem
|
2018-09-12 06:32:05 +00:00
|
|
|
|
{
|
2018-09-13 02:39:21 +00:00
|
|
|
|
volatile bool queued;
|
2018-09-12 21:34:44 +00:00
|
|
|
|
Thread thread;
|
|
|
|
|
readonly ContentManager contentManager;
|
|
|
|
|
readonly Queue<ContentLoad> queue;
|
|
|
|
|
Dictionary<string, IDisposable> assets;
|
2018-09-12 06:32:05 +00:00
|
|
|
|
readonly Dictionary<Type, IContentResolver> contentResolver;
|
|
|
|
|
|
|
|
|
|
public ContentSystem(ContentManager contentManager)
|
|
|
|
|
{
|
|
|
|
|
this.contentManager = contentManager;
|
|
|
|
|
assets = new Dictionary<string, IDisposable>();
|
2018-09-12 21:34:44 +00:00
|
|
|
|
queue = new Queue<ContentLoad>();
|
2018-09-12 06:32:05 +00:00
|
|
|
|
contentResolver = new Dictionary<Type, IContentResolver>();
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 21:34:44 +00:00
|
|
|
|
private void Load(string assetName, Type type)
|
2018-09-12 06:32:05 +00:00
|
|
|
|
{
|
2018-09-12 21:34:44 +00:00
|
|
|
|
IContentResolver handler = contentResolver[type];
|
|
|
|
|
string path = handler.Load(assetName);
|
|
|
|
|
assets.Add(assetName, contentManager.Load<IDisposable>(path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadBatch()
|
|
|
|
|
{
|
|
|
|
|
while (queue.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
lock (queue)
|
|
|
|
|
{
|
|
|
|
|
ContentLoad content = queue.Dequeue();
|
|
|
|
|
Load(content.assetName, content.type);
|
|
|
|
|
}
|
2018-09-13 02:39:21 +00:00
|
|
|
|
queued = false;
|
2018-09-12 21:34:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 18:15:32 +00:00
|
|
|
|
public T Get<T>(string assetName)
|
2018-09-12 21:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
lock(queue)
|
|
|
|
|
{
|
|
|
|
|
return (T)assets[assetName];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 18:15:32 +00:00
|
|
|
|
public void Queue(string assetName, Type type)
|
2018-09-12 21:34:44 +00:00
|
|
|
|
{
|
2018-09-13 02:39:21 +00:00
|
|
|
|
queued = true;
|
2018-09-12 21:34:44 +00:00
|
|
|
|
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)
|
2018-09-12 06:32:05 +00:00
|
|
|
|
{
|
2018-09-12 21:34:44 +00:00
|
|
|
|
if (thread == null || !thread.IsAlive)
|
|
|
|
|
{
|
|
|
|
|
ThreadStart threadStart = new ThreadStart(LoadBatch);
|
|
|
|
|
thread = new Thread(threadStart);
|
|
|
|
|
}
|
2018-09-12 06:32:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 21:34:44 +00:00
|
|
|
|
void UnloadAll()
|
2018-09-12 06:32:05 +00:00
|
|
|
|
{
|
2018-09-12 21:34:44 +00:00
|
|
|
|
foreach (KeyValuePair<string, IDisposable> asset in assets)
|
|
|
|
|
{
|
|
|
|
|
asset.Value.Dispose();
|
|
|
|
|
assets.Remove(asset.Key);
|
|
|
|
|
}
|
2018-09-12 06:32:05 +00:00
|
|
|
|
}
|
2018-09-13 02:39:21 +00:00
|
|
|
|
|
|
|
|
|
bool Done()
|
|
|
|
|
{
|
|
|
|
|
return queued;
|
|
|
|
|
}
|
2018-09-12 06:32:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|