diff --git a/Content.Client/Changelog/ChangelogManager.cs b/Content.Client/Changelog/ChangelogManager.cs index ef678af489..51f41d718f 100644 --- a/Content.Client/Changelog/ChangelogManager.cs +++ b/Content.Client/Changelog/ChangelogManager.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -43,8 +43,7 @@ namespace Content.Client.Changelog NewChangelogEntries = false; NewChangelogEntriesChanged?.Invoke(); - using var file = _resource.UserData.Create(new ResourcePath($"/changelog_last_seen_{_configManager.GetCVar(CCVars.ServerId)}")); - using var sw = new StreamWriter(file); + using var sw = _resource.UserData.OpenWriteText(new ResourcePath($"/changelog_last_seen_{_configManager.GetCVar(CCVars.ServerId)}")); sw.Write(MaxId.ToString()); } @@ -62,9 +61,9 @@ namespace Content.Client.Changelog MaxId = changelog.Max(c => c.Id); var path = new ResourcePath($"/changelog_last_seen_{_configManager.GetCVar(CCVars.ServerId)}"); - if (_resource.UserData.Exists(path)) + if(_resource.UserData.TryReadAllText(path, out var lastReadIdText)) { - LastReadId = int.Parse(_resource.UserData.ReadAllText(path)); + LastReadId = int.Parse(lastReadIdText); } NewChangelogEntries = LastReadId < MaxId; diff --git a/Content.Client/Info/RulesManager.cs b/Content.Client/Info/RulesManager.cs index 0dc6a95b11..a1eece5434 100644 --- a/Content.Client/Info/RulesManager.cs +++ b/Content.Client/Info/RulesManager.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Globalization; using System.IO; using Content.Client.HUD; @@ -21,14 +21,14 @@ public sealed class RulesManager private void OnConnectStateChanged(ClientConnectionState state) { + if (state != ClientConnectionState.Connected) return; var path = new ResourcePath($"/rules_last_seen_{_configManager.GetCVar(CCVars.ServerId)}"); var showRules = true; - if (_resource.UserData.Exists(path) - && DateTime.TryParse(_resource.UserData.ReadAllText(path), null, DateTimeStyles.AssumeUniversal, - out var lastReadTime)) + if (_resource.UserData.TryReadAllText(path, out var lastReadTimeText) + && DateTime.TryParse(lastReadTimeText, null, DateTimeStyles.AssumeUniversal, out var lastReadTime)) showRules = lastReadTime < DateTime.UtcNow - TimeSpan.FromDays(60); else SaveLastReadTime(); @@ -42,8 +42,7 @@ public sealed class RulesManager /// public void SaveLastReadTime() { - using var file = _resource.UserData.Create(new ResourcePath($"/rules_last_seen_{_configManager.GetCVar(CCVars.ServerId)}")); - using var sw = new StreamWriter(file); + using var sw = _resource.UserData.OpenWriteText(new ResourcePath($"/rules_last_seen_{_configManager.GetCVar(CCVars.ServerId)}")); sw.Write(DateTime.UtcNow.ToUniversalTime()); } diff --git a/Content.Client/Parallax/Managers/ParallaxManager.cs b/Content.Client/Parallax/Managers/ParallaxManager.cs index f69f45f906..d3d7d07966 100644 --- a/Content.Client/Parallax/Managers/ParallaxManager.cs +++ b/Content.Client/Parallax/Managers/ParallaxManager.cs @@ -26,8 +26,8 @@ namespace Content.Client.Parallax.Managers private static readonly ResourcePath ParallaxConfigPath = new("/parallax_config.toml"); // 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"); + private static readonly ResourcePath ParallaxCachedImagePath = new("/parallax_cache.png"); + private static readonly ResourcePath PreviousParallaxConfigPath = new("/parallax_config_old"); public event Action? OnTextureLoaded; public Texture? ParallaxTexture { get; private set; } @@ -35,88 +35,71 @@ namespace Content.Client.Parallax.Managers public async void LoadParallax() { if (!_configurationManager.GetCVar(CCVars.ParallaxEnabled)) - { return; - } + + var parallaxConfig = GetParallaxConfig(); + if (parallaxConfig == null) + return; var debugParallax = _configurationManager.GetCVar(CCVars.ParallaxDebug); - string contents; - TomlTable table; - // Load normal config into memory - if (!_resourceCache.TryContentFileRead(ParallaxConfigPath, out var configStream)) + + if (debugParallax + || !_resourceCache.UserData.TryReadAllText(PreviousParallaxConfigPath, out var previousParallaxConfig) + || previousParallaxConfig != parallaxConfig) { - Logger.ErrorS("parallax", "Parallax config not found."); - return; + var table = Toml.ReadString(parallaxConfig); + await UpdateCachedTexture(table, debugParallax); + + //Update the previous config + using var writer = _resourceCache.UserData.OpenWriteText(PreviousParallaxConfigPath); + writer.Write(parallaxConfig); } - using (configStream) - { - using (var reader = new StreamReader(configStream, EncodingHelpers.UTF8)) - { - contents = reader.ReadToEnd().Replace(Environment.NewLine, "\n"); - } + ParallaxTexture = GetCachedTexture(); + OnTextureLoaded?.Invoke(ParallaxTexture); + } - if (!debugParallax && _resourceCache.UserData.Exists(ParallaxConfigOld)) - { - var match = _resourceCache.UserData.ReadAllText(ParallaxConfigOld) == contents; - - if (match) - { - using (var stream = _resourceCache.UserData.OpenRead(ParallaxPath)) - { - ParallaxTexture = Texture.LoadFromPNGStream(stream, "Parallax"); - } - - OnTextureLoaded?.Invoke(ParallaxTexture); - return; - } - } - - table = Toml.ReadString(contents); - } - - List>? debugImages = null; - if (debugParallax) - { - debugImages = new List>(); - } + private async Task UpdateCachedTexture(TomlTable config, bool saveDebugLayers) + { + var debugImages = saveDebugLayers ? new List>() : null; var sawmill = _logManager.GetSawmill("parallax"); // Generate the parallax in the thread pool. - var image = await Task.Run(() => - ParallaxGenerator.GenerateParallax(table, new Size(1920, 1080), sawmill, debugImages)); + using var newParallexImage = await Task.Run(() => + ParallaxGenerator.GenerateParallax(config, new Size(1920, 1080), sawmill, debugImages)); // And load it in the main thread for safety reasons. - ParallaxTexture = Texture.LoadFromImage(image, "Parallax"); // Store it and CRC so further game starts don't need to regenerate it. - using (var stream = _resourceCache.UserData.Create(ParallaxPath)) - { - image.SaveAsPng(stream); - } + using var imageStream = _resourceCache.UserData.OpenWrite(ParallaxCachedImagePath); + newParallexImage.SaveAsPng(imageStream); - if (debugParallax && debugImages != null) + if (saveDebugLayers) { - var i = 0; - foreach (var debugImage in debugImages) + for (var i = 0; i < debugImages!.Count; i++) { - using (var stream = _resourceCache.UserData.Create(new ResourcePath($"/parallax_debug_{i}.png"))) - { - debugImage.SaveAsPng(stream); - } - - i += 1; + var debugImage = debugImages[i]; + using var debugImageStream = _resourceCache.UserData.OpenWrite(new ResourcePath($"/parallax_debug_{i}.png")); + debugImage.SaveAsPng(debugImageStream); } } + } - image.Dispose(); + private Texture GetCachedTexture() + { + using var imageStream = _resourceCache.UserData.OpenRead(ParallaxCachedImagePath); + return Texture.LoadFromPNGStream(imageStream, "Parallax"); + } - using (var stream = _resourceCache.UserData.Create(ParallaxConfigOld)) - using (var writer = new StreamWriter(stream, EncodingHelpers.UTF8)) + private string? GetParallaxConfig() + { + if (!_resourceCache.TryContentFileRead(ParallaxConfigPath, out var configStream)) { - writer.Write(contents); + Logger.ErrorS("parallax", "Parallax config not found."); + return null; } - OnTextureLoaded?.Invoke(ParallaxTexture); + using var configReader = new StreamReader(configStream, EncodingHelpers.UTF8); + return configReader.ReadToEnd().Replace(Environment.NewLine, "\n"); } } }