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

@@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using JetBrains.Annotations;
using Nett;
using Content.Shared;
using Content.Shared.CCVar;
using Content.Client.Resources;
using Content.Client.IoC;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Utility;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
namespace Content.Client.Parallax.Data;
[UsedImplicitly]
[DataDefinition]
public sealed class GeneratedParallaxTextureSource : IParallaxTextureSource
{
/// <summary>
/// Parallax config path (the TOML file).
/// In client resources.
/// </summary>
[DataField("configPath")]
public ResourcePath ParallaxConfigPath { get; } = new("/parallax_config.toml");
/// <summary>
/// ID for debugging, caching, and so forth.
/// The empty string here is reserved for the original parallax.
/// It is advisible to provide a roughly unique ID for any unique config contents.
/// </summary>
[DataField("id")]
public string Identifier { get; } = "other";
/// <summary>
/// Cached path.
/// In user directory.
/// </summary>
private ResourcePath ParallaxCachedImagePath => new($"/parallax_{Identifier}cache.png");
/// <summary>
/// Old parallax config path (for checking for parallax updates).
/// In user directory.
/// </summary>
private ResourcePath PreviousParallaxConfigPath => new($"/parallax_{Identifier}config_old");
async Task<Texture> IParallaxTextureSource.GenerateTexture(CancellationToken cancel = default)
{
var parallaxConfig = GetParallaxConfig();
if (parallaxConfig == null)
{
Logger.ErrorS("parallax", $"Parallax config not found or unreadable: {ParallaxConfigPath}");
// The show must go on.
return Texture.Transparent;
}
var debugParallax = IoCManager.Resolve<IConfigurationManager>().GetCVar(CCVars.ParallaxDebug);
if (debugParallax
|| !StaticIoC.ResC.UserData.TryReadAllText(PreviousParallaxConfigPath, out var previousParallaxConfig)
|| previousParallaxConfig != parallaxConfig)
{
var table = Toml.ReadString(parallaxConfig);
await UpdateCachedTexture(table, debugParallax, cancel);
//Update the previous config
using var writer = StaticIoC.ResC.UserData.OpenWriteText(PreviousParallaxConfigPath);
writer.Write(parallaxConfig);
}
try
{
return GetCachedTexture();
}
catch (Exception ex)
{
Logger.ErrorS("parallax", $"Couldn't retrieve parallax cached texture: {ex}");
// The show must go on.
try
{
// Also try to at least sort of fix this if we've been fooled by a config backup
StaticIoC.ResC.UserData.Delete(PreviousParallaxConfigPath);
}
catch (Exception)
{
}
return Texture.Transparent;
}
}
private async Task UpdateCachedTexture(TomlTable config, bool saveDebugLayers, CancellationToken cancel = default)
{
var debugImages = saveDebugLayers ? new List<Image<Rgba32>>() : null;
var sawmill = IoCManager.Resolve<ILogManager>().GetSawmill("parallax");
// Generate the parallax in the thread pool.
using var newParallexImage = await Task.Run(() =>
ParallaxGenerator.GenerateParallax(config, new Size(1920, 1080), sawmill, debugImages, cancel), cancel);
// And load it in the main thread for safety reasons.
// But before spending time saving it, make sure to exit out early if it's not wanted.
cancel.ThrowIfCancellationRequested();
// Store it and CRC so further game starts don't need to regenerate it.
using var imageStream = StaticIoC.ResC.UserData.OpenWrite(ParallaxCachedImagePath);
newParallexImage.SaveAsPng(imageStream);
if (saveDebugLayers)
{
for (var i = 0; i < debugImages!.Count; i++)
{
var debugImage = debugImages[i];
using var debugImageStream = StaticIoC.ResC.UserData.OpenWrite(new ResourcePath($"/parallax_{Identifier}debug_{i}.png"));
debugImage.SaveAsPng(debugImageStream);
}
}
}
private Texture GetCachedTexture()
{
using var imageStream = StaticIoC.ResC.UserData.OpenRead(ParallaxCachedImagePath);
return Texture.LoadFromPNGStream(imageStream, "Parallax");
}
private string? GetParallaxConfig()
{
if (!StaticIoC.ResC.TryContentFileRead(ParallaxConfigPath, out var configStream))
{
return null;
}
using var configReader = new StreamReader(configStream, EncodingHelpers.UTF8);
return configReader.ReadToEnd().Replace(Environment.NewLine, "\n");
}
}

