From cceab7d98dc8ec6ddd6dd9c4fc7662b9309f2e38 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 9 Sep 2022 14:58:43 +0200 Subject: [PATCH] Add temp collection stuff. --- Penumbra/Api/IpcTester.cs | 9 ++++- Penumbra/Mods/Mod.TemporaryMod.cs | 67 +++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/Penumbra/Api/IpcTester.cs b/Penumbra/Api/IpcTester.cs index f5140db4..2bc4ebad 100644 --- a/Penumbra/Api/IpcTester.cs +++ b/Penumbra/Api/IpcTester.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.IO; using System.Linq; using System.Numerics; using System.Reflection; @@ -62,7 +63,6 @@ public class IpcTester : IDisposable { if( !_subscribed ) { - _initialized.Subscribe( AddInitialized ); _disposed.Subscribe( AddDisposed ); _redrawn.Subscribe( SetLastRedrawn ); @@ -911,7 +911,7 @@ public class IpcTester : IDisposable return; } - using var table = ImRaii.Table( "##collTree", 4 ); + using var table = ImRaii.Table( "##collTree", 5 ); if( !table ) { return; @@ -927,6 +927,11 @@ public class IpcTester : IDisposable ImGui.TextUnformatted( collection.ResolvedFiles.Count.ToString() ); ImGui.TableNextColumn(); ImGui.TextUnformatted( collection.MetaCache?.Count.ToString() ?? "0" ); + ImGui.TableNextColumn(); + if( ImGui.Button( $"Save##{character}" ) ) + { + Mod.TemporaryMod.SaveTempCollection( collection, character ); + } } } diff --git a/Penumbra/Mods/Mod.TemporaryMod.cs b/Penumbra/Mods/Mod.TemporaryMod.cs index 10d90979..7b55b994 100644 --- a/Penumbra/Mods/Mod.TemporaryMod.cs +++ b/Penumbra/Mods/Mod.TemporaryMod.cs @@ -1,6 +1,11 @@ using System; using System.Collections.Generic; +using System.IO; +using System.Linq; +using Dalamud.Logging; +using FFXIVClientStructs.FFXIV.Client.Game.Character; using OtterGui.Classes; +using Penumbra.Collections; using Penumbra.GameData.ByteString; using Penumbra.Meta.Manipulations; @@ -42,5 +47,67 @@ public sealed partial class Mod _default.FileData = dict; _default.ManipulationData = manips; } + + public static void SaveTempCollection( ModCollection collection, string? character = null ) + { + DirectoryInfo? dir = null; + try + { + dir = CreateModFolder( Penumbra.ModManager.BasePath, collection.Name ); + var fileDir = Directory.CreateDirectory( Path.Combine( dir.FullName, "files" ) ); + CreateMeta( dir, collection.Name, character ?? Penumbra.Config.DefaultModAuthor, + $"Mod generated from temporary collection {collection.Name} for {character ?? "Unknown Character"}.", null, null ); + var mod = new Mod( dir ); + var defaultMod = mod._default; + foreach( var (gamePath, fullPath) in collection.ResolvedFiles ) + { + if( gamePath.Path.EndsWith( '.', 'i', 'm', 'c' ) ) + { + continue; + } + + var targetPath = fullPath.Path.FullName; + if( fullPath.Path.Name.StartsWith( '|' ) ) + { + targetPath = targetPath.Split( '|', 3, StringSplitOptions.RemoveEmptyEntries ).Last(); + } + + if( Path.IsPathRooted(targetPath) ) + { + var target = Path.Combine( fileDir.FullName, Path.GetFileName(targetPath) ); + File.Copy( targetPath, target, true ); + defaultMod.FileData[ gamePath ] = new FullPath( target ); + } + else + { + defaultMod.FileSwapData[ gamePath ] = new FullPath(targetPath); + } + } + + foreach( var manip in collection.MetaCache?.Manipulations ?? Array.Empty< MetaManipulation >() ) + { + defaultMod.ManipulationData.Add( manip ); + } + + mod.SaveDefaultMod(); + Penumbra.ModManager.AddMod( dir ); + PluginLog.Information( $"Successfully generated mod {mod.Name} at {mod.ModPath.FullName} for collection {collection.Name}." ); + } + catch( Exception e ) + { + PluginLog.Error( $"Could not save temporary collection {collection.Name} to permanent Mod:\n{e}" ); + if( dir != null && Directory.Exists( dir.FullName ) ) + { + try + { + Directory.Delete( dir.FullName, true ); + } + catch + { + // ignored + } + } + } + } } } \ No newline at end of file