diff --git a/NoSoliciting/Definitions.cs b/NoSoliciting/Definitions.cs index daf4e40..e05b057 100644 --- a/NoSoliciting/Definitions.cs +++ b/NoSoliciting/Definitions.cs @@ -64,7 +64,7 @@ namespace NoSoliciting { throw new ArgumentNullException(nameof(plugin), "Plugin cannot be null"); } - var pluginFolder = Util.PluginFolder(plugin); + var pluginFolder = plugin.Interface.ConfigDirectory.ToString(); var cachedPath = Path.Combine(pluginFolder, "definitions.yaml"); if (!File.Exists(cachedPath)) { @@ -96,7 +96,7 @@ namespace NoSoliciting { var text = await client.DownloadStringTaskAsync(Url).ConfigureAwait(true); LastError = null; return Tuple.Create(Load(text), text); - } catch (Exception e) when (e is WebException || e is YamlException) { + } catch (Exception e) when (e is WebException or YamlException) { PluginLog.Log("Could not download newest definitions."); PluginLog.Log(e.ToString()); LastError = e.Message; @@ -104,8 +104,8 @@ namespace NoSoliciting { } } - private static async void UpdateCache(IDalamudPlugin plugin, string defs) { - var pluginFolder = Util.PluginFolder(plugin); + private static async void UpdateCache(Plugin plugin, string defs) { + var pluginFolder = plugin.Interface.ConfigDirectory.ToString(); Directory.CreateDirectory(pluginFolder); var cachePath = Path.Combine(pluginFolder, "definitions.yaml"); diff --git a/NoSoliciting/Interface/Settings.cs b/NoSoliciting/Interface/Settings.cs index a385f28..c4f273f 100755 --- a/NoSoliciting/Interface/Settings.cs +++ b/NoSoliciting/Interface/Settings.cs @@ -125,7 +125,7 @@ namespace NoSoliciting.Interface { if (ImGui.Button("Update model")) { // prevent issues when people spam the button - if (ImGui.GetIO().KeyCtrl || this.Plugin.MlStatus == MlFilterStatus.Uninitialised || this.Plugin.MlStatus == MlFilterStatus.Initialised) { + if (ImGui.GetIO().KeyCtrl || this.Plugin.MlStatus is MlFilterStatus.Uninitialised or MlFilterStatus.Initialised) { this.Plugin.MlFilter?.Dispose(); this.Plugin.MlFilter = null; this.Plugin.MlStatus = MlFilterStatus.Uninitialised; diff --git a/NoSoliciting/Ml/MlFilter.cs b/NoSoliciting/Ml/MlFilter.cs index 4f7e6c6..79bd9a2 100644 --- a/NoSoliciting/Ml/MlFilter.cs +++ b/NoSoliciting/Ml/MlFilter.cs @@ -53,28 +53,37 @@ namespace NoSoliciting.Ml { public static async Task Load(Plugin plugin, bool showWindow) { plugin.MlStatus = MlFilterStatus.DownloadingManifest; + // download and parse the remote manifest var manifest = await DownloadManifest(); if (manifest == null) { PluginLog.LogWarning("Could not download manifest. Will attempt to fall back on cached version."); } + // model zip file data byte[]? data = null; + // load the cached manifest var localManifest = LoadCachedManifest(plugin); - if (localManifest != null && (manifest?.Item1 == null || localManifest.Version == manifest.Item1.Version)) { + // if there is a cached manifest and we either couldn't download/parse the remote OR the cached version is the same as remote version + if (localManifest != null && (manifest?.Item1 == null || localManifest.Version == manifest.Value.manifest.Version)) { try { + // try to reach the cached model data = File.ReadAllBytes(CachedFilePath(plugin, ModelName)); - manifest ??= Tuple.Create(localManifest, string.Empty); + // set the manifest to our local one and an empty string for the source + manifest ??= (localManifest, string.Empty); } catch (IOException) { // ignored } } - if (!string.IsNullOrEmpty(manifest?.Item2)) { + // if there is source for the manifest + if (!string.IsNullOrEmpty(manifest?.source)) { plugin.MlStatus = MlFilterStatus.DownloadingModel; - data ??= await DownloadModel(manifest!.Item1!.ModelUrl); + // download the model if necessary + data ??= await DownloadModel(manifest!.Value.manifest!.ModelUrl); } + // give up if we couldn't get any data at this point if (data == null) { plugin.MlStatus = MlFilterStatus.Uninitialised; return null; @@ -82,12 +91,15 @@ namespace NoSoliciting.Ml { plugin.MlStatus = MlFilterStatus.Initialising; - if (!string.IsNullOrEmpty(manifest!.Item2)) { + // if there is source for the manifest + if (!string.IsNullOrEmpty(manifest!.Value.source)) { + // update the cached files UpdateCachedFile(plugin, ModelName, data); - UpdateCachedFile(plugin, ManifestName, Encoding.UTF8.GetBytes(manifest.Item2)); + UpdateCachedFile(plugin, ManifestName, Encoding.UTF8.GetBytes(manifest.Value.source)); } - var pluginFolder = Util.PluginFolder(plugin); + // initialise the classifier + var pluginFolder = plugin.Interface.ConfigDirectory.ToString(); var exePath = await ExtractClassifier(pluginFolder); @@ -97,8 +109,8 @@ namespace NoSoliciting.Ml { var client = await CreateClassifierClient(pipeId, data); return new MlFilter( - manifest.Item1!.Version, - manifest.Item1!.ReportUrl, + manifest.Value.manifest!.Version, + manifest.Value.manifest!.ReportUrl, process!, client ); @@ -158,28 +170,28 @@ namespace NoSoliciting.Ml { } } - private static string CachedFilePath(IDalamudPlugin plugin, string name) { - var pluginFolder = Util.PluginFolder(plugin); + private static string CachedFilePath(Plugin plugin, string name) { + var pluginFolder = plugin.Interface.ConfigDirectory.ToString(); Directory.CreateDirectory(pluginFolder); return Path.Combine(pluginFolder, name); } - private static async void UpdateCachedFile(IDalamudPlugin plugin, string name, byte[] data) { + private static async void UpdateCachedFile(Plugin plugin, string name, byte[] data) { var cachePath = CachedFilePath(plugin, name); - var file = File.OpenWrite(cachePath); + var file = File.Create(cachePath); await file.WriteAsync(data, 0, data.Length); await file.FlushAsync(); file.Dispose(); } - private static async Task?> DownloadManifest() { + private static async Task<(Manifest manifest, string source)?> DownloadManifest() { try { using var client = new WebClient(); var data = await client.DownloadStringTaskAsync(Url); LastError = null; - return Tuple.Create(LoadYaml(data), data); - } catch (Exception e) when (e is WebException || e is YamlException) { + return (LoadYaml(data), data); + } catch (Exception e) when (e is WebException or YamlException) { PluginLog.LogError("Could not download newest model manifest."); PluginLog.LogError(e.ToString()); LastError = e.Message; @@ -187,7 +199,7 @@ namespace NoSoliciting.Ml { } } - private static Manifest? LoadCachedManifest(IDalamudPlugin plugin) { + private static Manifest? LoadCachedManifest(Plugin plugin) { var manifestPath = CachedFilePath(plugin, ManifestName); if (!File.Exists(manifestPath)) { return null; diff --git a/NoSoliciting/Util.cs b/NoSoliciting/Util.cs deleted file mode 100644 index 4171a29..0000000 --- a/NoSoliciting/Util.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.IO; -using Dalamud.Plugin; - -namespace NoSoliciting { - public static class Util { - public static string PluginFolder(IDalamudPlugin plugin) { - return Path.Combine(new[] { - Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), - "XIVLauncher", - "pluginConfigs", - plugin.Name, - }); - } - } -}