分离玩家信息插件,以及更新检测

This commit is contained in:
2021-10-22 08:54:04 +02:00
parent 2a96f8efac
commit 08700abd4f
25 changed files with 860 additions and 234 deletions

View File

@@ -2,18 +2,20 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using static System.Text.Json.JsonSerializer;
namespace AnotherReplayReader
{
internal sealed class Cache
public sealed class Cache
{
public static string CacheDirectory => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "RA3Bar.Lanyi.AnotherReplayReader");
public static string OldCacheDirectory => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "RA3Bar.Lanyi.AnotherReplayReader");
public static string CacheDirectory => App.LibsFolder;
public static string CacheFilePath => Path.Combine(CacheDirectory, "AnotherReplayReader.cache");
private readonly ConcurrentDictionary<string, string> _storage = new();
public Task Initialization { get; }
public Cache()
@@ -27,6 +29,28 @@ namespace AnotherReplayReader
Directory.CreateDirectory(CacheDirectory);
}
try
{
var old = new DirectoryInfo(OldCacheDirectory);
if (old.Exists)
{
foreach (var file in old.EnumerateFiles())
{
try
{
file.MoveTo(Path.Combine(CacheDirectory, file.Name));
}
catch { }
}
try
{
old.Delete();
}
catch { }
}
}
catch { }
using var cacheStream = File.OpenRead(CacheFilePath);
var futureCache = DeserializeAsync<Dictionary<string, string>>(cacheStream).ConfigureAwait(false);
if (await futureCache is not { } cached)
@@ -42,7 +66,7 @@ namespace AnotherReplayReader
});
}
public T GetOrDefault<T>(string key, in T defaultValue)
public T GetOrDefault<T>(string key, T defaultValue)
{
try
{
@@ -55,12 +79,11 @@ namespace AnotherReplayReader
return defaultValue;
}
public void Set<T>(string key, in T value)
public void Set<T>(string key, T value)
{
try
{
_storage[key] = Serialize(value);
Save();
}
catch { }
}
@@ -73,7 +96,6 @@ namespace AnotherReplayReader
{
_storage[key] = Serialize(value);
}
Save();
}
catch { }
}
@@ -81,10 +103,9 @@ namespace AnotherReplayReader
public void Remove(string key)
{
_storage.TryRemove(key, out _);
Save();
}
public async void Save()
public async Task Save()
{
try
{