refactor: removed dash from project name and underscore from namespaces. Began working on pipeline project.
This commit is contained in:
22
RecrownedAthenaeum/ContentSystem/ContentLoad.cs
Normal file
22
RecrownedAthenaeum/ContentSystem/ContentLoad.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RecrownedAthenaeum.ContentSystem
|
||||
{
|
||||
struct LoadableContent
|
||||
{
|
||||
internal Type type;
|
||||
internal string assetName;
|
||||
internal bool usePathModifier;
|
||||
|
||||
public LoadableContent(string assetName, Type type, bool usePathModifier)
|
||||
{
|
||||
this.type = type;
|
||||
this.assetName = assetName;
|
||||
this.usePathModifier = usePathModifier;
|
||||
}
|
||||
}
|
||||
}
|
157
RecrownedAthenaeum/ContentSystem/ContentManagerController.cs
Normal file
157
RecrownedAthenaeum/ContentSystem/ContentManagerController.cs
Normal file
@@ -0,0 +1,157 @@
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
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;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RecrownedAthenaeum.ContentSystem
|
||||
{
|
||||
public class ContentManagerController
|
||||
{
|
||||
Thread thread;
|
||||
readonly ContentManager contentManager;
|
||||
readonly Queue<LoadableContent> queue;
|
||||
Dictionary<string, IDisposable> assets;
|
||||
public readonly Dictionary<Type, IContentPathModifier> contentPathModifier;
|
||||
volatile float progress;
|
||||
volatile bool running;
|
||||
|
||||
public ContentManagerController(ContentManager contentManager)
|
||||
{
|
||||
this.contentManager = contentManager;
|
||||
assets = new Dictionary<string, IDisposable>();
|
||||
queue = new Queue<LoadableContent>();
|
||||
contentPathModifier = new Dictionary<Type, IContentPathModifier>();
|
||||
}
|
||||
|
||||
private void Load(string assetName, Type type, bool usePathModifier)
|
||||
{
|
||||
string path = assetName;
|
||||
if (usePathModifier)
|
||||
{
|
||||
IContentPathModifier handler = contentPathModifier[type];
|
||||
path = handler.Modify(assetName);
|
||||
|
||||
}
|
||||
assets.Add(assetName, contentManager.Load<IDisposable>(path));
|
||||
|
||||
Debug.WriteLine("Loaded asset: " + assetName);
|
||||
}
|
||||
|
||||
public T Get<T>(string assetName)
|
||||
{
|
||||
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));
|
||||
Debug.WriteLine("Queued asset: " + assetName);
|
||||
}
|
||||
else
|
||||
{
|
||||
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()
|
||||
{
|
||||
if (queue.Count > 0 && (thread == null || !thread.IsAlive))
|
||||
{
|
||||
thread = new Thread(LoadBatch);
|
||||
thread.Start();
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadBatch()
|
||||
{
|
||||
running = true;
|
||||
int totalTasks = queue.Count;
|
||||
int tasksCompleted = 0;
|
||||
while (queue.Count != 0)
|
||||
{
|
||||
lock (queue)
|
||||
{
|
||||
LoadableContent content = queue.Dequeue();
|
||||
Load(content.assetName, content.type, content.usePathModifier);
|
||||
tasksCompleted++;
|
||||
progress = (float)tasksCompleted / totalTasks;
|
||||
}
|
||||
}
|
||||
running = false;
|
||||
}
|
||||
/// <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()
|
||||
{
|
||||
lock (queue)
|
||||
{
|
||||
queue.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unloads everything from both queue and loaded list while properly disposing of the assets loaded.
|
||||
/// </summary>
|
||||
public void UnloadAll()
|
||||
{
|
||||
lock (queue)
|
||||
{
|
||||
contentManager.Unload();
|
||||
assets.Clear();
|
||||
ClearQueue();
|
||||
Debug.WriteLine("Unloaded all assets.");
|
||||
}
|
||||
}
|
||||
|
||||
public bool Done
|
||||
{
|
||||
get
|
||||
{
|
||||
return !running && queue.Count == 0;
|
||||
}
|
||||
}
|
||||
|
||||
public float Progress
|
||||
{
|
||||
get
|
||||
{
|
||||
return progress;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
18
RecrownedAthenaeum/ContentSystem/IContentPathModifier.cs
Normal file
18
RecrownedAthenaeum/ContentSystem/IContentPathModifier.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RecrownedAthenaeum.ContentSystem
|
||||
{
|
||||
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 Modify(string assetName);
|
||||
}
|
||||
}
|
16
RecrownedAthenaeum/ContentSystem/NormalContentResolver.cs
Normal file
16
RecrownedAthenaeum/ContentSystem/NormalContentResolver.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RecrownedAthenaeum.ContentSystem
|
||||
{
|
||||
class NormalContentResolver : IContentPathModifier
|
||||
{
|
||||
public string Modify(string assetName)
|
||||
{
|
||||
return assetName;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user