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

123 lines
4.3 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.Client.Interfaces.Parallax;
using Content.Shared;
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
namespace Content.Client.Parallax
{
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 ParallaxPath = new("/parallax_cache.png");
private static readonly ResourcePath ParallaxConfigOld = 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 debugParallax = _configurationManager.GetCVar(CCVars.ParallaxDebug);
2018-12-03 16:55:00 +01:00
string contents;
2018-11-30 21:54:30 +01:00
TomlTable table;
2019-08-29 14:36:31 +02:00
// Load normal config into memory
if (!_resourceCache.TryContentFileRead(ParallaxConfigPath, out var configStream))
2018-11-30 21:54:30 +01:00
{
2019-08-29 14:36:31 +02:00
Logger.ErrorS("parallax", "Parallax config not found.");
return;
}
2018-11-30 21:54:30 +01:00
2019-08-29 14:36:31 +02:00
using (configStream)
{
using (var reader = new StreamReader(configStream, EncodingHelpers.UTF8))
2018-12-03 16:55:00 +01:00
{
contents = reader.ReadToEnd().Replace(Environment.NewLine, "\n");
2018-12-03 16:55:00 +01:00
}
2018-11-30 21:54:30 +01:00
2019-08-29 14:36:31 +02:00
if (!debugParallax && _resourceCache.UserData.Exists(ParallaxConfigOld))
2018-11-30 21:54:30 +01:00
{
var match = _resourceCache.UserData.ReadAllText(ParallaxConfigOld) == contents;
2018-11-30 21:54:30 +01:00
if (match)
{
using (var stream = _resourceCache.UserData.OpenRead(ParallaxPath))
2018-11-30 21:54:30 +01:00
{
2019-02-11 19:14:37 +01:00
ParallaxTexture = Texture.LoadFromPNGStream(stream, "Parallax");
2018-11-30 21:54:30 +01:00
}
OnTextureLoaded?.Invoke(ParallaxTexture);
return;
}
}
2018-12-03 16:55:00 +01:00
table = Toml.ReadString(contents);
2018-11-30 21:54:30 +01:00
}
2019-08-29 14:36:31 +02:00
List<Image<Rgba32>>? debugImages = null;
2019-08-29 14:36:31 +02:00
if (debugParallax)
2018-11-30 21:54:30 +01:00
{
2019-08-29 14:36:31 +02:00
debugImages = new List<Image<Rgba32>>();
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.
2019-08-29 14:36:31 +02:00
var image = await Task.Run(() =>
ParallaxGenerator.GenerateParallax(table, new Size(1920, 1080), sawmill, debugImages));
2018-11-30 21:54:30 +01:00
// And load it in the main thread for safety reasons.
2019-02-11 19:14:37 +01:00
ParallaxTexture = Texture.LoadFromImage(image, "Parallax");
2018-11-30 21:54:30 +01:00
// Store it and CRC so further game starts don't need to regenerate it.
using (var stream = _resourceCache.UserData.Create(ParallaxPath))
2018-11-30 21:54:30 +01:00
{
image.SaveAsPng(stream);
}
if (debugParallax && debugImages != null)
2019-08-29 14:36:31 +02:00
{
var i = 0;
foreach (var debugImage in debugImages)
{
using (var stream = _resourceCache.UserData.Create(new ResourcePath($"/parallax_debug_{i}.png")))
2019-08-29 14:36:31 +02:00
{
debugImage.SaveAsPng(stream);
}
i += 1;
}
}
2019-08-28 12:08:10 +02:00
image.Dispose();
using (var stream = _resourceCache.UserData.Create(ParallaxConfigOld))
using (var writer = new StreamWriter(stream, EncodingHelpers.UTF8))
2018-11-30 21:54:30 +01:00
{
2018-12-03 16:55:00 +01:00
writer.Write(contents);
2018-11-30 21:54:30 +01:00
}
OnTextureLoaded?.Invoke(ParallaxTexture);
}
}
}