Files
crystall-punk-14/Content.Client/Parallax/Managers/ParallaxManager.cs

126 lines
4.1 KiB
C#
Raw Normal View History

using System.Collections.Concurrent;
using System.Numerics;
2022-05-04 17:55:21 +01:00
using System.Threading;
2018-11-30 21:54:30 +01:00
using System.Threading.Tasks;
2022-05-04 17:55:21 +01:00
using Content.Client.Parallax.Data;
using Content.Shared.CCVar;
2022-05-04 17:55:21 +01:00
using Robust.Shared.Prototypes;
using Robust.Shared.Configuration;
using Robust.Shared.Utility;
2018-11-30 21:54:30 +01:00
2022-05-04 17:55:21 +01:00
namespace Content.Client.Parallax.Managers;
public sealed class ParallaxManager : IParallaxManager
2018-11-30 21:54:30 +01:00
{
2022-05-04 17:55:21 +01:00
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
2018-11-30 21:54:30 +01:00
private ISawmill _sawmill = Logger.GetSawmill("parallax");
2018-11-30 21:54:30 +01:00
2022-05-04 17:55:21 +01:00
public Vector2 ParallaxAnchor { get; set; }
2018-11-30 21:54:30 +01:00
2022-08-15 14:16:53 +10:00
private readonly Dictionary<string, ParallaxLayerPrepared[]> _parallaxesLQ = new();
private readonly Dictionary<string, ParallaxLayerPrepared[]> _parallaxesHQ = new();
2018-11-30 21:54:30 +01:00
2022-08-15 14:16:53 +10:00
private readonly Dictionary<string, CancellationTokenSource> _loadingParallaxes = new();
2019-06-29 01:58:16 +02:00
public bool IsLoaded(string name) => _parallaxesLQ.ContainsKey(name);
2018-11-30 21:54:30 +01:00
public ParallaxLayerPrepared[] GetParallaxLayers(string name)
2022-05-04 17:55:21 +01:00
{
if (_configurationManager.GetCVar(CCVars.ParallaxLowQuality))
{
return !_parallaxesLQ.TryGetValue(name, out var lq) ? Array.Empty<ParallaxLayerPrepared>() : lq;
}
2022-08-15 14:16:53 +10:00
return !_parallaxesHQ.TryGetValue(name, out var hq) ? Array.Empty<ParallaxLayerPrepared>() : hq;
2022-05-04 17:55:21 +01:00
}
2018-11-30 21:54:30 +01:00
public void UnloadParallax(string name)
2022-05-04 17:55:21 +01:00
{
if (_loadingParallaxes.TryGetValue(name, out var loading))
2022-05-04 17:55:21 +01:00
{
loading.Cancel();
_loadingParallaxes.Remove(name, out _);
2022-05-04 17:55:21 +01:00
return;
}
2018-11-30 21:54:30 +01:00
if (!_parallaxesLQ.ContainsKey(name)) return;
2022-08-15 14:16:53 +10:00
_parallaxesLQ.Remove(name);
_parallaxesHQ.Remove(name);
}
2019-08-29 14:36:31 +02:00
public async void LoadDefaultParallax()
{
2023-03-06 15:37:35 +11:00
_sawmill.Level = LogLevel.Info;
await LoadParallaxByName("Default");
}
public async Task LoadParallaxByName(string name)
{
if (_parallaxesLQ.ContainsKey(name) || _loadingParallaxes.ContainsKey(name)) return;
// Cancel any existing load and setup the new cancellation token
var token = new CancellationTokenSource();
_loadingParallaxes[name] = token;
var cancel = token.Token;
2022-05-04 17:55:21 +01:00
// Begin (for real)
2023-03-06 15:37:35 +11:00
_sawmill.Debug($"Loading parallax {name}");
2022-05-04 17:55:21 +01:00
try
{
2022-05-04 17:55:21 +01:00
var parallaxPrototype = _prototypeManager.Index<ParallaxPrototype>(name);
2018-11-30 21:54:30 +01:00
ParallaxLayerPrepared[][] layers;
2018-11-30 21:54:30 +01:00
2022-05-04 17:55:21 +01:00
if (parallaxPrototype.LayersLQUseHQ)
{
layers = new ParallaxLayerPrepared[2][];
layers[0] = layers[1] = await LoadParallaxLayers(parallaxPrototype.Layers, cancel);
2022-05-04 17:55:21 +01:00
}
else
{
layers = await Task.WhenAll(
2022-05-04 17:55:21 +01:00
LoadParallaxLayers(parallaxPrototype.Layers, cancel),
LoadParallaxLayers(parallaxPrototype.LayersLQ, cancel)
);
}
2018-11-30 21:54:30 +01:00
_loadingParallaxes.Remove(name, out _);
if (token.Token.IsCancellationRequested) return;
2022-08-15 14:16:53 +10:00
_parallaxesLQ[name] = layers[1];
_parallaxesHQ[name] = layers[0];
}
2022-05-04 17:55:21 +01:00
catch (Exception ex)
{
_sawmill.Error($"Failed to loaded parallax {name}: {ex}");
}
2022-05-04 17:55:21 +01:00
}
2019-08-28 12:08:10 +02:00
2022-05-04 17:55:21 +01:00
private async Task<ParallaxLayerPrepared[]> LoadParallaxLayers(List<ParallaxLayerConfig> layersIn, CancellationToken cancel = default)
{
// Because this is async, make sure it doesn't change (prototype reloads could muck this up)
// Since the tasks aren't awaited until the end, this should be fine
var tasks = new Task<ParallaxLayerPrepared>[layersIn.Count];
for (var i = 0; i < layersIn.Count; i++)
{
2022-05-04 17:55:21 +01:00
tasks[i] = LoadParallaxLayer(layersIn[i], cancel);
2018-11-30 21:54:30 +01:00
}
2022-05-04 17:55:21 +01:00
return await Task.WhenAll(tasks);
}
private async Task<ParallaxLayerPrepared> LoadParallaxLayer(ParallaxLayerConfig config, CancellationToken cancel = default)
{
return new ParallaxLayerPrepared()
{
Texture = await config.Texture.GenerateTexture(cancel),
Config = config
};
2018-11-30 21:54:30 +01:00
}
}
2022-05-04 17:55:21 +01:00