actually start to bring structure into the project

make it resilent against restarts/reloads
remove all user interaction for resource gathering
compute hashes on first time file resolving and on updates of said file on resolving
This commit is contained in:
Stanley Dimant
2022-06-14 21:53:41 +02:00
parent c0de781421
commit da2b2701e8
12 changed files with 379 additions and 232 deletions

View File

@@ -6,6 +6,8 @@ using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using MareSynchronos.FileCacheDB;
using System.IO;
using MareSynchronos.Utils;
namespace MareSynchronos.Models
{
@@ -24,6 +26,9 @@ namespace MareSynchronos.Models
[JsonProperty]
public string ImcData { get; set; } = string.Empty;
public bool HasFileReplacement => GamePath != ResolvedPath;
public bool Computed => (computationTask == null || (computationTask?.IsCompleted ?? true)) && Associated.All(f => f.Computed);
private Task? computationTask = null;
public FileReplacement(string gamePath, string penumbraDirectory)
{
GamePath = gamePath;
@@ -50,15 +55,58 @@ namespace MareSynchronos.Models
ResolvedPath = path.ToLower().Replace('/', '\\').Replace(penumbraDirectory, "").Replace('\\', '/');
if (!HasFileReplacement) return;
Task.Run(() =>
computationTask = Task.Run(() =>
{
using FileCacheContext db = new FileCacheContext();
var fileCache = db.FileCaches.SingleOrDefault(f => f.Filepath == path.ToLower());
FileCache? fileCache;
using (FileCacheContext db = new())
{
fileCache = db.FileCaches.SingleOrDefault(f => f.Filepath == path.ToLower());
}
if (fileCache != null)
Hash = fileCache.Hash;
{
FileInfo fi = new(fileCache.Filepath);
if (fi.LastWriteTimeUtc.Ticks == long.Parse(fileCache.LastModifiedDate))
{
Hash = fileCache.Hash;
}
else
{
Hash = ComputeHash(fi);
using var db = new FileCacheContext();
var newTempCache = db.FileCaches.Single(f => f.Filepath == path.ToLower());
newTempCache.Hash = Hash;
db.Update(newTempCache);
db.SaveChanges();
}
}
else
{
Hash = ComputeHash(new FileInfo(path));
}
});
}
private string ComputeHash(FileInfo fi)
{
// compute hash if hash is not present
string hash = Crypto.GetFileHash(fi.FullName);
using FileCacheContext db = new();
var fileAddedDuringCompute = db.FileCaches.SingleOrDefault(f => f.Filepath == fi.FullName.ToLower());
if (fileAddedDuringCompute != null) return fileAddedDuringCompute.Hash;
db.Add(new FileCache()
{
Hash = hash,
Filepath = fi.FullName.ToLower(),
LastModifiedDate = fi.LastWriteTimeUtc.Ticks.ToString()
});
db.SaveChanges();
return hash;
}
public bool IsReplacedByThis(string path)
{
return GamePath.ToLower() == path.ToLower() || ResolvedPath.ToLower() == path.ToLower();
@@ -83,5 +131,16 @@ namespace MareSynchronos.Models
}
return builder.ToString();
}
public override bool Equals(object? obj)
{
if (obj == null) return true;
if (obj.GetType() == typeof(FileReplacement))
{
return Hash == ((FileReplacement)obj).Hash && GamePath == ((FileReplacement)obj).GamePath;
}
return base.Equals(obj);
}
}
}