Skip to content

Commit

Permalink
use delegates in asynccontentmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyDuchess committed Jun 29, 2024
1 parent 56fa955 commit 913a87d
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions Assets/Scripts/OpenTS2/Content/AsyncContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ namespace OpenTS2.Content
{
public class AsyncContentManager
{
public class Request
public class AssetRequest
{
public bool Finished = false;
public Results Result;
public ResourceKey Key;
public AbstractAsset Asset;
}
public enum Results
Expand All @@ -24,7 +23,7 @@ public enum Results
}
public static AsyncContentManager Instance { get; private set; }
private Thread _thread;
private Queue<Request> _requests = new Queue<Request>();
private Queue<Action> _requests = new Queue<Action>();
private ContentManager _contentManager;
public AsyncContentManager()
{
Expand All @@ -36,13 +35,18 @@ public AsyncContentManager()
_thread.Start();
}

public Request RequestAsset(ResourceKey key)
public AssetRequest RequestAsset(ResourceKey key)
{
var request = new Request();
request.Key = key;
_requests.Enqueue(request);
var requestResult = new AssetRequest();
_requests.Enqueue(() =>
{
var asset = _contentManager.GetAsset(key);
requestResult.Asset = asset;
requestResult.Finished = true;
requestResult.Result = asset != null ? Results.Success : Results.Failed;
});
_thread.Interrupt();
return request;
return requestResult;
}

private void ThreadLoop()
Expand All @@ -51,11 +55,8 @@ private void ThreadLoop()
{
if (_requests.Count > 0)
{
var request = _requests.Peek();
request.Asset = _contentManager.GetAsset(request.Key);
request.Result = request.Asset != null ? Results.Success : Results.Failed;
request.Finished = true;
_requests.Dequeue();
var request = _requests.Dequeue();
request.Invoke();
}
else
{
Expand Down

0 comments on commit 913a87d

Please sign in to comment.