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

106 lines
4.1 KiB
C#
Raw Normal View History

2018-11-30 21:54:30 +01:00
using System;
2019-08-29 14:36:31 +02:00
using System.Collections.Generic;
2018-11-30 21:54:30 +01:00
using System.IO;
using System.Threading.Tasks;
using Content.Shared;
using Content.Shared.CCVar;
2018-11-30 21:54:30 +01:00
using Nett;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Utility;
2019-08-01 00:13:49 +02:00
using SixLabors.ImageSharp;
2019-08-29 14:36:31 +02:00
using SixLabors.ImageSharp.PixelFormats;
2018-11-30 21:54:30 +01:00
2021-06-09 22:19:39 +02:00
namespace Content.Client.Parallax.Managers
2018-11-30 21:54:30 +01:00
{
2020-11-27 12:19:01 +01:00
internal sealed class ParallaxManager : IParallaxManager
2018-11-30 21:54:30 +01:00
{
[Dependency] private readonly IResourceCache _resourceCache = default!;
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
2018-11-30 21:54:30 +01:00
private static readonly ResourcePath ParallaxConfigPath = new("/parallax_config.toml");
2018-11-30 21:54:30 +01:00
// 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");
2018-11-30 21:54:30 +01:00
public event Action<Texture>? OnTextureLoaded;
public Texture? ParallaxTexture { get; private set; }
2018-11-30 21:54:30 +01:00
public async void LoadParallax()
{
if (!_configurationManager.GetCVar(CCVars.ParallaxEnabled))
2019-06-29 01:58:16 +02:00
return;
var parallaxConfig = GetParallaxConfig();
if (parallaxConfig == null)
2019-08-29 14:36:31 +02:00
return;
2018-11-30 21:54:30 +01:00
var debugParallax = _configurationManager.GetCVar(CCVars.ParallaxDebug);
2018-11-30 21:54:30 +01:00
if (debugParallax
|| !_resourceCache.UserData.TryReadAllText(PreviousParallaxConfigPath, out var previousParallaxConfig)
|| previousParallaxConfig != parallaxConfig)
{
var table = Toml.ReadString(parallaxConfig);
await UpdateCachedTexture(table, debugParallax);
2018-11-30 21:54:30 +01:00
//Update the previous config
using var writer = _resourceCache.UserData.OpenWriteText(PreviousParallaxConfigPath);
writer.Write(parallaxConfig);
2018-11-30 21:54:30 +01:00
}
2019-08-29 14:36:31 +02:00
ParallaxTexture = GetCachedTexture();
OnTextureLoaded?.Invoke(ParallaxTexture);
}
private async Task UpdateCachedTexture(TomlTable config, bool saveDebugLayers)
{
var debugImages = saveDebugLayers ? new List<Image<Rgba32>>() : null;
2018-11-30 21:54:30 +01:00
var sawmill = _logManager.GetSawmill("parallax");
2018-11-30 21:54:30 +01:00
// Generate the parallax in the thread pool.
using var newParallexImage = await Task.Run(() =>
ParallaxGenerator.GenerateParallax(config, new Size(1920, 1080), sawmill, debugImages));
2018-11-30 21:54:30 +01:00
// 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);
2018-11-30 21:54:30 +01:00
if (saveDebugLayers)
2019-08-29 14:36:31 +02:00
{
for (var i = 0; i < debugImages!.Count; i++)
2019-08-29 14:36:31 +02:00
{
var debugImage = debugImages[i];
using var debugImageStream = _resourceCache.UserData.OpenWrite(new ResourcePath($"/parallax_debug_{i}.png"));
debugImage.SaveAsPng(debugImageStream);
2019-08-29 14:36:31 +02:00
}
}
}
2019-08-29 14:36:31 +02:00
private Texture GetCachedTexture()
{
using var imageStream = _resourceCache.UserData.OpenRead(ParallaxCachedImagePath);
return Texture.LoadFromPNGStream(imageStream, "Parallax");
}
2019-08-28 12:08:10 +02:00
private string? GetParallaxConfig()
{
if (!_resourceCache.TryContentFileRead(ParallaxConfigPath, out var configStream))
2018-11-30 21:54:30 +01:00
{
Logger.ErrorS("parallax", "Parallax config not found.");
return null;
2018-11-30 21:54:30 +01:00
}
using var configReader = new StreamReader(configStream, EncodingHelpers.UTF8);
return configReader.ReadToEnd().Replace(Environment.NewLine, "\n");
2018-11-30 21:54:30 +01:00
}
}
}