fix: use correct config dir and truncate new models

This commit is contained in:
Anna 2021-04-25 10:53:44 -04:00
parent 0191cb8c17
commit caa2a93962
Signed by: anna
GPG Key ID: 0B391D8F06FCD9E0
4 changed files with 34 additions and 38 deletions

View File

@ -64,7 +64,7 @@ namespace NoSoliciting {
throw new ArgumentNullException(nameof(plugin), "Plugin cannot be null"); 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"); var cachedPath = Path.Combine(pluginFolder, "definitions.yaml");
if (!File.Exists(cachedPath)) { if (!File.Exists(cachedPath)) {
@ -96,7 +96,7 @@ namespace NoSoliciting {
var text = await client.DownloadStringTaskAsync(Url).ConfigureAwait(true); var text = await client.DownloadStringTaskAsync(Url).ConfigureAwait(true);
LastError = null; LastError = null;
return Tuple.Create(Load(text), text); 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("Could not download newest definitions.");
PluginLog.Log(e.ToString()); PluginLog.Log(e.ToString());
LastError = e.Message; LastError = e.Message;
@ -104,8 +104,8 @@ namespace NoSoliciting {
} }
} }
private static async void UpdateCache(IDalamudPlugin plugin, string defs) { private static async void UpdateCache(Plugin plugin, string defs) {
var pluginFolder = Util.PluginFolder(plugin); var pluginFolder = plugin.Interface.ConfigDirectory.ToString();
Directory.CreateDirectory(pluginFolder); Directory.CreateDirectory(pluginFolder);
var cachePath = Path.Combine(pluginFolder, "definitions.yaml"); var cachePath = Path.Combine(pluginFolder, "definitions.yaml");

View File

@ -125,7 +125,7 @@ namespace NoSoliciting.Interface {
if (ImGui.Button("Update model")) { if (ImGui.Button("Update model")) {
// prevent issues when people spam the button // 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?.Dispose();
this.Plugin.MlFilter = null; this.Plugin.MlFilter = null;
this.Plugin.MlStatus = MlFilterStatus.Uninitialised; this.Plugin.MlStatus = MlFilterStatus.Uninitialised;

View File

@ -53,28 +53,37 @@ namespace NoSoliciting.Ml {
public static async Task<MlFilter?> Load(Plugin plugin, bool showWindow) { public static async Task<MlFilter?> Load(Plugin plugin, bool showWindow) {
plugin.MlStatus = MlFilterStatus.DownloadingManifest; plugin.MlStatus = MlFilterStatus.DownloadingManifest;
// download and parse the remote manifest
var manifest = await DownloadManifest(); var manifest = await DownloadManifest();
if (manifest == null) { if (manifest == null) {
PluginLog.LogWarning("Could not download manifest. Will attempt to fall back on cached version."); PluginLog.LogWarning("Could not download manifest. Will attempt to fall back on cached version.");
} }
// model zip file data
byte[]? data = null; byte[]? data = null;
// load the cached manifest
var localManifest = LoadCachedManifest(plugin); 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 {
// try to reach the cached model
data = File.ReadAllBytes(CachedFilePath(plugin, ModelName)); 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) { } catch (IOException) {
// ignored // ignored
} }
} }
if (!string.IsNullOrEmpty(manifest?.Item2)) { // if there is source for the manifest
if (!string.IsNullOrEmpty(manifest?.source)) {
plugin.MlStatus = MlFilterStatus.DownloadingModel; 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) { if (data == null) {
plugin.MlStatus = MlFilterStatus.Uninitialised; plugin.MlStatus = MlFilterStatus.Uninitialised;
return null; return null;
@ -82,12 +91,15 @@ namespace NoSoliciting.Ml {
plugin.MlStatus = MlFilterStatus.Initialising; 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, 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); var exePath = await ExtractClassifier(pluginFolder);
@ -97,8 +109,8 @@ namespace NoSoliciting.Ml {
var client = await CreateClassifierClient(pipeId, data); var client = await CreateClassifierClient(pipeId, data);
return new MlFilter( return new MlFilter(
manifest.Item1!.Version, manifest.Value.manifest!.Version,
manifest.Item1!.ReportUrl, manifest.Value.manifest!.ReportUrl,
process!, process!,
client client
); );
@ -158,28 +170,28 @@ namespace NoSoliciting.Ml {
} }
} }
private static string CachedFilePath(IDalamudPlugin plugin, string name) { private static string CachedFilePath(Plugin plugin, string name) {
var pluginFolder = Util.PluginFolder(plugin); var pluginFolder = plugin.Interface.ConfigDirectory.ToString();
Directory.CreateDirectory(pluginFolder); Directory.CreateDirectory(pluginFolder);
return Path.Combine(pluginFolder, name); 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 cachePath = CachedFilePath(plugin, name);
var file = File.OpenWrite(cachePath); var file = File.Create(cachePath);
await file.WriteAsync(data, 0, data.Length); await file.WriteAsync(data, 0, data.Length);
await file.FlushAsync(); await file.FlushAsync();
file.Dispose(); file.Dispose();
} }
private static async Task<Tuple<Manifest, string>?> DownloadManifest() { private static async Task<(Manifest manifest, string source)?> DownloadManifest() {
try { try {
using var client = new WebClient(); using var client = new WebClient();
var data = await client.DownloadStringTaskAsync(Url); var data = await client.DownloadStringTaskAsync(Url);
LastError = null; LastError = null;
return Tuple.Create(LoadYaml<Manifest>(data), data); return (LoadYaml<Manifest>(data), data);
} catch (Exception e) when (e is WebException || e is YamlException) { } catch (Exception e) when (e is WebException or YamlException) {
PluginLog.LogError("Could not download newest model manifest."); PluginLog.LogError("Could not download newest model manifest.");
PluginLog.LogError(e.ToString()); PluginLog.LogError(e.ToString());
LastError = e.Message; 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); var manifestPath = CachedFilePath(plugin, ManifestName);
if (!File.Exists(manifestPath)) { if (!File.Exists(manifestPath)) {
return null; return null;

View File

@ -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,
});
}
}
}