bye bye asynccontentmanager

This commit is contained in:
Nahuel Rocchetti 2024-06-30 00:08:44 -03:00
parent e19875e508
commit 4ebbff1b0d
4 changed files with 10 additions and 98 deletions

View file

@ -6,6 +6,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
/// <summary>
@ -34,7 +35,7 @@ public class TSAudioSource : MonoBehaviour
private float _timeAudioSourcePlaying = 0f;
private bool _audioClipPlaying = false;
private bool _paused = false;
private AsyncContentManager _asyncContentLoader;
private ContentManager _contentManager;
public void Pause()
{
@ -61,18 +62,13 @@ public class TSAudioSource : MonoBehaviour
public void PlayAsync(ResourceKey audioResourceKey)
{
CleanUp();
StartCoroutine(PlayAsyncInternal(audioResourceKey));
}
private IEnumerator PlayAsyncInternal(ResourceKey key)
{
var audioRequest = _asyncContentLoader.RequestAsset(key);
while (!audioRequest.Finished)
yield return null;
if (audioRequest.Result == AsyncContentManager.Results.Success)
PlayInternal(audioRequest.Asset as AudioAsset);
else
Stop();
Task.Run(() =>
{
_audio = _contentManager.GetAsset<AudioAsset>(audioResourceKey);
}).ContinueWith(task =>
{
PlayInternal(_audio);
}, TaskScheduler.FromCurrentSynchronizationContext());
}
private void PlayInternal(AudioAsset asset)
@ -93,7 +89,7 @@ public class TSAudioSource : MonoBehaviour
private void Awake()
{
_asyncContentLoader = AsyncContentManager.Instance;
_contentManager = ContentManager.Instance;
_audioSource = GetComponent<AudioSource>();
if (_audioSource == null)
{

View file

@ -1,72 +0,0 @@
using OpenTS2.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace OpenTS2.Content
{
public class AsyncContentManager
{
public class AssetRequest
{
public bool Finished = false;
public Results Result;
public AbstractAsset Asset;
}
public enum Results
{
Success,
Failed
}
public static AsyncContentManager Instance { get; private set; }
private Thread _thread;
private Queue<Action> _requests = new Queue<Action>();
private ContentManager _contentManager;
public AsyncContentManager()
{
Instance = this;
_contentManager = ContentManager.Instance;
_thread = new Thread(new ThreadStart(ThreadLoop));
_thread.IsBackground = true;
_thread.Name = "AsyncContentLoader";
_thread.Start();
}
public AssetRequest RequestAsset(ResourceKey key)
{
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 requestResult;
}
private void ThreadLoop()
{
while (true)
{
if (_requests.Count > 0)
{
var request = _requests.Dequeue();
request.Invoke();
}
else
{
try
{
Thread.Sleep(Timeout.Infinite);
}
catch (ThreadInterruptedException) { }
}
}
}
}
}

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: fe0727e0583480d4bb881ac3fc7c2b79
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -44,7 +44,6 @@ namespace OpenTS2.Engine
var nhoodManager = new NeighborhoodManager();
var casController = new CASManager();
var lotManger = new LotManager();
var asyncContentLoader = new AsyncContentManager();
TerrainManager.Initialize();
MaterialManager.Initialize();