From ccdafcf85d52c1d485dd90cec1dabfcfe6ea5455 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 26 Mar 2023 18:02:32 +0200 Subject: [PATCH] More Stuff. --- Penumbra/Collections/ModCollection.cs | 2 -- Penumbra/Collections/ResolveData.cs | 30 +++++++++--------- Penumbra/Mods/Manager/ModDataEditor.cs | 7 ++--- Penumbra/Mods/Mod.LocalData.cs | 4 ++- Penumbra/Mods/Mod.Meta.Migration.cs | 43 ++++++++++++++------------ Penumbra/Mods/Mod.Meta.cs | 6 ++-- Penumbra/Penumbra.cs | 7 ++--- Penumbra/UI/Tabs/ResourceTab.cs | 2 +- 8 files changed, 49 insertions(+), 52 deletions(-) diff --git a/Penumbra/Collections/ModCollection.cs b/Penumbra/Collections/ModCollection.cs index a8a4e1f4..54f1f9a8 100644 --- a/Penumbra/Collections/ModCollection.cs +++ b/Penumbra/Collections/ModCollection.cs @@ -4,8 +4,6 @@ using System; using System.Collections.Generic; using System.Linq; using OtterGui; -using OtterGui.Classes; -using Penumbra.Services; namespace Penumbra.Collections; diff --git a/Penumbra/Collections/ResolveData.cs b/Penumbra/Collections/ResolveData.cs index 485f2c08..f816069e 100644 --- a/Penumbra/Collections/ResolveData.cs +++ b/Penumbra/Collections/ResolveData.cs @@ -1,7 +1,6 @@ using System; using System.Linq; using FFXIVClientStructs.FFXIV.Client.Game.Object; -using Penumbra.String; namespace Penumbra.Collections; @@ -25,14 +24,14 @@ public readonly struct ResolveData AssociatedGameObject = IntPtr.Zero; } - public ResolveData( ModCollection collection, IntPtr gameObject ) + public ResolveData(ModCollection collection, IntPtr gameObject) { _modCollection = collection; AssociatedGameObject = gameObject; } - public ResolveData( ModCollection collection ) - : this( collection, IntPtr.Zero ) + public ResolveData(ModCollection collection) + : this(collection, IntPtr.Zero) { } public override string ToString() @@ -40,19 +39,18 @@ public readonly struct ResolveData public unsafe string AssociatedName() { - if( AssociatedGameObject == IntPtr.Zero ) - { + if (AssociatedGameObject == IntPtr.Zero) return "no associated object."; - } try { - var id = Penumbra.Actors.FromObject( ( GameObject* )AssociatedGameObject, out _, false, true, true ); - if( id.IsValid ) + var id = Penumbra.Actors.FromObject((GameObject*)AssociatedGameObject, out _, false, true, true); + if (id.IsValid) { var name = id.ToString(); - var parts = name.Split( ' ', 3 ); - return string.Join( " ", parts.Length != 3 ? parts.Select( n => $"{n[ 0 ]}." ) : parts[ ..2 ].Select( n => $"{n[ 0 ]}." ).Append( parts[ 2 ] ) ); + var parts = name.Split(' ', 3); + return string.Join(" ", + parts.Length != 3 ? parts.Select(n => $"{n[0]}.") : parts[..2].Select(n => $"{n[0]}.").Append(parts[2])); } } catch @@ -66,12 +64,12 @@ public readonly struct ResolveData public static class ResolveDataExtensions { - public static ResolveData ToResolveData( this ModCollection collection ) + public static ResolveData ToResolveData(this ModCollection collection) => new(collection); - public static ResolveData ToResolveData( this ModCollection collection, IntPtr ptr ) + public static ResolveData ToResolveData(this ModCollection collection, IntPtr ptr) => new(collection, ptr); - public static unsafe ResolveData ToResolveData( this ModCollection collection, void* ptr ) - => new(collection, ( IntPtr )ptr); -} \ No newline at end of file + public static unsafe ResolveData ToResolveData(this ModCollection collection, void* ptr) + => new(collection, (IntPtr)ptr); +} diff --git a/Penumbra/Mods/Manager/ModDataEditor.cs b/Penumbra/Mods/Manager/ModDataEditor.cs index 27253ff1..2f236b5f 100644 --- a/Penumbra/Mods/Manager/ModDataEditor.cs +++ b/Penumbra/Mods/Manager/ModDataEditor.cs @@ -120,7 +120,7 @@ public class ModDataEditor var newDescription = json[nameof(Mod.Description)]?.Value() ?? string.Empty; var newVersion = json[nameof(Mod.Version)]?.Value() ?? string.Empty; var newWebsite = json[nameof(Mod.Website)]?.Value() ?? string.Empty; - var newFileVersion = json[nameof(Mod.FileVersion)]?.Value() ?? 0; + var newFileVersion = json[nameof(Mod.ModMeta.FileVersion)]?.Value() ?? 0; var importDate = json[nameof(Mod.ImportDate)]?.Value(); var modTags = json[nameof(Mod.ModTags)]?.Values().OfType(); @@ -155,10 +155,9 @@ public class ModDataEditor mod.Website = newWebsite; } - if (mod.FileVersion != newFileVersion) + if (newFileVersion != Mod.ModMeta.FileVersion) { - mod.FileVersion = newFileVersion; - if (Mod.Migration.Migrate(mod, json)) + if (Mod.Migration.Migrate(mod, json, ref newFileVersion)) { changes |= ModDataChangeType.Migration; _saveService.ImmediateSave(new Mod.ModMeta(mod)); diff --git a/Penumbra/Mods/Mod.LocalData.cs b/Penumbra/Mods/Mod.LocalData.cs index 71a73f0f..1c0ae1e0 100644 --- a/Penumbra/Mods/Mod.LocalData.cs +++ b/Penumbra/Mods/Mod.LocalData.cs @@ -54,6 +54,8 @@ public sealed partial class Mod internal readonly struct ModData : ISavable { + public const int FileVersion = 3; + private readonly Mod _mod; public ModData(Mod mod) @@ -66,7 +68,7 @@ public sealed partial class Mod { var jObject = new JObject { - { nameof(FileVersion), JToken.FromObject(_mod.FileVersion) }, + { nameof(FileVersion), JToken.FromObject(FileVersion) }, { nameof(ImportDate), JToken.FromObject(_mod.ImportDate) }, { nameof(LocalTags), JToken.FromObject(_mod.LocalTags) }, { nameof(Note), JToken.FromObject(_mod.Note) }, diff --git a/Penumbra/Mods/Mod.Meta.Migration.cs b/Penumbra/Mods/Mod.Meta.Migration.cs index 64501009..5a07fd29 100644 --- a/Penumbra/Mods/Mod.Meta.Migration.cs +++ b/Penumbra/Mods/Mod.Meta.Migration.cs @@ -15,31 +15,34 @@ public sealed partial class Mod { public static partial class Migration { - public static bool Migrate(Mod mod, JObject json) - => MigrateV0ToV1(mod, json) || MigrateV1ToV2(mod) || MigrateV2ToV3(mod); - - private static bool MigrateV2ToV3(Mod mod) - { - if (mod.FileVersion > 2) - return false; - - // Remove import time. - mod.FileVersion = 3; - return true; - } - [GeneratedRegex(@"group_\d{3}_", RegexOptions.Compiled | RegexOptions.NonBacktracking | RegexOptions.ExplicitCapture)] private static partial Regex GroupRegex(); - private static bool MigrateV1ToV2(Mod mod) + [GeneratedRegex("^group_", RegexOptions.Compiled)] + private static partial Regex GroupStartRegex(); + + public static bool Migrate(Mod mod, JObject json, ref uint fileVersion) + => MigrateV0ToV1(mod, json, ref fileVersion) || MigrateV1ToV2(mod, ref fileVersion) || MigrateV2ToV3(mod, ref fileVersion); + + private static bool MigrateV2ToV3(Mod _, ref uint fileVersion) { - if (mod.FileVersion > 1) + if (fileVersion > 2) + return false; + + // Remove import time. + fileVersion = 3; + return true; + } + + private static bool MigrateV1ToV2(Mod mod, ref uint fileVersion) + { + if (fileVersion > 1) return false; if (!mod.GroupFiles.All(g => GroupRegex().IsMatch(g.Name))) foreach (var (group, index) in mod.GroupFiles.WithIndex().ToArray()) { - var newName = Regex.Replace(group.Name, "^group_", $"group_{index + 1:D3}_", RegexOptions.Compiled); + var newName = GroupStartRegex().Replace(group.Name, $"group_{index + 1:D3}_"); try { if (newName != group.Name) @@ -51,14 +54,14 @@ public sealed partial class Mod } } - mod.FileVersion = 2; + fileVersion = 2; return true; } - private static bool MigrateV0ToV1(Mod mod, JObject json) + private static bool MigrateV0ToV1(Mod mod, JObject json, ref uint fileVersion) { - if (mod.FileVersion > 0) + if (fileVersion > 0) return false; var swaps = json["FileSwaps"]?.ToObject>() @@ -110,7 +113,7 @@ public sealed partial class Mod Penumbra.Log.Warning($"Could not delete old meta file {oldMetaFile} during migration:\n{e}"); } - mod.FileVersion = 1; + fileVersion = 1; mod.SaveDefaultMod(); return true; diff --git a/Penumbra/Mods/Mod.Meta.cs b/Penumbra/Mods/Mod.Meta.cs index 0e29a12c..3b716298 100644 --- a/Penumbra/Mods/Mod.Meta.cs +++ b/Penumbra/Mods/Mod.Meta.cs @@ -18,8 +18,6 @@ public sealed partial class Mod : IMod Priority = int.MaxValue, }; - public const uint CurrentFileVersion = 3; - public uint FileVersion { get; internal set; } = CurrentFileVersion; public LowerString Name { get; internal set; } = "New Mod"; public LowerString Author { get; internal set; } = LowerString.Empty; public string Description { get; internal set; } = string.Empty; @@ -32,6 +30,8 @@ public sealed partial class Mod : IMod internal readonly struct ModMeta : ISavable { + public const uint FileVersion = 3; + private readonly Mod _mod; public ModMeta(Mod mod) @@ -44,7 +44,7 @@ public sealed partial class Mod : IMod { var jObject = new JObject { - { nameof(FileVersion), JToken.FromObject(_mod.FileVersion) }, + { nameof(FileVersion), JToken.FromObject(FileVersion) }, { nameof(Name), JToken.FromObject(_mod.Name) }, { nameof(Author), JToken.FromObject(_mod.Author) }, { nameof(Description), JToken.FromObject(_mod.Description) }, diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index 8b31e9e0..46b8ad3a 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -72,9 +72,6 @@ public class Penumbra : IDalamudPlugin private bool _disposed; private readonly PenumbraNew _tmp; - // TODO - public static ResourceManagerService ResourceManagerService { get; private set; } = null!; - public static ResourceService ResourceService { get; private set; } = null!; public Penumbra(DalamudPluginInterface pluginInterface) { @@ -98,13 +95,13 @@ public class Penumbra : IDalamudPlugin StainService = _tmp.Services.GetRequiredService(); TempMods = _tmp.Services.GetRequiredService(); ResidentResources = _tmp.Services.GetRequiredService(); - ResourceManagerService = _tmp.Services.GetRequiredService(); + _tmp.Services.GetRequiredService(); ModManager = _tmp.Services.GetRequiredService(); CollectionManager = _tmp.Services.GetRequiredService(); TempCollections = _tmp.Services.GetRequiredService(); ModFileSystem = _tmp.Services.GetRequiredService(); RedrawService = _tmp.Services.GetRequiredService(); - ResourceService = _tmp.Services.GetRequiredService(); + _tmp.Services.GetRequiredService(); ResourceLoader = _tmp.Services.GetRequiredService(); using (var t = _tmp.Services.GetRequiredService().Measure(StartTimeType.PathResolver)) { diff --git a/Penumbra/UI/Tabs/ResourceTab.cs b/Penumbra/UI/Tabs/ResourceTab.cs index c1724b8e..95ff0d2a 100644 --- a/Penumbra/UI/Tabs/ResourceTab.cs +++ b/Penumbra/UI/Tabs/ResourceTab.cs @@ -48,7 +48,7 @@ public class ResourceTab : ITab unsafe { - Penumbra.ResourceManagerService.IterateGraphs(DrawCategoryContainer); + _resourceManager.IterateGraphs(DrawCategoryContainer); } ImGui.NewLine();