View File

@@ -0,0 +1,19 @@
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Client.Graphics;
namespace Content.Client.Parallax.Data
{
[ImplicitDataDefinitionForInheritors]
public interface IParallaxTextureSource
{
/// <summary>
/// Generates or loads the texture.
/// Note that this should be cached, but not necessarily *here*.
/// </summary>
Task<Texture> GenerateTexture(CancellationToken cancel = default);
}
}

View File

@@ -0,0 +1,29 @@
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using JetBrains.Annotations;
using Content.Client.Resources;
using Content.Client.IoC;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
namespace Content.Client.Parallax.Data;
[UsedImplicitly]
[DataDefinition]
public sealed class ImageParallaxTextureSource : IParallaxTextureSource
{
/// <summary>
/// Texture path.
/// </summary>
[DataField("path", required: true)]
public ResourcePath Path { get; } = default!;
async Task<Texture> IParallaxTextureSource.GenerateTexture(CancellationToken cancel = default)
{
return StaticIoC.ResC.GetTexture(Path);
}
}

View File

@@ -0,0 +1,66 @@
using System;
using Robust.Client.Graphics;
using Content.Client.Parallax.Data;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Client.Parallax.Data;
/// <summary>
/// The configuration for a parallax layer.
/// </summary>
[DataDefinition]
public sealed class ParallaxLayerConfig
{
/// <summary>
/// The texture source for this layer.
/// </summary>
[DataField("texture", required: true)]
public IParallaxTextureSource Texture { get; set; } = default!;
/// <summary>
/// A scaling factor for the texture.
/// In the interest of simplifying maths, this is rounded down to integer for ParallaxControl, so be careful.
/// </summary>
[DataField("scale")]
public Vector2 Scale { get; set; } = Vector2.One;
/// <summary>
/// If true, this layer is tiled as the camera scrolls around.
/// If false, this layer only shows up around it's home position.
/// </summary>
[DataField("tiled")]
public bool Tiled { get; set; } = true;
/// <summary>
/// A position relative to the centre of a ParallaxControl that this parallax should be drawn at, in pixels.
/// Used for menus.
/// Note that this is ignored if the parallax layer is tiled - in that event a random pixel offset is used and slowness is applied.
/// </summary>
[DataField("controlHomePosition")]
public Vector2 ControlHomePosition { get; set; }
/// <summary>
/// The "relative to ParallaxAnchor" starting world position for this layer.
/// Essentially, an unclamped lerp occurs between here and the eye position, with Slowness as the factor.
/// Used for in-game.
/// </summary>
[DataField("worldHomePosition")]
public Vector2 WorldHomePosition { get; set; }
/// <summary>
/// An adjustment performed to the world position of this layer after parallax shifting.
/// Used for in-game.
/// Useful for moving around Slowness = 1.0 objects (which can't otherwise be moved from screen centre).
/// </summary>
[DataField("worldAdjustPosition")]
public Vector2 WorldAdjustPosition { get; set; }
/// <summary>
/// Multiplier of parallax shift.
/// A slowness of 0.0f anchors this layer to the world.
/// A slowness of 1.0f anchors this layer to the camera.
/// </summary>
[DataField("slowness")]
public float Slowness { get; set; } = 0.5f;
}

View File

@@ -0,0 +1,37 @@
using System.Collections.Generic;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
using Robust.Shared.Utility;
namespace Content.Client.Parallax.Data;
/// <summary>
/// Prototype data for a parallax.
/// </summary>
[Prototype("parallax")]
public sealed class ParallaxPrototype : IPrototype
{
/// <inheritdoc/>
[IdDataFieldAttribute]
public string ID { get; } = default!;
/// <summary>
/// Parallax layers.
/// </summary>
[DataField("layers")]
public List<ParallaxLayerConfig> Layers { get; } = new();
/// <summary>
/// Parallax layers, low-quality.
/// </summary>
[DataField("layersLQ")]
public List<ParallaxLayerConfig> LayersLQ { get; } = new();
/// <summary>
/// If low-quality layers don't exist for this parallax and high-quality should be used instead.
/// </summary>
[DataField("layersLQUseHQ")]
public bool LayersLQUseHQ { get; } = true;
}