GeneratedParallaxTextureSource.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.IO;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using JetBrains.Annotations;
  5. using Nett;
  6. using Content.Shared.CCVar;
  7. using Content.Client.IoC;
  8. using Robust.Client.Graphics;
  9. using Robust.Shared.Utility;
  10. using Robust.Shared.Configuration;
  11. using Robust.Shared.ContentPack;
  12. using Robust.Shared.Graphics;
  13. using SixLabors.ImageSharp;
  14. using SixLabors.ImageSharp.PixelFormats;
  15. namespace Content.Client.Parallax.Data;
  16. [UsedImplicitly]
  17. [DataDefinition]
  18. public sealed partial class GeneratedParallaxTextureSource : IParallaxTextureSource
  19. {
  20. /// <summary>
  21. /// Parallax config path (the TOML file).
  22. /// In client resources.
  23. /// </summary>
  24. [DataField("configPath")]
  25. public ResPath ParallaxConfigPath { get; private set; } = new("/parallax_config.toml");
  26. /// <summary>
  27. /// ID for debugging, caching, and so forth.
  28. /// The empty string here is reserved for the original parallax.
  29. /// It is advisible to provide a roughly unique ID for any unique config contents.
  30. /// </summary>
  31. [DataField("id")]
  32. public string Identifier { get; private set; } = "other";
  33. /// <summary>
  34. /// Cached path.
  35. /// In user directory.
  36. /// </summary>
  37. private ResPath ParallaxCachedImagePath => new($"/parallax_{Identifier}cache.png");
  38. /// <summary>
  39. /// Old parallax config path (for checking for parallax updates).
  40. /// In user directory.
  41. /// </summary>
  42. private ResPath PreviousParallaxConfigPath => new($"/parallax_{Identifier}config_old");
  43. async Task<Texture> IParallaxTextureSource.GenerateTexture(CancellationToken cancel)
  44. {
  45. var parallaxConfig = GetParallaxConfig();
  46. if (parallaxConfig == null)
  47. {
  48. Logger.ErrorS("parallax", $"Parallax config not found or unreadable: {ParallaxConfigPath}");
  49. // The show must go on.
  50. return Texture.Transparent;
  51. }
  52. var debugParallax = IoCManager.Resolve<IConfigurationManager>().GetCVar(CCVars.ParallaxDebug);
  53. var resManager = IoCManager.Resolve<IResourceManager>();
  54. if (debugParallax
  55. || !resManager.UserData.TryReadAllText(PreviousParallaxConfigPath, out var previousParallaxConfig)
  56. || previousParallaxConfig != parallaxConfig)
  57. {
  58. var table = Toml.ReadString(parallaxConfig);
  59. await UpdateCachedTexture(table, debugParallax, cancel);
  60. //Update the previous config
  61. using var writer = resManager.UserData.OpenWriteText(PreviousParallaxConfigPath);
  62. writer.Write(parallaxConfig);
  63. }
  64. try
  65. {
  66. return GetCachedTexture();
  67. }
  68. catch (Exception ex)
  69. {
  70. Logger.ErrorS("parallax", $"Couldn't retrieve parallax cached texture: {ex}");
  71. try
  72. {
  73. // Also try to at least sort of fix this if we've been fooled by a config backup
  74. resManager.UserData.Delete(PreviousParallaxConfigPath);
  75. }
  76. catch (Exception)
  77. {
  78. // The show must go on.
  79. }
  80. return Texture.Transparent;
  81. }
  82. }
  83. private async Task UpdateCachedTexture(TomlTable config, bool saveDebugLayers, CancellationToken cancel = default)
  84. {
  85. var debugImages = saveDebugLayers ? new List<Image<Rgba32>>() : null;
  86. var sawmill = IoCManager.Resolve<ILogManager>().GetSawmill("parallax");
  87. // Generate the parallax in the thread pool.
  88. using var newParallexImage = await Task.Run(() =>
  89. ParallaxGenerator.GenerateParallax(config, new Size(1920, 1080), sawmill, debugImages, cancel), cancel);
  90. // And load it in the main thread for safety reasons.
  91. // But before spending time saving it, make sure to exit out early if it's not wanted.
  92. cancel.ThrowIfCancellationRequested();
  93. var resManager = IoCManager.Resolve<IResourceManager>();
  94. // Store it and CRC so further game starts don't need to regenerate it.
  95. await using var imageStream = resManager.UserData.OpenWrite(ParallaxCachedImagePath);
  96. await newParallexImage.SaveAsPngAsync(imageStream, cancel);
  97. if (saveDebugLayers)
  98. {
  99. for (var i = 0; i < debugImages!.Count; i++)
  100. {
  101. var debugImage = debugImages[i];
  102. await using var debugImageStream = resManager.UserData.OpenWrite(new ResPath($"/parallax_{Identifier}debug_{i}.png"));
  103. await debugImage.SaveAsPngAsync(debugImageStream, cancel);
  104. }
  105. }
  106. }
  107. private Texture GetCachedTexture()
  108. {
  109. var resManager = IoCManager.Resolve<IResourceManager>();
  110. using var imageStream = resManager.UserData.OpenRead(ParallaxCachedImagePath);
  111. return Texture.LoadFromPNGStream(imageStream, "Parallax");
  112. }
  113. private string? GetParallaxConfig()
  114. {
  115. var resManager = IoCManager.Resolve<IResourceManager>();
  116. if (!resManager.TryContentFileRead(ParallaxConfigPath, out var configStream))
  117. {
  118. return null;
  119. }
  120. using var configReader = new StreamReader(configStream, EncodingHelpers.UTF8);
  121. return configReader.ReadToEnd().Replace(Environment.NewLine, "\n");
  122. }
  123. }