Use IWritableDirProvider helpers (#5739)

This commit is contained in:
wrexbe
2021-12-12 18:26:22 -08:00
committed by GitHub
parent b2da936848
commit 5afd68e33c
3 changed files with 53 additions and 72 deletions

View File

@@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
@@ -43,8 +43,7 @@ namespace Content.Client.Changelog
NewChangelogEntries = false; NewChangelogEntries = false;
NewChangelogEntriesChanged?.Invoke(); NewChangelogEntriesChanged?.Invoke();
using var file = _resource.UserData.Create(new ResourcePath($"/changelog_last_seen_{_configManager.GetCVar(CCVars.ServerId)}")); using var sw = _resource.UserData.OpenWriteText(new ResourcePath($"/changelog_last_seen_{_configManager.GetCVar(CCVars.ServerId)}"));
using var sw = new StreamWriter(file);
sw.Write(MaxId.ToString()); sw.Write(MaxId.ToString());
} }
@@ -62,9 +61,9 @@ namespace Content.Client.Changelog
MaxId = changelog.Max(c => c.Id); MaxId = changelog.Max(c => c.Id);
var path = new ResourcePath($"/changelog_last_seen_{_configManager.GetCVar(CCVars.ServerId)}"); 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; NewChangelogEntries = LastReadId < MaxId;

View File

