Skip to content

Commit

Permalink
refactor: update for api 9
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-is-cute committed Sep 28, 2023
1 parent d192efa commit ec2c5c4
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 66 deletions.
11 changes: 5 additions & 6 deletions DownloadTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Text;
using Blake3;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.Logging;
using gfoidl.Base64;
using Heliosphere.Exceptions;
using Heliosphere.Model;
Expand Down Expand Up @@ -196,7 +195,7 @@ private async Task DownloadFiles(IDownloadTask_GetVersion info) {
Directory.Move(oldName, this.PenumbraModPath);
}
} else if (directories.Length > 1) {
PluginLog.Warning($"multiple heliosphere mod directories found for {info.Variant.Package.Name} - not attempting a rename");
Plugin.Log.Warning($"multiple heliosphere mod directories found for {info.Variant.Package.Name} - not attempting a rename");
}

var filesPath = Path.Join(this.PenumbraModPath, "files");
Expand Down Expand Up @@ -371,7 +370,7 @@ private IEnumerable<Task> DownloadBatchedFiles(IDownloadTask_GetVersion_NeededFi
foreach (var path in toDuplicate) {
if (!File.Exists(path)) {
PluginLog.Warning($"{path} was supposed to be duplicated but no longer exists");
Plugin.Log.Warning($"{path} was supposed to be duplicated but no longer exists");
continue;
}
Expand Down Expand Up @@ -521,7 +520,7 @@ private static void GetExtensionsAndDiscriminators(IReadOnlyCollection<List<stri

if (extensions.Count == 0) {
// how does this happen?
PluginLog.LogWarning($"{hash} has no extension");
Plugin.Log.Warning($"{hash} has no extension");
extensions.Add(".unk");
}
}
Expand Down Expand Up @@ -583,7 +582,7 @@ private void RemoveOldFiles(IDownloadTask_GetVersion info) {
foreach (var extra in present) {
foreach (var file in presentHashes[extra]) {
var extraPath = Path.Join(filesPath, file);
PluginLog.Log($"removing extra file {extraPath}");
Plugin.Log.Info($"removing extra file {extraPath}");
File.Delete(extraPath);

done += 1;
Expand All @@ -602,7 +601,7 @@ private void RemoveOldFiles(IDownloadTask_GetVersion info) {
throw;
}

PluginLog.LogError(ex, message);
Plugin.Log.Error(ex, message);
await Task.Delay(TimeSpan.FromSeconds(3));
}
}
Expand Down
19 changes: 9 additions & 10 deletions PackageState.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Collections.Immutable;
using System.Text;
using Blake3;
using Dalamud.Logging;
using Dalamud.Interface.Internal;
using Heliosphere.Model;
using Heliosphere.Util;
using ImGuiScene;
using Newtonsoft.Json;

namespace Heliosphere;
Expand Down Expand Up @@ -143,7 +142,7 @@ internal async Task UpdatePackages() {
using (var cached = await this.Plugin.CoverImages.WaitAsync()) {
// more images are cached than mods were installed, clear cache
if (numPreviouslyInstalled < cached.Data.Count) {
PluginLog.LogVerbose("clearing cover image cache");
Plugin.Log.Verbose("clearing cover image cache");

foreach (var wrap in cached.Data.Values) {
wrap.Dispose();
Expand Down Expand Up @@ -196,7 +195,7 @@ internal async Task UpdatePackages() {
// downgrading these to a warning - most of the time it just doesn't
// matter, and I can't be fucked handling every bad meta json out
// there to prevent sentry being mad
PluginLog.LogWarning(ex, "Could not load heliosphere.json");
Plugin.Log.Warning(ex, "Could not load heliosphere.json");
return null;
}
}
Expand Down Expand Up @@ -281,15 +280,15 @@ internal async Task RenameDirectory(HeliosphereMeta meta, string penumbraPath, s
return;
}

PluginLog.Log($"Fixing incorrect folder name for {directory}");
Plugin.Log.Info($"Fixing incorrect folder name for {directory}");

var oldPath = Path.Join(penumbraPath, directory);
var newPath = Path.Join(penumbraPath, correctName);
if (Directory.Exists(newPath)) {
throw new ModAlreadyExistsException(oldPath, newPath);
}

PluginLog.Debug($" {oldPath} -> {newPath}");
Plugin.Log.Debug($" {oldPath} -> {newPath}");
Directory.Move(oldPath, newPath);
if (!await PathHelper.WaitToExist(newPath)) {
throw new DirectoryNotFoundException($"Directory '{newPath}' could not be found after waiting");
Expand All @@ -307,7 +306,7 @@ await this.Plugin.Framework.RunOnFrameworkThread(() => {
}

private async Task<(string, string[])> MigrateOldDirectory(HeliosphereMeta meta, string penumbraPath, string directory) {
PluginLog.Debug($"Migrating old folder name layout for {directory}");
Plugin.Log.Debug($"Migrating old folder name layout for {directory}");
var variant = await Plugin.GraphQl.GetVariant.ExecuteAsync(meta.VersionId);
if (variant.Data?.GetVersion == null) {
throw new Exception($"no variant for version id {meta.VersionId}");
Expand All @@ -322,14 +321,14 @@ await this.Plugin.Framework.RunOnFrameworkThread(() => {

var parts = newName.Split('-');

PluginLog.Debug($" {oldPath} -> {newPath}");
Plugin.Log.Debug($" {oldPath} -> {newPath}");
Directory.Move(oldPath, newPath);
await this.Plugin.Framework.RunOnFrameworkThread(() => {
this.Plugin.Penumbra.AddMod(newName);
this.Plugin.Penumbra.ReloadMod(directory);
});

PluginLog.Debug(" writing new meta");
Plugin.Log.Debug(" writing new meta");
var json = JsonConvert.SerializeObject(meta, Formatting.Indented);
var path = Path.Join(penumbraPath, newName, "heliosphere.json");
await using var file = File.Create(path);
Expand All @@ -356,7 +355,7 @@ internal class InstalledPackage : IDisposable {
internal string Author { get; }
internal string CoverImagePath { get; }

internal TextureWrap? CoverImage { get; private set; }
internal IDalamudTextureWrap? CoverImage { get; private set; }

internal List<HeliosphereMeta> InternalVariants { get; }
internal IReadOnlyList<HeliosphereMeta> Variants => this.InternalVariants.ToImmutableList();
Expand Down
31 changes: 19 additions & 12 deletions Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text.RegularExpressions;
using Dalamud.Game;
using Dalamud.Game.ClientState;
using Dalamud.Game.Command;
using Dalamud.Game.Gui;
using Dalamud.Interface.Internal;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.IoC;
using Dalamud.Logging;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using Heliosphere.Exceptions;
using Heliosphere.Model.Generated;
using Heliosphere.Ui;
using Heliosphere.Util;
using ImGuiScene;
using Microsoft.Extensions.DependencyInjection;
using Sentry;
using Sentry.Extensibility;
Expand All @@ -34,21 +30,25 @@ public class Plugin : IDalamudPlugin {

internal static GameFont GameFont { get; private set; }
internal static DalamudPluginInterface PluginInterface { get; private set; }
internal static IPluginLog Log { get; private set; }

[PluginService]
internal DalamudPluginInterface Interface { get; init; }

[PluginService]
internal ChatGui ChatGui { get; init; }
internal IChatGui ChatGui { get; init; }

[PluginService]
internal ClientState ClientState { get; init; }
internal IClientState ClientState { get; init; }

[PluginService]
internal CommandManager CommandManager { get; init; }
internal ICommandManager CommandManager { get; init; }

[PluginService]
internal Framework Framework { get; init; }
internal IFramework Framework { get; init; }

[PluginService]
private IPluginLog PluginLog { get; init; }

internal Configuration Config { get; }
internal DownloadCodes DownloadCodes { get; }
Expand All @@ -62,7 +62,7 @@ public class Plugin : IDalamudPlugin {
private IDisposable Sentry { get; }

internal bool IntegrityFailed { get; private set; }
internal Guard<Dictionary<string, TextureWrap>> CoverImages { get; } = new(new Dictionary<string, TextureWrap>());
internal Guard<Dictionary<string, IDalamudTextureWrap>> CoverImages { get; } = new(new Dictionary<string, IDalamudTextureWrap>());

public Plugin() {
var checkTask = Task.Run(async () => {
Expand All @@ -78,6 +78,7 @@ public Plugin() {
Instance = this;
GameFont = new GameFont(this);
PluginInterface = this.Interface!;
Log = this.PluginLog!;

this.Sentry = SentrySdk.Init(o => {
o.Dsn = "https://[email protected]/4";
Expand Down Expand Up @@ -107,7 +108,7 @@ public Plugin() {
try {
DependencyLoader.Load();
} catch (Exception ex) {
PluginLog.Error(ex, "Failed to initialise native libraries (probably AV)");
Log.Error(ex, "Failed to initialise native libraries (probably AV)");
startWithAvWarning = true;
this.IntegrityFailed = true;
}
Expand Down Expand Up @@ -281,6 +282,7 @@ public class FileList {
public Dictionary<string, List<List<string?>>> Files { get; init; }
}

// ReSharper disable once ClassNeverInstantiated.Global
public class FileListSerializer : ScalarSerializer<JsonElement, FileList> {
public FileListSerializer(string typeName = "FileList") : base(typeName) {
}
Expand All @@ -296,6 +298,7 @@ protected override JsonElement Format(FileList runtimeValue) {
}
}

// ReSharper disable once ClassNeverInstantiated.Global
public class OptionsSerializer : ScalarSerializer<JsonElement, Dictionary<string, List<string>>> {
public OptionsSerializer(string typeName = "Options") : base(typeName) {
}
Expand All @@ -314,6 +317,7 @@ public class InstallerImageList {
public Dictionary<string, HashSet<string>> Images { get; init; }
}

// ReSharper disable once ClassNeverInstantiated.Global
public class InstallerImageListSerializer : ScalarSerializer<JsonElement, InstallerImageList> {
public InstallerImageListSerializer(string typeName = "InstallerImageList") : base(typeName) {
}
Expand Down Expand Up @@ -346,6 +350,7 @@ public class BatchedFile {
public ulong SizeUncompressed => this.size_uncompressed;
}

// ReSharper disable once ClassNeverInstantiated.Global
public class BatchListSerializer : ScalarSerializer<JsonElement, BatchList> {
public BatchListSerializer(string typeName = "BatchList") : base(typeName) {
}
Expand All @@ -366,6 +371,7 @@ public class FileSwaps {
public Dictionary<string, string> Swaps { get; init; }
}

// ReSharper disable once ClassNeverInstantiated.Global
public class FileSwapsSerializer : ScalarSerializer<JsonElement, FileSwaps> {
public FileSwapsSerializer(string typeName = "FileSwaps") : base(typeName) {
}
Expand All @@ -381,6 +387,7 @@ protected override JsonElement Format(FileSwaps runtimeValue) {
}
}

// ReSharper disable once ClassNeverInstantiated.Global
public class GraphqlJsonSerializer : ScalarSerializer<JsonElement, JsonElement> {
public GraphqlJsonSerializer(string typeName = "JSON") : base(typeName) {
}
Expand Down
3 changes: 1 addition & 2 deletions Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Security.Cryptography;
using System.Text;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.Logging;
using gfoidl.Base64;
using Heliosphere.Ui;
using Heliosphere.Util;
Expand Down Expand Up @@ -419,7 +418,7 @@ private bool OneClickPassed(string? providedPassword, bool holdingShift) {
Base64.Default.Decode(this.Plugin.Config.OneClickHash)
);
} catch (Exception ex) {
PluginLog.LogWarning(ex, "Failed to decode one-click password");
Plugin.Log.Warning(ex, "Failed to decode one-click password");
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion Ui/DownloadStatusWindow.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Diagnostics;
using System.Numerics;
using Dalamud.Interface;
using Dalamud.Interface.Utility;
using Heliosphere.Util;
using ImGuiNET;

Expand Down
8 changes: 4 additions & 4 deletions Ui/InstallerWindow.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Numerics;
using Dalamud.Interface;
using Dalamud.Interface.Internal;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.Interface.Utility;
using Heliosphere.Exceptions;
using Heliosphere.Model.Generated;
using Heliosphere.Util;
using ImGuiNET;
using ImGuiScene;
using StrawberryShake;

namespace Heliosphere.Ui;
Expand All @@ -22,7 +22,7 @@ internal class InstallerWindow : IDrawable {
private string? DownloadKey { get; }

private class ImageCache {
internal Dictionary<string, TextureWrap> HashImages { get; } = new();
internal Dictionary<string, IDalamudTextureWrap> HashImages { get; } = new();
internal Dictionary<string, string> PathHashes { get; } = new();
}

Expand Down Expand Up @@ -314,7 +314,7 @@ private void DrawTableColumn2(IReadOnlyList<IInstallerWindow_GetVersion_Groups_O
ImGui.PopTextWrapPos();
}

private TextureWrap? GetImage(string path) {
private IDalamudTextureWrap? GetImage(string path) {
using var guard = this.Images.Wait(0);
if (guard == null || !guard.Data.PathHashes.TryGetValue(path, out var hash)) {
return null;
Expand Down
8 changes: 4 additions & 4 deletions Ui/MultiVariantPromptWindow.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Dalamud.Interface.Internal;
using Dalamud.Interface.Internal.Notifications;
using Heliosphere.Model.Generated;
using Heliosphere.Util;
using ImGuiNET;
using ImGuiScene;
using StrawberryShake;

namespace Heliosphere.Ui;
Expand All @@ -17,10 +17,10 @@ internal class MultiVariantPromptWindow : IDrawable {
private bool _includeTags;
private bool _openInPenumbra;
private string? _collection;
private readonly TextureWrap? _coverImage;
private readonly IDalamudTextureWrap? _coverImage;
private readonly string? _downloadKey;

private MultiVariantPromptWindow(Plugin plugin, Guid packageId, IMultiVariantInstall_Package package, Dictionary<IMultiVariantInstall_Package_Variants, IMultiVariantInstall_Package_Variants_Versions> variants, TextureWrap? cover, string? downloadKey) {
private MultiVariantPromptWindow(Plugin plugin, Guid packageId, IMultiVariantInstall_Package package, Dictionary<IMultiVariantInstall_Package_Variants, IMultiVariantInstall_Package_Variants_Versions> variants, IDalamudTextureWrap? cover, string? downloadKey) {
this.Plugin = plugin;
this.PackageId = packageId;
this.Package = package;
Expand Down Expand Up @@ -132,7 +132,7 @@ internal static async Task<MultiVariantPromptWindow> Open(Plugin plugin, Guid pa
throw new Exception("Variants with no versions specified.");
}

TextureWrap? cover = null;
IDalamudTextureWrap? cover = null;
if (pkg.Images.Count > 0) {
try {
using var imgResp = await DownloadTask.GetImage(packageId, pkg.Images[0].Id);
Expand Down
8 changes: 4 additions & 4 deletions Ui/PromptWindow.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Dalamud.Interface.Internal;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.Interface.Style;
using Heliosphere.Model.Generated;
using Heliosphere.Util;
using ImGuiNET;
using ImGuiScene;

namespace Heliosphere.Ui;

Expand All @@ -19,9 +19,9 @@ internal class PromptWindow : IDrawable {
private bool _openInPenumbra;
private string? _collection;
private readonly string? _downloadKey;
private readonly TextureWrap? _coverImage;
private readonly IDalamudTextureWrap? _coverImage;

private PromptWindow(Plugin plugin, Guid packageId, IInstallerWindow_GetVersion info, Guid versionId, string version, TextureWrap? coverImage, string? downloadKey) {
private PromptWindow(Plugin plugin, Guid packageId, IInstallerWindow_GetVersion info, Guid versionId, string version, IDalamudTextureWrap? coverImage, string? downloadKey) {
this.Plugin = plugin;
this.PackageId = packageId;
this.Info = info;
Expand All @@ -44,7 +44,7 @@ internal static async Task<PromptWindow> Open(Plugin plugin, Guid packageId, Gui
throw new Exception("Invalid package install URI.");
}

TextureWrap? cover = null;
IDalamudTextureWrap? cover = null;
if (info.Variant.Package.Images.Count > 0) {
var coverImage = info.Variant.Package.Images[0];

Expand Down
Loading

0 comments on commit ec2c5c4

Please sign in to comment.