Parallax refactors (#7654)

This commit is contained in:
20kdc
2022-05-04 17:55:21 +01:00
committed by GitHub
parent 9bc965409a
commit 3d606f4316
24 changed files with 857 additions and 179 deletions

View File

@@ -1,12 +1,16 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Content.Client.Parallax.Data;
using Content.Shared;
using Content.Shared.CCVar;
using Nett;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Shared.Prototypes;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.IoC;
@@ -15,91 +19,115 @@ using Robust.Shared.Utility;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
namespace Content.Client.Parallax.Managers
namespace Content.Client.Parallax.Managers;
internal sealed class ParallaxManager : IParallaxManager
{
internal sealed class ParallaxManager : IParallaxManager
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
private string _parallaxName = "";
public string ParallaxName
{
[Dependency] private readonly IResourceCache _resourceCache = default!;
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
private static readonly ResourcePath ParallaxConfigPath = new("/parallax_config.toml");
// Both of these below are in the user directory.
private static readonly ResourcePath ParallaxCachedImagePath = new("/parallax_cache.png");
private static readonly ResourcePath PreviousParallaxConfigPath = new("/parallax_config_old");
public event Action<Texture>? OnTextureLoaded;
public Texture? ParallaxTexture { get; private set; }
public async void LoadParallax()
get => _parallaxName;
set
{
if (!_configurationManager.GetCVar(CCVars.ParallaxEnabled))
return;
var parallaxConfig = GetParallaxConfig();
if (parallaxConfig == null)
return;
var debugParallax = _configurationManager.GetCVar(CCVars.ParallaxDebug);
if (debugParallax
|| !_resourceCache.UserData.TryReadAllText(PreviousParallaxConfigPath, out var previousParallaxConfig)
|| previousParallaxConfig != parallaxConfig)
{
var table = Toml.ReadString(parallaxConfig);
await UpdateCachedTexture(table, debugParallax);
//Update the previous config
using var writer = _resourceCache.UserData.OpenWriteText(PreviousParallaxConfigPath);
writer.Write(parallaxConfig);
}
ParallaxTexture = GetCachedTexture();
OnTextureLoaded?.Invoke(ParallaxTexture);
}
private async Task UpdateCachedTexture(TomlTable config, bool saveDebugLayers)
{
var debugImages = saveDebugLayers ? new List<Image<Rgba32>>() : null;
var sawmill = _logManager.GetSawmill("parallax");
// Generate the parallax in the thread pool.
using var newParallexImage = await Task.Run(() =>
ParallaxGenerator.GenerateParallax(config, new Size(1920, 1080), sawmill, debugImages));
// And load it in the main thread for safety reasons.
// Store it and CRC so further game starts don't need to regenerate it.
using var imageStream = _resourceCache.UserData.OpenWrite(ParallaxCachedImagePath);
newParallexImage.SaveAsPng(imageStream);
if (saveDebugLayers)
{
for (var i = 0; i < debugImages!.Count; i++)
{
var debugImage = debugImages[i];
using var debugImageStream = _resourceCache.UserData.OpenWrite(new ResourcePath($"/parallax_debug_{i}.png"));
debugImage.SaveAsPng(debugImageStream);
}
}
}
private Texture GetCachedTexture()
{
using var imageStream = _resourceCache.UserData.OpenRead(ParallaxCachedImagePath);
return Texture.LoadFromPNGStream(imageStream, "Parallax");
}
private string? GetParallaxConfig()
{
if (!_resourceCache.TryContentFileRead(ParallaxConfigPath, out var configStream))
{
Logger.ErrorS("parallax", "Parallax config not found.");
return null;
}
using var configReader = new StreamReader(configStream, EncodingHelpers.UTF8);
return configReader.ReadToEnd().Replace(Environment.NewLine, "\n");
LoadParallaxByName(value);
}
}
public Vector2 ParallaxAnchor { get; set; }
private CancellationTokenSource? _presentParallaxLoadCancel;
private ParallaxLayerPrepared[] _parallaxLayersHQ = {};
private ParallaxLayerPrepared[] _parallaxLayersLQ = {};
public ParallaxLayerPrepared[] ParallaxLayers => _configurationManager.GetCVar(CCVars.ParallaxLowQuality) ? _parallaxLayersLQ : _parallaxLayersHQ;
public async void LoadParallax()
{
await LoadParallaxByName("default");
}
private async Task LoadParallaxByName(string name)
{
// Update _parallaxName
if (_parallaxName == name)
{
return;
}
_parallaxName = name;
// Cancel any existing load and setup the new cancellation token
_presentParallaxLoadCancel?.Cancel();
_presentParallaxLoadCancel = new CancellationTokenSource();
var cancel = _presentParallaxLoadCancel.Token;
// Empty parallax name = no layers (this is so that the initial "" parallax name is consistent)
if (_parallaxName == "")
{
_parallaxLayersHQ = _parallaxLayersLQ = new ParallaxLayerPrepared[] {};
return;
}
// Begin (for real)
Logger.InfoS("parallax", $"Loading parallax {name}");
try
{
var parallaxPrototype = _prototypeManager.Index<ParallaxPrototype>(name);
ParallaxLayerPrepared[] hq;
ParallaxLayerPrepared[] lq;
if (parallaxPrototype.LayersLQUseHQ)
{
lq = hq = await LoadParallaxLayers(parallaxPrototype.Layers, cancel);
}
else
{
var results = await Task.WhenAll(
LoadParallaxLayers(parallaxPrototype.Layers, cancel),
LoadParallaxLayers(parallaxPrototype.LayersLQ, cancel)
);
hq = results[0];
lq = results[1];
}
// Still keeping this check just in case.
if (_parallaxName == name)
{
_parallaxLayersHQ = hq;
_parallaxLayersLQ = lq;
Logger.InfoS("parallax", $"Loaded parallax {name}");
}
}
catch (Exception ex)
{
Logger.ErrorS("parallax", $"Failed to loaded parallax {name}: {ex}");
}
}
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++)
{
tasks[i] = LoadParallaxLayer(layersIn[i], cancel);
}
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
};
}
}