@@ -1,4 +1,4 @@
using System; using System;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using Content.Client.HUD; using Content.Client.HUD;
@@ -21,14 +21,14 @@ public sealed class RulesManager
private void OnConnectStateChanged(ClientConnectionState state) private void OnConnectStateChanged(ClientConnectionState state)
{ {
if (state != ClientConnectionState.Connected) if (state != ClientConnectionState.Connected)
return; return;
var path = new ResourcePath($"/rules_last_seen_{_configManager.GetCVar(CCVars.ServerId)}"); var path = new ResourcePath($"/rules_last_seen_{_configManager.GetCVar(CCVars.ServerId)}");
var showRules = true; var showRules = true;
if (_resource.UserData.Exists(path) if (_resource.UserData.TryReadAllText(path, out var lastReadTimeText)
&& DateTime.TryParse(_resource.UserData.ReadAllText(path), null, DateTimeStyles.AssumeUniversal, && DateTime.TryParse(lastReadTimeText, null, DateTimeStyles.AssumeUniversal, out var lastReadTime))
out var lastReadTime))
showRules = lastReadTime < DateTime.UtcNow - TimeSpan.FromDays(60); showRules = lastReadTime < DateTime.UtcNow - TimeSpan.FromDays(60);
else else
SaveLastReadTime(); SaveLastReadTime();
@@ -42,8 +42,7 @@ public sealed class RulesManager
/// </summary> /// </summary>
public void SaveLastReadTime() public void SaveLastReadTime()
{ {
using var file = _resource.UserData.Create(new ResourcePath($"/rules_last_seen_{_configManager.GetCVar(CCVars.ServerId)}")); using var sw = _resource.UserData.OpenWriteText(new ResourcePath($"/rules_last_seen_{_configManager.GetCVar(CCVars.ServerId)}"));
using var sw = new StreamWriter(file);
sw.Write(DateTime.UtcNow.ToUniversalTime()); sw.Write(DateTime.UtcNow.ToUniversalTime());
} }

View File

@@ -26,8 +26,8 @@ namespace Content.Client.Parallax.Managers
private static readonly ResourcePath ParallaxConfigPath = new("/parallax_config.toml"); private static readonly ResourcePath ParallaxConfigPath = new("/parallax_config.toml");
// Both of these below are in the user directory. // Both of these below are in the user directory.
private static readonly ResourcePath ParallaxPath = new("/parallax_cache.png"); private static readonly ResourcePath ParallaxCachedImagePath = new("/parallax_cache.png");
private static readonly ResourcePath ParallaxConfigOld = new("/parallax_config_old"); private static readonly ResourcePath PreviousParallaxConfigPath = new("/parallax_config_old");
public event Action<Texture>? OnTextureLoaded; public event Action<Texture>? OnTextureLoaded;
public Texture? ParallaxTexture { get; private set; } public Texture? ParallaxTexture { get; private set; }
@@ -35,88 +35,71 @@ namespace Content.Client.Parallax.Managers
public async void LoadParallax() public async void LoadParallax()
{ {
if (!_configurationManager.GetCVar(CCVars.ParallaxEnabled)) if (!_configurationManager.GetCVar(CCVars.ParallaxEnabled))
{
return; return;
}
var parallaxConfig = GetParallaxConfig();
if (parallaxConfig == null)
return;
var debugParallax = _configurationManager.GetCVar(CCVars.ParallaxDebug); var debugParallax = _configurationManager.GetCVar(CCVars.ParallaxDebug);
string contents;
TomlTable table; if (debugParallax
// Load normal config into memory || !_resourceCache.UserData.TryReadAllText(PreviousParallaxConfigPath, out var previousParallaxConfig)
if (!_resourceCache.TryContentFileRead(ParallaxConfigPath, out var configStream)) || previousParallaxConfig != parallaxConfig)
{ {
Logger.ErrorS("parallax", "Parallax config not found."); var table = Toml.ReadString(parallaxConfig);
return; await UpdateCachedTexture(table, debugParallax);
}
//Update the previous config
using (configStream) using var writer = _resourceCache.UserData.OpenWriteText(PreviousParallaxConfigPath);
{ writer.Write(parallaxConfig);
using (var reader = new StreamReader(configStream, EncodingHelpers.UTF8))
{
contents = reader.ReadToEnd().Replace(Environment.NewLine, "\n");
}
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");
} }
ParallaxTexture = GetCachedTexture();
OnTextureLoaded?.Invoke(ParallaxTexture); OnTextureLoaded?.Invoke(ParallaxTexture);
return;
}
} }
table = Toml.ReadString(contents); private async Task UpdateCachedTexture(TomlTable config, bool saveDebugLayers)
}
List<Image<Rgba32>>? debugImages = null;
if (debugParallax)
{ {
debugImages = new List<Image<Rgba32>>(); var debugImages = saveDebugLayers ? new List<Image<Rgba32>>() : null;
}
var sawmill = _logManager.GetSawmill("parallax"); var sawmill = _logManager.GetSawmill("parallax");
// Generate the parallax in the thread pool. // Generate the parallax in the thread pool.
var image = await Task.Run(() => using var newParallexImage = await Task.Run(() =>
ParallaxGenerator.GenerateParallax(table, new Size(1920, 1080), sawmill, debugImages)); ParallaxGenerator.GenerateParallax(config, new Size(1920, 1080), sawmill, debugImages));
// And load it in the main thread for safety reasons. // 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. // Store it and CRC so further game starts don't need to regenerate it.
using (var stream = _resourceCache.UserData.Create(ParallaxPath)) using var imageStream = _resourceCache.UserData.OpenWrite(ParallaxCachedImagePath);
{ newParallexImage.SaveAsPng(imageStream);
image.SaveAsPng(stream);
}
if (debugParallax && debugImages != null) if (saveDebugLayers)
{ {
var i = 0; for (var i = 0; i < debugImages!.Count; i++)
foreach (var debugImage in debugImages)
{ {
using (var stream = _resourceCache.UserData.Create(new ResourcePath($"/parallax_debug_{i}.png"))) var debugImage = debugImages[i];
{ using var debugImageStream = _resourceCache.UserData.OpenWrite(new ResourcePath($"/parallax_debug_{i}.png"));
debugImage.SaveAsPng(stream); debugImage.SaveAsPng(debugImageStream);
} }
i += 1;
} }
} }
image.Dispose(); private Texture GetCachedTexture()
using (var stream = _resourceCache.UserData.Create(ParallaxConfigOld))
using (var writer = new StreamWriter(stream, EncodingHelpers.UTF8))
{ {
writer.Write(contents); using var imageStream = _resourceCache.UserData.OpenRead(ParallaxCachedImagePath);
return Texture.LoadFromPNGStream(imageStream, "Parallax");
} }
OnTextureLoaded?.Invoke(ParallaxTexture); private string? GetParallaxConfig()
{
if (!_resourceCache.TryContentFileRead(ParallaxConfigPath, out var configStream))
{
Logger.ErrorS("parallax", "Parallax config not found.");
return null;
}
using var configReader = new StreamReader(configStream, EncodingHelpers.UTF8);
return configReader.ReadToEnd().Replace(Environment.NewLine, "\n");
} }
} }
} }