I should actually be working
This commit is contained in:
89
MareSynchronos/Models/CharacterCache.cs
Normal file
89
MareSynchronos/Models/CharacterCache.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using Dalamud.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MareSynchronos.Models
|
||||
{
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
public class CharacterCache
|
||||
{
|
||||
public List<FileReplacement> FileReplacements { get; set; } = new List<FileReplacement>();
|
||||
|
||||
[JsonProperty]
|
||||
public List<FileReplacement> AllReplacements =>
|
||||
FileReplacements.Where(x => x.HasFileReplacement)
|
||||
.Concat(FileReplacements.SelectMany(f => f.Associated).Where(f => f.HasFileReplacement))
|
||||
.Concat(FileReplacements.SelectMany(f => f.Associated).SelectMany(f => f.Associated).Where(f => f.HasFileReplacement))
|
||||
.ToList();
|
||||
|
||||
public CharacterCache()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Invalidate(List<FileReplacement>? fileReplacements = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fileReplacement = fileReplacements ?? FileReplacements.ToList();
|
||||
foreach (var item in fileReplacement)
|
||||
{
|
||||
item.IsInUse = false;
|
||||
Invalidate(item.Associated);
|
||||
if (FileReplacements.Contains(item))
|
||||
{
|
||||
FileReplacements.Remove(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PluginLog.Debug(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddAssociatedResource(FileReplacement resource, FileReplacement mdlParent, FileReplacement mtrlParent)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (resource == null) return;
|
||||
if (mdlParent == null)
|
||||
{
|
||||
resource.IsInUse = true;
|
||||
FileReplacements.Add(resource);
|
||||
return;
|
||||
}
|
||||
|
||||
FileReplacement replacement;
|
||||
|
||||
if (mtrlParent == null && (replacement = FileReplacements.SingleOrDefault(f => f == mdlParent)!) != null)
|
||||
{
|
||||
replacement.AddAssociated(resource);
|
||||
}
|
||||
|
||||
if ((replacement = FileReplacements.SingleOrDefault(f => f == mdlParent)?.Associated.SingleOrDefault(f => f == mtrlParent)!) != null)
|
||||
{
|
||||
replacement.AddAssociated(resource);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PluginLog.Debug(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
foreach (var fileReplacement in FileReplacements.OrderBy(a => a.GamePath))
|
||||
{
|
||||
stringBuilder.AppendLine(fileReplacement.ToString());
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,29 @@
|
||||
using System;
|
||||
using Dalamud.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using MareSynchronos.FileCacheDB;
|
||||
|
||||
namespace MareSynchronos.Models
|
||||
{
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
public class FileReplacement
|
||||
{
|
||||
private readonly string penumbraDirectory;
|
||||
|
||||
[JsonProperty]
|
||||
public string GamePath { get; private set; }
|
||||
public string ReplacedPath { get; private set; } = string.Empty;
|
||||
|
||||
public string ResolvedPath { get; private set; } = string.Empty;
|
||||
[JsonProperty]
|
||||
public string Hash { get; set; } = string.Empty;
|
||||
public bool IsInUse { get; set; } = false;
|
||||
public List<FileReplacement> Associated { get; set; } = new List<FileReplacement>();
|
||||
|
||||
public bool HasFileReplacement => GamePath != ReplacedPath;
|
||||
[JsonProperty]
|
||||
public string ImcData { get; set; } = string.Empty;
|
||||
public bool HasFileReplacement => GamePath != ResolvedPath;
|
||||
public FileReplacement(string gamePath, string penumbraDirectory)
|
||||
{
|
||||
GamePath = gamePath;
|
||||
@@ -24,6 +32,8 @@ namespace MareSynchronos.Models
|
||||
|
||||
public void AddAssociated(FileReplacement fileReplacement)
|
||||
{
|
||||
fileReplacement.IsInUse = true;
|
||||
|
||||
if (!Associated.Any(a => a.IsReplacedByThis(fileReplacement)))
|
||||
{
|
||||
Associated.Add(fileReplacement);
|
||||
@@ -35,31 +45,40 @@ namespace MareSynchronos.Models
|
||||
GamePath = path;
|
||||
}
|
||||
|
||||
public void SetReplacedPath(string path)
|
||||
public void SetResolvedPath(string path)
|
||||
{
|
||||
ReplacedPath = path.ToLower().Replace('/', '\\').Replace(penumbraDirectory, "").Replace('\\', '/');
|
||||
ResolvedPath = path.ToLower().Replace('/', '\\').Replace(penumbraDirectory, "").Replace('\\', '/');
|
||||
if (!HasFileReplacement) return;
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
using FileCacheContext db = new FileCacheContext();
|
||||
var fileCache = db.FileCaches.SingleOrDefault(f => f.Filepath == path.ToLower());
|
||||
if (fileCache != null)
|
||||
Hash = fileCache.Hash;
|
||||
});
|
||||
}
|
||||
|
||||
public bool IsReplacedByThis(string path)
|
||||
{
|
||||
return GamePath.ToLower() == path.ToLower() || ReplacedPath.ToLower() == path.ToLower();
|
||||
return GamePath.ToLower() == path.ToLower() || ResolvedPath.ToLower() == path.ToLower();
|
||||
}
|
||||
|
||||
public bool IsReplacedByThis(FileReplacement replacement)
|
||||
{
|
||||
return IsReplacedByThis(replacement.GamePath) || IsReplacedByThis(replacement.ReplacedPath);
|
||||
return IsReplacedByThis(replacement.GamePath) || IsReplacedByThis(replacement.ResolvedPath);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.AppendLine($"Modded: {HasFileReplacement} - {GamePath} => {ReplacedPath}");
|
||||
builder.AppendLine($"Modded: {HasFileReplacement} - {GamePath} => {ResolvedPath}");
|
||||
foreach (var l1 in Associated)
|
||||
{
|
||||
builder.AppendLine($" + Modded: {l1.HasFileReplacement} - {l1.GamePath} => {l1.ReplacedPath}");
|
||||
builder.AppendLine($" + Modded: {l1.HasFileReplacement} - {l1.GamePath} => {l1.ResolvedPath}");
|
||||
foreach (var l2 in l1.Associated)
|
||||
{
|
||||
builder.AppendLine($" + Modded: {l2.HasFileReplacement} - {l2.GamePath} => {l2.ReplacedPath}");
|
||||
builder.AppendLine($" + Modded: {l2.HasFileReplacement} - {l2.GamePath} => {l2.ResolvedPath}");
|
||||
}
|
||||
}
|
||||
return builder.ToString();
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
using Dalamud.Game.ClientState;
|
||||
using Dalamud.Plugin;
|
||||
using Dalamud.Plugin.Ipc;
|
||||
|
||||
namespace MareSynchronos.Models
|
||||
{
|
||||
public class FileReplacementFactory
|
||||
{
|
||||
private readonly ClientState clientState;
|
||||
private ICallGateSubscriber<string, string, string> resolvePath;
|
||||
private string penumbraDirectory;
|
||||
|
||||
public FileReplacementFactory(DalamudPluginInterface pluginInterface, ClientState clientState)
|
||||
{
|
||||
resolvePath = pluginInterface.GetIpcSubscriber<string, string, string>("Penumbra.ResolveCharacterPath");
|
||||
penumbraDirectory = pluginInterface.GetIpcSubscriber<string>("Penumbra.GetModDirectory").InvokeFunc().ToLower() + '\\';
|
||||
this.clientState = clientState;
|
||||
}
|
||||
public FileReplacement Create(string gamePath)
|
||||
{
|
||||
var fileReplacement = new FileReplacement(gamePath, penumbraDirectory);
|
||||
fileReplacement.SetReplacedPath(resolvePath.InvokeFunc(gamePath, clientState.LocalPlayer!.Name.ToString()));
|
||||
if (!fileReplacement.HasFileReplacement)
|
||||
{
|
||||
// try to resolve path with -- instead?
|
||||
string[] tempGamePath = gamePath.Split('/');
|
||||
tempGamePath[tempGamePath.Length - 1] = "--" + tempGamePath[tempGamePath.Length - 1];
|
||||
string newTempGamePath = string.Join('/', tempGamePath);
|
||||
var resolvedPath = resolvePath.InvokeFunc(newTempGamePath, clientState.LocalPlayer!.Name.ToString());
|
||||
if (resolvedPath != newTempGamePath)
|
||||
{
|
||||
fileReplacement.SetReplacedPath(resolvedPath);
|
||||
fileReplacement.SetGamePath(newTempGamePath);
|
||||
}
|
||||
}
|
||||
return fileReplacement;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user