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;
|
2020-11-07 01:15:56 +01:00
|
|
|
using Content.Shared;
|
2021-06-13 14:52:40 +02:00
|
|
|
using Content.Shared.CCVar;
|
2018-11-30 21:54:30 +01:00
|
|
|
using Nett;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Client.Graphics;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Client.ResourceManagement;
|
|
|
|
|
using Robust.Shared.Configuration;
|
|
|
|
|
using Robust.Shared.ContentPack;
|
2019-04-15 21:11:38 -06:00
|
|
|
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
|
|
|
{
|
2020-08-24 14:10:28 +02: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
|
|
|
|
2020-11-27 11:00:49 +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.
|
2021-12-12 18:26:22 -08:00
|
|
|
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
|
|
|
|
2021-03-10 14:48:29 +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()
|
|
|
|
|
{
|
2020-11-07 01:15:56 +01:00
|
|
|
if (!_configurationManager.GetCVar(CCVars.ParallaxEnabled))
|
2019-06-29 01:58:16 +02:00
|
|
|
return;
|
|
|
|
|
|
2021-12-12 18:26:22 -08:00
|
|
|
var parallaxConfig = GetParallaxConfig();
|
|
|
|
|
if (parallaxConfig == null)
|
2019-08-29 14:36:31 +02:00
|
|
|
return;
|
2018-11-30 21:54:30 +01:00
|
|
|
|
2021-12-12 18:26:22 -08:00
|
|
|
var debugParallax = _configurationManager.GetCVar(CCVars.ParallaxDebug);
|
2018-11-30 21:54:30 +01:00
|
|
|
|
2021-12-12 18:26:22 -08: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
|
|
|
|
2021-12-12 18:26:22 -08: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
|
|
|
|
2021-12-12 18:26:22 -08: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
|
|
|
|
2019-05-08 22:01:14 +02:00
|
|
|
var sawmill = _logManager.GetSawmill("parallax");
|
2018-11-30 21:54:30 +01:00
|
|
|
// Generate the parallax in the thread pool.
|
2021-12-12 18:26:22 -08:00
|
|
|
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.
|
2021-12-12 18:26:22 -08:00
|
|
|
using var imageStream = _resourceCache.UserData.OpenWrite(ParallaxCachedImagePath);
|
|
|
|
|
newParallexImage.SaveAsPng(imageStream);
|
2018-11-30 21:54:30 +01:00
|
|
|
|
2021-12-12 18:26:22 -08:00
|
|
|
if (saveDebugLayers)
|
2019-08-29 14:36:31 +02:00
|
|
|
{
|
2021-12-12 18:26:22 -08:00
|
|
|
for (var i = 0; i < debugImages!.Count; i++)
|
2019-08-29 14:36:31 +02:00
|
|
|
{
|
2021-12-12 18:26:22 -08: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
|
|
|
}
|
|
|
|
|
}
|
2021-12-12 18:26:22 -08:00
|
|
|
}
|
2019-08-29 14:36:31 +02:00
|
|
|
|
2021-12-12 18:26:22 -08:00
|
|
|
private Texture GetCachedTexture()
|
|
|
|
|
{
|
|
|
|
|
using var imageStream = _resourceCache.UserData.OpenRead(ParallaxCachedImagePath);
|
|
|
|
|
return Texture.LoadFromPNGStream(imageStream, "Parallax");
|
|
|
|
|
}
|
2019-08-28 12:08:10 +02:00
|
|
|
|
2021-12-12 18:26:22 -08:00
|
|
|
private string? GetParallaxConfig()
|
|
|
|
|
{
|
|
|
|
|
if (!_resourceCache.TryContentFileRead(ParallaxConfigPath, out var configStream))
|
2018-11-30 21:54:30 +01:00
|
|
|
{
|
2021-12-12 18:26:22 -08:00
|
|
|
Logger.ErrorS("parallax", "Parallax config not found.");
|
|
|
|
|
return null;
|
2018-11-30 21:54:30 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-12 18:26:22 -08:00
|
|
|
using var configReader = new StreamReader(configStream, EncodingHelpers.UTF8);
|
|
|
|
|
return configReader.ReadToEnd().Replace(Environment.NewLine, "\n");
|
2018-11-30 21:54:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|