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;
|
2020-11-07 01:15:56 +01:00
|
|
|
using Content.Shared;
|
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
|
|
|
|
|
|
|
|
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
|
|
|
{
|
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.
|
2020-11-27 11:00:49 +01:00
|
|
|
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
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-07 01:15:56 +01:00
|
|
|
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)
|
|
|
|
|
{
|
2019-04-05 02:04:09 +02:00
|
|
|
using (var reader = new StreamReader(configStream, EncodingHelpers.UTF8))
|
2018-12-03 16:55:00 +01:00
|
|
|
{
|
2021-01-31 00:08:34 +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
|
|
|
{
|
2020-08-16 01:12:30 +02:00
|
|
|
var match = _resourceCache.UserData.ReadAllText(ParallaxConfigOld) == contents;
|
2018-11-30 21:54:30 +01:00
|
|
|
|
|
|
|
|
if (match)
|
|
|
|
|
{
|
2020-08-16 01:12:30 +02:00
|
|
|
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
|
|
|
|
2021-03-10 14:48:29 +01: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
|
|
|
}
|
|
|
|
|
|
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.
|
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.
|
2020-08-16 01:12:30 +02:00
|
|
|
using (var stream = _resourceCache.UserData.Create(ParallaxPath))
|
2018-11-30 21:54:30 +01:00
|
|
|
{
|
|
|
|
|
image.SaveAsPng(stream);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
if (debugParallax && debugImages != null)
|
2019-08-29 14:36:31 +02:00
|
|
|
{
|
|
|
|
|
var i = 0;
|
|
|
|
|
foreach (var debugImage in debugImages)
|
|
|
|
|
{
|
2020-08-16 01:12:30 +02:00
|
|
|
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();
|
|
|
|
|
|
2020-08-16 01:12:30 +02:00
|
|
|
using (var stream = _resourceCache.UserData.Create(ParallaxConfigOld))
|
2019-04-05 02:04:09 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|