Content loading system functioning.

This commit is contained in:
2018-10-30 18:29:54 -05:00
parent 3a4dfb94ac
commit a9647f2146
14 changed files with 189 additions and 87 deletions

View File

@@ -2,6 +2,8 @@
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
@@ -11,57 +13,57 @@ namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
{
public class ContentSystem
{
volatile bool queued;
Thread thread;
readonly ContentManager contentManager;
readonly Queue<LoadableContent> queue;
Dictionary<string, IDisposable> assets;
public readonly Dictionary<Type, IContentResolver> contentResolver;
public readonly Dictionary<Type, IContentPathModifier> contentPathModifier;
public ContentSystem(ContentManager contentManager)
{
this.contentManager = contentManager;
assets = new Dictionary<string, IDisposable>();
queue = new Queue<LoadableContent>();
contentResolver = new Dictionary<Type, IContentResolver>();
contentPathModifier = new Dictionary<Type, IContentPathModifier>();
}
private void Load(string assetName, Type type)
{
IContentResolver handler = contentResolver[type];
string path = handler.Load(assetName);
assets.Add(assetName, contentManager.Load<IDisposable>(path));
IContentPathModifier handler = contentPathModifier[type];
string path = handler.Modify(assetName);
try
{
assets.Add(assetName, contentManager.Load<IDisposable>(path));
} catch (ContentLoadException cle)
{
Debug.WriteLine("Failed to load " + assetName + "with modified path: " + cle.Message);
Debug.WriteLine("Loading asset from root: " + assetName);
assets.Add(assetName, contentManager.Load<IDisposable>(assetName));
}
Debug.WriteLine("Loaded asset: " + assetName);
}
private void LoadBatch()
{
while (queue.Count != 0)
{
lock (queue)
{
LoadableContent content = queue.Dequeue();
Load(content.assetName, content.type);
}
queued = false;
}
}
public T Get<T>(string assetName)
{
lock(queue)
lock (queue)
{
return (T)assets[assetName];
}
}
public void Queue(string assetName, Type type)
public void Queue<T>(string assetName) where T : IDisposable
{
queued = true;
lock (queue)
{
if (!assets.ContainsKey(assetName))
{
queue.Enqueue(new LoadableContent(assetName, type));
queue.Enqueue(new LoadableContent(assetName, typeof(T)));
Debug.WriteLine("Queued asset: " + assetName);
}
else
{
Debug.WriteLine("Did not queue asset due to asset with same name being loaded: " + assetName);
}
}
}
@@ -77,10 +79,22 @@ namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
{
ThreadStart threadStart = new ThreadStart(LoadBatch);
thread = new Thread(threadStart);
thread.Start();
}
}
}
private void LoadBatch()
{
while (queue.Count != 0)
{
lock (queue)
{
LoadableContent content = queue.Dequeue();
Load(content.assetName, content.type);
}
}
}
/// <summary>
/// Removes the asset from the list of assets in the system.
/// Cannot remove from queue.
@@ -100,9 +114,9 @@ namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
/// </summary>
public void ClearQueue()
{
lock(queue)
lock (queue)
{
queue.Clear();
queue.Clear();
}
}
@@ -119,9 +133,12 @@ namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
ClearQueue();
}
public bool Done()
public bool Done
{
return queued;
get
{
return queue.Count == 0;
}
}
}
}

View File

@@ -6,13 +6,13 @@ using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
{
public interface IContentResolver
public interface IContentPathModifier
{
/// <summary>
/// Returns the complete path with the content folder as root.
/// </summary>
/// <param name="assetName">Is the asset's name</param>
/// <returns></returns>
string Load(string assetName);
string Modify(string assetName);
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
{
class NormalContentResolver : IContentPathModifier
{
public string Modify(string assetName)
{
return assetName;
}
}
}