rebuild PlayerManager to CacheCreationService and optimize creation of the local file cache

This commit is contained in:
rootdarkarchon
2023-02-02 15:25:58 +01:00
parent 86549b2d3f
commit ede62fabae
23 changed files with 461 additions and 737 deletions

View File

@@ -1,79 +1,31 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Text.RegularExpressions;
using MareSynchronos.FileCache;
using MareSynchronos.Managers;
using MareSynchronos.Utils;
using MareSynchronos.API.Data;
namespace MareSynchronos.Models;
public class FileReplacement
{
private readonly FileCacheManager _fileDbManager;
private readonly IpcManager _ipcManager;
public FileReplacement(FileCacheManager fileDbManager, IpcManager ipcManager)
public FileReplacement(List<string> gamePaths, string filePath, FileCacheManager fileDbManager)
{
_fileDbManager = fileDbManager;
_ipcManager = ipcManager;
GamePaths = gamePaths.Select(g => g.Replace('\\', '/')).ToList();
ResolvedPath = filePath.Replace('\\', '/');
HashLazy = new(() => !IsFileSwap ? fileDbManager.GetFileCacheByPath(ResolvedPath)?.Hash ?? string.Empty : string.Empty);
}
public bool Computed => IsFileSwap || !HasFileReplacement || !string.IsNullOrEmpty(Hash);
public List<string> GamePaths { get; set; } = new();
public List<string> GamePaths { get; init; } = new();
public bool HasFileReplacement => GamePaths.Count >= 1 && GamePaths.Any(p => !string.Equals(p, ResolvedPath, StringComparison.Ordinal));
public bool IsFileSwap => !Regex.IsMatch(ResolvedPath, @"^[a-zA-Z]:(/|\\)", RegexOptions.ECMAScript) && !string.Equals(GamePaths[0], ResolvedPath, StringComparison.Ordinal);
public string Hash { get; private set; } = string.Empty;
public string Hash => HashLazy.Value;
public string ResolvedPath { get; set; } = string.Empty;
private Lazy<string> HashLazy;
private void SetResolvedPath(string path)
{
ResolvedPath = path.ToLowerInvariant().Replace('\\', '/');
if (!HasFileReplacement || IsFileSwap) return;
_ = Task.Run(() =>
{
try
{
var cache = _fileDbManager.GetFileCacheByPath(ResolvedPath)!;
Hash = cache.Hash;
}
catch (Exception ex)
{
Logger.Warn("Could not set Hash for " + ResolvedPath + ", resetting to original", ex);
ResolvedPath = GamePaths[0];
}
});
}
public bool Verify()
{
if (!IsFileSwap)
{
var cache = _fileDbManager.GetFileCacheByPath(ResolvedPath);
if (cache == null)
{
Logger.Warn("Replacement Failed verification: " + GamePaths[0]);
return false;
}
Hash = cache.Hash;
return true;
}
ResolvePath(GamePaths[0]);
var success = IsFileSwap;
if (!success)
{
Logger.Warn("FileSwap Failed verification: " + GamePaths[0]);
}
return success;
}
public string ResolvedPath { get; init; } = string.Empty;
public FileReplacementData ToFileReplacementDto()
{
@@ -87,20 +39,6 @@ public class FileReplacement
public override string ToString()
{
StringBuilder builder = new();
builder.AppendLine($"Modded: {HasFileReplacement} - {string.Join(",", GamePaths)} => {ResolvedPath}");
return builder.ToString();
}
internal void ReverseResolvePath(string path)
{
GamePaths = _ipcManager.PenumbraReverseResolvePlayer(path).ToList();
SetResolvedPath(path);
}
internal void ResolvePath(string path)
{
GamePaths = new List<string> { path };
SetResolvedPath(_ipcManager.PenumbraResolvePath(path));
return $"Modded: {HasFileReplacement} - {string.Join(",", GamePaths)} => {ResolvedPath}";
}
}