rhythmbullet/RhythmBullet/Zer01HD/Utilities/ContentSystem/ContentSystem.cs

164 lines
4.8 KiB
C#
Raw Normal View History

2018-09-12 06:32:05 +00:00
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
2018-09-12 06:32:05 +00:00
using System;
using System.Collections.Generic;
2018-10-30 23:29:54 +00:00
using System.Diagnostics;
using System.IO;
2018-09-12 06:32:05 +00:00
using System.Linq;
using System.Text;
using System.Threading;
2018-09-12 06:32:05 +00:00
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
{
public delegate void ContentSystemUpdate(String fileName, float completed);
2018-11-01 04:57:36 +00:00
public class ContentSystem
2018-09-12 06:32:05 +00:00
{
public event ContentSystemUpdate ContentSystemListeners;
Thread thread;
readonly ContentManager contentManager;
2018-10-29 05:33:26 +00:00
readonly Queue<LoadableContent> queue;
Dictionary<string, IDisposable> assets;
2018-10-30 23:29:54 +00:00
public readonly Dictionary<Type, IContentPathModifier> contentPathModifier;
2018-11-01 05:14:31 +00:00
volatile float progress;
2018-09-12 06:32:05 +00:00
public ContentSystem(ContentManager contentManager)
{
this.contentManager = contentManager;
assets = new Dictionary<string, IDisposable>();
2018-10-29 05:33:26 +00:00
queue = new Queue<LoadableContent>();
2018-10-30 23:29:54 +00:00
contentPathModifier = new Dictionary<Type, IContentPathModifier>();
2018-09-12 06:32:05 +00:00
}
private void Load(string assetName, Type type, bool usePathModifier)
2018-09-12 06:32:05 +00:00
{
string path = assetName;
if (usePathModifier)
{
IContentPathModifier handler = contentPathModifier[type];
path = handler.Modify(assetName);
}
assets.Add(assetName, contentManager.Load<IDisposable>(path));
2018-10-30 23:29:54 +00:00
Debug.WriteLine("Loaded asset: " + assetName);
}
public T Get<T>(string assetName)
{
2018-10-30 23:29:54 +00:00
lock (queue)
{
return (T)assets[assetName];
}
}
public void Queue<T>(string assetName, bool usePathModifier = true) where T : IDisposable
{
lock (queue)
{
if (!assets.ContainsKey(assetName))
{
queue.Enqueue(new LoadableContent(assetName, typeof(T), usePathModifier));
2018-10-30 23:29:54 +00:00
Debug.WriteLine("Queued asset: " + assetName);
}
else
{
2018-11-01 04:57:36 +00:00
throw new InvalidOperationException("Did not queue asset due to asset with same name being loaded: " + assetName);
}
}
}
/// <summary>
/// Called whenever a batch of assets should be loaded from the queue. Safe to call once every frame.
/// </summary>
public void Update()
{
2018-10-31 00:47:01 +00:00
if (queue.Count > 0 && (thread == null || !thread.IsAlive))
2018-09-12 06:32:05 +00:00
{
2018-10-31 00:47:01 +00:00
ThreadStart threadStart = new ThreadStart(LoadBatch);
thread = new Thread(threadStart);
thread.Start();
2018-09-12 06:32:05 +00:00
}
}
2018-10-30 23:29:54 +00:00
private void LoadBatch()
{
2018-11-01 04:57:36 +00:00
int totalTasks = queue.Count;
int tasksCompleted = 0;
2018-10-30 23:29:54 +00:00
while (queue.Count != 0)
{
lock (queue)
{
LoadableContent content = queue.Dequeue();
Load(content.assetName, content.type, content.usePathModifier);
2018-11-01 04:57:36 +00:00
tasksCompleted++;
progress = (float)tasksCompleted / totalTasks;
OnProgress(content.assetName, progress);
2018-10-30 23:29:54 +00:00
}
}
}
/// <summary>
/// Removes the asset from the list of assets in the system.
/// Cannot remove from queue.
/// </summary>
/// <param name="name">the string name used to load the asset</param>
public void Remove(string name)
{
lock (queue)
{
if (assets.ContainsKey(name))
{
assets[name].Dispose();
assets.Remove(name);
}
}
}
/// <summary>
/// Clears the queue.
/// </summary>
public void ClearQueue()
{
2018-10-30 23:29:54 +00:00
lock (queue)
{
2018-10-30 23:29:54 +00:00
queue.Clear();
}
}
/// <summary>
/// Unloads everything from both queue and loaded list while properly disposing of the assets loaded.
/// </summary>
public void UnloadAll()
2018-09-12 06:32:05 +00:00
{
lock (queue)
{
contentManager.Unload();
assets.Clear();
ClearQueue();
Debug.WriteLine("Unloaded all assets.");
}
2018-09-12 06:32:05 +00:00
}
2018-10-30 23:29:54 +00:00
public bool Done
{
2018-10-30 23:29:54 +00:00
get
{
return queue.Count == 0;
}
}
2018-11-01 04:57:36 +00:00
protected virtual void OnProgress(string fileName, float progress)
{
ContentSystemListeners?.Invoke(fileName, progress);
2018-11-01 04:57:36 +00:00
}
public float Progress
{
get
{
return progress;
}
}
2018-09-12 06:32:05 +00:00
}
}