From cb4f9f81314d2b20e4e48c80224fb9bbc055c54c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 20 Oct 2022 14:25:28 +0200 Subject: [PATCH] Make migration and immediate file saving somewhat more stable, actually dispose Framework. --- Penumbra/Mods/Mod.Files.cs | 2 +- Penumbra/Mods/Mod.Meta.Migration.cs | 38 +++++++++++++++++++---------- Penumbra/Penumbra.cs | 2 +- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Penumbra/Mods/Mod.Files.cs b/Penumbra/Mods/Mod.Files.cs index 3f6a79b4..b09f680c 100644 --- a/Penumbra/Mods/Mod.Files.cs +++ b/Penumbra/Mods/Mod.Files.cs @@ -139,7 +139,7 @@ public partial class Mod foreach( var (group, index) in _groups.WithIndex() ) { - IModGroup.SaveDelayed( group, ModPath, index ); + IModGroup.Save( group, ModPath, index ); } } } \ No newline at end of file diff --git a/Penumbra/Mods/Mod.Meta.Migration.cs b/Penumbra/Mods/Mod.Meta.Migration.cs index 3710896e..8d907be9 100644 --- a/Penumbra/Mods/Mod.Meta.Migration.cs +++ b/Penumbra/Mods/Mod.Meta.Migration.cs @@ -16,7 +16,16 @@ public sealed partial class Mod private static class Migration { public static bool Migrate( Mod mod, JObject json ) - => MigrateV0ToV1( mod, json ) || MigrateV1ToV2( mod ) || MigrateV2ToV3( mod ); + { + var ret = MigrateV0ToV1( mod, json ) || MigrateV1ToV2( mod ) || MigrateV2ToV3( mod ); + if( ret ) + { + // Immediately save on migration. + mod.SaveMetaFile(); + } + + return ret; + } private static bool MigrateV2ToV3( Mod mod ) { @@ -27,10 +36,11 @@ public sealed partial class Mod // Remove import time. mod.FileVersion = 3; - mod.SaveMeta(); return true; } + + private static readonly Regex GroupRegex = new( @"group_\d{3}_", RegexOptions.Compiled ); private static bool MigrateV1ToV2( Mod mod ) { if( mod.FileVersion > 1 ) @@ -38,24 +48,26 @@ public sealed partial class Mod return false; } - foreach( var (group, index) in mod.GroupFiles.WithIndex().ToArray() ) + if (!mod.GroupFiles.All( g => GroupRegex.IsMatch( g.Name ))) { - var newName = Regex.Replace( group.Name, "^group_", $"group_{index + 1:D3}_", RegexOptions.Compiled ); - try + foreach( var (group, index) in mod.GroupFiles.WithIndex().ToArray() ) { - if( newName != group.Name ) + var newName = Regex.Replace( group.Name, "^group_", $"group_{index + 1:D3}_", RegexOptions.Compiled ); + try { - group.MoveTo( Path.Combine( group.DirectoryName ?? string.Empty, newName ), false ); + if( newName != group.Name ) + { + group.MoveTo( Path.Combine( group.DirectoryName ?? string.Empty, newName ), false ); + } + } + catch( Exception e ) + { + Penumbra.Log.Error( $"Could not rename group file {group.Name} to {newName} during migration:\n{e}" ); } - } - catch( Exception e ) - { - Penumbra.Log.Error( $"Could not rename group file {group.Name} to {newName} during migration:\n{e}" ); } } mod.FileVersion = 2; - mod.SaveMeta(); return true; } @@ -128,7 +140,7 @@ public sealed partial class Mod mod.FileVersion = 1; mod.SaveDefaultMod(); - mod.SaveMeta(); + mod.SaveMetaFile(); return true; } diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index 57648940..791bac47 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -7,7 +7,6 @@ using System.Text; using Dalamud.Game.Command; using Dalamud.Interface.Windowing; using Dalamud.Plugin; -using Dalamud.Utility; using EmbedIO; using EmbedIO.WebApi; using ImGuiNET; @@ -291,6 +290,7 @@ public class Penumbra : IDalamudPlugin ObjectReloader?.Dispose(); ModFileSystem?.Dispose(); CollectionManager?.Dispose(); + Framework?.Dispose(); Dalamud.Commands.RemoveHandler( CommandName );