From 95d5d6c4b01c9269950bf42851d20f303317541f Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 12 Feb 2024 23:30:41 +0100 Subject: [PATCH 001/433] Update submodules. --- OtterGui | 2 +- Penumbra.Api | 2 +- Penumbra.GameData | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/OtterGui b/OtterGui index c6f101bb..1a187f75 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit c6f101bbef976b74eb651523445563dd81fafbaf +Subproject commit 1a187f756f2e8823197bd43db1c3383231f5eaff diff --git a/Penumbra.Api b/Penumbra.Api index 31bf4ad9..a28219ac 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit 31bf4ad9b82fc980d6bda049da595368ad754931 +Subproject commit a28219ac57b53c3be6ca8c252ceb9f76ae0b6c21 diff --git a/Penumbra.GameData b/Penumbra.GameData index 63f4de73..0d267233 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 63f4de7305616b6cb8921513e5d83baa8913353f +Subproject commit 0d267233724d493d9ae2bf8d1e67bfbb8b337916 From d2bfcefb899d7e8ede7f4a424ea5de9da66c681e Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 12 Feb 2024 23:32:45 +0100 Subject: [PATCH 002/433] Update Combos. --- Penumbra/Import/Textures/TextureDrawer.cs | 14 +++++++------- Penumbra/Mods/Manager/ModStorage.cs | 2 +- Penumbra/Services/StainService.cs | 6 +++--- Penumbra/UI/AdvancedWindow/FileEditor.cs | 2 +- Penumbra/UI/AdvancedWindow/ItemSwapTab.cs | 17 ++--------------- Penumbra/UI/CollectionTab/CollectionCombo.cs | 14 +++++--------- 6 files changed, 19 insertions(+), 36 deletions(-) diff --git a/Penumbra/Import/Textures/TextureDrawer.cs b/Penumbra/Import/Textures/TextureDrawer.cs index 04422116..427db92d 100644 --- a/Penumbra/Import/Textures/TextureDrawer.cs +++ b/Penumbra/Import/Textures/TextureDrawer.cs @@ -32,7 +32,8 @@ public static class TextureDrawer if (texture.LoadError is DllNotFoundException) { - ImGuiUtil.TextColored(Colors.RegexWarningBorder, "A texture handling dependency could not be found. Try installing a current Microsoft VC Redistributable."); + ImGuiUtil.TextColored(Colors.RegexWarningBorder, + "A texture handling dependency could not be found. Try installing a current Microsoft VC Redistributable."); if (ImGui.Button("Microsoft VC Redistributables")) Dalamud.Utility.Util.OpenLink(link); ImGuiUtil.HoverTooltip($"Open {link} in your browser."); @@ -111,14 +112,12 @@ public static class TextureDrawer } } - public sealed class PathSelectCombo : FilterComboCache<(string Path, bool Game, bool IsOnPlayer)> + public sealed class PathSelectCombo(TextureManager textures, ModEditor editor, Func> getPlayerResources) + : FilterComboCache<(string Path, bool Game, bool IsOnPlayer)>(() => CreateFiles(textures, editor, getPlayerResources), + MouseWheelType.None, Penumbra.Log) { private int _skipPrefix = 0; - public PathSelectCombo(TextureManager textures, ModEditor editor, Func> getPlayerResources) - : base(() => CreateFiles(textures, editor, getPlayerResources), Penumbra.Log) - { } - protected override string ToString((string Path, bool Game, bool IsOnPlayer) obj) => obj.Path; @@ -140,7 +139,8 @@ public static class TextureDrawer return ret; } - private static IReadOnlyList<(string Path, bool Game, bool IsOnPlayer)> CreateFiles(TextureManager textures, ModEditor editor, Func> getPlayerResources) + private static IReadOnlyList<(string Path, bool Game, bool IsOnPlayer)> CreateFiles(TextureManager textures, ModEditor editor, + Func> getPlayerResources) { var playerResources = getPlayerResources(); diff --git a/Penumbra/Mods/Manager/ModStorage.cs b/Penumbra/Mods/Manager/ModStorage.cs index 490381d6..1e5df6b9 100644 --- a/Penumbra/Mods/Manager/ModStorage.cs +++ b/Penumbra/Mods/Manager/ModStorage.cs @@ -12,7 +12,7 @@ public class ModCombo : FilterComboCache => obj.Name.Text; public ModCombo(Func> generator) - : base(generator, Penumbra.Log) + : base(generator, MouseWheelType.None, Penumbra.Log) { } } diff --git a/Penumbra/Services/StainService.cs b/Penumbra/Services/StainService.cs index 00fc0737..26b39229 100644 --- a/Penumbra/Services/StainService.cs +++ b/Penumbra/Services/StainService.cs @@ -13,7 +13,7 @@ namespace Penumbra.Services; public class StainService : IService { public sealed class StainTemplateCombo(FilterComboColors stainCombo, StmFile stmFile) - : FilterComboCache(stmFile.Entries.Keys.Prepend((ushort)0), Penumbra.Log) + : FilterComboCache(stmFile.Entries.Keys.Prepend((ushort)0), MouseWheelType.None, Penumbra.Log) { protected override float GetFilterWidth() { @@ -70,8 +70,8 @@ public class StainService : IService public StainService(IDataManager dataManager, DictStain stainData) { StainData = stainData; - StainCombo = new FilterComboColors(140, - () => StainData.Value.Prepend(new KeyValuePair(0, ("None", 0, false))).ToList(), + StainCombo = new FilterComboColors(140, MouseWheelType.None, + () => StainData.Value.Prepend(new KeyValuePair(0, ("None", 0, false))).ToList(), Penumbra.Log); StmFile = new StmFile(dataManager); TemplateCombo = new StainTemplateCombo(StainCombo, StmFile); diff --git a/Penumbra/UI/AdvancedWindow/FileEditor.cs b/Penumbra/UI/AdvancedWindow/FileEditor.cs index 16cacaa4..89d47eb2 100644 --- a/Penumbra/UI/AdvancedWindow/FileEditor.cs +++ b/Penumbra/UI/AdvancedWindow/FileEditor.cs @@ -278,7 +278,7 @@ public class FileEditor : IDisposable where T : class, IWritable private readonly Configuration _config; public Combo(Configuration config, Func> generator) - : base(generator, Penumbra.Log) + : base(generator, MouseWheelType.None, Penumbra.Log) => _config = config; protected override bool DrawSelectable(int globalIdx, bool selected) diff --git a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs index d31a08ae..0205f3c6 100644 --- a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs +++ b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs @@ -125,26 +125,13 @@ public class ItemSwapTab : IDisposable, ITab Weapon, } - private class ItemSelector : FilterComboCache + private class ItemSelector(ItemData data, FullEquipType type) + : FilterComboCache(() => data.ByType[type], MouseWheelType.None, Penumbra.Log) { - public ItemSelector(ItemData data, FullEquipType type) - : base(() => data.ByType[type], Penumbra.Log) - { } - protected override string ToString(EquipItem obj) => obj.Name; } - private class WeaponSelector : FilterComboCache - { - public WeaponSelector() - : base(FullEquipTypeExtensions.WeaponTypes.Concat(FullEquipTypeExtensions.ToolTypes), Penumbra.Log) - { } - - protected override string ToString(FullEquipType type) - => type.ToName(); - } - private readonly Dictionary _selectors; private readonly ItemSwapContainer _swapData; diff --git a/Penumbra/UI/CollectionTab/CollectionCombo.cs b/Penumbra/UI/CollectionTab/CollectionCombo.cs index b2ee5c3b..9d195eed 100644 --- a/Penumbra/UI/CollectionTab/CollectionCombo.cs +++ b/Penumbra/UI/CollectionTab/CollectionCombo.cs @@ -7,14 +7,10 @@ using Penumbra.GameData.Actors; namespace Penumbra.UI.CollectionTab; -public sealed class CollectionCombo : FilterComboCache +public sealed class CollectionCombo(CollectionManager manager, Func> items) + : FilterComboCache(items, MouseWheelType.None, Penumbra.Log) { - private readonly CollectionManager _collectionManager; - private readonly ImRaii.Color _color = new(); - - public CollectionCombo(CollectionManager manager, Func> items) - : base(items, Penumbra.Log) - => _collectionManager = manager; + private readonly ImRaii.Color _color = new(); protected override void DrawFilter(int currentSelected, float width) { @@ -24,11 +20,11 @@ public sealed class CollectionCombo : FilterComboCache public void Draw(string label, float width, uint color) { - var current = _collectionManager.Active.ByType(CollectionType.Current, ActorIdentifier.Invalid); + var current = manager.Active.ByType(CollectionType.Current, ActorIdentifier.Invalid); _color.Push(ImGuiCol.FrameBg, color).Push(ImGuiCol.FrameBgHovered, color); if (Draw(label, current?.Name ?? string.Empty, string.Empty, width, ImGui.GetTextLineHeightWithSpacing()) && CurrentSelection != null) - _collectionManager.Active.SetCollection(CurrentSelection, CollectionType.Current); + manager.Active.SetCollection(CurrentSelection, CollectionType.Current); _color.Dispose(); } From a0ac0dfcfa16befa83d5c9f94e9191e0c0bf7e40 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 13 Feb 2024 00:22:44 +0100 Subject: [PATCH 003/433] Bit more update. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 0d267233..3a7f6d86 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 0d267233724d493d9ae2bf8d1e67bfbb8b337916 +Subproject commit 3a7f6d86c9975a4892f58be3c629b7664e6c3733 From 06cf6ce752b1c864cad72b5e509984cc1065d44f Mon Sep 17 00:00:00 2001 From: Actions User Date: Tue, 13 Feb 2024 12:42:23 +0000 Subject: [PATCH 004/433] [CI] Updating repo.json for 1.0.0.5 --- repo.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/repo.json b/repo.json index fc95fef3..547ccca1 100644 --- a/repo.json +++ b/repo.json @@ -4,8 +4,8 @@ "Name": "Penumbra", "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", - "AssemblyVersion": "1.0.0.4", - "TestingAssemblyVersion": "1.0.0.4", + "AssemblyVersion": "1.0.0.5", + "TestingAssemblyVersion": "1.0.0.5", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -16,9 +16,9 @@ "LoadPriority": 69420, "LoadRequiredState": 2, "LoadSync": true, - "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.4/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.4/Penumbra.zip", - "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.4/Penumbra.zip", + "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.5/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.5/Penumbra.zip", + "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.5/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } ] From 2e0e125913d2b9e92587cdefc55e0650fcbd8469 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 17 Feb 2024 13:35:42 +0100 Subject: [PATCH 005/433] Fix issue with renaming mods with open advanced window. --- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index 6d406461..38fdf482 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -629,7 +629,10 @@ public partial class ModEditWindow : Window, IDisposable private void OnModPathChange(ModPathChangeType type, Mod mod, DirectoryInfo? _1, DirectoryInfo? _2) { - if (type is ModPathChangeType.Reloaded or ModPathChangeType.Moved) - ChangeMod(mod); + if (type is not (ModPathChangeType.Reloaded or ModPathChangeType.Moved) || mod != _mod) + return; + + _mod = null; + ChangeMod(mod); } } From 80ce6fe21f92793340267b01d8c4d9f3d2a3cafd Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 18 Feb 2024 12:59:06 +0100 Subject: [PATCH 006/433] Use new font functionality. --- Penumbra/UI/CollectionTab/CollectionPanel.cs | 11 ++++++----- Penumbra/UI/ModsTab/ModPanelHeader.cs | 9 +++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Penumbra/UI/CollectionTab/CollectionPanel.cs b/Penumbra/UI/CollectionTab/CollectionPanel.cs index 8f90750f..6f6e4dbd 100644 --- a/Penumbra/UI/CollectionTab/CollectionPanel.cs +++ b/Penumbra/UI/CollectionTab/CollectionPanel.cs @@ -2,6 +2,7 @@ using Dalamud.Game.ClientState.Objects; using Dalamud.Interface; using Dalamud.Interface.Components; using Dalamud.Interface.GameFonts; +using Dalamud.Interface.ManagedFontAtlas; using Dalamud.Interface.Utility; using Dalamud.Plugin; using ImGuiNET; @@ -28,7 +29,7 @@ public sealed class CollectionPanel : IDisposable private readonly InheritanceUi _inheritanceUi; private readonly ModStorage _mods; - private readonly GameFontHandle _nameFont; + private readonly IFontHandle _nameFont; private static readonly IReadOnlyDictionary Buttons = CreateButtons(); private static readonly IReadOnlyList<(CollectionType, bool, bool, string, uint)> AdvancedTree = CreateTree(); @@ -47,7 +48,7 @@ public sealed class CollectionPanel : IDisposable _mods = mods; _individualAssignmentUi = new IndividualAssignmentUi(communicator, actors, manager); _inheritanceUi = new InheritanceUi(manager, _selector); - _nameFont = pi.UiBuilder.GetGameFontHandle(new GameFontStyle(GameFontFamilyAndSize.Jupiter23)); + _nameFont = pi.UiBuilder.FontAtlas.NewGameFontHandle(new GameFontStyle(GameFontFamilyAndSize.Jupiter23)); } public void Dispose() @@ -426,7 +427,7 @@ public sealed class CollectionPanel : IDisposable ImGui.Dummy(Vector2.One); using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.MetaInfoText); using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, 2 * UiHelpers.Scale); - using var font = ImRaii.PushFont(_nameFont.ImFont, _nameFont.Available); + using var f = _nameFont.Available ? _nameFont.Push() : null; var name = Name(collection); var size = ImGui.CalcTextSize(name).X; var pos = ImGui.GetContentRegionAvail().X - size + ImGui.GetStyle().FramePadding.X * 2; @@ -445,7 +446,7 @@ public sealed class CollectionPanel : IDisposable if (_inUseCache.Count == 0 && collection.DirectParentOf.Count == 0) { ImGui.Dummy(Vector2.One); - using var font = ImRaii.PushFont(_nameFont.ImFont, _nameFont.Available); + using var f = _nameFont.Available ? _nameFont.Push() : null; ImGuiUtil.DrawTextButton("Collection is not used.", new Vector2(ImGui.GetContentRegionAvail().X, buttonHeight), Colors.PressEnterWarningBg); ImGui.Dummy(Vector2.One); @@ -512,7 +513,7 @@ public sealed class CollectionPanel : IDisposable ImGuiUtil.DrawTextButton("Inherited by", ImGui.GetContentRegionAvail() with { Y = 0 }, 0); } - using var font = ImRaii.PushFont(_nameFont.ImFont, _nameFont.Available); + using var f = _nameFont.Available ? _nameFont.Push() : null; using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale); using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.MetaInfoText); ImGuiUtil.DrawTextButton(Name(collection.DirectParentOf[0]), Vector2.Zero, 0); diff --git a/Penumbra/UI/ModsTab/ModPanelHeader.cs b/Penumbra/UI/ModsTab/ModPanelHeader.cs index 4b127059..a28c3668 100644 --- a/Penumbra/UI/ModsTab/ModPanelHeader.cs +++ b/Penumbra/UI/ModsTab/ModPanelHeader.cs @@ -1,4 +1,5 @@ using Dalamud.Interface.GameFonts; +using Dalamud.Interface.ManagedFontAtlas; using Dalamud.Plugin; using ImGuiNET; using OtterGui; @@ -14,14 +15,14 @@ namespace Penumbra.UI.ModsTab; public class ModPanelHeader : IDisposable { /// We use a big, nice game font for the title. - private readonly GameFontHandle _nameFont; + private readonly IFontHandle _nameFont; private readonly CommunicatorService _communicator; public ModPanelHeader(DalamudPluginInterface pi, CommunicatorService communicator) { _communicator = communicator; - _nameFont = pi.UiBuilder.GetGameFontHandle(new GameFontStyle(GameFontFamilyAndSize.Jupiter23)); + _nameFont = pi.UiBuilder.FontAtlas.NewGameFontHandle(new GameFontStyle(GameFontFamilyAndSize.Jupiter23)); _communicator.ModDataChanged.Subscribe(OnModDataChange, ModDataChanged.Priority.ModPanelHeader); } @@ -46,7 +47,7 @@ public class ModPanelHeader : IDisposable var name = $" {mod.Name} "; if (name != _modName) { - using var font = ImRaii.PushFont(_nameFont.ImFont, _nameFont.Available); + using var f = _nameFont.Available ? _nameFont.Push() : null; _modName = name; _modNameWidth = ImGui.CalcTextSize(name).X + 2 * (ImGui.GetStyle().FramePadding.X + 2 * UiHelpers.Scale); } @@ -121,7 +122,7 @@ public class ModPanelHeader : IDisposable using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.MetaInfoText); using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, 2 * UiHelpers.Scale); - using var font = ImRaii.PushFont(_nameFont.ImFont, _nameFont.Available); + using var f = _nameFont.Available ? _nameFont.Push() : null; ImGuiUtil.DrawTextButton(_modName, Vector2.Zero, 0); return offset; } From 31bc5ec6f9d4b21dfa1b09f8764f87860cffc14b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 18 Feb 2024 13:03:56 +0100 Subject: [PATCH 007/433] Make settings change invoke on Temporary Mods. --- Penumbra.Api | 2 +- Penumbra/Api/TempModManager.cs | 3 +++ Penumbra/Collections/Cache/CollectionCacheManager.cs | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Penumbra.Api b/Penumbra.Api index a28219ac..2b6bcf33 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit a28219ac57b53c3be6ca8c252ceb9f76ae0b6c21 +Subproject commit 2b6bcf338794b34bcba2730c70dcbb73ce97311b diff --git a/Penumbra/Api/TempModManager.cs b/Penumbra/Api/TempModManager.cs index efbfd7f9..c7840b75 100644 --- a/Penumbra/Api/TempModManager.cs +++ b/Penumbra/Api/TempModManager.cs @@ -1,3 +1,4 @@ +using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.Meta.Manipulations; using Penumbra.Mods; @@ -84,11 +85,13 @@ public class TempModManager : IDisposable { Penumbra.Log.Verbose($"Removing temporary Mod {mod.Name} from {collection.AnonymizedName}."); collection.Remove(mod); + _communicator.ModSettingChanged.Invoke(collection, ModSettingChange.TemporaryMod, null, 0, 0, false); } else { Penumbra.Log.Verbose($"Adding {(created ? "new " : string.Empty)}temporary Mod {mod.Name} to {collection.AnonymizedName}."); collection.Apply(mod, created); + _communicator.ModSettingChanged.Invoke(collection, ModSettingChange.TemporaryMod, null, 1, 0, false); } } else diff --git a/Penumbra/Collections/Cache/CollectionCacheManager.cs b/Penumbra/Collections/Cache/CollectionCacheManager.cs index 7d4a5722..4e524ddf 100644 --- a/Penumbra/Collections/Cache/CollectionCacheManager.cs +++ b/Penumbra/Collections/Cache/CollectionCacheManager.cs @@ -322,6 +322,9 @@ public class CollectionCacheManager : IDisposable case ModSettingChange.MultiEnableState: FullRecalculation(collection); break; + case ModSettingChange.TemporaryMod: + // handled otherwise + break; } } From a2bf477481f78c62333947498fc21c092fc505f8 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 18 Feb 2024 14:46:22 +0100 Subject: [PATCH 008/433] Cleanup. --- Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs | 2 +- Penumbra/UI/ModsTab/ModFileSystemSelector.cs | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs b/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs index 776f2f92..31387101 100644 --- a/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs +++ b/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs @@ -42,7 +42,7 @@ public sealed unsafe class ResourceHandleDestructor : EventWrapperPtr Date: Sun, 18 Feb 2024 13:48:18 +0000 Subject: [PATCH 009/433] [CI] Updating repo.json for 1.0.0.6 --- repo.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/repo.json b/repo.json index 547ccca1..29b1647d 100644 --- a/repo.json +++ b/repo.json @@ -4,8 +4,8 @@ "Name": "Penumbra", "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", - "AssemblyVersion": "1.0.0.5", - "TestingAssemblyVersion": "1.0.0.5", + "AssemblyVersion": "1.0.0.6", + "TestingAssemblyVersion": "1.0.0.6", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -16,9 +16,9 @@ "LoadPriority": 69420, "LoadRequiredState": 2, "LoadSync": true, - "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.5/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.5/Penumbra.zip", - "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.5/Penumbra.zip", + "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.6/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.6/Penumbra.zip", + "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.6/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } ] From 529788d2e57b95adf434dac84b119b17c02921d7 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 20 Feb 2024 16:26:33 +0100 Subject: [PATCH 010/433] Change font pushes. --- Penumbra/UI/CollectionTab/CollectionPanel.cs | 6 +++--- Penumbra/UI/ModsTab/ModPanelHeader.cs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Penumbra/UI/CollectionTab/CollectionPanel.cs b/Penumbra/UI/CollectionTab/CollectionPanel.cs index 6f6e4dbd..4d922af5 100644 --- a/Penumbra/UI/CollectionTab/CollectionPanel.cs +++ b/Penumbra/UI/CollectionTab/CollectionPanel.cs @@ -427,7 +427,7 @@ public sealed class CollectionPanel : IDisposable ImGui.Dummy(Vector2.One); using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.MetaInfoText); using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, 2 * UiHelpers.Scale); - using var f = _nameFont.Available ? _nameFont.Push() : null; + using var f = _nameFont.Push(); var name = Name(collection); var size = ImGui.CalcTextSize(name).X; var pos = ImGui.GetContentRegionAvail().X - size + ImGui.GetStyle().FramePadding.X * 2; @@ -446,7 +446,7 @@ public sealed class CollectionPanel : IDisposable if (_inUseCache.Count == 0 && collection.DirectParentOf.Count == 0) { ImGui.Dummy(Vector2.One); - using var f = _nameFont.Available ? _nameFont.Push() : null; + using var f = _nameFont.Push(); ImGuiUtil.DrawTextButton("Collection is not used.", new Vector2(ImGui.GetContentRegionAvail().X, buttonHeight), Colors.PressEnterWarningBg); ImGui.Dummy(Vector2.One); @@ -513,7 +513,7 @@ public sealed class CollectionPanel : IDisposable ImGuiUtil.DrawTextButton("Inherited by", ImGui.GetContentRegionAvail() with { Y = 0 }, 0); } - using var f = _nameFont.Available ? _nameFont.Push() : null; + using var f = _nameFont.Push(); using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale); using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.MetaInfoText); ImGuiUtil.DrawTextButton(Name(collection.DirectParentOf[0]), Vector2.Zero, 0); diff --git a/Penumbra/UI/ModsTab/ModPanelHeader.cs b/Penumbra/UI/ModsTab/ModPanelHeader.cs index a28c3668..ed499b4f 100644 --- a/Penumbra/UI/ModsTab/ModPanelHeader.cs +++ b/Penumbra/UI/ModsTab/ModPanelHeader.cs @@ -47,7 +47,7 @@ public class ModPanelHeader : IDisposable var name = $" {mod.Name} "; if (name != _modName) { - using var f = _nameFont.Available ? _nameFont.Push() : null; + using var f = _nameFont.Push(); _modName = name; _modNameWidth = ImGui.CalcTextSize(name).X + 2 * (ImGui.GetStyle().FramePadding.X + 2 * UiHelpers.Scale); } @@ -122,7 +122,7 @@ public class ModPanelHeader : IDisposable using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.MetaInfoText); using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, 2 * UiHelpers.Scale); - using var f = _nameFont.Available ? _nameFont.Push() : null; + using var f = _nameFont.Push(); ImGuiUtil.DrawTextButton(_modName, Vector2.Zero, 0); return offset; } From add4b8aa83b55f1e274bd994396faaeda3809226 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 23 Feb 2024 21:44:49 +0100 Subject: [PATCH 011/433] Misc. --- Penumbra/Collections/ModCollectionSave.cs | 31 ++++++------------- .../ResourceLoading/CreateFileWHook.cs | 2 +- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/Penumbra/Collections/ModCollectionSave.cs b/Penumbra/Collections/ModCollectionSave.cs index 4cc7706e..f2cb4ada 100644 --- a/Penumbra/Collections/ModCollectionSave.cs +++ b/Penumbra/Collections/ModCollectionSave.cs @@ -1,32 +1,21 @@ using Newtonsoft.Json.Linq; -using Penumbra.Mods; using Penumbra.Services; using Newtonsoft.Json; using Penumbra.Mods.Manager; using Penumbra.Mods.Subclasses; -using Penumbra.Util; namespace Penumbra.Collections; /// /// Handle saving and loading a collection. /// -internal readonly struct ModCollectionSave : ISavable +internal readonly struct ModCollectionSave(ModStorage modStorage, ModCollection modCollection) : ISavable { - private readonly ModStorage _modStorage; - private readonly ModCollection _modCollection; - - public ModCollectionSave(ModStorage modStorage, ModCollection modCollection) - { - _modStorage = modStorage; - _modCollection = modCollection; - } - public string ToFilename(FilenameService fileNames) - => fileNames.CollectionFile(_modCollection); + => fileNames.CollectionFile(modCollection); public string LogName(string _) - => _modCollection.AnonymizedName; + => modCollection.AnonymizedName; public string TypeName => "Collection"; @@ -40,20 +29,20 @@ internal readonly struct ModCollectionSave : ISavable j.WritePropertyName("Version"); j.WriteValue(ModCollection.CurrentVersion); j.WritePropertyName(nameof(ModCollection.Name)); - j.WriteValue(_modCollection.Name); + j.WriteValue(modCollection.Name); j.WritePropertyName(nameof(ModCollection.Settings)); // Write all used and unused settings by mod directory name. j.WriteStartObject(); - var list = new List<(string, ModSettings.SavedSettings)>(_modCollection.Settings.Count + _modCollection.UnusedSettings.Count); - for (var i = 0; i < _modCollection.Settings.Count; ++i) + var list = new List<(string, ModSettings.SavedSettings)>(modCollection.Settings.Count + modCollection.UnusedSettings.Count); + for (var i = 0; i < modCollection.Settings.Count; ++i) { - var settings = _modCollection.Settings[i]; + var settings = modCollection.Settings[i]; if (settings != null) - list.Add((_modStorage[i].ModPath.Name, new ModSettings.SavedSettings(settings, _modStorage[i]))); + list.Add((modStorage[i].ModPath.Name, new ModSettings.SavedSettings(settings, modStorage[i]))); } - list.AddRange(_modCollection.UnusedSettings.Select(kvp => (kvp.Key, kvp.Value))); + list.AddRange(modCollection.UnusedSettings.Select(kvp => (kvp.Key, kvp.Value))); list.Sort((a, b) => string.Compare(a.Item1, b.Item1, StringComparison.OrdinalIgnoreCase)); foreach (var (modDir, settings) in list) @@ -66,7 +55,7 @@ internal readonly struct ModCollectionSave : ISavable // Inherit by collection name. j.WritePropertyName("Inheritance"); - x.Serialize(j, _modCollection.InheritanceByName ?? _modCollection.DirectlyInheritsFrom.Select(c => c.Name)); + x.Serialize(j, modCollection.InheritanceByName ?? modCollection.DirectlyInheritsFrom.Select(c => c.Name)); j.WriteEndObject(); } diff --git a/Penumbra/Interop/ResourceLoading/CreateFileWHook.cs b/Penumbra/Interop/ResourceLoading/CreateFileWHook.cs index 7d94b1d5..8a5e779b 100644 --- a/Penumbra/Interop/ResourceLoading/CreateFileWHook.cs +++ b/Penumbra/Interop/ResourceLoading/CreateFileWHook.cs @@ -100,7 +100,7 @@ public unsafe class CreateFileWHook : IDisposable { // Use static storage. var ptr = WriteFileName(name); - Penumbra.Log.Verbose($"[ResourceHooks] Calling CreateFileWDetour with {ByteString.FromSpanUnsafe(name, false)}."); + Penumbra.Log.Excessive($"[ResourceHooks] Calling CreateFileWDetour with {ByteString.FromSpanUnsafe(name, false)}."); return _createFileWHook.OriginalDisposeSafe(ptr, access, shareMode, security, creation, flags, template); } From 1000841f69b8c439e498d2b36986514aadc9d3a7 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 24 Feb 2024 12:06:57 +0100 Subject: [PATCH 012/433] Add some settingchanged events. --- Penumbra.Api | 2 +- Penumbra/Api/PenumbraApi.cs | 46 +++++--- .../Cache/CollectionCacheManager.cs | 1 + .../Collections/Manager/CollectionManager.cs | 31 +++--- Penumbra/Communication/ModOptionChanged.cs | 4 + Penumbra/Communication/ModPathChanged.cs | 6 +- Penumbra/Mods/Editor/ModMetaEditor.cs | 2 +- Penumbra/Mods/Manager/ModDataEditor.cs | 61 +++++------ Penumbra/Mods/Manager/ModOptionEditor.cs | 101 ++++++++---------- 9 files changed, 128 insertions(+), 126 deletions(-) diff --git a/Penumbra.Api b/Penumbra.Api index 2b6bcf33..79ffdd69 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit 2b6bcf338794b34bcba2730c70dcbb73ce97311b +Subproject commit 79ffdd69a28141a1ac93daa24d76573b2fa0d71e diff --git a/Penumbra/Api/PenumbraApi.cs b/Penumbra/Api/PenumbraApi.cs index 1a0764c7..59ef6677 100644 --- a/Penumbra/Api/PenumbraApi.cs +++ b/Penumbra/Api/PenumbraApi.cs @@ -141,6 +141,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi _communicator.ModPathChanged.Subscribe(ModPathChangeSubscriber, ModPathChanged.Priority.Api); _communicator.ModSettingChanged.Subscribe(OnModSettingChange, Communication.ModSettingChanged.Priority.Api); _communicator.CreatedCharacterBase.Subscribe(OnCreatedCharacterBase, Communication.CreatedCharacterBase.Priority.Api); + _communicator.ModOptionChanged.Subscribe(OnModOptionEdited, ModOptionChanged.Priority.Api); } public unsafe void Dispose() @@ -342,10 +343,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi { CheckInitialized(); if (!_config.EnableMods) - return new[] - { - path, - }; + return [path]; var ret = _collectionManager.Active.Individual(NameToIdentifier(characterName, worldId)).ReverseResolvePath(new FullPath(path)); return ret.Select(r => r.ToString()).ToArray(); @@ -355,10 +353,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi { CheckInitialized(); if (!_config.EnableMods) - return new[] - { - path, - }; + return [path]; AssociatedCollection(gameObjectIdx, out var collection); var ret = collection.ReverseResolvePath(new FullPath(path)); @@ -369,10 +364,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi { CheckInitialized(); if (!_config.EnableMods) - return new[] - { - path, - }; + return [path]; var ret = _collectionResolver.PlayerCollection().ReverseResolvePath(new FullPath(path)); return ret.Select(r => r.ToString()).ToArray(); @@ -698,6 +690,9 @@ public class PenumbraApi : IDisposable, IPenumbraApi { switch (type) { + case ModPathChangeType.Reloaded: + TriggerSettingEdited(mod); + break; case ModPathChangeType.Deleted when oldDirectory != null: ModDeleted?.Invoke(oldDirectory.Name); break; @@ -1262,4 +1257,31 @@ public class PenumbraApi : IDisposable, IPenumbraApi private void OnCreatedCharacterBase(nint gameObject, ModCollection collection, nint drawObject) => CreatedCharacterBase?.Invoke(gameObject, collection.Name, drawObject); + + private void OnModOptionEdited(ModOptionChangeType type, Mod mod, int groupIndex, int optionIndex, int moveIndex) + { + switch (type) + { + case ModOptionChangeType.GroupDeleted: + case ModOptionChangeType.GroupMoved: + case ModOptionChangeType.GroupTypeChanged: + case ModOptionChangeType.PriorityChanged: + case ModOptionChangeType.OptionDeleted: + case ModOptionChangeType.OptionMoved: + case ModOptionChangeType.OptionFilesChanged: + case ModOptionChangeType.OptionFilesAdded: + case ModOptionChangeType.OptionSwapsChanged: + case ModOptionChangeType.OptionMetaChanged: + TriggerSettingEdited(mod); + break; + } + } + + private void TriggerSettingEdited(Mod mod) + { + var collection = _collectionResolver.PlayerCollection(); + var (settings, parent) = collection[mod.Index]; + if (settings != null) + ModSettingChanged?.Invoke(ModSettingChange.Edited, collection.Name, mod.Identifier, parent != collection); + } } diff --git a/Penumbra/Collections/Cache/CollectionCacheManager.cs b/Penumbra/Collections/Cache/CollectionCacheManager.cs index 4e524ddf..94b3ef5a 100644 --- a/Penumbra/Collections/Cache/CollectionCacheManager.cs +++ b/Penumbra/Collections/Cache/CollectionCacheManager.cs @@ -323,6 +323,7 @@ public class CollectionCacheManager : IDisposable FullRecalculation(collection); break; case ModSettingChange.TemporaryMod: + case ModSettingChange.Edited: // handled otherwise break; } diff --git a/Penumbra/Collections/Manager/CollectionManager.cs b/Penumbra/Collections/Manager/CollectionManager.cs index 16bf754c..e95617b1 100644 --- a/Penumbra/Collections/Manager/CollectionManager.cs +++ b/Penumbra/Collections/Manager/CollectionManager.cs @@ -2,23 +2,18 @@ using Penumbra.Collections.Cache; namespace Penumbra.Collections.Manager; -public class CollectionManager +public class CollectionManager( + CollectionStorage storage, + ActiveCollections active, + InheritanceManager inheritances, + CollectionCacheManager caches, + TempCollectionManager temp, + CollectionEditor editor) { - public readonly CollectionStorage Storage; - public readonly ActiveCollections Active; - public readonly InheritanceManager Inheritances; - public readonly CollectionCacheManager Caches; - public readonly TempCollectionManager Temp; - public readonly CollectionEditor Editor; - - public CollectionManager(CollectionStorage storage, ActiveCollections active, InheritanceManager inheritances, - CollectionCacheManager caches, TempCollectionManager temp, CollectionEditor editor) - { - Storage = storage; - Active = active; - Inheritances = inheritances; - Caches = caches; - Temp = temp; - Editor = editor; - } + public readonly CollectionStorage Storage = storage; + public readonly ActiveCollections Active = active; + public readonly InheritanceManager Inheritances = inheritances; + public readonly CollectionCacheManager Caches = caches; + public readonly TempCollectionManager Temp = temp; + public readonly CollectionEditor Editor = editor; } diff --git a/Penumbra/Communication/ModOptionChanged.cs b/Penumbra/Communication/ModOptionChanged.cs index a0b4d26c..f02b17dc 100644 --- a/Penumbra/Communication/ModOptionChanged.cs +++ b/Penumbra/Communication/ModOptionChanged.cs @@ -1,4 +1,5 @@ using OtterGui.Classes; +using Penumbra.Api; using Penumbra.Mods; using Penumbra.Mods.Manager; @@ -18,6 +19,9 @@ public sealed class ModOptionChanged() { public enum Priority { + /// + Api = int.MinValue, + /// CollectionCacheManager = -100, diff --git a/Penumbra/Communication/ModPathChanged.cs b/Penumbra/Communication/ModPathChanged.cs index e6291781..01c8fa64 100644 --- a/Penumbra/Communication/ModPathChanged.cs +++ b/Penumbra/Communication/ModPathChanged.cs @@ -19,15 +19,15 @@ public sealed class ModPathChanged() { public enum Priority { + /// + Api = int.MinValue, + /// EphemeralConfig = -500, /// CollectionCacheManagerAddition = -100, - /// - Api = 0, - /// ModCacheManager = 0, diff --git a/Penumbra/Mods/Editor/ModMetaEditor.cs b/Penumbra/Mods/Editor/ModMetaEditor.cs index bbf0d4b5..31aefdf5 100644 --- a/Penumbra/Mods/Editor/ModMetaEditor.cs +++ b/Penumbra/Mods/Editor/ModMetaEditor.cs @@ -2,7 +2,7 @@ using Penumbra.Meta.Manipulations; using Penumbra.Mods.Manager; using Penumbra.Mods.Subclasses; -namespace Penumbra.Mods; +namespace Penumbra.Mods.Editor; public class ModMetaEditor(ModManager modManager) { diff --git a/Penumbra/Mods/Manager/ModDataEditor.cs b/Penumbra/Mods/Manager/ModDataEditor.cs index 6c5f9c25..e0af6f36 100644 --- a/Penumbra/Mods/Manager/ModDataEditor.cs +++ b/Penumbra/Mods/Manager/ModDataEditor.cs @@ -23,17 +23,8 @@ public enum ModDataChangeType : ushort Note = 0x0800, } -public class ModDataEditor +public class ModDataEditor(SaveService saveService, CommunicatorService communicatorService) { - private readonly SaveService _saveService; - private readonly CommunicatorService _communicatorService; - - public ModDataEditor(SaveService saveService, CommunicatorService communicatorService) - { - _saveService = saveService; - _communicatorService = communicatorService; - } - /// Create the file containing the meta information about a mod from scratch. public void CreateMeta(DirectoryInfo directory, string? name, string? author, string? description, string? version, string? website) @@ -44,12 +35,12 @@ public class ModDataEditor mod.Description = description ?? mod.Description; mod.Version = version ?? mod.Version; mod.Website = website ?? mod.Website; - _saveService.ImmediateSave(new ModMeta(mod)); + saveService.ImmediateSave(new ModMeta(mod)); } public ModDataChangeType LoadLocalData(Mod mod) { - var dataFile = _saveService.FileNames.LocalDataFile(mod); + var dataFile = saveService.FileNames.LocalDataFile(mod); var importDate = 0L; var localTags = Enumerable.Empty(); @@ -101,14 +92,14 @@ public class ModDataEditor } if (save) - _saveService.QueueSave(new ModLocalData(mod)); + saveService.QueueSave(new ModLocalData(mod)); return changes; } public ModDataChangeType LoadMeta(ModCreator creator, Mod mod) { - var metaFile = _saveService.FileNames.ModMetaPath(mod); + var metaFile = saveService.FileNames.ModMetaPath(mod); if (!File.Exists(metaFile)) { Penumbra.Log.Debug($"No mod meta found for {mod.ModPath.Name}."); @@ -161,10 +152,10 @@ public class ModDataEditor } if (newFileVersion != ModMeta.FileVersion) - if (ModMigration.Migrate(creator, _saveService, mod, json, ref newFileVersion)) + if (ModMigration.Migrate(creator, saveService, mod, json, ref newFileVersion)) { changes |= ModDataChangeType.Migration; - _saveService.ImmediateSave(new ModMeta(mod)); + saveService.ImmediateSave(new ModMeta(mod)); } if (importDate != null && mod.ImportDate != importDate.Value) @@ -191,8 +182,8 @@ public class ModDataEditor var oldName = mod.Name; mod.Name = newName; - _saveService.QueueSave(new ModMeta(mod)); - _communicatorService.ModDataChanged.Invoke(ModDataChangeType.Name, mod, oldName.Text); + saveService.QueueSave(new ModMeta(mod)); + communicatorService.ModDataChanged.Invoke(ModDataChangeType.Name, mod, oldName.Text); } public void ChangeModAuthor(Mod mod, string newAuthor) @@ -201,8 +192,8 @@ public class ModDataEditor return; mod.Author = newAuthor; - _saveService.QueueSave(new ModMeta(mod)); - _communicatorService.ModDataChanged.Invoke(ModDataChangeType.Author, mod, null); + saveService.QueueSave(new ModMeta(mod)); + communicatorService.ModDataChanged.Invoke(ModDataChangeType.Author, mod, null); } public void ChangeModDescription(Mod mod, string newDescription) @@ -211,8 +202,8 @@ public class ModDataEditor return; mod.Description = newDescription; - _saveService.QueueSave(new ModMeta(mod)); - _communicatorService.ModDataChanged.Invoke(ModDataChangeType.Description, mod, null); + saveService.QueueSave(new ModMeta(mod)); + communicatorService.ModDataChanged.Invoke(ModDataChangeType.Description, mod, null); } public void ChangeModVersion(Mod mod, string newVersion) @@ -221,8 +212,8 @@ public class ModDataEditor return; mod.Version = newVersion; - _saveService.QueueSave(new ModMeta(mod)); - _communicatorService.ModDataChanged.Invoke(ModDataChangeType.Version, mod, null); + saveService.QueueSave(new ModMeta(mod)); + communicatorService.ModDataChanged.Invoke(ModDataChangeType.Version, mod, null); } public void ChangeModWebsite(Mod mod, string newWebsite) @@ -231,8 +222,8 @@ public class ModDataEditor return; mod.Website = newWebsite; - _saveService.QueueSave(new ModMeta(mod)); - _communicatorService.ModDataChanged.Invoke(ModDataChangeType.Website, mod, null); + saveService.QueueSave(new ModMeta(mod)); + communicatorService.ModDataChanged.Invoke(ModDataChangeType.Website, mod, null); } public void ChangeModTag(Mod mod, int tagIdx, string newTag) @@ -247,9 +238,9 @@ public class ModDataEditor return; mod.Favorite = state; - _saveService.QueueSave(new ModLocalData(mod)); + saveService.QueueSave(new ModLocalData(mod)); ; - _communicatorService.ModDataChanged.Invoke(ModDataChangeType.Favorite, mod, null); + communicatorService.ModDataChanged.Invoke(ModDataChangeType.Favorite, mod, null); } public void ChangeModNote(Mod mod, string newNote) @@ -258,9 +249,9 @@ public class ModDataEditor return; mod.Note = newNote; - _saveService.QueueSave(new ModLocalData(mod)); + saveService.QueueSave(new ModLocalData(mod)); ; - _communicatorService.ModDataChanged.Invoke(ModDataChangeType.Favorite, mod, null); + communicatorService.ModDataChanged.Invoke(ModDataChangeType.Favorite, mod, null); } private void ChangeTag(Mod mod, int tagIdx, string newTag, bool local) @@ -282,19 +273,19 @@ public class ModDataEditor } if (flags.HasFlag(ModDataChangeType.ModTags)) - _saveService.QueueSave(new ModMeta(mod)); + saveService.QueueSave(new ModMeta(mod)); if (flags.HasFlag(ModDataChangeType.LocalTags)) - _saveService.QueueSave(new ModLocalData(mod)); + saveService.QueueSave(new ModLocalData(mod)); if (flags != 0) - _communicatorService.ModDataChanged.Invoke(flags, mod, null); + communicatorService.ModDataChanged.Invoke(flags, mod, null); } public void MoveDataFile(DirectoryInfo oldMod, DirectoryInfo newMod) { - var oldFile = _saveService.FileNames.LocalDataFile(oldMod.Name); - var newFile = _saveService.FileNames.LocalDataFile(newMod.Name); + var oldFile = saveService.FileNames.LocalDataFile(oldMod.Name); + var newFile = saveService.FileNames.LocalDataFile(newMod.Name); if (!File.Exists(oldFile)) return; diff --git a/Penumbra/Mods/Manager/ModOptionEditor.cs b/Penumbra/Mods/Manager/ModOptionEditor.cs index 73cb80cc..3459ce1a 100644 --- a/Penumbra/Mods/Manager/ModOptionEditor.cs +++ b/Penumbra/Mods/Manager/ModOptionEditor.cs @@ -31,19 +31,8 @@ public enum ModOptionChangeType DefaultOptionChanged, } -public class ModOptionEditor +public class ModOptionEditor(CommunicatorService communicator, SaveService saveService, Configuration config) { - private readonly Configuration _config; - private readonly CommunicatorService _communicator; - private readonly SaveService _saveService; - - public ModOptionEditor(CommunicatorService communicator, SaveService saveService, Configuration config) - { - _communicator = communicator; - _saveService = saveService; - _config = config; - } - /// Change the type of a group given by mod and index to type, if possible. public void ChangeModGroupType(Mod mod, int groupIdx, GroupType type) { @@ -52,8 +41,8 @@ public class ModOptionEditor return; mod.Groups[groupIdx] = group.Convert(type); - _saveService.QueueSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupTypeChanged, mod, groupIdx, -1, -1); + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupTypeChanged, mod, groupIdx, -1, -1); } /// Change the settings stored as default options in a mod. @@ -64,8 +53,8 @@ public class ModOptionEditor return; group.DefaultSettings = defaultOption; - _saveService.QueueSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.DefaultOptionChanged, mod, groupIdx, -1, -1); + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.DefaultOptionChanged, mod, groupIdx, -1, -1); } /// Rename an option group if possible. @@ -76,7 +65,7 @@ public class ModOptionEditor if (oldName == newName || !VerifyFileName(mod, group, newName, true)) return; - _saveService.ImmediateDelete(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); + saveService.ImmediateDelete(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); var _ = group switch { SingleModGroup s => s.Name = newName, @@ -84,8 +73,8 @@ public class ModOptionEditor _ => newName, }; - _saveService.ImmediateSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupRenamed, mod, groupIdx, -1, -1); + saveService.ImmediateSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupRenamed, mod, groupIdx, -1, -1); } /// Add a new, empty option group of the given type and name. @@ -107,8 +96,8 @@ public class ModOptionEditor Name = newName, Priority = maxPriority, }); - _saveService.ImmediateSave(new ModSaveGroup(mod, mod.Groups.Count - 1, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupAdded, mod, mod.Groups.Count - 1, -1, -1); + saveService.ImmediateSave(new ModSaveGroup(mod, mod.Groups.Count - 1, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupAdded, mod, mod.Groups.Count - 1, -1, -1); } /// Add a new mod, empty option group of the given type and name if it does not exist already. @@ -128,11 +117,11 @@ public class ModOptionEditor /// Delete a given option group. Fires an event to prepare before actually deleting. public void DeleteModGroup(Mod mod, int groupIdx) { - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, -1, -1); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, -1, -1); mod.Groups.RemoveAt(groupIdx); UpdateSubModPositions(mod, groupIdx); - _saveService.SaveAllOptionGroups(mod, false, _config.ReplaceNonAsciiOnImport); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupDeleted, mod, groupIdx, -1, -1); + saveService.SaveAllOptionGroups(mod, false, config.ReplaceNonAsciiOnImport); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupDeleted, mod, groupIdx, -1, -1); } /// Move the index of a given option group. @@ -142,8 +131,8 @@ public class ModOptionEditor return; UpdateSubModPositions(mod, Math.Min(groupIdxFrom, groupIdxTo)); - _saveService.SaveAllOptionGroups(mod, false, _config.ReplaceNonAsciiOnImport); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupMoved, mod, groupIdxFrom, -1, groupIdxTo); + saveService.SaveAllOptionGroups(mod, false, config.ReplaceNonAsciiOnImport); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupMoved, mod, groupIdxFrom, -1, groupIdxTo); } /// Change the description of the given option group. @@ -159,8 +148,8 @@ public class ModOptionEditor MultiModGroup m => m.Description = newDescription, _ => newDescription, }; - _saveService.QueueSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, mod, groupIdx, -1, -1); + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, mod, groupIdx, -1, -1); } /// Change the description of the given option. @@ -172,8 +161,8 @@ public class ModOptionEditor return; s.Description = newDescription; - _saveService.QueueSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, mod, groupIdx, optionIdx, -1); + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, mod, groupIdx, optionIdx, -1); } /// Change the internal priority of the given option group. @@ -189,8 +178,8 @@ public class ModOptionEditor MultiModGroup m => m.Priority = newPriority, _ => newPriority, }; - _saveService.QueueSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.PriorityChanged, mod, groupIdx, -1, -1); + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.PriorityChanged, mod, groupIdx, -1, -1); } /// Change the internal priority of the given option. @@ -206,8 +195,8 @@ public class ModOptionEditor return; m.PrioritizedOptions[optionIdx] = (m.PrioritizedOptions[optionIdx].Mod, newPriority); - _saveService.QueueSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.PriorityChanged, mod, groupIdx, optionIdx, -1); + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.PriorityChanged, mod, groupIdx, optionIdx, -1); return; } } @@ -232,8 +221,8 @@ public class ModOptionEditor break; } - _saveService.QueueSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, mod, groupIdx, optionIdx, -1); + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, mod, groupIdx, optionIdx, -1); } /// Add a new empty option of the given name for the given group. @@ -252,8 +241,8 @@ public class ModOptionEditor break; } - _saveService.QueueSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, group.Count - 1, -1); + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, group.Count - 1, -1); } /// Add a new empty option of the given name for the given group if it does not exist already. @@ -298,15 +287,15 @@ public class ModOptionEditor break; } - _saveService.QueueSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, group.Count - 1, -1); + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, group.Count - 1, -1); } /// Delete the given option from the given group. public void DeleteOption(Mod mod, int groupIdx, int optionIdx) { var group = mod.Groups[groupIdx]; - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, optionIdx, -1); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, optionIdx, -1); switch (group) { case SingleModGroup s: @@ -319,8 +308,8 @@ public class ModOptionEditor } group.UpdatePositions(optionIdx); - _saveService.QueueSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionDeleted, mod, groupIdx, optionIdx, -1); + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionDeleted, mod, groupIdx, optionIdx, -1); } /// Move an option inside the given option group. @@ -330,8 +319,8 @@ public class ModOptionEditor if (!group.MoveOption(optionIdxFrom, optionIdxTo)) return; - _saveService.QueueSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMoved, mod, groupIdx, optionIdxFrom, optionIdxTo); + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMoved, mod, groupIdx, optionIdxFrom, optionIdxTo); } /// Set the meta manipulations for a given option. Replaces existing manipulations. @@ -342,10 +331,10 @@ public class ModOptionEditor && subMod.Manipulations.All(m => manipulations.TryGetValue(m, out var old) && old.EntryEquals(m))) return; - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, optionIdx, -1); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, optionIdx, -1); subMod.ManipulationData.SetTo(manipulations); - _saveService.QueueSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMetaChanged, mod, groupIdx, optionIdx, -1); + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMetaChanged, mod, groupIdx, optionIdx, -1); } /// Set the file redirections for a given option. Replaces existing redirections. @@ -355,10 +344,10 @@ public class ModOptionEditor if (subMod.FileData.SetEquals(replacements)) return; - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, optionIdx, -1); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, optionIdx, -1); subMod.FileData.SetTo(replacements); - _saveService.QueueSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesChanged, mod, groupIdx, optionIdx, -1); + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesChanged, mod, groupIdx, optionIdx, -1); } /// Add additional file redirections to a given option, keeping already existing ones. Only fires an event if anything is actually added. @@ -369,8 +358,8 @@ public class ModOptionEditor subMod.FileData.AddFrom(additions); if (oldCount != subMod.FileData.Count) { - _saveService.QueueSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesAdded, mod, groupIdx, optionIdx, -1); + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesAdded, mod, groupIdx, optionIdx, -1); } } @@ -381,10 +370,10 @@ public class ModOptionEditor if (subMod.FileSwapData.SetEquals(swaps)) return; - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, optionIdx, -1); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, optionIdx, -1); subMod.FileSwapData.SetTo(swaps); - _saveService.QueueSave(new ModSaveGroup(mod, groupIdx, _config.ReplaceNonAsciiOnImport)); - _communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionSwapsChanged, mod, groupIdx, optionIdx, -1); + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionSwapsChanged, mod, groupIdx, optionIdx, -1); } From 883580d4654c66a92e0435a628878d3b4fe38a17 Mon Sep 17 00:00:00 2001 From: Actions User Date: Sat, 24 Feb 2024 11:09:19 +0000 Subject: [PATCH 013/433] [CI] Updating repo.json for 1.0.0.7 --- repo.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/repo.json b/repo.json index 29b1647d..1a599362 100644 --- a/repo.json +++ b/repo.json @@ -4,8 +4,8 @@ "Name": "Penumbra", "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", - "AssemblyVersion": "1.0.0.6", - "TestingAssemblyVersion": "1.0.0.6", + "AssemblyVersion": "1.0.0.7", + "TestingAssemblyVersion": "1.0.0.7", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -16,9 +16,9 @@ "LoadPriority": 69420, "LoadRequiredState": 2, "LoadSync": true, - "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.6/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.6/Penumbra.zip", - "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.6/Penumbra.zip", + "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.7/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.7/Penumbra.zip", + "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.7/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } ] From 7b0be25f6ee4d07eae56248887abbb16847b1160 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 24 Feb 2024 14:04:39 +0100 Subject: [PATCH 014/433] Add even more setting changed events and add auto-player-redraw on saving files. --- Penumbra/Api/PenumbraApi.cs | 14 ++- .../Collections/Manager/CollectionStorage.cs | 32 +++++- Penumbra/Communication/ModFileChanged.cs | 28 ++++++ Penumbra/Configuration.cs | 1 - Penumbra/EphemeralConfig.cs | 1 + Penumbra/Interop/Services/RedrawService.cs | 31 ++++-- Penumbra/Mods/Editor/ModFileEditor.cs | 4 +- Penumbra/Services/CommunicatorService.cs | 3 + Penumbra/UI/AdvancedWindow/FileEditor.cs | 99 ++++++++++--------- .../UI/AdvancedWindow/ModEditWindow.Files.cs | 2 +- .../ModEditWindow.Materials.Shpk.cs | 2 +- .../AdvancedWindow/ModEditWindow.Materials.cs | 2 +- .../UI/AdvancedWindow/ModEditWindow.Meta.cs | 2 +- .../UI/AdvancedWindow/ModEditWindow.Models.cs | 4 +- .../ModEditWindow.QuickImport.cs | 2 +- .../AdvancedWindow/ModEditWindow.Textures.cs | 38 ++++++- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 55 ++++++----- 17 files changed, 221 insertions(+), 99 deletions(-) create mode 100644 Penumbra/Communication/ModFileChanged.cs diff --git a/Penumbra/Api/PenumbraApi.cs b/Penumbra/Api/PenumbraApi.cs index 59ef6677..aed1a963 100644 --- a/Penumbra/Api/PenumbraApi.cs +++ b/Penumbra/Api/PenumbraApi.cs @@ -24,6 +24,7 @@ using Penumbra.Interop.Services; using Penumbra.UI; using TextureType = Penumbra.Api.Enums.TextureType; using Penumbra.Interop.ResourceTree; +using Penumbra.Mods.Editor; namespace Penumbra.Api; @@ -142,6 +143,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi _communicator.ModSettingChanged.Subscribe(OnModSettingChange, Communication.ModSettingChanged.Priority.Api); _communicator.CreatedCharacterBase.Subscribe(OnCreatedCharacterBase, Communication.CreatedCharacterBase.Priority.Api); _communicator.ModOptionChanged.Subscribe(OnModOptionEdited, ModOptionChanged.Priority.Api); + _communicator.ModFileChanged.Subscribe(OnModFileChanged, ModFileChanged.Priority.Api); } public unsafe void Dispose() @@ -153,6 +155,8 @@ public class PenumbraApi : IDisposable, IPenumbraApi _communicator.ModPathChanged.Unsubscribe(ModPathChangeSubscriber); _communicator.ModSettingChanged.Unsubscribe(OnModSettingChange); _communicator.CreatedCharacterBase.Unsubscribe(OnCreatedCharacterBase); + _communicator.ModOptionChanged.Unsubscribe(OnModOptionEdited); + _communicator.ModFileChanged.Unsubscribe(OnModFileChanged); _lumina = null; _communicator = null!; _modManager = null!; @@ -1277,11 +1281,19 @@ public class PenumbraApi : IDisposable, IPenumbraApi } } + private void OnModFileChanged(Mod mod, FileRegistry file) + { + if (file.CurrentUsage == 0) + return; + + TriggerSettingEdited(mod); + } + private void TriggerSettingEdited(Mod mod) { var collection = _collectionResolver.PlayerCollection(); var (settings, parent) = collection[mod.Index]; - if (settings != null) + if (settings is { Enabled: true }) ModSettingChanged?.Invoke(ModSettingChange.Edited, collection.Name, mod.Identifier, parent != collection); } } diff --git a/Penumbra/Collections/Manager/CollectionStorage.cs b/Penumbra/Collections/Manager/CollectionStorage.cs index c43c3817..a84c79e6 100644 --- a/Penumbra/Collections/Manager/CollectionStorage.cs +++ b/Penumbra/Collections/Manager/CollectionStorage.cs @@ -4,6 +4,7 @@ using OtterGui.Classes; using OtterGui.Filesystem; using Penumbra.Communication; using Penumbra.Mods; +using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; using Penumbra.Mods.Subclasses; using Penumbra.Services; @@ -56,6 +57,7 @@ public class CollectionStorage : IReadOnlyList, IDisposable _communicator.ModDiscoveryFinished.Subscribe(OnModDiscoveryFinished, ModDiscoveryFinished.Priority.CollectionStorage); _communicator.ModPathChanged.Subscribe(OnModPathChange, ModPathChanged.Priority.CollectionStorage); _communicator.ModOptionChanged.Subscribe(OnModOptionChange, ModOptionChanged.Priority.CollectionStorage); + _communicator.ModFileChanged.Subscribe(OnModFileChanged, ModFileChanged.Priority.CollectionStorage); ReadCollections(out DefaultNamed); } @@ -65,6 +67,7 @@ public class CollectionStorage : IReadOnlyList, IDisposable _communicator.ModDiscoveryFinished.Unsubscribe(OnModDiscoveryFinished); _communicator.ModPathChanged.Unsubscribe(OnModPathChange); _communicator.ModOptionChanged.Unsubscribe(OnModOptionChange); + _communicator.ModFileChanged.Unsubscribe(OnModFileChanged); } /// @@ -104,7 +107,8 @@ public class CollectionStorage : IReadOnlyList, IDisposable if (!CanAddCollection(name, out var fixedName)) { Penumbra.Messager.NotificationMessage( - $"The new collection {name} would lead to the same path {fixedName} as one that already exists.", NotificationType.Warning, false); + $"The new collection {name} would lead to the same path {fixedName} as one that already exists.", NotificationType.Warning, + false); return false; } @@ -185,20 +189,23 @@ public class CollectionStorage : IReadOnlyList, IDisposable if (!IsValidName(name)) { // TODO: handle better. - Penumbra.Messager.NotificationMessage($"Collection of unsupported name found: {name} is not a valid collection name.", NotificationType.Warning); + Penumbra.Messager.NotificationMessage($"Collection of unsupported name found: {name} is not a valid collection name.", + NotificationType.Warning); continue; } if (ByName(name, out _)) { - Penumbra.Messager.NotificationMessage($"Duplicate collection found: {name} already exists. Import skipped.", NotificationType.Warning); + Penumbra.Messager.NotificationMessage($"Duplicate collection found: {name} already exists. Import skipped.", + NotificationType.Warning); continue; } var collection = ModCollection.CreateFromData(_saveService, _modStorage, name, version, Count, settings, inheritance); var correctName = _saveService.FileNames.CollectionFile(collection); if (file.FullName != correctName) - Penumbra.Messager.NotificationMessage($"Collection {file.Name} does not correspond to {collection.Name}.", NotificationType.Warning); + Penumbra.Messager.NotificationMessage($"Collection {file.Name} does not correspond to {collection.Name}.", + NotificationType.Warning); _collections.Add(collection); } @@ -220,7 +227,8 @@ public class CollectionStorage : IReadOnlyList, IDisposable return _collections[^1]; Penumbra.Messager.NotificationMessage( - $"Unknown problem creating a collection with the name {ModCollection.DefaultCollectionName}, which is required to exist.", NotificationType.Error); + $"Unknown problem creating a collection with the name {ModCollection.DefaultCollectionName}, which is required to exist.", + NotificationType.Error); return Count > 1 ? _collections[1] : _collections[0]; } @@ -273,4 +281,18 @@ public class CollectionStorage : IReadOnlyList, IDisposable _saveService.QueueSave(new ModCollectionSave(_modStorage, collection)); } } + + /// Update change counters when changing files. + private void OnModFileChanged(Mod mod, FileRegistry file) + { + if (file.CurrentUsage == 0) + return; + + foreach (var collection in this) + { + var (settings, _) = collection[mod.Index]; + if (settings is { Enabled: true }) + collection.IncrementCounter(); + } + } } diff --git a/Penumbra/Communication/ModFileChanged.cs b/Penumbra/Communication/ModFileChanged.cs new file mode 100644 index 00000000..8b4b6f5d --- /dev/null +++ b/Penumbra/Communication/ModFileChanged.cs @@ -0,0 +1,28 @@ +using OtterGui.Classes; +using Penumbra.Api; +using Penumbra.Mods; +using Penumbra.Mods.Editor; + +namespace Penumbra.Communication; + +/// +/// Triggered whenever an existing file in a mod is overwritten by Penumbra. +/// +/// Parameter is the changed mod. +/// Parameter file registry of the changed file. +/// +public sealed class ModFileChanged() + : EventWrapper(nameof(ModFileChanged)) +{ + public enum Priority + { + /// + Api = int.MinValue, + + /// + RedrawService = -50, + + /// + CollectionStorage = 0, + } +} diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index a5a615bd..188be65d 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -47,7 +47,6 @@ public class Configuration : IPluginConfiguration, ISavable public bool UseNoModsInInspect { get; set; } = false; public bool HideChangedItemFilters { get; set; } = false; public bool ReplaceNonAsciiOnImport { get; set; } = false; - public bool HidePrioritiesInSelector { get; set; } = false; public bool HideRedrawBar { get; set; } = false; public int OptionGroupCollapsibleMin { get; set; } = 5; diff --git a/Penumbra/EphemeralConfig.cs b/Penumbra/EphemeralConfig.cs index 8cf23de6..98b1a5d6 100644 --- a/Penumbra/EphemeralConfig.cs +++ b/Penumbra/EphemeralConfig.cs @@ -39,6 +39,7 @@ public class EphemeralConfig : ISavable, IDisposable public bool FixMainWindow { get; set; } = false; public string LastModPath { get; set; } = string.Empty; public bool AdvancedEditingOpen { get; set; } = false; + public bool ForceRedrawOnFileChange { get; set; } = false; /// /// Load the current configuration. diff --git a/Penumbra/Interop/Services/RedrawService.cs b/Penumbra/Interop/Services/RedrawService.cs index e2e57b1c..c1bd8573 100644 --- a/Penumbra/Interop/Services/RedrawService.cs +++ b/Penumbra/Interop/Services/RedrawService.cs @@ -8,9 +8,13 @@ using FFXIVClientStructs.FFXIV.Client.Game.Housing; using FFXIVClientStructs.Interop; using Penumbra.Api; using Penumbra.Api.Enums; +using Penumbra.Communication; using Penumbra.GameData; using Penumbra.GameData.Enums; using Penumbra.Interop.Structs; +using Penumbra.Mods; +using Penumbra.Mods.Editor; +using Penumbra.Services; using Character = FFXIVClientStructs.FFXIV.Client.Game.Character.Character; namespace Penumbra.Interop.Services; @@ -106,11 +110,13 @@ public sealed unsafe partial class RedrawService : IDisposable { private const int FurnitureIdx = 1337; - private readonly IFramework _framework; - private readonly IObjectTable _objects; - private readonly ITargetManager _targets; - private readonly ICondition _conditions; - private readonly IClientState _clientState; + private readonly IFramework _framework; + private readonly IObjectTable _objects; + private readonly ITargetManager _targets; + private readonly ICondition _conditions; + private readonly IClientState _clientState; + private readonly Configuration _config; + private readonly CommunicatorService _communicator; private readonly List _queue = new(100); private readonly List _afterGPoseQueue = new(GPoseSlots); @@ -127,19 +133,24 @@ public sealed unsafe partial class RedrawService : IDisposable public event GameObjectRedrawnDelegate? GameObjectRedrawn; - public RedrawService(IFramework framework, IObjectTable objects, ITargetManager targets, ICondition conditions, IClientState clientState) + public RedrawService(IFramework framework, IObjectTable objects, ITargetManager targets, ICondition conditions, IClientState clientState, + Configuration config, CommunicatorService communicator) { _framework = framework; _objects = objects; _targets = targets; _conditions = conditions; _clientState = clientState; + _config = config; + _communicator = communicator; _framework.Update += OnUpdateEvent; + _communicator.ModFileChanged.Subscribe(OnModFileChanged, ModFileChanged.Priority.RedrawService); } public void Dispose() { _framework.Update -= OnUpdateEvent; + _communicator.ModFileChanged.Unsubscribe(OnModFileChanged); } public static DrawState* ActorDrawState(GameObject actor) @@ -419,4 +430,12 @@ public sealed unsafe partial class RedrawService : IDisposable gameObject->DisableDraw(); } } + + private void OnModFileChanged(Mod _1, FileRegistry _2) + { + if (!_config.ForceRedrawOnFileChange) + return; + + RedrawObject(0, RedrawType.Redraw); + } } diff --git a/Penumbra/Mods/Editor/ModFileEditor.cs b/Penumbra/Mods/Editor/ModFileEditor.cs index 5328b8fe..30e97093 100644 --- a/Penumbra/Mods/Editor/ModFileEditor.cs +++ b/Penumbra/Mods/Editor/ModFileEditor.cs @@ -1,10 +1,11 @@ using Penumbra.Mods.Manager; using Penumbra.Mods.Subclasses; +using Penumbra.Services; using Penumbra.String.Classes; namespace Penumbra.Mods.Editor; -public class ModFileEditor(ModFileCollection files, ModManager modManager) +public class ModFileEditor(ModFileCollection files, ModManager modManager, CommunicatorService communicator) { public bool Changes { get; private set; } @@ -136,6 +137,7 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager) try { File.Delete(file.File.FullName); + communicator.ModFileChanged.Invoke(mod, file); Penumbra.Log.Debug($"[DeleteFiles] Deleted {file.File.FullName} from {mod.Name}."); ++deletions; } diff --git a/Penumbra/Services/CommunicatorService.cs b/Penumbra/Services/CommunicatorService.cs index be94a31e..da852855 100644 --- a/Penumbra/Services/CommunicatorService.cs +++ b/Penumbra/Services/CommunicatorService.cs @@ -42,6 +42,9 @@ public class CommunicatorService : IDisposable, IService /// public readonly ModDirectoryChanged ModDirectoryChanged = new(); + /// + public readonly ModFileChanged ModFileChanged = new(); + /// public readonly ModPathChanged ModPathChanged = new(); diff --git a/Penumbra/UI/AdvancedWindow/FileEditor.cs b/Penumbra/UI/AdvancedWindow/FileEditor.cs index 89d47eb2..c891d33a 100644 --- a/Penumbra/UI/AdvancedWindow/FileEditor.cs +++ b/Penumbra/UI/AdvancedWindow/FileEditor.cs @@ -8,39 +8,32 @@ using OtterGui.Compression; using OtterGui.Raii; using OtterGui.Widgets; using Penumbra.GameData.Files; -using Penumbra.Mods; using Penumbra.Mods.Editor; +using Penumbra.Services; using Penumbra.String.Classes; using Penumbra.UI.Classes; namespace Penumbra.UI.AdvancedWindow; -public class FileEditor : IDisposable where T : class, IWritable +public class FileEditor( + ModEditWindow owner, + CommunicatorService communicator, + IDataManager gameData, + Configuration config, + FileCompactor compactor, + FileDialogService fileDialog, + string tabName, + string fileType, + Func> getFiles, + Func drawEdit, + Func getInitialPath, + Func parseFile) + : IDisposable + where T : class, IWritable { - private readonly FileDialogService _fileDialog; - private readonly IDataManager _gameData; - private readonly ModEditWindow _owner; - private readonly FileCompactor _compactor; - - public FileEditor(ModEditWindow owner, IDataManager gameData, Configuration config, FileCompactor compactor, FileDialogService fileDialog, - string tabName, string fileType, Func> getFiles, Func drawEdit, Func getInitialPath, - Func parseFile) - { - _owner = owner; - _gameData = gameData; - _fileDialog = fileDialog; - _tabName = tabName; - _fileType = fileType; - _drawEdit = drawEdit; - _getInitialPath = getInitialPath; - _parseFile = parseFile; - _compactor = compactor; - _combo = new Combo(config, getFiles); - } - public void Draw() { - using var tab = ImRaii.TabItem(_tabName); + using var tab = ImRaii.TabItem(tabName); if (!tab) { _quickImport = null; @@ -53,12 +46,26 @@ public class FileEditor : IDisposable where T : class, IWritable ImGui.SameLine(); ResetButton(); ImGui.SameLine(); + RedrawOnSaveBox(); + ImGui.SameLine(); DefaultInput(); ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); DrawFilePanel(); } + private void RedrawOnSaveBox() + { + var redraw = config.Ephemeral.ForceRedrawOnFileChange; + if (ImGui.Checkbox("Redraw on Save", ref redraw)) + { + config.Ephemeral.ForceRedrawOnFileChange = redraw; + config.Ephemeral.Save(); + } + + ImGuiUtil.HoverTooltip("Force a redraw of your player character whenever you save a file here."); + } + public void Dispose() { (_currentFile as IDisposable)?.Dispose(); @@ -67,12 +74,6 @@ public class FileEditor : IDisposable where T : class, IWritable _defaultFile = null; } - private readonly string _tabName; - private readonly string _fileType; - private readonly Func _drawEdit; - private readonly Func _getInitialPath; - private readonly Func _parseFile; - private FileRegistry? _currentPath; private T? _currentFile; private Exception? _currentException; @@ -85,7 +86,7 @@ public class FileEditor : IDisposable where T : class, IWritable private T? _defaultFile; private Exception? _defaultException; - private readonly Combo _combo; + private readonly Combo _combo = new(config, getFiles); private ModEditWindow.QuickImportAction? _quickImport; @@ -99,16 +100,16 @@ public class FileEditor : IDisposable where T : class, IWritable { _isDefaultPathUtf8Valid = Utf8GamePath.FromString(_defaultPath, out _defaultPathUtf8, true); _quickImport = null; - _fileDialog.Reset(); + fileDialog.Reset(); try { - var file = _gameData.GetFile(_defaultPath); + var file = gameData.GetFile(_defaultPath); if (file != null) { _defaultException = null; (_defaultFile as IDisposable)?.Dispose(); _defaultFile = null; // Avoid double disposal if an exception occurs during the parsing of the new file. - _defaultFile = _parseFile(file.Data, _defaultPath, false); + _defaultFile = parseFile(file.Data, _defaultPath, false); } else { @@ -126,7 +127,7 @@ public class FileEditor : IDisposable where T : class, IWritable ImGui.SameLine(); if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Save.ToIconString(), new Vector2(ImGui.GetFrameHeight()), "Export this file.", _defaultFile == null, true)) - _fileDialog.OpenSavePicker($"Export {_defaultPath} to...", _fileType, Path.GetFileNameWithoutExtension(_defaultPath), _fileType, + fileDialog.OpenSavePicker($"Export {_defaultPath} to...", fileType, Path.GetFileNameWithoutExtension(_defaultPath), fileType, (success, name) => { if (!success) @@ -134,16 +135,16 @@ public class FileEditor : IDisposable where T : class, IWritable try { - _compactor.WriteAllBytes(name, _defaultFile?.Write() ?? throw new Exception("File invalid.")); + compactor.WriteAllBytes(name, _defaultFile?.Write() ?? throw new Exception("File invalid.")); } catch (Exception e) { Penumbra.Messager.NotificationMessage(e, $"Could not export {_defaultPath}.", NotificationType.Error); } - }, _getInitialPath(), false); + }, getInitialPath(), false); _quickImport ??= - ModEditWindow.QuickImportAction.Prepare(_owner, _isDefaultPathUtf8Valid ? _defaultPathUtf8 : Utf8GamePath.Empty, _defaultFile); + ModEditWindow.QuickImportAction.Prepare(owner, _isDefaultPathUtf8Valid ? _defaultPathUtf8 : Utf8GamePath.Empty, _defaultFile); ImGui.SameLine(); if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.FileImport.ToIconString(), new Vector2(ImGui.GetFrameHeight()), $"Add a copy of this file to {_quickImport.OptionName}.", !_quickImport.CanExecute, true)) @@ -172,7 +173,7 @@ public class FileEditor : IDisposable where T : class, IWritable private void DrawFileSelectCombo() { - if (_combo.Draw("##fileSelect", _currentPath?.RelPath.ToString() ?? $"Select {_fileType} File...", string.Empty, + if (_combo.Draw("##fileSelect", _currentPath?.RelPath.ToString() ?? $"Select {fileType} File...", string.Empty, ImGui.GetContentRegionAvail().X, ImGui.GetTextLineHeight()) && _combo.CurrentSelection != null) UpdateCurrentFile(_combo.CurrentSelection); @@ -191,7 +192,7 @@ public class FileEditor : IDisposable where T : class, IWritable var bytes = File.ReadAllBytes(_currentPath.File.FullName); (_currentFile as IDisposable)?.Dispose(); _currentFile = null; // Avoid double disposal if an exception occurs during the parsing of the new file. - _currentFile = _parseFile(bytes, _currentPath.File.FullName, true); + _currentFile = parseFile(bytes, _currentPath.File.FullName, true); } catch (Exception e) { @@ -204,9 +205,11 @@ public class FileEditor : IDisposable where T : class, IWritable private void SaveButton() { if (ImGuiUtil.DrawDisabledButton("Save to File", Vector2.Zero, - $"Save the selected {_fileType} file with all changes applied. This is not revertible.", !_changed)) + $"Save the selected {fileType} file with all changes applied. This is not revertible.", !_changed)) { - _compactor.WriteAllBytes(_currentPath!.File.FullName, _currentFile!.Write()); + compactor.WriteAllBytes(_currentPath!.File.FullName, _currentFile!.Write()); + if (owner.Mod != null) + communicator.ModFileChanged.Invoke(owner.Mod, _currentPath); _changed = false; } } @@ -214,7 +217,7 @@ public class FileEditor : IDisposable where T : class, IWritable private void ResetButton() { if (ImGuiUtil.DrawDisabledButton("Reset Changes", Vector2.Zero, - $"Reset all changes made to the {_fileType} file.", !_changed)) + $"Reset all changes made to the {fileType} file.", !_changed)) { var tmp = _currentPath; _currentPath = null; @@ -232,7 +235,7 @@ public class FileEditor : IDisposable where T : class, IWritable { if (_currentFile == null) { - ImGui.TextUnformatted($"Could not parse selected {_fileType} file."); + ImGui.TextUnformatted($"Could not parse selected {fileType} file."); if (_currentException != null) { using var tab = ImRaii.PushIndent(); @@ -242,7 +245,7 @@ public class FileEditor : IDisposable where T : class, IWritable else { using var id = ImRaii.PushId(0); - _changed |= _drawEdit(_currentFile, false); + _changed |= drawEdit(_currentFile, false); } } @@ -258,7 +261,7 @@ public class FileEditor : IDisposable where T : class, IWritable if (_defaultFile == null) { - ImGui.TextUnformatted($"Could not parse provided {_fileType} game file:\n"); + ImGui.TextUnformatted($"Could not parse provided {fileType} game file:\n"); if (_defaultException != null) { using var tab = ImRaii.PushIndent(); @@ -268,7 +271,7 @@ public class FileEditor : IDisposable where T : class, IWritable else { using var id = ImRaii.PushId(1); - _drawEdit(_defaultFile, true); + drawEdit(_defaultFile, true); } } } @@ -283,7 +286,7 @@ public class FileEditor : IDisposable where T : class, IWritable protected override bool DrawSelectable(int globalIdx, bool selected) { - var file = Items[globalIdx]; + var file = Items[globalIdx]; bool ret; using (var c = ImRaii.PushColor(ImGuiCol.Text, ColorId.HandledConflictMod.Value(), file.IsOnPlayer)) { diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs index bae23729..c8db7770 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs @@ -80,7 +80,7 @@ public partial class ModEditWindow return f.SubModUsage.Count == 0 ? Enumerable.Repeat((file, "Unused", string.Empty, 0x40000080u), 1) : f.SubModUsage.Select(s => (file, s.Item2.ToString(), s.Item1.FullName, - _editor.Option! == s.Item1 && _mod!.HasOptions ? 0x40008000u : 0u)); + _editor.Option! == s.Item1 && Mod!.HasOptions ? 0x40008000u : 0u)); }); void DrawLine((string, string, string, uint) data) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.Shpk.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.Shpk.cs index 9e9557d3..3ce10224 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.Shpk.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.Shpk.cs @@ -143,7 +143,7 @@ public partial class ModEditWindow { if (success) tab.LoadShpk(new FullPath(name[0])); - }, 1, _mod!.ModPath.FullName, false); + }, 1, Mod!.ModPath.FullName, false); var moddedPath = tab.FindAssociatedShpk(out var defaultPath, out var gamePath); ImGui.SameLine(); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs index df20d60f..fab41c7d 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs @@ -209,7 +209,7 @@ public partial class ModEditWindow info.Restore(); ImGui.TableNextColumn(); - ImGui.TextUnformatted(info.Path.FullName[(_mod!.ModPath.FullName.Length + 1)..]); + ImGui.TextUnformatted(info.Path.FullName[(Mod!.ModPath.FullName.Length + 1)..]); ImGui.TableNextColumn(); ImGui.SetNextItemWidth(400 * UiHelpers.Scale); var tmp = info.CurrentMaterials[0]; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs index 20550a15..aad70cb3 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs @@ -60,7 +60,7 @@ public partial class ModEditWindow CopyToClipboardButton("Copy all current manipulations to clipboard.", _iconSize, _editor.MetaEditor.Recombine()); ImGui.SameLine(); if (ImGui.Button("Write as TexTools Files")) - _metaFileManager.WriteAllTexToolsMeta(_mod!); + _metaFileManager.WriteAllTexToolsMeta(Mod!); using var child = ImRaii.Child("##meta", -Vector2.One, true); if (!child) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index 561cbed7..67ec97f2 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -94,7 +94,7 @@ public partial class ModEditWindow { if (success && paths.Count > 0) tab.Import(paths[0]); - }, 1, _mod!.ModPath.FullName, false); + }, 1, Mod!.ModPath.FullName, false); ImGui.SameLine(); DrawDocumentationLink(MdlImportDocumentation); @@ -142,7 +142,7 @@ public partial class ModEditWindow tab.Export(path, gamePath); }, - _mod!.ModPath.FullName, + Mod!.ModPath.FullName, false ); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs index c9cd3d06..9a38a5d5 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs @@ -195,7 +195,7 @@ public partial class ModEditWindow if (subMod.Files.ContainsKey(gamePath) || subMod.FileSwaps.ContainsKey(gamePath)) return new QuickImportAction(editor, optionName, gamePath); - var mod = owner._mod; + var mod = owner.Mod; if (mod == null) return new QuickImportAction(editor, optionName, gamePath); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Textures.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Textures.cs index 34d0800c..71c64059 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Textures.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Textures.cs @@ -3,6 +3,7 @@ using OtterGui; using OtterGui.Raii; using OtterTex; using Penumbra.Import.Textures; +using Penumbra.Mods; using Penumbra.UI.Classes; namespace Penumbra.UI.AdvancedWindow; @@ -45,10 +46,10 @@ public partial class ModEditWindow using (var disabled = ImRaii.Disabled(!_center.SaveTask.IsCompleted)) { TextureDrawer.PathInputBox(_textures, tex, ref tex.TmpPath, "##input", "Import Image...", - "Can import game paths as well as your own files.", _mod!.ModPath.FullName, _fileDialog, _config.DefaultModImportPath); + "Can import game paths as well as your own files.", Mod!.ModPath.FullName, _fileDialog, _config.DefaultModImportPath); if (_textureSelectCombo.Draw("##combo", "Select the textures included in this mod on your drive or the ones they replace from the game files.", tex.Path, - _mod.ModPath.FullName.Length + 1, out var newPath) + Mod.ModPath.FullName.Length + 1, out var newPath) && newPath != tex.Path) tex.Load(_textures, newPath); @@ -84,6 +85,18 @@ public partial class ModEditWindow ImGuiUtil.SelectableHelpMarker(newDesc); } + } + + private void RedrawOnSaveBox() + { + var redraw = _config.Ephemeral.ForceRedrawOnFileChange; + if (ImGui.Checkbox("Redraw on Save", ref redraw)) + { + _config.Ephemeral.ForceRedrawOnFileChange = redraw; + _config.Ephemeral.Save(); + } + + ImGuiUtil.HoverTooltip("Force a redraw of your player character whenever you save a file here."); } private void MipMapInput() @@ -103,6 +116,8 @@ public partial class ModEditWindow if (_center.IsLoaded) { + RedrawOnSaveBox(); + ImGui.SameLine(); SaveAsCombo(); ImGui.SameLine(); MipMapInput(); @@ -118,6 +133,7 @@ public partial class ModEditWindow tt, !isActive || !canSaveInPlace || _center.IsLeftCopy && _currentSaveAs == (int)CombinedTexture.TextureSaveType.AsIs)) { _center.SaveAs(_left.Type, _textures, _left.Path, (CombinedTexture.TextureSaveType)_currentSaveAs, _addMipMaps); + InvokeChange(Mod, _left.Path); AddReloadTask(_left.Path, false); } @@ -141,6 +157,7 @@ public partial class ModEditWindow !canConvertInPlace || _left.Format is DXGIFormat.BC7Typeless or DXGIFormat.BC7UNorm or DXGIFormat.BC7UNormSRGB)) { _center.SaveAsTex(_textures, _left.Path, CombinedTexture.TextureSaveType.BC7, _left.MipMaps > 1); + InvokeChange(Mod, _left.Path); AddReloadTask(_left.Path, false); } @@ -150,6 +167,7 @@ public partial class ModEditWindow !canConvertInPlace || _left.Format is DXGIFormat.BC3Typeless or DXGIFormat.BC3UNorm or DXGIFormat.BC3UNormSRGB)) { _center.SaveAsTex(_textures, _left.Path, CombinedTexture.TextureSaveType.BC3, _left.MipMaps > 1); + InvokeChange(Mod, _left.Path); AddReloadTask(_left.Path, false); } @@ -160,6 +178,7 @@ public partial class ModEditWindow || _left.Format is DXGIFormat.B8G8R8A8UNorm or DXGIFormat.B8G8R8A8Typeless or DXGIFormat.B8G8R8A8UNormSRGB)) { _center.SaveAsTex(_textures, _left.Path, CombinedTexture.TextureSaveType.Bitmap, _left.MipMaps > 1); + InvokeChange(Mod, _left.Path); AddReloadTask(_left.Path, false); } } @@ -192,6 +211,18 @@ public partial class ModEditWindow _center.Draw(_textures, imageSize); } + private void InvokeChange(Mod? mod, string path) + { + if (mod == null) + return; + + if (!_editor.Files.Tex.FindFirst(r => string.Equals(r.File.FullName, path, StringComparison.OrdinalIgnoreCase), + out var registry)) + return; + + _communicator.ModFileChanged.Invoke(mod, registry); + } + private void OpenSaveAsDialog(string defaultExtension) { var fileName = Path.GetFileNameWithoutExtension(_left.Path.Length > 0 ? _left.Path : _right.Path); @@ -201,12 +232,13 @@ public partial class ModEditWindow if (a) { _center.SaveAs(null, _textures, b, (CombinedTexture.TextureSaveType)_currentSaveAs, _addMipMaps); + InvokeChange(Mod, b); if (b == _left.Path) AddReloadTask(_left.Path, false); else if (b == _right.Path) AddReloadTask(_right.Path, true); } - }, _mod!.ModPath.FullName, _forceTextureStartPath); + }, Mod!.ModPath.FullName, _forceTextureStartPath); _forceTextureStartPath = false; } diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index 38fdf482..afa846b5 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -49,17 +49,18 @@ public partial class ModEditWindow : Window, IDisposable private readonly IObjectTable _objects; private readonly CharacterBaseDestructor _characterBaseDestructor; - private Mod? _mod; private Vector2 _iconSize = Vector2.Zero; private bool _allowReduplicate; + public Mod? Mod { get; private set; } + public void ChangeMod(Mod mod) { - if (mod == _mod) + if (mod == Mod) return; _editor.LoadMod(mod, -1, 0); - _mod = mod; + Mod = mod; SizeConstraints = new WindowSizeConstraints { @@ -80,12 +81,12 @@ public partial class ModEditWindow : Window, IDisposable public void UpdateModels() { - if (_mod != null) - _editor.MdlMaterialEditor.ScanModels(_mod); + if (Mod != null) + _editor.MdlMaterialEditor.ScanModels(Mod); } public override bool DrawConditions() - => _mod != null; + => Mod != null; public override void PreDraw() { @@ -106,13 +107,13 @@ public partial class ModEditWindow : Window, IDisposable }); var manipulations = 0; var subMods = 0; - var swaps = _mod!.AllSubMods.Sum(m => + var swaps = Mod!.AllSubMods.Sum(m => { ++subMods; manipulations += m.Manipulations.Count; return m.FileSwaps.Count; }); - sb.Append(_mod!.Name); + sb.Append(Mod!.Name); if (subMods > 1) sb.Append($" | {subMods} Options"); @@ -271,7 +272,7 @@ public partial class ModEditWindow : Window, IDisposable ImGui.NewLine(); if (ImGui.Button("Remove Missing Files from Mod")) - _editor.FileEditor.RemoveMissingPaths(_mod!, _editor.Option!); + _editor.FileEditor.RemoveMissingPaths(Mod!, _editor.Option!); using var child = ImRaii.Child("##unusedFiles", -Vector2.One, true); if (!child) @@ -324,8 +325,8 @@ public partial class ModEditWindow : Window, IDisposable } else if (ImGuiUtil.DrawDisabledButton("Re-Duplicate and Normalize Mod", Vector2.Zero, tt, !_allowReduplicate && !modifier)) { - _editor.ModNormalizer.Normalize(_mod!); - _editor.ModNormalizer.Worker.ContinueWith(_ => _editor.LoadMod(_mod!, _editor.GroupIdx, _editor.OptionIdx)); + _editor.ModNormalizer.Normalize(Mod!); + _editor.ModNormalizer.Worker.ContinueWith(_ => _editor.LoadMod(Mod!, _editor.GroupIdx, _editor.OptionIdx)); } if (!_editor.Duplicates.Worker.IsCompleted) @@ -363,7 +364,7 @@ public partial class ModEditWindow : Window, IDisposable foreach (var (set, size, hash) in _editor.Duplicates.Duplicates.Where(s => s.Paths.Length > 1)) { ImGui.TableNextColumn(); - using var tree = ImRaii.TreeNode(set[0].FullName[(_mod!.ModPath.FullName.Length + 1)..], + using var tree = ImRaii.TreeNode(set[0].FullName[(Mod!.ModPath.FullName.Length + 1)..], ImGuiTreeNodeFlags.NoTreePushOnOpen); ImGui.TableNextColumn(); ImGuiUtil.RightAlign(Functions.HumanReadableSize(size)); @@ -384,7 +385,7 @@ public partial class ModEditWindow : Window, IDisposable { ImGui.TableNextColumn(); ImGui.TableSetBgColor(ImGuiTableBgTarget.CellBg, Colors.RedTableBgTint); - using var node = ImRaii.TreeNode(duplicate.FullName[(_mod!.ModPath.FullName.Length + 1)..], ImGuiTreeNodeFlags.Leaf); + using var node = ImRaii.TreeNode(duplicate.FullName[(Mod!.ModPath.FullName.Length + 1)..], ImGuiTreeNodeFlags.Leaf); ImGui.TableNextColumn(); ImGui.TableSetBgColor(ImGuiTableBgTarget.CellBg, Colors.RedTableBgTint); ImGui.TableNextColumn(); @@ -421,7 +422,7 @@ public partial class ModEditWindow : Window, IDisposable if (!combo) return ret; - foreach (var (option, idx) in _mod!.AllSubMods.WithIndex()) + foreach (var (option, idx) in Mod!.AllSubMods.WithIndex()) { using var id = ImRaii.PushId(idx); if (ImGui.Selectable(option.FullName, option == _editor.Option)) @@ -537,10 +538,10 @@ public partial class ModEditWindow : Window, IDisposable if (currentFile != null) return currentFile.Value; - if (_mod != null) - foreach (var option in _mod.Groups.OrderByDescending(g => g.Priority) + if (Mod != null) + foreach (var option in Mod.Groups.OrderByDescending(g => g.Priority) .SelectMany(g => g.WithIndex().OrderByDescending(o => g.OptionPriority(o.Index)).Select(g => g.Value)) - .Append(_mod.Default)) + .Append(Mod.Default)) { if (option.Files.TryGetValue(path, out var value) || option.FileSwaps.TryGetValue(path, out value)) return value; @@ -559,8 +560,8 @@ public partial class ModEditWindow : Window, IDisposable ret.Add(path); } - if (_mod != null) - foreach (var option in _mod.Groups.SelectMany(g => g).Append(_mod.Default)) + if (Mod != null) + foreach (var option in Mod.Groups.SelectMany(g => g).Append(Mod.Default)) { foreach (var path in option.Files.Keys) { @@ -596,15 +597,15 @@ public partial class ModEditWindow : Window, IDisposable _objects = objects; _framework = framework; _characterBaseDestructor = characterBaseDestructor; - _materialTab = new FileEditor(this, gameData, config, _editor.Compactor, _fileDialog, "Materials", ".mtrl", - () => PopulateIsOnPlayer(_editor.Files.Mtrl, ResourceType.Mtrl), DrawMaterialPanel, () => _mod?.ModPath.FullName ?? string.Empty, + _materialTab = new FileEditor(this, _communicator, gameData, config, _editor.Compactor, _fileDialog, "Materials", ".mtrl", + () => PopulateIsOnPlayer(_editor.Files.Mtrl, ResourceType.Mtrl), DrawMaterialPanel, () => Mod?.ModPath.FullName ?? string.Empty, (bytes, path, writable) => new MtrlTab(this, new MtrlFile(bytes), path, writable)); - _modelTab = new FileEditor(this, gameData, config, _editor.Compactor, _fileDialog, "Models", ".mdl", - () => PopulateIsOnPlayer(_editor.Files.Mdl, ResourceType.Mdl), DrawModelPanel, () => _mod?.ModPath.FullName ?? string.Empty, + _modelTab = new FileEditor(this, _communicator, gameData, config, _editor.Compactor, _fileDialog, "Models", ".mdl", + () => PopulateIsOnPlayer(_editor.Files.Mdl, ResourceType.Mdl), DrawModelPanel, () => Mod?.ModPath.FullName ?? string.Empty, (bytes, path, _) => new MdlTab(this, bytes, path)); - _shaderPackageTab = new FileEditor(this, gameData, config, _editor.Compactor, _fileDialog, "Shaders", ".shpk", + _shaderPackageTab = new FileEditor(this, _communicator, gameData, config, _editor.Compactor, _fileDialog, "Shaders", ".shpk", () => PopulateIsOnPlayer(_editor.Files.Shpk, ResourceType.Shpk), DrawShaderPackagePanel, - () => _mod?.ModPath.FullName ?? string.Empty, + () => Mod?.ModPath.FullName ?? string.Empty, (bytes, _, _) => new ShpkTab(_fileDialog, bytes)); _center = new CombinedTexture(_left, _right); _textureSelectCombo = new TextureDrawer.PathSelectCombo(textures, editor, () => GetPlayerResourcesOfType(ResourceType.Tex)); @@ -629,10 +630,10 @@ public partial class ModEditWindow : Window, IDisposable private void OnModPathChange(ModPathChangeType type, Mod mod, DirectoryInfo? _1, DirectoryInfo? _2) { - if (type is not (ModPathChangeType.Reloaded or ModPathChangeType.Moved) || mod != _mod) + if (type is not (ModPathChangeType.Reloaded or ModPathChangeType.Moved) || mod != Mod) return; - _mod = null; + Mod = null; ChangeMod(mod); } } From a89aafb31008961eea0993e155150b77673c1b71 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 24 Feb 2024 14:24:50 +0100 Subject: [PATCH 015/433] Let's do this one more time. --- Penumbra/Interop/Services/RedrawService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Interop/Services/RedrawService.cs b/Penumbra/Interop/Services/RedrawService.cs index c1bd8573..e0a94d30 100644 --- a/Penumbra/Interop/Services/RedrawService.cs +++ b/Penumbra/Interop/Services/RedrawService.cs @@ -433,7 +433,7 @@ public sealed unsafe partial class RedrawService : IDisposable private void OnModFileChanged(Mod _1, FileRegistry _2) { - if (!_config.ForceRedrawOnFileChange) + if (!_config.Ephemeral.ForceRedrawOnFileChange) return; RedrawObject(0, RedrawType.Redraw); From 1d74001281083620af6ec6f1423fa74fb9217a88 Mon Sep 17 00:00:00 2001 From: Actions User Date: Sat, 24 Feb 2024 13:27:11 +0000 Subject: [PATCH 016/433] [CI] Updating repo.json for testing_1.0.0.8 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 1a599362..cbc5677f 100644 --- a/repo.json +++ b/repo.json @@ -5,7 +5,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.0.0.7", - "TestingAssemblyVersion": "1.0.0.7", + "TestingAssemblyVersion": "1.0.0.8", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -17,7 +17,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.7/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.7/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.0.8/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.7/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From af6100dfe4d8af2f475e09df8411b2f5cbb07bf4 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 1 Mar 2024 14:35:41 +0100 Subject: [PATCH 017/433] Mawp. --- Penumbra/Collections/ResolveData.cs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Penumbra/Collections/ResolveData.cs b/Penumbra/Collections/ResolveData.cs index 0f3a1155..8fe160b3 100644 --- a/Penumbra/Collections/ResolveData.cs +++ b/Penumbra/Collections/ResolveData.cs @@ -1,15 +1,15 @@ namespace Penumbra.Collections; -public readonly struct ResolveData +public readonly struct ResolveData(ModCollection collection, nint gameObject) { public static readonly ResolveData Invalid = new(); - private readonly ModCollection? _modCollection; + private readonly ModCollection? _modCollection = collection; public ModCollection ModCollection => _modCollection ?? ModCollection.Empty; - public readonly nint AssociatedGameObject; + public readonly nint AssociatedGameObject = gameObject; public bool Valid => _modCollection != null; @@ -18,12 +18,6 @@ public readonly struct ResolveData : this(null!, nint.Zero) { } - public ResolveData(ModCollection collection, nint gameObject) - { - _modCollection = collection; - AssociatedGameObject = gameObject; - } - public ResolveData(ModCollection collection) : this(collection, nint.Zero) { } From 0220257efa49d57acea181eddb94561de2fbbe01 Mon Sep 17 00:00:00 2001 From: Actions User Date: Fri, 1 Mar 2024 13:37:51 +0000 Subject: [PATCH 018/433] [CI] Updating repo.json for 1.0.1.0 --- repo.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/repo.json b/repo.json index cbc5677f..1780c6c0 100644 --- a/repo.json +++ b/repo.json @@ -4,8 +4,8 @@ "Name": "Penumbra", "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", - "AssemblyVersion": "1.0.0.7", - "TestingAssemblyVersion": "1.0.0.8", + "AssemblyVersion": "1.0.1.0", + "TestingAssemblyVersion": "1.0.1.0", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -16,9 +16,9 @@ "LoadPriority": 69420, "LoadRequiredState": 2, "LoadSync": true, - "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.7/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.0.8/Penumbra.zip", - "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.0.7/Penumbra.zip", + "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", + "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } ] From 7128326ab9f30e2fb2a3449953ed9d6a4c0e0ba1 Mon Sep 17 00:00:00 2001 From: AeAstralis Date: Fri, 1 Mar 2024 17:10:33 -0500 Subject: [PATCH 019/433] Add shared tag system for tagging individual mods Adds a new system of shared tags that are saved in the Penumbra config, and can then be 1-click added or removed to/from mods via a popup menu. The use case for this new system is to allow users to more easily re-use tags and to allow them to quickly tag individual mods. Shared tags can be added/removed/modified via a new Tags section of the main Penumbra Settings tab. Once any shared tags have been saved, they can be added via a new tags button that shows up in the Description and Edit Mod tabs, to the right of the existing + button that already existed for typing in new tags. Shared tags have the same restrictions as regular mod tags, and the application of shared tags should respect the same limits as application of normal tags. Signed-off-by: AeAstralis --- Penumbra/Configuration.cs | 2 + Penumbra/Services/ServiceManagerA.cs | 3 +- Penumbra/UI/Classes/Colors.cs | 4 + Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs | 29 +++- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 30 +++- Penumbra/UI/SharedTagManager.cs | 160 ++++++++++++++++++ Penumbra/UI/Tabs/SettingsTab.cs | 26 ++- 7 files changed, 248 insertions(+), 6 deletions(-) create mode 100644 Penumbra/UI/SharedTagManager.cs diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index 188be65d..43253223 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -87,6 +87,8 @@ public class Configuration : IPluginConfiguration, ISavable public Dictionary Colors { get; set; } = Enum.GetValues().ToDictionary(c => c, c => c.Data().DefaultColor); + public IReadOnlyList SharedTags { get; set; } + /// /// Load the current configuration. /// Includes adding new colors and migrating from old versions. diff --git a/Penumbra/Services/ServiceManagerA.cs b/Penumbra/Services/ServiceManagerA.cs index f25aac7c..b0ecdcf0 100644 --- a/Penumbra/Services/ServiceManagerA.cs +++ b/Penumbra/Services/ServiceManagerA.cs @@ -103,7 +103,8 @@ public static class ServiceManagerA private static ServiceManager AddConfiguration(this ServiceManager services) => services.AddSingleton() - .AddSingleton(); + .AddSingleton() + .AddSingleton(); private static ServiceManager AddCollections(this ServiceManager services) => services.AddSingleton() diff --git a/Penumbra/UI/Classes/Colors.cs b/Penumbra/UI/Classes/Colors.cs index 93d7e091..50096696 100644 --- a/Penumbra/UI/Classes/Colors.cs +++ b/Penumbra/UI/Classes/Colors.cs @@ -28,6 +28,8 @@ public enum ColorId ResTreePlayer, ResTreeNetworked, ResTreeNonNetworked, + SharedTagAdd, + SharedTagRemove } public static class Colors @@ -73,6 +75,8 @@ public static class Colors ColorId.ResTreePlayer => ( 0xFFC0FFC0, "On-Screen: Other Players", "Other players and what they own, in the On-Screen tab." ), ColorId.ResTreeNetworked => ( 0xFFFFFFFF, "On-Screen: Non-Players (Networked)", "Non-player entities handled by the game server, in the On-Screen tab." ), ColorId.ResTreeNonNetworked => ( 0xFFC0C0FF, "On-Screen: Non-Players (Local)", "Non-player entities handled locally, in the On-Screen tab." ), + ColorId.SharedTagAdd => ( 0xFF44AA44, "Shared Tags: Add Tag", "A shared tag that is not present on the current mod and can be added." ), + ColorId.SharedTagRemove => ( 0xFF2222AA, "Shared Tags: Remove Tag", "A shared tag that is already present on the current mod and can be removed." ), _ => throw new ArgumentOutOfRangeException( nameof( color ), color, null ), // @formatter:on }; diff --git a/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs b/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs index 3cc59661..7da13966 100644 --- a/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs @@ -12,14 +12,16 @@ public class ModPanelDescriptionTab : ITab private readonly ModFileSystemSelector _selector; private readonly TutorialService _tutorial; private readonly ModManager _modManager; + private readonly SharedTagManager _sharedTagManager; private readonly TagButtons _localTags = new(); private readonly TagButtons _modTags = new(); - public ModPanelDescriptionTab(ModFileSystemSelector selector, TutorialService tutorial, ModManager modManager) + public ModPanelDescriptionTab(ModFileSystemSelector selector, TutorialService tutorial, ModManager modManager, SharedTagManager sharedTagsConfig) { _selector = selector; _tutorial = tutorial; _modManager = modManager; + _sharedTagManager = sharedTagsConfig; } public ReadOnlySpan Label @@ -34,14 +36,37 @@ public class ModPanelDescriptionTab : ITab ImGui.Dummy(ImGuiHelpers.ScaledVector2(2)); ImGui.Dummy(ImGuiHelpers.ScaledVector2(2)); + var sharedTagsEnabled = _sharedTagManager.SharedTags.Count() > 0; + var sharedTagButtonOffset = sharedTagsEnabled ? ImGui.GetFrameHeight() + ImGui.GetStyle().FramePadding.X : 0; var tagIdx = _localTags.Draw("Local Tags: ", "Custom tags you can set personally that will not be exported to the mod data but only set for you.\n" + "If the mod already contains a local tag in its own tags, the local tag will be ignored.", _selector.Selected!.LocalTags, - out var editedTag); + out var editedTag, rightEndOffset: sharedTagButtonOffset); _tutorial.OpenTutorial(BasicTutorialSteps.Tags); if (tagIdx >= 0) _modManager.DataEditor.ChangeLocalTag(_selector.Selected!, tagIdx, editedTag); + if (sharedTagsEnabled) + { + ImGui.SetCursorPosY(ImGui.GetCursorPosY() - ImGui.GetFrameHeightWithSpacing()); + ImGui.SetCursorPosX(ImGui.GetWindowWidth() - ImGui.GetFrameHeight() - ImGui.GetStyle().FramePadding.X); + var sharedTag = _sharedTagManager.DrawAddFromSharedTags(_selector.Selected!.LocalTags, _selector.Selected!.ModTags, true); + if (sharedTag.Length > 0) + { + var index = _selector.Selected!.LocalTags.IndexOf(sharedTag); + if (index < 0) + { + index = _selector.Selected!.LocalTags.Count; + _modManager.DataEditor.ChangeLocalTag(_selector.Selected, index, sharedTag); + } + else + { + _modManager.DataEditor.ChangeLocalTag(_selector.Selected, index, string.Empty); + } + + } + } + if (_selector.Selected!.ModTags.Count > 0) _modTags.Draw("Mod Tags: ", "Tags assigned by the mod creator and saved with the mod data. To edit these, look at Edit Mod.", _selector.Selected!.ModTags, out var _, false, diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index 20da8fde..3620c7ac 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -28,6 +28,7 @@ public class ModPanelEditTab : ITab private readonly ModEditWindow _editWindow; private readonly ModEditor _editor; private readonly Configuration _config; + private readonly SharedTagManager _sharedTagManager; private readonly TagButtons _modTags = new(); @@ -37,7 +38,8 @@ public class ModPanelEditTab : ITab private Mod _mod = null!; public ModPanelEditTab(ModManager modManager, ModFileSystemSelector selector, ModFileSystem fileSystem, Services.MessageService messager, - ModEditWindow editWindow, ModEditor editor, FilenameService filenames, ModExportManager modExportManager, Configuration config) + ModEditWindow editWindow, ModEditor editor, FilenameService filenames, ModExportManager modExportManager, Configuration config, + SharedTagManager sharedTagManager) { _modManager = modManager; _selector = selector; @@ -48,6 +50,7 @@ public class ModPanelEditTab : ITab _filenames = filenames; _modExportManager = modExportManager; _config = config; + _sharedTagManager = sharedTagManager; } public ReadOnlySpan Label @@ -80,11 +83,34 @@ public class ModPanelEditTab : ITab } UiHelpers.DefaultLineSpace(); + var sharedTagsEnabled = _sharedTagManager.SharedTags.Count() > 0; + var sharedTagButtonOffset = sharedTagsEnabled ? ImGui.GetFrameHeight() + ImGui.GetStyle().FramePadding.X : 0; var tagIdx = _modTags.Draw("Mod Tags: ", "Edit tags by clicking them, or add new tags. Empty tags are removed.", _mod.ModTags, - out var editedTag); + out var editedTag, rightEndOffset: sharedTagButtonOffset); if (tagIdx >= 0) _modManager.DataEditor.ChangeModTag(_mod, tagIdx, editedTag); + if (sharedTagsEnabled) + { + ImGui.SetCursorPosY(ImGui.GetCursorPosY() - ImGui.GetFrameHeightWithSpacing()); + ImGui.SetCursorPosX(ImGui.GetWindowWidth() - ImGui.GetFrameHeight() - ImGui.GetStyle().FramePadding.X); + var sharedTag = _sharedTagManager.DrawAddFromSharedTags(_selector.Selected!.LocalTags, _selector.Selected!.ModTags, false); + if (sharedTag.Length > 0) + { + var index = _selector.Selected!.ModTags.IndexOf(sharedTag); + if (index < 0) + { + index = _selector.Selected!.ModTags.Count; + _modManager.DataEditor.ChangeModTag(_selector.Selected, index, sharedTag); + } + else + { + _modManager.DataEditor.ChangeModTag(_selector.Selected, index, string.Empty); + } + + } + } + UiHelpers.DefaultLineSpace(); AddOptionGroup.Draw(_filenames, _modManager, _mod, _config.ReplaceNonAsciiOnImport); UiHelpers.DefaultLineSpace(); diff --git a/Penumbra/UI/SharedTagManager.cs b/Penumbra/UI/SharedTagManager.cs new file mode 100644 index 00000000..9562b24c --- /dev/null +++ b/Penumbra/UI/SharedTagManager.cs @@ -0,0 +1,160 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Dalamud.Interface; +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Widgets; +using Penumbra.Mods; +using Penumbra.UI.Classes; + +namespace Penumbra.UI; +public sealed class SharedTagManager +{ + private static uint _tagButtonAddColor = ColorId.SharedTagAdd.Value(); + private static uint _tagButtonRemoveColor = ColorId.SharedTagRemove.Value(); + + private static float _minTagButtonWidth = 15; + + private const string PopupContext = "SharedTagsPopup"; + private bool _isPopupOpen = false; + + + public IReadOnlyList SharedTags { get; internal set; } = Array.Empty(); + + public SharedTagManager() + { + } + + public void ChangeSharedTag(int tagIdx, string tag) + { + if (tagIdx < 0 || tagIdx > SharedTags.Count) + return; + + if (tagIdx == SharedTags.Count) // Adding a new tag + { + SharedTags = SharedTags.Append(tag).Distinct().Where(tag => tag.Length > 0).OrderBy(a => a).ToArray(); + } + else // Editing an existing tag + { + var tmpTags = SharedTags.ToArray(); + tmpTags[tagIdx] = tag; + SharedTags = tmpTags.Distinct().Where(tag => tag.Length > 0).OrderBy(a => a).ToArray(); + } + } + + public string DrawAddFromSharedTags(IReadOnlyCollection localTags, IReadOnlyCollection modTags, bool editLocal) + { + var tagToAdd = ""; + if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Tags.ToIconString(), new Vector2(ImGui.GetFrameHeight()), "Add Shared Tag... (Right-click to close popup)", + false, true) || _isPopupOpen) + return DrawSharedTagsPopup(localTags, modTags, editLocal); + + + return tagToAdd; + } + + private string DrawSharedTagsPopup(IReadOnlyCollection localTags, IReadOnlyCollection modTags, bool editLocal) + { + var selected = ""; + if (!ImGui.IsPopupOpen(PopupContext)) + { + ImGui.OpenPopup(PopupContext); + _isPopupOpen = true; + } + + var display = ImGui.GetIO().DisplaySize; + var height = Math.Min(display.Y / 4, 10 * ImGui.GetFrameHeightWithSpacing()); + var width = display.X / 6; + var size = new Vector2(width, height); + ImGui.SetNextWindowSize(size); + using var popup = ImRaii.Popup(PopupContext); + if (!popup) + return selected; + + ImGui.Text("Shared Tags"); + ImGuiUtil.HoverTooltip("Right-click to close popup"); + ImGui.Separator(); + + foreach (var tag in SharedTags) + { + if (DrawColoredButton(localTags, modTags, tag, editLocal)) + { + selected = tag; + return selected; + } + ImGui.SameLine(); + } + + if (ImGui.IsMouseClicked(ImGuiMouseButton.Right)) + { + _isPopupOpen = false; + } + + return selected; + } + + private static bool DrawColoredButton(IReadOnlyCollection localTags, IReadOnlyCollection modTags, string buttonLabel, bool editLocal) + { + var isLocalTagPresent = localTags.Contains(buttonLabel); + var isModTagPresent = modTags.Contains(buttonLabel); + + var buttonWidth = CalcTextButtonWidth(buttonLabel); + // Would prefer to be able to fit at least 2 buttons per line so the popup doesn't look sparse with lots of long tags. Thus long tags will be trimmed. + var maxButtonWidth = (ImGui.GetContentRegionMax().X - ImGui.GetWindowContentRegionMin().X) * 0.5f - ImGui.GetStyle().ItemSpacing.X; + var displayedLabel = buttonLabel; + if (buttonWidth >= maxButtonWidth) + { + displayedLabel = TrimButtonTextToWidth(buttonLabel, maxButtonWidth); + buttonWidth = CalcTextButtonWidth(displayedLabel); + } + + // Prevent adding a new tag past the right edge of the popup + if (buttonWidth + ImGui.GetStyle().ItemSpacing.X >= ImGui.GetContentRegionAvail().X) + ImGui.NewLine(); + + // Trimmed tag names can collide, but the full tags are guaranteed distinct so use the full tag as the ID to avoid an ImGui moment. + ImRaii.PushId(buttonLabel); + + if (editLocal && isModTagPresent || !editLocal && isLocalTagPresent) + { + using var alpha = ImRaii.PushStyle(ImGuiStyleVar.Alpha, 0.5f); + ImGui.Button(displayedLabel); + alpha.Pop(); + return false; + } + + using (ImRaii.PushColor(ImGuiCol.Button, isLocalTagPresent || isModTagPresent ? _tagButtonRemoveColor : _tagButtonAddColor)) + { + return ImGui.Button(displayedLabel); + } + } + + private static string TrimButtonTextToWidth(string fullText, float maxWidth) + { + var trimmedText = fullText; + + while (trimmedText.Length > _minTagButtonWidth) + { + var nextTrim = trimmedText.Substring(0, Math.Max(trimmedText.Length - 1, 0)); + + // An ellipsis will be used to indicate trimmed tags + if (CalcTextButtonWidth(nextTrim + "...") < maxWidth) + { + return nextTrim + "..."; + } + trimmedText = nextTrim; + } + + return trimmedText; + } + + private static float CalcTextButtonWidth(string text) + { + return ImGui.CalcTextSize(text).X + 2 * ImGui.GetStyle().FramePadding.X; + } + +} diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index a03e7b87..f37c2c81 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -41,15 +41,18 @@ public class SettingsTab : ITab private readonly DalamudConfigService _dalamudConfig; private readonly DalamudPluginInterface _pluginInterface; private readonly IDataManager _gameData; + private readonly SharedTagManager _sharedTagManager; private int _minimumX = int.MaxValue; private int _minimumY = int.MaxValue; + private readonly TagButtons _sharedTags = new(); + public SettingsTab(DalamudPluginInterface pluginInterface, Configuration config, FontReloader fontReloader, TutorialService tutorial, Penumbra penumbra, FileDialogService fileDialog, ModManager modManager, ModFileSystemSelector selector, CharacterUtility characterUtility, ResidentResourceManager residentResources, ModExportManager modExportManager, HttpApi httpApi, DalamudSubstitutionProvider dalamudSubstitutionProvider, FileCompactor compactor, DalamudConfigService dalamudConfig, - IDataManager gameData) + IDataManager gameData, SharedTagManager sharedTagConfig) { _pluginInterface = pluginInterface; _config = config; @@ -69,6 +72,9 @@ public class SettingsTab : ITab _gameData = gameData; if (_compactor.CanCompact) _compactor.Enabled = _config.UseFileSystemCompression; + _sharedTagManager = sharedTagConfig; + if (sharedTagConfig.SharedTags.Count == 0 && _config.SharedTags != null) + sharedTagConfig.SharedTags = _config.SharedTags; } public void DrawHeader() @@ -96,6 +102,7 @@ public class SettingsTab : ITab DrawGeneralSettings(); DrawColorSettings(); DrawAdvancedSettings(); + DrawSharedTagsSection(); DrawSupportButtons(); } @@ -902,4 +909,21 @@ public class SettingsTab : ITab if (ImGui.Button("Show Changelogs", new Vector2(width, 0))) _penumbra.ForceChangelogOpen(); } + + private void DrawSharedTagsSection() + { + if (!ImGui.CollapsingHeader("Tags")) + return; + + var tagIdx = _sharedTags.Draw("Shared Tags: ", + "Tags that can be added/removed from mods with 1 click.", _sharedTagManager.SharedTags, + out var editedTag); + + if (tagIdx >= 0) + { + _sharedTagManager.ChangeSharedTag(tagIdx, editedTag); + _config.SharedTags = _sharedTagManager.SharedTags; + _config.Save(); + } + } } From 334be441f88dc554461cdddbb81ba658abe5b958 Mon Sep 17 00:00:00 2001 From: AeAstralis Date: Fri, 1 Mar 2024 18:04:03 -0500 Subject: [PATCH 020/433] Update OtterGui to 97ac353 Updates OtterGui to 97ac3538536a17e980027f783ec5e5167b371f71 to include change that xivdev/Penumbra#399 is dependent on. --- OtterGui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OtterGui b/OtterGui index 1a187f75..97ac3538 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 1a187f756f2e8823197bd43db1c3383231f5eaff +Subproject commit 97ac3538536a17e980027f783ec5e5167b371f71 From 282f6d48551e0208d4ad06da18167ad74c48d9e2 Mon Sep 17 00:00:00 2001 From: AeAstralis Date: Fri, 1 Mar 2024 21:03:34 -0500 Subject: [PATCH 021/433] Migrate shared tag to own config, address comments Migrates the configuration for shared tags to a separate config file, and addresses CR feedback. --- Penumbra/Configuration.cs | 2 - Penumbra/Services/FilenameService.cs | 1 + Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs | 20 +- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 20 +- Penumbra/UI/SharedTagManager.cs | 171 ++++++++++++++---- Penumbra/UI/Tabs/SettingsTab.cs | 6 +- 6 files changed, 143 insertions(+), 77 deletions(-) diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index 43253223..188be65d 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -87,8 +87,6 @@ public class Configuration : IPluginConfiguration, ISavable public Dictionary Colors { get; set; } = Enum.GetValues().ToDictionary(c => c, c => c.Data().DefaultColor); - public IReadOnlyList SharedTags { get; set; } - /// /// Load the current configuration. /// Includes adding new colors and migrating from old versions. diff --git a/Penumbra/Services/FilenameService.cs b/Penumbra/Services/FilenameService.cs index 5f918a90..23694ebc 100644 --- a/Penumbra/Services/FilenameService.cs +++ b/Penumbra/Services/FilenameService.cs @@ -14,6 +14,7 @@ public class FilenameService(DalamudPluginInterface pi) : IService public readonly string EphemeralConfigFile = Path.Combine(pi.ConfigDirectory.FullName, "ephemeral_config.json"); public readonly string FilesystemFile = Path.Combine(pi.ConfigDirectory.FullName, "sort_order.json"); public readonly string ActiveCollectionsFile = Path.Combine(pi.ConfigDirectory.FullName, "active_collections.json"); + public readonly string SharedTagFile = Path.Combine(pi.ConfigDirectory.FullName, "shared_tags.json"); /// Obtain the path of a collection file given its name. public string CollectionFile(ModCollection collection) diff --git a/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs b/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs index 7da13966..5f2687c3 100644 --- a/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs @@ -36,7 +36,7 @@ public class ModPanelDescriptionTab : ITab ImGui.Dummy(ImGuiHelpers.ScaledVector2(2)); ImGui.Dummy(ImGuiHelpers.ScaledVector2(2)); - var sharedTagsEnabled = _sharedTagManager.SharedTags.Count() > 0; + var sharedTagsEnabled = _sharedTagManager.SharedTags.Count > 0; var sharedTagButtonOffset = sharedTagsEnabled ? ImGui.GetFrameHeight() + ImGui.GetStyle().FramePadding.X : 0; var tagIdx = _localTags.Draw("Local Tags: ", "Custom tags you can set personally that will not be exported to the mod data but only set for you.\n" @@ -48,23 +48,7 @@ public class ModPanelDescriptionTab : ITab if (sharedTagsEnabled) { - ImGui.SetCursorPosY(ImGui.GetCursorPosY() - ImGui.GetFrameHeightWithSpacing()); - ImGui.SetCursorPosX(ImGui.GetWindowWidth() - ImGui.GetFrameHeight() - ImGui.GetStyle().FramePadding.X); - var sharedTag = _sharedTagManager.DrawAddFromSharedTags(_selector.Selected!.LocalTags, _selector.Selected!.ModTags, true); - if (sharedTag.Length > 0) - { - var index = _selector.Selected!.LocalTags.IndexOf(sharedTag); - if (index < 0) - { - index = _selector.Selected!.LocalTags.Count; - _modManager.DataEditor.ChangeLocalTag(_selector.Selected, index, sharedTag); - } - else - { - _modManager.DataEditor.ChangeLocalTag(_selector.Selected, index, string.Empty); - } - - } + _sharedTagManager.DrawAddFromSharedTagsAndUpdateTags(_selector.Selected!.LocalTags, _selector.Selected!.ModTags, true, _selector.Selected!); } if (_selector.Selected!.ModTags.Count > 0) diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index 3620c7ac..9b4a582f 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -83,7 +83,7 @@ public class ModPanelEditTab : ITab } UiHelpers.DefaultLineSpace(); - var sharedTagsEnabled = _sharedTagManager.SharedTags.Count() > 0; + var sharedTagsEnabled = _sharedTagManager.SharedTags.Count > 0; var sharedTagButtonOffset = sharedTagsEnabled ? ImGui.GetFrameHeight() + ImGui.GetStyle().FramePadding.X : 0; var tagIdx = _modTags.Draw("Mod Tags: ", "Edit tags by clicking them, or add new tags. Empty tags are removed.", _mod.ModTags, out var editedTag, rightEndOffset: sharedTagButtonOffset); @@ -92,23 +92,7 @@ public class ModPanelEditTab : ITab if (sharedTagsEnabled) { - ImGui.SetCursorPosY(ImGui.GetCursorPosY() - ImGui.GetFrameHeightWithSpacing()); - ImGui.SetCursorPosX(ImGui.GetWindowWidth() - ImGui.GetFrameHeight() - ImGui.GetStyle().FramePadding.X); - var sharedTag = _sharedTagManager.DrawAddFromSharedTags(_selector.Selected!.LocalTags, _selector.Selected!.ModTags, false); - if (sharedTag.Length > 0) - { - var index = _selector.Selected!.ModTags.IndexOf(sharedTag); - if (index < 0) - { - index = _selector.Selected!.ModTags.Count; - _modManager.DataEditor.ChangeModTag(_selector.Selected, index, sharedTag); - } - else - { - _modManager.DataEditor.ChangeModTag(_selector.Selected, index, string.Empty); - } - - } + _sharedTagManager.DrawAddFromSharedTagsAndUpdateTags(_selector.Selected!.LocalTags, _selector.Selected!.ModTags, false, _selector.Selected!); } UiHelpers.DefaultLineSpace(); diff --git a/Penumbra/UI/SharedTagManager.cs b/Penumbra/UI/SharedTagManager.cs index 9562b24c..23196319 100644 --- a/Penumbra/UI/SharedTagManager.cs +++ b/Penumbra/UI/SharedTagManager.cs @@ -1,19 +1,22 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Dalamud.Interface; +using Dalamud.Interface; +using Dalamud.Interface.Internal.Notifications; +using Dalamud.Utility; using ImGuiNET; +using Newtonsoft.Json; using OtterGui; +using OtterGui.Classes; using OtterGui.Raii; -using OtterGui.Widgets; -using Penumbra.Mods; +using Penumbra.Mods.Manager; +using Penumbra.Services; using Penumbra.UI.Classes; +using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs; namespace Penumbra.UI; -public sealed class SharedTagManager +public sealed class SharedTagManager : ISavable { + private readonly ModManager _modManager; + private readonly SaveService _saveService; + private static uint _tagButtonAddColor = ColorId.SharedTagAdd.Value(); private static uint _tagButtonRemoveColor = ColorId.SharedTagRemove.Value(); @@ -22,11 +25,66 @@ public sealed class SharedTagManager private const string PopupContext = "SharedTagsPopup"; private bool _isPopupOpen = false; + // Operations on this list assume that it is sorted and will keep it sorted if that is the case. + // The list also gets re-sorted when first loaded from config in case the config was modified. + [JsonRequired] + private readonly List _sharedTags = []; + [JsonIgnore] + public IReadOnlyList SharedTags => _sharedTags; - public IReadOnlyList SharedTags { get; internal set; } = Array.Empty(); + public int ConfigVersion = 1; - public SharedTagManager() + public SharedTagManager(ModManager modManager, SaveService saveService) { + _modManager = modManager; + _saveService = saveService; + Load(); + } + + public string ToFilename(FilenameService fileNames) + { + return fileNames.SharedTagFile; + } + + public void Save(StreamWriter writer) + { + using var jWriter = new JsonTextWriter(writer) { Formatting = Formatting.Indented }; + var serializer = new JsonSerializer { Formatting = Formatting.Indented }; + serializer.Serialize(jWriter, this); + } + + public void Save() + => _saveService.DelaySave(this, TimeSpan.FromSeconds(5)); + + private void Load() + { + static void HandleDeserializationError(object? sender, ErrorEventArgs errorArgs) + { + Penumbra.Log.Error( + $"Error parsing shared tags Configuration at {errorArgs.ErrorContext.Path}, using default or migrating:\n{errorArgs.ErrorContext.Error}"); + errorArgs.ErrorContext.Handled = true; + } + + if (!File.Exists(_saveService.FileNames.SharedTagFile)) + return; + + try + { + var text = File.ReadAllText(_saveService.FileNames.SharedTagFile); + JsonConvert.PopulateObject(text, this, new JsonSerializerSettings + { + Error = HandleDeserializationError, + }); + + // Any changes to this within this class should keep it sorted, but in case someone went in and manually changed the JSON, run a sort on initial load. + _sharedTags.Sort(); + } + catch (Exception ex) + { + Penumbra.Messager.NotificationMessage(ex, + "Error reading shared tags Configuration, reverting to default.", + "Error reading shared tags Configuration", NotificationType.Error); + } } public void ChangeSharedTag(int tagIdx, string tag) @@ -34,32 +92,74 @@ public sealed class SharedTagManager if (tagIdx < 0 || tagIdx > SharedTags.Count) return; - if (tagIdx == SharedTags.Count) // Adding a new tag + // In the case of editing a tag, remove what's there prior to doing an insert. + if (tagIdx != SharedTags.Count) { - SharedTags = SharedTags.Append(tag).Distinct().Where(tag => tag.Length > 0).OrderBy(a => a).ToArray(); + _sharedTags.RemoveAt(tagIdx); } - else // Editing an existing tag + + if (!string.IsNullOrEmpty(tag)) { - var tmpTags = SharedTags.ToArray(); - tmpTags[tagIdx] = tag; - SharedTags = tmpTags.Distinct().Where(tag => tag.Length > 0).OrderBy(a => a).ToArray(); + // Taking advantage of the fact that BinarySearch returns the complement of the correct sorted position for the tag. + var existingIdx = _sharedTags.BinarySearch(tag); + if (existingIdx < 0) + _sharedTags.Insert(~existingIdx, tag); + } + + Save(); + } + + public void DrawAddFromSharedTagsAndUpdateTags(IReadOnlyCollection localTags, IReadOnlyCollection modTags, bool editLocal, Mods.Mod mod) + { + ImGui.SetCursorPosY(ImGui.GetCursorPosY() - ImGui.GetFrameHeightWithSpacing()); + ImGui.SetCursorPosX(ImGui.GetWindowWidth() - ImGui.GetFrameHeight() - ImGui.GetStyle().FramePadding.X); + + var sharedTag = DrawAddFromSharedTags(localTags, modTags, editLocal); + + if (sharedTag.Length > 0) + { + var index = editLocal ? mod.LocalTags.IndexOf(sharedTag) : mod.ModTags.IndexOf(sharedTag); + + if (editLocal) + { + if (index < 0) + { + index = mod.LocalTags.Count; + _modManager.DataEditor.ChangeLocalTag(mod, index, sharedTag); + } + else + { + _modManager.DataEditor.ChangeLocalTag(mod, index, string.Empty); + } + } else + { + if (index < 0) + { + index = mod.ModTags.Count; + _modManager.DataEditor.ChangeModTag(mod, index, sharedTag); + } + else + { + _modManager.DataEditor.ChangeModTag(mod, index, string.Empty); + } + } + } } public string DrawAddFromSharedTags(IReadOnlyCollection localTags, IReadOnlyCollection modTags, bool editLocal) { - var tagToAdd = ""; + var tagToAdd = string.Empty; if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Tags.ToIconString(), new Vector2(ImGui.GetFrameHeight()), "Add Shared Tag... (Right-click to close popup)", false, true) || _isPopupOpen) return DrawSharedTagsPopup(localTags, modTags, editLocal); - return tagToAdd; } private string DrawSharedTagsPopup(IReadOnlyCollection localTags, IReadOnlyCollection modTags, bool editLocal) { - var selected = ""; + var selected = string.Empty; if (!ImGui.IsPopupOpen(PopupContext)) { ImGui.OpenPopup(PopupContext); @@ -75,16 +175,15 @@ public sealed class SharedTagManager if (!popup) return selected; - ImGui.Text("Shared Tags"); + ImGui.TextUnformatted("Shared Tags"); ImGuiUtil.HoverTooltip("Right-click to close popup"); ImGui.Separator(); - foreach (var tag in SharedTags) + foreach (var (tag, idx) in SharedTags.WithIndex()) { - if (DrawColoredButton(localTags, modTags, tag, editLocal)) + if (DrawColoredButton(localTags, modTags, tag, editLocal, idx)) { selected = tag; - return selected; } ImGui.SameLine(); } @@ -97,8 +196,10 @@ public sealed class SharedTagManager return selected; } - private static bool DrawColoredButton(IReadOnlyCollection localTags, IReadOnlyCollection modTags, string buttonLabel, bool editLocal) + private static bool DrawColoredButton(IReadOnlyCollection localTags, IReadOnlyCollection modTags, string buttonLabel, bool editLocal, int index) { + var ret = false; + var isLocalTagPresent = localTags.Contains(buttonLabel); var isModTagPresent = modTags.Contains(buttonLabel); @@ -116,21 +217,24 @@ public sealed class SharedTagManager if (buttonWidth + ImGui.GetStyle().ItemSpacing.X >= ImGui.GetContentRegionAvail().X) ImGui.NewLine(); - // Trimmed tag names can collide, but the full tags are guaranteed distinct so use the full tag as the ID to avoid an ImGui moment. - ImRaii.PushId(buttonLabel); + // Trimmed tag names can collide, and while tag names are currently distinct this may not always be the case. As such use the index to avoid an ImGui moment. + using var id = ImRaii.PushId(index); if (editLocal && isModTagPresent || !editLocal && isLocalTagPresent) { using var alpha = ImRaii.PushStyle(ImGuiStyleVar.Alpha, 0.5f); ImGui.Button(displayedLabel); - alpha.Pop(); - return false; + } + else + { + using (ImRaii.PushColor(ImGuiCol.Button, isLocalTagPresent || isModTagPresent ? _tagButtonRemoveColor : _tagButtonAddColor)) + { + if (ImGui.Button(displayedLabel)) + ret = true; + } } - using (ImRaii.PushColor(ImGuiCol.Button, isLocalTagPresent || isModTagPresent ? _tagButtonRemoveColor : _tagButtonAddColor)) - { - return ImGui.Button(displayedLabel); - } + return ret; } private static string TrimButtonTextToWidth(string fullText, float maxWidth) @@ -156,5 +260,4 @@ public sealed class SharedTagManager { return ImGui.CalcTextSize(text).X + 2 * ImGui.GetStyle().FramePadding.X; } - } diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index f37c2c81..71f108c2 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -73,8 +73,6 @@ public class SettingsTab : ITab if (_compactor.CanCompact) _compactor.Enabled = _config.UseFileSystemCompression; _sharedTagManager = sharedTagConfig; - if (sharedTagConfig.SharedTags.Count == 0 && _config.SharedTags != null) - sharedTagConfig.SharedTags = _config.SharedTags; } public void DrawHeader() @@ -916,14 +914,12 @@ public class SettingsTab : ITab return; var tagIdx = _sharedTags.Draw("Shared Tags: ", - "Tags that can be added/removed from mods with 1 click.", _sharedTagManager.SharedTags, + "Predefined tags that can be added or removed from mods with a single click.", _sharedTagManager.SharedTags, out var editedTag); if (tagIdx >= 0) { _sharedTagManager.ChangeSharedTag(tagIdx, editedTag); - _config.SharedTags = _sharedTagManager.SharedTags; - _config.Save(); } } } From c1cdb28bb5f35a9816b3042d786bcde40e4bed18 Mon Sep 17 00:00:00 2001 From: ackwell Date: Mon, 29 Jan 2024 16:58:47 +1100 Subject: [PATCH 022/433] Use named enum values for vertex decl mismatch error --- Penumbra/Import/Models/Import/Utility.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Penumbra/Import/Models/Import/Utility.cs b/Penumbra/Import/Models/Import/Utility.cs index a1e44136..21655563 100644 --- a/Penumbra/Import/Models/Import/Utility.cs +++ b/Penumbra/Import/Models/Import/Utility.cs @@ -1,4 +1,5 @@ using Lumina.Data.Parsing; +using Penumbra.GameData.Files; namespace Penumbra.Import.Models.Import; @@ -43,15 +44,15 @@ public static class Utility throw notifier.Exception( $""" All sub-meshes of a mesh must have equivalent vertex declarations. - Current: {FormatVertexDeclaration(current)} - New: {FormatVertexDeclaration(@new)} + Current: {FormatVertexDeclaration(current)} + New: {FormatVertexDeclaration(@new)} """ ); } private static string FormatVertexDeclaration(MdlStructs.VertexDeclarationStruct vertexDeclaration) => string.Join(", ", - vertexDeclaration.VertexElements.Select(element => $"{element.Usage} ({element.Type}@{element.Stream}:{element.Offset})")); + vertexDeclaration.VertexElements.Select(element => $"{(MdlFile.VertexUsage)element.Usage} ({(MdlFile.VertexType)element.Type}@{element.Stream}:{element.Offset})")); private static bool VertexDeclarationMismatch(MdlStructs.VertexDeclarationStruct a, MdlStructs.VertexDeclarationStruct b) { From 1cee1c24ec219a00f20bb5112771b77591f32844 Mon Sep 17 00:00:00 2001 From: ackwell Date: Tue, 30 Jan 2024 21:39:12 +1100 Subject: [PATCH 023/433] Skip degenerate triangles targeted by shape keys --- Penumbra/Import/Models/Export/MeshExporter.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Penumbra/Import/Models/Export/MeshExporter.cs b/Penumbra/Import/Models/Export/MeshExporter.cs index df315094..d3ca87dc 100644 --- a/Penumbra/Import/Models/Export/MeshExporter.cs +++ b/Penumbra/Import/Models/Export/MeshExporter.cs @@ -214,10 +214,18 @@ public class MeshExporter var morphBuilder = meshBuilder.UseMorphTarget(shapeNames.Count); shapeNames.Add(shape.ShapeName); - foreach (var shapeValue in shapeValues) + foreach (var (shapeValue, shapeValueIndex) in shapeValues.WithIndex()) { + var gltfIndex = gltfIndices[shapeValue.BaseIndicesIndex - indexBase]; + + if (gltfIndex == -1) + { + _notifier.Warning($"{name}: Shape {shape.ShapeName} mapping {shapeValueIndex} targets a degenerate triangle, ignoring."); + continue; + } + morphBuilder.SetVertex( - primitiveVertices[gltfIndices[shapeValue.BaseIndicesIndex - indexBase]].GetGeometry(), + primitiveVertices[gltfIndex].GetGeometry(), vertices[shapeValue.ReplacingVertexIndex].GetGeometry() ); } From a4bd015836a6a7e8dec83a3a3553e946832c40b8 Mon Sep 17 00:00:00 2001 From: ackwell Date: Tue, 20 Feb 2024 21:35:14 +1100 Subject: [PATCH 024/433] Fix index offset mis-cast causing overflow --- Penumbra/Import/Models/Import/MeshImporter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Import/Models/Import/MeshImporter.cs b/Penumbra/Import/Models/Import/MeshImporter.cs index 8ab55734..efebdba4 100644 --- a/Penumbra/Import/Models/Import/MeshImporter.cs +++ b/Penumbra/Import/Models/Import/MeshImporter.cs @@ -167,7 +167,7 @@ public class MeshImporter(IEnumerable nodes, IoNotifier notifier) // And finally, merge in the sub-mesh struct itself. _subMeshes.Add(subMesh.SubMeshStruct with { - IndexOffset = (ushort)(subMesh.SubMeshStruct.IndexOffset + indexOffset), + IndexOffset = (uint)(subMesh.SubMeshStruct.IndexOffset + indexOffset), AttributeIndexMask = Utility.GetMergedAttributeMask( subMesh.SubMeshStruct.AttributeIndexMask, subMesh.MetaAttributes, _metaAttributes), }); From 5c6e0701d96f626ba7f58fcf7541ddc1b62be930 Mon Sep 17 00:00:00 2001 From: ackwell Date: Sun, 25 Feb 2024 22:09:55 +1100 Subject: [PATCH 025/433] Simplify EID handling because IDFK at this point --- Penumbra/Import/Models/Import/MeshImporter.cs | 1 - .../ModEditWindow.Models.MdlTab.cs | 21 +++---------------- 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/Penumbra/Import/Models/Import/MeshImporter.cs b/Penumbra/Import/Models/Import/MeshImporter.cs index efebdba4..1d4b223d 100644 --- a/Penumbra/Import/Models/Import/MeshImporter.cs +++ b/Penumbra/Import/Models/Import/MeshImporter.cs @@ -80,7 +80,6 @@ public class MeshImporter(IEnumerable nodes, IoNotifier notifier) StartIndex = 0, IndexCount = (uint)_indices.Count, - // TODO: import material names MaterialIndex = 0, SubMeshIndex = 0, SubMeshCount = (ushort)_subMeshes.Count, diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs index 7adc4379..637c8401 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs @@ -220,24 +220,9 @@ public partial class ModEditWindow /// Model to copy element ids from. private static void MergeElementIds(MdlFile target, MdlFile source) { - var elementIds = new List(); - - foreach (var sourceElement in source.ElementIds) - { - var sourceBone = source.Bones[sourceElement.ParentBoneName]; - var targetIndex = target.Bones.IndexOf(sourceBone); - // Given that there's no means of authoring these at the moment, this should probably remain a hard error. - if (targetIndex == -1) - throw new Exception( - $"Failed to merge element IDs. Original model contains element IDs targeting bone {sourceBone}, which is not present on the imported model."); - - elementIds.Add(sourceElement with - { - ParentBoneName = (uint)targetIndex, - }); - } - - target.ElementIds = [.. elementIds]; + // This is overly simplistic, but effectively reproduces what TT did, sort of. + // TODO: Get a better idea of what these values represent. `ParentBoneName`, if it is a pointer into the bone array, does not seem to be _bounded_ by the bone array length, at least in the model. I'm guessing it _may_ be pointing into a .sklb instead? (i.e. the weapon's skeleton). EID stuff in general needs more work. + target.ElementIds = [.. source.ElementIds]; } private void BeginIo() From da423b746400d72f3fd7e79ab9d191dc7593bcf6 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 8 Mar 2024 18:13:44 +0100 Subject: [PATCH 026/433] Make lack of Root Directory louder. --- Penumbra/UI/Tabs/SettingsTab.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index a03e7b87..7ea37a75 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -1,5 +1,6 @@ using Dalamud.Interface; using Dalamud.Interface.Components; +using Dalamud.Interface.Utility; using Dalamud.Plugin; using Dalamud.Plugin.Services; using Dalamud.Utility; @@ -223,7 +224,15 @@ public class SettingsTab : ITab using var group = ImRaii.Group(); ImGui.SetNextItemWidth(UiHelpers.InputTextMinusButton3); - var save = ImGui.InputText("##rootDirectory", ref _newModDirectory, RootDirectoryMaxLength, ImGuiInputTextFlags.EnterReturnsTrue); + bool save; + using (ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale, !_modManager.Valid)) + { + using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.RegexWarningBorder) + .Push(ImGuiCol.TextDisabled, Colors.RegexWarningBorder, !_modManager.Valid); + save = ImGui.InputTextWithHint("##rootDirectory", "Enter Root Directory here (MANDATORY)...", ref _newModDirectory, + RootDirectoryMaxLength, ImGuiInputTextFlags.EnterReturnsTrue); + } + var selected = ImGui.IsItemActive(); using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, new Vector2(UiHelpers.ScaleX3, 0)); ImGui.SameLine(); From 29c93f46a0131ba28eca812697b3010b86e520db Mon Sep 17 00:00:00 2001 From: Exter-N Date: Sat, 9 Mar 2024 22:28:46 +0100 Subject: [PATCH 027/433] Add characterglass.shpk to the skin.shpk fixer --- Penumbra.GameData | 2 +- Penumbra/Communication/MtrlShpkLoaded.cs | 4 +- .../Resources/ResourceHandleDestructor.cs | 4 +- Penumbra/Interop/Services/ModelRenderer.cs | 71 +++++++ .../Services/ShaderReplacementFixer.cs | 197 ++++++++++++++++++ Penumbra/Interop/Services/SkinFixer.cs | 139 ------------ Penumbra/Penumbra.cs | 2 +- Penumbra/Services/ServiceManagerA.cs | 3 +- Penumbra/UI/Tabs/Debug/DebugTab.cs | 23 +- 9 files changed, 290 insertions(+), 155 deletions(-) create mode 100644 Penumbra/Interop/Services/ModelRenderer.cs create mode 100644 Penumbra/Interop/Services/ShaderReplacementFixer.cs delete mode 100644 Penumbra/Interop/Services/SkinFixer.cs diff --git a/Penumbra.GameData b/Penumbra.GameData index 3a7f6d86..c0c7eb0d 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 3a7f6d86c9975a4892f58be3c629b7664e6c3733 +Subproject commit c0c7eb0dedb32ea83b019626abba041e90a95319 diff --git a/Penumbra/Communication/MtrlShpkLoaded.cs b/Penumbra/Communication/MtrlShpkLoaded.cs index bd560fd8..8aab0e0e 100644 --- a/Penumbra/Communication/MtrlShpkLoaded.cs +++ b/Penumbra/Communication/MtrlShpkLoaded.cs @@ -10,7 +10,7 @@ public sealed class MtrlShpkLoaded() : EventWrapper - SkinFixer = 0, + /// + ShaderReplacementFixer = 0, } } diff --git a/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs b/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs index 31387101..5ddb7eaa 100644 --- a/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs +++ b/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs @@ -13,8 +13,8 @@ public sealed unsafe class ResourceHandleDestructor : EventWrapperPtr SubfileHelper, - /// - SkinFixer, + /// + ShaderReplacementFixer, } public ResourceHandleDestructor(HookManager hooks) diff --git a/Penumbra/Interop/Services/ModelRenderer.cs b/Penumbra/Interop/Services/ModelRenderer.cs new file mode 100644 index 00000000..6a3bf776 --- /dev/null +++ b/Penumbra/Interop/Services/ModelRenderer.cs @@ -0,0 +1,71 @@ +using Dalamud.Plugin.Services; +using Dalamud.Utility.Signatures; +using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; +using Penumbra.GameData; + +namespace Penumbra.Interop.Services; + +// TODO ClientStructs-ify (https://github.com/aers/FFXIVClientStructs/pull/817) +public unsafe class ModelRenderer : IDisposable +{ + // Will be Manager.Instance()->ModelRenderer.CharacterGlassShaderPackage in CS + private const nint ModelRendererOffset = 0x13660; + private const nint CharacterGlassShaderPackageOffset = 0xD0; + + /// A static pointer to the Render::Manager address. + [Signature(Sigs.RenderManager, ScanType = ScanType.StaticAddress)] + private readonly nint* _renderManagerAddress = null; + + public bool Ready { get; private set; } + + public ShaderPackageResourceHandle** CharacterGlassShaderPackage + => *_renderManagerAddress == 0 + ? null + : (ShaderPackageResourceHandle**)(*_renderManagerAddress + ModelRendererOffset + CharacterGlassShaderPackageOffset).ToPointer(); + + public ShaderPackageResourceHandle* DefaultCharacterGlassShaderPackage { get; private set; } + + private readonly IFramework _framework; + + public ModelRenderer(IFramework framework, IGameInteropProvider interop) + { + interop.InitializeFromAttributes(this); + _framework = framework; + LoadDefaultResources(null!); + if (!Ready) + _framework.Update += LoadDefaultResources; + } + + /// We store the default data of the resources so we can always restore them. + private void LoadDefaultResources(object _) + { + if (*_renderManagerAddress == 0) + return; + + var anyMissing = false; + + if (DefaultCharacterGlassShaderPackage == null) + { + DefaultCharacterGlassShaderPackage = *CharacterGlassShaderPackage; + anyMissing |= DefaultCharacterGlassShaderPackage == null; + } + + if (anyMissing) + return; + + Ready = true; + _framework.Update -= LoadDefaultResources; + } + + /// Return all relevant resources to the default resource. + public void ResetAll() + { + if (!Ready) + return; + + *CharacterGlassShaderPackage = DefaultCharacterGlassShaderPackage; + } + + public void Dispose() + => ResetAll(); +} diff --git a/Penumbra/Interop/Services/ShaderReplacementFixer.cs b/Penumbra/Interop/Services/ShaderReplacementFixer.cs new file mode 100644 index 00000000..e57fe313 --- /dev/null +++ b/Penumbra/Interop/Services/ShaderReplacementFixer.cs @@ -0,0 +1,197 @@ +using Dalamud.Hooking; +using Dalamud.Plugin.Services; +using Dalamud.Utility.Signatures; +using FFXIVClientStructs.FFXIV.Client.Graphics.Render; +using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; +using OtterGui.Classes; +using Penumbra.Communication; +using Penumbra.GameData; +using Penumbra.Interop.Hooks.Resources; +using Penumbra.Services; + +namespace Penumbra.Interop.Services; + +public sealed unsafe class ShaderReplacementFixer : IDisposable +{ + public static ReadOnlySpan SkinShpkName + => "skin.shpk"u8; + + public static ReadOnlySpan CharacterGlassShpkName + => "characterglass.shpk"u8; + + [Signature(Sigs.HumanVTable, ScanType = ScanType.StaticAddress)] + private readonly nint* _humanVTable = null!; + + private delegate nint CharacterBaseOnRenderMaterialDelegate(nint drawObject, OnRenderMaterialParams* param); + private delegate nint ModelRendererOnRenderMaterialDelegate(nint modelRenderer, nint outFlags, nint param, Material* material, uint materialIndex); + + [StructLayout(LayoutKind.Explicit)] + private struct OnRenderMaterialParams + { + [FieldOffset(0x0)] + public Model* Model; + + [FieldOffset(0x8)] + public uint MaterialIndex; + } + + private readonly Hook _humanOnRenderMaterialHook; + + [Signature(Sigs.ModelRendererOnRenderMaterial, DetourName = nameof(ModelRendererOnRenderMaterialDetour))] + private readonly Hook _modelRendererOnRenderMaterialHook = null!; + + private readonly ResourceHandleDestructor _resourceHandleDestructor; + private readonly CommunicatorService _communicator; + private readonly CharacterUtility _utility; + private readonly ModelRenderer _modelRenderer; + + // MaterialResourceHandle set + private readonly ConcurrentSet _moddedSkinShpkMaterials = new(); + private readonly ConcurrentSet _moddedCharacterGlassShpkMaterials = new(); + + private readonly object _skinLock = new(); + private readonly object _characterGlassLock = new(); + + // ConcurrentDictionary.Count uses a lock in its current implementation. + private int _moddedSkinShpkCount; + private int _moddedCharacterGlassShpkCount; + private ulong _skinSlowPathCallDelta; + private ulong _characterGlassSlowPathCallDelta; + + public bool Enabled { get; internal set; } = true; + + public int ModdedSkinShpkCount + => _moddedSkinShpkCount; + + public int ModdedCharacterGlassShpkCount + => _moddedCharacterGlassShpkCount; + + public ShaderReplacementFixer(ResourceHandleDestructor resourceHandleDestructor, CharacterUtility utility, ModelRenderer modelRenderer, + CommunicatorService communicator, IGameInteropProvider interop) + { + interop.InitializeFromAttributes(this); + _resourceHandleDestructor = resourceHandleDestructor; + _utility = utility; + _modelRenderer = modelRenderer; + _communicator = communicator; + _humanOnRenderMaterialHook = interop.HookFromAddress(_humanVTable[62], OnRenderHumanMaterial); + _communicator.MtrlShpkLoaded.Subscribe(OnMtrlShpkLoaded, MtrlShpkLoaded.Priority.ShaderReplacementFixer); + _resourceHandleDestructor.Subscribe(OnResourceHandleDestructor, ResourceHandleDestructor.Priority.ShaderReplacementFixer); + _humanOnRenderMaterialHook.Enable(); + _modelRendererOnRenderMaterialHook.Enable(); + } + + public void Dispose() + { + _modelRendererOnRenderMaterialHook.Dispose(); + _humanOnRenderMaterialHook.Dispose(); + _communicator.MtrlShpkLoaded.Unsubscribe(OnMtrlShpkLoaded); + _resourceHandleDestructor.Unsubscribe(OnResourceHandleDestructor); + _moddedCharacterGlassShpkMaterials.Clear(); + _moddedSkinShpkMaterials.Clear(); + _moddedCharacterGlassShpkCount = 0; + _moddedSkinShpkCount = 0; + } + + public (ulong Skin, ulong CharacterGlass) GetAndResetSlowPathCallDeltas() + => (Interlocked.Exchange(ref _skinSlowPathCallDelta, 0), Interlocked.Exchange(ref _characterGlassSlowPathCallDelta, 0)); + + private static bool IsMaterialWithShpk(MaterialResourceHandle* mtrlResource, ReadOnlySpan shpkName) + { + if (mtrlResource == null) + return false; + + return shpkName.SequenceEqual(mtrlResource->ShpkNameSpan); + } + + private void OnMtrlShpkLoaded(nint mtrlResourceHandle, nint gameObject) + { + var mtrl = (MaterialResourceHandle*)mtrlResourceHandle; + var shpk = mtrl->ShaderPackageResourceHandle; + if (shpk == null) + return; + + var shpkName = mtrl->ShpkNameSpan; + + if (SkinShpkName.SequenceEqual(shpkName) && (nint)shpk != _utility.DefaultSkinShpkResource) + { + if (_moddedSkinShpkMaterials.TryAdd(mtrlResourceHandle)) + Interlocked.Increment(ref _moddedSkinShpkCount); + } + + if (CharacterGlassShpkName.SequenceEqual(shpkName) && shpk != _modelRenderer.DefaultCharacterGlassShaderPackage) + { + if (_moddedCharacterGlassShpkMaterials.TryAdd(mtrlResourceHandle)) + Interlocked.Increment(ref _moddedCharacterGlassShpkCount); + } + } + + private void OnResourceHandleDestructor(Structs.ResourceHandle* handle) + { + if (_moddedSkinShpkMaterials.TryRemove((nint)handle)) + Interlocked.Decrement(ref _moddedSkinShpkCount); + + if (_moddedCharacterGlassShpkMaterials.TryRemove((nint)handle)) + Interlocked.Decrement(ref _moddedCharacterGlassShpkCount); + } + + private nint OnRenderHumanMaterial(nint human, OnRenderMaterialParams* param) + { + // If we don't have any on-screen instances of modded skin.shpk, we don't need the slow path at all. + if (!Enabled || _moddedSkinShpkCount == 0) + return _humanOnRenderMaterialHook.Original(human, param); + + var material = param->Model->Materials[param->MaterialIndex]; + var mtrlResource = material->MaterialResourceHandle; + if (!IsMaterialWithShpk(mtrlResource, SkinShpkName)) + return _humanOnRenderMaterialHook.Original(human, param); + + Interlocked.Increment(ref _skinSlowPathCallDelta); + + // Performance considerations: + // - This function is called from several threads simultaneously, hence the need for synchronization in the swapping path ; + // - Function is called each frame for each material on screen, after culling, i. e. up to thousands of times a frame in crowded areas ; + // - Swapping path is taken up to hundreds of times a frame. + // At the time of writing, the lock doesn't seem to have a noticeable impact in either framerate or CPU usage, but the swapping path shall still be avoided as much as possible. + lock (_skinLock) + { + try + { + _utility.Address->SkinShpkResource = (Structs.ResourceHandle*)mtrlResource->ShaderPackageResourceHandle; + return _humanOnRenderMaterialHook.Original(human, param); + } + finally + { + _utility.Address->SkinShpkResource = (Structs.ResourceHandle*)_utility.DefaultSkinShpkResource; + } + } + } + + private nint ModelRendererOnRenderMaterialDetour(nint modelRenderer, nint outFlags, nint param, Material* material, uint materialIndex) + { + + // If we don't have any on-screen instances of modded characterglass.shpk, we don't need the slow path at all. + if (!Enabled || _moddedCharacterGlassShpkCount == 0) + return _modelRendererOnRenderMaterialHook.Original(modelRenderer, outFlags, param, material, materialIndex); + + var mtrlResource = material->MaterialResourceHandle; + if (!IsMaterialWithShpk(mtrlResource, CharacterGlassShpkName)) + return _modelRendererOnRenderMaterialHook.Original(modelRenderer, outFlags, param, material, materialIndex); + + Interlocked.Increment(ref _characterGlassSlowPathCallDelta); + + // Same performance considerations as above. + lock (_characterGlassLock) + { + try + { + *_modelRenderer.CharacterGlassShaderPackage = mtrlResource->ShaderPackageResourceHandle; + return _modelRendererOnRenderMaterialHook.Original(modelRenderer, outFlags, param, material, materialIndex); + } + finally + { + *_modelRenderer.CharacterGlassShaderPackage = _modelRenderer.DefaultCharacterGlassShaderPackage; + } + } + } +} diff --git a/Penumbra/Interop/Services/SkinFixer.cs b/Penumbra/Interop/Services/SkinFixer.cs deleted file mode 100644 index 21331916..00000000 --- a/Penumbra/Interop/Services/SkinFixer.cs +++ /dev/null @@ -1,139 +0,0 @@ -using Dalamud.Hooking; -using Dalamud.Plugin.Services; -using Dalamud.Utility.Signatures; -using FFXIVClientStructs.FFXIV.Client.Graphics.Render; -using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; -using OtterGui.Classes; -using Penumbra.Communication; -using Penumbra.GameData; -using Penumbra.Interop.Hooks.Resources; -using Penumbra.Services; - -namespace Penumbra.Interop.Services; - -public sealed unsafe class SkinFixer : IDisposable -{ - public static ReadOnlySpan SkinShpkName - => "skin.shpk"u8; - - [Signature(Sigs.HumanVTable, ScanType = ScanType.StaticAddress)] - private readonly nint* _humanVTable = null!; - - private delegate nint OnRenderMaterialDelegate(nint drawObject, OnRenderMaterialParams* param); - - [StructLayout(LayoutKind.Explicit)] - private struct OnRenderMaterialParams - { - [FieldOffset(0x0)] - public Model* Model; - - [FieldOffset(0x8)] - public uint MaterialIndex; - } - - private readonly Hook _onRenderMaterialHook; - - private readonly ResourceHandleDestructor _resourceHandleDestructor; - private readonly CommunicatorService _communicator; - private readonly CharacterUtility _utility; - - // MaterialResourceHandle set - private readonly ConcurrentSet _moddedSkinShpkMaterials = new(); - - private readonly object _lock = new(); - - // ConcurrentDictionary.Count uses a lock in its current implementation. - private int _moddedSkinShpkCount; - private ulong _slowPathCallDelta; - - public bool Enabled { get; internal set; } = true; - - public int ModdedSkinShpkCount - => _moddedSkinShpkCount; - - public SkinFixer(ResourceHandleDestructor resourceHandleDestructor, CharacterUtility utility, CommunicatorService communicator, - IGameInteropProvider interop) - { - interop.InitializeFromAttributes(this); - _resourceHandleDestructor = resourceHandleDestructor; - _utility = utility; - _communicator = communicator; - _onRenderMaterialHook = interop.HookFromAddress(_humanVTable[62], OnRenderHumanMaterial); - _communicator.MtrlShpkLoaded.Subscribe(OnMtrlShpkLoaded, MtrlShpkLoaded.Priority.SkinFixer); - _resourceHandleDestructor.Subscribe(OnResourceHandleDestructor, ResourceHandleDestructor.Priority.SkinFixer); - _onRenderMaterialHook.Enable(); - } - - public void Dispose() - { - _onRenderMaterialHook.Dispose(); - _communicator.MtrlShpkLoaded.Unsubscribe(OnMtrlShpkLoaded); - _resourceHandleDestructor.Unsubscribe(OnResourceHandleDestructor); - _moddedSkinShpkMaterials.Clear(); - _moddedSkinShpkCount = 0; - } - - public ulong GetAndResetSlowPathCallDelta() - => Interlocked.Exchange(ref _slowPathCallDelta, 0); - - private static bool IsSkinMaterial(MaterialResourceHandle* mtrlResource) - { - if (mtrlResource == null) - return false; - - var shpkName = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(mtrlResource->ShpkName); - return SkinShpkName.SequenceEqual(shpkName); - } - - private void OnMtrlShpkLoaded(nint mtrlResourceHandle, nint gameObject) - { - var mtrl = (MaterialResourceHandle*)mtrlResourceHandle; - var shpk = mtrl->ShaderPackageResourceHandle; - if (shpk == null) - return; - - if (!IsSkinMaterial(mtrl) || (nint)shpk == _utility.DefaultSkinShpkResource) - return; - - if (_moddedSkinShpkMaterials.TryAdd(mtrlResourceHandle)) - Interlocked.Increment(ref _moddedSkinShpkCount); - } - - private void OnResourceHandleDestructor(Structs.ResourceHandle* handle) - { - if (_moddedSkinShpkMaterials.TryRemove((nint)handle)) - Interlocked.Decrement(ref _moddedSkinShpkCount); - } - - private nint OnRenderHumanMaterial(nint human, OnRenderMaterialParams* param) - { - // If we don't have any on-screen instances of modded skin.shpk, we don't need the slow path at all. - if (!Enabled || _moddedSkinShpkCount == 0) - return _onRenderMaterialHook.Original(human, param); - - var material = param->Model->Materials[param->MaterialIndex]; - var mtrlResource = material->MaterialResourceHandle; - if (!IsSkinMaterial(mtrlResource)) - return _onRenderMaterialHook.Original(human, param); - - Interlocked.Increment(ref _slowPathCallDelta); - - // Performance considerations: - // - This function is called from several threads simultaneously, hence the need for synchronization in the swapping path ; - // - Function is called each frame for each material on screen, after culling, i. e. up to thousands of times a frame in crowded areas ; - // - Swapping path is taken up to hundreds of times a frame. - // At the time of writing, the lock doesn't seem to have a noticeable impact in either framerate or CPU usage, but the swapping path shall still be avoided as much as possible. - lock (_lock) - { - try - { - _utility.Address->SkinShpkResource = (Structs.ResourceHandle*)mtrlResource->ShaderPackageResourceHandle; - return _onRenderMaterialHook.Original(human, param); - } - finally - { - _utility.Address->SkinShpkResource = (Structs.ResourceHandle*)_utility.DefaultSkinShpkResource; - } - } - } -} diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index 15b7ce56..67f523ba 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -78,7 +78,7 @@ public class Penumbra : IDalamudPlugin _services.GetService(); // Initialize because not required anywhere else. _collectionManager.Caches.CreateNecessaryCaches(); _services.GetService(); - _services.GetService(); + _services.GetService(); _services.GetService(); // Initialize before Interface. diff --git a/Penumbra/Services/ServiceManagerA.cs b/Penumbra/Services/ServiceManagerA.cs index f25aac7c..191d8d11 100644 --- a/Penumbra/Services/ServiceManagerA.cs +++ b/Penumbra/Services/ServiceManagerA.cs @@ -92,6 +92,7 @@ public static class ServiceManagerA return new CutsceneResolver(cutsceneService.GetParentIndex); }) .AddSingleton() + .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() @@ -132,7 +133,7 @@ public static class ServiceManagerA .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton(); + .AddSingleton(); private static ServiceManager AddResolvers(this ServiceManager services) => services.AddSingleton() diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index 66b93b04..f4ddbe31 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -86,7 +86,7 @@ public class DebugTab : Window, ITab private readonly ImportPopup _importPopup; private readonly FrameworkManager _framework; private readonly TextureManager _textureManager; - private readonly SkinFixer _skinFixer; + private readonly ShaderReplacementFixer _shaderReplacementFixer; private readonly RedrawService _redraws; private readonly DictEmote _emotes; private readonly Diagnostics _diagnostics; @@ -99,7 +99,7 @@ public class DebugTab : Window, ITab ResourceManagerService resourceManager, PenumbraIpcProviders ipc, CollectionResolver collectionResolver, DrawObjectState drawObjectState, PathState pathState, SubfileHelper subfileHelper, IdentifiedCollectionCache identifiedCollectionCache, CutsceneService cutsceneService, ModImportManager modImporter, ImportPopup importPopup, FrameworkManager framework, - TextureManager textureManager, SkinFixer skinFixer, RedrawService redraws, DictEmote emotes, Diagnostics diagnostics, IpcTester ipcTester) + TextureManager textureManager, ShaderReplacementFixer shaderReplacementFixer, RedrawService redraws, DictEmote emotes, Diagnostics diagnostics, IpcTester ipcTester) : base("Penumbra Debug Window", ImGuiWindowFlags.NoCollapse) { IsOpen = true; @@ -130,7 +130,7 @@ public class DebugTab : Window, ITab _importPopup = importPopup; _framework = framework; _textureManager = textureManager; - _skinFixer = skinFixer; + _shaderReplacementFixer = shaderReplacementFixer; _redraws = redraws; _emotes = emotes; _diagnostics = diagnostics; @@ -702,20 +702,25 @@ public class DebugTab : Window, ITab if (!ImGui.CollapsingHeader("Character Utility")) return; - var enableSkinFixer = _skinFixer.Enabled; - if (ImGui.Checkbox("Enable Skin Fixer", ref enableSkinFixer)) - _skinFixer.Enabled = enableSkinFixer; + var enableShaderReplacementFixer = _shaderReplacementFixer.Enabled; + if (ImGui.Checkbox("Enable Shader Replacement Fixer", ref enableShaderReplacementFixer)) + _shaderReplacementFixer.Enabled = enableShaderReplacementFixer; - if (enableSkinFixer) + if (enableShaderReplacementFixer) { ImGui.SameLine(); ImGui.Dummy(ImGuiHelpers.ScaledVector2(20, 0)); + var slowPathCallDeltas = _shaderReplacementFixer.GetAndResetSlowPathCallDeltas(); ImGui.SameLine(); - ImGui.TextUnformatted($"\u0394 Slow-Path Calls: {_skinFixer.GetAndResetSlowPathCallDelta()}"); + ImGui.TextUnformatted($"\u0394 Slow-Path Calls for skin.shpk: {slowPathCallDeltas.Skin}"); + ImGui.SameLine(); + ImGui.TextUnformatted($"characterglass.shpk: {slowPathCallDeltas.CharacterGlass}"); ImGui.SameLine(); ImGui.Dummy(ImGuiHelpers.ScaledVector2(20, 0)); ImGui.SameLine(); - ImGui.TextUnformatted($"Materials with Modded skin.shpk: {_skinFixer.ModdedSkinShpkCount}"); + ImGui.TextUnformatted($"Materials with Modded skin.shpk: {_shaderReplacementFixer.ModdedSkinShpkCount}"); + ImGui.SameLine(); + ImGui.TextUnformatted($"characterglass.shpk: {_shaderReplacementFixer.ModdedCharacterGlassShpkCount}"); } using var table = Table("##CharacterUtility", 7, ImGuiTableFlags.RowBg | ImGuiTableFlags.SizingFixedFit, From 9ba6e4d0afb74c9603dc9353ea27244223dcbd0b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 9 Mar 2024 22:49:19 +0100 Subject: [PATCH 028/433] Update API. --- Penumbra.Api | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.Api b/Penumbra.Api index 79ffdd69..34921fd2 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit 79ffdd69a28141a1ac93daa24d76573b2fa0d71e +Subproject commit 34921fd2c5a9aff5d34aef664bdb78331e8b9436 From e08e9c4d1368765af5fa36a5537673cda645f3a5 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 16 Mar 2024 16:20:34 +0100 Subject: [PATCH 029/433] Add crash handler stuff. --- .../Buffers/AnimationInvocationBuffer.cs | 119 +++++++ .../Buffers/CharacterBaseBuffer.cs | 85 +++++ .../Buffers/MemoryMappedBuffer.cs | 215 +++++++++++++ .../Buffers/ModdedFileBuffer.cs | 99 ++++++ Penumbra.CrashHandler/CrashData.cs | 62 ++++ Penumbra.CrashHandler/GameEventLogReader.cs | 53 ++++ Penumbra.CrashHandler/GameEventLogWriter.cs | 17 + .../Penumbra.CrashHandler.csproj | 28 ++ Penumbra.CrashHandler/Program.cs | 30 ++ Penumbra.sln | 6 + .../Communication/CreatingCharacterBase.cs | 4 + Penumbra/Configuration.cs | 1 + Penumbra/EphemeralConfig.cs | 8 +- .../Animation/ApricotListenerSoundPlay.cs | 11 +- .../Animation/CharacterBaseLoadAnimation.cs | 16 +- .../Interop/Hooks/Animation/LoadAreaVfx.cs | 15 +- .../Hooks/Animation/LoadCharacterSound.cs | 16 +- .../Hooks/Animation/LoadCharacterVfx.cs | 14 +- .../Hooks/Animation/LoadTimelineResources.cs | 24 +- .../Hooks/Animation/ScheduleClipUpdate.cs | 32 +- .../Interop/Hooks/Animation/SomeActionLoad.cs | 26 +- .../Interop/Hooks/Animation/SomePapLoad.cs | 18 +- Penumbra/Penumbra.csproj | 9 +- Penumbra/Services/CrashHandlerService.cs | 296 ++++++++++++++++++ Penumbra/Services/FilenameService.cs | 6 + Penumbra/UI/Tabs/ChangedItemsTab.cs | 37 +-- Penumbra/UI/Tabs/ConfigTabBar.cs | 6 +- Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs | 104 ++++++ Penumbra/UI/Tabs/Debug/CrashHandlerPanel.cs | 136 ++++++++ Penumbra/UI/Tabs/Debug/DebugTab.cs | 23 +- Penumbra/UI/Tabs/EffectiveTab.cs | 17 +- Penumbra/UI/Tabs/ModsTab.cs | 85 +++-- Penumbra/UI/Tabs/OnScreenTab.cs | 2 +- Penumbra/UI/Tabs/ResourceTab.cs | 26 +- Penumbra/packages.lock.json | 63 +--- 35 files changed, 1472 insertions(+), 237 deletions(-) create mode 100644 Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs create mode 100644 Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs create mode 100644 Penumbra.CrashHandler/Buffers/MemoryMappedBuffer.cs create mode 100644 Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs create mode 100644 Penumbra.CrashHandler/CrashData.cs create mode 100644 Penumbra.CrashHandler/GameEventLogReader.cs create mode 100644 Penumbra.CrashHandler/GameEventLogWriter.cs create mode 100644 Penumbra.CrashHandler/Penumbra.CrashHandler.csproj create mode 100644 Penumbra.CrashHandler/Program.cs create mode 100644 Penumbra/Services/CrashHandlerService.cs create mode 100644 Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs create mode 100644 Penumbra/UI/Tabs/Debug/CrashHandlerPanel.cs diff --git a/Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs b/Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs new file mode 100644 index 00000000..dd966542 --- /dev/null +++ b/Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs @@ -0,0 +1,119 @@ +using System.Text.Json.Nodes; + +namespace Penumbra.CrashHandler.Buffers; + +/// The types of currently hooked and relevant animation loading functions. +public enum AnimationInvocationType : int +{ + PapLoad, + ActionLoad, + ScheduleClipUpdate, + LoadTimelineResources, + LoadCharacterVfx, + LoadCharacterSound, + ApricotSoundPlay, + LoadAreaVfx, + CharacterBaseLoadAnimation, +} + +/// The full crash entry for an invoked vfx function. +public record struct VfxFuncInvokedEntry( + double Age, + DateTimeOffset Timestamp, + int ThreadId, + string InvocationType, + string CharacterName, + string CharacterAddress, + string CollectionName) : ICrashDataEntry; + +/// Only expose the write interface for the buffer. +public interface IAnimationInvocationBufferWriter +{ + /// Write a line into the buffer with the given data. + /// The address of the related character, if known. + /// The name of the related character, anonymized or relying on index if unavailable, if known. + /// The name of the associated collection. Not anonymized. + /// The type of VFX func called. + public void WriteLine(nint characterAddress, ReadOnlySpan characterName, string collectionName, AnimationInvocationType type); +} + +internal sealed class AnimationInvocationBuffer : MemoryMappedBuffer, IAnimationInvocationBufferWriter, IBufferReader +{ + private const int _version = 1; + private const int _lineCount = 64; + private const int _lineCapacity = 256; + private const string _name = "Penumbra.AnimationInvocation"; + + public void WriteLine(nint characterAddress, ReadOnlySpan characterName, string collectionName, AnimationInvocationType type) + { + var accessor = GetCurrentLineLocking(); + lock (accessor) + { + accessor.Write(0, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()); + accessor.Write(8, Environment.CurrentManagedThreadId); + accessor.Write(12, (int)type); + accessor.Write(16, characterAddress); + var span = GetSpan(accessor, 24, 104); + WriteSpan(characterName, span); + span = GetSpan(accessor, 128); + WriteString(collectionName, span); + } + } + + public uint TotalCount + => TotalWrittenLines; + + public IEnumerable GetLines(DateTimeOffset crashTime) + { + var lineCount = (int)CurrentLineCount; + for (var i = lineCount - 1; i >= 0; --i) + { + var line = GetLine(i); + var timestamp = DateTimeOffset.FromUnixTimeMilliseconds(BitConverter.ToInt64(line)); + var thread = BitConverter.ToInt32(line[8..]); + var type = (AnimationInvocationType)BitConverter.ToInt32(line[12..]); + var address = BitConverter.ToUInt64(line[16..]); + var characterName = ReadString(line[24..]); + var collectionName = ReadString(line[128..]); + yield return new JsonObject() + { + [nameof(VfxFuncInvokedEntry.Age)] = (crashTime - timestamp).TotalSeconds, + [nameof(VfxFuncInvokedEntry.Timestamp)] = timestamp, + [nameof(VfxFuncInvokedEntry.ThreadId)] = thread, + [nameof(VfxFuncInvokedEntry.InvocationType)] = ToName(type), + [nameof(VfxFuncInvokedEntry.CharacterName)] = characterName, + [nameof(VfxFuncInvokedEntry.CharacterAddress)] = address.ToString("X"), + [nameof(VfxFuncInvokedEntry.CollectionName)] = collectionName, + }; + } + } + + public static IBufferReader CreateReader() + => new AnimationInvocationBuffer(false); + + public static IAnimationInvocationBufferWriter CreateWriter() + => new AnimationInvocationBuffer(); + + private AnimationInvocationBuffer(bool writer) + : base(_name, _version) + { } + + private AnimationInvocationBuffer() + : base(_name, _version, _lineCount, _lineCapacity) + { } + + private static string ToName(AnimationInvocationType type) + => type switch + { + AnimationInvocationType.PapLoad => "PAP Load", + AnimationInvocationType.ActionLoad => "Action Load", + AnimationInvocationType.ScheduleClipUpdate => "Schedule Clip Update", + AnimationInvocationType.LoadTimelineResources => "Load Timeline Resources", + AnimationInvocationType.LoadCharacterVfx => "Load Character VFX", + AnimationInvocationType.LoadCharacterSound => "Load Character Sound", + AnimationInvocationType.ApricotSoundPlay => "Apricot Sound Play", + AnimationInvocationType.LoadAreaVfx => "Load Area VFX", + AnimationInvocationType.CharacterBaseLoadAnimation => "Load Animation (CharacterBase)", + _ => $"Unknown ({(int)type})", + }; +} diff --git a/Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs b/Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs new file mode 100644 index 00000000..1fe5d7ba --- /dev/null +++ b/Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs @@ -0,0 +1,85 @@ +using System.Text.Json.Nodes; + +namespace Penumbra.CrashHandler.Buffers; + +/// Only expose the write interface for the buffer. +public interface ICharacterBaseBufferWriter +{ + /// Write a line into the buffer with the given data. + /// The address of the related character, if known. + /// The name of the related character, anonymized or relying on index if unavailable, if known. + /// The name of the associated collection. Not anonymized. + public void WriteLine(nint characterAddress, ReadOnlySpan characterName, string collectionName); +} + +/// The full crash entry for a loaded character base. +public record struct CharacterLoadedEntry( + double Age, + DateTimeOffset Timestamp, + int ThreadId, + string CharacterName, + string CharacterAddress, + string CollectionName) : ICrashDataEntry; + +internal sealed class CharacterBaseBuffer : MemoryMappedBuffer, ICharacterBaseBufferWriter, IBufferReader +{ + private const int _version = 1; + private const int _lineCount = 10; + private const int _lineCapacity = 256; + private const string _name = "Penumbra.CharacterBase"; + + public void WriteLine(nint characterAddress, ReadOnlySpan characterName, string collectionName) + { + var accessor = GetCurrentLineLocking(); + lock (accessor) + { + accessor.Write(0, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()); + accessor.Write(8, Environment.CurrentManagedThreadId); + accessor.Write(12, characterAddress); + var span = GetSpan(accessor, 20, 108); + WriteSpan(characterName, span); + span = GetSpan(accessor, 128); + WriteString(collectionName, span); + } + } + + public IEnumerable GetLines(DateTimeOffset crashTime) + { + var lineCount = (int)CurrentLineCount; + for (var i = lineCount - 1; i >= 0; --i) + { + var line = GetLine(i); + var timestamp = DateTimeOffset.FromUnixTimeMilliseconds(BitConverter.ToInt64(line)); + var thread = BitConverter.ToInt32(line[8..]); + var address = BitConverter.ToUInt64(line[12..]); + var characterName = ReadString(line[20..]); + var collectionName = ReadString(line[128..]); + yield return new JsonObject + { + [nameof(CharacterLoadedEntry.Age)] = (crashTime - timestamp).TotalSeconds, + [nameof(CharacterLoadedEntry.Timestamp)] = timestamp, + [nameof(CharacterLoadedEntry.ThreadId)] = thread, + [nameof(CharacterLoadedEntry.CharacterName)] = characterName, + [nameof(CharacterLoadedEntry.CharacterAddress)] = address.ToString("X"), + [nameof(CharacterLoadedEntry.CollectionName)] = collectionName, + }; + } + } + + public uint TotalCount + => TotalWrittenLines; + + public static IBufferReader CreateReader() + => new CharacterBaseBuffer(false); + + public static ICharacterBaseBufferWriter CreateWriter() + => new CharacterBaseBuffer(); + + private CharacterBaseBuffer(bool writer) + : base(_name, _version) + { } + + private CharacterBaseBuffer() + : base(_name, _version, _lineCount, _lineCapacity) + { } +} diff --git a/Penumbra.CrashHandler/Buffers/MemoryMappedBuffer.cs b/Penumbra.CrashHandler/Buffers/MemoryMappedBuffer.cs new file mode 100644 index 00000000..35055864 --- /dev/null +++ b/Penumbra.CrashHandler/Buffers/MemoryMappedBuffer.cs @@ -0,0 +1,215 @@ +using System.Diagnostics.CodeAnalysis; +using System.IO.MemoryMappedFiles; +using System.Numerics; +using System.Text; + +namespace Penumbra.CrashHandler.Buffers; + +public class MemoryMappedBuffer : IDisposable +{ + private const int MinHeaderLength = 4 + 4 + 4 + 4 + 4 + 4 + 4 + 4; + + private readonly MemoryMappedFile _file; + private readonly MemoryMappedViewAccessor _header; + private readonly MemoryMappedViewAccessor[] _lines; + + public readonly int Version; + public readonly uint LineCount; + public readonly uint LineCapacity; + private readonly uint _lineMask; + private bool _disposed; + + protected uint CurrentLineCount + { + get => _header.ReadUInt32(16); + set => _header.Write(16, value); + } + + protected uint CurrentLinePosition + { + get => _header.ReadUInt32(20); + set => _header.Write(20, value); + } + + public uint TotalWrittenLines + { + get => _header.ReadUInt32(24); + protected set => _header.Write(24, value); + } + + public MemoryMappedBuffer(string mapName, int version, uint lineCount, uint lineCapacity) + { + Version = version; + LineCount = BitOperations.RoundUpToPowerOf2(Math.Clamp(lineCount, 2, int.MaxValue >> 3)); + LineCapacity = BitOperations.RoundUpToPowerOf2(Math.Clamp(lineCapacity, 2, int.MaxValue >> 3)); + _lineMask = LineCount - 1; + var fileName = Encoding.UTF8.GetBytes(mapName); + var headerLength = (uint)(4 + 4 + 4 + 4 + 4 + 4 + 4 + fileName.Length + 1); + headerLength = (headerLength & 0b111) > 0 ? (headerLength & ~0b111u) + 0b1000 : headerLength; + var capacity = LineCount * LineCapacity + headerLength; + _file = MemoryMappedFile.CreateNew(mapName, capacity, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, + HandleInheritability.Inheritable); + _header = _file.CreateViewAccessor(0, headerLength); + _header.Write(0, headerLength); + _header.Write(4, Version); + _header.Write(8, LineCount); + _header.Write(12, LineCapacity); + _header.WriteArray(28, fileName, 0, fileName.Length); + _header.Write(fileName.Length + 28, (byte)0); + _lines = Enumerable.Range(0, (int)LineCount).Select(i + => _file.CreateViewAccessor(headerLength + i * LineCapacity, LineCapacity, MemoryMappedFileAccess.ReadWrite)) + .ToArray(); + } + + public MemoryMappedBuffer(string mapName, int? expectedVersion = null, uint? expectedMinLineCount = null, + uint? expectedMinLineCapacity = null) + { + _file = MemoryMappedFile.OpenExisting(mapName, MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable); + using var headerLine = _file.CreateViewAccessor(0, 4, MemoryMappedFileAccess.Read); + var headerLength = headerLine.ReadUInt32(0); + if (headerLength < MinHeaderLength) + Throw($"Map {mapName} did not contain a valid header."); + + _header = _file.CreateViewAccessor(0, headerLength, MemoryMappedFileAccess.ReadWrite); + Version = _header.ReadInt32(4); + LineCount = _header.ReadUInt32(8); + LineCapacity = _header.ReadUInt32(12); + _lineMask = LineCount - 1; + if (expectedVersion.HasValue && expectedVersion.Value != Version) + Throw($"Map {mapName} has version {Version} instead of {expectedVersion.Value}."); + + if (LineCount < expectedMinLineCount) + Throw($"Map {mapName} has line count {LineCount} but line count >= {expectedMinLineCount.Value} is required."); + + if (LineCapacity < expectedMinLineCapacity) + Throw($"Map {mapName} has line capacity {LineCapacity} but line capacity >= {expectedMinLineCapacity.Value} is required."); + + var name = ReadString(GetSpan(_header, 28)); + if (name != mapName) + Throw($"Map {mapName} does not contain its map name at the expected location."); + + _lines = Enumerable.Range(0, (int)LineCount).Select(i + => _file.CreateViewAccessor(headerLength + i * LineCapacity, LineCapacity, MemoryMappedFileAccess.ReadWrite)) + .ToArray(); + + [DoesNotReturn] + void Throw(string text) + { + _file.Dispose(); + _disposed = true; + throw new Exception(text); + } + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + _disposed = true; + } + + protected static string ReadString(Span span) + { + if (span.IsEmpty) + throw new Exception("String from empty span requested."); + + var termination = span.IndexOf((byte)0); + if (termination < 0) + throw new Exception("String in span is not terminated."); + + return Encoding.UTF8.GetString(span[..termination]); + } + + protected static int WriteString(string text, Span span) + { + var bytes = Encoding.UTF8.GetBytes(text); + var length = bytes.Length + 1; + if (length > span.Length) + throw new Exception($"String {text} is too long to write into span."); + + bytes.CopyTo(span); + span[bytes.Length] = 0; + return length; + } + + protected static int WriteSpan(ReadOnlySpan input, Span span) + { + var length = input.Length + 1; + if (length > span.Length) + throw new Exception("Byte array is too long to write into span."); + + input.CopyTo(span); + span[input.Length] = 0; + return length; + } + + protected Span GetLine(int i) + { + if (i < 0 || i > LineCount) + return null; + + lock (_header) + { + var lineIdx = CurrentLinePosition + i & _lineMask; + if (lineIdx > CurrentLineCount) + return null; + + return GetSpan(_lines[lineIdx]); + } + } + + + protected MemoryMappedViewAccessor GetCurrentLineLocking() + { + MemoryMappedViewAccessor view; + lock (_header) + { + var currentLineCount = CurrentLineCount; + if (currentLineCount == LineCount) + { + var currentLinePos = CurrentLinePosition; + view = _lines[currentLinePos]!; + CurrentLinePosition = currentLinePos + 1 & _lineMask; + } + else + { + view = _lines[currentLineCount]; + ++CurrentLineCount; + } + + ++TotalWrittenLines; + _header.Flush(); + } + + return view; + } + + protected static Span GetSpan(MemoryMappedViewAccessor accessor, int offset = 0) + => GetSpan(accessor, offset, (int)accessor.Capacity - offset); + + protected static unsafe Span GetSpan(MemoryMappedViewAccessor accessor, int offset, int size) + { + byte* ptr = null; + accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr); + size = Math.Min(size, (int)accessor.Capacity - offset); + if (size < 0) + return []; + + var span = new Span(ptr + offset + accessor.PointerOffset, size); + return span; + } + + protected void Dispose(bool disposing) + { + if (_disposed) + return; + + _header.Dispose(); + foreach (var line in _lines) + line.Dispose(); + _file.Dispose(); + } + + ~MemoryMappedBuffer() + => Dispose(false); +} diff --git a/Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs b/Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs new file mode 100644 index 00000000..b472d413 --- /dev/null +++ b/Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs @@ -0,0 +1,99 @@ +using System.Text.Json.Nodes; + +namespace Penumbra.CrashHandler.Buffers; + +/// Only expose the write interface for the buffer. +public interface IModdedFileBufferWriter +{ + /// Write a line into the buffer with the given data. + /// The address of the related character, if known. + /// The name of the related character, anonymized or relying on index if unavailable, if known. + /// The name of the associated collection. Not anonymized. + /// The file name as requested by the game. + /// The actual modded file name loaded. + public void WriteLine(nint characterAddress, ReadOnlySpan characterName, string collectionName, ReadOnlySpan requestedFileName, + ReadOnlySpan actualFileName); +} + +/// The full crash entry for a loaded modded file. +public record struct ModdedFileLoadedEntry( + double Age, + DateTimeOffset Timestamp, + int ThreadId, + string CharacterName, + string CharacterAddress, + string CollectionName, + string RequestedFileName, + string ActualFileName) : ICrashDataEntry; + +internal sealed class ModdedFileBuffer : MemoryMappedBuffer, IModdedFileBufferWriter, IBufferReader +{ + private const int _version = 1; + private const int _lineCount = 128; + private const int _lineCapacity = 1024; + private const string _name = "Penumbra.ModdedFile"; + + public void WriteLine(nint characterAddress, ReadOnlySpan characterName, string collectionName, ReadOnlySpan requestedFileName, + ReadOnlySpan actualFileName) + { + var accessor = GetCurrentLineLocking(); + lock (accessor) + { + accessor.Write(0, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()); + accessor.Write(8, Environment.CurrentManagedThreadId); + accessor.Write(12, characterAddress); + var span = GetSpan(accessor, 20, 80); + WriteSpan(characterName, span); + span = GetSpan(accessor, 92, 80); + WriteString(collectionName, span); + span = GetSpan(accessor, 172, 260); + WriteSpan(requestedFileName, span); + span = GetSpan(accessor, 432); + WriteSpan(actualFileName, span); + } + } + + public uint TotalCount + => TotalWrittenLines; + + public IEnumerable GetLines(DateTimeOffset crashTime) + { + var lineCount = (int)CurrentLineCount; + for (var i = lineCount - 1; i >= 0; --i) + { + var line = GetLine(i); + var timestamp = DateTimeOffset.FromUnixTimeMilliseconds(BitConverter.ToInt64(line)); + var thread = BitConverter.ToInt32(line[8..]); + var address = BitConverter.ToUInt64(line[12..]); + var characterName = ReadString(line[20..]); + var collectionName = ReadString(line[92..]); + var requestedFileName = ReadString(line[172..]); + var actualFileName = ReadString(line[432..]); + yield return new JsonObject() + { + [nameof(ModdedFileLoadedEntry.Age)] = (crashTime - timestamp).TotalSeconds, + [nameof(ModdedFileLoadedEntry.Timestamp)] = timestamp, + [nameof(ModdedFileLoadedEntry.ThreadId)] = thread, + [nameof(ModdedFileLoadedEntry.CharacterName)] = characterName, + [nameof(ModdedFileLoadedEntry.CharacterAddress)] = address.ToString("X"), + [nameof(ModdedFileLoadedEntry.CollectionName)] = collectionName, + [nameof(ModdedFileLoadedEntry.RequestedFileName)] = requestedFileName, + [nameof(ModdedFileLoadedEntry.ActualFileName)] = actualFileName, + }; + } + } + + public static IBufferReader CreateReader() + => new ModdedFileBuffer(false); + + public static IModdedFileBufferWriter CreateWriter() + => new ModdedFileBuffer(); + + private ModdedFileBuffer(bool writer) + : base(_name, _version) + { } + + private ModdedFileBuffer() + : base(_name, _version, _lineCount, _lineCapacity) + { } +} diff --git a/Penumbra.CrashHandler/CrashData.cs b/Penumbra.CrashHandler/CrashData.cs new file mode 100644 index 00000000..956a3db7 --- /dev/null +++ b/Penumbra.CrashHandler/CrashData.cs @@ -0,0 +1,62 @@ +using Penumbra.CrashHandler.Buffers; + +namespace Penumbra.CrashHandler; + +/// A base entry for crash data. +public interface ICrashDataEntry +{ + /// The timestamp of the event. + DateTimeOffset Timestamp { get; } + + /// The thread invoking the event. + int ThreadId { get; } + + /// The age of the event compared to the crash. (Redundantly with the timestamp) + double Age { get; } +} + +/// A full set of crash data. +public class CrashData +{ + /// The mode this data was obtained - manually or from a crash. + public string Mode { get; set; } = "Unknown"; + + /// The time this crash data was generated. + public DateTimeOffset CrashTime { get; set; } = DateTimeOffset.UnixEpoch; + + /// The FFXIV process ID when this data was generated. + public int ProcessId { get; set; } = 0; + + /// The FFXIV Exit Code (if any) when this data was generated. + public int ExitCode { get; set; } = 0; + + /// The total amount of characters loaded during this session. + public int TotalCharactersLoaded { get; set; } = 0; + + /// The total amount of modded files loaded during this session. + public int TotalModdedFilesLoaded { get; set; } = 0; + + /// The total amount of vfx functions invoked during this session. + public int TotalVFXFuncsInvoked { get; set; } = 0; + + /// The last character loaded before this crash data was generated. + public CharacterLoadedEntry? LastCharacterLoaded + => LastCharactersLoaded.Count == 0 ? default : LastCharactersLoaded[0]; + + /// The last modded file loaded before this crash data was generated. + public ModdedFileLoadedEntry? LastModdedFileLoaded + => LastModdedFilesLoaded.Count == 0 ? default : LastModdedFilesLoaded[0]; + + /// The last vfx function invoked before this crash data was generated. + public VfxFuncInvokedEntry? LastVfxFuncInvoked + => LastVfxFuncsInvoked.Count == 0 ? default : LastVfxFuncsInvoked[0]; + + /// A collection of the last few characters loaded before this crash data was generated. + public List LastCharactersLoaded { get; } = []; + + /// A collection of the last few modded files loaded before this crash data was generated. + public List LastModdedFilesLoaded { get; } = []; + + /// A collection of the last few vfx functions invoked before this crash data was generated. + public List LastVfxFuncsInvoked { get; } = []; +} diff --git a/Penumbra.CrashHandler/GameEventLogReader.cs b/Penumbra.CrashHandler/GameEventLogReader.cs new file mode 100644 index 00000000..283be526 --- /dev/null +++ b/Penumbra.CrashHandler/GameEventLogReader.cs @@ -0,0 +1,53 @@ +using System.Text.Json.Nodes; +using Penumbra.CrashHandler.Buffers; + +namespace Penumbra.CrashHandler; + +public interface IBufferReader +{ + public uint TotalCount { get; } + public IEnumerable GetLines(DateTimeOffset crashTime); +} + +public sealed class GameEventLogReader : IDisposable +{ + public readonly (IBufferReader Reader, string TypeSingular, string TypePlural)[] Readers = + [ + (CharacterBaseBuffer.CreateReader(), "CharacterLoaded", "CharactersLoaded"), + (ModdedFileBuffer.CreateReader(), "ModdedFileLoaded", "ModdedFilesLoaded"), + (AnimationInvocationBuffer.CreateReader(), "VFXFuncInvoked", "VFXFuncsInvoked"), + ]; + + public void Dispose() + { + foreach (var (reader, _, _) in Readers) + (reader as IDisposable)?.Dispose(); + } + + + public JsonObject Dump(string mode, int processId, int exitCode) + { + var crashTime = DateTimeOffset.UtcNow; + var obj = new JsonObject + { + [nameof(CrashData.Mode)] = mode, + [nameof(CrashData.CrashTime)] = DateTimeOffset.UtcNow, + [nameof(CrashData.ProcessId)] = processId, + [nameof(CrashData.ExitCode)] = exitCode, + }; + + foreach (var (reader, singular, _) in Readers) + obj["Last" + singular] = reader.GetLines(crashTime).FirstOrDefault(); + + foreach (var (reader, _, plural) in Readers) + { + obj["Total" + plural] = reader.TotalCount; + var array = new JsonArray(); + foreach (var file in reader.GetLines(crashTime)) + array.Add(file); + obj["Last" + plural] = array; + } + + return obj; + } +} diff --git a/Penumbra.CrashHandler/GameEventLogWriter.cs b/Penumbra.CrashHandler/GameEventLogWriter.cs new file mode 100644 index 00000000..8e809cec --- /dev/null +++ b/Penumbra.CrashHandler/GameEventLogWriter.cs @@ -0,0 +1,17 @@ +using Penumbra.CrashHandler.Buffers; + +namespace Penumbra.CrashHandler; + +public sealed class GameEventLogWriter : IDisposable +{ + public readonly ICharacterBaseBufferWriter CharacterBase = CharacterBaseBuffer.CreateWriter(); + public readonly IModdedFileBufferWriter FileLoaded = ModdedFileBuffer.CreateWriter(); + public readonly IAnimationInvocationBufferWriter AnimationFuncInvoked = AnimationInvocationBuffer.CreateWriter(); + + public void Dispose() + { + (CharacterBase as IDisposable)?.Dispose(); + (FileLoaded as IDisposable)?.Dispose(); + (AnimationFuncInvoked as IDisposable)?.Dispose(); + } +} diff --git a/Penumbra.CrashHandler/Penumbra.CrashHandler.csproj b/Penumbra.CrashHandler/Penumbra.CrashHandler.csproj new file mode 100644 index 00000000..ea61b968 --- /dev/null +++ b/Penumbra.CrashHandler/Penumbra.CrashHandler.csproj @@ -0,0 +1,28 @@ + + + + Exe + net7.0-windows + preview + enable + x64 + enable + true + false + + + + $(appdata)\XIVLauncher\addon\Hooks\dev\ + $(HOME)/.xlcore/dalamud/Hooks/dev/ + $(DALAMUD_HOME)/ + + + + embedded + + + + embedded + + + diff --git a/Penumbra.CrashHandler/Program.cs b/Penumbra.CrashHandler/Program.cs new file mode 100644 index 00000000..e4a46348 --- /dev/null +++ b/Penumbra.CrashHandler/Program.cs @@ -0,0 +1,30 @@ +using System.Diagnostics; +using System.Text.Json; + +namespace Penumbra.CrashHandler; + +public class CrashHandler +{ + public static void Main(string[] args) + { + if (args.Length < 2 || !int.TryParse(args[1], out var pid)) + return; + + try + { + using var reader = new GameEventLogReader(); + var parent = Process.GetProcessById(pid); + + parent.WaitForExit(); + var exitCode = parent.ExitCode; + var obj = reader.Dump("Crash", pid, exitCode); + using var fs = File.Open(args[0], FileMode.Create); + using var w = new Utf8JsonWriter(fs, new JsonWriterOptions { Indented = true }); + obj.WriteTo(w, new JsonSerializerOptions() { WriteIndented = true }); + } + catch (Exception ex) + { + File.WriteAllText(args[0], $"{DateTime.UtcNow} {pid} {ex}"); + } + } +} diff --git a/Penumbra.sln b/Penumbra.sln index 5c11aaea..78fa1543 100644 --- a/Penumbra.sln +++ b/Penumbra.sln @@ -18,6 +18,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Penumbra.Api", "Penumbra.Ap EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Penumbra.String", "Penumbra.String\Penumbra.String.csproj", "{5549BAFD-6357-4B1A-800C-75AC36E5B76D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Penumbra.CrashHandler", "Penumbra.CrashHandler\Penumbra.CrashHandler.csproj", "{EE834491-A98F-4395-BE0D-6861AE5AD953}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -44,6 +46,10 @@ Global {5549BAFD-6357-4B1A-800C-75AC36E5B76D}.Debug|Any CPU.Build.0 = Debug|Any CPU {5549BAFD-6357-4B1A-800C-75AC36E5B76D}.Release|Any CPU.ActiveCfg = Release|Any CPU {5549BAFD-6357-4B1A-800C-75AC36E5B76D}.Release|Any CPU.Build.0 = Release|Any CPU + {EE834491-A98F-4395-BE0D-6861AE5AD953}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EE834491-A98F-4395-BE0D-6861AE5AD953}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EE834491-A98F-4395-BE0D-6861AE5AD953}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EE834491-A98F-4395-BE0D-6861AE5AD953}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Penumbra/Communication/CreatingCharacterBase.cs b/Penumbra/Communication/CreatingCharacterBase.cs index 1e232761..2f249c14 100644 --- a/Penumbra/Communication/CreatingCharacterBase.cs +++ b/Penumbra/Communication/CreatingCharacterBase.cs @@ -1,5 +1,6 @@ using OtterGui.Classes; using Penumbra.Api; +using Penumbra.Services; namespace Penumbra.Communication; @@ -19,5 +20,8 @@ public sealed class CreatingCharacterBase() { /// Api = 0, + + /// + CrashHandler = 0, } } diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index 188be65d..9c0b4f2d 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -33,6 +33,7 @@ public class Configuration : IPluginConfiguration, ISavable public string ModDirectory { get; set; } = string.Empty; public string ExportDirectory { get; set; } = string.Empty; + public bool UseCrashHandler { get; set; } = true; public bool OpenWindowAtStart { get; set; } = false; public bool HideUiInGPose { get; set; } = false; public bool HideUiInCutscenes { get; set; } = true; diff --git a/Penumbra/EphemeralConfig.cs b/Penumbra/EphemeralConfig.cs index 98b1a5d6..0a542d04 100644 --- a/Penumbra/EphemeralConfig.cs +++ b/Penumbra/EphemeralConfig.cs @@ -47,7 +47,7 @@ public class EphemeralConfig : ISavable, IDisposable /// public EphemeralConfig(SaveService saveService, ModPathChanged modPathChanged) { - _saveService = saveService; + _saveService = saveService; _modPathChanged = modPathChanged; Load(); _modPathChanged.Subscribe(OnModPathChanged, ModPathChanged.Priority.EphemeralConfig); @@ -94,13 +94,13 @@ public class EphemeralConfig : ISavable, IDisposable public void Save(StreamWriter writer) { - using var jWriter = new JsonTextWriter(writer); + using var jWriter = new JsonTextWriter(writer); jWriter.Formatting = Formatting.Indented; - var serializer = new JsonSerializer { Formatting = Formatting.Indented }; + var serializer = new JsonSerializer { Formatting = Formatting.Indented }; serializer.Serialize(jWriter, this); } - /// Overwrite the last saved mod path if it changes. + /// Overwrite the last saved mod path if it changes. private void OnModPathChanged(ModPathChangeType type, Mod mod, DirectoryInfo? old, DirectoryInfo? _) { if (type is not ModPathChangeType.Moved || !string.Equals(old?.Name, LastModPath, StringComparison.OrdinalIgnoreCase)) diff --git a/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs b/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs index b91c5375..77927593 100644 --- a/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs +++ b/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs @@ -2,21 +2,25 @@ using FFXIVClientStructs.FFXIV.Client.Game.Object; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; using Penumbra.Collections; +using Penumbra.CrashHandler.Buffers; using Penumbra.GameData; using Penumbra.Interop.PathResolving; +using Penumbra.Services; namespace Penumbra.Interop.Hooks.Animation; /// Called for some sound effects caused by animations or VFX. public sealed unsafe class ApricotListenerSoundPlay : FastHook { - private readonly GameState _state; - private readonly CollectionResolver _collectionResolver; + private readonly GameState _state; + private readonly CollectionResolver _collectionResolver; + private readonly CrashHandlerService _crashHandler; - public ApricotListenerSoundPlay(HookManager hooks, GameState state, CollectionResolver collectionResolver) + public ApricotListenerSoundPlay(HookManager hooks, GameState state, CollectionResolver collectionResolver, CrashHandlerService crashHandler) { _state = state; _collectionResolver = collectionResolver; + _crashHandler = crashHandler; Task = hooks.CreateHook("Apricot Listener Sound Play", Sigs.ApricotListenerSoundPlay, Detour, true); } @@ -46,6 +50,7 @@ public sealed unsafe class ApricotListenerSoundPlay : FastHook public sealed unsafe class CharacterBaseLoadAnimation : FastHook { - private readonly GameState _state; - private readonly CollectionResolver _collectionResolver; - private readonly DrawObjectState _drawObjectState; + private readonly GameState _state; + private readonly CollectionResolver _collectionResolver; + private readonly DrawObjectState _drawObjectState; + private readonly CrashHandlerService _crashHandler; public CharacterBaseLoadAnimation(HookManager hooks, GameState state, CollectionResolver collectionResolver, - DrawObjectState drawObjectState) + DrawObjectState drawObjectState, CrashHandlerService crashHandler) { _state = state; _collectionResolver = collectionResolver; _drawObjectState = drawObjectState; + _crashHandler = crashHandler; Task = hooks.CreateHook("CharacterBase Load Animation", Sigs.CharacterBaseLoadAnimation, Detour, true); } @@ -33,7 +37,9 @@ public sealed unsafe class CharacterBaseLoadAnimation : FastHook Load a ground-based area VFX. public sealed unsafe class LoadAreaVfx : FastHook { - private readonly GameState _state; - private readonly CollectionResolver _collectionResolver; + private readonly GameState _state; + private readonly CollectionResolver _collectionResolver; + private readonly CrashHandlerService _crashHandler; - public LoadAreaVfx(HookManager hooks, GameState state, CollectionResolver collectionResolver) + public LoadAreaVfx(HookManager hooks, GameState state, CollectionResolver collectionResolver, CrashHandlerService crashHandler) { _state = state; _collectionResolver = collectionResolver; + _crashHandler = crashHandler; Task = hooks.CreateHook("Load Area VFX", Sigs.LoadAreaVfx, Detour, true); } @@ -25,10 +29,11 @@ public sealed unsafe class LoadAreaVfx : FastHook private nint Detour(uint vfxId, float* pos, GameObject* caster, float unk1, float unk2, byte unk3) { var newData = caster != null - ? _collectionResolver.IdentifyCollection(caster, true) - : ResolveData.Invalid; + ? _collectionResolver.IdentifyCollection(caster, true) + : ResolveData.Invalid; var last = _state.SetAnimationData(newData); + _crashHandler.LogAnimation(newData.AssociatedGameObject, newData.ModCollection, AnimationInvocationType.LoadAreaVfx); var ret = Task.Result.Original(vfxId, pos, caster, unk1, unk2, unk3); Penumbra.Log.Excessive( $"[Load Area VFX] Invoked with {vfxId}, [{pos[0]} {pos[1]} {pos[2]}], 0x{(nint)caster:X}, {unk1}, {unk2}, {unk3} -> 0x{ret:X}."); diff --git a/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs b/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs index af13805d..98454a77 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs @@ -1,19 +1,23 @@ using FFXIVClientStructs.FFXIV.Client.Game.Object; using OtterGui.Services; +using Penumbra.CrashHandler.Buffers; using Penumbra.Interop.PathResolving; +using Penumbra.Services; namespace Penumbra.Interop.Hooks.Animation; /// Characters load some of their voice lines or whatever with this function. public sealed unsafe class LoadCharacterSound : FastHook { - private readonly GameState _state; - private readonly CollectionResolver _collectionResolver; + private readonly GameState _state; + private readonly CollectionResolver _collectionResolver; + private readonly CrashHandlerService _crashHandler; - public LoadCharacterSound(HookManager hooks, GameState state, CollectionResolver collectionResolver) + public LoadCharacterSound(HookManager hooks, GameState state, CollectionResolver collectionResolver, CrashHandlerService crashHandler) { - _state = state; + _state = state; _collectionResolver = collectionResolver; + _crashHandler = crashHandler; Task = hooks.CreateHook("Load Character Sound", (nint)FFXIVClientStructs.FFXIV.Client.Game.Character.Character.VfxContainer.MemberFunctionPointers.LoadCharacterSound, Detour, true); @@ -25,7 +29,9 @@ public sealed unsafe class LoadCharacterSound : FastHook {ret}."); _state.RestoreSoundData(last); diff --git a/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs b/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs index 240c062e..69c22773 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs @@ -2,9 +2,11 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Object; using OtterGui.Services; using Penumbra.Collections; +using Penumbra.CrashHandler.Buffers; using Penumbra.GameData; using Penumbra.Interop.PathResolving; using Penumbra.Interop.Structs; +using Penumbra.Services; using Penumbra.String; namespace Penumbra.Interop.Hooks.Animation; @@ -12,15 +14,18 @@ namespace Penumbra.Interop.Hooks.Animation; /// Load a VFX specifically for a character. public sealed unsafe class LoadCharacterVfx : FastHook { - private readonly GameState _state; - private readonly CollectionResolver _collectionResolver; - private readonly IObjectTable _objects; + private readonly GameState _state; + private readonly CollectionResolver _collectionResolver; + private readonly IObjectTable _objects; + private readonly CrashHandlerService _crashHandler; - public LoadCharacterVfx(HookManager hooks, GameState state, CollectionResolver collectionResolver, IObjectTable objects) + public LoadCharacterVfx(HookManager hooks, GameState state, CollectionResolver collectionResolver, IObjectTable objects, + CrashHandlerService crashHandler) { _state = state; _collectionResolver = collectionResolver; _objects = objects; + _crashHandler = crashHandler; Task = hooks.CreateHook("Load Character VFX", Sigs.LoadCharacterVfx, Detour, true); } @@ -45,6 +50,7 @@ public sealed unsafe class LoadCharacterVfx : FastHookGameObjectId:X}, {vfxParams->TargetCount}, {unk1}, {unk2}, {unk3}, {unk4} -> 0x{ret:X}."); diff --git a/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs b/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs index 2ca8ffe7..ade957b9 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs @@ -3,8 +3,10 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Object; using OtterGui.Services; using Penumbra.Collections; +using Penumbra.CrashHandler; using Penumbra.GameData; using Penumbra.Interop.PathResolving; +using Penumbra.Services; namespace Penumbra.Interop.Hooks.Animation; @@ -14,19 +16,21 @@ namespace Penumbra.Interop.Hooks.Animation; /// public sealed unsafe class LoadTimelineResources : FastHook { - private readonly GameState _state; - private readonly CollectionResolver _collectionResolver; - private readonly ICondition _conditions; - private readonly IObjectTable _objects; + private readonly GameState _state; + private readonly CollectionResolver _collectionResolver; + private readonly ICondition _conditions; + private readonly IObjectTable _objects; + private readonly CrashHandlerService _crashHandler; public LoadTimelineResources(HookManager hooks, GameState state, CollectionResolver collectionResolver, ICondition conditions, - IObjectTable objects) + IObjectTable objects, CrashHandlerService crashHandler) { _state = state; _collectionResolver = collectionResolver; _conditions = conditions; _objects = objects; - Task = hooks.CreateHook("Load Timeline Resources", Sigs.LoadTimelineResources, Detour, true); + _crashHandler = crashHandler; + Task = hooks.CreateHook("Load Timeline Resources", Sigs.LoadTimelineResources, Detour, true); } public delegate ulong Delegate(nint timeline); @@ -39,7 +43,13 @@ public sealed unsafe class LoadTimelineResources : FastHook Called when some action timelines update. public sealed unsafe class ScheduleClipUpdate : FastHook { - private readonly GameState _state; - private readonly CollectionResolver _collectionResolver; - private readonly IObjectTable _objects; + private readonly GameState _state; + private readonly CollectionResolver _collectionResolver; + private readonly IObjectTable _objects; + private readonly CrashHandlerService _crashHandler; - public ScheduleClipUpdate(HookManager hooks, GameState state, CollectionResolver collectionResolver, IObjectTable objects) + public ScheduleClipUpdate(HookManager hooks, GameState state, CollectionResolver collectionResolver, IObjectTable objects, + CrashHandlerService crashHandler) { _state = state; _collectionResolver = collectionResolver; _objects = objects; + _crashHandler = crashHandler; Task = hooks.CreateHook("Schedule Clip Update", Sigs.ScheduleClipUpdate, Detour, true); } @@ -27,8 +32,9 @@ public sealed unsafe class ScheduleClipUpdate : FastHookSchedulerTimeline)); + var newData = LoadTimelineResources.GetDataFromTimeline(_objects, _collectionResolver, clipScheduler->SchedulerTimeline); + var last = _state.SetAnimationData(newData); + _crashHandler.LogAnimation(newData.AssociatedGameObject, newData.ModCollection, AnimationInvocationType.ScheduleClipUpdate); Task.Result.Original(clipScheduler); _state.RestoreAnimationData(last); } diff --git a/Penumbra/Interop/Hooks/Animation/SomeActionLoad.cs b/Penumbra/Interop/Hooks/Animation/SomeActionLoad.cs index 48931d73..6de3aeb0 100644 --- a/Penumbra/Interop/Hooks/Animation/SomeActionLoad.cs +++ b/Penumbra/Interop/Hooks/Animation/SomeActionLoad.cs @@ -1,21 +1,25 @@ -using FFXIVClientStructs.FFXIV.Client.Game; -using FFXIVClientStructs.FFXIV.Client.Game.Object; -using OtterGui.Services; -using Penumbra.GameData; +using FFXIVClientStructs.FFXIV.Client.Game; +using FFXIVClientStructs.FFXIV.Client.Game.Object; +using OtterGui.Services; +using Penumbra.CrashHandler.Buffers; +using Penumbra.GameData; using Penumbra.Interop.PathResolving; +using Penumbra.Services; + +namespace Penumbra.Interop.Hooks.Animation; -namespace Penumbra.Interop.Hooks.Animation; - /// Seems to load character actions when zoning or changing class, maybe. public sealed unsafe class SomeActionLoad : FastHook { - private readonly GameState _state; - private readonly CollectionResolver _collectionResolver; + private readonly GameState _state; + private readonly CollectionResolver _collectionResolver; + private readonly CrashHandlerService _crashHandler; - public SomeActionLoad(HookManager hooks, GameState state, CollectionResolver collectionResolver) + public SomeActionLoad(HookManager hooks, GameState state, CollectionResolver collectionResolver, CrashHandlerService crashHandler) { _state = state; _collectionResolver = collectionResolver; + _crashHandler = crashHandler; Task = hooks.CreateHook("Some Action Load", Sigs.LoadSomeAction, Detour, true); } @@ -24,8 +28,10 @@ public sealed unsafe class SomeActionLoad : FastHook [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private void Detour(ActionTimelineManager* timelineManager) { - var last = _state.SetAnimationData(_collectionResolver.IdentifyCollection((GameObject*)timelineManager->Parent, true)); + var newData = _collectionResolver.IdentifyCollection((GameObject*)timelineManager->Parent, true); + var last = _state.SetAnimationData(newData); Penumbra.Log.Excessive($"[Some Action Load] Invoked on 0x{(nint)timelineManager:X}."); + _crashHandler.LogAnimation(newData.AssociatedGameObject, newData.ModCollection, AnimationInvocationType.ActionLoad); Task.Result.Original(timelineManager); _state.RestoreAnimationData(last); } diff --git a/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs b/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs index 75caacee..3e60e62f 100644 --- a/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs +++ b/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs @@ -1,23 +1,28 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Object; using OtterGui.Services; +using Penumbra.CrashHandler.Buffers; using Penumbra.GameData; using Penumbra.Interop.PathResolving; +using Penumbra.Services; namespace Penumbra.Interop.Hooks.Animation; /// Unknown what exactly this is, but it seems to load a bunch of paps. public sealed unsafe class SomePapLoad : FastHook { - private readonly GameState _state; - private readonly CollectionResolver _collectionResolver; - private readonly IObjectTable _objects; + private readonly GameState _state; + private readonly CollectionResolver _collectionResolver; + private readonly IObjectTable _objects; + private readonly CrashHandlerService _crashHandler; - public SomePapLoad(HookManager hooks, GameState state, CollectionResolver collectionResolver, IObjectTable objects) + public SomePapLoad(HookManager hooks, GameState state, CollectionResolver collectionResolver, IObjectTable objects, + CrashHandlerService crashHandler) { _state = state; _collectionResolver = collectionResolver; _objects = objects; + _crashHandler = crashHandler; Task = hooks.CreateHook("Some PAP Load", Sigs.LoadSomePap, Detour, true); } @@ -33,8 +38,9 @@ public sealed unsafe class SomePapLoad : FastHook var actorIdx = (int)(*(*(ulong**)timelinePtr + 1) >> 3); if (actorIdx >= 0 && actorIdx < _objects.Length) { - var last = _state.SetAnimationData(_collectionResolver.IdentifyCollection((GameObject*)_objects.GetObjectAddress(actorIdx), - true)); + var newData = _collectionResolver.IdentifyCollection((GameObject*)_objects.GetObjectAddress(actorIdx), true); + var last = _state.SetAnimationData(newData); + _crashHandler.LogAnimation(newData.AssociatedGameObject, newData.ModCollection, AnimationInvocationType.PapLoad); Task.Result.Original(a1, a2, a3, a4); _state.RestoreAnimationData(last); return; diff --git a/Penumbra/Penumbra.csproj b/Penumbra/Penumbra.csproj index 01b3d680..796eae01 100644 --- a/Penumbra/Penumbra.csproj +++ b/Penumbra/Penumbra.csproj @@ -1,4 +1,4 @@ - + net7.0-windows preview @@ -69,8 +69,7 @@ - - + @@ -79,8 +78,10 @@ + + @@ -103,4 +104,4 @@ $(GitCommitHash) - \ No newline at end of file + diff --git a/Penumbra/Services/CrashHandlerService.cs b/Penumbra/Services/CrashHandlerService.cs new file mode 100644 index 00000000..4e2bce0f --- /dev/null +++ b/Penumbra/Services/CrashHandlerService.cs @@ -0,0 +1,296 @@ +using System.Text.Json; +using System.Text.Json.Nodes; +using FFXIVClientStructs.FFXIV.Client.Game.Object; +using OtterGui.Services; +using Penumbra.Collections; +using Penumbra.Communication; +using Penumbra.CrashHandler; +using Penumbra.CrashHandler.Buffers; +using Penumbra.GameData.Actors; +using Penumbra.Interop.ResourceLoading; +using Penumbra.Interop.Structs; +using Penumbra.String; +using Penumbra.String.Classes; +using FileMode = System.IO.FileMode; + +namespace Penumbra.Services; + +public sealed class CrashHandlerService : IDisposable, IService +{ + private readonly FilenameService _files; + private readonly CommunicatorService _communicator; + private readonly ActorManager _actors; + private readonly ResourceLoader _resourceLoader; + private readonly Configuration _config; + + public CrashHandlerService(FilenameService files, CommunicatorService communicator, ActorManager actors, ResourceLoader resourceLoader, + Configuration config) + { + _files = files; + _communicator = communicator; + _actors = actors; + _resourceLoader = resourceLoader; + _config = config; + + if (!_config.UseCrashHandler) + return; + + OpenEventWriter(); + LaunchCrashHandler(); + if (_eventWriter != null) + Subscribe(); + } + + public void Dispose() + { + CloseEventWriter(); + _eventWriter?.Dispose(); + if (_child != null) + { + _child.Kill(); + Penumbra.Log.Debug($"Killed crash handler child process {_child.Id}."); + } + + Unsubscribe(); + } + + private Process? _child; + private GameEventLogWriter? _eventWriter; + + public string CopiedExe = string.Empty; + + public string OriginalExe + => _files.CrashHandlerExe; + + public string LogPath + => _files.LogFileName; + + public int ChildProcessId + => _child?.Id ?? -1; + + public int ProcessId + => Environment.ProcessId; + + public bool IsRunning + => _eventWriter != null && _child is { HasExited: false }; + + public int ChildExitCode + => IsRunning ? 0 : _child?.ExitCode ?? 0; + + public void Enable() + { + if (_config.UseCrashHandler) + return; + + _config.UseCrashHandler = true; + _config.Save(); + OpenEventWriter(); + LaunchCrashHandler(); + if (_eventWriter != null) + Subscribe(); + } + + public void Disable() + { + if (!_config.UseCrashHandler) + return; + + _config.UseCrashHandler = false; + _config.Save(); + CloseEventWriter(); + CloseCrashHandler(); + Unsubscribe(); + } + + public JsonObject? Load(string fileName) + { + if (!File.Exists(fileName)) + return null; + + try + { + var data = File.ReadAllText(fileName); + return JsonNode.Parse(data) as JsonObject; + } + catch (Exception ex) + { + Penumbra.Log.Error($"Could not parse crash dump at {fileName}:\n{ex}"); + return null; + } + } + + public void CloseCrashHandler() + { + if (_child == null) + return; + + try + { + if (_child.HasExited) + return; + + _child.Kill(); + Penumbra.Log.Debug($"Closed Crash Handler at {CopiedExe}."); + } + catch (Exception ex) + { + _child = null; + Penumbra.Log.Debug($"Closed not close Crash Handler at {CopiedExe}:\n{ex}."); + } + } + + public void LaunchCrashHandler() + { + try + { + CloseCrashHandler(); + CopiedExe = CopyExecutables(); + var info = new ProcessStartInfo() + { + CreateNoWindow = true, + FileName = CopiedExe, + }; + info.ArgumentList.Add(_files.LogFileName); + info.ArgumentList.Add(Environment.ProcessId.ToString()); + _child = Process.Start(info); + if (_child == null) + throw new Exception("Child Process could not be created."); + + Penumbra.Log.Information($"Opened Crash Handler at {CopiedExe}, PID {_child.Id}."); + } + catch (Exception ex) + { + Penumbra.Log.Error($"Could not launch crash handler process:\n{ex}"); + CloseCrashHandler(); + _child = null; + } + } + + public JsonObject? Dump() + { + if (_eventWriter == null) + return null; + + try + { + using var reader = new GameEventLogReader(); + JsonObject jObj; + lock (_eventWriter) + { + jObj = reader.Dump("Manual Dump", Environment.ProcessId, 0); + } + + var logFile = _files.LogFileName; + using var s = File.Open(logFile, FileMode.Create); + using var jw = new Utf8JsonWriter(s, new JsonWriterOptions() { Indented = true }); + jObj.WriteTo(jw); + Penumbra.Log.Information($"Dumped crash handler memory to {logFile}."); + return jObj; + } + catch (Exception ex) + { + Penumbra.Log.Error($"Error dumping crash handler memory to file:\n{ex}"); + return null; + } + } + + private string CopyExecutables() + { + var parent = Path.GetDirectoryName(_files.CrashHandlerExe)!; + var folder = Path.Combine(parent, "temp"); + Directory.CreateDirectory(folder); + foreach (var file in Directory.EnumerateFiles(parent, "Penumbra.CrashHandler.*")) + File.Copy(file, Path.Combine(folder, Path.GetFileName(file)), true); + return Path.Combine(folder, Path.GetFileName(_files.CrashHandlerExe)); + } + + public void LogAnimation(nint character, ModCollection collection, AnimationInvocationType type) + { + if (_eventWriter == null) + return; + + var name = GetActorName(character); + lock (_eventWriter) + { + _eventWriter?.AnimationFuncInvoked.WriteLine(character, name.Span, collection.Name, type); + } + } + + private void OnCreatingCharacterBase(nint address, string collection, nint _1, nint _2, nint _3) + { + if (_eventWriter == null) + return; + + var name = GetActorName(address); + + lock (_eventWriter) + { + _eventWriter?.CharacterBase.WriteLine(address, name.Span, collection); + } + } + + private unsafe ByteString GetActorName(nint address) + { + var obj = (GameObject*)address; + if (obj == null) + return ByteString.FromSpanUnsafe("Unknown"u8, true, false, true); + + var id = _actors.FromObject(obj, out _, false, true, false); + return id.IsValid ? ByteString.FromStringUnsafe(id.Incognito(null), false) : + obj->Name[0] != 0 ? new ByteString(obj->Name) : ByteString.FromStringUnsafe($"Actor #{obj->ObjectIndex}", false); + } + + private unsafe void OnResourceLoaded(ResourceHandle* handle, Utf8GamePath originalPath, FullPath? manipulatedPath, ResolveData resolveData) + { + if (manipulatedPath == null || _eventWriter == null) + return; + + var dashIdx = manipulatedPath.Value.InternalName[0] == (byte)'|' ? manipulatedPath.Value.InternalName.IndexOf((byte)'|', 1) : -1; + if (dashIdx >= 0 && !Utf8GamePath.IsRooted(manipulatedPath.Value.InternalName.Substring(dashIdx + 1))) + return; + + var name = GetActorName(resolveData.AssociatedGameObject); + lock (_eventWriter) + { + _eventWriter!.FileLoaded.WriteLine(resolveData.AssociatedGameObject, name.Span, resolveData.ModCollection.Name, + manipulatedPath.Value.InternalName.Span, originalPath.Path.Span); + } + } + + private void CloseEventWriter() + { + if (_eventWriter == null) + return; + + _eventWriter.Dispose(); + _eventWriter = null; + Penumbra.Log.Debug("Closed Event Writer for crash handler."); + } + + private void OpenEventWriter() + { + try + { + CloseEventWriter(); + _eventWriter = new GameEventLogWriter(); + Penumbra.Log.Debug("Opened new Event Writer for crash handler."); + } + catch (Exception ex) + { + Penumbra.Log.Error($"Could not open Event Writer:\n{ex}"); + CloseEventWriter(); + } + } + + private unsafe void Subscribe() + { + _communicator.CreatingCharacterBase.Subscribe(OnCreatingCharacterBase, CreatingCharacterBase.Priority.CrashHandler); + _resourceLoader.ResourceLoaded += OnResourceLoaded; + } + + private unsafe void Unsubscribe() + { + _communicator.CreatingCharacterBase.Unsubscribe(OnCreatingCharacterBase); + _resourceLoader.ResourceLoaded -= OnResourceLoaded; + } +} diff --git a/Penumbra/Services/FilenameService.cs b/Penumbra/Services/FilenameService.cs index 5f918a90..49b4cb42 100644 --- a/Penumbra/Services/FilenameService.cs +++ b/Penumbra/Services/FilenameService.cs @@ -15,6 +15,12 @@ public class FilenameService(DalamudPluginInterface pi) : IService public readonly string FilesystemFile = Path.Combine(pi.ConfigDirectory.FullName, "sort_order.json"); public readonly string ActiveCollectionsFile = Path.Combine(pi.ConfigDirectory.FullName, "active_collections.json"); + public readonly string CrashHandlerExe = + Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Penumbra.CrashHandler.exe"); + + public readonly string LogFileName = + Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(pi.ConfigDirectory.FullName)!)!, "Penumbra.log"); + /// Obtain the path of a collection file given its name. public string CollectionFile(ModCollection collection) => CollectionFile(collection.Name); diff --git a/Penumbra/UI/Tabs/ChangedItemsTab.cs b/Penumbra/UI/Tabs/ChangedItemsTab.cs index 76fb8c96..ab0badf4 100644 --- a/Penumbra/UI/Tabs/ChangedItemsTab.cs +++ b/Penumbra/UI/Tabs/ChangedItemsTab.cs @@ -12,22 +12,13 @@ using Penumbra.UI.Classes; namespace Penumbra.UI.Tabs; -public class ChangedItemsTab : ITab +public class ChangedItemsTab( + CollectionManager collectionManager, + CollectionSelectHeader collectionHeader, + ChangedItemDrawer drawer, + CommunicatorService communicator) + : ITab { - private readonly CollectionManager _collectionManager; - private readonly ChangedItemDrawer _drawer; - private readonly CollectionSelectHeader _collectionHeader; - private readonly CommunicatorService _communicator; - - public ChangedItemsTab(CollectionManager collectionManager, CollectionSelectHeader collectionHeader, ChangedItemDrawer drawer, - CommunicatorService communicator) - { - _collectionManager = collectionManager; - _collectionHeader = collectionHeader; - _drawer = drawer; - _communicator = communicator; - } - public ReadOnlySpan Label => "Changed Items"u8; @@ -36,8 +27,8 @@ public class ChangedItemsTab : ITab public void DrawContent() { - _collectionHeader.Draw(true); - _drawer.DrawTypeFilter(); + collectionHeader.Draw(true); + drawer.DrawTypeFilter(); var varWidth = DrawFilters(); using var child = ImRaii.Child("##changedItemsChild", -Vector2.One); if (!child) @@ -54,7 +45,7 @@ public class ChangedItemsTab : ITab ImGui.TableSetupColumn("mods", flags, varWidth - 130 * UiHelpers.Scale); ImGui.TableSetupColumn("id", flags, 130 * UiHelpers.Scale); - var items = _collectionManager.Active.Current.ChangedItems; + var items = collectionManager.Active.Current.ChangedItems; var rest = ImGuiClip.FilteredClippedDraw(items, skips, FilterChangedItem, DrawChangedItemColumn); ImGuiClip.DrawEndDummy(rest, height); } @@ -75,21 +66,21 @@ public class ChangedItemsTab : ITab /// Apply the current filters. private bool FilterChangedItem(KeyValuePair, object?)> item) - => _drawer.FilterChangedItem(item.Key, item.Value.Item2, _changedItemFilter) + => drawer.FilterChangedItem(item.Key, item.Value.Item2, _changedItemFilter) && (_changedItemModFilter.IsEmpty || item.Value.Item1.Any(m => m.Name.Contains(_changedItemModFilter))); /// Draw a full column for a changed item. private void DrawChangedItemColumn(KeyValuePair, object?)> item) { ImGui.TableNextColumn(); - _drawer.DrawCategoryIcon(item.Key, item.Value.Item2); + drawer.DrawCategoryIcon(item.Key, item.Value.Item2); ImGui.SameLine(); - _drawer.DrawChangedItem(item.Key, item.Value.Item2); + drawer.DrawChangedItem(item.Key, item.Value.Item2); ImGui.TableNextColumn(); DrawModColumn(item.Value.Item1); ImGui.TableNextColumn(); - _drawer.DrawModelData(item.Value.Item2); + drawer.DrawModelData(item.Value.Item2); } private void DrawModColumn(SingleArray mods) @@ -102,7 +93,7 @@ public class ChangedItemsTab : ITab if (ImGui.Selectable(first.Name, false, ImGuiSelectableFlags.None, new Vector2(0, ImGui.GetFrameHeight())) && ImGui.GetIO().KeyCtrl && first is Mod mod) - _communicator.SelectTab.Invoke(TabType.Mods, mod); + communicator.SelectTab.Invoke(TabType.Mods, mod); if (ImGui.IsItemHovered()) { diff --git a/Penumbra/UI/Tabs/ConfigTabBar.cs b/Penumbra/UI/Tabs/ConfigTabBar.cs index ad3fdb3d..9fd07f27 100644 --- a/Penumbra/UI/Tabs/ConfigTabBar.cs +++ b/Penumbra/UI/Tabs/ConfigTabBar.cs @@ -44,8 +44,8 @@ public class ConfigTabBar : IDisposable Watcher = watcher; OnScreen = onScreen; Messages = messages; - Tabs = new ITab[] - { + Tabs = + [ Settings, Collections, Mods, @@ -56,7 +56,7 @@ public class ConfigTabBar : IDisposable Resource, Watcher, Messages, - }; + ]; _communicator.SelectTab.Subscribe(OnSelectTab, Communication.SelectTab.Priority.ConfigTabBar); } diff --git a/Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs b/Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs new file mode 100644 index 00000000..3d32d267 --- /dev/null +++ b/Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs @@ -0,0 +1,104 @@ +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using Penumbra.CrashHandler; + +namespace Penumbra.UI.Tabs.Debug; + +public static class CrashDataExtensions +{ + public static void DrawMeta(this CrashData data) + { + using (ImRaii.Group()) + { + ImGui.TextUnformatted(nameof(data.Mode)); + ImGui.TextUnformatted(nameof(data.CrashTime)); + ImGui.TextUnformatted(nameof(data.ExitCode)); + ImGui.TextUnformatted(nameof(data.ProcessId)); + ImGui.TextUnformatted(nameof(data.TotalModdedFilesLoaded)); + ImGui.TextUnformatted(nameof(data.TotalCharactersLoaded)); + ImGui.TextUnformatted(nameof(data.TotalVFXFuncsInvoked)); + } + + ImGui.SameLine(); + using (ImRaii.Group()) + { + ImGui.TextUnformatted(data.Mode); + ImGui.TextUnformatted(data.CrashTime.ToString()); + ImGui.TextUnformatted(data.ExitCode.ToString()); + ImGui.TextUnformatted(data.ProcessId.ToString()); + ImGui.TextUnformatted(data.TotalModdedFilesLoaded.ToString()); + ImGui.TextUnformatted(data.TotalCharactersLoaded.ToString()); + ImGui.TextUnformatted(data.TotalVFXFuncsInvoked.ToString()); + } + } + + public static void DrawCharacters(this CrashData data) + { + using var tree = ImRaii.TreeNode("Last Characters"); + if (!tree) + return; + + using var table = ImRaii.Table("##characterTable", 6, + ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg | ImGuiTableFlags.BordersInner); + if (!table) + return; + + ImGuiClip.ClippedDraw(data.LastCharactersLoaded, character => + { + ImGuiUtil.DrawTableColumn(character.Age.ToString(CultureInfo.InvariantCulture)); + ImGuiUtil.DrawTableColumn(character.ThreadId.ToString()); + ImGuiUtil.DrawTableColumn(character.CharacterName); + ImGuiUtil.DrawTableColumn(character.CollectionName); + ImGuiUtil.DrawTableColumn(character.CharacterAddress); + ImGuiUtil.DrawTableColumn(character.Timestamp.ToString()); + }, ImGui.GetTextLineHeightWithSpacing()); + } + + public static void DrawFiles(this CrashData data) + { + using var tree = ImRaii.TreeNode("Last Files"); + if (!tree) + return; + + using var table = ImRaii.Table("##filesTable", 8, + ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg | ImGuiTableFlags.BordersInner); + if (!table) + return; + + ImGuiClip.ClippedDraw(data.LastModdedFilesLoaded, file => + { + ImGuiUtil.DrawTableColumn(file.Age.ToString(CultureInfo.InvariantCulture)); + ImGuiUtil.DrawTableColumn(file.ThreadId.ToString()); + ImGuiUtil.DrawTableColumn(file.ActualFileName); + ImGuiUtil.DrawTableColumn(file.RequestedFileName); + ImGuiUtil.DrawTableColumn(file.CharacterName); + ImGuiUtil.DrawTableColumn(file.CollectionName); + ImGuiUtil.DrawTableColumn(file.CharacterAddress); + ImGuiUtil.DrawTableColumn(file.Timestamp.ToString()); + }, ImGui.GetTextLineHeightWithSpacing()); + } + + public static void DrawVfxInvocations(this CrashData data) + { + using var tree = ImRaii.TreeNode("Last VFX Invocations"); + if (!tree) + return; + + using var table = ImRaii.Table("##vfxTable", 7, + ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg | ImGuiTableFlags.BordersInner); + if (!table) + return; + + ImGuiClip.ClippedDraw(data.LastVfxFuncsInvoked, vfx => + { + ImGuiUtil.DrawTableColumn(vfx.Age.ToString(CultureInfo.InvariantCulture)); + ImGuiUtil.DrawTableColumn(vfx.ThreadId.ToString()); + ImGuiUtil.DrawTableColumn(vfx.InvocationType); + ImGuiUtil.DrawTableColumn(vfx.CharacterName); + ImGuiUtil.DrawTableColumn(vfx.CollectionName); + ImGuiUtil.DrawTableColumn(vfx.CharacterAddress); + ImGuiUtil.DrawTableColumn(vfx.Timestamp.ToString()); + }, ImGui.GetTextLineHeightWithSpacing()); + } +} diff --git a/Penumbra/UI/Tabs/Debug/CrashHandlerPanel.cs b/Penumbra/UI/Tabs/Debug/CrashHandlerPanel.cs new file mode 100644 index 00000000..c8e7f001 --- /dev/null +++ b/Penumbra/UI/Tabs/Debug/CrashHandlerPanel.cs @@ -0,0 +1,136 @@ +using System.Text.Json; +using Dalamud.Interface.DragDrop; +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Services; +using Penumbra.CrashHandler; +using Penumbra.Services; + +namespace Penumbra.UI.Tabs.Debug; + +public class CrashHandlerPanel(CrashHandlerService _service, Configuration _config, IDragDropManager _dragDrop) : IService +{ + private CrashData? _lastDump; + private string _lastLoadedFile = string.Empty; + private CrashData? _lastLoad; + private Exception? _lastLoadException; + + public void Draw() + { + DrawDropSource(); + DrawData(); + DrawDropTarget(); + } + + private void DrawData() + { + using var _ = ImRaii.Group(); + using var header = ImRaii.CollapsingHeader("Crash Handler"); + if (!header) + return; + + DrawButtons(); + DrawMainData(); + DrawObject("Last Manual Dump", _lastDump, null); + DrawObject(_lastLoadedFile.Length > 0 ? $"Loaded File ({_lastLoadedFile})###Loaded File" : "Loaded File", _lastLoad, + _lastLoadException); + } + + private void DrawMainData() + { + using var table = ImRaii.Table("##CrashHandlerTable", 2, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + PrintValue("Enabled", _config.UseCrashHandler); + PrintValue("Copied Executable Path", _service.CopiedExe); + PrintValue("Original Executable Path", _service.OriginalExe); + PrintValue("Log File Path", _service.LogPath); + PrintValue("XIV Process ID", _service.ProcessId.ToString()); + PrintValue("Crash Handler Running", _service.IsRunning.ToString()); + PrintValue("Crash Handler Process ID", _service.ChildProcessId.ToString()); + PrintValue("Crash Handler Exit Code", _service.ChildExitCode.ToString()); + } + + private void DrawButtons() + { + if (ImGui.Button("Dump Crash Handler Memory")) + _lastDump = _service.Dump()?.Deserialize(); + + if (ImGui.Button("Enable")) + _service.Enable(); + + ImGui.SameLine(); + if (ImGui.Button("Disable")) + _service.Disable(); + + if (ImGui.Button("Shutdown Crash Handler")) + _service.CloseCrashHandler(); + ImGui.SameLine(); + if (ImGui.Button("Relaunch Crash Handler")) + _service.LaunchCrashHandler(); + } + + private void DrawDropSource() + { + _dragDrop.CreateImGuiSource("LogDragDrop", m => m.Files.Any(f => f.EndsWith("Penumbra.log")), m => + { + ImGui.TextUnformatted("Dragging Penumbra.log for import."); + return true; + }); + } + + private void DrawDropTarget() + { + if (!_dragDrop.CreateImGuiTarget("LogDragDrop", out var files, out _)) + return; + + var file = files.FirstOrDefault(f => f.EndsWith("Penumbra.log")); + if (file == null) + return; + + _lastLoadedFile = file; + try + { + var jObj = _service.Load(file); + _lastLoad = jObj?.Deserialize(); + _lastLoadException = null; + } + catch (Exception ex) + { + _lastLoad = null; + _lastLoadException = ex; + } + } + + private static void DrawObject(string name, CrashData? data, Exception? ex) + { + using var tree = ImRaii.TreeNode(name); + if (!tree) + return; + + if (ex != null) + { + ImGuiUtil.TextWrapped(ex.ToString()); + return; + } + + if (data == null) + { + ImGui.TextUnformatted("Nothing loaded."); + return; + } + + data.DrawMeta(); + data.DrawFiles(); + data.DrawCharacters(); + data.DrawVfxInvocations(); + } + + private static void PrintValue(string label, in T data) + { + ImGuiUtil.DrawTableColumn(label); + ImGuiUtil.DrawTableColumn(data?.ToString() ?? "NULL"); + } +} diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index f4ddbe31..ab7ccf0c 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -53,7 +53,7 @@ public class Diagnostics(IServiceProvider provider) foreach (var type in typeof(ActorManager).Assembly.GetTypes() .Where(t => t is { IsAbstract: false, IsInterface: false } && t.IsAssignableTo(typeof(IAsyncDataContainer)))) { - var container = (IAsyncDataContainer) provider.GetRequiredService(type); + var container = (IAsyncDataContainer)provider.GetRequiredService(type); ImGuiUtil.DrawTableColumn(container.Name); ImGuiUtil.DrawTableColumn(container.Time.ToString()); ImGuiUtil.DrawTableColumn(Functions.HumanReadableSize(container.Memory)); @@ -88,18 +88,22 @@ public class DebugTab : Window, ITab private readonly TextureManager _textureManager; private readonly ShaderReplacementFixer _shaderReplacementFixer; private readonly RedrawService _redraws; - private readonly DictEmote _emotes; + private readonly DictEmote _emotes; private readonly Diagnostics _diagnostics; private readonly IObjectTable _objects; private readonly IClientState _clientState; private readonly IpcTester _ipcTester; + private readonly CrashHandlerPanel _crashHandlerPanel; - public DebugTab(PerformanceTracker performance, Configuration config, CollectionManager collectionManager, IObjectTable objects, IClientState clientState, - ValidityChecker validityChecker, ModManager modManager, HttpApi httpApi, ActorManager actors, StainService stains, CharacterUtility characterUtility, ResidentResourceManager residentResources, + public DebugTab(PerformanceTracker performance, Configuration config, CollectionManager collectionManager, IObjectTable objects, + IClientState clientState, + ValidityChecker validityChecker, ModManager modManager, HttpApi httpApi, ActorManager actors, StainService stains, + CharacterUtility characterUtility, ResidentResourceManager residentResources, ResourceManagerService resourceManager, PenumbraIpcProviders ipc, CollectionResolver collectionResolver, DrawObjectState drawObjectState, PathState pathState, SubfileHelper subfileHelper, IdentifiedCollectionCache identifiedCollectionCache, CutsceneService cutsceneService, ModImportManager modImporter, ImportPopup importPopup, FrameworkManager framework, - TextureManager textureManager, ShaderReplacementFixer shaderReplacementFixer, RedrawService redraws, DictEmote emotes, Diagnostics diagnostics, IpcTester ipcTester) + TextureManager textureManager, ShaderReplacementFixer shaderReplacementFixer, RedrawService redraws, DictEmote emotes, + Diagnostics diagnostics, IpcTester ipcTester, CrashHandlerPanel crashHandlerPanel) : base("Penumbra Debug Window", ImGuiWindowFlags.NoCollapse) { IsOpen = true; @@ -134,7 +138,8 @@ public class DebugTab : Window, ITab _redraws = redraws; _emotes = emotes; _diagnostics = diagnostics; - _ipcTester = ipcTester; + _ipcTester = ipcTester; + _crashHandlerPanel = crashHandlerPanel; _objects = objects; _clientState = clientState; } @@ -158,6 +163,9 @@ public class DebugTab : Window, ITab return; DrawDebugTabGeneral(); + ImGui.NewLine(); + _crashHandlerPanel.Draw(); + ImGui.NewLine(); _diagnostics.DrawDiagnostics(); DrawPerformanceTab(); ImGui.NewLine(); @@ -257,6 +265,7 @@ public class DebugTab : Window, ITab } } + var issues = _modManager.WithIndex().Count(p => p.Index != p.Value.Index); using (var tree = TreeNode($"Mods ({issues} Issues)###Mods")) { @@ -394,7 +403,7 @@ public class DebugTab : Window, ITab private void DrawPerformanceTab() { ImGui.NewLine(); - if (ImGui.CollapsingHeader("Performance")) + if (!ImGui.CollapsingHeader("Performance")) return; using (var start = TreeNode("Startup Performance", ImGuiTreeNodeFlags.DefaultOpen)) diff --git a/Penumbra/UI/Tabs/EffectiveTab.cs b/Penumbra/UI/Tabs/EffectiveTab.cs index 8ef6c30e..37561000 100644 --- a/Penumbra/UI/Tabs/EffectiveTab.cs +++ b/Penumbra/UI/Tabs/EffectiveTab.cs @@ -8,31 +8,22 @@ using Penumbra.Collections; using Penumbra.Collections.Cache; using Penumbra.Collections.Manager; using Penumbra.Meta.Manipulations; -using Penumbra.Mods; using Penumbra.Mods.Editor; using Penumbra.String.Classes; using Penumbra.UI.Classes; namespace Penumbra.UI.Tabs; -public class EffectiveTab : ITab +public class EffectiveTab(CollectionManager collectionManager, CollectionSelectHeader collectionHeader) + : ITab { - private readonly CollectionManager _collectionManager; - private readonly CollectionSelectHeader _collectionHeader; - - public EffectiveTab(CollectionManager collectionManager, CollectionSelectHeader collectionHeader) - { - _collectionManager = collectionManager; - _collectionHeader = collectionHeader; - } - public ReadOnlySpan Label => "Effective Changes"u8; public void DrawContent() { SetupEffectiveSizes(); - _collectionHeader.Draw(true); + collectionHeader.Draw(true); DrawFilters(); using var child = ImRaii.Child("##EffectiveChangesTab", -Vector2.One, false); if (!child) @@ -48,7 +39,7 @@ public class EffectiveTab : ITab ImGui.TableSetupColumn(string.Empty, ImGuiTableColumnFlags.WidthFixed, _effectiveArrowLength); ImGui.TableSetupColumn("##file", ImGuiTableColumnFlags.WidthFixed, _effectiveRightTextLength); - DrawEffectiveRows(_collectionManager.Active.Current, skips, height, + DrawEffectiveRows(collectionManager.Active.Current, skips, height, _effectiveFilePathFilter.Length > 0 || _effectiveGamePathFilter.Length > 0); } diff --git a/Penumbra/UI/Tabs/ModsTab.cs b/Penumbra/UI/Tabs/ModsTab.cs index 9f070d35..d111c465 100644 --- a/Penumbra/UI/Tabs/ModsTab.cs +++ b/Penumbra/UI/Tabs/ModsTab.cs @@ -17,68 +17,53 @@ using Penumbra.Collections.Manager; namespace Penumbra.UI.Tabs; -public class ModsTab : ITab +public class ModsTab( + ModManager modManager, + CollectionManager collectionManager, + ModFileSystemSelector selector, + ModPanel panel, + TutorialService tutorial, + RedrawService redrawService, + Configuration config, + IClientState clientState, + CollectionSelectHeader collectionHeader, + ITargetManager targets, + IObjectTable objectTable) + : ITab { - private readonly ModFileSystemSelector _selector; - private readonly ModPanel _panel; - private readonly TutorialService _tutorial; - private readonly ModManager _modManager; - private readonly ActiveCollections _activeCollections; - private readonly RedrawService _redrawService; - private readonly Configuration _config; - private readonly IClientState _clientState; - private readonly CollectionSelectHeader _collectionHeader; - private readonly ITargetManager _targets; - private readonly IObjectTable _objectTable; - - public ModsTab(ModManager modManager, CollectionManager collectionManager, ModFileSystemSelector selector, ModPanel panel, - TutorialService tutorial, RedrawService redrawService, Configuration config, IClientState clientState, - CollectionSelectHeader collectionHeader, ITargetManager targets, IObjectTable objectTable) - { - _modManager = modManager; - _activeCollections = collectionManager.Active; - _selector = selector; - _panel = panel; - _tutorial = tutorial; - _redrawService = redrawService; - _config = config; - _clientState = clientState; - _collectionHeader = collectionHeader; - _targets = targets; - _objectTable = objectTable; - } + private readonly ActiveCollections _activeCollections = collectionManager.Active; public bool IsVisible - => _modManager.Valid; + => modManager.Valid; public ReadOnlySpan Label => "Mods"u8; public void DrawHeader() - => _tutorial.OpenTutorial(BasicTutorialSteps.Mods); + => tutorial.OpenTutorial(BasicTutorialSteps.Mods); public Mod SelectMod { - set => _selector.SelectByValue(value); + set => selector.SelectByValue(value); } public void DrawContent() { try { - _selector.Draw(GetModSelectorSize(_config)); + selector.Draw(GetModSelectorSize(config)); ImGui.SameLine(); using var group = ImRaii.Group(); - _collectionHeader.Draw(false); + collectionHeader.Draw(false); using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, Vector2.Zero); - using (var child = ImRaii.Child("##ModsTabMod", new Vector2(-1, _config.HideRedrawBar ? 0 : -ImGui.GetFrameHeight()), + using (var child = ImRaii.Child("##ModsTabMod", new Vector2(-1, config.HideRedrawBar ? 0 : -ImGui.GetFrameHeight()), true, ImGuiWindowFlags.HorizontalScrollbar)) { style.Pop(); if (child) - _panel.Draw(); + panel.Draw(); style.Push(ImGuiStyleVar.ItemSpacing, Vector2.Zero); } @@ -89,14 +74,14 @@ public class ModsTab : ITab catch (Exception e) { Penumbra.Log.Error($"Exception thrown during ModPanel Render:\n{e}"); - Penumbra.Log.Error($"{_modManager.Count} Mods\n" + Penumbra.Log.Error($"{modManager.Count} Mods\n" + $"{_activeCollections.Current.AnonymizedName} Current Collection\n" + $"{_activeCollections.Current.Settings.Count} Settings\n" - + $"{_selector.SortMode.Name} Sort Mode\n" - + $"{_selector.SelectedLeaf?.Name ?? "NULL"} Selected Leaf\n" - + $"{_selector.Selected?.Name ?? "NULL"} Selected Mod\n" + + $"{selector.SortMode.Name} Sort Mode\n" + + $"{selector.SelectedLeaf?.Name ?? "NULL"} Selected Leaf\n" + + $"{selector.Selected?.Name ?? "NULL"} Selected Mod\n" + $"{string.Join(", ", _activeCollections.Current.DirectlyInheritsFrom.Select(c => c.AnonymizedName))} Inheritances\n" - + $"{_selector.SelectedSettingCollection.AnonymizedName} Collection\n"); + + $"{selector.SelectedSettingCollection.AnonymizedName} Collection\n"); } } @@ -115,9 +100,9 @@ public class ModsTab : ITab private void DrawRedrawLine() { - if (_config.HideRedrawBar) + if (config.HideRedrawBar) { - _tutorial.SkipTutorial(BasicTutorialSteps.Redrawing); + tutorial.SkipTutorial(BasicTutorialSteps.Redrawing); return; } @@ -135,15 +120,15 @@ public class ModsTab : ITab } var hovered = ImGui.IsItemHovered(); - _tutorial.OpenTutorial(BasicTutorialSteps.Redrawing); + tutorial.OpenTutorial(BasicTutorialSteps.Redrawing); if (hovered) ImGui.SetTooltip($"The supported modifiers for '/penumbra redraw' are:\n{TutorialService.SupportedRedrawModifiers}"); using var id = ImRaii.PushId("Redraw"); - using var disabled = ImRaii.Disabled(_clientState.LocalPlayer == null); + using var disabled = ImRaii.Disabled(clientState.LocalPlayer == null); ImGui.SameLine(); var buttonWidth = frameHeight with { X = ImGui.GetContentRegionAvail().X / 5 }; - var tt = _objectTable.GetObjectAddress(0) == nint.Zero + var tt = objectTable.GetObjectAddress(0) == nint.Zero ? "\nCan only be used when you are logged in and your character is available." : string.Empty; DrawButton(buttonWidth, "All", string.Empty, tt); @@ -151,13 +136,13 @@ public class ModsTab : ITab DrawButton(buttonWidth, "Self", "self", tt); ImGui.SameLine(); - tt = _targets.Target == null && _targets.GPoseTarget == null + tt = targets.Target == null && targets.GPoseTarget == null ? "\nCan only be used when you have a target." : string.Empty; DrawButton(buttonWidth, "Target", "target", tt); ImGui.SameLine(); - tt = _targets.FocusTarget == null + tt = targets.FocusTarget == null ? "\nCan only be used when you have a focus target." : string.Empty; DrawButton(buttonWidth, "Focus", "focus", tt); @@ -176,9 +161,9 @@ public class ModsTab : ITab if (ImGui.Button(label, size)) { if (lower.Length > 0) - _redrawService.RedrawObject(lower, RedrawType.Redraw); + redrawService.RedrawObject(lower, RedrawType.Redraw); else - _redrawService.RedrawAll(RedrawType.Redraw); + redrawService.RedrawAll(RedrawType.Redraw); } } diff --git a/Penumbra/UI/Tabs/OnScreenTab.cs b/Penumbra/UI/Tabs/OnScreenTab.cs index 8d323baf..09772d8e 100644 --- a/Penumbra/UI/Tabs/OnScreenTab.cs +++ b/Penumbra/UI/Tabs/OnScreenTab.cs @@ -7,7 +7,7 @@ namespace Penumbra.UI.Tabs; public class OnScreenTab : ITab { private readonly Configuration _config; - private ResourceTreeViewer _viewer; + private readonly ResourceTreeViewer _viewer; public OnScreenTab(Configuration config, ResourceTreeFactory treeFactory, ChangedItemDrawer changedItemDrawer) { diff --git a/Penumbra/UI/Tabs/ResourceTab.cs b/Penumbra/UI/Tabs/ResourceTab.cs index 6f3dec30..bbb0561b 100644 --- a/Penumbra/UI/Tabs/ResourceTab.cs +++ b/Penumbra/UI/Tabs/ResourceTab.cs @@ -12,24 +12,14 @@ using Penumbra.String.Classes; namespace Penumbra.UI.Tabs; -public class ResourceTab : ITab +public class ResourceTab(Configuration config, ResourceManagerService resourceManager, ISigScanner sigScanner) + : ITab { - private readonly Configuration _config; - private readonly ResourceManagerService _resourceManager; - private readonly ISigScanner _sigScanner; - - public ResourceTab(Configuration config, ResourceManagerService resourceManager, ISigScanner sigScanner) - { - _config = config; - _resourceManager = resourceManager; - _sigScanner = sigScanner; - } - public ReadOnlySpan Label => "Resource Manager"u8; public bool IsVisible - => _config.DebugMode; + => config.DebugMode; /// Draw a tab to iterate over the main resource maps and see what resources are currently loaded. public void DrawContent() @@ -44,15 +34,15 @@ public class ResourceTab : ITab unsafe { - _resourceManager.IterateGraphs(DrawCategoryContainer); + resourceManager.IterateGraphs(DrawCategoryContainer); } ImGui.NewLine(); unsafe { ImGui.TextUnformatted( - $"Static Address: 0x{(ulong)_resourceManager.ResourceManagerAddress:X} (+0x{(ulong)_resourceManager.ResourceManagerAddress - (ulong)_sigScanner.Module.BaseAddress:X})"); - ImGui.TextUnformatted($"Actual Address: 0x{(ulong)_resourceManager.ResourceManager:X}"); + $"Static Address: 0x{(ulong)resourceManager.ResourceManagerAddress:X} (+0x{(ulong)resourceManager.ResourceManagerAddress - (ulong)sigScanner.Module.BaseAddress:X})"); + ImGui.TextUnformatted($"Actual Address: 0x{(ulong)resourceManager.ResourceManager:X}"); } } @@ -82,7 +72,7 @@ public class ResourceTab : ITab ImGui.TableSetupColumn("Refs", ImGuiTableColumnFlags.WidthFixed, _refsColumnWidth); ImGui.TableHeadersRow(); - _resourceManager.IterateResourceMap(map, (hash, r) => + resourceManager.IterateResourceMap(map, (hash, r) => { // Filter unwanted names. if (_resourceManagerFilter.Length != 0 @@ -125,7 +115,7 @@ public class ResourceTab : ITab if (tree) { SetTableWidths(); - _resourceManager.IterateExtMap(map, (ext, m) => DrawResourceMap(category, ext, m)); + resourceManager.IterateExtMap(map, (ext, m) => DrawResourceMap(category, ext, m)); } } diff --git a/Penumbra/packages.lock.json b/Penumbra/packages.lock.json index cb873592..d0e8fa1a 100644 --- a/Penumbra/packages.lock.json +++ b/Penumbra/packages.lock.json @@ -11,18 +11,6 @@ "Unosquare.Swan.Lite": "3.0.0" } }, - "Microsoft.CodeAnalysis.Common": { - "type": "Direct", - "requested": "[4.8.0, )", - "resolved": "4.8.0", - "contentHash": "/jR+e/9aT+BApoQJABlVCKnnggGQbvGh7BKq2/wI1LamxC+LbzhcLj4Vj7gXCofl1n4E521YfF9w0WcASGg/KA==", - "dependencies": { - "Microsoft.CodeAnalysis.Analyzers": "3.3.4", - "System.Collections.Immutable": "7.0.0", - "System.Reflection.Metadata": "7.0.0", - "System.Runtime.CompilerServices.Unsafe": "6.0.0" - } - }, "Microsoft.Extensions.DependencyInjection": { "type": "Direct", "requested": "[7.0.0, )", @@ -55,29 +43,15 @@ }, "SixLabors.ImageSharp": { "type": "Direct", - "requested": "[2.1.2, )", - "resolved": "2.1.2", - "contentHash": "In0pC521LqJXJXZgFVHegvSzES10KkKRN31McxqA1+fKtKsNe+EShWavBFQnKRlXCdeAmfx/wDjLILbvCaq+8Q==", - "dependencies": { - "System.Runtime.CompilerServices.Unsafe": "5.0.0", - "System.Text.Encoding.CodePages": "5.0.0" - } - }, - "Microsoft.CodeAnalysis.Analyzers": { - "type": "Transitive", - "resolved": "3.3.4", - "contentHash": "AxkxcPR+rheX0SmvpLVIGLhOUXAKG56a64kV9VQZ4y9gR9ZmPXnqZvHJnmwLSwzrEP6junUF11vuc+aqo5r68g==" + "requested": "[3.1.3, )", + "resolved": "3.1.3", + "contentHash": "wybtaqZQ1ZRZ4ZeU+9h+PaSeV14nyiGKIy7qRbDfSHzHq4ybqyOcjoifeaYbiKLO1u+PVxLBuy7MF/DMmwwbfg==" }, "Microsoft.Extensions.DependencyInjection.Abstractions": { "type": "Transitive", "resolved": "7.0.0", "contentHash": "h3j/QfmFN4S0w4C2A6X7arXij/M/OVw3uQHSOFxnND4DyAzO1F9eMX7Eti7lU/OkSthEE0WzRsfT/Dmx86jzCw==" }, - "Microsoft.NETCore.Platforms": { - "type": "Transitive", - "resolved": "5.0.0", - "contentHash": "VyPlqzH2wavqquTcYpkIIAQ6WdenuKoFN0BdYBbCWsclXacSOHNQn66Gt4z5NBqEYW0FAPm5rlvki9ZiCij5xQ==" - }, "SharpGLTF.Runtime": { "type": "Transitive", "resolved": "1.0.0-alpha0030", @@ -86,32 +60,6 @@ "SharpGLTF.Core": "1.0.0-alpha0030" } }, - "System.Collections.Immutable": { - "type": "Transitive", - "resolved": "7.0.0", - "contentHash": "dQPcs0U1IKnBdRDBkrCTi1FoajSTBzLcVTpjO4MBCMC7f4pDOIPzgBoX8JjG7X6uZRJ8EBxsi8+DR1JuwjnzOQ==" - }, - "System.Reflection.Metadata": { - "type": "Transitive", - "resolved": "7.0.0", - "contentHash": "MclTG61lsD9sYdpNz9xsKBzjsmsfCtcMZYXz/IUr2zlhaTaABonlr1ESeompTgM+Xk+IwtGYU7/voh3YWB/fWw==", - "dependencies": { - "System.Collections.Immutable": "7.0.0" - } - }, - "System.Runtime.CompilerServices.Unsafe": { - "type": "Transitive", - "resolved": "6.0.0", - "contentHash": "/iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg==" - }, - "System.Text.Encoding.CodePages": { - "type": "Transitive", - "resolved": "5.0.0", - "contentHash": "NyscU59xX6Uo91qvhOs2Ccho3AR2TnZPomo1Z0K6YpyztBPM/A5VbkzOO19sy3A3i1TtEnTxA7bCe3Us+r5MWg==", - "dependencies": { - "Microsoft.NETCore.Platforms": "5.0.0" - } - }, "System.ValueTuple": { "type": "Transitive", "resolved": "4.5.0", @@ -134,11 +82,14 @@ "penumbra.api": { "type": "Project" }, + "penumbra.crashhandler": { + "type": "Project" + }, "penumbra.gamedata": { "type": "Project", "dependencies": { "OtterGui": "[1.0.0, )", - "Penumbra.Api": "[1.0.13, )", + "Penumbra.Api": "[1.0.14, )", "Penumbra.String": "[1.0.4, )" } }, From 8318a4bd84786858c67171261f18f55a6a8a6cc7 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 16 Mar 2024 16:20:46 +0100 Subject: [PATCH 030/433] Compatibility fix. --- Penumbra/Import/Models/Export/MaterialExporter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Penumbra/Import/Models/Export/MaterialExporter.cs b/Penumbra/Import/Models/Export/MaterialExporter.cs index f17fdaa2..82e13eb9 100644 --- a/Penumbra/Import/Models/Export/MaterialExporter.cs +++ b/Penumbra/Import/Models/Export/MaterialExporter.cs @@ -53,7 +53,7 @@ public class MaterialExporter var normal = material.Textures[TextureUsage.SamplerNormal]; var operation = new ProcessCharacterNormalOperation(normal, table); - ParallelRowIterator.IterateRows(ImageSharpConfiguration.Default, normal.Bounds(), in operation); + ParallelRowIterator.IterateRows(ImageSharpConfiguration.Default, normal.Bounds, in operation); // Check if full textures are provided, and merge in if available. var baseColor = operation.BaseColor; @@ -199,7 +199,7 @@ public class MaterialExporter small.Mutate(context => context.Resize(large.Width, large.Height)); var operation = new MultiplyOperation(target, multiplier); - ParallelRowIterator.IterateRows(ImageSharpConfiguration.Default, target.Bounds(), in operation); + ParallelRowIterator.IterateRows(ImageSharpConfiguration.Default, target.Bounds, in operation); } } From 4fc763c9aa719948d41b4075e684e0647c5e96de Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 16 Mar 2024 16:21:06 +0100 Subject: [PATCH 031/433] Keep first empty option in its desired location. --- Penumbra/Import/TexToolsImporter.ModPack.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Penumbra/Import/TexToolsImporter.ModPack.cs b/Penumbra/Import/TexToolsImporter.ModPack.cs index 94a5e5ac..7c4b94d8 100644 --- a/Penumbra/Import/TexToolsImporter.ModPack.cs +++ b/Penumbra/Import/TexToolsImporter.ModPack.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using OtterGui; using Penumbra.Api.Enums; using Penumbra.Import.Structs; using Penumbra.Mods; @@ -35,7 +36,8 @@ public partial class TexToolsImporter var modList = modListRaw.Select(m => JsonConvert.DeserializeObject(m, JsonSettings)!).ToList(); - _currentModDirectory = ModCreator.CreateModFolder(_baseDirectory, Path.GetFileNameWithoutExtension(modPackFile.Name), _config.ReplaceNonAsciiOnImport, true); + _currentModDirectory = ModCreator.CreateModFolder(_baseDirectory, Path.GetFileNameWithoutExtension(modPackFile.Name), + _config.ReplaceNonAsciiOnImport, true); // Create a new ModMeta from the TTMP mod list info _modManager.DataEditor.CreateMeta(_currentModDirectory, _currentModName, DefaultTexToolsData.Author, DefaultTexToolsData.Description, null, null); @@ -193,15 +195,17 @@ public partial class TexToolsImporter optionIdx += maxOptions; // Handle empty options for single select groups without creating a folder for them. - // We only want one of those at most, and it should usually be the first option. + // We only want one of those at most. if (group.SelectionType == GroupType.Single) { - var empty = group.OptionList.FirstOrDefault(o => o.Name.Length > 0 && o.ModsJsons.Length == 0); - if (empty != null) + var idx = group.OptionList.IndexOf(o => o.Name.Length > 0 && o.ModsJsons.Length == 0); + if (idx >= 0) { - _currentOptionName = empty.Name; - options.Insert(0, ModCreator.CreateEmptySubMod(empty.Name)); - defaultSettings = defaultSettings == null ? 0 : defaultSettings.Value + 1; + var option = group.OptionList[idx]; + _currentOptionName = option.Name; + options.Insert(idx, ModCreator.CreateEmptySubMod(option.Name)); + if (option.IsChecked) + defaultSettings = (uint) idx; } } From 50f81cc889999c2cee7634a72d547eeecd3516b7 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 16 Mar 2024 16:21:23 +0100 Subject: [PATCH 032/433] Skip locals init. --- .../MaterialPreview/LiveColorTablePreviewer.cs | 1 + .../ResolveContext.PathResolution.cs | 4 +++- .../Interop/ResourceTree/ResolveContext.cs | 1 + Penumbra/Mods/Editor/DuplicateManager.cs | 18 +++++++++--------- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs b/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs index 801c3bf0..a8e4ea4d 100644 --- a/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs +++ b/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs @@ -62,6 +62,7 @@ public sealed unsafe class LiveColorTablePreviewer : LiveMaterialPreviewerBase _updatePending = true; } + [SkipLocalsInit] private void OnFrameworkUpdate(IFramework _) { if (!_updatePending) diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs index cf939292..d5b4fa39 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs @@ -89,6 +89,7 @@ internal partial record ResolveContext }; } + [SkipLocalsInit] private unsafe Utf8GamePath ResolveEquipmentMaterialPath(Utf8GamePath modelPath, ResourceHandle* imc, byte* mtrlFileName) { var variant = ResolveMaterialVariant(imc, Equipment.Variant); @@ -100,6 +101,7 @@ internal partial record ResolveContext return Utf8GamePath.FromSpan(pathBuffer, out var path) ? path.Clone() : Utf8GamePath.Empty; } + [SkipLocalsInit] private unsafe Utf8GamePath ResolveWeaponMaterialPath(Utf8GamePath modelPath, ResourceHandle* imc, byte* mtrlFileName) { var setIdHigh = Equipment.Set.Id / 100; @@ -168,7 +170,7 @@ internal partial record ResolveContext { var modelPosition = modelPath.IndexOf("/model/"u8); if (modelPosition < 0) - return Span.Empty; + return []; var baseDirectory = modelPath[..modelPosition]; diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.cs b/Penumbra/Interop/ResourceTree/ResolveContext.cs index 0637cba6..d1701f47 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.cs @@ -54,6 +54,7 @@ internal unsafe partial record ResolveContext( return GetOrCreateNode(ResourceType.Shpk, (nint)resourceHandle->ShaderPackage, &resourceHandle->ResourceHandle, path); } + [SkipLocalsInit] private ResourceNode? CreateNodeFromTex(TextureResourceHandle* resourceHandle, ByteString gamePath, bool dx11) { if (resourceHandle == null) diff --git a/Penumbra/Mods/Editor/DuplicateManager.cs b/Penumbra/Mods/Editor/DuplicateManager.cs index dad05102..77d10cc4 100644 --- a/Penumbra/Mods/Editor/DuplicateManager.cs +++ b/Penumbra/Mods/Editor/DuplicateManager.cs @@ -7,8 +7,8 @@ namespace Penumbra.Mods.Editor; public class DuplicateManager(ModManager modManager, SaveService saveService, Configuration config) { - private readonly SHA256 _hasher = SHA256.Create(); - private readonly List<(FullPath[] Paths, long Size, byte[] Hash)> _duplicates = []; + private readonly SHA256 _hasher = SHA256.Create(); + private readonly List<(FullPath[] Paths, long Size, byte[] Hash)> _duplicates = []; public IReadOnlyList<(FullPath[] Paths, long Size, byte[] Hash)> Duplicates => _duplicates; @@ -164,17 +164,17 @@ public class DuplicateManager(ModManager modManager, SaveService saveService, Co } /// Check if two files are identical on a binary level. Returns true if they are identical. + [SkipLocalsInit] public static unsafe bool CompareFilesDirectly(FullPath f1, FullPath f2) { + const int size = 256; if (!f1.Exists || !f2.Exists) return false; - using var s1 = File.OpenRead(f1.FullName); - using var s2 = File.OpenRead(f2.FullName); - var buffer1 = stackalloc byte[256]; - var buffer2 = stackalloc byte[256]; - var span1 = new Span(buffer1, 256); - var span2 = new Span(buffer2, 256); + using var s1 = File.OpenRead(f1.FullName); + using var s2 = File.OpenRead(f2.FullName); + Span span1 = stackalloc byte[size]; + Span span2 = stackalloc byte[size]; while (true) { @@ -186,7 +186,7 @@ public class DuplicateManager(ModManager modManager, SaveService saveService, Co if (!span1[..bytes1].SequenceEqual(span2[..bytes2])) return false; - if (bytes1 < 256) + if (bytes1 < size) return true; } } From c1a9c798ae844c79023a1248fa4ce80731dcf50b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 16 Mar 2024 16:22:07 +0100 Subject: [PATCH 033/433] auto format. --- Penumbra/Penumbra.cs | 71 ++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index 67f523ba..b532339f 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -19,7 +19,6 @@ using ChangedItemClick = Penumbra.Communication.ChangedItemClick; using ChangedItemHover = Penumbra.Communication.ChangedItemHover; using OtterGui.Tasks; using Penumbra.GameData.Enums; -using Penumbra.Interop.Structs; using Penumbra.UI; using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManager; @@ -74,8 +73,8 @@ public class Penumbra : IDalamudPlugin _tempCollections = _services.GetService(); _redrawService = _services.GetService(); _communicatorService = _services.GetService(); - _services.GetService(); // Initialize because not required anywhere else. - _services.GetService(); // Initialize because not required anywhere else. + _services.GetService(); // Initialize because not required anywhere else. + _services.GetService(); // Initialize because not required anywhere else. _collectionManager.Caches.CreateNecessaryCaches(); _services.GetService(); _services.GetService(); @@ -245,38 +244,38 @@ public class Penumbra : IDalamudPlugin return sb.ToString(); } - private static string CollectLocaleEnvironmentVariables() - { - var variableNames = new List(); - var variables = new Dictionary(StringComparer.Ordinal); - foreach (DictionaryEntry variable in Environment.GetEnvironmentVariables()) - { - var key = (string)variable.Key; - if (key.Equals("LANG", StringComparison.Ordinal) || key.StartsWith("LC_", StringComparison.Ordinal)) - { - variableNames.Add(key); - variables.Add(key, ((string?)variable.Value) ?? string.Empty); - } - } - - variableNames.Sort(); - - var pos = variableNames.IndexOf("LC_ALL"); - if (pos > 0) // If it's == 0, we're going to do a no-op. - { - variableNames.RemoveAt(pos); - variableNames.Insert(0, "LC_ALL"); - } - - pos = variableNames.IndexOf("LANG"); - if (pos >= 0 && pos < variableNames.Count - 1) - { - variableNames.RemoveAt(pos); - variableNames.Add("LANG"); - } - - return variableNames.Count == 0 - ? "None" - : string.Join(", ", variableNames.Select(name => $"`{name}={variables[name]}`")); + private static string CollectLocaleEnvironmentVariables() + { + var variableNames = new List(); + var variables = new Dictionary(StringComparer.Ordinal); + foreach (DictionaryEntry variable in Environment.GetEnvironmentVariables()) + { + var key = (string)variable.Key; + if (key.Equals("LANG", StringComparison.Ordinal) || key.StartsWith("LC_", StringComparison.Ordinal)) + { + variableNames.Add(key); + variables.Add(key, (string?)variable.Value ?? string.Empty); + } + } + + variableNames.Sort(); + + var pos = variableNames.IndexOf("LC_ALL"); + if (pos > 0) // If it's == 0, we're going to do a no-op. + { + variableNames.RemoveAt(pos); + variableNames.Insert(0, "LC_ALL"); + } + + pos = variableNames.IndexOf("LANG"); + if (pos >= 0 && pos < variableNames.Count - 1) + { + variableNames.RemoveAt(pos); + variableNames.Add("LANG"); + } + + return variableNames.Count == 0 + ? "None" + : string.Join(", ", variableNames.Select(name => $"`{name}={variables[name]}`")); } } From 7cb50030f97d353b3870d41c4edb00e082b2c177 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 16 Mar 2024 16:58:02 +0100 Subject: [PATCH 034/433] Fix some stuff, add versions. --- Penumbra.CrashHandler/CrashData.cs | 12 +++++++++--- Penumbra.CrashHandler/GameEventLogReader.cs | 12 +++++++----- Penumbra.CrashHandler/Program.cs | 4 ++-- Penumbra/Services/CrashHandlerService.cs | 18 +++++++++++------- Penumbra/Services/ValidityChecker.cs | 13 ++++++++++--- Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs | 6 ++++++ 6 files changed, 45 insertions(+), 20 deletions(-) diff --git a/Penumbra.CrashHandler/CrashData.cs b/Penumbra.CrashHandler/CrashData.cs index 956a3db7..cdac103f 100644 --- a/Penumbra.CrashHandler/CrashData.cs +++ b/Penumbra.CrashHandler/CrashData.cs @@ -24,6 +24,12 @@ public class CrashData /// The time this crash data was generated. public DateTimeOffset CrashTime { get; set; } = DateTimeOffset.UnixEpoch; + /// Penumbra's Version when this crash data was created. + public string Version { get; set; } = string.Empty; + + /// The Game's Version when this crash data was created. + public string GameVersion { get; set; } = string.Empty; + /// The FFXIV process ID when this data was generated. public int ProcessId { get; set; } = 0; @@ -52,11 +58,11 @@ public class CrashData => LastVfxFuncsInvoked.Count == 0 ? default : LastVfxFuncsInvoked[0]; /// A collection of the last few characters loaded before this crash data was generated. - public List LastCharactersLoaded { get; } = []; + public List LastCharactersLoaded { get; set; } = []; /// A collection of the last few modded files loaded before this crash data was generated. - public List LastModdedFilesLoaded { get; } = []; + public List LastModdedFilesLoaded { get; set; } = []; /// A collection of the last few vfx functions invoked before this crash data was generated. - public List LastVfxFuncsInvoked { get; } = []; + public List LastVfxFuncsInvoked { get; set; } = []; } diff --git a/Penumbra.CrashHandler/GameEventLogReader.cs b/Penumbra.CrashHandler/GameEventLogReader.cs index 283be526..1ae49fa5 100644 --- a/Penumbra.CrashHandler/GameEventLogReader.cs +++ b/Penumbra.CrashHandler/GameEventLogReader.cs @@ -25,15 +25,17 @@ public sealed class GameEventLogReader : IDisposable } - public JsonObject Dump(string mode, int processId, int exitCode) + public JsonObject Dump(string mode, int processId, int exitCode, string version, string gameVersion) { var crashTime = DateTimeOffset.UtcNow; var obj = new JsonObject { - [nameof(CrashData.Mode)] = mode, - [nameof(CrashData.CrashTime)] = DateTimeOffset.UtcNow, - [nameof(CrashData.ProcessId)] = processId, - [nameof(CrashData.ExitCode)] = exitCode, + [nameof(CrashData.Mode)] = mode, + [nameof(CrashData.CrashTime)] = DateTimeOffset.UtcNow, + [nameof(CrashData.ProcessId)] = processId, + [nameof(CrashData.ExitCode)] = exitCode, + [nameof(CrashData.Version)] = version, + [nameof(CrashData.GameVersion)] = gameVersion, }; foreach (var (reader, singular, _) in Readers) diff --git a/Penumbra.CrashHandler/Program.cs b/Penumbra.CrashHandler/Program.cs index e4a46348..518e2d04 100644 --- a/Penumbra.CrashHandler/Program.cs +++ b/Penumbra.CrashHandler/Program.cs @@ -7,7 +7,7 @@ public class CrashHandler { public static void Main(string[] args) { - if (args.Length < 2 || !int.TryParse(args[1], out var pid)) + if (args.Length < 4 || !int.TryParse(args[1], out var pid)) return; try @@ -17,7 +17,7 @@ public class CrashHandler parent.WaitForExit(); var exitCode = parent.ExitCode; - var obj = reader.Dump("Crash", pid, exitCode); + var obj = reader.Dump("Crash", pid, exitCode, args[2], args[3]); using var fs = File.Open(args[0], FileMode.Create); using var w = new Utf8JsonWriter(fs, new JsonWriterOptions { Indented = true }); obj.WriteTo(w, new JsonSerializerOptions() { WriteIndented = true }); diff --git a/Penumbra/Services/CrashHandlerService.cs b/Penumbra/Services/CrashHandlerService.cs index 4e2bce0f..a3a35b78 100644 --- a/Penumbra/Services/CrashHandlerService.cs +++ b/Penumbra/Services/CrashHandlerService.cs @@ -22,15 +22,17 @@ public sealed class CrashHandlerService : IDisposable, IService private readonly ActorManager _actors; private readonly ResourceLoader _resourceLoader; private readonly Configuration _config; + private readonly ValidityChecker _validityChecker; public CrashHandlerService(FilenameService files, CommunicatorService communicator, ActorManager actors, ResourceLoader resourceLoader, - Configuration config) + Configuration config, ValidityChecker validityChecker) { - _files = files; - _communicator = communicator; - _actors = actors; - _resourceLoader = resourceLoader; - _config = config; + _files = files; + _communicator = communicator; + _actors = actors; + _resourceLoader = resourceLoader; + _config = config; + _validityChecker = validityChecker; if (!_config.UseCrashHandler) return; @@ -152,6 +154,8 @@ public sealed class CrashHandlerService : IDisposable, IService }; info.ArgumentList.Add(_files.LogFileName); info.ArgumentList.Add(Environment.ProcessId.ToString()); + info.ArgumentList.Add($"{_validityChecker.Version} ({_validityChecker.CommitHash})"); + info.ArgumentList.Add(_validityChecker.GameVersion); _child = Process.Start(info); if (_child == null) throw new Exception("Child Process could not be created."); @@ -177,7 +181,7 @@ public sealed class CrashHandlerService : IDisposable, IService JsonObject jObj; lock (_eventWriter) { - jObj = reader.Dump("Manual Dump", Environment.ProcessId, 0); + jObj = reader.Dump("Manual Dump", Environment.ProcessId, 0, $"{_validityChecker.Version} ({_validityChecker.CommitHash})", _validityChecker.GameVersion); } var logFile = _files.LogFileName; diff --git a/Penumbra/Services/ValidityChecker.cs b/Penumbra/Services/ValidityChecker.cs index 4d071f85..d4b5005f 100644 --- a/Penumbra/Services/ValidityChecker.cs +++ b/Penumbra/Services/ValidityChecker.cs @@ -1,5 +1,6 @@ using Dalamud.Interface.Internal.Notifications; using Dalamud.Plugin; +using FFXIVClientStructs.FFXIV.Client.System.Framework; using OtterGui.Classes; using OtterGui.Services; @@ -20,6 +21,7 @@ public class ValidityChecker : IService public readonly string Version; public readonly string CommitHash; + public readonly string GameVersion; public ValidityChecker(DalamudPluginInterface pi) { @@ -28,14 +30,19 @@ public class ValidityChecker : IService IsValidSourceRepo = CheckSourceRepo(pi); var assembly = GetType().Assembly; - Version = assembly.GetName().Version?.ToString() ?? string.Empty; - CommitHash = assembly.GetCustomAttribute()?.InformationalVersion ?? "Unknown"; + Version = assembly.GetName().Version?.ToString() ?? string.Empty; + CommitHash = assembly.GetCustomAttribute()?.InformationalVersion ?? "Unknown"; + GameVersion = GetGameVersion(); } + private static unsafe string GetGameVersion() + => Framework.Instance()->GameVersion[0]; + public void LogExceptions() { if (ImcExceptions.Count > 0) - Penumbra.Messager.NotificationMessage($"{ImcExceptions} IMC Exceptions thrown during Penumbra load. Please repair your game files.", NotificationType.Warning); + Penumbra.Messager.NotificationMessage($"{ImcExceptions} IMC Exceptions thrown during Penumbra load. Please repair your game files.", + NotificationType.Warning); } // Because remnants of penumbra in devPlugins cause issues, we check for them to warn users to remove them. diff --git a/Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs b/Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs index 3d32d267..78014054 100644 --- a/Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs +++ b/Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs @@ -13,6 +13,9 @@ public static class CrashDataExtensions { ImGui.TextUnformatted(nameof(data.Mode)); ImGui.TextUnformatted(nameof(data.CrashTime)); + ImGui.TextUnformatted("Current Age"); + ImGui.TextUnformatted(nameof(data.Version)); + ImGui.TextUnformatted(nameof(data.GameVersion)); ImGui.TextUnformatted(nameof(data.ExitCode)); ImGui.TextUnformatted(nameof(data.ProcessId)); ImGui.TextUnformatted(nameof(data.TotalModdedFilesLoaded)); @@ -25,6 +28,9 @@ public static class CrashDataExtensions { ImGui.TextUnformatted(data.Mode); ImGui.TextUnformatted(data.CrashTime.ToString()); + ImGui.TextUnformatted((DateTimeOffset.UtcNow - data.CrashTime).ToString(@"dd\.hh\:mm\:ss")); + ImGui.TextUnformatted(data.Version); + ImGui.TextUnformatted(data.GameVersion); ImGui.TextUnformatted(data.ExitCode.ToString()); ImGui.TextUnformatted(data.ProcessId.ToString()); ImGui.TextUnformatted(data.TotalModdedFilesLoaded.ToString()); From ceb3bbc5c37f1f4d929d36cd6e11e4eac8bd0d1d Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 17 Mar 2024 13:34:49 +0100 Subject: [PATCH 035/433] Fix issue with compressed file sizes. --- OtterGui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OtterGui b/OtterGui index 1a187f75..1be9365d 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 1a187f756f2e8823197bd43db1c3383231f5eaff +Subproject commit 1be9365d048bf1da3700e8cf1df9acbe42523f5c From 038c230427f6bf8c7bd2283939b343fcc60c6d4e Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 17 Mar 2024 13:59:40 +0100 Subject: [PATCH 036/433] Rename to Predefined. --- Penumbra/Services/FilenameService.cs | 6 +- Penumbra/Services/ServiceManagerA.cs | 2 +- Penumbra/UI/Classes/Colors.cs | 8 +- Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs | 42 ++++------ Penumbra/UI/ModsTab/ModPanelEditTab.cs | 83 ++++++++----------- ...dTagManager.cs => PredefinedTagManager.cs} | 71 ++++++++-------- Penumbra/UI/Tabs/SettingsTab.cs | 22 +++-- 7 files changed, 102 insertions(+), 132 deletions(-) rename Penumbra/UI/{SharedTagManager.cs => PredefinedTagManager.cs} (83%) diff --git a/Penumbra/Services/FilenameService.cs b/Penumbra/Services/FilenameService.cs index 20794f12..2de4bff0 100644 --- a/Penumbra/Services/FilenameService.cs +++ b/Penumbra/Services/FilenameService.cs @@ -14,7 +14,7 @@ public class FilenameService(DalamudPluginInterface pi) : IService public readonly string EphemeralConfigFile = Path.Combine(pi.ConfigDirectory.FullName, "ephemeral_config.json"); public readonly string FilesystemFile = Path.Combine(pi.ConfigDirectory.FullName, "sort_order.json"); public readonly string ActiveCollectionsFile = Path.Combine(pi.ConfigDirectory.FullName, "active_collections.json"); - public readonly string SharedTagFile = Path.Combine(pi.ConfigDirectory.FullName, "shared_tags.json"); + public readonly string PredefinedTagFile = Path.Combine(pi.ConfigDirectory.FullName, "predefined_tags.json"); public readonly string CrashHandlerExe = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Penumbra.CrashHandler.exe"); @@ -44,7 +44,7 @@ public class FilenameService(DalamudPluginInterface pi) : IService get { var directory = new DirectoryInfo(CollectionDirectory); - return directory.Exists ? directory.EnumerateFiles("*.json") : Array.Empty(); + return directory.Exists ? directory.EnumerateFiles("*.json") : []; } } @@ -54,7 +54,7 @@ public class FilenameService(DalamudPluginInterface pi) : IService get { var directory = new DirectoryInfo(LocalDataDirectory); - return directory.Exists ? directory.EnumerateFiles("*.json") : Array.Empty(); + return directory.Exists ? directory.EnumerateFiles("*.json") : []; } } diff --git a/Penumbra/Services/ServiceManagerA.cs b/Penumbra/Services/ServiceManagerA.cs index cb2032a2..39ef0560 100644 --- a/Penumbra/Services/ServiceManagerA.cs +++ b/Penumbra/Services/ServiceManagerA.cs @@ -105,7 +105,7 @@ public static class ServiceManagerA private static ServiceManager AddConfiguration(this ServiceManager services) => services.AddSingleton() .AddSingleton() - .AddSingleton(); + .AddSingleton(); private static ServiceManager AddCollections(this ServiceManager services) => services.AddSingleton() diff --git a/Penumbra/UI/Classes/Colors.cs b/Penumbra/UI/Classes/Colors.cs index 50096696..4d0c62af 100644 --- a/Penumbra/UI/Classes/Colors.cs +++ b/Penumbra/UI/Classes/Colors.cs @@ -28,8 +28,8 @@ public enum ColorId ResTreePlayer, ResTreeNetworked, ResTreeNonNetworked, - SharedTagAdd, - SharedTagRemove + PredefinedTagAdd, + PredefinedTagRemove, } public static class Colors @@ -75,8 +75,8 @@ public static class Colors ColorId.ResTreePlayer => ( 0xFFC0FFC0, "On-Screen: Other Players", "Other players and what they own, in the On-Screen tab." ), ColorId.ResTreeNetworked => ( 0xFFFFFFFF, "On-Screen: Non-Players (Networked)", "Non-player entities handled by the game server, in the On-Screen tab." ), ColorId.ResTreeNonNetworked => ( 0xFFC0C0FF, "On-Screen: Non-Players (Local)", "Non-player entities handled locally, in the On-Screen tab." ), - ColorId.SharedTagAdd => ( 0xFF44AA44, "Shared Tags: Add Tag", "A shared tag that is not present on the current mod and can be added." ), - ColorId.SharedTagRemove => ( 0xFF2222AA, "Shared Tags: Remove Tag", "A shared tag that is already present on the current mod and can be removed." ), + ColorId.PredefinedTagAdd => ( 0xFF44AA44, "Predefined Tags: Add Tag", "A predefined tag that is not present on the current mod and can be added." ), + ColorId.PredefinedTagRemove => ( 0xFF2222AA, "Predefined Tags: Remove Tag", "A predefined tag that is already present on the current mod and can be removed." ), _ => throw new ArgumentOutOfRangeException( nameof( color ), color, null ), // @formatter:on }; diff --git a/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs b/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs index 5f2687c3..4c5e68ff 100644 --- a/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs @@ -7,22 +7,15 @@ using Penumbra.Mods.Manager; namespace Penumbra.UI.ModsTab; -public class ModPanelDescriptionTab : ITab +public class ModPanelDescriptionTab( + ModFileSystemSelector selector, + TutorialService tutorial, + ModManager modManager, + PredefinedTagManager predefinedTagsConfig) + : ITab { - private readonly ModFileSystemSelector _selector; - private readonly TutorialService _tutorial; - private readonly ModManager _modManager; - private readonly SharedTagManager _sharedTagManager; - private readonly TagButtons _localTags = new(); - private readonly TagButtons _modTags = new(); - - public ModPanelDescriptionTab(ModFileSystemSelector selector, TutorialService tutorial, ModManager modManager, SharedTagManager sharedTagsConfig) - { - _selector = selector; - _tutorial = tutorial; - _modManager = modManager; - _sharedTagManager = sharedTagsConfig; - } + private readonly TagButtons _localTags = new(); + private readonly TagButtons _modTags = new(); public ReadOnlySpan Label => "Description"u8; @@ -36,29 +29,28 @@ public class ModPanelDescriptionTab : ITab ImGui.Dummy(ImGuiHelpers.ScaledVector2(2)); ImGui.Dummy(ImGuiHelpers.ScaledVector2(2)); - var sharedTagsEnabled = _sharedTagManager.SharedTags.Count > 0; + var sharedTagsEnabled = predefinedTagsConfig.SharedTags.Count > 0; var sharedTagButtonOffset = sharedTagsEnabled ? ImGui.GetFrameHeight() + ImGui.GetStyle().FramePadding.X : 0; var tagIdx = _localTags.Draw("Local Tags: ", "Custom tags you can set personally that will not be exported to the mod data but only set for you.\n" - + "If the mod already contains a local tag in its own tags, the local tag will be ignored.", _selector.Selected!.LocalTags, + + "If the mod already contains a local tag in its own tags, the local tag will be ignored.", selector.Selected!.LocalTags, out var editedTag, rightEndOffset: sharedTagButtonOffset); - _tutorial.OpenTutorial(BasicTutorialSteps.Tags); + tutorial.OpenTutorial(BasicTutorialSteps.Tags); if (tagIdx >= 0) - _modManager.DataEditor.ChangeLocalTag(_selector.Selected!, tagIdx, editedTag); + modManager.DataEditor.ChangeLocalTag(selector.Selected!, tagIdx, editedTag); if (sharedTagsEnabled) - { - _sharedTagManager.DrawAddFromSharedTagsAndUpdateTags(_selector.Selected!.LocalTags, _selector.Selected!.ModTags, true, _selector.Selected!); - } + predefinedTagsConfig.DrawAddFromSharedTagsAndUpdateTags(selector.Selected!.LocalTags, selector.Selected!.ModTags, true, + selector.Selected!); - if (_selector.Selected!.ModTags.Count > 0) + if (selector.Selected!.ModTags.Count > 0) _modTags.Draw("Mod Tags: ", "Tags assigned by the mod creator and saved with the mod data. To edit these, look at Edit Mod.", - _selector.Selected!.ModTags, out var _, false, + selector.Selected!.ModTags, out _, false, ImGui.CalcTextSize("Local ").X - ImGui.CalcTextSize("Mod ").X); ImGui.Dummy(ImGuiHelpers.ScaledVector2(2)); ImGui.Separator(); - ImGuiUtil.TextWrapped(_selector.Selected!.Description); + ImGuiUtil.TextWrapped(selector.Selected!.Description); } } diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index 9b4a582f..275c89ef 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -17,18 +17,20 @@ using Penumbra.UI.AdvancedWindow; namespace Penumbra.UI.ModsTab; -public class ModPanelEditTab : ITab +public class ModPanelEditTab( + ModManager modManager, + ModFileSystemSelector selector, + ModFileSystem fileSystem, + Services.MessageService messager, + ModEditWindow editWindow, + ModEditor editor, + FilenameService filenames, + ModExportManager modExportManager, + Configuration config, + PredefinedTagManager predefinedTagManager) + : ITab { - private readonly Services.MessageService _messager; - private readonly FilenameService _filenames; - private readonly ModManager _modManager; - private readonly ModExportManager _modExportManager; - private readonly ModFileSystem _fileSystem; - private readonly ModFileSystemSelector _selector; - private readonly ModEditWindow _editWindow; - private readonly ModEditor _editor; - private readonly Configuration _config; - private readonly SharedTagManager _sharedTagManager; + private readonly ModManager _modManager = modManager; private readonly TagButtons _modTags = new(); @@ -37,22 +39,6 @@ public class ModPanelEditTab : ITab private ModFileSystem.Leaf _leaf = null!; private Mod _mod = null!; - public ModPanelEditTab(ModManager modManager, ModFileSystemSelector selector, ModFileSystem fileSystem, Services.MessageService messager, - ModEditWindow editWindow, ModEditor editor, FilenameService filenames, ModExportManager modExportManager, Configuration config, - SharedTagManager sharedTagManager) - { - _modManager = modManager; - _selector = selector; - _fileSystem = fileSystem; - _messager = messager; - _editWindow = editWindow; - _editor = editor; - _filenames = filenames; - _modExportManager = modExportManager; - _config = config; - _sharedTagManager = sharedTagManager; - } - public ReadOnlySpan Label => "Edit Mod"u8; @@ -62,8 +48,8 @@ public class ModPanelEditTab : ITab if (!child) return; - _leaf = _selector.SelectedLeaf!; - _mod = _selector.Selected!; + _leaf = selector.SelectedLeaf!; + _mod = selector.Selected!; _cellPadding = ImGui.GetStyle().CellPadding with { X = 2 * UiHelpers.Scale }; _itemSpacing = ImGui.GetStyle().CellPadding with { X = 4 * UiHelpers.Scale }; @@ -75,15 +61,15 @@ public class ModPanelEditTab : ITab if (Input.Text("Mod Path", Input.Path, Input.None, _leaf.FullName(), out var newPath, 256, UiHelpers.InputTextWidth.X)) try { - _fileSystem.RenameAndMove(_leaf, newPath); + fileSystem.RenameAndMove(_leaf, newPath); } catch (Exception e) { - _messager.NotificationMessage(e.Message, NotificationType.Warning, false); + messager.NotificationMessage(e.Message, NotificationType.Warning, false); } UiHelpers.DefaultLineSpace(); - var sharedTagsEnabled = _sharedTagManager.SharedTags.Count > 0; + var sharedTagsEnabled = predefinedTagManager.SharedTags.Count > 0; var sharedTagButtonOffset = sharedTagsEnabled ? ImGui.GetFrameHeight() + ImGui.GetStyle().FramePadding.X : 0; var tagIdx = _modTags.Draw("Mod Tags: ", "Edit tags by clicking them, or add new tags. Empty tags are removed.", _mod.ModTags, out var editedTag, rightEndOffset: sharedTagButtonOffset); @@ -91,12 +77,11 @@ public class ModPanelEditTab : ITab _modManager.DataEditor.ChangeModTag(_mod, tagIdx, editedTag); if (sharedTagsEnabled) - { - _sharedTagManager.DrawAddFromSharedTagsAndUpdateTags(_selector.Selected!.LocalTags, _selector.Selected!.ModTags, false, _selector.Selected!); - } + predefinedTagManager.DrawAddFromSharedTagsAndUpdateTags(selector.Selected!.LocalTags, selector.Selected!.ModTags, false, + selector.Selected!); UiHelpers.DefaultLineSpace(); - AddOptionGroup.Draw(_filenames, _modManager, _mod, _config.ReplaceNonAsciiOnImport); + AddOptionGroup.Draw(filenames, _modManager, _mod, config.ReplaceNonAsciiOnImport); UiHelpers.DefaultLineSpace(); for (var groupIdx = 0; groupIdx < _mod.Groups.Count; ++groupIdx) @@ -144,11 +129,11 @@ public class ModPanelEditTab : ITab { if (ImGui.Button("Update Bibo Material", buttonSize)) { - _editor.LoadMod(_mod); - _editor.MdlMaterialEditor.ReplaceAllMaterials("bibo", "b"); - _editor.MdlMaterialEditor.ReplaceAllMaterials("bibopube", "c"); - _editor.MdlMaterialEditor.SaveAllModels(_editor.Compactor); - _editWindow.UpdateModels(); + editor.LoadMod(_mod); + editor.MdlMaterialEditor.ReplaceAllMaterials("bibo", "b"); + editor.MdlMaterialEditor.ReplaceAllMaterials("bibopube", "c"); + editor.MdlMaterialEditor.SaveAllModels(editor.Compactor); + editWindow.UpdateModels(); } ImGuiUtil.HoverTooltip( @@ -160,7 +145,7 @@ public class ModPanelEditTab : ITab private void BackupButtons(Vector2 buttonSize) { - var backup = new ModBackup(_modExportManager, _mod); + var backup = new ModBackup(modExportManager, _mod); var tt = ModBackup.CreatingBackup ? "Already exporting a mod." : backup.Exists @@ -171,16 +156,16 @@ public class ModPanelEditTab : ITab ImGui.SameLine(); tt = backup.Exists - ? $"Delete existing mod export \"{backup.Name}\" (hold {_config.DeleteModModifier} while clicking)." + ? $"Delete existing mod export \"{backup.Name}\" (hold {config.DeleteModModifier} while clicking)." : $"Exported mod \"{backup.Name}\" does not exist."; - if (ImGuiUtil.DrawDisabledButton("Delete Export", buttonSize, tt, !backup.Exists || !_config.DeleteModModifier.IsActive())) + if (ImGuiUtil.DrawDisabledButton("Delete Export", buttonSize, tt, !backup.Exists || !config.DeleteModModifier.IsActive())) backup.Delete(); tt = backup.Exists - ? $"Restore mod from exported file \"{backup.Name}\" (hold {_config.DeleteModModifier} while clicking)." + ? $"Restore mod from exported file \"{backup.Name}\" (hold {config.DeleteModModifier} while clicking)." : $"Exported mod \"{backup.Name}\" does not exist."; ImGui.SameLine(); - if (ImGuiUtil.DrawDisabledButton("Restore From Export", buttonSize, tt, !backup.Exists || !_config.DeleteModModifier.IsActive())) + if (ImGuiUtil.DrawDisabledButton("Restore From Export", buttonSize, tt, !backup.Exists || !config.DeleteModModifier.IsActive())) backup.Restore(_modManager); if (backup.Exists) { @@ -218,13 +203,13 @@ public class ModPanelEditTab : ITab _delayedActions.Enqueue(() => DescriptionEdit.OpenPopup(_mod, Input.Description)); ImGui.SameLine(); - var fileExists = File.Exists(_filenames.ModMetaPath(_mod)); + var fileExists = File.Exists(filenames.ModMetaPath(_mod)); var tt = fileExists ? "Open the metadata json file in the text editor of your choice." : "The metadata json file does not exist."; if (ImGuiUtil.DrawDisabledButton($"{FontAwesomeIcon.FileExport.ToIconString()}##metaFile", UiHelpers.IconButtonSize, tt, !fileExists, true)) - Process.Start(new ProcessStartInfo(_filenames.ModMetaPath(_mod)) { UseShellExecute = true }); + Process.Start(new ProcessStartInfo(filenames.ModMetaPath(_mod)) { UseShellExecute = true }); } /// Do some edits outside of iterations. @@ -448,7 +433,7 @@ public class ModPanelEditTab : ITab _delayedActions.Enqueue(() => DescriptionEdit.OpenPopup(_mod, groupIdx)); ImGui.SameLine(); - var fileName = _filenames.OptionGroupFile(_mod, groupIdx, _config.ReplaceNonAsciiOnImport); + var fileName = filenames.OptionGroupFile(_mod, groupIdx, config.ReplaceNonAsciiOnImport); var fileExists = File.Exists(fileName); tt = fileExists ? $"Open the {group.Name} json file in the text editor of your choice." diff --git a/Penumbra/UI/SharedTagManager.cs b/Penumbra/UI/PredefinedTagManager.cs similarity index 83% rename from Penumbra/UI/SharedTagManager.cs rename to Penumbra/UI/PredefinedTagManager.cs index 23196319..fafca101 100644 --- a/Penumbra/UI/SharedTagManager.cs +++ b/Penumbra/UI/PredefinedTagManager.cs @@ -1,6 +1,5 @@ using Dalamud.Interface; using Dalamud.Interface.Internal.Notifications; -using Dalamud.Utility; using ImGuiNET; using Newtonsoft.Json; using OtterGui; @@ -12,44 +11,45 @@ using Penumbra.UI.Classes; using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs; namespace Penumbra.UI; -public sealed class SharedTagManager : ISavable + +public sealed class PredefinedTagManager : ISavable { - private readonly ModManager _modManager; + private readonly ModManager _modManager; private readonly SaveService _saveService; - private static uint _tagButtonAddColor = ColorId.SharedTagAdd.Value(); - private static uint _tagButtonRemoveColor = ColorId.SharedTagRemove.Value(); + private static uint _tagButtonAddColor = ColorId.PredefinedTagAdd.Value(); + private static uint _tagButtonRemoveColor = ColorId.PredefinedTagRemove.Value(); private static float _minTagButtonWidth = 15; private const string PopupContext = "SharedTagsPopup"; - private bool _isPopupOpen = false; + private bool _isPopupOpen = false; // Operations on this list assume that it is sorted and will keep it sorted if that is the case. // The list also gets re-sorted when first loaded from config in case the config was modified. [JsonRequired] private readonly List _sharedTags = []; + [JsonIgnore] - public IReadOnlyList SharedTags => _sharedTags; + public IReadOnlyList SharedTags + => _sharedTags; public int ConfigVersion = 1; - public SharedTagManager(ModManager modManager, SaveService saveService) + public PredefinedTagManager(ModManager modManager, SaveService saveService) { - _modManager = modManager; + _modManager = modManager; _saveService = saveService; Load(); } public string ToFilename(FilenameService fileNames) - { - return fileNames.SharedTagFile; - } + => fileNames.PredefinedTagFile; public void Save(StreamWriter writer) { - using var jWriter = new JsonTextWriter(writer) { Formatting = Formatting.Indented }; - var serializer = new JsonSerializer { Formatting = Formatting.Indented }; + using var jWriter = new JsonTextWriter(writer) { Formatting = Formatting.Indented }; + var serializer = new JsonSerializer { Formatting = Formatting.Indented }; serializer.Serialize(jWriter, this); } @@ -65,12 +65,12 @@ public sealed class SharedTagManager : ISavable errorArgs.ErrorContext.Handled = true; } - if (!File.Exists(_saveService.FileNames.SharedTagFile)) + if (!File.Exists(_saveService.FileNames.PredefinedTagFile)) return; try { - var text = File.ReadAllText(_saveService.FileNames.SharedTagFile); + var text = File.ReadAllText(_saveService.FileNames.PredefinedTagFile); JsonConvert.PopulateObject(text, this, new JsonSerializerSettings { Error = HandleDeserializationError, @@ -94,9 +94,7 @@ public sealed class SharedTagManager : ISavable // In the case of editing a tag, remove what's there prior to doing an insert. if (tagIdx != SharedTags.Count) - { _sharedTags.RemoveAt(tagIdx); - } if (!string.IsNullOrEmpty(tag)) { @@ -109,7 +107,8 @@ public sealed class SharedTagManager : ISavable Save(); } - public void DrawAddFromSharedTagsAndUpdateTags(IReadOnlyCollection localTags, IReadOnlyCollection modTags, bool editLocal, Mods.Mod mod) + public void DrawAddFromSharedTagsAndUpdateTags(IReadOnlyCollection localTags, IReadOnlyCollection modTags, bool editLocal, + Mods.Mod mod) { ImGui.SetCursorPosY(ImGui.GetCursorPosY() - ImGui.GetFrameHeightWithSpacing()); ImGui.SetCursorPosX(ImGui.GetWindowWidth() - ImGui.GetFrameHeight() - ImGui.GetStyle().FramePadding.X); @@ -131,7 +130,8 @@ public sealed class SharedTagManager : ISavable { _modManager.DataEditor.ChangeLocalTag(mod, index, string.Empty); } - } else + } + else { if (index < 0) { @@ -143,15 +143,16 @@ public sealed class SharedTagManager : ISavable _modManager.DataEditor.ChangeModTag(mod, index, string.Empty); } } - } } public string DrawAddFromSharedTags(IReadOnlyCollection localTags, IReadOnlyCollection modTags, bool editLocal) { var tagToAdd = string.Empty; - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Tags.ToIconString(), new Vector2(ImGui.GetFrameHeight()), "Add Shared Tag... (Right-click to close popup)", - false, true) || _isPopupOpen) + if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Tags.ToIconString(), new Vector2(ImGui.GetFrameHeight()), + "Add Shared Tag... (Right-click to close popup)", + false, true) + || _isPopupOpen) return DrawSharedTagsPopup(localTags, modTags, editLocal); return tagToAdd; @@ -167,9 +168,9 @@ public sealed class SharedTagManager : ISavable } var display = ImGui.GetIO().DisplaySize; - var height = Math.Min(display.Y / 4, 10 * ImGui.GetFrameHeightWithSpacing()); - var width = display.X / 6; - var size = new Vector2(width, height); + var height = Math.Min(display.Y / 4, 10 * ImGui.GetFrameHeightWithSpacing()); + var width = display.X / 6; + var size = new Vector2(width, height); ImGui.SetNextWindowSize(size); using var popup = ImRaii.Popup(PopupContext); if (!popup) @@ -182,26 +183,23 @@ public sealed class SharedTagManager : ISavable foreach (var (tag, idx) in SharedTags.WithIndex()) { if (DrawColoredButton(localTags, modTags, tag, editLocal, idx)) - { selected = tag; - } ImGui.SameLine(); } if (ImGui.IsMouseClicked(ImGuiMouseButton.Right)) - { _isPopupOpen = false; - } return selected; } - private static bool DrawColoredButton(IReadOnlyCollection localTags, IReadOnlyCollection modTags, string buttonLabel, bool editLocal, int index) + private static bool DrawColoredButton(IReadOnlyCollection localTags, IReadOnlyCollection modTags, string buttonLabel, + bool editLocal, int index) { var ret = false; var isLocalTagPresent = localTags.Contains(buttonLabel); - var isModTagPresent = modTags.Contains(buttonLabel); + var isModTagPresent = modTags.Contains(buttonLabel); var buttonWidth = CalcTextButtonWidth(buttonLabel); // Would prefer to be able to fit at least 2 buttons per line so the popup doesn't look sparse with lots of long tags. Thus long tags will be trimmed. @@ -210,7 +208,7 @@ public sealed class SharedTagManager : ISavable if (buttonWidth >= maxButtonWidth) { displayedLabel = TrimButtonTextToWidth(buttonLabel, maxButtonWidth); - buttonWidth = CalcTextButtonWidth(displayedLabel); + buttonWidth = CalcTextButtonWidth(displayedLabel); } // Prevent adding a new tag past the right edge of the popup @@ -247,9 +245,8 @@ public sealed class SharedTagManager : ISavable // An ellipsis will be used to indicate trimmed tags if (CalcTextButtonWidth(nextTrim + "...") < maxWidth) - { return nextTrim + "..."; - } + trimmedText = nextTrim; } @@ -257,7 +254,5 @@ public sealed class SharedTagManager : ISavable } private static float CalcTextButtonWidth(string text) - { - return ImGui.CalcTextSize(text).X + 2 * ImGui.GetStyle().FramePadding.X; - } + => ImGui.CalcTextSize(text).X + 2 * ImGui.GetStyle().FramePadding.X; } diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index b311bb93..c36c63b2 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -42,7 +42,7 @@ public class SettingsTab : ITab private readonly DalamudConfigService _dalamudConfig; private readonly DalamudPluginInterface _pluginInterface; private readonly IDataManager _gameData; - private readonly SharedTagManager _sharedTagManager; + private readonly PredefinedTagManager _predefinedTagManager; private int _minimumX = int.MaxValue; private int _minimumY = int.MaxValue; @@ -53,7 +53,7 @@ public class SettingsTab : ITab Penumbra penumbra, FileDialogService fileDialog, ModManager modManager, ModFileSystemSelector selector, CharacterUtility characterUtility, ResidentResourceManager residentResources, ModExportManager modExportManager, HttpApi httpApi, DalamudSubstitutionProvider dalamudSubstitutionProvider, FileCompactor compactor, DalamudConfigService dalamudConfig, - IDataManager gameData, SharedTagManager sharedTagConfig) + IDataManager gameData, PredefinedTagManager predefinedTagConfig) { _pluginInterface = pluginInterface; _config = config; @@ -73,7 +73,7 @@ public class SettingsTab : ITab _gameData = gameData; if (_compactor.CanCompact) _compactor.Enabled = _config.UseFileSystemCompression; - _sharedTagManager = sharedTagConfig; + _predefinedTagManager = predefinedTagConfig; } public void DrawHeader() @@ -101,7 +101,7 @@ public class SettingsTab : ITab DrawGeneralSettings(); DrawColorSettings(); DrawAdvancedSettings(); - DrawSharedTagsSection(); + DrawPredefinedTagsSection(); DrawSupportButtons(); } @@ -239,7 +239,7 @@ public class SettingsTab : ITab } var selected = ImGui.IsItemActive(); - using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, new Vector2(UiHelpers.ScaleX3, 0)); + using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, new Vector2(UiHelpers.ScaleX3, 0)); ImGui.SameLine(); DrawDirectoryPickerButton(); style.Pop(); @@ -388,7 +388,7 @@ public class SettingsTab : ITab "Hide the Penumbra main window when you manually hide the in-game user interface.", _config.HideUiWhenUiHidden, v => { - _config.HideUiWhenUiHidden = v; + _config.HideUiWhenUiHidden = v; _pluginInterface.UiBuilder.DisableUserUiHide = !v; }); Checkbox("Hide Config Window when in Cutscenes", @@ -917,18 +917,16 @@ public class SettingsTab : ITab _penumbra.ForceChangelogOpen(); } - private void DrawSharedTagsSection() + private void DrawPredefinedTagsSection() { if (!ImGui.CollapsingHeader("Tags")) return; - var tagIdx = _sharedTags.Draw("Shared Tags: ", - "Predefined tags that can be added or removed from mods with a single click.", _sharedTagManager.SharedTags, + var tagIdx = _sharedTags.Draw("Predefined Tags: ", + "Predefined tags that can be added or removed from mods with a single click.", _predefinedTagManager.SharedTags, out var editedTag); if (tagIdx >= 0) - { - _sharedTagManager.ChangeSharedTag(tagIdx, editedTag); - } + _predefinedTagManager.ChangeSharedTag(tagIdx, editedTag); } } From 814aa92e19e7a971ef12c2950c0cff023d3e0b8d Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 17 Mar 2024 14:08:14 +0100 Subject: [PATCH 037/433] Move tags before advanced. --- Penumbra/UI/Tabs/SettingsTab.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index c36c63b2..60c18d5f 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -100,8 +100,8 @@ public class SettingsTab : ITab DrawGeneralSettings(); DrawColorSettings(); - DrawAdvancedSettings(); DrawPredefinedTagsSection(); + DrawAdvancedSettings(); DrawSupportButtons(); } From 19526dd92d4361fe7eebbeb1bc6fe0844be7a8c0 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 17 Mar 2024 14:09:01 +0100 Subject: [PATCH 038/433] Help marker position. --- OtterGui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OtterGui b/OtterGui index 1be9365d..cf42043c 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 1be9365d048bf1da3700e8cf1df9acbe42523f5c +Subproject commit cf42043c2b0e76b59919688dc250a762fe52d4b1 From 0ec440c388e0667e73069fa0cc8ae2216e7972a9 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 18 Mar 2024 16:41:40 +0100 Subject: [PATCH 039/433] Fix reloading mods not fixing settings, add some messages on IPC. --- Penumbra/Api/PenumbraApi.cs | 93 +++++++++++++++---- .../Collections/Manager/CollectionStorage.cs | 8 ++ Penumbra/Mods/Subclasses/ModSettings.cs | 16 ++++ Penumbra/UI/ModsTab/ModPanelSettingsTab.cs | 9 +- 4 files changed, 103 insertions(+), 23 deletions(-) diff --git a/Penumbra/Api/PenumbraApi.cs b/Penumbra/Api/PenumbraApi.cs index aed1a963..f5bb67bd 100644 --- a/Penumbra/Api/PenumbraApi.cs +++ b/Penumbra/Api/PenumbraApi.cs @@ -10,6 +10,7 @@ using Penumbra.Meta.Manipulations; using Penumbra.Mods; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Compression; +using OtterGui.Log; using Penumbra.Api.Enums; using Penumbra.GameData.Actors; using Penumbra.Interop.ResourceLoading; @@ -642,10 +643,10 @@ public class PenumbraApi : IDisposable, IPenumbraApi { CheckInitialized(); if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) - return PenumbraApiEc.ModMissing; + return Return(PenumbraApiEc.ModMissing, Args("ModDirectory", modDirectory, "ModName", modName)); _modManager.ReloadMod(mod); - return PenumbraApiEc.Success; + return Return(PenumbraApiEc.Success, Args("ModDirectory", modDirectory, "ModName", modName)); } public PenumbraApiEc InstallMod(string modFilePackagePath) @@ -653,11 +654,11 @@ public class PenumbraApi : IDisposable, IPenumbraApi if (File.Exists(modFilePackagePath)) { _modImportManager.AddUnpack(modFilePackagePath); - return PenumbraApiEc.Success; + return Return(PenumbraApiEc.Success, Args("ModFilePackagePath", modFilePackagePath)); } else { - return PenumbraApiEc.FileMissing; + return Return(PenumbraApiEc.FileMissing, Args("ModFilePackagePath", modFilePackagePath)); } } @@ -666,23 +667,24 @@ public class PenumbraApi : IDisposable, IPenumbraApi CheckInitialized(); var dir = new DirectoryInfo(Path.Join(_modManager.BasePath.FullName, Path.GetFileName(modDirectory))); if (!dir.Exists) - return PenumbraApiEc.FileMissing; + return Return(PenumbraApiEc.FileMissing, Args("ModDirectory", modDirectory)); + _modManager.AddMod(dir); if (_config.UseFileSystemCompression) new FileCompactor(Penumbra.Log).StartMassCompact(dir.EnumerateFiles("*.*", SearchOption.AllDirectories), CompressionAlgorithm.Xpress8K); - return PenumbraApiEc.Success; + return Return(PenumbraApiEc.Success, Args("ModDirectory", modDirectory)); } public PenumbraApiEc DeleteMod(string modDirectory, string modName) { CheckInitialized(); if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) - return PenumbraApiEc.NothingChanged; + return Return(PenumbraApiEc.NothingChanged, Args("ModDirectory", modDirectory, "ModName", modName)); _modManager.DeleteMod(mod); - return PenumbraApiEc.Success; + return Return(PenumbraApiEc.Success, Args("ModDirectory", modDirectory, "ModName", modName)); } public event Action? ModDeleted; @@ -784,22 +786,33 @@ public class PenumbraApi : IDisposable, IPenumbraApi { CheckInitialized(); if (!_collectionManager.Storage.ByName(collectionName, out var collection)) - return PenumbraApiEc.CollectionMissing; + return Return(PenumbraApiEc.CollectionMissing, + Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, + "OptionName", optionName)); if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) - return PenumbraApiEc.ModMissing; + return Return(PenumbraApiEc.ModMissing, + Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, + "OptionName", optionName)); var groupIdx = mod.Groups.IndexOf(g => g.Name == optionGroupName); if (groupIdx < 0) - return PenumbraApiEc.OptionGroupMissing; + return Return(PenumbraApiEc.OptionGroupMissing, + Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, + "OptionName", optionName)); var optionIdx = mod.Groups[groupIdx].IndexOf(o => o.Name == optionName); if (optionIdx < 0) - return PenumbraApiEc.OptionMissing; + return Return(PenumbraApiEc.OptionMissing, + Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, + "OptionName", optionName)); var setting = mod.Groups[groupIdx].Type == GroupType.Multi ? 1u << optionIdx : (uint)optionIdx; - return _collectionEditor.SetModSetting(collection, mod, groupIdx, setting) ? PenumbraApiEc.Success : PenumbraApiEc.NothingChanged; + return Return( + _collectionEditor.SetModSetting(collection, mod, groupIdx, setting) ? PenumbraApiEc.Success : PenumbraApiEc.NothingChanged, + Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, + "OptionName", optionName)); } public PenumbraApiEc TrySetModSettings(string collectionName, string modDirectory, string modName, string optionGroupName, @@ -807,14 +820,20 @@ public class PenumbraApi : IDisposable, IPenumbraApi { CheckInitialized(); if (!_collectionManager.Storage.ByName(collectionName, out var collection)) - return PenumbraApiEc.CollectionMissing; + return Return(PenumbraApiEc.CollectionMissing, + Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, + "#optionNames", optionNames.Count.ToString())); if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) - return PenumbraApiEc.ModMissing; + return Return(PenumbraApiEc.ModMissing, + Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, + "#optionNames", optionNames.Count.ToString())); var groupIdx = mod.Groups.IndexOf(g => g.Name == optionGroupName); if (groupIdx < 0) - return PenumbraApiEc.OptionGroupMissing; + return Return(PenumbraApiEc.OptionGroupMissing, + Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, + "#optionNames", optionNames.Count.ToString())); var group = mod.Groups[groupIdx]; @@ -823,7 +842,9 @@ public class PenumbraApi : IDisposable, IPenumbraApi { var optionIdx = optionNames.Count == 0 ? -1 : group.IndexOf(o => o.Name == optionNames[^1]); if (optionIdx < 0) - return PenumbraApiEc.OptionMissing; + return Return(PenumbraApiEc.OptionMissing, + Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, + "#optionNames", optionNames.Count.ToString())); setting = (uint)optionIdx; } @@ -833,13 +854,18 @@ public class PenumbraApi : IDisposable, IPenumbraApi { var optionIdx = group.IndexOf(o => o.Name == name); if (optionIdx < 0) - return PenumbraApiEc.OptionMissing; + return Return(PenumbraApiEc.OptionMissing, + Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", + optionGroupName, "#optionNames", optionNames.Count.ToString())); setting |= 1u << optionIdx; } } - return _collectionEditor.SetModSetting(collection, mod, groupIdx, setting) ? PenumbraApiEc.Success : PenumbraApiEc.NothingChanged; + return Return( + _collectionEditor.SetModSetting(collection, mod, groupIdx, setting) ? PenumbraApiEc.Success : PenumbraApiEc.NothingChanged, + Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, + "#optionNames", optionNames.Count.ToString())); } @@ -1296,4 +1322,33 @@ public class PenumbraApi : IDisposable, IPenumbraApi if (settings is { Enabled: true }) ModSettingChanged?.Invoke(ModSettingChange.Edited, collection.Name, mod.Identifier, parent != collection); } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + private static LazyString Args(params string[] arguments) + { + if (arguments.Length == 0) + return new LazyString(() => "no arguments"); + + return new LazyString(() => + { + var sb = new StringBuilder(); + for (var i = 0; i < arguments.Length / 2; ++i) + { + sb.Append(arguments[2 * i]); + sb.Append(" = "); + sb.Append(arguments[2 * i + 1]); + sb.Append(", "); + } + + return sb.ToString(0, sb.Length - 2); + }); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + private static PenumbraApiEc Return(PenumbraApiEc ec, LazyString args, [CallerMemberName] string name = "Unknown") + { + Penumbra.Log.Debug( + $"[{name}] Called with {args}, returned {ec}."); + return ec; + } } diff --git a/Penumbra/Collections/Manager/CollectionStorage.cs b/Penumbra/Collections/Manager/CollectionStorage.cs index a84c79e6..0ee55376 100644 --- a/Penumbra/Collections/Manager/CollectionStorage.cs +++ b/Penumbra/Collections/Manager/CollectionStorage.cs @@ -265,6 +265,14 @@ public class CollectionStorage : IReadOnlyList, IDisposable foreach (var collection in this.Where(collection => collection.Settings[mod.Index] != null)) _saveService.QueueSave(new ModCollectionSave(_modStorage, collection)); break; + case ModPathChangeType.Reloaded: + foreach (var collection in this) + { + if (collection.Settings[mod.Index]?.FixAllSettings(mod) ?? false) + _saveService.QueueSave(new ModCollectionSave(_modStorage, collection)); + } + + break; } } diff --git a/Penumbra/Mods/Subclasses/ModSettings.cs b/Penumbra/Mods/Subclasses/ModSettings.cs index a20cb9cb..ed8ad84e 100644 --- a/Penumbra/Mods/Subclasses/ModSettings.cs +++ b/Penumbra/Mods/Subclasses/ModSettings.cs @@ -138,6 +138,22 @@ public class ModSettings } } + public bool FixAllSettings(Mod mod) + { + var ret = false; + for (var i = 0; i < Settings.Count; ++i) + { + var newValue = FixSetting(mod.Groups[i], Settings[i]); + if (newValue != Settings[i]) + { + ret = true; + Settings[i] = newValue; + } + } + + return AddMissingSettings(mod) || ret; + } + // Ensure that a value is valid for a group. private static uint FixSetting(IModGroup group, uint value) => group.Type switch diff --git a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs index 195c07d6..5c7ddbf3 100644 --- a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs @@ -211,6 +211,11 @@ public class ModPanelSettingsTab : ITab var selectedOption = _empty ? (int)group.DefaultSettings : (int)_settings.Settings[groupIdx]; var minWidth = Widget.BeginFramedGroup(group.Name, description:group.Description); + DrawCollapseHandling(group, minWidth, DrawOptions); + + Widget.EndFramedGroup(); + return; + void DrawOptions() { for (var idx = 0; idx < group.Count; ++idx) @@ -227,10 +232,6 @@ public class ModPanelSettingsTab : ITab ImGuiComponents.HelpMarker(option.Description); } } - - DrawCollapseHandling(group, minWidth, DrawOptions); - - Widget.EndFramedGroup(); } From 9f7b95746dff2f83a82e810276bf48f9f3f93a2f Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 18 Mar 2024 16:50:26 +0100 Subject: [PATCH 040/433] Use default task scheduler. --- OtterGui | 2 +- Penumbra.GameData | 2 +- Penumbra/Collections/Manager/IndividualCollections.Files.cs | 2 +- Penumbra/Import/Models/ModelManager.cs | 4 ++-- Penumbra/Import/TexToolsImport.cs | 4 ++-- Penumbra/Import/Textures/TextureManager.cs | 2 +- Penumbra/Mods/Manager/ModCacheManager.cs | 2 +- Penumbra/Services/ServiceWrapper.cs | 2 +- Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs | 6 +++--- Penumbra/UI/AdvancedWindow/ModEditWindow.Textures.cs | 2 +- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 2 +- Penumbra/UI/CollectionTab/IndividualAssignmentUi.cs | 2 +- 12 files changed, 16 insertions(+), 16 deletions(-) diff --git a/OtterGui b/OtterGui index 1be9365d..5a2e12a1 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 1be9365d048bf1da3700e8cf1df9acbe42523f5c +Subproject commit 5a2e12a1acd6760a3a592447a92215135e79197c diff --git a/Penumbra.GameData b/Penumbra.GameData index c0c7eb0d..c39f683d 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit c0c7eb0dedb32ea83b019626abba041e90a95319 +Subproject commit c39f683d65d4541e9f97ed4ea1abcb10e8ca5690 diff --git a/Penumbra/Collections/Manager/IndividualCollections.Files.cs b/Penumbra/Collections/Manager/IndividualCollections.Files.cs index dc20da1e..21a8cf8a 100644 --- a/Penumbra/Collections/Manager/IndividualCollections.Files.cs +++ b/Penumbra/Collections/Manager/IndividualCollections.Files.cs @@ -41,7 +41,7 @@ public partial class IndividualCollections saver.ImmediateSave(parent); IsLoaded = true; Loaded.Invoke(); - }); + }, TaskScheduler.Default); return false; } diff --git a/Penumbra/Import/Models/ModelManager.cs b/Penumbra/Import/Models/ModelManager.cs index 2c341c8b..1a52c4dd 100644 --- a/Penumbra/Import/Models/ModelManager.cs +++ b/Penumbra/Import/Models/ModelManager.cs @@ -162,7 +162,7 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect { return _tasks.TryRemove(a, out var unused); } - }, CancellationToken.None); + }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.Default); return (t, token); }).Item1; } @@ -178,7 +178,7 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect throw task.Exception; return process(action); - }); + }, TaskScheduler.Default); private class ExportToGltfAction( ModelManager manager, diff --git a/Penumbra/Import/TexToolsImport.cs b/Penumbra/Import/TexToolsImport.cs index 3f3304b8..bb006d8d 100644 --- a/Penumbra/Import/TexToolsImport.cs +++ b/Penumbra/Import/TexToolsImport.cs @@ -46,12 +46,12 @@ public partial class TexToolsImporter : IDisposable ExtractedMods = new List<(FileInfo, DirectoryInfo?, Exception?)>(count); _token = _cancellation.Token; Task.Run(ImportFiles, _token) - .ContinueWith(_ => CloseStreams()) + .ContinueWith(_ => CloseStreams(), TaskScheduler.Default) .ContinueWith(_ => { foreach (var (file, dir, error) in ExtractedMods) handler(file, dir, error); - }); + }, TaskScheduler.Default); } private void CloseStreams() diff --git a/Penumbra/Import/Textures/TextureManager.cs b/Penumbra/Import/Textures/TextureManager.cs index 5653d760..976bc179 100644 --- a/Penumbra/Import/Textures/TextureManager.cs +++ b/Penumbra/Import/Textures/TextureManager.cs @@ -64,7 +64,7 @@ public sealed class TextureManager : SingleTaskQueue, IDisposable { var token = new CancellationTokenSource(); var task = Enqueue(a, token.Token); - task.ContinueWith(_ => _tasks.TryRemove(a, out var unused), CancellationToken.None); + task.ContinueWith(_ => _tasks.TryRemove(a, out var unused), CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.Default); return (task, token); }).Item1; } diff --git a/Penumbra/Mods/Manager/ModCacheManager.cs b/Penumbra/Mods/Manager/ModCacheManager.cs index a95567ce..99ad1a4f 100644 --- a/Penumbra/Mods/Manager/ModCacheManager.cs +++ b/Penumbra/Mods/Manager/ModCacheManager.cs @@ -23,7 +23,7 @@ public class ModCacheManager : IDisposable _communicator.ModPathChanged.Subscribe(OnModPathChange, ModPathChanged.Priority.ModCacheManager); _communicator.ModDataChanged.Subscribe(OnModDataChange, ModDataChanged.Priority.ModCacheManager); _communicator.ModDiscoveryFinished.Subscribe(OnModDiscoveryFinished, ModDiscoveryFinished.Priority.ModCacheManager); - identifier.Awaiter.ContinueWith(_ => OnIdentifierCreation()); + identifier.Awaiter.ContinueWith(_ => OnIdentifierCreation(), TaskScheduler.Default); OnModDiscoveryFinished(); } diff --git a/Penumbra/Services/ServiceWrapper.cs b/Penumbra/Services/ServiceWrapper.cs index 37acdfd0..e321b35c 100644 --- a/Penumbra/Services/ServiceWrapper.cs +++ b/Penumbra/Services/ServiceWrapper.cs @@ -74,7 +74,7 @@ public abstract class AsyncServiceWrapper : IDisposable { if (!_isDisposed) FinishedCreation?.Invoke(); - }, null); + }, TaskScheduler.Default); } public void Dispose() diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs index 637c8401..cca8fe10 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs @@ -92,7 +92,7 @@ public partial class ModEditWindow .ToList(); }); - task.ContinueWith(t => { GamePaths = FinalizeIo(t); }); + task.ContinueWith(t => { GamePaths = FinalizeIo(t); }, TaskScheduler.Default); } private EstManipulation[] GetCurrentEstManipulations() @@ -130,7 +130,7 @@ public partial class ModEditWindow BeginIo(); _edit._models.ExportToGltf(ExportConfig, Mdl, sklbPaths, ReadFile, outputPath) - .ContinueWith(FinalizeIo); + .ContinueWith(FinalizeIo, TaskScheduler.Default); } /// Import a model from an interchange format. @@ -144,7 +144,7 @@ public partial class ModEditWindow var mdlFile = FinalizeIo(task, result => result.Item1, result => result.Item2); if (mdlFile != null) FinalizeImport(mdlFile); - }); + }, TaskScheduler.Default); } /// Finalise the import of a .mdl, applying any post-import transformations and state updates. diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Textures.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Textures.cs index 71c64059..652ecb49 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Textures.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Textures.cs @@ -255,7 +255,7 @@ public partial class ModEditWindow return; _framework.RunOnFrameworkThread(() => tex.Reload(_textures)); - }); + }, TaskScheduler.Default); } private Vector2 GetChildWidth() diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index afa846b5..72dd91d3 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -326,7 +326,7 @@ public partial class ModEditWindow : Window, IDisposable else if (ImGuiUtil.DrawDisabledButton("Re-Duplicate and Normalize Mod", Vector2.Zero, tt, !_allowReduplicate && !modifier)) { _editor.ModNormalizer.Normalize(Mod!); - _editor.ModNormalizer.Worker.ContinueWith(_ => _editor.LoadMod(Mod!, _editor.GroupIdx, _editor.OptionIdx)); + _editor.ModNormalizer.Worker.ContinueWith(_ => _editor.LoadMod(Mod!, _editor.GroupIdx, _editor.OptionIdx), TaskScheduler.Default); } if (!_editor.Duplicates.Worker.IsCompleted) diff --git a/Penumbra/UI/CollectionTab/IndividualAssignmentUi.cs b/Penumbra/UI/CollectionTab/IndividualAssignmentUi.cs index a0e35cff..fd8f9b25 100644 --- a/Penumbra/UI/CollectionTab/IndividualAssignmentUi.cs +++ b/Penumbra/UI/CollectionTab/IndividualAssignmentUi.cs @@ -33,7 +33,7 @@ public class IndividualAssignmentUi : IDisposable _actors = actors; _collectionManager = collectionManager; _communicator.CollectionChange.Subscribe(UpdateIdentifiers, CollectionChange.Priority.IndividualAssignmentUi); - _actors.Awaiter.ContinueWith(_ => SetupCombos()); + _actors.Awaiter.ContinueWith(_ => SetupCombos(), TaskScheduler.Default); } public string PlayerTooltip { get; private set; } = NewPlayerTooltipEmpty; From 0f89e243779334d3c916698290e9157fd787775c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 18 Mar 2024 17:12:30 +0100 Subject: [PATCH 041/433] Improve tag button positioning. --- OtterGui | 2 +- Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs | 9 +++++---- Penumbra/UI/PredefinedTagManager.cs | 4 +--- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/OtterGui b/OtterGui index 5a2e12a1..c59b1c09 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 5a2e12a1acd6760a3a592447a92215135e79197c +Subproject commit c59b1c09ff7b8093d3a70c45957f9c41341dd3a4 diff --git a/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs b/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs index 4c5e68ff..e1b80b23 100644 --- a/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs @@ -29,17 +29,18 @@ public class ModPanelDescriptionTab( ImGui.Dummy(ImGuiHelpers.ScaledVector2(2)); ImGui.Dummy(ImGuiHelpers.ScaledVector2(2)); - var sharedTagsEnabled = predefinedTagsConfig.SharedTags.Count > 0; - var sharedTagButtonOffset = sharedTagsEnabled ? ImGui.GetFrameHeight() + ImGui.GetStyle().FramePadding.X : 0; + var (predefinedTagsEnabled, predefinedTagButtonOffset) = predefinedTagsConfig.SharedTags.Count > 0 + ? (true, ImGui.GetFrameHeight() + ImGui.GetStyle().WindowPadding.X + (ImGui.GetScrollMaxY() > 0 ? ImGui.GetStyle().ScrollbarSize : 0)) + : (false, 0); var tagIdx = _localTags.Draw("Local Tags: ", "Custom tags you can set personally that will not be exported to the mod data but only set for you.\n" + "If the mod already contains a local tag in its own tags, the local tag will be ignored.", selector.Selected!.LocalTags, - out var editedTag, rightEndOffset: sharedTagButtonOffset); + out var editedTag, rightEndOffset: predefinedTagButtonOffset); tutorial.OpenTutorial(BasicTutorialSteps.Tags); if (tagIdx >= 0) modManager.DataEditor.ChangeLocalTag(selector.Selected!, tagIdx, editedTag); - if (sharedTagsEnabled) + if (predefinedTagsEnabled) predefinedTagsConfig.DrawAddFromSharedTagsAndUpdateTags(selector.Selected!.LocalTags, selector.Selected!.ModTags, true, selector.Selected!); diff --git a/Penumbra/UI/PredefinedTagManager.cs b/Penumbra/UI/PredefinedTagManager.cs index fafca101..b85b5dea 100644 --- a/Penumbra/UI/PredefinedTagManager.cs +++ b/Penumbra/UI/PredefinedTagManager.cs @@ -110,9 +110,7 @@ public sealed class PredefinedTagManager : ISavable public void DrawAddFromSharedTagsAndUpdateTags(IReadOnlyCollection localTags, IReadOnlyCollection modTags, bool editLocal, Mods.Mod mod) { - ImGui.SetCursorPosY(ImGui.GetCursorPosY() - ImGui.GetFrameHeightWithSpacing()); - ImGui.SetCursorPosX(ImGui.GetWindowWidth() - ImGui.GetFrameHeight() - ImGui.GetStyle().FramePadding.X); - + ImGui.SameLine(ImGui.GetContentRegionMax().X - ImGui.GetFrameHeight() - ImGui.GetStyle().WindowPadding.X); var sharedTag = DrawAddFromSharedTags(localTags, modTags, editLocal); if (sharedTag.Length > 0) From 05b7234748ddc2cbcb7d5cfe329cd7a39e50276b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 19 Mar 2024 17:35:05 +0100 Subject: [PATCH 042/433] Update to .net8. --- OtterGui | 2 +- Penumbra.Api | 2 +- Penumbra.CrashHandler/Penumbra.CrashHandler.csproj | 2 +- Penumbra.GameData | 2 +- Penumbra.String | 2 +- Penumbra/Import/Models/Export/MaterialExporter.cs | 3 +-- Penumbra/Penumbra.csproj | 2 +- Penumbra/Services/FilenameService.cs | 2 +- Penumbra/Services/MessageService.cs | 3 ++- Penumbra/Services/ServiceManagerA.cs | 3 ++- Penumbra/UI/TutorialService.cs | 2 +- Penumbra/packages.lock.json | 2 +- 12 files changed, 14 insertions(+), 13 deletions(-) diff --git a/OtterGui b/OtterGui index 5a2e12a1..b4b14367 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 5a2e12a1acd6760a3a592447a92215135e79197c +Subproject commit b4b14367d8235eabedd561ad3626beb1d2a83889 diff --git a/Penumbra.Api b/Penumbra.Api index 34921fd2..1df06807 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit 34921fd2c5a9aff5d34aef664bdb78331e8b9436 +Subproject commit 1df06807650a79813791effaa01fb7c4710b3dab diff --git a/Penumbra.CrashHandler/Penumbra.CrashHandler.csproj b/Penumbra.CrashHandler/Penumbra.CrashHandler.csproj index ea61b968..c9f97fde 100644 --- a/Penumbra.CrashHandler/Penumbra.CrashHandler.csproj +++ b/Penumbra.CrashHandler/Penumbra.CrashHandler.csproj @@ -2,7 +2,7 @@ Exe - net7.0-windows + net8.0-windows preview enable x64 diff --git a/Penumbra.GameData b/Penumbra.GameData index c39f683d..33b51274 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit c39f683d65d4541e9f97ed4ea1abcb10e8ca5690 +Subproject commit 33b512746e80b7b1276b644430923eee9bec9fba diff --git a/Penumbra.String b/Penumbra.String index 620a7edf..14e00f77 160000 --- a/Penumbra.String +++ b/Penumbra.String @@ -1 +1 @@ -Subproject commit 620a7edf009b92288257ce7d64fffb8fba44d8b5 +Subproject commit 14e00f77d42bc677e02325660db765ef11932560 diff --git a/Penumbra/Import/Models/Export/MaterialExporter.cs b/Penumbra/Import/Models/Export/MaterialExporter.cs index 82e13eb9..2fa4e1b2 100644 --- a/Penumbra/Import/Models/Export/MaterialExporter.cs +++ b/Penumbra/Import/Models/Export/MaterialExporter.cs @@ -144,8 +144,7 @@ public class MaterialExporter // Specular (table) var lerpedSpecularColor = Vector3.Lerp(prevRow.Specular, nextRow.Specular, tableRow.Weight); - // float.Lerp is .NET8 ;-; #TODO - var lerpedSpecularFactor = prevRow.SpecularStrength * (1.0f - tableRow.Weight) + nextRow.SpecularStrength * tableRow.Weight; + var lerpedSpecularFactor = float.Lerp(prevRow.SpecularStrength, nextRow.SpecularStrength, tableRow.Weight); specularSpan[x].FromVector4(new Vector4(lerpedSpecularColor, lerpedSpecularFactor)); // Emissive (table) diff --git a/Penumbra/Penumbra.csproj b/Penumbra/Penumbra.csproj index 796eae01..df18ff13 100644 --- a/Penumbra/Penumbra.csproj +++ b/Penumbra/Penumbra.csproj @@ -1,6 +1,6 @@ - net7.0-windows + net8.0-windows preview x64 Penumbra diff --git a/Penumbra/Services/FilenameService.cs b/Penumbra/Services/FilenameService.cs index 49b4cb42..c1b067d0 100644 --- a/Penumbra/Services/FilenameService.cs +++ b/Penumbra/Services/FilenameService.cs @@ -16,7 +16,7 @@ public class FilenameService(DalamudPluginInterface pi) : IService public readonly string ActiveCollectionsFile = Path.Combine(pi.ConfigDirectory.FullName, "active_collections.json"); public readonly string CrashHandlerExe = - Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Penumbra.CrashHandler.exe"); + Path.Combine(pi.AssemblyLocation.DirectoryName!, "Penumbra.CrashHandler.exe"); public readonly string LogFileName = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(pi.ConfigDirectory.FullName)!)!, "Penumbra.log"); diff --git a/Penumbra/Services/MessageService.cs b/Penumbra/Services/MessageService.cs index daad29ef..0a85a569 100644 --- a/Penumbra/Services/MessageService.cs +++ b/Penumbra/Services/MessageService.cs @@ -9,7 +9,8 @@ using OtterGui.Services; namespace Penumbra.Services; -public class MessageService(Logger log, UiBuilder uiBuilder, IChatGui chat) : OtterGui.Classes.MessageService(log, uiBuilder, chat), IService +public class MessageService(Logger log, UiBuilder uiBuilder, IChatGui chat, INotificationManager notificationManager) + : OtterGui.Classes.MessageService(log, uiBuilder, chat, notificationManager), IService { public void LinkItem(Item item) { diff --git a/Penumbra/Services/ServiceManagerA.cs b/Penumbra/Services/ServiceManagerA.cs index 191d8d11..a5de33bb 100644 --- a/Penumbra/Services/ServiceManagerA.cs +++ b/Penumbra/Services/ServiceManagerA.cs @@ -81,7 +81,8 @@ public static class ServiceManagerA .AddDalamudService(pi) .AddDalamudService(pi) .AddDalamudService(pi) - .AddDalamudService(pi); + .AddDalamudService(pi) + .AddDalamudService(pi); private static ServiceManager AddInterop(this ServiceManager services) => services.AddSingleton() diff --git a/Penumbra/UI/TutorialService.cs b/Penumbra/UI/TutorialService.cs index 6c6b0612..d87df19e 100644 --- a/Penumbra/UI/TutorialService.cs +++ b/Penumbra/UI/TutorialService.cs @@ -108,7 +108,7 @@ public class TutorialService .Register("Initial Setup, Step 8: Mod Import", "Click this button to open a file selector with which to select TTMP mod files. You can select multiple at once.\n\n" + "It is not recommended to import huge mod packs of all your TexTools mods, but rather import the mods themselves, otherwise you lose out on a lot of Penumbra features!\n\n" - + "A feature to import raw texture mods for Tattoos etc. is available under Advanced Editing, but is currently a work in progress.") // TODO + + "A feature to import raw texture mods for Tattoos etc. is available under Advanced Editing, but is currently a work in progress.") .Register("Advanced Help", "Click this button to get detailed information on what you can do in the mod selector.\n\n" + "Import and select a mod now to continue.") .Register("Mod Filters", "You can filter the available mods by name, author, changed items or various attributes here.") diff --git a/Penumbra/packages.lock.json b/Penumbra/packages.lock.json index d0e8fa1a..3f795631 100644 --- a/Penumbra/packages.lock.json +++ b/Penumbra/packages.lock.json @@ -1,7 +1,7 @@ { "version": 1, "dependencies": { - "net7.0-windows7.0": { + "net8.0-windows7.0": { "EmbedIO": { "type": "Direct", "requested": "[3.4.3, )", From 02f2bf1bc18fd9362f6e765a424a23797f2442f3 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 19 Mar 2024 17:38:02 +0100 Subject: [PATCH 043/433] Update actions. --- .github/workflows/build.yml | 2 +- .github/workflows/release.yml | 2 +- .github/workflows/test_release.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3dd1d45b..b40b2538 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,7 +16,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v1 with: - dotnet-version: '7.x.x' + dotnet-version: '8.x.x' - name: Restore dependencies run: dotnet restore - name: Download Dalamud diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f3afe9c1..7c9e2909 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -15,7 +15,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v1 with: - dotnet-version: '7.x.x' + dotnet-version: '8.x.x' - name: Restore dependencies run: dotnet restore - name: Download Dalamud diff --git a/.github/workflows/test_release.yml b/.github/workflows/test_release.yml index 0968430d..91361646 100644 --- a/.github/workflows/test_release.yml +++ b/.github/workflows/test_release.yml @@ -15,7 +15,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v1 with: - dotnet-version: '7.x.x' + dotnet-version: '8.x.x' - name: Restore dependencies run: dotnet restore - name: Download Dalamud From a6e08a18652499caf5ede1a010b34e2ac7da4073 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 19 Mar 2024 18:30:00 +0100 Subject: [PATCH 044/433] Rename ServiceManagerA. --- Penumbra/Penumbra.cs | 2 +- .../{ServiceManagerA.cs => StaticServiceManager.cs} | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) rename Penumbra/Services/{ServiceManagerA.cs => StaticServiceManager.cs} (97%) diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index b532339f..ff068928 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -51,7 +51,7 @@ public class Penumbra : IDalamudPlugin { try { - _services = ServiceManagerA.CreateProvider(this, pluginInterface, Log); + _services = StaticServiceManager.CreateProvider(this, pluginInterface, Log); Messager = _services.GetService(); _validityChecker = _services.GetService(); _services.EnsureRequiredServices(); diff --git a/Penumbra/Services/ServiceManagerA.cs b/Penumbra/Services/StaticServiceManager.cs similarity index 97% rename from Penumbra/Services/ServiceManagerA.cs rename to Penumbra/Services/StaticServiceManager.cs index a5de33bb..66c90e84 100644 --- a/Penumbra/Services/ServiceManagerA.cs +++ b/Penumbra/Services/StaticServiceManager.cs @@ -6,7 +6,6 @@ using Dalamud.Plugin.Services; using Microsoft.Extensions.DependencyInjection; using OtterGui; using OtterGui.Classes; -using OtterGui.Compression; using OtterGui.Log; using OtterGui.Services; using Penumbra.Api; @@ -14,14 +13,12 @@ using Penumbra.Collections.Cache; using Penumbra.Collections.Manager; using Penumbra.GameData.Actors; using Penumbra.Import.Models; -using Penumbra.GameData.DataContainers; using Penumbra.GameData.Structs; using Penumbra.Import.Textures; using Penumbra.Interop.PathResolving; using Penumbra.Interop.ResourceLoading; using Penumbra.Interop.ResourceTree; using Penumbra.Interop.Services; -using Penumbra.Interop.Structs; using Penumbra.Meta; using Penumbra.Mods; using Penumbra.Mods.Editor; @@ -38,7 +35,7 @@ using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManage namespace Penumbra.Services; -public static class ServiceManagerA +public static class StaticServiceManager { public static ServiceManager CreateProvider(Penumbra penumbra, DalamudPluginInterface pi, Logger log) { @@ -105,7 +102,8 @@ public static class ServiceManagerA private static ServiceManager AddConfiguration(this ServiceManager services) => services.AddSingleton() - .AddSingleton(); + .AddSingleton() + .AddSingleton(); private static ServiceManager AddCollections(this ServiceManager services) => services.AddSingleton() From fe6e1edecc7877d99ed2a467601310a420d1575f Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 19 Mar 2024 18:31:02 +0100 Subject: [PATCH 045/433] Fix a game object table warning. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 33b51274..d53db6a3 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 33b512746e80b7b1276b644430923eee9bec9fba +Subproject commit d53db6a358cedecd3ef18f62f12a07deff4b61ee From 52c1708dd270ec46a3de242d57a42116a9de094c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 19 Mar 2024 20:45:16 +0100 Subject: [PATCH 046/433] Change predefined tag handling. --- Penumbra/Mods/Mod.cs | 6 +- Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs | 2 +- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 2 +- Penumbra/UI/PredefinedTagManager.cs | 210 ++++++------------ Penumbra/UI/Tabs/ModsTab.cs | 2 +- Penumbra/UI/Tabs/SettingsTab.cs | 65 +++--- 6 files changed, 112 insertions(+), 175 deletions(-) diff --git a/Penumbra/Mods/Mod.cs b/Penumbra/Mods/Mod.cs index a9ef22cb..c5e671af 100644 --- a/Penumbra/Mods/Mod.cs +++ b/Penumbra/Mods/Mod.cs @@ -45,19 +45,19 @@ public sealed class Mod : IMod public string Description { get; internal set; } = string.Empty; public string Version { get; internal set; } = string.Empty; public string Website { get; internal set; } = string.Empty; - public IReadOnlyList ModTags { get; internal set; } = Array.Empty(); + public IReadOnlyList ModTags { get; internal set; } = []; // Local Data public long ImportDate { get; internal set; } = DateTimeOffset.UnixEpoch.ToUnixTimeMilliseconds(); - public IReadOnlyList LocalTags { get; internal set; } = Array.Empty(); + public IReadOnlyList LocalTags { get; internal set; } = []; public string Note { get; internal set; } = string.Empty; public bool Favorite { get; internal set; } = false; // Options public readonly SubMod Default; - public readonly List Groups = new(); + public readonly List Groups = []; ISubMod IMod.Default => Default; diff --git a/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs b/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs index e1b80b23..4ad30a6f 100644 --- a/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs @@ -29,7 +29,7 @@ public class ModPanelDescriptionTab( ImGui.Dummy(ImGuiHelpers.ScaledVector2(2)); ImGui.Dummy(ImGuiHelpers.ScaledVector2(2)); - var (predefinedTagsEnabled, predefinedTagButtonOffset) = predefinedTagsConfig.SharedTags.Count > 0 + var (predefinedTagsEnabled, predefinedTagButtonOffset) = predefinedTagsConfig.PredefinedTags.Count > 0 ? (true, ImGui.GetFrameHeight() + ImGui.GetStyle().WindowPadding.X + (ImGui.GetScrollMaxY() > 0 ? ImGui.GetStyle().ScrollbarSize : 0)) : (false, 0); var tagIdx = _localTags.Draw("Local Tags: ", diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index 275c89ef..a0e32c22 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -69,7 +69,7 @@ public class ModPanelEditTab( } UiHelpers.DefaultLineSpace(); - var sharedTagsEnabled = predefinedTagManager.SharedTags.Count > 0; + var sharedTagsEnabled = predefinedTagManager.PredefinedTags.Count > 0; var sharedTagButtonOffset = sharedTagsEnabled ? ImGui.GetFrameHeight() + ImGui.GetStyle().FramePadding.X : 0; var tagIdx = _modTags.Draw("Mod Tags: ", "Edit tags by clicking them, or add new tags. Empty tags are removed.", _mod.ModTags, out var editedTag, rightEndOffset: sharedTagButtonOffset); diff --git a/Penumbra/UI/PredefinedTagManager.cs b/Penumbra/UI/PredefinedTagManager.cs index b85b5dea..63be42de 100644 --- a/Penumbra/UI/PredefinedTagManager.cs +++ b/Penumbra/UI/PredefinedTagManager.cs @@ -17,22 +17,19 @@ public sealed class PredefinedTagManager : ISavable private readonly ModManager _modManager; private readonly SaveService _saveService; - private static uint _tagButtonAddColor = ColorId.PredefinedTagAdd.Value(); - private static uint _tagButtonRemoveColor = ColorId.PredefinedTagRemove.Value(); + private bool _isListOpen = false; + private uint _enabledColor; + private uint _disabledColor; - private static float _minTagButtonWidth = 15; - - private const string PopupContext = "SharedTagsPopup"; - private bool _isPopupOpen = false; // Operations on this list assume that it is sorted and will keep it sorted if that is the case. // The list also gets re-sorted when first loaded from config in case the config was modified. [JsonRequired] - private readonly List _sharedTags = []; + private readonly List _predefinedTags = []; [JsonIgnore] - public IReadOnlyList SharedTags - => _sharedTags; + public IReadOnlyList PredefinedTags + => _predefinedTags; public int ConfigVersion = 1; @@ -48,8 +45,9 @@ public sealed class PredefinedTagManager : ISavable public void Save(StreamWriter writer) { - using var jWriter = new JsonTextWriter(writer) { Formatting = Formatting.Indented }; - var serializer = new JsonSerializer { Formatting = Formatting.Indented }; + using var jWriter = new JsonTextWriter(writer); + jWriter.Formatting = Formatting.Indented; + var serializer = new JsonSerializer { Formatting = Formatting.Indented }; serializer.Serialize(jWriter, this); } @@ -58,13 +56,6 @@ public sealed class PredefinedTagManager : ISavable private void Load() { - static void HandleDeserializationError(object? sender, ErrorEventArgs errorArgs) - { - Penumbra.Log.Error( - $"Error parsing shared tags Configuration at {errorArgs.ErrorContext.Path}, using default or migrating:\n{errorArgs.ErrorContext.Error}"); - errorArgs.ErrorContext.Handled = true; - } - if (!File.Exists(_saveService.FileNames.PredefinedTagFile)) return; @@ -77,7 +68,7 @@ public sealed class PredefinedTagManager : ISavable }); // Any changes to this within this class should keep it sorted, but in case someone went in and manually changed the JSON, run a sort on initial load. - _sharedTags.Sort(); + _predefinedTags.Sort(); } catch (Exception ex) { @@ -85,23 +76,32 @@ public sealed class PredefinedTagManager : ISavable "Error reading shared tags Configuration, reverting to default.", "Error reading shared tags Configuration", NotificationType.Error); } + + return; + + static void HandleDeserializationError(object? sender, ErrorEventArgs errorArgs) + { + Penumbra.Log.Error( + $"Error parsing shared tags Configuration at {errorArgs.ErrorContext.Path}, using default or migrating:\n{errorArgs.ErrorContext.Error}"); + errorArgs.ErrorContext.Handled = true; + } } public void ChangeSharedTag(int tagIdx, string tag) { - if (tagIdx < 0 || tagIdx > SharedTags.Count) + if (tagIdx < 0 || tagIdx > PredefinedTags.Count) return; // In the case of editing a tag, remove what's there prior to doing an insert. - if (tagIdx != SharedTags.Count) - _sharedTags.RemoveAt(tagIdx); + if (tagIdx != PredefinedTags.Count) + _predefinedTags.RemoveAt(tagIdx); if (!string.IsNullOrEmpty(tag)) { // Taking advantage of the fact that BinarySearch returns the complement of the correct sorted position for the tag. - var existingIdx = _sharedTags.BinarySearch(tag); + var existingIdx = _predefinedTags.BinarySearch(tag); if (existingIdx < 0) - _sharedTags.Insert(~existingIdx, tag); + _predefinedTags.Insert(~existingIdx, tag); } Save(); @@ -110,147 +110,83 @@ public sealed class PredefinedTagManager : ISavable public void DrawAddFromSharedTagsAndUpdateTags(IReadOnlyCollection localTags, IReadOnlyCollection modTags, bool editLocal, Mods.Mod mod) { - ImGui.SameLine(ImGui.GetContentRegionMax().X - ImGui.GetFrameHeight() - ImGui.GetStyle().WindowPadding.X); - var sharedTag = DrawAddFromSharedTags(localTags, modTags, editLocal); + DrawToggleButton(); + if (!DrawList(localTags, modTags, editLocal, out var changedTag, out var index)) + return; - if (sharedTag.Length > 0) - { - var index = editLocal ? mod.LocalTags.IndexOf(sharedTag) : mod.ModTags.IndexOf(sharedTag); - - if (editLocal) - { - if (index < 0) - { - index = mod.LocalTags.Count; - _modManager.DataEditor.ChangeLocalTag(mod, index, sharedTag); - } - else - { - _modManager.DataEditor.ChangeLocalTag(mod, index, string.Empty); - } - } - else - { - if (index < 0) - { - index = mod.ModTags.Count; - _modManager.DataEditor.ChangeModTag(mod, index, sharedTag); - } - else - { - _modManager.DataEditor.ChangeModTag(mod, index, string.Empty); - } - } - } + if (editLocal) + _modManager.DataEditor.ChangeLocalTag(mod, index, changedTag); + else + _modManager.DataEditor.ChangeModTag(mod, index, changedTag); } - public string DrawAddFromSharedTags(IReadOnlyCollection localTags, IReadOnlyCollection modTags, bool editLocal) + private void DrawToggleButton() { - var tagToAdd = string.Empty; + ImGui.SameLine(ImGui.GetContentRegionMax().X + - ImGui.GetFrameHeight() + - (ImGui.GetScrollMaxY() > 0 ? ImGui.GetStyle().ItemInnerSpacing.X : 0)); + using var color = ImRaii.PushColor(ImGuiCol.Button, ImGui.GetColorU32(ImGuiCol.ButtonActive), _isListOpen); if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Tags.ToIconString(), new Vector2(ImGui.GetFrameHeight()), - "Add Shared Tag... (Right-click to close popup)", - false, true) - || _isPopupOpen) - return DrawSharedTagsPopup(localTags, modTags, editLocal); - - return tagToAdd; + "Add Predefined Tags...", false, true)) + _isListOpen = !_isListOpen; } - private string DrawSharedTagsPopup(IReadOnlyCollection localTags, IReadOnlyCollection modTags, bool editLocal) + private bool DrawList(IReadOnlyCollection localTags, IReadOnlyCollection modTags, bool editLocal, out string changedTag, + out int changedIndex) { - var selected = string.Empty; - if (!ImGui.IsPopupOpen(PopupContext)) - { - ImGui.OpenPopup(PopupContext); - _isPopupOpen = true; - } + changedTag = string.Empty; + changedIndex = -1; - var display = ImGui.GetIO().DisplaySize; - var height = Math.Min(display.Y / 4, 10 * ImGui.GetFrameHeightWithSpacing()); - var width = display.X / 6; - var size = new Vector2(width, height); - ImGui.SetNextWindowSize(size); - using var popup = ImRaii.Popup(PopupContext); - if (!popup) - return selected; + if (!_isListOpen) + return false; - ImGui.TextUnformatted("Shared Tags"); - ImGuiUtil.HoverTooltip("Right-click to close popup"); + ImGui.TextUnformatted("Predefined Tags"); ImGui.Separator(); - foreach (var (tag, idx) in SharedTags.WithIndex()) + var ret = false; + _enabledColor = ColorId.PredefinedTagAdd.Value(); + _disabledColor = ColorId.PredefinedTagRemove.Value(); + var (edited, others) = editLocal ? (localTags, modTags) : (modTags, localTags); + foreach (var (tag, idx) in PredefinedTags.WithIndex()) { - if (DrawColoredButton(localTags, modTags, tag, editLocal, idx)) - selected = tag; + var tagIdx = edited.IndexOf(tag); + var inOther = tagIdx < 0 && others.IndexOf(tag) >= 0; + if (DrawColoredButton(tag, idx, tagIdx, inOther)) + { + (changedTag, changedIndex) = tagIdx >= 0 ? (string.Empty, tagIdx) : (tag, edited.Count); + ret = true; + } + ImGui.SameLine(); } - if (ImGui.IsMouseClicked(ImGuiMouseButton.Right)) - _isPopupOpen = false; - - return selected; + ImGui.NewLine(); + ImGui.Separator(); + return ret; } - private static bool DrawColoredButton(IReadOnlyCollection localTags, IReadOnlyCollection modTags, string buttonLabel, - bool editLocal, int index) + private bool DrawColoredButton(string buttonLabel, int index, int tagIdx, bool inOther) { - var ret = false; - - var isLocalTagPresent = localTags.Contains(buttonLabel); - var isModTagPresent = modTags.Contains(buttonLabel); - - var buttonWidth = CalcTextButtonWidth(buttonLabel); - // Would prefer to be able to fit at least 2 buttons per line so the popup doesn't look sparse with lots of long tags. Thus long tags will be trimmed. - var maxButtonWidth = (ImGui.GetContentRegionMax().X - ImGui.GetWindowContentRegionMin().X) * 0.5f - ImGui.GetStyle().ItemSpacing.X; - var displayedLabel = buttonLabel; - if (buttonWidth >= maxButtonWidth) - { - displayedLabel = TrimButtonTextToWidth(buttonLabel, maxButtonWidth); - buttonWidth = CalcTextButtonWidth(displayedLabel); - } - + using var id = ImRaii.PushId(index); + var buttonWidth = CalcTextButtonWidth(buttonLabel); // Prevent adding a new tag past the right edge of the popup if (buttonWidth + ImGui.GetStyle().ItemSpacing.X >= ImGui.GetContentRegionAvail().X) ImGui.NewLine(); - // Trimmed tag names can collide, and while tag names are currently distinct this may not always be the case. As such use the index to avoid an ImGui moment. - using var id = ImRaii.PushId(index); + bool ret; + using (ImRaii.Disabled(inOther)) + { + using var color = ImRaii.PushColor(ImGuiCol.Button, tagIdx >= 0 || inOther ? _disabledColor : _enabledColor); + ret = ImGui.Button(buttonLabel); + } + + if (inOther && ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled)) + ImGui.SetTooltip("This tag is already present in the other set of tags."); - if (editLocal && isModTagPresent || !editLocal && isLocalTagPresent) - { - using var alpha = ImRaii.PushStyle(ImGuiStyleVar.Alpha, 0.5f); - ImGui.Button(displayedLabel); - } - else - { - using (ImRaii.PushColor(ImGuiCol.Button, isLocalTagPresent || isModTagPresent ? _tagButtonRemoveColor : _tagButtonAddColor)) - { - if (ImGui.Button(displayedLabel)) - ret = true; - } - } return ret; } - private static string TrimButtonTextToWidth(string fullText, float maxWidth) - { - var trimmedText = fullText; - - while (trimmedText.Length > _minTagButtonWidth) - { - var nextTrim = trimmedText.Substring(0, Math.Max(trimmedText.Length - 1, 0)); - - // An ellipsis will be used to indicate trimmed tags - if (CalcTextButtonWidth(nextTrim + "...") < maxWidth) - return nextTrim + "..."; - - trimmedText = nextTrim; - } - - return trimmedText; - } - private static float CalcTextButtonWidth(string text) => ImGui.CalcTextSize(text).X + 2 * ImGui.GetStyle().FramePadding.X; } diff --git a/Penumbra/UI/Tabs/ModsTab.cs b/Penumbra/UI/Tabs/ModsTab.cs index d111c465..bb8856b3 100644 --- a/Penumbra/UI/Tabs/ModsTab.cs +++ b/Penumbra/UI/Tabs/ModsTab.cs @@ -110,7 +110,7 @@ public class ModsTab( var frameColor = ImGui.GetColorU32(ImGuiCol.FrameBg); using (var _ = ImRaii.Group()) { - using (var font = ImRaii.PushFont(UiBuilder.IconFont)) + using (ImRaii.PushFont(UiBuilder.IconFont)) { ImGuiUtil.DrawTextButton(FontAwesomeIcon.InfoCircle.ToIconString(), frameHeight, frameColor); ImGui.SameLine(); diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index 60c18d5f..9f8ffb38 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -227,37 +227,38 @@ public class SettingsTab : ITab if (_newModDirectory.IsNullOrEmpty()) _newModDirectory = _config.ModDirectory; - using var group = ImRaii.Group(); - ImGui.SetNextItemWidth(UiHelpers.InputTextMinusButton3); - bool save; - using (ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale, !_modManager.Valid)) - { - using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.RegexWarningBorder) - .Push(ImGuiCol.TextDisabled, Colors.RegexWarningBorder, !_modManager.Valid); - save = ImGui.InputTextWithHint("##rootDirectory", "Enter Root Directory here (MANDATORY)...", ref _newModDirectory, - RootDirectoryMaxLength, ImGuiInputTextFlags.EnterReturnsTrue); + bool save, selected; + using (ImRaii.Group()) + { + ImGui.SetNextItemWidth(UiHelpers.InputTextMinusButton3); + using (ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale, !_modManager.Valid)) + { + using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.RegexWarningBorder) + .Push(ImGuiCol.TextDisabled, Colors.RegexWarningBorder, !_modManager.Valid); + save = ImGui.InputTextWithHint("##rootDirectory", "Enter Root Directory here (MANDATORY)...", ref _newModDirectory, + RootDirectoryMaxLength, ImGuiInputTextFlags.EnterReturnsTrue); + } + + selected = ImGui.IsItemActive(); + using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, new Vector2(UiHelpers.ScaleX3, 0)); + ImGui.SameLine(); + DrawDirectoryPickerButton(); + style.Pop(); + ImGui.SameLine(); + + const string tt = "This is where Penumbra will store your extracted mod files.\n" + + "TTMP files are not copied, just extracted.\n" + + "This directory needs to be accessible and you need write access here.\n" + + "It is recommended that this directory is placed on a fast hard drive, preferably an SSD.\n" + + "It should also be placed near the root of a logical drive - the shorter the total path to this folder, the better.\n" + + "Definitely do not place it in your Dalamud directory or any sub-directory thereof."; + ImGuiComponents.HelpMarker(tt); + _tutorial.OpenTutorial(BasicTutorialSteps.GeneralTooltips); + ImGui.SameLine(); + ImGui.TextUnformatted("Root Directory"); + ImGuiUtil.HoverTooltip(tt); } - var selected = ImGui.IsItemActive(); - using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, new Vector2(UiHelpers.ScaleX3, 0)); - ImGui.SameLine(); - DrawDirectoryPickerButton(); - style.Pop(); - ImGui.SameLine(); - - const string tt = "This is where Penumbra will store your extracted mod files.\n" - + "TTMP files are not copied, just extracted.\n" - + "This directory needs to be accessible and you need write access here.\n" - + "It is recommended that this directory is placed on a fast hard drive, preferably an SSD.\n" - + "It should also be placed near the root of a logical drive - the shorter the total path to this folder, the better.\n" - + "Definitely do not place it in your Dalamud directory or any sub-directory thereof."; - ImGuiComponents.HelpMarker(tt); - _tutorial.OpenTutorial(BasicTutorialSteps.GeneralTooltips); - ImGui.SameLine(); - ImGui.TextUnformatted("Root Directory"); - ImGuiUtil.HoverTooltip(tt); - - group.Dispose(); _tutorial.OpenTutorial(BasicTutorialSteps.ModDirectory); ImGui.SameLine(); var pos = ImGui.GetCursorPosX(); @@ -685,7 +686,7 @@ public class SettingsTab : ITab foreach (var color in Enum.GetValues()) { var (defaultColor, name, description) = color.Data(); - var currentColor = _config.Colors.TryGetValue(color, out var current) ? current : defaultColor; + var currentColor = _config.Colors.GetValueOrDefault(color, defaultColor); if (Widget.ColorPicker(name, description, currentColor, c => _config.Colors[color] = c, defaultColor)) _config.Save(); } @@ -871,7 +872,7 @@ public class SettingsTab : ITab if (!_dalamudConfig.GetDalamudConfig(DalamudConfigService.WaitingForPluginsOption, out bool value)) { using var disabled = ImRaii.Disabled(); - Checkbox("Wait for Plugins on Startup (Disabled, can not access Dalamud Configuration)", string.Empty, false, v => { }); + Checkbox("Wait for Plugins on Startup (Disabled, can not access Dalamud Configuration)", string.Empty, false, _ => { }); } else { @@ -923,7 +924,7 @@ public class SettingsTab : ITab return; var tagIdx = _sharedTags.Draw("Predefined Tags: ", - "Predefined tags that can be added or removed from mods with a single click.", _predefinedTagManager.SharedTags, + "Predefined tags that can be added or removed from mods with a single click.", _predefinedTagManager.PredefinedTags, out var editedTag); if (tagIdx >= 0) From 0e50a8a9e5155423ab8db2334a07861d83cf44db Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 19 Mar 2024 21:01:48 +0100 Subject: [PATCH 047/433] More future proof structure for tags. --- Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs | 2 +- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 2 +- Penumbra/UI/PredefinedTagManager.cs | 86 ++++++++++--------- Penumbra/UI/Tabs/SettingsTab.cs | 2 +- 4 files changed, 47 insertions(+), 45 deletions(-) diff --git a/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs b/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs index 4ad30a6f..ed6340ab 100644 --- a/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs @@ -29,7 +29,7 @@ public class ModPanelDescriptionTab( ImGui.Dummy(ImGuiHelpers.ScaledVector2(2)); ImGui.Dummy(ImGuiHelpers.ScaledVector2(2)); - var (predefinedTagsEnabled, predefinedTagButtonOffset) = predefinedTagsConfig.PredefinedTags.Count > 0 + var (predefinedTagsEnabled, predefinedTagButtonOffset) = predefinedTagsConfig.Count > 0 ? (true, ImGui.GetFrameHeight() + ImGui.GetStyle().WindowPadding.X + (ImGui.GetScrollMaxY() > 0 ? ImGui.GetStyle().ScrollbarSize : 0)) : (false, 0); var tagIdx = _localTags.Draw("Local Tags: ", diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index a0e32c22..eb79869e 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -69,7 +69,7 @@ public class ModPanelEditTab( } UiHelpers.DefaultLineSpace(); - var sharedTagsEnabled = predefinedTagManager.PredefinedTags.Count > 0; + var sharedTagsEnabled = predefinedTagManager.Count > 0; var sharedTagButtonOffset = sharedTagsEnabled ? ImGui.GetFrameHeight() + ImGui.GetStyle().FramePadding.X : 0; var tagIdx = _modTags.Draw("Mod Tags: ", "Edit tags by clicking them, or add new tags. Empty tags are removed.", _mod.ModTags, out var editedTag, rightEndOffset: sharedTagButtonOffset); diff --git a/Penumbra/UI/PredefinedTagManager.cs b/Penumbra/UI/PredefinedTagManager.cs index 63be42de..17e8432b 100644 --- a/Penumbra/UI/PredefinedTagManager.cs +++ b/Penumbra/UI/PredefinedTagManager.cs @@ -2,6 +2,7 @@ using Dalamud.Interface.Internal.Notifications; using ImGuiNET; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using OtterGui; using OtterGui.Classes; using OtterGui.Raii; @@ -12,8 +13,13 @@ using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs; namespace Penumbra.UI; -public sealed class PredefinedTagManager : ISavable +public sealed class PredefinedTagManager : ISavable, IReadOnlyList { + public const int Version = 1; + + public record struct TagData + { } + private readonly ModManager _modManager; private readonly SaveService _saveService; @@ -21,17 +27,7 @@ public sealed class PredefinedTagManager : ISavable private uint _enabledColor; private uint _disabledColor; - - // Operations on this list assume that it is sorted and will keep it sorted if that is the case. - // The list also gets re-sorted when first loaded from config in case the config was modified. - [JsonRequired] - private readonly List _predefinedTags = []; - - [JsonIgnore] - public IReadOnlyList PredefinedTags - => _predefinedTags; - - public int ConfigVersion = 1; + private readonly SortedList _predefinedTags = []; public PredefinedTagManager(ModManager modManager, SaveService saveService) { @@ -47,8 +43,12 @@ public sealed class PredefinedTagManager : ISavable { using var jWriter = new JsonTextWriter(writer); jWriter.Formatting = Formatting.Indented; - var serializer = new JsonSerializer { Formatting = Formatting.Indented }; - serializer.Serialize(jWriter, this); + var jObj = new JObject() + { + ["Version"] = Version, + ["Tags"] = JObject.FromObject(_predefinedTags), + }; + jObj.WriteTo(jWriter); } public void Save() @@ -61,48 +61,38 @@ public sealed class PredefinedTagManager : ISavable try { - var text = File.ReadAllText(_saveService.FileNames.PredefinedTagFile); - JsonConvert.PopulateObject(text, this, new JsonSerializerSettings + var text = File.ReadAllText(_saveService.FileNames.PredefinedTagFile); + var jObj = JObject.Parse(text); + var version = jObj["Version"]?.ToObject() ?? 0; + switch (version) { - Error = HandleDeserializationError, - }); - - // Any changes to this within this class should keep it sorted, but in case someone went in and manually changed the JSON, run a sort on initial load. - _predefinedTags.Sort(); + case 1: + var tags = jObj["Tags"]?.ToObject>() ?? []; + foreach (var (tag, data) in tags) + _predefinedTags.TryAdd(tag, data); + break; + default: + throw new Exception($"Invalid version {version}."); + } } catch (Exception ex) { Penumbra.Messager.NotificationMessage(ex, - "Error reading shared tags Configuration, reverting to default.", - "Error reading shared tags Configuration", NotificationType.Error); - } - - return; - - static void HandleDeserializationError(object? sender, ErrorEventArgs errorArgs) - { - Penumbra.Log.Error( - $"Error parsing shared tags Configuration at {errorArgs.ErrorContext.Path}, using default or migrating:\n{errorArgs.ErrorContext.Error}"); - errorArgs.ErrorContext.Handled = true; + "Error reading predefined tags Configuration, reverting to default.", + "Error reading predefined tags Configuration", NotificationType.Error); } } public void ChangeSharedTag(int tagIdx, string tag) { - if (tagIdx < 0 || tagIdx > PredefinedTags.Count) + if (tagIdx < 0 || tagIdx > _predefinedTags.Count) return; - // In the case of editing a tag, remove what's there prior to doing an insert. - if (tagIdx != PredefinedTags.Count) + if (tagIdx != _predefinedTags.Count) _predefinedTags.RemoveAt(tagIdx); if (!string.IsNullOrEmpty(tag)) - { - // Taking advantage of the fact that BinarySearch returns the complement of the correct sorted position for the tag. - var existingIdx = _predefinedTags.BinarySearch(tag); - if (existingIdx < 0) - _predefinedTags.Insert(~existingIdx, tag); - } + _predefinedTags.TryAdd(tag, default); Save(); } @@ -147,7 +137,7 @@ public sealed class PredefinedTagManager : ISavable _enabledColor = ColorId.PredefinedTagAdd.Value(); _disabledColor = ColorId.PredefinedTagRemove.Value(); var (edited, others) = editLocal ? (localTags, modTags) : (modTags, localTags); - foreach (var (tag, idx) in PredefinedTags.WithIndex()) + foreach (var (tag, idx) in _predefinedTags.Keys.WithIndex()) { var tagIdx = edited.IndexOf(tag); var inOther = tagIdx < 0 && others.IndexOf(tag) >= 0; @@ -189,4 +179,16 @@ public sealed class PredefinedTagManager : ISavable private static float CalcTextButtonWidth(string text) => ImGui.CalcTextSize(text).X + 2 * ImGui.GetStyle().FramePadding.X; + + public IEnumerator GetEnumerator() + => _predefinedTags.Keys.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() + => GetEnumerator(); + + public int Count + => _predefinedTags.Count; + + public string this[int index] + => _predefinedTags.Keys[index]; } diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index 9f8ffb38..80fe6fb6 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -924,7 +924,7 @@ public class SettingsTab : ITab return; var tagIdx = _sharedTags.Draw("Predefined Tags: ", - "Predefined tags that can be added or removed from mods with a single click.", _predefinedTagManager.PredefinedTags, + "Predefined tags that can be added or removed from mods with a single click.", _predefinedTagManager, out var editedTag); if (tagIdx >= 0) From c8216b0accd2d8ce99dc2ae44d50ac037e2daf45 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 19 Mar 2024 22:52:20 +0100 Subject: [PATCH 048/433] Use ObjectManager, Actor and Model. --- Penumbra.Api | 2 +- Penumbra.GameData | 2 +- Penumbra/Api/IpcTester.cs | 140 +++++++++--------- Penumbra/Api/PenumbraApi.cs | 47 +++--- .../Hooks/Animation/LoadCharacterVfx.cs | 26 ++-- .../Hooks/Animation/LoadTimelineResources.cs | 14 +- .../Hooks/Animation/ScheduleClipUpdate.cs | 6 +- .../Interop/Hooks/Animation/SomePapLoad.cs | 11 +- .../LiveColorTablePreviewer.cs | 3 +- .../MaterialPreview/LiveMaterialPreviewer.cs | 4 +- .../LiveMaterialPreviewerBase.cs | 5 +- .../Interop/MaterialPreview/MaterialInfo.cs | 36 ++--- .../PathResolving/CollectionResolver.cs | 32 ++-- .../Interop/PathResolving/CutsceneService.cs | 31 ++-- .../Interop/PathResolving/DrawObjectState.cs | 15 +- .../ResourceTree/ResourceTreeFactory.cs | 89 ++--------- .../Interop/ResourceTree/TreeBuildCache.cs | 41 ++--- Penumbra/Interop/Services/RedrawService.cs | 39 ++--- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 7 +- Penumbra/UI/PredefinedTagManager.cs | 3 +- Penumbra/UI/Tabs/Debug/DebugTab.cs | 7 +- Penumbra/UI/Tabs/ModsTab.cs | 5 +- 22 files changed, 240 insertions(+), 325 deletions(-) diff --git a/Penumbra.Api b/Penumbra.Api index 1df06807..d2a1406b 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit 1df06807650a79813791effaa01fb7c4710b3dab +Subproject commit d2a1406bc32f715c0687613f02e3f74caf7ceea9 diff --git a/Penumbra.GameData b/Penumbra.GameData index d53db6a3..a1262e24 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit d53db6a358cedecd3ef18f62f12a07deff4b61ee +Subproject commit a1262e242ca33bb0e9e4f080d294d9160b5e54eb diff --git a/Penumbra/Api/IpcTester.cs b/Penumbra/Api/IpcTester.cs index 380b741c..e1dd81cb 100644 --- a/Penumbra/Api/IpcTester.cs +++ b/Penumbra/Api/IpcTester.cs @@ -1,3 +1,4 @@ +using Dalamud.Game.ClientState.Objects.Enums; using Dalamud.Interface; using Dalamud.Interface.Utility; using Dalamud.Plugin; @@ -18,6 +19,7 @@ using Penumbra.Collections.Manager; using Dalamud.Plugin.Services; using Penumbra.GameData.Structs; using Penumbra.GameData.Enums; +using Penumbra.GameData.Interop; namespace Penumbra.Api; @@ -40,7 +42,7 @@ public class IpcTester : IDisposable private readonly Temporary _temporary; private readonly ResourceTree _resourceTree; - public IpcTester(Configuration config, DalamudPluginInterface pi, IObjectTable objects, IClientState clientState, + public IpcTester(Configuration config, DalamudPluginInterface pi, ObjectManager objects, IClientState clientState, PenumbraIpcProviders ipcProviders, ModManager modManager, CollectionManager collections, TempModManager tempMods, TempCollectionManager tempCollections, SaveService saveService) { @@ -280,16 +282,16 @@ public class IpcTester : IDisposable { ImGui.SetNextWindowSize(ImGuiHelpers.ScaledVector2(500, 500)); using var popup = ImRaii.Popup("Config Popup"); - if (popup) - { - using (var font = ImRaii.PushFont(UiBuilder.MonoFont)) - { - ImGuiUtil.TextWrapped(_currentConfiguration); - } + if (!popup) + return; - if (ImGui.Button("Close", -Vector2.UnitX) || !ImGui.IsWindowFocused()) - ImGui.CloseCurrentPopup(); + using (ImRaii.PushFont(UiBuilder.MonoFont)) + { + ImGuiUtil.TextWrapped(_currentConfiguration); } + + if (ImGui.Button("Close", -Vector2.UnitX) || !ImGui.IsWindowFocused()) + ImGui.CloseCurrentPopup(); } private void UpdateModDirectoryChanged(string path, bool valid) @@ -304,15 +306,15 @@ public class IpcTester : IDisposable public readonly EventSubscriber Tooltip; public readonly EventSubscriber Click; - private string _lastDrawnMod = string.Empty; - private DateTimeOffset _lastDrawnModTime = DateTimeOffset.MinValue; - private bool _subscribedToTooltip = false; - private bool _subscribedToClick = false; - private string _lastClicked = string.Empty; - private string _lastHovered = string.Empty; - private TabType _selectTab = TabType.None; - private string _modName = string.Empty; - private PenumbraApiEc _ec = PenumbraApiEc.Success; + private string _lastDrawnMod = string.Empty; + private DateTimeOffset _lastDrawnModTime = DateTimeOffset.MinValue; + private bool _subscribedToTooltip; + private bool _subscribedToClick; + private string _lastClicked = string.Empty; + private string _lastHovered = string.Empty; + private TabType _selectTab = TabType.None; + private string _modName = string.Empty; + private PenumbraApiEc _ec = PenumbraApiEc.Success; public Ui(DalamudPluginInterface pi) { @@ -401,14 +403,14 @@ public class IpcTester : IDisposable { private readonly DalamudPluginInterface _pi; private readonly IClientState _clientState; - private readonly IObjectTable _objects; + private readonly ObjectManager _objects; public readonly EventSubscriber Redrawn; - private string _redrawName = string.Empty; - private int _redrawIndex = 0; + private string _redrawName = string.Empty; + private int _redrawIndex; private string _lastRedrawnString = "None"; - public Redrawing(DalamudPluginInterface pi, IObjectTable objects, IClientState clientState) + public Redrawing(DalamudPluginInterface pi, ObjectManager objects, IClientState clientState) { _pi = pi; _objects = objects; @@ -440,8 +442,8 @@ public class IpcTester : IDisposable DrawIntro(Ipc.RedrawObjectByIndex.Label, "Redraw by Index"); var tmp = _redrawIndex; ImGui.SetNextItemWidth(100 * UiHelpers.Scale); - if (ImGui.DragInt("##redrawIndex", ref tmp, 0.1f, 0, _objects.Length)) - _redrawIndex = Math.Clamp(tmp, 0, _objects.Length); + if (ImGui.DragInt("##redrawIndex", ref tmp, 0.1f, 0, _objects.Count)) + _redrawIndex = Math.Clamp(tmp, 0, _objects.Count); ImGui.SameLine(); if (ImGui.Button("Redraw##Index")) @@ -458,12 +460,12 @@ public class IpcTester : IDisposable private void SetLastRedrawn(IntPtr address, int index) { if (index < 0 - || index > _objects.Length + || index > _objects.Count || address == IntPtr.Zero - || _objects[index]?.Address != address) + || _objects[index].Address != address) _lastRedrawnString = "Invalid"; - _lastRedrawnString = $"{_objects[index]!.Name} (0x{address:X}, {index})"; + _lastRedrawnString = $"{_objects[index].Utf8Name} (0x{address:X}, {index})"; } } @@ -588,8 +590,8 @@ public class IpcTester : IDisposable private string _currentResolvePath = string.Empty; private string _currentResolveCharacter = string.Empty; private string _currentReversePath = string.Empty; - private int _currentReverseIdx = 0; - private Task<(string[], string[][])> _task = Task.FromException<(string[], string[][])>(new Exception()); + private int _currentReverseIdx; + private Task<(string[], string[][])> _task = Task.FromException<(string[], string[][])>(new Exception()); public Resolve(DalamudPluginInterface pi) => _pi = pi; @@ -696,8 +698,6 @@ public class IpcTester : IDisposable return text; } - ; - DrawIntro(Ipc.ResolvePlayerPaths.Label, "Resolved Paths (Player)"); if (forwardArray.Length > 0 || reverseArray.Length > 0) { @@ -721,18 +721,18 @@ public class IpcTester : IDisposable { private readonly DalamudPluginInterface _pi; - private int _objectIdx = 0; + private int _objectIdx; private string _collectionName = string.Empty; private bool _allowCreation = true; private bool _allowDeletion = true; private ApiCollectionType _type = ApiCollectionType.Current; private string _characterCollectionName = string.Empty; - private IList _collections = new List(); + private IList _collections = []; private string _changedItemCollection = string.Empty; private IReadOnlyDictionary _changedItems = new Dictionary(); private PenumbraApiEc _returnCode = PenumbraApiEc.Success; - private string? _oldCollection = null; + private string? _oldCollection; public Collections(DalamudPluginInterface pi) => _pi = pi; @@ -845,8 +845,8 @@ public class IpcTester : IDisposable { private readonly DalamudPluginInterface _pi; - private string _characterName = string.Empty; - private int _gameObjectIndex = 0; + private string _characterName = string.Empty; + private int _gameObjectIndex; public Meta(DalamudPluginInterface pi) => _pi = pi; @@ -1040,11 +1040,11 @@ public class IpcTester : IDisposable private string _settingsModName = string.Empty; private string _settingsCollection = string.Empty; private bool _settingsAllowInheritance = true; - private bool _settingsInherit = false; - private bool _settingsEnabled = false; - private int _settingsPriority = 0; + private bool _settingsInherit; + private bool _settingsEnabled; + private int _settingsPriority; private IDictionary, GroupType)>? _availableSettings; - private IDictionary>? _currentSettings = null; + private IDictionary>? _currentSettings; public ModSettings(DalamudPluginInterface pi) { @@ -1287,7 +1287,7 @@ public class IpcTester : IDisposable private string _tempFilePath = "test/success.mtrl"; private string _tempManipulation = string.Empty; private PenumbraApiEc _lastTempError; - private int _tempActorIndex = 0; + private int _tempActorIndex; private bool _forceOverwrite; public void Draw() @@ -1441,15 +1441,13 @@ public class IpcTester : IDisposable } } - private class ResourceTree + private class ResourceTree(DalamudPluginInterface pi, ObjectManager objects) { - private readonly DalamudPluginInterface _pi; - private readonly IObjectTable _objects; - private readonly Stopwatch _stopwatch = new(); + private readonly Stopwatch _stopwatch = new(); private string _gameObjectIndices = "0"; private ResourceType _type = ResourceType.Mtrl; - private bool _withUIData = false; + private bool _withUiData; private (string, IReadOnlyDictionary?)[]? _lastGameObjectResourcePaths; private (string, IReadOnlyDictionary?)[]? _lastPlayerResourcePaths; @@ -1459,12 +1457,6 @@ public class IpcTester : IDisposable private (string, Ipc.ResourceTree)[]? _lastPlayerResourceTrees; private TimeSpan _lastCallDuration; - public ResourceTree(DalamudPluginInterface pi, IObjectTable objects) - { - _pi = pi; - _objects = objects; - } - public void Draw() { using var _ = ImRaii.TreeNode("Resource Tree"); @@ -1473,7 +1465,7 @@ public class IpcTester : IDisposable ImGui.InputText("GameObject indices", ref _gameObjectIndices, 511); ImGuiUtil.GenericEnumCombo("Resource type", ImGui.CalcItemWidth(), _type, out _type, Enum.GetValues()); - ImGui.Checkbox("Also get names and icons", ref _withUIData); + ImGui.Checkbox("Also get names and icons", ref _withUiData); using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); if (!table) @@ -1483,7 +1475,7 @@ public class IpcTester : IDisposable if (ImGui.Button("Get##GameObjectResourcePaths")) { var gameObjects = GetSelectedGameObjects(); - var subscriber = Ipc.GetGameObjectResourcePaths.Subscriber(_pi); + var subscriber = Ipc.GetGameObjectResourcePaths.Subscriber(pi); _stopwatch.Restart(); var resourcePaths = subscriber.Invoke(gameObjects); @@ -1499,7 +1491,7 @@ public class IpcTester : IDisposable DrawIntro(Ipc.GetPlayerResourcePaths.Label, "Get local player resource paths"); if (ImGui.Button("Get##PlayerResourcePaths")) { - var subscriber = Ipc.GetPlayerResourcePaths.Subscriber(_pi); + var subscriber = Ipc.GetPlayerResourcePaths.Subscriber(pi); _stopwatch.Restart(); var resourcePaths = subscriber.Invoke(); @@ -1515,9 +1507,9 @@ public class IpcTester : IDisposable if (ImGui.Button("Get##GameObjectResourcesOfType")) { var gameObjects = GetSelectedGameObjects(); - var subscriber = Ipc.GetGameObjectResourcesOfType.Subscriber(_pi); + var subscriber = Ipc.GetGameObjectResourcesOfType.Subscriber(pi); _stopwatch.Restart(); - var resourcesOfType = subscriber.Invoke(_type, _withUIData, gameObjects); + var resourcesOfType = subscriber.Invoke(_type, _withUiData, gameObjects); _lastCallDuration = _stopwatch.Elapsed; _lastGameObjectResourcesOfType = gameObjects @@ -1531,9 +1523,9 @@ public class IpcTester : IDisposable DrawIntro(Ipc.GetPlayerResourcesOfType.Label, "Get local player resources of type"); if (ImGui.Button("Get##PlayerResourcesOfType")) { - var subscriber = Ipc.GetPlayerResourcesOfType.Subscriber(_pi); + var subscriber = Ipc.GetPlayerResourcesOfType.Subscriber(pi); _stopwatch.Restart(); - var resourcesOfType = subscriber.Invoke(_type, _withUIData); + var resourcesOfType = subscriber.Invoke(_type, _withUiData); _lastCallDuration = _stopwatch.Elapsed; _lastPlayerResourcesOfType = resourcesOfType @@ -1547,9 +1539,9 @@ public class IpcTester : IDisposable if (ImGui.Button("Get##GameObjectResourceTrees")) { var gameObjects = GetSelectedGameObjects(); - var subscriber = Ipc.GetGameObjectResourceTrees.Subscriber(_pi); + var subscriber = Ipc.GetGameObjectResourceTrees.Subscriber(pi); _stopwatch.Restart(); - var trees = subscriber.Invoke(_withUIData, gameObjects); + var trees = subscriber.Invoke(_withUiData, gameObjects); _lastCallDuration = _stopwatch.Elapsed; _lastGameObjectResourceTrees = gameObjects @@ -1563,9 +1555,9 @@ public class IpcTester : IDisposable DrawIntro(Ipc.GetPlayerResourceTrees.Label, "Get local player resource trees"); if (ImGui.Button("Get##PlayerResourceTrees")) { - var subscriber = Ipc.GetPlayerResourceTrees.Subscriber(_pi); + var subscriber = Ipc.GetPlayerResourceTrees.Subscriber(pi); _stopwatch.Restart(); - var trees = subscriber.Invoke(_withUIData); + var trees = subscriber.Invoke(_withUiData); _lastCallDuration = _stopwatch.Elapsed; _lastPlayerResourceTrees = trees @@ -1666,13 +1658,13 @@ public class IpcTester : IDisposable { DrawWithHeaders(result, resources => { - using var table = ImRaii.Table(string.Empty, _withUIData ? 3 : 2, ImGuiTableFlags.SizingFixedFit); + using var table = ImRaii.Table(string.Empty, _withUiData ? 3 : 2, ImGuiTableFlags.SizingFixedFit); if (!table) return; ImGui.TableSetupColumn("Resource Handle", ImGuiTableColumnFlags.WidthStretch, 0.15f); - ImGui.TableSetupColumn("Actual Path", ImGuiTableColumnFlags.WidthStretch, _withUIData ? 0.55f : 0.85f); - if (_withUIData) + ImGui.TableSetupColumn("Actual Path", ImGuiTableColumnFlags.WidthStretch, _withUiData ? 0.55f : 0.85f); + if (_withUiData) ImGui.TableSetupColumn("Icon & Name", ImGuiTableColumnFlags.WidthStretch, 0.3f); ImGui.TableHeadersRow(); @@ -1682,7 +1674,7 @@ public class IpcTester : IDisposable TextUnformattedMono($"0x{resourceHandle:X}"); ImGui.TableNextColumn(); ImGui.TextUnformatted(actualPath); - if (_withUIData) + if (_withUiData) { ImGui.TableNextColumn(); TextUnformattedMono(icon.ToString()); @@ -1699,11 +1691,11 @@ public class IpcTester : IDisposable { ImGui.TextUnformatted($"Name: {tree.Name}\nRaceCode: {(GenderRace)tree.RaceCode}"); - using var table = ImRaii.Table(string.Empty, _withUIData ? 7 : 5, ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.Resizable); + using var table = ImRaii.Table(string.Empty, _withUiData ? 7 : 5, ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.Resizable); if (!table) return; - if (_withUIData) + if (_withUiData) { ImGui.TableSetupColumn("Name", ImGuiTableColumnFlags.WidthStretch, 0.5f); ImGui.TableSetupColumn("Type", ImGuiTableColumnFlags.WidthStretch, 0.1f); @@ -1726,11 +1718,11 @@ public class IpcTester : IDisposable ImGui.TableNextColumn(); var hasChildren = node.Children.Any(); using var treeNode = ImRaii.TreeNode( - $"{(_withUIData ? node.Name ?? "Unknown" : node.Type)}##{node.ObjectAddress:X8}", + $"{(_withUiData ? node.Name ?? "Unknown" : node.Type)}##{node.ObjectAddress:X8}", hasChildren ? ImGuiTreeNodeFlags.SpanFullWidth : ImGuiTreeNodeFlags.SpanFullWidth | ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.NoTreePushOnOpen); - if (_withUIData) + if (_withUiData) { ImGui.TableNextColumn(); TextUnformattedMono(node.Type.ToString()); @@ -1770,10 +1762,10 @@ public class IpcTester : IDisposable private unsafe string GameObjectToString(ObjectIndex gameObjectIndex) { - var gameObject = _objects[gameObjectIndex.Index]; + var gameObject = objects[gameObjectIndex]; - return gameObject != null - ? $"[{gameObjectIndex}] {gameObject.Name} ({gameObject.ObjectKind})" + return gameObject.Valid + ? $"[{gameObjectIndex}] {gameObject.Utf8Name} ({(ObjectKind)gameObject.AsObject->ObjectKind})" : $"[{gameObjectIndex}] null"; } } diff --git a/Penumbra/Api/PenumbraApi.cs b/Penumbra/Api/PenumbraApi.cs index f5bb67bd..5146383d 100644 --- a/Penumbra/Api/PenumbraApi.cs +++ b/Penumbra/Api/PenumbraApi.cs @@ -20,6 +20,7 @@ using Penumbra.String.Classes; using Penumbra.Services; using Penumbra.Collections.Manager; using Penumbra.Communication; +using Penumbra.GameData.Interop; using Penumbra.Import.Textures; using Penumbra.Interop.Services; using Penumbra.UI; @@ -93,7 +94,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi private IDataManager _gameData; private IFramework _framework; - private IObjectTable _objects; + private ObjectManager _objects; private ModManager _modManager; private ResourceLoader _resourceLoader; private Configuration _config; @@ -111,7 +112,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi private TextureManager _textureManager; private ResourceTreeFactory _resourceTreeFactory; - public unsafe PenumbraApi(CommunicatorService communicator, IDataManager gameData, IFramework framework, IObjectTable objects, + public unsafe PenumbraApi(CommunicatorService communicator, IDataManager gameData, IFramework framework, ObjectManager objects, ModManager modManager, ResourceLoader resourceLoader, Configuration config, CollectionManager collectionManager, TempCollectionManager tempCollections, TempModManager tempMods, ActorManager actors, CollectionResolver collectionResolver, CutsceneService cutsceneService, ModImportManager modImportManager, CollectionEditor collectionEditor, RedrawService redrawService, @@ -928,10 +929,10 @@ public class PenumbraApi : IDisposable, IPenumbraApi { CheckInitialized(); - if (actorIndex < 0 || actorIndex >= _objects.Length) + if (actorIndex < 0 || actorIndex >= _objects.Count) return PenumbraApiEc.InvalidArgument; - var identifier = _actors.FromObject(_objects[actorIndex], false, false, true); + var identifier = _actors.FromObject(_objects[actorIndex], out _, false, false, true); if (!identifier.IsValid) return PenumbraApiEc.InvalidArgument; @@ -1076,11 +1077,11 @@ public class PenumbraApi : IDisposable, IPenumbraApi public IReadOnlyDictionary?[] GetGameObjectResourcePaths(ushort[] gameObjects) { - var characters = gameObjects.Select(index => _objects[index]).OfType(); + var characters = gameObjects.Select(index => _objects.GetDalamudObject((int) index)).OfType(); var resourceTrees = _resourceTreeFactory.FromCharacters(characters, 0); var pathDictionaries = ResourceTreeApiHelper.GetResourcePathDictionaries(resourceTrees); - return Array.ConvertAll(gameObjects, obj => pathDictionaries.TryGetValue(obj, out var pathDict) ? pathDict : null); + return Array.ConvertAll(gameObjects, obj => pathDictionaries.GetValueOrDefault(obj)); } public IReadOnlyDictionary> GetPlayerResourcePaths() @@ -1091,39 +1092,39 @@ public class PenumbraApi : IDisposable, IPenumbraApi return pathDictionaries.AsReadOnly(); } - public IReadOnlyDictionary?[] GetGameObjectResourcesOfType(ResourceType type, bool withUIData, + public IReadOnlyDictionary?[] GetGameObjectResourcesOfType(ResourceType type, bool withUiData, params ushort[] gameObjects) { - var characters = gameObjects.Select(index => _objects[index]).OfType(); - var resourceTrees = _resourceTreeFactory.FromCharacters(characters, withUIData ? ResourceTreeFactory.Flags.WithUiData : 0); + var characters = gameObjects.Select(index => _objects.GetDalamudObject((int)index)).OfType(); + var resourceTrees = _resourceTreeFactory.FromCharacters(characters, withUiData ? ResourceTreeFactory.Flags.WithUiData : 0); var resDictionaries = ResourceTreeApiHelper.GetResourcesOfType(resourceTrees, type); - return Array.ConvertAll(gameObjects, obj => resDictionaries.TryGetValue(obj, out var resDict) ? resDict : null); + return Array.ConvertAll(gameObjects, obj => resDictionaries.GetValueOrDefault(obj)); } public IReadOnlyDictionary> GetPlayerResourcesOfType(ResourceType type, - bool withUIData) + bool withUiData) { var resourceTrees = _resourceTreeFactory.FromObjectTable(ResourceTreeFactory.Flags.LocalPlayerRelatedOnly - | (withUIData ? ResourceTreeFactory.Flags.WithUiData : 0)); + | (withUiData ? ResourceTreeFactory.Flags.WithUiData : 0)); var resDictionaries = ResourceTreeApiHelper.GetResourcesOfType(resourceTrees, type); return resDictionaries.AsReadOnly(); } - public Ipc.ResourceTree?[] GetGameObjectResourceTrees(bool withUIData, params ushort[] gameObjects) + public Ipc.ResourceTree?[] GetGameObjectResourceTrees(bool withUiData, params ushort[] gameObjects) { - var characters = gameObjects.Select(index => _objects[index]).OfType(); - var resourceTrees = _resourceTreeFactory.FromCharacters(characters, withUIData ? ResourceTreeFactory.Flags.WithUiData : 0); + var characters = gameObjects.Select(index => _objects.GetDalamudObject((int)index)).OfType(); + var resourceTrees = _resourceTreeFactory.FromCharacters(characters, withUiData ? ResourceTreeFactory.Flags.WithUiData : 0); var resDictionary = ResourceTreeApiHelper.EncapsulateResourceTrees(resourceTrees); - return Array.ConvertAll(gameObjects, obj => resDictionary.TryGetValue(obj, out var nodes) ? nodes : null); + return Array.ConvertAll(gameObjects, obj => resDictionary.GetValueOrDefault(obj)); } - public IReadOnlyDictionary GetPlayerResourceTrees(bool withUIData) + public IReadOnlyDictionary GetPlayerResourceTrees(bool withUiData) { var resourceTrees = _resourceTreeFactory.FromObjectTable(ResourceTreeFactory.Flags.LocalPlayerRelatedOnly - | (withUIData ? ResourceTreeFactory.Flags.WithUiData : 0)); + | (withUiData ? ResourceTreeFactory.Flags.WithUiData : 0)); var resDictionary = ResourceTreeApiHelper.EncapsulateResourceTrees(resourceTrees); return resDictionary.AsReadOnly(); @@ -1165,11 +1166,11 @@ public class PenumbraApi : IDisposable, IPenumbraApi private unsafe bool AssociatedCollection(int gameObjectIdx, out ModCollection collection) { collection = _collectionManager.Active.Default; - if (gameObjectIdx < 0 || gameObjectIdx >= _objects.Length) + if (gameObjectIdx < 0 || gameObjectIdx >= _objects.Count) return false; - var ptr = (FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject*)_objects.GetObjectAddress(gameObjectIdx); - var data = _collectionResolver.IdentifyCollection(ptr, false); + var ptr = _objects[gameObjectIdx]; + var data = _collectionResolver.IdentifyCollection(ptr.AsObject, false); if (data.Valid) collection = data.ModCollection; @@ -1179,10 +1180,10 @@ public class PenumbraApi : IDisposable, IPenumbraApi [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private unsafe ActorIdentifier AssociatedIdentifier(int gameObjectIdx) { - if (gameObjectIdx < 0 || gameObjectIdx >= _objects.Length) + if (gameObjectIdx < 0 || gameObjectIdx >= _objects.Count) return ActorIdentifier.Invalid; - var ptr = (FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject*)_objects.GetObjectAddress(gameObjectIdx); + var ptr = _objects[gameObjectIdx]; return _actors.FromObject(ptr, out _, false, true, true); } diff --git a/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs b/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs index 69c22773..77aaa742 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs @@ -1,9 +1,9 @@ -using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Object; using OtterGui.Services; using Penumbra.Collections; using Penumbra.CrashHandler.Buffers; using Penumbra.GameData; +using Penumbra.GameData.Interop; using Penumbra.Interop.PathResolving; using Penumbra.Interop.Structs; using Penumbra.Services; @@ -16,10 +16,10 @@ public sealed unsafe class LoadCharacterVfx : FastHookGameObjectType switch { - 0 => _objects.SearchById(vfxParams->GameObjectId), + 0 => _objects.ById(vfxParams->GameObjectId), 2 => _objects[(int)vfxParams->GameObjectId], 4 => GetOwnedObject(vfxParams->GameObjectId), - _ => null, + _ => Actor.Null, }; - newData = obj != null + newData = obj.Valid ? _collectionResolver.IdentifyCollection((GameObject*)obj.Address, true) : ResolveData.Invalid; } var last = _state.SetAnimationData(newData); _crashHandler.LogAnimation(newData.AssociatedGameObject, newData.ModCollection, AnimationInvocationType.LoadCharacterVfx); - var ret = Task.Result.Original(vfxPath, vfxParams, unk1, unk2, unk3, unk4); + var ret = Task.Result.Original(vfxPath, vfxParams, unk1, unk2, unk3, unk4); Penumbra.Log.Excessive( $"[Load Character VFX] Invoked with {new ByteString(vfxPath)}, 0x{vfxParams->GameObjectId:X}, {vfxParams->TargetCount}, {unk1}, {unk2}, {unk3}, {unk4} -> 0x{ret:X}."); _state.RestoreAnimationData(last); @@ -59,13 +59,11 @@ public sealed unsafe class LoadCharacterVfx : FastHook Search an object by its id, then get its minion/mount/ornament. - private Dalamud.Game.ClientState.Objects.Types.GameObject? GetOwnedObject(uint id) + private Actor GetOwnedObject(uint id) { - var owner = _objects.SearchById(id); - if (owner == null) - return null; - - var idx = ((GameObject*)owner.Address)->ObjectIndex; - return _objects[idx + 1]; + var owner = _objects.ById(id); + return !owner.Valid + ? Actor.Null + : _objects[owner.Index.Index + 1]; } } diff --git a/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs b/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs index ade957b9..4bae084e 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs @@ -3,8 +3,8 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Object; using OtterGui.Services; using Penumbra.Collections; -using Penumbra.CrashHandler; using Penumbra.GameData; +using Penumbra.GameData.Interop; using Penumbra.Interop.PathResolving; using Penumbra.Services; @@ -19,11 +19,11 @@ public sealed unsafe class LoadTimelineResources : FastHook Use timelines vfuncs to obtain the associated game object. - public static ResolveData GetDataFromTimeline(IObjectTable objects, CollectionResolver resolver, nint timeline) + public static ResolveData GetDataFromTimeline(ObjectManager objects, CollectionResolver resolver, nint timeline) { try { @@ -64,10 +64,10 @@ public sealed unsafe class LoadTimelineResources : FastHook**)timeline)[0][Offsets.GetGameObjectIdxVfunc]; var idx = getGameObjectIdx(timeline); - if (idx >= 0 && idx < objects.Length) + if (idx >= 0 && idx < objects.Count) { - var obj = (GameObject*)objects.GetObjectAddress(idx); - return obj != null ? resolver.IdentifyCollection(obj, true) : ResolveData.Invalid; + var obj = objects[idx]; + return obj.Valid ? resolver.IdentifyCollection(obj.AsObject, true) : ResolveData.Invalid; } } } diff --git a/Penumbra/Interop/Hooks/Animation/ScheduleClipUpdate.cs b/Penumbra/Interop/Hooks/Animation/ScheduleClipUpdate.cs index 91b63838..342ffc25 100644 --- a/Penumbra/Interop/Hooks/Animation/ScheduleClipUpdate.cs +++ b/Penumbra/Interop/Hooks/Animation/ScheduleClipUpdate.cs @@ -1,7 +1,7 @@ -using Dalamud.Plugin.Services; using OtterGui.Services; using Penumbra.CrashHandler.Buffers; using Penumbra.GameData; +using Penumbra.GameData.Interop; using Penumbra.Interop.PathResolving; using Penumbra.Interop.Structs; using Penumbra.Services; @@ -13,10 +13,10 @@ public sealed unsafe class ScheduleClipUpdate : FastHook { private readonly GameState _state; private readonly CollectionResolver _collectionResolver; - private readonly IObjectTable _objects; + private readonly ObjectManager _objects; private readonly CrashHandlerService _crashHandler; - public SomePapLoad(HookManager hooks, GameState state, CollectionResolver collectionResolver, IObjectTable objects, + public SomePapLoad(HookManager hooks, GameState state, CollectionResolver collectionResolver, ObjectManager objects, CrashHandlerService crashHandler) { _state = state; @@ -36,9 +35,9 @@ public sealed unsafe class SomePapLoad : FastHook if (timelinePtr != nint.Zero) { var actorIdx = (int)(*(*(ulong**)timelinePtr + 1) >> 3); - if (actorIdx >= 0 && actorIdx < _objects.Length) + if (actorIdx >= 0 && actorIdx < _objects.Count) { - var newData = _collectionResolver.IdentifyCollection((GameObject*)_objects.GetObjectAddress(actorIdx), true); + var newData = _collectionResolver.IdentifyCollection(_objects[actorIdx].AsObject, true); var last = _state.SetAnimationData(newData); _crashHandler.LogAnimation(newData.AssociatedGameObject, newData.ModCollection, AnimationInvocationType.PapLoad); Task.Result.Original(a1, a2, a3, a4); diff --git a/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs b/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs index a8e4ea4d..4d35e68a 100644 --- a/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs +++ b/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs @@ -1,6 +1,7 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Graphics.Kernel; using Penumbra.GameData.Files; +using Penumbra.GameData.Interop; using Penumbra.Interop.SafeHandles; namespace Penumbra.Interop.MaterialPreview; @@ -20,7 +21,7 @@ public sealed unsafe class LiveColorTablePreviewer : LiveMaterialPreviewerBase public Half[] ColorTable { get; } - public LiveColorTablePreviewer(IObjectTable objects, IFramework framework, MaterialInfo materialInfo) + public LiveColorTablePreviewer(ObjectManager objects, IFramework framework, MaterialInfo materialInfo) : base(objects, materialInfo) { _framework = framework; diff --git a/Penumbra/Interop/MaterialPreview/LiveMaterialPreviewer.cs b/Penumbra/Interop/MaterialPreview/LiveMaterialPreviewer.cs index 9ed7ca3d..0556fdc4 100644 --- a/Penumbra/Interop/MaterialPreview/LiveMaterialPreviewer.cs +++ b/Penumbra/Interop/MaterialPreview/LiveMaterialPreviewer.cs @@ -1,5 +1,5 @@ -using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Graphics.Kernel; +using Penumbra.GameData.Interop; namespace Penumbra.Interop.MaterialPreview; @@ -11,7 +11,7 @@ public sealed unsafe class LiveMaterialPreviewer : LiveMaterialPreviewerBase private readonly float[] _originalMaterialParameter; private readonly uint[] _originalSamplerFlags; - public LiveMaterialPreviewer(IObjectTable objects, MaterialInfo materialInfo) + public LiveMaterialPreviewer(ObjectManager objects, MaterialInfo materialInfo) : base(objects, materialInfo) { var mtrlHandle = Material->MaterialResourceHandle; diff --git a/Penumbra/Interop/MaterialPreview/LiveMaterialPreviewerBase.cs b/Penumbra/Interop/MaterialPreview/LiveMaterialPreviewerBase.cs index 07986f52..f176990e 100644 --- a/Penumbra/Interop/MaterialPreview/LiveMaterialPreviewerBase.cs +++ b/Penumbra/Interop/MaterialPreview/LiveMaterialPreviewerBase.cs @@ -1,12 +1,13 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Graphics.Render; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using Penumbra.GameData.Interop; namespace Penumbra.Interop.MaterialPreview; public abstract unsafe class LiveMaterialPreviewerBase : IDisposable { - private readonly IObjectTable _objects; + private readonly ObjectManager _objects; public readonly MaterialInfo MaterialInfo; public readonly CharacterBase* DrawObject; @@ -14,7 +15,7 @@ public abstract unsafe class LiveMaterialPreviewerBase : IDisposable protected bool Valid; - public LiveMaterialPreviewerBase(IObjectTable objects, MaterialInfo materialInfo) + public LiveMaterialPreviewerBase(ObjectManager objects, MaterialInfo materialInfo) { _objects = objects; diff --git a/Penumbra/Interop/MaterialPreview/MaterialInfo.cs b/Penumbra/Interop/MaterialPreview/MaterialInfo.cs index 686b5a86..61e7c764 100644 --- a/Penumbra/Interop/MaterialPreview/MaterialInfo.cs +++ b/Penumbra/Interop/MaterialPreview/MaterialInfo.cs @@ -1,10 +1,11 @@ -using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Character; using FFXIVClientStructs.FFXIV.Client.Graphics.Render; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using Penumbra.GameData.Interop; using Penumbra.GameData.Structs; using Penumbra.Interop.ResourceTree; using Penumbra.String; +using Model = Penumbra.GameData.Interop.Model; namespace Penumbra.Interop.MaterialPreview; @@ -18,13 +19,13 @@ public enum DrawObjectType public readonly record struct MaterialInfo(ObjectIndex ObjectIndex, DrawObjectType Type, int ModelSlot, int MaterialSlot) { - public nint GetCharacter(IObjectTable objects) - => objects.GetObjectAddress(ObjectIndex.Index); + public Actor GetCharacter(ObjectManager objects) + => objects[ObjectIndex]; - public nint GetDrawObject(nint address) + public nint GetDrawObject(Actor address) => GetDrawObject(Type, address); - public unsafe Material* GetDrawObjectMaterial(IObjectTable objects) + public unsafe Material* GetDrawObjectMaterial(ObjectManager objects) => GetDrawObjectMaterial((CharacterBase*)GetDrawObject(GetCharacter(objects))); public unsafe Material* GetDrawObjectMaterial(CharacterBase* drawObject) @@ -60,13 +61,13 @@ public readonly record struct MaterialInfo(ObjectIndex ObjectIndex, DrawObjectTy foreach (var type in Enum.GetValues()) { - var drawObject = (CharacterBase*)GetDrawObject(type, objectPtr); - if (drawObject == null) + var drawObject = GetDrawObject(type, objectPtr); + if (!drawObject.Valid) continue; - for (var i = 0; i < drawObject->SlotCount; ++i) + for (var i = 0; i < drawObject.AsCharacterBase->SlotCount; ++i) { - var model = drawObject->Models[i]; + var model = drawObject.AsCharacterBase->Models[i]; if (model == null) continue; @@ -88,19 +89,18 @@ public readonly record struct MaterialInfo(ObjectIndex ObjectIndex, DrawObjectTy return result; } - private static unsafe nint GetDrawObject(DrawObjectType type, nint address) + private static unsafe Model GetDrawObject(DrawObjectType type, Actor address) { - var gameObject = (Character*)address; - if (gameObject == null) - return nint.Zero; + if (!address.Valid) + return Model.Null; return type switch { - DrawObjectType.Character => (nint)gameObject->GameObject.GetDrawObject(), - DrawObjectType.Mainhand => (nint)gameObject->DrawData.Weapon(DrawDataContainer.WeaponSlot.MainHand).DrawObject, - DrawObjectType.Offhand => (nint)gameObject->DrawData.Weapon(DrawDataContainer.WeaponSlot.OffHand).DrawObject, - DrawObjectType.Vfx => (nint)gameObject->DrawData.Weapon(DrawDataContainer.WeaponSlot.Unk).DrawObject, - _ => nint.Zero, + DrawObjectType.Character => address.Model, + DrawObjectType.Mainhand => address.AsCharacter->DrawData.Weapon(DrawDataContainer.WeaponSlot.MainHand).DrawObject, + DrawObjectType.Offhand => address.AsCharacter->DrawData.Weapon(DrawDataContainer.WeaponSlot.OffHand).DrawObject, + DrawObjectType.Vfx => address.AsCharacter->DrawData.Weapon(DrawDataContainer.WeaponSlot.Unk).DrawObject, + _ => Model.Null, }; } } diff --git a/Penumbra/Interop/PathResolving/CollectionResolver.cs b/Penumbra/Interop/PathResolving/CollectionResolver.cs index 1a715f13..fa122e39 100644 --- a/Penumbra/Interop/PathResolving/CollectionResolver.cs +++ b/Penumbra/Interop/PathResolving/CollectionResolver.cs @@ -6,6 +6,7 @@ using Penumbra.Collections.Manager; using Penumbra.GameData.Actors; using Penumbra.GameData.DataContainers; using Penumbra.GameData.Enums; +using Penumbra.GameData.Interop; using Penumbra.Util; using Character = FFXIVClientStructs.FFXIV.Client.Game.Character.Character; using GameObject = FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject; @@ -170,10 +171,10 @@ public sealed unsafe class CollectionResolver( : null; /// Check for the Yourself collection. - private ModCollection? CheckYourself(ActorIdentifier identifier, GameObject* actor) + private ModCollection? CheckYourself(ActorIdentifier identifier, Actor actor) { - if (actor->ObjectIndex == 0 - || cutscenes.GetParentIndex(actor->ObjectIndex) == 0 + if (actor.Index == 0 + || cutscenes.GetParentIndex(actor.Index.Index) == 0 || identifier.Equals(actors.GetCurrentPlayer())) return collectionManager.Active.ByType(CollectionType.Yourself); @@ -181,23 +182,23 @@ public sealed unsafe class CollectionResolver( } /// Check special collections given the actor. Returns notYetReady if the customize array is not filled. - private ModCollection? CollectionByAttributes(GameObject* actor, ref bool notYetReady) + private ModCollection? CollectionByAttributes(Actor actor, ref bool notYetReady) { - if (!actor->IsCharacter()) + if (!actor.IsCharacter) return null; // Only handle human models. - var character = (Character*)actor; - if (!IsModelHuman((uint)character->CharacterData.ModelCharaId)) + + if (!IsModelHuman((uint)actor.AsCharacter->CharacterData.ModelCharaId)) return null; - if (character->DrawData.CustomizeData[0] == 0) + if (actor.Customize->Data[0] == 0) { notYetReady = true; return null; } - var bodyType = character->DrawData.CustomizeData[2]; + var bodyType = actor.Customize->Data[2]; var collection = bodyType switch { 3 => collectionManager.Active.ByType(CollectionType.NonPlayerElderly), @@ -207,9 +208,9 @@ public sealed unsafe class CollectionResolver( if (collection != null) return collection; - var race = (SubRace)character->DrawData.CustomizeData[4]; - var gender = (Gender)(character->DrawData.CustomizeData[1] + 1); - var isNpc = actor->ObjectKind != (byte)ObjectKind.Player; + var race = (SubRace)actor.Customize->Data[4]; + var gender = (Gender)(actor.Customize->Data[1] + 1); + var isNpc = !actor.IsPlayer; var type = CollectionTypeExtensions.FromParts(race, gender, isNpc); collection = collectionManager.Active.ByType(type); @@ -218,15 +219,14 @@ public sealed unsafe class CollectionResolver( } /// Get the collection applying to the owner if it is available. - private ModCollection? CheckOwnedCollection(ActorIdentifier identifier, GameObject* owner, ref bool notYetReady) + private ModCollection? CheckOwnedCollection(ActorIdentifier identifier, Actor owner, ref bool notYetReady) { - if (identifier.Type != IdentifierType.Owned || !config.UseOwnerNameForCharacterCollection || owner == null) + if (identifier.Type != IdentifierType.Owned || !config.UseOwnerNameForCharacterCollection || !owner.Valid) return null; var id = actors.CreateIndividualUnchecked(IdentifierType.Player, identifier.PlayerName, identifier.HomeWorld.Id, ObjectKind.None, uint.MaxValue); - return CheckYourself(id, owner) - ?? CollectionByAttributes(owner, ref notYetReady); + return CheckYourself(id, owner) ?? CollectionByAttributes(owner, ref notYetReady); } } diff --git a/Penumbra/Interop/PathResolving/CutsceneService.cs b/Penumbra/Interop/PathResolving/CutsceneService.cs index 89b9f917..93fee11e 100644 --- a/Penumbra/Interop/PathResolving/CutsceneService.cs +++ b/Penumbra/Interop/PathResolving/CutsceneService.cs @@ -3,6 +3,7 @@ using FFXIVClientStructs.FFXIV.Client.Game.Character; using FFXIVClientStructs.FFXIV.Client.Game.Object; using OtterGui.Services; using Penumbra.GameData.Enums; +using Penumbra.GameData.Interop; using Penumbra.Interop.Hooks.Objects; using Penumbra.String; @@ -14,17 +15,17 @@ public sealed class CutsceneService : IService, IDisposable public const int CutsceneEndIdx = (int)ScreenActor.CutsceneEnd; public const int CutsceneSlots = CutsceneEndIdx - CutsceneStartIdx; - private readonly IObjectTable _objects; + private readonly ObjectManager _objects; private readonly CopyCharacter _copyCharacter; private readonly CharacterDestructor _characterDestructor; private readonly short[] _copiedCharacters = Enumerable.Repeat((short)-1, CutsceneSlots).ToArray(); public IEnumerable> Actors => Enumerable.Range(CutsceneStartIdx, CutsceneSlots) - .Where(i => _objects[i] != null) - .Select(i => KeyValuePair.Create(i, this[i] ?? _objects[i]!)); + .Where(i => _objects[i].Valid) + .Select(i => KeyValuePair.Create(i, this[i] ?? _objects.GetDalamudObject(i)!)); - public unsafe CutsceneService(IObjectTable objects, CopyCharacter copyCharacter, CharacterDestructor characterDestructor, + public unsafe CutsceneService(ObjectManager objects, CopyCharacter copyCharacter, CharacterDestructor characterDestructor, IClientState clientState) { _objects = objects; @@ -42,13 +43,13 @@ public sealed class CutsceneService : IService, IDisposable /// Does not check for valid input index. /// Returns null if no connected actor is set or the actor does not exist anymore. /// - public Dalamud.Game.ClientState.Objects.Types.GameObject? this[int idx] + private Dalamud.Game.ClientState.Objects.Types.GameObject? this[int idx] { get { Debug.Assert(idx is >= CutsceneStartIdx and < CutsceneEndIdx); idx = _copiedCharacters[idx - CutsceneStartIdx]; - return idx < 0 ? null : _objects[idx]; + return idx < 0 ? null : _objects.GetDalamudObject(idx); } } @@ -64,10 +65,10 @@ public sealed class CutsceneService : IService, IDisposable if (parentIdx is < -1 or >= CutsceneEndIdx) return false; - if (_objects.GetObjectAddress(copyIdx) == nint.Zero) + if (!_objects[copyIdx].Valid) return false; - if (parentIdx != -1 && _objects.GetObjectAddress(parentIdx) == nint.Zero) + if (parentIdx != -1 && !_objects[parentIdx].Valid) return false; _copiedCharacters[copyIdx - CutsceneStartIdx] = (short)parentIdx; @@ -99,9 +100,9 @@ public sealed class CutsceneService : IService, IDisposable { // A hack to deal with GPose actors leaving and thus losing the link, we just set the home world instead. // I do not think this breaks anything? - var address = (GameObject*)_objects.GetObjectAddress(i + CutsceneStartIdx); - if (address != null && address->GetObjectKind() is (byte)ObjectKind.Pc) - ((Character*)address)->HomeWorld = character->HomeWorld; + var address = _objects[i + CutsceneStartIdx]; + if (address.IsPlayer) + address.AsCharacter->HomeWorld = character->HomeWorld; _copiedCharacters[i] = -1; } @@ -125,7 +126,7 @@ public sealed class CutsceneService : IService, IDisposable /// Try to recover GPose actors on reloads into a running game. /// This is not 100% accurate due to world IDs, minions etc., but will be mostly sane. - private unsafe void RecoverGPoseActors() + private void RecoverGPoseActors() { Dictionary? actors = null; @@ -143,11 +144,11 @@ public sealed class CutsceneService : IService, IDisposable bool TryGetName(int idx, out ByteString name) { name = ByteString.Empty; - var address = (GameObject*)_objects.GetObjectAddress(idx); - if (address == null) + var address = _objects[idx]; + if (!address.Valid) return false; - name = new ByteString(address->Name); + name = address.Utf8Name; return !name.IsEmpty; } diff --git a/Penumbra/Interop/PathResolving/DrawObjectState.cs b/Penumbra/Interop/PathResolving/DrawObjectState.cs index b3ae108b..784ac3fb 100644 --- a/Penumbra/Interop/PathResolving/DrawObjectState.cs +++ b/Penumbra/Interop/PathResolving/DrawObjectState.cs @@ -1,8 +1,7 @@ -using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Character; -using FFXIVClientStructs.FFXIV.Client.Game.Object; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; +using Penumbra.GameData.Interop; using Object = FFXIVClientStructs.FFXIV.Client.Graphics.Scene.Object; using Penumbra.GameData.Structs; using Penumbra.Interop.Hooks.Objects; @@ -11,7 +10,7 @@ namespace Penumbra.Interop.PathResolving; public sealed class DrawObjectState : IDisposable, IReadOnlyDictionary, IService { - private readonly IObjectTable _objects; + private readonly ObjectManager _objects; private readonly CreateCharacterBase _createCharacterBase; private readonly WeaponReload _weaponReload; private readonly CharacterBaseDestructor _characterBaseDestructor; @@ -22,7 +21,7 @@ public sealed class DrawObjectState : IDisposable, IReadOnlyDictionary _gameState.LastGameObject; - public unsafe DrawObjectState(IObjectTable objects, CreateCharacterBase createCharacterBase, WeaponReload weaponReload, + public unsafe DrawObjectState(ObjectManager objects, CreateCharacterBase createCharacterBase, WeaponReload weaponReload, CharacterBaseDestructor characterBaseDestructor, GameState gameState) { _objects = objects; @@ -95,11 +94,11 @@ public sealed class DrawObjectState : IDisposable, IReadOnlyDictionary private unsafe void InitializeDrawObjects() { - for (var i = 0; i < _objects.Length; ++i) + for (var i = 0; i < _objects.Count; ++i) { - var ptr = (GameObject*)_objects.GetObjectAddress(i); - if (ptr != null && ptr->IsCharacter() && ptr->DrawObject != null) - IterateDrawObjectTree(&ptr->DrawObject->Object, (nint)ptr, false, false); + var ptr = _objects[i]; + if (ptr is { IsCharacter: true, Model.Valid: true }) + IterateDrawObjectTree((Object*)ptr.Model.Address, ptr, false, false); } } diff --git a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs index df5e1964..ae7187f0 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs @@ -1,39 +1,26 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Object; using Penumbra.Api.Enums; -using Penumbra.Collections; using Penumbra.GameData.Actors; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; +using Penumbra.GameData.Interop; using Penumbra.Interop.PathResolving; using Penumbra.String.Classes; namespace Penumbra.Interop.ResourceTree; -public class ResourceTreeFactory +public class ResourceTreeFactory( + IDataManager gameData, + ObjectManager objects, + CollectionResolver resolver, + ObjectIdentification identifier, + Configuration config, + ActorManager actors, + PathState pathState) { - private readonly IDataManager _gameData; - private readonly IObjectTable _objects; - private readonly CollectionResolver _collectionResolver; - private readonly ObjectIdentification _identifier; - private readonly Configuration _config; - private readonly ActorManager _actors; - private readonly PathState _pathState; - - public ResourceTreeFactory(IDataManager gameData, IObjectTable objects, CollectionResolver resolver, ObjectIdentification identifier, - Configuration config, ActorManager actors, PathState pathState) - { - _gameData = gameData; - _objects = objects; - _collectionResolver = resolver; - _identifier = identifier; - _config = config; - _actors = actors; - _pathState = pathState; - } - private TreeBuildCache CreateTreeBuildCache() - => new(_objects, _gameData, _actors); + => new(objects, gameData, actors); public IEnumerable GetLocalPlayerRelatedCharacters() { @@ -80,7 +67,7 @@ public class ResourceTreeFactory if (drawObjStruct == null) return null; - var collectionResolveData = _collectionResolver.IdentifyCollection(gameObjStruct, true); + var collectionResolveData = resolver.IdentifyCollection(gameObjStruct, true); if (!collectionResolveData.Valid) return null; @@ -89,9 +76,9 @@ public class ResourceTreeFactory var networked = character.ObjectId != Dalamud.Game.ClientState.Objects.Types.GameObject.InvalidGameObjectId; var tree = new ResourceTree(name, character.ObjectIndex, (nint)gameObjStruct, (nint)drawObjStruct, localPlayerRelated, related, networked, collectionResolveData.ModCollection.Name); - var globalContext = new GlobalResolveContext(_identifier, collectionResolveData.ModCollection, + var globalContext = new GlobalResolveContext(identifier, collectionResolveData.ModCollection, cache, (flags & Flags.WithUiData) != 0); - using (var _ = _pathState.EnterInternalResolve()) + using (var _ = pathState.EnterInternalResolve()) { tree.LoadResources(globalContext); } @@ -103,56 +90,12 @@ public class ResourceTreeFactory // ResolveGamePaths(tree, collectionResolveData.ModCollection); if (globalContext.WithUiData) ResolveUiData(tree); - FilterFullPaths(tree, (flags & Flags.RedactExternalPaths) != 0 ? _config.ModDirectory : null); + FilterFullPaths(tree, (flags & Flags.RedactExternalPaths) != 0 ? config.ModDirectory : null); Cleanup(tree); return tree; } - private static void ResolveGamePaths(ResourceTree tree, ModCollection collection) - { - var forwardDictionary = new Dictionary(); - var reverseDictionary = new Dictionary>(); - foreach (var node in tree.FlatNodes) - { - if (node.PossibleGamePaths.Length == 0 && !node.FullPath.InternalName.IsEmpty) - reverseDictionary.TryAdd(node.FullPath.ToPath(), null!); - else if (node.FullPath.InternalName.IsEmpty && node.PossibleGamePaths.Length == 1) - forwardDictionary.TryAdd(node.GamePath, null); - } - - foreach (var key in forwardDictionary.Keys) - forwardDictionary[key] = collection.ResolvePath(key); - - var reverseResolvedArray = collection.ReverseResolvePaths(reverseDictionary.Keys); - foreach (var (key, set) in reverseDictionary.Keys.Zip(reverseResolvedArray)) - reverseDictionary[key] = set; - - foreach (var node in tree.FlatNodes) - { - if (node.PossibleGamePaths.Length == 0 && !node.FullPath.InternalName.IsEmpty) - { - if (!reverseDictionary.TryGetValue(node.FullPath.ToPath(), out var resolvedSet)) - continue; - - if (resolvedSet.Count != 1) - { - Penumbra.Log.Debug( - $"Found {resolvedSet.Count} game paths while reverse-resolving {node.FullPath} in {collection.Name}:"); - foreach (var gamePath in resolvedSet) - Penumbra.Log.Debug($"Game path: {gamePath}"); - } - - node.PossibleGamePaths = resolvedSet.ToArray(); - } - else if (node.FullPath.InternalName.IsEmpty && node.PossibleGamePaths.Length == 1) - { - if (forwardDictionary.TryGetValue(node.GamePath, out var resolved)) - node.FullPath = resolved ?? new FullPath(node.GamePath); - } - } - } - private static void ResolveUiData(ResourceTree tree) { foreach (var node in tree.FlatNodes) @@ -217,12 +160,12 @@ public class ResourceTreeFactory private unsafe (string Name, bool PlayerRelated) GetCharacterName(Dalamud.Game.ClientState.Objects.Types.Character character, TreeBuildCache cache) { - var identifier = _actors.FromObject((GameObject*)character.Address, out var owner, true, false, false); + var identifier = actors.FromObject((GameObject*)character.Address, out var owner, true, false, false); switch (identifier.Type) { case IdentifierType.Player: return (identifier.PlayerName.ToString(), true); case IdentifierType.Owned: - var ownerChara = _objects.CreateObjectReference((nint)owner) as Dalamud.Game.ClientState.Objects.Types.Character; + var ownerChara = objects.Objects.CreateObjectReference(owner) as Dalamud.Game.ClientState.Objects.Types.Character; if (ownerChara != null) { var ownerName = GetCharacterName(ownerChara, cache); diff --git a/Penumbra/Interop/ResourceTree/TreeBuildCache.cs b/Penumbra/Interop/ResourceTree/TreeBuildCache.cs index 7582c753..2798002a 100644 --- a/Penumbra/Interop/ResourceTree/TreeBuildCache.cs +++ b/Penumbra/Interop/ResourceTree/TreeBuildCache.cs @@ -3,19 +3,19 @@ using Dalamud.Plugin.Services; using Penumbra.GameData.Actors; using Penumbra.GameData.Enums; using Penumbra.GameData.Files; +using Penumbra.GameData.Interop; using Penumbra.GameData.Structs; -using Penumbra.String; using Penumbra.String.Classes; namespace Penumbra.Interop.ResourceTree; -internal readonly struct TreeBuildCache(IObjectTable objects, IDataManager dataManager, ActorManager actors) +internal readonly struct TreeBuildCache(ObjectManager objects, IDataManager dataManager, ActorManager actors) { private readonly Dictionary _shaderPackages = []; public unsafe bool IsLocalPlayerRelated(Character character) { - var player = objects[0]; + var player = objects.GetDalamudObject(0); if (player == null) return false; @@ -31,30 +31,30 @@ internal readonly struct TreeBuildCache(IObjectTable objects, IDataManager dataM } public IEnumerable GetCharacters() - => objects.OfType(); + => objects.Objects.OfType(); public IEnumerable GetLocalPlayerRelatedCharacters() { - var player = objects[0]; + var player = objects.GetDalamudObject(0); if (player == null) yield break; yield return (Character)player; - var minion = objects[1]; + var minion = objects.GetDalamudObject(1); if (minion != null) yield return (Character)minion; var playerId = player.ObjectId; for (var i = 2; i < ObjectIndex.CutsceneStart.Index; i += 2) { - if (objects[i] is Character owned && owned.OwnerId == playerId) + if (objects.GetDalamudObject(i) is Character owned && owned.OwnerId == playerId) yield return owned; } for (var i = ObjectIndex.CutsceneStart.Index; i < ObjectIndex.CharacterScreen.Index; ++i) { - var character = objects[i] as Character; + var character = objects.GetDalamudObject((int) i) as Character; if (character == null) continue; @@ -62,34 +62,11 @@ internal readonly struct TreeBuildCache(IObjectTable objects, IDataManager dataM if (parent < 0) continue; - if (parent is 0 or 1 || objects[parent]?.OwnerId == playerId) + if (parent is 0 or 1 || objects.GetDalamudObject(parent)?.OwnerId == playerId) yield return character; } } - private unsafe ByteString GetPlayerName(GameObject player) - { - var gameObject = (FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject*)player.Address; - return new ByteString(gameObject->Name); - } - - private unsafe bool GetOwnedId(ByteString playerName, uint playerId, int idx, [NotNullWhen(true)] out Character? character) - { - character = objects[idx] as Character; - if (character == null) - return false; - - var actorId = actors.FromObject(character, out var owner, true, true, true); - if (!actorId.IsValid) - return false; - if (owner != null && owner->OwnerID != playerId) - return false; - if (actorId.Type is not IdentifierType.Player || !actorId.PlayerName.Equals(playerName)) - return false; - - return true; - } - /// Try to read a shpk file from the given path and cache it on success. public ShpkFile? ReadShaderPackage(FullPath path) => ReadFile(dataManager, path, _shaderPackages, bytes => new ShpkFile(bytes)); diff --git a/Penumbra/Interop/Services/RedrawService.cs b/Penumbra/Interop/Services/RedrawService.cs index e0a94d30..a6fec0b5 100644 --- a/Penumbra/Interop/Services/RedrawService.cs +++ b/Penumbra/Interop/Services/RedrawService.cs @@ -11,6 +11,7 @@ using Penumbra.Api.Enums; using Penumbra.Communication; using Penumbra.GameData; using Penumbra.GameData.Enums; +using Penumbra.GameData.Interop; using Penumbra.Interop.Structs; using Penumbra.Mods; using Penumbra.Mods.Editor; @@ -57,7 +58,7 @@ public unsafe partial class RedrawService // this will be in obj and true will be returned. private bool FindCorrectActor(int idx, out GameObject? obj) { - obj = _objects[idx]; + obj = _objects.GetDalamudObject(idx); if (!InGPose || obj == null || IsGPoseActor(idx)) return false; @@ -70,21 +71,21 @@ public unsafe partial class RedrawService if (name == gPoseName) { - obj = _objects[GPosePlayerIdx + i]; + obj = _objects.GetDalamudObject(GPosePlayerIdx + i); return true; } } for (; _gPoseNameCounter < GPoseSlots; ++_gPoseNameCounter) { - var gPoseName = _objects[GPosePlayerIdx + _gPoseNameCounter]?.Name.ToString(); + var gPoseName = _objects.GetDalamudObject(GPosePlayerIdx + _gPoseNameCounter)?.Name.ToString(); _gPoseNames[_gPoseNameCounter] = gPoseName; if (gPoseName == null) break; if (name == gPoseName) { - obj = _objects[GPosePlayerIdx + _gPoseNameCounter]; + obj = _objects.GetDalamudObject(GPosePlayerIdx + _gPoseNameCounter); return true; } } @@ -111,7 +112,7 @@ public sealed unsafe partial class RedrawService : IDisposable private const int FurnitureIdx = 1337; private readonly IFramework _framework; - private readonly IObjectTable _objects; + private readonly ObjectManager _objects; private readonly ITargetManager _targets; private readonly ICondition _conditions; private readonly IClientState _clientState; @@ -133,7 +134,7 @@ public sealed unsafe partial class RedrawService : IDisposable public event GameObjectRedrawnDelegate? GameObjectRedrawn; - public RedrawService(IFramework framework, IObjectTable objects, ITargetManager targets, ICondition conditions, IClientState clientState, + public RedrawService(IFramework framework, ObjectManager objects, ITargetManager targets, ICondition conditions, IClientState clientState, Configuration config, CommunicatorService communicator) { _framework = framework; @@ -170,7 +171,7 @@ public sealed unsafe partial class RedrawService : IDisposable if (gPose) DisableDraw(actor!); - if (actor is PlayerCharacter && _objects[tableIndex + 1] is { ObjectKind: ObjectKind.MountType or ObjectKind.Ornament } mountOrOrnament) + if (actor is PlayerCharacter && _objects.GetDalamudObject(tableIndex + 1) is { ObjectKind: ObjectKind.MountType or ObjectKind.Ornament } mountOrOrnament) { *ActorDrawState(mountOrOrnament) |= DrawState.Invisibility; if (gPose) @@ -189,7 +190,7 @@ public sealed unsafe partial class RedrawService : IDisposable if (gPose) EnableDraw(actor!); - if (actor is PlayerCharacter && _objects[tableIndex + 1] is { ObjectKind: ObjectKind.MountType or ObjectKind.Ornament } mountOrOrnament) + if (actor is PlayerCharacter && _objects.GetDalamudObject(tableIndex + 1) is { ObjectKind: ObjectKind.MountType or ObjectKind.Ornament } mountOrOrnament) { *ActorDrawState(mountOrOrnament) &= ~DrawState.Invisibility; if (gPose) @@ -212,7 +213,7 @@ public sealed unsafe partial class RedrawService : IDisposable private void ReloadActorAfterGPose(GameObject? actor) { - if (_objects[GPosePlayerIdx] != null) + if (_objects[GPosePlayerIdx].Valid) { ReloadActor(actor); return; @@ -230,7 +231,7 @@ public sealed unsafe partial class RedrawService : IDisposable if (_target < 0) return; - var actor = _objects[_target]; + var actor = _objects.GetDalamudObject(_target); if (actor == null || _targets.Target != null) return; @@ -316,12 +317,12 @@ public sealed unsafe partial class RedrawService : IDisposable if (idx < 0) { var newIdx = ~idx; - WriteInvisible(_objects[newIdx]); + WriteInvisible(_objects.GetDalamudObject(newIdx)); _afterGPoseQueue[numKept++] = newIdx; } else { - WriteVisible(_objects[idx]); + WriteVisible(_objects.GetDalamudObject(idx)); } } @@ -357,8 +358,8 @@ public sealed unsafe partial class RedrawService : IDisposable private GameObject? GetLocalPlayer() { - var gPosePlayer = _objects[GPosePlayerIdx]; - return gPosePlayer ?? _objects[0]; + var gPosePlayer = _objects.GetDalamudObject(GPosePlayerIdx); + return gPosePlayer ?? _objects.GetDalamudObject(0); } public bool GetName(string lowerName, out GameObject? actor) @@ -379,7 +380,7 @@ public sealed unsafe partial class RedrawService : IDisposable if (!ret && lowerName.Length > 1 && lowerName[0] == '#' && ushort.TryParse(lowerName[1..], out var objectIndex)) { ret = true; - actor = _objects[objectIndex]; + actor = _objects.GetDalamudObject((int) objectIndex); } return ret; @@ -387,8 +388,8 @@ public sealed unsafe partial class RedrawService : IDisposable public void RedrawObject(int tableIndex, RedrawType settings) { - if (tableIndex >= 0 && tableIndex < _objects.Length) - RedrawObject(_objects[tableIndex], settings); + if (tableIndex >= 0 && tableIndex < _objects.Count) + RedrawObject(_objects.GetDalamudObject(tableIndex), settings); } public void RedrawObject(string name, RedrawType settings) @@ -399,13 +400,13 @@ public sealed unsafe partial class RedrawService : IDisposable else if (GetName(lowerName, out var target)) RedrawObject(target, settings); else - foreach (var actor in _objects.Where(a => a.Name.ToString().ToLowerInvariant() == lowerName)) + foreach (var actor in _objects.Objects.Where(a => a.Name.ToString().ToLowerInvariant() == lowerName)) RedrawObject(actor, settings); } public void RedrawAll(RedrawType settings) { - foreach (var actor in _objects) + foreach (var actor in _objects.Objects) RedrawObject(actor, settings); } diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index 72dd91d3..6cf24f62 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -12,6 +12,7 @@ using Penumbra.Collections.Manager; using Penumbra.Communication; using Penumbra.GameData.Enums; using Penumbra.GameData.Files; +using Penumbra.GameData.Interop; using Penumbra.Import.Models; using Penumbra.Import.Textures; using Penumbra.Interop.Hooks.Objects; @@ -46,7 +47,7 @@ public partial class ModEditWindow : Window, IDisposable private readonly IDragDropManager _dragDropManager; private readonly IDataManager _gameData; private readonly IFramework _framework; - private readonly IObjectTable _objects; + private readonly ObjectManager _objects; private readonly CharacterBaseDestructor _characterBaseDestructor; private Vector2 _iconSize = Vector2.Zero; @@ -446,7 +447,7 @@ public partial class ModEditWindow : Window, IDisposable DrawOptionSelectHeader(); - var setsEqual = !_editor!.SwapEditor.Changes; + var setsEqual = !_editor.SwapEditor.Changes; var tt = setsEqual ? "No changes staged." : "Apply the currently staged changes to the option."; ImGui.NewLine(); if (ImGuiUtil.DrawDisabledButton("Apply Changes", Vector2.Zero, tt, setsEqual)) @@ -577,7 +578,7 @@ public partial class ModEditWindow : Window, IDisposable Configuration config, ModEditor editor, ResourceTreeFactory resourceTreeFactory, MetaFileManager metaFileManager, StainService stainService, ActiveCollections activeCollections, ModMergeTab modMergeTab, CommunicatorService communicator, TextureManager textures, ModelManager models, IDragDropManager dragDropManager, - ChangedItemDrawer changedItemDrawer, IObjectTable objects, IFramework framework, CharacterBaseDestructor characterBaseDestructor) + ChangedItemDrawer changedItemDrawer, ObjectManager objects, IFramework framework, CharacterBaseDestructor characterBaseDestructor) : base(WindowBaseLabel) { _performance = performance; diff --git a/Penumbra/UI/PredefinedTagManager.cs b/Penumbra/UI/PredefinedTagManager.cs index 17e8432b..0e5377d6 100644 --- a/Penumbra/UI/PredefinedTagManager.cs +++ b/Penumbra/UI/PredefinedTagManager.cs @@ -71,8 +71,7 @@ public sealed class PredefinedTagManager : ISavable, IReadOnlyList foreach (var (tag, data) in tags) _predefinedTags.TryAdd(tag, data); break; - default: - throw new Exception($"Invalid version {version}."); + default: throw new Exception($"Invalid version {version}."); } } catch (Exception ex) diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index ab7ccf0c..2003f0ef 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -21,6 +21,7 @@ using Penumbra.Collections.Manager; using Penumbra.GameData.Actors; using Penumbra.GameData.DataContainers; using Penumbra.GameData.Files; +using Penumbra.GameData.Interop; using Penumbra.Import.Structs; using Penumbra.Import.Textures; using Penumbra.Interop.PathResolving; @@ -90,12 +91,12 @@ public class DebugTab : Window, ITab private readonly RedrawService _redraws; private readonly DictEmote _emotes; private readonly Diagnostics _diagnostics; - private readonly IObjectTable _objects; + private readonly ObjectManager _objects; private readonly IClientState _clientState; private readonly IpcTester _ipcTester; private readonly CrashHandlerPanel _crashHandlerPanel; - public DebugTab(PerformanceTracker performance, Configuration config, CollectionManager collectionManager, IObjectTable objects, + public DebugTab(PerformanceTracker performance, Configuration config, CollectionManager collectionManager, ObjectManager objects, IClientState clientState, ValidityChecker validityChecker, ModManager modManager, HttpApi httpApi, ActorManager actors, StainService stains, CharacterUtility characterUtility, ResidentResourceManager residentResources, @@ -430,7 +431,7 @@ public class DebugTab : Window, ITab DrawSpecial("Current Card", _actors.GetCardPlayer()); DrawSpecial("Current Glamour", _actors.GetGlamourPlayer()); - foreach (var obj in _objects) + foreach (var obj in _objects.Objects) { ImGuiUtil.DrawTableColumn($"{((GameObject*)obj.Address)->ObjectIndex}"); ImGuiUtil.DrawTableColumn($"0x{obj.Address:X}"); diff --git a/Penumbra/UI/Tabs/ModsTab.cs b/Penumbra/UI/Tabs/ModsTab.cs index bb8856b3..e4d94bb5 100644 --- a/Penumbra/UI/Tabs/ModsTab.cs +++ b/Penumbra/UI/Tabs/ModsTab.cs @@ -14,6 +14,7 @@ using Penumbra.Mods.Manager; using Penumbra.UI.ModsTab; using ModFileSystemSelector = Penumbra.UI.ModsTab.ModFileSystemSelector; using Penumbra.Collections.Manager; +using Penumbra.GameData.Interop; namespace Penumbra.UI.Tabs; @@ -28,7 +29,7 @@ public class ModsTab( IClientState clientState, CollectionSelectHeader collectionHeader, ITargetManager targets, - IObjectTable objectTable) + ObjectManager objects) : ITab { private readonly ActiveCollections _activeCollections = collectionManager.Active; @@ -128,7 +129,7 @@ public class ModsTab( using var disabled = ImRaii.Disabled(clientState.LocalPlayer == null); ImGui.SameLine(); var buttonWidth = frameHeight with { X = ImGui.GetContentRegionAvail().X / 5 }; - var tt = objectTable.GetObjectAddress(0) == nint.Zero + var tt = !objects[0].Valid ? "\nCan only be used when you are logged in and your character is available." : string.Empty; DrawButton(buttonWidth, "All", string.Empty, tt); From 26f3742b31b93787ed608b23c7cfbacf7b107876 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 20 Mar 2024 15:27:51 +0100 Subject: [PATCH 049/433] Update GameData. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index a1262e24..74a30576 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit a1262e242ca33bb0e9e4f080d294d9160b5e54eb +Subproject commit 74a305768880cd783b21e85ef97e9be77b119885 From 6cb4f7ac8a141faa908b0641ff7c6991ac582122 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 20 Mar 2024 17:35:45 +0100 Subject: [PATCH 050/433] Update GameData. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 74a30576..529e1811 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 74a305768880cd783b21e85ef97e9be77b119885 +Subproject commit 529e18115023732794994bfb8df4818b68951ea4 From e8fffa47a05d33e2c4f2e9364e72c2b61e0e71d2 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 20 Mar 2024 18:20:08 +0100 Subject: [PATCH 051/433] 1.0.2.0 --- Penumbra/UI/Changelog.cs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/Penumbra/UI/Changelog.cs b/Penumbra/UI/Changelog.cs index 67ab1a87..8b00ab8d 100644 --- a/Penumbra/UI/Changelog.cs +++ b/Penumbra/UI/Changelog.cs @@ -47,18 +47,46 @@ public class PenumbraChangelog Add8_2_0(Changelog); Add8_3_0(Changelog); Add1_0_0_0(Changelog); + Add1_0_2_0(Changelog); } #region Changelogs + private static void Add1_0_2_0(Changelog log) + => log.NextVersion("Version 1.0.2.0") + .RegisterEntry("Updated to .net8 and XIV 6.58, using some new framework facilities to improve performance and stability.") + .RegisterHighlight( + "Added an experimental crash handler that is supposed to write a Penumbra.log file when the game crashes, containing Penumbra-specific information.") + .RegisterEntry("Various improvements to model import/export by ackwell (throughout all patches).") + .RegisterHighlight( + "Added predefined tags that can be setup in the Settings tab and can be more easily applied or removed from mods. (by DZD)") + .RegisterEntry( + "The first empty option in a single-select option group imported from a TTMP will now keep its location instead of being moved to the first option.") + .RegisterEntry("Further empty options are still removed.", 1) + .RegisterEntry("Made it more obvious if a user has not set their root directory yet.") + .RegisterEntry("Added the characterglass.shpk shader file to special shader treatment to fix issues when replacing it. (By Ny)") + .RegisterEntry("Fixed some issues with the file sizes of compressed files.") + .RegisterEntry("Fixed an issue where reloading a mod did not ensure settings for that mod being correct afterwards.") + .RegisterEntry("Added an option to automatically redraw the player character when saving files. (1.0.0.8)") + .RegisterEntry("Fixed issue with manipulating mods not triggering some events. (1.0.0.7)") + .RegisterEntry("Fixed issue with temporary mods not triggering some events. (1.0.0.6)") + .RegisterEntry("Fixed issue when renaming mods while the advanced edit window is open. (1.0.0.6)") + .RegisterEntry("Fixed issue with empty option groups. (1.0.0.5)") + .RegisterEntry("Fixed issues with cutscene character identification. (1.0.0.4)") + .RegisterEntry("Added locale environment information to support info. (1.0.0.4)") + .RegisterEntry("Fixed an issue with copied mod settings in IPC missing unused settings. (1.0.0.3)"); + private static void Add1_0_0_0(Changelog log) => log.NextVersion("Version 1.0.0.0") .RegisterHighlight("Mods in the mod selector can now be filtered by changed item categories.") .RegisterHighlight("Model Editing options in the Advanced Editing Window have been greatly extended (by ackwell):") .RegisterEntry("Attributes and referenced materials can now be set per mesh.", 1) - .RegisterEntry("Model files (.mdl) can now be exported to the well-established glTF format, which can be imported e.g. by Blender.", 1) + .RegisterEntry("Model files (.mdl) can now be exported to the well-established glTF format, which can be imported e.g. by Blender.", + 1) .RegisterEntry("glTF files can also be imported back to a .mdl file.", 1) - .RegisterHighlight("Model Export and Import are a work in progress and may encounter issues, not support all cases or produce wrong results, please let us know!", 1) + .RegisterHighlight( + "Model Export and Import are a work in progress and may encounter issues, not support all cases or produce wrong results, please let us know!", + 1) .RegisterEntry("The last selected mod and the open/close state of the Advanced Editing Window are now stored across launches.") .RegisterEntry("Footsteps of certain mounts will now be associated to collections correctly.") .RegisterEntry("Save-in-Place in the texture tab now requires the configurable modifier.") @@ -67,7 +95,8 @@ public class PenumbraChangelog .RegisterEntry("Fixed an issue with the mod panels header not updating its data when the selected mod updates.") .RegisterEntry("Fixed some issues with EQDP files for invalid characters.") .RegisterEntry("Fixed an issue with the FileDialog being drawn twice in certain situations.") - .RegisterEntry("A lot of backend changes that should not have an effect on users, but may cause issues if something got messed up."); + .RegisterEntry( + "A lot of backend changes that should not have an effect on users, but may cause issues if something got messed up."); private static void Add8_3_0(Changelog log) => log.NextVersion("Version 0.8.3.0") From fda77b49cdd79a60ede094262473088ca6f2ebf6 Mon Sep 17 00:00:00 2001 From: Actions User Date: Wed, 20 Mar 2024 17:22:25 +0000 Subject: [PATCH 052/433] [CI] Updating repo.json for testing_1.0.2.0 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 1780c6c0..a4dc2f49 100644 --- a/repo.json +++ b/repo.json @@ -5,7 +5,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.0.1.0", - "TestingAssemblyVersion": "1.0.1.0", + "TestingAssemblyVersion": "1.0.2.0", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -17,7 +17,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.0/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 978f41a4d9a282c53d5cbc176fecfe462acc75e3 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 20 Mar 2024 22:38:39 +0100 Subject: [PATCH 053/433] Make some stuff safer maybe. --- .../Buffers/MemoryMappedBuffer.cs | 60 +++++++++---------- Penumbra.CrashHandler/Program.cs | 19 ++++-- Penumbra/Services/ValidityChecker.cs | 44 +++++++------- 3 files changed, 67 insertions(+), 56 deletions(-) diff --git a/Penumbra.CrashHandler/Buffers/MemoryMappedBuffer.cs b/Penumbra.CrashHandler/Buffers/MemoryMappedBuffer.cs index 35055864..c4e2627e 100644 --- a/Penumbra.CrashHandler/Buffers/MemoryMappedBuffer.cs +++ b/Penumbra.CrashHandler/Buffers/MemoryMappedBuffer.cs @@ -9,15 +9,15 @@ public class MemoryMappedBuffer : IDisposable { private const int MinHeaderLength = 4 + 4 + 4 + 4 + 4 + 4 + 4 + 4; - private readonly MemoryMappedFile _file; - private readonly MemoryMappedViewAccessor _header; + private readonly MemoryMappedFile _file; + private readonly MemoryMappedViewAccessor _header; private readonly MemoryMappedViewAccessor[] _lines; - public readonly int Version; - public readonly uint LineCount; - public readonly uint LineCapacity; + public readonly int Version; + public readonly uint LineCount; + public readonly uint LineCapacity; private readonly uint _lineMask; - private bool _disposed; + private bool _disposed; protected uint CurrentLineCount { @@ -39,20 +39,20 @@ public class MemoryMappedBuffer : IDisposable public MemoryMappedBuffer(string mapName, int version, uint lineCount, uint lineCapacity) { - Version = version; - LineCount = BitOperations.RoundUpToPowerOf2(Math.Clamp(lineCount, 2, int.MaxValue >> 3)); + Version = version; + LineCount = BitOperations.RoundUpToPowerOf2(Math.Clamp(lineCount, 2, int.MaxValue >> 3)); LineCapacity = BitOperations.RoundUpToPowerOf2(Math.Clamp(lineCapacity, 2, int.MaxValue >> 3)); - _lineMask = LineCount - 1; - var fileName = Encoding.UTF8.GetBytes(mapName); + _lineMask = LineCount - 1; + var fileName = Encoding.UTF8.GetBytes(mapName); var headerLength = (uint)(4 + 4 + 4 + 4 + 4 + 4 + 4 + fileName.Length + 1); headerLength = (headerLength & 0b111) > 0 ? (headerLength & ~0b111u) + 0b1000 : headerLength; var capacity = LineCount * LineCapacity + headerLength; _file = MemoryMappedFile.CreateNew(mapName, capacity, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, HandleInheritability.Inheritable); _header = _file.CreateViewAccessor(0, headerLength); - _header.Write(0, headerLength); - _header.Write(4, Version); - _header.Write(8, LineCount); + _header.Write(0, headerLength); + _header.Write(4, Version); + _header.Write(8, LineCount); _header.Write(12, LineCapacity); _header.WriteArray(28, fileName, 0, fileName.Length); _header.Write(fileName.Length + 28, (byte)0); @@ -65,16 +65,16 @@ public class MemoryMappedBuffer : IDisposable uint? expectedMinLineCapacity = null) { _file = MemoryMappedFile.OpenExisting(mapName, MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable); - using var headerLine = _file.CreateViewAccessor(0, 4, MemoryMappedFileAccess.Read); - var headerLength = headerLine.ReadUInt32(0); + using var headerLine = _file.CreateViewAccessor(0, 4, MemoryMappedFileAccess.Read); + var headerLength = headerLine.ReadUInt32(0); if (headerLength < MinHeaderLength) Throw($"Map {mapName} did not contain a valid header."); - _header = _file.CreateViewAccessor(0, headerLength, MemoryMappedFileAccess.ReadWrite); - Version = _header.ReadInt32(4); - LineCount = _header.ReadUInt32(8); + _header = _file.CreateViewAccessor(0, headerLength, MemoryMappedFileAccess.ReadWrite); + Version = _header.ReadInt32(4); + LineCount = _header.ReadUInt32(8); LineCapacity = _header.ReadUInt32(12); - _lineMask = LineCount - 1; + _lineMask = LineCount - 1; if (expectedVersion.HasValue && expectedVersion.Value != Version) Throw($"Map {mapName} has version {Version} instead of {expectedVersion.Value}."); @@ -122,25 +122,25 @@ public class MemoryMappedBuffer : IDisposable protected static int WriteString(string text, Span span) { - var bytes = Encoding.UTF8.GetBytes(text); - var length = bytes.Length + 1; + var bytes = Encoding.UTF8.GetBytes(text); + var source = (Span)bytes; + var length = source.Length + 1; if (length > span.Length) - throw new Exception($"String {text} is too long to write into span."); - - bytes.CopyTo(span); + source = source[..(span.Length - 1)]; + source.CopyTo(span); span[bytes.Length] = 0; - return length; + return source.Length + 1; } protected static int WriteSpan(ReadOnlySpan input, Span span) { var length = input.Length + 1; if (length > span.Length) - throw new Exception("Byte array is too long to write into span."); + input = input[..(span.Length - 1)]; input.CopyTo(span); span[input.Length] = 0; - return length; + return input.Length + 1; } protected Span GetLine(int i) @@ -150,7 +150,7 @@ public class MemoryMappedBuffer : IDisposable lock (_header) { - var lineIdx = CurrentLinePosition + i & _lineMask; + var lineIdx = (CurrentLinePosition + i) & _lineMask; if (lineIdx > CurrentLineCount) return null; @@ -168,8 +168,8 @@ public class MemoryMappedBuffer : IDisposable if (currentLineCount == LineCount) { var currentLinePos = CurrentLinePosition; - view = _lines[currentLinePos]!; - CurrentLinePosition = currentLinePos + 1 & _lineMask; + view = _lines[currentLinePos]!; + CurrentLinePosition = (currentLinePos + 1) & _lineMask; } else { diff --git a/Penumbra.CrashHandler/Program.cs b/Penumbra.CrashHandler/Program.cs index 518e2d04..0ea76ac6 100644 --- a/Penumbra.CrashHandler/Program.cs +++ b/Penumbra.CrashHandler/Program.cs @@ -14,12 +14,21 @@ public class CrashHandler { using var reader = new GameEventLogReader(); var parent = Process.GetProcessById(pid); - + using var handle = parent.SafeHandle; parent.WaitForExit(); - var exitCode = parent.ExitCode; - var obj = reader.Dump("Crash", pid, exitCode, args[2], args[3]); - using var fs = File.Open(args[0], FileMode.Create); - using var w = new Utf8JsonWriter(fs, new JsonWriterOptions { Indented = true }); + int exitCode; + try + { + exitCode = parent.ExitCode; + } + catch (Exception ex) + { + exitCode = -1; + } + + var obj = reader.Dump("Crash", pid, exitCode, args[2], args[3]); + using var fs = File.Open(args[0], FileMode.Create); + using var w = new Utf8JsonWriter(fs, new JsonWriterOptions { Indented = true }); obj.WriteTo(w, new JsonSerializerOptions() { WriteIndented = true }); } catch (Exception ex) diff --git a/Penumbra/Services/ValidityChecker.cs b/Penumbra/Services/ValidityChecker.cs index d4b5005f..cc70306b 100644 --- a/Penumbra/Services/ValidityChecker.cs +++ b/Penumbra/Services/ValidityChecker.cs @@ -21,7 +21,15 @@ public class ValidityChecker : IService public readonly string Version; public readonly string CommitHash; - public readonly string GameVersion; + + public unsafe string GameVersion + { + get + { + var framework = Framework.Instance(); + return framework == null ? string.Empty : framework->GameVersion[0]; + } + } public ValidityChecker(DalamudPluginInterface pi) { @@ -30,14 +38,10 @@ public class ValidityChecker : IService IsValidSourceRepo = CheckSourceRepo(pi); var assembly = GetType().Assembly; - Version = assembly.GetName().Version?.ToString() ?? string.Empty; - CommitHash = assembly.GetCustomAttribute()?.InformationalVersion ?? "Unknown"; - GameVersion = GetGameVersion(); + Version = assembly.GetName().Version?.ToString() ?? string.Empty; + CommitHash = assembly.GetCustomAttribute()?.InformationalVersion ?? "Unknown"; } - private static unsafe string GetGameVersion() - => Framework.Instance()->GameVersion[0]; - public void LogExceptions() { if (ImcExceptions.Count > 0) @@ -49,16 +53,16 @@ public class ValidityChecker : IService private static bool CheckDevPluginPenumbra(DalamudPluginInterface pi) { #if !DEBUG - var path = Path.Combine( pi.DalamudAssetDirectory.Parent?.FullName ?? "INVALIDPATH", "devPlugins", "Penumbra" ); - var dir = new DirectoryInfo( path ); + var path = Path.Combine(pi.DalamudAssetDirectory.Parent?.FullName ?? "INVALIDPATH", "devPlugins", "Penumbra"); + var dir = new DirectoryInfo(path); try { - return dir.Exists && dir.EnumerateFiles( "*.dll", SearchOption.AllDirectories ).Any(); + return dir.Exists && dir.EnumerateFiles("*.dll", SearchOption.AllDirectories).Any(); } - catch( Exception e ) + catch (Exception e) { - Penumbra.Log.Error( $"Could not check for dev plugin Penumbra:\n{e}" ); + Penumbra.Log.Error($"Could not check for dev plugin Penumbra:\n{e}"); return true; } #else @@ -71,11 +75,9 @@ public class ValidityChecker : IService { #if !DEBUG var checkedDirectory = pi.AssemblyLocation.Directory?.Parent?.Parent?.Name; - var ret = checkedDirectory?.Equals( "installedPlugins", StringComparison.OrdinalIgnoreCase ) ?? false; - if( !ret ) - { - Penumbra.Log.Error( $"Penumbra is not correctly installed. Application loaded from \"{pi.AssemblyLocation.Directory!.FullName}\"." ); - } + var ret = checkedDirectory?.Equals("installedPlugins", StringComparison.OrdinalIgnoreCase) ?? false; + if (!ret) + Penumbra.Log.Error($"Penumbra is not correctly installed. Application loaded from \"{pi.AssemblyLocation.Directory!.FullName}\"."); return !ret; #else @@ -89,10 +91,10 @@ public class ValidityChecker : IService #if !DEBUG return pi.SourceRepository?.Trim().ToLowerInvariant() switch { - null => false, - RepositoryLower => true, - SeaOfStarsLower => true, - _ => false, + null => false, + RepositoryLower => true, + SeaOfStarsLower => true, + _ => false, }; #else return true; From 384b8fd489c1e4b952703ed1d75544875bf790c8 Mon Sep 17 00:00:00 2001 From: Actions User Date: Wed, 20 Mar 2024 21:40:54 +0000 Subject: [PATCH 054/433] [CI] Updating repo.json for testing_1.0.2.1 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index a4dc2f49..9487b2b3 100644 --- a/repo.json +++ b/repo.json @@ -5,7 +5,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.0.1.0", - "TestingAssemblyVersion": "1.0.2.0", + "TestingAssemblyVersion": "1.0.2.1", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -17,7 +17,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.0/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.1/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 0ad769e08e5c3b92249ff7bc1f3ac65878e73449 Mon Sep 17 00:00:00 2001 From: AeAstralis Date: Wed, 20 Mar 2024 22:33:35 -0400 Subject: [PATCH 055/433] Add tri count per LoD to models tab Adds the tri count of each LoD on the selected model to the Models tab under Advanced Editing. --- .../UI/AdvancedWindow/ModEditWindow.Models.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index 67ec97f2..494ad3f6 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -508,6 +508,11 @@ public partial class ModEditWindow ImGuiUtil.DrawTableColumn(file.VertexDeclarations.Length.ToString()); ImGuiUtil.DrawTableColumn("Stack Size"); ImGuiUtil.DrawTableColumn(file.StackSize.ToString()); + for (var lod = 0; lod < file.Lods.Length; lod++) + { + ImGuiUtil.DrawTableColumn("LoD " + lod + " Triangle Count"); + ImGuiUtil.DrawTableColumn(GetTriangleCountForLod(file, lod).ToString()); + } } } @@ -555,6 +560,18 @@ public partial class ModEditWindow return file != null; } + private static long GetTriangleCountForLod(MdlFile model, int lod) + { + var vertSum = 0u; + var meshIndex = model.Lods[lod].MeshIndex; + var meshCount = model.Lods[lod].MeshCount; + + for (var i = meshIndex; i < meshIndex + meshCount; i++) + vertSum += model.Meshes[i].IndexCount; + + return vertSum / 3; + } + private static readonly string[] ValidModelExtensions = [ ".gltf", From 739b5b5ad6a7c58c0d95c90557bf44c4a342e895 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Wed, 20 Mar 2024 20:02:09 +0100 Subject: [PATCH 056/433] CS-ify ShaderReplacementFixer and ModelRenderer --- Penumbra/Interop/Services/ModelRenderer.cs | 25 ++++++------------- .../Services/ShaderReplacementFixer.cs | 20 +++++---------- 2 files changed, 14 insertions(+), 31 deletions(-) diff --git a/Penumbra/Interop/Services/ModelRenderer.cs b/Penumbra/Interop/Services/ModelRenderer.cs index 6a3bf776..7df83cf7 100644 --- a/Penumbra/Interop/Services/ModelRenderer.cs +++ b/Penumbra/Interop/Services/ModelRenderer.cs @@ -1,35 +1,26 @@ using Dalamud.Plugin.Services; -using Dalamud.Utility.Signatures; +using FFXIVClientStructs.FFXIV.Client.Graphics.Render; using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; -using Penumbra.GameData; namespace Penumbra.Interop.Services; -// TODO ClientStructs-ify (https://github.com/aers/FFXIVClientStructs/pull/817) public unsafe class ModelRenderer : IDisposable { - // Will be Manager.Instance()->ModelRenderer.CharacterGlassShaderPackage in CS - private const nint ModelRendererOffset = 0x13660; - private const nint CharacterGlassShaderPackageOffset = 0xD0; - - /// A static pointer to the Render::Manager address. - [Signature(Sigs.RenderManager, ScanType = ScanType.StaticAddress)] - private readonly nint* _renderManagerAddress = null; - public bool Ready { get; private set; } public ShaderPackageResourceHandle** CharacterGlassShaderPackage - => *_renderManagerAddress == 0 - ? null - : (ShaderPackageResourceHandle**)(*_renderManagerAddress + ModelRendererOffset + CharacterGlassShaderPackageOffset).ToPointer(); + => Manager.Instance() switch + { + null => null, + var renderManager => &renderManager->ModelRenderer.CharacterGlassShaderPackage, + }; public ShaderPackageResourceHandle* DefaultCharacterGlassShaderPackage { get; private set; } private readonly IFramework _framework; - public ModelRenderer(IFramework framework, IGameInteropProvider interop) + public ModelRenderer(IFramework framework) { - interop.InitializeFromAttributes(this); _framework = framework; LoadDefaultResources(null!); if (!Ready) @@ -39,7 +30,7 @@ public unsafe class ModelRenderer : IDisposable /// We store the default data of the resources so we can always restore them. private void LoadDefaultResources(object _) { - if (*_renderManagerAddress == 0) + if (Manager.Instance() == null) return; var anyMissing = false; diff --git a/Penumbra/Interop/Services/ShaderReplacementFixer.cs b/Penumbra/Interop/Services/ShaderReplacementFixer.cs index e57fe313..26906ace 100644 --- a/Penumbra/Interop/Services/ShaderReplacementFixer.cs +++ b/Penumbra/Interop/Services/ShaderReplacementFixer.cs @@ -2,12 +2,14 @@ using Dalamud.Hooking; using Dalamud.Plugin.Services; using Dalamud.Utility.Signatures; using FFXIVClientStructs.FFXIV.Client.Graphics.Render; +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; using OtterGui.Classes; using Penumbra.Communication; using Penumbra.GameData; using Penumbra.Interop.Hooks.Resources; using Penumbra.Services; +using CSModelRenderer = FFXIVClientStructs.FFXIV.Client.Graphics.Render.ModelRenderer; namespace Penumbra.Interop.Services; @@ -22,18 +24,8 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable [Signature(Sigs.HumanVTable, ScanType = ScanType.StaticAddress)] private readonly nint* _humanVTable = null!; - private delegate nint CharacterBaseOnRenderMaterialDelegate(nint drawObject, OnRenderMaterialParams* param); - private delegate nint ModelRendererOnRenderMaterialDelegate(nint modelRenderer, nint outFlags, nint param, Material* material, uint materialIndex); - - [StructLayout(LayoutKind.Explicit)] - private struct OnRenderMaterialParams - { - [FieldOffset(0x0)] - public Model* Model; - - [FieldOffset(0x8)] - public uint MaterialIndex; - } + private delegate nint CharacterBaseOnRenderMaterialDelegate(CharacterBase* drawObject, CSModelRenderer.OnRenderMaterialParams* param); + private delegate nint ModelRendererOnRenderMaterialDelegate(CSModelRenderer* modelRenderer, ushort* outFlags, CSModelRenderer.OnRenderModelParams* param, Material* material, uint materialIndex); private readonly Hook _humanOnRenderMaterialHook; @@ -135,7 +127,7 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable Interlocked.Decrement(ref _moddedCharacterGlassShpkCount); } - private nint OnRenderHumanMaterial(nint human, OnRenderMaterialParams* param) + private nint OnRenderHumanMaterial(CharacterBase* human, CSModelRenderer.OnRenderMaterialParams* param) { // If we don't have any on-screen instances of modded skin.shpk, we don't need the slow path at all. if (!Enabled || _moddedSkinShpkCount == 0) @@ -167,7 +159,7 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable } } - private nint ModelRendererOnRenderMaterialDetour(nint modelRenderer, nint outFlags, nint param, Material* material, uint materialIndex) + private nint ModelRendererOnRenderMaterialDetour(CSModelRenderer* modelRenderer, ushort* outFlags, CSModelRenderer.OnRenderModelParams* param, Material* material, uint materialIndex) { // If we don't have any on-screen instances of modded characterglass.shpk, we don't need the slow path at all. From c55d6966cd02e0d765ddfccfd928649680592206 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 21 Mar 2024 14:28:57 +0100 Subject: [PATCH 057/433] Update Internal csproj. --- OtterGui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OtterGui b/OtterGui index b4b14367..2bbb9b2a 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit b4b14367d8235eabedd561ad3626beb1d2a83889 +Subproject commit 2bbb9b2a8a4479461b252594b9d1b788b551c13c From f3ceb9034e8ad4456e6541fbe3d61024e7a55a55 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 21 Mar 2024 20:27:55 +0100 Subject: [PATCH 058/433] Make crashhandler somewhat more stable, support multi boxing maybe? --- .../Buffers/AnimationInvocationBuffer.cs | 16 ++-- .../Buffers/CharacterBaseBuffer.cs | 16 ++-- .../Buffers/MemoryMappedBuffer.cs | 12 +-- .../Buffers/ModdedFileBuffer.cs | 16 ++-- Penumbra.CrashHandler/GameEventLogReader.cs | 8 +- Penumbra.CrashHandler/GameEventLogWriter.cs | 8 +- Penumbra.CrashHandler/Program.cs | 2 +- Penumbra/Services/CrashHandlerService.cs | 88 ++++++++++++++----- 8 files changed, 105 insertions(+), 61 deletions(-) diff --git a/Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs b/Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs index dd966542..c92a14fd 100644 --- a/Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs +++ b/Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs @@ -88,18 +88,18 @@ internal sealed class AnimationInvocationBuffer : MemoryMappedBuffer, IAnimation } } - public static IBufferReader CreateReader() - => new AnimationInvocationBuffer(false); + public static IBufferReader CreateReader(int pid) + => new AnimationInvocationBuffer(false, pid); - public static IAnimationInvocationBufferWriter CreateWriter() - => new AnimationInvocationBuffer(); + public static IAnimationInvocationBufferWriter CreateWriter(int pid) + => new AnimationInvocationBuffer(pid); - private AnimationInvocationBuffer(bool writer) - : base(_name, _version) + private AnimationInvocationBuffer(bool writer, int pid) + : base($"{_name}_{pid}_{_version}", _version) { } - private AnimationInvocationBuffer() - : base(_name, _version, _lineCount, _lineCapacity) + private AnimationInvocationBuffer(int pid) + : base($"{_name}_{pid}_{_version}", _version, _lineCount, _lineCapacity) { } private static string ToName(AnimationInvocationType type) diff --git a/Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs b/Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs index 1fe5d7ba..d83c6e6c 100644 --- a/Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs +++ b/Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs @@ -69,17 +69,17 @@ internal sealed class CharacterBaseBuffer : MemoryMappedBuffer, ICharacterBaseBu public uint TotalCount => TotalWrittenLines; - public static IBufferReader CreateReader() - => new CharacterBaseBuffer(false); + public static IBufferReader CreateReader(int pid) + => new CharacterBaseBuffer(false, pid); - public static ICharacterBaseBufferWriter CreateWriter() - => new CharacterBaseBuffer(); + public static ICharacterBaseBufferWriter CreateWriter(int pid) + => new CharacterBaseBuffer(pid); - private CharacterBaseBuffer(bool writer) - : base(_name, _version) + private CharacterBaseBuffer(bool writer, int pid) + : base($"{_name}_{pid}_{_version}", _version) { } - private CharacterBaseBuffer() - : base(_name, _version, _lineCount, _lineCapacity) + private CharacterBaseBuffer(int pid) + : base($"{_name}_{pid}_{_version}", _version, _lineCount, _lineCapacity) { } } diff --git a/Penumbra.CrashHandler/Buffers/MemoryMappedBuffer.cs b/Penumbra.CrashHandler/Buffers/MemoryMappedBuffer.cs index c4e2627e..a1b3de52 100644 --- a/Penumbra.CrashHandler/Buffers/MemoryMappedBuffer.cs +++ b/Penumbra.CrashHandler/Buffers/MemoryMappedBuffer.cs @@ -11,7 +11,7 @@ public class MemoryMappedBuffer : IDisposable private readonly MemoryMappedFile _file; private readonly MemoryMappedViewAccessor _header; - private readonly MemoryMappedViewAccessor[] _lines; + private readonly MemoryMappedViewAccessor[] _lines = []; public readonly int Version; public readonly uint LineCount; @@ -64,7 +64,8 @@ public class MemoryMappedBuffer : IDisposable public MemoryMappedBuffer(string mapName, int? expectedVersion = null, uint? expectedMinLineCount = null, uint? expectedMinLineCapacity = null) { - _file = MemoryMappedFile.OpenExisting(mapName, MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable); + _lines = []; + _file = MemoryMappedFile.OpenExisting(mapName, MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable); using var headerLine = _file.CreateViewAccessor(0, 4, MemoryMappedFileAccess.Read); var headerLength = headerLine.ReadUInt32(0); if (headerLength < MinHeaderLength) @@ -96,6 +97,7 @@ public class MemoryMappedBuffer : IDisposable void Throw(string text) { _file.Dispose(); + _header?.Dispose(); _disposed = true; throw new Exception(text); } @@ -204,10 +206,10 @@ public class MemoryMappedBuffer : IDisposable if (_disposed) return; - _header.Dispose(); + _header?.Dispose(); foreach (var line in _lines) - line.Dispose(); - _file.Dispose(); + line?.Dispose(); + _file?.Dispose(); } ~MemoryMappedBuffer() diff --git a/Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs b/Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs index b472d413..6c774e4b 100644 --- a/Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs +++ b/Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs @@ -83,17 +83,17 @@ internal sealed class ModdedFileBuffer : MemoryMappedBuffer, IModdedFileBufferWr } } - public static IBufferReader CreateReader() - => new ModdedFileBuffer(false); + public static IBufferReader CreateReader(int pid) + => new ModdedFileBuffer(false, pid); - public static IModdedFileBufferWriter CreateWriter() - => new ModdedFileBuffer(); + public static IModdedFileBufferWriter CreateWriter(int pid) + => new ModdedFileBuffer(pid); - private ModdedFileBuffer(bool writer) - : base(_name, _version) + private ModdedFileBuffer(bool writer, int pid) + : base($"{_name}_{pid}_{_version}", _version) { } - private ModdedFileBuffer() - : base(_name, _version, _lineCount, _lineCapacity) + private ModdedFileBuffer(int pid) + : base($"{_name}_{pid}_{_version}", _version, _lineCount, _lineCapacity) { } } diff --git a/Penumbra.CrashHandler/GameEventLogReader.cs b/Penumbra.CrashHandler/GameEventLogReader.cs index 1ae49fa5..1813a671 100644 --- a/Penumbra.CrashHandler/GameEventLogReader.cs +++ b/Penumbra.CrashHandler/GameEventLogReader.cs @@ -9,13 +9,13 @@ public interface IBufferReader public IEnumerable GetLines(DateTimeOffset crashTime); } -public sealed class GameEventLogReader : IDisposable +public sealed class GameEventLogReader(int pid) : IDisposable { public readonly (IBufferReader Reader, string TypeSingular, string TypePlural)[] Readers = [ - (CharacterBaseBuffer.CreateReader(), "CharacterLoaded", "CharactersLoaded"), - (ModdedFileBuffer.CreateReader(), "ModdedFileLoaded", "ModdedFilesLoaded"), - (AnimationInvocationBuffer.CreateReader(), "VFXFuncInvoked", "VFXFuncsInvoked"), + (CharacterBaseBuffer.CreateReader(pid), "CharacterLoaded", "CharactersLoaded"), + (ModdedFileBuffer.CreateReader(pid), "ModdedFileLoaded", "ModdedFilesLoaded"), + (AnimationInvocationBuffer.CreateReader(pid), "VFXFuncInvoked", "VFXFuncsInvoked"), ]; public void Dispose() diff --git a/Penumbra.CrashHandler/GameEventLogWriter.cs b/Penumbra.CrashHandler/GameEventLogWriter.cs index 8e809cec..e2c461f4 100644 --- a/Penumbra.CrashHandler/GameEventLogWriter.cs +++ b/Penumbra.CrashHandler/GameEventLogWriter.cs @@ -2,11 +2,11 @@ namespace Penumbra.CrashHandler; -public sealed class GameEventLogWriter : IDisposable +public sealed class GameEventLogWriter(int pid) : IDisposable { - public readonly ICharacterBaseBufferWriter CharacterBase = CharacterBaseBuffer.CreateWriter(); - public readonly IModdedFileBufferWriter FileLoaded = ModdedFileBuffer.CreateWriter(); - public readonly IAnimationInvocationBufferWriter AnimationFuncInvoked = AnimationInvocationBuffer.CreateWriter(); + public readonly ICharacterBaseBufferWriter CharacterBase = CharacterBaseBuffer.CreateWriter(pid); + public readonly IModdedFileBufferWriter FileLoaded = ModdedFileBuffer.CreateWriter(pid); + public readonly IAnimationInvocationBufferWriter AnimationFuncInvoked = AnimationInvocationBuffer.CreateWriter(pid); public void Dispose() { diff --git a/Penumbra.CrashHandler/Program.cs b/Penumbra.CrashHandler/Program.cs index 0ea76ac6..94e90bfc 100644 --- a/Penumbra.CrashHandler/Program.cs +++ b/Penumbra.CrashHandler/Program.cs @@ -12,7 +12,7 @@ public class CrashHandler try { - using var reader = new GameEventLogReader(); + using var reader = new GameEventLogReader(pid); var parent = Process.GetProcessById(pid); using var handle = parent.SafeHandle; parent.WaitForExit(); diff --git a/Penumbra/Services/CrashHandlerService.cs b/Penumbra/Services/CrashHandlerService.cs index a3a35b78..c713d623 100644 --- a/Penumbra/Services/CrashHandlerService.cs +++ b/Penumbra/Services/CrashHandlerService.cs @@ -24,6 +24,8 @@ public sealed class CrashHandlerService : IDisposable, IService private readonly Configuration _config; private readonly ValidityChecker _validityChecker; + private string _tempExecutableDirectory = string.Empty; + public CrashHandlerService(FilenameService files, CommunicatorService communicator, ActorManager actors, ResourceLoader resourceLoader, Configuration config, ValidityChecker validityChecker) { @@ -54,6 +56,7 @@ public sealed class CrashHandlerService : IDisposable, IService } Unsubscribe(); + CleanExecutables(); } private Process? _child; @@ -177,11 +180,12 @@ public sealed class CrashHandlerService : IDisposable, IService try { - using var reader = new GameEventLogReader(); + using var reader = new GameEventLogReader(Environment.ProcessId); JsonObject jObj; lock (_eventWriter) { - jObj = reader.Dump("Manual Dump", Environment.ProcessId, 0, $"{_validityChecker.Version} ({_validityChecker.CommitHash})", _validityChecker.GameVersion); + jObj = reader.Dump("Manual Dump", Environment.ProcessId, 0, $"{_validityChecker.Version} ({_validityChecker.CommitHash})", + _validityChecker.GameVersion); } var logFile = _files.LogFileName; @@ -198,14 +202,31 @@ public sealed class CrashHandlerService : IDisposable, IService } } - private string CopyExecutables() + private void CleanExecutables() { var parent = Path.GetDirectoryName(_files.CrashHandlerExe)!; - var folder = Path.Combine(parent, "temp"); - Directory.CreateDirectory(folder); + foreach (var dir in Directory.EnumerateDirectories(parent, "temp_*")) + { + try + { + Directory.Delete(dir, true); + } + catch (Exception ex) + { + Penumbra.Log.Debug($"Could not delete {dir}:\n{ex}"); + } + } + } + + private string CopyExecutables() + { + CleanExecutables(); + var parent = Path.GetDirectoryName(_files.CrashHandlerExe)!; + _tempExecutableDirectory = Path.Combine(parent, $"temp_{Environment.ProcessId}"); + Directory.CreateDirectory(_tempExecutableDirectory); foreach (var file in Directory.EnumerateFiles(parent, "Penumbra.CrashHandler.*")) - File.Copy(file, Path.Combine(folder, Path.GetFileName(file)), true); - return Path.Combine(folder, Path.GetFileName(_files.CrashHandlerExe)); + File.Copy(file, Path.Combine(_tempExecutableDirectory, Path.GetFileName(file)), true); + return Path.Combine(_tempExecutableDirectory, Path.GetFileName(_files.CrashHandlerExe)); } public void LogAnimation(nint character, ModCollection collection, AnimationInvocationType type) @@ -213,10 +234,17 @@ public sealed class CrashHandlerService : IDisposable, IService if (_eventWriter == null) return; - var name = GetActorName(character); - lock (_eventWriter) + try { - _eventWriter?.AnimationFuncInvoked.WriteLine(character, name.Span, collection.Name, type); + var name = GetActorName(character); + lock (_eventWriter) + { + _eventWriter?.AnimationFuncInvoked.WriteLine(character, name.Span, collection.Name, type); + } + } + catch (Exception ex) + { + Penumbra.Log.Warning($"Error logging animation function {type} to crash handler:\n{ex}"); } } @@ -225,11 +253,18 @@ public sealed class CrashHandlerService : IDisposable, IService if (_eventWriter == null) return; - var name = GetActorName(address); - - lock (_eventWriter) + try { - _eventWriter?.CharacterBase.WriteLine(address, name.Span, collection); + var name = GetActorName(address); + + lock (_eventWriter) + { + _eventWriter?.CharacterBase.WriteLine(address, name.Span, collection); + } + } + catch (Exception ex) + { + Penumbra.Log.Warning($"Error logging character creation to crash handler:\n{ex}"); } } @@ -249,15 +284,22 @@ public sealed class CrashHandlerService : IDisposable, IService if (manipulatedPath == null || _eventWriter == null) return; - var dashIdx = manipulatedPath.Value.InternalName[0] == (byte)'|' ? manipulatedPath.Value.InternalName.IndexOf((byte)'|', 1) : -1; - if (dashIdx >= 0 && !Utf8GamePath.IsRooted(manipulatedPath.Value.InternalName.Substring(dashIdx + 1))) - return; - - var name = GetActorName(resolveData.AssociatedGameObject); - lock (_eventWriter) + try { - _eventWriter!.FileLoaded.WriteLine(resolveData.AssociatedGameObject, name.Span, resolveData.ModCollection.Name, - manipulatedPath.Value.InternalName.Span, originalPath.Path.Span); + var dashIdx = manipulatedPath.Value.InternalName[0] == (byte)'|' ? manipulatedPath.Value.InternalName.IndexOf((byte)'|', 1) : -1; + if (dashIdx >= 0 && !Utf8GamePath.IsRooted(manipulatedPath.Value.InternalName.Substring(dashIdx + 1))) + return; + + var name = GetActorName(resolveData.AssociatedGameObject); + lock (_eventWriter) + { + _eventWriter!.FileLoaded.WriteLine(resolveData.AssociatedGameObject, name.Span, resolveData.ModCollection.Name, + manipulatedPath.Value.InternalName.Span, originalPath.Path.Span); + } + } + catch (Exception ex) + { + Penumbra.Log.Warning($"Error logging resource to crash handler:\n{ex}"); } } @@ -276,7 +318,7 @@ public sealed class CrashHandlerService : IDisposable, IService try { CloseEventWriter(); - _eventWriter = new GameEventLogWriter(); + _eventWriter = new GameEventLogWriter(Environment.ProcessId); Penumbra.Log.Debug("Opened new Event Writer for crash handler."); } catch (Exception ex) From 72979a9743158749d01d74f400c878c3e3f9c41e Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 21 Mar 2024 21:31:30 +0100 Subject: [PATCH 059/433] Remarked changes. --- OtterGui | 2 +- .../UI/AdvancedWindow/ModEditWindow.Models.cs | 139 ++++++++++-------- 2 files changed, 82 insertions(+), 59 deletions(-) diff --git a/OtterGui b/OtterGui index b4b14367..2bbb9b2a 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit b4b14367d8235eabedd561ad3626beb1d2a83889 +Subproject commit 2bbb9b2a8a4479461b252594b9d1b788b551c13c diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index 494ad3f6..3672dce7 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -7,6 +7,7 @@ using OtterGui.Widgets; using Penumbra.GameData; using Penumbra.GameData.Files; using Penumbra.Import.Models; +using Penumbra.Mods; using Penumbra.String.Classes; using Penumbra.UI.Classes; @@ -14,9 +15,13 @@ namespace Penumbra.UI.AdvancedWindow; public partial class ModEditWindow { - private const int MdlMaterialMaximum = 4; - private const string MdlImportDocumentation = @"https://github.com/xivdev/Penumbra/wiki/Model-IO#user-content-9b49d296-23ab-410a-845b-a3be769b71ea"; - private const string MdlExportDocumentation = @"https://github.com/xivdev/Penumbra/wiki/Model-IO#user-content-25968400-ebe5-4861-b610-cb1556db7ec4"; + private const int MdlMaterialMaximum = 4; + + private const string MdlImportDocumentation = + @"https://github.com/xivdev/Penumbra/wiki/Model-IO#user-content-9b49d296-23ab-410a-845b-a3be769b71ea"; + + private const string MdlExportDocumentation = + @"https://github.com/xivdev/Penumbra/wiki/Model-IO#user-content-25968400-ebe5-4861-b610-cb1556db7ec4"; private readonly FileEditor _modelTab; private readonly ModelManager _models; @@ -25,31 +30,51 @@ public partial class ModEditWindow private readonly List _subMeshAttributeTagWidgets = []; private string _customPath = string.Empty; private Utf8GamePath _customGamePath = Utf8GamePath.Empty; + private MdlFile _lastFile = null!; + private long[] _lodTriCount = []; + + private void UpdateFile(MdlFile file, bool force) + { + if (file == _lastFile) + { + if (force) + UpdateMeshes(); + } + else + { + UpdateMeshes(); + _lastFile = file; + _lodTriCount = Enumerable.Range(0, file.Lods.Length).Select(l => GetTriangleCountForLod(file, l)).ToArray(); + } + + return; + + void UpdateMeshes() + { + var subMeshTotal = file.Meshes.Aggregate(0, (count, mesh) => count + mesh.SubMeshCount); + if (_subMeshAttributeTagWidgets.Count != subMeshTotal) + { + _subMeshAttributeTagWidgets.Clear(); + _subMeshAttributeTagWidgets.AddRange( + Enumerable.Range(0, subMeshTotal).Select(_ => new TagButtons()) + ); + } + } + } private bool DrawModelPanel(MdlTab tab, bool disabled) { - var file = tab.Mdl; - - var subMeshTotal = file.Meshes.Aggregate(0, (count, mesh) => count + mesh.SubMeshCount); - if (_subMeshAttributeTagWidgets.Count != subMeshTotal) - { - _subMeshAttributeTagWidgets.Clear(); - _subMeshAttributeTagWidgets.AddRange( - Enumerable.Range(0, subMeshTotal).Select(_ => new TagButtons()) - ); - } - + UpdateFile(tab.Mdl, tab.Dirty); DrawImportExport(tab, disabled); var ret = tab.Dirty; - ret |= DrawModelMaterialDetails(tab, disabled); - if (ImGui.CollapsingHeader($"Meshes ({file.Meshes.Length})###meshes")) - for (var i = 0; i < file.LodCount; ++i) + if (ImGui.CollapsingHeader($"Meshes ({_lastFile.Meshes.Length})###meshes")) + for (var i = 0; i < _lastFile.LodCount; ++i) ret |= DrawModelLodDetails(tab, i, disabled); - ret |= DrawOtherModelDetails(file, disabled); + ret |= DrawOtherModelDetails(disabled); return !disabled && ret; } @@ -85,7 +110,7 @@ public partial class ModEditWindow using (var frame = ImRaii.FramedGroup("Import", size, headerPreIcon: FontAwesomeIcon.FileImport)) { - ImGui.Checkbox("Keep current materials", ref tab.ImportKeepMaterials); + ImGui.Checkbox("Keep current materials", ref tab.ImportKeepMaterials); ImGui.Checkbox("Keep current attributes", ref tab.ImportKeepAttributes); if (ImGuiUtil.DrawDisabledButton("Import from glTF", Vector2.Zero, "Imports a glTF file, overriding the content of this mdl.", @@ -111,10 +136,7 @@ public partial class ModEditWindow if (tab.GamePaths == null) { - if (tab.IoExceptions.Count == 0) - ImGui.TextUnformatted("Resolving model game paths."); - else - ImGui.TextUnformatted("Failed to resolve model game paths."); + ImGui.TextUnformatted(tab.IoExceptions.Count == 0 ? "Resolving model game paths." : "Failed to resolve model game paths."); return; } @@ -149,14 +171,15 @@ public partial class ModEditWindow ImGui.SameLine(); DrawDocumentationLink(MdlExportDocumentation); } - + private static void DrawIoExceptions(MdlTab tab) { if (tab.IoExceptions.Count == 0) return; var size = new Vector2(ImGui.GetContentRegionAvail().X, 0); - using var frame = ImRaii.FramedGroup("Exceptions", size, headerPreIcon: FontAwesomeIcon.TimesCircle, borderColor: Colors.RegexWarningBorder); + using var frame = ImRaii.FramedGroup("Exceptions", size, headerPreIcon: FontAwesomeIcon.TimesCircle, + borderColor: Colors.RegexWarningBorder); var spaceAvail = ImGui.GetContentRegionAvail().X - ImGui.GetStyle().ItemSpacing.X - 100; foreach (var (exception, index) in tab.IoExceptions.WithIndex()) @@ -181,7 +204,7 @@ public partial class ModEditWindow if (tab.IoWarnings.Count == 0) return; - var size = new Vector2(ImGui.GetContentRegionAvail().X, 0); + var size = new Vector2(ImGui.GetContentRegionAvail().X, 0); using var frame = ImRaii.FramedGroup("Warnings", size, headerPreIcon: FontAwesomeIcon.ExclamationCircle, borderColor: 0xFF40FFFF); var spaceAvail = ImGui.GetContentRegionAvail().X - ImGui.GetStyle().ItemSpacing.X - 100; @@ -445,7 +468,7 @@ public partial class ModEditWindow if (attributes == null) { attributes = ["invalid attribute data"]; - disabled = true; + disabled = true; } var tagIndex = widget.Draw(string.Empty, string.Empty, attributes, @@ -460,7 +483,7 @@ public partial class ModEditWindow return true; } - private static bool DrawOtherModelDetails(MdlFile file, bool _) + private bool DrawOtherModelDetails(bool _) { using var header = ImRaii.CollapsingHeader("Further Content"); if (!header) @@ -471,47 +494,47 @@ public partial class ModEditWindow if (table) { ImGuiUtil.DrawTableColumn("Version"); - ImGuiUtil.DrawTableColumn(file.Version.ToString()); + ImGuiUtil.DrawTableColumn(_lastFile.Version.ToString()); ImGuiUtil.DrawTableColumn("Radius"); - ImGuiUtil.DrawTableColumn(file.Radius.ToString(CultureInfo.InvariantCulture)); + ImGuiUtil.DrawTableColumn(_lastFile.Radius.ToString(CultureInfo.InvariantCulture)); ImGuiUtil.DrawTableColumn("Model Clip Out Distance"); - ImGuiUtil.DrawTableColumn(file.ModelClipOutDistance.ToString(CultureInfo.InvariantCulture)); + ImGuiUtil.DrawTableColumn(_lastFile.ModelClipOutDistance.ToString(CultureInfo.InvariantCulture)); ImGuiUtil.DrawTableColumn("Shadow Clip Out Distance"); - ImGuiUtil.DrawTableColumn(file.ShadowClipOutDistance.ToString(CultureInfo.InvariantCulture)); + ImGuiUtil.DrawTableColumn(_lastFile.ShadowClipOutDistance.ToString(CultureInfo.InvariantCulture)); ImGuiUtil.DrawTableColumn("LOD Count"); - ImGuiUtil.DrawTableColumn(file.LodCount.ToString()); + ImGuiUtil.DrawTableColumn(_lastFile.LodCount.ToString()); ImGuiUtil.DrawTableColumn("Enable Index Buffer Streaming"); - ImGuiUtil.DrawTableColumn(file.EnableIndexBufferStreaming.ToString()); + ImGuiUtil.DrawTableColumn(_lastFile.EnableIndexBufferStreaming.ToString()); ImGuiUtil.DrawTableColumn("Enable Edge Geometry"); - ImGuiUtil.DrawTableColumn(file.EnableEdgeGeometry.ToString()); + ImGuiUtil.DrawTableColumn(_lastFile.EnableEdgeGeometry.ToString()); ImGuiUtil.DrawTableColumn("Flags 1"); - ImGuiUtil.DrawTableColumn(file.Flags1.ToString()); + ImGuiUtil.DrawTableColumn(_lastFile.Flags1.ToString()); ImGuiUtil.DrawTableColumn("Flags 2"); - ImGuiUtil.DrawTableColumn(file.Flags2.ToString()); + ImGuiUtil.DrawTableColumn(_lastFile.Flags2.ToString()); ImGuiUtil.DrawTableColumn("Vertex Declarations"); - ImGuiUtil.DrawTableColumn(file.VertexDeclarations.Length.ToString()); + ImGuiUtil.DrawTableColumn(_lastFile.VertexDeclarations.Length.ToString()); ImGuiUtil.DrawTableColumn("Bone Bounding Boxes"); - ImGuiUtil.DrawTableColumn(file.BoneBoundingBoxes.Length.ToString()); + ImGuiUtil.DrawTableColumn(_lastFile.BoneBoundingBoxes.Length.ToString()); ImGuiUtil.DrawTableColumn("Bone Tables"); - ImGuiUtil.DrawTableColumn(file.BoneTables.Length.ToString()); + ImGuiUtil.DrawTableColumn(_lastFile.BoneTables.Length.ToString()); ImGuiUtil.DrawTableColumn("Element IDs"); - ImGuiUtil.DrawTableColumn(file.ElementIds.Length.ToString()); + ImGuiUtil.DrawTableColumn(_lastFile.ElementIds.Length.ToString()); ImGuiUtil.DrawTableColumn("Extra LoDs"); - ImGuiUtil.DrawTableColumn(file.ExtraLods.Length.ToString()); + ImGuiUtil.DrawTableColumn(_lastFile.ExtraLods.Length.ToString()); ImGuiUtil.DrawTableColumn("Meshes"); - ImGuiUtil.DrawTableColumn(file.Meshes.Length.ToString()); + ImGuiUtil.DrawTableColumn(_lastFile.Meshes.Length.ToString()); ImGuiUtil.DrawTableColumn("Shape Meshes"); - ImGuiUtil.DrawTableColumn(file.ShapeMeshes.Length.ToString()); + ImGuiUtil.DrawTableColumn(_lastFile.ShapeMeshes.Length.ToString()); ImGuiUtil.DrawTableColumn("LoDs"); - ImGuiUtil.DrawTableColumn(file.Lods.Length.ToString()); + ImGuiUtil.DrawTableColumn(_lastFile.Lods.Length.ToString()); ImGuiUtil.DrawTableColumn("Vertex Declarations"); - ImGuiUtil.DrawTableColumn(file.VertexDeclarations.Length.ToString()); + ImGuiUtil.DrawTableColumn(_lastFile.VertexDeclarations.Length.ToString()); ImGuiUtil.DrawTableColumn("Stack Size"); - ImGuiUtil.DrawTableColumn(file.StackSize.ToString()); - for (var lod = 0; lod < file.Lods.Length; lod++) + ImGuiUtil.DrawTableColumn(_lastFile.StackSize.ToString()); + foreach (var (triCount, lod) in _lodTriCount.WithIndex()) { - ImGuiUtil.DrawTableColumn("LoD " + lod + " Triangle Count"); - ImGuiUtil.DrawTableColumn(GetTriangleCountForLod(file, lod).ToString()); + ImGuiUtil.DrawTableColumn($"LOD #{lod + 1} Triangle Count"); + ImGuiUtil.DrawTableColumn(triCount.ToString()); } } } @@ -519,36 +542,36 @@ public partial class ModEditWindow using (var materials = ImRaii.TreeNode("Materials", ImGuiTreeNodeFlags.DefaultOpen)) { if (materials) - foreach (var material in file.Materials) + foreach (var material in _lastFile.Materials) ImRaii.TreeNode(material, ImGuiTreeNodeFlags.Leaf).Dispose(); } using (var attributes = ImRaii.TreeNode("Attributes", ImGuiTreeNodeFlags.DefaultOpen)) { if (attributes) - foreach (var attribute in file.Attributes) + foreach (var attribute in _lastFile.Attributes) ImRaii.TreeNode(attribute, ImGuiTreeNodeFlags.Leaf).Dispose(); } using (var bones = ImRaii.TreeNode("Bones", ImGuiTreeNodeFlags.DefaultOpen)) { if (bones) - foreach (var bone in file.Bones) + foreach (var bone in _lastFile.Bones) ImRaii.TreeNode(bone, ImGuiTreeNodeFlags.Leaf).Dispose(); } using (var shapes = ImRaii.TreeNode("Shapes", ImGuiTreeNodeFlags.DefaultOpen)) { if (shapes) - foreach (var shape in file.Shapes) + foreach (var shape in _lastFile.Shapes) ImRaii.TreeNode(shape.ShapeName, ImGuiTreeNodeFlags.Leaf).Dispose(); } - if (file.RemainingData.Length > 0) + if (_lastFile.RemainingData.Length > 0) { - using var t = ImRaii.TreeNode($"Additional Data (Size: {file.RemainingData.Length})###AdditionalData"); + using var t = ImRaii.TreeNode($"Additional Data (Size: {_lastFile.RemainingData.Length})###AdditionalData"); if (t) - ImGuiUtil.TextWrapped(string.Join(' ', file.RemainingData.Select(c => $"{c:X2}"))); + ImGuiUtil.TextWrapped(string.Join(' ', _lastFile.RemainingData.Select(c => $"{c:X2}"))); } return false; @@ -562,7 +585,7 @@ public partial class ModEditWindow private static long GetTriangleCountForLod(MdlFile model, int lod) { - var vertSum = 0u; + var vertSum = 0u; var meshIndex = model.Lods[lod].MeshIndex; var meshCount = model.Lods[lod].MeshCount; From cc88738e3e28232b6735e3279240b833bccb5b1c Mon Sep 17 00:00:00 2001 From: Actions User Date: Thu, 21 Mar 2024 20:33:37 +0000 Subject: [PATCH 060/433] [CI] Updating repo.json for testing_1.0.2.2 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 9487b2b3..3aac5de6 100644 --- a/repo.json +++ b/repo.json @@ -5,7 +5,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.0.1.0", - "TestingAssemblyVersion": "1.0.2.1", + "TestingAssemblyVersion": "1.0.2.2", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -17,7 +17,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.1/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.2/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 1a9239a358fbbd0e423f606995ab8cdbfe484105 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 21 Mar 2024 23:02:07 +0100 Subject: [PATCH 061/433] Test improving object manager. --- Penumbra.GameData | 2 +- Penumbra/Api/IpcTester.cs | 6 +++--- Penumbra/Api/PenumbraApi.cs | 6 +++--- .../Interop/Hooks/Animation/LoadTimelineResources.cs | 2 +- Penumbra/Interop/Hooks/Animation/SomePapLoad.cs | 2 +- Penumbra/Interop/PathResolving/DrawObjectState.cs | 9 ++++----- Penumbra/Interop/Services/RedrawService.cs | 2 +- Penumbra/UI/Tabs/Debug/DebugTab.cs | 6 +++--- 8 files changed, 17 insertions(+), 18 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 529e1811..9a1e5e72 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 529e18115023732794994bfb8df4818b68951ea4 +Subproject commit 9a1e5e7294a6929ee1cc72b1b5d76e12c000c327 diff --git a/Penumbra/Api/IpcTester.cs b/Penumbra/Api/IpcTester.cs index e1dd81cb..30f3c80f 100644 --- a/Penumbra/Api/IpcTester.cs +++ b/Penumbra/Api/IpcTester.cs @@ -442,8 +442,8 @@ public class IpcTester : IDisposable DrawIntro(Ipc.RedrawObjectByIndex.Label, "Redraw by Index"); var tmp = _redrawIndex; ImGui.SetNextItemWidth(100 * UiHelpers.Scale); - if (ImGui.DragInt("##redrawIndex", ref tmp, 0.1f, 0, _objects.Count)) - _redrawIndex = Math.Clamp(tmp, 0, _objects.Count); + if (ImGui.DragInt("##redrawIndex", ref tmp, 0.1f, 0, _objects.TotalCount)) + _redrawIndex = Math.Clamp(tmp, 0, _objects.TotalCount); ImGui.SameLine(); if (ImGui.Button("Redraw##Index")) @@ -460,7 +460,7 @@ public class IpcTester : IDisposable private void SetLastRedrawn(IntPtr address, int index) { if (index < 0 - || index > _objects.Count + || index > _objects.TotalCount || address == IntPtr.Zero || _objects[index].Address != address) _lastRedrawnString = "Invalid"; diff --git a/Penumbra/Api/PenumbraApi.cs b/Penumbra/Api/PenumbraApi.cs index 5146383d..c8be90aa 100644 --- a/Penumbra/Api/PenumbraApi.cs +++ b/Penumbra/Api/PenumbraApi.cs @@ -929,7 +929,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi { CheckInitialized(); - if (actorIndex < 0 || actorIndex >= _objects.Count) + if (actorIndex < 0 || actorIndex >= _objects.TotalCount) return PenumbraApiEc.InvalidArgument; var identifier = _actors.FromObject(_objects[actorIndex], out _, false, false, true); @@ -1166,7 +1166,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi private unsafe bool AssociatedCollection(int gameObjectIdx, out ModCollection collection) { collection = _collectionManager.Active.Default; - if (gameObjectIdx < 0 || gameObjectIdx >= _objects.Count) + if (gameObjectIdx < 0 || gameObjectIdx >= _objects.TotalCount) return false; var ptr = _objects[gameObjectIdx]; @@ -1180,7 +1180,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private unsafe ActorIdentifier AssociatedIdentifier(int gameObjectIdx) { - if (gameObjectIdx < 0 || gameObjectIdx >= _objects.Count) + if (gameObjectIdx < 0 || gameObjectIdx >= _objects.TotalCount) return ActorIdentifier.Invalid; var ptr = _objects[gameObjectIdx]; diff --git a/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs b/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs index 4bae084e..018892a0 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs @@ -64,7 +64,7 @@ public sealed unsafe class LoadTimelineResources : FastHook**)timeline)[0][Offsets.GetGameObjectIdxVfunc]; var idx = getGameObjectIdx(timeline); - if (idx >= 0 && idx < objects.Count) + if (idx >= 0 && idx < objects.TotalCount) { var obj = objects[idx]; return obj.Valid ? resolver.IdentifyCollection(obj.AsObject, true) : ResolveData.Invalid; diff --git a/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs b/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs index a4563bf3..fad1f819 100644 --- a/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs +++ b/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs @@ -35,7 +35,7 @@ public sealed unsafe class SomePapLoad : FastHook if (timelinePtr != nint.Zero) { var actorIdx = (int)(*(*(ulong**)timelinePtr + 1) >> 3); - if (actorIdx >= 0 && actorIdx < _objects.Count) + if (actorIdx >= 0 && actorIdx < _objects.TotalCount) { var newData = _collectionResolver.IdentifyCollection(_objects[actorIdx].AsObject, true); var last = _state.SetAnimationData(newData); diff --git a/Penumbra/Interop/PathResolving/DrawObjectState.cs b/Penumbra/Interop/PathResolving/DrawObjectState.cs index 784ac3fb..2a9ec7a9 100644 --- a/Penumbra/Interop/PathResolving/DrawObjectState.cs +++ b/Penumbra/Interop/PathResolving/DrawObjectState.cs @@ -94,11 +94,10 @@ public sealed class DrawObjectState : IDisposable, IReadOnlyDictionary private unsafe void InitializeDrawObjects() { - for (var i = 0; i < _objects.Count; ++i) - { - var ptr = _objects[i]; - if (ptr is { IsCharacter: true, Model.Valid: true }) - IterateDrawObjectTree((Object*)ptr.Model.Address, ptr, false, false); + foreach(var actor in _objects) + { + if (actor is { IsCharacter: true, Model.Valid: true }) + IterateDrawObjectTree((Object*)actor.Model.Address, actor, false, false); } } diff --git a/Penumbra/Interop/Services/RedrawService.cs b/Penumbra/Interop/Services/RedrawService.cs index a6fec0b5..21ecfd4f 100644 --- a/Penumbra/Interop/Services/RedrawService.cs +++ b/Penumbra/Interop/Services/RedrawService.cs @@ -388,7 +388,7 @@ public sealed unsafe partial class RedrawService : IDisposable public void RedrawObject(int tableIndex, RedrawType settings) { - if (tableIndex >= 0 && tableIndex < _objects.Count) + if (tableIndex >= 0 && tableIndex < _objects.TotalCount) RedrawObject(_objects.GetDalamudObject(tableIndex), settings); } diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index 2003f0ef..06f1d126 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -431,16 +431,16 @@ public class DebugTab : Window, ITab DrawSpecial("Current Card", _actors.GetCardPlayer()); DrawSpecial("Current Glamour", _actors.GetGlamourPlayer()); - foreach (var obj in _objects.Objects) + foreach (var obj in _objects) { ImGuiUtil.DrawTableColumn($"{((GameObject*)obj.Address)->ObjectIndex}"); ImGuiUtil.DrawTableColumn($"0x{obj.Address:X}"); ImGuiUtil.DrawTableColumn(obj.Address == nint.Zero ? string.Empty : $"0x{(nint)((Character*)obj.Address)->GameObject.GetDrawObject():X}"); - var identifier = _actors.FromObject(obj, false, true, false); + var identifier = _actors.FromObject(obj, out _, false, true, false); ImGuiUtil.DrawTableColumn(_actors.ToString(identifier)); - var id = obj.ObjectKind == ObjectKind.BattleNpc ? $"{identifier.DataId} | {obj.DataId}" : identifier.DataId.ToString(); + var id = obj.AsObject->ObjectKind ==(byte) ObjectKind.BattleNpc ? $"{identifier.DataId} | {obj.AsObject->DataID}" : identifier.DataId.ToString(); ImGuiUtil.DrawTableColumn(id); } From 8fded888137117823f521d6d4a8e8d4d826db87d Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 21 Mar 2024 23:50:55 +0100 Subject: [PATCH 062/433] Update. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 9a1e5e72..66687643 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 9a1e5e7294a6929ee1cc72b1b5d76e12c000c327 +Subproject commit 66687643da2163c938575ad6949c8d0fbd03afe7 From 5ea140db980ff112149fdb0dbc7cf069a68f1ec3 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 22 Mar 2024 00:45:52 +0100 Subject: [PATCH 063/433] Add new draw events. --- Penumbra.Api | 2 +- Penumbra/Api/PenumbraApi.cs | 14 ++++++++++++- Penumbra/Communication/PostEnabledDraw.cs | 18 +++++++++++++++++ .../Communication/PreSettingsTabBarDraw.cs | 20 +++++++++++++++++++ Penumbra/Penumbra.cs | 11 ++++++++++ Penumbra/Services/CommunicatorService.cs | 8 ++++++++ Penumbra/UI/ModsTab/ModPanelHeader.cs | 16 ++++++++++++--- Penumbra/UI/ModsTab/ModPanelSettingsTab.cs | 6 ++++-- 8 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 Penumbra/Communication/PostEnabledDraw.cs create mode 100644 Penumbra/Communication/PreSettingsTabBarDraw.cs diff --git a/Penumbra.Api b/Penumbra.Api index d2a1406b..a28a2537 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit d2a1406bc32f715c0687613f02e3f74caf7ceea9 +Subproject commit a28a2537c0ff030cf09f740bff3b1a51ac0b41f6 diff --git a/Penumbra/Api/PenumbraApi.cs b/Penumbra/Api/PenumbraApi.cs index c8be90aa..da1eafd0 100644 --- a/Penumbra/Api/PenumbraApi.cs +++ b/Penumbra/Api/PenumbraApi.cs @@ -33,12 +33,24 @@ namespace Penumbra.Api; public class PenumbraApi : IDisposable, IPenumbraApi { public (int, int) ApiVersion - => (4, 23); + => (4, 24); + + public event Action? PreSettingsTabBarDraw + { + add => _communicator.PreSettingsTabBarDraw.Subscribe(value!, Communication.PreSettingsTabBarDraw.Priority.Default); + remove => _communicator.PreSettingsTabBarDraw.Unsubscribe(value!); + } public event Action? PreSettingsPanelDraw { add => _communicator.PreSettingsPanelDraw.Subscribe(value!, Communication.PreSettingsPanelDraw.Priority.Default); remove => _communicator.PreSettingsPanelDraw.Unsubscribe(value!); + } + + public event Action? PostEnabledDraw + { + add => _communicator.PostEnabledDraw.Subscribe(value!, Communication.PostEnabledDraw.Priority.Default); + remove => _communicator.PostEnabledDraw.Unsubscribe(value!); } public event Action? PostSettingsPanelDraw diff --git a/Penumbra/Communication/PostEnabledDraw.cs b/Penumbra/Communication/PostEnabledDraw.cs new file mode 100644 index 00000000..68637442 --- /dev/null +++ b/Penumbra/Communication/PostEnabledDraw.cs @@ -0,0 +1,18 @@ +using OtterGui.Classes; + +namespace Penumbra.Communication; + +/// +/// Triggered after the Enabled Checkbox line in settings is drawn, but before options are drawn. +/// +/// Parameter is the identifier (directory name) of the currently selected mod. +/// +/// +public sealed class PostEnabledDraw() : EventWrapper(nameof(PostEnabledDraw)) +{ + public enum Priority + { + /// + Default = 0, + } +} diff --git a/Penumbra/Communication/PreSettingsTabBarDraw.cs b/Penumbra/Communication/PreSettingsTabBarDraw.cs new file mode 100644 index 00000000..2c14cdf1 --- /dev/null +++ b/Penumbra/Communication/PreSettingsTabBarDraw.cs @@ -0,0 +1,20 @@ +using OtterGui.Classes; + +namespace Penumbra.Communication; + +/// +/// Triggered before the settings tab bar for a mod is drawn, after the title group is drawn. +/// +/// Parameter is the identifier (directory name) of the currently selected mod. +/// is the total width of the header group. +/// is the width of the title box. +/// +/// +public sealed class PreSettingsTabBarDraw() : EventWrapper(nameof(PreSettingsTabBarDraw)) +{ + public enum Priority + { + /// + Default = 0, + } +} diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index ff068928..34451dfa 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -95,6 +95,17 @@ public class Penumbra : IDalamudPlugin if (_characterUtility.Ready) _residentResources.Reload(); + + var api = _services.GetService(); + api.PostEnabledDraw += ImGui.TextUnformatted; + api.PreSettingsTabBarDraw += (name, width, titleWidth) => + { + ImGui.TextUnformatted(width.ToString()); + ImGui.TextUnformatted(titleWidth.ToString()); + ImGui.TextUnformatted(ImGui.GetContentRegionMax().X.ToString()); + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + (width - titleWidth) / 2); + ImGui.Button("Test", new Vector2(titleWidth, 0)); + }; } catch (Exception ex) { diff --git a/Penumbra/Services/CommunicatorService.cs b/Penumbra/Services/CommunicatorService.cs index da852855..cacbe689 100644 --- a/Penumbra/Services/CommunicatorService.cs +++ b/Penumbra/Services/CommunicatorService.cs @@ -57,9 +57,15 @@ public class CommunicatorService : IDisposable, IService /// public readonly EnabledChanged EnabledChanged = new(); + /// + public readonly PreSettingsTabBarDraw PreSettingsTabBarDraw = new(); + /// public readonly PreSettingsPanelDraw PreSettingsPanelDraw = new(); + /// + public readonly PostEnabledDraw PostEnabledDraw = new(); + /// public readonly PostSettingsPanelDraw PostSettingsPanelDraw = new(); @@ -91,7 +97,9 @@ public class CommunicatorService : IDisposable, IService ModSettingChanged.Dispose(); CollectionInheritanceChanged.Dispose(); EnabledChanged.Dispose(); + PreSettingsTabBarDraw.Dispose(); PreSettingsPanelDraw.Dispose(); + PostEnabledDraw.Dispose(); PostSettingsPanelDraw.Dispose(); ChangedItemHover.Dispose(); ChangedItemClick.Dispose(); diff --git a/Penumbra/UI/ModsTab/ModPanelHeader.cs b/Penumbra/UI/ModsTab/ModPanelHeader.cs index ed499b4f..05f47809 100644 --- a/Penumbra/UI/ModsTab/ModPanelHeader.cs +++ b/Penumbra/UI/ModsTab/ModPanelHeader.cs @@ -32,9 +32,14 @@ public class ModPanelHeader : IDisposable /// public void Draw() { - var offset = DrawModName(); - DrawVersion(offset); - DrawSecondRow(offset); + using (ImRaii.Group()) + { + var offset = DrawModName(); + DrawVersion(offset); + DrawSecondRow(offset); + } + + _communicator.PreSettingsTabBarDraw.Invoke(_mod.Identifier, ImGui.GetItemRectSize().X, _nameWidth); } /// @@ -43,6 +48,7 @@ public class ModPanelHeader : IDisposable /// public void UpdateModData(Mod mod) { + _mod = mod; // Name var name = $" {mod.Name} "; if (name != _modName) @@ -90,6 +96,7 @@ public class ModPanelHeader : IDisposable } // Header data. + private Mod _mod = null!; private string _modName = string.Empty; private string _modAuthor = string.Empty; private string _modVersion = string.Empty; @@ -103,6 +110,8 @@ public class ModPanelHeader : IDisposable private float _modWebsiteButtonWidth; private float _secondRowWidth; + private float _nameWidth; + /// /// Draw the mod name in the game font with a 2px border, centered, /// with at least the width of the version space to each side. @@ -124,6 +133,7 @@ public class ModPanelHeader : IDisposable using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, 2 * UiHelpers.Scale); using var f = _nameFont.Push(); ImGuiUtil.DrawTextButton(_modName, Vector2.Zero, 0); + _nameWidth = ImGui.GetItemRectSize().X; return offset; } diff --git a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs index 5c7ddbf3..b14cad01 100644 --- a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs @@ -61,7 +61,7 @@ public class ModPanelSettingsTab : ITab DrawInheritedWarning(); UiHelpers.DefaultLineSpace(); - _communicator.PreSettingsPanelDraw.Invoke(_selector.Selected!.ModPath.Name); + _communicator.PreSettingsPanelDraw.Invoke(_selector.Selected!.Identifier); DrawEnabledInput(); _tutorial.OpenTutorial(BasicTutorialSteps.EnablingMods); ImGui.SameLine(); @@ -69,6 +69,8 @@ public class ModPanelSettingsTab : ITab _tutorial.OpenTutorial(BasicTutorialSteps.Priority); DrawRemoveSettings(); + _communicator.PostEnabledDraw.Invoke(_selector.Selected!.Identifier); + if (_selector.Selected!.Groups.Count > 0) { var useDummy = true; @@ -98,7 +100,7 @@ public class ModPanelSettingsTab : ITab } UiHelpers.DefaultLineSpace(); - _communicator.PostSettingsPanelDraw.Invoke(_selector.Selected!.ModPath.Name); + _communicator.PostSettingsPanelDraw.Invoke(_selector.Selected!.Identifier); } /// Draw a big red bar if the current setting is inherited. From ca95b9c14c1eb487a1e38a6aa1d7272e7549ecba Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 22 Mar 2024 00:55:53 +0100 Subject: [PATCH 064/433] PackageVersion. --- Penumbra.Api | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.Api b/Penumbra.Api index a28a2537..8787efc8 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit a28a2537c0ff030cf09f740bff3b1a51ac0b41f6 +Subproject commit 8787efc8fc897dfbb4515ebbabbcd5e6f54d1b42 From 26730efc1bee0a415c433fd06d8cbf53ac93d3b1 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 22 Mar 2024 13:31:38 +0100 Subject: [PATCH 065/433] Update OtterGui --- OtterGui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OtterGui b/OtterGui index 2bbb9b2a..4e06921d 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 2bbb9b2a8a4479461b252594b9d1b788b551c13c +Subproject commit 4e06921da239788331a4527aa6a2943cf0e809fe From d171cea627c65a22b9fed2671b55b140085e4ca4 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 22 Mar 2024 13:36:55 +0100 Subject: [PATCH 066/433] Remove DI Reference. --- Penumbra.CrashHandler/Program.cs | 2 +- Penumbra/Penumbra.csproj | 1 - Penumbra/packages.lock.json | 25 ++++++++++++------------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/Penumbra.CrashHandler/Program.cs b/Penumbra.CrashHandler/Program.cs index 94e90bfc..3bc461f7 100644 --- a/Penumbra.CrashHandler/Program.cs +++ b/Penumbra.CrashHandler/Program.cs @@ -21,7 +21,7 @@ public class CrashHandler { exitCode = parent.ExitCode; } - catch (Exception ex) + catch { exitCode = -1; } diff --git a/Penumbra/Penumbra.csproj b/Penumbra/Penumbra.csproj index df18ff13..b07917e8 100644 --- a/Penumbra/Penumbra.csproj +++ b/Penumbra/Penumbra.csproj @@ -71,7 +71,6 @@ - diff --git a/Penumbra/packages.lock.json b/Penumbra/packages.lock.json index 3f795631..68e1b880 100644 --- a/Penumbra/packages.lock.json +++ b/Penumbra/packages.lock.json @@ -11,15 +11,6 @@ "Unosquare.Swan.Lite": "3.0.0" } }, - "Microsoft.Extensions.DependencyInjection": { - "type": "Direct", - "requested": "[7.0.0, )", - "resolved": "7.0.0", - "contentHash": "elNeOmkeX3eDVG6pYVeV82p29hr+UKDaBhrZyWvWLw/EVZSYEkZlQdkp0V39k/Xehs2Qa0mvoCvkVj3eQxNQ1Q==", - "dependencies": { - "Microsoft.Extensions.DependencyInjection.Abstractions": "7.0.0" - } - }, "SharpCompress": { "type": "Direct", "requested": "[0.33.0, )", @@ -47,10 +38,18 @@ "resolved": "3.1.3", "contentHash": "wybtaqZQ1ZRZ4ZeU+9h+PaSeV14nyiGKIy7qRbDfSHzHq4ybqyOcjoifeaYbiKLO1u+PVxLBuy7MF/DMmwwbfg==" }, + "Microsoft.Extensions.DependencyInjection": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "V8S3bsm50ig6JSyrbcJJ8bW2b9QLGouz+G1miK3UTaOWmMtFwNNNzUf4AleyDWUmTrWMLNnFSLEQtxmxgNQnNQ==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0" + } + }, "Microsoft.Extensions.DependencyInjection.Abstractions": { "type": "Transitive", - "resolved": "7.0.0", - "contentHash": "h3j/QfmFN4S0w4C2A6X7arXij/M/OVw3uQHSOFxnND4DyAzO1F9eMX7Eti7lU/OkSthEE0WzRsfT/Dmx86jzCw==" + "resolved": "8.0.0", + "contentHash": "cjWrLkJXK0rs4zofsK4bSdg+jhDLTaxrkXu4gS6Y7MAlCvRyNNgwY/lJi5RDlQOnSZweHqoyvgvbdvQsRIW+hg==" }, "SharpGLTF.Runtime": { "type": "Transitive", @@ -76,7 +75,7 @@ "ottergui": { "type": "Project", "dependencies": { - "Microsoft.Extensions.DependencyInjection": "[7.0.0, )" + "Microsoft.Extensions.DependencyInjection": "[8.0.0, )" } }, "penumbra.api": { @@ -89,7 +88,7 @@ "type": "Project", "dependencies": { "OtterGui": "[1.0.0, )", - "Penumbra.Api": "[1.0.14, )", + "Penumbra.Api": "[1.0.15, )", "Penumbra.String": "[1.0.4, )" } }, From aabd988a7747fc974171298b28bb6e9458555969 Mon Sep 17 00:00:00 2001 From: Actions User Date: Fri, 22 Mar 2024 12:38:55 +0000 Subject: [PATCH 067/433] [CI] Updating repo.json for testing_1.2.1.2 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 3aac5de6..a359a4ce 100644 --- a/repo.json +++ b/repo.json @@ -5,7 +5,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.0.1.0", - "TestingAssemblyVersion": "1.0.2.2", + "TestingAssemblyVersion": "1.2.1.2", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -17,7 +17,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.2/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.1.2/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From d0d35a79387c19e40baefc20eb339505d0533767 Mon Sep 17 00:00:00 2001 From: Ottermandias <70807659+Ottermandias@users.noreply.github.com> Date: Fri, 22 Mar 2024 13:39:46 +0100 Subject: [PATCH 068/433] Update repo.json --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index a359a4ce..3aac5de6 100644 --- a/repo.json +++ b/repo.json @@ -5,7 +5,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.0.1.0", - "TestingAssemblyVersion": "1.2.1.2", + "TestingAssemblyVersion": "1.0.2.2", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -17,7 +17,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.1.2/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.2/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 8dcd0451babfb5354808279a0cca8b62c18a5490 Mon Sep 17 00:00:00 2001 From: Ottermandias <70807659+Ottermandias@users.noreply.github.com> Date: Fri, 22 Mar 2024 13:43:38 +0100 Subject: [PATCH 069/433] Update repo.json --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 3aac5de6..44ada6e8 100644 --- a/repo.json +++ b/repo.json @@ -5,7 +5,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.0.1.0", - "TestingAssemblyVersion": "1.0.2.2", + "TestingAssemblyVersion": "1.0.2.3", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -17,7 +17,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.2/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.3/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 6792ed4f9489f061f657a536c323d588e6d53d56 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 22 Mar 2024 14:44:41 +0100 Subject: [PATCH 070/433] Remove the fucking test I'm a fucking moron. --- Penumbra/Penumbra.cs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index 34451dfa..ff068928 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -95,17 +95,6 @@ public class Penumbra : IDalamudPlugin if (_characterUtility.Ready) _residentResources.Reload(); - - var api = _services.GetService(); - api.PostEnabledDraw += ImGui.TextUnformatted; - api.PreSettingsTabBarDraw += (name, width, titleWidth) => - { - ImGui.TextUnformatted(width.ToString()); - ImGui.TextUnformatted(titleWidth.ToString()); - ImGui.TextUnformatted(ImGui.GetContentRegionMax().X.ToString()); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + (width - titleWidth) / 2); - ImGui.Button("Test", new Vector2(titleWidth, 0)); - }; } catch (Exception ex) { From 78a3ff177f8304c7ed9f0bc59b6f15985c6929c1 Mon Sep 17 00:00:00 2001 From: Actions User Date: Fri, 22 Mar 2024 13:46:45 +0000 Subject: [PATCH 071/433] [CI] Updating repo.json for testing_1.0.2.4 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 44ada6e8..2af4a645 100644 --- a/repo.json +++ b/repo.json @@ -5,7 +5,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.0.1.0", - "TestingAssemblyVersion": "1.0.2.3", + "TestingAssemblyVersion": "1.0.2.4", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -17,7 +17,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.3/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.4/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 4175a582b8ece7d088a27c6fd95cd2be5c2e54ef Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 22 Mar 2024 15:15:41 +0100 Subject: [PATCH 072/433] Add IPC Providers because I'm still a fucking moron. --- Penumbra/Api/PenumbraIpcProviders.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Penumbra/Api/PenumbraIpcProviders.cs b/Penumbra/Api/PenumbraIpcProviders.cs index d478b675..78887156 100644 --- a/Penumbra/Api/PenumbraIpcProviders.cs +++ b/Penumbra/Api/PenumbraIpcProviders.cs @@ -30,7 +30,9 @@ public class PenumbraIpcProviders : IDisposable internal readonly EventProvider ModDirectoryChanged; // UI + internal readonly EventProvider PreSettingsTabBarDraw; internal readonly EventProvider PreSettingsDraw; + internal readonly EventProvider PostEnabledDraw; internal readonly EventProvider PostSettingsDraw; internal readonly EventProvider ChangedItemTooltip; internal readonly EventProvider ChangedItemClick; @@ -130,8 +132,8 @@ public class PenumbraIpcProviders : IDisposable FuncProvider>> GetPlayerResourcesOfType; - internal readonly FuncProvider GetGameObjectResourceTrees; - internal readonly FuncProvider> GetPlayerResourceTrees; + internal readonly FuncProvider GetGameObjectResourceTrees; + internal readonly FuncProvider> GetPlayerResourceTrees; public PenumbraIpcProviders(DalamudPluginInterface pi, IPenumbraApi api, ModManager modManager, CollectionManager collections, TempModManager tempMods, TempCollectionManager tempCollections, SaveService saveService, Configuration config) @@ -153,7 +155,11 @@ public class PenumbraIpcProviders : IDisposable ModDirectoryChanged = Ipc.ModDirectoryChanged.Provider(pi, a => Api.ModDirectoryChanged += a, a => Api.ModDirectoryChanged -= a); // UI - PreSettingsDraw = Ipc.PreSettingsDraw.Provider(pi, a => Api.PreSettingsPanelDraw += a, a => Api.PreSettingsPanelDraw -= a); + PreSettingsTabBarDraw = + Ipc.PreSettingsTabBarDraw.Provider(pi, a => Api.PreSettingsTabBarDraw += a, a => Api.PreSettingsTabBarDraw -= a); + PreSettingsDraw = Ipc.PreSettingsDraw.Provider(pi, a => Api.PreSettingsPanelDraw += a, a => Api.PreSettingsPanelDraw -= a); + PostEnabledDraw = + Ipc.PostEnabledDraw.Provider(pi, a => Api.PostEnabledDraw += a, a => Api.PostEnabledDraw -= a); PostSettingsDraw = Ipc.PostSettingsDraw.Provider(pi, a => Api.PostSettingsPanelDraw += a, a => Api.PostSettingsPanelDraw -= a); ChangedItemTooltip = Ipc.ChangedItemTooltip.Provider(pi, () => Api.ChangedItemTooltip += OnTooltip, () => Api.ChangedItemTooltip -= OnTooltip); @@ -278,7 +284,9 @@ public class PenumbraIpcProviders : IDisposable ModDirectoryChanged.Dispose(); // UI + PreSettingsTabBarDraw.Dispose(); PreSettingsDraw.Dispose(); + PostEnabledDraw.Dispose(); PostSettingsDraw.Dispose(); ChangedItemTooltip.Dispose(); ChangedItemClick.Dispose(); From 12532dee2810c9f770a467a2f61c0c007226e373 Mon Sep 17 00:00:00 2001 From: Actions User Date: Fri, 22 Mar 2024 14:17:47 +0000 Subject: [PATCH 073/433] [CI] Updating repo.json for testing_1.0.2.5 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 2af4a645..5a51afa2 100644 --- a/repo.json +++ b/repo.json @@ -5,7 +5,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.0.1.0", - "TestingAssemblyVersion": "1.0.2.4", + "TestingAssemblyVersion": "1.0.2.5", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -17,7 +17,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.4/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.5/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From efdd5a824b16fb9e5a423062b644cea038a9275f Mon Sep 17 00:00:00 2001 From: Exter-N Date: Tue, 26 Mar 2024 02:29:07 +0100 Subject: [PATCH 074/433] Make human.pbd moddable --- Penumbra/Collections/Cache/CollectionCache.cs | 31 ++++-- .../Interop/ResourceLoading/ResourceLoader.cs | 28 ++++++ .../ResourceLoading/ResourceService.cs | 9 +- .../Interop/ResourceTree/ResolveContext.cs | 9 ++ Penumbra/Interop/ResourceTree/ResourceTree.cs | 18 +++- .../Interop/SafeHandles/SafeResourceHandle.cs | 10 +- Penumbra/Interop/Services/CharacterUtility.cs | 8 ++ .../Services/PreBoneDeformerReplacer.cs | 95 +++++++++++++++++++ .../Interop/Structs/CharacterUtilityData.cs | 4 + Penumbra/Penumbra.cs | 1 + Penumbra/Services/StaticServiceManager.cs | 1 + 11 files changed, 202 insertions(+), 12 deletions(-) create mode 100644 Penumbra/Interop/Services/PreBoneDeformerReplacer.cs diff --git a/Penumbra/Collections/Cache/CollectionCache.cs b/Penumbra/Collections/Cache/CollectionCache.cs index 9a0e525b..c2c215aa 100644 --- a/Penumbra/Collections/Cache/CollectionCache.cs +++ b/Penumbra/Collections/Cache/CollectionCache.cs @@ -8,6 +8,9 @@ using Penumbra.Mods.Editor; using Penumbra.String.Classes; using Penumbra.Mods.Manager; using Penumbra.Mods.Subclasses; +using Penumbra.Interop.SafeHandles; +using System.IO; +using static Penumbra.GameData.Files.ShpkFile; namespace Penumbra.Collections.Cache; @@ -20,13 +23,14 @@ public record ModConflicts(IMod Mod2, List Conflicts, bool HasPriority, /// public class CollectionCache : IDisposable { - private readonly CollectionCacheManager _manager; - private readonly ModCollection _collection; - public readonly CollectionModData ModData = new(); - private readonly SortedList, object?)> _changedItems = []; - public readonly ConcurrentDictionary ResolvedFiles = new(); - public readonly MetaCache Meta; - public readonly Dictionary> ConflictDict = []; + private readonly CollectionCacheManager _manager; + private readonly ModCollection _collection; + public readonly CollectionModData ModData = new(); + private readonly SortedList, object?)> _changedItems = []; + public readonly ConcurrentDictionary ResolvedFiles = new(); + public readonly ConcurrentDictionary LoadedResources = new(); + public readonly MetaCache Meta; + public readonly Dictionary> ConflictDict = []; public int Calculating = -1; @@ -136,6 +140,13 @@ public class CollectionCache : IDisposable public void RemoveMod(IMod mod, bool addMetaChanges) => _manager.AddChange(ChangeData.ModRemoval(this, mod, addMetaChanges)); + /// Invalidates caches subsequently to a resolved file being modified. + private void InvalidateResolvedFile(Utf8GamePath path) + { + if (LoadedResources.Remove(path, out var handle)) + handle.Dispose(); + } + /// Force a file to be resolved to a specific path regardless of conflicts. internal void ForceFileSync(Utf8GamePath path, FullPath fullPath) { @@ -148,17 +159,20 @@ public class CollectionCache : IDisposable if (fullPath.FullName.Length > 0) { ResolvedFiles.TryAdd(path, new ModPath(Mod.ForcedFiles, fullPath)); + InvalidateResolvedFile(path); InvokeResolvedFileChange(_collection, ResolvedFileChanged.Type.Replaced, path, fullPath, modPath.Path, Mod.ForcedFiles); } else { + InvalidateResolvedFile(path); InvokeResolvedFileChange(_collection, ResolvedFileChanged.Type.Removed, path, FullPath.Empty, modPath.Path, null); } } else if (fullPath.FullName.Length > 0) { ResolvedFiles.TryAdd(path, new ModPath(Mod.ForcedFiles, fullPath)); + InvalidateResolvedFile(path); InvokeResolvedFileChange(_collection, ResolvedFileChanged.Type.Added, path, fullPath, FullPath.Empty, Mod.ForcedFiles); } } @@ -181,6 +195,7 @@ public class CollectionCache : IDisposable { if (ResolvedFiles.Remove(path, out var mp)) { + InvalidateResolvedFile(path); if (mp.Mod != mod) Penumbra.Log.Warning( $"Invalid mod state, removing {mod.Name} and associated file {path} returned current mod {mp.Mod.Name}."); @@ -295,6 +310,7 @@ public class CollectionCache : IDisposable if (ResolvedFiles.TryAdd(path, new ModPath(mod, file))) { ModData.AddPath(mod, path); + InvalidateResolvedFile(path); InvokeResolvedFileChange(_collection, ResolvedFileChanged.Type.Added, path, file, FullPath.Empty, mod); return; } @@ -309,6 +325,7 @@ public class CollectionCache : IDisposable ModData.RemovePath(modPath.Mod, path); ResolvedFiles[path] = new ModPath(mod, file); ModData.AddPath(mod, path); + InvalidateResolvedFile(path); InvokeResolvedFileChange(_collection, ResolvedFileChanged.Type.Replaced, path, file, modPath.Path, mod); } } diff --git a/Penumbra/Interop/ResourceLoading/ResourceLoader.cs b/Penumbra/Interop/ResourceLoading/ResourceLoader.cs index 8ccdfa80..1b467a8c 100644 --- a/Penumbra/Interop/ResourceLoading/ResourceLoader.cs +++ b/Penumbra/Interop/ResourceLoading/ResourceLoader.cs @@ -1,6 +1,7 @@ using FFXIVClientStructs.FFXIV.Client.System.Resource; using Penumbra.Api.Enums; using Penumbra.Collections; +using Penumbra.Interop.SafeHandles; using Penumbra.Interop.Structs; using Penumbra.String; using Penumbra.String.Classes; @@ -39,6 +40,33 @@ public unsafe class ResourceLoader : IDisposable return ret; } + /// Load a resource for a given path and a specific collection. + public SafeResourceHandle LoadResolvedSafeResource(ResourceCategory category, ResourceType type, ByteString path, ResolveData resolveData) + { + _resolvedData = resolveData; + var ret = _resources.GetSafeResource(category, type, path); + _resolvedData = ResolveData.Invalid; + return ret; + } + + public SafeResourceHandle LoadCacheableSafeResource(ResourceCategory category, ResourceType type, Utf8GamePath path, ResolveData resolveData) + { + var cache = resolveData.ModCollection._cache; + if (cache == null) + return LoadResolvedSafeResource(category, type, path.Path, resolveData); + + if (cache.LoadedResources.TryGetValue(path, out var cached)) + return cached.Clone(); + + var ret = LoadResolvedSafeResource(category, type, path.Path, resolveData); + + cached = ret.Clone(); + if (!cache.LoadedResources.TryAdd(path, cached)) + cached.Dispose(); + + return ret; + } + /// The function to use to resolve a given path. public Func ResolvePath = null!; diff --git a/Penumbra/Interop/ResourceLoading/ResourceService.cs b/Penumbra/Interop/ResourceLoading/ResourceService.cs index 6fb2e560..e3338e6c 100644 --- a/Penumbra/Interop/ResourceLoading/ResourceService.cs +++ b/Penumbra/Interop/ResourceLoading/ResourceService.cs @@ -4,10 +4,12 @@ using Dalamud.Utility.Signatures; using FFXIVClientStructs.FFXIV.Client.System.Resource; using Penumbra.Api.Enums; using Penumbra.GameData; +using Penumbra.Interop.SafeHandles; using Penumbra.Interop.Structs; using Penumbra.String; using Penumbra.String.Classes; using Penumbra.Util; +using CSResourceHandle = FFXIVClientStructs.FFXIV.Client.System.Resource.Handle.ResourceHandle; namespace Penumbra.Interop.ResourceLoading; @@ -25,11 +27,11 @@ public unsafe class ResourceService : IDisposable _getResourceAsyncHook.Enable(); _resourceHandleDestructorHook.Enable(); _incRefHook = interop.HookFromAddress( - (nint)FFXIVClientStructs.FFXIV.Client.System.Resource.Handle.ResourceHandle.MemberFunctionPointers.IncRef, + (nint)CSResourceHandle.MemberFunctionPointers.IncRef, ResourceHandleIncRefDetour); _incRefHook.Enable(); _decRefHook = interop.HookFromAddress( - (nint)FFXIVClientStructs.FFXIV.Client.System.Resource.Handle.ResourceHandle.MemberFunctionPointers.DecRef, + (nint)CSResourceHandle.MemberFunctionPointers.DecRef, ResourceHandleDecRefDetour); _decRefHook.Enable(); } @@ -41,6 +43,9 @@ public unsafe class ResourceService : IDisposable &category, &type, &hash, path.Path, null, false); } + public SafeResourceHandle GetSafeResource(ResourceCategory category, ResourceType type, ByteString path) + => new((CSResourceHandle*)GetResource(category, type, path), false); + public void Dispose() { _getResourceSyncHook.Dispose(); diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.cs b/Penumbra/Interop/ResourceTree/ResolveContext.cs index d1701f47..615ef2b0 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.cs @@ -9,6 +9,7 @@ using Penumbra.Collections; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; +using Penumbra.Interop.Services; using Penumbra.String; using Penumbra.String.Classes; using Penumbra.UI; @@ -144,6 +145,14 @@ internal unsafe partial record ResolveContext( return GetOrCreateNode(ResourceType.Imc, 0, imc, path); } + public ResourceNode? CreateNodeFromPbd(ResourceHandle* pbd) + { + if (pbd == null) + return null; + + return GetOrCreateNode(ResourceType.Pbd, 0, pbd, PreBoneDeformerReplacer.PreBoneDeformerPath); + } + public ResourceNode? CreateNodeFromTex(TextureResourceHandle* tex, string gamePath) { if (tex == null) diff --git a/Penumbra/Interop/ResourceTree/ResourceTree.cs b/Penumbra/Interop/ResourceTree/ResourceTree.cs index 24112a9f..15546ed3 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTree.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTree.cs @@ -1,4 +1,3 @@ -using Dalamud.Game.ClientState.Objects.Enums; using FFXIVClientStructs.FFXIV.Client.Game.Character; using FFXIVClientStructs.FFXIV.Client.Graphics.Render; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; @@ -6,6 +5,7 @@ using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; +using Penumbra.Interop.Services; using Penumbra.UI; using CustomizeData = FFXIVClientStructs.FFXIV.Client.Game.Character.CustomizeData; using CustomizeIndex = Dalamud.Game.ClientState.Objects.Enums.CustomizeIndex; @@ -155,6 +155,22 @@ public class ResourceTree { var genericContext = globalContext.CreateContext(&human->CharacterBase); + var cache = globalContext.Collection._cache; + if (cache != null && cache.LoadedResources.TryGetValue(PreBoneDeformerReplacer.PreBoneDeformerPath, out var pbdHandle)) + { + var pbdNode = genericContext.CreateNodeFromPbd(pbdHandle.ResourceHandle); + if (pbdNode != null) + { + if (globalContext.WithUiData) + { + pbdNode = pbdNode.Clone(); + pbdNode.FallbackName = "Racial Deformer"; + pbdNode.Icon = ChangedItemDrawer.ChangedItemIcon.Customization; + } + Nodes.Add(pbdNode); + } + } + var decalId = (byte)(human->Customize[(int)CustomizeIndex.Facepaint] & 0x7F); var decalPath = decalId != 0 ? GamePaths.Human.Decal.FaceDecalPath(decalId) diff --git a/Penumbra/Interop/SafeHandles/SafeResourceHandle.cs b/Penumbra/Interop/SafeHandles/SafeResourceHandle.cs index 1f788a39..a5e73867 100644 --- a/Penumbra/Interop/SafeHandles/SafeResourceHandle.cs +++ b/Penumbra/Interop/SafeHandles/SafeResourceHandle.cs @@ -1,8 +1,8 @@ -using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; +using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; namespace Penumbra.Interop.SafeHandles; -public unsafe class SafeResourceHandle : SafeHandle +public unsafe class SafeResourceHandle : SafeHandle, ICloneable { public ResourceHandle* ResourceHandle => (ResourceHandle*)handle; @@ -21,6 +21,12 @@ public unsafe class SafeResourceHandle : SafeHandle SetHandle((nint)handle); } + public SafeResourceHandle Clone() + => new(ResourceHandle, true); + + object ICloneable.Clone() + => Clone(); + public static SafeResourceHandle CreateInvalid() => new(null, false); diff --git a/Penumbra/Interop/Services/CharacterUtility.cs b/Penumbra/Interop/Services/CharacterUtility.cs index 699b59e0..da04bf90 100644 --- a/Penumbra/Interop/Services/CharacterUtility.cs +++ b/Penumbra/Interop/Services/CharacterUtility.cs @@ -28,6 +28,7 @@ public unsafe class CharacterUtility : IDisposable public bool Ready { get; private set; } public event Action LoadingFinished; + public nint DefaultHumanPbdResource { get; private set; } public nint DefaultTransparentResource { get; private set; } public nint DefaultDecalResource { get; private set; } public nint DefaultSkinShpkResource { get; private set; } @@ -88,6 +89,12 @@ public unsafe class CharacterUtility : IDisposable anyMissing |= !_lists[i].Ready; } + if (DefaultHumanPbdResource == nint.Zero) + { + DefaultHumanPbdResource = (nint)Address->HumanPbdResource; + anyMissing |= DefaultHumanPbdResource == nint.Zero; + } + if (DefaultTransparentResource == nint.Zero) { DefaultTransparentResource = (nint)Address->TransparentTexResource; @@ -151,6 +158,7 @@ public unsafe class CharacterUtility : IDisposable foreach (var list in _lists) list.Dispose(); + Address->HumanPbdResource = (ResourceHandle*)DefaultHumanPbdResource; Address->TransparentTexResource = (TextureResourceHandle*)DefaultTransparentResource; Address->DecalTexResource = (TextureResourceHandle*)DefaultDecalResource; Address->SkinShpkResource = (ResourceHandle*)DefaultSkinShpkResource; diff --git a/Penumbra/Interop/Services/PreBoneDeformerReplacer.cs b/Penumbra/Interop/Services/PreBoneDeformerReplacer.cs new file mode 100644 index 00000000..44c6541e --- /dev/null +++ b/Penumbra/Interop/Services/PreBoneDeformerReplacer.cs @@ -0,0 +1,95 @@ +using Dalamud.Hooking; +using Dalamud.Plugin.Services; +using Dalamud.Utility.Signatures; +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using FFXIVClientStructs.FFXIV.Client.System.Resource; +using Penumbra.Api.Enums; +using Penumbra.GameData; +using Penumbra.Interop.PathResolving; +using Penumbra.Interop.ResourceLoading; +using Penumbra.Interop.SafeHandles; +using Penumbra.String.Classes; + +namespace Penumbra.Interop.Services; + +public sealed unsafe class PreBoneDeformerReplacer : IDisposable +{ + public static readonly Utf8GamePath PreBoneDeformerPath = + Utf8GamePath.FromSpan("chara/xls/boneDeformer/human.pbd"u8, out var p) ? p : Utf8GamePath.Empty; + + [Signature(Sigs.HumanVTable, ScanType = ScanType.StaticAddress)] + private readonly nint* _humanVTable = null!; + + // Approximate name guesses. + private delegate void CharacterBaseSetupScalingDelegate(CharacterBase* drawObject, uint slotIndex); + private delegate void* CharacterBaseCreateDeformerDelegate(CharacterBase* drawObject, uint slotIndex); + + private readonly Hook _humanSetupScalingHook; + private readonly Hook _humanCreateDeformerHook; + + private readonly CharacterUtility _utility; + private readonly CollectionResolver _collectionResolver; + private readonly ResourceLoader _resourceLoader; + private readonly IFramework _framework; + + public PreBoneDeformerReplacer(CharacterUtility utility, CollectionResolver collectionResolver, ResourceLoader resourceLoader, IGameInteropProvider interop, IFramework framework) + { + interop.InitializeFromAttributes(this); + _utility = utility; + _collectionResolver = collectionResolver; + _resourceLoader = resourceLoader; + _framework = framework; + _humanSetupScalingHook = interop.HookFromAddress(_humanVTable[57], SetupScaling); + _humanCreateDeformerHook = interop.HookFromAddress(_humanVTable[91], CreateDeformer); + _humanSetupScalingHook.Enable(); + _humanCreateDeformerHook.Enable(); + } + + public void Dispose() + { + _humanCreateDeformerHook.Dispose(); + _humanSetupScalingHook.Dispose(); + } + + private SafeResourceHandle GetPreBoneDeformerForCharacter(CharacterBase* drawObject) + { + var resolveData = _collectionResolver.IdentifyCollection(&drawObject->DrawObject, true); + return _resourceLoader.LoadCacheableSafeResource(ResourceCategory.Chara, ResourceType.Pbd, PreBoneDeformerPath, resolveData); + } + + private void SetupScaling(CharacterBase* drawObject, uint slotIndex) + { + if (!_framework.IsInFrameworkUpdateThread) + Penumbra.Log.Warning($"{nameof(PreBoneDeformerReplacer)}.{nameof(SetupScaling)}(0x{(nint)drawObject:X}, {slotIndex}) called out of framework thread"); + + using var preBoneDeformer = GetPreBoneDeformerForCharacter(drawObject); + try + { + if (!preBoneDeformer.IsInvalid) + _utility.Address->HumanPbdResource = (Structs.ResourceHandle*)preBoneDeformer.ResourceHandle; + _humanSetupScalingHook.Original(drawObject, slotIndex); + } + finally + { + _utility.Address->HumanPbdResource = (Structs.ResourceHandle*)_utility.DefaultHumanPbdResource; + } + } + + private void* CreateDeformer(CharacterBase* drawObject, uint slotIndex) + { + if (!_framework.IsInFrameworkUpdateThread) + Penumbra.Log.Warning($"{nameof(PreBoneDeformerReplacer)}.{nameof(CreateDeformer)}(0x{(nint)drawObject:X}, {slotIndex}) called out of framework thread"); + + using var preBoneDeformer = GetPreBoneDeformerForCharacter(drawObject); + try + { + if (!preBoneDeformer.IsInvalid) + _utility.Address->HumanPbdResource = (Structs.ResourceHandle*)preBoneDeformer.ResourceHandle; + return _humanCreateDeformerHook.Original(drawObject, slotIndex); + } + finally + { + _utility.Address->HumanPbdResource = (Structs.ResourceHandle*)_utility.DefaultHumanPbdResource; + } + } +} diff --git a/Penumbra/Interop/Structs/CharacterUtilityData.cs b/Penumbra/Interop/Structs/CharacterUtilityData.cs index 08857292..22150cc1 100644 --- a/Penumbra/Interop/Structs/CharacterUtilityData.cs +++ b/Penumbra/Interop/Structs/CharacterUtilityData.cs @@ -5,6 +5,7 @@ namespace Penumbra.Interop.Structs; [StructLayout(LayoutKind.Explicit)] public unsafe struct CharacterUtilityData { + public const int IndexHumanPbd = 63; public const int IndexTransparentTex = 72; public const int IndexDecalTex = 73; public const int IndexSkinShpk = 76; @@ -72,6 +73,9 @@ public unsafe struct CharacterUtilityData public ResourceHandle* EqdpResource(GenderRace raceCode, bool accessory) => Resource((int)EqdpIdx(raceCode, accessory)); + [FieldOffset(8 + IndexHumanPbd * 8)] + public ResourceHandle* HumanPbdResource; + [FieldOffset(8 + (int)MetaIndex.HumanCmp * 8)] public ResourceHandle* HumanCmpResource; diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index ff068928..c34a7771 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -77,6 +77,7 @@ public class Penumbra : IDalamudPlugin _services.GetService(); // Initialize because not required anywhere else. _collectionManager.Caches.CreateNecessaryCaches(); _services.GetService(); + _services.GetService(); _services.GetService(); _services.GetService(); // Initialize before Interface. diff --git a/Penumbra/Services/StaticServiceManager.cs b/Penumbra/Services/StaticServiceManager.cs index 66c90e84..6f85ddd2 100644 --- a/Penumbra/Services/StaticServiceManager.cs +++ b/Penumbra/Services/StaticServiceManager.cs @@ -132,6 +132,7 @@ public static class StaticServiceManager .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton(); private static ServiceManager AddResolvers(this ServiceManager services) From 3066bf84d5fc4398e642d21c0449ba7a2300e6c2 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Tue, 26 Mar 2024 02:32:33 +0100 Subject: [PATCH 075/433] Where did these `using`s even come from? --- Penumbra/Collections/Cache/CollectionCache.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Penumbra/Collections/Cache/CollectionCache.cs b/Penumbra/Collections/Cache/CollectionCache.cs index c2c215aa..00968175 100644 --- a/Penumbra/Collections/Cache/CollectionCache.cs +++ b/Penumbra/Collections/Cache/CollectionCache.cs @@ -9,8 +9,6 @@ using Penumbra.String.Classes; using Penumbra.Mods.Manager; using Penumbra.Mods.Subclasses; using Penumbra.Interop.SafeHandles; -using System.IO; -using static Penumbra.GameData.Files.ShpkFile; namespace Penumbra.Collections.Cache; From e793e7793b1e4eeae991ba0beb4f7f49745b2bd4 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 28 Mar 2024 15:19:19 +0100 Subject: [PATCH 076/433] Fix model import resetting dirty flag. --- .../UI/AdvancedWindow/ModEditWindow.Models.cs | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index 3672dce7..737c41d9 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -35,39 +35,28 @@ public partial class ModEditWindow private void UpdateFile(MdlFile file, bool force) { - if (file == _lastFile) + if (file == _lastFile && !force) + return; + + _lastFile = file; + var subMeshTotal = file.Meshes.Aggregate(0, (count, mesh) => count + mesh.SubMeshCount); + if (_subMeshAttributeTagWidgets.Count != subMeshTotal) { - if (force) - UpdateMeshes(); - } - else - { - UpdateMeshes(); - _lastFile = file; - _lodTriCount = Enumerable.Range(0, file.Lods.Length).Select(l => GetTriangleCountForLod(file, l)).ToArray(); + _subMeshAttributeTagWidgets.Clear(); + _subMeshAttributeTagWidgets.AddRange( + Enumerable.Range(0, subMeshTotal).Select(_ => new TagButtons()) + ); } - return; - - void UpdateMeshes() - { - var subMeshTotal = file.Meshes.Aggregate(0, (count, mesh) => count + mesh.SubMeshCount); - if (_subMeshAttributeTagWidgets.Count != subMeshTotal) - { - _subMeshAttributeTagWidgets.Clear(); - _subMeshAttributeTagWidgets.AddRange( - Enumerable.Range(0, subMeshTotal).Select(_ => new TagButtons()) - ); - } - } + _lodTriCount = Enumerable.Range(0, file.Lods.Length).Select(l => GetTriangleCountForLod(file, l)).ToArray(); } private bool DrawModelPanel(MdlTab tab, bool disabled) { - UpdateFile(tab.Mdl, tab.Dirty); + var ret = tab.Dirty; + UpdateFile(tab.Mdl, ret); DrawImportExport(tab, disabled); - var ret = tab.Dirty; ret |= DrawModelMaterialDetails(tab, disabled); if (ImGui.CollapsingHeader($"Meshes ({_lastFile.Meshes.Length})###meshes")) From 1ba5011bfa5120821a30a39a8953f33329a59bc7 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 28 Mar 2024 17:37:17 +0100 Subject: [PATCH 077/433] Small changes. --- Penumbra/Collections/Cache/CollectionCache.cs | 65 +++++++++---------- .../Cache/CollectionCacheManager.cs | 8 ++- .../Collections/Cache/CustomResourceCache.cs | 49 ++++++++++++++ .../Interop/ResourceLoading/ResourceLoader.cs | 18 ----- Penumbra/Interop/ResourceTree/ResourceTree.cs | 2 +- .../Services/PreBoneDeformerReplacer.cs | 34 +++++----- .../Services/ShaderReplacementFixer.cs | 33 +++++----- Penumbra/Penumbra.cs | 2 - Penumbra/Services/StaticServiceManager.cs | 4 +- 9 files changed, 120 insertions(+), 95 deletions(-) create mode 100644 Penumbra/Collections/Cache/CustomResourceCache.cs diff --git a/Penumbra/Collections/Cache/CollectionCache.cs b/Penumbra/Collections/Cache/CollectionCache.cs index 00968175..6b2b688b 100644 --- a/Penumbra/Collections/Cache/CollectionCache.cs +++ b/Penumbra/Collections/Cache/CollectionCache.cs @@ -8,7 +8,6 @@ using Penumbra.Mods.Editor; using Penumbra.String.Classes; using Penumbra.Mods.Manager; using Penumbra.Mods.Subclasses; -using Penumbra.Interop.SafeHandles; namespace Penumbra.Collections.Cache; @@ -19,16 +18,16 @@ public record ModConflicts(IMod Mod2, List Conflicts, bool HasPriority, /// The Cache contains all required temporary data to use a collection. /// It will only be setup if a collection gets activated in any way. /// -public class CollectionCache : IDisposable +public sealed class CollectionCache : IDisposable { - private readonly CollectionCacheManager _manager; - private readonly ModCollection _collection; - public readonly CollectionModData ModData = new(); - private readonly SortedList, object?)> _changedItems = []; - public readonly ConcurrentDictionary ResolvedFiles = new(); - public readonly ConcurrentDictionary LoadedResources = new(); - public readonly MetaCache Meta; - public readonly Dictionary> ConflictDict = []; + private readonly CollectionCacheManager _manager; + private readonly ModCollection _collection; + public readonly CollectionModData ModData = new(); + private readonly SortedList, object?)> _changedItems = []; + public readonly ConcurrentDictionary ResolvedFiles = new(); + public readonly CustomResourceCache CustomResources; + public readonly MetaCache Meta; + public readonly Dictionary> ConflictDict = []; public int Calculating = -1; @@ -39,7 +38,7 @@ public class CollectionCache : IDisposable => ConflictDict.Values; public SingleArray Conflicts(IMod mod) - => ConflictDict.TryGetValue(mod, out SingleArray c) ? c : new SingleArray(); + => ConflictDict.TryGetValue(mod, out var c) ? c : new SingleArray(); private int _changedItemsSaveCounter = -1; @@ -56,16 +55,21 @@ public class CollectionCache : IDisposable // The cache reacts through events on its collection changing. public CollectionCache(CollectionCacheManager manager, ModCollection collection) { - _manager = manager; - _collection = collection; - Meta = new MetaCache(manager.MetaFileManager, _collection); + _manager = manager; + _collection = collection; + Meta = new MetaCache(manager.MetaFileManager, _collection); + CustomResources = new CustomResourceCache(manager.ResourceLoader); } public void Dispose() - => Meta.Dispose(); + { + Meta.Dispose(); + CustomResources.Dispose(); + GC.SuppressFinalize(this); + } ~CollectionCache() - => Meta.Dispose(); + => Dispose(); // Resolve a given game path according to this collection. public FullPath? ResolvePath(Utf8GamePath gameResourcePath) @@ -74,7 +78,7 @@ public class CollectionCache : IDisposable return null; if (candidate.Path.InternalName.Length > Utf8GamePath.MaxGamePathLength - || candidate.Path.IsRooted && !candidate.Path.Exists) + || candidate.Path is { IsRooted: true, Exists: false }) return null; return candidate.Path; @@ -102,7 +106,7 @@ public class CollectionCache : IDisposable public HashSet[] ReverseResolvePaths(IReadOnlyCollection fullPaths) { if (fullPaths.Count == 0) - return Array.Empty>(); + return []; var ret = new HashSet[fullPaths.Count]; var dict = new Dictionary(fullPaths.Count); @@ -110,8 +114,8 @@ public class CollectionCache : IDisposable { dict[new FullPath(path)] = idx; ret[idx] = !Path.IsPathRooted(path) && Utf8GamePath.FromString(path, out var utf8) - ? new HashSet { utf8 } - : new HashSet(); + ? [utf8] + : []; } foreach (var (game, full) in ResolvedFiles) @@ -138,13 +142,6 @@ public class CollectionCache : IDisposable public void RemoveMod(IMod mod, bool addMetaChanges) => _manager.AddChange(ChangeData.ModRemoval(this, mod, addMetaChanges)); - /// Invalidates caches subsequently to a resolved file being modified. - private void InvalidateResolvedFile(Utf8GamePath path) - { - if (LoadedResources.Remove(path, out var handle)) - handle.Dispose(); - } - /// Force a file to be resolved to a specific path regardless of conflicts. internal void ForceFileSync(Utf8GamePath path, FullPath fullPath) { @@ -157,20 +154,20 @@ public class CollectionCache : IDisposable if (fullPath.FullName.Length > 0) { ResolvedFiles.TryAdd(path, new ModPath(Mod.ForcedFiles, fullPath)); - InvalidateResolvedFile(path); + CustomResources.Invalidate(path); InvokeResolvedFileChange(_collection, ResolvedFileChanged.Type.Replaced, path, fullPath, modPath.Path, Mod.ForcedFiles); } else { - InvalidateResolvedFile(path); + CustomResources.Invalidate(path); InvokeResolvedFileChange(_collection, ResolvedFileChanged.Type.Removed, path, FullPath.Empty, modPath.Path, null); } } else if (fullPath.FullName.Length > 0) { ResolvedFiles.TryAdd(path, new ModPath(Mod.ForcedFiles, fullPath)); - InvalidateResolvedFile(path); + CustomResources.Invalidate(path); InvokeResolvedFileChange(_collection, ResolvedFileChanged.Type.Added, path, fullPath, FullPath.Empty, Mod.ForcedFiles); } } @@ -193,7 +190,7 @@ public class CollectionCache : IDisposable { if (ResolvedFiles.Remove(path, out var mp)) { - InvalidateResolvedFile(path); + CustomResources.Invalidate(path); if (mp.Mod != mod) Penumbra.Log.Warning( $"Invalid mod state, removing {mod.Name} and associated file {path} returned current mod {mp.Mod.Name}."); @@ -308,7 +305,7 @@ public class CollectionCache : IDisposable if (ResolvedFiles.TryAdd(path, new ModPath(mod, file))) { ModData.AddPath(mod, path); - InvalidateResolvedFile(path); + CustomResources.Invalidate(path); InvokeResolvedFileChange(_collection, ResolvedFileChanged.Type.Added, path, file, FullPath.Empty, mod); return; } @@ -323,14 +320,14 @@ public class CollectionCache : IDisposable ModData.RemovePath(modPath.Mod, path); ResolvedFiles[path] = new ModPath(mod, file); ModData.AddPath(mod, path); - InvalidateResolvedFile(path); + CustomResources.Invalidate(path); InvokeResolvedFileChange(_collection, ResolvedFileChanged.Type.Replaced, path, file, modPath.Path, mod); } } catch (Exception ex) { Penumbra.Log.Error( - $"[{Thread.CurrentThread.ManagedThreadId}] Error adding redirection {file} -> {path} for mod {mod.Name} to collection cache {AnonymizedName}:\n{ex}"); + $"[{Environment.CurrentManagedThreadId}] Error adding redirection {file} -> {path} for mod {mod.Name} to collection cache {AnonymizedName}:\n{ex}"); } } diff --git a/Penumbra/Collections/Cache/CollectionCacheManager.cs b/Penumbra/Collections/Cache/CollectionCacheManager.cs index 94b3ef5a..5a6b5593 100644 --- a/Penumbra/Collections/Cache/CollectionCacheManager.cs +++ b/Penumbra/Collections/Cache/CollectionCacheManager.cs @@ -4,6 +4,7 @@ using Penumbra.Api; using Penumbra.Api.Enums; using Penumbra.Collections.Manager; using Penumbra.Communication; +using Penumbra.Interop.ResourceLoading; using Penumbra.Meta; using Penumbra.Mods; using Penumbra.Mods.Manager; @@ -21,8 +22,8 @@ public class CollectionCacheManager : IDisposable private readonly CollectionStorage _storage; private readonly ActiveCollections _active; internal readonly ResolvedFileChanged ResolvedFileChanged; - - internal readonly MetaFileManager MetaFileManager; + internal readonly MetaFileManager MetaFileManager; + internal readonly ResourceLoader ResourceLoader; private readonly ConcurrentQueue _changeQueue = new(); @@ -35,7 +36,7 @@ public class CollectionCacheManager : IDisposable => _storage.Where(c => c.HasCache); public CollectionCacheManager(FrameworkManager framework, CommunicatorService communicator, TempModManager tempMods, ModStorage modStorage, - MetaFileManager metaFileManager, ActiveCollections active, CollectionStorage storage) + MetaFileManager metaFileManager, ActiveCollections active, CollectionStorage storage, ResourceLoader resourceLoader) { _framework = framework; _communicator = communicator; @@ -44,6 +45,7 @@ public class CollectionCacheManager : IDisposable MetaFileManager = metaFileManager; _active = active; _storage = storage; + ResourceLoader = resourceLoader; ResolvedFileChanged = _communicator.ResolvedFileChanged; if (!_active.Individuals.IsLoaded) diff --git a/Penumbra/Collections/Cache/CustomResourceCache.cs b/Penumbra/Collections/Cache/CustomResourceCache.cs new file mode 100644 index 00000000..46c28393 --- /dev/null +++ b/Penumbra/Collections/Cache/CustomResourceCache.cs @@ -0,0 +1,49 @@ +using FFXIVClientStructs.FFXIV.Client.System.Resource; +using Penumbra.Api.Enums; +using Penumbra.Interop.ResourceLoading; +using Penumbra.Interop.SafeHandles; +using Penumbra.String.Classes; + +namespace Penumbra.Collections.Cache; + +/// A cache for resources owned by a collection. +public sealed class CustomResourceCache(ResourceLoader loader) + : ConcurrentDictionary, IDisposable +{ + /// Invalidate an existing resource by clearing it from the cache and disposing it. + public void Invalidate(Utf8GamePath path) + { + if (TryRemove(path, out var handle)) + handle.Dispose(); + } + + public void Dispose() + { + foreach (var handle in Values) + handle.Dispose(); + Clear(); + } + + /// Get the requested resource either from the cached resource, or load a new one if it does not exist. + public SafeResourceHandle Get(ResourceCategory category, ResourceType type, Utf8GamePath path, ResolveData data) + { + if (TryGetClonedValue(path, out var handle)) + return handle; + + handle = loader.LoadResolvedSafeResource(category, type, path.Path, data); + var clone = handle.Clone(); + if (!TryAdd(path, clone)) + clone.Dispose(); + return handle; + } + + /// Get a cloned cached resource if it exists. + private bool TryGetClonedValue(Utf8GamePath path, [NotNullWhen(true)] out SafeResourceHandle? handle) + { + if (!TryGetValue(path, out handle)) + return false; + + handle = handle.Clone(); + return true; + } +} diff --git a/Penumbra/Interop/ResourceLoading/ResourceLoader.cs b/Penumbra/Interop/ResourceLoading/ResourceLoader.cs index 1b467a8c..6c2c83b3 100644 --- a/Penumbra/Interop/ResourceLoading/ResourceLoader.cs +++ b/Penumbra/Interop/ResourceLoading/ResourceLoader.cs @@ -49,24 +49,6 @@ public unsafe class ResourceLoader : IDisposable return ret; } - public SafeResourceHandle LoadCacheableSafeResource(ResourceCategory category, ResourceType type, Utf8GamePath path, ResolveData resolveData) - { - var cache = resolveData.ModCollection._cache; - if (cache == null) - return LoadResolvedSafeResource(category, type, path.Path, resolveData); - - if (cache.LoadedResources.TryGetValue(path, out var cached)) - return cached.Clone(); - - var ret = LoadResolvedSafeResource(category, type, path.Path, resolveData); - - cached = ret.Clone(); - if (!cache.LoadedResources.TryAdd(path, cached)) - cached.Dispose(); - - return ret; - } - /// The function to use to resolve a given path. public Func ResolvePath = null!; diff --git a/Penumbra/Interop/ResourceTree/ResourceTree.cs b/Penumbra/Interop/ResourceTree/ResourceTree.cs index 15546ed3..dac86e44 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTree.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTree.cs @@ -156,7 +156,7 @@ public class ResourceTree var genericContext = globalContext.CreateContext(&human->CharacterBase); var cache = globalContext.Collection._cache; - if (cache != null && cache.LoadedResources.TryGetValue(PreBoneDeformerReplacer.PreBoneDeformerPath, out var pbdHandle)) + if (cache != null && cache.CustomResources.TryGetValue(PreBoneDeformerReplacer.PreBoneDeformerPath, out var pbdHandle)) { var pbdNode = genericContext.CreateNodeFromPbd(pbdHandle.ResourceHandle); if (pbdNode != null) diff --git a/Penumbra/Interop/Services/PreBoneDeformerReplacer.cs b/Penumbra/Interop/Services/PreBoneDeformerReplacer.cs index 44c6541e..9f553257 100644 --- a/Penumbra/Interop/Services/PreBoneDeformerReplacer.cs +++ b/Penumbra/Interop/Services/PreBoneDeformerReplacer.cs @@ -1,10 +1,9 @@ using Dalamud.Hooking; using Dalamud.Plugin.Services; -using Dalamud.Utility.Signatures; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using FFXIVClientStructs.FFXIV.Client.System.Resource; +using OtterGui.Services; using Penumbra.Api.Enums; -using Penumbra.GameData; using Penumbra.Interop.PathResolving; using Penumbra.Interop.ResourceLoading; using Penumbra.Interop.SafeHandles; @@ -12,14 +11,11 @@ using Penumbra.String.Classes; namespace Penumbra.Interop.Services; -public sealed unsafe class PreBoneDeformerReplacer : IDisposable +public sealed unsafe class PreBoneDeformerReplacer : IDisposable, IRequiredService { public static readonly Utf8GamePath PreBoneDeformerPath = Utf8GamePath.FromSpan("chara/xls/boneDeformer/human.pbd"u8, out var p) ? p : Utf8GamePath.Empty; - [Signature(Sigs.HumanVTable, ScanType = ScanType.StaticAddress)] - private readonly nint* _humanVTable = null!; - // Approximate name guesses. private delegate void CharacterBaseSetupScalingDelegate(CharacterBase* drawObject, uint slotIndex); private delegate void* CharacterBaseCreateDeformerDelegate(CharacterBase* drawObject, uint slotIndex); @@ -32,15 +28,16 @@ public sealed unsafe class PreBoneDeformerReplacer : IDisposable private readonly ResourceLoader _resourceLoader; private readonly IFramework _framework; - public PreBoneDeformerReplacer(CharacterUtility utility, CollectionResolver collectionResolver, ResourceLoader resourceLoader, IGameInteropProvider interop, IFramework framework) + public PreBoneDeformerReplacer(CharacterUtility utility, CollectionResolver collectionResolver, ResourceLoader resourceLoader, + IGameInteropProvider interop, IFramework framework, CharacterBaseVTables vTables) { interop.InitializeFromAttributes(this); - _utility = utility; - _collectionResolver = collectionResolver; - _resourceLoader = resourceLoader; - _framework = framework; - _humanSetupScalingHook = interop.HookFromAddress(_humanVTable[57], SetupScaling); - _humanCreateDeformerHook = interop.HookFromAddress(_humanVTable[91], CreateDeformer); + _utility = utility; + _collectionResolver = collectionResolver; + _resourceLoader = resourceLoader; + _framework = framework; + _humanSetupScalingHook = interop.HookFromAddress(vTables.HumanVTable[57], SetupScaling); + _humanCreateDeformerHook = interop.HookFromAddress(vTables.HumanVTable[91], CreateDeformer); _humanSetupScalingHook.Enable(); _humanCreateDeformerHook.Enable(); } @@ -54,13 +51,17 @@ public sealed unsafe class PreBoneDeformerReplacer : IDisposable private SafeResourceHandle GetPreBoneDeformerForCharacter(CharacterBase* drawObject) { var resolveData = _collectionResolver.IdentifyCollection(&drawObject->DrawObject, true); - return _resourceLoader.LoadCacheableSafeResource(ResourceCategory.Chara, ResourceType.Pbd, PreBoneDeformerPath, resolveData); + if (resolveData.ModCollection._cache is not { } cache) + return _resourceLoader.LoadResolvedSafeResource(ResourceCategory.Chara, ResourceType.Pbd, PreBoneDeformerPath.Path, resolveData); + + return cache.CustomResources.Get(ResourceCategory.Chara, ResourceType.Pbd, PreBoneDeformerPath, resolveData); } private void SetupScaling(CharacterBase* drawObject, uint slotIndex) { if (!_framework.IsInFrameworkUpdateThread) - Penumbra.Log.Warning($"{nameof(PreBoneDeformerReplacer)}.{nameof(SetupScaling)}(0x{(nint)drawObject:X}, {slotIndex}) called out of framework thread"); + Penumbra.Log.Warning( + $"{nameof(PreBoneDeformerReplacer)}.{nameof(SetupScaling)}(0x{(nint)drawObject:X}, {slotIndex}) called out of framework thread"); using var preBoneDeformer = GetPreBoneDeformerForCharacter(drawObject); try @@ -78,7 +79,8 @@ public sealed unsafe class PreBoneDeformerReplacer : IDisposable private void* CreateDeformer(CharacterBase* drawObject, uint slotIndex) { if (!_framework.IsInFrameworkUpdateThread) - Penumbra.Log.Warning($"{nameof(PreBoneDeformerReplacer)}.{nameof(CreateDeformer)}(0x{(nint)drawObject:X}, {slotIndex}) called out of framework thread"); + Penumbra.Log.Warning( + $"{nameof(PreBoneDeformerReplacer)}.{nameof(CreateDeformer)}(0x{(nint)drawObject:X}, {slotIndex}) called out of framework thread"); using var preBoneDeformer = GetPreBoneDeformerForCharacter(drawObject); try diff --git a/Penumbra/Interop/Services/ShaderReplacementFixer.cs b/Penumbra/Interop/Services/ShaderReplacementFixer.cs index 26906ace..3809ecbd 100644 --- a/Penumbra/Interop/Services/ShaderReplacementFixer.cs +++ b/Penumbra/Interop/Services/ShaderReplacementFixer.cs @@ -5,6 +5,7 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Render; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; using OtterGui.Classes; +using OtterGui.Services; using Penumbra.Communication; using Penumbra.GameData; using Penumbra.Interop.Hooks.Resources; @@ -13,7 +14,7 @@ using CSModelRenderer = FFXIVClientStructs.FFXIV.Client.Graphics.Render.ModelRen namespace Penumbra.Interop.Services; -public sealed unsafe class ShaderReplacementFixer : IDisposable +public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredService { public static ReadOnlySpan SkinShpkName => "skin.shpk"u8; @@ -21,11 +22,10 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable public static ReadOnlySpan CharacterGlassShpkName => "characterglass.shpk"u8; - [Signature(Sigs.HumanVTable, ScanType = ScanType.StaticAddress)] - private readonly nint* _humanVTable = null!; - private delegate nint CharacterBaseOnRenderMaterialDelegate(CharacterBase* drawObject, CSModelRenderer.OnRenderMaterialParams* param); - private delegate nint ModelRendererOnRenderMaterialDelegate(CSModelRenderer* modelRenderer, ushort* outFlags, CSModelRenderer.OnRenderModelParams* param, Material* material, uint materialIndex); + + private delegate nint ModelRendererOnRenderMaterialDelegate(CSModelRenderer* modelRenderer, ushort* outFlags, + CSModelRenderer.OnRenderModelParams* param, Material* material, uint materialIndex); private readonly Hook _humanOnRenderMaterialHook; @@ -59,14 +59,15 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable => _moddedCharacterGlassShpkCount; public ShaderReplacementFixer(ResourceHandleDestructor resourceHandleDestructor, CharacterUtility utility, ModelRenderer modelRenderer, - CommunicatorService communicator, IGameInteropProvider interop) + CommunicatorService communicator, IGameInteropProvider interop, CharacterBaseVTables vTables) { interop.InitializeFromAttributes(this); - _resourceHandleDestructor = resourceHandleDestructor; - _utility = utility; - _modelRenderer = modelRenderer; - _communicator = communicator; - _humanOnRenderMaterialHook = interop.HookFromAddress(_humanVTable[62], OnRenderHumanMaterial); + _resourceHandleDestructor = resourceHandleDestructor; + _utility = utility; + _modelRenderer = modelRenderer; + _communicator = communicator; + _humanOnRenderMaterialHook = + interop.HookFromAddress(vTables.HumanVTable[62], OnRenderHumanMaterial); _communicator.MtrlShpkLoaded.Subscribe(OnMtrlShpkLoaded, MtrlShpkLoaded.Priority.ShaderReplacementFixer); _resourceHandleDestructor.Subscribe(OnResourceHandleDestructor, ResourceHandleDestructor.Priority.ShaderReplacementFixer); _humanOnRenderMaterialHook.Enable(); @@ -82,7 +83,7 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable _moddedCharacterGlassShpkMaterials.Clear(); _moddedSkinShpkMaterials.Clear(); _moddedCharacterGlassShpkCount = 0; - _moddedSkinShpkCount = 0; + _moddedSkinShpkCount = 0; } public (ulong Skin, ulong CharacterGlass) GetAndResetSlowPathCallDeltas() @@ -106,16 +107,12 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable var shpkName = mtrl->ShpkNameSpan; if (SkinShpkName.SequenceEqual(shpkName) && (nint)shpk != _utility.DefaultSkinShpkResource) - { if (_moddedSkinShpkMaterials.TryAdd(mtrlResourceHandle)) Interlocked.Increment(ref _moddedSkinShpkCount); - } if (CharacterGlassShpkName.SequenceEqual(shpkName) && shpk != _modelRenderer.DefaultCharacterGlassShaderPackage) - { if (_moddedCharacterGlassShpkMaterials.TryAdd(mtrlResourceHandle)) Interlocked.Increment(ref _moddedCharacterGlassShpkCount); - } } private void OnResourceHandleDestructor(Structs.ResourceHandle* handle) @@ -159,9 +156,9 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable } } - private nint ModelRendererOnRenderMaterialDetour(CSModelRenderer* modelRenderer, ushort* outFlags, CSModelRenderer.OnRenderModelParams* param, Material* material, uint materialIndex) + private nint ModelRendererOnRenderMaterialDetour(CSModelRenderer* modelRenderer, ushort* outFlags, + CSModelRenderer.OnRenderModelParams* param, Material* material, uint materialIndex) { - // If we don't have any on-screen instances of modded characterglass.shpk, we don't need the slow path at all. if (!Enabled || _moddedCharacterGlassShpkCount == 0) return _modelRendererOnRenderMaterialHook.Original(modelRenderer, outFlags, param, material, materialIndex); diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index c34a7771..b76780c0 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -77,8 +77,6 @@ public class Penumbra : IDalamudPlugin _services.GetService(); // Initialize because not required anywhere else. _collectionManager.Caches.CreateNecessaryCaches(); _services.GetService(); - _services.GetService(); - _services.GetService(); _services.GetService(); // Initialize before Interface. diff --git a/Penumbra/Services/StaticServiceManager.cs b/Penumbra/Services/StaticServiceManager.cs index 6f85ddd2..9e6071b4 100644 --- a/Penumbra/Services/StaticServiceManager.cs +++ b/Penumbra/Services/StaticServiceManager.cs @@ -131,9 +131,7 @@ public static class StaticServiceManager => services.AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton(); + .AddSingleton(); private static ServiceManager AddResolvers(this ServiceManager services) => services.AddSingleton() From b04cb343dd5aacd4a6134ad28e7dca1154cce979 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 28 Mar 2024 19:32:10 +0100 Subject: [PATCH 078/433] Make setting for crash handler. --- Penumbra/Configuration.cs | 18 +++--- Penumbra/Services/CrashHandlerService.cs | 6 +- Penumbra/UI/Tabs/SettingsTab.cs | 77 +++++++++++++++--------- 3 files changed, 59 insertions(+), 42 deletions(-) diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index 9c0b4f2d..f91e0534 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -33,12 +33,12 @@ public class Configuration : IPluginConfiguration, ISavable public string ModDirectory { get; set; } = string.Empty; public string ExportDirectory { get; set; } = string.Empty; - public bool UseCrashHandler { get; set; } = true; - public bool OpenWindowAtStart { get; set; } = false; - public bool HideUiInGPose { get; set; } = false; - public bool HideUiInCutscenes { get; set; } = true; - public bool HideUiWhenUiHidden { get; set; } = false; - public bool UseDalamudUiTextureRedirection { get; set; } = true; + public bool? UseCrashHandler { get; set; } = null; + public bool OpenWindowAtStart { get; set; } = false; + public bool HideUiInGPose { get; set; } = false; + public bool HideUiInCutscenes { get; set; } = true; + public bool HideUiWhenUiHidden { get; set; } = false; + public bool UseDalamudUiTextureRedirection { get; set; } = true; public bool UseCharacterCollectionInMainWindow { get; set; } = true; public bool UseCharacterCollectionsInCards { get; set; } = true; @@ -48,9 +48,9 @@ public class Configuration : IPluginConfiguration, ISavable public bool UseNoModsInInspect { get; set; } = false; public bool HideChangedItemFilters { get; set; } = false; public bool ReplaceNonAsciiOnImport { get; set; } = false; - public bool HidePrioritiesInSelector { get; set; } = false; - public bool HideRedrawBar { get; set; } = false; - public int OptionGroupCollapsibleMin { get; set; } = 5; + public bool HidePrioritiesInSelector { get; set; } = false; + public bool HideRedrawBar { get; set; } = false; + public int OptionGroupCollapsibleMin { get; set; } = 5; public Vector2 MinimumSize = new(Constants.MinimumSizeX, Constants.MinimumSizeY); diff --git a/Penumbra/Services/CrashHandlerService.cs b/Penumbra/Services/CrashHandlerService.cs index c713d623..6025c2c0 100644 --- a/Penumbra/Services/CrashHandlerService.cs +++ b/Penumbra/Services/CrashHandlerService.cs @@ -36,7 +36,7 @@ public sealed class CrashHandlerService : IDisposable, IService _config = config; _validityChecker = validityChecker; - if (!_config.UseCrashHandler) + if (_config.UseCrashHandler ?? false) return; OpenEventWriter(); @@ -84,7 +84,7 @@ public sealed class CrashHandlerService : IDisposable, IService public void Enable() { - if (_config.UseCrashHandler) + if (_config.UseCrashHandler ?? false) return; _config.UseCrashHandler = true; @@ -97,7 +97,7 @@ public sealed class CrashHandlerService : IDisposable, IService public void Disable() { - if (!_config.UseCrashHandler) + if (!(_config.UseCrashHandler ?? false)) return; _config.UseCrashHandler = false; diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index 80fe6fb6..c524a840 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -43,6 +43,7 @@ public class SettingsTab : ITab private readonly DalamudPluginInterface _pluginInterface; private readonly IDataManager _gameData; private readonly PredefinedTagManager _predefinedTagManager; + private readonly CrashHandlerService _crashService; private int _minimumX = int.MaxValue; private int _minimumY = int.MaxValue; @@ -53,7 +54,7 @@ public class SettingsTab : ITab Penumbra penumbra, FileDialogService fileDialog, ModManager modManager, ModFileSystemSelector selector, CharacterUtility characterUtility, ResidentResourceManager residentResources, ModExportManager modExportManager, HttpApi httpApi, DalamudSubstitutionProvider dalamudSubstitutionProvider, FileCompactor compactor, DalamudConfigService dalamudConfig, - IDataManager gameData, PredefinedTagManager predefinedTagConfig) + IDataManager gameData, PredefinedTagManager predefinedTagConfig, CrashHandlerService crashService) { _pluginInterface = pluginInterface; _config = config; @@ -74,6 +75,7 @@ public class SettingsTab : ITab if (_compactor.CanCompact) _compactor.Enabled = _config.UseFileSystemCompression; _predefinedTagManager = predefinedTagConfig; + _crashService = crashService; } public void DrawHeader() @@ -228,35 +230,35 @@ public class SettingsTab : ITab _newModDirectory = _config.ModDirectory; bool save, selected; - using (ImRaii.Group()) - { - ImGui.SetNextItemWidth(UiHelpers.InputTextMinusButton3); - using (ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale, !_modManager.Valid)) - { - using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.RegexWarningBorder) - .Push(ImGuiCol.TextDisabled, Colors.RegexWarningBorder, !_modManager.Valid); - save = ImGui.InputTextWithHint("##rootDirectory", "Enter Root Directory here (MANDATORY)...", ref _newModDirectory, - RootDirectoryMaxLength, ImGuiInputTextFlags.EnterReturnsTrue); - } - - selected = ImGui.IsItemActive(); - using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, new Vector2(UiHelpers.ScaleX3, 0)); - ImGui.SameLine(); - DrawDirectoryPickerButton(); - style.Pop(); - ImGui.SameLine(); - - const string tt = "This is where Penumbra will store your extracted mod files.\n" - + "TTMP files are not copied, just extracted.\n" - + "This directory needs to be accessible and you need write access here.\n" - + "It is recommended that this directory is placed on a fast hard drive, preferably an SSD.\n" - + "It should also be placed near the root of a logical drive - the shorter the total path to this folder, the better.\n" - + "Definitely do not place it in your Dalamud directory or any sub-directory thereof."; - ImGuiComponents.HelpMarker(tt); - _tutorial.OpenTutorial(BasicTutorialSteps.GeneralTooltips); - ImGui.SameLine(); - ImGui.TextUnformatted("Root Directory"); - ImGuiUtil.HoverTooltip(tt); + using (ImRaii.Group()) + { + ImGui.SetNextItemWidth(UiHelpers.InputTextMinusButton3); + using (ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale, !_modManager.Valid)) + { + using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.RegexWarningBorder) + .Push(ImGuiCol.TextDisabled, Colors.RegexWarningBorder, !_modManager.Valid); + save = ImGui.InputTextWithHint("##rootDirectory", "Enter Root Directory here (MANDATORY)...", ref _newModDirectory, + RootDirectoryMaxLength, ImGuiInputTextFlags.EnterReturnsTrue); + } + + selected = ImGui.IsItemActive(); + using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, new Vector2(UiHelpers.ScaleX3, 0)); + ImGui.SameLine(); + DrawDirectoryPickerButton(); + style.Pop(); + ImGui.SameLine(); + + const string tt = "This is where Penumbra will store your extracted mod files.\n" + + "TTMP files are not copied, just extracted.\n" + + "This directory needs to be accessible and you need write access here.\n" + + "It is recommended that this directory is placed on a fast hard drive, preferably an SSD.\n" + + "It should also be placed near the root of a logical drive - the shorter the total path to this folder, the better.\n" + + "Definitely do not place it in your Dalamud directory or any sub-directory thereof."; + ImGuiComponents.HelpMarker(tt); + _tutorial.OpenTutorial(BasicTutorialSteps.GeneralTooltips); + ImGui.SameLine(); + ImGui.TextUnformatted("Root Directory"); + ImGuiUtil.HoverTooltip(tt); } _tutorial.OpenTutorial(BasicTutorialSteps.ModDirectory); @@ -704,6 +706,7 @@ public class SettingsTab : ITab if (!header) return; + DrawCrashHandler(); DrawMinimumDimensionConfig(); Checkbox("Auto Deduplicate on Import", "Automatically deduplicate mod files on import. This will make mod file sizes smaller, but deletes (binary identical) files.", @@ -721,6 +724,20 @@ public class SettingsTab : ITab ImGui.NewLine(); } + private void DrawCrashHandler() + { + Checkbox("Enable Penumbra Crash Logging (Experimental)", + "Enables Penumbra to launch a secondary process that records some game activity which may or may not help diagnosing Penumbra-related game crashes.", + _config.UseCrashHandler ?? false, + v => + { + if (v) + _crashService.Enable(); + else + _crashService.Disable(); + }); + } + private void DrawCompressionBox() { if (!_compactor.CanCompact) From 47c5187ad929468610f49ae04a4f534e21dc4a21 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 28 Mar 2024 19:34:30 +0100 Subject: [PATCH 079/433] Derp. --- Penumbra/Services/CrashHandlerService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Services/CrashHandlerService.cs b/Penumbra/Services/CrashHandlerService.cs index 6025c2c0..5423ec15 100644 --- a/Penumbra/Services/CrashHandlerService.cs +++ b/Penumbra/Services/CrashHandlerService.cs @@ -36,7 +36,7 @@ public sealed class CrashHandlerService : IDisposable, IService _config = config; _validityChecker = validityChecker; - if (_config.UseCrashHandler ?? false) + if (!(_config.UseCrashHandler ?? false)) return; OpenEventWriter(); From a39419288c1432f5fa9a9a7e1b7b97a8e48ea574 Mon Sep 17 00:00:00 2001 From: Actions User Date: Thu, 28 Mar 2024 18:36:31 +0000 Subject: [PATCH 080/433] [CI] Updating repo.json for testing_1.0.2.6 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 5a51afa2..936adfc0 100644 --- a/repo.json +++ b/repo.json @@ -5,7 +5,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.0.1.0", - "TestingAssemblyVersion": "1.0.2.5", + "TestingAssemblyVersion": "1.0.2.6", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -17,7 +17,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.5/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.6/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From de239578ccb83137d2c4f69ad2b7bd8781d686d5 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 29 Mar 2024 14:44:08 +0100 Subject: [PATCH 081/433] Fix weird exception. --- Penumbra/Api/IpcTester.cs | 76 +++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/Penumbra/Api/IpcTester.cs b/Penumbra/Api/IpcTester.cs index 30f3c80f..898c5de3 100644 --- a/Penumbra/Api/IpcTester.cs +++ b/Penumbra/Api/IpcTester.cs @@ -583,23 +583,18 @@ public class IpcTester : IDisposable } } - private class Resolve + private class Resolve(DalamudPluginInterface pi) { - private readonly DalamudPluginInterface _pi; - private string _currentResolvePath = string.Empty; private string _currentResolveCharacter = string.Empty; private string _currentReversePath = string.Empty; private int _currentReverseIdx; - private Task<(string[], string[][])> _task = Task.FromException<(string[], string[][])>(new Exception()); - - public Resolve(DalamudPluginInterface pi) - => _pi = pi; + private Task<(string[], string[][])> _task = Task.FromResult<(string[], string[][])>(([], [])); public void Draw() { - using var _ = ImRaii.TreeNode("Resolving"); - if (!_) + using var tree = ImRaii.TreeNode("Resolving"); + if (!tree) return; ImGui.InputTextWithHint("##resolvePath", "Resolve this game path...", ref _currentResolvePath, Utf8GamePath.MaxGamePathLength); @@ -613,28 +608,28 @@ public class IpcTester : IDisposable DrawIntro(Ipc.ResolveDefaultPath.Label, "Default Collection Resolve"); if (_currentResolvePath.Length != 0) - ImGui.TextUnformatted(Ipc.ResolveDefaultPath.Subscriber(_pi).Invoke(_currentResolvePath)); + ImGui.TextUnformatted(Ipc.ResolveDefaultPath.Subscriber(pi).Invoke(_currentResolvePath)); DrawIntro(Ipc.ResolveInterfacePath.Label, "Interface Collection Resolve"); if (_currentResolvePath.Length != 0) - ImGui.TextUnformatted(Ipc.ResolveInterfacePath.Subscriber(_pi).Invoke(_currentResolvePath)); + ImGui.TextUnformatted(Ipc.ResolveInterfacePath.Subscriber(pi).Invoke(_currentResolvePath)); DrawIntro(Ipc.ResolvePlayerPath.Label, "Player Collection Resolve"); if (_currentResolvePath.Length != 0) - ImGui.TextUnformatted(Ipc.ResolvePlayerPath.Subscriber(_pi).Invoke(_currentResolvePath)); + ImGui.TextUnformatted(Ipc.ResolvePlayerPath.Subscriber(pi).Invoke(_currentResolvePath)); DrawIntro(Ipc.ResolveCharacterPath.Label, "Character Collection Resolve"); if (_currentResolvePath.Length != 0 && _currentResolveCharacter.Length != 0) - ImGui.TextUnformatted(Ipc.ResolveCharacterPath.Subscriber(_pi).Invoke(_currentResolvePath, _currentResolveCharacter)); + ImGui.TextUnformatted(Ipc.ResolveCharacterPath.Subscriber(pi).Invoke(_currentResolvePath, _currentResolveCharacter)); DrawIntro(Ipc.ResolveGameObjectPath.Label, "Game Object Collection Resolve"); if (_currentResolvePath.Length != 0) - ImGui.TextUnformatted(Ipc.ResolveGameObjectPath.Subscriber(_pi).Invoke(_currentResolvePath, _currentReverseIdx)); + ImGui.TextUnformatted(Ipc.ResolveGameObjectPath.Subscriber(pi).Invoke(_currentResolvePath, _currentReverseIdx)); DrawIntro(Ipc.ReverseResolvePath.Label, "Reversed Game Paths"); if (_currentReversePath.Length > 0) { - var list = Ipc.ReverseResolvePath.Subscriber(_pi).Invoke(_currentReversePath, _currentResolveCharacter); + var list = Ipc.ReverseResolvePath.Subscriber(pi).Invoke(_currentReversePath, _currentResolveCharacter); if (list.Length > 0) { ImGui.TextUnformatted(list[0]); @@ -646,7 +641,7 @@ public class IpcTester : IDisposable DrawIntro(Ipc.ReverseResolvePlayerPath.Label, "Reversed Game Paths (Player)"); if (_currentReversePath.Length > 0) { - var list = Ipc.ReverseResolvePlayerPath.Subscriber(_pi).Invoke(_currentReversePath); + var list = Ipc.ReverseResolvePlayerPath.Subscriber(pi).Invoke(_currentReversePath); if (list.Length > 0) { ImGui.TextUnformatted(list[0]); @@ -658,7 +653,7 @@ public class IpcTester : IDisposable DrawIntro(Ipc.ReverseResolveGameObjectPath.Label, "Reversed Game Paths (Game Object)"); if (_currentReversePath.Length > 0) { - var list = Ipc.ReverseResolveGameObjectPath.Subscriber(_pi).Invoke(_currentReversePath, _currentReverseIdx); + var list = Ipc.ReverseResolveGameObjectPath.Subscriber(pi).Invoke(_currentReversePath, _currentReverseIdx); if (list.Length > 0) { ImGui.TextUnformatted(list[0]); @@ -668,19 +663,31 @@ public class IpcTester : IDisposable } var forwardArray = _currentResolvePath.Length > 0 - ? new[] - { - _currentResolvePath, - } + ? [_currentResolvePath] : Array.Empty(); var reverseArray = _currentReversePath.Length > 0 - ? new[] - { - _currentReversePath, - } + ? [_currentReversePath] : Array.Empty(); - string ConvertText((string[], string[][]) data) + DrawIntro(Ipc.ResolvePlayerPaths.Label, "Resolved Paths (Player)"); + if (forwardArray.Length > 0 || reverseArray.Length > 0) + { + var ret = Ipc.ResolvePlayerPaths.Subscriber(pi).Invoke(forwardArray, reverseArray); + ImGui.TextUnformatted(ConvertText(ret)); + } + + DrawIntro(Ipc.ResolvePlayerPathsAsync.Label, "Resolved Paths Async (Player)"); + if (ImGui.Button("Start")) + _task = Ipc.ResolvePlayerPathsAsync.Subscriber(pi).Invoke(forwardArray, reverseArray); + var hovered = ImGui.IsItemHovered(); + ImGui.SameLine(); + ImGui.AlignTextToFramePadding(); + ImGui.TextUnformatted(_task.Status.ToString()); + if ((hovered || ImGui.IsItemHovered()) && _task.IsCompletedSuccessfully) + ImGui.SetTooltip(ConvertText(_task.Result)); + return; + + static string ConvertText((string[], string[][]) data) { var text = string.Empty; if (data.Item1.Length > 0) @@ -697,23 +704,6 @@ public class IpcTester : IDisposable return text; } - - DrawIntro(Ipc.ResolvePlayerPaths.Label, "Resolved Paths (Player)"); - if (forwardArray.Length > 0 || reverseArray.Length > 0) - { - var ret = Ipc.ResolvePlayerPaths.Subscriber(_pi).Invoke(forwardArray, reverseArray); - ImGui.TextUnformatted(ConvertText(ret)); - } - - DrawIntro(Ipc.ResolvePlayerPathsAsync.Label, "Resolved Paths Async (Player)"); - if (ImGui.Button("Start")) - _task = Ipc.ResolvePlayerPathsAsync.Subscriber(_pi).Invoke(forwardArray, reverseArray); - var hovered = ImGui.IsItemHovered(); - ImGui.SameLine(); - ImGui.AlignTextToFramePadding(); - ImGui.TextUnformatted(_task.Status.ToString()); - if ((hovered || ImGui.IsItemHovered()) && _task.IsCompletedSuccessfully) - ImGui.SetTooltip(ConvertText(_task.Result)); } } From b4b813fe5e05ef321cda034de6691d58cc8d5b9d Mon Sep 17 00:00:00 2001 From: Exter-N Date: Fri, 29 Mar 2024 20:05:25 +0100 Subject: [PATCH 082/433] Advanced Editing minor improvements --- OtterGui | 2 +- Penumbra.GameData | 2 +- .../UI/AdvancedWindow/ModEditWindow.Materials.cs | 3 ++- Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs | 2 +- .../AdvancedWindow/ModEditWindow.ShaderPackages.cs | 12 +++++++++--- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/OtterGui b/OtterGui index 4e06921d..b70c7cc2 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 4e06921da239788331a4527aa6a2943cf0e809fe +Subproject commit b70c7cc2f6c71f80884de30a237cab201d7fe150 diff --git a/Penumbra.GameData b/Penumbra.GameData index 66687643..45679aa3 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 66687643da2163c938575ad6949c8d0fbd03afe7 +Subproject commit 45679aa32cc37b59f5eeb7cf6bf5a3ea36c626e0 diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs index fab41c7d..68b3717f 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs @@ -3,6 +3,7 @@ using Dalamud.Interface.Utility; using ImGuiNET; using OtterGui; using OtterGui.Raii; +using OtterGui.Widgets; using Penumbra.GameData.Files; using Penumbra.String.Classes; using Penumbra.UI.Classes; @@ -170,7 +171,7 @@ public partial class ModEditWindow using var t = ImRaii.TreeNode($"Additional Data (Size: {file.AdditionalData.Length})###AdditionalData"); if (t) - ImGuiUtil.TextWrapped(string.Join(' ', file.AdditionalData.Select(c => $"{c:X2}"))); + Widget.DrawHexViewer(file.AdditionalData); } private void DrawMaterialReassignmentTab() diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index 737c41d9..03f276ea 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -560,7 +560,7 @@ public partial class ModEditWindow { using var t = ImRaii.TreeNode($"Additional Data (Size: {_lastFile.RemainingData.Length})###AdditionalData"); if (t) - ImGuiUtil.TextWrapped(string.Join(' ', _lastFile.RemainingData.Select(c => $"{c:X2}"))); + Widget.DrawHexViewer(_lastFile.RemainingData); } return false; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs index 8d1c8cb7..070895b5 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs @@ -10,6 +10,7 @@ using Penumbra.GameData.Files; using Penumbra.GameData.Interop; using Penumbra.String; using static Penumbra.GameData.Files.ShpkFile; +using OtterGui.Widgets; namespace Penumbra.UI.AdvancedWindow; @@ -172,11 +173,16 @@ public partial class ModEditWindow ret |= DrawShaderPackageResourceArray("Samplers", "slot", false, shader.Samplers, true); ret |= DrawShaderPackageResourceArray("Unordered Access Views", "slot", true, shader.Uavs, true); - if (shader.AdditionalHeader.Length > 0) + if (shader.DeclaredInputs != 0) + ImRaii.TreeNode($"Declared Inputs: {shader.DeclaredInputs}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + if (shader.UsedInputs != 0) + ImRaii.TreeNode($"Used Inputs: {shader.UsedInputs}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + + if (shader.AdditionalHeader.Length > 8) { using var t2 = ImRaii.TreeNode($"Additional Header (Size: {shader.AdditionalHeader.Length})###AdditionalHeader"); if (t2) - ImGuiUtil.TextWrapped(string.Join(' ', shader.AdditionalHeader.Select(c => $"{c:X2}"))); + Widget.DrawHexViewer(shader.AdditionalHeader); } if (tab.Shpk.Disassembled) @@ -549,7 +555,7 @@ public partial class ModEditWindow { using var t = ImRaii.TreeNode($"Additional Data (Size: {tab.Shpk.AdditionalData.Length})###AdditionalData"); if (t) - ImGuiUtil.TextWrapped(string.Join(' ', tab.Shpk.AdditionalData.Select(c => $"{c:X2}"))); + Widget.DrawHexViewer(tab.Shpk.AdditionalData); } } From 5cebddb0ab680fa38edb62e3b5cc6cfe23203388 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 30 Mar 2024 14:59:31 +0100 Subject: [PATCH 083/433] Update OtterGui. --- OtterGui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OtterGui b/OtterGui index b70c7cc2..f641a34f 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit b70c7cc2f6c71f80884de30a237cab201d7fe150 +Subproject commit f641a34ffa80e89bd61701f60f15d15c4c5b361e From a65009dfb0f7714a4343c44d9f0dd0dc61ee0760 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 1 Apr 2024 13:59:09 +0200 Subject: [PATCH 084/433] Fix issue with merging and deduplicating. --- OtterGui | 2 +- Penumbra/Mods/Editor/DuplicateManager.cs | 16 ++++--- Penumbra/Mods/Editor/ModMerger.cs | 58 ++++++++++++------------ Penumbra/Mods/Manager/ModOptionEditor.cs | 31 +++++++------ Penumbra/Services/CrashHandlerService.cs | 2 +- 5 files changed, 59 insertions(+), 50 deletions(-) diff --git a/OtterGui b/OtterGui index f641a34f..f48c6886 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit f641a34ffa80e89bd61701f60f15d15c4c5b361e +Subproject commit f48c6886cbc163c5a292fa8b9fd919cb01c11d7b diff --git a/Penumbra/Mods/Editor/DuplicateManager.cs b/Penumbra/Mods/Editor/DuplicateManager.cs index 77d10cc4..c8530936 100644 --- a/Penumbra/Mods/Editor/DuplicateManager.cs +++ b/Penumbra/Mods/Editor/DuplicateManager.cs @@ -1,3 +1,4 @@ +using OtterGui.Classes; using Penumbra.Mods.Manager; using Penumbra.Mods.Subclasses; using Penumbra.Services; @@ -81,7 +82,7 @@ public class DuplicateManager(ModManager modManager, SaveService saveService, Co if (useModManager) { - modManager.OptionEditor.OptionSetFiles(mod, groupIdx, optionIdx, dict); + modManager.OptionEditor.OptionSetFiles(mod, groupIdx, optionIdx, dict, SaveType.ImmediateSync); } else { @@ -216,18 +217,21 @@ public class DuplicateManager(ModManager modManager, SaveService saveService, Co } /// Deduplicate a mod simply by its directory without any confirmation or waiting time. - internal void DeduplicateMod(DirectoryInfo modDirectory) + internal void DeduplicateMod(DirectoryInfo modDirectory, bool useModManager = false) { try { - var mod = new Mod(modDirectory); - modManager.Creator.ReloadMod(mod, true, out _); + if (!useModManager || !modManager.TryGetMod(modDirectory.Name, string.Empty, out var mod)) + { + mod = new Mod(modDirectory); + modManager.Creator.ReloadMod(mod, true, out _); + } Clear(); var files = new ModFileCollection(); files.UpdateAll(mod, mod.Default); - CheckDuplicates(files.Available.OrderByDescending(f => f.FileSize).ToArray(), CancellationToken.None); - DeleteDuplicates(files, mod, mod.Default, false); + CheckDuplicates([.. files.Available.OrderByDescending(f => f.FileSize)], CancellationToken.None); + DeleteDuplicates(files, mod, mod.Default, useModManager); } catch (Exception e) { diff --git a/Penumbra/Mods/Editor/ModMerger.cs b/Penumbra/Mods/Editor/ModMerger.cs index f5d0e4a4..842b1bb3 100644 --- a/Penumbra/Mods/Editor/ModMerger.cs +++ b/Penumbra/Mods/Editor/ModMerger.cs @@ -36,7 +36,7 @@ public class ModMerger : IDisposable public readonly HashSet SelectedOptions = []; - public readonly IReadOnlyList Warnings = new List(); + public readonly IReadOnlyList Warnings = []; public Exception? Error { get; private set; } public ModMerger(ModManager mods, ModOptionEditor editor, ModFileSystemSelector selector, DuplicateManager duplicates, @@ -78,7 +78,8 @@ public class ModMerger : IDisposable MergeWithOptions(); else MergeIntoOption(OptionGroupName, OptionName); - _duplicates.DeduplicateMod(MergeToMod.ModPath); + + _duplicates.DeduplicateMod(MergeToMod.ModPath, true); } catch (Exception ex) { @@ -134,10 +135,10 @@ public class ModMerger : IDisposable return; } - var (group, groupIdx, groupCreated) = _editor.FindOrAddModGroup(MergeToMod!, GroupType.Multi, groupName); + var (group, groupIdx, groupCreated) = _editor.FindOrAddModGroup(MergeToMod!, GroupType.Multi, groupName, SaveType.None); if (groupCreated) _createdGroups.Add(groupIdx); - var (option, optionCreated) = _editor.FindOrAddOption(MergeToMod!, groupIdx, optionName); + var (option, optionCreated) = _editor.FindOrAddOption(MergeToMod!, groupIdx, optionName, SaveType.None); if (optionCreated) _createdOptions.Add(option); var dir = ModCreator.NewOptionDirectory(MergeToMod!.ModPath, groupName, _config.ReplaceNonAsciiOnImport); @@ -156,27 +157,6 @@ public class ModMerger : IDisposable var swaps = option.FileSwapData.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); var manips = option.ManipulationData.ToHashSet(); - bool GetFullPath(FullPath input, out FullPath ret) - { - if (fromFileToFile) - { - if (!_fileToFile.TryGetValue(input.FullName, out var s)) - { - ret = input; - return false; - } - - ret = new FullPath(s); - return true; - } - - if (!Utf8RelPath.FromFile(input, MergeFromMod!.ModPath, out var relPath)) - throw new Exception($"Could not create relative path from {input} and {MergeFromMod!.ModPath}."); - - ret = new FullPath(MergeToMod!.ModPath, relPath); - return true; - } - foreach (var originalOption in mergeOptions) { foreach (var manip in originalOption.Manipulations) @@ -204,9 +184,31 @@ public class ModMerger : IDisposable } } - _editor.OptionSetFiles(MergeToMod!, option.GroupIdx, option.OptionIdx, redirections); - _editor.OptionSetFileSwaps(MergeToMod!, option.GroupIdx, option.OptionIdx, swaps); - _editor.OptionSetManipulations(MergeToMod!, option.GroupIdx, option.OptionIdx, manips); + _editor.OptionSetFiles(MergeToMod!, option.GroupIdx, option.OptionIdx, redirections, SaveType.None); + _editor.OptionSetFileSwaps(MergeToMod!, option.GroupIdx, option.OptionIdx, swaps, SaveType.None); + _editor.OptionSetManipulations(MergeToMod!, option.GroupIdx, option.OptionIdx, manips, SaveType.ImmediateSync); + return; + + bool GetFullPath(FullPath input, out FullPath ret) + { + if (fromFileToFile) + { + if (!_fileToFile.TryGetValue(input.FullName, out var s)) + { + ret = input; + return false; + } + + ret = new FullPath(s); + return true; + } + + if (!Utf8RelPath.FromFile(input, MergeFromMod!.ModPath, out var relPath)) + throw new Exception($"Could not create relative path from {input} and {MergeFromMod!.ModPath}."); + + ret = new FullPath(MergeToMod!.ModPath, relPath); + return true; + } } private void CopyFiles(DirectoryInfo directory) diff --git a/Penumbra/Mods/Manager/ModOptionEditor.cs b/Penumbra/Mods/Manager/ModOptionEditor.cs index 3459ce1a..60508d33 100644 --- a/Penumbra/Mods/Manager/ModOptionEditor.cs +++ b/Penumbra/Mods/Manager/ModOptionEditor.cs @@ -78,7 +78,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS } /// Add a new, empty option group of the given type and name. - public void AddModGroup(Mod mod, GroupType type, string newName) + public void AddModGroup(Mod mod, GroupType type, string newName, SaveType saveType = SaveType.ImmediateSync) { if (!VerifyFileName(mod, null, newName, true)) return; @@ -96,18 +96,18 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS Name = newName, Priority = maxPriority, }); - saveService.ImmediateSave(new ModSaveGroup(mod, mod.Groups.Count - 1, config.ReplaceNonAsciiOnImport)); + saveService.Save(saveType, new ModSaveGroup(mod, mod.Groups.Count - 1, config.ReplaceNonAsciiOnImport)); communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupAdded, mod, mod.Groups.Count - 1, -1, -1); } /// Add a new mod, empty option group of the given type and name if it does not exist already. - public (IModGroup, int, bool) FindOrAddModGroup(Mod mod, GroupType type, string newName) + public (IModGroup, int, bool) FindOrAddModGroup(Mod mod, GroupType type, string newName, SaveType saveType = SaveType.ImmediateSync) { var idx = mod.Groups.IndexOf(g => g.Name == newName); if (idx >= 0) return (mod.Groups[idx], idx, false); - AddModGroup(mod, type, newName); + AddModGroup(mod, type, newName, saveType); if (mod.Groups[^1].Name != newName) throw new Exception($"Could not create new mod group with name {newName}."); @@ -226,7 +226,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS } /// Add a new empty option of the given name for the given group. - public void AddOption(Mod mod, int groupIdx, string newName) + public void AddOption(Mod mod, int groupIdx, string newName, SaveType saveType = SaveType.Queue) { var group = mod.Groups[groupIdx]; var subMod = new SubMod(mod) { Name = newName }; @@ -241,19 +241,19 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS break; } - saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, group.Count - 1, -1); } /// Add a new empty option of the given name for the given group if it does not exist already. - public (SubMod, bool) FindOrAddOption(Mod mod, int groupIdx, string newName) + public (SubMod, bool) FindOrAddOption(Mod mod, int groupIdx, string newName, SaveType saveType = SaveType.Queue) { var group = mod.Groups[groupIdx]; var idx = group.IndexOf(o => o.Name == newName); if (idx >= 0) return ((SubMod)group[idx], false); - AddOption(mod, groupIdx, newName); + AddOption(mod, groupIdx, newName, saveType); if (group[^1].Name != newName) throw new Exception($"Could not create new option with name {newName} in {group.Name}."); @@ -324,7 +324,8 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS } /// Set the meta manipulations for a given option. Replaces existing manipulations. - public void OptionSetManipulations(Mod mod, int groupIdx, int optionIdx, HashSet manipulations) + public void OptionSetManipulations(Mod mod, int groupIdx, int optionIdx, HashSet manipulations, + SaveType saveType = SaveType.Queue) { var subMod = GetSubMod(mod, groupIdx, optionIdx); if (subMod.Manipulations.Count == manipulations.Count @@ -333,12 +334,13 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, optionIdx, -1); subMod.ManipulationData.SetTo(manipulations); - saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMetaChanged, mod, groupIdx, optionIdx, -1); } /// Set the file redirections for a given option. Replaces existing redirections. - public void OptionSetFiles(Mod mod, int groupIdx, int optionIdx, IReadOnlyDictionary replacements) + public void OptionSetFiles(Mod mod, int groupIdx, int optionIdx, IReadOnlyDictionary replacements, + SaveType saveType = SaveType.Queue) { var subMod = GetSubMod(mod, groupIdx, optionIdx); if (subMod.FileData.SetEquals(replacements)) @@ -346,7 +348,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, optionIdx, -1); subMod.FileData.SetTo(replacements); - saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesChanged, mod, groupIdx, optionIdx, -1); } @@ -364,7 +366,8 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS } /// Set the file swaps for a given option. Replaces existing swaps. - public void OptionSetFileSwaps(Mod mod, int groupIdx, int optionIdx, IReadOnlyDictionary swaps) + public void OptionSetFileSwaps(Mod mod, int groupIdx, int optionIdx, IReadOnlyDictionary swaps, + SaveType saveType = SaveType.Queue) { var subMod = GetSubMod(mod, groupIdx, optionIdx); if (subMod.FileSwapData.SetEquals(swaps)) @@ -372,7 +375,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, optionIdx, -1); subMod.FileSwapData.SetTo(swaps); - saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionSwapsChanged, mod, groupIdx, optionIdx, -1); } diff --git a/Penumbra/Services/CrashHandlerService.cs b/Penumbra/Services/CrashHandlerService.cs index 5423ec15..078b812b 100644 --- a/Penumbra/Services/CrashHandlerService.cs +++ b/Penumbra/Services/CrashHandlerService.cs @@ -213,7 +213,7 @@ public sealed class CrashHandlerService : IDisposable, IService } catch (Exception ex) { - Penumbra.Log.Debug($"Could not delete {dir}:\n{ex}"); + Penumbra.Log.Verbose($"Could not delete {dir}. This is generally not an error:\n{ex}"); } } } From 6e7512c13e20d0585f4d9c36aeb0c1563b62c568 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 5 Apr 2024 14:53:30 +0200 Subject: [PATCH 085/433] Add Punchline. --- Penumbra/Penumbra.json | 1 + repo.json | 1 + 2 files changed, 2 insertions(+) diff --git a/Penumbra/Penumbra.json b/Penumbra/Penumbra.json index 8173e001..85e01c84 100644 --- a/Penumbra/Penumbra.json +++ b/Penumbra/Penumbra.json @@ -1,6 +1,7 @@ { "Author": "Ottermandias, Adam, Wintermute", "Name": "Penumbra", + "Punchline": "Runtime mod loader and manager.", "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "9.0.0.1", diff --git a/repo.json b/repo.json index 936adfc0..232afaa0 100644 --- a/repo.json +++ b/repo.json @@ -2,6 +2,7 @@ { "Author": "Ottermandias, Adam, Wintermute", "Name": "Penumbra", + "Punchline": "Runtime mod loader and manager.", "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.0.1.0", From 77bf441e626a00a1d7e4b4de2c791472c2de66ec Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 5 Apr 2024 15:29:19 +0200 Subject: [PATCH 086/433] Update Open Settings and Main UI. --- Penumbra/UI/ConfigWindow.cs | 7 +++++++ Penumbra/UI/WindowSystem.cs | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Penumbra/UI/ConfigWindow.cs b/Penumbra/UI/ConfigWindow.cs index d52ebb99..9ae11fc3 100644 --- a/Penumbra/UI/ConfigWindow.cs +++ b/Penumbra/UI/ConfigWindow.cs @@ -4,6 +4,7 @@ using ImGuiNET; using OtterGui; using OtterGui.Custom; using OtterGui.Raii; +using Penumbra.Api.Enums; using Penumbra.Services; using Penumbra.UI.Classes; using Penumbra.UI.Tabs; @@ -35,6 +36,12 @@ public sealed class ConfigWindow : Window IsOpen = _config.OpenWindowAtStart; } + public void OpenSettings() + { + _configTabs.SelectTab = TabType.Settings; + IsOpen = true; + } + public void Setup(Penumbra penumbra, ConfigTabBar configTabs) { _penumbra = penumbra; diff --git a/Penumbra/UI/WindowSystem.cs b/Penumbra/UI/WindowSystem.cs index 62ad5a6e..c5418eb3 100644 --- a/Penumbra/UI/WindowSystem.cs +++ b/Penumbra/UI/WindowSystem.cs @@ -27,7 +27,8 @@ public class PenumbraWindowSystem : IDisposable _windowSystem.AddWindow(editWindow); _windowSystem.AddWindow(importPopup); _windowSystem.AddWindow(debugTab); - _uiBuilder.OpenConfigUi += Window.Toggle; + _uiBuilder.OpenMainUi += Window.Toggle; + _uiBuilder.OpenConfigUi += Window.OpenSettings; _uiBuilder.Draw += _windowSystem.Draw; _uiBuilder.Draw += _fileDialog.Draw; _uiBuilder.DisableGposeUiHide = !config.HideUiInGPose; @@ -40,7 +41,8 @@ public class PenumbraWindowSystem : IDisposable public void Dispose() { - _uiBuilder.OpenConfigUi -= Window.Toggle; + _uiBuilder.OpenMainUi -= Window.Toggle; + _uiBuilder.OpenConfigUi -= Window.OpenSettings; _uiBuilder.Draw -= _windowSystem.Draw; _uiBuilder.Draw -= _fileDialog.Draw; } From b1ca073276e140ab88cc2121ee52b0a166af02dc Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 5 Apr 2024 16:35:55 +0200 Subject: [PATCH 087/433] Turn Settings and Priority into their own types. --- Penumbra/Api/PenumbraApi.cs | 49 ++++---- Penumbra/Api/TempModManager.cs | 21 ++-- Penumbra/Collections/Cache/CollectionCache.cs | 8 +- .../Cache/CollectionCacheManager.cs | 7 +- .../Collections/Manager/CollectionEditor.cs | 66 +++------- .../Collections/Manager/CollectionStorage.cs | 2 +- .../Manager/ModCollectionMigration.cs | 4 +- Penumbra/Communication/ModSettingChanged.cs | 5 +- Penumbra/Import/TexToolsImporter.ModPack.cs | 10 +- .../PathResolving/CollectionResolver.cs | 5 +- Penumbra/Mods/Editor/IMod.cs | 4 +- Penumbra/Mods/Manager/ModOptionEditor.cs | 5 +- Penumbra/Mods/Mod.cs | 8 +- Penumbra/Mods/ModCreator.cs | 2 +- Penumbra/Mods/Subclasses/IModGroup.cs | 7 +- Penumbra/Mods/Subclasses/ModPriority.cs | 61 ++++++++++ Penumbra/Mods/Subclasses/ModSettings.cs | 115 ++++++------------ Penumbra/Mods/Subclasses/MultiModGroup.cs | 22 ++-- Penumbra/Mods/Subclasses/Setting.cs | 62 ++++++++++ Penumbra/Mods/Subclasses/SettingList.cs | 57 +++++++++ Penumbra/Mods/Subclasses/SingleModGroup.cs | 32 ++--- Penumbra/Mods/TemporaryMod.cs | 7 +- Penumbra/Services/ConfigMigrationService.cs | 17 ++- Penumbra/UI/AdvancedWindow/ItemSwapTab.cs | 2 +- Penumbra/UI/ModsTab/ModFileSystemSelector.cs | 18 +-- Penumbra/UI/ModsTab/ModPanelConflictsTab.cs | 57 ++++----- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 10 +- Penumbra/UI/ModsTab/ModPanelSettingsTab.cs | 55 +++++---- Penumbra/UI/Tabs/Debug/DebugTab.cs | 2 +- 29 files changed, 422 insertions(+), 298 deletions(-) create mode 100644 Penumbra/Mods/Subclasses/ModPriority.cs create mode 100644 Penumbra/Mods/Subclasses/Setting.cs create mode 100644 Penumbra/Mods/Subclasses/SettingList.cs diff --git a/Penumbra/Api/PenumbraApi.cs b/Penumbra/Api/PenumbraApi.cs index da1eafd0..dc1e8472 100644 --- a/Penumbra/Api/PenumbraApi.cs +++ b/Penumbra/Api/PenumbraApi.cs @@ -27,6 +27,7 @@ using Penumbra.UI; using TextureType = Penumbra.Api.Enums.TextureType; using Penumbra.Interop.ResourceTree; using Penumbra.Mods.Editor; +using Penumbra.Mods.Subclasses; namespace Penumbra.Api; @@ -39,13 +40,13 @@ public class PenumbraApi : IDisposable, IPenumbraApi { add => _communicator.PreSettingsTabBarDraw.Subscribe(value!, Communication.PreSettingsTabBarDraw.Priority.Default); remove => _communicator.PreSettingsTabBarDraw.Unsubscribe(value!); - } + } public event Action? PreSettingsPanelDraw { add => _communicator.PreSettingsPanelDraw.Subscribe(value!, Communication.PreSettingsPanelDraw.Priority.Default); remove => _communicator.PreSettingsPanelDraw.Unsubscribe(value!); - } + } public event Action? PostEnabledDraw { @@ -649,7 +650,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi var shareSettings = settings.ConvertToShareable(mod); return (PenumbraApiEc.Success, - (shareSettings.Enabled, shareSettings.Priority, shareSettings.Settings, collection.Settings[mod.Index] != null)); + (shareSettings.Enabled, shareSettings.Priority.Value, shareSettings.Settings, collection.Settings[mod.Index] != null)); } public PenumbraApiEc ReloadMod(string modDirectory, string modName) @@ -791,7 +792,9 @@ public class PenumbraApi : IDisposable, IPenumbraApi if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) return PenumbraApiEc.ModMissing; - return _collectionEditor.SetModPriority(collection, mod, priority) ? PenumbraApiEc.Success : PenumbraApiEc.NothingChanged; + return _collectionEditor.SetModPriority(collection, mod, new ModPriority(priority)) + ? PenumbraApiEc.Success + : PenumbraApiEc.NothingChanged; } public PenumbraApiEc TrySetModSetting(string collectionName, string modDirectory, string modName, string optionGroupName, @@ -820,7 +823,12 @@ public class PenumbraApi : IDisposable, IPenumbraApi Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, "OptionName", optionName)); - var setting = mod.Groups[groupIdx].Type == GroupType.Multi ? 1u << optionIdx : (uint)optionIdx; + var setting = mod.Groups[groupIdx].Type switch + { + GroupType.Multi => Setting.Multi(optionIdx), + GroupType.Single => Setting.Single(optionIdx), + _ => Setting.Zero, + }; return Return( _collectionEditor.SetModSetting(collection, mod, groupIdx, setting) ? PenumbraApiEc.Success : PenumbraApiEc.NothingChanged, @@ -850,7 +858,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi var group = mod.Groups[groupIdx]; - uint setting = 0; + var setting = Setting.Zero; if (group.Type == GroupType.Single) { var optionIdx = optionNames.Count == 0 ? -1 : group.IndexOf(o => o.Name == optionNames[^1]); @@ -859,7 +867,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, "#optionNames", optionNames.Count.ToString())); - setting = (uint)optionIdx; + setting = Setting.Single(optionIdx); } else { @@ -871,7 +879,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, "#optionNames", optionNames.Count.ToString())); - setting |= 1u << optionIdx; + setting |= Setting.Multi(optionIdx); } } @@ -993,7 +1001,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi if (!ConvertManips(manipString, out var m)) return PenumbraApiEc.InvalidManipulation; - return _tempMods.Register(tag, null, p, m, priority) switch + return _tempMods.Register(tag, null, p, m, new ModPriority(priority)) switch { RedirectResult.Success => PenumbraApiEc.Success, _ => PenumbraApiEc.UnknownError, @@ -1014,7 +1022,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi if (!ConvertManips(manipString, out var m)) return PenumbraApiEc.InvalidManipulation; - return _tempMods.Register(tag, collection, p, m, priority) switch + return _tempMods.Register(tag, collection, p, m, new ModPriority(priority)) switch { RedirectResult.Success => PenumbraApiEc.Success, _ => PenumbraApiEc.UnknownError, @@ -1024,7 +1032,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi public PenumbraApiEc RemoveTemporaryModAll(string tag, int priority) { CheckInitialized(); - return _tempMods.Unregister(tag, null, priority) switch + return _tempMods.Unregister(tag, null, new ModPriority(priority)) switch { RedirectResult.Success => PenumbraApiEc.Success, RedirectResult.NotRegistered => PenumbraApiEc.NothingChanged, @@ -1039,7 +1047,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi && !_collectionManager.Storage.ByName(collectionName, out collection)) return PenumbraApiEc.CollectionMissing; - return _tempMods.Unregister(tag, collection, priority) switch + return _tempMods.Unregister(tag, collection, new ModPriority(priority)) switch { RedirectResult.Success => PenumbraApiEc.Success, RedirectResult.NotRegistered => PenumbraApiEc.NothingChanged, @@ -1089,7 +1097,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi public IReadOnlyDictionary?[] GetGameObjectResourcePaths(ushort[] gameObjects) { - var characters = gameObjects.Select(index => _objects.GetDalamudObject((int) index)).OfType(); + var characters = gameObjects.Select(index => _objects.GetDalamudObject((int)index)).OfType(); var resourceTrees = _resourceTreeFactory.FromCharacters(characters, 0); var pathDictionaries = ResourceTreeApiHelper.GetResourcePathDictionaries(resourceTrees); @@ -1153,7 +1161,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi var collection = _tempCollections.Collections.TryGetCollection(identifier, out var c) ? c : _collectionManager.Active.Individual(identifier); - var set = collection.MetaCache?.Manipulations.ToArray() ?? Array.Empty(); + var set = collection.MetaCache?.Manipulations.ToArray() ?? []; return Functions.ToCompressedBase64(set, MetaManipulation.CurrentVersion); } @@ -1161,7 +1169,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi { CheckInitialized(); AssociatedCollection(gameObjectIdx, out var collection); - var set = collection.MetaCache?.Manipulations.ToArray() ?? Array.Empty(); + var set = collection.MetaCache?.Manipulations.ToArray() ?? []; return Functions.ToCompressedBase64(set, MetaManipulation.CurrentVersion); } @@ -1190,7 +1198,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi } [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private unsafe ActorIdentifier AssociatedIdentifier(int gameObjectIdx) + private ActorIdentifier AssociatedIdentifier(int gameObjectIdx) { if (gameObjectIdx < 0 || gameObjectIdx >= _objects.TotalCount) return ActorIdentifier.Invalid; @@ -1217,10 +1225,9 @@ public class PenumbraApi : IDisposable, IPenumbraApi CheckInitialized(); try { - if (Path.IsPathRooted(resolvedPath)) - return _lumina?.GetFileFromDisk(resolvedPath); - - return _gameData.GetFile(resolvedPath); + return Path.IsPathRooted(resolvedPath) + ? _lumina?.GetFileFromDisk(resolvedPath) + : _gameData.GetFile(resolvedPath); } catch (Exception e) { @@ -1295,7 +1302,7 @@ public class PenumbraApi : IDisposable, IPenumbraApi return _actors.CreatePlayer(b, worldId); } - private void OnModSettingChange(ModCollection collection, ModSettingChange type, Mod? mod, int _1, int _2, bool inherited) + private void OnModSettingChange(ModCollection collection, ModSettingChange type, Mod? mod, Setting _1, int _2, bool inherited) => ModSettingChanged?.Invoke(type, collection.Name, mod?.ModPath.Name ?? string.Empty, inherited); private void OnCreatedCharacterBase(nint gameObject, ModCollection collection, nint drawObject) diff --git a/Penumbra/Api/TempModManager.cs b/Penumbra/Api/TempModManager.cs index c7840b75..7d682338 100644 --- a/Penumbra/Api/TempModManager.cs +++ b/Penumbra/Api/TempModManager.cs @@ -6,6 +6,7 @@ using Penumbra.Services; using Penumbra.String.Classes; using Penumbra.Collections.Manager; using Penumbra.Communication; +using Penumbra.Mods.Subclasses; namespace Penumbra.Api; @@ -21,8 +22,8 @@ public class TempModManager : IDisposable { private readonly CommunicatorService _communicator; - private readonly Dictionary> _mods = new(); - private readonly List _modsForAllCollections = new(); + private readonly Dictionary> _mods = []; + private readonly List _modsForAllCollections = []; public TempModManager(CommunicatorService communicator) { @@ -42,7 +43,7 @@ public class TempModManager : IDisposable => _modsForAllCollections; public RedirectResult Register(string tag, ModCollection? collection, Dictionary dict, - HashSet manips, int priority) + HashSet manips, ModPriority priority) { var mod = GetOrCreateMod(tag, collection, priority, out var created); Penumbra.Log.Verbose($"{(created ? "Created" : "Changed")} temporary Mod {mod.Name}."); @@ -51,10 +52,10 @@ public class TempModManager : IDisposable return RedirectResult.Success; } - public RedirectResult Unregister(string tag, ModCollection? collection, int? priority) + public RedirectResult Unregister(string tag, ModCollection? collection, ModPriority? priority) { Penumbra.Log.Verbose($"Removing temporary mod with tag {tag}..."); - var list = collection == null ? _modsForAllCollections : _mods.TryGetValue(collection, out var l) ? l : null; + var list = collection == null ? _modsForAllCollections : _mods.GetValueOrDefault(collection); if (list == null) return RedirectResult.NotRegistered; @@ -85,13 +86,13 @@ public class TempModManager : IDisposable { Penumbra.Log.Verbose($"Removing temporary Mod {mod.Name} from {collection.AnonymizedName}."); collection.Remove(mod); - _communicator.ModSettingChanged.Invoke(collection, ModSettingChange.TemporaryMod, null, 0, 0, false); + _communicator.ModSettingChanged.Invoke(collection, ModSettingChange.TemporaryMod, null, Setting.False, 0, false); } else { Penumbra.Log.Verbose($"Adding {(created ? "new " : string.Empty)}temporary Mod {mod.Name} to {collection.AnonymizedName}."); collection.Apply(mod, created); - _communicator.ModSettingChanged.Invoke(collection, ModSettingChange.TemporaryMod, null, 1, 0, false); + _communicator.ModSettingChanged.Invoke(collection, ModSettingChange.TemporaryMod, null, Setting.True, 0, false); } } else @@ -116,7 +117,7 @@ public class TempModManager : IDisposable // Find or create a mod with the given tag as name and the given priority, for the given collection (or all collections). // Returns the found or created mod and whether it was newly created. - private TemporaryMod GetOrCreateMod(string tag, ModCollection? collection, int priority, out bool created) + private TemporaryMod GetOrCreateMod(string tag, ModCollection? collection, ModPriority priority, out bool created) { List list; if (collection == null) @@ -129,14 +130,14 @@ public class TempModManager : IDisposable } else { - list = new List(); + list = []; _mods.Add(collection, list); } var mod = list.Find(m => m.Priority == priority && m.Name == tag); if (mod == null) { - mod = new TemporaryMod() + mod = new TemporaryMod { Name = tag, Priority = priority, diff --git a/Penumbra/Collections/Cache/CollectionCache.cs b/Penumbra/Collections/Cache/CollectionCache.cs index 6b2b688b..72f0fb59 100644 --- a/Penumbra/Collections/Cache/CollectionCache.cs +++ b/Penumbra/Collections/Cache/CollectionCache.cs @@ -237,7 +237,7 @@ public sealed class CollectionCache : IDisposable if (settings is not { Enabled: true }) return; - foreach (var (group, groupIndex) in mod.Groups.WithIndex().OrderByDescending(g => g.Item1.Priority)) + foreach (var (group, groupIndex) in mod.Groups.WithIndex().OrderByDescending(g => g.Value.Priority)) { if (group.Count == 0) continue; @@ -246,13 +246,13 @@ public sealed class CollectionCache : IDisposable switch (group.Type) { case GroupType.Single: - AddSubMod(group[(int)config], mod); + AddSubMod(group[config.AsIndex], mod); break; case GroupType.Multi: { foreach (var (option, _) in group.WithIndex() - .Where(p => ((1 << p.Item2) & config) != 0) - .OrderByDescending(p => group.OptionPriority(p.Item2))) + .Where(p => config.HasFlag(p.Index)) + .OrderByDescending(p => group.OptionPriority(p.Index))) AddSubMod(option, mod); break; diff --git a/Penumbra/Collections/Cache/CollectionCacheManager.cs b/Penumbra/Collections/Cache/CollectionCacheManager.cs index 5a6b5593..f6c6e14a 100644 --- a/Penumbra/Collections/Cache/CollectionCacheManager.cs +++ b/Penumbra/Collections/Cache/CollectionCacheManager.cs @@ -8,6 +8,7 @@ using Penumbra.Interop.ResourceLoading; using Penumbra.Meta; using Penumbra.Mods; using Penumbra.Mods.Manager; +using Penumbra.Mods.Subclasses; using Penumbra.Services; using Penumbra.String.Classes; @@ -288,7 +289,7 @@ public class CollectionCacheManager : IDisposable MetaFileManager.CharacterUtility.LoadingFinished -= IncrementCounters; } - private void OnModSettingChange(ModCollection collection, ModSettingChange type, Mod? mod, int oldValue, int groupIdx, bool _) + private void OnModSettingChange(ModCollection collection, ModSettingChange type, Mod? mod, Setting oldValue, int groupIdx, bool _) { if (!collection.HasCache) return; @@ -300,9 +301,9 @@ public class CollectionCacheManager : IDisposable cache.ReloadMod(mod!, true); break; case ModSettingChange.EnableState: - if (oldValue == 0) + if (oldValue == Setting.False) cache.AddMod(mod!, true); - else if (oldValue == 1) + else if (oldValue == Setting.True) cache.RemoveMod(mod!, true); else if (collection[mod!.Index].Settings?.Enabled == true) cache.ReloadMod(mod!, true); diff --git a/Penumbra/Collections/Manager/CollectionEditor.cs b/Penumbra/Collections/Manager/CollectionEditor.cs index 73950942..4af19e6b 100644 --- a/Penumbra/Collections/Manager/CollectionEditor.cs +++ b/Penumbra/Collections/Manager/CollectionEditor.cs @@ -7,26 +7,15 @@ using Penumbra.Services; namespace Penumbra.Collections.Manager; -public class CollectionEditor +public class CollectionEditor(SaveService saveService, CommunicatorService communicator, ModStorage modStorage) { - private readonly CommunicatorService _communicator; - private readonly SaveService _saveService; - private readonly ModStorage _modStorage; - - public CollectionEditor(SaveService saveService, CommunicatorService communicator, ModStorage modStorage) - { - _saveService = saveService; - _communicator = communicator; - _modStorage = modStorage; - } - /// Enable or disable the mod inheritance of mod idx. public bool SetModInheritance(ModCollection collection, Mod mod, bool inherit) { if (!FixInheritance(collection, mod, inherit)) return false; - InvokeChange(collection, ModSettingChange.Inheritance, mod, inherit ? 0 : 1, 0); + InvokeChange(collection, ModSettingChange.Inheritance, mod, inherit ? Setting.False : Setting.True, 0); return true; } @@ -42,7 +31,8 @@ public class CollectionEditor var inheritance = FixInheritance(collection, mod, false); ((List)collection.Settings)[mod.Index]!.Enabled = newValue; - InvokeChange(collection, ModSettingChange.EnableState, mod, inheritance ? -1 : newValue ? 0 : 1, 0); + InvokeChange(collection, ModSettingChange.EnableState, mod, inheritance ? Setting.Indefinite : newValue ? Setting.False : Setting.True, + 0); return true; } @@ -52,7 +42,7 @@ public class CollectionEditor if (!mods.Aggregate(false, (current, mod) => current | FixInheritance(collection, mod, inherit))) return; - InvokeChange(collection, ModSettingChange.MultiInheritance, null, -1, 0); + InvokeChange(collection, ModSettingChange.MultiInheritance, null, Setting.Indefinite, 0); } /// @@ -76,22 +66,22 @@ public class CollectionEditor if (!changes) return; - InvokeChange(collection, ModSettingChange.MultiEnableState, null, -1, 0); + InvokeChange(collection, ModSettingChange.MultiEnableState, null, Setting.Indefinite, 0); } /// /// Set the priority of mod idx to newValue if it differs from the current priority. /// If the mod is currently inherited, stop the inheritance. /// - public bool SetModPriority(ModCollection collection, Mod mod, int newValue) + public bool SetModPriority(ModCollection collection, Mod mod, ModPriority newValue) { - var oldValue = collection.Settings[mod.Index]?.Priority ?? collection[mod.Index].Settings?.Priority ?? 0; + var oldValue = collection.Settings[mod.Index]?.Priority ?? collection[mod.Index].Settings?.Priority ?? ModPriority.Default; if (newValue == oldValue) return false; var inheritance = FixInheritance(collection, mod, false); ((List)collection.Settings)[mod.Index]!.Priority = newValue; - InvokeChange(collection, ModSettingChange.Priority, mod, inheritance ? -1 : oldValue, 0); + InvokeChange(collection, ModSettingChange.Priority, mod, inheritance ? Setting.Indefinite : oldValue.AsSetting, 0); return true; } @@ -99,7 +89,7 @@ public class CollectionEditor /// Set a given setting group settingName of mod idx to newValue if it differs from the current value and fix it if necessary. /// /// If the mod is currently inherited, stop the inheritance. /// - public bool SetModSetting(ModCollection collection, Mod mod, int groupIdx, uint newValue) + public bool SetModSetting(ModCollection collection, Mod mod, int groupIdx, Setting newValue) { var settings = collection.Settings[mod.Index] != null ? collection.Settings[mod.Index]!.Settings @@ -110,7 +100,7 @@ public class CollectionEditor var inheritance = FixInheritance(collection, mod, false); ((List)collection.Settings)[mod.Index]!.SetValue(mod, groupIdx, newValue); - InvokeChange(collection, ModSettingChange.Setting, mod, inheritance ? -1 : (int)oldValue, groupIdx); + InvokeChange(collection, ModSettingChange.Setting, mod, inheritance ? Setting.Indefinite : oldValue, groupIdx); return true; } @@ -158,35 +148,17 @@ public class CollectionEditor if (savedSettings != null) { ((Dictionary)collection.UnusedSettings)[targetName] = savedSettings.Value; - _saveService.QueueSave(new ModCollectionSave(_modStorage, collection)); + saveService.QueueSave(new ModCollectionSave(modStorage, collection)); } else if (((Dictionary)collection.UnusedSettings).Remove(targetName)) { - _saveService.QueueSave(new ModCollectionSave(_modStorage, collection)); + saveService.QueueSave(new ModCollectionSave(modStorage, collection)); } } return true; } - /// - /// Change one of the available mod settings for mod idx discerned by type. - /// If type == Setting, settingName should be a valid setting for that mod, otherwise it will be ignored. - /// The setting will also be automatically fixed if it is invalid for that setting group. - /// For boolean parameters, newValue == 0 will be treated as false and != 0 as true. - /// - public bool ChangeModSetting(ModCollection collection, ModSettingChange type, Mod mod, int newValue, int groupIdx) - { - return type switch - { - ModSettingChange.Inheritance => SetModInheritance(collection, mod, newValue != 0), - ModSettingChange.EnableState => SetModState(collection, mod, newValue != 0), - ModSettingChange.Priority => SetModPriority(collection, mod, newValue), - ModSettingChange.Setting => SetModSetting(collection, mod, groupIdx, (uint)newValue), - _ => throw new ArgumentOutOfRangeException(nameof(type), type, null), - }; - } - /// /// Set inheritance of a mod without saving, /// to be used as an intermediary. @@ -204,16 +176,16 @@ public class CollectionEditor /// Queue saves and trigger changes for any non-inherited change in a collection, then trigger changes for all inheritors. [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private void InvokeChange(ModCollection changedCollection, ModSettingChange type, Mod? mod, int oldValue, int groupIdx) + private void InvokeChange(ModCollection changedCollection, ModSettingChange type, Mod? mod, Setting oldValue, int groupIdx) { - _saveService.QueueSave(new ModCollectionSave(_modStorage, changedCollection)); - _communicator.ModSettingChanged.Invoke(changedCollection, type, mod, oldValue, groupIdx, false); + saveService.QueueSave(new ModCollectionSave(modStorage, changedCollection)); + communicator.ModSettingChanged.Invoke(changedCollection, type, mod, oldValue, groupIdx, false); RecurseInheritors(changedCollection, type, mod, oldValue, groupIdx); } /// Trigger changes in all inherited collections. [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private void RecurseInheritors(ModCollection directParent, ModSettingChange type, Mod? mod, int oldValue, int groupIdx) + private void RecurseInheritors(ModCollection directParent, ModSettingChange type, Mod? mod, Setting oldValue, int groupIdx) { foreach (var directInheritor in directParent.DirectParentOf) { @@ -221,11 +193,11 @@ public class CollectionEditor { case ModSettingChange.MultiInheritance: case ModSettingChange.MultiEnableState: - _communicator.ModSettingChanged.Invoke(directInheritor, type, null, oldValue, groupIdx, true); + communicator.ModSettingChanged.Invoke(directInheritor, type, null, oldValue, groupIdx, true); break; default: if (directInheritor.Settings[mod!.Index] == null) - _communicator.ModSettingChanged.Invoke(directInheritor, type, mod, oldValue, groupIdx, true); + communicator.ModSettingChanged.Invoke(directInheritor, type, mod, oldValue, groupIdx, true); break; } diff --git a/Penumbra/Collections/Manager/CollectionStorage.cs b/Penumbra/Collections/Manager/CollectionStorage.cs index 0ee55376..d0b61e57 100644 --- a/Penumbra/Collections/Manager/CollectionStorage.cs +++ b/Penumbra/Collections/Manager/CollectionStorage.cs @@ -268,7 +268,7 @@ public class CollectionStorage : IReadOnlyList, IDisposable case ModPathChangeType.Reloaded: foreach (var collection in this) { - if (collection.Settings[mod.Index]?.FixAllSettings(mod) ?? false) + if (collection.Settings[mod.Index]?.Settings.FixAll(mod) ?? false) _saveService.QueueSave(new ModCollectionSave(_modStorage, collection)); } diff --git a/Penumbra/Collections/Manager/ModCollectionMigration.cs b/Penumbra/Collections/Manager/ModCollectionMigration.cs index b2b8df0d..053f0a2b 100644 --- a/Penumbra/Collections/Manager/ModCollectionMigration.cs +++ b/Penumbra/Collections/Manager/ModCollectionMigration.cs @@ -40,9 +40,9 @@ internal static class ModCollectionMigration /// We treat every completely defaulted setting as inheritance-ready. private static bool SettingIsDefaultV0(ModSettings.SavedSettings setting) - => setting is { Enabled: false, Priority: 0 } && setting.Settings.Values.All(s => s == 0); + => setting is { Enabled: true, Priority.IsDefault: true } && setting.Settings.Values.All(s => s == Setting.Zero); /// private static bool SettingIsDefaultV0(ModSettings? setting) - => setting is { Enabled: false, Priority: 0 } && setting.Settings.All(s => s == 0); + => setting is { Enabled: true, Priority.IsDefault: true } && setting.Settings.All(s => s == Setting.Zero); } diff --git a/Penumbra/Communication/ModSettingChanged.cs b/Penumbra/Communication/ModSettingChanged.cs index 5e0bc0c0..412b3003 100644 --- a/Penumbra/Communication/ModSettingChanged.cs +++ b/Penumbra/Communication/ModSettingChanged.cs @@ -3,6 +3,7 @@ using Penumbra.Api; using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.Mods; +using Penumbra.Mods.Subclasses; namespace Penumbra.Communication; @@ -12,13 +13,13 @@ namespace Penumbra.Communication; /// Parameter is the collection in which the setting was changed. /// Parameter is the type of change. /// Parameter is the mod the setting was changed for, unless it was a multi-change. -/// Parameter is the old value of the setting before the change as int. +/// Parameter is the old value of the setting before the change as Setting. /// Parameter is the index of the changed group if the change type is Setting. /// Parameter is whether the change was inherited from another collection. /// /// public sealed class ModSettingChanged() - : EventWrapper(nameof(ModSettingChanged)) + : EventWrapper(nameof(ModSettingChanged)) { public enum Priority { diff --git a/Penumbra/Import/TexToolsImporter.ModPack.cs b/Penumbra/Import/TexToolsImporter.ModPack.cs index 7c4b94d8..7a247a53 100644 --- a/Penumbra/Import/TexToolsImporter.ModPack.cs +++ b/Penumbra/Import/TexToolsImporter.ModPack.cs @@ -174,7 +174,7 @@ public partial class TexToolsImporter ?? new DirectoryInfo(Path.Combine(_currentModDirectory.FullName, numGroups == 1 ? $"Group {groupPriority + 1}" : $"Group {groupPriority + 1}, Part {groupId + 1}")); - uint? defaultSettings = group.SelectionType == GroupType.Multi ? 0u : null; + Setting? defaultSettings = group.SelectionType == GroupType.Multi ? Setting.Zero : null; for (var i = 0; i + optionIdx < allOptions.Count && i < maxOptions; ++i) { var option = allOptions[i + optionIdx]; @@ -186,8 +186,8 @@ public partial class TexToolsImporter options.Add(_modManager.Creator.CreateSubMod(_currentModDirectory, optionFolder, option)); if (option.IsChecked) defaultSettings = group.SelectionType == GroupType.Multi - ? defaultSettings!.Value | (1u << i) - : (uint)i; + ? defaultSettings!.Value | Setting.Multi(i) + : Setting.Single(i); ++_currentOptionIdx; } @@ -205,12 +205,12 @@ public partial class TexToolsImporter _currentOptionName = option.Name; options.Insert(idx, ModCreator.CreateEmptySubMod(option.Name)); if (option.IsChecked) - defaultSettings = (uint) idx; + defaultSettings = Setting.Single(idx); } } _modManager.Creator.CreateOptionGroup(_currentModDirectory, group.SelectionType, name, groupPriority, groupPriority, - defaultSettings ?? 0, group.Description, options); + defaultSettings ?? Setting.Zero, group.Description, options); ++groupPriority; } } diff --git a/Penumbra/Interop/PathResolving/CollectionResolver.cs b/Penumbra/Interop/PathResolving/CollectionResolver.cs index fa122e39..aea58304 100644 --- a/Penumbra/Interop/PathResolving/CollectionResolver.cs +++ b/Penumbra/Interop/PathResolving/CollectionResolver.cs @@ -18,6 +18,7 @@ public sealed unsafe class CollectionResolver( PerformanceTracker performance, IdentifiedCollectionCache cache, IClientState clientState, + ObjectManager objects, IGameGui gameGui, ActorManager actors, CutsceneService cutscenes, @@ -35,8 +36,8 @@ public sealed unsafe class CollectionResolver( public ModCollection PlayerCollection() { using var performance1 = performance.Measure(PerformanceType.IdentifyCollection); - var gameObject = (GameObject*)(clientState.LocalPlayer?.Address ?? nint.Zero); - if (gameObject == null) + var gameObject = objects[0]; + if (!gameObject.Valid) return collectionManager.Active.ByType(CollectionType.Yourself) ?? collectionManager.Active.Default; diff --git a/Penumbra/Mods/Editor/IMod.cs b/Penumbra/Mods/Editor/IMod.cs index 78250341..d3bc19b0 100644 --- a/Penumbra/Mods/Editor/IMod.cs +++ b/Penumbra/Mods/Editor/IMod.cs @@ -7,8 +7,8 @@ public interface IMod { LowerString Name { get; } - public int Index { get; } - public int Priority { get; } + public int Index { get; } + public ModPriority Priority { get; } public ISubMod Default { get; } public IReadOnlyList Groups { get; } diff --git a/Penumbra/Mods/Manager/ModOptionEditor.cs b/Penumbra/Mods/Manager/ModOptionEditor.cs index 60508d33..ea6a62df 100644 --- a/Penumbra/Mods/Manager/ModOptionEditor.cs +++ b/Penumbra/Mods/Manager/ModOptionEditor.cs @@ -1,3 +1,4 @@ +using System.Security.AccessControl; using Dalamud.Interface.Internal.Notifications; using OtterGui; using OtterGui.Classes; @@ -33,6 +34,8 @@ public enum ModOptionChangeType public class ModOptionEditor(CommunicatorService communicator, SaveService saveService, Configuration config) { + + /// Change the type of a group given by mod and index to type, if possible. public void ChangeModGroupType(Mod mod, int groupIdx, GroupType type) { @@ -46,7 +49,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS } /// Change the settings stored as default options in a mod. - public void ChangeModGroupDefaultOption(Mod mod, int groupIdx, uint defaultOption) + public void ChangeModGroupDefaultOption(Mod mod, int groupIdx, Setting defaultOption) { var group = mod.Groups[groupIdx]; if (group.DefaultSettings == defaultOption) diff --git a/Penumbra/Mods/Mod.cs b/Penumbra/Mods/Mod.cs index c5e671af..b7d1186d 100644 --- a/Penumbra/Mods/Mod.cs +++ b/Penumbra/Mods/Mod.cs @@ -12,7 +12,7 @@ public sealed class Mod : IMod { Name = "Forced Files", Index = -1, - Priority = int.MaxValue, + Priority = ModPriority.MaxValue, }; // Main Data @@ -26,9 +26,9 @@ public sealed class Mod : IMod public bool IsTemporary => Index < 0; - /// Unused if Index < 0 but used for special temporary mods. - public int Priority - => 0; + /// Unused if Index is less than 0 but used for special temporary mods. + public ModPriority Priority + => ModPriority.Default; internal Mod(DirectoryInfo modPath) { diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index 042c98b4..c324af48 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -235,7 +235,7 @@ public partial class ModCreator(SaveService _saveService, Configuration config, /// Create a file for an option group from given data. public void CreateOptionGroup(DirectoryInfo baseFolder, GroupType type, string name, - int priority, int index, uint defaultSettings, string desc, IEnumerable subMods) + int priority, int index, Setting defaultSettings, string desc, IEnumerable subMods) { switch (type) { diff --git a/Penumbra/Mods/Subclasses/IModGroup.cs b/Penumbra/Mods/Subclasses/IModGroup.cs index ea5f176c..2f6b2403 100644 --- a/Penumbra/Mods/Subclasses/IModGroup.cs +++ b/Penumbra/Mods/Subclasses/IModGroup.cs @@ -12,7 +12,7 @@ public interface IModGroup : IEnumerable public string Description { get; } public GroupType Type { get; } public int Priority { get; } - public uint DefaultSettings { get; set; } + public Setting DefaultSettings { get; set; } public int OptionPriority(Index optionIdx); @@ -31,6 +31,9 @@ public interface IModGroup : IEnumerable public IModGroup Convert(GroupType type); public bool MoveOption(int optionIdxFrom, int optionIdxTo); public void UpdatePositions(int from = 0); + + /// Ensure that a value is valid for a group. + public Setting FixSetting(Setting setting); } public readonly struct ModSaveGroup : ISavable @@ -87,7 +90,7 @@ public readonly struct ModSaveGroup : ISavable j.WritePropertyName(nameof(Type)); j.WriteValue(_group.Type.ToString()); j.WritePropertyName(nameof(_group.DefaultSettings)); - j.WriteValue(_group.DefaultSettings); + j.WriteValue(_group.DefaultSettings.Value); j.WritePropertyName("Options"); j.WriteStartArray(); for (var idx = 0; idx < _group.Count; ++idx) diff --git a/Penumbra/Mods/Subclasses/ModPriority.cs b/Penumbra/Mods/Subclasses/ModPriority.cs new file mode 100644 index 00000000..3302c627 --- /dev/null +++ b/Penumbra/Mods/Subclasses/ModPriority.cs @@ -0,0 +1,61 @@ +using Newtonsoft.Json; + +namespace Penumbra.Mods.Subclasses; + +[JsonConverter(typeof(Converter))] +public readonly record struct ModPriority(int Value) : + IComparisonOperators, + IAdditionOperators, + IAdditionOperators, + ISubtractionOperators, + ISubtractionOperators +{ + public static readonly ModPriority Default = new(0); + public static readonly ModPriority MaxValue = new(int.MaxValue); + + public bool IsDefault + => Value == Default.Value; + + public Setting AsSetting + => new((uint)Value); + + public ModPriority Max(ModPriority other) + => this < other ? other : this; + + public override string ToString() + => Value.ToString(); + + private class Converter : JsonConverter + { + public override void WriteJson(JsonWriter writer, ModPriority value, JsonSerializer serializer) + => serializer.Serialize(writer, value.Value); + + public override ModPriority ReadJson(JsonReader reader, Type objectType, ModPriority existingValue, bool hasExistingValue, + JsonSerializer serializer) + => new(serializer.Deserialize(reader)); + } + + public static bool operator >(ModPriority left, ModPriority right) + => left.Value > right.Value; + + public static bool operator >=(ModPriority left, ModPriority right) + => left.Value >= right.Value; + + public static bool operator <(ModPriority left, ModPriority right) + => left.Value < right.Value; + + public static bool operator <=(ModPriority left, ModPriority right) + => left.Value <= right.Value; + + public static ModPriority operator +(ModPriority left, ModPriority right) + => new(left.Value + right.Value); + + public static ModPriority operator +(ModPriority left, int right) + => new(left.Value + right); + + public static ModPriority operator -(ModPriority left, ModPriority right) + => new(left.Value - right.Value); + + public static ModPriority operator -(ModPriority left, int right) + => new(left.Value - right); +} diff --git a/Penumbra/Mods/Subclasses/ModSettings.cs b/Penumbra/Mods/Subclasses/ModSettings.cs index ed8ad84e..b79b3242 100644 --- a/Penumbra/Mods/Subclasses/ModSettings.cs +++ b/Penumbra/Mods/Subclasses/ModSettings.cs @@ -11,8 +11,8 @@ namespace Penumbra.Mods.Subclasses; public class ModSettings { public static readonly ModSettings Empty = new(); - public List Settings { get; private init; } = []; - public int Priority { get; set; } + public SettingList Settings { get; private init; } = []; + public ModPriority Priority { get; set; } public bool Enabled { get; set; } // Create an independent copy of the current settings. @@ -21,7 +21,7 @@ public class ModSettings { Enabled = Enabled, Priority = Priority, - Settings = [.. Settings], + Settings = Settings.Clone(), }; // Create default settings for a given mod. @@ -29,8 +29,8 @@ public class ModSettings => new() { Enabled = false, - Priority = 0, - Settings = mod.Groups.Select(g => g.DefaultSettings).ToList(), + Priority = ModPriority.Default, + Settings = SettingList.Default(mod), }; // Return everything required to resolve things for a single mod with given settings (which can be null, in which case the default is used. @@ -39,7 +39,7 @@ public class ModSettings if (settings == null) settings = DefaultSettings(mod); else - settings.AddMissingSettings(mod); + settings.Settings.FixSize(mod); var dict = new Dictionary(); var set = new HashSet(); @@ -49,13 +49,13 @@ public class ModSettings if (group.Type is GroupType.Single) { if (group.Count > 0) - AddOption(group[(int)settings.Settings[index]]); + AddOption(group[settings.Settings[index].AsIndex]); } else { foreach (var (option, optionIdx) in group.WithIndex().OrderByDescending(o => group.OptionPriority(o.Index))) { - if (((settings.Settings[index] >> optionIdx) & 1) == 1) + if (settings.Settings[index].HasFlag(optionIdx)) AddOption(option); } } @@ -97,8 +97,8 @@ public class ModSettings var config = Settings[groupIdx]; Settings[groupIdx] = group.Type switch { - GroupType.Single => (uint)Math.Max(Math.Min(group.Count - 1, BitOperations.TrailingZeroCount(config)), 0), - GroupType.Multi => 1u << (int)config, + GroupType.Single => config.TurnMulti(group.Count), + GroupType.Multi => Setting.Multi((int)config.Value), _ => config, }; return config != Settings[groupIdx]; @@ -111,9 +111,11 @@ public class ModSettings var config = Settings[groupIdx]; Settings[groupIdx] = group.Type switch { - GroupType.Single => config >= optionIdx ? config > 1 ? config - 1 : 0 : config, - GroupType.Multi => Functions.RemoveBit(config, optionIdx), - _ => config, + GroupType.Single => config.AsIndex >= optionIdx + ? config.AsIndex > 1 ? Setting.Single(config.AsIndex - 1) : Setting.Zero + : config, + GroupType.Multi => config.RemoveBit(optionIdx), + _ => config, }; return config != Settings[groupIdx]; } @@ -128,8 +130,8 @@ public class ModSettings var config = Settings[groupIdx]; Settings[groupIdx] = group.Type switch { - GroupType.Single => config == optionIdx ? (uint)movedToIdx : config, - GroupType.Multi => Functions.MoveBit(config, optionIdx, movedToIdx), + GroupType.Single => config.AsIndex == optionIdx ? Setting.Single(movedToIdx) : config, + GroupType.Multi => config.MoveBit(optionIdx, movedToIdx), _ => config, }; return config != Settings[groupIdx]; @@ -138,96 +140,52 @@ public class ModSettings } } - public bool FixAllSettings(Mod mod) + /// Set a setting. Ensures that there are enough settings and fixes the setting beforehand. + public void SetValue(Mod mod, int groupIdx, Setting newValue) { - var ret = false; - for (var i = 0; i < Settings.Count; ++i) - { - var newValue = FixSetting(mod.Groups[i], Settings[i]); - if (newValue != Settings[i]) - { - ret = true; - Settings[i] = newValue; - } - } - - return AddMissingSettings(mod) || ret; - } - - // Ensure that a value is valid for a group. - private static uint FixSetting(IModGroup group, uint value) - => group.Type switch - { - GroupType.Single => (uint)Math.Min(value, group.Count - 1), - GroupType.Multi => (uint)(value & ((1ul << group.Count) - 1)), - _ => value, - }; - - // Set a setting. Ensures that there are enough settings and fixes the setting beforehand. - public void SetValue(Mod mod, int groupIdx, uint newValue) - { - AddMissingSettings(mod); + Settings.FixSize(mod); var group = mod.Groups[groupIdx]; - Settings[groupIdx] = FixSetting(group, newValue); - } - - // Add defaulted settings up to the required count. - private bool AddMissingSettings(Mod mod) - { - var changes = false; - for (var i = Settings.Count; i < mod.Groups.Count; ++i) - { - Settings.Add(mod.Groups[i].DefaultSettings); - changes = true; - } - - return changes; + Settings[groupIdx] = group.FixSetting(newValue); } // A simple struct conversion to easily save settings by name instead of value. public struct SavedSettings { - public Dictionary Settings; - public int Priority; - public bool Enabled; + public Dictionary Settings; + public ModPriority Priority; + public bool Enabled; public SavedSettings DeepCopy() - => new() - { - Enabled = Enabled, - Priority = Priority, - Settings = Settings.ToDictionary(kvp => kvp.Key, kvp => kvp.Value), - }; + => this with { Settings = Settings.ToDictionary(kvp => kvp.Key, kvp => kvp.Value) }; public SavedSettings(ModSettings settings, Mod mod) { Priority = settings.Priority; Enabled = settings.Enabled; - Settings = new Dictionary(mod.Groups.Count); - settings.AddMissingSettings(mod); + Settings = new Dictionary(mod.Groups.Count); + settings.Settings.FixSize(mod); foreach (var (group, setting) in mod.Groups.Zip(settings.Settings)) Settings.Add(group.Name, setting); } // Convert and fix. - public bool ToSettings(Mod mod, out ModSettings settings) + public readonly bool ToSettings(Mod mod, out ModSettings settings) { - var list = new List(mod.Groups.Count); + var list = new SettingList(mod.Groups.Count); var changes = Settings.Count != mod.Groups.Count; foreach (var group in mod.Groups) { if (Settings.TryGetValue(group.Name, out var config)) { - var castConfig = (uint)Math.Clamp(config, 0, uint.MaxValue); - var actualConfig = FixSetting(group, castConfig); + var actualConfig = group.FixSetting(config); list.Add(actualConfig); if (actualConfig != config) changes = true; } else { - list.Add(0); + list.Add(group.DefaultSettings); changes = true; } } @@ -245,7 +203,7 @@ public class ModSettings // Return the settings for a given mod in a shareable format, using the names of groups and options instead of indices. // Does not repair settings but ignores settings not fitting to the given mod. - public (bool Enabled, int Priority, Dictionary> Settings) ConvertToShareable(Mod mod) + public (bool Enabled, ModPriority Priority, Dictionary> Settings) ConvertToShareable(Mod mod) { var dict = new Dictionary>(Settings.Count); foreach (var (setting, idx) in Settings.WithIndex()) @@ -254,16 +212,13 @@ public class ModSettings break; var group = mod.Groups[idx]; - if (group.Type == GroupType.Single && setting < group.Count) + if (group.Type == GroupType.Single && setting.Value < (ulong)group.Count) { - dict.Add(group.Name, new[] - { - group[(int)setting].Name, - }); + dict.Add(group.Name, [group[(int)setting.Value].Name]); } else { - var list = group.Where((_, optionIdx) => (setting & (1 << optionIdx)) != 0).Select(o => o.Name).ToList(); + var list = group.Where((_, optionIdx) => (setting.Value & (1ul << optionIdx)) != 0).Select(o => o.Name).ToList(); dict.Add(group.Name, list); } } diff --git a/Penumbra/Mods/Subclasses/MultiModGroup.cs b/Penumbra/Mods/Subclasses/MultiModGroup.cs index 07f84722..8a8e10bd 100644 --- a/Penumbra/Mods/Subclasses/MultiModGroup.cs +++ b/Penumbra/Mods/Subclasses/MultiModGroup.cs @@ -14,10 +14,10 @@ public sealed class MultiModGroup : IModGroup public GroupType Type => GroupType.Multi; - public string Name { get; set; } = "Group"; - public string Description { get; set; } = "A non-exclusive group of settings."; - public int Priority { get; set; } - public uint DefaultSettings { get; set; } + public string Name { get; set; } = "Group"; + public string Description { get; set; } = "A non-exclusive group of settings."; + public int Priority { get; set; } + public Setting DefaultSettings { get; set; } public int OptionPriority(Index idx) => PrioritizedOptions[idx].Priority; @@ -44,7 +44,7 @@ public sealed class MultiModGroup : IModGroup Name = json[nameof(Name)]?.ToObject() ?? string.Empty, Description = json[nameof(Description)]?.ToObject() ?? string.Empty, Priority = json[nameof(Priority)]?.ToObject() ?? 0, - DefaultSettings = json[nameof(DefaultSettings)]?.ToObject() ?? 0, + DefaultSettings = json[nameof(DefaultSettings)]?.ToObject() ?? Setting.Zero, }; if (ret.Name.Length == 0) return null; @@ -56,7 +56,8 @@ public sealed class MultiModGroup : IModGroup if (ret.PrioritizedOptions.Count == IModGroup.MaxMultiOptions) { Penumbra.Messager.NotificationMessage( - $"Multi Group {ret.Name} in {mod.Name} has more than {IModGroup.MaxMultiOptions} options, ignoring excessive options.", NotificationType.Warning); + $"Multi Group {ret.Name} in {mod.Name} has more than {IModGroup.MaxMultiOptions} options, ignoring excessive options.", + NotificationType.Warning); break; } @@ -66,7 +67,7 @@ public sealed class MultiModGroup : IModGroup ret.PrioritizedOptions.Add((subMod, priority)); } - ret.DefaultSettings = (uint)(ret.DefaultSettings & ((1ul << ret.Count) - 1)); + ret.DefaultSettings = ret.FixSetting(ret.DefaultSettings); return ret; } @@ -82,7 +83,7 @@ public sealed class MultiModGroup : IModGroup Name = Name, Description = Description, Priority = Priority, - DefaultSettings = (uint)Math.Max(Math.Min(Count - 1, BitOperations.TrailingZeroCount(DefaultSettings)), 0), + DefaultSettings = DefaultSettings.TurnMulti(Count), }; multi.OptionData.AddRange(PrioritizedOptions.Select(p => p.Mod)); return multi; @@ -95,7 +96,7 @@ public sealed class MultiModGroup : IModGroup if (!PrioritizedOptions.Move(optionIdxFrom, optionIdxTo)) return false; - DefaultSettings = Functions.MoveBit(DefaultSettings, optionIdxFrom, optionIdxTo); + DefaultSettings = DefaultSettings.MoveBit(optionIdxFrom, optionIdxTo); UpdatePositions(Math.Min(optionIdxFrom, optionIdxTo)); return true; } @@ -105,4 +106,7 @@ public sealed class MultiModGroup : IModGroup foreach (var ((o, _), i) in PrioritizedOptions.WithIndex().Skip(from)) o.SetPosition(o.GroupIdx, i); } + + public Setting FixSetting(Setting setting) + => new(setting.Value & ((1ul << Count) - 1)); } diff --git a/Penumbra/Mods/Subclasses/Setting.cs b/Penumbra/Mods/Subclasses/Setting.cs new file mode 100644 index 00000000..18b1e4ca --- /dev/null +++ b/Penumbra/Mods/Subclasses/Setting.cs @@ -0,0 +1,62 @@ +using Newtonsoft.Json; +using OtterGui; + +namespace Penumbra.Mods.Subclasses; + +[JsonConverter(typeof(Converter))] +public readonly record struct Setting(ulong Value) +{ + public static readonly Setting Zero = new(0); + public static readonly Setting True = new(1); + public static readonly Setting False = new(0); + public static readonly Setting Indefinite = new(ulong.MaxValue); + + public static Setting Multi(int idx) + => new(1ul << idx); + + public static Setting Single(int idx) + => new(Math.Max(0ul, (ulong)idx)); + + public static Setting operator |(Setting lhs, Setting rhs) + => new(lhs.Value | rhs.Value); + + public int AsIndex + => (int)Math.Clamp(Value, 0ul, int.MaxValue); + + public bool HasFlag(int idx) + => idx >= 0 && (Value & (1ul << idx)) != 0; + + public Setting MoveBit(int idx1, int idx2) + => new(Functions.MoveBit(Value, idx1, idx2)); + + public Setting RemoveBit(int idx) + => new(Functions.RemoveBit(Value, idx)); + + public Setting SetBit(int idx, bool value) + => new(value ? Value | (1ul << idx) : Value & ~(1ul << idx)); + + public static Setting AllBits(int count) + => new((1ul << Math.Clamp(count, 0, 63)) - 1); + + public Setting TurnMulti(int count) + => new(Math.Max((ulong)Math.Min(count - 1, BitOperations.TrailingZeroCount(Value)), 0)); + + public ModPriority AsPriority + => new((int)(Value & 0xFFFFFFFF)); + + public static Setting FromBool(bool value) + => value ? True : False; + + public bool AsBool + => Value != 0; + + private class Converter : JsonConverter + { + public override void WriteJson(JsonWriter writer, Setting value, JsonSerializer serializer) + => serializer.Serialize(writer, value.Value); + + public override Setting ReadJson(JsonReader reader, Type objectType, Setting existingValue, bool hasExistingValue, + JsonSerializer serializer) + => new(serializer.Deserialize(reader)); + } +} diff --git a/Penumbra/Mods/Subclasses/SettingList.cs b/Penumbra/Mods/Subclasses/SettingList.cs new file mode 100644 index 00000000..ea1e447f --- /dev/null +++ b/Penumbra/Mods/Subclasses/SettingList.cs @@ -0,0 +1,57 @@ +namespace Penumbra.Mods.Subclasses; + +public class SettingList : List +{ + public SettingList() + { } + + public SettingList(int capacity) + : base(capacity) + { } + + public SettingList(IEnumerable settings) + => AddRange(settings); + + public SettingList Clone() + => new(this); + + public static SettingList Default(Mod mod) + => new(mod.Groups.Select(g => g.DefaultSettings)); + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public bool FixSize(Mod mod) + { + var diff = Count - mod.Groups.Count; + + switch (diff) + { + case 0: return false; + case > 0: + RemoveRange(mod.Groups.Count, diff); + return true; + default: + EnsureCapacity(mod.Groups.Count); + for (var i = Count; i < mod.Groups.Count; ++i) + Add(mod.Groups[i].DefaultSettings); + return true; + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public bool FixAll(Mod mod) + { + var ret = false; + for (var i = 0; i < Count; ++i) + { + var oldValue = this[i]; + var newValue = mod.Groups[i].FixSetting(oldValue); + if (newValue == oldValue) + continue; + + ret = true; + this[i] = newValue; + } + + return FixSize(mod) | ret; + } +} diff --git a/Penumbra/Mods/Subclasses/SingleModGroup.cs b/Penumbra/Mods/Subclasses/SingleModGroup.cs index 2b7ebd09..be1dbde5 100644 --- a/Penumbra/Mods/Subclasses/SingleModGroup.cs +++ b/Penumbra/Mods/Subclasses/SingleModGroup.cs @@ -12,10 +12,10 @@ public sealed class SingleModGroup : IModGroup public GroupType Type => GroupType.Single; - public string Name { get; set; } = "Option"; - public string Description { get; set; } = "A mutually exclusive group of settings."; - public int Priority { get; set; } - public uint DefaultSettings { get; set; } + public string Name { get; set; } = "Option"; + public string Description { get; set; } = "A mutually exclusive group of settings."; + public int Priority { get; set; } + public Setting DefaultSettings { get; set; } public readonly List OptionData = []; @@ -43,7 +43,7 @@ public sealed class SingleModGroup : IModGroup Name = json[nameof(Name)]?.ToObject() ?? string.Empty, Description = json[nameof(Description)]?.ToObject() ?? string.Empty, Priority = json[nameof(Priority)]?.ToObject() ?? 0, - DefaultSettings = json[nameof(DefaultSettings)]?.ToObject() ?? 0u, + DefaultSettings = json[nameof(DefaultSettings)]?.ToObject() ?? Setting.Zero, }; if (ret.Name.Length == 0) return null; @@ -57,9 +57,7 @@ public sealed class SingleModGroup : IModGroup ret.OptionData.Add(subMod); } - if ((int)ret.DefaultSettings >= ret.Count) - ret.DefaultSettings = 0; - + ret.DefaultSettings = ret.FixSetting(ret.DefaultSettings); return ret; } @@ -74,7 +72,7 @@ public sealed class SingleModGroup : IModGroup Name = Name, Description = Description, Priority = Priority, - DefaultSettings = 1u << (int)DefaultSettings, + DefaultSettings = Setting.Multi((int) DefaultSettings.Value), }; multi.PrioritizedOptions.AddRange(OptionData.Select((o, i) => (o, i))); return multi; @@ -87,19 +85,20 @@ public sealed class SingleModGroup : IModGroup if (!OptionData.Move(optionIdxFrom, optionIdxTo)) return false; + var currentIndex = DefaultSettings.AsIndex; // Update default settings with the move. - if (DefaultSettings == optionIdxFrom) + if (currentIndex == optionIdxFrom) { - DefaultSettings = (uint)optionIdxTo; + DefaultSettings = Setting.Single(optionIdxTo); } else if (optionIdxFrom < optionIdxTo) { - if (DefaultSettings > optionIdxFrom && DefaultSettings <= optionIdxTo) - --DefaultSettings; + if (currentIndex > optionIdxFrom && currentIndex <= optionIdxTo) + DefaultSettings = Setting.Single(currentIndex - 1); } - else if (DefaultSettings < optionIdxFrom && DefaultSettings >= optionIdxTo) + else if (currentIndex < optionIdxFrom && currentIndex >= optionIdxTo) { - ++DefaultSettings; + DefaultSettings = Setting.Single(currentIndex + 1); } UpdatePositions(Math.Min(optionIdxFrom, optionIdxTo)); @@ -111,4 +110,7 @@ public sealed class SingleModGroup : IModGroup foreach (var (o, i) in OptionData.WithIndex().Skip(from)) o.SetPosition(o.GroupIdx, i); } + + public Setting FixSetting(Setting setting) + => Count == 0 ? Setting.Zero : new Setting(Math.Min(setting.Value, (ulong)(Count - 1))); } diff --git a/Penumbra/Mods/TemporaryMod.cs b/Penumbra/Mods/TemporaryMod.cs index c80334aa..4de2ac13 100644 --- a/Penumbra/Mods/TemporaryMod.cs +++ b/Penumbra/Mods/TemporaryMod.cs @@ -13,7 +13,7 @@ public class TemporaryMod : IMod { public LowerString Name { get; init; } = LowerString.Empty; public int Index { get; init; } = -2; - public int Priority { get; init; } = int.MaxValue; + public ModPriority Priority { get; init; } = ModPriority.MaxValue; public int TotalManipulations => Default.Manipulations.Count; @@ -27,10 +27,7 @@ public class TemporaryMod : IMod => Array.Empty(); public IEnumerable AllSubMods - => new[] - { - Default, - }; + => [Default]; public TemporaryMod() => Default = new SubMod(this); diff --git a/Penumbra/Services/ConfigMigrationService.cs b/Penumbra/Services/ConfigMigrationService.cs index b84c0996..d1e952f1 100644 --- a/Penumbra/Services/ConfigMigrationService.cs +++ b/Penumbra/Services/ConfigMigrationService.cs @@ -341,15 +341,15 @@ public class ConfigMigrationService(SaveService saveService) : IService var text = File.ReadAllText(collectionJson.FullName); var data = JArray.Parse(text); - var maxPriority = 0; + var maxPriority = ModPriority.Default; var dict = new Dictionary(); foreach (var setting in data.Cast()) { - var modName = (string)setting["FolderName"]!; - var enabled = (bool)setting["Enabled"]!; - var priority = (int)setting["Priority"]!; - var settings = setting["Settings"]!.ToObject>() - ?? setting["Conf"]!.ToObject>(); + var modName = setting["FolderName"]?.ToObject()!; + var enabled = setting["Enabled"]?.ToObject() ?? false; + var priority = setting["Priority"]?.ToObject() ?? ModPriority.Default; + var settings = setting["Settings"]!.ToObject>() + ?? setting["Conf"]!.ToObject>(); dict[modName] = new ModSettings.SavedSettings() { @@ -357,7 +357,7 @@ public class ConfigMigrationService(SaveService saveService) : IService Priority = priority, Settings = settings!, }; - maxPriority = Math.Max(maxPriority, priority); + maxPriority = maxPriority.Max(priority); } InvertModListOrder = _data[nameof(InvertModListOrder)]?.ToObject() ?? InvertModListOrder; @@ -365,8 +365,7 @@ public class ConfigMigrationService(SaveService saveService) : IService dict = dict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value with { Priority = maxPriority - kvp.Value.Priority }); var emptyStorage = new ModStorage(); - var collection = ModCollection.CreateFromData(saveService, emptyStorage, ModCollection.DefaultCollectionName, 0, 1, dict, - Array.Empty()); + var collection = ModCollection.CreateFromData(saveService, emptyStorage, ModCollection.DefaultCollectionName, 0, 1, dict, []); saveService.ImmediateSaveSync(new ModCollectionSave(emptyStorage, collection)); } catch (Exception e) diff --git a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs index 0205f3c6..7b5ce2dc 100644 --- a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs +++ b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs @@ -694,7 +694,7 @@ public class ItemSwapTab : IDisposable, ITab UpdateMod(_mod, _mod.Index < newCollection.Settings.Count ? newCollection[_mod.Index].Settings : null); } - private void OnSettingChange(ModCollection collection, ModSettingChange type, Mod? mod, int oldValue, int groupIdx, bool inherited) + private void OnSettingChange(ModCollection collection, ModSettingChange type, Mod? mod, Setting oldValue, int groupIdx, bool inherited) { if (collection != _collectionManager.Active.Current || mod != _mod) return; diff --git a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs index 15b18692..cd0eb982 100644 --- a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs +++ b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs @@ -74,7 +74,7 @@ public sealed class ModFileSystemSelector : FileSystemSelector 0) { var mod = _modManager.FirstOrDefault(m @@ -92,7 +92,7 @@ public sealed class ModFileSystemSelector : FileSystemSelector Label => "Conflicts"u8; public bool IsVisible - => _collectionManager.Active.Current.Conflicts(_selector.Selected!).Count > 0; + => collectionManager.Active.Current.Conflicts(selector.Selected!).Count > 0; - private readonly ConditionalWeakTable _expandedMods = new(); + private readonly ConditionalWeakTable _expandedMods = []; - private int GetPriority(ModConflicts conflicts) + private ModPriority GetPriority(ModConflicts conflicts) { if (conflicts.Mod2.Index < 0) return conflicts.Mod2.Priority; - return _collectionManager.Active.Current[conflicts.Mod2.Index].Settings?.Priority ?? 0; + return collectionManager.Active.Current[conflicts.Mod2.Index].Settings?.Priority ?? ModPriority.Default; } public void DrawContent() @@ -63,8 +55,8 @@ public class ModPanelConflictsTab : ITab DrawCurrentRow(priorityWidth); // Can not be null because otherwise the tab bar is never drawn. - var mod = _selector.Selected!; - foreach (var (conflict, index) in _collectionManager.Active.Current.Conflicts(mod).OrderByDescending(GetPriority) + var mod = selector.Selected!; + foreach (var (conflict, index) in collectionManager.Active.Current.Conflicts(mod).OrderByDescending(GetPriority) .ThenBy(c => c.Mod2.Name.Lower).WithIndex()) { using var id = ImRaii.PushId(index); @@ -77,18 +69,18 @@ public class ModPanelConflictsTab : ITab ImGui.TableNextColumn(); using var c = ImRaii.PushColor(ImGuiCol.Text, ColorId.FolderLine.Value()); ImGui.AlignTextToFramePadding(); - ImGui.TextUnformatted(_selector.Selected!.Name); + ImGui.TextUnformatted(selector.Selected!.Name); ImGui.TableNextColumn(); - var priority = _collectionManager.Active.Current[_selector.Selected!.Index].Settings!.Priority; + var priority = collectionManager.Active.Current[selector.Selected!.Index].Settings!.Priority.Value; ImGui.SetNextItemWidth(priorityWidth); if (ImGui.InputInt("##priority", ref priority, 0, 0, ImGuiInputTextFlags.EnterReturnsTrue)) _currentPriority = priority; if (ImGui.IsItemDeactivatedAfterEdit() && _currentPriority.HasValue) { - if (_currentPriority != _collectionManager.Active.Current[_selector.Selected!.Index].Settings!.Priority) - _collectionManager.Editor.SetModPriority(_collectionManager.Active.Current, (Mod)_selector.Selected!, - _currentPriority.Value); + if (_currentPriority != collectionManager.Active.Current[selector.Selected!.Index].Settings!.Priority.Value) + collectionManager.Editor.SetModPriority(collectionManager.Active.Current, selector.Selected!, + new ModPriority(_currentPriority.Value)); _currentPriority = null; } @@ -104,7 +96,7 @@ public class ModPanelConflictsTab : ITab { ImGui.AlignTextToFramePadding(); if (ImGui.Selectable(conflict.Mod2.Name) && conflict.Mod2 is Mod otherMod) - _selector.SelectByValue(otherMod); + selector.SelectByValue(otherMod); var hovered = ImGui.IsItemHovered(); var rightClicked = ImGui.IsItemClicked(ImGuiMouseButton.Right); if (conflict.Mod2 is Mod otherMod2) @@ -112,7 +104,7 @@ public class ModPanelConflictsTab : ITab if (hovered) ImGui.SetTooltip("Click to jump to mod, Control + Right-Click to disable mod."); if (rightClicked && ImGui.GetIO().KeyCtrl) - _collectionManager.Editor.SetModState(_collectionManager.Active.Current, otherMod2, false); + collectionManager.Editor.SetModState(collectionManager.Active.Current, otherMod2, false); } } @@ -146,7 +138,7 @@ public class ModPanelConflictsTab : ITab ImGui.TableNextColumn(); var conflictPriority = DrawPriorityInput(conflict, priorityWidth); ImGui.SameLine(); - var selectedPriority = _collectionManager.Active.Current[_selector.Selected!.Index].Settings!.Priority; + var selectedPriority = collectionManager.Active.Current[selector.Selected!.Index].Settings!.Priority.Value; DrawPriorityButtons(conflict.Mod2 as Mod, conflictPriority, selectedPriority, buttonSize); ImGui.TableNextColumn(); DrawExpandButton(conflict.Mod2, expanded, buttonSize); @@ -171,7 +163,7 @@ public class ModPanelConflictsTab : ITab using var color = ImRaii.PushColor(ImGuiCol.Text, conflict.HasPriority ? ColorId.HandledConflictMod.Value() : ColorId.ConflictingMod.Value()); using var disabled = ImRaii.Disabled(conflict.Mod2.Index < 0); - var priority = _currentPriority ?? GetPriority(conflict); + var priority = _currentPriority ?? GetPriority(conflict).Value; ImGui.SetNextItemWidth(priorityWidth); if (ImGui.InputInt("##priority", ref priority, 0, 0, ImGuiInputTextFlags.EnterReturnsTrue)) @@ -179,8 +171,9 @@ public class ModPanelConflictsTab : ITab if (ImGui.IsItemDeactivatedAfterEdit() && _currentPriority.HasValue) { - if (_currentPriority != GetPriority(conflict)) - _collectionManager.Editor.SetModPriority(_collectionManager.Active.Current, (Mod)conflict.Mod2, _currentPriority.Value); + if (_currentPriority != GetPriority(conflict).Value) + collectionManager.Editor.SetModPriority(collectionManager.Active.Current, (Mod)conflict.Mod2, + new ModPriority(_currentPriority.Value)); _currentPriority = null; } @@ -195,12 +188,14 @@ public class ModPanelConflictsTab : ITab private void DrawPriorityButtons(Mod? conflict, int conflictPriority, int selectedPriority, Vector2 buttonSize) { if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.SortNumericUpAlt.ToIconString(), buttonSize, - $"Set the priority of the currently selected mod to this mods priority plus one. ({selectedPriority} -> {conflictPriority + 1})", selectedPriority > conflictPriority, true)) - _collectionManager.Editor.SetModPriority(_collectionManager.Active.Current, _selector.Selected!, conflictPriority + 1); + $"Set the priority of the currently selected mod to this mods priority plus one. ({selectedPriority} -> {conflictPriority + 1})", + selectedPriority > conflictPriority, true)) + collectionManager.Editor.SetModPriority(collectionManager.Active.Current, selector.Selected!, + new ModPriority(conflictPriority + 1)); ImGui.SameLine(); if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.SortNumericDownAlt.ToIconString(), buttonSize, $"Set the priority of this mod to the currently selected mods priority minus one. ({conflictPriority} -> {selectedPriority - 1})", selectedPriority > conflictPriority || conflict == null, true)) - _collectionManager.Editor.SetModPriority(_collectionManager.Active.Current, conflict!, selectedPriority - 1); + collectionManager.Editor.SetModPriority(collectionManager.Active.Current, conflict!, new ModPriority(selectedPriority - 1)); } } diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index eb79869e..1292367a 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -502,18 +502,16 @@ public class ModPanelEditTab( if (group.Type == GroupType.Single) { - if (ImGui.RadioButton("##default", group.DefaultSettings == optionIdx)) - panel._modManager.OptionEditor.ChangeModGroupDefaultOption(panel._mod, groupIdx, (uint)optionIdx); + if (ImGui.RadioButton("##default", group.DefaultSettings.AsIndex == optionIdx)) + panel._modManager.OptionEditor.ChangeModGroupDefaultOption(panel._mod, groupIdx, Setting.Single(optionIdx)); ImGuiUtil.HoverTooltip($"Set {option.Name} as the default choice for this group."); } else { - var isDefaultOption = ((group.DefaultSettings >> optionIdx) & 1) != 0; + var isDefaultOption = group.DefaultSettings.HasFlag(optionIdx); if (ImGui.Checkbox("##default", ref isDefaultOption)) - panel._modManager.OptionEditor.ChangeModGroupDefaultOption(panel._mod, groupIdx, isDefaultOption - ? group.DefaultSettings | (1u << optionIdx) - : group.DefaultSettings & ~(1u << optionIdx)); + panel._modManager.OptionEditor.ChangeModGroupDefaultOption(panel._mod, groupIdx, group.DefaultSettings.SetBit(optionIdx, isDefaultOption)); ImGuiUtil.HoverTooltip($"{(isDefaultOption ? "Disable" : "Enable")} {option.Name} per default in this group."); } diff --git a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs index b14cad01..1107aa20 100644 --- a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs @@ -26,7 +26,7 @@ public class ModPanelSettingsTab : ITab private ModSettings _settings = null!; private ModCollection _collection = null!; private bool _empty; - private int? _currentPriority = null; + private int? _currentPriority; public ModPanelSettingsTab(CollectionManager collectionManager, ModManager modManager, ModFileSystemSelector selector, TutorialService tutorial, CommunicatorService communicator, Configuration config) @@ -136,15 +136,15 @@ public class ModPanelSettingsTab : ITab private void DrawPriorityInput() { using var group = ImRaii.Group(); - var priority = _currentPriority ?? _settings.Priority; + var priority = _currentPriority ?? _settings.Priority.Value; ImGui.SetNextItemWidth(50 * UiHelpers.Scale); if (ImGui.InputInt("##Priority", ref priority, 0, 0)) _currentPriority = priority; if (ImGui.IsItemDeactivatedAfterEdit() && _currentPriority.HasValue) { - if (_currentPriority != _settings.Priority) - _collectionManager.Editor.SetModPriority(_collectionManager.Active.Current, _selector.Selected!, _currentPriority.Value); + if (_currentPriority != _settings.Priority.Value) + _collectionManager.Editor.SetModPriority(_collectionManager.Active.Current, _selector.Selected!, new ModPriority(_currentPriority.Value)); _currentPriority = null; } @@ -179,7 +179,7 @@ public class ModPanelSettingsTab : ITab private void DrawSingleGroupCombo(IModGroup group, int groupIdx) { using var id = ImRaii.PushId(groupIdx); - var selectedOption = _empty ? (int)group.DefaultSettings : (int)_settings.Settings[groupIdx]; + var selectedOption = _empty ? group.DefaultSettings.AsIndex : _settings.Settings[groupIdx].AsIndex; ImGui.SetNextItemWidth(UiHelpers.InputTextWidth.X * 3 / 4); using (var combo = ImRaii.Combo(string.Empty, group[selectedOption].Name)) { @@ -189,7 +189,8 @@ public class ModPanelSettingsTab : ITab id.Push(idx2); var option = group[idx2]; if (ImGui.Selectable(option.Name, idx2 == selectedOption)) - _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, (uint)idx2); + _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, + Setting.Single(idx2)); if (option.Description.Length > 0) ImGuiUtil.SelectableHelpMarker(option.Description); @@ -210,8 +211,8 @@ public class ModPanelSettingsTab : ITab private void DrawSingleGroupRadio(IModGroup group, int groupIdx) { using var id = ImRaii.PushId(groupIdx); - var selectedOption = _empty ? (int)group.DefaultSettings : (int)_settings.Settings[groupIdx]; - var minWidth = Widget.BeginFramedGroup(group.Name, description:group.Description); + var selectedOption = _empty ? group.DefaultSettings.AsIndex : _settings.Settings[groupIdx].AsIndex; + var minWidth = Widget.BeginFramedGroup(group.Name, group.Description); DrawCollapseHandling(group, minWidth, DrawOptions); @@ -225,7 +226,8 @@ public class ModPanelSettingsTab : ITab using var i = ImRaii.PushId(idx); var option = group[idx]; if (ImGui.RadioButton(option.Name, selectedOption == idx)) - _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, (uint)idx); + _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, + Setting.Single(idx)); if (option.Description.Length <= 0) continue; @@ -291,7 +293,17 @@ public class ModPanelSettingsTab : ITab { using var id = ImRaii.PushId(groupIdx); var flags = _empty ? group.DefaultSettings : _settings.Settings[groupIdx]; - var minWidth = Widget.BeginFramedGroup(group.Name, description: group.Description); + var minWidth = Widget.BeginFramedGroup(group.Name, group.Description); + + DrawCollapseHandling(group, minWidth, DrawOptions); + + Widget.EndFramedGroup(); + var label = $"##multi{groupIdx}"; + if (ImGui.IsItemClicked(ImGuiMouseButton.Right)) + ImGui.OpenPopup($"##multi{groupIdx}"); + + DrawMultiPopup(group, groupIdx, label); + return; void DrawOptions() { @@ -299,12 +311,11 @@ public class ModPanelSettingsTab : ITab { using var i = ImRaii.PushId(idx); var option = group[idx]; - var flag = 1u << idx; - var setting = (flags & flag) != 0; + var setting = flags.HasFlag(idx); if (ImGui.Checkbox(option.Name, ref setting)) { - flags = setting ? flags | flag : flags & ~flag; + flags = flags.SetBit(idx, setting); _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, flags); } @@ -315,14 +326,10 @@ public class ModPanelSettingsTab : ITab } } } + } - DrawCollapseHandling(group, minWidth, DrawOptions); - - Widget.EndFramedGroup(); - var label = $"##multi{groupIdx}"; - if (ImGui.IsItemClicked(ImGuiMouseButton.Right)) - ImGui.OpenPopup($"##multi{groupIdx}"); - + private void DrawMultiPopup(IModGroup group, int groupIdx, string label) + { using var style = ImRaii.PushStyle(ImGuiStyleVar.PopupBorderSize, 1); using var popup = ImRaii.Popup(label); if (!popup) @@ -331,12 +338,10 @@ public class ModPanelSettingsTab : ITab ImGui.TextUnformatted(group.Name); ImGui.Separator(); if (ImGui.Selectable("Enable All")) - { - flags = group.Count == 32 ? uint.MaxValue : (1u << group.Count) - 1u; - _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, flags); - } + _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, + Setting.AllBits(group.Count)); if (ImGui.Selectable("Disable All")) - _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, 0); + _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, Setting.Zero); } } diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index 06f1d126..9a956d2d 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -211,7 +211,7 @@ public class DebugTab : Window, ITab color.Pop(); foreach (var (mod, paths, manips) in collection._cache!.ModData.Data.OrderBy(t => t.Item1.Name)) { - using var id = mod is TemporaryMod t ? PushId(t.Priority) : PushId(((Mod)mod).ModPath.Name); + using var id = mod is TemporaryMod t ? PushId(t.Priority.Value) : PushId(((Mod)mod).ModPath.Name); using var node2 = TreeNode(mod.Name.Text); if (!node2) continue; From e94cdaec4627cf06e4dfcb5cb55e746779a04f94 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 5 Apr 2024 23:19:41 +0200 Subject: [PATCH 088/433] Some more. --- Penumbra/Import/TexToolsImporter.ModPack.cs | 6 +++--- Penumbra/Mods/Manager/ModMigration.cs | 12 ++++++------ Penumbra/Mods/Manager/ModOptionEditor.cs | 18 ++++++++++-------- Penumbra/Mods/ModCreator.cs | 4 ++-- Penumbra/Mods/Subclasses/IModGroup.cs | 18 +++++++++++------- Penumbra/Mods/Subclasses/ISubMod.cs | 4 ++-- Penumbra/Mods/Subclasses/ModPriority.cs | 10 +++++++++- Penumbra/Mods/Subclasses/MultiModGroup.cs | 14 +++++++------- Penumbra/Mods/Subclasses/SingleModGroup.cs | 16 ++++++++-------- Penumbra/Mods/Subclasses/SubMod.cs | 4 ++-- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 19 ++++++++++--------- 11 files changed, 70 insertions(+), 55 deletions(-) diff --git a/Penumbra/Import/TexToolsImporter.ModPack.cs b/Penumbra/Import/TexToolsImporter.ModPack.cs index 7a247a53..099b133c 100644 --- a/Penumbra/Import/TexToolsImporter.ModPack.cs +++ b/Penumbra/Import/TexToolsImporter.ModPack.cs @@ -153,7 +153,7 @@ public partial class TexToolsImporter // Iterate through all pages var options = new List(); - var groupPriority = 0; + var groupPriority = ModPriority.Default; var groupNames = new HashSet(); foreach (var page in modList.ModPackPages) { @@ -209,9 +209,9 @@ public partial class TexToolsImporter } } - _modManager.Creator.CreateOptionGroup(_currentModDirectory, group.SelectionType, name, groupPriority, groupPriority, + _modManager.Creator.CreateOptionGroup(_currentModDirectory, group.SelectionType, name, groupPriority, groupPriority.Value, defaultSettings ?? Setting.Zero, group.Description, options); - ++groupPriority; + groupPriority += 1; } } } diff --git a/Penumbra/Mods/Manager/ModMigration.cs b/Penumbra/Mods/Manager/ModMigration.cs index 8b73cae5..295afd7b 100644 --- a/Penumbra/Mods/Manager/ModMigration.cs +++ b/Penumbra/Mods/Manager/ModMigration.cs @@ -61,10 +61,9 @@ public static partial class ModMigration if (fileVersion > 0) return false; - var swaps = json["FileSwaps"]?.ToObject>() - ?? new Dictionary(); - var groups = json["Groups"]?.ToObject>() ?? new Dictionary(); - var priority = 1; + var swaps = json["FileSwaps"]?.ToObject>() ?? []; + var groups = json["Groups"]?.ToObject>() ?? []; + var priority = new ModPriority(1); var seenMetaFiles = new HashSet(); foreach (var group in groups.Values) ConvertGroup(creator, mod, group, ref priority, seenMetaFiles); @@ -116,7 +115,8 @@ public static partial class ModMigration return true; } - private static void ConvertGroup(ModCreator creator, Mod mod, OptionGroupV0 group, ref int priority, HashSet seenMetaFiles) + private static void ConvertGroup(ModCreator creator, Mod mod, OptionGroupV0 group, ref ModPriority priority, + HashSet seenMetaFiles) { if (group.Options.Count == 0) return; @@ -125,7 +125,7 @@ public static partial class ModMigration { case GroupType.Multi: - var optionPriority = 0; + var optionPriority = ModPriority.Default; var newMultiGroup = new MultiModGroup() { Name = group.GroupName, diff --git a/Penumbra/Mods/Manager/ModOptionEditor.cs b/Penumbra/Mods/Manager/ModOptionEditor.cs index ea6a62df..0ffdc4af 100644 --- a/Penumbra/Mods/Manager/ModOptionEditor.cs +++ b/Penumbra/Mods/Manager/ModOptionEditor.cs @@ -34,8 +34,6 @@ public enum ModOptionChangeType public class ModOptionEditor(CommunicatorService communicator, SaveService saveService, Configuration config) { - - /// Change the type of a group given by mod and index to type, if possible. public void ChangeModGroupType(Mod mod, int groupIdx, GroupType type) { @@ -86,7 +84,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS if (!VerifyFileName(mod, null, newName, true)) return; - var maxPriority = mod.Groups.Count == 0 ? 0 : mod.Groups.Max(o => o.Priority) + 1; + var maxPriority = mod.Groups.Count == 0 ? ModPriority.Default : mod.Groups.Max(o => o.Priority) + 1; mod.Groups.Add(type == GroupType.Multi ? new MultiModGroup @@ -169,7 +167,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS } /// Change the internal priority of the given option group. - public void ChangeGroupPriority(Mod mod, int groupIdx, int newPriority) + public void ChangeGroupPriority(Mod mod, int groupIdx, ModPriority newPriority) { var group = mod.Groups[groupIdx]; if (group.Priority == newPriority) @@ -186,7 +184,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS } /// Change the internal priority of the given option. - public void ChangeOptionPriority(Mod mod, int groupIdx, int optionIdx, int newPriority) + public void ChangeOptionPriority(Mod mod, int groupIdx, int optionIdx, ModPriority newPriority) { switch (mod.Groups[groupIdx]) { @@ -240,7 +238,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS s.OptionData.Add(subMod); break; case MultiModGroup m: - m.PrioritizedOptions.Add((subMod, 0)); + m.PrioritizedOptions.Add((subMod, ModPriority.Default)); break; } @@ -263,8 +261,12 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS return ((SubMod)group[^1], true); } - /// Add an existing option to a given group with a given priority. - public void AddOption(Mod mod, int groupIdx, ISubMod option, int priority = 0) + /// Add an existing option to a given group with default priority. + public void AddOption(Mod mod, int groupIdx, ISubMod option) + => AddOption(mod, groupIdx, option, ModPriority.Default); + + /// Add an existing option to a given group with a given priority. + public void AddOption(Mod mod, int groupIdx, ISubMod option, ModPriority priority) { if (option is not SubMod o) return; diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index c324af48..2bcdd3b1 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -235,7 +235,7 @@ public partial class ModCreator(SaveService _saveService, Configuration config, /// Create a file for an option group from given data. public void CreateOptionGroup(DirectoryInfo baseFolder, GroupType type, string name, - int priority, int index, Setting defaultSettings, string desc, IEnumerable subMods) + ModPriority priority, int index, Setting defaultSettings, string desc, IEnumerable subMods) { switch (type) { @@ -248,7 +248,7 @@ public partial class ModCreator(SaveService _saveService, Configuration config, Priority = priority, DefaultSettings = defaultSettings, }; - group.PrioritizedOptions.AddRange(subMods.OfType().Select((s, idx) => (s, idx))); + group.PrioritizedOptions.AddRange(subMods.OfType().Select((s, idx) => (s, new ModPriority(idx)))); _saveService.ImmediateSaveSync(new ModSaveGroup(baseFolder, group, index, Config.ReplaceNonAsciiOnImport)); break; } diff --git a/Penumbra/Mods/Subclasses/IModGroup.cs b/Penumbra/Mods/Subclasses/IModGroup.cs index 2f6b2403..e9e2a93b 100644 --- a/Penumbra/Mods/Subclasses/IModGroup.cs +++ b/Penumbra/Mods/Subclasses/IModGroup.cs @@ -8,13 +8,13 @@ public interface IModGroup : IEnumerable { public const int MaxMultiOptions = 32; - public string Name { get; } - public string Description { get; } - public GroupType Type { get; } - public int Priority { get; } - public Setting DefaultSettings { get; set; } + public string Name { get; } + public string Description { get; } + public GroupType Type { get; } + public ModPriority Priority { get; } + public Setting DefaultSettings { get; set; } - public int OptionPriority(Index optionIdx); + public ModPriority OptionPriority(Index optionIdx); public ISubMod this[Index idx] { get; } @@ -94,7 +94,11 @@ public readonly struct ModSaveGroup : ISavable j.WritePropertyName("Options"); j.WriteStartArray(); for (var idx = 0; idx < _group.Count; ++idx) - ISubMod.WriteSubMod(j, serializer, _group[idx], _basePath, _group.Type == GroupType.Multi ? _group.OptionPriority(idx) : null); + ISubMod.WriteSubMod(j, serializer, _group[idx], _basePath, _group.Type switch + { + GroupType.Multi => _group.OptionPriority(idx), + _ => null, + }); j.WriteEndArray(); j.WriteEndObject(); diff --git a/Penumbra/Mods/Subclasses/ISubMod.cs b/Penumbra/Mods/Subclasses/ISubMod.cs index 8c296f20..29323c1d 100644 --- a/Penumbra/Mods/Subclasses/ISubMod.cs +++ b/Penumbra/Mods/Subclasses/ISubMod.cs @@ -16,7 +16,7 @@ public interface ISubMod public bool IsDefault { get; } - public static void WriteSubMod(JsonWriter j, JsonSerializer serializer, ISubMod mod, DirectoryInfo basePath, int? priority) + public static void WriteSubMod(JsonWriter j, JsonSerializer serializer, ISubMod mod, DirectoryInfo basePath, ModPriority? priority) { j.WriteStartObject(); j.WritePropertyName(nameof(Name)); @@ -26,7 +26,7 @@ public interface ISubMod if (priority != null) { j.WritePropertyName(nameof(IModGroup.Priority)); - j.WriteValue(priority.Value); + j.WriteValue(priority.Value.Value); } j.WritePropertyName(nameof(mod.Files)); diff --git a/Penumbra/Mods/Subclasses/ModPriority.cs b/Penumbra/Mods/Subclasses/ModPriority.cs index 3302c627..a99c12ed 100644 --- a/Penumbra/Mods/Subclasses/ModPriority.cs +++ b/Penumbra/Mods/Subclasses/ModPriority.cs @@ -8,7 +8,9 @@ public readonly record struct ModPriority(int Value) : IAdditionOperators, IAdditionOperators, ISubtractionOperators, - ISubtractionOperators + ISubtractionOperators, + IIncrementOperators, + IComparable { public static readonly ModPriority Default = new(0); public static readonly ModPriority MaxValue = new(int.MaxValue); @@ -58,4 +60,10 @@ public readonly record struct ModPriority(int Value) : public static ModPriority operator -(ModPriority left, int right) => new(left.Value - right); + + public static ModPriority operator ++(ModPriority value) + => new(value.Value + 1); + + public int CompareTo(ModPriority other) + => Value.CompareTo(other.Value); } diff --git a/Penumbra/Mods/Subclasses/MultiModGroup.cs b/Penumbra/Mods/Subclasses/MultiModGroup.cs index 8a8e10bd..444e8e2c 100644 --- a/Penumbra/Mods/Subclasses/MultiModGroup.cs +++ b/Penumbra/Mods/Subclasses/MultiModGroup.cs @@ -14,12 +14,12 @@ public sealed class MultiModGroup : IModGroup public GroupType Type => GroupType.Multi; - public string Name { get; set; } = "Group"; - public string Description { get; set; } = "A non-exclusive group of settings."; - public int Priority { get; set; } - public Setting DefaultSettings { get; set; } + public string Name { get; set; } = "Group"; + public string Description { get; set; } = "A non-exclusive group of settings."; + public ModPriority Priority { get; set; } + public Setting DefaultSettings { get; set; } - public int OptionPriority(Index idx) + public ModPriority OptionPriority(Index idx) => PrioritizedOptions[idx].Priority; public ISubMod this[Index idx] @@ -29,7 +29,7 @@ public sealed class MultiModGroup : IModGroup public int Count => PrioritizedOptions.Count; - public readonly List<(SubMod Mod, int Priority)> PrioritizedOptions = new(); + public readonly List<(SubMod Mod, ModPriority Priority)> PrioritizedOptions = []; public IEnumerator GetEnumerator() => PrioritizedOptions.Select(o => o.Mod).GetEnumerator(); @@ -43,7 +43,7 @@ public sealed class MultiModGroup : IModGroup { Name = json[nameof(Name)]?.ToObject() ?? string.Empty, Description = json[nameof(Description)]?.ToObject() ?? string.Empty, - Priority = json[nameof(Priority)]?.ToObject() ?? 0, + Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, DefaultSettings = json[nameof(DefaultSettings)]?.ToObject() ?? Setting.Zero, }; if (ret.Name.Length == 0) diff --git a/Penumbra/Mods/Subclasses/SingleModGroup.cs b/Penumbra/Mods/Subclasses/SingleModGroup.cs index be1dbde5..0bfa04f4 100644 --- a/Penumbra/Mods/Subclasses/SingleModGroup.cs +++ b/Penumbra/Mods/Subclasses/SingleModGroup.cs @@ -12,14 +12,14 @@ public sealed class SingleModGroup : IModGroup public GroupType Type => GroupType.Single; - public string Name { get; set; } = "Option"; - public string Description { get; set; } = "A mutually exclusive group of settings."; - public int Priority { get; set; } - public Setting DefaultSettings { get; set; } + public string Name { get; set; } = "Option"; + public string Description { get; set; } = "A mutually exclusive group of settings."; + public ModPriority Priority { get; set; } + public Setting DefaultSettings { get; set; } public readonly List OptionData = []; - public int OptionPriority(Index _) + public ModPriority OptionPriority(Index _) => Priority; public ISubMod this[Index idx] @@ -42,7 +42,7 @@ public sealed class SingleModGroup : IModGroup { Name = json[nameof(Name)]?.ToObject() ?? string.Empty, Description = json[nameof(Description)]?.ToObject() ?? string.Empty, - Priority = json[nameof(Priority)]?.ToObject() ?? 0, + Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, DefaultSettings = json[nameof(DefaultSettings)]?.ToObject() ?? Setting.Zero, }; if (ret.Name.Length == 0) @@ -72,9 +72,9 @@ public sealed class SingleModGroup : IModGroup Name = Name, Description = Description, Priority = Priority, - DefaultSettings = Setting.Multi((int) DefaultSettings.Value), + DefaultSettings = Setting.Multi((int)DefaultSettings.Value), }; - multi.PrioritizedOptions.AddRange(OptionData.Select((o, i) => (o, i))); + multi.PrioritizedOptions.AddRange(OptionData.Select((o, i) => (o, new ModPriority(i)))); return multi; default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } diff --git a/Penumbra/Mods/Subclasses/SubMod.cs b/Penumbra/Mods/Subclasses/SubMod.cs index 88c4e4ce..4f35cd33 100644 --- a/Penumbra/Mods/Subclasses/SubMod.cs +++ b/Penumbra/Mods/Subclasses/SubMod.cs @@ -53,7 +53,7 @@ public sealed class SubMod : ISubMod OptionIdx = optionIdx; } - public void Load(DirectoryInfo basePath, JToken json, out int priority) + public void Load(DirectoryInfo basePath, JToken json, out ModPriority priority) { FileData.Clear(); FileSwapData.Clear(); @@ -62,7 +62,7 @@ public sealed class SubMod : ISubMod // Every option has a name, but priorities are only relevant for multi group options. Name = json[nameof(ISubMod.Name)]?.ToObject() ?? string.Empty; Description = json[nameof(ISubMod.Description)]?.ToObject() ?? string.Empty; - priority = json[nameof(IModGroup.Priority)]?.ToObject() ?? 0; + priority = json[nameof(IModGroup.Priority)]?.ToObject() ?? ModPriority.Default; var files = (JObject?)json[nameof(Files)]; if (files != null) diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index 1292367a..80af7b15 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -511,7 +511,8 @@ public class ModPanelEditTab( { var isDefaultOption = group.DefaultSettings.HasFlag(optionIdx); if (ImGui.Checkbox("##default", ref isDefaultOption)) - panel._modManager.OptionEditor.ChangeModGroupDefaultOption(panel._mod, groupIdx, group.DefaultSettings.SetBit(optionIdx, isDefaultOption)); + panel._modManager.OptionEditor.ChangeModGroupDefaultOption(panel._mod, groupIdx, + group.DefaultSettings.SetBit(optionIdx, isDefaultOption)); ImGuiUtil.HoverTooltip($"{(isDefaultOption ? "Disable" : "Enable")} {option.Name} per default in this group."); } @@ -669,10 +670,10 @@ public class ModPanelEditTab( public const int Description = -7; // Temporary strings - private static string? _currentEdit; - private static int? _currentGroupPriority; - private static int _currentField = None; - private static int _optionIndex = None; + private static string? _currentEdit; + private static ModPriority? _currentGroupPriority; + private static int _currentField = None; + private static int _optionIndex = None; public static void Reset() { @@ -705,13 +706,13 @@ public class ModPanelEditTab( return false; } - public static bool Priority(string label, int field, int option, int oldValue, out int value, float width) + public static bool Priority(string label, int field, int option, ModPriority oldValue, out ModPriority value, float width) { - var tmp = field == _currentField && option == _optionIndex ? _currentGroupPriority ?? oldValue : oldValue; + var tmp = (field == _currentField && option == _optionIndex ? _currentGroupPriority ?? oldValue : oldValue).Value; ImGui.SetNextItemWidth(width); if (ImGui.InputInt(label, ref tmp, 0, 0)) { - _currentGroupPriority = tmp; + _currentGroupPriority = new ModPriority(tmp); _optionIndex = option; _currentField = field; } @@ -724,7 +725,7 @@ public class ModPanelEditTab( return ret; } - value = 0; + value = ModPriority.Default; return false; } } From 21a55b95d9a3379e3df1c8cd885780412476fc1c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 9 Apr 2024 15:19:44 +0200 Subject: [PATCH 089/433] Add rename mod field. --- Penumbra/Configuration.cs | 24 +++++----- Penumbra/UI/ModsTab/ModFileSystemSelector.cs | 49 +++++++++++++++++++- Penumbra/UI/ModsTab/RenameField.cs | 26 +++++++++++ Penumbra/UI/Tabs/SettingsTab.cs | 30 ++++++++++++ 4 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 Penumbra/UI/ModsTab/RenameField.cs diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index f91e0534..98668e8a 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -11,6 +11,7 @@ using Penumbra.Mods; using Penumbra.Mods.Manager; using Penumbra.Services; using Penumbra.UI.Classes; +using Penumbra.UI.ModsTab; using Penumbra.UI.ResourceWatcher; using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs; @@ -40,17 +41,18 @@ public class Configuration : IPluginConfiguration, ISavable public bool HideUiWhenUiHidden { get; set; } = false; public bool UseDalamudUiTextureRedirection { get; set; } = true; - public bool UseCharacterCollectionInMainWindow { get; set; } = true; - public bool UseCharacterCollectionsInCards { get; set; } = true; - public bool UseCharacterCollectionInInspect { get; set; } = true; - public bool UseCharacterCollectionInTryOn { get; set; } = true; - public bool UseOwnerNameForCharacterCollection { get; set; } = true; - public bool UseNoModsInInspect { get; set; } = false; - public bool HideChangedItemFilters { get; set; } = false; - public bool ReplaceNonAsciiOnImport { get; set; } = false; - public bool HidePrioritiesInSelector { get; set; } = false; - public bool HideRedrawBar { get; set; } = false; - public int OptionGroupCollapsibleMin { get; set; } = 5; + public bool UseCharacterCollectionInMainWindow { get; set; } = true; + public bool UseCharacterCollectionsInCards { get; set; } = true; + public bool UseCharacterCollectionInInspect { get; set; } = true; + public bool UseCharacterCollectionInTryOn { get; set; } = true; + public bool UseOwnerNameForCharacterCollection { get; set; } = true; + public bool UseNoModsInInspect { get; set; } = false; + public bool HideChangedItemFilters { get; set; } = false; + public bool ReplaceNonAsciiOnImport { get; set; } = false; + public bool HidePrioritiesInSelector { get; set; } = false; + public bool HideRedrawBar { get; set; } = false; + public RenameField ShowRename { get; set; } = RenameField.BothDataPrio; + public int OptionGroupCollapsibleMin { get; set; } = 5; public Vector2 MinimumSize = new(Constants.MinimumSizeX, Constants.MinimumSizeY); diff --git a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs index cd0eb982..11a2d96f 100644 --- a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs +++ b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs @@ -66,7 +66,7 @@ public sealed class ModFileSystemSelector : FileSystemSelector ClearQuickMove(1, _config.QuickMoveFolder2, () => {_config.QuickMoveFolder2 = string.Empty; _config.Save();}), 120); SubscribeRightClickMain(() => ClearQuickMove(2, _config.QuickMoveFolder3, () => {_config.QuickMoveFolder3 = string.Empty; _config.Save();}), 130); UnsubscribeRightClickLeaf(RenameLeaf); - SubscribeRightClickLeaf(RenameLeafMod, 1000); + SetRenameSearchPath(_config.ShowRename); AddButton(AddNewModButton, 0); AddButton(AddImportModButton, 1); AddButton(AddHelpButton, 2); @@ -92,6 +92,37 @@ public sealed class ModFileSystemSelector : FileSystemSelector DeleteSelectionButton(size, _config.DeleteModModifier, "mod", "mods", _modManager.DeleteMod); diff --git a/Penumbra/UI/ModsTab/RenameField.cs b/Penumbra/UI/ModsTab/RenameField.cs new file mode 100644 index 00000000..00232750 --- /dev/null +++ b/Penumbra/UI/ModsTab/RenameField.cs @@ -0,0 +1,26 @@ +namespace Penumbra.UI.ModsTab; + +public enum RenameField +{ + None, + RenameSearchPath, + RenameData, + BothSearchPathPrio, + BothDataPrio, +} + +public static class RenameFieldExtensions +{ + public static (string Name, string Desc) GetData(this RenameField value) + => value switch + { + RenameField.None => ("None", "Show no rename fields in the context menu for mods."), + RenameField.RenameSearchPath => ("Search Path", "Show only the search path / move field in the context menu for mods."), + RenameField.RenameData => ("Mod Name", "Show only the mod name field in the context menu for mods."), + RenameField.BothSearchPathPrio => ("Both (Focus Search Path)", + "Show both rename fields in the context menu for mods, but put the keyboard cursor on the search path field."), + RenameField.BothDataPrio => ("Both (Focus Mod Name)", + "Show both rename fields in the context menu for mods, but put the keyboard cursor on the mod name field"), + _ => (string.Empty, string.Empty), + }; +} diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index c524a840..439f7be4 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -533,12 +533,42 @@ public class SettingsTab : ITab "Instead of keeping the mod-selector in the Installed Mods tab a fixed width, this will let it scale with the total size of the Penumbra window."); } + private void DrawRenameSettings() + { + ImGui.SetNextItemWidth(UiHelpers.InputTextWidth.X); + using (var combo = ImRaii.Combo("##renameSettings", _config.ShowRename.GetData().Name)) + { + if (combo) + foreach (var value in Enum.GetValues()) + { + var (name, desc) = value.GetData(); + if (ImGui.Selectable(name, _config.ShowRename == value)) + { + _config.ShowRename = value; + _selector.SetRenameSearchPath(value); + _config.Save(); + } + + ImGuiUtil.HoverTooltip(desc); + } + } + + ImGui.SameLine(); + const string tt = + "Select which of the two renaming input fields are visible when opening the right-click context menu of a mod in the mod selector."; + ImGuiComponents.HelpMarker(tt); + ImGui.SameLine(); + ImGui.TextUnformatted("Rename Fields in Mod Context Menu"); + ImGuiUtil.HoverTooltip(tt); + } + /// Draw all settings pertaining to the mod selector. private void DrawModSelectorSettings() { DrawFolderSortType(); DrawAbsoluteSizeSelector(); DrawRelativeSizeSelector(); + DrawRenameSettings(); Checkbox("Open Folders by Default", "Whether to start with all folders collapsed or expanded in the mod selector.", _config.OpenFoldersByDefault, v => { From 7280c4b2f72c6b14b2a04b0387a1e6536429f7b9 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 9 Apr 2024 15:20:38 +0200 Subject: [PATCH 090/433] Selector improvements. --- OtterGui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OtterGui b/OtterGui index f48c6886..5de71c22 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit f48c6886cbc163c5a292fa8b9fd919cb01c11d7b +Subproject commit 5de71c22c03581738c25aa43d7dff10365ec7db3 From c0ee80629df66b987b391afb4625b742dc7a298c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 9 Apr 2024 16:05:55 +0200 Subject: [PATCH 091/433] Allow right click to paste into filter as long as it is unfocused. --- OtterGui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OtterGui b/OtterGui index 5de71c22..4673e93f 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 5de71c22c03581738c25aa43d7dff10365ec7db3 +Subproject commit 4673e93f5165108a7f5b91236406d527f16384a5 From 45b1c55b67bc41fbf16aa36692c5d2657c380134 Mon Sep 17 00:00:00 2001 From: ocealot Date: Thu, 11 Apr 2024 15:00:54 -0400 Subject: [PATCH 092/433] Add accessory vfxs --- .../Hooks/Resources/ResolvePathHooksBase.cs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs index 6b4abf90..537992e2 100644 --- a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs +++ b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs @@ -1,4 +1,5 @@ using Dalamud.Hooking; +using Dalamud.Memory; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Classes; using OtterGui.Services; @@ -57,6 +58,7 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable _resolveSkpPathHook = Create($"{name}.{nameof(ResolveSkp)}", hooks, vTable[74], type, ResolveSkp, ResolveSkpHuman); _resolveTmbPathHook = Create( $"{name}.{nameof(ResolveTmb)}", hooks, vTable[77], ResolveTmb); _resolveVfxPathHook = Create( $"{name}.{nameof(ResolveVfx)}", hooks, vTable[84], ResolveVfx); + _resolveVfxPathHook = Create( $"{name}.{nameof(ResolveVfx)}", hooks, vTable[84], type, ResolveVfx, ResolveVfxHuman); // @formatter:on Enable(); } @@ -179,6 +181,27 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable return ResolvePath(data, _resolveSkpPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); } + private nint ResolveVfxHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint slotIndex, nint unkOutParam) + { + if (slotIndex <= 4) return ResolveVfx(drawObject, pathBuffer, pathBufferSize, slotIndex, unkOutParam); + + // Enable vfxs for accessories + var data = Marshal.ReadIntPtr(drawObject + 0xA38); + if (data == IntPtr.Zero) return ResolveVfx(drawObject, pathBuffer, pathBufferSize, slotIndex, unkOutParam); + + var slot = data + 12 * (nint)slotIndex; + var model = Marshal.ReadInt16(slot); + var variant = Marshal.ReadInt16(slot + 2); + var vfxId = Marshal.ReadInt16(slot + 8); + + if (model == 0 || variant == 0 || vfxId == 0) return ResolveVfx(drawObject, pathBuffer, pathBufferSize, slotIndex, unkOutParam); + var path = "chara/accessory/a" + model.ToString().PadLeft(4, '0') + "/vfx/eff/va" + vfxId.ToString().PadLeft(4, '0') + ".avfx"; + + MemoryHelper.WriteString(pathBuffer, path); + Marshal.WriteIntPtr(unkOutParam, 0x00000004); + return ResolvePath(drawObject, pathBuffer); + } + private DisposableContainer GetEstChanges(nint drawObject, out ResolveData data) { data = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); From eb0e7e2f5fd8ec434ff6caf0b047da8c8b5ee0eb Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 11 Apr 2024 21:25:00 +0200 Subject: [PATCH 093/433] Update ocealots code #1. --- .../Hooks/Resources/ResolvePathHooksBase.cs | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs index 537992e2..f322267b 100644 --- a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs +++ b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs @@ -1,5 +1,5 @@ +using System.Text.Unicode; using Dalamud.Hooking; -using Dalamud.Memory; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Classes; using OtterGui.Services; @@ -57,7 +57,6 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable _resolveSklbPathHook = Create($"{name}.{nameof(ResolveSklb)}", hooks, vTable[72], type, ResolveSklb, ResolveSklbHuman); _resolveSkpPathHook = Create($"{name}.{nameof(ResolveSkp)}", hooks, vTable[74], type, ResolveSkp, ResolveSkpHuman); _resolveTmbPathHook = Create( $"{name}.{nameof(ResolveTmb)}", hooks, vTable[77], ResolveTmb); - _resolveVfxPathHook = Create( $"{name}.{nameof(ResolveVfx)}", hooks, vTable[84], ResolveVfx); _resolveVfxPathHook = Create( $"{name}.{nameof(ResolveVfx)}", hooks, vTable[84], type, ResolveVfx, ResolveVfxHuman); // @formatter:on Enable(); @@ -183,22 +182,27 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable private nint ResolveVfxHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint slotIndex, nint unkOutParam) { - if (slotIndex <= 4) return ResolveVfx(drawObject, pathBuffer, pathBufferSize, slotIndex, unkOutParam); + if (slotIndex <= 4) + return ResolveVfx(drawObject, pathBuffer, pathBufferSize, slotIndex, unkOutParam); + var changedEquipData = ((Human*)drawObject)->ChangedEquipData; // Enable vfxs for accessories - var data = Marshal.ReadIntPtr(drawObject + 0xA38); - if (data == IntPtr.Zero) return ResolveVfx(drawObject, pathBuffer, pathBufferSize, slotIndex, unkOutParam); + if (changedEquipData == null) + return ResolveVfx(drawObject, pathBuffer, pathBufferSize, slotIndex, unkOutParam); - var slot = data + 12 * (nint)slotIndex; - var model = Marshal.ReadInt16(slot); - var variant = Marshal.ReadInt16(slot + 2); - var vfxId = Marshal.ReadInt16(slot + 8); + var slot = (ushort*)(changedEquipData + 12 * (nint)slotIndex); + var model = slot[0]; + var variant = slot[1]; + var vfxId = slot[4]; - if (model == 0 || variant == 0 || vfxId == 0) return ResolveVfx(drawObject, pathBuffer, pathBufferSize, slotIndex, unkOutParam); - var path = "chara/accessory/a" + model.ToString().PadLeft(4, '0') + "/vfx/eff/va" + vfxId.ToString().PadLeft(4, '0') + ".avfx"; + if (model == 0 || variant == 0 || vfxId == 0) + return ResolveVfx(drawObject, pathBuffer, pathBufferSize, slotIndex, unkOutParam); - MemoryHelper.WriteString(pathBuffer, path); - Marshal.WriteIntPtr(unkOutParam, 0x00000004); + if (!Utf8.TryWrite(new Span((void*)pathBuffer, (int)pathBufferSize), $"chara/accessory/a{model:D4}/vfx/eff/va{vfxId:D4}.avfx", + out _)) + return ResolveVfx(drawObject, pathBuffer, pathBufferSize, slotIndex, unkOutParam); + + *(ulong*)unkOutParam = 4; return ResolvePath(drawObject, pathBuffer); } From 793ed4f0a722c78b11332b391f8beeed938c896b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 12 Apr 2024 00:02:09 +0200 Subject: [PATCH 094/433] With explicit null-termination, maybe? --- Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs index f322267b..4e24ba39 100644 --- a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs +++ b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs @@ -198,7 +198,7 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable if (model == 0 || variant == 0 || vfxId == 0) return ResolveVfx(drawObject, pathBuffer, pathBufferSize, slotIndex, unkOutParam); - if (!Utf8.TryWrite(new Span((void*)pathBuffer, (int)pathBufferSize), $"chara/accessory/a{model:D4}/vfx/eff/va{vfxId:D4}.avfx", + if (!Utf8.TryWrite(new Span((void*)pathBuffer, (int)pathBufferSize), $"chara/accessory/a{model:D4}/vfx/eff/va{vfxId:D4}.avfx\0", out _)) return ResolveVfx(drawObject, pathBuffer, pathBufferSize, slotIndex, unkOutParam); From ba8999914fd24408fb0635cffbb4da48b0dafc44 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 12 Apr 2024 12:33:57 +0200 Subject: [PATCH 095/433] Rework API, use Collection ID in crash handler, use collection GUIDs in more places. --- OtterGui | 2 +- Penumbra.Api | 2 +- .../Buffers/AnimationInvocationBuffer.cs | 32 +- .../Buffers/CharacterBaseBuffer.cs | 48 +- .../Buffers/ModdedFileBuffer.cs | 60 +- Penumbra.GameData | 2 +- Penumbra/Api/Api/ApiHelpers.cs | 76 + Penumbra/Api/Api/CollectionApi.cs | 141 ++ Penumbra/Api/Api/EditingApi.cs | 40 + Penumbra/Api/Api/GameStateApi.cs | 82 + Penumbra/Api/Api/MetaApi.cs | 23 + Penumbra/Api/Api/ModSettingsApi.cs | 282 +++ Penumbra/Api/Api/ModsApi.cs | 132 ++ Penumbra/Api/Api/PenumbraApi.cs | 40 + Penumbra/Api/Api/PluginStateApi.cs | 30 + Penumbra/Api/Api/RedrawApi.cs | 27 + Penumbra/Api/Api/ResolveApi.cs | 101 + Penumbra/Api/Api/ResourceTreeApi.cs | 63 + Penumbra/Api/Api/TemporaryApi.cs | 190 ++ Penumbra/Api/Api/UiApi.cs | 101 + Penumbra/Api/DalamudSubstitutionProvider.cs | 3 +- Penumbra/Api/HttpApi.cs | 22 +- Penumbra/Api/IpcProviders.cs | 118 ++ Penumbra/Api/IpcTester.cs | 1762 ----------------- .../Api/IpcTester/CollectionsIpcTester.cs | 166 ++ Penumbra/Api/IpcTester/EditingIpcTester.cs | 70 + Penumbra/Api/IpcTester/GameStateIpcTester.cs | 137 ++ Penumbra/Api/IpcTester/IpcTester.cs | 133 ++ Penumbra/Api/IpcTester/MetaIpcTester.cs | 38 + .../Api/IpcTester/ModSettingsIpcTester.cs | 181 ++ Penumbra/Api/IpcTester/ModsIpcTester.cs | 154 ++ .../Api/IpcTester/PluginStateIpcTester.cs | 132 ++ Penumbra/Api/IpcTester/RedrawingIpcTester.cs | 72 + Penumbra/Api/IpcTester/ResolveIpcTester.cs | 114 ++ .../Api/IpcTester/ResourceTreeIpcTester.cs | 349 ++++ Penumbra/Api/IpcTester/TemporaryIpcTester.cs | 203 ++ Penumbra/Api/IpcTester/UiIpcTester.cs | 128 ++ Penumbra/Api/PenumbraApi.cs | 1374 ------------- Penumbra/Api/PenumbraIpcProviders.cs | 435 ---- Penumbra/Collections/Cache/CollectionCache.cs | 10 +- .../Cache/CollectionCacheManager.cs | 2 +- Penumbra/Collections/Cache/ImcCache.cs | 2 +- .../Collections/Manager/ActiveCollections.cs | 160 +- .../Collections/Manager/CollectionStorage.cs | 107 +- .../Manager/IndividualCollections.Files.cs | 98 +- .../Collections/Manager/InheritanceManager.cs | 11 +- .../Manager/TempCollectionManager.cs | 53 +- Penumbra/Collections/ModCollection.cs | 35 +- Penumbra/Collections/ModCollectionSave.cs | 18 +- Penumbra/CommandHandler.cs | 2 +- Penumbra/Communication/ChangedItemClick.cs | 3 +- Penumbra/Communication/ChangedItemHover.cs | 3 +- .../Communication/CreatedCharacterBase.cs | 1 + .../Communication/CreatingCharacterBase.cs | 3 +- Penumbra/Communication/EnabledChanged.cs | 3 +- Penumbra/Communication/ModDirectoryChanged.cs | 1 + Penumbra/Communication/ModFileChanged.cs | 1 + Penumbra/Communication/ModOptionChanged.cs | 1 + Penumbra/Communication/ModPathChanged.cs | 8 +- Penumbra/Communication/ModSettingChanged.cs | 1 + Penumbra/Communication/PostEnabledDraw.cs | 3 +- .../Communication/PostSettingsPanelDraw.cs | 3 +- .../Communication/PreSettingsPanelDraw.cs | 3 +- .../Communication/PreSettingsTabBarDraw.cs | 3 +- Penumbra/GuidExtensions.cs | 254 +++ Penumbra/Interop/PathResolving/MetaState.cs | 2 +- .../Interop/PathResolving/PathResolver.cs | 10 +- .../Interop/PathResolving/SubfileHelper.cs | 2 +- .../ResourceTree/ResourceTreeApiHelper.cs | 82 +- Penumbra/Mods/Manager/ModOptionEditor.cs | 17 +- Penumbra/Mods/Manager/ModStorage.cs | 2 +- Penumbra/Mods/Subclasses/IModGroup.cs | 18 +- Penumbra/Mods/Subclasses/ModSettings.cs | 8 +- Penumbra/Mods/Subclasses/MultiModGroup.cs | 3 + Penumbra/Mods/Subclasses/SingleModGroup.cs | 3 + Penumbra/Mods/TemporaryMod.cs | 6 +- Penumbra/Penumbra.cs | 4 +- Penumbra/Penumbra.csproj | 2 +- Penumbra/Services/ConfigMigrationService.cs | 4 +- Penumbra/Services/CrashHandlerService.cs | 6 +- Penumbra/Services/FilenameService.cs | 2 +- Penumbra/Services/StaticServiceManager.cs | 10 +- .../ModEditWindow.QuickImport.cs | 4 +- Penumbra/UI/CollectionTab/CollectionPanel.cs | 54 +- .../UI/CollectionTab/CollectionSelector.cs | 2 +- Penumbra/UI/Tabs/CollectionsTab.cs | 4 +- Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs | 6 +- Penumbra/UI/Tabs/Debug/DebugTab.cs | 18 +- 88 files changed, 4193 insertions(+), 3930 deletions(-) create mode 100644 Penumbra/Api/Api/ApiHelpers.cs create mode 100644 Penumbra/Api/Api/CollectionApi.cs create mode 100644 Penumbra/Api/Api/EditingApi.cs create mode 100644 Penumbra/Api/Api/GameStateApi.cs create mode 100644 Penumbra/Api/Api/MetaApi.cs create mode 100644 Penumbra/Api/Api/ModSettingsApi.cs create mode 100644 Penumbra/Api/Api/ModsApi.cs create mode 100644 Penumbra/Api/Api/PenumbraApi.cs create mode 100644 Penumbra/Api/Api/PluginStateApi.cs create mode 100644 Penumbra/Api/Api/RedrawApi.cs create mode 100644 Penumbra/Api/Api/ResolveApi.cs create mode 100644 Penumbra/Api/Api/ResourceTreeApi.cs create mode 100644 Penumbra/Api/Api/TemporaryApi.cs create mode 100644 Penumbra/Api/Api/UiApi.cs create mode 100644 Penumbra/Api/IpcProviders.cs delete mode 100644 Penumbra/Api/IpcTester.cs create mode 100644 Penumbra/Api/IpcTester/CollectionsIpcTester.cs create mode 100644 Penumbra/Api/IpcTester/EditingIpcTester.cs create mode 100644 Penumbra/Api/IpcTester/GameStateIpcTester.cs create mode 100644 Penumbra/Api/IpcTester/IpcTester.cs create mode 100644 Penumbra/Api/IpcTester/MetaIpcTester.cs create mode 100644 Penumbra/Api/IpcTester/ModSettingsIpcTester.cs create mode 100644 Penumbra/Api/IpcTester/ModsIpcTester.cs create mode 100644 Penumbra/Api/IpcTester/PluginStateIpcTester.cs create mode 100644 Penumbra/Api/IpcTester/RedrawingIpcTester.cs create mode 100644 Penumbra/Api/IpcTester/ResolveIpcTester.cs create mode 100644 Penumbra/Api/IpcTester/ResourceTreeIpcTester.cs create mode 100644 Penumbra/Api/IpcTester/TemporaryIpcTester.cs create mode 100644 Penumbra/Api/IpcTester/UiIpcTester.cs delete mode 100644 Penumbra/Api/PenumbraApi.cs delete mode 100644 Penumbra/Api/PenumbraIpcProviders.cs create mode 100644 Penumbra/GuidExtensions.cs diff --git a/OtterGui b/OtterGui index 4673e93f..9599c806 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 4673e93f5165108a7f5b91236406d527f16384a5 +Subproject commit 9599c806877e2972f964dfa68e5207cf3a8f2b84 diff --git a/Penumbra.Api b/Penumbra.Api index 8787efc8..e5c8f544 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit 8787efc8fc897dfbb4515ebbabbcd5e6f54d1b42 +Subproject commit e5c8f5446879e2e0e541eb4d8fee15e98b1885bc diff --git a/Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs b/Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs index c92a14fd..3446530a 100644 --- a/Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs +++ b/Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs @@ -24,7 +24,7 @@ public record struct VfxFuncInvokedEntry( string InvocationType, string CharacterName, string CharacterAddress, - string CollectionName) : ICrashDataEntry; + Guid CollectionId) : ICrashDataEntry; /// Only expose the write interface for the buffer. public interface IAnimationInvocationBufferWriter @@ -32,19 +32,19 @@ public interface IAnimationInvocationBufferWriter /// Write a line into the buffer with the given data. /// The address of the related character, if known. /// The name of the related character, anonymized or relying on index if unavailable, if known. - /// The name of the associated collection. Not anonymized. + /// The GUID of the associated collection. /// The type of VFX func called. - public void WriteLine(nint characterAddress, ReadOnlySpan characterName, string collectionName, AnimationInvocationType type); + public void WriteLine(nint characterAddress, ReadOnlySpan characterName, Guid collectionId, AnimationInvocationType type); } internal sealed class AnimationInvocationBuffer : MemoryMappedBuffer, IAnimationInvocationBufferWriter, IBufferReader { private const int _version = 1; private const int _lineCount = 64; - private const int _lineCapacity = 256; + private const int _lineCapacity = 128; private const string _name = "Penumbra.AnimationInvocation"; - public void WriteLine(nint characterAddress, ReadOnlySpan characterName, string collectionName, AnimationInvocationType type) + public void WriteLine(nint characterAddress, ReadOnlySpan characterName, Guid collectionId, AnimationInvocationType type) { var accessor = GetCurrentLineLocking(); lock (accessor) @@ -53,10 +53,10 @@ internal sealed class AnimationInvocationBuffer : MemoryMappedBuffer, IAnimation accessor.Write(8, Environment.CurrentManagedThreadId); accessor.Write(12, (int)type); accessor.Write(16, characterAddress); - var span = GetSpan(accessor, 24, 104); + var span = GetSpan(accessor, 24, 16); + collectionId.TryWriteBytes(span); + span = GetSpan(accessor, 40); WriteSpan(characterName, span); - span = GetSpan(accessor, 128); - WriteString(collectionName, span); } } @@ -68,13 +68,13 @@ internal sealed class AnimationInvocationBuffer : MemoryMappedBuffer, IAnimation var lineCount = (int)CurrentLineCount; for (var i = lineCount - 1; i >= 0; --i) { - var line = GetLine(i); - var timestamp = DateTimeOffset.FromUnixTimeMilliseconds(BitConverter.ToInt64(line)); - var thread = BitConverter.ToInt32(line[8..]); - var type = (AnimationInvocationType)BitConverter.ToInt32(line[12..]); - var address = BitConverter.ToUInt64(line[16..]); - var characterName = ReadString(line[24..]); - var collectionName = ReadString(line[128..]); + var line = GetLine(i); + var timestamp = DateTimeOffset.FromUnixTimeMilliseconds(BitConverter.ToInt64(line)); + var thread = BitConverter.ToInt32(line[8..]); + var type = (AnimationInvocationType)BitConverter.ToInt32(line[12..]); + var address = BitConverter.ToUInt64(line[16..]); + var collectionId = new Guid(line[24..40]); + var characterName = ReadString(line[40..]); yield return new JsonObject() { [nameof(VfxFuncInvokedEntry.Age)] = (crashTime - timestamp).TotalSeconds, @@ -83,7 +83,7 @@ internal sealed class AnimationInvocationBuffer : MemoryMappedBuffer, IAnimation [nameof(VfxFuncInvokedEntry.InvocationType)] = ToName(type), [nameof(VfxFuncInvokedEntry.CharacterName)] = characterName, [nameof(VfxFuncInvokedEntry.CharacterAddress)] = address.ToString("X"), - [nameof(VfxFuncInvokedEntry.CollectionName)] = collectionName, + [nameof(VfxFuncInvokedEntry.CollectionId)] = collectionId, }; } } diff --git a/Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs b/Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs index d83c6e6c..4036455d 100644 --- a/Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs +++ b/Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs @@ -8,8 +8,8 @@ public interface ICharacterBaseBufferWriter /// Write a line into the buffer with the given data. /// The address of the related character, if known. /// The name of the related character, anonymized or relying on index if unavailable, if known. - /// The name of the associated collection. Not anonymized. - public void WriteLine(nint characterAddress, ReadOnlySpan characterName, string collectionName); + /// The GUID of the associated collection. + public void WriteLine(nint characterAddress, ReadOnlySpan characterName, Guid collectionId); } /// The full crash entry for a loaded character base. @@ -19,27 +19,27 @@ public record struct CharacterLoadedEntry( int ThreadId, string CharacterName, string CharacterAddress, - string CollectionName) : ICrashDataEntry; + Guid CollectionId) : ICrashDataEntry; internal sealed class CharacterBaseBuffer : MemoryMappedBuffer, ICharacterBaseBufferWriter, IBufferReader { - private const int _version = 1; - private const int _lineCount = 10; - private const int _lineCapacity = 256; - private const string _name = "Penumbra.CharacterBase"; + private const int _version = 1; + private const int _lineCount = 10; + private const int _lineCapacity = 128; + private const string _name = "Penumbra.CharacterBase"; - public void WriteLine(nint characterAddress, ReadOnlySpan characterName, string collectionName) + public void WriteLine(nint characterAddress, ReadOnlySpan characterName, Guid collectionId) { var accessor = GetCurrentLineLocking(); lock (accessor) { - accessor.Write(0, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()); - accessor.Write(8, Environment.CurrentManagedThreadId); + accessor.Write(0, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()); + accessor.Write(8, Environment.CurrentManagedThreadId); accessor.Write(12, characterAddress); - var span = GetSpan(accessor, 20, 108); + var span = GetSpan(accessor, 20, 16); + collectionId.TryWriteBytes(span); + span = GetSpan(accessor, 36); WriteSpan(characterName, span); - span = GetSpan(accessor, 128); - WriteString(collectionName, span); } } @@ -48,20 +48,20 @@ internal sealed class CharacterBaseBuffer : MemoryMappedBuffer, ICharacterBaseBu var lineCount = (int)CurrentLineCount; for (var i = lineCount - 1; i >= 0; --i) { - var line = GetLine(i); - var timestamp = DateTimeOffset.FromUnixTimeMilliseconds(BitConverter.ToInt64(line)); - var thread = BitConverter.ToInt32(line[8..]); - var address = BitConverter.ToUInt64(line[12..]); - var characterName = ReadString(line[20..]); - var collectionName = ReadString(line[128..]); + var line = GetLine(i); + var timestamp = DateTimeOffset.FromUnixTimeMilliseconds(BitConverter.ToInt64(line)); + var thread = BitConverter.ToInt32(line[8..]); + var address = BitConverter.ToUInt64(line[12..]); + var collectionId = new Guid(line[20..36]); + var characterName = ReadString(line[36..]); yield return new JsonObject { - [nameof(CharacterLoadedEntry.Age)] = (crashTime - timestamp).TotalSeconds, - [nameof(CharacterLoadedEntry.Timestamp)] = timestamp, - [nameof(CharacterLoadedEntry.ThreadId)] = thread, - [nameof(CharacterLoadedEntry.CharacterName)] = characterName, + [nameof(CharacterLoadedEntry.Age)] = (crashTime - timestamp).TotalSeconds, + [nameof(CharacterLoadedEntry.Timestamp)] = timestamp, + [nameof(CharacterLoadedEntry.ThreadId)] = thread, + [nameof(CharacterLoadedEntry.CharacterName)] = characterName, [nameof(CharacterLoadedEntry.CharacterAddress)] = address.ToString("X"), - [nameof(CharacterLoadedEntry.CollectionName)] = collectionName, + [nameof(CharacterLoadedEntry.CollectionId)] = collectionId, }; } } diff --git a/Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs b/Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs index 6c774e4b..03f63ba4 100644 --- a/Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs +++ b/Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs @@ -8,10 +8,10 @@ public interface IModdedFileBufferWriter /// Write a line into the buffer with the given data. /// The address of the related character, if known. /// The name of the related character, anonymized or relying on index if unavailable, if known. - /// The name of the associated collection. Not anonymized. + /// The GUID of the associated collection. /// The file name as requested by the game. /// The actual modded file name loaded. - public void WriteLine(nint characterAddress, ReadOnlySpan characterName, string collectionName, ReadOnlySpan requestedFileName, + public void WriteLine(nint characterAddress, ReadOnlySpan characterName, Guid collectionId, ReadOnlySpan requestedFileName, ReadOnlySpan actualFileName); } @@ -22,33 +22,33 @@ public record struct ModdedFileLoadedEntry( int ThreadId, string CharacterName, string CharacterAddress, - string CollectionName, + Guid CollectionId, string RequestedFileName, string ActualFileName) : ICrashDataEntry; internal sealed class ModdedFileBuffer : MemoryMappedBuffer, IModdedFileBufferWriter, IBufferReader { - private const int _version = 1; - private const int _lineCount = 128; - private const int _lineCapacity = 1024; - private const string _name = "Penumbra.ModdedFile"; + private const int _version = 1; + private const int _lineCount = 128; + private const int _lineCapacity = 1024; + private const string _name = "Penumbra.ModdedFile"; - public void WriteLine(nint characterAddress, ReadOnlySpan characterName, string collectionName, ReadOnlySpan requestedFileName, + public void WriteLine(nint characterAddress, ReadOnlySpan characterName, Guid collectionId, ReadOnlySpan requestedFileName, ReadOnlySpan actualFileName) { var accessor = GetCurrentLineLocking(); lock (accessor) { - accessor.Write(0, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()); - accessor.Write(8, Environment.CurrentManagedThreadId); + accessor.Write(0, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()); + accessor.Write(8, Environment.CurrentManagedThreadId); accessor.Write(12, characterAddress); - var span = GetSpan(accessor, 20, 80); + var span = GetSpan(accessor, 20, 16); + collectionId.TryWriteBytes(span); + span = GetSpan(accessor, 36, 80); WriteSpan(characterName, span); - span = GetSpan(accessor, 92, 80); - WriteString(collectionName, span); - span = GetSpan(accessor, 172, 260); + span = GetSpan(accessor, 116, 260); WriteSpan(requestedFileName, span); - span = GetSpan(accessor, 432); + span = GetSpan(accessor, 376); WriteSpan(actualFileName, span); } } @@ -61,24 +61,24 @@ internal sealed class ModdedFileBuffer : MemoryMappedBuffer, IModdedFileBufferWr var lineCount = (int)CurrentLineCount; for (var i = lineCount - 1; i >= 0; --i) { - var line = GetLine(i); - var timestamp = DateTimeOffset.FromUnixTimeMilliseconds(BitConverter.ToInt64(line)); - var thread = BitConverter.ToInt32(line[8..]); - var address = BitConverter.ToUInt64(line[12..]); - var characterName = ReadString(line[20..]); - var collectionName = ReadString(line[92..]); - var requestedFileName = ReadString(line[172..]); - var actualFileName = ReadString(line[432..]); + var line = GetLine(i); + var timestamp = DateTimeOffset.FromUnixTimeMilliseconds(BitConverter.ToInt64(line)); + var thread = BitConverter.ToInt32(line[8..]); + var address = BitConverter.ToUInt64(line[12..]); + var collectionId = new Guid(line[20..36]); + var characterName = ReadString(line[36..]); + var requestedFileName = ReadString(line[116..]); + var actualFileName = ReadString(line[376..]); yield return new JsonObject() { - [nameof(ModdedFileLoadedEntry.Age)] = (crashTime - timestamp).TotalSeconds, - [nameof(ModdedFileLoadedEntry.Timestamp)] = timestamp, - [nameof(ModdedFileLoadedEntry.ThreadId)] = thread, - [nameof(ModdedFileLoadedEntry.CharacterName)] = characterName, - [nameof(ModdedFileLoadedEntry.CharacterAddress)] = address.ToString("X"), - [nameof(ModdedFileLoadedEntry.CollectionName)] = collectionName, + [nameof(ModdedFileLoadedEntry.Age)] = (crashTime - timestamp).TotalSeconds, + [nameof(ModdedFileLoadedEntry.Timestamp)] = timestamp, + [nameof(ModdedFileLoadedEntry.ThreadId)] = thread, + [nameof(ModdedFileLoadedEntry.CharacterName)] = characterName, + [nameof(ModdedFileLoadedEntry.CharacterAddress)] = address.ToString("X"), + [nameof(ModdedFileLoadedEntry.CollectionId)] = collectionId, [nameof(ModdedFileLoadedEntry.RequestedFileName)] = requestedFileName, - [nameof(ModdedFileLoadedEntry.ActualFileName)] = actualFileName, + [nameof(ModdedFileLoadedEntry.ActualFileName)] = actualFileName, }; } } diff --git a/Penumbra.GameData b/Penumbra.GameData index 45679aa3..60222d79 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 45679aa32cc37b59f5eeb7cf6bf5a3ea36c626e0 +Subproject commit 60222d79420662fb8e9960a66e262a380fcaf186 diff --git a/Penumbra/Api/Api/ApiHelpers.cs b/Penumbra/Api/Api/ApiHelpers.cs new file mode 100644 index 00000000..32a3956f --- /dev/null +++ b/Penumbra/Api/Api/ApiHelpers.cs @@ -0,0 +1,76 @@ +using OtterGui.Log; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Collections; +using Penumbra.Collections.Manager; +using Penumbra.GameData.Actors; +using Penumbra.GameData.Interop; +using Penumbra.Interop.PathResolving; + +namespace Penumbra.Api.Api; + +public class ApiHelpers( + CollectionManager collectionManager, + ObjectManager objects, + CollectionResolver collectionResolver, + ActorManager actors) : IApiService +{ + /// Return the associated identifier for an object given by its index. + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + internal ActorIdentifier AssociatedIdentifier(int gameObjectIdx) + { + if (gameObjectIdx < 0 || gameObjectIdx >= objects.TotalCount) + return ActorIdentifier.Invalid; + + var ptr = objects[gameObjectIdx]; + return actors.FromObject(ptr, out _, false, true, true); + } + + /// + /// Return the collection associated to a current game object. If it does not exist, return the default collection. + /// If the index is invalid, returns false and the default collection. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + internal unsafe bool AssociatedCollection(int gameObjectIdx, out ModCollection collection) + { + collection = collectionManager.Active.Default; + if (gameObjectIdx < 0 || gameObjectIdx >= objects.TotalCount) + return false; + + var ptr = objects[gameObjectIdx]; + var data = collectionResolver.IdentifyCollection(ptr.AsObject, false); + if (data.Valid) + collection = data.ModCollection; + + return true; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + internal static PenumbraApiEc Return(PenumbraApiEc ec, LazyString args, [CallerMemberName] string name = "Unknown") + { + Penumbra.Log.Debug( + $"[{name}] Called with {args}, returned {ec}."); + return ec; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + internal static LazyString Args(params object[] arguments) + { + if (arguments.Length == 0) + return new LazyString(() => "no arguments"); + + return new LazyString(() => + { + var sb = new StringBuilder(); + for (var i = 0; i < arguments.Length / 2; ++i) + { + sb.Append(arguments[2 * i]); + sb.Append(" = "); + sb.Append(arguments[2 * i + 1]); + sb.Append(", "); + } + + return sb.ToString(0, sb.Length - 2); + }); + } +} diff --git a/Penumbra/Api/Api/CollectionApi.cs b/Penumbra/Api/Api/CollectionApi.cs new file mode 100644 index 00000000..de704460 --- /dev/null +++ b/Penumbra/Api/Api/CollectionApi.cs @@ -0,0 +1,141 @@ +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Collections; +using Penumbra.Collections.Manager; + +namespace Penumbra.Api.Api; + +public class CollectionApi(CollectionManager collections, ApiHelpers helpers) : IPenumbraApiCollection, IApiService +{ + public Dictionary GetCollections() + => collections.Storage.ToDictionary(c => c.Id, c => c.Name); + + public Dictionary GetChangedItemsForCollection(Guid collectionId) + { + try + { + if (!collections.Storage.ById(collectionId, out var collection)) + collection = ModCollection.Empty; + + if (collection.HasCache) + return collection.ChangedItems.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Item2); + + Penumbra.Log.Warning($"Collection {collectionId} does not exist or is not loaded."); + return []; + } + catch (Exception e) + { + Penumbra.Log.Error($"Could not obtain Changed Items for {collectionId}:\n{e}"); + throw; + } + } + + public (Guid Id, string Name)? GetCollection(ApiCollectionType type) + { + if (!Enum.IsDefined(type)) + return null; + + var collection = collections.Active.ByType((CollectionType)type); + return collection == null ? null : (collection.Id, collection.Name); + } + + internal (Guid Id, string Name)? GetCollection(byte type) + => GetCollection((ApiCollectionType)type); + + public (bool ObjectValid, bool IndividualSet, (Guid Id, string Name) EffectiveCollection) GetCollectionForObject(int gameObjectIdx) + { + var id = helpers.AssociatedIdentifier(gameObjectIdx); + if (!id.IsValid) + return (false, false, (collections.Active.Default.Id, collections.Active.Default.Name)); + + if (collections.Active.Individuals.TryGetValue(id, out var collection)) + return (true, true, (collection.Id, collection.Name)); + + helpers.AssociatedCollection(gameObjectIdx, out collection); + return (true, false, (collection.Id, collection.Name)); + } + + public Guid[] GetCollectionByName(string name) + => collections.Storage.Where(c => string.Equals(name, c.Name, StringComparison.OrdinalIgnoreCase)).Select(c => c.Id).ToArray(); + + public (PenumbraApiEc, (Guid Id, string Name)? OldCollection) SetCollection(ApiCollectionType type, Guid? collectionId, + bool allowCreateNew, bool allowDelete) + { + if (!Enum.IsDefined(type)) + return (PenumbraApiEc.InvalidArgument, null); + + var oldCollection = collections.Active.ByType((CollectionType)type); + var old = oldCollection != null ? (oldCollection.Id, oldCollection.Name) : new ValueTuple?(); + if (collectionId == null) + { + if (old == null) + return (PenumbraApiEc.NothingChanged, old); + + if (!allowDelete || type is ApiCollectionType.Current or ApiCollectionType.Default or ApiCollectionType.Interface) + return (PenumbraApiEc.AssignmentDeletionDisallowed, old); + + collections.Active.RemoveSpecialCollection((CollectionType)type); + return (PenumbraApiEc.Success, old); + } + + if (!collections.Storage.ById(collectionId.Value, out var collection)) + return (PenumbraApiEc.CollectionMissing, old); + + if (old == null) + { + if (!allowCreateNew) + return (PenumbraApiEc.AssignmentCreationDisallowed, old); + + collections.Active.CreateSpecialCollection((CollectionType)type); + } + else if (old.Value.Item1 == collection.Id) + { + return (PenumbraApiEc.NothingChanged, old); + } + + collections.Active.SetCollection(collection, (CollectionType)type); + return (PenumbraApiEc.Success, old); + } + + public (PenumbraApiEc, (Guid Id, string Name)? OldCollection) SetCollectionForObject(int gameObjectIdx, Guid? collectionId, + bool allowCreateNew, bool allowDelete) + { + var id = helpers.AssociatedIdentifier(gameObjectIdx); + if (!id.IsValid) + return (PenumbraApiEc.InvalidIdentifier, (collections.Active.Default.Id, collections.Active.Default.Name)); + + var oldCollection = collections.Active.Individuals.TryGetValue(id, out var c) ? c : null; + var old = oldCollection != null ? (oldCollection.Id, oldCollection.Name) : new ValueTuple?(); + if (collectionId == null) + { + if (old == null) + return (PenumbraApiEc.NothingChanged, old); + + if (!allowDelete) + return (PenumbraApiEc.AssignmentDeletionDisallowed, old); + + var idx = collections.Active.Individuals.Index(id); + collections.Active.RemoveIndividualCollection(idx); + return (PenumbraApiEc.Success, old); + } + + if (!collections.Storage.ById(collectionId.Value, out var collection)) + return (PenumbraApiEc.CollectionMissing, old); + + if (old == null) + { + if (!allowCreateNew) + return (PenumbraApiEc.AssignmentCreationDisallowed, old); + + var ids = collections.Active.Individuals.GetGroup(id); + collections.Active.CreateIndividualCollection(ids); + } + else if (old.Value.Item1 == collection.Id) + { + return (PenumbraApiEc.NothingChanged, old); + } + + collections.Active.SetCollection(collection, CollectionType.Individual, collections.Active.Individuals.Index(id)); + return (PenumbraApiEc.Success, old); + } +} diff --git a/Penumbra/Api/Api/EditingApi.cs b/Penumbra/Api/Api/EditingApi.cs new file mode 100644 index 00000000..93345053 --- /dev/null +++ b/Penumbra/Api/Api/EditingApi.cs @@ -0,0 +1,40 @@ +using OtterGui.Services; +using Penumbra.Import.Textures; +using TextureType = Penumbra.Api.Enums.TextureType; + +namespace Penumbra.Api.Api; + +public class EditingApi(TextureManager textureManager) : IPenumbraApiEditing, IApiService +{ + public Task ConvertTextureFile(string inputFile, string outputFile, TextureType textureType, bool mipMaps) + => textureType switch + { + TextureType.Png => textureManager.SavePng(inputFile, outputFile), + TextureType.AsIsTex => textureManager.SaveAs(CombinedTexture.TextureSaveType.AsIs, mipMaps, true, inputFile, outputFile), + TextureType.AsIsDds => textureManager.SaveAs(CombinedTexture.TextureSaveType.AsIs, mipMaps, false, inputFile, outputFile), + TextureType.RgbaTex => textureManager.SaveAs(CombinedTexture.TextureSaveType.Bitmap, mipMaps, true, inputFile, outputFile), + TextureType.RgbaDds => textureManager.SaveAs(CombinedTexture.TextureSaveType.Bitmap, mipMaps, false, inputFile, outputFile), + TextureType.Bc3Tex => textureManager.SaveAs(CombinedTexture.TextureSaveType.BC3, mipMaps, true, inputFile, outputFile), + TextureType.Bc3Dds => textureManager.SaveAs(CombinedTexture.TextureSaveType.BC3, mipMaps, false, inputFile, outputFile), + TextureType.Bc7Tex => textureManager.SaveAs(CombinedTexture.TextureSaveType.BC7, mipMaps, true, inputFile, outputFile), + TextureType.Bc7Dds => textureManager.SaveAs(CombinedTexture.TextureSaveType.BC7, mipMaps, false, inputFile, outputFile), + _ => Task.FromException(new Exception($"Invalid input value {textureType}.")), + }; + + // @formatter:off + public Task ConvertTextureData(byte[] rgbaData, int width, string outputFile, TextureType textureType, bool mipMaps) + => textureType switch + { + TextureType.Png => textureManager.SavePng(new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), + TextureType.AsIsTex => textureManager.SaveAs(CombinedTexture.TextureSaveType.AsIs, mipMaps, true, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), + TextureType.AsIsDds => textureManager.SaveAs(CombinedTexture.TextureSaveType.AsIs, mipMaps, false, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), + TextureType.RgbaTex => textureManager.SaveAs(CombinedTexture.TextureSaveType.Bitmap, mipMaps, true, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), + TextureType.RgbaDds => textureManager.SaveAs(CombinedTexture.TextureSaveType.Bitmap, mipMaps, false, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), + TextureType.Bc3Tex => textureManager.SaveAs(CombinedTexture.TextureSaveType.BC3, mipMaps, true, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), + TextureType.Bc3Dds => textureManager.SaveAs(CombinedTexture.TextureSaveType.BC3, mipMaps, false, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), + TextureType.Bc7Tex => textureManager.SaveAs(CombinedTexture.TextureSaveType.BC7, mipMaps, true, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), + TextureType.Bc7Dds => textureManager.SaveAs(CombinedTexture.TextureSaveType.BC7, mipMaps, false, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), + _ => Task.FromException(new Exception($"Invalid input value {textureType}.")), + }; + // @formatter:on +} diff --git a/Penumbra/Api/Api/GameStateApi.cs b/Penumbra/Api/Api/GameStateApi.cs new file mode 100644 index 00000000..becb55ee --- /dev/null +++ b/Penumbra/Api/Api/GameStateApi.cs @@ -0,0 +1,82 @@ +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Collections; +using Penumbra.Interop.PathResolving; +using Penumbra.Interop.ResourceLoading; +using Penumbra.Interop.Structs; +using Penumbra.Services; +using Penumbra.String.Classes; + +namespace Penumbra.Api.Api; + +public class GameStateApi : IPenumbraApiGameState, IApiService, IDisposable +{ + private readonly CommunicatorService _communicator; + private readonly CollectionResolver _collectionResolver; + private readonly CutsceneService _cutsceneService; + private readonly ResourceLoader _resourceLoader; + + public unsafe GameStateApi(CommunicatorService communicator, CollectionResolver collectionResolver, CutsceneService cutsceneService, + ResourceLoader resourceLoader) + { + _communicator = communicator; + _collectionResolver = collectionResolver; + _cutsceneService = cutsceneService; + _resourceLoader = resourceLoader; + _resourceLoader.ResourceLoaded += OnResourceLoaded; + _communicator.CreatedCharacterBase.Subscribe(OnCreatedCharacterBase, Communication.CreatedCharacterBase.Priority.Api); + } + + public unsafe void Dispose() + { + _resourceLoader.ResourceLoaded -= OnResourceLoaded; + _communicator.CreatedCharacterBase.Unsubscribe(OnCreatedCharacterBase); + } + + public event CreatedCharacterBaseDelegate? CreatedCharacterBase; + public event GameObjectResourceResolvedDelegate? GameObjectResourceResolved; + + public event CreatingCharacterBaseDelegate? CreatingCharacterBase + { + add + { + if (value == null) + return; + + _communicator.CreatingCharacterBase.Subscribe(new Action(value), + Communication.CreatingCharacterBase.Priority.Api); + } + remove + { + if (value == null) + return; + + _communicator.CreatingCharacterBase.Unsubscribe(new Action(value)); + } + } + + public unsafe (nint GameObject, (Guid Id, string Name) Collection) GetDrawObjectInfo(nint drawObject) + { + var data = _collectionResolver.IdentifyCollection((DrawObject*)drawObject, true); + return (data.AssociatedGameObject, (data.ModCollection.Id, data.ModCollection.Name)); + } + + public int GetCutsceneParentIndex(int actorIdx) + => _cutsceneService.GetParentIndex(actorIdx); + + public PenumbraApiEc SetCutsceneParentIndex(int copyIdx, int newParentIdx) + => _cutsceneService.SetParentIndex(copyIdx, newParentIdx) + ? PenumbraApiEc.Success + : PenumbraApiEc.InvalidArgument; + + private unsafe void OnResourceLoaded(ResourceHandle* handle, Utf8GamePath originalPath, FullPath? manipulatedPath, ResolveData resolveData) + { + if (resolveData.AssociatedGameObject != nint.Zero) + GameObjectResourceResolved?.Invoke(resolveData.AssociatedGameObject, originalPath.ToString(), + manipulatedPath?.ToString() ?? originalPath.ToString()); + } + + private void OnCreatedCharacterBase(nint gameObject, ModCollection collection, nint drawObject) + => CreatedCharacterBase?.Invoke(gameObject, collection.Id, drawObject); +} diff --git a/Penumbra/Api/Api/MetaApi.cs b/Penumbra/Api/Api/MetaApi.cs new file mode 100644 index 00000000..c467df58 --- /dev/null +++ b/Penumbra/Api/Api/MetaApi.cs @@ -0,0 +1,23 @@ +using OtterGui; +using OtterGui.Services; +using Penumbra.Interop.PathResolving; +using Penumbra.Meta.Manipulations; + +namespace Penumbra.Api.Api; + +public class MetaApi(CollectionResolver collectionResolver, ApiHelpers helpers) : IPenumbraApiMeta, IApiService +{ + public string GetPlayerMetaManipulations() + { + var collection = collectionResolver.PlayerCollection(); + var set = collection.MetaCache?.Manipulations.ToArray() ?? []; + return Functions.ToCompressedBase64(set, MetaManipulation.CurrentVersion); + } + + public string GetMetaManipulations(int gameObjectIdx) + { + helpers.AssociatedCollection(gameObjectIdx, out var collection); + var set = collection.MetaCache?.Manipulations.ToArray() ?? []; + return Functions.ToCompressedBase64(set, MetaManipulation.CurrentVersion); + } +} diff --git a/Penumbra/Api/Api/ModSettingsApi.cs b/Penumbra/Api/Api/ModSettingsApi.cs new file mode 100644 index 00000000..2604a49d --- /dev/null +++ b/Penumbra/Api/Api/ModSettingsApi.cs @@ -0,0 +1,282 @@ +using OtterGui; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Api.Helpers; +using Penumbra.Collections; +using Penumbra.Collections.Manager; +using Penumbra.Communication; +using Penumbra.Interop.PathResolving; +using Penumbra.Mods; +using Penumbra.Mods.Editor; +using Penumbra.Mods.Manager; +using Penumbra.Mods.Subclasses; +using Penumbra.Services; + +namespace Penumbra.Api.Api; + +public class ModSettingsApi : IPenumbraApiModSettings, IApiService, IDisposable +{ + private readonly CollectionResolver _collectionResolver; + private readonly ModManager _modManager; + private readonly CollectionManager _collectionManager; + private readonly CollectionEditor _collectionEditor; + private readonly CommunicatorService _communicator; + + public ModSettingsApi(CollectionResolver collectionResolver, + ModManager modManager, + CollectionManager collectionManager, + CollectionEditor collectionEditor, + CommunicatorService communicator) + { + _collectionResolver = collectionResolver; + _modManager = modManager; + _collectionManager = collectionManager; + _collectionEditor = collectionEditor; + _communicator = communicator; + _communicator.ModPathChanged.Subscribe(OnModPathChange, ModPathChanged.Priority.ApiModSettings); + _communicator.ModSettingChanged.Subscribe(OnModSettingChange, Communication.ModSettingChanged.Priority.Api); + _communicator.ModOptionChanged.Subscribe(OnModOptionEdited, ModOptionChanged.Priority.Api); + _communicator.ModFileChanged.Subscribe(OnModFileChanged, ModFileChanged.Priority.Api); + } + + public void Dispose() + { + _communicator.ModPathChanged.Unsubscribe(OnModPathChange); + _communicator.ModSettingChanged.Unsubscribe(OnModSettingChange); + _communicator.ModOptionChanged.Unsubscribe(OnModOptionEdited); + _communicator.ModFileChanged.Unsubscribe(OnModFileChanged); + } + + public event ModSettingChangedDelegate? ModSettingChanged; + + public AvailableModSettings? GetAvailableModSettings(string modDirectory, string modName) + { + if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) + return null; + + var dict = new Dictionary(mod.Groups.Count); + foreach (var g in mod.Groups) + dict.Add(g.Name, (g.Select(o => o.Name).ToArray(), (int)g.Type)); + return new AvailableModSettings(dict); + } + + public Dictionary? GetAvailableModSettingsBase(string modDirectory, string modName) + => _modManager.TryGetMod(modDirectory, modName, out var mod) + ? mod.Groups.ToDictionary(g => g.Name, g => (g.Select(o => o.Name).ToArray(), (int)g.Type)) + : null; + + public (PenumbraApiEc, (bool, int, Dictionary>, bool)?) GetCurrentModSettings(Guid collectionId, string modDirectory, + string modName, bool ignoreInheritance) + { + if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) + return (PenumbraApiEc.ModMissing, null); + + if (!_collectionManager.Storage.ById(collectionId, out var collection)) + return (PenumbraApiEc.CollectionMissing, null); + + var settings = collection.Id == Guid.Empty + ? null + : ignoreInheritance + ? collection.Settings[mod.Index] + : collection[mod.Index].Settings; + if (settings == null) + return (PenumbraApiEc.Success, null); + + var (enabled, priority, dict) = settings.ConvertToShareable(mod); + return (PenumbraApiEc.Success, + (enabled, priority.Value, dict, collection.Settings[mod.Index] == null)); + } + + public PenumbraApiEc TryInheritMod(Guid collectionId, string modDirectory, string modName, bool inherit) + { + var args = ApiHelpers.Args("CollectionId", collectionId, "ModDirectory", modDirectory, "ModName", modName, "Inherit", + inherit.ToString()); + + if (collectionId == Guid.Empty) + return ApiHelpers.Return(PenumbraApiEc.InvalidArgument, args); + + if (!_collectionManager.Storage.ById(collectionId, out var collection)) + return ApiHelpers.Return(PenumbraApiEc.CollectionMissing, args); + + if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) + return ApiHelpers.Return(PenumbraApiEc.ModMissing, args); + + var ret = _collectionEditor.SetModInheritance(collection, mod, inherit) + ? PenumbraApiEc.Success + : PenumbraApiEc.NothingChanged; + return ApiHelpers.Return(ret, args); + } + + public PenumbraApiEc TrySetMod(Guid collectionId, string modDirectory, string modName, bool enabled) + { + var args = ApiHelpers.Args("CollectionId", collectionId, "ModDirectory", modDirectory, "ModName", modName, "Enabled", enabled); + if (!_collectionManager.Storage.ById(collectionId, out var collection)) + return ApiHelpers.Return(PenumbraApiEc.CollectionMissing, args); + + if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) + return ApiHelpers.Return(PenumbraApiEc.ModMissing, args); + + var ret = _collectionEditor.SetModState(collection, mod, enabled) + ? PenumbraApiEc.Success + : PenumbraApiEc.NothingChanged; + return ApiHelpers.Return(ret, args); + } + + public PenumbraApiEc TrySetModPriority(Guid collectionId, string modDirectory, string modName, int priority) + { + var args = ApiHelpers.Args("CollectionId", collectionId, "ModDirectory", modDirectory, "ModName", modName, "Priority", priority); + + if (!_collectionManager.Storage.ById(collectionId, out var collection)) + return ApiHelpers.Return(PenumbraApiEc.CollectionMissing, args); + + if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) + return ApiHelpers.Return(PenumbraApiEc.ModMissing, args); + + var ret = _collectionEditor.SetModPriority(collection, mod, new ModPriority(priority)) + ? PenumbraApiEc.Success + : PenumbraApiEc.NothingChanged; + return ApiHelpers.Return(ret, args); + } + + public PenumbraApiEc TrySetModSetting(Guid collectionId, string modDirectory, string modName, string optionGroupName, string optionName) + { + var args = ApiHelpers.Args("CollectionId", collectionId, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", + optionGroupName, "OptionName", optionName); + + if (!_collectionManager.Storage.ById(collectionId, out var collection)) + return ApiHelpers.Return(PenumbraApiEc.CollectionMissing, args); + + if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) + return ApiHelpers.Return(PenumbraApiEc.ModMissing, args); + + var groupIdx = mod.Groups.IndexOf(g => g.Name == optionGroupName); + if (groupIdx < 0) + return ApiHelpers.Return(PenumbraApiEc.OptionGroupMissing, args); + + var optionIdx = mod.Groups[groupIdx].IndexOf(o => o.Name == optionName); + if (optionIdx < 0) + return ApiHelpers.Return(PenumbraApiEc.OptionMissing, args); + + var setting = mod.Groups[groupIdx] switch + { + MultiModGroup => Setting.Multi(optionIdx), + SingleModGroup => Setting.Single(optionIdx), + _ => Setting.Zero, + }; + var ret = _collectionEditor.SetModSetting(collection, mod, groupIdx, setting) + ? PenumbraApiEc.Success + : PenumbraApiEc.NothingChanged; + return ApiHelpers.Return(ret, args); + } + + public PenumbraApiEc TrySetModSettings(Guid collectionId, string modDirectory, string modName, string optionGroupName, + IReadOnlyList optionNames) + { + var args = ApiHelpers.Args("CollectionId", collectionId, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", + optionGroupName, "#optionNames", optionNames.Count); + + if (!_collectionManager.Storage.ById(collectionId, out var collection)) + return ApiHelpers.Return(PenumbraApiEc.CollectionMissing, args); + + if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) + return ApiHelpers.Return(PenumbraApiEc.ModMissing, args); + + var groupIdx = mod.Groups.IndexOf(g => g.Name == optionGroupName); + if (groupIdx < 0) + return ApiHelpers.Return(PenumbraApiEc.OptionGroupMissing, args); + + var setting = Setting.Zero; + switch (mod.Groups[groupIdx]) + { + case SingleModGroup single: + { + var optionIdx = optionNames.Count == 0 ? -1 : single.IndexOf(o => o.Name == optionNames[^1]); + if (optionIdx < 0) + return ApiHelpers.Return(PenumbraApiEc.OptionMissing, args); + + setting = Setting.Single(optionIdx); + break; + } + case MultiModGroup multi: + { + foreach (var name in optionNames) + { + var optionIdx = multi.IndexOf(o => o.Name == name); + if (optionIdx < 0) + return ApiHelpers.Return(PenumbraApiEc.OptionMissing, args); + + setting |= Setting.Multi(optionIdx); + } + + break; + } + } + + var ret = _collectionEditor.SetModSetting(collection, mod, groupIdx, setting) + ? PenumbraApiEc.Success + : PenumbraApiEc.NothingChanged; + return ApiHelpers.Return(ret, args); + } + + public PenumbraApiEc CopyModSettings(Guid? collectionId, string modDirectoryFrom, string modDirectoryTo) + { + var args = ApiHelpers.Args("CollectionId", collectionId.HasValue ? collectionId.Value.ToString() : "NULL", + "From", modDirectoryFrom, "To", modDirectoryTo); + var sourceMod = _modManager.FirstOrDefault(m => string.Equals(m.ModPath.Name, modDirectoryFrom, StringComparison.OrdinalIgnoreCase)); + var targetMod = _modManager.FirstOrDefault(m => string.Equals(m.ModPath.Name, modDirectoryTo, StringComparison.OrdinalIgnoreCase)); + if (collectionId == null) + foreach (var collection in _collectionManager.Storage) + _collectionEditor.CopyModSettings(collection, sourceMod, modDirectoryFrom, targetMod, modDirectoryTo); + else if (_collectionManager.Storage.ById(collectionId.Value, out var collection)) + _collectionEditor.CopyModSettings(collection, sourceMod, modDirectoryFrom, targetMod, modDirectoryTo); + else + return ApiHelpers.Return(PenumbraApiEc.CollectionMissing, args); + + return ApiHelpers.Return(PenumbraApiEc.Success, args); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + private void TriggerSettingEdited(Mod mod) + { + var collection = _collectionResolver.PlayerCollection(); + var (settings, parent) = collection[mod.Index]; + if (settings is { Enabled: true }) + ModSettingChanged?.Invoke(ModSettingChange.Edited, collection.Id, mod.Identifier, parent != collection); + } + + private void OnModPathChange(ModPathChangeType type, Mod mod, DirectoryInfo? _1, DirectoryInfo? _2) + { + if (type == ModPathChangeType.Reloaded) + TriggerSettingEdited(mod); + } + + private void OnModSettingChange(ModCollection collection, ModSettingChange type, Mod? mod, Setting _1, int _2, bool inherited) + => ModSettingChanged?.Invoke(type, collection.Id, mod?.ModPath.Name ?? string.Empty, inherited); + + private void OnModOptionEdited(ModOptionChangeType type, Mod mod, int groupIndex, int optionIndex, int moveIndex) + { + switch (type) + { + case ModOptionChangeType.GroupDeleted: + case ModOptionChangeType.GroupMoved: + case ModOptionChangeType.GroupTypeChanged: + case ModOptionChangeType.PriorityChanged: + case ModOptionChangeType.OptionDeleted: + case ModOptionChangeType.OptionMoved: + case ModOptionChangeType.OptionFilesChanged: + case ModOptionChangeType.OptionFilesAdded: + case ModOptionChangeType.OptionSwapsChanged: + case ModOptionChangeType.OptionMetaChanged: + TriggerSettingEdited(mod); + break; + } + } + + private void OnModFileChanged(Mod mod, FileRegistry file) + { + if (file.CurrentUsage == 0) + return; + + TriggerSettingEdited(mod); + } +} diff --git a/Penumbra/Api/Api/ModsApi.cs b/Penumbra/Api/Api/ModsApi.cs new file mode 100644 index 00000000..c1e0c684 --- /dev/null +++ b/Penumbra/Api/Api/ModsApi.cs @@ -0,0 +1,132 @@ +using OtterGui.Compression; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Communication; +using Penumbra.Mods; +using Penumbra.Mods.Manager; +using Penumbra.Services; + +namespace Penumbra.Api.Api; + +public class ModsApi : IPenumbraApiMods, IApiService, IDisposable +{ + private readonly CommunicatorService _communicator; + private readonly ModManager _modManager; + private readonly ModImportManager _modImportManager; + private readonly Configuration _config; + private readonly ModFileSystem _modFileSystem; + + public ModsApi(ModManager modManager, ModImportManager modImportManager, Configuration config, ModFileSystem modFileSystem, + CommunicatorService communicator) + { + _modManager = modManager; + _modImportManager = modImportManager; + _config = config; + _modFileSystem = modFileSystem; + _communicator = communicator; + _communicator.ModPathChanged.Subscribe(OnModPathChanged, ModPathChanged.Priority.ApiMods); + } + + private void OnModPathChanged(ModPathChangeType type, Mod mod, DirectoryInfo? oldDirectory, DirectoryInfo? newDirectory) + { + switch (type) + { + case ModPathChangeType.Deleted when oldDirectory != null: + ModDeleted?.Invoke(oldDirectory.Name); + break; + case ModPathChangeType.Added when newDirectory != null: + ModAdded?.Invoke(newDirectory.Name); + break; + case ModPathChangeType.Moved when newDirectory != null && oldDirectory != null: + ModMoved?.Invoke(oldDirectory.Name, newDirectory.Name); + break; + } + } + + public void Dispose() + => _communicator.ModPathChanged.Unsubscribe(OnModPathChanged); + + public Dictionary GetModList() + => _modManager.ToDictionary(m => m.ModPath.Name, m => m.Name.Text); + + public PenumbraApiEc InstallMod(string modFilePackagePath) + { + if (!File.Exists(modFilePackagePath)) + return ApiHelpers.Return(PenumbraApiEc.FileMissing, ApiHelpers.Args("ModFilePackagePath", modFilePackagePath)); + + _modImportManager.AddUnpack(modFilePackagePath); + return ApiHelpers.Return(PenumbraApiEc.Success, ApiHelpers.Args("ModFilePackagePath", modFilePackagePath)); + } + + public PenumbraApiEc ReloadMod(string modDirectory, string modName) + { + if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) + return ApiHelpers.Return(PenumbraApiEc.ModMissing, ApiHelpers.Args("ModDirectory", modDirectory, "ModName", modName)); + + _modManager.ReloadMod(mod); + return ApiHelpers.Return(PenumbraApiEc.Success, ApiHelpers.Args("ModDirectory", modDirectory, "ModName", modName)); + } + + public PenumbraApiEc AddMod(string modDirectory) + { + var args = ApiHelpers.Args("ModDirectory", modDirectory); + + var dir = new DirectoryInfo(Path.Join(_modManager.BasePath.FullName, Path.GetFileName(modDirectory))); + if (!dir.Exists) + return ApiHelpers.Return(PenumbraApiEc.FileMissing, args); + + if (_modManager.BasePath.FullName != dir.Parent?.FullName) + return ApiHelpers.Return(PenumbraApiEc.InvalidArgument, args); + + _modManager.AddMod(dir); + if (_config.UseFileSystemCompression) + new FileCompactor(Penumbra.Log).StartMassCompact(dir.EnumerateFiles("*.*", SearchOption.AllDirectories), + CompressionAlgorithm.Xpress8K); + return ApiHelpers.Return(PenumbraApiEc.Success, args); + } + + public PenumbraApiEc DeleteMod(string modDirectory, string modName) + { + if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) + return ApiHelpers.Return(PenumbraApiEc.NothingChanged, ApiHelpers.Args("ModDirectory", modDirectory, "ModName", modName)); + + _modManager.DeleteMod(mod); + return ApiHelpers.Return(PenumbraApiEc.Success, ApiHelpers.Args("ModDirectory", modDirectory, "ModName", modName)); + } + + public event Action? ModDeleted; + public event Action? ModAdded; + public event Action? ModMoved; + + public (PenumbraApiEc, string, bool, bool) GetModPath(string modDirectory, string modName) + { + if (!_modManager.TryGetMod(modDirectory, modName, out var mod) + || !_modFileSystem.FindLeaf(mod, out var leaf)) + return (PenumbraApiEc.ModMissing, string.Empty, false, false); + + var fullPath = leaf.FullName(); + var isDefault = ModFileSystem.ModHasDefaultPath(mod, fullPath); + var isNameDefault = isDefault || ModFileSystem.ModHasDefaultPath(mod, leaf.Name); + return (PenumbraApiEc.Success, fullPath, !isDefault, !isNameDefault ); + } + + public PenumbraApiEc SetModPath(string modDirectory, string modName, string newPath) + { + if (newPath.Length == 0) + return PenumbraApiEc.InvalidArgument; + + if (!_modManager.TryGetMod(modDirectory, modName, out var mod) + || !_modFileSystem.FindLeaf(mod, out var leaf)) + return PenumbraApiEc.ModMissing; + + try + { + _modFileSystem.RenameAndMove(leaf, newPath); + return PenumbraApiEc.Success; + } + catch + { + return PenumbraApiEc.PathRenameFailed; + } + } +} diff --git a/Penumbra/Api/Api/PenumbraApi.cs b/Penumbra/Api/Api/PenumbraApi.cs new file mode 100644 index 00000000..1d5b1537 --- /dev/null +++ b/Penumbra/Api/Api/PenumbraApi.cs @@ -0,0 +1,40 @@ +using OtterGui.Services; + +namespace Penumbra.Api.Api; + +public class PenumbraApi( + CollectionApi collection, + EditingApi editing, + GameStateApi gameState, + MetaApi meta, + ModsApi mods, + ModSettingsApi modSettings, + PluginStateApi pluginState, + RedrawApi redraw, + ResolveApi resolve, + ResourceTreeApi resourceTree, + TemporaryApi temporary, + UiApi ui) : IDisposable, IApiService, IPenumbraApi +{ + public void Dispose() + { + Valid = false; + } + + public (int Breaking, int Feature) ApiVersion + => (5, 0); + + public bool Valid { get; private set; } = true; + public IPenumbraApiCollection Collection { get; } = collection; + public IPenumbraApiEditing Editing { get; } = editing; + public IPenumbraApiGameState GameState { get; } = gameState; + public IPenumbraApiMeta Meta { get; } = meta; + public IPenumbraApiMods Mods { get; } = mods; + public IPenumbraApiModSettings ModSettings { get; } = modSettings; + public IPenumbraApiPluginState PluginState { get; } = pluginState; + public IPenumbraApiRedraw Redraw { get; } = redraw; + public IPenumbraApiResolve Resolve { get; } = resolve; + public IPenumbraApiResourceTree ResourceTree { get; } = resourceTree; + public IPenumbraApiTemporary Temporary { get; } = temporary; + public IPenumbraApiUi Ui { get; } = ui; +} diff --git a/Penumbra/Api/Api/PluginStateApi.cs b/Penumbra/Api/Api/PluginStateApi.cs new file mode 100644 index 00000000..2e87486f --- /dev/null +++ b/Penumbra/Api/Api/PluginStateApi.cs @@ -0,0 +1,30 @@ +using Newtonsoft.Json; +using OtterGui.Services; +using Penumbra.Communication; +using Penumbra.Services; + +namespace Penumbra.Api.Api; + +public class PluginStateApi(Configuration config, CommunicatorService communicator) : IPenumbraApiPluginState, IApiService +{ + public string GetModDirectory() + => config.ModDirectory; + + public string GetConfiguration() + => JsonConvert.SerializeObject(config, Formatting.Indented); + + public event Action? ModDirectoryChanged + { + add => communicator.ModDirectoryChanged.Subscribe(value!, Communication.ModDirectoryChanged.Priority.Api); + remove => communicator.ModDirectoryChanged.Unsubscribe(value!); + } + + public bool GetEnabledState() + => config.EnableMods; + + public event Action? EnabledChange + { + add => communicator.EnabledChanged.Subscribe(value!, EnabledChanged.Priority.Api); + remove => communicator.EnabledChanged.Unsubscribe(value!); + } +} diff --git a/Penumbra/Api/Api/RedrawApi.cs b/Penumbra/Api/Api/RedrawApi.cs new file mode 100644 index 00000000..03b42493 --- /dev/null +++ b/Penumbra/Api/Api/RedrawApi.cs @@ -0,0 +1,27 @@ +using Dalamud.Game.ClientState.Objects.Types; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Interop.Services; + +namespace Penumbra.Api.Api; + +public class RedrawApi(RedrawService redrawService) : IPenumbraApiRedraw, IApiService +{ + public void RedrawObject(int gameObjectIndex, RedrawType setting) + => redrawService.RedrawObject(gameObjectIndex, setting); + + public void RedrawObject(string name, RedrawType setting) + => redrawService.RedrawObject(name, setting); + + public void RedrawObject(GameObject? gameObject, RedrawType setting) + => redrawService.RedrawObject(gameObject, setting); + + public void RedrawAll(RedrawType setting) + => redrawService.RedrawAll(setting); + + public event GameObjectRedrawnDelegate? GameObjectRedrawn + { + add => redrawService.GameObjectRedrawn += value; + remove => redrawService.GameObjectRedrawn -= value; + } +} diff --git a/Penumbra/Api/Api/ResolveApi.cs b/Penumbra/Api/Api/ResolveApi.cs new file mode 100644 index 00000000..ec57eba7 --- /dev/null +++ b/Penumbra/Api/Api/ResolveApi.cs @@ -0,0 +1,101 @@ +using Dalamud.Plugin.Services; +using OtterGui.Services; +using Penumbra.Collections; +using Penumbra.Collections.Manager; +using Penumbra.Interop.PathResolving; +using Penumbra.Mods.Manager; +using Penumbra.String.Classes; + +namespace Penumbra.Api.Api; + +public class ResolveApi( + ModManager modManager, + CollectionManager collectionManager, + Configuration config, + CollectionResolver collectionResolver, + ApiHelpers helpers, + IFramework framework) : IPenumbraApiResolve, IApiService +{ + public string ResolveDefaultPath(string gamePath) + => ResolvePath(gamePath, modManager, collectionManager.Active.Default); + + public string ResolveInterfacePath(string gamePath) + => ResolvePath(gamePath, modManager, collectionManager.Active.Interface); + + public string ResolveGameObjectPath(string gamePath, int gameObjectIdx) + { + helpers.AssociatedCollection(gameObjectIdx, out var collection); + return ResolvePath(gamePath, modManager, collection); + } + + public string ResolvePlayerPath(string gamePath) + => ResolvePath(gamePath, modManager, collectionResolver.PlayerCollection()); + + public string[] ReverseResolveGameObjectPath(string moddedPath, int gameObjectIdx) + { + if (!config.EnableMods) + return [moddedPath]; + + helpers.AssociatedCollection(gameObjectIdx, out var collection); + var ret = collection.ReverseResolvePath(new FullPath(moddedPath)); + return ret.Select(r => r.ToString()).ToArray(); + } + + public string[] ReverseResolvePlayerPath(string moddedPath) + { + if (!config.EnableMods) + return [moddedPath]; + + var ret = collectionResolver.PlayerCollection().ReverseResolvePath(new FullPath(moddedPath)); + return ret.Select(r => r.ToString()).ToArray(); + } + + public (string[], string[][]) ResolvePlayerPaths(string[] forward, string[] reverse) + { + if (!config.EnableMods) + return (forward, reverse.Select(p => new[] + { + p, + }).ToArray()); + + var playerCollection = collectionResolver.PlayerCollection(); + var resolved = forward.Select(p => ResolvePath(p, modManager, playerCollection)).ToArray(); + var reverseResolved = playerCollection.ReverseResolvePaths(reverse); + return (resolved, reverseResolved.Select(a => a.Select(p => p.ToString()).ToArray()).ToArray()); + } + + public async Task<(string[], string[][])> ResolvePlayerPathsAsync(string[] forward, string[] reverse) + { + if (!config.EnableMods) + return (forward, reverse.Select(p => new[] + { + p, + }).ToArray()); + + return await Task.Run(async () => + { + var playerCollection = await framework.RunOnFrameworkThread(collectionResolver.PlayerCollection).ConfigureAwait(false); + var forwardTask = Task.Run(() => + { + var forwardRet = new string[forward.Length]; + Parallel.For(0, forward.Length, idx => forwardRet[idx] = ResolvePath(forward[idx], modManager, playerCollection)); + return forwardRet; + }).ConfigureAwait(false); + var reverseTask = Task.Run(() => playerCollection.ReverseResolvePaths(reverse)).ConfigureAwait(false); + var reverseResolved = (await reverseTask).Select(a => a.Select(p => p.ToString()).ToArray()).ToArray(); + return (await forwardTask, reverseResolved); + }).ConfigureAwait(false); + } + + /// Resolve a path given by string for a specific collection. + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + private string ResolvePath(string path, ModManager _, ModCollection collection) + { + if (!config.EnableMods) + return path; + + var gamePath = Utf8GamePath.FromString(path, out var p, true) ? p : Utf8GamePath.Empty; + var ret = collection.ResolvePath(gamePath); + return ret?.ToString() ?? path; + } +} diff --git a/Penumbra/Api/Api/ResourceTreeApi.cs b/Penumbra/Api/Api/ResourceTreeApi.cs new file mode 100644 index 00000000..6e9aaa48 --- /dev/null +++ b/Penumbra/Api/Api/ResourceTreeApi.cs @@ -0,0 +1,63 @@ +using Dalamud.Game.ClientState.Objects.Types; +using Newtonsoft.Json.Linq; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Api.Helpers; +using Penumbra.GameData.Interop; +using Penumbra.Interop.ResourceTree; + +namespace Penumbra.Api.Api; + +public class ResourceTreeApi(ResourceTreeFactory resourceTreeFactory, ObjectManager objects) : IPenumbraApiResourceTree, IApiService +{ + public Dictionary>?[] GetGameObjectResourcePaths(params ushort[] gameObjects) + { + var characters = gameObjects.Select(index => objects.GetDalamudObject((int)index)).OfType(); + var resourceTrees = resourceTreeFactory.FromCharacters(characters, 0); + var pathDictionaries = ResourceTreeApiHelper.GetResourcePathDictionaries(resourceTrees); + + return Array.ConvertAll(gameObjects, obj => pathDictionaries.GetValueOrDefault(obj)); + } + + public Dictionary>> GetPlayerResourcePaths() + { + var resourceTrees = resourceTreeFactory.FromObjectTable(ResourceTreeFactory.Flags.LocalPlayerRelatedOnly); + return ResourceTreeApiHelper.GetResourcePathDictionaries(resourceTrees); + } + + public GameResourceDict?[] GetGameObjectResourcesOfType(ResourceType type, bool withUiData, + params ushort[] gameObjects) + { + var characters = gameObjects.Select(index => objects.GetDalamudObject((int)index)).OfType(); + var resourceTrees = resourceTreeFactory.FromCharacters(characters, withUiData ? ResourceTreeFactory.Flags.WithUiData : 0); + var resDictionaries = ResourceTreeApiHelper.GetResourcesOfType(resourceTrees, type); + + return Array.ConvertAll(gameObjects, obj => resDictionaries.GetValueOrDefault(obj)); + } + + public Dictionary GetPlayerResourcesOfType(ResourceType type, + bool withUiData) + { + var resourceTrees = resourceTreeFactory.FromObjectTable(ResourceTreeFactory.Flags.LocalPlayerRelatedOnly + | (withUiData ? ResourceTreeFactory.Flags.WithUiData : 0)); + return ResourceTreeApiHelper.GetResourcesOfType(resourceTrees, type); + } + + public JObject?[] GetGameObjectResourceTrees(bool withUiData, params ushort[] gameObjects) + { + var characters = gameObjects.Select(index => objects.GetDalamudObject((int)index)).OfType(); + var resourceTrees = resourceTreeFactory.FromCharacters(characters, withUiData ? ResourceTreeFactory.Flags.WithUiData : 0); + var resDictionary = ResourceTreeApiHelper.EncapsulateResourceTrees(resourceTrees); + + return Array.ConvertAll(gameObjects, obj => resDictionary.GetValueOrDefault(obj)); + } + + public Dictionary GetPlayerResourceTrees(bool withUiData) + { + var resourceTrees = resourceTreeFactory.FromObjectTable(ResourceTreeFactory.Flags.LocalPlayerRelatedOnly + | (withUiData ? ResourceTreeFactory.Flags.WithUiData : 0)); + var resDictionary = ResourceTreeApiHelper.EncapsulateResourceTrees(resourceTrees); + + return resDictionary; + } +} diff --git a/Penumbra/Api/Api/TemporaryApi.cs b/Penumbra/Api/Api/TemporaryApi.cs new file mode 100644 index 00000000..b4ffa8f4 --- /dev/null +++ b/Penumbra/Api/Api/TemporaryApi.cs @@ -0,0 +1,190 @@ +using OtterGui; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Collections.Manager; +using Penumbra.GameData.Actors; +using Penumbra.GameData.Interop; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Subclasses; +using Penumbra.String.Classes; + +namespace Penumbra.Api.Api; + +public class TemporaryApi( + TempCollectionManager tempCollections, + ObjectManager objects, + ActorManager actors, + CollectionManager collectionManager, + TempModManager tempMods) : IPenumbraApiTemporary, IApiService +{ + public Guid CreateTemporaryCollection(string name) + => tempCollections.CreateTemporaryCollection(name); + + public PenumbraApiEc DeleteTemporaryCollection(Guid collectionId) + => tempCollections.RemoveTemporaryCollection(collectionId) + ? PenumbraApiEc.Success + : PenumbraApiEc.CollectionMissing; + + public PenumbraApiEc AssignTemporaryCollection(Guid collectionId, int actorIndex, bool forceAssignment) + { + var args = ApiHelpers.Args("CollectionId", collectionId, "ActorIndex", actorIndex, "Forced", forceAssignment); + if (actorIndex < 0 || actorIndex >= objects.TotalCount) + return ApiHelpers.Return(PenumbraApiEc.InvalidArgument, args); + + var identifier = actors.FromObject(objects[actorIndex], out _, false, false, true); + if (!identifier.IsValid) + return ApiHelpers.Return(PenumbraApiEc.InvalidArgument, args); + + if (!tempCollections.CollectionById(collectionId, out var collection)) + return ApiHelpers.Return(PenumbraApiEc.CollectionMissing, args); + + if (forceAssignment) + { + if (tempCollections.Collections.ContainsKey(identifier) && !tempCollections.Collections.Delete(identifier)) + return ApiHelpers.Return(PenumbraApiEc.AssignmentDeletionFailed, args); + } + else if (tempCollections.Collections.ContainsKey(identifier) + || collectionManager.Active.Individuals.ContainsKey(identifier)) + { + return ApiHelpers.Return(PenumbraApiEc.CharacterCollectionExists, args); + } + + var group = tempCollections.Collections.GetGroup(identifier); + var ret = tempCollections.AddIdentifier(collection, group) + ? PenumbraApiEc.Success + : PenumbraApiEc.UnknownError; + return ApiHelpers.Return(ret, args); + } + + public PenumbraApiEc AddTemporaryModAll(string tag, Dictionary paths, string manipString, int priority) + { + var args = ApiHelpers.Args("Tag", tag, "#Paths", paths.Count, "ManipString", manipString, "Priority", priority); + if (!ConvertPaths(paths, out var p)) + return ApiHelpers.Return(PenumbraApiEc.InvalidGamePath, args); + + if (!ConvertManips(manipString, out var m)) + return ApiHelpers.Return(PenumbraApiEc.InvalidManipulation, args); + + var ret = tempMods.Register(tag, null, p, m, new ModPriority(priority)) switch + { + RedirectResult.Success => PenumbraApiEc.Success, + _ => PenumbraApiEc.UnknownError, + }; + return ApiHelpers.Return(ret, args); + } + + public PenumbraApiEc AddTemporaryMod(string tag, Guid collectionId, Dictionary paths, string manipString, int priority) + { + var args = ApiHelpers.Args("Tag", tag, "CollectionId", collectionId, "#Paths", paths.Count, "ManipString", + manipString, "Priority", priority); + + if (collectionId == Guid.Empty) + return ApiHelpers.Return(PenumbraApiEc.InvalidArgument, args); + + if (!tempCollections.CollectionById(collectionId, out var collection) + && !collectionManager.Storage.ById(collectionId, out collection)) + return ApiHelpers.Return(PenumbraApiEc.CollectionMissing, args); + + if (!ConvertPaths(paths, out var p)) + return ApiHelpers.Return(PenumbraApiEc.InvalidGamePath, args); + + if (!ConvertManips(manipString, out var m)) + return ApiHelpers.Return(PenumbraApiEc.InvalidManipulation, args); + + var ret = tempMods.Register(tag, collection, p, m, new ModPriority(priority)) switch + { + RedirectResult.Success => PenumbraApiEc.Success, + _ => PenumbraApiEc.UnknownError, + }; + return ApiHelpers.Return(ret, args); + } + + public PenumbraApiEc RemoveTemporaryModAll(string tag, int priority) + { + var ret = tempMods.Unregister(tag, null, new ModPriority(priority)) switch + { + RedirectResult.Success => PenumbraApiEc.Success, + RedirectResult.NotRegistered => PenumbraApiEc.NothingChanged, + _ => PenumbraApiEc.UnknownError, + }; + return ApiHelpers.Return(ret, ApiHelpers.Args("Tag", tag, "Priority", priority)); + } + + public PenumbraApiEc RemoveTemporaryMod(string tag, Guid collectionId, int priority) + { + var args = ApiHelpers.Args("Tag", tag, "CollectionId", collectionId, "Priority", priority); + + if (!tempCollections.CollectionById(collectionId, out var collection) + && !collectionManager.Storage.ById(collectionId, out collection)) + return ApiHelpers.Return(PenumbraApiEc.CollectionMissing, args); + + var ret = tempMods.Unregister(tag, collection, new ModPriority(priority)) switch + { + RedirectResult.Success => PenumbraApiEc.Success, + RedirectResult.NotRegistered => PenumbraApiEc.NothingChanged, + _ => PenumbraApiEc.UnknownError, + }; + return ApiHelpers.Return(ret, args); + } + + /// + /// Convert a dictionary of strings to a dictionary of game paths to full paths. + /// Only returns true if all paths can successfully be converted and added. + /// + private static bool ConvertPaths(IReadOnlyDictionary redirections, + [NotNullWhen(true)] out Dictionary? paths) + { + paths = new Dictionary(redirections.Count); + foreach (var (gString, fString) in redirections) + { + if (!Utf8GamePath.FromString(gString, out var path, false)) + { + paths = null; + return false; + } + + var fullPath = new FullPath(fString); + if (!paths.TryAdd(path, fullPath)) + { + paths = null; + return false; + } + } + + return true; + } + + /// + /// Convert manipulations from a transmitted base64 string to actual manipulations. + /// The empty string is treated as an empty set. + /// Only returns true if all conversions are successful and distinct. + /// + private static bool ConvertManips(string manipString, + [NotNullWhen(true)] out HashSet? manips) + { + if (manipString.Length == 0) + { + manips = []; + return true; + } + + if (Functions.FromCompressedBase64(manipString, out var manipArray) != MetaManipulation.CurrentVersion) + { + manips = null; + return false; + } + + manips = new HashSet(manipArray!.Length); + foreach (var manip in manipArray.Where(m => m.Validate())) + { + if (manips.Add(manip)) + continue; + + Penumbra.Log.Warning($"Manipulation {manip} {manip.EntryToString()} is invalid and was skipped."); + manips = null; + return false; + } + + return true; + } +} diff --git a/Penumbra/Api/Api/UiApi.cs b/Penumbra/Api/Api/UiApi.cs new file mode 100644 index 00000000..cf3cd8f2 --- /dev/null +++ b/Penumbra/Api/Api/UiApi.cs @@ -0,0 +1,101 @@ +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Communication; +using Penumbra.GameData.Enums; +using Penumbra.Mods.Manager; +using Penumbra.Services; +using Penumbra.UI; + +namespace Penumbra.Api.Api; + +public class UiApi : IPenumbraApiUi, IApiService, IDisposable +{ + private readonly CommunicatorService _communicator; + private readonly ConfigWindow _configWindow; + private readonly ModManager _modManager; + + public UiApi(CommunicatorService communicator, ConfigWindow configWindow, ModManager modManager) + { + _communicator = communicator; + _configWindow = configWindow; + _modManager = modManager; + _communicator.ChangedItemHover.Subscribe(OnChangedItemHover, ChangedItemHover.Priority.Default); + _communicator.ChangedItemClick.Subscribe(OnChangedItemClick, ChangedItemClick.Priority.Default); + } + + public void Dispose() + { + _communicator.ChangedItemHover.Unsubscribe(OnChangedItemHover); + _communicator.ChangedItemClick.Unsubscribe(OnChangedItemClick); + } + + public event Action? ChangedItemTooltip; + + public event Action? ChangedItemClicked; + + public event Action? PreSettingsTabBarDraw + { + add => _communicator.PreSettingsTabBarDraw.Subscribe(value!, Communication.PreSettingsTabBarDraw.Priority.Default); + remove => _communicator.PreSettingsTabBarDraw.Unsubscribe(value!); + } + + public event Action? PreSettingsPanelDraw + { + add => _communicator.PreSettingsPanelDraw.Subscribe(value!, Communication.PreSettingsPanelDraw.Priority.Default); + remove => _communicator.PreSettingsPanelDraw.Unsubscribe(value!); + } + + public event Action? PostEnabledDraw + { + add => _communicator.PostEnabledDraw.Subscribe(value!, Communication.PostEnabledDraw.Priority.Default); + remove => _communicator.PostEnabledDraw.Unsubscribe(value!); + } + + public event Action? PostSettingsPanelDraw + { + add => _communicator.PostSettingsPanelDraw.Subscribe(value!, Communication.PostSettingsPanelDraw.Priority.Default); + remove => _communicator.PostSettingsPanelDraw.Unsubscribe(value!); + } + + public PenumbraApiEc OpenMainWindow(TabType tab, string modDirectory, string modName) + { + _configWindow.IsOpen = true; + if (!Enum.IsDefined(tab)) + return PenumbraApiEc.InvalidArgument; + + if (tab == TabType.Mods && (modDirectory.Length > 0 || modName.Length > 0)) + { + if (_modManager.TryGetMod(modDirectory, modName, out var mod)) + _communicator.SelectTab.Invoke(tab, mod); + else + return PenumbraApiEc.ModMissing; + } + else if (tab != TabType.None) + { + _communicator.SelectTab.Invoke(tab, null); + } + + return PenumbraApiEc.Success; + } + + public void CloseMainWindow() + => _configWindow.IsOpen = false; + + private void OnChangedItemClick(MouseButton button, object? data) + { + if (ChangedItemClicked == null) + return; + + var (type, id) = ChangedItemExtensions.ChangedItemToTypeAndId(data); + ChangedItemClicked.Invoke(button, type, id); + } + + private void OnChangedItemHover(object? data) + { + if (ChangedItemTooltip == null) + return; + + var (type, id) = ChangedItemExtensions.ChangedItemToTypeAndId(data); + ChangedItemTooltip.Invoke(type, id); + } +} diff --git a/Penumbra/Api/DalamudSubstitutionProvider.cs b/Penumbra/Api/DalamudSubstitutionProvider.cs index 0374e31a..1c2cebcc 100644 --- a/Penumbra/Api/DalamudSubstitutionProvider.cs +++ b/Penumbra/Api/DalamudSubstitutionProvider.cs @@ -1,4 +1,5 @@ using Dalamud.Plugin.Services; +using OtterGui.Services; using Penumbra.Collections; using Penumbra.Collections.Manager; using Penumbra.Communication; @@ -9,7 +10,7 @@ using Penumbra.String.Classes; namespace Penumbra.Api; -public class DalamudSubstitutionProvider : IDisposable +public class DalamudSubstitutionProvider : IDisposable, IApiService { private readonly ITextureSubstitutionProvider _substitution; private readonly ActiveCollectionData _activeCollectionData; diff --git a/Penumbra/Api/HttpApi.cs b/Penumbra/Api/HttpApi.cs index e23f8b4f..859c46b4 100644 --- a/Penumbra/Api/HttpApi.cs +++ b/Penumbra/Api/HttpApi.cs @@ -1,11 +1,13 @@ using EmbedIO; using EmbedIO.Routing; using EmbedIO.WebApi; +using OtterGui.Services; +using Penumbra.Api.Api; using Penumbra.Api.Enums; namespace Penumbra.Api; -public class HttpApi : IDisposable +public class HttpApi : IDisposable, IApiService { private partial class Controller : WebApiController { @@ -67,7 +69,7 @@ public class HttpApi : IDisposable public partial object? GetMods() { Penumbra.Log.Debug($"[HTTP] {nameof(GetMods)} triggered."); - return _api.GetModList(); + return _api.Mods.GetModList(); } public async partial Task Redraw() @@ -75,17 +77,15 @@ public class HttpApi : IDisposable var data = await HttpContext.GetRequestDataAsync(); Penumbra.Log.Debug($"[HTTP] {nameof(Redraw)} triggered with {data}."); if (data.ObjectTableIndex >= 0) - _api.RedrawObject(data.ObjectTableIndex, data.Type); - else if (data.Name.Length > 0) - _api.RedrawObject(data.Name, data.Type); + _api.Redraw.RedrawObject(data.ObjectTableIndex, data.Type); else - _api.RedrawAll(data.Type); + _api.Redraw.RedrawAll(data.Type); } public partial void RedrawAll() { Penumbra.Log.Debug($"[HTTP] {nameof(RedrawAll)} triggered."); - _api.RedrawAll(RedrawType.Redraw); + _api.Redraw.RedrawAll(RedrawType.Redraw); } public async partial Task ReloadMod() @@ -95,10 +95,10 @@ public class HttpApi : IDisposable // Add the mod if it is not already loaded and if the directory name is given. // AddMod returns Success if the mod is already loaded. if (data.Path.Length != 0) - _api.AddMod(data.Path); + _api.Mods.AddMod(data.Path); // Reload the mod by path or name, which will also remove no-longer existing mods. - _api.ReloadMod(data.Path, data.Name); + _api.Mods.ReloadMod(data.Path, data.Name); } public async partial Task InstallMod() @@ -106,13 +106,13 @@ public class HttpApi : IDisposable var data = await HttpContext.GetRequestDataAsync(); Penumbra.Log.Debug($"[HTTP] {nameof(InstallMod)} triggered with {data}."); if (data.Path.Length != 0) - _api.InstallMod(data.Path); + _api.Mods.InstallMod(data.Path); } public partial void OpenWindow() { Penumbra.Log.Debug($"[HTTP] {nameof(OpenWindow)} triggered."); - _api.OpenMainWindow(TabType.Mods, string.Empty, string.Empty); + _api.Ui.OpenMainWindow(TabType.Mods, string.Empty, string.Empty); } private record ModReloadData(string Path, string Name) diff --git a/Penumbra/Api/IpcProviders.cs b/Penumbra/Api/IpcProviders.cs new file mode 100644 index 00000000..293af588 --- /dev/null +++ b/Penumbra/Api/IpcProviders.cs @@ -0,0 +1,118 @@ +using Dalamud.Plugin; +using OtterGui.Services; +using Penumbra.Api.Api; +using Penumbra.Api.Helpers; + +namespace Penumbra.Api; + +public sealed class IpcProviders : IDisposable, IApiService +{ + private readonly List _providers; + + private readonly EventProvider _disposedProvider; + private readonly EventProvider _initializedProvider; + + public IpcProviders(DalamudPluginInterface pi, IPenumbraApi api) + { + _disposedProvider = IpcSubscribers.Disposed.Provider(pi); + _initializedProvider = IpcSubscribers.Initialized.Provider(pi); + _providers = + [ + IpcSubscribers.GetCollections.Provider(pi, api.Collection), + IpcSubscribers.GetChangedItemsForCollection.Provider(pi, api.Collection), + IpcSubscribers.GetCollection.Provider(pi, api.Collection), + IpcSubscribers.GetCollectionForObject.Provider(pi, api.Collection), + IpcSubscribers.SetCollection.Provider(pi, api.Collection), + IpcSubscribers.SetCollectionForObject.Provider(pi, api.Collection), + + IpcSubscribers.ConvertTextureFile.Provider(pi, api.Editing), + IpcSubscribers.ConvertTextureData.Provider(pi, api.Editing), + + IpcSubscribers.GetDrawObjectInfo.Provider(pi, api.GameState), + IpcSubscribers.GetCutsceneParentIndex.Provider(pi, api.GameState), + IpcSubscribers.SetCutsceneParentIndex.Provider(pi, api.GameState), + IpcSubscribers.CreatingCharacterBase.Provider(pi, api.GameState), + IpcSubscribers.CreatedCharacterBase.Provider(pi, api.GameState), + IpcSubscribers.GameObjectResourcePathResolved.Provider(pi, api.GameState), + + IpcSubscribers.GetPlayerMetaManipulations.Provider(pi, api.Meta), + IpcSubscribers.GetMetaManipulations.Provider(pi, api.Meta), + + IpcSubscribers.GetModList.Provider(pi, api.Mods), + IpcSubscribers.InstallMod.Provider(pi, api.Mods), + IpcSubscribers.ReloadMod.Provider(pi, api.Mods), + IpcSubscribers.AddMod.Provider(pi, api.Mods), + IpcSubscribers.DeleteMod.Provider(pi, api.Mods), + IpcSubscribers.ModDeleted.Provider(pi, api.Mods), + IpcSubscribers.ModAdded.Provider(pi, api.Mods), + IpcSubscribers.ModMoved.Provider(pi, api.Mods), + IpcSubscribers.GetModPath.Provider(pi, api.Mods), + IpcSubscribers.SetModPath.Provider(pi, api.Mods), + + IpcSubscribers.GetAvailableModSettings.Provider(pi, api.ModSettings), + IpcSubscribers.GetCurrentModSettings.Provider(pi, api.ModSettings), + IpcSubscribers.TryInheritMod.Provider(pi, api.ModSettings), + IpcSubscribers.TrySetMod.Provider(pi, api.ModSettings), + IpcSubscribers.TrySetModPriority.Provider(pi, api.ModSettings), + IpcSubscribers.TrySetModSetting.Provider(pi, api.ModSettings), + IpcSubscribers.TrySetModSettings.Provider(pi, api.ModSettings), + IpcSubscribers.ModSettingChanged.Provider(pi, api.ModSettings), + IpcSubscribers.CopyModSettings.Provider(pi, api.ModSettings), + + IpcSubscribers.ApiVersion.Provider(pi, api), + IpcSubscribers.GetModDirectory.Provider(pi, api.PluginState), + IpcSubscribers.GetConfiguration.Provider(pi, api.PluginState), + IpcSubscribers.ModDirectoryChanged.Provider(pi, api.PluginState), + IpcSubscribers.GetEnabledState.Provider(pi, api.PluginState), + IpcSubscribers.EnabledChange.Provider(pi, api.PluginState), + + IpcSubscribers.RedrawObject.Provider(pi, api.Redraw), + IpcSubscribers.RedrawAll.Provider(pi, api.Redraw), + IpcSubscribers.GameObjectRedrawn.Provider(pi, api.Redraw), + + IpcSubscribers.ResolveDefaultPath.Provider(pi, api.Resolve), + IpcSubscribers.ResolveInterfacePath.Provider(pi, api.Resolve), + IpcSubscribers.ResolveGameObjectPath.Provider(pi, api.Resolve), + IpcSubscribers.ResolvePlayerPath.Provider(pi, api.Resolve), + IpcSubscribers.ReverseResolveGameObjectPath.Provider(pi, api.Resolve), + IpcSubscribers.ReverseResolvePlayerPath.Provider(pi, api.Resolve), + IpcSubscribers.ResolvePlayerPaths.Provider(pi, api.Resolve), + IpcSubscribers.ResolvePlayerPathsAsync.Provider(pi, api.Resolve), + + IpcSubscribers.GetGameObjectResourcePaths.Provider(pi, api.ResourceTree), + IpcSubscribers.GetPlayerResourcePaths.Provider(pi, api.ResourceTree), + IpcSubscribers.GetGameObjectResourcesOfType.Provider(pi, api.ResourceTree), + IpcSubscribers.GetPlayerResourcesOfType.Provider(pi, api.ResourceTree), + IpcSubscribers.GetGameObjectResourceTrees.Provider(pi, api.ResourceTree), + IpcSubscribers.GetPlayerResourceTrees.Provider(pi, api.ResourceTree), + + IpcSubscribers.CreateTemporaryCollection.Provider(pi, api.Temporary), + IpcSubscribers.DeleteTemporaryCollection.Provider(pi, api.Temporary), + IpcSubscribers.AssignTemporaryCollection.Provider(pi, api.Temporary), + IpcSubscribers.AddTemporaryModAll.Provider(pi, api.Temporary), + IpcSubscribers.AddTemporaryMod.Provider(pi, api.Temporary), + IpcSubscribers.RemoveTemporaryModAll.Provider(pi, api.Temporary), + IpcSubscribers.RemoveTemporaryMod.Provider(pi, api.Temporary), + + IpcSubscribers.ChangedItemTooltip.Provider(pi, api.Ui), + IpcSubscribers.ChangedItemClicked.Provider(pi, api.Ui), + IpcSubscribers.PreSettingsTabBarDraw.Provider(pi, api.Ui), + IpcSubscribers.PreSettingsPanelDraw.Provider(pi, api.Ui), + IpcSubscribers.PostEnabledDraw.Provider(pi, api.Ui), + IpcSubscribers.PostSettingsPanelDraw.Provider(pi, api.Ui), + IpcSubscribers.OpenMainWindow.Provider(pi, api.Ui), + IpcSubscribers.CloseMainWindow.Provider(pi, api.Ui), + ]; + _initializedProvider.Invoke(); + } + + public void Dispose() + { + foreach (var provider in _providers) + provider.Dispose(); + _providers.Clear(); + _initializedProvider.Dispose(); + _disposedProvider.Invoke(); + _disposedProvider.Dispose(); + } +} diff --git a/Penumbra/Api/IpcTester.cs b/Penumbra/Api/IpcTester.cs deleted file mode 100644 index 898c5de3..00000000 --- a/Penumbra/Api/IpcTester.cs +++ /dev/null @@ -1,1762 +0,0 @@ -using Dalamud.Game.ClientState.Objects.Enums; -using Dalamud.Interface; -using Dalamud.Interface.Utility; -using Dalamud.Plugin; -using ImGuiNET; -using OtterGui; -using OtterGui.Raii; -using Penumbra.Mods; -using Dalamud.Utility; -using Penumbra.Api.Enums; -using Penumbra.Api.Helpers; -using Penumbra.String; -using Penumbra.String.Classes; -using Penumbra.Meta.Manipulations; -using Penumbra.Mods.Manager; -using Penumbra.Services; -using Penumbra.UI; -using Penumbra.Collections.Manager; -using Dalamud.Plugin.Services; -using Penumbra.GameData.Structs; -using Penumbra.GameData.Enums; -using Penumbra.GameData.Interop; - -namespace Penumbra.Api; - -public class IpcTester : IDisposable -{ - private readonly PenumbraIpcProviders _ipcProviders; - private bool _subscribed = true; - - private readonly PluginState _pluginState; - private readonly IpcConfiguration _ipcConfiguration; - private readonly Ui _ui; - private readonly Redrawing _redrawing; - private readonly GameState _gameState; - private readonly Resolve _resolve; - private readonly Collections _collections; - private readonly Meta _meta; - private readonly Mods _mods; - private readonly ModSettings _modSettings; - private readonly Editing _editing; - private readonly Temporary _temporary; - private readonly ResourceTree _resourceTree; - - public IpcTester(Configuration config, DalamudPluginInterface pi, ObjectManager objects, IClientState clientState, - PenumbraIpcProviders ipcProviders, ModManager modManager, CollectionManager collections, TempModManager tempMods, - TempCollectionManager tempCollections, SaveService saveService) - { - _ipcProviders = ipcProviders; - _pluginState = new PluginState(pi); - _ipcConfiguration = new IpcConfiguration(pi); - _ui = new Ui(pi); - _redrawing = new Redrawing(pi, objects, clientState); - _gameState = new GameState(pi); - _resolve = new Resolve(pi); - _collections = new Collections(pi); - _meta = new Meta(pi); - _mods = new Mods(pi); - _modSettings = new ModSettings(pi); - _editing = new Editing(pi); - _temporary = new Temporary(pi, modManager, collections, tempMods, tempCollections, saveService, config); - _resourceTree = new ResourceTree(pi, objects); - UnsubscribeEvents(); - } - - public void Draw() - { - try - { - SubscribeEvents(); - ImGui.TextUnformatted($"API Version: {_ipcProviders.Api.ApiVersion.Breaking}.{_ipcProviders.Api.ApiVersion.Feature:D4}"); - _pluginState.Draw(); - _ipcConfiguration.Draw(); - _ui.Draw(); - _redrawing.Draw(); - _gameState.Draw(); - _resolve.Draw(); - _collections.Draw(); - _meta.Draw(); - _mods.Draw(); - _modSettings.Draw(); - _editing.Draw(); - _temporary.Draw(); - _temporary.DrawCollections(); - _temporary.DrawMods(); - _resourceTree.Draw(); - } - catch (Exception e) - { - Penumbra.Log.Error($"Error during IPC Tests:\n{e}"); - } - } - - private void SubscribeEvents() - { - if (!_subscribed) - { - _pluginState.Initialized.Enable(); - _pluginState.Disposed.Enable(); - _pluginState.EnabledChange.Enable(); - _redrawing.Redrawn.Enable(); - _ui.PreSettingsDraw.Enable(); - _ui.PostSettingsDraw.Enable(); - _modSettings.SettingChanged.Enable(); - _gameState.CharacterBaseCreating.Enable(); - _gameState.CharacterBaseCreated.Enable(); - _ipcConfiguration.ModDirectoryChanged.Enable(); - _gameState.GameObjectResourcePathResolved.Enable(); - _mods.DeleteSubscriber.Enable(); - _mods.AddSubscriber.Enable(); - _mods.MoveSubscriber.Enable(); - _subscribed = true; - } - } - - public void UnsubscribeEvents() - { - if (_subscribed) - { - _pluginState.Initialized.Disable(); - _pluginState.Disposed.Disable(); - _pluginState.EnabledChange.Disable(); - _redrawing.Redrawn.Disable(); - _ui.PreSettingsDraw.Disable(); - _ui.PostSettingsDraw.Disable(); - _ui.Tooltip.Disable(); - _ui.Click.Disable(); - _modSettings.SettingChanged.Disable(); - _gameState.CharacterBaseCreating.Disable(); - _gameState.CharacterBaseCreated.Disable(); - _ipcConfiguration.ModDirectoryChanged.Disable(); - _gameState.GameObjectResourcePathResolved.Disable(); - _mods.DeleteSubscriber.Disable(); - _mods.AddSubscriber.Disable(); - _mods.MoveSubscriber.Disable(); - _subscribed = false; - } - } - - public void Dispose() - { - _pluginState.Initialized.Dispose(); - _pluginState.Disposed.Dispose(); - _pluginState.EnabledChange.Dispose(); - _redrawing.Redrawn.Dispose(); - _ui.PreSettingsDraw.Dispose(); - _ui.PostSettingsDraw.Dispose(); - _ui.Tooltip.Dispose(); - _ui.Click.Dispose(); - _modSettings.SettingChanged.Dispose(); - _gameState.CharacterBaseCreating.Dispose(); - _gameState.CharacterBaseCreated.Dispose(); - _ipcConfiguration.ModDirectoryChanged.Dispose(); - _gameState.GameObjectResourcePathResolved.Dispose(); - _mods.DeleteSubscriber.Dispose(); - _mods.AddSubscriber.Dispose(); - _mods.MoveSubscriber.Dispose(); - _subscribed = false; - } - - private static void DrawIntro(string label, string info) - { - ImGui.TableNextColumn(); - ImGui.TextUnformatted(label); - ImGui.TableNextColumn(); - ImGui.TextUnformatted(info); - ImGui.TableNextColumn(); - } - - - private class PluginState - { - private readonly DalamudPluginInterface _pi; - public readonly EventSubscriber Initialized; - public readonly EventSubscriber Disposed; - public readonly EventSubscriber EnabledChange; - - private readonly List _initializedList = new(); - private readonly List _disposedList = new(); - - private DateTimeOffset _lastEnabledChange = DateTimeOffset.UnixEpoch; - private bool? _lastEnabledValue; - - public PluginState(DalamudPluginInterface pi) - { - _pi = pi; - Initialized = Ipc.Initialized.Subscriber(pi, AddInitialized); - Disposed = Ipc.Disposed.Subscriber(pi, AddDisposed); - EnabledChange = Ipc.EnabledChange.Subscriber(pi, SetLastEnabled); - } - - public void Draw() - { - using var _ = ImRaii.TreeNode("Plugin State"); - if (!_) - return; - - using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - void DrawList(string label, string text, List list) - { - DrawIntro(label, text); - if (list.Count == 0) - { - ImGui.TextUnformatted("Never"); - } - else - { - ImGui.TextUnformatted(list[^1].LocalDateTime.ToString(CultureInfo.CurrentCulture)); - if (list.Count > 1 && ImGui.IsItemHovered()) - ImGui.SetTooltip(string.Join("\n", - list.SkipLast(1).Select(t => t.LocalDateTime.ToString(CultureInfo.CurrentCulture)))); - } - } - - DrawList(Ipc.Initialized.Label, "Last Initialized", _initializedList); - DrawList(Ipc.Disposed.Label, "Last Disposed", _disposedList); - DrawIntro(Ipc.ApiVersions.Label, "Current Version"); - var (breaking, features) = Ipc.ApiVersions.Subscriber(_pi).Invoke(); - ImGui.TextUnformatted($"{breaking}.{features:D4}"); - DrawIntro(Ipc.GetEnabledState.Label, "Current State"); - ImGui.TextUnformatted($"{Ipc.GetEnabledState.Subscriber(_pi).Invoke()}"); - DrawIntro(Ipc.EnabledChange.Label, "Last Change"); - ImGui.TextUnformatted(_lastEnabledValue is { } v ? $"{_lastEnabledChange} (to {v})" : "Never"); - } - - private void AddInitialized() - => _initializedList.Add(DateTimeOffset.UtcNow); - - private void AddDisposed() - => _disposedList.Add(DateTimeOffset.UtcNow); - - private void SetLastEnabled(bool val) - => (_lastEnabledChange, _lastEnabledValue) = (DateTimeOffset.Now, val); - } - - private class IpcConfiguration - { - private readonly DalamudPluginInterface _pi; - public readonly EventSubscriber ModDirectoryChanged; - - private string _currentConfiguration = string.Empty; - private string _lastModDirectory = string.Empty; - private bool _lastModDirectoryValid; - private DateTimeOffset _lastModDirectoryTime = DateTimeOffset.MinValue; - - public IpcConfiguration(DalamudPluginInterface pi) - { - _pi = pi; - ModDirectoryChanged = Ipc.ModDirectoryChanged.Subscriber(pi, UpdateModDirectoryChanged); - } - - public void Draw() - { - using var _ = ImRaii.TreeNode("Configuration"); - if (!_) - return; - - using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - DrawIntro(Ipc.GetModDirectory.Label, "Current Mod Directory"); - ImGui.TextUnformatted(Ipc.GetModDirectory.Subscriber(_pi).Invoke()); - DrawIntro(Ipc.ModDirectoryChanged.Label, "Last Mod Directory Change"); - ImGui.TextUnformatted(_lastModDirectoryTime > DateTimeOffset.MinValue - ? $"{_lastModDirectory} ({(_lastModDirectoryValid ? "Valid" : "Invalid")}) at {_lastModDirectoryTime}" - : "None"); - DrawIntro(Ipc.GetConfiguration.Label, "Configuration"); - if (ImGui.Button("Get")) - { - _currentConfiguration = Ipc.GetConfiguration.Subscriber(_pi).Invoke(); - ImGui.OpenPopup("Config Popup"); - } - - DrawConfigPopup(); - } - - private void DrawConfigPopup() - { - ImGui.SetNextWindowSize(ImGuiHelpers.ScaledVector2(500, 500)); - using var popup = ImRaii.Popup("Config Popup"); - if (!popup) - return; - - using (ImRaii.PushFont(UiBuilder.MonoFont)) - { - ImGuiUtil.TextWrapped(_currentConfiguration); - } - - if (ImGui.Button("Close", -Vector2.UnitX) || !ImGui.IsWindowFocused()) - ImGui.CloseCurrentPopup(); - } - - private void UpdateModDirectoryChanged(string path, bool valid) - => (_lastModDirectory, _lastModDirectoryValid, _lastModDirectoryTime) = (path, valid, DateTimeOffset.Now); - } - - private class Ui - { - private readonly DalamudPluginInterface _pi; - public readonly EventSubscriber PreSettingsDraw; - public readonly EventSubscriber PostSettingsDraw; - public readonly EventSubscriber Tooltip; - public readonly EventSubscriber Click; - - private string _lastDrawnMod = string.Empty; - private DateTimeOffset _lastDrawnModTime = DateTimeOffset.MinValue; - private bool _subscribedToTooltip; - private bool _subscribedToClick; - private string _lastClicked = string.Empty; - private string _lastHovered = string.Empty; - private TabType _selectTab = TabType.None; - private string _modName = string.Empty; - private PenumbraApiEc _ec = PenumbraApiEc.Success; - - public Ui(DalamudPluginInterface pi) - { - _pi = pi; - PreSettingsDraw = Ipc.PreSettingsDraw.Subscriber(pi, UpdateLastDrawnMod); - PostSettingsDraw = Ipc.PostSettingsDraw.Subscriber(pi, UpdateLastDrawnMod); - Tooltip = Ipc.ChangedItemTooltip.Subscriber(pi, AddedTooltip); - Click = Ipc.ChangedItemClick.Subscriber(pi, AddedClick); - } - - public void Draw() - { - using var _ = ImRaii.TreeNode("UI"); - if (!_) - return; - - using (var combo = ImRaii.Combo("Tab to Open at", _selectTab.ToString())) - { - if (combo) - foreach (var val in Enum.GetValues()) - { - if (ImGui.Selectable(val.ToString(), _selectTab == val)) - _selectTab = val; - } - } - - ImGui.InputTextWithHint("##openMod", "Mod to Open at...", ref _modName, 256); - using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - DrawIntro(Ipc.PostSettingsDraw.Label, "Last Drawn Mod"); - ImGui.TextUnformatted(_lastDrawnMod.Length > 0 ? $"{_lastDrawnMod} at {_lastDrawnModTime}" : "None"); - - DrawIntro(Ipc.ChangedItemTooltip.Label, "Add Tooltip"); - if (ImGui.Checkbox("##tooltip", ref _subscribedToTooltip)) - { - if (_subscribedToTooltip) - Tooltip.Enable(); - else - Tooltip.Disable(); - } - - ImGui.SameLine(); - ImGui.TextUnformatted(_lastHovered); - - DrawIntro(Ipc.ChangedItemClick.Label, "Subscribe Click"); - if (ImGui.Checkbox("##click", ref _subscribedToClick)) - { - if (_subscribedToClick) - Click.Enable(); - else - Click.Disable(); - } - - ImGui.SameLine(); - ImGui.TextUnformatted(_lastClicked); - DrawIntro(Ipc.OpenMainWindow.Label, "Open Mod Window"); - if (ImGui.Button("Open##window")) - _ec = Ipc.OpenMainWindow.Subscriber(_pi).Invoke(_selectTab, _modName, _modName); - - ImGui.SameLine(); - ImGui.TextUnformatted(_ec.ToString()); - - DrawIntro(Ipc.CloseMainWindow.Label, "Close Mod Window"); - if (ImGui.Button("Close##window")) - Ipc.CloseMainWindow.Subscriber(_pi).Invoke(); - } - - private void UpdateLastDrawnMod(string name) - => (_lastDrawnMod, _lastDrawnModTime) = (name, DateTimeOffset.Now); - - private void AddedTooltip(ChangedItemType type, uint id) - { - _lastHovered = $"{type} {id} at {DateTime.UtcNow.ToLocalTime().ToString(CultureInfo.CurrentCulture)}"; - ImGui.TextUnformatted("IPC Test Successful"); - } - - private void AddedClick(MouseButton button, ChangedItemType type, uint id) - { - _lastClicked = $"{button}-click on {type} {id} at {DateTime.UtcNow.ToLocalTime().ToString(CultureInfo.CurrentCulture)}"; - } - } - - private class Redrawing - { - private readonly DalamudPluginInterface _pi; - private readonly IClientState _clientState; - private readonly ObjectManager _objects; - public readonly EventSubscriber Redrawn; - - private string _redrawName = string.Empty; - private int _redrawIndex; - private string _lastRedrawnString = "None"; - - public Redrawing(DalamudPluginInterface pi, ObjectManager objects, IClientState clientState) - { - _pi = pi; - _objects = objects; - _clientState = clientState; - Redrawn = Ipc.GameObjectRedrawn.Subscriber(_pi, SetLastRedrawn); - } - - public void Draw() - { - using var _ = ImRaii.TreeNode("Redrawing"); - if (!_) - return; - - using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - DrawIntro(Ipc.RedrawObjectByName.Label, "Redraw by Name"); - ImGui.SetNextItemWidth(100 * UiHelpers.Scale); - ImGui.InputTextWithHint("##redrawName", "Name...", ref _redrawName, 32); - ImGui.SameLine(); - if (ImGui.Button("Redraw##Name")) - Ipc.RedrawObjectByName.Subscriber(_pi).Invoke(_redrawName, RedrawType.Redraw); - - DrawIntro(Ipc.RedrawObject.Label, "Redraw Player Character"); - if (ImGui.Button("Redraw##pc") && _clientState.LocalPlayer != null) - Ipc.RedrawObject.Subscriber(_pi).Invoke(_clientState.LocalPlayer, RedrawType.Redraw); - - DrawIntro(Ipc.RedrawObjectByIndex.Label, "Redraw by Index"); - var tmp = _redrawIndex; - ImGui.SetNextItemWidth(100 * UiHelpers.Scale); - if (ImGui.DragInt("##redrawIndex", ref tmp, 0.1f, 0, _objects.TotalCount)) - _redrawIndex = Math.Clamp(tmp, 0, _objects.TotalCount); - - ImGui.SameLine(); - if (ImGui.Button("Redraw##Index")) - Ipc.RedrawObjectByIndex.Subscriber(_pi).Invoke(_redrawIndex, RedrawType.Redraw); - - DrawIntro(Ipc.RedrawAll.Label, "Redraw All"); - if (ImGui.Button("Redraw##All")) - Ipc.RedrawAll.Subscriber(_pi).Invoke(RedrawType.Redraw); - - DrawIntro(Ipc.GameObjectRedrawn.Label, "Last Redrawn Object:"); - ImGui.TextUnformatted(_lastRedrawnString); - } - - private void SetLastRedrawn(IntPtr address, int index) - { - if (index < 0 - || index > _objects.TotalCount - || address == IntPtr.Zero - || _objects[index].Address != address) - _lastRedrawnString = "Invalid"; - - _lastRedrawnString = $"{_objects[index].Utf8Name} (0x{address:X}, {index})"; - } - } - - private class GameState - { - private readonly DalamudPluginInterface _pi; - public readonly EventSubscriber CharacterBaseCreating; - public readonly EventSubscriber CharacterBaseCreated; - public readonly EventSubscriber GameObjectResourcePathResolved; - - - private string _lastCreatedGameObjectName = string.Empty; - private IntPtr _lastCreatedDrawObject = IntPtr.Zero; - private DateTimeOffset _lastCreatedGameObjectTime = DateTimeOffset.MaxValue; - private string _lastResolvedGamePath = string.Empty; - private string _lastResolvedFullPath = string.Empty; - private string _lastResolvedObject = string.Empty; - private DateTimeOffset _lastResolvedGamePathTime = DateTimeOffset.MaxValue; - private string _currentDrawObjectString = string.Empty; - private IntPtr _currentDrawObject = IntPtr.Zero; - private int _currentCutsceneActor; - private int _currentCutsceneParent; - private PenumbraApiEc _cutsceneError = PenumbraApiEc.Success; - - public GameState(DalamudPluginInterface pi) - { - _pi = pi; - CharacterBaseCreating = Ipc.CreatingCharacterBase.Subscriber(pi, UpdateLastCreated); - CharacterBaseCreated = Ipc.CreatedCharacterBase.Subscriber(pi, UpdateLastCreated2); - GameObjectResourcePathResolved = Ipc.GameObjectResourcePathResolved.Subscriber(pi, UpdateGameObjectResourcePath); - } - - public void Draw() - { - using var _ = ImRaii.TreeNode("Game State"); - if (!_) - return; - - if (ImGui.InputTextWithHint("##drawObject", "Draw Object Address..", ref _currentDrawObjectString, 16, - ImGuiInputTextFlags.CharsHexadecimal)) - _currentDrawObject = IntPtr.TryParse(_currentDrawObjectString, NumberStyles.HexNumber, CultureInfo.InvariantCulture, - out var tmp) - ? tmp - : IntPtr.Zero; - - ImGui.InputInt("Cutscene Actor", ref _currentCutsceneActor, 0); - ImGui.InputInt("Cutscene Parent", ref _currentCutsceneParent, 0); - if (_cutsceneError is not PenumbraApiEc.Success) - { - ImGui.SameLine(); - ImGui.TextUnformatted("Invalid Argument on last Call"); - } - - using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - DrawIntro(Ipc.GetDrawObjectInfo.Label, "Draw Object Info"); - if (_currentDrawObject == IntPtr.Zero) - { - ImGui.TextUnformatted("Invalid"); - } - else - { - var (ptr, collection) = Ipc.GetDrawObjectInfo.Subscriber(_pi).Invoke(_currentDrawObject); - ImGui.TextUnformatted(ptr == IntPtr.Zero ? $"No Actor Associated, {collection}" : $"{ptr:X}, {collection}"); - } - - DrawIntro(Ipc.GetCutsceneParentIndex.Label, "Cutscene Parent"); - ImGui.TextUnformatted(Ipc.GetCutsceneParentIndex.Subscriber(_pi).Invoke(_currentCutsceneActor).ToString()); - - DrawIntro(Ipc.SetCutsceneParentIndex.Label, "Cutscene Parent"); - if (ImGui.Button("Set Parent")) - _cutsceneError = Ipc.SetCutsceneParentIndex.Subscriber(_pi).Invoke(_currentCutsceneActor, _currentCutsceneParent); - - DrawIntro(Ipc.CreatingCharacterBase.Label, "Last Drawobject created"); - if (_lastCreatedGameObjectTime < DateTimeOffset.Now) - ImGui.TextUnformatted(_lastCreatedDrawObject != IntPtr.Zero - ? $"0x{_lastCreatedDrawObject:X} for <{_lastCreatedGameObjectName}> at {_lastCreatedGameObjectTime}" - : $"NULL for <{_lastCreatedGameObjectName}> at {_lastCreatedGameObjectTime}"); - - DrawIntro(Ipc.GameObjectResourcePathResolved.Label, "Last GamePath resolved"); - if (_lastResolvedGamePathTime < DateTimeOffset.Now) - ImGui.TextUnformatted( - $"{_lastResolvedGamePath} -> {_lastResolvedFullPath} for <{_lastResolvedObject}> at {_lastResolvedGamePathTime}"); - } - - private void UpdateLastCreated(IntPtr gameObject, string _, IntPtr _2, IntPtr _3, IntPtr _4) - { - _lastCreatedGameObjectName = GetObjectName(gameObject); - _lastCreatedGameObjectTime = DateTimeOffset.Now; - _lastCreatedDrawObject = IntPtr.Zero; - } - - private void UpdateLastCreated2(IntPtr gameObject, string _, IntPtr drawObject) - { - _lastCreatedGameObjectName = GetObjectName(gameObject); - _lastCreatedGameObjectTime = DateTimeOffset.Now; - _lastCreatedDrawObject = drawObject; - } - - private void UpdateGameObjectResourcePath(IntPtr gameObject, string gamePath, string fullPath) - { - _lastResolvedObject = GetObjectName(gameObject); - _lastResolvedGamePath = gamePath; - _lastResolvedFullPath = fullPath; - _lastResolvedGamePathTime = DateTimeOffset.Now; - } - - private static unsafe string GetObjectName(IntPtr gameObject) - { - var obj = (FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject*)gameObject; - var name = obj != null ? obj->Name : null; - return name != null && *name != 0 ? new ByteString(name).ToString() : "Unknown"; - } - } - - private class Resolve(DalamudPluginInterface pi) - { - private string _currentResolvePath = string.Empty; - private string _currentResolveCharacter = string.Empty; - private string _currentReversePath = string.Empty; - private int _currentReverseIdx; - private Task<(string[], string[][])> _task = Task.FromResult<(string[], string[][])>(([], [])); - - public void Draw() - { - using var tree = ImRaii.TreeNode("Resolving"); - if (!tree) - return; - - ImGui.InputTextWithHint("##resolvePath", "Resolve this game path...", ref _currentResolvePath, Utf8GamePath.MaxGamePathLength); - ImGui.InputTextWithHint("##resolveCharacter", "Character Name (leave blank for default)...", ref _currentResolveCharacter, 32); - ImGui.InputTextWithHint("##resolveInversePath", "Reverse-resolve this path...", ref _currentReversePath, - Utf8GamePath.MaxGamePathLength); - ImGui.InputInt("##resolveIdx", ref _currentReverseIdx, 0, 0); - using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - DrawIntro(Ipc.ResolveDefaultPath.Label, "Default Collection Resolve"); - if (_currentResolvePath.Length != 0) - ImGui.TextUnformatted(Ipc.ResolveDefaultPath.Subscriber(pi).Invoke(_currentResolvePath)); - - DrawIntro(Ipc.ResolveInterfacePath.Label, "Interface Collection Resolve"); - if (_currentResolvePath.Length != 0) - ImGui.TextUnformatted(Ipc.ResolveInterfacePath.Subscriber(pi).Invoke(_currentResolvePath)); - - DrawIntro(Ipc.ResolvePlayerPath.Label, "Player Collection Resolve"); - if (_currentResolvePath.Length != 0) - ImGui.TextUnformatted(Ipc.ResolvePlayerPath.Subscriber(pi).Invoke(_currentResolvePath)); - - DrawIntro(Ipc.ResolveCharacterPath.Label, "Character Collection Resolve"); - if (_currentResolvePath.Length != 0 && _currentResolveCharacter.Length != 0) - ImGui.TextUnformatted(Ipc.ResolveCharacterPath.Subscriber(pi).Invoke(_currentResolvePath, _currentResolveCharacter)); - - DrawIntro(Ipc.ResolveGameObjectPath.Label, "Game Object Collection Resolve"); - if (_currentResolvePath.Length != 0) - ImGui.TextUnformatted(Ipc.ResolveGameObjectPath.Subscriber(pi).Invoke(_currentResolvePath, _currentReverseIdx)); - - DrawIntro(Ipc.ReverseResolvePath.Label, "Reversed Game Paths"); - if (_currentReversePath.Length > 0) - { - var list = Ipc.ReverseResolvePath.Subscriber(pi).Invoke(_currentReversePath, _currentResolveCharacter); - if (list.Length > 0) - { - ImGui.TextUnformatted(list[0]); - if (list.Length > 1 && ImGui.IsItemHovered()) - ImGui.SetTooltip(string.Join("\n", list.Skip(1))); - } - } - - DrawIntro(Ipc.ReverseResolvePlayerPath.Label, "Reversed Game Paths (Player)"); - if (_currentReversePath.Length > 0) - { - var list = Ipc.ReverseResolvePlayerPath.Subscriber(pi).Invoke(_currentReversePath); - if (list.Length > 0) - { - ImGui.TextUnformatted(list[0]); - if (list.Length > 1 && ImGui.IsItemHovered()) - ImGui.SetTooltip(string.Join("\n", list.Skip(1))); - } - } - - DrawIntro(Ipc.ReverseResolveGameObjectPath.Label, "Reversed Game Paths (Game Object)"); - if (_currentReversePath.Length > 0) - { - var list = Ipc.ReverseResolveGameObjectPath.Subscriber(pi).Invoke(_currentReversePath, _currentReverseIdx); - if (list.Length > 0) - { - ImGui.TextUnformatted(list[0]); - if (list.Length > 1 && ImGui.IsItemHovered()) - ImGui.SetTooltip(string.Join("\n", list.Skip(1))); - } - } - - var forwardArray = _currentResolvePath.Length > 0 - ? [_currentResolvePath] - : Array.Empty(); - var reverseArray = _currentReversePath.Length > 0 - ? [_currentReversePath] - : Array.Empty(); - - DrawIntro(Ipc.ResolvePlayerPaths.Label, "Resolved Paths (Player)"); - if (forwardArray.Length > 0 || reverseArray.Length > 0) - { - var ret = Ipc.ResolvePlayerPaths.Subscriber(pi).Invoke(forwardArray, reverseArray); - ImGui.TextUnformatted(ConvertText(ret)); - } - - DrawIntro(Ipc.ResolvePlayerPathsAsync.Label, "Resolved Paths Async (Player)"); - if (ImGui.Button("Start")) - _task = Ipc.ResolvePlayerPathsAsync.Subscriber(pi).Invoke(forwardArray, reverseArray); - var hovered = ImGui.IsItemHovered(); - ImGui.SameLine(); - ImGui.AlignTextToFramePadding(); - ImGui.TextUnformatted(_task.Status.ToString()); - if ((hovered || ImGui.IsItemHovered()) && _task.IsCompletedSuccessfully) - ImGui.SetTooltip(ConvertText(_task.Result)); - return; - - static string ConvertText((string[], string[][]) data) - { - var text = string.Empty; - if (data.Item1.Length > 0) - { - if (data.Item2.Length > 0) - text = $"Forward: {data.Item1[0]} | Reverse: {string.Join("; ", data.Item2[0])}."; - else - text = $"Forward: {data.Item1[0]}."; - } - else if (data.Item2.Length > 0) - { - text = $"Reverse: {string.Join("; ", data.Item2[0])}."; - } - - return text; - } - } - } - - private class Collections - { - private readonly DalamudPluginInterface _pi; - - private int _objectIdx; - private string _collectionName = string.Empty; - private bool _allowCreation = true; - private bool _allowDeletion = true; - private ApiCollectionType _type = ApiCollectionType.Current; - - private string _characterCollectionName = string.Empty; - private IList _collections = []; - private string _changedItemCollection = string.Empty; - private IReadOnlyDictionary _changedItems = new Dictionary(); - private PenumbraApiEc _returnCode = PenumbraApiEc.Success; - private string? _oldCollection; - - public Collections(DalamudPluginInterface pi) - => _pi = pi; - - public void Draw() - { - using var _ = ImRaii.TreeNode("Collections"); - if (!_) - return; - - ImGuiUtil.GenericEnumCombo("Collection Type", 200, _type, out _type, t => ((CollectionType)t).ToName()); - ImGui.InputInt("Object Index##Collections", ref _objectIdx, 0, 0); - ImGui.InputText("Collection Name##Collections", ref _collectionName, 64); - ImGui.Checkbox("Allow Assignment Creation", ref _allowCreation); - ImGui.SameLine(); - ImGui.Checkbox("Allow Assignment Deletion", ref _allowDeletion); - - using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - DrawIntro("Last Return Code", _returnCode.ToString()); - if (_oldCollection != null) - ImGui.TextUnformatted(_oldCollection.Length == 0 ? "Created" : _oldCollection); - - DrawIntro(Ipc.GetCurrentCollectionName.Label, "Current Collection"); - ImGui.TextUnformatted(Ipc.GetCurrentCollectionName.Subscriber(_pi).Invoke()); - DrawIntro(Ipc.GetDefaultCollectionName.Label, "Default Collection"); - ImGui.TextUnformatted(Ipc.GetDefaultCollectionName.Subscriber(_pi).Invoke()); - DrawIntro(Ipc.GetInterfaceCollectionName.Label, "Interface Collection"); - ImGui.TextUnformatted(Ipc.GetInterfaceCollectionName.Subscriber(_pi).Invoke()); - DrawIntro(Ipc.GetCharacterCollectionName.Label, "Character"); - ImGui.SetNextItemWidth(200 * UiHelpers.Scale); - ImGui.InputTextWithHint("##characterCollectionName", "Character Name...", ref _characterCollectionName, 64); - var (c, s) = Ipc.GetCharacterCollectionName.Subscriber(_pi).Invoke(_characterCollectionName); - ImGui.SameLine(); - ImGui.TextUnformatted($"{c}, {(s ? "Custom" : "Default")}"); - - DrawIntro(Ipc.GetCollections.Label, "Collections"); - if (ImGui.Button("Get##Collections")) - { - _collections = Ipc.GetCollections.Subscriber(_pi).Invoke(); - ImGui.OpenPopup("Collections"); - } - - DrawIntro(Ipc.GetCollectionForType.Label, "Get Special Collection"); - var name = Ipc.GetCollectionForType.Subscriber(_pi).Invoke(_type); - ImGui.TextUnformatted(name.Length == 0 ? "Unassigned" : name); - DrawIntro(Ipc.SetCollectionForType.Label, "Set Special Collection"); - if (ImGui.Button("Set##TypeCollection")) - (_returnCode, _oldCollection) = - Ipc.SetCollectionForType.Subscriber(_pi).Invoke(_type, _collectionName, _allowCreation, _allowDeletion); - - DrawIntro(Ipc.GetCollectionForObject.Label, "Get Object Collection"); - (var valid, var individual, name) = Ipc.GetCollectionForObject.Subscriber(_pi).Invoke(_objectIdx); - ImGui.TextUnformatted( - $"{(valid ? "Valid" : "Invalid")} Object, {(name.Length == 0 ? "Unassigned" : name)}{(individual ? " (Individual Assignment)" : string.Empty)}"); - DrawIntro(Ipc.SetCollectionForObject.Label, "Set Object Collection"); - if (ImGui.Button("Set##ObjectCollection")) - (_returnCode, _oldCollection) = Ipc.SetCollectionForObject.Subscriber(_pi) - .Invoke(_objectIdx, _collectionName, _allowCreation, _allowDeletion); - - if (_returnCode == PenumbraApiEc.NothingChanged && _oldCollection.IsNullOrEmpty()) - _oldCollection = null; - - DrawIntro(Ipc.GetChangedItems.Label, "Changed Item List"); - ImGui.SetNextItemWidth(200 * UiHelpers.Scale); - ImGui.InputTextWithHint("##changedCollection", "Collection Name...", ref _changedItemCollection, 64); - ImGui.SameLine(); - if (ImGui.Button("Get")) - { - _changedItems = Ipc.GetChangedItems.Subscriber(_pi).Invoke(_changedItemCollection); - ImGui.OpenPopup("Changed Item List"); - } - - DrawChangedItemPopup(); - DrawCollectionPopup(); - } - - private void DrawChangedItemPopup() - { - ImGui.SetNextWindowSize(ImGuiHelpers.ScaledVector2(500, 500)); - using var p = ImRaii.Popup("Changed Item List"); - if (!p) - return; - - foreach (var item in _changedItems) - ImGui.TextUnformatted(item.Key); - - if (ImGui.Button("Close", -Vector2.UnitX) || !ImGui.IsWindowFocused()) - ImGui.CloseCurrentPopup(); - } - - private void DrawCollectionPopup() - { - ImGui.SetNextWindowSize(ImGuiHelpers.ScaledVector2(500, 500)); - using var p = ImRaii.Popup("Collections"); - if (!p) - return; - - foreach (var collection in _collections) - ImGui.TextUnformatted(collection); - - if (ImGui.Button("Close", -Vector2.UnitX) || !ImGui.IsWindowFocused()) - ImGui.CloseCurrentPopup(); - } - } - - private class Meta - { - private readonly DalamudPluginInterface _pi; - - private string _characterName = string.Empty; - private int _gameObjectIndex; - - public Meta(DalamudPluginInterface pi) - => _pi = pi; - - public void Draw() - { - using var _ = ImRaii.TreeNode("Meta"); - if (!_) - return; - - ImGui.InputTextWithHint("##characterName", "Character Name...", ref _characterName, 64); - ImGui.InputInt("##metaIdx", ref _gameObjectIndex, 0, 0); - using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - DrawIntro(Ipc.GetMetaManipulations.Label, "Meta Manipulations"); - if (ImGui.Button("Copy to Clipboard")) - { - var base64 = Ipc.GetMetaManipulations.Subscriber(_pi).Invoke(_characterName); - ImGui.SetClipboardText(base64); - } - - DrawIntro(Ipc.GetPlayerMetaManipulations.Label, "Player Meta Manipulations"); - if (ImGui.Button("Copy to Clipboard##Player")) - { - var base64 = Ipc.GetPlayerMetaManipulations.Subscriber(_pi).Invoke(); - ImGui.SetClipboardText(base64); - } - - DrawIntro(Ipc.GetGameObjectMetaManipulations.Label, "Game Object Manipulations"); - if (ImGui.Button("Copy to Clipboard##GameObject")) - { - var base64 = Ipc.GetGameObjectMetaManipulations.Subscriber(_pi).Invoke(_gameObjectIndex); - ImGui.SetClipboardText(base64); - } - } - } - - private class Mods - { - private readonly DalamudPluginInterface _pi; - - private string _modDirectory = string.Empty; - private string _modName = string.Empty; - private string _pathInput = string.Empty; - private string _newInstallPath = string.Empty; - private PenumbraApiEc _lastReloadEc; - private PenumbraApiEc _lastAddEc; - private PenumbraApiEc _lastDeleteEc; - private PenumbraApiEc _lastSetPathEc; - private PenumbraApiEc _lastInstallEc; - private IList<(string, string)> _mods = new List<(string, string)>(); - - public readonly EventSubscriber DeleteSubscriber; - public readonly EventSubscriber AddSubscriber; - public readonly EventSubscriber MoveSubscriber; - - private DateTimeOffset _lastDeletedModTime = DateTimeOffset.UnixEpoch; - private string _lastDeletedMod = string.Empty; - private DateTimeOffset _lastAddedModTime = DateTimeOffset.UnixEpoch; - private string _lastAddedMod = string.Empty; - private DateTimeOffset _lastMovedModTime = DateTimeOffset.UnixEpoch; - private string _lastMovedModFrom = string.Empty; - private string _lastMovedModTo = string.Empty; - - public Mods(DalamudPluginInterface pi) - { - _pi = pi; - DeleteSubscriber = Ipc.ModDeleted.Subscriber(pi, s => - { - _lastDeletedModTime = DateTimeOffset.UtcNow; - _lastDeletedMod = s; - }); - AddSubscriber = Ipc.ModAdded.Subscriber(pi, s => - { - _lastAddedModTime = DateTimeOffset.UtcNow; - _lastAddedMod = s; - }); - MoveSubscriber = Ipc.ModMoved.Subscriber(pi, (s1, s2) => - { - _lastMovedModTime = DateTimeOffset.UtcNow; - _lastMovedModFrom = s1; - _lastMovedModTo = s2; - }); - } - - public void Draw() - { - using var _ = ImRaii.TreeNode("Mods"); - if (!_) - return; - - ImGui.InputTextWithHint("##install", "Install File Path...", ref _newInstallPath, 100); - ImGui.InputTextWithHint("##modDir", "Mod Directory Name...", ref _modDirectory, 100); - ImGui.InputTextWithHint("##modName", "Mod Name...", ref _modName, 100); - ImGui.InputTextWithHint("##path", "New Path...", ref _pathInput, 100); - using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - DrawIntro(Ipc.GetMods.Label, "Mods"); - if (ImGui.Button("Get##Mods")) - { - _mods = Ipc.GetMods.Subscriber(_pi).Invoke(); - ImGui.OpenPopup("Mods"); - } - - DrawIntro(Ipc.ReloadMod.Label, "Reload Mod"); - if (ImGui.Button("Reload")) - _lastReloadEc = Ipc.ReloadMod.Subscriber(_pi).Invoke(_modDirectory, _modName); - - ImGui.SameLine(); - ImGui.TextUnformatted(_lastReloadEc.ToString()); - - DrawIntro(Ipc.InstallMod.Label, "Install Mod"); - if (ImGui.Button("Install")) - _lastInstallEc = Ipc.InstallMod.Subscriber(_pi).Invoke(_newInstallPath); - - ImGui.SameLine(); - ImGui.TextUnformatted(_lastInstallEc.ToString()); - - DrawIntro(Ipc.AddMod.Label, "Add Mod"); - if (ImGui.Button("Add")) - _lastAddEc = Ipc.AddMod.Subscriber(_pi).Invoke(_modDirectory); - - ImGui.SameLine(); - ImGui.TextUnformatted(_lastAddEc.ToString()); - - DrawIntro(Ipc.DeleteMod.Label, "Delete Mod"); - if (ImGui.Button("Delete")) - _lastDeleteEc = Ipc.DeleteMod.Subscriber(_pi).Invoke(_modDirectory, _modName); - - ImGui.SameLine(); - ImGui.TextUnformatted(_lastDeleteEc.ToString()); - - DrawIntro(Ipc.GetModPath.Label, "Current Path"); - var (ec, path, def) = Ipc.GetModPath.Subscriber(_pi).Invoke(_modDirectory, _modName); - ImGui.TextUnformatted($"{path} ({(def ? "Custom" : "Default")}) [{ec}]"); - - DrawIntro(Ipc.SetModPath.Label, "Set Path"); - if (ImGui.Button("Set")) - _lastSetPathEc = Ipc.SetModPath.Subscriber(_pi).Invoke(_modDirectory, _modName, _pathInput); - - ImGui.SameLine(); - ImGui.TextUnformatted(_lastSetPathEc.ToString()); - - DrawIntro(Ipc.ModDeleted.Label, "Last Mod Deleted"); - if (_lastDeletedModTime > DateTimeOffset.UnixEpoch) - ImGui.TextUnformatted($"{_lastDeletedMod} at {_lastDeletedModTime}"); - - DrawIntro(Ipc.ModAdded.Label, "Last Mod Added"); - if (_lastAddedModTime > DateTimeOffset.UnixEpoch) - ImGui.TextUnformatted($"{_lastAddedMod} at {_lastAddedModTime}"); - - DrawIntro(Ipc.ModMoved.Label, "Last Mod Moved"); - if (_lastMovedModTime > DateTimeOffset.UnixEpoch) - ImGui.TextUnformatted($"{_lastMovedModFrom} -> {_lastMovedModTo} at {_lastMovedModTime}"); - - DrawModsPopup(); - } - - private void DrawModsPopup() - { - ImGui.SetNextWindowSize(ImGuiHelpers.ScaledVector2(500, 500)); - using var p = ImRaii.Popup("Mods"); - if (!p) - return; - - foreach (var (modDir, modName) in _mods) - ImGui.TextUnformatted($"{modDir}: {modName}"); - - if (ImGui.Button("Close", -Vector2.UnitX) || !ImGui.IsWindowFocused()) - ImGui.CloseCurrentPopup(); - } - } - - private class ModSettings - { - private readonly DalamudPluginInterface _pi; - public readonly EventSubscriber SettingChanged; - - private PenumbraApiEc _lastSettingsError = PenumbraApiEc.Success; - private ModSettingChange _lastSettingChangeType; - private string _lastSettingChangeCollection = string.Empty; - private string _lastSettingChangeMod = string.Empty; - private bool _lastSettingChangeInherited; - private DateTimeOffset _lastSettingChange; - - private string _settingsModDirectory = string.Empty; - private string _settingsModName = string.Empty; - private string _settingsCollection = string.Empty; - private bool _settingsAllowInheritance = true; - private bool _settingsInherit; - private bool _settingsEnabled; - private int _settingsPriority; - private IDictionary, GroupType)>? _availableSettings; - private IDictionary>? _currentSettings; - - public ModSettings(DalamudPluginInterface pi) - { - _pi = pi; - SettingChanged = Ipc.ModSettingChanged.Subscriber(pi, UpdateLastModSetting); - } - - public void Draw() - { - using var _ = ImRaii.TreeNode("Mod Settings"); - if (!_) - return; - - ImGui.InputTextWithHint("##settingsDir", "Mod Directory Name...", ref _settingsModDirectory, 100); - ImGui.InputTextWithHint("##settingsName", "Mod Name...", ref _settingsModName, 100); - ImGui.InputTextWithHint("##settingsCollection", "Collection...", ref _settingsCollection, 100); - ImGui.Checkbox("Allow Inheritance", ref _settingsAllowInheritance); - - using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - DrawIntro("Last Error", _lastSettingsError.ToString()); - DrawIntro(Ipc.ModSettingChanged.Label, "Last Mod Setting Changed"); - ImGui.TextUnformatted(_lastSettingChangeMod.Length > 0 - ? $"{_lastSettingChangeType} of {_lastSettingChangeMod} in {_lastSettingChangeCollection}{(_lastSettingChangeInherited ? " (Inherited)" : string.Empty)} at {_lastSettingChange}" - : "None"); - DrawIntro(Ipc.GetAvailableModSettings.Label, "Get Available Settings"); - if (ImGui.Button("Get##Available")) - { - _availableSettings = Ipc.GetAvailableModSettings.Subscriber(_pi).Invoke(_settingsModDirectory, _settingsModName); - _lastSettingsError = _availableSettings == null ? PenumbraApiEc.ModMissing : PenumbraApiEc.Success; - } - - - DrawIntro(Ipc.GetCurrentModSettings.Label, "Get Current Settings"); - if (ImGui.Button("Get##Current")) - { - var ret = Ipc.GetCurrentModSettings.Subscriber(_pi) - .Invoke(_settingsCollection, _settingsModDirectory, _settingsModName, _settingsAllowInheritance); - _lastSettingsError = ret.Item1; - if (ret.Item1 == PenumbraApiEc.Success) - { - _settingsEnabled = ret.Item2?.Item1 ?? false; - _settingsInherit = ret.Item2?.Item4 ?? false; - _settingsPriority = ret.Item2?.Item2 ?? 0; - _currentSettings = ret.Item2?.Item3; - } - else - { - _currentSettings = null; - } - } - - DrawIntro(Ipc.TryInheritMod.Label, "Inherit Mod"); - ImGui.Checkbox("##inherit", ref _settingsInherit); - ImGui.SameLine(); - if (ImGui.Button("Set##Inherit")) - _lastSettingsError = Ipc.TryInheritMod.Subscriber(_pi) - .Invoke(_settingsCollection, _settingsModDirectory, _settingsModName, _settingsInherit); - - DrawIntro(Ipc.TrySetMod.Label, "Set Enabled"); - ImGui.Checkbox("##enabled", ref _settingsEnabled); - ImGui.SameLine(); - if (ImGui.Button("Set##Enabled")) - _lastSettingsError = Ipc.TrySetMod.Subscriber(_pi) - .Invoke(_settingsCollection, _settingsModDirectory, _settingsModName, _settingsEnabled); - - DrawIntro(Ipc.TrySetModPriority.Label, "Set Priority"); - ImGui.SetNextItemWidth(200 * UiHelpers.Scale); - ImGui.DragInt("##Priority", ref _settingsPriority); - ImGui.SameLine(); - if (ImGui.Button("Set##Priority")) - _lastSettingsError = Ipc.TrySetModPriority.Subscriber(_pi) - .Invoke(_settingsCollection, _settingsModDirectory, _settingsModName, _settingsPriority); - - DrawIntro(Ipc.CopyModSettings.Label, "Copy Mod Settings"); - if (ImGui.Button("Copy Settings")) - _lastSettingsError = Ipc.CopyModSettings.Subscriber(_pi).Invoke(_settingsCollection, _settingsModDirectory, _settingsModName); - - ImGuiUtil.HoverTooltip("Copy settings from Mod Directory Name to Mod Name (as directory) in collection."); - - DrawIntro(Ipc.TrySetModSetting.Label, "Set Setting(s)"); - if (_availableSettings == null) - return; - - foreach (var (group, (list, type)) in _availableSettings) - { - using var id = ImRaii.PushId(group); - var preview = list.Count > 0 ? list[0] : string.Empty; - IList current; - if (_currentSettings != null && _currentSettings.TryGetValue(group, out current!) && current.Count > 0) - { - preview = current[0]; - } - else - { - current = new List(); - if (_currentSettings != null) - _currentSettings[group] = current; - } - - ImGui.SetNextItemWidth(200 * UiHelpers.Scale); - using (var c = ImRaii.Combo("##group", preview)) - { - if (c) - foreach (var s in list) - { - var contained = current.Contains(s); - if (ImGui.Checkbox(s, ref contained)) - { - if (contained) - current.Add(s); - else - current.Remove(s); - } - } - } - - ImGui.SameLine(); - if (ImGui.Button("Set##setting")) - { - if (type == GroupType.Single) - _lastSettingsError = Ipc.TrySetModSetting.Subscriber(_pi).Invoke(_settingsCollection, - _settingsModDirectory, _settingsModName, group, current.Count > 0 ? current[0] : string.Empty); - else - _lastSettingsError = Ipc.TrySetModSettings.Subscriber(_pi).Invoke(_settingsCollection, - _settingsModDirectory, _settingsModName, group, current.ToArray()); - } - - ImGui.SameLine(); - ImGui.TextUnformatted(group); - } - } - - private void UpdateLastModSetting(ModSettingChange type, string collection, string mod, bool inherited) - { - _lastSettingChangeType = type; - _lastSettingChangeCollection = collection; - _lastSettingChangeMod = mod; - _lastSettingChangeInherited = inherited; - _lastSettingChange = DateTimeOffset.Now; - } - } - - private class Editing - { - private readonly DalamudPluginInterface _pi; - - private string _inputPath = string.Empty; - private string _inputPath2 = string.Empty; - private string _outputPath = string.Empty; - private string _outputPath2 = string.Empty; - - private TextureType _typeSelector; - private bool _mipMaps = true; - - private Task? _task1; - private Task? _task2; - - public Editing(DalamudPluginInterface pi) - => _pi = pi; - - public void Draw() - { - using var _ = ImRaii.TreeNode("Editing"); - if (!_) - return; - - ImGui.InputTextWithHint("##inputPath", "Input Texture Path...", ref _inputPath, 256); - ImGui.InputTextWithHint("##outputPath", "Output Texture Path...", ref _outputPath, 256); - ImGui.InputTextWithHint("##inputPath2", "Input Texture Path 2...", ref _inputPath2, 256); - ImGui.InputTextWithHint("##outputPath2", "Output Texture Path 2...", ref _outputPath2, 256); - TypeCombo(); - ImGui.Checkbox("Add MipMaps", ref _mipMaps); - - using var table = ImRaii.Table("...", 3, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - DrawIntro(Ipc.ConvertTextureFile.Label, "Convert Texture 1"); - if (ImGuiUtil.DrawDisabledButton("Save 1", Vector2.Zero, string.Empty, _task1 is { IsCompleted: false })) - _task1 = Ipc.ConvertTextureFile.Subscriber(_pi).Invoke(_inputPath, _outputPath, _typeSelector, _mipMaps); - ImGui.SameLine(); - ImGui.TextUnformatted(_task1 == null ? "Not Initiated" : _task1.Status.ToString()); - if (ImGui.IsItemHovered() && _task1?.Status == TaskStatus.Faulted) - ImGui.SetTooltip(_task1.Exception?.ToString()); - - DrawIntro(Ipc.ConvertTextureFile.Label, "Convert Texture 2"); - if (ImGuiUtil.DrawDisabledButton("Save 2", Vector2.Zero, string.Empty, _task2 is { IsCompleted: false })) - _task2 = Ipc.ConvertTextureFile.Subscriber(_pi).Invoke(_inputPath2, _outputPath2, _typeSelector, _mipMaps); - ImGui.SameLine(); - ImGui.TextUnformatted(_task2 == null ? "Not Initiated" : _task2.Status.ToString()); - if (ImGui.IsItemHovered() && _task2?.Status == TaskStatus.Faulted) - ImGui.SetTooltip(_task2.Exception?.ToString()); - } - - private void TypeCombo() - { - using var combo = ImRaii.Combo("Convert To", _typeSelector.ToString()); - if (!combo) - return; - - foreach (var value in Enum.GetValues()) - { - if (ImGui.Selectable(value.ToString(), _typeSelector == value)) - _typeSelector = value; - } - } - } - - private class Temporary - { - private readonly DalamudPluginInterface _pi; - private readonly ModManager _modManager; - private readonly CollectionManager _collections; - private readonly TempModManager _tempMods; - private readonly TempCollectionManager _tempCollections; - private readonly SaveService _saveService; - private readonly Configuration _config; - - public Temporary(DalamudPluginInterface pi, ModManager modManager, CollectionManager collections, TempModManager tempMods, - TempCollectionManager tempCollections, SaveService saveService, Configuration config) - { - _pi = pi; - _modManager = modManager; - _collections = collections; - _tempMods = tempMods; - _tempCollections = tempCollections; - _saveService = saveService; - _config = config; - } - - public string LastCreatedCollectionName = string.Empty; - - private string _tempCollectionName = string.Empty; - private string _tempCharacterName = string.Empty; - private string _tempModName = string.Empty; - private string _tempGamePath = "test/game/path.mtrl"; - private string _tempFilePath = "test/success.mtrl"; - private string _tempManipulation = string.Empty; - private PenumbraApiEc _lastTempError; - private int _tempActorIndex; - private bool _forceOverwrite; - - public void Draw() - { - using var _ = ImRaii.TreeNode("Temporary"); - if (!_) - return; - - ImGui.InputTextWithHint("##tempCollection", "Collection Name...", ref _tempCollectionName, 128); - ImGui.InputTextWithHint("##tempCollectionChar", "Collection Character...", ref _tempCharacterName, 32); - ImGui.InputInt("##tempActorIndex", ref _tempActorIndex, 0, 0); - ImGui.InputTextWithHint("##tempMod", "Temporary Mod Name...", ref _tempModName, 32); - ImGui.InputTextWithHint("##tempGame", "Game Path...", ref _tempGamePath, 256); - ImGui.InputTextWithHint("##tempFile", "File Path...", ref _tempFilePath, 256); - ImGui.InputTextWithHint("##tempManip", "Manipulation Base64 String...", ref _tempManipulation, 256); - ImGui.Checkbox("Force Character Collection Overwrite", ref _forceOverwrite); - - - using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - DrawIntro("Last Error", _lastTempError.ToString()); - DrawIntro("Last Created Collection", LastCreatedCollectionName); - DrawIntro(Ipc.CreateTemporaryCollection.Label, "Create Temporary Collection"); -#pragma warning disable 0612 - if (ImGui.Button("Create##Collection")) - (_lastTempError, LastCreatedCollectionName) = Ipc.CreateTemporaryCollection.Subscriber(_pi) - .Invoke(_tempCollectionName, _tempCharacterName, _forceOverwrite); - - DrawIntro(Ipc.CreateNamedTemporaryCollection.Label, "Create Named Temporary Collection"); - if (ImGui.Button("Create##NamedCollection")) - _lastTempError = Ipc.CreateNamedTemporaryCollection.Subscriber(_pi).Invoke(_tempCollectionName); - - DrawIntro(Ipc.RemoveTemporaryCollection.Label, "Remove Temporary Collection from Character"); - if (ImGui.Button("Delete##Collection")) - _lastTempError = Ipc.RemoveTemporaryCollection.Subscriber(_pi).Invoke(_tempCharacterName); -#pragma warning restore 0612 - DrawIntro(Ipc.RemoveTemporaryCollectionByName.Label, "Remove Temporary Collection"); - if (ImGui.Button("Delete##NamedCollection")) - _lastTempError = Ipc.RemoveTemporaryCollectionByName.Subscriber(_pi).Invoke(_tempCollectionName); - - DrawIntro(Ipc.AssignTemporaryCollection.Label, "Assign Temporary Collection"); - if (ImGui.Button("Assign##NamedCollection")) - _lastTempError = Ipc.AssignTemporaryCollection.Subscriber(_pi).Invoke(_tempCollectionName, _tempActorIndex, _forceOverwrite); - - DrawIntro(Ipc.AddTemporaryMod.Label, "Add Temporary Mod to specific Collection"); - if (ImGui.Button("Add##Mod")) - _lastTempError = Ipc.AddTemporaryMod.Subscriber(_pi).Invoke(_tempModName, _tempCollectionName, - new Dictionary { { _tempGamePath, _tempFilePath } }, - _tempManipulation.Length > 0 ? _tempManipulation : string.Empty, int.MaxValue); - - DrawIntro(Ipc.CreateTemporaryCollection.Label, "Copy Existing Collection"); - if (ImGuiUtil.DrawDisabledButton("Copy##Collection", Vector2.Zero, - "Copies the effective list from the collection named in Temporary Mod Name...", - !_collections.Storage.ByName(_tempModName, out var copyCollection)) - && copyCollection is { HasCache: true }) - { - var files = copyCollection.ResolvedFiles.ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.Path.ToString()); - var manips = Functions.ToCompressedBase64(copyCollection.MetaCache?.Manipulations.ToArray() ?? Array.Empty(), - MetaManipulation.CurrentVersion); - _lastTempError = Ipc.AddTemporaryMod.Subscriber(_pi).Invoke(_tempModName, _tempCollectionName, files, manips, 999); - } - - DrawIntro(Ipc.AddTemporaryModAll.Label, "Add Temporary Mod to all Collections"); - if (ImGui.Button("Add##All")) - _lastTempError = Ipc.AddTemporaryModAll.Subscriber(_pi).Invoke(_tempModName, - new Dictionary { { _tempGamePath, _tempFilePath } }, - _tempManipulation.Length > 0 ? _tempManipulation : string.Empty, int.MaxValue); - - DrawIntro(Ipc.RemoveTemporaryMod.Label, "Remove Temporary Mod from specific Collection"); - if (ImGui.Button("Remove##Mod")) - _lastTempError = Ipc.RemoveTemporaryMod.Subscriber(_pi).Invoke(_tempModName, _tempCollectionName, int.MaxValue); - - DrawIntro(Ipc.RemoveTemporaryModAll.Label, "Remove Temporary Mod from all Collections"); - if (ImGui.Button("Remove##ModAll")) - _lastTempError = Ipc.RemoveTemporaryModAll.Subscriber(_pi).Invoke(_tempModName, int.MaxValue); - } - - public void DrawCollections() - { - using var collTree = ImRaii.TreeNode("Temporary Collections##TempCollections"); - if (!collTree) - return; - - using var table = ImRaii.Table("##collTree", 5); - if (!table) - return; - - foreach (var collection in _tempCollections.Values) - { - ImGui.TableNextColumn(); - var character = _tempCollections.Collections.Where(p => p.Collection == collection).Select(p => p.DisplayName) - .FirstOrDefault() - ?? "Unknown"; - if (ImGui.Button($"Save##{collection.Name}")) - TemporaryMod.SaveTempCollection(_config, _saveService, _modManager, collection, character); - - ImGuiUtil.DrawTableColumn(collection.Name); - ImGuiUtil.DrawTableColumn(collection.ResolvedFiles.Count.ToString()); - ImGuiUtil.DrawTableColumn(collection.MetaCache?.Count.ToString() ?? "0"); - ImGuiUtil.DrawTableColumn(string.Join(", ", - _tempCollections.Collections.Where(p => p.Collection == collection).Select(c => c.DisplayName))); - } - } - - public void DrawMods() - { - using var modTree = ImRaii.TreeNode("Temporary Mods##TempMods"); - if (!modTree) - return; - - using var table = ImRaii.Table("##modTree", 5); - - void PrintList(string collectionName, IReadOnlyList list) - { - foreach (var mod in list) - { - ImGui.TableNextColumn(); - ImGui.TextUnformatted(mod.Name); - ImGui.TableNextColumn(); - ImGui.TextUnformatted(mod.Priority.ToString()); - ImGui.TableNextColumn(); - ImGui.TextUnformatted(collectionName); - ImGui.TableNextColumn(); - ImGui.TextUnformatted(mod.Default.Files.Count.ToString()); - if (ImGui.IsItemHovered()) - { - using var tt = ImRaii.Tooltip(); - foreach (var (path, file) in mod.Default.Files) - ImGui.TextUnformatted($"{path} -> {file}"); - } - - ImGui.TableNextColumn(); - ImGui.TextUnformatted(mod.TotalManipulations.ToString()); - if (ImGui.IsItemHovered()) - { - using var tt = ImRaii.Tooltip(); - foreach (var manip in mod.Default.Manipulations) - ImGui.TextUnformatted(manip.ToString()); - } - } - } - - if (table) - { - PrintList("All", _tempMods.ModsForAllCollections); - foreach (var (collection, list) in _tempMods.Mods) - PrintList(collection.Name, list); - } - } - } - - private class ResourceTree(DalamudPluginInterface pi, ObjectManager objects) - { - private readonly Stopwatch _stopwatch = new(); - - private string _gameObjectIndices = "0"; - private ResourceType _type = ResourceType.Mtrl; - private bool _withUiData; - - private (string, IReadOnlyDictionary?)[]? _lastGameObjectResourcePaths; - private (string, IReadOnlyDictionary?)[]? _lastPlayerResourcePaths; - private (string, IReadOnlyDictionary?)[]? _lastGameObjectResourcesOfType; - private (string, IReadOnlyDictionary?)[]? _lastPlayerResourcesOfType; - private (string, Ipc.ResourceTree?)[]? _lastGameObjectResourceTrees; - private (string, Ipc.ResourceTree)[]? _lastPlayerResourceTrees; - private TimeSpan _lastCallDuration; - - public void Draw() - { - using var _ = ImRaii.TreeNode("Resource Tree"); - if (!_) - return; - - ImGui.InputText("GameObject indices", ref _gameObjectIndices, 511); - ImGuiUtil.GenericEnumCombo("Resource type", ImGui.CalcItemWidth(), _type, out _type, Enum.GetValues()); - ImGui.Checkbox("Also get names and icons", ref _withUiData); - - using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - DrawIntro(Ipc.GetGameObjectResourcePaths.Label, "Get GameObject resource paths"); - if (ImGui.Button("Get##GameObjectResourcePaths")) - { - var gameObjects = GetSelectedGameObjects(); - var subscriber = Ipc.GetGameObjectResourcePaths.Subscriber(pi); - _stopwatch.Restart(); - var resourcePaths = subscriber.Invoke(gameObjects); - - _lastCallDuration = _stopwatch.Elapsed; - _lastGameObjectResourcePaths = gameObjects - .Select(i => GameObjectToString(i)) - .Zip(resourcePaths) - .ToArray(); - - ImGui.OpenPopup(nameof(Ipc.GetGameObjectResourcePaths)); - } - - DrawIntro(Ipc.GetPlayerResourcePaths.Label, "Get local player resource paths"); - if (ImGui.Button("Get##PlayerResourcePaths")) - { - var subscriber = Ipc.GetPlayerResourcePaths.Subscriber(pi); - _stopwatch.Restart(); - var resourcePaths = subscriber.Invoke(); - - _lastCallDuration = _stopwatch.Elapsed; - _lastPlayerResourcePaths = resourcePaths - .Select(pair => (GameObjectToString(pair.Key), (IReadOnlyDictionary?)pair.Value)) - .ToArray(); - - ImGui.OpenPopup(nameof(Ipc.GetPlayerResourcePaths)); - } - - DrawIntro(Ipc.GetGameObjectResourcesOfType.Label, "Get GameObject resources of type"); - if (ImGui.Button("Get##GameObjectResourcesOfType")) - { - var gameObjects = GetSelectedGameObjects(); - var subscriber = Ipc.GetGameObjectResourcesOfType.Subscriber(pi); - _stopwatch.Restart(); - var resourcesOfType = subscriber.Invoke(_type, _withUiData, gameObjects); - - _lastCallDuration = _stopwatch.Elapsed; - _lastGameObjectResourcesOfType = gameObjects - .Select(i => GameObjectToString(i)) - .Zip(resourcesOfType) - .ToArray(); - - ImGui.OpenPopup(nameof(Ipc.GetGameObjectResourcesOfType)); - } - - DrawIntro(Ipc.GetPlayerResourcesOfType.Label, "Get local player resources of type"); - if (ImGui.Button("Get##PlayerResourcesOfType")) - { - var subscriber = Ipc.GetPlayerResourcesOfType.Subscriber(pi); - _stopwatch.Restart(); - var resourcesOfType = subscriber.Invoke(_type, _withUiData); - - _lastCallDuration = _stopwatch.Elapsed; - _lastPlayerResourcesOfType = resourcesOfType - .Select(pair => (GameObjectToString(pair.Key), (IReadOnlyDictionary?)pair.Value)) - .ToArray(); - - ImGui.OpenPopup(nameof(Ipc.GetPlayerResourcesOfType)); - } - - DrawIntro(Ipc.GetGameObjectResourceTrees.Label, "Get GameObject resource trees"); - if (ImGui.Button("Get##GameObjectResourceTrees")) - { - var gameObjects = GetSelectedGameObjects(); - var subscriber = Ipc.GetGameObjectResourceTrees.Subscriber(pi); - _stopwatch.Restart(); - var trees = subscriber.Invoke(_withUiData, gameObjects); - - _lastCallDuration = _stopwatch.Elapsed; - _lastGameObjectResourceTrees = gameObjects - .Select(i => GameObjectToString(i)) - .Zip(trees) - .ToArray(); - - ImGui.OpenPopup(nameof(Ipc.GetGameObjectResourceTrees)); - } - - DrawIntro(Ipc.GetPlayerResourceTrees.Label, "Get local player resource trees"); - if (ImGui.Button("Get##PlayerResourceTrees")) - { - var subscriber = Ipc.GetPlayerResourceTrees.Subscriber(pi); - _stopwatch.Restart(); - var trees = subscriber.Invoke(_withUiData); - - _lastCallDuration = _stopwatch.Elapsed; - _lastPlayerResourceTrees = trees - .Select(pair => (GameObjectToString(pair.Key), pair.Value)) - .ToArray(); - - ImGui.OpenPopup(nameof(Ipc.GetPlayerResourceTrees)); - } - - DrawPopup(nameof(Ipc.GetGameObjectResourcePaths), ref _lastGameObjectResourcePaths, DrawResourcePaths, _lastCallDuration); - DrawPopup(nameof(Ipc.GetPlayerResourcePaths), ref _lastPlayerResourcePaths, DrawResourcePaths, _lastCallDuration); - - DrawPopup(nameof(Ipc.GetGameObjectResourcesOfType), ref _lastGameObjectResourcesOfType, DrawResourcesOfType, _lastCallDuration); - DrawPopup(nameof(Ipc.GetPlayerResourcesOfType), ref _lastPlayerResourcesOfType, DrawResourcesOfType, _lastCallDuration); - - DrawPopup(nameof(Ipc.GetGameObjectResourceTrees), ref _lastGameObjectResourceTrees, DrawResourceTrees, _lastCallDuration); - DrawPopup(nameof(Ipc.GetPlayerResourceTrees), ref _lastPlayerResourceTrees, DrawResourceTrees!, _lastCallDuration); - } - - private static void DrawPopup(string popupId, ref T? result, Action drawResult, TimeSpan duration) where T : class - { - ImGui.SetNextWindowSize(ImGuiHelpers.ScaledVector2(1000, 500)); - using var popup = ImRaii.Popup(popupId); - if (!popup) - { - result = null; - return; - } - - if (result == null) - { - ImGui.CloseCurrentPopup(); - return; - } - - drawResult(result); - - ImGui.TextUnformatted($"Invoked in {duration.TotalMilliseconds} ms"); - - if (ImGui.Button("Close", -Vector2.UnitX) || !ImGui.IsWindowFocused()) - { - result = null; - ImGui.CloseCurrentPopup(); - } - } - - private static void DrawWithHeaders((string, T?)[] result, Action drawItem) where T : class - { - var firstSeen = new Dictionary(); - foreach (var (label, item) in result) - { - if (item == null) - { - ImRaii.TreeNode($"{label}: null", ImGuiTreeNodeFlags.Leaf).Dispose(); - continue; - } - - if (firstSeen.TryGetValue(item, out var firstLabel)) - { - ImRaii.TreeNode($"{label}: same as {firstLabel}", ImGuiTreeNodeFlags.Leaf).Dispose(); - continue; - } - - firstSeen.Add(item, label); - - using var header = ImRaii.TreeNode(label); - if (!header) - continue; - - drawItem(item); - } - } - - private static void DrawResourcePaths((string, IReadOnlyDictionary?)[] result) - { - DrawWithHeaders(result, paths => - { - using var table = ImRaii.Table(string.Empty, 2, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - ImGui.TableSetupColumn("Actual Path", ImGuiTableColumnFlags.WidthStretch, 0.6f); - ImGui.TableSetupColumn("Game Paths", ImGuiTableColumnFlags.WidthStretch, 0.4f); - ImGui.TableHeadersRow(); - - foreach (var (actualPath, gamePaths) in paths) - { - ImGui.TableNextColumn(); - ImGui.TextUnformatted(actualPath); - ImGui.TableNextColumn(); - foreach (var gamePath in gamePaths) - ImGui.TextUnformatted(gamePath); - } - }); - } - - private void DrawResourcesOfType((string, IReadOnlyDictionary?)[] result) - { - DrawWithHeaders(result, resources => - { - using var table = ImRaii.Table(string.Empty, _withUiData ? 3 : 2, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - ImGui.TableSetupColumn("Resource Handle", ImGuiTableColumnFlags.WidthStretch, 0.15f); - ImGui.TableSetupColumn("Actual Path", ImGuiTableColumnFlags.WidthStretch, _withUiData ? 0.55f : 0.85f); - if (_withUiData) - ImGui.TableSetupColumn("Icon & Name", ImGuiTableColumnFlags.WidthStretch, 0.3f); - ImGui.TableHeadersRow(); - - foreach (var (resourceHandle, (actualPath, name, icon)) in resources) - { - ImGui.TableNextColumn(); - TextUnformattedMono($"0x{resourceHandle:X}"); - ImGui.TableNextColumn(); - ImGui.TextUnformatted(actualPath); - if (_withUiData) - { - ImGui.TableNextColumn(); - TextUnformattedMono(icon.ToString()); - ImGui.SameLine(); - ImGui.TextUnformatted(name); - } - } - }); - } - - private void DrawResourceTrees((string, Ipc.ResourceTree?)[] result) - { - DrawWithHeaders(result, tree => - { - ImGui.TextUnformatted($"Name: {tree.Name}\nRaceCode: {(GenderRace)tree.RaceCode}"); - - using var table = ImRaii.Table(string.Empty, _withUiData ? 7 : 5, ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.Resizable); - if (!table) - return; - - if (_withUiData) - { - ImGui.TableSetupColumn("Name", ImGuiTableColumnFlags.WidthStretch, 0.5f); - ImGui.TableSetupColumn("Type", ImGuiTableColumnFlags.WidthStretch, 0.1f); - ImGui.TableSetupColumn("Icon", ImGuiTableColumnFlags.WidthStretch, 0.15f); - } - else - { - ImGui.TableSetupColumn("Type", ImGuiTableColumnFlags.WidthStretch, 0.5f); - } - - ImGui.TableSetupColumn("Game Path", ImGuiTableColumnFlags.WidthStretch, 0.5f); - ImGui.TableSetupColumn("Actual Path", ImGuiTableColumnFlags.WidthStretch, 0.5f); - ImGui.TableSetupColumn("Object Address", ImGuiTableColumnFlags.WidthStretch, 0.2f); - ImGui.TableSetupColumn("Resource Handle", ImGuiTableColumnFlags.WidthStretch, 0.2f); - ImGui.TableHeadersRow(); - - void DrawNode(Ipc.ResourceNode node) - { - ImGui.TableNextRow(); - ImGui.TableNextColumn(); - var hasChildren = node.Children.Any(); - using var treeNode = ImRaii.TreeNode( - $"{(_withUiData ? node.Name ?? "Unknown" : node.Type)}##{node.ObjectAddress:X8}", - hasChildren - ? ImGuiTreeNodeFlags.SpanFullWidth - : ImGuiTreeNodeFlags.SpanFullWidth | ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.NoTreePushOnOpen); - if (_withUiData) - { - ImGui.TableNextColumn(); - TextUnformattedMono(node.Type.ToString()); - ImGui.TableNextColumn(); - TextUnformattedMono(node.Icon.ToString()); - } - - ImGui.TableNextColumn(); - ImGui.TextUnformatted(node.GamePath ?? "Unknown"); - ImGui.TableNextColumn(); - ImGui.TextUnformatted(node.ActualPath); - ImGui.TableNextColumn(); - TextUnformattedMono($"0x{node.ObjectAddress:X8}"); - ImGui.TableNextColumn(); - TextUnformattedMono($"0x{node.ResourceHandle:X8}"); - - if (treeNode) - foreach (var child in node.Children) - DrawNode(child); - } - - foreach (var node in tree.Nodes) - DrawNode(node); - }); - } - - private static void TextUnformattedMono(string text) - { - using var _ = ImRaii.PushFont(UiBuilder.MonoFont); - ImGui.TextUnformatted(text); - } - - private ushort[] GetSelectedGameObjects() - => _gameObjectIndices.Split(',') - .SelectWhere(index => (ushort.TryParse(index.Trim(), out var i), i)) - .ToArray(); - - private unsafe string GameObjectToString(ObjectIndex gameObjectIndex) - { - var gameObject = objects[gameObjectIndex]; - - return gameObject.Valid - ? $"[{gameObjectIndex}] {gameObject.Utf8Name} ({(ObjectKind)gameObject.AsObject->ObjectKind})" - : $"[{gameObjectIndex}] null"; - } - } -} diff --git a/Penumbra/Api/IpcTester/CollectionsIpcTester.cs b/Penumbra/Api/IpcTester/CollectionsIpcTester.cs new file mode 100644 index 00000000..12314f0c --- /dev/null +++ b/Penumbra/Api/IpcTester/CollectionsIpcTester.cs @@ -0,0 +1,166 @@ +using Dalamud.Interface; +using Dalamud.Interface.Utility; +using Dalamud.Plugin; +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Api.IpcSubscribers; +using Penumbra.Collections.Manager; +using Penumbra.GameData.Enums; +using ImGuiClip = OtterGui.ImGuiClip; + +namespace Penumbra.Api.IpcTester; + +public class CollectionsIpcTester(DalamudPluginInterface pi) : IUiService +{ + private int _objectIdx; + private string _collectionIdString = string.Empty; + private Guid? _collectionId = null; + private bool _allowCreation = true; + private bool _allowDeletion = true; + private ApiCollectionType _type = ApiCollectionType.Yourself; + + private Dictionary _collections = []; + private (string, ChangedItemType, uint)[] _changedItems = []; + private PenumbraApiEc _returnCode = PenumbraApiEc.Success; + private (Guid Id, string Name)? _oldCollection; + + public void Draw() + { + using var _ = ImRaii.TreeNode("Collections"); + if (!_) + return; + + ImGuiUtil.GenericEnumCombo("Collection Type", 200, _type, out _type, t => ((CollectionType)t).ToName()); + ImGui.InputInt("Object Index##Collections", ref _objectIdx, 0, 0); + ImGuiUtil.GuidInput("Collection Id##Collections", "Collection GUID...", string.Empty, ref _collectionId, ref _collectionIdString); + ImGui.Checkbox("Allow Assignment Creation", ref _allowCreation); + ImGui.SameLine(); + ImGui.Checkbox("Allow Assignment Deletion", ref _allowDeletion); + + using var table = ImRaii.Table(string.Empty, 4, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + IpcTester.DrawIntro("Last Return Code", _returnCode.ToString()); + if (_oldCollection != null) + ImGui.TextUnformatted(!_oldCollection.HasValue ? "Created" : _oldCollection.ToString()); + + IpcTester.DrawIntro(GetCollection.Label, "Current Collection"); + DrawCollection(new GetCollection(pi).Invoke(ApiCollectionType.Current)); + + IpcTester.DrawIntro(GetCollection.Label, "Default Collection"); + DrawCollection(new GetCollection(pi).Invoke(ApiCollectionType.Default)); + + IpcTester.DrawIntro(GetCollection.Label, "Interface Collection"); + DrawCollection(new GetCollection(pi).Invoke(ApiCollectionType.Interface)); + + IpcTester.DrawIntro(GetCollection.Label, "Special Collection"); + DrawCollection(new GetCollection(pi).Invoke(_type)); + + IpcTester.DrawIntro(GetCollections.Label, "Collections"); + DrawCollectionPopup(); + if (ImGui.Button("Get##Collections")) + { + _collections = new GetCollections(pi).Invoke(); + ImGui.OpenPopup("Collections"); + } + + IpcTester.DrawIntro(GetCollectionForObject.Label, "Get Object Collection"); + var (valid, individual, effectiveCollection) = new GetCollectionForObject(pi).Invoke(_objectIdx); + DrawCollection(effectiveCollection); + ImGui.SameLine(); + ImGui.TextUnformatted($"({(valid ? "Valid" : "Invalid")} Object{(individual ? ", Individual Assignment)" : ")")}"); + + IpcTester.DrawIntro(SetCollection.Label, "Set Special Collection"); + if (ImGui.Button("Set##SpecialCollection")) + (_returnCode, _oldCollection) = + new SetCollection(pi).Invoke(_type, _collectionId.GetValueOrDefault(Guid.Empty), _allowCreation, _allowDeletion); + ImGui.TableNextColumn(); + if (ImGui.Button("Remove##SpecialCollection")) + (_returnCode, _oldCollection) = new SetCollection(pi).Invoke(_type, null, _allowCreation, _allowDeletion); + + IpcTester.DrawIntro(SetCollectionForObject.Label, "Set Object Collection"); + if (ImGui.Button("Set##ObjectCollection")) + (_returnCode, _oldCollection) = new SetCollectionForObject(pi).Invoke(_objectIdx, _collectionId.GetValueOrDefault(Guid.Empty), + _allowCreation, _allowDeletion); + ImGui.TableNextColumn(); + if (ImGui.Button("Remove##ObjectCollection")) + (_returnCode, _oldCollection) = new SetCollectionForObject(pi).Invoke(_objectIdx, null, _allowCreation, _allowDeletion); + + IpcTester.DrawIntro(GetChangedItemsForCollection.Label, "Changed Item List"); + DrawChangedItemPopup(); + if (ImGui.Button("Get##ChangedItems")) + { + var items = new GetChangedItemsForCollection(pi).Invoke(_collectionId.GetValueOrDefault(Guid.Empty)); + _changedItems = items.Select(kvp => + { + var (type, id) = ChangedItemExtensions.ChangedItemToTypeAndId(kvp.Value); + return (kvp.Key, type, id); + }).ToArray(); + ImGui.OpenPopup("Changed Item List"); + } + } + + private void DrawChangedItemPopup() + { + ImGui.SetNextWindowSize(ImGuiHelpers.ScaledVector2(500, 500)); + using var p = ImRaii.Popup("Changed Item List"); + if (!p) + return; + + using (var t = ImRaii.Table("##ChangedItems", 3, ImGuiTableFlags.SizingFixedFit)) + { + if (t) + ImGuiClip.ClippedDraw(_changedItems, t => + { + ImGuiUtil.DrawTableColumn(t.Item1); + ImGuiUtil.DrawTableColumn(t.Item2.ToString()); + ImGuiUtil.DrawTableColumn(t.Item3.ToString()); + }, ImGui.GetTextLineHeightWithSpacing()); + } + + if (ImGui.Button("Close", -Vector2.UnitX) || !ImGui.IsWindowFocused()) + ImGui.CloseCurrentPopup(); + } + + private void DrawCollectionPopup() + { + ImGui.SetNextWindowSize(ImGuiHelpers.ScaledVector2(500, 500)); + using var p = ImRaii.Popup("Collections"); + if (!p) + return; + + using (var t = ImRaii.Table("collections", 2, ImGuiTableFlags.SizingFixedFit)) + { + if (t) + foreach (var collection in _collections) + { + ImGui.TableNextColumn(); + DrawCollection((collection.Key, collection.Value)); + } + } + + if (ImGui.Button("Close", -Vector2.UnitX) || !ImGui.IsWindowFocused()) + ImGui.CloseCurrentPopup(); + } + + private static void DrawCollection((Guid Id, string Name)? collection) + { + if (collection == null) + { + ImGui.TextUnformatted(""); + ImGui.TableNextColumn(); + return; + } + + ImGui.TextUnformatted(collection.Value.Name); + ImGui.TableNextColumn(); + using (ImRaii.PushFont(UiBuilder.MonoFont)) + { + ImGuiUtil.CopyOnClickSelectable(collection.Value.Id.ToString()); + } + } +} diff --git a/Penumbra/Api/IpcTester/EditingIpcTester.cs b/Penumbra/Api/IpcTester/EditingIpcTester.cs new file mode 100644 index 00000000..94b1e4e8 --- /dev/null +++ b/Penumbra/Api/IpcTester/EditingIpcTester.cs @@ -0,0 +1,70 @@ +using Dalamud.Plugin; +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Api.IpcSubscribers; + +namespace Penumbra.Api.IpcTester; + +public class EditingIpcTester(DalamudPluginInterface pi) : IUiService +{ + private string _inputPath = string.Empty; + private string _inputPath2 = string.Empty; + private string _outputPath = string.Empty; + private string _outputPath2 = string.Empty; + + private TextureType _typeSelector; + private bool _mipMaps = true; + + private Task? _task1; + private Task? _task2; + + public void Draw() + { + using var _ = ImRaii.TreeNode("Editing"); + if (!_) + return; + + ImGui.InputTextWithHint("##inputPath", "Input Texture Path...", ref _inputPath, 256); + ImGui.InputTextWithHint("##outputPath", "Output Texture Path...", ref _outputPath, 256); + ImGui.InputTextWithHint("##inputPath2", "Input Texture Path 2...", ref _inputPath2, 256); + ImGui.InputTextWithHint("##outputPath2", "Output Texture Path 2...", ref _outputPath2, 256); + TypeCombo(); + ImGui.Checkbox("Add MipMaps", ref _mipMaps); + + using var table = ImRaii.Table("...", 3, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + IpcTester.DrawIntro(ConvertTextureFile.Label, (string)"Convert Texture 1"); + if (ImGuiUtil.DrawDisabledButton("Save 1", Vector2.Zero, string.Empty, _task1 is { IsCompleted: false })) + _task1 = new ConvertTextureFile(pi).Invoke(_inputPath, _outputPath, _typeSelector, _mipMaps); + ImGui.SameLine(); + ImGui.TextUnformatted(_task1 == null ? "Not Initiated" : _task1.Status.ToString()); + if (ImGui.IsItemHovered() && _task1?.Status == TaskStatus.Faulted) + ImGui.SetTooltip(_task1.Exception?.ToString()); + + IpcTester.DrawIntro(ConvertTextureFile.Label, (string)"Convert Texture 2"); + if (ImGuiUtil.DrawDisabledButton("Save 2", Vector2.Zero, string.Empty, _task2 is { IsCompleted: false })) + _task2 = new ConvertTextureFile(pi).Invoke(_inputPath2, _outputPath2, _typeSelector, _mipMaps); + ImGui.SameLine(); + ImGui.TextUnformatted(_task2 == null ? "Not Initiated" : _task2.Status.ToString()); + if (ImGui.IsItemHovered() && _task2?.Status == TaskStatus.Faulted) + ImGui.SetTooltip(_task2.Exception?.ToString()); + } + + private void TypeCombo() + { + using var combo = ImRaii.Combo("Convert To", _typeSelector.ToString()); + if (!combo) + return; + + foreach (var value in Enum.GetValues()) + { + if (ImGui.Selectable(value.ToString(), _typeSelector == value)) + _typeSelector = value; + } + } +} diff --git a/Penumbra/Api/IpcTester/GameStateIpcTester.cs b/Penumbra/Api/IpcTester/GameStateIpcTester.cs new file mode 100644 index 00000000..2c41b882 --- /dev/null +++ b/Penumbra/Api/IpcTester/GameStateIpcTester.cs @@ -0,0 +1,137 @@ +using Dalamud.Interface; +using Dalamud.Plugin; +using ImGuiNET; +using OtterGui.Raii; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Api.Helpers; +using Penumbra.Api.IpcSubscribers; +using Penumbra.String; + +namespace Penumbra.Api.IpcTester; + +public class GameStateIpcTester : IUiService, IDisposable +{ + private readonly DalamudPluginInterface _pi; + public readonly EventSubscriber CharacterBaseCreating; + public readonly EventSubscriber CharacterBaseCreated; + public readonly EventSubscriber GameObjectResourcePathResolved; + + private string _lastCreatedGameObjectName = string.Empty; + private nint _lastCreatedDrawObject = nint.Zero; + private DateTimeOffset _lastCreatedGameObjectTime = DateTimeOffset.MaxValue; + private string _lastResolvedGamePath = string.Empty; + private string _lastResolvedFullPath = string.Empty; + private string _lastResolvedObject = string.Empty; + private DateTimeOffset _lastResolvedGamePathTime = DateTimeOffset.MaxValue; + private string _currentDrawObjectString = string.Empty; + private nint _currentDrawObject = nint.Zero; + private int _currentCutsceneActor; + private int _currentCutsceneParent; + private PenumbraApiEc _cutsceneError = PenumbraApiEc.Success; + + public GameStateIpcTester(DalamudPluginInterface pi) + { + _pi = pi; + CharacterBaseCreating = CreatingCharacterBase.Subscriber(pi, UpdateLastCreated); + CharacterBaseCreated = CreatedCharacterBase.Subscriber(pi, UpdateLastCreated2); + GameObjectResourcePathResolved = IpcSubscribers.GameObjectResourcePathResolved.Subscriber(pi, UpdateGameObjectResourcePath); + } + + public void Dispose() + { + CharacterBaseCreating.Dispose(); + CharacterBaseCreated.Dispose(); + GameObjectResourcePathResolved.Dispose(); + } + + public void Draw() + { + using var _ = ImRaii.TreeNode("Game State"); + if (!_) + return; + + if (ImGui.InputTextWithHint("##drawObject", "Draw Object Address..", ref _currentDrawObjectString, 16, + ImGuiInputTextFlags.CharsHexadecimal)) + _currentDrawObject = nint.TryParse(_currentDrawObjectString, NumberStyles.HexNumber, CultureInfo.InvariantCulture, + out var tmp) + ? tmp + : nint.Zero; + + ImGui.InputInt("Cutscene Actor", ref _currentCutsceneActor, 0); + ImGui.InputInt("Cutscene Parent", ref _currentCutsceneParent, 0); + if (_cutsceneError is not PenumbraApiEc.Success) + { + ImGui.SameLine(); + ImGui.TextUnformatted("Invalid Argument on last Call"); + } + + using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + IpcTester.DrawIntro(GetDrawObjectInfo.Label, "Draw Object Info"); + if (_currentDrawObject == nint.Zero) + { + ImGui.TextUnformatted("Invalid"); + } + else + { + var (ptr, (collectionId, collectionName)) = new GetDrawObjectInfo(_pi).Invoke(_currentDrawObject); + ImGui.TextUnformatted(ptr == nint.Zero ? $"No Actor Associated, {collectionName}" : $"{ptr:X}, {collectionName}"); + ImGui.SameLine(); + using (ImRaii.PushFont(UiBuilder.MonoFont)) + { + ImGui.TextUnformatted(collectionId.ToString()); + } + } + + IpcTester.DrawIntro(GetCutsceneParentIndex.Label, "Cutscene Parent"); + ImGui.TextUnformatted(new GetCutsceneParentIndex(_pi).Invoke(_currentCutsceneActor).ToString()); + + IpcTester.DrawIntro(SetCutsceneParentIndex.Label, "Cutscene Parent"); + if (ImGui.Button("Set Parent")) + _cutsceneError = new SetCutsceneParentIndex(_pi) + .Invoke(_currentCutsceneActor, _currentCutsceneParent); + + IpcTester.DrawIntro(CreatingCharacterBase.Label, "Last Drawobject created"); + if (_lastCreatedGameObjectTime < DateTimeOffset.Now) + ImGui.TextUnformatted(_lastCreatedDrawObject != nint.Zero + ? $"0x{_lastCreatedDrawObject:X} for <{_lastCreatedGameObjectName}> at {_lastCreatedGameObjectTime}" + : $"NULL for <{_lastCreatedGameObjectName}> at {_lastCreatedGameObjectTime}"); + + IpcTester.DrawIntro(IpcSubscribers.GameObjectResourcePathResolved.Label, "Last GamePath resolved"); + if (_lastResolvedGamePathTime < DateTimeOffset.Now) + ImGui.TextUnformatted( + $"{_lastResolvedGamePath} -> {_lastResolvedFullPath} for <{_lastResolvedObject}> at {_lastResolvedGamePathTime}"); + } + + private void UpdateLastCreated(nint gameObject, Guid _, nint _2, nint _3, nint _4) + { + _lastCreatedGameObjectName = GetObjectName(gameObject); + _lastCreatedGameObjectTime = DateTimeOffset.Now; + _lastCreatedDrawObject = nint.Zero; + } + + private void UpdateLastCreated2(nint gameObject, Guid _, nint drawObject) + { + _lastCreatedGameObjectName = GetObjectName(gameObject); + _lastCreatedGameObjectTime = DateTimeOffset.Now; + _lastCreatedDrawObject = drawObject; + } + + private void UpdateGameObjectResourcePath(nint gameObject, string gamePath, string fullPath) + { + _lastResolvedObject = GetObjectName(gameObject); + _lastResolvedGamePath = gamePath; + _lastResolvedFullPath = fullPath; + _lastResolvedGamePathTime = DateTimeOffset.Now; + } + + private static unsafe string GetObjectName(nint gameObject) + { + var obj = (FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject*)gameObject; + var name = obj != null ? obj->Name : null; + return name != null && *name != 0 ? new ByteString(name).ToString() : "Unknown"; + } +} diff --git a/Penumbra/Api/IpcTester/IpcTester.cs b/Penumbra/Api/IpcTester/IpcTester.cs new file mode 100644 index 00000000..201e7068 --- /dev/null +++ b/Penumbra/Api/IpcTester/IpcTester.cs @@ -0,0 +1,133 @@ +using Dalamud.Plugin.Services; +using FFXIVClientStructs.FFXIV.Client.System.Framework; +using ImGuiNET; +using OtterGui.Services; +using Penumbra.Api.Api; + +namespace Penumbra.Api.IpcTester; + +public class IpcTester( + IpcProviders ipcProviders, + IPenumbraApi api, + PluginStateIpcTester pluginStateIpcTester, + UiIpcTester uiIpcTester, + RedrawingIpcTester redrawingIpcTester, + GameStateIpcTester gameStateIpcTester, + ResolveIpcTester resolveIpcTester, + CollectionsIpcTester collectionsIpcTester, + MetaIpcTester metaIpcTester, + ModsIpcTester modsIpcTester, + ModSettingsIpcTester modSettingsIpcTester, + EditingIpcTester editingIpcTester, + TemporaryIpcTester temporaryIpcTester, + ResourceTreeIpcTester resourceTreeIpcTester, + IFramework framework) : IUiService +{ + private readonly IpcProviders _ipcProviders = ipcProviders; + private DateTime _lastUpdate; + private bool _subscribed = false; + + public void Draw() + { + try + { + _lastUpdate = framework.LastUpdateUTC.AddSeconds(1); + Subscribe(); + + ImGui.TextUnformatted($"API Version: {api.ApiVersion.Breaking}.{api.ApiVersion.Feature:D4}"); + collectionsIpcTester.Draw(); + editingIpcTester.Draw(); + gameStateIpcTester.Draw(); + metaIpcTester.Draw(); + modSettingsIpcTester.Draw(); + modsIpcTester.Draw(); + pluginStateIpcTester.Draw(); + redrawingIpcTester.Draw(); + resolveIpcTester.Draw(); + resourceTreeIpcTester.Draw(); + uiIpcTester.Draw(); + temporaryIpcTester.Draw(); + temporaryIpcTester.DrawCollections(); + temporaryIpcTester.DrawMods(); + } + catch (Exception e) + { + Penumbra.Log.Error($"Error during IPC Tests:\n{e}"); + } + } + + internal static void DrawIntro(string label, string info) + { + ImGui.TableNextRow(); + ImGui.TableNextColumn(); + ImGui.TextUnformatted(label); + ImGui.TableNextColumn(); + ImGui.TextUnformatted(info); + ImGui.TableNextColumn(); + } + + private void Subscribe() + { + if (_subscribed) + return; + + Penumbra.Log.Debug("[IPCTester] Subscribed to IPC events for IPC tester."); + gameStateIpcTester.GameObjectResourcePathResolved.Enable(); + gameStateIpcTester.CharacterBaseCreated.Enable(); + gameStateIpcTester.CharacterBaseCreating.Enable(); + modSettingsIpcTester.SettingChanged.Enable(); + modsIpcTester.DeleteSubscriber.Enable(); + modsIpcTester.AddSubscriber.Enable(); + modsIpcTester.MoveSubscriber.Enable(); + pluginStateIpcTester.ModDirectoryChanged.Enable(); + pluginStateIpcTester.Initialized.Enable(); + pluginStateIpcTester.Disposed.Enable(); + pluginStateIpcTester.EnabledChange.Enable(); + redrawingIpcTester.Redrawn.Enable(); + uiIpcTester.PreSettingsTabBar.Enable(); + uiIpcTester.PreSettingsPanel.Enable(); + uiIpcTester.PostEnabled.Enable(); + uiIpcTester.PostSettingsPanelDraw.Enable(); + uiIpcTester.ChangedItemTooltip.Enable(); + uiIpcTester.ChangedItemClicked.Enable(); + + framework.Update += CheckUnsubscribe; + _subscribed = true; + } + + private void CheckUnsubscribe(IFramework framework1) + { + if (_lastUpdate > framework.LastUpdateUTC) + return; + + Unsubscribe(); + framework.Update -= CheckUnsubscribe; + } + + private void Unsubscribe() + { + if (!_subscribed) + return; + + Penumbra.Log.Debug("[IPCTester] Unsubscribed from IPC events for IPC tester."); + _subscribed = false; + gameStateIpcTester.GameObjectResourcePathResolved.Disable(); + gameStateIpcTester.CharacterBaseCreated.Disable(); + gameStateIpcTester.CharacterBaseCreating.Disable(); + modSettingsIpcTester.SettingChanged.Disable(); + modsIpcTester.DeleteSubscriber.Disable(); + modsIpcTester.AddSubscriber.Disable(); + modsIpcTester.MoveSubscriber.Disable(); + pluginStateIpcTester.ModDirectoryChanged.Disable(); + pluginStateIpcTester.Initialized.Disable(); + pluginStateIpcTester.Disposed.Disable(); + pluginStateIpcTester.EnabledChange.Disable(); + redrawingIpcTester.Redrawn.Disable(); + uiIpcTester.PreSettingsTabBar.Disable(); + uiIpcTester.PreSettingsPanel.Disable(); + uiIpcTester.PostEnabled.Disable(); + uiIpcTester.PostSettingsPanelDraw.Disable(); + uiIpcTester.ChangedItemTooltip.Disable(); + uiIpcTester.ChangedItemClicked.Disable(); + } +} diff --git a/Penumbra/Api/IpcTester/MetaIpcTester.cs b/Penumbra/Api/IpcTester/MetaIpcTester.cs new file mode 100644 index 00000000..3fa7de7f --- /dev/null +++ b/Penumbra/Api/IpcTester/MetaIpcTester.cs @@ -0,0 +1,38 @@ +using Dalamud.Plugin; +using ImGuiNET; +using OtterGui.Raii; +using OtterGui.Services; +using Penumbra.Api.IpcSubscribers; + +namespace Penumbra.Api.IpcTester; + +public class MetaIpcTester(DalamudPluginInterface pi) : IUiService +{ + private int _gameObjectIndex; + + public void Draw() + { + using var _ = ImRaii.TreeNode("Meta"); + if (!_) + return; + + ImGui.InputInt("##metaIdx", ref _gameObjectIndex, 0, 0); + using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + IpcTester.DrawIntro(GetPlayerMetaManipulations.Label, "Player Meta Manipulations"); + if (ImGui.Button("Copy to Clipboard##Player")) + { + var base64 = new GetPlayerMetaManipulations(pi).Invoke(); + ImGui.SetClipboardText(base64); + } + + IpcTester.DrawIntro(GetMetaManipulations.Label, "Game Object Manipulations"); + if (ImGui.Button("Copy to Clipboard##GameObject")) + { + var base64 = new GetMetaManipulations(pi).Invoke(_gameObjectIndex); + ImGui.SetClipboardText(base64); + } + } +} diff --git a/Penumbra/Api/IpcTester/ModSettingsIpcTester.cs b/Penumbra/Api/IpcTester/ModSettingsIpcTester.cs new file mode 100644 index 00000000..c33fcdee --- /dev/null +++ b/Penumbra/Api/IpcTester/ModSettingsIpcTester.cs @@ -0,0 +1,181 @@ +using Dalamud.Plugin; +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Api.Helpers; +using Penumbra.Api.IpcSubscribers; +using Penumbra.UI; + +namespace Penumbra.Api.IpcTester; + +public class ModSettingsIpcTester : IUiService, IDisposable +{ + private readonly DalamudPluginInterface _pi; + public readonly EventSubscriber SettingChanged; + + private PenumbraApiEc _lastSettingsError = PenumbraApiEc.Success; + private ModSettingChange _lastSettingChangeType; + private Guid _lastSettingChangeCollection = Guid.Empty; + private string _lastSettingChangeMod = string.Empty; + private bool _lastSettingChangeInherited; + private DateTimeOffset _lastSettingChange; + + private string _settingsModDirectory = string.Empty; + private string _settingsModName = string.Empty; + private Guid? _settingsCollection; + private string _settingsCollectionName = string.Empty; + private bool _settingsIgnoreInheritance; + private bool _settingsInherit; + private bool _settingsEnabled; + private int _settingsPriority; + private IReadOnlyDictionary? _availableSettings; + private Dictionary>? _currentSettings; + + public ModSettingsIpcTester(DalamudPluginInterface pi) + { + _pi = pi; + SettingChanged = ModSettingChanged.Subscriber(pi, UpdateLastModSetting); + } + + public void Dispose() + { + SettingChanged.Dispose(); + } + + public void Draw() + { + using var _ = ImRaii.TreeNode("Mod Settings"); + if (!_) + return; + + ImGui.InputTextWithHint("##settingsDir", "Mod Directory Name...", ref _settingsModDirectory, 100); + ImGui.InputTextWithHint("##settingsName", "Mod Name...", ref _settingsModName, 100); + ImGuiUtil.GuidInput("##settingsCollection", "Collection...", string.Empty, ref _settingsCollection, ref _settingsCollectionName); + ImGui.Checkbox("Ignore Inheritance", ref _settingsIgnoreInheritance); + var collection = _settingsCollection.GetValueOrDefault(Guid.Empty); + + using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + IpcTester.DrawIntro("Last Error", _lastSettingsError.ToString()); + + IpcTester.DrawIntro(ModSettingChanged.Label, "Last Mod Setting Changed"); + ImGui.TextUnformatted(_lastSettingChangeMod.Length > 0 + ? $"{_lastSettingChangeType} of {_lastSettingChangeMod} in {_lastSettingChangeCollection}{(_lastSettingChangeInherited ? " (Inherited)" : string.Empty)} at {_lastSettingChange}" + : "None"); + + IpcTester.DrawIntro(GetAvailableModSettings.Label, "Get Available Settings"); + if (ImGui.Button("Get##Available")) + { + _availableSettings = new GetAvailableModSettings(_pi).Invoke(_settingsModDirectory, _settingsModName); + _lastSettingsError = _availableSettings == null ? PenumbraApiEc.ModMissing : PenumbraApiEc.Success; + } + + IpcTester.DrawIntro(GetCurrentModSettings.Label, "Get Current Settings"); + if (ImGui.Button("Get##Current")) + { + var ret = new GetCurrentModSettings(_pi) + .Invoke(collection, _settingsModDirectory, _settingsModName, _settingsIgnoreInheritance); + _lastSettingsError = ret.Item1; + if (ret.Item1 == PenumbraApiEc.Success) + { + _settingsEnabled = ret.Item2?.Item1 ?? false; + _settingsInherit = ret.Item2?.Item4 ?? true; + _settingsPriority = ret.Item2?.Item2 ?? 0; + _currentSettings = ret.Item2?.Item3; + } + else + { + _currentSettings = null; + } + } + + IpcTester.DrawIntro(TryInheritMod.Label, "Inherit Mod"); + ImGui.Checkbox("##inherit", ref _settingsInherit); + ImGui.SameLine(); + if (ImGui.Button("Set##Inherit")) + _lastSettingsError = new TryInheritMod(_pi) + .Invoke(collection, _settingsModDirectory, _settingsInherit, _settingsModName); + + IpcTester.DrawIntro(TrySetMod.Label, "Set Enabled"); + ImGui.Checkbox("##enabled", ref _settingsEnabled); + ImGui.SameLine(); + if (ImGui.Button("Set##Enabled")) + _lastSettingsError = new TrySetMod(_pi) + .Invoke(collection, _settingsModDirectory, _settingsEnabled, _settingsModName); + + IpcTester.DrawIntro(TrySetModPriority.Label, "Set Priority"); + ImGui.SetNextItemWidth(200 * UiHelpers.Scale); + ImGui.DragInt("##Priority", ref _settingsPriority); + ImGui.SameLine(); + if (ImGui.Button("Set##Priority")) + _lastSettingsError = new TrySetModPriority(_pi) + .Invoke(collection, _settingsModDirectory, _settingsPriority, _settingsModName); + + IpcTester.DrawIntro(CopyModSettings.Label, "Copy Mod Settings"); + if (ImGui.Button("Copy Settings")) + _lastSettingsError = new CopyModSettings(_pi) + .Invoke(_settingsCollection, _settingsModDirectory, _settingsModName); + + ImGuiUtil.HoverTooltip("Copy settings from Mod Directory Name to Mod Name (as directory) in collection."); + + IpcTester.DrawIntro(TrySetModSetting.Label, "Set Setting(s)"); + if (_availableSettings == null) + return; + + foreach (var (group, (list, type)) in _availableSettings) + { + using var id = ImRaii.PushId(group); + var preview = list.Length > 0 ? list[0] : string.Empty; + if (_currentSettings != null && _currentSettings.TryGetValue(group, out var current) && current.Count > 0) + { + preview = current[0]; + } + else + { + current = []; + if (_currentSettings != null) + _currentSettings[group] = current; + } + + ImGui.SetNextItemWidth(200 * UiHelpers.Scale); + using (var c = ImRaii.Combo("##group", preview)) + { + if (c) + foreach (var s in list) + { + var contained = current.Contains(s); + if (ImGui.Checkbox(s, ref contained)) + { + if (contained) + current.Add(s); + else + current.Remove(s); + } + } + } + + ImGui.SameLine(); + if (ImGui.Button("Set##setting")) + _lastSettingsError = type == GroupType.Single + ? new TrySetModSetting(_pi).Invoke(collection, _settingsModDirectory, group, current.Count > 0 ? current[0] : string.Empty, + _settingsModName) + : new TrySetModSettings(_pi).Invoke(collection, _settingsModDirectory, group, current.ToArray(), _settingsModName); + + ImGui.SameLine(); + ImGui.TextUnformatted(group); + } + } + + private void UpdateLastModSetting(ModSettingChange type, Guid collection, string mod, bool inherited) + { + _lastSettingChangeType = type; + _lastSettingChangeCollection = collection; + _lastSettingChangeMod = mod; + _lastSettingChangeInherited = inherited; + _lastSettingChange = DateTimeOffset.Now; + } +} diff --git a/Penumbra/Api/IpcTester/ModsIpcTester.cs b/Penumbra/Api/IpcTester/ModsIpcTester.cs new file mode 100644 index 00000000..878a8214 --- /dev/null +++ b/Penumbra/Api/IpcTester/ModsIpcTester.cs @@ -0,0 +1,154 @@ +using Dalamud.Interface.Utility; +using Dalamud.Plugin; +using ImGuiNET; +using OtterGui.Raii; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Api.Helpers; +using Penumbra.Api.IpcSubscribers; + +namespace Penumbra.Api.IpcTester; + +public class ModsIpcTester : IUiService, IDisposable +{ + private readonly DalamudPluginInterface _pi; + + private string _modDirectory = string.Empty; + private string _modName = string.Empty; + private string _pathInput = string.Empty; + private string _newInstallPath = string.Empty; + private PenumbraApiEc _lastReloadEc; + private PenumbraApiEc _lastAddEc; + private PenumbraApiEc _lastDeleteEc; + private PenumbraApiEc _lastSetPathEc; + private PenumbraApiEc _lastInstallEc; + private Dictionary _mods = []; + + public readonly EventSubscriber DeleteSubscriber; + public readonly EventSubscriber AddSubscriber; + public readonly EventSubscriber MoveSubscriber; + + private DateTimeOffset _lastDeletedModTime = DateTimeOffset.UnixEpoch; + private string _lastDeletedMod = string.Empty; + private DateTimeOffset _lastAddedModTime = DateTimeOffset.UnixEpoch; + private string _lastAddedMod = string.Empty; + private DateTimeOffset _lastMovedModTime = DateTimeOffset.UnixEpoch; + private string _lastMovedModFrom = string.Empty; + private string _lastMovedModTo = string.Empty; + + public ModsIpcTester(DalamudPluginInterface pi) + { + _pi = pi; + DeleteSubscriber = ModDeleted.Subscriber(pi, s => + { + _lastDeletedModTime = DateTimeOffset.UtcNow; + _lastDeletedMod = s; + }); + AddSubscriber = ModAdded.Subscriber(pi, s => + { + _lastAddedModTime = DateTimeOffset.UtcNow; + _lastAddedMod = s; + }); + MoveSubscriber = ModMoved.Subscriber(pi, (s1, s2) => + { + _lastMovedModTime = DateTimeOffset.UtcNow; + _lastMovedModFrom = s1; + _lastMovedModTo = s2; + }); + } + + public void Dispose() + { + DeleteSubscriber.Dispose(); + AddSubscriber.Dispose(); + MoveSubscriber.Dispose(); + } + + public void Draw() + { + using var _ = ImRaii.TreeNode("Mods"); + if (!_) + return; + + ImGui.InputTextWithHint("##install", "Install File Path...", ref _newInstallPath, 100); + ImGui.InputTextWithHint("##modDir", "Mod Directory Name...", ref _modDirectory, 100); + ImGui.InputTextWithHint("##modName", "Mod Name...", ref _modName, 100); + ImGui.InputTextWithHint("##path", "New Path...", ref _pathInput, 100); + using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + IpcTester.DrawIntro(GetModList.Label, "Mods"); + DrawModsPopup(); + if (ImGui.Button("Get##Mods")) + { + _mods = new GetModList(_pi).Invoke(); + ImGui.OpenPopup("Mods"); + } + + IpcTester.DrawIntro(ReloadMod.Label, "Reload Mod"); + if (ImGui.Button("Reload")) + _lastReloadEc = new ReloadMod(_pi).Invoke(_modDirectory, _modName); + + ImGui.SameLine(); + ImGui.TextUnformatted(_lastReloadEc.ToString()); + + IpcTester.DrawIntro(InstallMod.Label, "Install Mod"); + if (ImGui.Button("Install")) + _lastInstallEc = new InstallMod(_pi).Invoke(_newInstallPath); + + ImGui.SameLine(); + ImGui.TextUnformatted(_lastInstallEc.ToString()); + + IpcTester.DrawIntro(AddMod.Label, "Add Mod"); + if (ImGui.Button("Add")) + _lastAddEc = new AddMod(_pi).Invoke(_modDirectory); + + ImGui.SameLine(); + ImGui.TextUnformatted(_lastAddEc.ToString()); + + IpcTester.DrawIntro(DeleteMod.Label, "Delete Mod"); + if (ImGui.Button("Delete")) + _lastDeleteEc = new DeleteMod(_pi).Invoke(_modDirectory, _modName); + + ImGui.SameLine(); + ImGui.TextUnformatted(_lastDeleteEc.ToString()); + + IpcTester.DrawIntro(GetModPath.Label, "Current Path"); + var (ec, path, def, nameDef) = new GetModPath(_pi).Invoke(_modDirectory, _modName); + ImGui.TextUnformatted($"{path} ({(def ? "Custom" : "Default")} Path, {(nameDef ? "Custom" : "Default")} Name) [{ec}]"); + + IpcTester.DrawIntro(SetModPath.Label, "Set Path"); + if (ImGui.Button("Set")) + _lastSetPathEc = new SetModPath(_pi).Invoke(_modDirectory, _pathInput, _modName); + + ImGui.SameLine(); + ImGui.TextUnformatted(_lastSetPathEc.ToString()); + + IpcTester.DrawIntro(ModDeleted.Label, "Last Mod Deleted"); + if (_lastDeletedModTime > DateTimeOffset.UnixEpoch) + ImGui.TextUnformatted($"{_lastDeletedMod} at {_lastDeletedModTime}"); + + IpcTester.DrawIntro(ModAdded.Label, "Last Mod Added"); + if (_lastAddedModTime > DateTimeOffset.UnixEpoch) + ImGui.TextUnformatted($"{_lastAddedMod} at {_lastAddedModTime}"); + + IpcTester.DrawIntro(ModMoved.Label, "Last Mod Moved"); + if (_lastMovedModTime > DateTimeOffset.UnixEpoch) + ImGui.TextUnformatted($"{_lastMovedModFrom} -> {_lastMovedModTo} at {_lastMovedModTime}"); + } + + private void DrawModsPopup() + { + ImGui.SetNextWindowSize(ImGuiHelpers.ScaledVector2(500, 500)); + using var p = ImRaii.Popup("Mods"); + if (!p) + return; + + foreach (var (modDir, modName) in _mods) + ImGui.TextUnformatted($"{modDir}: {modName}"); + + if (ImGui.Button("Close", -Vector2.UnitX) || !ImGui.IsWindowFocused()) + ImGui.CloseCurrentPopup(); + } +} diff --git a/Penumbra/Api/IpcTester/PluginStateIpcTester.cs b/Penumbra/Api/IpcTester/PluginStateIpcTester.cs new file mode 100644 index 00000000..0588e5bd --- /dev/null +++ b/Penumbra/Api/IpcTester/PluginStateIpcTester.cs @@ -0,0 +1,132 @@ +using Dalamud.Interface; +using Dalamud.Interface.Utility; +using Dalamud.Plugin; +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Services; +using Penumbra.Api.Helpers; +using Penumbra.Api.IpcSubscribers; + +namespace Penumbra.Api.IpcTester; + +public class PluginStateIpcTester : IUiService, IDisposable +{ + private readonly DalamudPluginInterface _pi; + public readonly EventSubscriber ModDirectoryChanged; + public readonly EventSubscriber Initialized; + public readonly EventSubscriber Disposed; + public readonly EventSubscriber EnabledChange; + + private string _currentConfiguration = string.Empty; + private string _lastModDirectory = string.Empty; + private bool _lastModDirectoryValid; + private DateTimeOffset _lastModDirectoryTime = DateTimeOffset.MinValue; + + private readonly List _initializedList = []; + private readonly List _disposedList = []; + + private DateTimeOffset _lastEnabledChange = DateTimeOffset.UnixEpoch; + private bool? _lastEnabledValue; + + public PluginStateIpcTester(DalamudPluginInterface pi) + { + _pi = pi; + ModDirectoryChanged = IpcSubscribers.ModDirectoryChanged.Subscriber(pi, UpdateModDirectoryChanged); + Initialized = IpcSubscribers.Initialized.Subscriber(pi, AddInitialized); + Disposed = IpcSubscribers.Disposed.Subscriber(pi, AddDisposed); + EnabledChange = IpcSubscribers.EnabledChange.Subscriber(pi, SetLastEnabled); + } + + public void Dispose() + { + ModDirectoryChanged.Dispose(); + Initialized.Dispose(); + Disposed.Dispose(); + EnabledChange.Dispose(); + } + + public void Draw() + { + using var _ = ImRaii.TreeNode("Plugin State"); + if (!_) + return; + + using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + DrawList(IpcSubscribers.Initialized.Label, "Last Initialized", _initializedList); + DrawList(IpcSubscribers.Disposed.Label, "Last Disposed", _disposedList); + + IpcTester.DrawIntro(ApiVersion.Label, "Current Version"); + var (breaking, features) = new ApiVersion(_pi).Invoke(); + ImGui.TextUnformatted($"{breaking}.{features:D4}"); + + IpcTester.DrawIntro(GetEnabledState.Label, "Current State"); + ImGui.TextUnformatted($"{new GetEnabledState(_pi).Invoke()}"); + + IpcTester.DrawIntro(IpcSubscribers.EnabledChange.Label, "Last Change"); + ImGui.TextUnformatted(_lastEnabledValue is { } v ? $"{_lastEnabledChange} (to {v})" : "Never"); + + DrawConfigPopup(); + IpcTester.DrawIntro(GetConfiguration.Label, "Configuration"); + if (ImGui.Button("Get")) + { + _currentConfiguration = new GetConfiguration(_pi).Invoke(); + ImGui.OpenPopup("Config Popup"); + } + + IpcTester.DrawIntro(GetModDirectory.Label, "Current Mod Directory"); + ImGui.TextUnformatted(new GetModDirectory(_pi).Invoke()); + + IpcTester.DrawIntro(IpcSubscribers.ModDirectoryChanged.Label, "Last Mod Directory Change"); + ImGui.TextUnformatted(_lastModDirectoryTime > DateTimeOffset.MinValue + ? $"{_lastModDirectory} ({(_lastModDirectoryValid ? "Valid" : "Invalid")}) at {_lastModDirectoryTime}" + : "None"); + + void DrawList(string label, string text, List list) + { + IpcTester.DrawIntro(label, text); + if (list.Count == 0) + { + ImGui.TextUnformatted("Never"); + } + else + { + ImGui.TextUnformatted(list[^1].LocalDateTime.ToString(CultureInfo.CurrentCulture)); + if (list.Count > 1 && ImGui.IsItemHovered()) + ImGui.SetTooltip(string.Join("\n", + list.SkipLast(1).Select(t => t.LocalDateTime.ToString(CultureInfo.CurrentCulture)))); + } + } + } + + private void DrawConfigPopup() + { + ImGui.SetNextWindowSize(ImGuiHelpers.ScaledVector2(500, 500)); + using var popup = ImRaii.Popup("Config Popup"); + if (!popup) + return; + + using (ImRaii.PushFont(UiBuilder.MonoFont)) + { + ImGuiUtil.TextWrapped(_currentConfiguration); + } + + if (ImGui.Button("Close", -Vector2.UnitX) || !ImGui.IsWindowFocused()) + ImGui.CloseCurrentPopup(); + } + + private void UpdateModDirectoryChanged(string path, bool valid) + => (_lastModDirectory, _lastModDirectoryValid, _lastModDirectoryTime) = (path, valid, DateTimeOffset.Now); + + private void AddInitialized() + => _initializedList.Add(DateTimeOffset.UtcNow); + + private void AddDisposed() + => _disposedList.Add(DateTimeOffset.UtcNow); + + private void SetLastEnabled(bool val) + => (_lastEnabledChange, _lastEnabledValue) = (DateTimeOffset.Now, val); +} diff --git a/Penumbra/Api/IpcTester/RedrawingIpcTester.cs b/Penumbra/Api/IpcTester/RedrawingIpcTester.cs new file mode 100644 index 00000000..281c7ad4 --- /dev/null +++ b/Penumbra/Api/IpcTester/RedrawingIpcTester.cs @@ -0,0 +1,72 @@ +using Dalamud.Plugin; +using Dalamud.Plugin.Services; +using ImGuiNET; +using OtterGui.Raii; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Api.Helpers; +using Penumbra.Api.IpcSubscribers; +using Penumbra.GameData.Interop; +using Penumbra.UI; + +namespace Penumbra.Api.IpcTester; + +public class RedrawingIpcTester : IUiService, IDisposable +{ + private readonly DalamudPluginInterface _pi; + private readonly ObjectManager _objects; + public readonly EventSubscriber Redrawn; + + private int _redrawIndex; + private string _lastRedrawnString = "None"; + + public RedrawingIpcTester(DalamudPluginInterface pi, ObjectManager objects) + { + _pi = pi; + _objects = objects; + Redrawn = GameObjectRedrawn.Subscriber(_pi, SetLastRedrawn); + } + + public void Dispose() + { + Redrawn.Dispose(); + } + + public void Draw() + { + using var _ = ImRaii.TreeNode("Redrawing"); + if (!_) + return; + + using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + IpcTester.DrawIntro(RedrawObject.Label, "Redraw by Index"); + var tmp = _redrawIndex; + ImGui.SetNextItemWidth(100 * UiHelpers.Scale); + if (ImGui.DragInt("##redrawIndex", ref tmp, 0.1f, 0, _objects.TotalCount)) + _redrawIndex = Math.Clamp(tmp, 0, _objects.TotalCount); + ImGui.SameLine(); + if (ImGui.Button("Redraw##Index")) + new RedrawObject(_pi).Invoke(_redrawIndex); + + IpcTester.DrawIntro(RedrawAll.Label, "Redraw All"); + if (ImGui.Button("Redraw##All")) + new RedrawAll(_pi).Invoke(); + + IpcTester.DrawIntro(GameObjectRedrawn.Label, "Last Redrawn Object:"); + ImGui.TextUnformatted(_lastRedrawnString); + } + + private void SetLastRedrawn(nint address, int index) + { + if (index < 0 + || index > _objects.TotalCount + || address == nint.Zero + || _objects[index].Address != address) + _lastRedrawnString = "Invalid"; + + _lastRedrawnString = $"{_objects[index].Utf8Name} (0x{address:X}, {index})"; + } +} diff --git a/Penumbra/Api/IpcTester/ResolveIpcTester.cs b/Penumbra/Api/IpcTester/ResolveIpcTester.cs new file mode 100644 index 00000000..978ed8d6 --- /dev/null +++ b/Penumbra/Api/IpcTester/ResolveIpcTester.cs @@ -0,0 +1,114 @@ +using Dalamud.Plugin; +using ImGuiNET; +using OtterGui.Raii; +using OtterGui.Services; +using Penumbra.Api.IpcSubscribers; +using Penumbra.String.Classes; + +namespace Penumbra.Api.IpcTester; + +public class ResolveIpcTester(DalamudPluginInterface pi) : IUiService +{ + private string _currentResolvePath = string.Empty; + private string _currentReversePath = string.Empty; + private int _currentReverseIdx; + private Task<(string[], string[][])> _task = Task.FromResult<(string[], string[][])>(([], [])); + + public void Draw() + { + using var tree = ImRaii.TreeNode("Resolving"); + if (!tree) + return; + + ImGui.InputTextWithHint("##resolvePath", "Resolve this game path...", ref _currentResolvePath, Utf8GamePath.MaxGamePathLength); + ImGui.InputTextWithHint("##resolveInversePath", "Reverse-resolve this path...", ref _currentReversePath, + Utf8GamePath.MaxGamePathLength); + ImGui.InputInt("##resolveIdx", ref _currentReverseIdx, 0, 0); + using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + IpcTester.DrawIntro(ResolveDefaultPath.Label, "Default Collection Resolve"); + if (_currentResolvePath.Length != 0) + ImGui.TextUnformatted(new ResolveDefaultPath(pi).Invoke(_currentResolvePath)); + + IpcTester.DrawIntro(ResolveInterfacePath.Label, "Interface Collection Resolve"); + if (_currentResolvePath.Length != 0) + ImGui.TextUnformatted(new ResolveInterfacePath(pi).Invoke(_currentResolvePath)); + + IpcTester.DrawIntro(ResolvePlayerPath.Label, "Player Collection Resolve"); + if (_currentResolvePath.Length != 0) + ImGui.TextUnformatted(new ResolvePlayerPath(pi).Invoke(_currentResolvePath)); + + IpcTester.DrawIntro(ResolveGameObjectPath.Label, "Game Object Collection Resolve"); + if (_currentResolvePath.Length != 0) + ImGui.TextUnformatted(new ResolveGameObjectPath(pi).Invoke(_currentResolvePath, _currentReverseIdx)); + + IpcTester.DrawIntro(ReverseResolvePlayerPath.Label, "Reversed Game Paths (Player)"); + if (_currentReversePath.Length > 0) + { + var list = new ReverseResolvePlayerPath(pi).Invoke(_currentReversePath); + if (list.Length > 0) + { + ImGui.TextUnformatted(list[0]); + if (list.Length > 1 && ImGui.IsItemHovered()) + ImGui.SetTooltip(string.Join("\n", list.Skip(1))); + } + } + + IpcTester.DrawIntro(ReverseResolveGameObjectPath.Label, "Reversed Game Paths (Game Object)"); + if (_currentReversePath.Length > 0) + { + var list = new ReverseResolveGameObjectPath(pi).Invoke(_currentReversePath, _currentReverseIdx); + if (list.Length > 0) + { + ImGui.TextUnformatted(list[0]); + if (list.Length > 1 && ImGui.IsItemHovered()) + ImGui.SetTooltip(string.Join("\n", list.Skip(1))); + } + } + + var forwardArray = _currentResolvePath.Length > 0 + ? [_currentResolvePath] + : Array.Empty(); + var reverseArray = _currentReversePath.Length > 0 + ? [_currentReversePath] + : Array.Empty(); + + IpcTester.DrawIntro(ResolvePlayerPaths.Label, "Resolved Paths (Player)"); + if (forwardArray.Length > 0 || reverseArray.Length > 0) + { + var ret = new ResolvePlayerPaths(pi).Invoke(forwardArray, reverseArray); + ImGui.TextUnformatted(ConvertText(ret)); + } + + IpcTester.DrawIntro(ResolvePlayerPathsAsync.Label, "Resolved Paths Async (Player)"); + if (ImGui.Button("Start")) + _task = new ResolvePlayerPathsAsync(pi).Invoke(forwardArray, reverseArray); + var hovered = ImGui.IsItemHovered(); + ImGui.SameLine(); + ImGui.AlignTextToFramePadding(); + ImGui.TextUnformatted(_task.Status.ToString()); + if ((hovered || ImGui.IsItemHovered()) && _task.IsCompletedSuccessfully) + ImGui.SetTooltip(ConvertText(_task.Result)); + return; + + static string ConvertText((string[], string[][]) data) + { + var text = string.Empty; + if (data.Item1.Length > 0) + { + if (data.Item2.Length > 0) + text = $"Forward: {data.Item1[0]} | Reverse: {string.Join("; ", data.Item2[0])}."; + else + text = $"Forward: {data.Item1[0]}."; + } + else if (data.Item2.Length > 0) + { + text = $"Reverse: {string.Join("; ", data.Item2[0])}."; + } + + return text; + } + } +} diff --git a/Penumbra/Api/IpcTester/ResourceTreeIpcTester.cs b/Penumbra/Api/IpcTester/ResourceTreeIpcTester.cs new file mode 100644 index 00000000..1f57fc9d --- /dev/null +++ b/Penumbra/Api/IpcTester/ResourceTreeIpcTester.cs @@ -0,0 +1,349 @@ +using Dalamud.Game.ClientState.Objects.Enums; +using Dalamud.Interface; +using Dalamud.Interface.Utility; +using Dalamud.Plugin; +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Api.Helpers; +using Penumbra.Api.IpcSubscribers; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Interop; +using Penumbra.GameData.Structs; + +namespace Penumbra.Api.IpcTester; + +public class ResourceTreeIpcTester(DalamudPluginInterface pi, ObjectManager objects) : IUiService +{ + private readonly Stopwatch _stopwatch = new(); + + private string _gameObjectIndices = "0"; + private ResourceType _type = ResourceType.Mtrl; + private bool _withUiData; + + private (string, Dictionary>?)[]? _lastGameObjectResourcePaths; + private (string, Dictionary>?)[]? _lastPlayerResourcePaths; + private (string, IReadOnlyDictionary?)[]? _lastGameObjectResourcesOfType; + private (string, IReadOnlyDictionary?)[]? _lastPlayerResourcesOfType; + private (string, ResourceTreeDto?)[]? _lastGameObjectResourceTrees; + private (string, ResourceTreeDto)[]? _lastPlayerResourceTrees; + private TimeSpan _lastCallDuration; + + public void Draw() + { + using var _ = ImRaii.TreeNode("Resource Tree"); + if (!_) + return; + + ImGui.InputText("GameObject indices", ref _gameObjectIndices, 511); + ImGuiUtil.GenericEnumCombo("Resource type", ImGui.CalcItemWidth(), _type, out _type, Enum.GetValues()); + ImGui.Checkbox("Also get names and icons", ref _withUiData); + + using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + IpcTester.DrawIntro(GetGameObjectResourcePaths.Label, "Get GameObject resource paths"); + if (ImGui.Button("Get##GameObjectResourcePaths")) + { + var gameObjects = GetSelectedGameObjects(); + var subscriber = new GetGameObjectResourcePaths(pi); + _stopwatch.Restart(); + var resourcePaths = subscriber.Invoke(gameObjects); + + _lastCallDuration = _stopwatch.Elapsed; + _lastGameObjectResourcePaths = gameObjects + .Select(i => GameObjectToString(i)) + .Zip(resourcePaths) + .ToArray(); + + ImGui.OpenPopup(nameof(GetGameObjectResourcePaths)); + } + + IpcTester.DrawIntro(GetPlayerResourcePaths.Label, "Get local player resource paths"); + if (ImGui.Button("Get##PlayerResourcePaths")) + { + var subscriber = new GetPlayerResourcePaths(pi); + _stopwatch.Restart(); + var resourcePaths = subscriber.Invoke(); + + _lastCallDuration = _stopwatch.Elapsed; + _lastPlayerResourcePaths = resourcePaths + .Select(pair => (GameObjectToString(pair.Key), pair.Value)) + .ToArray()!; + + ImGui.OpenPopup(nameof(GetPlayerResourcePaths)); + } + + IpcTester.DrawIntro(GetGameObjectResourcesOfType.Label, "Get GameObject resources of type"); + if (ImGui.Button("Get##GameObjectResourcesOfType")) + { + var gameObjects = GetSelectedGameObjects(); + var subscriber = new GetGameObjectResourcesOfType(pi); + _stopwatch.Restart(); + var resourcesOfType = subscriber.Invoke(_type, _withUiData, gameObjects); + + _lastCallDuration = _stopwatch.Elapsed; + _lastGameObjectResourcesOfType = gameObjects + .Select(i => GameObjectToString(i)) + .Zip(resourcesOfType) + .ToArray(); + + ImGui.OpenPopup(nameof(GetGameObjectResourcesOfType)); + } + + IpcTester.DrawIntro(GetPlayerResourcesOfType.Label, "Get local player resources of type"); + if (ImGui.Button("Get##PlayerResourcesOfType")) + { + var subscriber = new GetPlayerResourcesOfType(pi); + _stopwatch.Restart(); + var resourcesOfType = subscriber.Invoke(_type, _withUiData); + + _lastCallDuration = _stopwatch.Elapsed; + _lastPlayerResourcesOfType = resourcesOfType + .Select(pair => (GameObjectToString(pair.Key), (IReadOnlyDictionary?)pair.Value)) + .ToArray(); + + ImGui.OpenPopup(nameof(GetPlayerResourcesOfType)); + } + + IpcTester.DrawIntro(GetGameObjectResourceTrees.Label, "Get GameObject resource trees"); + if (ImGui.Button("Get##GameObjectResourceTrees")) + { + var gameObjects = GetSelectedGameObjects(); + var subscriber = new GetGameObjectResourceTrees(pi); + _stopwatch.Restart(); + var trees = subscriber.Invoke(_withUiData, gameObjects); + + _lastCallDuration = _stopwatch.Elapsed; + _lastGameObjectResourceTrees = gameObjects + .Select(i => GameObjectToString(i)) + .Zip(trees) + .ToArray(); + + ImGui.OpenPopup(nameof(GetGameObjectResourceTrees)); + } + + IpcTester.DrawIntro(GetPlayerResourceTrees.Label, "Get local player resource trees"); + if (ImGui.Button("Get##PlayerResourceTrees")) + { + var subscriber = new GetPlayerResourceTrees(pi); + _stopwatch.Restart(); + var trees = subscriber.Invoke(_withUiData); + + _lastCallDuration = _stopwatch.Elapsed; + _lastPlayerResourceTrees = trees + .Select(pair => (GameObjectToString(pair.Key), pair.Value)) + .ToArray(); + + ImGui.OpenPopup(nameof(GetPlayerResourceTrees)); + } + + DrawPopup(nameof(GetGameObjectResourcePaths), ref _lastGameObjectResourcePaths, DrawResourcePaths, + _lastCallDuration); + DrawPopup(nameof(GetPlayerResourcePaths), ref _lastPlayerResourcePaths!, DrawResourcePaths, _lastCallDuration); + + DrawPopup(nameof(GetGameObjectResourcesOfType), ref _lastGameObjectResourcesOfType, DrawResourcesOfType, + _lastCallDuration); + DrawPopup(nameof(GetPlayerResourcesOfType), ref _lastPlayerResourcesOfType, DrawResourcesOfType, + _lastCallDuration); + + DrawPopup(nameof(GetGameObjectResourceTrees), ref _lastGameObjectResourceTrees, DrawResourceTrees, + _lastCallDuration); + DrawPopup(nameof(GetPlayerResourceTrees), ref _lastPlayerResourceTrees, DrawResourceTrees!, _lastCallDuration); + } + + private static void DrawPopup(string popupId, ref T? result, Action drawResult, TimeSpan duration) where T : class + { + ImGui.SetNextWindowSize(ImGuiHelpers.ScaledVector2(1000, 500)); + using var popup = ImRaii.Popup(popupId); + if (!popup) + { + result = null; + return; + } + + if (result == null) + { + ImGui.CloseCurrentPopup(); + return; + } + + drawResult(result); + + ImGui.TextUnformatted($"Invoked in {duration.TotalMilliseconds} ms"); + + if (ImGui.Button("Close", -Vector2.UnitX) || !ImGui.IsWindowFocused()) + { + result = null; + ImGui.CloseCurrentPopup(); + } + } + + private static void DrawWithHeaders((string, T?)[] result, Action drawItem) where T : class + { + var firstSeen = new Dictionary(); + foreach (var (label, item) in result) + { + if (item == null) + { + ImRaii.TreeNode($"{label}: null", ImGuiTreeNodeFlags.Leaf).Dispose(); + continue; + } + + if (firstSeen.TryGetValue(item, out var firstLabel)) + { + ImRaii.TreeNode($"{label}: same as {firstLabel}", ImGuiTreeNodeFlags.Leaf).Dispose(); + continue; + } + + firstSeen.Add(item, label); + + using var header = ImRaii.TreeNode(label); + if (!header) + continue; + + drawItem(item); + } + } + + private static void DrawResourcePaths((string, Dictionary>?)[] result) + { + DrawWithHeaders(result, paths => + { + using var table = ImRaii.Table(string.Empty, 2, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + ImGui.TableSetupColumn("Actual Path", ImGuiTableColumnFlags.WidthStretch, 0.6f); + ImGui.TableSetupColumn("Game Paths", ImGuiTableColumnFlags.WidthStretch, 0.4f); + ImGui.TableHeadersRow(); + + foreach (var (actualPath, gamePaths) in paths) + { + ImGui.TableNextColumn(); + ImGui.TextUnformatted(actualPath); + ImGui.TableNextColumn(); + foreach (var gamePath in gamePaths) + ImGui.TextUnformatted(gamePath); + } + }); + } + + private void DrawResourcesOfType((string, IReadOnlyDictionary?)[] result) + { + DrawWithHeaders(result, resources => + { + using var table = ImRaii.Table(string.Empty, _withUiData ? 3 : 2, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + ImGui.TableSetupColumn("Resource Handle", ImGuiTableColumnFlags.WidthStretch, 0.15f); + ImGui.TableSetupColumn("Actual Path", ImGuiTableColumnFlags.WidthStretch, _withUiData ? 0.55f : 0.85f); + if (_withUiData) + ImGui.TableSetupColumn("Icon & Name", ImGuiTableColumnFlags.WidthStretch, 0.3f); + ImGui.TableHeadersRow(); + + foreach (var (resourceHandle, (actualPath, name, icon)) in resources) + { + ImGui.TableNextColumn(); + TextUnformattedMono($"0x{resourceHandle:X}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted(actualPath); + if (_withUiData) + { + ImGui.TableNextColumn(); + TextUnformattedMono(icon.ToString()); + ImGui.SameLine(); + ImGui.TextUnformatted(name); + } + } + }); + } + + private void DrawResourceTrees((string, ResourceTreeDto?)[] result) + { + DrawWithHeaders(result, tree => + { + ImGui.TextUnformatted($"Name: {tree.Name}\nRaceCode: {(GenderRace)tree.RaceCode}"); + + using var table = ImRaii.Table(string.Empty, _withUiData ? 7 : 5, ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.Resizable); + if (!table) + return; + + if (_withUiData) + { + ImGui.TableSetupColumn("Name", ImGuiTableColumnFlags.WidthStretch, 0.5f); + ImGui.TableSetupColumn("Type", ImGuiTableColumnFlags.WidthStretch, 0.1f); + ImGui.TableSetupColumn("Icon", ImGuiTableColumnFlags.WidthStretch, 0.15f); + } + else + { + ImGui.TableSetupColumn("Type", ImGuiTableColumnFlags.WidthStretch, 0.5f); + } + + ImGui.TableSetupColumn("Game Path", ImGuiTableColumnFlags.WidthStretch, 0.5f); + ImGui.TableSetupColumn("Actual Path", ImGuiTableColumnFlags.WidthStretch, 0.5f); + ImGui.TableSetupColumn("Object Address", ImGuiTableColumnFlags.WidthStretch, 0.2f); + ImGui.TableSetupColumn("Resource Handle", ImGuiTableColumnFlags.WidthStretch, 0.2f); + ImGui.TableHeadersRow(); + + void DrawNode(ResourceNodeDto node) + { + ImGui.TableNextRow(); + ImGui.TableNextColumn(); + var hasChildren = node.Children.Any(); + using var treeNode = ImRaii.TreeNode( + $"{(_withUiData ? node.Name ?? "Unknown" : node.Type)}##{node.ObjectAddress:X8}", + hasChildren + ? ImGuiTreeNodeFlags.SpanFullWidth + : ImGuiTreeNodeFlags.SpanFullWidth | ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.NoTreePushOnOpen); + if (_withUiData) + { + ImGui.TableNextColumn(); + TextUnformattedMono(node.Type.ToString()); + ImGui.TableNextColumn(); + TextUnformattedMono(node.Icon.ToString()); + } + + ImGui.TableNextColumn(); + ImGui.TextUnformatted(node.GamePath ?? "Unknown"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted(node.ActualPath); + ImGui.TableNextColumn(); + TextUnformattedMono($"0x{node.ObjectAddress:X8}"); + ImGui.TableNextColumn(); + TextUnformattedMono($"0x{node.ResourceHandle:X8}"); + + if (treeNode) + foreach (var child in node.Children) + DrawNode(child); + } + + foreach (var node in tree.Nodes) + DrawNode(node); + }); + } + + private static void TextUnformattedMono(string text) + { + using var _ = ImRaii.PushFont(UiBuilder.MonoFont); + ImGui.TextUnformatted(text); + } + + private ushort[] GetSelectedGameObjects() + => _gameObjectIndices.Split(',') + .SelectWhere(index => (ushort.TryParse(index.Trim(), out var i), i)) + .ToArray(); + + private unsafe string GameObjectToString(ObjectIndex gameObjectIndex) + { + var gameObject = objects[gameObjectIndex]; + + return gameObject.Valid + ? $"[{gameObjectIndex}] {gameObject.Utf8Name} ({(ObjectKind)gameObject.AsObject->ObjectKind})" + : $"[{gameObjectIndex}] null"; + } +} diff --git a/Penumbra/Api/IpcTester/TemporaryIpcTester.cs b/Penumbra/Api/IpcTester/TemporaryIpcTester.cs new file mode 100644 index 00000000..a8405eb2 --- /dev/null +++ b/Penumbra/Api/IpcTester/TemporaryIpcTester.cs @@ -0,0 +1,203 @@ +using Dalamud.Interface; +using Dalamud.Plugin; +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Api.IpcSubscribers; +using Penumbra.Collections.Manager; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods; +using Penumbra.Mods.Manager; +using Penumbra.Services; + +namespace Penumbra.Api.IpcTester; + +public class TemporaryIpcTester( + DalamudPluginInterface pi, + ModManager modManager, + CollectionManager collections, + TempModManager tempMods, + TempCollectionManager tempCollections, + SaveService saveService, + Configuration config) + : IUiService +{ + public Guid LastCreatedCollectionId = Guid.Empty; + + private Guid? _tempGuid; + private string _tempCollectionName = string.Empty; + private string _tempCollectionGuidName = string.Empty; + private string _tempModName = string.Empty; + private string _tempGamePath = "test/game/path.mtrl"; + private string _tempFilePath = "test/success.mtrl"; + private string _tempManipulation = string.Empty; + private PenumbraApiEc _lastTempError; + private int _tempActorIndex; + private bool _forceOverwrite; + + public void Draw() + { + using var _ = ImRaii.TreeNode("Temporary"); + if (!_) + return; + + ImGui.InputTextWithHint("##tempCollection", "Collection Name...", ref _tempCollectionName, 128); + ImGuiUtil.GuidInput("##guid", "Collection GUID...", string.Empty, ref _tempGuid, ref _tempCollectionGuidName); + ImGui.InputInt("##tempActorIndex", ref _tempActorIndex, 0, 0); + ImGui.InputTextWithHint("##tempMod", "Temporary Mod Name...", ref _tempModName, 32); + ImGui.InputTextWithHint("##tempGame", "Game Path...", ref _tempGamePath, 256); + ImGui.InputTextWithHint("##tempFile", "File Path...", ref _tempFilePath, 256); + ImGui.InputTextWithHint("##tempManip", "Manipulation Base64 String...", ref _tempManipulation, 256); + ImGui.Checkbox("Force Character Collection Overwrite", ref _forceOverwrite); + + using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + IpcTester.DrawIntro("Last Error", _lastTempError.ToString()); + ImGuiUtil.DrawTableColumn("Last Created Collection"); + ImGui.TableNextColumn(); + using (ImRaii.PushFont(UiBuilder.MonoFont)) + { + ImGuiUtil.CopyOnClickSelectable(LastCreatedCollectionId.ToString()); + } + + IpcTester.DrawIntro(CreateTemporaryCollection.Label, "Create Temporary Collection"); + if (ImGui.Button("Create##Collection")) + { + LastCreatedCollectionId = new CreateTemporaryCollection(pi).Invoke(_tempCollectionName); + if (_tempGuid == null) + { + _tempGuid = LastCreatedCollectionId; + _tempCollectionGuidName = LastCreatedCollectionId.ToString(); + } + } + + var guid = _tempGuid.GetValueOrDefault(Guid.Empty); + + IpcTester.DrawIntro(DeleteTemporaryCollection.Label, "Delete Temporary Collection"); + if (ImGui.Button("Delete##Collection")) + _lastTempError = new DeleteTemporaryCollection(pi).Invoke(guid); + ImGui.SameLine(); + if (ImGui.Button("Delete Last##Collection")) + _lastTempError = new DeleteTemporaryCollection(pi).Invoke(LastCreatedCollectionId); + + IpcTester.DrawIntro(AssignTemporaryCollection.Label, "Assign Temporary Collection"); + if (ImGui.Button("Assign##NamedCollection")) + _lastTempError = new AssignTemporaryCollection(pi).Invoke(guid, _tempActorIndex, _forceOverwrite); + + IpcTester.DrawIntro(AddTemporaryMod.Label, "Add Temporary Mod to specific Collection"); + if (ImGui.Button("Add##Mod")) + _lastTempError = new AddTemporaryMod(pi).Invoke(_tempModName, guid, + new Dictionary { { _tempGamePath, _tempFilePath } }, + _tempManipulation.Length > 0 ? _tempManipulation : string.Empty, int.MaxValue); + + IpcTester.DrawIntro(CreateTemporaryCollection.Label, "Copy Existing Collection"); + if (ImGuiUtil.DrawDisabledButton("Copy##Collection", Vector2.Zero, + "Copies the effective list from the collection named in Temporary Mod Name...", + !collections.Storage.ByName(_tempModName, out var copyCollection)) + && copyCollection is { HasCache: true }) + { + var files = copyCollection.ResolvedFiles.ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.Path.ToString()); + var manips = Functions.ToCompressedBase64(copyCollection.MetaCache?.Manipulations.ToArray() ?? Array.Empty(), + MetaManipulation.CurrentVersion); + _lastTempError = new AddTemporaryMod(pi).Invoke(_tempModName, guid, files, manips, 999); + } + + IpcTester.DrawIntro(AddTemporaryModAll.Label, "Add Temporary Mod to all Collections"); + if (ImGui.Button("Add##All")) + _lastTempError = new AddTemporaryModAll(pi).Invoke(_tempModName, + new Dictionary { { _tempGamePath, _tempFilePath } }, + _tempManipulation.Length > 0 ? _tempManipulation : string.Empty, int.MaxValue); + + IpcTester.DrawIntro(RemoveTemporaryMod.Label, "Remove Temporary Mod from specific Collection"); + if (ImGui.Button("Remove##Mod")) + _lastTempError = new RemoveTemporaryMod(pi).Invoke(_tempModName, guid, int.MaxValue); + + IpcTester.DrawIntro(RemoveTemporaryModAll.Label, "Remove Temporary Mod from all Collections"); + if (ImGui.Button("Remove##ModAll")) + _lastTempError = new RemoveTemporaryModAll(pi).Invoke(_tempModName, int.MaxValue); + } + + public void DrawCollections() + { + using var collTree = ImRaii.TreeNode("Temporary Collections##TempCollections"); + if (!collTree) + return; + + using var table = ImRaii.Table("##collTree", 6, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + foreach (var (collection, idx) in tempCollections.Values.WithIndex()) + { + using var id = ImRaii.PushId(idx); + ImGui.TableNextColumn(); + var character = tempCollections.Collections.Where(p => p.Collection == collection).Select(p => p.DisplayName) + .FirstOrDefault() + ?? "Unknown"; + if (ImGui.Button("Save##Collection")) + TemporaryMod.SaveTempCollection(config, saveService, modManager, collection, character); + + using (ImRaii.PushFont(UiBuilder.MonoFont)) + { + ImGui.TableNextColumn(); + ImGuiUtil.CopyOnClickSelectable(collection.Identifier); + } + + ImGuiUtil.DrawTableColumn(collection.Name); + ImGuiUtil.DrawTableColumn(collection.ResolvedFiles.Count.ToString()); + ImGuiUtil.DrawTableColumn(collection.MetaCache?.Count.ToString() ?? "0"); + ImGuiUtil.DrawTableColumn(string.Join(", ", + tempCollections.Collections.Where(p => p.Collection == collection).Select(c => c.DisplayName))); + } + } + + public void DrawMods() + { + using var modTree = ImRaii.TreeNode("Temporary Mods##TempMods"); + if (!modTree) + return; + + using var table = ImRaii.Table("##modTree", 5, ImGuiTableFlags.SizingFixedFit); + + void PrintList(string collectionName, IReadOnlyList list) + { + foreach (var mod in list) + { + ImGui.TableNextColumn(); + ImGui.TextUnformatted(mod.Name); + ImGui.TableNextColumn(); + ImGui.TextUnformatted(mod.Priority.ToString()); + ImGui.TableNextColumn(); + ImGui.TextUnformatted(collectionName); + ImGui.TableNextColumn(); + ImGui.TextUnformatted(mod.Default.Files.Count.ToString()); + if (ImGui.IsItemHovered()) + { + using var tt = ImRaii.Tooltip(); + foreach (var (path, file) in mod.Default.Files) + ImGui.TextUnformatted($"{path} -> {file}"); + } + + ImGui.TableNextColumn(); + ImGui.TextUnformatted(mod.TotalManipulations.ToString()); + if (ImGui.IsItemHovered()) + { + using var tt = ImRaii.Tooltip(); + foreach (var manip in mod.Default.Manipulations) + ImGui.TextUnformatted(manip.ToString()); + } + } + } + + if (table) + { + PrintList("All", tempMods.ModsForAllCollections); + foreach (var (collection, list) in tempMods.Mods) + PrintList(collection.Name, list); + } + } +} diff --git a/Penumbra/Api/IpcTester/UiIpcTester.cs b/Penumbra/Api/IpcTester/UiIpcTester.cs new file mode 100644 index 00000000..29ddc22e --- /dev/null +++ b/Penumbra/Api/IpcTester/UiIpcTester.cs @@ -0,0 +1,128 @@ +using Dalamud.Plugin; +using ImGuiNET; +using OtterGui.Raii; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Api.Helpers; +using Penumbra.Api.IpcSubscribers; +using Penumbra.Communication; + +namespace Penumbra.Api.IpcTester; + +public class UiIpcTester : IUiService, IDisposable +{ + private readonly DalamudPluginInterface _pi; + public readonly EventSubscriber PreSettingsTabBar; + public readonly EventSubscriber PreSettingsPanel; + public readonly EventSubscriber PostEnabled; + public readonly EventSubscriber PostSettingsPanelDraw; + public readonly EventSubscriber ChangedItemTooltip; + public readonly EventSubscriber ChangedItemClicked; + + private string _lastDrawnMod = string.Empty; + private DateTimeOffset _lastDrawnModTime = DateTimeOffset.MinValue; + private bool _subscribedToTooltip; + private bool _subscribedToClick; + private string _lastClicked = string.Empty; + private string _lastHovered = string.Empty; + private TabType _selectTab = TabType.None; + private string _modName = string.Empty; + private PenumbraApiEc _ec = PenumbraApiEc.Success; + + public UiIpcTester(DalamudPluginInterface pi) + { + _pi = pi; + PreSettingsTabBar = IpcSubscribers.PreSettingsTabBarDraw.Subscriber(pi, UpdateLastDrawnMod); + PreSettingsPanel = IpcSubscribers.PreSettingsPanelDraw.Subscriber(pi, UpdateLastDrawnMod); + PostEnabled = IpcSubscribers.PostEnabledDraw.Subscriber(pi, UpdateLastDrawnMod); + PostSettingsPanelDraw = IpcSubscribers.PostSettingsPanelDraw.Subscriber(pi, UpdateLastDrawnMod); + ChangedItemTooltip = IpcSubscribers.ChangedItemTooltip.Subscriber(pi, AddedTooltip); + ChangedItemClicked = IpcSubscribers.ChangedItemClicked.Subscriber(pi, AddedClick); + } + + public void Dispose() + { + PreSettingsTabBar.Dispose(); + PreSettingsPanel.Dispose(); + PostEnabled.Dispose(); + PostSettingsPanelDraw.Dispose(); + ChangedItemTooltip.Dispose(); + ChangedItemClicked.Dispose(); + } + + public void Draw() + { + using var _ = ImRaii.TreeNode("UI"); + if (!_) + return; + + using (var combo = ImRaii.Combo("Tab to Open at", _selectTab.ToString())) + { + if (combo) + foreach (var val in Enum.GetValues()) + { + if (ImGui.Selectable(val.ToString(), _selectTab == val)) + _selectTab = val; + } + } + + ImGui.InputTextWithHint("##openMod", "Mod to Open at...", ref _modName, 256); + using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + IpcTester.DrawIntro(IpcSubscribers.PostSettingsPanelDraw.Label, "Last Drawn Mod"); + ImGui.TextUnformatted(_lastDrawnMod.Length > 0 ? $"{_lastDrawnMod} at {_lastDrawnModTime}" : "None"); + + IpcTester.DrawIntro(IpcSubscribers.ChangedItemTooltip.Label, "Add Tooltip"); + if (ImGui.Checkbox("##tooltip", ref _subscribedToTooltip)) + { + if (_subscribedToTooltip) + ChangedItemTooltip.Enable(); + else + ChangedItemTooltip.Disable(); + } + + ImGui.SameLine(); + ImGui.TextUnformatted(_lastHovered); + + IpcTester.DrawIntro(IpcSubscribers.ChangedItemClicked.Label, "Subscribe Click"); + if (ImGui.Checkbox("##click", ref _subscribedToClick)) + { + if (_subscribedToClick) + ChangedItemClicked.Enable(); + else + ChangedItemClicked.Disable(); + } + + ImGui.SameLine(); + ImGui.TextUnformatted(_lastClicked); + IpcTester.DrawIntro(OpenMainWindow.Label, "Open Mod Window"); + if (ImGui.Button("Open##window")) + _ec = new OpenMainWindow(_pi).Invoke(_selectTab, _modName, _modName); + + ImGui.SameLine(); + ImGui.TextUnformatted(_ec.ToString()); + + IpcTester.DrawIntro(CloseMainWindow.Label, "Close Mod Window"); + if (ImGui.Button("Close##window")) + new CloseMainWindow(_pi).Invoke(); + } + + private void UpdateLastDrawnMod(string name) + => (_lastDrawnMod, _lastDrawnModTime) = (name, DateTimeOffset.Now); + + private void UpdateLastDrawnMod(string name, float _1, float _2) + => (_lastDrawnMod, _lastDrawnModTime) = (name, DateTimeOffset.Now); + + private void AddedTooltip(ChangedItemType type, uint id) + { + _lastHovered = $"{type} {id} at {DateTime.UtcNow.ToLocalTime().ToString(CultureInfo.CurrentCulture)}"; + ImGui.TextUnformatted("IPC Test Successful"); + } + + private void AddedClick(MouseButton button, ChangedItemType type, uint id) + { + _lastClicked = $"{button}-click on {type} {id} at {DateTime.UtcNow.ToLocalTime().ToString(CultureInfo.CurrentCulture)}"; + } +} diff --git a/Penumbra/Api/PenumbraApi.cs b/Penumbra/Api/PenumbraApi.cs deleted file mode 100644 index dc1e8472..00000000 --- a/Penumbra/Api/PenumbraApi.cs +++ /dev/null @@ -1,1374 +0,0 @@ -using Dalamud.Game.ClientState.Objects.Types; -using Dalamud.Plugin.Services; -using Lumina.Data; -using Newtonsoft.Json; -using OtterGui; -using Penumbra.Collections; -using Penumbra.Interop.PathResolving; -using Penumbra.Interop.Structs; -using Penumbra.Meta.Manipulations; -using Penumbra.Mods; -using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; -using OtterGui.Compression; -using OtterGui.Log; -using Penumbra.Api.Enums; -using Penumbra.GameData.Actors; -using Penumbra.Interop.ResourceLoading; -using Penumbra.Mods.Manager; -using Penumbra.String; -using Penumbra.String.Classes; -using Penumbra.Services; -using Penumbra.Collections.Manager; -using Penumbra.Communication; -using Penumbra.GameData.Interop; -using Penumbra.Import.Textures; -using Penumbra.Interop.Services; -using Penumbra.UI; -using TextureType = Penumbra.Api.Enums.TextureType; -using Penumbra.Interop.ResourceTree; -using Penumbra.Mods.Editor; -using Penumbra.Mods.Subclasses; - -namespace Penumbra.Api; - -public class PenumbraApi : IDisposable, IPenumbraApi -{ - public (int, int) ApiVersion - => (4, 24); - - public event Action? PreSettingsTabBarDraw - { - add => _communicator.PreSettingsTabBarDraw.Subscribe(value!, Communication.PreSettingsTabBarDraw.Priority.Default); - remove => _communicator.PreSettingsTabBarDraw.Unsubscribe(value!); - } - - public event Action? PreSettingsPanelDraw - { - add => _communicator.PreSettingsPanelDraw.Subscribe(value!, Communication.PreSettingsPanelDraw.Priority.Default); - remove => _communicator.PreSettingsPanelDraw.Unsubscribe(value!); - } - - public event Action? PostEnabledDraw - { - add => _communicator.PostEnabledDraw.Subscribe(value!, Communication.PostEnabledDraw.Priority.Default); - remove => _communicator.PostEnabledDraw.Unsubscribe(value!); - } - - public event Action? PostSettingsPanelDraw - { - add => _communicator.PostSettingsPanelDraw.Subscribe(value!, Communication.PostSettingsPanelDraw.Priority.Default); - remove => _communicator.PostSettingsPanelDraw.Unsubscribe(value!); - } - - public event GameObjectRedrawnDelegate? GameObjectRedrawn - { - add - { - CheckInitialized(); - _redrawService.GameObjectRedrawn += value; - } - remove - { - CheckInitialized(); - _redrawService.GameObjectRedrawn -= value; - } - } - - public event ModSettingChangedDelegate? ModSettingChanged; - - public event CreatingCharacterBaseDelegate? CreatingCharacterBase - { - add - { - if (value == null) - return; - - CheckInitialized(); - _communicator.CreatingCharacterBase.Subscribe(new Action(value), - Communication.CreatingCharacterBase.Priority.Api); - } - remove - { - if (value == null) - return; - - CheckInitialized(); - _communicator.CreatingCharacterBase.Unsubscribe(new Action(value)); - } - } - - public event CreatedCharacterBaseDelegate? CreatedCharacterBase; - - public bool Valid - => _lumina != null; - - private CommunicatorService _communicator; - private Lumina.GameData? _lumina; - - private IDataManager _gameData; - private IFramework _framework; - private ObjectManager _objects; - private ModManager _modManager; - private ResourceLoader _resourceLoader; - private Configuration _config; - private CollectionManager _collectionManager; - private TempCollectionManager _tempCollections; - private TempModManager _tempMods; - private ActorManager _actors; - private CollectionResolver _collectionResolver; - private CutsceneService _cutsceneService; - private ModImportManager _modImportManager; - private CollectionEditor _collectionEditor; - private RedrawService _redrawService; - private ModFileSystem _modFileSystem; - private ConfigWindow _configWindow; - private TextureManager _textureManager; - private ResourceTreeFactory _resourceTreeFactory; - - public unsafe PenumbraApi(CommunicatorService communicator, IDataManager gameData, IFramework framework, ObjectManager objects, - ModManager modManager, ResourceLoader resourceLoader, Configuration config, CollectionManager collectionManager, - TempCollectionManager tempCollections, TempModManager tempMods, ActorManager actors, CollectionResolver collectionResolver, - CutsceneService cutsceneService, ModImportManager modImportManager, CollectionEditor collectionEditor, RedrawService redrawService, - ModFileSystem modFileSystem, ConfigWindow configWindow, TextureManager textureManager, ResourceTreeFactory resourceTreeFactory) - { - _communicator = communicator; - _gameData = gameData; - _framework = framework; - _objects = objects; - _modManager = modManager; - _resourceLoader = resourceLoader; - _config = config; - _collectionManager = collectionManager; - _tempCollections = tempCollections; - _tempMods = tempMods; - _actors = actors; - _collectionResolver = collectionResolver; - _cutsceneService = cutsceneService; - _modImportManager = modImportManager; - _collectionEditor = collectionEditor; - _redrawService = redrawService; - _modFileSystem = modFileSystem; - _configWindow = configWindow; - _textureManager = textureManager; - _resourceTreeFactory = resourceTreeFactory; - _lumina = gameData.GameData; - - _resourceLoader.ResourceLoaded += OnResourceLoaded; - _communicator.ModPathChanged.Subscribe(ModPathChangeSubscriber, ModPathChanged.Priority.Api); - _communicator.ModSettingChanged.Subscribe(OnModSettingChange, Communication.ModSettingChanged.Priority.Api); - _communicator.CreatedCharacterBase.Subscribe(OnCreatedCharacterBase, Communication.CreatedCharacterBase.Priority.Api); - _communicator.ModOptionChanged.Subscribe(OnModOptionEdited, ModOptionChanged.Priority.Api); - _communicator.ModFileChanged.Subscribe(OnModFileChanged, ModFileChanged.Priority.Api); - } - - public unsafe void Dispose() - { - if (!Valid) - return; - - _resourceLoader.ResourceLoaded -= OnResourceLoaded; - _communicator.ModPathChanged.Unsubscribe(ModPathChangeSubscriber); - _communicator.ModSettingChanged.Unsubscribe(OnModSettingChange); - _communicator.CreatedCharacterBase.Unsubscribe(OnCreatedCharacterBase); - _communicator.ModOptionChanged.Unsubscribe(OnModOptionEdited); - _communicator.ModFileChanged.Unsubscribe(OnModFileChanged); - _lumina = null; - _communicator = null!; - _modManager = null!; - _resourceLoader = null!; - _config = null!; - _collectionManager = null!; - _tempCollections = null!; - _tempMods = null!; - _actors = null!; - _collectionResolver = null!; - _cutsceneService = null!; - _modImportManager = null!; - _collectionEditor = null!; - _redrawService = null!; - _modFileSystem = null!; - _configWindow = null!; - _textureManager = null!; - _resourceTreeFactory = null!; - _framework = null!; - } - - public event ChangedItemClick? ChangedItemClicked - { - add => _communicator.ChangedItemClick.Subscribe(new Action(value!), - Communication.ChangedItemClick.Priority.Default); - remove => _communicator.ChangedItemClick.Unsubscribe(new Action(value!)); - } - - public string GetModDirectory() - { - CheckInitialized(); - return _config.ModDirectory; - } - - private unsafe void OnResourceLoaded(ResourceHandle* _, Utf8GamePath originalPath, FullPath? manipulatedPath, - ResolveData resolveData) - { - if (resolveData.AssociatedGameObject != nint.Zero) - GameObjectResourceResolved?.Invoke(resolveData.AssociatedGameObject, originalPath.ToString(), - manipulatedPath?.ToString() ?? originalPath.ToString()); - } - - public event Action? ModDirectoryChanged - { - add - { - CheckInitialized(); - _communicator.ModDirectoryChanged.Subscribe(value!, Communication.ModDirectoryChanged.Priority.Api); - } - remove - { - CheckInitialized(); - _communicator.ModDirectoryChanged.Unsubscribe(value!); - } - } - - public bool GetEnabledState() - => _config.EnableMods; - - public event Action? EnabledChange - { - add - { - CheckInitialized(); - _communicator.EnabledChanged.Subscribe(value!, EnabledChanged.Priority.Api); - } - remove - { - CheckInitialized(); - _communicator.EnabledChanged.Unsubscribe(value!); - } - } - - public string GetConfiguration() - { - CheckInitialized(); - return JsonConvert.SerializeObject(_config, Formatting.Indented); - } - - public event ChangedItemHover? ChangedItemTooltip - { - add => _communicator.ChangedItemHover.Subscribe(new Action(value!), Communication.ChangedItemHover.Priority.Default); - remove => _communicator.ChangedItemHover.Unsubscribe(new Action(value!)); - } - - public event GameObjectResourceResolvedDelegate? GameObjectResourceResolved; - - public PenumbraApiEc OpenMainWindow(TabType tab, string modDirectory, string modName) - { - CheckInitialized(); - if (_configWindow == null) - return PenumbraApiEc.SystemDisposed; - - _configWindow.IsOpen = true; - - if (!Enum.IsDefined(tab)) - return PenumbraApiEc.InvalidArgument; - - if (tab == TabType.Mods && (modDirectory.Length > 0 || modName.Length > 0)) - { - if (_modManager.TryGetMod(modDirectory, modName, out var mod)) - _communicator.SelectTab.Invoke(tab, mod); - else - return PenumbraApiEc.ModMissing; - } - else if (tab != TabType.None) - { - _communicator.SelectTab.Invoke(tab, null); - } - - return PenumbraApiEc.Success; - } - - public void CloseMainWindow() - { - CheckInitialized(); - if (_configWindow == null) - return; - - _configWindow.IsOpen = false; - } - - public void RedrawObject(int tableIndex, RedrawType setting) - { - CheckInitialized(); - _redrawService.RedrawObject(tableIndex, setting); - } - - public void RedrawObject(string name, RedrawType setting) - { - CheckInitialized(); - _redrawService.RedrawObject(name, setting); - } - - public void RedrawObject(GameObject? gameObject, RedrawType setting) - { - CheckInitialized(); - _redrawService.RedrawObject(gameObject, setting); - } - - public void RedrawAll(RedrawType setting) - { - CheckInitialized(); - _redrawService.RedrawAll(setting); - } - - public string ResolveDefaultPath(string path) - { - CheckInitialized(); - return ResolvePath(path, _modManager, _collectionManager.Active.Default); - } - - public string ResolveInterfacePath(string path) - { - CheckInitialized(); - return ResolvePath(path, _modManager, _collectionManager.Active.Interface); - } - - public string ResolvePlayerPath(string path) - { - CheckInitialized(); - return ResolvePath(path, _modManager, _collectionResolver.PlayerCollection()); - } - - // TODO: cleanup when incrementing API level - public string ResolvePath(string path, string characterName) - => ResolvePath(path, characterName, ushort.MaxValue); - - public string ResolveGameObjectPath(string path, int gameObjectIdx) - { - CheckInitialized(); - AssociatedCollection(gameObjectIdx, out var collection); - return ResolvePath(path, _modManager, collection); - } - - public string ResolvePath(string path, string characterName, ushort worldId) - { - CheckInitialized(); - return ResolvePath(path, _modManager, - _collectionManager.Active.Individual(NameToIdentifier(characterName, worldId))); - } - - // TODO: cleanup when incrementing API level - public string[] ReverseResolvePath(string path, string characterName) - => ReverseResolvePath(path, characterName, ushort.MaxValue); - - public string[] ReverseResolvePath(string path, string characterName, ushort worldId) - { - CheckInitialized(); - if (!_config.EnableMods) - return [path]; - - var ret = _collectionManager.Active.Individual(NameToIdentifier(characterName, worldId)).ReverseResolvePath(new FullPath(path)); - return ret.Select(r => r.ToString()).ToArray(); - } - - public string[] ReverseResolveGameObjectPath(string path, int gameObjectIdx) - { - CheckInitialized(); - if (!_config.EnableMods) - return [path]; - - AssociatedCollection(gameObjectIdx, out var collection); - var ret = collection.ReverseResolvePath(new FullPath(path)); - return ret.Select(r => r.ToString()).ToArray(); - } - - public string[] ReverseResolvePlayerPath(string path) - { - CheckInitialized(); - if (!_config.EnableMods) - return [path]; - - var ret = _collectionResolver.PlayerCollection().ReverseResolvePath(new FullPath(path)); - return ret.Select(r => r.ToString()).ToArray(); - } - - public (string[], string[][]) ResolvePlayerPaths(string[] forward, string[] reverse) - { - CheckInitialized(); - if (!_config.EnableMods) - return (forward, reverse.Select(p => new[] - { - p, - }).ToArray()); - - var playerCollection = _collectionResolver.PlayerCollection(); - var resolved = forward.Select(p => ResolvePath(p, _modManager, playerCollection)).ToArray(); - var reverseResolved = playerCollection.ReverseResolvePaths(reverse); - return (resolved, reverseResolved.Select(a => a.Select(p => p.ToString()).ToArray()).ToArray()); - } - - public async Task<(string[], string[][])> ResolvePlayerPathsAsync(string[] forward, string[] reverse) - { - CheckInitialized(); - if (!_config.EnableMods) - return (forward, reverse.Select(p => new[] - { - p, - }).ToArray()); - - return await Task.Run(async () => - { - var playerCollection = await _framework.RunOnFrameworkThread(_collectionResolver.PlayerCollection).ConfigureAwait(false); - var forwardTask = Task.Run(() => - { - var forwardRet = new string[forward.Length]; - Parallel.For(0, forward.Length, idx => forwardRet[idx] = ResolvePath(forward[idx], _modManager, playerCollection)); - return forwardRet; - }).ConfigureAwait(false); - var reverseTask = Task.Run(() => playerCollection.ReverseResolvePaths(reverse)).ConfigureAwait(false); - var reverseResolved = (await reverseTask).Select(a => a.Select(p => p.ToString()).ToArray()).ToArray(); - return (await forwardTask, reverseResolved); - }); - } - - public T? GetFile(string gamePath) where T : FileResource - => GetFileIntern(ResolveDefaultPath(gamePath)); - - public T? GetFile(string gamePath, string characterName) where T : FileResource - => GetFileIntern(ResolvePath(gamePath, characterName)); - - public IReadOnlyDictionary GetChangedItemsForCollection(string collectionName) - { - CheckInitialized(); - try - { - if (!_collectionManager.Storage.ByName(collectionName, out var collection)) - collection = ModCollection.Empty; - - if (collection.HasCache) - return collection.ChangedItems.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Item2); - - Penumbra.Log.Warning($"Collection {collectionName} does not exist or is not loaded."); - return new Dictionary(); - } - catch (Exception e) - { - Penumbra.Log.Error($"Could not obtain Changed Items for {collectionName}:\n{e}"); - throw; - } - } - - public string GetCollectionForType(ApiCollectionType type) - { - CheckInitialized(); - if (!Enum.IsDefined(type)) - return string.Empty; - - var collection = _collectionManager.Active.ByType((CollectionType)type); - return collection?.Name ?? string.Empty; - } - - public (PenumbraApiEc, string OldCollection) SetCollectionForType(ApiCollectionType type, string collectionName, bool allowCreateNew, - bool allowDelete) - { - CheckInitialized(); - if (!Enum.IsDefined(type)) - return (PenumbraApiEc.InvalidArgument, string.Empty); - - var oldCollection = _collectionManager.Active.ByType((CollectionType)type)?.Name ?? string.Empty; - - if (collectionName.Length == 0) - { - if (oldCollection.Length == 0) - return (PenumbraApiEc.NothingChanged, oldCollection); - - if (!allowDelete || type is ApiCollectionType.Current or ApiCollectionType.Default or ApiCollectionType.Interface) - return (PenumbraApiEc.AssignmentDeletionDisallowed, oldCollection); - - _collectionManager.Active.RemoveSpecialCollection((CollectionType)type); - return (PenumbraApiEc.Success, oldCollection); - } - - if (!_collectionManager.Storage.ByName(collectionName, out var collection)) - return (PenumbraApiEc.CollectionMissing, oldCollection); - - if (oldCollection.Length == 0) - { - if (!allowCreateNew) - return (PenumbraApiEc.AssignmentCreationDisallowed, oldCollection); - - _collectionManager.Active.CreateSpecialCollection((CollectionType)type); - } - else if (oldCollection == collection.Name) - { - return (PenumbraApiEc.NothingChanged, oldCollection); - } - - _collectionManager.Active.SetCollection(collection, (CollectionType)type); - return (PenumbraApiEc.Success, oldCollection); - } - - public (bool ObjectValid, bool IndividualSet, string EffectiveCollection) GetCollectionForObject(int gameObjectIdx) - { - CheckInitialized(); - var id = AssociatedIdentifier(gameObjectIdx); - if (!id.IsValid) - return (false, false, _collectionManager.Active.Default.Name); - - if (_collectionManager.Active.Individuals.TryGetValue(id, out var collection)) - return (true, true, collection.Name); - - AssociatedCollection(gameObjectIdx, out collection); - return (true, false, collection.Name); - } - - public (PenumbraApiEc, string OldCollection) SetCollectionForObject(int gameObjectIdx, string collectionName, bool allowCreateNew, - bool allowDelete) - { - CheckInitialized(); - var id = AssociatedIdentifier(gameObjectIdx); - if (!id.IsValid) - return (PenumbraApiEc.InvalidIdentifier, _collectionManager.Active.Default.Name); - - var oldCollection = _collectionManager.Active.Individuals.TryGetValue(id, out var c) ? c.Name : string.Empty; - - if (collectionName.Length == 0) - { - if (oldCollection.Length == 0) - return (PenumbraApiEc.NothingChanged, oldCollection); - - if (!allowDelete) - return (PenumbraApiEc.AssignmentDeletionDisallowed, oldCollection); - - var idx = _collectionManager.Active.Individuals.Index(id); - _collectionManager.Active.RemoveIndividualCollection(idx); - return (PenumbraApiEc.Success, oldCollection); - } - - if (!_collectionManager.Storage.ByName(collectionName, out var collection)) - return (PenumbraApiEc.CollectionMissing, oldCollection); - - if (oldCollection.Length == 0) - { - if (!allowCreateNew) - return (PenumbraApiEc.AssignmentCreationDisallowed, oldCollection); - - var ids = _collectionManager.Active.Individuals.GetGroup(id); - _collectionManager.Active.CreateIndividualCollection(ids); - } - else if (oldCollection == collection.Name) - { - return (PenumbraApiEc.NothingChanged, oldCollection); - } - - _collectionManager.Active.SetCollection(collection, CollectionType.Individual, _collectionManager.Active.Individuals.Index(id)); - return (PenumbraApiEc.Success, oldCollection); - } - - public IList GetCollections() - { - CheckInitialized(); - return _collectionManager.Storage.Select(c => c.Name).ToArray(); - } - - public string GetCurrentCollection() - { - CheckInitialized(); - return _collectionManager.Active.Current.Name; - } - - public string GetDefaultCollection() - { - CheckInitialized(); - return _collectionManager.Active.Default.Name; - } - - public string GetInterfaceCollection() - { - CheckInitialized(); - return _collectionManager.Active.Interface.Name; - } - - // TODO: cleanup when incrementing API level - public (string, bool) GetCharacterCollection(string characterName) - => GetCharacterCollection(characterName, ushort.MaxValue); - - public (string, bool) GetCharacterCollection(string characterName, ushort worldId) - { - CheckInitialized(); - return _collectionManager.Active.Individuals.TryGetCollection(NameToIdentifier(characterName, worldId), out var collection) - ? (collection.Name, true) - : (_collectionManager.Active.Default.Name, false); - } - - public unsafe (nint, string) GetDrawObjectInfo(nint drawObject) - { - CheckInitialized(); - var data = _collectionResolver.IdentifyCollection((DrawObject*)drawObject, true); - return (data.AssociatedGameObject, data.ModCollection.Name); - } - - public int GetCutsceneParentIndex(int actorIdx) - { - CheckInitialized(); - return _cutsceneService.GetParentIndex(actorIdx); - } - - public PenumbraApiEc SetCutsceneParentIndex(int copyIdx, int newParentIdx) - { - CheckInitialized(); - if (_cutsceneService.SetParentIndex(copyIdx, newParentIdx)) - return PenumbraApiEc.Success; - - return PenumbraApiEc.InvalidArgument; - } - - public IList<(string, string)> GetModList() - { - CheckInitialized(); - return _modManager.Select(m => (m.ModPath.Name, m.Name.Text)).ToArray(); - } - - public IDictionary, GroupType)>? GetAvailableModSettings(string modDirectory, string modName) - { - CheckInitialized(); - return _modManager.TryGetMod(modDirectory, modName, out var mod) - ? mod.Groups.ToDictionary(g => g.Name, g => ((IList)g.Select(o => o.Name).ToList(), g.Type)) - : null; - } - - public (PenumbraApiEc, (bool, int, IDictionary>, bool)?) GetCurrentModSettings(string collectionName, - string modDirectory, string modName, bool allowInheritance) - { - CheckInitialized(); - if (!_collectionManager.Storage.ByName(collectionName, out var collection)) - return (PenumbraApiEc.CollectionMissing, null); - - if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) - return (PenumbraApiEc.ModMissing, null); - - var settings = allowInheritance ? collection.Settings[mod.Index] : collection[mod.Index].Settings; - if (settings == null) - return (PenumbraApiEc.Success, null); - - var shareSettings = settings.ConvertToShareable(mod); - return (PenumbraApiEc.Success, - (shareSettings.Enabled, shareSettings.Priority.Value, shareSettings.Settings, collection.Settings[mod.Index] != null)); - } - - public PenumbraApiEc ReloadMod(string modDirectory, string modName) - { - CheckInitialized(); - if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) - return Return(PenumbraApiEc.ModMissing, Args("ModDirectory", modDirectory, "ModName", modName)); - - _modManager.ReloadMod(mod); - return Return(PenumbraApiEc.Success, Args("ModDirectory", modDirectory, "ModName", modName)); - } - - public PenumbraApiEc InstallMod(string modFilePackagePath) - { - if (File.Exists(modFilePackagePath)) - { - _modImportManager.AddUnpack(modFilePackagePath); - return Return(PenumbraApiEc.Success, Args("ModFilePackagePath", modFilePackagePath)); - } - else - { - return Return(PenumbraApiEc.FileMissing, Args("ModFilePackagePath", modFilePackagePath)); - } - } - - public PenumbraApiEc AddMod(string modDirectory) - { - CheckInitialized(); - var dir = new DirectoryInfo(Path.Join(_modManager.BasePath.FullName, Path.GetFileName(modDirectory))); - if (!dir.Exists) - return Return(PenumbraApiEc.FileMissing, Args("ModDirectory", modDirectory)); - - - _modManager.AddMod(dir); - if (_config.UseFileSystemCompression) - new FileCompactor(Penumbra.Log).StartMassCompact(dir.EnumerateFiles("*.*", SearchOption.AllDirectories), - CompressionAlgorithm.Xpress8K); - return Return(PenumbraApiEc.Success, Args("ModDirectory", modDirectory)); - } - - public PenumbraApiEc DeleteMod(string modDirectory, string modName) - { - CheckInitialized(); - if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) - return Return(PenumbraApiEc.NothingChanged, Args("ModDirectory", modDirectory, "ModName", modName)); - - _modManager.DeleteMod(mod); - return Return(PenumbraApiEc.Success, Args("ModDirectory", modDirectory, "ModName", modName)); - } - - public event Action? ModDeleted; - public event Action? ModAdded; - public event Action? ModMoved; - - private void ModPathChangeSubscriber(ModPathChangeType type, Mod mod, DirectoryInfo? oldDirectory, - DirectoryInfo? newDirectory) - { - switch (type) - { - case ModPathChangeType.Reloaded: - TriggerSettingEdited(mod); - break; - case ModPathChangeType.Deleted when oldDirectory != null: - ModDeleted?.Invoke(oldDirectory.Name); - break; - case ModPathChangeType.Added when newDirectory != null: - ModAdded?.Invoke(newDirectory.Name); - break; - case ModPathChangeType.Moved when newDirectory != null && oldDirectory != null: - ModMoved?.Invoke(oldDirectory.Name, newDirectory.Name); - break; - } - } - - public (PenumbraApiEc, string, bool) GetModPath(string modDirectory, string modName) - { - CheckInitialized(); - if (!_modManager.TryGetMod(modDirectory, modName, out var mod) - || !_modFileSystem.FindLeaf(mod, out var leaf)) - return (PenumbraApiEc.ModMissing, string.Empty, false); - - var fullPath = leaf.FullName(); - - return (PenumbraApiEc.Success, fullPath, !ModFileSystem.ModHasDefaultPath(mod, fullPath)); - } - - public PenumbraApiEc SetModPath(string modDirectory, string modName, string newPath) - { - CheckInitialized(); - if (newPath.Length == 0) - return PenumbraApiEc.InvalidArgument; - - if (!_modManager.TryGetMod(modDirectory, modName, out var mod) - || !_modFileSystem.FindLeaf(mod, out var leaf)) - return PenumbraApiEc.ModMissing; - - try - { - _modFileSystem.RenameAndMove(leaf, newPath); - return PenumbraApiEc.Success; - } - catch - { - return PenumbraApiEc.PathRenameFailed; - } - } - - public PenumbraApiEc TryInheritMod(string collectionName, string modDirectory, string modName, bool inherit) - { - CheckInitialized(); - if (!_collectionManager.Storage.ByName(collectionName, out var collection)) - return PenumbraApiEc.CollectionMissing; - - if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) - return PenumbraApiEc.ModMissing; - - - return _collectionEditor.SetModInheritance(collection, mod, inherit) ? PenumbraApiEc.Success : PenumbraApiEc.NothingChanged; - } - - public PenumbraApiEc TrySetMod(string collectionName, string modDirectory, string modName, bool enabled) - { - CheckInitialized(); - if (!_collectionManager.Storage.ByName(collectionName, out var collection)) - return PenumbraApiEc.CollectionMissing; - - if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) - return PenumbraApiEc.ModMissing; - - return _collectionEditor.SetModState(collection, mod, enabled) ? PenumbraApiEc.Success : PenumbraApiEc.NothingChanged; - } - - public PenumbraApiEc TrySetModPriority(string collectionName, string modDirectory, string modName, int priority) - { - CheckInitialized(); - if (!_collectionManager.Storage.ByName(collectionName, out var collection)) - return PenumbraApiEc.CollectionMissing; - - if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) - return PenumbraApiEc.ModMissing; - - return _collectionEditor.SetModPriority(collection, mod, new ModPriority(priority)) - ? PenumbraApiEc.Success - : PenumbraApiEc.NothingChanged; - } - - public PenumbraApiEc TrySetModSetting(string collectionName, string modDirectory, string modName, string optionGroupName, - string optionName) - { - CheckInitialized(); - if (!_collectionManager.Storage.ByName(collectionName, out var collection)) - return Return(PenumbraApiEc.CollectionMissing, - Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, - "OptionName", optionName)); - - if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) - return Return(PenumbraApiEc.ModMissing, - Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, - "OptionName", optionName)); - - var groupIdx = mod.Groups.IndexOf(g => g.Name == optionGroupName); - if (groupIdx < 0) - return Return(PenumbraApiEc.OptionGroupMissing, - Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, - "OptionName", optionName)); - - var optionIdx = mod.Groups[groupIdx].IndexOf(o => o.Name == optionName); - if (optionIdx < 0) - return Return(PenumbraApiEc.OptionMissing, - Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, - "OptionName", optionName)); - - var setting = mod.Groups[groupIdx].Type switch - { - GroupType.Multi => Setting.Multi(optionIdx), - GroupType.Single => Setting.Single(optionIdx), - _ => Setting.Zero, - }; - - return Return( - _collectionEditor.SetModSetting(collection, mod, groupIdx, setting) ? PenumbraApiEc.Success : PenumbraApiEc.NothingChanged, - Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, - "OptionName", optionName)); - } - - public PenumbraApiEc TrySetModSettings(string collectionName, string modDirectory, string modName, string optionGroupName, - IReadOnlyList optionNames) - { - CheckInitialized(); - if (!_collectionManager.Storage.ByName(collectionName, out var collection)) - return Return(PenumbraApiEc.CollectionMissing, - Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, - "#optionNames", optionNames.Count.ToString())); - - if (!_modManager.TryGetMod(modDirectory, modName, out var mod)) - return Return(PenumbraApiEc.ModMissing, - Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, - "#optionNames", optionNames.Count.ToString())); - - var groupIdx = mod.Groups.IndexOf(g => g.Name == optionGroupName); - if (groupIdx < 0) - return Return(PenumbraApiEc.OptionGroupMissing, - Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, - "#optionNames", optionNames.Count.ToString())); - - var group = mod.Groups[groupIdx]; - - var setting = Setting.Zero; - if (group.Type == GroupType.Single) - { - var optionIdx = optionNames.Count == 0 ? -1 : group.IndexOf(o => o.Name == optionNames[^1]); - if (optionIdx < 0) - return Return(PenumbraApiEc.OptionMissing, - Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, - "#optionNames", optionNames.Count.ToString())); - - setting = Setting.Single(optionIdx); - } - else - { - foreach (var name in optionNames) - { - var optionIdx = group.IndexOf(o => o.Name == name); - if (optionIdx < 0) - return Return(PenumbraApiEc.OptionMissing, - Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", - optionGroupName, "#optionNames", optionNames.Count.ToString())); - - setting |= Setting.Multi(optionIdx); - } - } - - return Return( - _collectionEditor.SetModSetting(collection, mod, groupIdx, setting) ? PenumbraApiEc.Success : PenumbraApiEc.NothingChanged, - Args("CollectionName", collectionName, "ModDirectory", modDirectory, "ModName", modName, "OptionGroupName", optionGroupName, - "#optionNames", optionNames.Count.ToString())); - } - - - public PenumbraApiEc CopyModSettings(string? collectionName, string modDirectoryFrom, string modDirectoryTo) - { - CheckInitialized(); - - var sourceMod = _modManager.FirstOrDefault(m => string.Equals(m.ModPath.Name, modDirectoryFrom, StringComparison.OrdinalIgnoreCase)); - var targetMod = _modManager.FirstOrDefault(m => string.Equals(m.ModPath.Name, modDirectoryTo, StringComparison.OrdinalIgnoreCase)); - if (string.IsNullOrEmpty(collectionName)) - foreach (var collection in _collectionManager.Storage) - _collectionEditor.CopyModSettings(collection, sourceMod, modDirectoryFrom, targetMod, modDirectoryTo); - else if (_collectionManager.Storage.ByName(collectionName, out var collection)) - _collectionEditor.CopyModSettings(collection, sourceMod, modDirectoryFrom, targetMod, modDirectoryTo); - else - return PenumbraApiEc.CollectionMissing; - - return PenumbraApiEc.Success; - } - - public (PenumbraApiEc, string) CreateTemporaryCollection(string tag, string character, bool forceOverwriteCharacter) - { - CheckInitialized(); - - if (!ActorIdentifierFactory.VerifyPlayerName(character.AsSpan()) || tag.Length == 0) - return (PenumbraApiEc.InvalidArgument, string.Empty); - - var identifier = NameToIdentifier(character, ushort.MaxValue); - if (!identifier.IsValid) - return (PenumbraApiEc.InvalidArgument, string.Empty); - - if (!forceOverwriteCharacter && _collectionManager.Active.Individuals.ContainsKey(identifier) - || _tempCollections.Collections.ContainsKey(identifier)) - return (PenumbraApiEc.CharacterCollectionExists, string.Empty); - - var name = $"{tag}_{character}"; - var ret = CreateNamedTemporaryCollection(name); - if (ret != PenumbraApiEc.Success) - return (ret, name); - - if (_tempCollections.AddIdentifier(name, identifier)) - return (PenumbraApiEc.Success, name); - - _tempCollections.RemoveTemporaryCollection(name); - return (PenumbraApiEc.UnknownError, string.Empty); - } - - public PenumbraApiEc CreateNamedTemporaryCollection(string name) - { - CheckInitialized(); - if (name.Length == 0 || ModCreator.ReplaceBadXivSymbols(name, _config.ReplaceNonAsciiOnImport) != name || name.Contains('|')) - return PenumbraApiEc.InvalidArgument; - - return _tempCollections.CreateTemporaryCollection(name).Length > 0 - ? PenumbraApiEc.Success - : PenumbraApiEc.CollectionExists; - } - - public PenumbraApiEc AssignTemporaryCollection(string collectionName, int actorIndex, bool forceAssignment) - { - CheckInitialized(); - - if (actorIndex < 0 || actorIndex >= _objects.TotalCount) - return PenumbraApiEc.InvalidArgument; - - var identifier = _actors.FromObject(_objects[actorIndex], out _, false, false, true); - if (!identifier.IsValid) - return PenumbraApiEc.InvalidArgument; - - if (!_tempCollections.CollectionByName(collectionName, out var collection)) - return PenumbraApiEc.CollectionMissing; - - if (forceAssignment) - { - if (_tempCollections.Collections.ContainsKey(identifier) && !_tempCollections.Collections.Delete(identifier)) - return PenumbraApiEc.AssignmentDeletionFailed; - } - else if (_tempCollections.Collections.ContainsKey(identifier) - || _collectionManager.Active.Individuals.ContainsKey(identifier)) - { - return PenumbraApiEc.CharacterCollectionExists; - } - - var group = _tempCollections.Collections.GetGroup(identifier); - return _tempCollections.AddIdentifier(collection, group) - ? PenumbraApiEc.Success - : PenumbraApiEc.UnknownError; - } - - public PenumbraApiEc RemoveTemporaryCollection(string character) - { - CheckInitialized(); - return _tempCollections.RemoveByCharacterName(character) - ? PenumbraApiEc.Success - : PenumbraApiEc.NothingChanged; - } - - public PenumbraApiEc RemoveTemporaryCollectionByName(string name) - { - CheckInitialized(); - return _tempCollections.RemoveTemporaryCollection(name) - ? PenumbraApiEc.Success - : PenumbraApiEc.NothingChanged; - } - - public PenumbraApiEc AddTemporaryModAll(string tag, Dictionary paths, string manipString, int priority) - { - CheckInitialized(); - if (!ConvertPaths(paths, out var p)) - return PenumbraApiEc.InvalidGamePath; - - if (!ConvertManips(manipString, out var m)) - return PenumbraApiEc.InvalidManipulation; - - return _tempMods.Register(tag, null, p, m, new ModPriority(priority)) switch - { - RedirectResult.Success => PenumbraApiEc.Success, - _ => PenumbraApiEc.UnknownError, - }; - } - - public PenumbraApiEc AddTemporaryMod(string tag, string collectionName, Dictionary paths, string manipString, - int priority) - { - CheckInitialized(); - if (!_tempCollections.CollectionByName(collectionName, out var collection) - && !_collectionManager.Storage.ByName(collectionName, out collection)) - return PenumbraApiEc.CollectionMissing; - - if (!ConvertPaths(paths, out var p)) - return PenumbraApiEc.InvalidGamePath; - - if (!ConvertManips(manipString, out var m)) - return PenumbraApiEc.InvalidManipulation; - - return _tempMods.Register(tag, collection, p, m, new ModPriority(priority)) switch - { - RedirectResult.Success => PenumbraApiEc.Success, - _ => PenumbraApiEc.UnknownError, - }; - } - - public PenumbraApiEc RemoveTemporaryModAll(string tag, int priority) - { - CheckInitialized(); - return _tempMods.Unregister(tag, null, new ModPriority(priority)) switch - { - RedirectResult.Success => PenumbraApiEc.Success, - RedirectResult.NotRegistered => PenumbraApiEc.NothingChanged, - _ => PenumbraApiEc.UnknownError, - }; - } - - public PenumbraApiEc RemoveTemporaryMod(string tag, string collectionName, int priority) - { - CheckInitialized(); - if (!_tempCollections.CollectionByName(collectionName, out var collection) - && !_collectionManager.Storage.ByName(collectionName, out collection)) - return PenumbraApiEc.CollectionMissing; - - return _tempMods.Unregister(tag, collection, new ModPriority(priority)) switch - { - RedirectResult.Success => PenumbraApiEc.Success, - RedirectResult.NotRegistered => PenumbraApiEc.NothingChanged, - _ => PenumbraApiEc.UnknownError, - }; - } - - public string GetPlayerMetaManipulations() - { - CheckInitialized(); - var collection = _collectionResolver.PlayerCollection(); - var set = collection.MetaCache?.Manipulations.ToArray() ?? Array.Empty(); - return Functions.ToCompressedBase64(set, MetaManipulation.CurrentVersion); - } - - public Task ConvertTextureFile(string inputFile, string outputFile, TextureType textureType, bool mipMaps) - => textureType switch - { - TextureType.Png => _textureManager.SavePng(inputFile, outputFile), - TextureType.AsIsTex => _textureManager.SaveAs(CombinedTexture.TextureSaveType.AsIs, mipMaps, true, inputFile, outputFile), - TextureType.AsIsDds => _textureManager.SaveAs(CombinedTexture.TextureSaveType.AsIs, mipMaps, false, inputFile, outputFile), - TextureType.RgbaTex => _textureManager.SaveAs(CombinedTexture.TextureSaveType.Bitmap, mipMaps, true, inputFile, outputFile), - TextureType.RgbaDds => _textureManager.SaveAs(CombinedTexture.TextureSaveType.Bitmap, mipMaps, false, inputFile, outputFile), - TextureType.Bc3Tex => _textureManager.SaveAs(CombinedTexture.TextureSaveType.BC3, mipMaps, true, inputFile, outputFile), - TextureType.Bc3Dds => _textureManager.SaveAs(CombinedTexture.TextureSaveType.BC3, mipMaps, false, inputFile, outputFile), - TextureType.Bc7Tex => _textureManager.SaveAs(CombinedTexture.TextureSaveType.BC7, mipMaps, true, inputFile, outputFile), - TextureType.Bc7Dds => _textureManager.SaveAs(CombinedTexture.TextureSaveType.BC7, mipMaps, false, inputFile, outputFile), - _ => Task.FromException(new Exception($"Invalid input value {textureType}.")), - }; - - // @formatter:off - public Task ConvertTextureData(byte[] rgbaData, int width, string outputFile, TextureType textureType, bool mipMaps) - => textureType switch - { - TextureType.Png => _textureManager.SavePng(new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), - TextureType.AsIsTex => _textureManager.SaveAs(CombinedTexture.TextureSaveType.AsIs, mipMaps, true, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), - TextureType.AsIsDds => _textureManager.SaveAs(CombinedTexture.TextureSaveType.AsIs, mipMaps, false, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), - TextureType.RgbaTex => _textureManager.SaveAs(CombinedTexture.TextureSaveType.Bitmap, mipMaps, true, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), - TextureType.RgbaDds => _textureManager.SaveAs(CombinedTexture.TextureSaveType.Bitmap, mipMaps, false, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), - TextureType.Bc3Tex => _textureManager.SaveAs(CombinedTexture.TextureSaveType.BC3, mipMaps, true, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), - TextureType.Bc3Dds => _textureManager.SaveAs(CombinedTexture.TextureSaveType.BC3, mipMaps, false, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), - TextureType.Bc7Tex => _textureManager.SaveAs(CombinedTexture.TextureSaveType.BC7, mipMaps, true, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), - TextureType.Bc7Dds => _textureManager.SaveAs(CombinedTexture.TextureSaveType.BC7, mipMaps, false, new BaseImage(), outputFile, rgbaData, width, rgbaData.Length / 4 / width), - _ => Task.FromException(new Exception($"Invalid input value {textureType}.")), - }; - // @formatter:on - - public IReadOnlyDictionary?[] GetGameObjectResourcePaths(ushort[] gameObjects) - { - var characters = gameObjects.Select(index => _objects.GetDalamudObject((int)index)).OfType(); - var resourceTrees = _resourceTreeFactory.FromCharacters(characters, 0); - var pathDictionaries = ResourceTreeApiHelper.GetResourcePathDictionaries(resourceTrees); - - return Array.ConvertAll(gameObjects, obj => pathDictionaries.GetValueOrDefault(obj)); - } - - public IReadOnlyDictionary> GetPlayerResourcePaths() - { - var resourceTrees = _resourceTreeFactory.FromObjectTable(ResourceTreeFactory.Flags.LocalPlayerRelatedOnly); - var pathDictionaries = ResourceTreeApiHelper.GetResourcePathDictionaries(resourceTrees); - - return pathDictionaries.AsReadOnly(); - } - - public IReadOnlyDictionary?[] GetGameObjectResourcesOfType(ResourceType type, bool withUiData, - params ushort[] gameObjects) - { - var characters = gameObjects.Select(index => _objects.GetDalamudObject((int)index)).OfType(); - var resourceTrees = _resourceTreeFactory.FromCharacters(characters, withUiData ? ResourceTreeFactory.Flags.WithUiData : 0); - var resDictionaries = ResourceTreeApiHelper.GetResourcesOfType(resourceTrees, type); - - return Array.ConvertAll(gameObjects, obj => resDictionaries.GetValueOrDefault(obj)); - } - - public IReadOnlyDictionary> GetPlayerResourcesOfType(ResourceType type, - bool withUiData) - { - var resourceTrees = _resourceTreeFactory.FromObjectTable(ResourceTreeFactory.Flags.LocalPlayerRelatedOnly - | (withUiData ? ResourceTreeFactory.Flags.WithUiData : 0)); - var resDictionaries = ResourceTreeApiHelper.GetResourcesOfType(resourceTrees, type); - - return resDictionaries.AsReadOnly(); - } - - public Ipc.ResourceTree?[] GetGameObjectResourceTrees(bool withUiData, params ushort[] gameObjects) - { - var characters = gameObjects.Select(index => _objects.GetDalamudObject((int)index)).OfType(); - var resourceTrees = _resourceTreeFactory.FromCharacters(characters, withUiData ? ResourceTreeFactory.Flags.WithUiData : 0); - var resDictionary = ResourceTreeApiHelper.EncapsulateResourceTrees(resourceTrees); - - return Array.ConvertAll(gameObjects, obj => resDictionary.GetValueOrDefault(obj)); - } - - public IReadOnlyDictionary GetPlayerResourceTrees(bool withUiData) - { - var resourceTrees = _resourceTreeFactory.FromObjectTable(ResourceTreeFactory.Flags.LocalPlayerRelatedOnly - | (withUiData ? ResourceTreeFactory.Flags.WithUiData : 0)); - var resDictionary = ResourceTreeApiHelper.EncapsulateResourceTrees(resourceTrees); - - return resDictionary.AsReadOnly(); - } - - // TODO: cleanup when incrementing API - public string GetMetaManipulations(string characterName) - => GetMetaManipulations(characterName, ushort.MaxValue); - - public string GetMetaManipulations(string characterName, ushort worldId) - { - CheckInitialized(); - var identifier = NameToIdentifier(characterName, worldId); - var collection = _tempCollections.Collections.TryGetCollection(identifier, out var c) - ? c - : _collectionManager.Active.Individual(identifier); - var set = collection.MetaCache?.Manipulations.ToArray() ?? []; - return Functions.ToCompressedBase64(set, MetaManipulation.CurrentVersion); - } - - public string GetGameObjectMetaManipulations(int gameObjectIdx) - { - CheckInitialized(); - AssociatedCollection(gameObjectIdx, out var collection); - var set = collection.MetaCache?.Manipulations.ToArray() ?? []; - return Functions.ToCompressedBase64(set, MetaManipulation.CurrentVersion); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private void CheckInitialized() - { - if (!Valid) - throw new Exception("PluginShare is not initialized."); - } - - // Return the collection associated to a current game object. If it does not exist, return the default collection. - // If the index is invalid, returns false and the default collection. - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private unsafe bool AssociatedCollection(int gameObjectIdx, out ModCollection collection) - { - collection = _collectionManager.Active.Default; - if (gameObjectIdx < 0 || gameObjectIdx >= _objects.TotalCount) - return false; - - var ptr = _objects[gameObjectIdx]; - var data = _collectionResolver.IdentifyCollection(ptr.AsObject, false); - if (data.Valid) - collection = data.ModCollection; - - return true; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private ActorIdentifier AssociatedIdentifier(int gameObjectIdx) - { - if (gameObjectIdx < 0 || gameObjectIdx >= _objects.TotalCount) - return ActorIdentifier.Invalid; - - var ptr = _objects[gameObjectIdx]; - return _actors.FromObject(ptr, out _, false, true, true); - } - - // Resolve a path given by string for a specific collection. - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private string ResolvePath(string path, ModManager _, ModCollection collection) - { - if (!_config.EnableMods) - return path; - - var gamePath = Utf8GamePath.FromString(path, out var p, true) ? p : Utf8GamePath.Empty; - var ret = collection.ResolvePath(gamePath); - return ret?.ToString() ?? path; - } - - // Get a file for a resolved path. - private T? GetFileIntern(string resolvedPath) where T : FileResource - { - CheckInitialized(); - try - { - return Path.IsPathRooted(resolvedPath) - ? _lumina?.GetFileFromDisk(resolvedPath) - : _gameData.GetFile(resolvedPath); - } - catch (Exception e) - { - Penumbra.Log.Warning($"Could not load file {resolvedPath}:\n{e}"); - return null; - } - } - - - // Convert a dictionary of strings to a dictionary of gamepaths to full paths. - // Only returns true if all paths can successfully be converted and added. - private static bool ConvertPaths(IReadOnlyDictionary redirections, - [NotNullWhen(true)] out Dictionary? paths) - { - paths = new Dictionary(redirections.Count); - foreach (var (gString, fString) in redirections) - { - if (!Utf8GamePath.FromString(gString, out var path, false)) - { - paths = null; - return false; - } - - var fullPath = new FullPath(fString); - if (!paths.TryAdd(path, fullPath)) - { - paths = null; - return false; - } - } - - return true; - } - - // Convert manipulations from a transmitted base64 string to actual manipulations. - // The empty string is treated as an empty set. - // Only returns true if all conversions are successful and distinct. - private static bool ConvertManips(string manipString, - [NotNullWhen(true)] out HashSet? manips) - { - if (manipString.Length == 0) - { - manips = new HashSet(); - return true; - } - - if (Functions.FromCompressedBase64(manipString, out var manipArray) != MetaManipulation.CurrentVersion) - { - manips = null; - return false; - } - - manips = new HashSet(manipArray!.Length); - foreach (var manip in manipArray.Where(m => m.Validate())) - { - if (manips.Add(manip)) - continue; - - Penumbra.Log.Warning($"Manipulation {manip} {manip.EntryToString()} is invalid and was skipped."); - manips = null; - return false; - } - - return true; - } - - // TODO: replace all usages with ActorIdentifier stuff when incrementing API - private ActorIdentifier NameToIdentifier(string name, ushort worldId) - { - // Verified to be valid name beforehand. - var b = ByteString.FromStringUnsafe(name, false); - return _actors.CreatePlayer(b, worldId); - } - - private void OnModSettingChange(ModCollection collection, ModSettingChange type, Mod? mod, Setting _1, int _2, bool inherited) - => ModSettingChanged?.Invoke(type, collection.Name, mod?.ModPath.Name ?? string.Empty, inherited); - - private void OnCreatedCharacterBase(nint gameObject, ModCollection collection, nint drawObject) - => CreatedCharacterBase?.Invoke(gameObject, collection.Name, drawObject); - - private void OnModOptionEdited(ModOptionChangeType type, Mod mod, int groupIndex, int optionIndex, int moveIndex) - { - switch (type) - { - case ModOptionChangeType.GroupDeleted: - case ModOptionChangeType.GroupMoved: - case ModOptionChangeType.GroupTypeChanged: - case ModOptionChangeType.PriorityChanged: - case ModOptionChangeType.OptionDeleted: - case ModOptionChangeType.OptionMoved: - case ModOptionChangeType.OptionFilesChanged: - case ModOptionChangeType.OptionFilesAdded: - case ModOptionChangeType.OptionSwapsChanged: - case ModOptionChangeType.OptionMetaChanged: - TriggerSettingEdited(mod); - break; - } - } - - private void OnModFileChanged(Mod mod, FileRegistry file) - { - if (file.CurrentUsage == 0) - return; - - TriggerSettingEdited(mod); - } - - private void TriggerSettingEdited(Mod mod) - { - var collection = _collectionResolver.PlayerCollection(); - var (settings, parent) = collection[mod.Index]; - if (settings is { Enabled: true }) - ModSettingChanged?.Invoke(ModSettingChange.Edited, collection.Name, mod.Identifier, parent != collection); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private static LazyString Args(params string[] arguments) - { - if (arguments.Length == 0) - return new LazyString(() => "no arguments"); - - return new LazyString(() => - { - var sb = new StringBuilder(); - for (var i = 0; i < arguments.Length / 2; ++i) - { - sb.Append(arguments[2 * i]); - sb.Append(" = "); - sb.Append(arguments[2 * i + 1]); - sb.Append(", "); - } - - return sb.ToString(0, sb.Length - 2); - }); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private static PenumbraApiEc Return(PenumbraApiEc ec, LazyString args, [CallerMemberName] string name = "Unknown") - { - Penumbra.Log.Debug( - $"[{name}] Called with {args}, returned {ec}."); - return ec; - } -} diff --git a/Penumbra/Api/PenumbraIpcProviders.cs b/Penumbra/Api/PenumbraIpcProviders.cs deleted file mode 100644 index 78887156..00000000 --- a/Penumbra/Api/PenumbraIpcProviders.cs +++ /dev/null @@ -1,435 +0,0 @@ -using Dalamud.Game.ClientState.Objects.Types; -using Dalamud.Plugin; -using Penumbra.GameData.Enums; -using Penumbra.Api.Enums; -using Penumbra.Api.Helpers; -using Penumbra.Collections.Manager; -using Penumbra.Mods.Manager; -using Penumbra.Services; -using Penumbra.Util; - -namespace Penumbra.Api; - -using CurrentSettings = ValueTuple>, bool)?>; - -public class PenumbraIpcProviders : IDisposable -{ - internal readonly IPenumbraApi Api; - - // Plugin State - internal readonly EventProvider Initialized; - internal readonly EventProvider Disposed; - internal readonly FuncProvider ApiVersion; - internal readonly FuncProvider<(int Breaking, int Features)> ApiVersions; - internal readonly FuncProvider GetEnabledState; - internal readonly EventProvider EnabledChange; - - // Configuration - internal readonly FuncProvider GetModDirectory; - internal readonly FuncProvider GetConfiguration; - internal readonly EventProvider ModDirectoryChanged; - - // UI - internal readonly EventProvider PreSettingsTabBarDraw; - internal readonly EventProvider PreSettingsDraw; - internal readonly EventProvider PostEnabledDraw; - internal readonly EventProvider PostSettingsDraw; - internal readonly EventProvider ChangedItemTooltip; - internal readonly EventProvider ChangedItemClick; - internal readonly FuncProvider OpenMainWindow; - internal readonly ActionProvider CloseMainWindow; - - // Redrawing - internal readonly ActionProvider RedrawAll; - internal readonly ActionProvider RedrawObject; - internal readonly ActionProvider RedrawObjectByIndex; - internal readonly ActionProvider RedrawObjectByName; - internal readonly EventProvider GameObjectRedrawn; - - // Game State - internal readonly FuncProvider GetDrawObjectInfo; - internal readonly FuncProvider GetCutsceneParentIndex; - internal readonly FuncProvider SetCutsceneParentIndex; - internal readonly EventProvider CreatingCharacterBase; - internal readonly EventProvider CreatedCharacterBase; - internal readonly EventProvider GameObjectResourcePathResolved; - - // Resolve - internal readonly FuncProvider ResolveDefaultPath; - internal readonly FuncProvider ResolveInterfacePath; - internal readonly FuncProvider ResolvePlayerPath; - internal readonly FuncProvider ResolveGameObjectPath; - internal readonly FuncProvider ResolveCharacterPath; - internal readonly FuncProvider ReverseResolvePath; - internal readonly FuncProvider ReverseResolveGameObjectPath; - internal readonly FuncProvider ReverseResolvePlayerPath; - internal readonly FuncProvider ResolvePlayerPaths; - internal readonly FuncProvider> ResolvePlayerPathsAsync; - - // Collections - internal readonly FuncProvider> GetCollections; - internal readonly FuncProvider GetCurrentCollectionName; - internal readonly FuncProvider GetDefaultCollectionName; - internal readonly FuncProvider GetInterfaceCollectionName; - internal readonly FuncProvider GetCharacterCollectionName; - internal readonly FuncProvider GetCollectionForType; - internal readonly FuncProvider SetCollectionForType; - internal readonly FuncProvider GetCollectionForObject; - internal readonly FuncProvider SetCollectionForObject; - internal readonly FuncProvider> GetChangedItems; - - // Meta - internal readonly FuncProvider GetPlayerMetaManipulations; - internal readonly FuncProvider GetMetaManipulations; - internal readonly FuncProvider GetGameObjectMetaManipulations; - - // Mods - internal readonly FuncProvider> GetMods; - internal readonly FuncProvider ReloadMod; - internal readonly FuncProvider InstallMod; - internal readonly FuncProvider AddMod; - internal readonly FuncProvider DeleteMod; - internal readonly FuncProvider GetModPath; - internal readonly FuncProvider SetModPath; - internal readonly EventProvider ModDeleted; - internal readonly EventProvider ModAdded; - internal readonly EventProvider ModMoved; - - // ModSettings - internal readonly FuncProvider, GroupType)>?> GetAvailableModSettings; - internal readonly FuncProvider GetCurrentModSettings; - internal readonly FuncProvider TryInheritMod; - internal readonly FuncProvider TrySetMod; - internal readonly FuncProvider TrySetModPriority; - internal readonly FuncProvider TrySetModSetting; - internal readonly FuncProvider, PenumbraApiEc> TrySetModSettings; - internal readonly EventProvider ModSettingChanged; - internal readonly FuncProvider CopyModSettings; - - // Editing - internal readonly FuncProvider ConvertTextureFile; - internal readonly FuncProvider ConvertTextureData; - - // Temporary - internal readonly FuncProvider CreateTemporaryCollection; - internal readonly FuncProvider RemoveTemporaryCollection; - internal readonly FuncProvider CreateNamedTemporaryCollection; - internal readonly FuncProvider RemoveTemporaryCollectionByName; - internal readonly FuncProvider AssignTemporaryCollection; - internal readonly FuncProvider, string, int, PenumbraApiEc> AddTemporaryModAll; - internal readonly FuncProvider, string, int, PenumbraApiEc> AddTemporaryMod; - internal readonly FuncProvider RemoveTemporaryModAll; - internal readonly FuncProvider RemoveTemporaryMod; - - // Resource Tree - internal readonly FuncProvider?[]> GetGameObjectResourcePaths; - internal readonly FuncProvider>> GetPlayerResourcePaths; - - internal readonly FuncProvider?[]> - GetGameObjectResourcesOfType; - - internal readonly - FuncProvider>> - GetPlayerResourcesOfType; - - internal readonly FuncProvider GetGameObjectResourceTrees; - internal readonly FuncProvider> GetPlayerResourceTrees; - - public PenumbraIpcProviders(DalamudPluginInterface pi, IPenumbraApi api, ModManager modManager, CollectionManager collections, - TempModManager tempMods, TempCollectionManager tempCollections, SaveService saveService, Configuration config) - { - Api = api; - - // Plugin State - Initialized = Ipc.Initialized.Provider(pi); - Disposed = Ipc.Disposed.Provider(pi); - ApiVersion = Ipc.ApiVersion.Provider(pi, DeprecatedVersion); - ApiVersions = Ipc.ApiVersions.Provider(pi, () => Api.ApiVersion); - GetEnabledState = Ipc.GetEnabledState.Provider(pi, Api.GetEnabledState); - EnabledChange = - Ipc.EnabledChange.Provider(pi, () => Api.EnabledChange += EnabledChangeEvent, () => Api.EnabledChange -= EnabledChangeEvent); - - // Configuration - GetModDirectory = Ipc.GetModDirectory.Provider(pi, Api.GetModDirectory); - GetConfiguration = Ipc.GetConfiguration.Provider(pi, Api.GetConfiguration); - ModDirectoryChanged = Ipc.ModDirectoryChanged.Provider(pi, a => Api.ModDirectoryChanged += a, a => Api.ModDirectoryChanged -= a); - - // UI - PreSettingsTabBarDraw = - Ipc.PreSettingsTabBarDraw.Provider(pi, a => Api.PreSettingsTabBarDraw += a, a => Api.PreSettingsTabBarDraw -= a); - PreSettingsDraw = Ipc.PreSettingsDraw.Provider(pi, a => Api.PreSettingsPanelDraw += a, a => Api.PreSettingsPanelDraw -= a); - PostEnabledDraw = - Ipc.PostEnabledDraw.Provider(pi, a => Api.PostEnabledDraw += a, a => Api.PostEnabledDraw -= a); - PostSettingsDraw = Ipc.PostSettingsDraw.Provider(pi, a => Api.PostSettingsPanelDraw += a, a => Api.PostSettingsPanelDraw -= a); - ChangedItemTooltip = - Ipc.ChangedItemTooltip.Provider(pi, () => Api.ChangedItemTooltip += OnTooltip, () => Api.ChangedItemTooltip -= OnTooltip); - ChangedItemClick = Ipc.ChangedItemClick.Provider(pi, () => Api.ChangedItemClicked += OnClick, () => Api.ChangedItemClicked -= OnClick); - OpenMainWindow = Ipc.OpenMainWindow.Provider(pi, Api.OpenMainWindow); - CloseMainWindow = Ipc.CloseMainWindow.Provider(pi, Api.CloseMainWindow); - - // Redrawing - RedrawAll = Ipc.RedrawAll.Provider(pi, Api.RedrawAll); - RedrawObject = Ipc.RedrawObject.Provider(pi, Api.RedrawObject); - RedrawObjectByIndex = Ipc.RedrawObjectByIndex.Provider(pi, Api.RedrawObject); - RedrawObjectByName = Ipc.RedrawObjectByName.Provider(pi, Api.RedrawObject); - GameObjectRedrawn = Ipc.GameObjectRedrawn.Provider(pi, () => Api.GameObjectRedrawn += OnGameObjectRedrawn, - () => Api.GameObjectRedrawn -= OnGameObjectRedrawn); - - // Game State - GetDrawObjectInfo = Ipc.GetDrawObjectInfo.Provider(pi, Api.GetDrawObjectInfo); - GetCutsceneParentIndex = Ipc.GetCutsceneParentIndex.Provider(pi, Api.GetCutsceneParentIndex); - SetCutsceneParentIndex = Ipc.SetCutsceneParentIndex.Provider(pi, Api.SetCutsceneParentIndex); - CreatingCharacterBase = Ipc.CreatingCharacterBase.Provider(pi, - () => Api.CreatingCharacterBase += CreatingCharacterBaseEvent, - () => Api.CreatingCharacterBase -= CreatingCharacterBaseEvent); - CreatedCharacterBase = Ipc.CreatedCharacterBase.Provider(pi, - () => Api.CreatedCharacterBase += CreatedCharacterBaseEvent, - () => Api.CreatedCharacterBase -= CreatedCharacterBaseEvent); - GameObjectResourcePathResolved = Ipc.GameObjectResourcePathResolved.Provider(pi, - () => Api.GameObjectResourceResolved += GameObjectResourceResolvedEvent, - () => Api.GameObjectResourceResolved -= GameObjectResourceResolvedEvent); - - // Resolve - ResolveDefaultPath = Ipc.ResolveDefaultPath.Provider(pi, Api.ResolveDefaultPath); - ResolveInterfacePath = Ipc.ResolveInterfacePath.Provider(pi, Api.ResolveInterfacePath); - ResolvePlayerPath = Ipc.ResolvePlayerPath.Provider(pi, Api.ResolvePlayerPath); - ResolveGameObjectPath = Ipc.ResolveGameObjectPath.Provider(pi, Api.ResolveGameObjectPath); - ResolveCharacterPath = Ipc.ResolveCharacterPath.Provider(pi, Api.ResolvePath); - ReverseResolvePath = Ipc.ReverseResolvePath.Provider(pi, Api.ReverseResolvePath); - ReverseResolveGameObjectPath = Ipc.ReverseResolveGameObjectPath.Provider(pi, Api.ReverseResolveGameObjectPath); - ReverseResolvePlayerPath = Ipc.ReverseResolvePlayerPath.Provider(pi, Api.ReverseResolvePlayerPath); - ResolvePlayerPaths = Ipc.ResolvePlayerPaths.Provider(pi, Api.ResolvePlayerPaths); - ResolvePlayerPathsAsync = Ipc.ResolvePlayerPathsAsync.Provider(pi, Api.ResolvePlayerPathsAsync); - - // Collections - GetCollections = Ipc.GetCollections.Provider(pi, Api.GetCollections); - GetCurrentCollectionName = Ipc.GetCurrentCollectionName.Provider(pi, Api.GetCurrentCollection); - GetDefaultCollectionName = Ipc.GetDefaultCollectionName.Provider(pi, Api.GetDefaultCollection); - GetInterfaceCollectionName = Ipc.GetInterfaceCollectionName.Provider(pi, Api.GetInterfaceCollection); - GetCharacterCollectionName = Ipc.GetCharacterCollectionName.Provider(pi, Api.GetCharacterCollection); - GetCollectionForType = Ipc.GetCollectionForType.Provider(pi, Api.GetCollectionForType); - SetCollectionForType = Ipc.SetCollectionForType.Provider(pi, Api.SetCollectionForType); - GetCollectionForObject = Ipc.GetCollectionForObject.Provider(pi, Api.GetCollectionForObject); - SetCollectionForObject = Ipc.SetCollectionForObject.Provider(pi, Api.SetCollectionForObject); - GetChangedItems = Ipc.GetChangedItems.Provider(pi, Api.GetChangedItemsForCollection); - - // Meta - GetPlayerMetaManipulations = Ipc.GetPlayerMetaManipulations.Provider(pi, Api.GetPlayerMetaManipulations); - GetMetaManipulations = Ipc.GetMetaManipulations.Provider(pi, Api.GetMetaManipulations); - GetGameObjectMetaManipulations = Ipc.GetGameObjectMetaManipulations.Provider(pi, Api.GetGameObjectMetaManipulations); - - // Mods - GetMods = Ipc.GetMods.Provider(pi, Api.GetModList); - ReloadMod = Ipc.ReloadMod.Provider(pi, Api.ReloadMod); - InstallMod = Ipc.InstallMod.Provider(pi, Api.InstallMod); - AddMod = Ipc.AddMod.Provider(pi, Api.AddMod); - DeleteMod = Ipc.DeleteMod.Provider(pi, Api.DeleteMod); - GetModPath = Ipc.GetModPath.Provider(pi, Api.GetModPath); - SetModPath = Ipc.SetModPath.Provider(pi, Api.SetModPath); - ModDeleted = Ipc.ModDeleted.Provider(pi, () => Api.ModDeleted += ModDeletedEvent, () => Api.ModDeleted -= ModDeletedEvent); - ModAdded = Ipc.ModAdded.Provider(pi, () => Api.ModAdded += ModAddedEvent, () => Api.ModAdded -= ModAddedEvent); - ModMoved = Ipc.ModMoved.Provider(pi, () => Api.ModMoved += ModMovedEvent, () => Api.ModMoved -= ModMovedEvent); - - // ModSettings - GetAvailableModSettings = Ipc.GetAvailableModSettings.Provider(pi, Api.GetAvailableModSettings); - GetCurrentModSettings = Ipc.GetCurrentModSettings.Provider(pi, Api.GetCurrentModSettings); - TryInheritMod = Ipc.TryInheritMod.Provider(pi, Api.TryInheritMod); - TrySetMod = Ipc.TrySetMod.Provider(pi, Api.TrySetMod); - TrySetModPriority = Ipc.TrySetModPriority.Provider(pi, Api.TrySetModPriority); - TrySetModSetting = Ipc.TrySetModSetting.Provider(pi, Api.TrySetModSetting); - TrySetModSettings = Ipc.TrySetModSettings.Provider(pi, Api.TrySetModSettings); - ModSettingChanged = Ipc.ModSettingChanged.Provider(pi, - () => Api.ModSettingChanged += ModSettingChangedEvent, - () => Api.ModSettingChanged -= ModSettingChangedEvent); - CopyModSettings = Ipc.CopyModSettings.Provider(pi, Api.CopyModSettings); - - // Editing - ConvertTextureFile = Ipc.ConvertTextureFile.Provider(pi, Api.ConvertTextureFile); - ConvertTextureData = Ipc.ConvertTextureData.Provider(pi, Api.ConvertTextureData); - - // Temporary - CreateTemporaryCollection = Ipc.CreateTemporaryCollection.Provider(pi, Api.CreateTemporaryCollection); - RemoveTemporaryCollection = Ipc.RemoveTemporaryCollection.Provider(pi, Api.RemoveTemporaryCollection); - CreateNamedTemporaryCollection = Ipc.CreateNamedTemporaryCollection.Provider(pi, Api.CreateNamedTemporaryCollection); - RemoveTemporaryCollectionByName = Ipc.RemoveTemporaryCollectionByName.Provider(pi, Api.RemoveTemporaryCollectionByName); - AssignTemporaryCollection = Ipc.AssignTemporaryCollection.Provider(pi, Api.AssignTemporaryCollection); - AddTemporaryModAll = Ipc.AddTemporaryModAll.Provider(pi, Api.AddTemporaryModAll); - AddTemporaryMod = Ipc.AddTemporaryMod.Provider(pi, Api.AddTemporaryMod); - RemoveTemporaryModAll = Ipc.RemoveTemporaryModAll.Provider(pi, Api.RemoveTemporaryModAll); - RemoveTemporaryMod = Ipc.RemoveTemporaryMod.Provider(pi, Api.RemoveTemporaryMod); - - // ResourceTree - GetGameObjectResourcePaths = Ipc.GetGameObjectResourcePaths.Provider(pi, Api.GetGameObjectResourcePaths); - GetPlayerResourcePaths = Ipc.GetPlayerResourcePaths.Provider(pi, Api.GetPlayerResourcePaths); - GetGameObjectResourcesOfType = Ipc.GetGameObjectResourcesOfType.Provider(pi, Api.GetGameObjectResourcesOfType); - GetPlayerResourcesOfType = Ipc.GetPlayerResourcesOfType.Provider(pi, Api.GetPlayerResourcesOfType); - GetGameObjectResourceTrees = Ipc.GetGameObjectResourceTrees.Provider(pi, Api.GetGameObjectResourceTrees); - GetPlayerResourceTrees = Ipc.GetPlayerResourceTrees.Provider(pi, Api.GetPlayerResourceTrees); - - Initialized.Invoke(); - } - - public void Dispose() - { - // Plugin State - Initialized.Dispose(); - ApiVersion.Dispose(); - ApiVersions.Dispose(); - GetEnabledState.Dispose(); - EnabledChange.Dispose(); - - // Configuration - GetModDirectory.Dispose(); - GetConfiguration.Dispose(); - ModDirectoryChanged.Dispose(); - - // UI - PreSettingsTabBarDraw.Dispose(); - PreSettingsDraw.Dispose(); - PostEnabledDraw.Dispose(); - PostSettingsDraw.Dispose(); - ChangedItemTooltip.Dispose(); - ChangedItemClick.Dispose(); - OpenMainWindow.Dispose(); - CloseMainWindow.Dispose(); - - // Redrawing - RedrawAll.Dispose(); - RedrawObject.Dispose(); - RedrawObjectByIndex.Dispose(); - RedrawObjectByName.Dispose(); - GameObjectRedrawn.Dispose(); - - // Game State - GetDrawObjectInfo.Dispose(); - GetCutsceneParentIndex.Dispose(); - SetCutsceneParentIndex.Dispose(); - CreatingCharacterBase.Dispose(); - CreatedCharacterBase.Dispose(); - GameObjectResourcePathResolved.Dispose(); - - // Resolve - ResolveDefaultPath.Dispose(); - ResolveInterfacePath.Dispose(); - ResolvePlayerPath.Dispose(); - ResolveGameObjectPath.Dispose(); - ResolveCharacterPath.Dispose(); - ReverseResolvePath.Dispose(); - ReverseResolveGameObjectPath.Dispose(); - ReverseResolvePlayerPath.Dispose(); - ResolvePlayerPaths.Dispose(); - ResolvePlayerPathsAsync.Dispose(); - - // Collections - GetCollections.Dispose(); - GetCurrentCollectionName.Dispose(); - GetDefaultCollectionName.Dispose(); - GetInterfaceCollectionName.Dispose(); - GetCharacterCollectionName.Dispose(); - GetCollectionForType.Dispose(); - SetCollectionForType.Dispose(); - GetCollectionForObject.Dispose(); - SetCollectionForObject.Dispose(); - GetChangedItems.Dispose(); - - // Meta - GetPlayerMetaManipulations.Dispose(); - GetMetaManipulations.Dispose(); - GetGameObjectMetaManipulations.Dispose(); - - // Mods - GetMods.Dispose(); - ReloadMod.Dispose(); - InstallMod.Dispose(); - AddMod.Dispose(); - DeleteMod.Dispose(); - GetModPath.Dispose(); - SetModPath.Dispose(); - ModDeleted.Dispose(); - ModAdded.Dispose(); - ModMoved.Dispose(); - - // ModSettings - GetAvailableModSettings.Dispose(); - GetCurrentModSettings.Dispose(); - TryInheritMod.Dispose(); - TrySetMod.Dispose(); - TrySetModPriority.Dispose(); - TrySetModSetting.Dispose(); - TrySetModSettings.Dispose(); - ModSettingChanged.Dispose(); - CopyModSettings.Dispose(); - - // Temporary - CreateTemporaryCollection.Dispose(); - RemoveTemporaryCollection.Dispose(); - CreateNamedTemporaryCollection.Dispose(); - RemoveTemporaryCollectionByName.Dispose(); - AssignTemporaryCollection.Dispose(); - AddTemporaryModAll.Dispose(); - AddTemporaryMod.Dispose(); - RemoveTemporaryModAll.Dispose(); - RemoveTemporaryMod.Dispose(); - - // Editing - ConvertTextureFile.Dispose(); - ConvertTextureData.Dispose(); - - // Resource Tree - GetGameObjectResourcePaths.Dispose(); - GetPlayerResourcePaths.Dispose(); - GetGameObjectResourcesOfType.Dispose(); - GetPlayerResourcesOfType.Dispose(); - GetGameObjectResourceTrees.Dispose(); - GetPlayerResourceTrees.Dispose(); - - Disposed.Invoke(); - Disposed.Dispose(); - } - - // Wrappers - private int DeprecatedVersion() - { - Penumbra.Log.Warning($"{Ipc.ApiVersion.Label} is outdated. Please use {Ipc.ApiVersions.Label} instead."); - return Api.ApiVersion.Breaking; - } - - private void OnClick(MouseButton click, object? item) - { - var (type, id) = ChangedItemExtensions.ChangedItemToTypeAndId(item); - ChangedItemClick.Invoke(click, type, id); - } - - private void OnTooltip(object? item) - { - var (type, id) = ChangedItemExtensions.ChangedItemToTypeAndId(item); - ChangedItemTooltip.Invoke(type, id); - } - - private void EnabledChangeEvent(bool value) - => EnabledChange.Invoke(value); - - private void OnGameObjectRedrawn(IntPtr objectAddress, int objectTableIndex) - => GameObjectRedrawn.Invoke(objectAddress, objectTableIndex); - - private void CreatingCharacterBaseEvent(IntPtr gameObject, string collectionName, IntPtr modelId, IntPtr customize, IntPtr equipData) - => CreatingCharacterBase.Invoke(gameObject, collectionName, modelId, customize, equipData); - - private void CreatedCharacterBaseEvent(IntPtr gameObject, string collectionName, IntPtr drawObject) - => CreatedCharacterBase.Invoke(gameObject, collectionName, drawObject); - - private void GameObjectResourceResolvedEvent(IntPtr gameObject, string gamePath, string localPath) - => GameObjectResourcePathResolved.Invoke(gameObject, gamePath, localPath); - - private void ModSettingChangedEvent(ModSettingChange type, string collection, string mod, bool inherited) - => ModSettingChanged.Invoke(type, collection, mod, inherited); - - private void ModDeletedEvent(string name) - => ModDeleted.Invoke(name); - - private void ModAddedEvent(string name) - => ModAdded.Invoke(name); - - private void ModMovedEvent(string from, string to) - => ModMoved.Invoke(from, to); -} diff --git a/Penumbra/Collections/Cache/CollectionCache.cs b/Penumbra/Collections/Cache/CollectionCache.cs index 72f0fb59..e1b32204 100644 --- a/Penumbra/Collections/Cache/CollectionCache.cs +++ b/Penumbra/Collections/Cache/CollectionCache.cs @@ -243,14 +243,14 @@ public sealed class CollectionCache : IDisposable continue; var config = settings.Settings[groupIndex]; - switch (group.Type) + switch (group) { - case GroupType.Single: - AddSubMod(group[config.AsIndex], mod); + case SingleModGroup single: + AddSubMod(single[config.AsIndex], mod); break; - case GroupType.Multi: + case MultiModGroup multi: { - foreach (var (option, _) in group.WithIndex() + foreach (var (option, _) in multi.WithIndex() .Where(p => config.HasFlag(p.Index)) .OrderByDescending(p => group.OptionPriority(p.Index))) AddSubMod(option, mod); diff --git a/Penumbra/Collections/Cache/CollectionCacheManager.cs b/Penumbra/Collections/Cache/CollectionCacheManager.cs index f6c6e14a..4b5c4337 100644 --- a/Penumbra/Collections/Cache/CollectionCacheManager.cs +++ b/Penumbra/Collections/Cache/CollectionCacheManager.cs @@ -119,7 +119,7 @@ public class CollectionCacheManager : IDisposable /// Does not create caches. /// public void CalculateEffectiveFileList(ModCollection collection) - => _framework.RegisterImportant(nameof(CalculateEffectiveFileList) + collection.Name, + => _framework.RegisterImportant(nameof(CalculateEffectiveFileList) + collection.Identifier, () => CalculateEffectiveFileListInternal(collection)); private void CalculateEffectiveFileListInternal(ModCollection collection) diff --git a/Penumbra/Collections/Cache/ImcCache.cs b/Penumbra/Collections/Cache/ImcCache.cs index 3b865d4b..bc928360 100644 --- a/Penumbra/Collections/Cache/ImcCache.cs +++ b/Penumbra/Collections/Cache/ImcCache.cs @@ -116,7 +116,7 @@ public readonly struct ImcCache : IDisposable } private static FullPath CreateImcPath(ModCollection collection, Utf8GamePath path) - => new($"|{collection.Name}_{collection.ChangeCounter}|{path}"); + => new($"|{collection.Id.OptimizedString()}_{collection.ChangeCounter}|{path}"); public bool GetImcFile(Utf8GamePath path, [NotNullWhen(true)] out ImcFile? file) => _imcFiles.TryGetValue(path, out file); diff --git a/Penumbra/Collections/Manager/ActiveCollections.cs b/Penumbra/Collections/Manager/ActiveCollections.cs index 38679612..4e8ebe36 100644 --- a/Penumbra/Collections/Manager/ActiveCollections.cs +++ b/Penumbra/Collections/Manager/ActiveCollections.cs @@ -22,7 +22,7 @@ public class ActiveCollectionData public class ActiveCollections : ISavable, IDisposable { - public const int Version = 1; + public const int Version = 2; private readonly CollectionStorage _storage; private readonly CommunicatorService _communicator; @@ -261,16 +261,17 @@ public class ActiveCollections : ISavable, IDisposable var jObj = new JObject { { nameof(Version), Version }, - { nameof(Default), Default.Name }, - { nameof(Interface), Interface.Name }, - { nameof(Current), Current.Name }, + { nameof(Default), Default.Id }, + { nameof(Interface), Interface.Id }, + { nameof(Current), Current.Id }, }; foreach (var (type, collection) in SpecialCollections.WithIndex().Where(p => p.Value != null) .Select(p => ((CollectionType)p.Index, p.Value!))) - jObj.Add(type.ToString(), collection.Name); + jObj.Add(type.ToString(), collection.Id); jObj.Add(nameof(Individuals), Individuals.ToJObject()); - using var j = new JsonTextWriter(writer) { Formatting = Formatting.Indented }; + using var j = new JsonTextWriter(writer); + j.Formatting = Formatting.Indented; jObj.WriteTo(j); } @@ -319,22 +320,16 @@ public class ActiveCollections : ISavable, IDisposable } } - /// - /// Load default, current, special, and character collections from config. - /// If a collection does not exist anymore, reset it to an appropriate default. - /// - private void LoadCollections() + private bool LoadCollectionsV1(JObject jObject) { - Penumbra.Log.Debug("[Collections] Reading collection assignments..."); - var configChanged = !Load(_saveService.FileNames, out var jObject); - - // Load the default collection. If the string does not exist take the Default name if no file existed or the Empty name if one existed. - var defaultName = jObject[nameof(Default)]?.ToObject() - ?? (configChanged ? ModCollection.DefaultCollectionName : ModCollection.Empty.Name); + var configChanged = false; + // Load the default collection. If the name does not exist take the empty collection. + var defaultName = jObject[nameof(Default)]?.ToObject() ?? ModCollection.Empty.Name; if (!_storage.ByName(defaultName, out var defaultCollection)) { Penumbra.Messager.NotificationMessage( - $"Last choice of {TutorialService.DefaultCollection} {defaultName} is not available, reset to {ModCollection.Empty.Name}.", NotificationType.Warning); + $"Last choice of {TutorialService.DefaultCollection} {defaultName} is not available, reset to {ModCollection.Empty.Name}.", + NotificationType.Warning); Default = ModCollection.Empty; configChanged = true; } @@ -348,7 +343,8 @@ public class ActiveCollections : ISavable, IDisposable if (!_storage.ByName(interfaceName, out var interfaceCollection)) { Penumbra.Messager.NotificationMessage( - $"Last choice of {TutorialService.InterfaceCollection} {interfaceName} is not available, reset to {ModCollection.Empty.Name}.", NotificationType.Warning); + $"Last choice of {TutorialService.InterfaceCollection} {interfaceName} is not available, reset to {ModCollection.Empty.Name}.", + NotificationType.Warning); Interface = ModCollection.Empty; configChanged = true; } @@ -362,7 +358,8 @@ public class ActiveCollections : ISavable, IDisposable if (!_storage.ByName(currentName, out var currentCollection)) { Penumbra.Messager.NotificationMessage( - $"Last choice of {TutorialService.SelectedCollection} {currentName} is not available, reset to {ModCollection.DefaultCollectionName}.", NotificationType.Warning); + $"Last choice of {TutorialService.SelectedCollection} {currentName} is not available, reset to {ModCollection.DefaultCollectionName}.", + NotificationType.Warning); Current = _storage.DefaultNamed; configChanged = true; } @@ -393,11 +390,124 @@ public class ActiveCollections : ISavable, IDisposable Penumbra.Log.Debug("[Collections] Loaded non-individual collection assignments."); configChanged |= ActiveCollectionMigration.MigrateIndividualCollections(_storage, Individuals, jObject); - configChanged |= Individuals.ReadJObject(_saveService, this, jObject[nameof(Individuals)] as JArray, _storage); + configChanged |= Individuals.ReadJObject(_saveService, this, jObject[nameof(Individuals)] as JArray, _storage, 1); - // Save any changes. - if (configChanged) - _saveService.ImmediateSave(this); + return configChanged; + } + + private bool LoadCollectionsV2(JObject jObject) + { + var configChanged = false; + // Load the default collection. If the guid does not exist take the empty collection. + var defaultId = jObject[nameof(Default)]?.ToObject() ?? Guid.Empty; + if (!_storage.ById(defaultId, out var defaultCollection)) + { + Penumbra.Messager.NotificationMessage( + $"Last choice of {TutorialService.DefaultCollection} {defaultId} is not available, reset to {ModCollection.Empty.Name}.", + NotificationType.Warning); + Default = ModCollection.Empty; + configChanged = true; + } + else + { + Default = defaultCollection; + } + + // Load the interface collection. If no string is set, use the name of whatever was set as Default. + var interfaceId = jObject[nameof(Interface)]?.ToObject() ?? Default.Id; + if (!_storage.ById(interfaceId, out var interfaceCollection)) + { + Penumbra.Messager.NotificationMessage( + $"Last choice of {TutorialService.InterfaceCollection} {interfaceId} is not available, reset to {ModCollection.Empty.Name}.", + NotificationType.Warning); + Interface = ModCollection.Empty; + configChanged = true; + } + else + { + Interface = interfaceCollection; + } + + // Load the current collection. + var currentId = jObject[nameof(Current)]?.ToObject() ?? _storage.DefaultNamed.Id; + if (!_storage.ById(currentId, out var currentCollection)) + { + Penumbra.Messager.NotificationMessage( + $"Last choice of {TutorialService.SelectedCollection} {currentId} is not available, reset to {ModCollection.DefaultCollectionName}.", + NotificationType.Warning); + Current = _storage.DefaultNamed; + configChanged = true; + } + else + { + Current = currentCollection; + } + + // Load special collections. + foreach (var (type, name, _) in CollectionTypeExtensions.Special) + { + var typeId = jObject[type.ToString()]?.ToObject(); + if (typeId == null) + continue; + + if (!_storage.ById(typeId.Value, out var typeCollection)) + { + Penumbra.Messager.NotificationMessage($"Last choice of {name} Collection {typeId.Value} is not available, removed.", + NotificationType.Warning); + configChanged = true; + } + else + { + SpecialCollections[(int)type] = typeCollection; + } + } + + Penumbra.Log.Debug("[Collections] Loaded non-individual collection assignments."); + + configChanged |= ActiveCollectionMigration.MigrateIndividualCollections(_storage, Individuals, jObject); + configChanged |= Individuals.ReadJObject(_saveService, this, jObject[nameof(Individuals)] as JArray, _storage, 2); + + return configChanged; + } + + private bool LoadCollectionsNew() + { + Current = _storage.DefaultNamed; + Default = _storage.DefaultNamed; + Interface = _storage.DefaultNamed; + return true; + } + + /// + /// Load default, current, special, and character collections from config. + /// If a collection does not exist anymore, reset it to an appropriate default. + /// + private void LoadCollections() + { + Penumbra.Log.Debug("[Collections] Reading collection assignments..."); + var configChanged = !Load(_saveService.FileNames, out var jObject); + var version = jObject["Version"]?.ToObject() ?? 0; + var changed = false; + switch (version) + { + case 1: + changed = LoadCollectionsV1(jObject); + break; + case 2: + changed = LoadCollectionsV2(jObject); + break; + case 0 when configChanged: + changed = LoadCollectionsNew(); + break; + case 0: + Penumbra.Messager.NotificationMessage("Active Collections File has unknown version and will be reset.", + NotificationType.Warning); + changed = LoadCollectionsNew(); + break; + } + + if (changed) + _saveService.ImmediateSaveSync(this); } /// @@ -410,7 +520,7 @@ public class ActiveCollections : ISavable, IDisposable var jObj = BackupService.GetJObjectForFile(fileNames, file); if (jObj == null) { - ret = new JObject(); + ret = []; return false; } diff --git a/Penumbra/Collections/Manager/CollectionStorage.cs b/Penumbra/Collections/Manager/CollectionStorage.cs index d0b61e57..2da2a569 100644 --- a/Penumbra/Collections/Manager/CollectionStorage.cs +++ b/Penumbra/Collections/Manager/CollectionStorage.cs @@ -1,7 +1,6 @@ using Dalamud.Interface.Internal.Notifications; using OtterGui; using OtterGui.Classes; -using OtterGui.Filesystem; using Penumbra.Communication; using Penumbra.Mods; using Penumbra.Mods.Editor; @@ -48,6 +47,25 @@ public class CollectionStorage : IReadOnlyList, IDisposable return true; } + /// Find a collection by its id. If the GUID is empty, the empty collection is returned. + public bool ById(Guid id, [NotNullWhen(true)] out ModCollection? collection) + { + if (id != Guid.Empty) + return _collections.FindFirst(c => c.Id == id, out collection); + + collection = ModCollection.Empty; + return true; + } + + /// Find a collection by an identifier, which is interpreted as a GUID first and if it does not correspond to one, as a name. + public bool ByIdentifier(string identifier, [NotNullWhen(true)] out ModCollection? collection) + { + if (Guid.TryParse(identifier, out var guid)) + return ById(guid, out collection); + + return ByName(identifier, out collection); + } + public CollectionStorage(CommunicatorService communicator, SaveService saveService, ModStorage modStorage) { _communicator = communicator; @@ -70,31 +88,6 @@ public class CollectionStorage : IReadOnlyList, IDisposable _communicator.ModFileChanged.Unsubscribe(OnModFileChanged); } - /// - /// Returns true if the name is not empty, it is not the name of the empty collection - /// and no existing collection results in the same filename as name. Also returns the fixed name. - /// - public bool CanAddCollection(string name, out string fixedName) - { - if (!IsValidName(name)) - { - fixedName = string.Empty; - return false; - } - - name = name.ToLowerInvariant(); - if (name.Length == 0 - || name == ModCollection.Empty.Name.ToLowerInvariant() - || _collections.Any(c => c.Name.ToLowerInvariant() == name)) - { - fixedName = string.Empty; - return false; - } - - fixedName = name; - return true; - } - /// /// Add a new collection of the given name. /// If duplicate is not-null, the new collection will be a duplicate of it. @@ -104,14 +97,6 @@ public class CollectionStorage : IReadOnlyList, IDisposable /// public bool AddCollection(string name, ModCollection? duplicate) { - if (!CanAddCollection(name, out var fixedName)) - { - Penumbra.Messager.NotificationMessage( - $"The new collection {name} would lead to the same path {fixedName} as one that already exists.", NotificationType.Warning, - false); - return false; - } - var newCollection = duplicate?.Duplicate(name, _collections.Count) ?? ModCollection.CreateEmpty(name, _collections.Count, _modStorage.Count); _collections.Add(newCollection); @@ -166,16 +151,9 @@ public class CollectionStorage : IReadOnlyList, IDisposable _saveService.QueueSave(new ModCollectionSave(_modStorage, collection)); } - /// - /// Check if a name is valid to use for a collection. - /// Does not check for uniqueness. - /// - private static bool IsValidName(string name) - => name.Length is > 0 and < 64 && name.All(c => !c.IsInvalidAscii() && c is not '|' && !c.IsInvalidInPath()); - /// /// Read all collection files in the Collection Directory. - /// Ensure that the default named collection exists, and apply inheritances afterwards. + /// Ensure that the default named collection exists, and apply inheritances afterward. /// Duplicate collection files are not deleted, just not added here. /// private void ReadCollections(out ModCollection defaultNamedCollection) @@ -183,29 +161,46 @@ public class CollectionStorage : IReadOnlyList, IDisposable Penumbra.Log.Debug("[Collections] Reading saved collections..."); foreach (var file in _saveService.FileNames.CollectionFiles) { - if (!ModCollectionSave.LoadFromFile(file, out var name, out var version, out var settings, out var inheritance)) + if (!ModCollectionSave.LoadFromFile(file, out var id, out var name, out var version, out var settings, out var inheritance)) continue; - if (!IsValidName(name)) + if (id == Guid.Empty) { - // TODO: handle better. - Penumbra.Messager.NotificationMessage($"Collection of unsupported name found: {name} is not a valid collection name.", + Penumbra.Messager.NotificationMessage("Collection without ID found.", NotificationType.Warning); + continue; + } + + if (ById(id, out _)) + { + Penumbra.Messager.NotificationMessage($"Duplicate collection found: {id} already exists. Import skipped.", NotificationType.Warning); continue; } - if (ByName(name, out _)) - { - Penumbra.Messager.NotificationMessage($"Duplicate collection found: {name} already exists. Import skipped.", - NotificationType.Warning); - continue; - } - - var collection = ModCollection.CreateFromData(_saveService, _modStorage, name, version, Count, settings, inheritance); + var collection = ModCollection.CreateFromData(_saveService, _modStorage, id, name, version, Count, settings, inheritance); var correctName = _saveService.FileNames.CollectionFile(collection); if (file.FullName != correctName) - Penumbra.Messager.NotificationMessage($"Collection {file.Name} does not correspond to {collection.Name}.", - NotificationType.Warning); + try + { + if (version >= 2) + { + File.Move(file.FullName, correctName, false); + Penumbra.Messager.NotificationMessage($"Collection {file.Name} does not correspond to {collection.Identifier}, renamed.", + NotificationType.Warning); + } + else + { + _saveService.ImmediateSaveSync(new ModCollectionSave(_modStorage, collection)); + File.Delete(file.FullName); + Penumbra.Log.Information($"Migrated collection {name} to Guid {id}."); + } + } + catch (Exception e) + { + Penumbra.Messager.NotificationMessage(e, + $"Collection {file.Name} does not correspond to {collection.Identifier}, but could not rename.", NotificationType.Error); + } + _collections.Add(collection); } diff --git a/Penumbra/Collections/Manager/IndividualCollections.Files.cs b/Penumbra/Collections/Manager/IndividualCollections.Files.cs index 21a8cf8a..8a717b35 100644 --- a/Penumbra/Collections/Manager/IndividualCollections.Files.cs +++ b/Penumbra/Collections/Manager/IndividualCollections.Files.cs @@ -18,7 +18,7 @@ public partial class IndividualCollections foreach (var (name, identifiers, collection) in Assignments) { var tmp = identifiers[0].ToJson(); - tmp.Add("Collection", collection.Name); + tmp.Add("Collection", collection.Id); tmp.Add("Display", name); ret.Add(tmp); } @@ -26,18 +26,28 @@ public partial class IndividualCollections return ret; } - public bool ReadJObject(SaveService saver, ActiveCollections parent, JArray? obj, CollectionStorage storage) + public bool ReadJObject(SaveService saver, ActiveCollections parent, JArray? obj, CollectionStorage storage, int version) { if (_actors.Awaiter.IsCompletedSuccessfully) { - var ret = ReadJObjectInternal(obj, storage); + var ret = version switch + { + 1 => ReadJObjectInternalV1(obj, storage), + 2 => ReadJObjectInternalV2(obj, storage), + _ => true, + }; return ret; } Penumbra.Log.Debug("[Collections] Delayed reading individual assignments until actor service is ready..."); _actors.Awaiter.ContinueWith(_ => { - if (ReadJObjectInternal(obj, storage)) + if (version switch + { + 1 => ReadJObjectInternalV1(obj, storage), + 2 => ReadJObjectInternalV2(obj, storage), + _ => true, + }) saver.ImmediateSave(parent); IsLoaded = true; Loaded.Invoke(); @@ -45,7 +55,55 @@ public partial class IndividualCollections return false; } - private bool ReadJObjectInternal(JArray? obj, CollectionStorage storage) + private bool ReadJObjectInternalV1(JArray? obj, CollectionStorage storage) + { + Penumbra.Log.Debug("[Collections] Reading individual assignments..."); + if (obj == null) + { + Penumbra.Log.Debug($"[Collections] Finished reading {Count} individual assignments..."); + return true; + } + + foreach (var data in obj) + { + try + { + var identifier = _actors.FromJson(data as JObject); + var group = GetGroup(identifier); + if (group.Length == 0 || group.Any(i => !i.IsValid)) + { + Penumbra.Messager.NotificationMessage("Could not load an unknown individual collection, removed.", + NotificationType.Error); + continue; + } + + var collectionName = data["Collection"]?.ToObject() ?? string.Empty; + if (collectionName.Length == 0 || !storage.ByName(collectionName, out var collection)) + { + Penumbra.Messager.NotificationMessage( + $"Could not load the collection \"{collectionName}\" as individual collection for {identifier}, set to None.", + NotificationType.Warning); + continue; + } + + if (!Add(group, collection)) + { + Penumbra.Messager.NotificationMessage($"Could not add an individual collection for {identifier}, removed.", + NotificationType.Warning); + } + } + catch (Exception e) + { + Penumbra.Messager.NotificationMessage(e, $"Could not load an unknown individual collection, removed.", NotificationType.Error); + } + } + + Penumbra.Log.Debug($"Finished reading {Count} individual assignments..."); + + return true; + } + + private bool ReadJObjectInternalV2(JArray? obj, CollectionStorage storage) { Penumbra.Log.Debug("[Collections] Reading individual assignments..."); if (obj == null) @@ -64,17 +122,17 @@ public partial class IndividualCollections if (group.Length == 0 || group.Any(i => !i.IsValid)) { changes = true; - Penumbra.Messager.NotificationMessage("Could not load an unknown individual collection, removed.", + Penumbra.Messager.NotificationMessage("Could not load an unknown individual collection, removed assignment.", NotificationType.Error); continue; } - var collectionName = data["Collection"]?.ToObject() ?? string.Empty; - if (collectionName.Length == 0 || !storage.ByName(collectionName, out var collection)) + var collectionId = data["Collection"]?.ToObject(); + if (!collectionId.HasValue || !storage.ById(collectionId.Value, out var collection)) { changes = true; Penumbra.Messager.NotificationMessage( - $"Could not load the collection \"{collectionName}\" as individual collection for {identifier}, set to None.", + $"Could not load the collection {collectionId} as individual collection for {identifier}, removed assignment.", NotificationType.Warning); continue; } @@ -82,14 +140,14 @@ public partial class IndividualCollections if (!Add(group, collection)) { changes = true; - Penumbra.Messager.NotificationMessage($"Could not add an individual collection for {identifier}, removed.", + Penumbra.Messager.NotificationMessage($"Could not add an individual collection for {identifier}, removed assignment.", NotificationType.Warning); } } catch (Exception e) { changes = true; - Penumbra.Messager.NotificationMessage(e, $"Could not load an unknown individual collection, removed.", NotificationType.Error); + Penumbra.Messager.NotificationMessage(e, $"Could not load an unknown individual collection, removed assignment.", NotificationType.Error); } } @@ -100,14 +158,6 @@ public partial class IndividualCollections internal void Migrate0To1(Dictionary old) { - static bool FindDataId(string name, NameDictionary data, out NpcId dataId) - { - var kvp = data.FirstOrDefault(kvp => kvp.Value.Equals(name, StringComparison.OrdinalIgnoreCase), - new KeyValuePair(uint.MaxValue, string.Empty)); - dataId = kvp.Key; - return kvp.Value.Length > 0; - } - foreach (var (name, collection) in old) { var kind = ObjectKind.None; @@ -155,5 +205,15 @@ public partial class IndividualCollections NotificationType.Error); } } + + return; + + static bool FindDataId(string name, NameDictionary data, out NpcId dataId) + { + var kvp = data.FirstOrDefault(kvp => kvp.Value.Equals(name, StringComparison.OrdinalIgnoreCase), + new KeyValuePair(uint.MaxValue, string.Empty)); + dataId = kvp.Key; + return kvp.Value.Length > 0; + } } } diff --git a/Penumbra/Collections/Manager/InheritanceManager.cs b/Penumbra/Collections/Manager/InheritanceManager.cs index 771f9463..6003b5f9 100644 --- a/Penumbra/Collections/Manager/InheritanceManager.cs +++ b/Penumbra/Collections/Manager/InheritanceManager.cs @@ -138,7 +138,7 @@ public class InheritanceManager : IDisposable var changes = false; foreach (var subCollectionName in collection.InheritanceByName) { - if (_storage.ByName(subCollectionName, out var subCollection)) + if (Guid.TryParse(subCollectionName, out var guid) && _storage.ById(guid, out var subCollection)) { if (AddInheritance(collection, subCollection, false)) continue; @@ -146,6 +146,15 @@ public class InheritanceManager : IDisposable changes = true; Penumbra.Messager.NotificationMessage($"{collection.Name} can not inherit from {subCollection.Name}, removed.", NotificationType.Warning); } + else if (_storage.ByName(subCollectionName, out subCollection)) + { + changes = true; + Penumbra.Log.Information($"Migrating inheritance for {collection.AnonymizedName} from name to GUID."); + if (AddInheritance(collection, subCollection, false)) + continue; + + Penumbra.Messager.NotificationMessage($"{collection.Name} can not inherit from {subCollection.Name}, removed.", NotificationType.Warning); + } else { Penumbra.Messager.NotificationMessage( diff --git a/Penumbra/Collections/Manager/TempCollectionManager.cs b/Penumbra/Collections/Manager/TempCollectionManager.cs index 5d9de13d..de08c6a2 100644 --- a/Penumbra/Collections/Manager/TempCollectionManager.cs +++ b/Penumbra/Collections/Manager/TempCollectionManager.cs @@ -1,3 +1,4 @@ +using OtterGui; using Penumbra.Api; using Penumbra.Communication; using Penumbra.GameData.Actors; @@ -9,13 +10,13 @@ namespace Penumbra.Collections.Manager; public class TempCollectionManager : IDisposable { - public int GlobalChangeCounter { get; private set; } = 0; + public int GlobalChangeCounter { get; private set; } public readonly IndividualCollections Collections; - private readonly CommunicatorService _communicator; - private readonly CollectionStorage _storage; - private readonly ActorManager _actors; - private readonly Dictionary _customCollections = new(); + private readonly CommunicatorService _communicator; + private readonly CollectionStorage _storage; + private readonly ActorManager _actors; + private readonly Dictionary _customCollections = []; public TempCollectionManager(Configuration config, CommunicatorService communicator, ActorManager actors, CollectionStorage storage) { @@ -42,36 +43,36 @@ public class TempCollectionManager : IDisposable => _customCollections.Values; public bool CollectionByName(string name, [NotNullWhen(true)] out ModCollection? collection) - => _customCollections.TryGetValue(name.ToLowerInvariant(), out collection); + => _customCollections.Values.FindFirst(c => string.Equals(name, c.Name, StringComparison.OrdinalIgnoreCase), out collection); - public string CreateTemporaryCollection(string name) + public bool CollectionById(Guid id, [NotNullWhen(true)] out ModCollection? collection) + => _customCollections.TryGetValue(id, out collection); + + public Guid CreateTemporaryCollection(string name) { - if (_storage.ByName(name, out _)) - return string.Empty; - if (GlobalChangeCounter == int.MaxValue) GlobalChangeCounter = 0; var collection = ModCollection.CreateTemporary(name, ~Count, GlobalChangeCounter++); - Penumbra.Log.Debug($"Creating temporary collection {collection.AnonymizedName}."); - if (_customCollections.TryAdd(collection.Name.ToLowerInvariant(), collection)) + Penumbra.Log.Debug($"Creating temporary collection {collection.Name} with {collection.Id}."); + if (_customCollections.TryAdd(collection.Id, collection)) { // Temporary collection created. _communicator.CollectionChange.Invoke(CollectionType.Temporary, null, collection, string.Empty); - return collection.Name; + return collection.Id; } - return string.Empty; + return Guid.Empty; } - public bool RemoveTemporaryCollection(string collectionName) + public bool RemoveTemporaryCollection(Guid collectionId) { - if (!_customCollections.Remove(collectionName.ToLowerInvariant(), out var collection)) + if (!_customCollections.Remove(collectionId, out var collection)) { - Penumbra.Log.Debug($"Tried to delete temporary collection {collectionName.ToLowerInvariant()}, but did not exist."); + Penumbra.Log.Debug($"Tried to delete temporary collection {collectionId}, but did not exist."); return false; } - Penumbra.Log.Debug($"Deleted temporary collection {collection.AnonymizedName}."); + Penumbra.Log.Debug($"Deleted temporary collection {collection.Id}."); GlobalChangeCounter += Math.Max(collection.ChangeCounter + 1 - GlobalChangeCounter, 0); for (var i = 0; i < Collections.Count; ++i) { @@ -80,7 +81,7 @@ public class TempCollectionManager : IDisposable // Temporary collection assignment removed. _communicator.CollectionChange.Invoke(CollectionType.Temporary, collection, null, Collections[i].DisplayName); - Penumbra.Log.Verbose($"Unassigned temporary collection {collection.AnonymizedName} from {Collections[i].DisplayName}."); + Penumbra.Log.Verbose($"Unassigned temporary collection {collection.Id} from {Collections[i].DisplayName}."); Collections.Delete(i--); } @@ -98,32 +99,32 @@ public class TempCollectionManager : IDisposable return true; } - public bool AddIdentifier(string collectionName, params ActorIdentifier[] identifiers) + public bool AddIdentifier(Guid collectionId, params ActorIdentifier[] identifiers) { - if (!_customCollections.TryGetValue(collectionName.ToLowerInvariant(), out var collection)) + if (!_customCollections.TryGetValue(collectionId, out var collection)) return false; return AddIdentifier(collection, identifiers); } - public bool AddIdentifier(string collectionName, string characterName, ushort worldId = ushort.MaxValue) + public bool AddIdentifier(Guid collectionId, string characterName, ushort worldId = ushort.MaxValue) { - if (!ByteString.FromString(characterName, out var byteString, false)) + if (!ByteString.FromString(characterName, out var byteString)) return false; var identifier = _actors.CreatePlayer(byteString, worldId); if (!identifier.IsValid) return false; - return AddIdentifier(collectionName, identifier); + return AddIdentifier(collectionId, identifier); } internal bool RemoveByCharacterName(string characterName, ushort worldId = ushort.MaxValue) { - if (!ByteString.FromString(characterName, out var byteString, false)) + if (!ByteString.FromString(characterName, out var byteString)) return false; var identifier = _actors.CreatePlayer(byteString, worldId); - return Collections.TryGetValue(identifier, out var collection) && RemoveTemporaryCollection(collection.Name); + return Collections.TryGetValue(identifier, out var collection) && RemoveTemporaryCollection(collection.Id); } } diff --git a/Penumbra/Collections/ModCollection.cs b/Penumbra/Collections/ModCollection.cs index b63be6cd..c1143c71 100644 --- a/Penumbra/Collections/ModCollection.cs +++ b/Penumbra/Collections/ModCollection.cs @@ -17,7 +17,7 @@ namespace Penumbra.Collections; /// public partial class ModCollection { - public const int CurrentVersion = 1; + public const int CurrentVersion = 2; public const string DefaultCollectionName = "Default"; public const string EmptyCollectionName = "None"; @@ -27,15 +27,23 @@ public partial class ModCollection /// public static readonly ModCollection Empty = CreateEmpty(EmptyCollectionName, 0, 0); - /// The name of a collection can not contain characters invalid in a path. - public string Name { get; internal init; } + /// The name of a collection. + public string Name { get; set; } = string.Empty; + + public Guid Id { get; } + + public string Identifier + => Id.ToString(); + + public string ShortIdentifier + => Identifier[..8]; public override string ToString() - => Name; + => Name.Length > 0 ? Name : ShortIdentifier; /// Get the first two letters of a collection name and its Index (or None if it is the empty collection). public string AnonymizedName - => this == Empty ? Empty.Name : Name.Length > 2 ? $"{Name[..2]}... ({Index})" : $"{Name} ({Index})"; + => this == Empty ? Empty.Name : Name == DefaultCollectionName ? Name : ShortIdentifier; /// The index of the collection is set and kept up-to-date by the CollectionManager. public int Index { get; internal set; } @@ -112,16 +120,16 @@ public partial class ModCollection public ModCollection Duplicate(string name, int index) { Debug.Assert(index > 0, "Collection duplicated with non-positive index."); - return new ModCollection(name, index, 0, CurrentVersion, Settings.Select(s => s?.DeepCopy()).ToList(), + return new ModCollection(Guid.NewGuid(), name, index, 0, CurrentVersion, Settings.Select(s => s?.DeepCopy()).ToList(), [.. DirectlyInheritsFrom], UnusedSettings.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.DeepCopy())); } /// Constructor for reading from files. - public static ModCollection CreateFromData(SaveService saver, ModStorage mods, string name, int version, int index, + public static ModCollection CreateFromData(SaveService saver, ModStorage mods, Guid id, string name, int version, int index, Dictionary allSettings, IReadOnlyList inheritances) { Debug.Assert(index > 0, "Collection read with non-positive index."); - var ret = new ModCollection(name, index, 0, version, new List(), new List(), allSettings) + var ret = new ModCollection(id, name, index, 0, version, [], [], allSettings) { InheritanceByName = inheritances, }; @@ -134,8 +142,7 @@ public partial class ModCollection public static ModCollection CreateTemporary(string name, int index, int changeCounter) { Debug.Assert(index < 0, "Temporary collection created with non-negative index."); - var ret = new ModCollection(name, index, changeCounter, CurrentVersion, new List(), new List(), - new Dictionary()); + var ret = new ModCollection(Guid.NewGuid(), name, index, changeCounter, CurrentVersion, [], [], []); return ret; } @@ -143,9 +150,8 @@ public partial class ModCollection public static ModCollection CreateEmpty(string name, int index, int modCount) { Debug.Assert(index >= 0, "Empty collection created with negative index."); - return new ModCollection(name, index, 0, CurrentVersion, Enumerable.Repeat((ModSettings?)null, modCount).ToList(), - new List(), - new Dictionary()); + return new ModCollection(Guid.Empty, name, index, 0, CurrentVersion, Enumerable.Repeat((ModSettings?)null, modCount).ToList(), [], + []); } /// Add settings for a new appended mod, by checking if the mod had settings from a previous deletion. @@ -193,10 +199,11 @@ public partial class ModCollection saver.ImmediateSave(new ModCollectionSave(mods, this)); } - private ModCollection(string name, int index, int changeCounter, int version, List appliedSettings, + private ModCollection(Guid id, string name, int index, int changeCounter, int version, List appliedSettings, List inheritsFrom, Dictionary settings) { Name = name; + Id = id; Index = index; ChangeCounter = changeCounter; Settings = appliedSettings; diff --git a/Penumbra/Collections/ModCollectionSave.cs b/Penumbra/Collections/ModCollectionSave.cs index f2cb4ada..acc38d83 100644 --- a/Penumbra/Collections/ModCollectionSave.cs +++ b/Penumbra/Collections/ModCollectionSave.cs @@ -28,6 +28,8 @@ internal readonly struct ModCollectionSave(ModStorage modStorage, ModCollection j.WriteStartObject(); j.WritePropertyName("Version"); j.WriteValue(ModCollection.CurrentVersion); + j.WritePropertyName(nameof(ModCollection.Id)); + j.WriteValue(modCollection.Identifier); j.WritePropertyName(nameof(ModCollection.Name)); j.WriteValue(modCollection.Name); j.WritePropertyName(nameof(ModCollection.Settings)); @@ -55,20 +57,20 @@ internal readonly struct ModCollectionSave(ModStorage modStorage, ModCollection // Inherit by collection name. j.WritePropertyName("Inheritance"); - x.Serialize(j, modCollection.InheritanceByName ?? modCollection.DirectlyInheritsFrom.Select(c => c.Name)); + x.Serialize(j, modCollection.InheritanceByName ?? modCollection.DirectlyInheritsFrom.Select(c => c.Identifier)); j.WriteEndObject(); } - public static bool LoadFromFile(FileInfo file, out string name, out int version, out Dictionary settings, + public static bool LoadFromFile(FileInfo file, out Guid id, out string name, out int version, out Dictionary settings, out IReadOnlyList inheritance) { - settings = new Dictionary(); - inheritance = Array.Empty(); + settings = []; + inheritance = []; if (!file.Exists) { Penumbra.Log.Error("Could not read collection because file does not exist."); - name = string.Empty; - + name = string.Empty; + id = Guid.Empty; version = 0; return false; } @@ -76,8 +78,9 @@ internal readonly struct ModCollectionSave(ModStorage modStorage, ModCollection try { var obj = JObject.Parse(File.ReadAllText(file.FullName)); - name = obj[nameof(ModCollection.Name)]?.ToObject() ?? string.Empty; version = obj["Version"]?.ToObject() ?? 0; + name = obj[nameof(ModCollection.Name)]?.ToObject() ?? string.Empty; + id = obj[nameof(ModCollection.Id)]?.ToObject() ?? (version == 1 ? Guid.NewGuid() : Guid.Empty); // Custom deserialization that is converted with the constructor. settings = obj[nameof(ModCollection.Settings)]?.ToObject>() ?? settings; inheritance = obj["Inheritance"]?.ToObject>() ?? inheritance; @@ -87,6 +90,7 @@ internal readonly struct ModCollectionSave(ModStorage modStorage, ModCollection { name = string.Empty; version = 0; + id = Guid.Empty; Penumbra.Log.Error($"Could not read collection information from file:\n{e}"); return false; } diff --git a/Penumbra/CommandHandler.cs b/Penumbra/CommandHandler.cs index 537b08da..4e1d6453 100644 --- a/Penumbra/CommandHandler.cs +++ b/Penumbra/CommandHandler.cs @@ -513,7 +513,7 @@ public class CommandHandler : IDisposable collection = string.Equals(lowerName, ModCollection.Empty.Name, StringComparison.OrdinalIgnoreCase) ? ModCollection.Empty - : _collectionManager.Storage.ByName(lowerName, out var c) + : _collectionManager.Storage.ByIdentifier(lowerName, out var c) ? c : null; if (collection != null) diff --git a/Penumbra/Communication/ChangedItemClick.cs b/Penumbra/Communication/ChangedItemClick.cs index 754570e2..554e2221 100644 --- a/Penumbra/Communication/ChangedItemClick.cs +++ b/Penumbra/Communication/ChangedItemClick.cs @@ -1,4 +1,5 @@ using OtterGui.Classes; +using Penumbra.Api.Api; using Penumbra.Api.Enums; namespace Penumbra.Communication; @@ -14,7 +15,7 @@ public sealed class ChangedItemClick() : EventWrapper + /// Default = 0, /// diff --git a/Penumbra/Communication/ChangedItemHover.cs b/Penumbra/Communication/ChangedItemHover.cs index 10607da4..2dcced35 100644 --- a/Penumbra/Communication/ChangedItemHover.cs +++ b/Penumbra/Communication/ChangedItemHover.cs @@ -1,4 +1,5 @@ using OtterGui.Classes; +using Penumbra.Api.Api; namespace Penumbra.Communication; @@ -12,7 +13,7 @@ public sealed class ChangedItemHover() : EventWrapper + /// Default = 0, /// diff --git a/Penumbra/Communication/CreatedCharacterBase.cs b/Penumbra/Communication/CreatedCharacterBase.cs index 397f7bfd..8992f9fc 100644 --- a/Penumbra/Communication/CreatedCharacterBase.cs +++ b/Penumbra/Communication/CreatedCharacterBase.cs @@ -1,5 +1,6 @@ using OtterGui.Classes; using Penumbra.Api; +using Penumbra.Api.Api; using Penumbra.Collections; namespace Penumbra.Communication; diff --git a/Penumbra/Communication/CreatingCharacterBase.cs b/Penumbra/Communication/CreatingCharacterBase.cs index 2f249c14..8a906ca0 100644 --- a/Penumbra/Communication/CreatingCharacterBase.cs +++ b/Penumbra/Communication/CreatingCharacterBase.cs @@ -1,5 +1,6 @@ using OtterGui.Classes; using Penumbra.Api; +using Penumbra.Api.Api; using Penumbra.Services; namespace Penumbra.Communication; @@ -14,7 +15,7 @@ namespace Penumbra.Communication; /// Parameter is a pointer to the equip data array. /// public sealed class CreatingCharacterBase() - : EventWrapper(nameof(CreatingCharacterBase)) + : EventWrapper(nameof(CreatingCharacterBase)) { public enum Priority { diff --git a/Penumbra/Communication/EnabledChanged.cs b/Penumbra/Communication/EnabledChanged.cs index be6343b7..846b1a58 100644 --- a/Penumbra/Communication/EnabledChanged.cs +++ b/Penumbra/Communication/EnabledChanged.cs @@ -1,5 +1,6 @@ using OtterGui.Classes; using Penumbra.Api; +using Penumbra.Api.IpcSubscribers; namespace Penumbra.Communication; @@ -13,7 +14,7 @@ public sealed class EnabledChanged() : EventWrapper + /// Api = int.MinValue, /// diff --git a/Penumbra/Communication/ModDirectoryChanged.cs b/Penumbra/Communication/ModDirectoryChanged.cs index 20d13b20..02293873 100644 --- a/Penumbra/Communication/ModDirectoryChanged.cs +++ b/Penumbra/Communication/ModDirectoryChanged.cs @@ -1,5 +1,6 @@ using OtterGui.Classes; using Penumbra.Api; +using Penumbra.Api.Api; namespace Penumbra.Communication; diff --git a/Penumbra/Communication/ModFileChanged.cs b/Penumbra/Communication/ModFileChanged.cs index 8b4b6f5d..8cda48e9 100644 --- a/Penumbra/Communication/ModFileChanged.cs +++ b/Penumbra/Communication/ModFileChanged.cs @@ -1,5 +1,6 @@ using OtterGui.Classes; using Penumbra.Api; +using Penumbra.Api.Api; using Penumbra.Mods; using Penumbra.Mods.Editor; diff --git a/Penumbra/Communication/ModOptionChanged.cs b/Penumbra/Communication/ModOptionChanged.cs index f02b17dc..0df58b5f 100644 --- a/Penumbra/Communication/ModOptionChanged.cs +++ b/Penumbra/Communication/ModOptionChanged.cs @@ -1,5 +1,6 @@ using OtterGui.Classes; using Penumbra.Api; +using Penumbra.Api.Api; using Penumbra.Mods; using Penumbra.Mods.Manager; diff --git a/Penumbra/Communication/ModPathChanged.cs b/Penumbra/Communication/ModPathChanged.cs index 01c8fa64..1e4f8d36 100644 --- a/Penumbra/Communication/ModPathChanged.cs +++ b/Penumbra/Communication/ModPathChanged.cs @@ -1,5 +1,6 @@ using OtterGui.Classes; using Penumbra.Api; +using Penumbra.Api.Api; using Penumbra.Mods; using Penumbra.Mods.Manager; @@ -19,8 +20,11 @@ public sealed class ModPathChanged() { public enum Priority { - /// - Api = int.MinValue, + /// + ApiMods = int.MinValue, + + /// + ApiModSettings = int.MinValue, /// EphemeralConfig = -500, diff --git a/Penumbra/Communication/ModSettingChanged.cs b/Penumbra/Communication/ModSettingChanged.cs index 412b3003..968f78a7 100644 --- a/Penumbra/Communication/ModSettingChanged.cs +++ b/Penumbra/Communication/ModSettingChanged.cs @@ -1,5 +1,6 @@ using OtterGui.Classes; using Penumbra.Api; +using Penumbra.Api.Api; using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.Mods; diff --git a/Penumbra/Communication/PostEnabledDraw.cs b/Penumbra/Communication/PostEnabledDraw.cs index 68637442..e21f0183 100644 --- a/Penumbra/Communication/PostEnabledDraw.cs +++ b/Penumbra/Communication/PostEnabledDraw.cs @@ -1,4 +1,5 @@ using OtterGui.Classes; +using Penumbra.Api.Api; namespace Penumbra.Communication; @@ -12,7 +13,7 @@ public sealed class PostEnabledDraw() : EventWrapper + /// Default = 0, } } diff --git a/Penumbra/Communication/PostSettingsPanelDraw.cs b/Penumbra/Communication/PostSettingsPanelDraw.cs index a918b610..525ac73e 100644 --- a/Penumbra/Communication/PostSettingsPanelDraw.cs +++ b/Penumbra/Communication/PostSettingsPanelDraw.cs @@ -1,4 +1,5 @@ using OtterGui.Classes; +using Penumbra.Api.Api; namespace Penumbra.Communication; @@ -12,7 +13,7 @@ public sealed class PostSettingsPanelDraw() : EventWrapper + /// Default = 0, } } diff --git a/Penumbra/Communication/PreSettingsPanelDraw.cs b/Penumbra/Communication/PreSettingsPanelDraw.cs index cda00d78..33f6b4e1 100644 --- a/Penumbra/Communication/PreSettingsPanelDraw.cs +++ b/Penumbra/Communication/PreSettingsPanelDraw.cs @@ -1,4 +1,5 @@ using OtterGui.Classes; +using Penumbra.Api.Api; namespace Penumbra.Communication; @@ -12,7 +13,7 @@ public sealed class PreSettingsPanelDraw() : EventWrapper + /// Default = 0, } } diff --git a/Penumbra/Communication/PreSettingsTabBarDraw.cs b/Penumbra/Communication/PreSettingsTabBarDraw.cs index 2c14cdf1..8614bbbe 100644 --- a/Penumbra/Communication/PreSettingsTabBarDraw.cs +++ b/Penumbra/Communication/PreSettingsTabBarDraw.cs @@ -1,4 +1,5 @@ using OtterGui.Classes; +using Penumbra.Api.Api; namespace Penumbra.Communication; @@ -14,7 +15,7 @@ public sealed class PreSettingsTabBarDraw() : EventWrapper + /// Default = 0, } } diff --git a/Penumbra/GuidExtensions.cs b/Penumbra/GuidExtensions.cs new file mode 100644 index 00000000..fcbc8a3b --- /dev/null +++ b/Penumbra/GuidExtensions.cs @@ -0,0 +1,254 @@ +using System.Collections.Frozen; +using OtterGui; + +namespace Penumbra; + +public static class GuidExtensions +{ + private const string Chars = + "0123456789" + + "abcdefghij" + + "klmnopqrst" + + "uv"; + + private static ReadOnlySpan Bytes + => "0123456789abcdefghijklmnopqrstuv"u8; + + private static readonly FrozenDictionary + ReverseChars = Chars.WithIndex().ToFrozenDictionary(t => t.Value, t => (byte)t.Index); + + private static readonly FrozenDictionary ReverseBytes = + ReverseChars.ToFrozenDictionary(kvp => (byte)kvp.Key, kvp => kvp.Value); + + public static unsafe string OptimizedString(this Guid guid) + { + var bytes = stackalloc ulong[2]; + if (!guid.TryWriteBytes(new Span(bytes, 16))) + return guid.ToString("N"); + + var u1 = bytes[0]; + var u2 = bytes[1]; + Span text = + [ + Chars[(int)(u1 & 0x1F)], + Chars[(int)((u1 >> 5) & 0x1F)], + Chars[(int)((u1 >> 10) & 0x1F)], + Chars[(int)((u1 >> 15) & 0x1F)], + Chars[(int)((u1 >> 20) & 0x1F)], + Chars[(int)((u1 >> 25) & 0x1F)], + Chars[(int)((u1 >> 30) & 0x1F)], + Chars[(int)((u1 >> 35) & 0x1F)], + Chars[(int)((u1 >> 40) & 0x1F)], + Chars[(int)((u1 >> 45) & 0x1F)], + Chars[(int)((u1 >> 50) & 0x1F)], + Chars[(int)((u1 >> 55) & 0x1F)], + Chars[(int)((u1 >> 60) | ((u2 & 0x01) << 4))], + Chars[(int)((u2 >> 1) & 0x1F)], + Chars[(int)((u2 >> 6) & 0x1F)], + Chars[(int)((u2 >> 11) & 0x1F)], + Chars[(int)((u2 >> 16) & 0x1F)], + Chars[(int)((u2 >> 21) & 0x1F)], + Chars[(int)((u2 >> 26) & 0x1F)], + Chars[(int)((u2 >> 31) & 0x1F)], + Chars[(int)((u2 >> 36) & 0x1F)], + Chars[(int)((u2 >> 41) & 0x1F)], + Chars[(int)((u2 >> 46) & 0x1F)], + Chars[(int)((u2 >> 51) & 0x1F)], + Chars[(int)((u2 >> 56) & 0x1F)], + Chars[(int)((u2 >> 61) & 0x1F)], + ]; + return new string(text); + } + + public static unsafe bool FromOptimizedString(ReadOnlySpan text, out Guid guid) + { + if (text.Length != 26) + return Return(out guid); + + var bytes = stackalloc ulong[2]; + if (!ReverseChars.TryGetValue(text[0], out var b0)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[1], out var b1)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[2], out var b2)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[3], out var b3)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[4], out var b4)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[5], out var b5)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[6], out var b6)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[7], out var b7)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[8], out var b8)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[9], out var b9)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[10], out var b10)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[11], out var b11)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[12], out var b12)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[13], out var b13)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[14], out var b14)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[15], out var b15)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[16], out var b16)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[17], out var b17)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[18], out var b18)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[19], out var b19)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[20], out var b20)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[21], out var b21)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[22], out var b22)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[23], out var b23)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[24], out var b24)) + return Return(out guid); + if (!ReverseChars.TryGetValue(text[25], out var b25)) + return Return(out guid); + + bytes[0] = b0 + | ((ulong)b1 << 5) + | ((ulong)b2 << 10) + | ((ulong)b3 << 15) + | ((ulong)b4 << 20) + | ((ulong)b5 << 25) + | ((ulong)b6 << 30) + | ((ulong)b7 << 35) + | ((ulong)b8 << 40) + | ((ulong)b9 << 45) + | ((ulong)b10 << 50) + | ((ulong)b11 << 55) + | ((ulong)b12 << 60); + bytes[1] = ((ulong)b12 >> 4) + | ((ulong)b13 << 1) + | ((ulong)b14 << 6) + | ((ulong)b15 << 11) + | ((ulong)b16 << 16) + | ((ulong)b17 << 21) + | ((ulong)b18 << 26) + | ((ulong)b19 << 31) + | ((ulong)b20 << 36) + | ((ulong)b21 << 41) + | ((ulong)b22 << 46) + | ((ulong)b23 << 51) + | ((ulong)b24 << 56) + | ((ulong)b25 << 61); + guid = new Guid(new Span(bytes, 16)); + return true; + + static bool Return(out Guid guid) + { + guid = Guid.Empty; + return false; + } + } + + public static unsafe bool FromOptimizedString(ReadOnlySpan text, out Guid guid) + { + if (text.Length != 26) + return Return(out guid); + + var bytes = stackalloc ulong[2]; + if (!ReverseBytes.TryGetValue(text[0], out var b0)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[1], out var b1)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[2], out var b2)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[3], out var b3)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[4], out var b4)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[5], out var b5)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[6], out var b6)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[7], out var b7)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[8], out var b8)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[9], out var b9)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[10], out var b10)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[11], out var b11)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[12], out var b12)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[13], out var b13)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[14], out var b14)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[15], out var b15)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[16], out var b16)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[17], out var b17)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[18], out var b18)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[19], out var b19)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[20], out var b20)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[21], out var b21)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[22], out var b22)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[23], out var b23)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[24], out var b24)) + return Return(out guid); + if (!ReverseBytes.TryGetValue(text[25], out var b25)) + return Return(out guid); + + bytes[0] = b0 + | ((ulong)b1 << 5) + | ((ulong)b2 << 10) + | ((ulong)b3 << 15) + | ((ulong)b4 << 20) + | ((ulong)b5 << 25) + | ((ulong)b6 << 30) + | ((ulong)b7 << 35) + | ((ulong)b8 << 40) + | ((ulong)b9 << 45) + | ((ulong)b10 << 50) + | ((ulong)b11 << 55) + | ((ulong)b12 << 60); + bytes[1] = ((ulong)b12 >> 4) + | ((ulong)b13 << 1) + | ((ulong)b14 << 6) + | ((ulong)b15 << 11) + | ((ulong)b16 << 16) + | ((ulong)b17 << 21) + | ((ulong)b18 << 26) + | ((ulong)b19 << 31) + | ((ulong)b20 << 36) + | ((ulong)b21 << 41) + | ((ulong)b22 << 46) + | ((ulong)b23 << 51) + | ((ulong)b24 << 56) + | ((ulong)b25 << 61); + guid = new Guid(new Span(bytes, 16)); + return true; + + static bool Return(out Guid guid) + { + guid = Guid.Empty; + return false; + } + } +} diff --git a/Penumbra/Interop/PathResolving/MetaState.cs b/Penumbra/Interop/PathResolving/MetaState.cs index a3400540..5f07ffc5 100644 --- a/Penumbra/Interop/PathResolving/MetaState.cs +++ b/Penumbra/Interop/PathResolving/MetaState.cs @@ -130,7 +130,7 @@ public sealed unsafe class MetaState : IDisposable _lastCreatedCollection = _collectionResolver.IdentifyLastGameObjectCollection(true); if (_lastCreatedCollection.Valid && _lastCreatedCollection.AssociatedGameObject != nint.Zero) _communicator.CreatingCharacterBase.Invoke(_lastCreatedCollection.AssociatedGameObject, - _lastCreatedCollection.ModCollection.Name, (nint)modelCharaId, (nint)customize, (nint)equipData); + _lastCreatedCollection.ModCollection.Id, (nint)modelCharaId, (nint)customize, (nint)equipData); var decal = new DecalReverter(_config, _characterUtility, _resources, _lastCreatedCollection, UsesDecal(*(uint*)modelCharaId, (nint)customize)); diff --git a/Penumbra/Interop/PathResolving/PathResolver.cs b/Penumbra/Interop/PathResolving/PathResolver.cs index 7c16b97b..5c3d8d19 100644 --- a/Penumbra/Interop/PathResolving/PathResolver.cs +++ b/Penumbra/Interop/PathResolving/PathResolver.cs @@ -1,3 +1,4 @@ +using System.Runtime; using FFXIVClientStructs.FFXIV.Client.System.Resource; using Penumbra.Api.Enums; using Penumbra.Collections; @@ -43,8 +44,8 @@ public class PathResolver : IDisposable } /// Obtain a temporary or permanent collection by name. - public bool CollectionByName(string name, [NotNullWhen(true)] out ModCollection? collection) - => _tempCollections.CollectionByName(name, out collection) || _collectionManager.Storage.ByName(name, out collection); + public bool CollectionById(Guid id, [NotNullWhen(true)] out ModCollection? collection) + => _tempCollections.CollectionById(id, out collection) || _collectionManager.Storage.ById(id, out collection); /// Try to resolve the given game path to the replaced path. public (FullPath?, ResolveData) ResolvePath(Utf8GamePath path, ResourceCategory category, ResourceType resourceType) @@ -136,9 +137,10 @@ public class PathResolver : IDisposable return; var lastUnderscore = additionalData.LastIndexOf((byte)'_'); - var name = lastUnderscore == -1 ? additionalData.ToString() : additionalData.Substring(0, lastUnderscore).ToString(); + var idString = lastUnderscore == -1 ? additionalData : additionalData.Substring(0, lastUnderscore); if (Utf8GamePath.FromByteString(path, out var gamePath) - && CollectionByName(name, out var collection) + && GuidExtensions.FromOptimizedString(idString.Span, out var id) + && CollectionById(id, out var collection) && collection.HasCache && collection.GetImcFile(gamePath, out var file)) { diff --git a/Penumbra/Interop/PathResolving/SubfileHelper.cs b/Penumbra/Interop/PathResolving/SubfileHelper.cs index 2359c36e..844baaa9 100644 --- a/Penumbra/Interop/PathResolving/SubfileHelper.cs +++ b/Penumbra/Interop/PathResolving/SubfileHelper.cs @@ -76,7 +76,7 @@ public sealed unsafe class SubfileHelper : IDisposable, IReadOnlyCollection> GetResourcePathDictionaries(IEnumerable<(Character, ResourceTree)> resourceTrees) + public static Dictionary>> GetResourcePathDictionaries( + IEnumerable<(Character, ResourceTree)> resourceTrees) { var pathDictionaries = new Dictionary>>(4); @@ -23,8 +25,7 @@ internal static class ResourceTreeApiHelper CollectResourcePaths(pathDictionary, resourceTree); } - return pathDictionaries.ToDictionary(pair => pair.Key, - pair => (IReadOnlyDictionary)pair.Value.ToDictionary(pair => pair.Key, pair => pair.Value.ToArray()).AsReadOnly()); + return pathDictionaries; } private static void CollectResourcePaths(Dictionary> pathDictionary, ResourceTree resourceTree) @@ -37,7 +38,7 @@ internal static class ResourceTreeApiHelper var fullPath = node.FullPath.ToPath(); if (!pathDictionary.TryGetValue(fullPath, out var gamePaths)) { - gamePaths = new(); + gamePaths = []; pathDictionary.Add(fullPath, gamePaths); } @@ -46,17 +47,17 @@ internal static class ResourceTreeApiHelper } } - public static Dictionary> GetResourcesOfType(IEnumerable<(Character, ResourceTree)> resourceTrees, + public static Dictionary GetResourcesOfType(IEnumerable<(Character, ResourceTree)> resourceTrees, ResourceType type) { - var resDictionaries = new Dictionary>(4); + var resDictionaries = new Dictionary(4); foreach (var (gameObject, resourceTree) in resourceTrees) { if (resDictionaries.ContainsKey(gameObject.ObjectIndex)) continue; - var resDictionary = new Dictionary(); - resDictionaries.Add(gameObject.ObjectIndex, resDictionary); + var resDictionary = new Dictionary(); + resDictionaries.Add(gameObject.ObjectIndex, new GameResourceDict(resDictionary)); foreach (var node in resourceTree.FlatNodes) { @@ -66,38 +67,16 @@ internal static class ResourceTreeApiHelper continue; var fullPath = node.FullPath.ToPath(); - resDictionary.Add(node.ResourceHandle, (fullPath, node.Name ?? string.Empty, ChangedItemDrawer.ToApiIcon(node.Icon))); + resDictionary.Add(node.ResourceHandle, (fullPath, node.Name ?? string.Empty, (uint)ChangedItemDrawer.ToApiIcon(node.Icon))); } } - return resDictionaries.ToDictionary(pair => pair.Key, - pair => (IReadOnlyDictionary)pair.Value.AsReadOnly()); + return resDictionaries; } - public static Dictionary EncapsulateResourceTrees(IEnumerable<(Character, ResourceTree)> resourceTrees) + public static Dictionary EncapsulateResourceTrees(IEnumerable<(Character, ResourceTree)> resourceTrees) { - static Ipc.ResourceNode GetIpcNode(ResourceNode node) => - new() - { - Type = node.Type, - Icon = ChangedItemDrawer.ToApiIcon(node.Icon), - Name = node.Name, - GamePath = node.GamePath.Equals(Utf8GamePath.Empty) ? null : node.GamePath.ToString(), - ActualPath = node.FullPath.ToString(), - ObjectAddress = node.ObjectAddress, - ResourceHandle = node.ResourceHandle, - Children = node.Children.Select(GetIpcNode).ToList(), - }; - - static Ipc.ResourceTree GetIpcTree(ResourceTree tree) => - new() - { - Name = tree.Name, - RaceCode = (ushort)tree.RaceCode, - Nodes = tree.Nodes.Select(GetIpcNode).ToList(), - }; - - var resDictionary = new Dictionary(4); + var resDictionary = new Dictionary(4); foreach (var (gameObject, resourceTree) in resourceTrees) { if (resDictionary.ContainsKey(gameObject.ObjectIndex)) @@ -107,5 +86,38 @@ internal static class ResourceTreeApiHelper } return resDictionary; + + static JObject GetIpcTree(ResourceTree tree) + { + var ret = new JObject + { + [nameof(ResourceTreeDto.Name)] = tree.Name, + [nameof(ResourceTreeDto.RaceCode)] = (ushort)tree.RaceCode, + }; + var children = new JArray(); + foreach (var child in tree.Nodes) + children.Add(GetIpcNode(child)); + ret[nameof(ResourceTreeDto.Nodes)] = children; + return ret; + } + + static JObject GetIpcNode(ResourceNode node) + { + var ret = new JObject + { + [nameof(ResourceNodeDto.Type)] = new JValue(node.Type), + [nameof(ResourceNodeDto.Icon)] = new JValue(ChangedItemDrawer.ToApiIcon(node.Icon)), + [nameof(ResourceNodeDto.Name)] = node.Name, + [nameof(ResourceNodeDto.GamePath)] = node.GamePath.Equals(Utf8GamePath.Empty) ? null : node.GamePath.ToString(), + [nameof(ResourceNodeDto.ActualPath)] = node.FullPath.ToString(), + [nameof(ResourceNodeDto.ObjectAddress)] = node.ObjectAddress, + [nameof(ResourceNodeDto.ResourceHandle)] = node.ResourceHandle, + }; + var children = new JArray(); + foreach (var child in node.Children) + children.Add(GetIpcNode(child)); + ret[nameof(ResourceNodeDto.Children)] = children; + return ret; + } } } diff --git a/Penumbra/Mods/Manager/ModOptionEditor.cs b/Penumbra/Mods/Manager/ModOptionEditor.cs index 0ffdc4af..9efb8a3f 100644 --- a/Penumbra/Mods/Manager/ModOptionEditor.cs +++ b/Penumbra/Mods/Manager/ModOptionEditor.cs @@ -272,22 +272,19 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS return; var group = mod.Groups[groupIdx]; - if (group.Type is GroupType.Multi && group.Count >= IModGroup.MaxMultiOptions) - { - Penumbra.Log.Error( - $"Could not add option {option.Name} to {group.Name} for mod {mod.Name}, " - + $"since only up to {IModGroup.MaxMultiOptions} options are supported in one group."); - return; - } - - o.SetPosition(groupIdx, group.Count); - switch (group) { + case MultiModGroup { Count: >= IModGroup.MaxMultiOptions }: + Penumbra.Log.Error( + $"Could not add option {option.Name} to {group.Name} for mod {mod.Name}, " + + $"since only up to {IModGroup.MaxMultiOptions} options are supported in one group."); + return; case SingleModGroup s: + o.SetPosition(groupIdx, s.Count); s.OptionData.Add(o); break; case MultiModGroup m: + o.SetPosition(groupIdx, m.Count); m.PrioritizedOptions.Add((o, priority)); break; } diff --git a/Penumbra/Mods/Manager/ModStorage.cs b/Penumbra/Mods/Manager/ModStorage.cs index 1e5df6b9..65b8ddd9 100644 --- a/Penumbra/Mods/Manager/ModStorage.cs +++ b/Penumbra/Mods/Manager/ModStorage.cs @@ -19,7 +19,7 @@ public class ModCombo : FilterComboCache public class ModStorage : IReadOnlyList { /// The actual list of mods. - protected readonly List Mods = new(); + protected readonly List Mods = []; public int Count => Mods.Count; diff --git a/Penumbra/Mods/Subclasses/IModGroup.cs b/Penumbra/Mods/Subclasses/IModGroup.cs index e9e2a93b..2daf31e6 100644 --- a/Penumbra/Mods/Subclasses/IModGroup.cs +++ b/Penumbra/Mods/Subclasses/IModGroup.cs @@ -4,9 +4,9 @@ using Penumbra.Services; namespace Penumbra.Mods.Subclasses; -public interface IModGroup : IEnumerable +public interface IModGroup : IReadOnlyCollection { - public const int MaxMultiOptions = 32; + public const int MaxMultiOptions = 63; public string Name { get; } public string Description { get; } @@ -18,15 +18,7 @@ public interface IModGroup : IEnumerable public ISubMod this[Index idx] { get; } - public int Count { get; } - - public bool IsOption - => Type switch - { - GroupType.Single => Count > 1, - GroupType.Multi => Count > 0, - _ => false, - }; + public bool IsOption { get; } public IModGroup Convert(GroupType type); public bool MoveOption(int optionIdxFrom, int optionIdxTo); @@ -94,11 +86,13 @@ public readonly struct ModSaveGroup : ISavable j.WritePropertyName("Options"); j.WriteStartArray(); for (var idx = 0; idx < _group.Count; ++idx) + { ISubMod.WriteSubMod(j, serializer, _group[idx], _basePath, _group.Type switch { GroupType.Multi => _group.OptionPriority(idx), - _ => null, + _ => null, }); + } j.WriteEndArray(); j.WriteEndObject(); diff --git a/Penumbra/Mods/Subclasses/ModSettings.cs b/Penumbra/Mods/Subclasses/ModSettings.cs index b79b3242..380b242c 100644 --- a/Penumbra/Mods/Subclasses/ModSettings.cs +++ b/Penumbra/Mods/Subclasses/ModSettings.cs @@ -12,7 +12,7 @@ public class ModSettings { public static readonly ModSettings Empty = new(); public SettingList Settings { get; private init; } = []; - public ModPriority Priority { get; set; } + public ModPriority Priority { get; set; } public bool Enabled { get; set; } // Create an independent copy of the current settings. @@ -152,7 +152,7 @@ public class ModSettings public struct SavedSettings { public Dictionary Settings; - public ModPriority Priority; + public ModPriority Priority; public bool Enabled; public SavedSettings DeepCopy() @@ -203,9 +203,9 @@ public class ModSettings // Return the settings for a given mod in a shareable format, using the names of groups and options instead of indices. // Does not repair settings but ignores settings not fitting to the given mod. - public (bool Enabled, ModPriority Priority, Dictionary> Settings) ConvertToShareable(Mod mod) + public (bool Enabled, ModPriority Priority, Dictionary> Settings) ConvertToShareable(Mod mod) { - var dict = new Dictionary>(Settings.Count); + var dict = new Dictionary>(Settings.Count); foreach (var (setting, idx) in Settings.WithIndex()) { if (idx >= mod.Groups.Count) diff --git a/Penumbra/Mods/Subclasses/MultiModGroup.cs b/Penumbra/Mods/Subclasses/MultiModGroup.cs index 444e8e2c..7479cd54 100644 --- a/Penumbra/Mods/Subclasses/MultiModGroup.cs +++ b/Penumbra/Mods/Subclasses/MultiModGroup.cs @@ -25,6 +25,9 @@ public sealed class MultiModGroup : IModGroup public ISubMod this[Index idx] => PrioritizedOptions[idx].Mod; + public bool IsOption + => Count > 0; + [JsonIgnore] public int Count => PrioritizedOptions.Count; diff --git a/Penumbra/Mods/Subclasses/SingleModGroup.cs b/Penumbra/Mods/Subclasses/SingleModGroup.cs index 0bfa04f4..74769c7e 100644 --- a/Penumbra/Mods/Subclasses/SingleModGroup.cs +++ b/Penumbra/Mods/Subclasses/SingleModGroup.cs @@ -25,6 +25,9 @@ public sealed class SingleModGroup : IModGroup public ISubMod this[Index idx] => OptionData[idx]; + public bool IsOption + => Count > 1; + [JsonIgnore] public int Count => OptionData.Count; diff --git a/Penumbra/Mods/TemporaryMod.cs b/Penumbra/Mods/TemporaryMod.cs index 4de2ac13..6be07881 100644 --- a/Penumbra/Mods/TemporaryMod.cs +++ b/Penumbra/Mods/TemporaryMod.cs @@ -53,7 +53,7 @@ public class TemporaryMod : IMod dir = ModCreator.CreateModFolder(modManager.BasePath, collection.Name, config.ReplaceNonAsciiOnImport, true); var fileDir = Directory.CreateDirectory(Path.Combine(dir.FullName, "files")); modManager.DataEditor.CreateMeta(dir, collection.Name, character ?? config.DefaultModAuthor, - $"Mod generated from temporary collection {collection.Name} for {character ?? "Unknown Character"}.", null, null); + $"Mod generated from temporary collection {collection.Id} for {character ?? "Unknown Character"} with name {collection.Name}.", null, null); var mod = new Mod(dir); var defaultMod = mod.Default; foreach (var (gamePath, fullPath) in collection.ResolvedFiles) @@ -86,11 +86,11 @@ public class TemporaryMod : IMod saveService.ImmediateSave(new ModSaveGroup(dir, defaultMod, config.ReplaceNonAsciiOnImport)); modManager.AddMod(dir); - Penumbra.Log.Information($"Successfully generated mod {mod.Name} at {mod.ModPath.FullName} for collection {collection.Name}."); + Penumbra.Log.Information($"Successfully generated mod {mod.Name} at {mod.ModPath.FullName} for collection {collection.Identifier}."); } catch (Exception e) { - Penumbra.Log.Error($"Could not save temporary collection {collection.Name} to permanent Mod:\n{e}"); + Penumbra.Log.Error($"Could not save temporary collection {collection.Identifier} to permanent Mod:\n{e}"); if (dir != null && Directory.Exists(dir.FullName)) { try diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index b76780c0..42be0aa3 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -20,6 +20,7 @@ using ChangedItemHover = Penumbra.Communication.ChangedItemHover; using OtterGui.Tasks; using Penumbra.GameData.Enums; using Penumbra.UI; +using IPenumbraApi = Penumbra.Api.Api.IPenumbraApi; using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManager; namespace Penumbra; @@ -105,8 +106,7 @@ public class Penumbra : IDalamudPlugin private void SetupApi() { - var api = _services.GetService(); - _services.GetService(); + _services.GetService(); _communicatorService.ChangedItemHover.Subscribe(it => { if (it is (Item, FullEquipType)) diff --git a/Penumbra/Penumbra.csproj b/Penumbra/Penumbra.csproj index b07917e8..c8961579 100644 --- a/Penumbra/Penumbra.csproj +++ b/Penumbra/Penumbra.csproj @@ -1,4 +1,4 @@ - + net8.0-windows preview diff --git a/Penumbra/Services/ConfigMigrationService.cs b/Penumbra/Services/ConfigMigrationService.cs index d1e952f1..e775d81a 100644 --- a/Penumbra/Services/ConfigMigrationService.cs +++ b/Penumbra/Services/ConfigMigrationService.cs @@ -223,7 +223,7 @@ public class ConfigMigrationService(SaveService saveService) : IService try { var jObject = JObject.Parse(File.ReadAllText(collection.FullName)); - if (jObject[nameof(ModCollection.Name)]?.ToObject() == ForcedCollection) + if (jObject["Name"]?.ToObject() == ForcedCollection) continue; jObject[nameof(ModCollection.DirectlyInheritsFrom)] = JToken.FromObject(new List { ForcedCollection }); @@ -365,7 +365,7 @@ public class ConfigMigrationService(SaveService saveService) : IService dict = dict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value with { Priority = maxPriority - kvp.Value.Priority }); var emptyStorage = new ModStorage(); - var collection = ModCollection.CreateFromData(saveService, emptyStorage, ModCollection.DefaultCollectionName, 0, 1, dict, []); + var collection = ModCollection.CreateFromData(saveService, emptyStorage, Guid.NewGuid(), ModCollection.DefaultCollectionName, 0, 1, dict, []); saveService.ImmediateSaveSync(new ModCollectionSave(emptyStorage, collection)); } catch (Exception e) diff --git a/Penumbra/Services/CrashHandlerService.cs b/Penumbra/Services/CrashHandlerService.cs index 078b812b..6805e7db 100644 --- a/Penumbra/Services/CrashHandlerService.cs +++ b/Penumbra/Services/CrashHandlerService.cs @@ -239,7 +239,7 @@ public sealed class CrashHandlerService : IDisposable, IService var name = GetActorName(character); lock (_eventWriter) { - _eventWriter?.AnimationFuncInvoked.WriteLine(character, name.Span, collection.Name, type); + _eventWriter?.AnimationFuncInvoked.WriteLine(character, name.Span, collection.Id, type); } } catch (Exception ex) @@ -248,7 +248,7 @@ public sealed class CrashHandlerService : IDisposable, IService } } - private void OnCreatingCharacterBase(nint address, string collection, nint _1, nint _2, nint _3) + private void OnCreatingCharacterBase(nint address, Guid collection, nint _1, nint _2, nint _3) { if (_eventWriter == null) return; @@ -293,7 +293,7 @@ public sealed class CrashHandlerService : IDisposable, IService var name = GetActorName(resolveData.AssociatedGameObject); lock (_eventWriter) { - _eventWriter!.FileLoaded.WriteLine(resolveData.AssociatedGameObject, name.Span, resolveData.ModCollection.Name, + _eventWriter!.FileLoaded.WriteLine(resolveData.AssociatedGameObject, name.Span, resolveData.ModCollection.Id, manipulatedPath.Value.InternalName.Span, originalPath.Path.Span); } } diff --git a/Penumbra/Services/FilenameService.cs b/Penumbra/Services/FilenameService.cs index 40c63f15..e1c482f7 100644 --- a/Penumbra/Services/FilenameService.cs +++ b/Penumbra/Services/FilenameService.cs @@ -24,7 +24,7 @@ public class FilenameService(DalamudPluginInterface pi) : IService /// Obtain the path of a collection file given its name. public string CollectionFile(ModCollection collection) - => CollectionFile(collection.Name); + => CollectionFile(collection.Identifier); /// Obtain the path of a collection file given its name. public string CollectionFile(string collectionName) diff --git a/Penumbra/Services/StaticServiceManager.cs b/Penumbra/Services/StaticServiceManager.cs index 9e6071b4..e758aa35 100644 --- a/Penumbra/Services/StaticServiceManager.cs +++ b/Penumbra/Services/StaticServiceManager.cs @@ -9,6 +9,7 @@ using OtterGui.Classes; using OtterGui.Log; using OtterGui.Services; using Penumbra.Api; +using Penumbra.Api.Api; using Penumbra.Collections.Cache; using Penumbra.Collections.Manager; using Penumbra.GameData.Actors; @@ -30,8 +31,10 @@ using Penumbra.UI.ModsTab; using Penumbra.UI.ResourceWatcher; using Penumbra.UI.Tabs; using Penumbra.UI.Tabs.Debug; +using IPenumbraApi = Penumbra.Api.Api.IPenumbraApi; using MdlMaterialEditor = Penumbra.Mods.Editor.MdlMaterialEditor; using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManager; +using Penumbra.Api.IpcTester; namespace Penumbra.Services; @@ -195,10 +198,5 @@ public static class StaticServiceManager .AddSingleton(); private static ServiceManager AddApi(this ServiceManager services) - => services.AddSingleton() - .AddSingleton(x => x.GetRequiredService()) - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton(); + => services.AddSingleton(x => x.GetRequiredService()); } diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs index 9a38a5d5..10956deb 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs @@ -25,8 +25,8 @@ public partial class ModEditWindow var resources = ResourceTreeApiHelper .GetResourcesOfType(_resourceTreeFactory.FromObjectTable(ResourceTreeFactory.Flags.LocalPlayerRelatedOnly), type) .Values - .SelectMany(resources => resources.Values) - .Select(resource => resource.Item1); + .SelectMany(r => r.Values) + .Select(r => r.Item1); return new HashSet(resources, StringComparer.OrdinalIgnoreCase); } diff --git a/Penumbra/UI/CollectionTab/CollectionPanel.cs b/Penumbra/UI/CollectionTab/CollectionPanel.cs index 4d922af5..cbeabbd6 100644 --- a/Penumbra/UI/CollectionTab/CollectionPanel.cs +++ b/Penumbra/UI/CollectionTab/CollectionPanel.cs @@ -2,11 +2,13 @@ using Dalamud.Game.ClientState.Objects; using Dalamud.Interface; using Dalamud.Interface.Components; using Dalamud.Interface.GameFonts; +using Dalamud.Interface.Internal.Notifications; using Dalamud.Interface.ManagedFontAtlas; using Dalamud.Interface.Utility; using Dalamud.Plugin; using ImGuiNET; using OtterGui; +using OtterGui.Classes; using OtterGui.Raii; using Penumbra.Collections; using Penumbra.Collections.Manager; @@ -28,8 +30,8 @@ public sealed class CollectionPanel : IDisposable private readonly IndividualAssignmentUi _individualAssignmentUi; private readonly InheritanceUi _inheritanceUi; private readonly ModStorage _mods; - - private readonly IFontHandle _nameFont; + private readonly FilenameService _fileNames; + private readonly IFontHandle _nameFont; private static readonly IReadOnlyDictionary Buttons = CreateButtons(); private static readonly IReadOnlyList<(CollectionType, bool, bool, string, uint)> AdvancedTree = CreateTree(); @@ -38,7 +40,7 @@ public sealed class CollectionPanel : IDisposable private int _draggedIndividualAssignment = -1; public CollectionPanel(DalamudPluginInterface pi, CommunicatorService communicator, CollectionManager manager, - CollectionSelector selector, ActorManager actors, ITargetManager targets, ModStorage mods) + CollectionSelector selector, ActorManager actors, ITargetManager targets, ModStorage mods, FilenameService fileNames) { _collections = manager.Storage; _active = manager.Active; @@ -46,6 +48,7 @@ public sealed class CollectionPanel : IDisposable _actors = actors; _targets = targets; _mods = mods; + _fileNames = fileNames; _individualAssignmentUi = new IndividualAssignmentUi(communicator, actors, manager); _inheritanceUi = new InheritanceUi(manager, _selector); _nameFont = pi.UiBuilder.FontAtlas.NewGameFontHandle(new GameFontStyle(GameFontFamilyAndSize.Jupiter23)); @@ -206,12 +209,57 @@ public sealed class CollectionPanel : IDisposable var collection = _active.Current; DrawCollectionName(collection); DrawStatistics(collection); + DrawCollectionData(collection); _inheritanceUi.Draw(); ImGui.Separator(); DrawInactiveSettingsList(collection); DrawSettingsList(collection); } + private void DrawCollectionData(ModCollection collection) + { + ImGui.Dummy(Vector2.Zero); + ImGui.BeginGroup(); + ImGui.AlignTextToFramePadding(); + ImGui.TextUnformatted("Name"); + ImGui.AlignTextToFramePadding(); + ImGui.TextUnformatted("Identifier"); + ImGui.EndGroup(); + ImGui.SameLine(); + ImGui.BeginGroup(); + using var style = ImRaii.PushStyle(ImGuiStyleVar.ButtonTextAlign, new Vector2(0, 0.5f)); + var name = collection.Name; + var identifier = collection.Identifier; + var width = ImGui.GetContentRegionAvail().X; + var fileName = _fileNames.CollectionFile(collection); + ImGui.SetNextItemWidth(width); + ImGui.InputText("##name", ref name, 128); + using (ImRaii.PushFont(UiBuilder.MonoFont)) + { + if (ImGui.Button(collection.Identifier, new Vector2(width, 0))) + try + { + Process.Start(new ProcessStartInfo(fileName) { UseShellExecute = true }); + } + catch (Exception ex) + { + Penumbra.Messager.NotificationMessage(ex, $"Could not open file {fileName}.", $"Could not open file {fileName}", + NotificationType.Warning); + } + } + + if (ImGui.IsItemClicked(ImGuiMouseButton.Right)) + ImGui.SetClipboardText(identifier); + + ImGuiUtil.HoverTooltip( + $"Open the file\n\t{fileName}\ncontaining this design in the .json-editor of your choice.\n\nRight-Click to copy identifier to clipboard."); + + ImGui.EndGroup(); + ImGui.Dummy(Vector2.Zero); + ImGui.Separator(); + ImGui.Dummy(Vector2.Zero); + } + private void DrawContext(bool open, ModCollection? collection, CollectionType type, ActorIdentifier identifier, string text, char suffix) { var label = $"{type}{text}{suffix}"; diff --git a/Penumbra/UI/CollectionTab/CollectionSelector.cs b/Penumbra/UI/CollectionTab/CollectionSelector.cs index e568ecaf..fac85d4d 100644 --- a/Penumbra/UI/CollectionTab/CollectionSelector.cs +++ b/Penumbra/UI/CollectionTab/CollectionSelector.cs @@ -24,7 +24,7 @@ public sealed class CollectionSelector : ItemSelector, IDisposabl public CollectionSelector(Configuration config, CommunicatorService communicator, CollectionStorage storage, ActiveCollections active, TutorialService tutorial) - : base(new List(), Flags.Delete | Flags.Add | Flags.Duplicate | Flags.Filter) + : base([], Flags.Delete | Flags.Add | Flags.Duplicate | Flags.Filter) { _config = config; _communicator = communicator; diff --git a/Penumbra/UI/Tabs/CollectionsTab.cs b/Penumbra/UI/Tabs/CollectionsTab.cs index 3c6a3ed9..fe1471b3 100644 --- a/Penumbra/UI/Tabs/CollectionsTab.cs +++ b/Penumbra/UI/Tabs/CollectionsTab.cs @@ -41,12 +41,12 @@ public sealed class CollectionsTab : IDisposable, ITab } public CollectionsTab(DalamudPluginInterface pi, Configuration configuration, CommunicatorService communicator, - CollectionManager collectionManager, ModStorage modStorage, ActorManager actors, ITargetManager targets, TutorialService tutorial) + CollectionManager collectionManager, ModStorage modStorage, ActorManager actors, ITargetManager targets, TutorialService tutorial, FilenameService fileNames) { _config = configuration.Ephemeral; _tutorial = tutorial; _selector = new CollectionSelector(configuration, communicator, collectionManager.Storage, collectionManager.Active, _tutorial); - _panel = new CollectionPanel(pi, communicator, collectionManager, _selector, actors, targets, modStorage); + _panel = new CollectionPanel(pi, communicator, collectionManager, _selector, actors, targets, modStorage, fileNames); } public void Dispose() diff --git a/Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs b/Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs index 78014054..4649e548 100644 --- a/Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs +++ b/Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs @@ -55,7 +55,7 @@ public static class CrashDataExtensions ImGuiUtil.DrawTableColumn(character.Age.ToString(CultureInfo.InvariantCulture)); ImGuiUtil.DrawTableColumn(character.ThreadId.ToString()); ImGuiUtil.DrawTableColumn(character.CharacterName); - ImGuiUtil.DrawTableColumn(character.CollectionName); + ImGuiUtil.DrawTableColumn(character.CollectionId.ToString()); ImGuiUtil.DrawTableColumn(character.CharacterAddress); ImGuiUtil.DrawTableColumn(character.Timestamp.ToString()); }, ImGui.GetTextLineHeightWithSpacing()); @@ -79,7 +79,7 @@ public static class CrashDataExtensions ImGuiUtil.DrawTableColumn(file.ActualFileName); ImGuiUtil.DrawTableColumn(file.RequestedFileName); ImGuiUtil.DrawTableColumn(file.CharacterName); - ImGuiUtil.DrawTableColumn(file.CollectionName); + ImGuiUtil.DrawTableColumn(file.CollectionId.ToString()); ImGuiUtil.DrawTableColumn(file.CharacterAddress); ImGuiUtil.DrawTableColumn(file.Timestamp.ToString()); }, ImGui.GetTextLineHeightWithSpacing()); @@ -102,7 +102,7 @@ public static class CrashDataExtensions ImGuiUtil.DrawTableColumn(vfx.ThreadId.ToString()); ImGuiUtil.DrawTableColumn(vfx.InvocationType); ImGuiUtil.DrawTableColumn(vfx.CharacterName); - ImGuiUtil.DrawTableColumn(vfx.CollectionName); + ImGuiUtil.DrawTableColumn(vfx.CollectionId.ToString()); ImGuiUtil.DrawTableColumn(vfx.CharacterAddress); ImGuiUtil.DrawTableColumn(vfx.Timestamp.ToString()); }, ImGui.GetTextLineHeightWithSpacing()); diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index 9a956d2d..1813a7e3 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -40,6 +40,7 @@ using CharacterUtility = Penumbra.Interop.Services.CharacterUtility; using ObjectKind = Dalamud.Game.ClientState.Objects.Enums.ObjectKind; using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManager; using ImGuiClip = OtterGui.ImGuiClip; +using Penumbra.Api.IpcTester; namespace Penumbra.UI.Tabs.Debug; @@ -76,7 +77,6 @@ public class DebugTab : Window, ITab private readonly CharacterUtility _characterUtility; private readonly ResidentResourceManager _residentResources; private readonly ResourceManagerService _resourceManager; - private readonly PenumbraIpcProviders _ipc; private readonly CollectionResolver _collectionResolver; private readonly DrawObjectState _drawObjectState; private readonly PathState _pathState; @@ -100,7 +100,7 @@ public class DebugTab : Window, ITab IClientState clientState, ValidityChecker validityChecker, ModManager modManager, HttpApi httpApi, ActorManager actors, StainService stains, CharacterUtility characterUtility, ResidentResourceManager residentResources, - ResourceManagerService resourceManager, PenumbraIpcProviders ipc, CollectionResolver collectionResolver, + ResourceManagerService resourceManager, CollectionResolver collectionResolver, DrawObjectState drawObjectState, PathState pathState, SubfileHelper subfileHelper, IdentifiedCollectionCache identifiedCollectionCache, CutsceneService cutsceneService, ModImportManager modImporter, ImportPopup importPopup, FrameworkManager framework, TextureManager textureManager, ShaderReplacementFixer shaderReplacementFixer, RedrawService redraws, DictEmote emotes, @@ -124,7 +124,6 @@ public class DebugTab : Window, ITab _characterUtility = characterUtility; _residentResources = residentResources; _resourceManager = resourceManager; - _ipc = ipc; _collectionResolver = collectionResolver; _drawObjectState = drawObjectState; _pathState = pathState; @@ -440,7 +439,9 @@ public class DebugTab : Window, ITab : $"0x{(nint)((Character*)obj.Address)->GameObject.GetDrawObject():X}"); var identifier = _actors.FromObject(obj, out _, false, true, false); ImGuiUtil.DrawTableColumn(_actors.ToString(identifier)); - var id = obj.AsObject->ObjectKind ==(byte) ObjectKind.BattleNpc ? $"{identifier.DataId} | {obj.AsObject->DataID}" : identifier.DataId.ToString(); + var id = obj.AsObject->ObjectKind == (byte)ObjectKind.BattleNpc + ? $"{identifier.DataId} | {obj.AsObject->DataID}" + : identifier.DataId.ToString(); ImGuiUtil.DrawTableColumn(id); } @@ -969,13 +970,8 @@ public class DebugTab : Window, ITab /// Draw information about IPC options and availability. private void DrawDebugTabIpc() { - if (!ImGui.CollapsingHeader("IPC")) - { - _ipcTester.UnsubscribeEvents(); - return; - } - - _ipcTester.Draw(); + if (ImGui.CollapsingHeader("IPC")) + _ipcTester.Draw(); } /// Helper to print a property and its value in a 2-column table. From 1ef9346eab9e827bf31664dae0171656d2cae1af Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 12 Apr 2024 12:42:16 +0200 Subject: [PATCH 096/433] Allow renaming of collection. --- .../Collections/ModCollection.Cache.Access.cs | 2 +- Penumbra/Collections/ModCollection.cs | 2 +- Penumbra/UI/CollectionTab/CollectionPanel.cs | 37 ++++++++++++------- Penumbra/UI/CollectionTab/InheritanceUi.cs | 29 +++++---------- 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/Penumbra/Collections/ModCollection.Cache.Access.cs b/Penumbra/Collections/ModCollection.Cache.Access.cs index 7c29676d..b073e731 100644 --- a/Penumbra/Collections/ModCollection.Cache.Access.cs +++ b/Penumbra/Collections/ModCollection.Cache.Access.cs @@ -77,7 +77,7 @@ public partial class ModCollection else { _cache.Meta.SetFiles(); - Penumbra.Log.Debug($"Set CharacterUtility resources for collection {Name}."); + Penumbra.Log.Debug($"Set CharacterUtility resources for collection {Identifier}."); } } diff --git a/Penumbra/Collections/ModCollection.cs b/Penumbra/Collections/ModCollection.cs index c1143c71..327d6544 100644 --- a/Penumbra/Collections/ModCollection.cs +++ b/Penumbra/Collections/ModCollection.cs @@ -28,7 +28,7 @@ public partial class ModCollection public static readonly ModCollection Empty = CreateEmpty(EmptyCollectionName, 0, 0); /// The name of a collection. - public string Name { get; set; } = string.Empty; + public string Name { get; set; } public Guid Id { get; } diff --git a/Penumbra/UI/CollectionTab/CollectionPanel.cs b/Penumbra/UI/CollectionTab/CollectionPanel.cs index cbeabbd6..8625335e 100644 --- a/Penumbra/UI/CollectionTab/CollectionPanel.cs +++ b/Penumbra/UI/CollectionTab/CollectionPanel.cs @@ -35,7 +35,8 @@ public sealed class CollectionPanel : IDisposable private static readonly IReadOnlyDictionary Buttons = CreateButtons(); private static readonly IReadOnlyList<(CollectionType, bool, bool, string, uint)> AdvancedTree = CreateTree(); - private readonly List<(CollectionType Type, ActorIdentifier Identifier)> _inUseCache = new(); + private readonly List<(CollectionType Type, ActorIdentifier Identifier)> _inUseCache = []; + private string? _newName; private int _draggedIndividualAssignment = -1; @@ -93,6 +94,18 @@ public sealed class CollectionPanel : IDisposable var first = true; + Button(CollectionType.NonPlayerChild); + Button(CollectionType.NonPlayerElderly); + foreach (var race in Enum.GetValues().Skip(1)) + { + Button(CollectionTypeExtensions.FromParts(race, Gender.Male, false)); + Button(CollectionTypeExtensions.FromParts(race, Gender.Female, false)); + Button(CollectionTypeExtensions.FromParts(race, Gender.Male, true)); + Button(CollectionTypeExtensions.FromParts(race, Gender.Female, true)); + } + + return; + void Button(CollectionType type) { var (name, border) = Buttons[type]; @@ -112,16 +125,6 @@ public sealed class CollectionPanel : IDisposable if (ImGui.GetContentRegionAvail().X < buttonWidth.X + ImGui.GetStyle().ItemSpacing.X + ImGui.GetStyle().WindowPadding.X) ImGui.NewLine(); } - - Button(CollectionType.NonPlayerChild); - Button(CollectionType.NonPlayerElderly); - foreach (var race in Enum.GetValues().Skip(1)) - { - Button(CollectionTypeExtensions.FromParts(race, Gender.Male, false)); - Button(CollectionTypeExtensions.FromParts(race, Gender.Female, false)); - Button(CollectionTypeExtensions.FromParts(race, Gender.Male, true)); - Button(CollectionTypeExtensions.FromParts(race, Gender.Female, true)); - } } /// Draw the panel containing new and existing individual assignments. @@ -228,12 +231,20 @@ public sealed class CollectionPanel : IDisposable ImGui.SameLine(); ImGui.BeginGroup(); using var style = ImRaii.PushStyle(ImGuiStyleVar.ButtonTextAlign, new Vector2(0, 0.5f)); - var name = collection.Name; + var name = _newName ?? collection.Name; var identifier = collection.Identifier; var width = ImGui.GetContentRegionAvail().X; var fileName = _fileNames.CollectionFile(collection); ImGui.SetNextItemWidth(width); - ImGui.InputText("##name", ref name, 128); + if (ImGui.InputText("##name", ref name, 128)) + _newName = name; + if (ImGui.IsItemDeactivatedAfterEdit() && _newName != null) + { + collection.Name = _newName; + _newName = null; + } + else if (ImGui.IsItemDeactivated()) + _newName = null; using (ImRaii.PushFont(UiBuilder.MonoFont)) { if (ImGui.Button(collection.Identifier, new Vector2(width, 0))) diff --git a/Penumbra/UI/CollectionTab/InheritanceUi.cs b/Penumbra/UI/CollectionTab/InheritanceUi.cs index 88344e6a..2290592d 100644 --- a/Penumbra/UI/CollectionTab/InheritanceUi.cs +++ b/Penumbra/UI/CollectionTab/InheritanceUi.cs @@ -2,30 +2,21 @@ using Dalamud.Interface; using ImGuiNET; using OtterGui; using OtterGui.Raii; +using OtterGui.Services; using Penumbra.Collections; using Penumbra.Collections.Manager; using Penumbra.UI.Classes; namespace Penumbra.UI.CollectionTab; -public class InheritanceUi +public class InheritanceUi(CollectionManager collectionManager, CollectionSelector selector) : IUiService { private const int InheritedCollectionHeight = 9; private const string InheritanceDragDropLabel = "##InheritanceMove"; - private readonly CollectionStorage _collections; - private readonly ActiveCollections _active; - private readonly InheritanceManager _inheritance; - private readonly CollectionSelector _selector; - - public InheritanceUi(CollectionManager collectionManager, CollectionSelector selector) - { - _selector = selector; - _collections = collectionManager.Storage; - _active = collectionManager.Active; - _inheritance = collectionManager.Inheritances; - } - + private readonly CollectionStorage _collections = collectionManager.Storage; + private readonly ActiveCollections _active = collectionManager.Active; + private readonly InheritanceManager _inheritance = collectionManager.Inheritances; /// Draw the whole inheritance block. public void Draw() @@ -59,7 +50,7 @@ public class InheritanceUi private (int, int)? _inheritanceAction; private ModCollection? _newCurrentCollection; - private void DrawRightText() + private static void DrawRightText() { using var group = ImRaii.Group(); ImGuiUtil.TextWrapped( @@ -68,7 +59,7 @@ public class InheritanceUi "You can select inheritances from the combo below to add them.\nSince the order of inheritances is important, you can reorder them here via drag and drop.\nYou can also delete inheritances by dragging them onto the trash can."); } - private void DrawHelpPopup() + private static void DrawHelpPopup() => ImGuiUtil.HelpPopup("InheritanceHelp", new Vector2(1000 * UiHelpers.Scale, 20 * ImGui.GetTextLineHeightWithSpacing()), () => { ImGui.NewLine(); @@ -123,7 +114,7 @@ public class InheritanceUi _seenInheritedCollections.Contains(inheritance)); _seenInheritedCollections.Add(inheritance); - ImRaii.TreeNode($"{Name(inheritance)}###{inheritance.Name}", + ImRaii.TreeNode($"{Name(inheritance)}###{inheritance.Id}", ImGuiTreeNodeFlags.NoTreePushOnOpen | ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet); var (minRect, maxRect) = (ImGui.GetItemRectMin(), ImGui.GetItemRectMax()); DrawInheritanceTreeClicks(inheritance, false); @@ -134,7 +125,7 @@ public class InheritanceUi // Draw the notch and increase the line length. var midPoint = (minRect.Y + maxRect.Y) / 2f - 1f; - drawList.AddLine(new Vector2(lineStart.X, midPoint), new Vector2(lineStart.X + lineSize, midPoint), Colors.MetaInfoText, + drawList.AddLine(lineStart with { Y = midPoint }, new Vector2(lineStart.X + lineSize, midPoint), Colors.MetaInfoText, UiHelpers.Scale); lineEnd.Y = midPoint; } @@ -321,5 +312,5 @@ public class InheritanceUi } private string Name(ModCollection collection) - => _selector.IncognitoMode ? collection.AnonymizedName : collection.Name; + => selector.IncognitoMode ? collection.AnonymizedName : collection.Name; } From 791583e183d4afd0a72e047d988f8ad5cb62a728 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 12 Apr 2024 12:51:33 +0200 Subject: [PATCH 097/433] Silence readme warnings. --- Penumbra.Api | 2 +- Penumbra.String | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Penumbra.Api b/Penumbra.Api index e5c8f544..9bbc3b98 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit e5c8f5446879e2e0e541eb4d8fee15e98b1885bc +Subproject commit 9bbc3b98efc2af3707adc75b716d4f3072908e31 diff --git a/Penumbra.String b/Penumbra.String index 14e00f77..caa58c5c 160000 --- a/Penumbra.String +++ b/Penumbra.String @@ -1 +1 @@ -Subproject commit 14e00f77d42bc677e02325660db765ef11932560 +Subproject commit caa58c5c92710e69ce07b9d736ebe2d228cb4488 From e4f9150c9fc22ab85582f13a5dc866b115e61626 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 12 Apr 2024 14:30:47 +0200 Subject: [PATCH 098/433] Fix? --- Penumbra.Api | 2 +- Penumbra/Api/Api/PluginStateApi.cs | 8 ++++---- Penumbra/Communication/ModDirectoryChanged.cs | 3 +-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Penumbra.Api b/Penumbra.Api index 9bbc3b98..cd56068a 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit 9bbc3b98efc2af3707adc75b716d4f3072908e31 +Subproject commit cd56068aac3762c7b011d13a04637a3c3f09775f diff --git a/Penumbra/Api/Api/PluginStateApi.cs b/Penumbra/Api/Api/PluginStateApi.cs index 2e87486f..e1eec1b2 100644 --- a/Penumbra/Api/Api/PluginStateApi.cs +++ b/Penumbra/Api/Api/PluginStateApi.cs @@ -13,16 +13,16 @@ public class PluginStateApi(Configuration config, CommunicatorService communicat public string GetConfiguration() => JsonConvert.SerializeObject(config, Formatting.Indented); - public event Action? ModDirectoryChanged + public event Action ModDirectoryChanged { - add => communicator.ModDirectoryChanged.Subscribe(value!, Communication.ModDirectoryChanged.Priority.Api); - remove => communicator.ModDirectoryChanged.Unsubscribe(value!); + add => communicator.ModDirectoryChanged.Subscribe(value, Communication.ModDirectoryChanged.Priority.Api); + remove => communicator.ModDirectoryChanged.Unsubscribe(value); } public bool GetEnabledState() => config.EnableMods; - public event Action? EnabledChange + public event Action EnabledChange { add => communicator.EnabledChanged.Subscribe(value!, EnabledChanged.Priority.Api); remove => communicator.EnabledChanged.Unsubscribe(value!); diff --git a/Penumbra/Communication/ModDirectoryChanged.cs b/Penumbra/Communication/ModDirectoryChanged.cs index 02293873..9c64573f 100644 --- a/Penumbra/Communication/ModDirectoryChanged.cs +++ b/Penumbra/Communication/ModDirectoryChanged.cs @@ -1,5 +1,4 @@ using OtterGui.Classes; -using Penumbra.Api; using Penumbra.Api.Api; namespace Penumbra.Communication; @@ -15,7 +14,7 @@ public sealed class ModDirectoryChanged() : EventWrapper + /// Api = 0, /// From d5ed4a38e4b760b369cc2cbe773a9d017ff464d6 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 12 Apr 2024 14:39:12 +0200 Subject: [PATCH 099/433] Fix2? --- Penumbra.Api | 2 +- Penumbra/Api/Api/PluginStateApi.cs | 43 ++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/Penumbra.Api b/Penumbra.Api index cd56068a..a8e2fe02 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit cd56068aac3762c7b011d13a04637a3c3f09775f +Subproject commit a8e2fe0219b8fd1f787171f11e33571317e531c1 diff --git a/Penumbra/Api/Api/PluginStateApi.cs b/Penumbra/Api/Api/PluginStateApi.cs index e1eec1b2..e053e56f 100644 --- a/Penumbra/Api/Api/PluginStateApi.cs +++ b/Penumbra/Api/Api/PluginStateApi.cs @@ -5,26 +5,41 @@ using Penumbra.Services; namespace Penumbra.Api.Api; -public class PluginStateApi(Configuration config, CommunicatorService communicator) : IPenumbraApiPluginState, IApiService +public sealed class PluginStateApi : IPenumbraApiPluginState, IApiService, IDisposable { + private readonly Configuration _config; + private readonly CommunicatorService _communicator; + + public PluginStateApi(Configuration config, CommunicatorService communicator) + { + _config = config; + _communicator = communicator; + _communicator.ModDirectoryChanged.Subscribe(OnModDirectoryChanged, Communication.ModDirectoryChanged.Priority.Api); + _communicator.EnabledChanged.Subscribe(OnEnabledChanged, EnabledChanged.Priority.Api); + } + + public void Dispose() + { + _communicator.ModDirectoryChanged.Unsubscribe(OnModDirectoryChanged); + _communicator.EnabledChanged.Unsubscribe(OnEnabledChanged); + } + public string GetModDirectory() - => config.ModDirectory; + => _config.ModDirectory; public string GetConfiguration() - => JsonConvert.SerializeObject(config, Formatting.Indented); + => JsonConvert.SerializeObject(_config, Formatting.Indented); - public event Action ModDirectoryChanged - { - add => communicator.ModDirectoryChanged.Subscribe(value, Communication.ModDirectoryChanged.Priority.Api); - remove => communicator.ModDirectoryChanged.Unsubscribe(value); - } + public event Action? ModDirectoryChanged; public bool GetEnabledState() - => config.EnableMods; + => _config.EnableMods; - public event Action EnabledChange - { - add => communicator.EnabledChanged.Subscribe(value!, EnabledChanged.Priority.Api); - remove => communicator.EnabledChanged.Unsubscribe(value!); - } + public event Action? EnabledChange; + + private void OnModDirectoryChanged(string modDirectory, bool valid) + => ModDirectoryChanged?.Invoke(modDirectory, valid); + + private void OnEnabledChanged(bool value) + => EnabledChange?.Invoke(value); } From d9bd05c9ecf675da9d2d793b1c0654c8a6c5a613 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 12 Apr 2024 14:45:25 +0200 Subject: [PATCH 100/433] Fix issue with Hex viewer. --- OtterGui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OtterGui b/OtterGui index 9599c806..a50d2aed 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 9599c806877e2972f964dfa68e5207cf3a8f2b84 +Subproject commit a50d2aedbb7b2e37d30987bd0b3b96a832fdc0af From 6b5321dad8103ca3a9077f511f47047e90ccdddf Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 12 Apr 2024 14:46:26 +0200 Subject: [PATCH 101/433] Test subscription like before but without primary constructor. --- Penumbra/Api/Api/PluginStateApi.cs | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/Penumbra/Api/Api/PluginStateApi.cs b/Penumbra/Api/Api/PluginStateApi.cs index e053e56f..d69df448 100644 --- a/Penumbra/Api/Api/PluginStateApi.cs +++ b/Penumbra/Api/Api/PluginStateApi.cs @@ -5,7 +5,7 @@ using Penumbra.Services; namespace Penumbra.Api.Api; -public sealed class PluginStateApi : IPenumbraApiPluginState, IApiService, IDisposable +public class PluginStateApi : IPenumbraApiPluginState, IApiService { private readonly Configuration _config; private readonly CommunicatorService _communicator; @@ -14,14 +14,6 @@ public sealed class PluginStateApi : IPenumbraApiPluginState, IApiService, IDisp { _config = config; _communicator = communicator; - _communicator.ModDirectoryChanged.Subscribe(OnModDirectoryChanged, Communication.ModDirectoryChanged.Priority.Api); - _communicator.EnabledChanged.Subscribe(OnEnabledChanged, EnabledChanged.Priority.Api); - } - - public void Dispose() - { - _communicator.ModDirectoryChanged.Unsubscribe(OnModDirectoryChanged); - _communicator.EnabledChanged.Unsubscribe(OnEnabledChanged); } public string GetModDirectory() @@ -30,16 +22,18 @@ public sealed class PluginStateApi : IPenumbraApiPluginState, IApiService, IDisp public string GetConfiguration() => JsonConvert.SerializeObject(_config, Formatting.Indented); - public event Action? ModDirectoryChanged; + public event Action? ModDirectoryChanged + { + add => _communicator.ModDirectoryChanged.Subscribe(value!, Communication.ModDirectoryChanged.Priority.Api); + remove => _communicator.ModDirectoryChanged.Unsubscribe(value!); + } public bool GetEnabledState() => _config.EnableMods; - public event Action? EnabledChange; - - private void OnModDirectoryChanged(string modDirectory, bool valid) - => ModDirectoryChanged?.Invoke(modDirectory, valid); - - private void OnEnabledChanged(bool value) - => EnabledChange?.Invoke(value); + public event Action? EnabledChange + { + add => _communicator.EnabledChanged.Subscribe(value!, EnabledChanged.Priority.Api); + remove => _communicator.EnabledChanged.Unsubscribe(value!); + } } From 42ad941ec22906f2d631ca22dd7dba16b16bca18 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 13 Apr 2024 16:05:44 +0200 Subject: [PATCH 102/433] Add GetCollectionsByIdentifier. --- Penumbra.Api | 2 +- Penumbra/Api/Api/ApiHelpers.cs | 6 ++++-- Penumbra/Api/Api/CollectionApi.cs | 19 +++++++++++++++++ Penumbra/Api/IpcProviders.cs | 1 + .../Api/IpcTester/CollectionsIpcTester.cs | 21 ++++++++++++++++++- 5 files changed, 45 insertions(+), 4 deletions(-) diff --git a/Penumbra.Api b/Penumbra.Api index a8e2fe02..2f76f42e 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit a8e2fe0219b8fd1f787171f11e33571317e531c1 +Subproject commit 2f76f42e54141258d89300aa78d42fb3f1878092 diff --git a/Penumbra/Api/Api/ApiHelpers.cs b/Penumbra/Api/Api/ApiHelpers.cs index 32a3956f..92a30bce 100644 --- a/Penumbra/Api/Api/ApiHelpers.cs +++ b/Penumbra/Api/Api/ApiHelpers.cs @@ -48,8 +48,10 @@ public class ApiHelpers( [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] internal static PenumbraApiEc Return(PenumbraApiEc ec, LazyString args, [CallerMemberName] string name = "Unknown") { - Penumbra.Log.Debug( - $"[{name}] Called with {args}, returned {ec}."); + if (ec is PenumbraApiEc.Success or PenumbraApiEc.NothingChanged) + Penumbra.Log.Verbose($"[{name}] Called with {args}, returned {ec}."); + else + Penumbra.Log.Debug($"[{name}] Called with {args}, returned {ec}."); return ec; } diff --git a/Penumbra/Api/Api/CollectionApi.cs b/Penumbra/Api/Api/CollectionApi.cs index de704460..e99850a6 100644 --- a/Penumbra/Api/Api/CollectionApi.cs +++ b/Penumbra/Api/Api/CollectionApi.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Collections; @@ -10,6 +11,24 @@ public class CollectionApi(CollectionManager collections, ApiHelpers helpers) : public Dictionary GetCollections() => collections.Storage.ToDictionary(c => c.Id, c => c.Name); + public List<(Guid Id, string Name)> GetCollectionsByIdentifier(string identifier) + { + if (identifier.Length == 0) + return []; + + var list = new List<(Guid Id, string Name)>(4); + if (Guid.TryParse(identifier, out var guid) && collections.Storage.ById(guid, out var collection) && collection != ModCollection.Empty) + list.Add((collection.Id, collection.Name)); + else if (identifier.Length >= 8) + list.AddRange(collections.Storage.Where(c => c.Identifier.StartsWith(identifier, StringComparison.OrdinalIgnoreCase)) + .Select(c => (c.Id, c.Name))); + + list.AddRange(collections.Storage + .Where(c => string.Equals(c.Name, identifier, StringComparison.OrdinalIgnoreCase) && !list.Contains((c.Id, c.Name))) + .Select(c => (c.Id, c.Name))); + return list; + } + public Dictionary GetChangedItemsForCollection(Guid collectionId) { try diff --git a/Penumbra/Api/IpcProviders.cs b/Penumbra/Api/IpcProviders.cs index 293af588..cc98ef0d 100644 --- a/Penumbra/Api/IpcProviders.cs +++ b/Penumbra/Api/IpcProviders.cs @@ -19,6 +19,7 @@ public sealed class IpcProviders : IDisposable, IApiService _providers = [ IpcSubscribers.GetCollections.Provider(pi, api.Collection), + IpcSubscribers.GetCollectionsByIdentifier.Provider(pi, api.Collection), IpcSubscribers.GetChangedItemsForCollection.Provider(pi, api.Collection), IpcSubscribers.GetCollection.Provider(pi, api.Collection), IpcSubscribers.GetCollectionForObject.Provider(pi, api.Collection), diff --git a/Penumbra/Api/IpcTester/CollectionsIpcTester.cs b/Penumbra/Api/IpcTester/CollectionsIpcTester.cs index 12314f0c..2679bc69 100644 --- a/Penumbra/Api/IpcTester/CollectionsIpcTester.cs +++ b/Penumbra/Api/IpcTester/CollectionsIpcTester.cs @@ -35,7 +35,7 @@ public class CollectionsIpcTester(DalamudPluginInterface pi) : IUiService ImGuiUtil.GenericEnumCombo("Collection Type", 200, _type, out _type, t => ((CollectionType)t).ToName()); ImGui.InputInt("Object Index##Collections", ref _objectIdx, 0, 0); - ImGuiUtil.GuidInput("Collection Id##Collections", "Collection GUID...", string.Empty, ref _collectionId, ref _collectionIdString); + ImGuiUtil.GuidInput("Collection Id##Collections", "Collection Identifier...", string.Empty, ref _collectionId, ref _collectionIdString); ImGui.Checkbox("Allow Assignment Creation", ref _allowCreation); ImGui.SameLine(); ImGui.Checkbox("Allow Assignment Deletion", ref _allowDeletion); @@ -48,6 +48,25 @@ public class CollectionsIpcTester(DalamudPluginInterface pi) : IUiService if (_oldCollection != null) ImGui.TextUnformatted(!_oldCollection.HasValue ? "Created" : _oldCollection.ToString()); + IpcTester.DrawIntro(GetCollectionsByIdentifier.Label, "Collection Identifier"); + var collectionList = new GetCollectionsByIdentifier(pi).Invoke(_collectionIdString); + if (collectionList.Count == 0) + { + DrawCollection(null); + } + else + { + DrawCollection(collectionList[0]); + foreach (var pair in collectionList.Skip(1)) + { + ImGui.TableNextRow(); + ImGui.TableNextColumn(); + ImGui.TableNextColumn(); + ImGui.TableNextColumn(); + DrawCollection(pair); + } + } + IpcTester.DrawIntro(GetCollection.Label, "Current Collection"); DrawCollection(new GetCollection(pi).Invoke(ApiCollectionType.Current)); From 94b53ce7fab320286a08c2533fc400e0055eccd4 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 13 Apr 2024 16:06:04 +0200 Subject: [PATCH 103/433] Meh. --- Penumbra/Api/Api/CollectionApi.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Penumbra/Api/Api/CollectionApi.cs b/Penumbra/Api/Api/CollectionApi.cs index e99850a6..ff393aaf 100644 --- a/Penumbra/Api/Api/CollectionApi.cs +++ b/Penumbra/Api/Api/CollectionApi.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Collections; From d4183a03c0734fb72e810fdbcfc2f3b33495ed1c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 14 Apr 2024 15:00:48 +0200 Subject: [PATCH 104/433] Fix bug with new empty collections. --- Penumbra/Collections/ModCollection.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Penumbra/Collections/ModCollection.cs b/Penumbra/Collections/ModCollection.cs index 327d6544..e666b151 100644 --- a/Penumbra/Collections/ModCollection.cs +++ b/Penumbra/Collections/ModCollection.cs @@ -25,7 +25,7 @@ public partial class ModCollection /// Create the always available Empty Collection that will always sit at index 0, /// can not be deleted and does never create a cache. /// - public static readonly ModCollection Empty = CreateEmpty(EmptyCollectionName, 0, 0); + public static readonly ModCollection Empty = new(Guid.Empty, EmptyCollectionName, 0, 0, CurrentVersion, [], [], []); /// The name of a collection. public string Name { get; set; } @@ -150,7 +150,7 @@ public partial class ModCollection public static ModCollection CreateEmpty(string name, int index, int modCount) { Debug.Assert(index >= 0, "Empty collection created with negative index."); - return new ModCollection(Guid.Empty, name, index, 0, CurrentVersion, Enumerable.Repeat((ModSettings?)null, modCount).ToList(), [], + return new ModCollection(Guid.NewGuid(), name, index, 0, CurrentVersion, Enumerable.Repeat((ModSettings?)null, modCount).ToList(), [], []); } From aeccf2b1c60074a49ec01b6430d9806323c89079 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 14 Apr 2024 15:38:14 +0200 Subject: [PATCH 105/433] Update Submodules. --- OtterGui | 2 +- Penumbra.Api | 2 +- Penumbra.GameData | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/OtterGui b/OtterGui index a50d2aed..3460a817 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit a50d2aedbb7b2e37d30987bd0b3b96a832fdc0af +Subproject commit 3460a817fc5e01a6b60eb834c3c59031938388fc diff --git a/Penumbra.Api b/Penumbra.Api index 2f76f42e..0c8578cf 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit 2f76f42e54141258d89300aa78d42fb3f1878092 +Subproject commit 0c8578cfa12bf0591ed204fd89b30b66719f678f diff --git a/Penumbra.GameData b/Penumbra.GameData index 60222d79..fe9d563d 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 60222d79420662fb8e9960a66e262a380fcaf186 +Subproject commit fe9d563d9845630673cf098f7a6bfbd26e600fb4 From 0fa62f40d76c1fbbddf9f4c569a7195db811ee82 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 17 Apr 2024 16:57:23 +0200 Subject: [PATCH 106/433] Add Versions provider. --- Penumbra/Api/IpcProviders.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Penumbra/Api/IpcProviders.cs b/Penumbra/Api/IpcProviders.cs index cc98ef0d..21fe0a7c 100644 --- a/Penumbra/Api/IpcProviders.cs +++ b/Penumbra/Api/IpcProviders.cs @@ -61,6 +61,7 @@ public sealed class IpcProviders : IDisposable, IApiService IpcSubscribers.CopyModSettings.Provider(pi, api.ModSettings), IpcSubscribers.ApiVersion.Provider(pi, api), + new FuncProvider<(int Major, int Minor)>(pi, "Penumbra.ApiVersions", () => api.ApiVersion), // backward compatibility IpcSubscribers.GetModDirectory.Provider(pi, api.PluginState), IpcSubscribers.GetConfiguration.Provider(pi, api.PluginState), IpcSubscribers.ModDirectoryChanged.Provider(pi, api.PluginState), From 1641166d6e168d8b7e185ae0762e574cddf63201 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 17 Apr 2024 18:15:26 +0200 Subject: [PATCH 107/433] Disable IPC listeners by default. --- Penumbra/Api/IpcTester/GameStateIpcTester.cs | 7 +++++-- Penumbra/Api/IpcTester/ModSettingsIpcTester.cs | 1 + Penumbra/Api/IpcTester/ModsIpcTester.cs | 6 ++++++ Penumbra/Api/IpcTester/PluginStateIpcTester.cs | 2 ++ Penumbra/Api/IpcTester/RedrawingIpcTester.cs | 7 ++++--- Penumbra/Api/IpcTester/UiIpcTester.cs | 7 ++++++- 6 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Penumbra/Api/IpcTester/GameStateIpcTester.cs b/Penumbra/Api/IpcTester/GameStateIpcTester.cs index 2c41b882..93806162 100644 --- a/Penumbra/Api/IpcTester/GameStateIpcTester.cs +++ b/Penumbra/Api/IpcTester/GameStateIpcTester.cs @@ -33,9 +33,12 @@ public class GameStateIpcTester : IUiService, IDisposable public GameStateIpcTester(DalamudPluginInterface pi) { _pi = pi; - CharacterBaseCreating = CreatingCharacterBase.Subscriber(pi, UpdateLastCreated); - CharacterBaseCreated = CreatedCharacterBase.Subscriber(pi, UpdateLastCreated2); + CharacterBaseCreating = IpcSubscribers.CreatingCharacterBase.Subscriber(pi, UpdateLastCreated); + CharacterBaseCreated = IpcSubscribers.CreatedCharacterBase.Subscriber(pi, UpdateLastCreated2); GameObjectResourcePathResolved = IpcSubscribers.GameObjectResourcePathResolved.Subscriber(pi, UpdateGameObjectResourcePath); + CharacterBaseCreating.Disable(); + CharacterBaseCreated.Disable(); + GameObjectResourcePathResolved.Disable(); } public void Dispose() diff --git a/Penumbra/Api/IpcTester/ModSettingsIpcTester.cs b/Penumbra/Api/IpcTester/ModSettingsIpcTester.cs index c33fcdee..b117d603 100644 --- a/Penumbra/Api/IpcTester/ModSettingsIpcTester.cs +++ b/Penumbra/Api/IpcTester/ModSettingsIpcTester.cs @@ -37,6 +37,7 @@ public class ModSettingsIpcTester : IUiService, IDisposable { _pi = pi; SettingChanged = ModSettingChanged.Subscriber(pi, UpdateLastModSetting); + SettingChanged.Disable(); } public void Dispose() diff --git a/Penumbra/Api/IpcTester/ModsIpcTester.cs b/Penumbra/Api/IpcTester/ModsIpcTester.cs index 878a8214..43f397e5 100644 --- a/Penumbra/Api/IpcTester/ModsIpcTester.cs +++ b/Penumbra/Api/IpcTester/ModsIpcTester.cs @@ -55,13 +55,19 @@ public class ModsIpcTester : IUiService, IDisposable _lastMovedModFrom = s1; _lastMovedModTo = s2; }); + DeleteSubscriber.Disable(); + AddSubscriber.Disable(); + MoveSubscriber.Disable(); } public void Dispose() { DeleteSubscriber.Dispose(); + DeleteSubscriber.Disable(); AddSubscriber.Dispose(); + AddSubscriber.Disable(); MoveSubscriber.Dispose(); + MoveSubscriber.Disable(); } public void Draw() diff --git a/Penumbra/Api/IpcTester/PluginStateIpcTester.cs b/Penumbra/Api/IpcTester/PluginStateIpcTester.cs index 0588e5bd..984f17b1 100644 --- a/Penumbra/Api/IpcTester/PluginStateIpcTester.cs +++ b/Penumbra/Api/IpcTester/PluginStateIpcTester.cs @@ -36,6 +36,8 @@ public class PluginStateIpcTester : IUiService, IDisposable Initialized = IpcSubscribers.Initialized.Subscriber(pi, AddInitialized); Disposed = IpcSubscribers.Disposed.Subscriber(pi, AddDisposed); EnabledChange = IpcSubscribers.EnabledChange.Subscriber(pi, SetLastEnabled); + ModDirectoryChanged.Disable(); + EnabledChange.Disable(); } public void Dispose() diff --git a/Penumbra/Api/IpcTester/RedrawingIpcTester.cs b/Penumbra/Api/IpcTester/RedrawingIpcTester.cs index 281c7ad4..801f0b97 100644 --- a/Penumbra/Api/IpcTester/RedrawingIpcTester.cs +++ b/Penumbra/Api/IpcTester/RedrawingIpcTester.cs @@ -22,9 +22,10 @@ public class RedrawingIpcTester : IUiService, IDisposable public RedrawingIpcTester(DalamudPluginInterface pi, ObjectManager objects) { - _pi = pi; - _objects = objects; - Redrawn = GameObjectRedrawn.Subscriber(_pi, SetLastRedrawn); + _pi = pi; + _objects = objects; + Redrawn = GameObjectRedrawn.Subscriber(_pi, SetLastRedrawn); + Redrawn.Disable(); } public void Dispose() diff --git a/Penumbra/Api/IpcTester/UiIpcTester.cs b/Penumbra/Api/IpcTester/UiIpcTester.cs index 29ddc22e..d95b79b8 100644 --- a/Penumbra/Api/IpcTester/UiIpcTester.cs +++ b/Penumbra/Api/IpcTester/UiIpcTester.cs @@ -5,7 +5,6 @@ using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Api.Helpers; using Penumbra.Api.IpcSubscribers; -using Penumbra.Communication; namespace Penumbra.Api.IpcTester; @@ -38,6 +37,12 @@ public class UiIpcTester : IUiService, IDisposable PostSettingsPanelDraw = IpcSubscribers.PostSettingsPanelDraw.Subscriber(pi, UpdateLastDrawnMod); ChangedItemTooltip = IpcSubscribers.ChangedItemTooltip.Subscriber(pi, AddedTooltip); ChangedItemClicked = IpcSubscribers.ChangedItemClicked.Subscriber(pi, AddedClick); + PreSettingsTabBar.Disable(); + PreSettingsPanel.Disable(); + PostEnabled.Disable(); + PostSettingsPanelDraw.Disable(); + ChangedItemTooltip.Disable(); + ChangedItemClicked.Disable(); } public void Dispose() From fd1f9b95d60b85d036f27addd3f7a965e815be75 Mon Sep 17 00:00:00 2001 From: ackwell Date: Thu, 18 Apr 2024 21:23:18 +1000 Subject: [PATCH 108/433] Add Single2 support for UVs --- Penumbra/Import/Models/Export/MeshExporter.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Penumbra/Import/Models/Export/MeshExporter.cs b/Penumbra/Import/Models/Export/MeshExporter.cs index d3ca87dc..c2562293 100644 --- a/Penumbra/Import/Models/Export/MeshExporter.cs +++ b/Penumbra/Import/Models/Export/MeshExporter.cs @@ -312,6 +312,7 @@ public class MeshExporter { return type switch { + MdlFile.VertexType.Single2 => new Vector2(reader.ReadSingle(), reader.ReadSingle()), MdlFile.VertexType.Single3 => new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()), MdlFile.VertexType.Single4 => new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()), MdlFile.VertexType.UByte4 => reader.ReadBytes(4), @@ -379,6 +380,7 @@ public class MeshExporter { MdlFile.VertexType.Half2 => 1, MdlFile.VertexType.Half4 => 2, + MdlFile.VertexType.Single2 => 1, MdlFile.VertexType.Single4 => 2, _ => throw _notifier.Exception($"Unexpected UV vertex type {type}."), }; From dbfaf37800f20c202f8d01a6dacf6348ecdb7a9f Mon Sep 17 00:00:00 2001 From: ackwell Date: Thu, 18 Apr 2024 21:47:07 +1000 Subject: [PATCH 109/433] Export to .glb --- Penumbra/Import/Models/ModelManager.cs | 2 +- Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Penumbra/Import/Models/ModelManager.cs b/Penumbra/Import/Models/ModelManager.cs index 1a52c4dd..485a76a7 100644 --- a/Penumbra/Import/Models/ModelManager.cs +++ b/Penumbra/Import/Models/ModelManager.cs @@ -213,7 +213,7 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect Penumbra.Log.Debug("[GLTF Export] Saving..."); var gltfModel = scene.ToGltf2(); - gltfModel.SaveGLTF(outputPath); + gltfModel.Save(outputPath); Penumbra.Log.Debug("[GLTF Export] Done."); } diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index 03f276ea..6cd9b912 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -145,8 +145,8 @@ public partial class ModEditWindow if (ImGuiUtil.DrawDisabledButton("Export to glTF", Vector2.Zero, "Exports this mdl file to glTF, for use in 3D authoring applications.", tab.PendingIo || gamePath.IsEmpty)) - _fileDialog.OpenSavePicker("Save model as glTF.", ".gltf", Path.GetFileNameWithoutExtension(gamePath.Filename().ToString()), - ".gltf", (valid, path) => + _fileDialog.OpenSavePicker("Save model as glTF.", ".glb", Path.GetFileNameWithoutExtension(gamePath.Filename().ToString()), + ".glb", (valid, path) => { if (!valid) return; From aeb7bd5431d0e3822010305ebeeb87de5b52604b Mon Sep 17 00:00:00 2001 From: ackwell Date: Fri, 19 Apr 2024 00:34:08 +1000 Subject: [PATCH 110/433] Ensure materials contain at least one / --- .../ModEditWindow.Models.MdlTab.cs | 13 ++++- .../UI/AdvancedWindow/ModEditWindow.Models.cs | 54 +++++++++++++------ 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs index cca8fe10..b8c0176a 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs @@ -49,7 +49,7 @@ public partial class ModEditWindow /// public bool Valid - => Mdl.Valid; + => Mdl.Valid && Mdl.Materials.All(ValidateMaterial); /// public byte[] Write() @@ -285,6 +285,17 @@ public partial class ModEditWindow : _edit._gameData.GetFile(resolvedPath.InternalName.ToString())?.Data; } + /// Validate the specified material. + /// + /// While materials can be relative (`/mt_...`) or absolute (`bg/...`), + /// they invariably must contain at least one directory seperator. + /// Missing this can lead to a crash. + /// + public bool ValidateMaterial(string material) + { + return material.Contains('/'); + } + /// Remove the material given by the index. /// Meshes using the removed material are redirected to material 0, and those after the index are corrected. public void RemoveMaterial(int materialIndex) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index 6cd9b912..1cfa7585 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -1,4 +1,5 @@ using Dalamud.Interface; +using Dalamud.Interface.Components; using ImGuiNET; using OtterGui; using OtterGui.Custom; @@ -295,7 +296,7 @@ public partial class ModEditWindow if (!ImGui.CollapsingHeader("Materials")) return false; - using var table = ImRaii.Table(string.Empty, disabled ? 2 : 3, ImGuiTableFlags.SizingFixedFit); + using var table = ImRaii.Table(string.Empty, disabled ? 2 : 4, ImGuiTableFlags.SizingFixedFit); if (!table) return false; @@ -305,7 +306,10 @@ public partial class ModEditWindow ImGui.TableSetupColumn("index", ImGuiTableColumnFlags.WidthFixed, 80 * UiHelpers.Scale); ImGui.TableSetupColumn("path", ImGuiTableColumnFlags.WidthStretch, 1); if (!disabled) + { ImGui.TableSetupColumn("actions", ImGuiTableColumnFlags.WidthFixed, UiHelpers.IconButtonSize.X); + ImGui.TableSetupColumn("help", ImGuiTableColumnFlags.WidthFixed, UiHelpers.IconButtonSize.X); + } var inputFlags = disabled ? ImGuiInputTextFlags.ReadOnly : ImGuiInputTextFlags.None; for (var materialIndex = 0; materialIndex < materials.Length; materialIndex++) @@ -321,12 +325,15 @@ public partial class ModEditWindow ImGui.InputTextWithHint("##newMaterial", "Add new material...", ref _modelNewMaterial, Utf8GamePath.MaxGamePathLength, inputFlags); var validName = _modelNewMaterial.Length > 0 && _modelNewMaterial[0] == '/'; ImGui.TableNextColumn(); - if (!ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), UiHelpers.IconButtonSize, string.Empty, !validName, true)) - return ret; + if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), UiHelpers.IconButtonSize, string.Empty, !validName, true)) + { + ret |= true; + tab.Mdl.Materials = materials.AddItem(_modelNewMaterial); + _modelNewMaterial = string.Empty; + } + ImGui.TableNextColumn(); - tab.Mdl.Materials = materials.AddItem(_modelNewMaterial); - _modelNewMaterial = string.Empty; - return true; + return ret; } private bool DrawMaterialRow(MdlTab tab, bool disabled, string[] materials, int materialIndex, ImGuiInputTextFlags inputFlags) @@ -353,20 +360,33 @@ public partial class ModEditWindow return ret; ImGui.TableNextColumn(); - // Need to have at least one material. - if (materials.Length <= 1) - return ret; + if (materials.Length > 1) + { + var tt = "Delete this material.\nAny meshes targeting this material will be updated to use material #1."; + var modifierActive = _config.DeleteModModifier.IsActive(); + if (!modifierActive) + tt += $"\nHold {_config.DeleteModModifier} to delete."; - var tt = "Delete this material.\nAny meshes targeting this material will be updated to use material #1."; - var modifierActive = _config.DeleteModModifier.IsActive(); - if (!modifierActive) - tt += $"\nHold {_config.DeleteModModifier} to delete."; - if (!ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Trash.ToIconString(), UiHelpers.IconButtonSize, tt, !modifierActive, true)) - return ret; + if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Trash.ToIconString(), UiHelpers.IconButtonSize, tt, !modifierActive, true)) + { + tab.RemoveMaterial(materialIndex); + ret |= true; + } + } - tab.RemoveMaterial(materialIndex); - return true; + ImGui.TableNextColumn(); + // Add markers to invalid materials. + if (!tab.ValidateMaterial(temp)) + using (var colorHandle = ImRaii.PushColor(ImGuiCol.TextDisabled, 0xFF0000FF, true)) + { + ImGuiComponents.HelpMarker( + "Materials must be either relative (e.g. \"/filename.mtrl\")\n" + + "or absolute (e.g. \"chara/full/path/to/filename.mtrl\").", + FontAwesomeIcon.TimesCircle); + } + + return ret; } private bool DrawModelLodDetails(MdlTab tab, int lodIndex, bool disabled) From ef1bbb6d9d8443791a0c4d534bf373f0e490e966 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 19 Apr 2024 15:40:12 +0200 Subject: [PATCH 111/433] I don't know what I'm doing --- Penumbra.GameData | 2 +- .../Import/Models/Export/MaterialExporter.cs | 3 +- Penumbra/Import/Models/Export/MeshExporter.cs | 19 +++++----- Penumbra/Import/Models/Import/MeshImporter.cs | 13 ++++--- .../Import/Models/Import/ModelImporter.cs | 34 +++++++++--------- .../LiveColorTablePreviewer.cs | 7 ++-- .../ModEditWindow.Materials.ColorTable.cs | 35 ++++++++++--------- .../ModEditWindow.Materials.MtrlTab.cs | 9 ++--- .../UI/AdvancedWindow/ModEditWindow.Models.cs | 2 +- 9 files changed, 65 insertions(+), 59 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index fe9d563d..845d1f99 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit fe9d563d9845630673cf098f7a6bfbd26e600fb4 +Subproject commit 845d1f99a752f4d23288a316e42d4bfa32fa987f diff --git a/Penumbra/Import/Models/Export/MaterialExporter.cs b/Penumbra/Import/Models/Export/MaterialExporter.cs index 2fa4e1b2..73a5e725 100644 --- a/Penumbra/Import/Models/Export/MaterialExporter.cs +++ b/Penumbra/Import/Models/Export/MaterialExporter.cs @@ -1,5 +1,6 @@ using Lumina.Data.Parsing; using Penumbra.GameData.Files; +using Penumbra.GameData.Files.MaterialStructs; using SharpGLTF.Materials; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Advanced; @@ -102,7 +103,7 @@ public class MaterialExporter // TODO: It feels a little silly to request the entire normal here when extracting the normal only needs some of the components. // As a future refactor, it would be neat to accept a single-channel field here, and then do composition of other stuff later. - private readonly struct ProcessCharacterNormalOperation(Image normal, MtrlFile.ColorTable table) : IRowOperation + private readonly struct ProcessCharacterNormalOperation(Image normal, ColorTable table) : IRowOperation { public Image Normal { get; } = normal.Clone(); public Image BaseColor { get; } = new(normal.Width, normal.Height); diff --git a/Penumbra/Import/Models/Export/MeshExporter.cs b/Penumbra/Import/Models/Export/MeshExporter.cs index d3ca87dc..f372f665 100644 --- a/Penumbra/Import/Models/Export/MeshExporter.cs +++ b/Penumbra/Import/Models/Export/MeshExporter.cs @@ -3,6 +3,7 @@ using Lumina.Data.Parsing; using Lumina.Extensions; using OtterGui; using Penumbra.GameData.Files; +using Penumbra.GameData.Files.ModelStructs; using SharpGLTF.Geometry; using SharpGLTF.Geometry.VertexTypes; using SharpGLTF.IO; @@ -55,7 +56,7 @@ public class MeshExporter private readonly byte _lod; private readonly ushort _meshIndex; - private MdlStructs.MeshStruct XivMesh + private MeshStruct XivMesh => _mdl.Meshes[_meshIndex]; private readonly MaterialBuilder _material; @@ -109,8 +110,8 @@ public class MeshExporter var xivBoneTable = _mdl.BoneTables[XivMesh.BoneTableIndex]; var indexMap = new Dictionary(); - - foreach (var (xivBoneIndex, tableIndex) in xivBoneTable.BoneIndex.Take(xivBoneTable.BoneCount).WithIndex()) + // #TODO @ackwell maybe fix for V6 Models, I think this works fine. + foreach (var (xivBoneIndex, tableIndex) in xivBoneTable.BoneIndex.Take((int)xivBoneTable.BoneCount).WithIndex()) { var boneName = _mdl.Bones[xivBoneIndex]; if (!skeleton.Names.TryGetValue(boneName, out var gltfBoneIndex)) @@ -238,19 +239,15 @@ public class MeshExporter { "targetNames", shapeNames }, }); - string[] attributes = []; - var maxAttribute = 31 - BitOperations.LeadingZeroCount(attributeMask); + string[] attributes = []; + var maxAttribute = 31 - BitOperations.LeadingZeroCount(attributeMask); if (maxAttribute < _mdl.Attributes.Length) - { attributes = Enumerable.Range(0, 32) .Where(index => ((attributeMask >> index) & 1) == 1) .Select(index => _mdl.Attributes[index]) .ToArray(); - } else - { _notifier.Warning("Invalid attribute data, ignoring."); - } return new MeshData { @@ -278,7 +275,7 @@ public class MeshExporter for (var streamIndex = 0; streamIndex < MaximumMeshBufferStreams; streamIndex++) { streams[streamIndex] = new BinaryReader(new MemoryStream(_mdl.RemainingData)); - streams[streamIndex].Seek(_mdl.VertexOffset[_lod] + XivMesh.VertexBufferOffset[streamIndex]); + streams[streamIndex].Seek(_mdl.VertexOffset[_lod] + XivMesh.VertexBufferOffset(streamIndex)); } var sortedElements = _mdl.VertexDeclarations[_meshIndex].VertexElements @@ -315,7 +312,7 @@ public class MeshExporter MdlFile.VertexType.Single3 => new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()), MdlFile.VertexType.Single4 => new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()), MdlFile.VertexType.UByte4 => reader.ReadBytes(4), - MdlFile.VertexType.NByte4 => new Vector4(reader.ReadByte() / 255f, reader.ReadByte() / 255f, reader.ReadByte() / 255f, + MdlFile.VertexType.NByte4 => new Vector4(reader.ReadByte() / 255f, reader.ReadByte() / 255f, reader.ReadByte() / 255f, reader.ReadByte() / 255f), MdlFile.VertexType.Half2 => new Vector2((float)reader.ReadHalf(), (float)reader.ReadHalf()), MdlFile.VertexType.Half4 => new Vector4((float)reader.ReadHalf(), (float)reader.ReadHalf(), (float)reader.ReadHalf(), diff --git a/Penumbra/Import/Models/Import/MeshImporter.cs b/Penumbra/Import/Models/Import/MeshImporter.cs index 1d4b223d..3a11cb04 100644 --- a/Penumbra/Import/Models/Import/MeshImporter.cs +++ b/Penumbra/Import/Models/Import/MeshImporter.cs @@ -1,5 +1,6 @@ using Lumina.Data.Parsing; using OtterGui; +using Penumbra.GameData.Files.ModelStructs; using SharpGLTF.Schema2; namespace Penumbra.Import.Models.Import; @@ -8,7 +9,7 @@ public class MeshImporter(IEnumerable nodes, IoNotifier notifier) { public struct Mesh { - public MdlStructs.MeshStruct MeshStruct; + public MeshStruct MeshStruct; public List SubMeshStructs; public string? Material; @@ -69,10 +70,14 @@ public class MeshImporter(IEnumerable nodes, IoNotifier notifier) return new Mesh { - MeshStruct = new MdlStructs.MeshStruct + MeshStruct = new MeshStruct { - VertexBufferOffset = [0, (uint)_streams[0].Count, (uint)(_streams[0].Count + _streams[1].Count)], - VertexBufferStride = _strides, + VertexBufferOffset1 = 0, + VertexBufferOffset2 = (uint)_streams[0].Count, + VertexBufferOffset3 = (uint)(_streams[0].Count + _streams[1].Count), + VertexBufferStride1 = _strides[0], + VertexBufferStride2 = _strides[1], + VertexBufferStride3 = _strides[2], VertexCount = _vertexCount, VertexStreamCount = (byte)_vertexDeclaration.Value.VertexElements .Select(element => element.Stream + 1) diff --git a/Penumbra/Import/Models/Import/ModelImporter.cs b/Penumbra/Import/Models/Import/ModelImporter.cs index 8f917b0e..eedd12ab 100644 --- a/Penumbra/Import/Models/Import/ModelImporter.cs +++ b/Penumbra/Import/Models/Import/ModelImporter.cs @@ -1,6 +1,7 @@ using Lumina.Data.Parsing; using OtterGui; using Penumbra.GameData.Files; +using Penumbra.GameData.Files.ModelStructs; using SharpGLTF.Schema2; namespace Penumbra.Import.Models.Import; @@ -14,10 +15,11 @@ public partial class ModelImporter(ModelRoot model, IoNotifier notifier) } // NOTE: This is intended to match TexTool's grouping regex, ".*[_ ^]([0-9]+)[\\.\\-]?([0-9]+)?$" - [GeneratedRegex(@"[_ ^](?'Mesh'[0-9]+)[.-]?(?'SubMesh'[0-9]+)?$", RegexOptions.Compiled | RegexOptions.NonBacktracking | RegexOptions.ExplicitCapture)] + [GeneratedRegex(@"[_ ^](?'Mesh'[0-9]+)[.-]?(?'SubMesh'[0-9]+)?$", + RegexOptions.Compiled | RegexOptions.NonBacktracking | RegexOptions.ExplicitCapture)] private static partial Regex MeshNameGroupingRegex(); - private readonly List _meshes = []; + private readonly List _meshes = []; private readonly List _subMeshes = []; private readonly List _materials = []; @@ -27,10 +29,10 @@ public partial class ModelImporter(ModelRoot model, IoNotifier notifier) private readonly List _indices = []; - private readonly List _bones = []; - private readonly List _boneTables = []; + private readonly List _bones = []; + private readonly List _boneTables = []; - private readonly BoundingBox _boundingBox = new BoundingBox(); + private readonly BoundingBox _boundingBox = new(); private readonly List _metaAttributes = []; @@ -95,9 +97,7 @@ public partial class ModelImporter(ModelRoot model, IoNotifier notifier) IndexBufferSize = (uint)indexBuffer.Length, }, ], - - Materials = [.. materials], - + Materials = [.. materials], BoundingBoxes = _boundingBox.ToStruct(), // TODO: Would be good to calculate all of this up the tree. @@ -132,9 +132,9 @@ public partial class ModelImporter(ModelRoot model, IoNotifier notifier) private void BuildMeshForGroup(IEnumerable subMeshNodes, int index) { // Record some offsets we'll be using later, before they get mutated with mesh values. - var subMeshOffset = _subMeshes.Count; - var vertexOffset = _vertexBuffer.Count; - var indexOffset = _indices.Count; + var subMeshOffset = _subMeshes.Count; + var vertexOffset = _vertexBuffer.Count; + var indexOffset = _indices.Count; var mesh = MeshImporter.Import(subMeshNodes, notifier.WithContext($"Mesh {index}")); var meshStartIndex = (uint)(mesh.MeshStruct.StartIndex + indexOffset); @@ -154,9 +154,9 @@ public partial class ModelImporter(ModelRoot model, IoNotifier notifier) SubMeshIndex = (ushort)(mesh.MeshStruct.SubMeshIndex + subMeshOffset), BoneTableIndex = boneTableIndex, StartIndex = meshStartIndex, - VertexBufferOffset = mesh.MeshStruct.VertexBufferOffset - .Select(offset => (uint)(offset + vertexOffset)) - .ToArray(), + VertexBufferOffset1 = (uint)(mesh.MeshStruct.VertexBufferOffset1 + vertexOffset), + VertexBufferOffset2 = (uint)(mesh.MeshStruct.VertexBufferOffset2 + vertexOffset), + VertexBufferOffset3 = (uint)(mesh.MeshStruct.VertexBufferOffset3 + vertexOffset), }); _boundingBox.Merge(mesh.BoundingBox); @@ -196,7 +196,8 @@ public partial class ModelImporter(ModelRoot model, IoNotifier notifier) // arrays, values is practically guaranteed to be the highest of the // group, so a failure on any of them will be a failure on it. if (_shapeValues.Count > ushort.MaxValue) - throw notifier.Exception($"Importing this file would require more than the maximum of {ushort.MaxValue} shape values.\nTry removing or applying shape keys that do not need to be changed at runtime in-game."); + throw notifier.Exception( + $"Importing this file would require more than the maximum of {ushort.MaxValue} shape values.\nTry removing or applying shape keys that do not need to be changed at runtime in-game."); } private ushort GetMaterialIndex(string materialName) @@ -216,6 +217,7 @@ public partial class ModelImporter(ModelRoot model, IoNotifier notifier) return (ushort)count; } + // #TODO @ackwell fix for V6 Models private ushort BuildBoneTable(List boneNames) { var boneIndices = new List(); @@ -238,7 +240,7 @@ public partial class ModelImporter(ModelRoot model, IoNotifier notifier) Array.Copy(boneIndices.ToArray(), boneIndicesArray, boneIndices.Count); var boneTableIndex = _boneTables.Count; - _boneTables.Add(new MdlStructs.BoneTableStruct() + _boneTables.Add(new BoneTableStruct() { BoneIndex = boneIndicesArray, BoneCount = (byte)boneIndices.Count, diff --git a/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs b/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs index 4d35e68a..f211e0bc 100644 --- a/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs +++ b/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs @@ -1,6 +1,5 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Graphics.Kernel; -using Penumbra.GameData.Files; using Penumbra.GameData.Interop; using Penumbra.Interop.SafeHandles; @@ -9,7 +8,7 @@ namespace Penumbra.Interop.MaterialPreview; public sealed unsafe class LiveColorTablePreviewer : LiveMaterialPreviewerBase { public const int TextureWidth = 4; - public const int TextureHeight = MtrlFile.ColorTable.NumRows; + public const int TextureHeight = GameData.Files.MaterialStructs.ColorTable.NumUsedRows; public const int TextureLength = TextureWidth * TextureHeight * 4; private readonly IFramework _framework; @@ -17,7 +16,7 @@ public sealed unsafe class LiveColorTablePreviewer : LiveMaterialPreviewerBase private readonly Texture** _colorTableTexture; private readonly SafeTextureHandle _originalColorTableTexture; - private bool _updatePending; + private bool _updatePending; public Half[] ColorTable { get; } @@ -40,7 +39,7 @@ public sealed unsafe class LiveColorTablePreviewer : LiveMaterialPreviewerBase if (_originalColorTableTexture == null) throw new InvalidOperationException("Material doesn't have a color table"); - ColorTable = new Half[TextureLength]; + ColorTable = new Half[TextureLength]; _updatePending = true; framework.Update += OnFrameworkUpdate; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs index a4e25f77..54c0eff6 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs @@ -4,6 +4,7 @@ using ImGuiNET; using OtterGui; using OtterGui.Raii; using Penumbra.GameData.Files; +using Penumbra.GameData.Files.MaterialStructs; using Penumbra.String.Functions; namespace Penumbra.UI.AdvancedWindow; @@ -74,7 +75,7 @@ public partial class ModEditWindow ImGui.TableHeader("Dye Preview"); } - for (var i = 0; i < MtrlFile.ColorTable.NumRows; ++i) + for (var i = 0; i < ColorTable.NumUsedRows; ++i) { ret |= DrawColorTableRow(tab, i, disabled); ImGui.TableNextRow(); @@ -115,8 +116,8 @@ public partial class ModEditWindow { var ret = false; if (tab.Mtrl.HasDyeTable) - for (var i = 0; i < MtrlFile.ColorTable.NumRows; ++i) - ret |= tab.Mtrl.ApplyDyeTemplate(_stainService.StmFile, i, dyeId); + for (var i = 0; i < ColorTable.NumUsedRows; ++i) + ret |= tab.Mtrl.ApplyDyeTemplate(_stainService.StmFile, i, dyeId, 0); tab.UpdateColorTablePreview(); @@ -140,21 +141,21 @@ public partial class ModEditWindow { var text = ImGui.GetClipboardText(); var data = Convert.FromBase64String(text); - if (data.Length < Marshal.SizeOf()) + if (data.Length < Marshal.SizeOf()) return false; ref var rows = ref tab.Mtrl.Table; fixed (void* ptr = data, output = &rows) { - MemoryUtility.MemCpyUnchecked(output, ptr, Marshal.SizeOf()); - if (data.Length >= Marshal.SizeOf() + Marshal.SizeOf() + MemoryUtility.MemCpyUnchecked(output, ptr, Marshal.SizeOf()); + if (data.Length >= Marshal.SizeOf() + Marshal.SizeOf() && tab.Mtrl.HasDyeTable) { ref var dyeRows = ref tab.Mtrl.DyeTable; fixed (void* output2 = &dyeRows) { - MemoryUtility.MemCpyUnchecked(output2, (byte*)ptr + Marshal.SizeOf(), - Marshal.SizeOf()); + MemoryUtility.MemCpyUnchecked(output2, (byte*)ptr + Marshal.SizeOf(), + Marshal.SizeOf()); } } } @@ -169,7 +170,7 @@ public partial class ModEditWindow } } - private static unsafe void ColorTableCopyClipboardButton(MtrlFile.ColorTable.Row row, MtrlFile.ColorDyeTable.Row dye) + private static unsafe void ColorTableCopyClipboardButton(ColorTable.Row row, ColorDyeTable.Row dye) { if (!ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Clipboard.ToIconString(), ImGui.GetFrameHeight() * Vector2.One, "Export this row to your clipboard.", false, true)) @@ -177,11 +178,11 @@ public partial class ModEditWindow try { - var data = new byte[MtrlFile.ColorTable.Row.Size + 2]; + var data = new byte[ColorTable.Row.Size + 2]; fixed (byte* ptr = data) { - MemoryUtility.MemCpyUnchecked(ptr, &row, MtrlFile.ColorTable.Row.Size); - MemoryUtility.MemCpyUnchecked(ptr + MtrlFile.ColorTable.Row.Size, &dye, 2); + MemoryUtility.MemCpyUnchecked(ptr, &row, ColorTable.Row.Size); + MemoryUtility.MemCpyUnchecked(ptr + ColorTable.Row.Size, &dye, 2); } var text = Convert.ToBase64String(data); @@ -217,15 +218,15 @@ public partial class ModEditWindow { var text = ImGui.GetClipboardText(); var data = Convert.FromBase64String(text); - if (data.Length != MtrlFile.ColorTable.Row.Size + 2 + if (data.Length != ColorTable.Row.Size + 2 || !tab.Mtrl.HasTable) return false; fixed (byte* ptr = data) { - tab.Mtrl.Table[rowIdx] = *(MtrlFile.ColorTable.Row*)ptr; + tab.Mtrl.Table[rowIdx] = *(ColorTable.Row*)ptr; if (tab.Mtrl.HasDyeTable) - tab.Mtrl.DyeTable[rowIdx] = *(MtrlFile.ColorDyeTable.Row*)(ptr + MtrlFile.ColorTable.Row.Size); + tab.Mtrl.DyeTable[rowIdx] = *(ColorDyeTable.Row*)(ptr + ColorTable.Row.Size); } tab.UpdateColorTableRowPreview(rowIdx); @@ -451,7 +452,7 @@ public partial class ModEditWindow return ret; } - private bool DrawDyePreview(MtrlTab tab, int rowIdx, bool disabled, MtrlFile.ColorDyeTable.Row dye, float floatSize) + private bool DrawDyePreview(MtrlTab tab, int rowIdx, bool disabled, ColorDyeTable.Row dye, float floatSize) { var stain = _stainService.StainCombo.CurrentSelection.Key; if (stain == 0 || !_stainService.StmFile.Entries.TryGetValue(dye.Template, out var entry)) @@ -463,7 +464,7 @@ public partial class ModEditWindow var ret = ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.PaintBrush.ToIconString(), new Vector2(ImGui.GetFrameHeight()), "Apply the selected dye to this row.", disabled, true); - ret = ret && tab.Mtrl.ApplyDyeTemplate(_stainService.StmFile, rowIdx, stain); + ret = ret && tab.Mtrl.ApplyDyeTemplate(_stainService.StmFile, rowIdx, stain, 0); if (ret) tab.UpdateColorTableRowPreview(rowIdx); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs index b4801f5f..9421493e 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs @@ -8,6 +8,7 @@ using OtterGui.Classes; using OtterGui.Raii; using Penumbra.GameData.Data; using Penumbra.GameData.Files; +using Penumbra.GameData.Files.MaterialStructs; using Penumbra.GameData.Structs; using Penumbra.Interop.Hooks.Objects; using Penumbra.Interop.MaterialPreview; @@ -601,7 +602,7 @@ public partial class ModEditWindow var stm = _edit._stainService.StmFile; var dye = Mtrl.DyeTable[rowIdx]; if (stm.TryGetValue(dye.Template, _edit._stainService.StainCombo.CurrentSelection.Key, out var dyes)) - row.ApplyDyeTemplate(dye, dyes); + row.ApplyDyeTemplate(dye, dyes, default); } if (HighlightedColorTableRow == rowIdx) @@ -628,12 +629,12 @@ public partial class ModEditWindow { var stm = _edit._stainService.StmFile; var stainId = (StainId)_edit._stainService.StainCombo.CurrentSelection.Key; - for (var i = 0; i < MtrlFile.ColorTable.NumRows; ++i) + for (var i = 0; i < ColorTable.NumUsedRows; ++i) { ref var row = ref rows[i]; var dye = Mtrl.DyeTable[i]; if (stm.TryGetValue(dye.Template, stainId, out var dyes)) - row.ApplyDyeTemplate(dye, dyes); + row.ApplyDyeTemplate(dye, dyes, default); } } @@ -647,7 +648,7 @@ public partial class ModEditWindow } } - private static void ApplyHighlight(ref MtrlFile.ColorTable.Row row, float time) + private static void ApplyHighlight(ref ColorTable.Row row, float time) { var level = (MathF.Sin(time * 2.0f * MathF.PI) + 2.0f) / 3.0f / 255.0f; var baseColor = ColorId.InGameHighlight.Value(); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index 03f276ea..80b1a5d5 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -483,7 +483,7 @@ public partial class ModEditWindow if (table) { ImGuiUtil.DrawTableColumn("Version"); - ImGuiUtil.DrawTableColumn(_lastFile.Version.ToString()); + ImGuiUtil.DrawTableColumn($"0x{_lastFile.Version:X}"); ImGuiUtil.DrawTableColumn("Radius"); ImGuiUtil.DrawTableColumn(_lastFile.Radius.ToString(CultureInfo.InvariantCulture)); ImGuiUtil.DrawTableColumn("Model Clip Out Distance"); From 624dd40d58bc3817c3fbd271564042c58ad72439 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 19 Apr 2024 15:46:52 +0200 Subject: [PATCH 112/433] Handle writing. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 845d1f99..aff136e2 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 845d1f99a752f4d23288a316e42d4bfa32fa987f +Subproject commit aff136e2ff79990989cbe1c518a79b7b83e294a5 From 75cfffeba73e80f970d617e7d36622e709e444c3 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 19 Apr 2024 15:51:23 +0200 Subject: [PATCH 113/433] Oops. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index aff136e2..9208c9c2 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit aff136e2ff79990989cbe1c518a79b7b83e294a5 +Subproject commit 9208c9c242244beeb3c1fb826582d72da09831af From ceb3d39a9ac71373acc3e2d33623876488c872e8 Mon Sep 17 00:00:00 2001 From: ackwell Date: Sat, 20 Apr 2024 01:22:03 +1000 Subject: [PATCH 114/433] Normalise _FFXIV_COLOR values Fixes xivdev/Penumbra#411 --- Penumbra/Import/Models/Export/VertexFragment.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Penumbra/Import/Models/Export/VertexFragment.cs b/Penumbra/Import/Models/Export/VertexFragment.cs index 08b2a214..7a82e994 100644 --- a/Penumbra/Import/Models/Export/VertexFragment.cs +++ b/Penumbra/Import/Models/Export/VertexFragment.cs @@ -12,7 +12,7 @@ and there's reason to overhaul the export pipeline. public struct VertexColorFfxiv : IVertexCustom { // NOTE: We only realistically require UNSIGNED_BYTE for this, however Blender 3.6 errors on that (fixed in 4.0). - [VertexAttribute("_FFXIV_COLOR", EncodingType.UNSIGNED_SHORT, false)] + [VertexAttribute("_FFXIV_COLOR", EncodingType.UNSIGNED_SHORT, true)] public Vector4 FfxivColor; public int MaxColors => 0; @@ -81,7 +81,7 @@ public struct VertexTexture1ColorFfxiv : IVertexCustom [VertexAttribute("TEXCOORD_0")] public Vector2 TexCoord0; - [VertexAttribute("_FFXIV_COLOR", EncodingType.UNSIGNED_SHORT, false)] + [VertexAttribute("_FFXIV_COLOR", EncodingType.UNSIGNED_SHORT, true)] public Vector4 FfxivColor; public int MaxColors => 0; @@ -163,7 +163,7 @@ public struct VertexTexture2ColorFfxiv : IVertexCustom [VertexAttribute("TEXCOORD_1")] public Vector2 TexCoord1; - [VertexAttribute("_FFXIV_COLOR", EncodingType.UNSIGNED_SHORT, false)] + [VertexAttribute("_FFXIV_COLOR", EncodingType.UNSIGNED_SHORT, true)] public Vector4 FfxivColor; public int MaxColors => 0; From 8fc7de64d9d23c9874861567dd6ada6a0f246d57 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 19 Apr 2024 17:55:28 +0200 Subject: [PATCH 115/433] Start group rework. --- Penumbra/Collections/Cache/CollectionCache.cs | 50 +++++-------------- .../Collections/Cache/CollectionModData.cs | 10 ++-- Penumbra/Mods/Editor/IMod.cs | 14 +++++- Penumbra/Mods/Mod.cs | 19 +++++++ Penumbra/Mods/Subclasses/IModGroup.cs | 4 ++ Penumbra/Mods/Subclasses/ISubMod.cs | 10 ++++ Penumbra/Mods/Subclasses/MultiModGroup.cs | 11 ++++ Penumbra/Mods/Subclasses/SingleModGroup.cs | 5 ++ Penumbra/Mods/TemporaryMod.cs | 28 ++++++++++- 9 files changed, 106 insertions(+), 45 deletions(-) diff --git a/Penumbra/Collections/Cache/CollectionCache.cs b/Penumbra/Collections/Cache/CollectionCache.cs index e1b32204..ded1dc73 100644 --- a/Penumbra/Collections/Cache/CollectionCache.cs +++ b/Penumbra/Collections/Cache/CollectionCache.cs @@ -2,12 +2,10 @@ using OtterGui; using OtterGui.Classes; using Penumbra.Meta.Manipulations; using Penumbra.Mods; -using Penumbra.Api.Enums; using Penumbra.Communication; using Penumbra.Mods.Editor; using Penumbra.String.Classes; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; namespace Penumbra.Collections.Cache; @@ -231,37 +229,12 @@ public sealed class CollectionCache : IDisposable /// Add all files and possibly manipulations of a given mod according to its settings in this collection. internal void AddModSync(IMod mod, bool addMetaChanges) { - if (mod.Index >= 0) - { - var settings = _collection[mod.Index].Settings; - if (settings is not { Enabled: true }) - return; + var files = GetFiles(mod); + foreach (var (path, file) in files.FileRedirections) + AddFile(path, file, mod); - foreach (var (group, groupIndex) in mod.Groups.WithIndex().OrderByDescending(g => g.Value.Priority)) - { - if (group.Count == 0) - continue; - - var config = settings.Settings[groupIndex]; - switch (group) - { - case SingleModGroup single: - AddSubMod(single[config.AsIndex], mod); - break; - case MultiModGroup multi: - { - foreach (var (option, _) in multi.WithIndex() - .Where(p => config.HasFlag(p.Index)) - .OrderByDescending(p => group.OptionPriority(p.Index))) - AddSubMod(option, mod); - - break; - } - } - } - } - - AddSubMod(mod.Default, mod); + foreach (var manip in files.Manipulations) + AddManipulation(manip, mod); if (addMetaChanges) { @@ -273,14 +246,15 @@ public sealed class CollectionCache : IDisposable } } - // Add all files and possibly manipulations of a specific submod - private void AddSubMod(ISubMod subMod, IMod parentMod) + private AppliedModData GetFiles(IMod mod) { - foreach (var (path, file) in subMod.Files.Concat(subMod.FileSwaps)) - AddFile(path, file, parentMod); + if (mod.Index < 0) + return mod.GetData(); - foreach (var manip in subMod.Manipulations) - AddManipulation(manip, parentMod); + var settings = _collection[mod.Index].Settings; + return settings is not { Enabled: true } + ? AppliedModData.Empty + : mod.GetData(settings); } /// Invoke only if not in a full recalculation. diff --git a/Penumbra/Collections/Cache/CollectionModData.cs b/Penumbra/Collections/Cache/CollectionModData.cs index 3a3afad2..d0a3bc76 100644 --- a/Penumbra/Collections/Cache/CollectionModData.cs +++ b/Penumbra/Collections/Cache/CollectionModData.cs @@ -1,10 +1,12 @@ using Penumbra.Meta.Manipulations; -using Penumbra.Mods; using Penumbra.Mods.Editor; using Penumbra.String.Classes; namespace Penumbra.Collections.Cache; +/// +/// Contains associations between a mod and the paths and meta manipulations affected by that mod. +/// public class CollectionModData { private readonly Dictionary, HashSet)> _data = new(); @@ -17,7 +19,7 @@ public class CollectionModData if (_data.Remove(mod, out var data)) return data; - return (Array.Empty(), Array.Empty()); + return ([], []); } public void AddPath(IMod mod, Utf8GamePath path) @@ -28,7 +30,7 @@ public class CollectionModData } else { - data = (new HashSet { path }, new HashSet()); + data = ([path], []); _data.Add(mod, data); } } @@ -41,7 +43,7 @@ public class CollectionModData } else { - data = (new HashSet(), new HashSet { manipulation }); + data = ([], [manipulation]); _data.Add(mod, data); } } diff --git a/Penumbra/Mods/Editor/IMod.cs b/Penumbra/Mods/Editor/IMod.cs index d3bc19b0..8b5b65e1 100644 --- a/Penumbra/Mods/Editor/IMod.cs +++ b/Penumbra/Mods/Editor/IMod.cs @@ -1,15 +1,27 @@ using OtterGui.Classes; +using Penumbra.Meta.Manipulations; using Penumbra.Mods.Subclasses; +using Penumbra.String.Classes; namespace Penumbra.Mods.Editor; +public record struct AppliedModData( + IReadOnlyCollection> FileRedirections, + IReadOnlyCollection Manipulations) +{ + public static readonly AppliedModData Empty = new([], []); +} + public interface IMod { LowerString Name { get; } - public int Index { get; } + public int Index { get; } public ModPriority Priority { get; } + public AppliedModData GetData(ModSettings? settings = null); + + public ISubMod Default { get; } public IReadOnlyList Groups { get; } diff --git a/Penumbra/Mods/Mod.cs b/Penumbra/Mods/Mod.cs index b7d1186d..3c996c8f 100644 --- a/Penumbra/Mods/Mod.cs +++ b/Penumbra/Mods/Mod.cs @@ -1,5 +1,7 @@ using OtterGui; using OtterGui.Classes; +using Penumbra.Collections.Cache; +using Penumbra.Meta.Manipulations; using Penumbra.Mods.Editor; using Penumbra.Mods.Subclasses; using Penumbra.String.Classes; @@ -59,6 +61,23 @@ public sealed class Mod : IMod public readonly SubMod Default; public readonly List Groups = []; + public AppliedModData GetData(ModSettings? settings = null) + { + if (settings is not { Enabled: true }) + return AppliedModData.Empty; + + var dictRedirections = new Dictionary(TotalFileCount); + var setManips = new HashSet(TotalManipulations); + foreach (var (group, groupIndex) in Groups.WithIndex().OrderByDescending(g => g.Value.Priority)) + { + var config = settings.Settings[groupIndex]; + group.AddData(config, dictRedirections, setManips); + } + + ((ISubMod)Default).AddData(dictRedirections, setManips); + return new AppliedModData(dictRedirections, setManips); + } + ISubMod IMod.Default => Default; diff --git a/Penumbra/Mods/Subclasses/IModGroup.cs b/Penumbra/Mods/Subclasses/IModGroup.cs index 2daf31e6..57ef4e98 100644 --- a/Penumbra/Mods/Subclasses/IModGroup.cs +++ b/Penumbra/Mods/Subclasses/IModGroup.cs @@ -1,6 +1,8 @@ using Newtonsoft.Json; using Penumbra.Api.Enums; +using Penumbra.Meta.Manipulations; using Penumbra.Services; +using Penumbra.String.Classes; namespace Penumbra.Mods.Subclasses; @@ -24,6 +26,8 @@ public interface IModGroup : IReadOnlyCollection public bool MoveOption(int optionIdxFrom, int optionIdxTo); public void UpdatePositions(int from = 0); + public void AddData(Setting setting, Dictionary redirections, HashSet manipulations); + /// Ensure that a value is valid for a group. public Setting FixSetting(Setting setting); } diff --git a/Penumbra/Mods/Subclasses/ISubMod.cs b/Penumbra/Mods/Subclasses/ISubMod.cs index 29323c1d..e997e07d 100644 --- a/Penumbra/Mods/Subclasses/ISubMod.cs +++ b/Penumbra/Mods/Subclasses/ISubMod.cs @@ -14,6 +14,16 @@ public interface ISubMod public IReadOnlyDictionary FileSwaps { get; } public IReadOnlySet Manipulations { get; } + public void AddData(Dictionary redirections, HashSet manipulations) + { + foreach (var (path, file) in Files) + redirections.TryAdd(path, file); + + foreach (var (path, file) in FileSwaps) + redirections.TryAdd(path, file); + manipulations.UnionWith(Manipulations); + } + public bool IsDefault { get; } public static void WriteSubMod(JsonWriter j, JsonSerializer serializer, ISubMod mod, DirectoryInfo basePath, ModPriority? priority) diff --git a/Penumbra/Mods/Subclasses/MultiModGroup.cs b/Penumbra/Mods/Subclasses/MultiModGroup.cs index 7479cd54..266d3037 100644 --- a/Penumbra/Mods/Subclasses/MultiModGroup.cs +++ b/Penumbra/Mods/Subclasses/MultiModGroup.cs @@ -5,6 +5,8 @@ using OtterGui; using OtterGui.Classes; using OtterGui.Filesystem; using Penumbra.Api.Enums; +using Penumbra.Meta.Manipulations; +using Penumbra.String.Classes; namespace Penumbra.Mods.Subclasses; @@ -110,6 +112,15 @@ public sealed class MultiModGroup : IModGroup o.SetPosition(o.GroupIdx, i); } + public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) + { + foreach (var (option, index) in PrioritizedOptions.WithIndex().OrderByDescending(o => o.Value.Priority)) + { + if (setting.HasFlag(index)) + ((ISubMod)option.Mod).AddData(redirections, manipulations); + } + } + public Setting FixSetting(Setting setting) => new(setting.Value & ((1ul << Count) - 1)); } diff --git a/Penumbra/Mods/Subclasses/SingleModGroup.cs b/Penumbra/Mods/Subclasses/SingleModGroup.cs index 74769c7e..f797a709 100644 --- a/Penumbra/Mods/Subclasses/SingleModGroup.cs +++ b/Penumbra/Mods/Subclasses/SingleModGroup.cs @@ -3,6 +3,8 @@ using Newtonsoft.Json.Linq; using OtterGui; using OtterGui.Filesystem; using Penumbra.Api.Enums; +using Penumbra.Meta.Manipulations; +using Penumbra.String.Classes; namespace Penumbra.Mods.Subclasses; @@ -114,6 +116,9 @@ public sealed class SingleModGroup : IModGroup o.SetPosition(o.GroupIdx, i); } + public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) + => this[setting.AsIndex].AddData(redirections, manipulations); + public Setting FixSetting(Setting setting) => Count == 0 ? Setting.Zero : new Setting(Math.Min(setting.Value, (ulong)(Count - 1))); } diff --git a/Penumbra/Mods/TemporaryMod.cs b/Penumbra/Mods/TemporaryMod.cs index 6be07881..8f27e201 100644 --- a/Penumbra/Mods/TemporaryMod.cs +++ b/Penumbra/Mods/TemporaryMod.cs @@ -20,6 +20,28 @@ public class TemporaryMod : IMod public readonly SubMod Default; + public AppliedModData GetData(ModSettings? settings = null) + { + Dictionary dict; + if (Default.FileSwapData.Count == 0) + { + dict = Default.FileData; + } + else if (Default.FileData.Count == 0) + { + dict = Default.FileSwapData; + } + else + { + // Need to ensure uniqueness. + dict = new Dictionary(Default.FileData.Count + Default.FileSwaps.Count); + foreach (var (gamePath, file) in Default.FileData.Concat(Default.FileSwaps)) + dict.TryAdd(gamePath, file); + } + + return new AppliedModData(dict, Default.Manipulations); + } + ISubMod IMod.Default => Default; @@ -53,7 +75,8 @@ public class TemporaryMod : IMod dir = ModCreator.CreateModFolder(modManager.BasePath, collection.Name, config.ReplaceNonAsciiOnImport, true); var fileDir = Directory.CreateDirectory(Path.Combine(dir.FullName, "files")); modManager.DataEditor.CreateMeta(dir, collection.Name, character ?? config.DefaultModAuthor, - $"Mod generated from temporary collection {collection.Id} for {character ?? "Unknown Character"} with name {collection.Name}.", null, null); + $"Mod generated from temporary collection {collection.Id} for {character ?? "Unknown Character"} with name {collection.Name}.", + null, null); var mod = new Mod(dir); var defaultMod = mod.Default; foreach (var (gamePath, fullPath) in collection.ResolvedFiles) @@ -86,7 +109,8 @@ public class TemporaryMod : IMod saveService.ImmediateSave(new ModSaveGroup(dir, defaultMod, config.ReplaceNonAsciiOnImport)); modManager.AddMod(dir); - Penumbra.Log.Information($"Successfully generated mod {mod.Name} at {mod.ModPath.FullName} for collection {collection.Identifier}."); + Penumbra.Log.Information( + $"Successfully generated mod {mod.Name} at {mod.ModPath.FullName} for collection {collection.Identifier}."); } catch (Exception e) { From 9f4c6767f822be23632b39e3ab73792d19290ec3 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 19 Apr 2024 18:28:25 +0200 Subject: [PATCH 116/433] Remove ISubMod. --- Penumbra/Import/TexToolsImporter.ModPack.cs | 2 +- Penumbra/Mods/Editor/DuplicateManager.cs | 7 +- Penumbra/Mods/Editor/FileRegistry.cs | 2 +- Penumbra/Mods/Editor/IMod.cs | 12 ++-- Penumbra/Mods/Editor/ModEditor.cs | 4 +- Penumbra/Mods/Editor/ModFileCollection.cs | 14 ++-- Penumbra/Mods/Editor/ModFileEditor.cs | 16 ++--- Penumbra/Mods/Editor/ModMerger.cs | 2 +- Penumbra/Mods/Editor/ModMetaEditor.cs | 2 +- Penumbra/Mods/Editor/ModSwapEditor.cs | 2 +- Penumbra/Mods/ItemSwap/ItemSwapContainer.cs | 15 ++--- Penumbra/Mods/Manager/ModOptionEditor.cs | 15 ++--- Penumbra/Mods/Mod.cs | 13 ++-- Penumbra/Mods/ModCreator.cs | 8 +-- Penumbra/Mods/Subclasses/IModGroup.cs | 12 ++-- Penumbra/Mods/Subclasses/ISubMod.cs | 67 ------------------- Penumbra/Mods/Subclasses/ModSettings.cs | 35 +--------- Penumbra/Mods/Subclasses/MultiModGroup.cs | 6 +- Penumbra/Mods/Subclasses/SingleModGroup.cs | 4 +- Penumbra/Mods/Subclasses/SubMod.cs | 58 ++++++++++++++-- Penumbra/Mods/TemporaryMod.cs | 5 +- .../UI/AdvancedWindow/ModEditWindow.Files.cs | 4 +- .../ModEditWindow.QuickImport.cs | 2 +- 23 files changed, 123 insertions(+), 184 deletions(-) delete mode 100644 Penumbra/Mods/Subclasses/ISubMod.cs diff --git a/Penumbra/Import/TexToolsImporter.ModPack.cs b/Penumbra/Import/TexToolsImporter.ModPack.cs index 099b133c..f4b7d47e 100644 --- a/Penumbra/Import/TexToolsImporter.ModPack.cs +++ b/Penumbra/Import/TexToolsImporter.ModPack.cs @@ -152,7 +152,7 @@ public partial class TexToolsImporter } // Iterate through all pages - var options = new List(); + var options = new List(); var groupPriority = ModPriority.Default; var groupNames = new HashSet(); foreach (var page in modList.ModPackPages) diff --git a/Penumbra/Mods/Editor/DuplicateManager.cs b/Penumbra/Mods/Editor/DuplicateManager.cs index c8530936..938199aa 100644 --- a/Penumbra/Mods/Editor/DuplicateManager.cs +++ b/Penumbra/Mods/Editor/DuplicateManager.cs @@ -29,7 +29,7 @@ public class DuplicateManager(ModManager modManager, SaveService saveService, Co Worker = Task.Run(() => CheckDuplicates(filesTmp, _cancellationTokenSource.Token), _cancellationTokenSource.Token); } - public void DeleteDuplicates(ModFileCollection files, Mod mod, ISubMod option, bool useModManager) + public void DeleteDuplicates(ModFileCollection files, Mod mod, SubMod option, bool useModManager) { if (!Worker.IsCompleted || _duplicates.Count == 0) return; @@ -72,7 +72,7 @@ public class DuplicateManager(ModManager modManager, SaveService saveService, Co return; - void HandleSubMod(ISubMod subMod, int groupIdx, int optionIdx) + void HandleSubMod(SubMod subMod, int groupIdx, int optionIdx) { var changes = false; var dict = subMod.Files.ToDictionary(kvp => kvp.Key, @@ -86,8 +86,7 @@ public class DuplicateManager(ModManager modManager, SaveService saveService, Co } else { - var sub = (SubMod)subMod; - sub.FileData = dict; + subMod.FileData = dict; saveService.ImmediateSaveSync(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); } } diff --git a/Penumbra/Mods/Editor/FileRegistry.cs b/Penumbra/Mods/Editor/FileRegistry.cs index 96d027b3..427c58ca 100644 --- a/Penumbra/Mods/Editor/FileRegistry.cs +++ b/Penumbra/Mods/Editor/FileRegistry.cs @@ -5,7 +5,7 @@ namespace Penumbra.Mods.Editor; public class FileRegistry : IEquatable { - public readonly List<(ISubMod, Utf8GamePath)> SubModUsage = []; + public readonly List<(SubMod, Utf8GamePath)> SubModUsage = []; public FullPath File { get; private init; } public Utf8RelPath RelPath { get; private init; } public long FileSize { get; private init; } diff --git a/Penumbra/Mods/Editor/IMod.cs b/Penumbra/Mods/Editor/IMod.cs index 8b5b65e1..c4c4be2f 100644 --- a/Penumbra/Mods/Editor/IMod.cs +++ b/Penumbra/Mods/Editor/IMod.cs @@ -6,8 +6,8 @@ using Penumbra.String.Classes; namespace Penumbra.Mods.Editor; public record struct AppliedModData( - IReadOnlyCollection> FileRedirections, - IReadOnlyCollection Manipulations) + Dictionary FileRedirections, + HashSet Manipulations) { public static readonly AppliedModData Empty = new([], []); } @@ -19,14 +19,10 @@ public interface IMod public int Index { get; } public ModPriority Priority { get; } + public IReadOnlyList Groups { get; } + public AppliedModData GetData(ModSettings? settings = null); - - public ISubMod Default { get; } - public IReadOnlyList Groups { get; } - - public IEnumerable AllSubMods { get; } - // Cache public int TotalManipulations { get; } } diff --git a/Penumbra/Mods/Editor/ModEditor.cs b/Penumbra/Mods/Editor/ModEditor.cs index b22aea17..d9781c06 100644 --- a/Penumbra/Mods/Editor/ModEditor.cs +++ b/Penumbra/Mods/Editor/ModEditor.cs @@ -29,7 +29,7 @@ public class ModEditor( public int OptionIdx { get; private set; } public IModGroup? Group { get; private set; } - public ISubMod? Option { get; private set; } + public SubMod? Option { get; private set; } public void LoadMod(Mod mod) => LoadMod(mod, -1, 0); @@ -104,7 +104,7 @@ public class ModEditor( => Clear(); /// Apply a option action to all available option in a mod, including the default option. - public static void ApplyToAllOptions(Mod mod, Action action) + public static void ApplyToAllOptions(Mod mod, Action action) { action(mod.Default, -1, 0); foreach (var (group, groupIdx) in mod.Groups.WithIndex()) diff --git a/Penumbra/Mods/Editor/ModFileCollection.cs b/Penumbra/Mods/Editor/ModFileCollection.cs index 2f8bdfb1..9dd78217 100644 --- a/Penumbra/Mods/Editor/ModFileCollection.cs +++ b/Penumbra/Mods/Editor/ModFileCollection.cs @@ -38,13 +38,13 @@ public class ModFileCollection : IDisposable public bool Ready { get; private set; } = true; - public void UpdateAll(Mod mod, ISubMod option) + public void UpdateAll(Mod mod, SubMod option) { UpdateFiles(mod, new CancellationToken()); UpdatePaths(mod, option, false, new CancellationToken()); } - public void UpdatePaths(Mod mod, ISubMod option) + public void UpdatePaths(Mod mod, SubMod option) => UpdatePaths(mod, option, true, new CancellationToken()); public void Clear() @@ -59,7 +59,7 @@ public class ModFileCollection : IDisposable public void ClearMissingFiles() => _missing.Clear(); - public void RemoveUsedPath(ISubMod option, FileRegistry? file, Utf8GamePath gamePath) + public void RemoveUsedPath(SubMod option, FileRegistry? file, Utf8GamePath gamePath) { _usedPaths.Remove(gamePath); if (file != null) @@ -69,10 +69,10 @@ public class ModFileCollection : IDisposable } } - public void RemoveUsedPath(ISubMod option, FullPath file, Utf8GamePath gamePath) + public void RemoveUsedPath(SubMod option, FullPath file, Utf8GamePath gamePath) => RemoveUsedPath(option, _available.FirstOrDefault(f => f.File.Equals(file)), gamePath); - public void AddUsedPath(ISubMod option, FileRegistry? file, Utf8GamePath gamePath) + public void AddUsedPath(SubMod option, FileRegistry? file, Utf8GamePath gamePath) { _usedPaths.Add(gamePath); if (file == null) @@ -82,7 +82,7 @@ public class ModFileCollection : IDisposable file.SubModUsage.Add((option, gamePath)); } - public void AddUsedPath(ISubMod option, FullPath file, Utf8GamePath gamePath) + public void AddUsedPath(SubMod option, FullPath file, Utf8GamePath gamePath) => AddUsedPath(option, _available.FirstOrDefault(f => f.File.Equals(file)), gamePath); public void ChangeUsedPath(FileRegistry file, int pathIdx, Utf8GamePath gamePath) @@ -154,7 +154,7 @@ public class ModFileCollection : IDisposable _usedPaths.Clear(); } - private void UpdatePaths(Mod mod, ISubMod option, bool clearRegistries, CancellationToken tok) + private void UpdatePaths(Mod mod, SubMod option, bool clearRegistries, CancellationToken tok) { tok.ThrowIfCancellationRequested(); ClearPaths(clearRegistries, tok); diff --git a/Penumbra/Mods/Editor/ModFileEditor.cs b/Penumbra/Mods/Editor/ModFileEditor.cs index 30e97093..4bdf4b1b 100644 --- a/Penumbra/Mods/Editor/ModFileEditor.cs +++ b/Penumbra/Mods/Editor/ModFileEditor.cs @@ -30,16 +30,16 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu return num; } - public void Revert(Mod mod, ISubMod option) + public void Revert(Mod mod, SubMod option) { files.UpdateAll(mod, option); Changes = false; } /// Remove all path redirections where the pointed-to file does not exist. - public void RemoveMissingPaths(Mod mod, ISubMod option) + public void RemoveMissingPaths(Mod mod, SubMod option) { - void HandleSubMod(ISubMod subMod, int groupIdx, int optionIdx) + void HandleSubMod(SubMod subMod, int groupIdx, int optionIdx) { var newDict = subMod.Files.Where(kvp => CheckAgainstMissing(mod, subMod, kvp.Value, kvp.Key, subMod == option)) .ToDictionary(kvp => kvp.Key, kvp => kvp.Value); @@ -61,7 +61,7 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu /// If path is empty, it will be deleted instead. /// If pathIdx is equal to the total number of paths, path will be added, otherwise replaced. /// - public bool SetGamePath(ISubMod option, int fileIdx, int pathIdx, Utf8GamePath path) + public bool SetGamePath(SubMod option, int fileIdx, int pathIdx, Utf8GamePath path) { if (!CanAddGamePath(path) || fileIdx < 0 || fileIdx > files.Available.Count) return false; @@ -84,7 +84,7 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu /// Transform a set of files to the appropriate game paths with the given number of folders skipped, /// and add them to the given option. /// - public int AddPathsToSelected(ISubMod option, IEnumerable files1, int skipFolders = 0) + public int AddPathsToSelected(SubMod option, IEnumerable files1, int skipFolders = 0) { var failed = 0; foreach (var file in files1) @@ -111,7 +111,7 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu } /// Remove all paths in the current option from the given files. - public void RemovePathsFromSelected(ISubMod option, IEnumerable files1) + public void RemovePathsFromSelected(SubMod option, IEnumerable files1) { foreach (var file in files1) { @@ -129,7 +129,7 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu } /// Delete all given files from your filesystem - public void DeleteFiles(Mod mod, ISubMod option, IEnumerable files1) + public void DeleteFiles(Mod mod, SubMod option, IEnumerable files1) { var deletions = 0; foreach (var file in files1) @@ -155,7 +155,7 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu } - private bool CheckAgainstMissing(Mod mod, ISubMod option, FullPath file, Utf8GamePath key, bool removeUsed) + private bool CheckAgainstMissing(Mod mod, SubMod option, FullPath file, Utf8GamePath key, bool removeUsed) { if (!files.Missing.Contains(file)) return true; diff --git a/Penumbra/Mods/Editor/ModMerger.cs b/Penumbra/Mods/Editor/ModMerger.cs index 842b1bb3..25590c49 100644 --- a/Penumbra/Mods/Editor/ModMerger.cs +++ b/Penumbra/Mods/Editor/ModMerger.cs @@ -151,7 +151,7 @@ public class ModMerger : IDisposable MergeIntoOption(MergeFromMod!.AllSubMods.Reverse(), option, true); } - private void MergeIntoOption(IEnumerable mergeOptions, SubMod option, bool fromFileToFile) + private void MergeIntoOption(IEnumerable mergeOptions, SubMod option, bool fromFileToFile) { var redirections = option.FileData.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); var swaps = option.FileSwapData.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); diff --git a/Penumbra/Mods/Editor/ModMetaEditor.cs b/Penumbra/Mods/Editor/ModMetaEditor.cs index 31aefdf5..a6218c6f 100644 --- a/Penumbra/Mods/Editor/ModMetaEditor.cs +++ b/Penumbra/Mods/Editor/ModMetaEditor.cs @@ -103,7 +103,7 @@ public class ModMetaEditor(ModManager modManager) Changes = true; } - public void Load(Mod mod, ISubMod currentOption) + public void Load(Mod mod, SubMod currentOption) { OtherImcCount = 0; OtherEqpCount = 0; diff --git a/Penumbra/Mods/Editor/ModSwapEditor.cs b/Penumbra/Mods/Editor/ModSwapEditor.cs index ada06264..0d5f05a9 100644 --- a/Penumbra/Mods/Editor/ModSwapEditor.cs +++ b/Penumbra/Mods/Editor/ModSwapEditor.cs @@ -11,7 +11,7 @@ public class ModSwapEditor(ModManager modManager) public IReadOnlyDictionary Swaps => _swaps; - public void Revert(ISubMod option) + public void Revert(SubMod option) { _swaps.SetTo(option.FileSwaps); Changes = false; diff --git a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs index e229738d..21b9ef2c 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs @@ -5,6 +5,7 @@ using Penumbra.GameData.Structs; using Penumbra.Meta.Manipulations; using Penumbra.String.Classes; using Penumbra.Meta; +using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; using Penumbra.Mods.Subclasses; @@ -15,14 +16,13 @@ public class ItemSwapContainer private readonly MetaFileManager _manager; private readonly ObjectIdentification _identifier; - private Dictionary _modRedirections = []; - private HashSet _modManipulations = []; + private AppliedModData _appliedModData = AppliedModData.Empty; public IReadOnlyDictionary ModRedirections - => _modRedirections; + => _appliedModData.FileRedirections; public IReadOnlySet ModManipulations - => _modManipulations; + => _appliedModData.Manipulations; public readonly List Swaps = []; @@ -97,12 +97,11 @@ public class ItemSwapContainer Clear(); if (mod == null || mod.Index < 0) { - _modRedirections = []; - _modManipulations = []; + _appliedModData = AppliedModData.Empty; } else { - (_modRedirections, _modManipulations) = ModSettings.GetResolveData(mod, settings); + _appliedModData = ModSettings.GetResolveData(mod, settings); } } @@ -120,7 +119,7 @@ public class ItemSwapContainer private Func MetaResolver(ModCollection? collection) { - var set = collection?.MetaCache?.Manipulations.ToHashSet() ?? _modManipulations; + var set = collection?.MetaCache?.Manipulations.ToHashSet() ?? _appliedModData.Manipulations; return m => set.TryGetValue(m, out var a) ? a : m; } diff --git a/Penumbra/Mods/Manager/ModOptionEditor.cs b/Penumbra/Mods/Manager/ModOptionEditor.cs index 9efb8a3f..07c6f38e 100644 --- a/Penumbra/Mods/Manager/ModOptionEditor.cs +++ b/Penumbra/Mods/Manager/ModOptionEditor.cs @@ -262,15 +262,12 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS } /// Add an existing option to a given group with default priority. - public void AddOption(Mod mod, int groupIdx, ISubMod option) + public void AddOption(Mod mod, int groupIdx, SubMod option) => AddOption(mod, groupIdx, option, ModPriority.Default); /// Add an existing option to a given group with a given priority. - public void AddOption(Mod mod, int groupIdx, ISubMod option, ModPriority priority) + public void AddOption(Mod mod, int groupIdx, SubMod option, ModPriority priority) { - if (option is not SubMod o) - return; - var group = mod.Groups[groupIdx]; switch (group) { @@ -280,12 +277,12 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS + $"since only up to {IModGroup.MaxMultiOptions} options are supported in one group."); return; case SingleModGroup s: - o.SetPosition(groupIdx, s.Count); - s.OptionData.Add(o); + option.SetPosition(groupIdx, s.Count); + s.OptionData.Add(option); break; case MultiModGroup m: - o.SetPosition(groupIdx, m.Count); - m.PrioritizedOptions.Add((o, priority)); + option.SetPosition(groupIdx, m.Count); + m.PrioritizedOptions.Add((option, priority)); break; } diff --git a/Penumbra/Mods/Mod.cs b/Penumbra/Mods/Mod.cs index 3c996c8f..25f3c510 100644 --- a/Penumbra/Mods/Mod.cs +++ b/Penumbra/Mods/Mod.cs @@ -32,6 +32,9 @@ public sealed class Mod : IMod public ModPriority Priority => ModPriority.Default; + IReadOnlyList IMod.Groups + => Groups; + internal Mod(DirectoryInfo modPath) { ModPath = modPath; @@ -74,18 +77,12 @@ public sealed class Mod : IMod group.AddData(config, dictRedirections, setManips); } - ((ISubMod)Default).AddData(dictRedirections, setManips); + Default.AddData(dictRedirections, setManips); return new AppliedModData(dictRedirections, setManips); } - ISubMod IMod.Default - => Default; - - IReadOnlyList IMod.Groups - => Groups; - public IEnumerable AllSubMods - => Groups.SelectMany(o => o).OfType().Prepend(Default); + => Groups.SelectMany(o => o).Prepend(Default); public List FindUnusedFiles() { diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index 2bcdd3b1..661dd6fb 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -235,7 +235,7 @@ public partial class ModCreator(SaveService _saveService, Configuration config, /// Create a file for an option group from given data. public void CreateOptionGroup(DirectoryInfo baseFolder, GroupType type, string name, - ModPriority priority, int index, Setting defaultSettings, string desc, IEnumerable subMods) + ModPriority priority, int index, Setting defaultSettings, string desc, IEnumerable subMods) { switch (type) { @@ -248,7 +248,7 @@ public partial class ModCreator(SaveService _saveService, Configuration config, Priority = priority, DefaultSettings = defaultSettings, }; - group.PrioritizedOptions.AddRange(subMods.OfType().Select((s, idx) => (s, new ModPriority(idx)))); + group.PrioritizedOptions.AddRange(subMods.Select((s, idx) => (s, new ModPriority(idx)))); _saveService.ImmediateSaveSync(new ModSaveGroup(baseFolder, group, index, Config.ReplaceNonAsciiOnImport)); break; } @@ -269,7 +269,7 @@ public partial class ModCreator(SaveService _saveService, Configuration config, } /// Create the data for a given sub mod from its data and the folder it is based on. - public ISubMod CreateSubMod(DirectoryInfo baseFolder, DirectoryInfo optionFolder, OptionList option) + public SubMod CreateSubMod(DirectoryInfo baseFolder, DirectoryInfo optionFolder, OptionList option) { var list = optionFolder.EnumerateNonHiddenFiles() .Select(f => (Utf8GamePath.FromFile(f, optionFolder, out var gamePath, true), gamePath, new FullPath(f))) @@ -288,7 +288,7 @@ public partial class ModCreator(SaveService _saveService, Configuration config, } /// Create an empty sub mod for single groups with None options. - internal static ISubMod CreateEmptySubMod(string name) + internal static SubMod CreateEmptySubMod(string name) => new SubMod(null!) // Mod is irrelevant here, only used for saving. { Name = name, diff --git a/Penumbra/Mods/Subclasses/IModGroup.cs b/Penumbra/Mods/Subclasses/IModGroup.cs index 57ef4e98..3f363542 100644 --- a/Penumbra/Mods/Subclasses/IModGroup.cs +++ b/Penumbra/Mods/Subclasses/IModGroup.cs @@ -6,7 +6,7 @@ using Penumbra.String.Classes; namespace Penumbra.Mods.Subclasses; -public interface IModGroup : IReadOnlyCollection +public interface IModGroup : IReadOnlyCollection { public const int MaxMultiOptions = 63; @@ -18,7 +18,7 @@ public interface IModGroup : IReadOnlyCollection public ModPriority OptionPriority(Index optionIdx); - public ISubMod this[Index idx] { get; } + public SubMod this[Index idx] { get; } public bool IsOption { get; } @@ -37,7 +37,7 @@ public readonly struct ModSaveGroup : ISavable private readonly DirectoryInfo _basePath; private readonly IModGroup? _group; private readonly int _groupIdx; - private readonly ISubMod? _defaultMod; + private readonly SubMod? _defaultMod; private readonly bool _onlyAscii; public ModSaveGroup(Mod mod, int groupIdx, bool onlyAscii) @@ -59,7 +59,7 @@ public readonly struct ModSaveGroup : ISavable _onlyAscii = onlyAscii; } - public ModSaveGroup(DirectoryInfo basePath, ISubMod @default, bool onlyAscii) + public ModSaveGroup(DirectoryInfo basePath, SubMod @default, bool onlyAscii) { _basePath = basePath; _groupIdx = -1; @@ -91,7 +91,7 @@ public readonly struct ModSaveGroup : ISavable j.WriteStartArray(); for (var idx = 0; idx < _group.Count; ++idx) { - ISubMod.WriteSubMod(j, serializer, _group[idx], _basePath, _group.Type switch + SubMod.WriteSubMod(j, serializer, _group[idx], _basePath, _group.Type switch { GroupType.Multi => _group.OptionPriority(idx), _ => null, @@ -103,7 +103,7 @@ public readonly struct ModSaveGroup : ISavable } else { - ISubMod.WriteSubMod(j, serializer, _defaultMod!, _basePath, null); + SubMod.WriteSubMod(j, serializer, _defaultMod!, _basePath, null); } } } diff --git a/Penumbra/Mods/Subclasses/ISubMod.cs b/Penumbra/Mods/Subclasses/ISubMod.cs deleted file mode 100644 index e997e07d..00000000 --- a/Penumbra/Mods/Subclasses/ISubMod.cs +++ /dev/null @@ -1,67 +0,0 @@ -using Newtonsoft.Json; -using Penumbra.Meta.Manipulations; -using Penumbra.String.Classes; - -namespace Penumbra.Mods.Subclasses; - -public interface ISubMod -{ - public string Name { get; } - public string FullName { get; } - public string Description { get; } - - public IReadOnlyDictionary Files { get; } - public IReadOnlyDictionary FileSwaps { get; } - public IReadOnlySet Manipulations { get; } - - public void AddData(Dictionary redirections, HashSet manipulations) - { - foreach (var (path, file) in Files) - redirections.TryAdd(path, file); - - foreach (var (path, file) in FileSwaps) - redirections.TryAdd(path, file); - manipulations.UnionWith(Manipulations); - } - - public bool IsDefault { get; } - - public static void WriteSubMod(JsonWriter j, JsonSerializer serializer, ISubMod mod, DirectoryInfo basePath, ModPriority? priority) - { - j.WriteStartObject(); - j.WritePropertyName(nameof(Name)); - j.WriteValue(mod.Name); - j.WritePropertyName(nameof(Description)); - j.WriteValue(mod.Description); - if (priority != null) - { - j.WritePropertyName(nameof(IModGroup.Priority)); - j.WriteValue(priority.Value.Value); - } - - j.WritePropertyName(nameof(mod.Files)); - j.WriteStartObject(); - foreach (var (gamePath, file) in mod.Files) - { - if (file.ToRelPath(basePath, out var relPath)) - { - j.WritePropertyName(gamePath.ToString()); - j.WriteValue(relPath.ToString()); - } - } - - j.WriteEndObject(); - j.WritePropertyName(nameof(mod.FileSwaps)); - j.WriteStartObject(); - foreach (var (gamePath, file) in mod.FileSwaps) - { - j.WritePropertyName(gamePath.ToString()); - j.WriteValue(file.ToString()); - } - - j.WriteEndObject(); - j.WritePropertyName(nameof(mod.Manipulations)); - serializer.Serialize(j, mod.Manipulations); - j.WriteEndObject(); - } -} diff --git a/Penumbra/Mods/Subclasses/ModSettings.cs b/Penumbra/Mods/Subclasses/ModSettings.cs index 380b242c..81a3bb41 100644 --- a/Penumbra/Mods/Subclasses/ModSettings.cs +++ b/Penumbra/Mods/Subclasses/ModSettings.cs @@ -2,6 +2,7 @@ using OtterGui; using OtterGui.Filesystem; using Penumbra.Api.Enums; using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; using Penumbra.String.Classes; @@ -34,44 +35,14 @@ public class ModSettings }; // Return everything required to resolve things for a single mod with given settings (which can be null, in which case the default is used. - public static (Dictionary, HashSet) GetResolveData(Mod mod, ModSettings? settings) + public static AppliedModData GetResolveData(Mod mod, ModSettings? settings) { if (settings == null) settings = DefaultSettings(mod); else settings.Settings.FixSize(mod); - var dict = new Dictionary(); - var set = new HashSet(); - - foreach (var (group, index) in mod.Groups.WithIndex().OrderByDescending(g => g.Value.Priority)) - { - if (group.Type is GroupType.Single) - { - if (group.Count > 0) - AddOption(group[settings.Settings[index].AsIndex]); - } - else - { - foreach (var (option, optionIdx) in group.WithIndex().OrderByDescending(o => group.OptionPriority(o.Index))) - { - if (settings.Settings[index].HasFlag(optionIdx)) - AddOption(option); - } - } - } - - AddOption(mod.Default); - return (dict, set); - - void AddOption(ISubMod option) - { - foreach (var (path, file) in option.Files.Concat(option.FileSwaps)) - dict.TryAdd(path, file); - - foreach (var manip in option.Manipulations) - set.Add(manip); - } + return mod.GetData(settings); } // Automatically react to changes in a mods available options. diff --git a/Penumbra/Mods/Subclasses/MultiModGroup.cs b/Penumbra/Mods/Subclasses/MultiModGroup.cs index 266d3037..1600072e 100644 --- a/Penumbra/Mods/Subclasses/MultiModGroup.cs +++ b/Penumbra/Mods/Subclasses/MultiModGroup.cs @@ -24,7 +24,7 @@ public sealed class MultiModGroup : IModGroup public ModPriority OptionPriority(Index idx) => PrioritizedOptions[idx].Priority; - public ISubMod this[Index idx] + public SubMod this[Index idx] => PrioritizedOptions[idx].Mod; public bool IsOption @@ -36,7 +36,7 @@ public sealed class MultiModGroup : IModGroup public readonly List<(SubMod Mod, ModPriority Priority)> PrioritizedOptions = []; - public IEnumerator GetEnumerator() + public IEnumerator GetEnumerator() => PrioritizedOptions.Select(o => o.Mod).GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() @@ -117,7 +117,7 @@ public sealed class MultiModGroup : IModGroup foreach (var (option, index) in PrioritizedOptions.WithIndex().OrderByDescending(o => o.Value.Priority)) { if (setting.HasFlag(index)) - ((ISubMod)option.Mod).AddData(redirections, manipulations); + option.Mod.AddData(redirections, manipulations); } } diff --git a/Penumbra/Mods/Subclasses/SingleModGroup.cs b/Penumbra/Mods/Subclasses/SingleModGroup.cs index f797a709..2d49fd1f 100644 --- a/Penumbra/Mods/Subclasses/SingleModGroup.cs +++ b/Penumbra/Mods/Subclasses/SingleModGroup.cs @@ -24,7 +24,7 @@ public sealed class SingleModGroup : IModGroup public ModPriority OptionPriority(Index _) => Priority; - public ISubMod this[Index idx] + public SubMod this[Index idx] => OptionData[idx]; public bool IsOption @@ -34,7 +34,7 @@ public sealed class SingleModGroup : IModGroup public int Count => OptionData.Count; - public IEnumerator GetEnumerator() + public IEnumerator GetEnumerator() => OptionData.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() diff --git a/Penumbra/Mods/Subclasses/SubMod.cs b/Penumbra/Mods/Subclasses/SubMod.cs index 4f35cd33..386910e5 100644 --- a/Penumbra/Mods/Subclasses/SubMod.cs +++ b/Penumbra/Mods/Subclasses/SubMod.cs @@ -1,3 +1,4 @@ +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Editor; @@ -15,7 +16,7 @@ namespace Penumbra.Mods.Subclasses; /// Nothing is checked for existence or validity when loading. /// Objects are also not checked for uniqueness, the first appearance of a game path or meta path decides. /// -public sealed class SubMod : ISubMod +public sealed class SubMod { public string Name { get; set; } = "Default"; @@ -29,7 +30,17 @@ public sealed class SubMod : ISubMod internal int OptionIdx { get; private set; } public bool IsDefault - => GroupIdx < 0; + => GroupIdx < 0; + + public void AddData(Dictionary redirections, HashSet manipulations) + { + foreach (var (path, file) in Files) + redirections.TryAdd(path, file); + + foreach (var (path, file) in FileSwaps) + redirections.TryAdd(path, file); + manipulations.UnionWith(Manipulations); + } public Dictionary FileData = []; public Dictionary FileSwapData = []; @@ -60,8 +71,8 @@ public sealed class SubMod : ISubMod ManipulationData.Clear(); // Every option has a name, but priorities are only relevant for multi group options. - Name = json[nameof(ISubMod.Name)]?.ToObject() ?? string.Empty; - Description = json[nameof(ISubMod.Description)]?.ToObject() ?? string.Empty; + Name = json[nameof(Name)]?.ToObject() ?? string.Empty; + Description = json[nameof(Description)]?.ToObject() ?? string.Empty; priority = json[nameof(IModGroup.Priority)]?.ToObject() ?? ModPriority.Default; var files = (JObject?)json[nameof(Files)]; @@ -104,4 +115,43 @@ public sealed class SubMod : ISubMod } } } + + public static void WriteSubMod(JsonWriter j, JsonSerializer serializer, SubMod mod, DirectoryInfo basePath, ModPriority? priority) + { + j.WriteStartObject(); + j.WritePropertyName(nameof(Name)); + j.WriteValue(mod.Name); + j.WritePropertyName(nameof(Description)); + j.WriteValue(mod.Description); + if (priority != null) + { + j.WritePropertyName(nameof(IModGroup.Priority)); + j.WriteValue(priority.Value.Value); + } + + j.WritePropertyName(nameof(mod.Files)); + j.WriteStartObject(); + foreach (var (gamePath, file) in mod.Files) + { + if (file.ToRelPath(basePath, out var relPath)) + { + j.WritePropertyName(gamePath.ToString()); + j.WriteValue(relPath.ToString()); + } + } + + j.WriteEndObject(); + j.WritePropertyName(nameof(mod.FileSwaps)); + j.WriteStartObject(); + foreach (var (gamePath, file) in mod.FileSwaps) + { + j.WritePropertyName(gamePath.ToString()); + j.WriteValue(file.ToString()); + } + + j.WriteEndObject(); + j.WritePropertyName(nameof(mod.Manipulations)); + serializer.Serialize(j, mod.Manipulations); + j.WriteEndObject(); + } } diff --git a/Penumbra/Mods/TemporaryMod.cs b/Penumbra/Mods/TemporaryMod.cs index 8f27e201..41c1211f 100644 --- a/Penumbra/Mods/TemporaryMod.cs +++ b/Penumbra/Mods/TemporaryMod.cs @@ -39,12 +39,9 @@ public class TemporaryMod : IMod dict.TryAdd(gamePath, file); } - return new AppliedModData(dict, Default.Manipulations); + return new AppliedModData(dict, Default.ManipulationData); } - ISubMod IMod.Default - => Default; - public IReadOnlyList Groups => Array.Empty(); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs index c8db7770..f765b47e 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs @@ -192,7 +192,7 @@ public partial class ModEditWindow ImGuiUtil.RightAlign(rightText); } - private void PrintGamePath(int i, int j, FileRegistry registry, ISubMod subMod, Utf8GamePath gamePath) + private void PrintGamePath(int i, int j, FileRegistry registry, SubMod subMod, Utf8GamePath gamePath) { using var id = ImRaii.PushId(j); ImGui.TableNextColumn(); @@ -228,7 +228,7 @@ public partial class ModEditWindow } } - private void PrintNewGamePath(int i, FileRegistry registry, ISubMod subMod) + private void PrintNewGamePath(int i, FileRegistry registry, SubMod subMod) { var tmp = _fileIdx == i && _pathIdx == -1 ? _gamePathEdit : string.Empty; var pos = ImGui.GetCursorPosX() - ImGui.GetFrameHeight(); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs index 10956deb..4ecacece 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs @@ -227,7 +227,7 @@ public partial class ModEditWindow return fileRegistry; } - private static (DirectoryInfo, int) GetPreferredPath(Mod mod, ISubMod subMod, bool replaceNonAscii) + private static (DirectoryInfo, int) GetPreferredPath(Mod mod, SubMod subMod, bool replaceNonAscii) { var path = mod.ModPath; var subDirs = 0; From 2d5afde61274f3602f15252eb64efbcb899c5cae Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 20 Apr 2024 11:02:30 +0200 Subject: [PATCH 117/433] Fix group priority writing. --- Penumbra/Mods/Subclasses/IModGroup.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Mods/Subclasses/IModGroup.cs b/Penumbra/Mods/Subclasses/IModGroup.cs index 3f363542..7554f6dc 100644 --- a/Penumbra/Mods/Subclasses/IModGroup.cs +++ b/Penumbra/Mods/Subclasses/IModGroup.cs @@ -82,7 +82,7 @@ public readonly struct ModSaveGroup : ISavable j.WritePropertyName(nameof(_group.Description)); j.WriteValue(_group.Description); j.WritePropertyName(nameof(_group.Priority)); - j.WriteValue(_group.Priority); + j.WriteValue(_group.Priority.Value); j.WritePropertyName(nameof(Type)); j.WriteValue(_group.Type.ToString()); j.WritePropertyName(nameof(_group.DefaultSettings)); From f86f29b44a4d3dc78929107a9c99538b0d314d6a Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 20 Apr 2024 11:03:50 +0200 Subject: [PATCH 118/433] Some fixes. --- Penumbra/Mods/Manager/ModOptionEditor.cs | 4 +-- Penumbra/Mods/Subclasses/IModGroup.cs | 33 ++++++++++++++---------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/Penumbra/Mods/Manager/ModOptionEditor.cs b/Penumbra/Mods/Manager/ModOptionEditor.cs index 07c6f38e..9d942574 100644 --- a/Penumbra/Mods/Manager/ModOptionEditor.cs +++ b/Penumbra/Mods/Manager/ModOptionEditor.cs @@ -158,10 +158,10 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS { var group = mod.Groups[groupIdx]; var option = group[optionIdx]; - if (option.Description == newDescription || option is not SubMod s) + if (option.Description == newDescription) return; - s.Description = newDescription; + option.Description = newDescription; saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, mod, groupIdx, optionIdx, -1); } diff --git a/Penumbra/Mods/Subclasses/IModGroup.cs b/Penumbra/Mods/Subclasses/IModGroup.cs index 7554f6dc..38f070b3 100644 --- a/Penumbra/Mods/Subclasses/IModGroup.cs +++ b/Penumbra/Mods/Subclasses/IModGroup.cs @@ -72,8 +72,9 @@ public readonly struct ModSaveGroup : ISavable public void Save(StreamWriter writer) { - using var j = new JsonTextWriter(writer) { Formatting = Formatting.Indented }; - var serializer = new JsonSerializer { Formatting = Formatting.Indented }; + using var j = new JsonTextWriter(writer); + j.Formatting = Formatting.Indented; + var serializer = new JsonSerializer { Formatting = Formatting.Indented }; if (_groupIdx >= 0) { j.WriteStartObject(); @@ -87,19 +88,25 @@ public readonly struct ModSaveGroup : ISavable j.WriteValue(_group.Type.ToString()); j.WritePropertyName(nameof(_group.DefaultSettings)); j.WriteValue(_group.DefaultSettings.Value); - j.WritePropertyName("Options"); - j.WriteStartArray(); - for (var idx = 0; idx < _group.Count; ++idx) + switch (_group) { - SubMod.WriteSubMod(j, serializer, _group[idx], _basePath, _group.Type switch - { - GroupType.Multi => _group.OptionPriority(idx), - _ => null, - }); + case SingleModGroup single: + j.WritePropertyName("Options"); + j.WriteStartArray(); + foreach (var option in single.OptionData) + SubMod.WriteSubMod(j, serializer, option, _basePath, null); + j.WriteEndArray(); + j.WriteEndObject(); + break; + case MultiModGroup multi: + j.WritePropertyName("Options"); + j.WriteStartArray(); + foreach (var (option, priority) in multi.PrioritizedOptions) + SubMod.WriteSubMod(j, serializer, option, _basePath, priority); + j.WriteEndArray(); + j.WriteEndObject(); + break; } - - j.WriteEndArray(); - j.WriteEndObject(); } else { From b99a809eba50019fad9aa7e1b25ce73c5ebca6fa Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 20 Apr 2024 11:26:12 +0200 Subject: [PATCH 119/433] Remove OptionPriority from general option groups. --- Penumbra/Mods/Subclasses/IModGroup.cs | 4 ++-- Penumbra/Mods/Subclasses/MultiModGroup.cs | 6 ++++-- Penumbra/Mods/Subclasses/SingleModGroup.cs | 6 ++++-- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 13 ++++++++----- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 10 +++++++--- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/Penumbra/Mods/Subclasses/IModGroup.cs b/Penumbra/Mods/Subclasses/IModGroup.cs index 38f070b3..96d7c6b7 100644 --- a/Penumbra/Mods/Subclasses/IModGroup.cs +++ b/Penumbra/Mods/Subclasses/IModGroup.cs @@ -16,7 +16,7 @@ public interface IModGroup : IReadOnlyCollection public ModPriority Priority { get; } public Setting DefaultSettings { get; set; } - public ModPriority OptionPriority(Index optionIdx); + public FullPath? FindBestMatch(Utf8GamePath gamePath); public SubMod this[Index idx] { get; } @@ -37,7 +37,7 @@ public readonly struct ModSaveGroup : ISavable private readonly DirectoryInfo _basePath; private readonly IModGroup? _group; private readonly int _groupIdx; - private readonly SubMod? _defaultMod; + private readonly SubMod? _defaultMod; private readonly bool _onlyAscii; public ModSaveGroup(Mod mod, int groupIdx, bool onlyAscii) diff --git a/Penumbra/Mods/Subclasses/MultiModGroup.cs b/Penumbra/Mods/Subclasses/MultiModGroup.cs index 1600072e..02ae07f4 100644 --- a/Penumbra/Mods/Subclasses/MultiModGroup.cs +++ b/Penumbra/Mods/Subclasses/MultiModGroup.cs @@ -21,8 +21,10 @@ public sealed class MultiModGroup : IModGroup public ModPriority Priority { get; set; } public Setting DefaultSettings { get; set; } - public ModPriority OptionPriority(Index idx) - => PrioritizedOptions[idx].Priority; + public FullPath? FindBestMatch(Utf8GamePath gamePath) + => PrioritizedOptions.OrderByDescending(o => o.Priority) + .SelectWhere(o => (o.Mod.FileData.TryGetValue(gamePath, out var file) || o.Mod.FileSwapData.TryGetValue(gamePath, out file), file)) + .FirstOrDefault(); public SubMod this[Index idx] => PrioritizedOptions[idx].Mod; diff --git a/Penumbra/Mods/Subclasses/SingleModGroup.cs b/Penumbra/Mods/Subclasses/SingleModGroup.cs index 2d49fd1f..b854d2b1 100644 --- a/Penumbra/Mods/Subclasses/SingleModGroup.cs +++ b/Penumbra/Mods/Subclasses/SingleModGroup.cs @@ -21,8 +21,10 @@ public sealed class SingleModGroup : IModGroup public readonly List OptionData = []; - public ModPriority OptionPriority(Index _) - => Priority; + public FullPath? FindBestMatch(Utf8GamePath gamePath) + => OptionData + .SelectWhere(m => (m.FileData.TryGetValue(gamePath, out var file) || m.FileSwapData.TryGetValue(gamePath, out file), file)) + .FirstOrDefault(); public SubMod this[Index idx] => OptionData[idx]; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index 6cf24f62..a70da628 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -540,14 +540,17 @@ public partial class ModEditWindow : Window, IDisposable return currentFile.Value; if (Mod != null) - foreach (var option in Mod.Groups.OrderByDescending(g => g.Priority) - .SelectMany(g => g.WithIndex().OrderByDescending(o => g.OptionPriority(o.Index)).Select(g => g.Value)) - .Append(Mod.Default)) + { + foreach (var option in Mod.Groups.OrderByDescending(g => g.Priority)) { - if (option.Files.TryGetValue(path, out var value) || option.FileSwaps.TryGetValue(path, out value)) - return value; + if (option.FindBestMatch(path) is { } fullPath) + return fullPath; } + if (Mod.Default.Files.TryGetValue(path, out var value) || Mod.Default.FileSwaps.TryGetValue(path, out value)) + return value; + } + return new FullPath(path); } diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index 80af7b15..b002dedd 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -532,10 +532,10 @@ public class ModPanelEditTab( panel._delayedActions.Enqueue(() => panel._modManager.OptionEditor.DeleteOption(panel._mod, groupIdx, optionIdx)); ImGui.TableNextColumn(); - if (group.Type != GroupType.Multi) + if (group is not MultiModGroup multi) return; - if (Input.Priority("##Priority", groupIdx, optionIdx, group.OptionPriority(optionIdx), out var priority, + if (Input.Priority("##Priority", groupIdx, optionIdx, multi.PrioritizedOptions[optionIdx].Priority, out var priority, 50 * UiHelpers.Scale)) panel._modManager.OptionEditor.ChangeOptionPriority(panel._mod, groupIdx, optionIdx, priority); @@ -613,7 +613,11 @@ public class ModPanelEditTab( var sourceGroup = panel._mod.Groups[sourceGroupIdx]; var currentCount = group.Count; var option = sourceGroup[sourceOption]; - var priority = sourceGroup.OptionPriority(_dragDropOptionIdx); + var priority = sourceGroup switch + { + MultiModGroup multi => multi.PrioritizedOptions[_dragDropOptionIdx].Priority, + _ => ModPriority.Default, + }; panel._delayedActions.Enqueue(() => { panel._modManager.OptionEditor.DeleteOption(panel._mod, sourceGroupIdx, sourceOption); From 4a6d94f0fb817810d14a9920130db7bada5ff47c Mon Sep 17 00:00:00 2001 From: ackwell Date: Sat, 20 Apr 2024 20:02:43 +1000 Subject: [PATCH 120/433] Avoid inclusion of zero-weighted bones in name mapping --- Penumbra/Import/Models/Import/MeshImporter.cs | 18 ++++++++++++++---- .../Import/Models/Import/VertexAttribute.cs | 19 +++++++++++-------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/Penumbra/Import/Models/Import/MeshImporter.cs b/Penumbra/Import/Models/Import/MeshImporter.cs index 1d4b223d..5d5df948 100644 --- a/Penumbra/Import/Models/Import/MeshImporter.cs +++ b/Penumbra/Import/Models/Import/MeshImporter.cs @@ -189,15 +189,25 @@ public class MeshImporter(IEnumerable nodes, IoNotifier notifier) foreach (var (primitive, primitiveIndex) in node.Mesh.Primitives.WithIndex()) { // Per glTF specification, an asset with a skin MUST contain skinning attributes on its meshes. - var jointsAccessor = primitive.GetVertexAccessor("JOINTS_0") - ?? throw notifier.Exception($"Primitive {primitiveIndex} is skinned but does not contain skinning vertex attributes."); + var jointsAccessor = primitive.GetVertexAccessor("JOINTS_0")?.AsVector4Array(); + var weightsAccessor = primitive.GetVertexAccessor("WEIGHTS_0")?.AsVector4Array(); + + if (jointsAccessor == null || weightsAccessor == null) + throw notifier.Exception($"Primitive {primitiveIndex} is skinned but does not contain skinning vertex attributes."); // Build a set of joints that are referenced by this mesh. - // TODO: Would be neat to omit 0-weighted joints here, but doing so will require some further work on bone mapping behavior to ensure the unweighted joints can still be resolved to valid bone indices during vertex data construction. - foreach (var joints in jointsAccessor.AsVector4Array()) + for (var i = 0; i < jointsAccessor.Count; i++) { + var joints = jointsAccessor[i]; + var weights = weightsAccessor[i]; for (var index = 0; index < 4; index++) + { + // If a joint has absolutely no weight, we omit the bone entirely. + if (weights[index] == 0) + continue; + usedJoints.Add((ushort)joints[index]); + } } } diff --git a/Penumbra/Import/Models/Import/VertexAttribute.cs b/Penumbra/Import/Models/Import/VertexAttribute.cs index 3cfedd6f..b7f5dcf1 100644 --- a/Penumbra/Import/Models/Import/VertexAttribute.cs +++ b/Penumbra/Import/Models/Import/VertexAttribute.cs @@ -144,10 +144,10 @@ public class VertexAttribute public static VertexAttribute? BlendIndex(Accessors accessors, IDictionary? boneMap, IoNotifier notifier) { - if (!accessors.TryGetValue("JOINTS_0", out var accessor)) + if (!accessors.TryGetValue("JOINTS_0", out var jointsAccessor)) return null; - if (!accessors.ContainsKey("WEIGHTS_0")) + if (!accessors.TryGetValue("WEIGHTS_0", out var weightsAccessor)) throw notifier.Exception("Mesh contained JOINTS_0 attribute but no corresponding WEIGHTS_0 attribute."); if (boneMap == null) @@ -160,18 +160,21 @@ public class VertexAttribute Usage = (byte)MdlFile.VertexUsage.BlendIndices, }; - var values = accessor.AsVector4Array(); + var joints = jointsAccessor.AsVector4Array(); + var weights = weightsAccessor.AsVector4Array(); return new VertexAttribute( element, index => { - var gltfIndices = values[index]; + var gltfIndices = joints[index]; + var gltfWeights = weights[index]; + return BuildUByte4(new Vector4( - boneMap[(ushort)gltfIndices.X], - boneMap[(ushort)gltfIndices.Y], - boneMap[(ushort)gltfIndices.Z], - boneMap[(ushort)gltfIndices.W] + gltfWeights.X == 0 ? 0 : boneMap[(ushort)gltfIndices.X], + gltfWeights.Y == 0 ? 0 : boneMap[(ushort)gltfIndices.Y], + gltfWeights.Z == 0 ? 0 : boneMap[(ushort)gltfIndices.Z], + gltfWeights.W == 0 ? 0 : boneMap[(ushort)gltfIndices.W] )); } ); From 11acd7d3f46534ce445ef3700849f1f2c706491e Mon Sep 17 00:00:00 2001 From: ackwell Date: Sat, 20 Apr 2024 20:53:55 +1000 Subject: [PATCH 121/433] Prevent import failure when no materials are present --- Penumbra/Import/Models/Import/PrimitiveImporter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Import/Models/Import/PrimitiveImporter.cs b/Penumbra/Import/Models/Import/PrimitiveImporter.cs index 0c2968df..5df7597e 100644 --- a/Penumbra/Import/Models/Import/PrimitiveImporter.cs +++ b/Penumbra/Import/Models/Import/PrimitiveImporter.cs @@ -65,7 +65,7 @@ public class PrimitiveImporter ArgumentNullException.ThrowIfNull(_indices); ArgumentNullException.ThrowIfNull(_shapeValues); - var material = _primitive.Material.Name; + var material = _primitive.Material?.Name; if (material == "") material = null; From cc2f72b73df1218c094c45f9cfea3e5fa17ce534 Mon Sep 17 00:00:00 2001 From: ackwell Date: Sat, 20 Apr 2024 20:55:04 +1000 Subject: [PATCH 122/433] Use bg/ for absolute path example --- Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index 1cfa7585..1f4607cf 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -382,7 +382,7 @@ public partial class ModEditWindow { ImGuiComponents.HelpMarker( "Materials must be either relative (e.g. \"/filename.mtrl\")\n" - + "or absolute (e.g. \"chara/full/path/to/filename.mtrl\").", + + "or absolute (e.g. \"bg/full/path/to/filename.mtrl\").", FontAwesomeIcon.TimesCircle); } From 1bc3bb17c9b8a95cbe6c3edd8cf7912f45b174f2 Mon Sep 17 00:00:00 2001 From: ackwell Date: Mon, 22 Apr 2024 23:45:30 +1000 Subject: [PATCH 123/433] Fix havok parsing for non-ANSI user paths Also improve parsing because otter is better at c# than me --- Penumbra/Import/Models/HavokConverter.cs | 10 ++++------ Penumbra/Import/Models/SkeletonConverter.cs | 5 ++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/Penumbra/Import/Models/HavokConverter.cs b/Penumbra/Import/Models/HavokConverter.cs index 89f9ac4f..38c8749a 100644 --- a/Penumbra/Import/Models/HavokConverter.cs +++ b/Penumbra/Import/Models/HavokConverter.cs @@ -71,8 +71,7 @@ public static unsafe class HavokConverter /// Path to a file on the filesystem. private static hkResource* Read(string filePath) { - var path = Marshal.StringToHGlobalAnsi(filePath); - + var path = Encoding.UTF8.GetBytes(filePath); var builtinTypeRegistry = hkBuiltinTypeRegistry.Instance(); var loadOptions = stackalloc hkSerializeUtil.LoadOptions[1]; @@ -81,8 +80,7 @@ public static unsafe class HavokConverter loadOptions->TypeInfoRegistry = builtinTypeRegistry->GetTypeInfoRegistry(); // TODO: probably can use LoadFromBuffer for this. - var resource = hkSerializeUtil.LoadFromFile((byte*)path, null, loadOptions); - return resource; + return hkSerializeUtil.LoadFromFile(path, null, loadOptions); } /// Serializes an hkResource* to a temporary file. @@ -94,9 +92,9 @@ public static unsafe class HavokConverter ) { var tempFile = CreateTempFile(); - var path = Marshal.StringToHGlobalAnsi(tempFile); + var path = Encoding.UTF8.GetBytes(tempFile); var oStream = new hkOstream(); - oStream.Ctor((byte*)path); + oStream.Ctor(path); var result = stackalloc hkResult[1]; diff --git a/Penumbra/Import/Models/SkeletonConverter.cs b/Penumbra/Import/Models/SkeletonConverter.cs index 7058a159..25e74332 100644 --- a/Penumbra/Import/Models/SkeletonConverter.cs +++ b/Penumbra/Import/Models/SkeletonConverter.cs @@ -84,9 +84,8 @@ public static class SkeletonConverter .Where(n => n.NodeType != XmlNodeType.Comment) .Select(n => { - var text = n.InnerText.Trim()[1..]; - // TODO: surely there's a less shit way to do this I mean seriously - return BitConverter.ToSingle(BitConverter.GetBytes(int.Parse(text, NumberStyles.HexNumber))); + var text = n.InnerText.AsSpan().Trim()[1..]; + return BitConverter.Int32BitsToSingle(int.Parse(text, NumberStyles.HexNumber)); }) .ToArray(); From c276f922a53fc5798a9ae71673614a54bc88d9df Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 22 Apr 2024 18:22:03 +0200 Subject: [PATCH 124/433] Update API. --- Penumbra.Api | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.Api b/Penumbra.Api index 0c8578cf..590629df 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit 0c8578cfa12bf0591ed204fd89b30b66719f678f +Subproject commit 590629df33f9ad92baddd1d65ec8c986f18d608a From b34114400fab2f83b119ddb1ac2d8c2ff7e4a708 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 23 Apr 2024 15:09:53 +0200 Subject: [PATCH 125/433] Fix Havok ANSI / UTF8 Issue. --- Penumbra/Import/Models/HavokConverter.cs | 10 ++++------ Penumbra/Import/Models/SkeletonConverter.cs | 5 ++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/Penumbra/Import/Models/HavokConverter.cs b/Penumbra/Import/Models/HavokConverter.cs index 89f9ac4f..dc9d3e6a 100644 --- a/Penumbra/Import/Models/HavokConverter.cs +++ b/Penumbra/Import/Models/HavokConverter.cs @@ -71,8 +71,7 @@ public static unsafe class HavokConverter /// Path to a file on the filesystem. private static hkResource* Read(string filePath) { - var path = Marshal.StringToHGlobalAnsi(filePath); - + var path = Encoding.UTF8.GetBytes(filePath); var builtinTypeRegistry = hkBuiltinTypeRegistry.Instance(); var loadOptions = stackalloc hkSerializeUtil.LoadOptions[1]; @@ -81,8 +80,7 @@ public static unsafe class HavokConverter loadOptions->TypeInfoRegistry = builtinTypeRegistry->GetTypeInfoRegistry(); // TODO: probably can use LoadFromBuffer for this. - var resource = hkSerializeUtil.LoadFromFile((byte*)path, null, loadOptions); - return resource; + return hkSerializeUtil.LoadFromFile(path, null, loadOptions); } /// Serializes an hkResource* to a temporary file. @@ -94,9 +92,9 @@ public static unsafe class HavokConverter ) { var tempFile = CreateTempFile(); - var path = Marshal.StringToHGlobalAnsi(tempFile); + var path = Encoding.UTF8.GetBytes(tempFile); var oStream = new hkOstream(); - oStream.Ctor((byte*)path); + oStream.Ctor(path); var result = stackalloc hkResult[1]; diff --git a/Penumbra/Import/Models/SkeletonConverter.cs b/Penumbra/Import/Models/SkeletonConverter.cs index 7058a159..25e74332 100644 --- a/Penumbra/Import/Models/SkeletonConverter.cs +++ b/Penumbra/Import/Models/SkeletonConverter.cs @@ -84,9 +84,8 @@ public static class SkeletonConverter .Where(n => n.NodeType != XmlNodeType.Comment) .Select(n => { - var text = n.InnerText.Trim()[1..]; - // TODO: surely there's a less shit way to do this I mean seriously - return BitConverter.ToSingle(BitConverter.GetBytes(int.Parse(text, NumberStyles.HexNumber))); + var text = n.InnerText.AsSpan().Trim()[1..]; + return BitConverter.Int32BitsToSingle(int.Parse(text, NumberStyles.HexNumber)); }) .ToArray(); From e21c9fb6d1d71e0f952d416a8d1b0e817e450826 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 23 Apr 2024 15:11:09 +0200 Subject: [PATCH 126/433] Fix some IPC stuff. --- Penumbra/Api/IpcProviders.cs | 5 +++-- Penumbra/Api/IpcTester/UiIpcTester.cs | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Penumbra/Api/IpcProviders.cs b/Penumbra/Api/IpcProviders.cs index 21fe0a7c..ebf71176 100644 --- a/Penumbra/Api/IpcProviders.cs +++ b/Penumbra/Api/IpcProviders.cs @@ -62,6 +62,7 @@ public sealed class IpcProviders : IDisposable, IApiService IpcSubscribers.ApiVersion.Provider(pi, api), new FuncProvider<(int Major, int Minor)>(pi, "Penumbra.ApiVersions", () => api.ApiVersion), // backward compatibility + new FuncProvider(pi, "Penumbra.ApiVersion", () => api.ApiVersion.Breaking), // backward compatibility IpcSubscribers.GetModDirectory.Provider(pi, api.PluginState), IpcSubscribers.GetConfiguration.Provider(pi, api.PluginState), IpcSubscribers.ModDirectoryChanged.Provider(pi, api.PluginState), @@ -99,9 +100,9 @@ public sealed class IpcProviders : IDisposable, IApiService IpcSubscribers.ChangedItemTooltip.Provider(pi, api.Ui), IpcSubscribers.ChangedItemClicked.Provider(pi, api.Ui), IpcSubscribers.PreSettingsTabBarDraw.Provider(pi, api.Ui), - IpcSubscribers.PreSettingsPanelDraw.Provider(pi, api.Ui), + IpcSubscribers.PreSettingsDraw.Provider(pi, api.Ui), IpcSubscribers.PostEnabledDraw.Provider(pi, api.Ui), - IpcSubscribers.PostSettingsPanelDraw.Provider(pi, api.Ui), + IpcSubscribers.PostSettingsDraw.Provider(pi, api.Ui), IpcSubscribers.OpenMainWindow.Provider(pi, api.Ui), IpcSubscribers.CloseMainWindow.Provider(pi, api.Ui), ]; diff --git a/Penumbra/Api/IpcTester/UiIpcTester.cs b/Penumbra/Api/IpcTester/UiIpcTester.cs index d95b79b8..a2c36938 100644 --- a/Penumbra/Api/IpcTester/UiIpcTester.cs +++ b/Penumbra/Api/IpcTester/UiIpcTester.cs @@ -32,9 +32,9 @@ public class UiIpcTester : IUiService, IDisposable { _pi = pi; PreSettingsTabBar = IpcSubscribers.PreSettingsTabBarDraw.Subscriber(pi, UpdateLastDrawnMod); - PreSettingsPanel = IpcSubscribers.PreSettingsPanelDraw.Subscriber(pi, UpdateLastDrawnMod); + PreSettingsPanel = IpcSubscribers.PreSettingsDraw.Subscriber(pi, UpdateLastDrawnMod); PostEnabled = IpcSubscribers.PostEnabledDraw.Subscriber(pi, UpdateLastDrawnMod); - PostSettingsPanelDraw = IpcSubscribers.PostSettingsPanelDraw.Subscriber(pi, UpdateLastDrawnMod); + PostSettingsPanelDraw = IpcSubscribers.PostSettingsDraw.Subscriber(pi, UpdateLastDrawnMod); ChangedItemTooltip = IpcSubscribers.ChangedItemTooltip.Subscriber(pi, AddedTooltip); ChangedItemClicked = IpcSubscribers.ChangedItemClicked.Subscriber(pi, AddedClick); PreSettingsTabBar.Disable(); @@ -76,7 +76,7 @@ public class UiIpcTester : IUiService, IDisposable if (!table) return; - IpcTester.DrawIntro(IpcSubscribers.PostSettingsPanelDraw.Label, "Last Drawn Mod"); + IpcTester.DrawIntro(IpcSubscribers.PostSettingsDraw.Label, "Last Drawn Mod"); ImGui.TextUnformatted(_lastDrawnMod.Length > 0 ? $"{_lastDrawnMod} at {_lastDrawnModTime}" : "None"); IpcTester.DrawIntro(IpcSubscribers.ChangedItemTooltip.Label, "Add Tooltip"); From 792a04337f31f2c53ff562c3d8116821192fe58e Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 23 Apr 2024 15:50:09 +0200 Subject: [PATCH 127/433] Add a try-catch when scanning for mods. --- Penumbra/Mods/Manager/ModManager.cs | 36 ++++++++++++++++++----------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/Penumbra/Mods/Manager/ModManager.cs b/Penumbra/Mods/Manager/ModManager.cs index 40585520..d912e292 100644 --- a/Penumbra/Mods/Manager/ModManager.cs +++ b/Penumbra/Mods/Manager/ModManager.cs @@ -1,3 +1,4 @@ +using System.Security.AccessControl; using Penumbra.Communication; using Penumbra.Mods.Editor; using Penumbra.Services; @@ -311,22 +312,31 @@ public sealed class ModManager : ModStorage, IDisposable /// private void ScanMods() { - var options = new ParallelOptions() + try { - MaxDegreeOfParallelism = Math.Max(1, Environment.ProcessorCount / 2), - }; - var queue = new ConcurrentQueue(); - Parallel.ForEach(BasePath.EnumerateDirectories(), options, dir => - { - var mod = Creator.LoadMod(dir, false); - if (mod != null) - queue.Enqueue(mod); - }); + var options = new ParallelOptions() + { + MaxDegreeOfParallelism = Math.Max(1, Environment.ProcessorCount / 2), + }; + var queue = new ConcurrentQueue(); + Parallel.ForEach(BasePath.EnumerateDirectories(), options, dir => + { + var mod = Creator.LoadMod(dir, false); + if (mod != null) + queue.Enqueue(mod); + }); - foreach (var mod in queue) + foreach (var mod in queue) + { + mod.Index = Count; + Mods.Add(mod); + } + } + catch (Exception ex) { - mod.Index = Count; - Mods.Add(mod); + Valid = false; + _communicator.ModDirectoryChanged.Invoke(BasePath.FullName, false); + Penumbra.Log.Error($"Could not scan for mods:\n{ex}"); } } } From 07afbfb22974c4ca49d76bce109ff0a301d17b60 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 23 Apr 2024 17:41:55 +0200 Subject: [PATCH 128/433] Rework options, pre-submod types. --- Penumbra/Api/Api/ModSettingsApi.cs | 10 +- Penumbra/Import/TexToolsImporter.ModPack.cs | 2 +- Penumbra/Meta/MetaFileManager.cs | 8 +- Penumbra/Mods/Editor/ModEditor.cs | 30 +++- Penumbra/Mods/Editor/ModFileEditor.cs | 3 +- Penumbra/Mods/Editor/ModMerger.cs | 38 ++++-- Penumbra/Mods/Editor/ModNormalizer.cs | 72 +++++++--- Penumbra/Mods/Manager/ModCacheManager.cs | 10 +- Penumbra/Mods/Manager/ModMigration.cs | 12 +- Penumbra/Mods/Manager/ModOptionEditor.cs | 128 ++++++++---------- Penumbra/Mods/Mod.cs | 9 +- Penumbra/Mods/ModCreator.cs | 45 +++--- Penumbra/Mods/Subclasses/IModDataContainer.cs | 80 +++++++++++ Penumbra/Mods/Subclasses/IModGroup.cs | 14 +- Penumbra/Mods/Subclasses/IModOption.cs | 25 ++++ Penumbra/Mods/Subclasses/ModSettings.cs | 18 +-- Penumbra/Mods/Subclasses/MultiModGroup.cs | 86 ++++++++---- Penumbra/Mods/Subclasses/SingleModGroup.cs | 82 +++++++---- Penumbra/Mods/Subclasses/SubMod.cs | 110 +++++++++++++-- Penumbra/Mods/TemporaryMod.cs | 2 +- Penumbra/UI/AdvancedWindow/ItemSwapTab.cs | 17 ++- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 10 +- Penumbra/UI/AdvancedWindow/ModMergeTab.cs | 20 ++- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 50 ++++--- Penumbra/UI/ModsTab/ModPanelSettingsTab.cs | 39 +++--- 25 files changed, 620 insertions(+), 300 deletions(-) create mode 100644 Penumbra/Mods/Subclasses/IModDataContainer.cs create mode 100644 Penumbra/Mods/Subclasses/IModOption.cs diff --git a/Penumbra/Api/Api/ModSettingsApi.cs b/Penumbra/Api/Api/ModSettingsApi.cs index 2604a49d..5ed26ce5 100644 --- a/Penumbra/Api/Api/ModSettingsApi.cs +++ b/Penumbra/Api/Api/ModSettingsApi.cs @@ -56,13 +56,13 @@ public class ModSettingsApi : IPenumbraApiModSettings, IApiService, IDisposable var dict = new Dictionary(mod.Groups.Count); foreach (var g in mod.Groups) - dict.Add(g.Name, (g.Select(o => o.Name).ToArray(), (int)g.Type)); + dict.Add(g.Name, (g.Options.Select(o => o.Name).ToArray(), (int)g.Type)); return new AvailableModSettings(dict); } public Dictionary? GetAvailableModSettingsBase(string modDirectory, string modName) => _modManager.TryGetMod(modDirectory, modName, out var mod) - ? mod.Groups.ToDictionary(g => g.Name, g => (g.Select(o => o.Name).ToArray(), (int)g.Type)) + ? mod.Groups.ToDictionary(g => g.Name, g => (g.Options.Select(o => o.Name).ToArray(), (int)g.Type)) : null; public (PenumbraApiEc, (bool, int, Dictionary>, bool)?) GetCurrentModSettings(Guid collectionId, string modDirectory, @@ -153,7 +153,7 @@ public class ModSettingsApi : IPenumbraApiModSettings, IApiService, IDisposable if (groupIdx < 0) return ApiHelpers.Return(PenumbraApiEc.OptionGroupMissing, args); - var optionIdx = mod.Groups[groupIdx].IndexOf(o => o.Name == optionName); + var optionIdx = mod.Groups[groupIdx].Options.IndexOf(o => o.Name == optionName); if (optionIdx < 0) return ApiHelpers.Return(PenumbraApiEc.OptionMissing, args); @@ -190,7 +190,7 @@ public class ModSettingsApi : IPenumbraApiModSettings, IApiService, IDisposable { case SingleModGroup single: { - var optionIdx = optionNames.Count == 0 ? -1 : single.IndexOf(o => o.Name == optionNames[^1]); + var optionIdx = optionNames.Count == 0 ? -1 : single.OptionData.IndexOf(o => o.Name == optionNames[^1]); if (optionIdx < 0) return ApiHelpers.Return(PenumbraApiEc.OptionMissing, args); @@ -201,7 +201,7 @@ public class ModSettingsApi : IPenumbraApiModSettings, IApiService, IDisposable { foreach (var name in optionNames) { - var optionIdx = multi.IndexOf(o => o.Name == name); + var optionIdx = multi.PrioritizedOptions.IndexOf(o => o.Mod.Name == name); if (optionIdx < 0) return ApiHelpers.Return(PenumbraApiEc.OptionMissing, args); diff --git a/Penumbra/Import/TexToolsImporter.ModPack.cs b/Penumbra/Import/TexToolsImporter.ModPack.cs index f4b7d47e..7d9388a9 100644 --- a/Penumbra/Import/TexToolsImporter.ModPack.cs +++ b/Penumbra/Import/TexToolsImporter.ModPack.cs @@ -203,7 +203,7 @@ public partial class TexToolsImporter { var option = group.OptionList[idx]; _currentOptionName = option.Name; - options.Insert(idx, ModCreator.CreateEmptySubMod(option.Name)); + options.Insert(idx, SubMod.CreateForSaving(option.Name)); if (option.IsChecked) defaultSettings = Setting.Single(idx); } diff --git a/Penumbra/Meta/MetaFileManager.cs b/Penumbra/Meta/MetaFileManager.cs index 5283f77e..b1823bd7 100644 --- a/Penumbra/Meta/MetaFileManager.cs +++ b/Penumbra/Meta/MetaFileManager.cs @@ -54,7 +54,13 @@ public unsafe class MetaFileManager if (!dir.Exists) dir.Create(); - foreach (var option in group.OfType()) + var optionEnumerator = group switch + { + SingleModGroup single => single.OptionData, + MultiModGroup multi => multi.PrioritizedOptions.Select(o => o.Mod), + _ => [], + }; + foreach (var option in optionEnumerator) { var optionDir = ModCreator.NewOptionDirectory(dir, option.Name, Config.ReplaceNonAsciiOnImport); if (!optionDir.Exists) diff --git a/Penumbra/Mods/Editor/ModEditor.cs b/Penumbra/Mods/Editor/ModEditor.cs index d9781c06..0a96e0fd 100644 --- a/Penumbra/Mods/Editor/ModEditor.cs +++ b/Penumbra/Mods/Editor/ModEditor.cs @@ -1,3 +1,4 @@ +using System; using OtterGui; using OtterGui.Compression; using Penumbra.Mods.Subclasses; @@ -72,12 +73,18 @@ public class ModEditor( if (groupIdx >= 0) { Group = Mod.Groups[groupIdx]; - if (optionIdx >= 0 && optionIdx < Group.Count) + switch(Group) { - Option = Group[optionIdx]; - GroupIdx = groupIdx; - OptionIdx = optionIdx; - return; + case SingleModGroup single when optionIdx >= 0 && optionIdx < single.OptionData.Count: + Option = single.OptionData[optionIdx]; + GroupIdx = groupIdx; + OptionIdx = optionIdx; + return; + case MultiModGroup multi when optionIdx >= 0 && optionIdx < multi.PrioritizedOptions.Count: + Option = multi.PrioritizedOptions[optionIdx].Mod; + GroupIdx = groupIdx; + OptionIdx = optionIdx; + return; } } } @@ -109,8 +116,17 @@ public class ModEditor( action(mod.Default, -1, 0); foreach (var (group, groupIdx) in mod.Groups.WithIndex()) { - for (var optionIdx = 0; optionIdx < group.Count; ++optionIdx) - action(group[optionIdx], groupIdx, optionIdx); + switch (group) + { + case SingleModGroup single: + for (var optionIdx = 0; optionIdx < single.OptionData.Count; ++optionIdx) + action(single.OptionData[optionIdx], groupIdx, optionIdx); + break; + case MultiModGroup multi: + for (var optionIdx = 0; optionIdx < multi.PrioritizedOptions.Count; ++optionIdx) + action(multi.PrioritizedOptions[optionIdx].Mod, groupIdx, optionIdx); + break; + } } } diff --git a/Penumbra/Mods/Editor/ModFileEditor.cs b/Penumbra/Mods/Editor/ModFileEditor.cs index 4bdf4b1b..51615b05 100644 --- a/Penumbra/Mods/Editor/ModFileEditor.cs +++ b/Penumbra/Mods/Editor/ModFileEditor.cs @@ -24,7 +24,8 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu num += dict.TryAdd(path.Item2, file.File) ? 0 : 1; } - modManager.OptionEditor.OptionSetFiles(mod, option.GroupIdx, option.OptionIdx, dict); + var (groupIdx, optionIdx) = option.GetIndices(); + modManager.OptionEditor.OptionSetFiles(mod, groupIdx, optionIdx, dict); files.UpdatePaths(mod, option); Changes = false; return num; diff --git a/Penumbra/Mods/Editor/ModMerger.cs b/Penumbra/Mods/Editor/ModMerger.cs index 25590c49..74e9007c 100644 --- a/Penumbra/Mods/Editor/ModMerger.cs +++ b/Penumbra/Mods/Editor/ModMerger.cs @@ -1,5 +1,6 @@ using Dalamud.Interface.Internal.Notifications; using Dalamud.Utility; +using ImGuizmoNET; using OtterGui; using OtterGui.Classes; using Penumbra.Api.Enums; @@ -104,9 +105,16 @@ public class ModMerger : IDisposable ((List)Warnings).Add( $"The merged group {group.Name} already existed, but has a different type {group.Type} than the original group of type {originalGroup.Type}."); - foreach (var originalOption in originalGroup) + var optionEnumerator = group switch { - var (option, optionCreated) = _editor.FindOrAddOption(MergeToMod!, groupIdx, originalOption.Name); + SingleModGroup single => single.OptionData, + MultiModGroup multi => multi.PrioritizedOptions.Select(o => o.Mod), + _ => [], + }; + + foreach (var originalOption in optionEnumerator) + { + var (option, _, optionCreated) = _editor.FindOrAddOption(MergeToMod!, groupIdx, originalOption.Name); if (optionCreated) { _createdOptions.Add(option); @@ -138,7 +146,7 @@ public class ModMerger : IDisposable var (group, groupIdx, groupCreated) = _editor.FindOrAddModGroup(MergeToMod!, GroupType.Multi, groupName, SaveType.None); if (groupCreated) _createdGroups.Add(groupIdx); - var (option, optionCreated) = _editor.FindOrAddOption(MergeToMod!, groupIdx, optionName, SaveType.None); + var (option, _, optionCreated) = _editor.FindOrAddOption(MergeToMod!, groupIdx, optionName, SaveType.None); if (optionCreated) _createdOptions.Add(option); var dir = ModCreator.NewOptionDirectory(MergeToMod!.ModPath, groupName, _config.ReplaceNonAsciiOnImport); @@ -184,9 +192,10 @@ public class ModMerger : IDisposable } } - _editor.OptionSetFiles(MergeToMod!, option.GroupIdx, option.OptionIdx, redirections, SaveType.None); - _editor.OptionSetFileSwaps(MergeToMod!, option.GroupIdx, option.OptionIdx, swaps, SaveType.None); - _editor.OptionSetManipulations(MergeToMod!, option.GroupIdx, option.OptionIdx, manips, SaveType.ImmediateSync); + var (groupIdx, optionIdx) = option.GetIndices(); + _editor.OptionSetFiles(MergeToMod!, groupIdx, optionIdx, redirections, SaveType.None); + _editor.OptionSetFileSwaps(MergeToMod!, groupIdx, optionIdx, swaps, SaveType.None); + _editor.OptionSetManipulations(MergeToMod!, groupIdx, optionIdx, manips, SaveType.ImmediateSync); return; bool GetFullPath(FullPath input, out FullPath ret) @@ -251,7 +260,7 @@ public class ModMerger : IDisposable Mod? result = null; try { - dir = _creator.CreateEmptyMod(_mods.BasePath, modName, $"Split off from {mods[0].ParentMod.Name}."); + dir = _creator.CreateEmptyMod(_mods.BasePath, modName, $"Split off from {mods[0].Mod.Name}."); if (dir == null) throw new Exception($"Could not split off mods, unable to create new mod with name {modName}."); @@ -268,7 +277,6 @@ public class ModMerger : IDisposable { foreach (var originalOption in mods) { - var originalGroup = originalOption.ParentMod.Groups[originalOption.GroupIdx]; if (originalOption.IsDefault) { var files = CopySubModFiles(mods[0], dir); @@ -278,13 +286,14 @@ public class ModMerger : IDisposable } else { + var originalGroup = originalOption.Group; var (group, groupIdx, _) = _editor.FindOrAddModGroup(result, originalGroup.Type, originalGroup.Name); - var (option, _) = _editor.FindOrAddOption(result, groupIdx, originalOption.Name); + var (option, optionIdx, _) = _editor.FindOrAddOption(result, groupIdx, originalOption.Name); var folder = Path.Combine(dir.FullName, group.Name, option.Name); var files = CopySubModFiles(originalOption, new DirectoryInfo(folder)); - _editor.OptionSetFiles(result, groupIdx, option.OptionIdx, files); - _editor.OptionSetFileSwaps(result, groupIdx, option.OptionIdx, originalOption.FileSwapData); - _editor.OptionSetManipulations(result, groupIdx, option.OptionIdx, originalOption.ManipulationData); + _editor.OptionSetFiles(result, groupIdx, optionIdx, files); + _editor.OptionSetFileSwaps(result, groupIdx, optionIdx, originalOption.FileSwapData); + _editor.OptionSetManipulations(result, groupIdx, optionIdx, originalOption.ManipulationData); } } } @@ -309,7 +318,7 @@ public class ModMerger : IDisposable private static Dictionary CopySubModFiles(SubMod option, DirectoryInfo newMod) { var ret = new Dictionary(option.FileData.Count); - var parentPath = ((Mod)option.ParentMod).ModPath.FullName; + var parentPath = ((Mod)option.Mod).ModPath.FullName; foreach (var (path, file) in option.FileData) { var target = Path.GetRelativePath(parentPath, file.FullName); @@ -339,7 +348,8 @@ public class ModMerger : IDisposable { foreach (var option in _createdOptions) { - _editor.DeleteOption(MergeToMod!, option.GroupIdx, option.OptionIdx); + var (groupIdx, optionIdx) = option.GetIndices(); + _editor.DeleteOption(MergeToMod!, groupIdx, optionIdx); Penumbra.Log.Verbose($"[Merger] Removed option {option.FullName}."); } diff --git a/Penumbra/Mods/Editor/ModNormalizer.cs b/Penumbra/Mods/Editor/ModNormalizer.cs index a9a31212..9698fdcb 100644 --- a/Penumbra/Mods/Editor/ModNormalizer.cs +++ b/Penumbra/Mods/Editor/ModNormalizer.cs @@ -167,28 +167,27 @@ public class ModNormalizer(ModManager _modManager, Configuration _config) // Normalize all other options. foreach (var (group, groupIdx) in Mod.Groups.WithIndex()) { - _redirections[groupIdx + 1].EnsureCapacity(group.Count); - for (var i = _redirections[groupIdx + 1].Count; i < group.Count; ++i) - _redirections[groupIdx + 1].Add([]); - var groupDir = ModCreator.CreateModFolder(directory, group.Name, _config.ReplaceNonAsciiOnImport, true); - foreach (var option in group.OfType()) + switch (group) { - var optionDir = ModCreator.CreateModFolder(groupDir, option.Name, _config.ReplaceNonAsciiOnImport, true); + case SingleModGroup single: + _redirections[groupIdx + 1].EnsureCapacity(single.OptionData.Count); + for (var i = _redirections[groupIdx + 1].Count; i < single.OptionData.Count; ++i) + _redirections[groupIdx + 1].Add([]); - newDict = _redirections[groupIdx + 1][option.OptionIdx]; - newDict.Clear(); - newDict.EnsureCapacity(option.FileData.Count); - foreach (var (gamePath, fullPath) in option.FileData) - { - var relPath = new Utf8RelPath(gamePath).ToString(); - var newFullPath = Path.Combine(optionDir.FullName, relPath); - var redirectPath = new FullPath(Path.Combine(Mod.ModPath.FullName, groupDir.Name, optionDir.Name, relPath)); - Directory.CreateDirectory(Path.GetDirectoryName(newFullPath)!); - File.Copy(fullPath.FullName, newFullPath, true); - newDict.Add(gamePath, redirectPath); - ++Step; - } + foreach (var (option, optionIdx) in single.OptionData.WithIndex()) + HandleSubMod(groupDir, option, _redirections[groupIdx + 1][optionIdx]); + + break; + case MultiModGroup multi: + _redirections[groupIdx + 1].EnsureCapacity(multi.PrioritizedOptions.Count); + for (var i = _redirections[groupIdx + 1].Count; i < multi.PrioritizedOptions.Count; ++i) + _redirections[groupIdx + 1].Add([]); + + foreach (var ((option, _), optionIdx) in multi.PrioritizedOptions.WithIndex()) + HandleSubMod(groupDir, option, _redirections[groupIdx + 1][optionIdx]); + + break; } } @@ -200,6 +199,24 @@ public class ModNormalizer(ModManager _modManager, Configuration _config) } return false; + + void HandleSubMod(DirectoryInfo groupDir, SubMod option, Dictionary newDict) + { + var optionDir = ModCreator.CreateModFolder(groupDir, option.Name, _config.ReplaceNonAsciiOnImport, true); + + newDict.Clear(); + newDict.EnsureCapacity(option.FileData.Count); + foreach (var (gamePath, fullPath) in option.FileData) + { + var relPath = new Utf8RelPath(gamePath).ToString(); + var newFullPath = Path.Combine(optionDir.FullName, relPath); + var redirectPath = new FullPath(Path.Combine(Mod.ModPath.FullName, groupDir.Name, optionDir.Name, relPath)); + Directory.CreateDirectory(Path.GetDirectoryName(newFullPath)!); + File.Copy(fullPath.FullName, newFullPath, true); + newDict.Add(gamePath, redirectPath); + ++Step; + } + } } private bool MoveOldFiles() @@ -274,9 +291,20 @@ public class ModNormalizer(ModManager _modManager, Configuration _config) private void ApplyRedirections() { - foreach (var option in Mod.AllSubMods) - _modManager.OptionEditor.OptionSetFiles(Mod, option.GroupIdx, option.OptionIdx, - _redirections[option.GroupIdx + 1][option.OptionIdx]); + foreach (var (group, groupIdx) in Mod.Groups.WithIndex()) + { + switch (group) + { + case SingleModGroup single: + foreach (var (_, optionIdx) in single.OptionData.WithIndex()) + _modManager.OptionEditor.OptionSetFiles(Mod, groupIdx, optionIdx, _redirections[groupIdx + 1][optionIdx]); + break; + case MultiModGroup multi: + foreach (var (_, optionIdx) in multi.PrioritizedOptions.WithIndex()) + _modManager.OptionEditor.OptionSetFiles(Mod, groupIdx, optionIdx, _redirections[groupIdx + 1][optionIdx]); + break; + } + } ++Step; } diff --git a/Penumbra/Mods/Manager/ModCacheManager.cs b/Penumbra/Mods/Manager/ModCacheManager.cs index 99ad1a4f..df243781 100644 --- a/Penumbra/Mods/Manager/ModCacheManager.cs +++ b/Penumbra/Mods/Manager/ModCacheManager.cs @@ -2,6 +2,7 @@ using Penumbra.Communication; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Subclasses; using Penumbra.Services; namespace Penumbra.Mods.Manager; @@ -211,7 +212,14 @@ public class ModCacheManager : IDisposable foreach (var group in mod.Groups) { mod.HasOptions |= group.IsOption; - foreach (var s in group) + var optionEnumerator = group switch + { + SingleModGroup single => single.OptionData, + MultiModGroup multi => multi.PrioritizedOptions.Select(o => o.Mod), + _ => [], + }; + + foreach (var s in optionEnumerator) { mod.TotalFileCount += s.Files.Count; mod.TotalSwapCount += s.FileSwaps.Count; diff --git a/Penumbra/Mods/Manager/ModMigration.cs b/Penumbra/Mods/Manager/ModMigration.cs index 295afd7b..9c8ced89 100644 --- a/Penumbra/Mods/Manager/ModMigration.cs +++ b/Penumbra/Mods/Manager/ModMigration.cs @@ -126,7 +126,7 @@ public static partial class ModMigration case GroupType.Multi: var optionPriority = ModPriority.Default; - var newMultiGroup = new MultiModGroup() + var newMultiGroup = new MultiModGroup(mod) { Name = group.GroupName, Priority = priority++, @@ -134,7 +134,7 @@ public static partial class ModMigration }; mod.Groups.Add(newMultiGroup); foreach (var option in group.Options) - newMultiGroup.PrioritizedOptions.Add((SubModFromOption(creator, mod, option, seenMetaFiles), optionPriority++)); + newMultiGroup.PrioritizedOptions.Add((SubModFromOption(creator, mod, newMultiGroup, option, seenMetaFiles), optionPriority++)); break; case GroupType.Single: @@ -144,7 +144,7 @@ public static partial class ModMigration return; } - var newSingleGroup = new SingleModGroup() + var newSingleGroup = new SingleModGroup(mod) { Name = group.GroupName, Priority = priority++, @@ -152,7 +152,7 @@ public static partial class ModMigration }; mod.Groups.Add(newSingleGroup); foreach (var option in group.Options) - newSingleGroup.OptionData.Add(SubModFromOption(creator, mod, option, seenMetaFiles)); + newSingleGroup.OptionData.Add(SubModFromOption(creator, mod, newSingleGroup, option, seenMetaFiles)); break; } @@ -171,9 +171,9 @@ public static partial class ModMigration } } - private static SubMod SubModFromOption(ModCreator creator, Mod mod, OptionV0 option, HashSet seenMetaFiles) + private static SubMod SubModFromOption(ModCreator creator, Mod mod, IModGroup group, OptionV0 option, HashSet seenMetaFiles) { - var subMod = new SubMod(mod) { Name = option.OptionName }; + var subMod = new SubMod(mod, group) { Name = option.OptionName }; AddFilesToSubMod(subMod, mod.ModPath, option, seenMetaFiles); creator.IncorporateMetaChanges(subMod, mod.ModPath, false); return subMod; diff --git a/Penumbra/Mods/Manager/ModOptionEditor.cs b/Penumbra/Mods/Manager/ModOptionEditor.cs index 9d942574..e78b6209 100644 --- a/Penumbra/Mods/Manager/ModOptionEditor.cs +++ b/Penumbra/Mods/Manager/ModOptionEditor.cs @@ -87,12 +87,12 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS var maxPriority = mod.Groups.Count == 0 ? ModPriority.Default : mod.Groups.Max(o => o.Priority) + 1; mod.Groups.Add(type == GroupType.Multi - ? new MultiModGroup + ? new MultiModGroup(mod) { Name = newName, Priority = maxPriority, } - : new SingleModGroup + : new SingleModGroup(mod) { Name = newName, Priority = maxPriority, @@ -120,7 +120,6 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS { communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, -1, -1); mod.Groups.RemoveAt(groupIdx); - UpdateSubModPositions(mod, groupIdx); saveService.SaveAllOptionGroups(mod, false, config.ReplaceNonAsciiOnImport); communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupDeleted, mod, groupIdx, -1, -1); } @@ -131,7 +130,6 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS if (!mod.Groups.Move(groupIdxFrom, groupIdxTo)) return; - UpdateSubModPositions(mod, Math.Min(groupIdxFrom, groupIdxTo)); saveService.SaveAllOptionGroups(mod, false, config.ReplaceNonAsciiOnImport); communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupMoved, mod, groupIdxFrom, -1, groupIdxTo); } @@ -156,12 +154,9 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS /// Change the description of the given option. public void ChangeOptionDescription(Mod mod, int groupIdx, int optionIdx, string newDescription) { - var group = mod.Groups[groupIdx]; - var option = group[optionIdx]; - if (option.Description == newDescription) + if (!mod.Groups[groupIdx].ChangeOptionDescription(optionIdx, newDescription)) return; - option.Description = newDescription; saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, mod, groupIdx, optionIdx, -1); } @@ -173,12 +168,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS if (group.Priority == newPriority) return; - var _ = group switch - { - SingleModGroup s => s.Priority = newPriority, - MultiModGroup m => m.Priority = newPriority, - _ => newPriority, - }; + group.Priority = newPriority; saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); communicator.ModOptionChanged.Invoke(ModOptionChangeType.PriorityChanged, mod, groupIdx, -1, -1); } @@ -188,14 +178,11 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS { switch (mod.Groups[groupIdx]) { - case SingleModGroup: - ChangeGroupPriority(mod, groupIdx, newPriority); - break; - case MultiModGroup m: - if (m.PrioritizedOptions[optionIdx].Priority == newPriority) + case MultiModGroup multi: + if (multi.PrioritizedOptions[optionIdx].Priority == newPriority) return; - m.PrioritizedOptions[optionIdx] = (m.PrioritizedOptions[optionIdx].Mod, newPriority); + multi.PrioritizedOptions[optionIdx] = (multi.PrioritizedOptions[optionIdx].Mod, newPriority); saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); communicator.ModOptionChanged.Invoke(ModOptionChangeType.PriorityChanged, mod, groupIdx, optionIdx, -1); return; @@ -205,60 +192,63 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS /// Rename the given option. public void RenameOption(Mod mod, int groupIdx, int optionIdx, string newName) { - switch (mod.Groups[groupIdx]) - { - case SingleModGroup s: - if (s.OptionData[optionIdx].Name == newName) - return; - - s.OptionData[optionIdx].Name = newName; - break; - case MultiModGroup m: - var option = m.PrioritizedOptions[optionIdx].Mod; - if (option.Name == newName) - return; - - option.Name = newName; - break; - } + if (!mod.Groups[groupIdx].ChangeOptionName(optionIdx, newName)) + return; saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, mod, groupIdx, optionIdx, -1); } /// Add a new empty option of the given name for the given group. - public void AddOption(Mod mod, int groupIdx, string newName, SaveType saveType = SaveType.Queue) + public int AddOption(Mod mod, int groupIdx, string newName, SaveType saveType = SaveType.Queue) { - var group = mod.Groups[groupIdx]; - var subMod = new SubMod(mod) { Name = newName }; - subMod.SetPosition(groupIdx, group.Count); - switch (group) - { - case SingleModGroup s: - s.OptionData.Add(subMod); - break; - case MultiModGroup m: - m.PrioritizedOptions.Add((subMod, ModPriority.Default)); - break; - } + var group = mod.Groups[groupIdx]; + var idx = group.AddOption(mod, newName); + if (idx < 0) + return -1; saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, group.Count - 1, -1); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, idx, -1); + return idx; } /// Add a new empty option of the given name for the given group if it does not exist already. - public (SubMod, bool) FindOrAddOption(Mod mod, int groupIdx, string newName, SaveType saveType = SaveType.Queue) + public (SubMod, int, bool) FindOrAddOption(Mod mod, int groupIdx, string newName, SaveType saveType = SaveType.Queue) { var group = mod.Groups[groupIdx]; - var idx = group.IndexOf(o => o.Name == newName); - if (idx >= 0) - return ((SubMod)group[idx], false); + switch (group) + { + case SingleModGroup single: + { + var idx = single.OptionData.IndexOf(o => o.Name == newName); + if (idx >= 0) + return (single.OptionData[idx], idx, false); - AddOption(mod, groupIdx, newName, saveType); - if (group[^1].Name != newName) - throw new Exception($"Could not create new option with name {newName} in {group.Name}."); + idx = single.AddOption(mod, newName); + if (idx < 0) + throw new Exception($"Could not create new option with name {newName} in {group.Name}."); - return ((SubMod)group[^1], true); + saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, idx, -1); + return (single.OptionData[^1], single.OptionData.Count - 1, true); + } + case MultiModGroup multi: + { + var idx = multi.PrioritizedOptions.IndexOf(o => o.Mod.Name == newName); + if (idx >= 0) + return (multi.PrioritizedOptions[idx].Mod, idx, false); + + idx = multi.AddOption(mod, newName); + if (idx < 0) + throw new Exception($"Could not create new option with name {newName} in {group.Name}."); + + saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, idx, -1); + return (multi.PrioritizedOptions[^1].Mod, multi.PrioritizedOptions.Count - 1, true); + } + } + + throw new Exception($"{nameof(FindOrAddOption)} is not supported for mod groups of type {group.GetType()}."); } /// Add an existing option to a given group with default priority. @@ -269,25 +259,28 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS public void AddOption(Mod mod, int groupIdx, SubMod option, ModPriority priority) { var group = mod.Groups[groupIdx]; + int idx; switch (group) { - case MultiModGroup { Count: >= IModGroup.MaxMultiOptions }: + case MultiModGroup { PrioritizedOptions.Count: >= IModGroup.MaxMultiOptions }: Penumbra.Log.Error( $"Could not add option {option.Name} to {group.Name} for mod {mod.Name}, " + $"since only up to {IModGroup.MaxMultiOptions} options are supported in one group."); return; case SingleModGroup s: - option.SetPosition(groupIdx, s.Count); + idx = s.OptionData.Count; s.OptionData.Add(option); break; case MultiModGroup m: - option.SetPosition(groupIdx, m.Count); + idx = m.PrioritizedOptions.Count; m.PrioritizedOptions.Add((option, priority)); break; + default: + return; } saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, group.Count - 1, -1); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, idx, -1); } /// Delete the given option from the given group. @@ -306,7 +299,6 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS break; } - group.UpdatePositions(optionIdx); saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionDeleted, mod, groupIdx, optionIdx, -1); } @@ -396,16 +388,6 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS return false; } - /// Update the indices stored in options from a given group on. - private static void UpdateSubModPositions(Mod mod, int fromGroup) - { - foreach (var (group, groupIdx) in mod.Groups.WithIndex().Skip(fromGroup)) - { - foreach (var (o, optionIdx) in group.OfType().WithIndex()) - o.SetPosition(groupIdx, optionIdx); - } - } - /// Get the correct option for the given group and option index. private static SubMod GetSubMod(Mod mod, int groupIdx, int optionIdx) { diff --git a/Penumbra/Mods/Mod.cs b/Penumbra/Mods/Mod.cs index 25f3c510..71f64205 100644 --- a/Penumbra/Mods/Mod.cs +++ b/Penumbra/Mods/Mod.cs @@ -38,7 +38,7 @@ public sealed class Mod : IMod internal Mod(DirectoryInfo modPath) { ModPath = modPath; - Default = new SubMod(this); + Default = SubMod.CreateDefault(this); } public override string ToString() @@ -82,7 +82,12 @@ public sealed class Mod : IMod } public IEnumerable AllSubMods - => Groups.SelectMany(o => o).Prepend(Default); + => Groups.SelectMany(o => o switch + { + SingleModGroup single => single.OptionData, + MultiModGroup multi => multi.PrioritizedOptions.Select(s => s.Mod), + _ => [], + }).Prepend(Default); public List FindUnusedFiles() { diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index 661dd6fb..4d32f395 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -16,7 +16,11 @@ using Penumbra.String.Classes; namespace Penumbra.Mods; -public partial class ModCreator(SaveService _saveService, Configuration config, ModDataEditor _dataEditor, MetaFileManager _metaFileManager, +public partial class ModCreator( + SaveService _saveService, + Configuration config, + ModDataEditor _dataEditor, + MetaFileManager _metaFileManager, GamePathParser _gamePathParser) { public readonly Configuration Config = config; @@ -106,7 +110,6 @@ public partial class ModCreator(SaveService _saveService, Configuration config, public void LoadDefaultOption(Mod mod) { var defaultFile = _saveService.FileNames.OptionGroupFile(mod, -1, Config.ReplaceNonAsciiOnImport); - mod.Default.SetPosition(-1, 0); try { if (!File.Exists(defaultFile)) @@ -241,27 +244,21 @@ public partial class ModCreator(SaveService _saveService, Configuration config, { case GroupType.Multi: { - var group = new MultiModGroup() - { - Name = name, - Description = desc, - Priority = priority, - DefaultSettings = defaultSettings, - }; + var group = MultiModGroup.CreateForSaving(name); + group.Description = desc; + group.Priority = priority; + group.DefaultSettings = defaultSettings; group.PrioritizedOptions.AddRange(subMods.Select((s, idx) => (s, new ModPriority(idx)))); _saveService.ImmediateSaveSync(new ModSaveGroup(baseFolder, group, index, Config.ReplaceNonAsciiOnImport)); break; } case GroupType.Single: { - var group = new SingleModGroup() - { - Name = name, - Description = desc, - Priority = priority, - DefaultSettings = defaultSettings, - }; - group.OptionData.AddRange(subMods.OfType()); + var group = SingleModGroup.CreateForSaving(name); + group.Description = desc; + group.Priority = priority; + group.DefaultSettings = defaultSettings; + group.OptionData.AddRange(subMods); _saveService.ImmediateSaveSync(new ModSaveGroup(baseFolder, group, index, Config.ReplaceNonAsciiOnImport)); break; } @@ -275,11 +272,8 @@ public partial class ModCreator(SaveService _saveService, Configuration config, .Select(f => (Utf8GamePath.FromFile(f, optionFolder, out var gamePath, true), gamePath, new FullPath(f))) .Where(t => t.Item1); - var mod = new SubMod(null!) // Mod is irrelevant here, only used for saving. - { - Name = option.Name, - Description = option.Description, - }; + var mod = SubMod.CreateForSaving(option.Name); + mod.Description = option.Description; foreach (var (_, gamePath, file) in list) mod.FileData.TryAdd(gamePath, file); @@ -287,13 +281,6 @@ public partial class ModCreator(SaveService _saveService, Configuration config, return mod; } - /// Create an empty sub mod for single groups with None options. - internal static SubMod CreateEmptySubMod(string name) - => new SubMod(null!) // Mod is irrelevant here, only used for saving. - { - Name = name, - }; - /// /// Create the default data file from all unused files that were not handled before /// and are used in sub mods. diff --git a/Penumbra/Mods/Subclasses/IModDataContainer.cs b/Penumbra/Mods/Subclasses/IModDataContainer.cs new file mode 100644 index 00000000..d0b444b8 --- /dev/null +++ b/Penumbra/Mods/Subclasses/IModDataContainer.cs @@ -0,0 +1,80 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Penumbra.Meta.Manipulations; +using Penumbra.String.Classes; + +namespace Penumbra.Mods.Subclasses; + +public interface IModDataContainer +{ + public Dictionary Files { get; set; } + public Dictionary FileSwaps { get; set; } + public HashSet Manipulations { get; set; } + + public void AddDataTo(Dictionary redirections, HashSet manipulations) + { + foreach (var (path, file) in Files) + redirections.TryAdd(path, file); + + foreach (var (path, file) in FileSwaps) + redirections.TryAdd(path, file); + manipulations.UnionWith(Manipulations); + } + + public static void Load(JToken json, IModDataContainer data, DirectoryInfo basePath) + { + data.Files.Clear(); + data.FileSwaps.Clear(); + data.Manipulations.Clear(); + + var files = (JObject?)json[nameof(Files)]; + if (files != null) + foreach (var property in files.Properties()) + { + if (Utf8GamePath.FromString(property.Name, out var p, true)) + data.Files.TryAdd(p, new FullPath(basePath, property.Value.ToObject())); + } + + var swaps = (JObject?)json[nameof(FileSwaps)]; + if (swaps != null) + foreach (var property in swaps.Properties()) + { + if (Utf8GamePath.FromString(property.Name, out var p, true)) + data.FileSwaps.TryAdd(p, new FullPath(property.Value.ToObject()!)); + } + + var manips = json[nameof(Manipulations)]; + if (manips != null) + foreach (var s in manips.Children().Select(c => c.ToObject()) + .Where(m => m.Validate())) + data.Manipulations.Add(s); + } + + public static void WriteModData(JsonWriter j, JsonSerializer serializer, IModDataContainer data, DirectoryInfo basePath) + { + j.WritePropertyName(nameof(data.Files)); + j.WriteStartObject(); + foreach (var (gamePath, file) in data.Files) + { + if (file.ToRelPath(basePath, out var relPath)) + { + j.WritePropertyName(gamePath.ToString()); + j.WriteValue(relPath.ToString()); + } + } + + j.WriteEndObject(); + j.WritePropertyName(nameof(data.FileSwaps)); + j.WriteStartObject(); + foreach (var (gamePath, file) in data.FileSwaps) + { + j.WritePropertyName(gamePath.ToString()); + j.WriteValue(file.ToString()); + } + + j.WriteEndObject(); + j.WritePropertyName(nameof(data.Manipulations)); + serializer.Serialize(j, data.Manipulations); + j.WriteEndObject(); + } +} diff --git a/Penumbra/Mods/Subclasses/IModGroup.cs b/Penumbra/Mods/Subclasses/IModGroup.cs index 96d7c6b7..a046ade0 100644 --- a/Penumbra/Mods/Subclasses/IModGroup.cs +++ b/Penumbra/Mods/Subclasses/IModGroup.cs @@ -6,25 +6,27 @@ using Penumbra.String.Classes; namespace Penumbra.Mods.Subclasses; -public interface IModGroup : IReadOnlyCollection +public interface IModGroup { public const int MaxMultiOptions = 63; + public Mod Mod { get; } public string Name { get; } public string Description { get; } public GroupType Type { get; } - public ModPriority Priority { get; } + public ModPriority Priority { get; set; } public Setting DefaultSettings { get; set; } public FullPath? FindBestMatch(Utf8GamePath gamePath); + public int AddOption(Mod mod, string name, string description = ""); + public bool ChangeOptionDescription(int optionIndex, string newDescription); + public bool ChangeOptionName(int optionIndex, string newName); - public SubMod this[Index idx] { get; } - - public bool IsOption { get; } + public IReadOnlyList Options { get; } + public bool IsOption { get; } public IModGroup Convert(GroupType type); public bool MoveOption(int optionIdxFrom, int optionIdxTo); - public void UpdatePositions(int from = 0); public void AddData(Setting setting, Dictionary redirections, HashSet manipulations); diff --git a/Penumbra/Mods/Subclasses/IModOption.cs b/Penumbra/Mods/Subclasses/IModOption.cs new file mode 100644 index 00000000..bb52a2cd --- /dev/null +++ b/Penumbra/Mods/Subclasses/IModOption.cs @@ -0,0 +1,25 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace Penumbra.Mods.Subclasses; + +public interface IModOption +{ + public string Name { get; set; } + public string FullName { get; } + public string Description { get; set; } + + public static void Load(JToken json, IModOption option) + { + option.Name = json[nameof(Name)]?.ToObject() ?? string.Empty; + option.Description = json[nameof(Description)]?.ToObject() ?? string.Empty; + } + + public static void WriteModOption(JsonWriter j, IModOption option) + { + j.WritePropertyName(nameof(Name)); + j.WriteValue(option.Name); + j.WritePropertyName(nameof(Description)); + j.WriteValue(option.Description); + } +} diff --git a/Penumbra/Mods/Subclasses/ModSettings.cs b/Penumbra/Mods/Subclasses/ModSettings.cs index 81a3bb41..2ddabdb8 100644 --- a/Penumbra/Mods/Subclasses/ModSettings.cs +++ b/Penumbra/Mods/Subclasses/ModSettings.cs @@ -68,7 +68,7 @@ public class ModSettings var config = Settings[groupIdx]; Settings[groupIdx] = group.Type switch { - GroupType.Single => config.TurnMulti(group.Count), + GroupType.Single => config.TurnMulti(group.Options.Count), GroupType.Multi => Setting.Multi((int)config.Value), _ => config, }; @@ -182,15 +182,15 @@ public class ModSettings if (idx >= mod.Groups.Count) break; - var group = mod.Groups[idx]; - if (group.Type == GroupType.Single && setting.Value < (ulong)group.Count) + switch (mod.Groups[idx]) { - dict.Add(group.Name, [group[(int)setting.Value].Name]); - } - else - { - var list = group.Where((_, optionIdx) => (setting.Value & (1ul << optionIdx)) != 0).Select(o => o.Name).ToList(); - dict.Add(group.Name, list); + case SingleModGroup single when setting.Value < (ulong)single.Options.Count: + dict.Add(single.Name, [single.Options[setting.AsIndex].Name]); + break; + case MultiModGroup multi: + var list = multi.Options.WithIndex().Where(p => setting.HasFlag(p.Index)).Select(p => p.Value.Name).ToList(); + dict.Add(multi.Name, list); + break; } } diff --git a/Penumbra/Mods/Subclasses/MultiModGroup.cs b/Penumbra/Mods/Subclasses/MultiModGroup.cs index 02ae07f4..4ec2c72a 100644 --- a/Penumbra/Mods/Subclasses/MultiModGroup.cs +++ b/Penumbra/Mods/Subclasses/MultiModGroup.cs @@ -1,5 +1,4 @@ using Dalamud.Interface.Internal.Notifications; -using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui; using OtterGui.Classes; @@ -11,11 +10,12 @@ using Penumbra.String.Classes; namespace Penumbra.Mods.Subclasses; /// Groups that allow all available options to be selected at once. -public sealed class MultiModGroup : IModGroup +public sealed class MultiModGroup(Mod mod) : IModGroup { public GroupType Type => GroupType.Multi; + public Mod Mod { get; set; } = mod; public string Name { get; set; } = "Group"; public string Description { get; set; } = "A non-exclusive group of settings."; public ModPriority Priority { get; set; } @@ -26,27 +26,58 @@ public sealed class MultiModGroup : IModGroup .SelectWhere(o => (o.Mod.FileData.TryGetValue(gamePath, out var file) || o.Mod.FileSwapData.TryGetValue(gamePath, out file), file)) .FirstOrDefault(); - public SubMod this[Index idx] - => PrioritizedOptions[idx].Mod; + public int AddOption(Mod mod, string name, string description = "") + { + var groupIdx = mod.Groups.IndexOf(this); + if (groupIdx < 0) + return -1; + + var subMod = new SubMod(mod, this) + { + Name = name, + Description = description, + }; + PrioritizedOptions.Add((subMod, ModPriority.Default)); + return PrioritizedOptions.Count - 1; + } + + public bool ChangeOptionDescription(int optionIndex, string newDescription) + { + if (optionIndex < 0 || optionIndex >= PrioritizedOptions.Count) + return false; + + var option = PrioritizedOptions[optionIndex].Mod; + if (option.Description == newDescription) + return false; + + option.Description = newDescription; + return true; + } + + public bool ChangeOptionName(int optionIndex, string newName) + { + if (optionIndex < 0 || optionIndex >= PrioritizedOptions.Count) + return false; + + var option = PrioritizedOptions[optionIndex].Mod; + if (option.Name == newName) + return false; + + option.Name = newName; + return true; + } + + public IReadOnlyList Options + => PrioritizedOptions.Select(p => p.Mod).ToArray(); public bool IsOption - => Count > 0; - - [JsonIgnore] - public int Count - => PrioritizedOptions.Count; + => PrioritizedOptions.Count > 0; public readonly List<(SubMod Mod, ModPriority Priority)> PrioritizedOptions = []; - public IEnumerator GetEnumerator() - => PrioritizedOptions.Select(o => o.Mod).GetEnumerator(); - - IEnumerator IEnumerable.GetEnumerator() - => GetEnumerator(); - public static MultiModGroup? Load(Mod mod, JObject json, int groupIdx) { - var ret = new MultiModGroup() + var ret = new MultiModGroup(mod) { Name = json[nameof(Name)]?.ToObject() ?? string.Empty, Description = json[nameof(Description)]?.ToObject() ?? string.Empty, @@ -68,8 +99,7 @@ public sealed class MultiModGroup : IModGroup break; } - var subMod = new SubMod(mod); - subMod.SetPosition(groupIdx, ret.PrioritizedOptions.Count); + var subMod = new SubMod(mod, ret); subMod.Load(mod.ModPath, child, out var priority); ret.PrioritizedOptions.Add((subMod, priority)); } @@ -85,12 +115,12 @@ public sealed class MultiModGroup : IModGroup { case GroupType.Multi: return this; case GroupType.Single: - var multi = new SingleModGroup() + var multi = new SingleModGroup(Mod) { Name = Name, Description = Description, Priority = Priority, - DefaultSettings = DefaultSettings.TurnMulti(Count), + DefaultSettings = DefaultSettings.TurnMulti(PrioritizedOptions.Count), }; multi.OptionData.AddRange(PrioritizedOptions.Select(p => p.Mod)); return multi; @@ -104,16 +134,9 @@ public sealed class MultiModGroup : IModGroup return false; DefaultSettings = DefaultSettings.MoveBit(optionIdxFrom, optionIdxTo); - UpdatePositions(Math.Min(optionIdxFrom, optionIdxTo)); return true; } - public void UpdatePositions(int from = 0) - { - foreach (var ((o, _), i) in PrioritizedOptions.WithIndex().Skip(from)) - o.SetPosition(o.GroupIdx, i); - } - public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) { foreach (var (option, index) in PrioritizedOptions.WithIndex().OrderByDescending(o => o.Value.Priority)) @@ -124,5 +147,12 @@ public sealed class MultiModGroup : IModGroup } public Setting FixSetting(Setting setting) - => new(setting.Value & ((1ul << Count) - 1)); + => new(setting.Value & ((1ul << PrioritizedOptions.Count) - 1)); + + /// Create a group without a mod only for saving it in the creator. + internal static MultiModGroup CreateForSaving(string name) + => new(null!) + { + Name = name, + }; } diff --git a/Penumbra/Mods/Subclasses/SingleModGroup.cs b/Penumbra/Mods/Subclasses/SingleModGroup.cs index b854d2b1..994a1f96 100644 --- a/Penumbra/Mods/Subclasses/SingleModGroup.cs +++ b/Penumbra/Mods/Subclasses/SingleModGroup.cs @@ -1,4 +1,3 @@ -using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui; using OtterGui.Filesystem; @@ -9,11 +8,12 @@ using Penumbra.String.Classes; namespace Penumbra.Mods.Subclasses; /// Groups that allow only one of their available options to be selected. -public sealed class SingleModGroup : IModGroup +public sealed class SingleModGroup(Mod mod) : IModGroup { public GroupType Type => GroupType.Single; + public Mod Mod { get; set; } = mod; public string Name { get; set; } = "Option"; public string Description { get; set; } = "A mutually exclusive group of settings."; public ModPriority Priority { get; set; } @@ -26,26 +26,53 @@ public sealed class SingleModGroup : IModGroup .SelectWhere(m => (m.FileData.TryGetValue(gamePath, out var file) || m.FileSwapData.TryGetValue(gamePath, out file), file)) .FirstOrDefault(); - public SubMod this[Index idx] - => OptionData[idx]; + public int AddOption(Mod mod, string name, string description = "") + { + var subMod = new SubMod(mod, this) + { + Name = name, + Description = description, + }; + OptionData.Add(subMod); + return OptionData.Count - 1; + } + + public bool ChangeOptionDescription(int optionIndex, string newDescription) + { + if (optionIndex < 0 || optionIndex >= OptionData.Count) + return false; + + var option = OptionData[optionIndex]; + if (option.Description == newDescription) + return false; + + option.Description = newDescription; + return true; + } + + public bool ChangeOptionName(int optionIndex, string newName) + { + if (optionIndex < 0 || optionIndex >= OptionData.Count) + return false; + + var option = OptionData[optionIndex]; + if (option.Name == newName) + return false; + + option.Name = newName; + return true; + } + + public IReadOnlyList Options + => OptionData; public bool IsOption - => Count > 1; - - [JsonIgnore] - public int Count - => OptionData.Count; - - public IEnumerator GetEnumerator() - => OptionData.GetEnumerator(); - - IEnumerator IEnumerable.GetEnumerator() - => GetEnumerator(); + => OptionData.Count > 1; public static SingleModGroup? Load(Mod mod, JObject json, int groupIdx) { var options = json["Options"]; - var ret = new SingleModGroup + var ret = new SingleModGroup(mod) { Name = json[nameof(Name)]?.ToObject() ?? string.Empty, Description = json[nameof(Description)]?.ToObject() ?? string.Empty, @@ -58,8 +85,7 @@ public sealed class SingleModGroup : IModGroup if (options != null) foreach (var child in options.Children()) { - var subMod = new SubMod(mod); - subMod.SetPosition(groupIdx, ret.OptionData.Count); + var subMod = new SubMod(mod, ret); subMod.Load(mod.ModPath, child, out _); ret.OptionData.Add(subMod); } @@ -74,7 +100,7 @@ public sealed class SingleModGroup : IModGroup { case GroupType.Single: return this; case GroupType.Multi: - var multi = new MultiModGroup() + var multi = new MultiModGroup(Mod) { Name = Name, Description = Description, @@ -108,19 +134,19 @@ public sealed class SingleModGroup : IModGroup DefaultSettings = Setting.Single(currentIndex + 1); } - UpdatePositions(Math.Min(optionIdxFrom, optionIdxTo)); return true; } - public void UpdatePositions(int from = 0) - { - foreach (var (o, i) in OptionData.WithIndex().Skip(from)) - o.SetPosition(o.GroupIdx, i); - } - public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) - => this[setting.AsIndex].AddData(redirections, manipulations); + => OptionData[setting.AsIndex].AddData(redirections, manipulations); public Setting FixSetting(Setting setting) - => Count == 0 ? Setting.Zero : new Setting(Math.Min(setting.Value, (ulong)(Count - 1))); + => OptionData.Count == 0 ? Setting.Zero : new Setting(Math.Min(setting.Value, (ulong)(OptionData.Count - 1))); + + /// Create a group without a mod only for saving it in the creator. + internal static SingleModGroup CreateForSaving(string name) + => new(null!) + { + Name = name, + }; } diff --git a/Penumbra/Mods/Subclasses/SubMod.cs b/Penumbra/Mods/Subclasses/SubMod.cs index 386910e5..bc93fcc4 100644 --- a/Penumbra/Mods/Subclasses/SubMod.cs +++ b/Penumbra/Mods/Subclasses/SubMod.cs @@ -1,11 +1,62 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using OtterGui; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Editor; using Penumbra.String.Classes; namespace Penumbra.Mods.Subclasses; +public class SingleSubMod(Mod mod, SingleModGroup group) : IModOption, IModDataContainer +{ + internal readonly Mod Mod = mod; + internal readonly SingleModGroup Group = group; + + public string Name { get; set; } = "Option"; + + public string FullName + => $"{Group.Name}: {Name}"; + + public string Description { get; set; } = string.Empty; + + public Dictionary Files { get; set; } = []; + public Dictionary FileSwaps { get; set; } = []; + public HashSet Manipulations { get; set; } = []; +} + +public class MultiSubMod(Mod mod, MultiModGroup group) : IModOption, IModDataContainer +{ + internal readonly Mod Mod = mod; + internal readonly MultiModGroup Group = group; + + public string Name { get; set; } = "Option"; + + public string FullName + => $"{Group.Name}: {Name}"; + + public string Description { get; set; } = string.Empty; + public ModPriority Priority { get; set; } = ModPriority.Default; + + public Dictionary Files { get; set; } = []; + public Dictionary FileSwaps { get; set; } = []; + public HashSet Manipulations { get; set; } = []; +} + +public class DefaultSubMod(IMod mod) : IModDataContainer +{ + public string FullName + => "Default Option"; + + public string Description + => string.Empty; + + internal readonly IMod Mod = mod; + + public Dictionary Files { get; set; } = []; + public Dictionary FileSwaps { get; set; } = []; + public HashSet Manipulations { get; set; } = []; +} + /// /// A sub mod is a collection of /// - file replacements @@ -16,21 +67,51 @@ namespace Penumbra.Mods.Subclasses; /// Nothing is checked for existence or validity when loading. /// Objects are also not checked for uniqueness, the first appearance of a game path or meta path decides. /// -public sealed class SubMod +public sealed class SubMod(IMod mod, IModGroup group) : IModOption { public string Name { get; set; } = "Default"; public string FullName - => GroupIdx < 0 ? "Default Option" : $"{ParentMod.Groups[GroupIdx].Name}: {Name}"; + => Group == null ? "Default Option" : $"{Group.Name}: {Name}"; public string Description { get; set; } = string.Empty; - internal IMod ParentMod { get; private init; } - internal int GroupIdx { get; private set; } - internal int OptionIdx { get; private set; } + internal readonly IMod Mod = mod; + internal readonly IModGroup? Group = group; + internal (int GroupIdx, int OptionIdx) GetIndices() + { + if (IsDefault) + return (-1, 0); + + var groupIdx = Mod.Groups.IndexOf(Group); + if (groupIdx < 0) + throw new Exception($"Group {Group.Name} from SubMod {Name} is not contained in Mod {Mod.Name}."); + + return (groupIdx, GetOptionIndex()); + } + + private int GetOptionIndex() + { + var optionIndex = Group switch + { + null => 0, + SingleModGroup single => single.OptionData.IndexOf(this), + MultiModGroup multi => multi.PrioritizedOptions.IndexOf(p => p.Mod == this), + _ => throw new Exception($"Group {Group.Name} from SubMod {Name} has unknown type {typeof(Group)}"), + }; + if (optionIndex < 0) + throw new Exception($"Group {Group!.Name} from SubMod {Name} does not contain this SubMod."); + + return optionIndex; + } + + public static SubMod CreateDefault(IMod mod) + => new(mod, null!); + + [MemberNotNullWhen(false, nameof(Group))] public bool IsDefault - => GroupIdx < 0; + => Group == null; public void AddData(Dictionary redirections, HashSet manipulations) { @@ -46,9 +127,6 @@ public sealed class SubMod public Dictionary FileSwapData = []; public HashSet ManipulationData = []; - public SubMod(IMod parentMod) - => ParentMod = parentMod; - public IReadOnlyDictionary Files => FileData; @@ -58,12 +136,6 @@ public sealed class SubMod public IReadOnlySet Manipulations => ManipulationData; - public void SetPosition(int groupIdx, int optionIdx) - { - GroupIdx = groupIdx; - OptionIdx = optionIdx; - } - public void Load(DirectoryInfo basePath, JToken json, out ModPriority priority) { FileData.Clear(); @@ -116,6 +188,14 @@ public sealed class SubMod } } + /// Create a sub mod without a mod or group only for saving it in the creator. + internal static SubMod CreateForSaving(string name) + => new(null!, null!) + { + Name = name, + }; + + public static void WriteSubMod(JsonWriter j, JsonSerializer serializer, SubMod mod, DirectoryInfo basePath, ModPriority? priority) { j.WriteStartObject(); diff --git a/Penumbra/Mods/TemporaryMod.cs b/Penumbra/Mods/TemporaryMod.cs index 41c1211f..a599b3bb 100644 --- a/Penumbra/Mods/TemporaryMod.cs +++ b/Penumbra/Mods/TemporaryMod.cs @@ -49,7 +49,7 @@ public class TemporaryMod : IMod => [Default]; public TemporaryMod() - => Default = new SubMod(this); + => Default = SubMod.CreateDefault(this); public void SetFile(Utf8GamePath gamePath, FullPath fullPath) => Default.FileData[gamePath] = fullPath; diff --git a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs index 7b5ce2dc..5125a5b2 100644 --- a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs +++ b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs @@ -253,7 +253,7 @@ public class ItemSwapTab : IDisposable, ITab _subModValid = _mod != null && _newGroupName.Length > 0 && _newOptionName.Length > 0 - && (_selectedGroup?.All(o => o.Name != _newOptionName) ?? true); + && (_selectedGroup?.Options.All(o => o.Name != _newOptionName) ?? true); } private void CreateMod() @@ -275,7 +275,7 @@ public class ItemSwapTab : IDisposable, ITab var groupCreated = false; var dirCreated = false; - var optionCreated = false; + var optionCreated = -1; DirectoryInfo? optionFolderName = null; try { @@ -294,14 +294,17 @@ public class ItemSwapTab : IDisposable, ITab groupCreated = true; } - _modManager.OptionEditor.AddOption(_mod, _mod.Groups.IndexOf(_selectedGroup), _newOptionName); - optionCreated = true; + var optionIdx = _modManager.OptionEditor.AddOption(_mod, _mod.Groups.IndexOf(_selectedGroup), _newOptionName); + if (optionIdx < 0) + throw new Exception($"Failure creating mod option."); + + optionCreated = optionIdx; optionFolderName = Directory.CreateDirectory(optionFolderName.FullName); dirCreated = true; if (!_swapData.WriteMod(_modManager, _mod, _useFileSwaps ? ItemSwapContainer.WriteType.UseSwaps : ItemSwapContainer.WriteType.NoSwaps, optionFolderName, - _mod.Groups.IndexOf(_selectedGroup), _selectedGroup.Count - 1)) + _mod.Groups.IndexOf(_selectedGroup), optionIdx)) throw new Exception("Failure writing files for mod swap."); } } @@ -310,8 +313,8 @@ public class ItemSwapTab : IDisposable, ITab Penumbra.Messager.NotificationMessage(e, "Could not create new Swap Option.", NotificationType.Error, false); try { - if (optionCreated && _selectedGroup != null) - _modManager.OptionEditor.DeleteOption(_mod, _mod.Groups.IndexOf(_selectedGroup), _selectedGroup.Count - 1); + if (optionCreated >= 0 && _selectedGroup != null) + _modManager.OptionEditor.DeleteOption(_mod, _mod.Groups.IndexOf(_selectedGroup), optionCreated); if (groupCreated) { diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index a70da628..3f5f6c37 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -78,7 +78,10 @@ public partial class ModEditWindow : Window, IDisposable } public void ChangeOption(SubMod? subMod) - => _editor.LoadOption(subMod?.GroupIdx ?? -1, subMod?.OptionIdx ?? 0); + { + var (groupIdx, optionIdx) = subMod?.GetIndices() ?? (-1, 0); + _editor.LoadOption(groupIdx, optionIdx); + } public void UpdateModels() { @@ -428,7 +431,8 @@ public partial class ModEditWindow : Window, IDisposable using var id = ImRaii.PushId(idx); if (ImGui.Selectable(option.FullName, option == _editor.Option)) { - _editor.LoadOption(option.GroupIdx, option.OptionIdx); + var (groupIdx, optionIdx) = option.GetIndices(); + _editor.LoadOption(groupIdx, optionIdx); ret = true; } } @@ -565,7 +569,7 @@ public partial class ModEditWindow : Window, IDisposable } if (Mod != null) - foreach (var option in Mod.Groups.SelectMany(g => g).Append(Mod.Default)) + foreach (var option in Mod.AllSubMods) { foreach (var path in option.Files.Keys) { diff --git a/Penumbra/UI/AdvancedWindow/ModMergeTab.cs b/Penumbra/UI/AdvancedWindow/ModMergeTab.cs index 1df814da..c34c7ef0 100644 --- a/Penumbra/UI/AdvancedWindow/ModMergeTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModMergeTab.cs @@ -71,7 +71,7 @@ public class ModMergeTab(ModMerger modMerger) color = color == Colors.DiscordColor ? Colors.DiscordColor - : group == null || group.Any(o => o.Name == modMerger.OptionName) + : group == null || group.Options.Any(o => o.Name == modMerger.OptionName) ? Colors.PressEnterWarningBg : Colors.DiscordColor; c.Push(ImGuiCol.Border, color); @@ -184,18 +184,26 @@ public class ModMergeTab(ModMerger modMerger) else { ImGuiUtil.DrawTableColumn(option.Name); - var group = option.ParentMod.Groups[option.GroupIdx]; + var group = option.Group; + var optionEnumerator = group switch + { + SingleModGroup single => single.OptionData, + MultiModGroup multi => multi.PrioritizedOptions.Select(o => o.Mod), + _ => [], + }; ImGui.TableNextColumn(); ImGui.Selectable(group.Name, false); if (ImGui.BeginPopupContextItem("##groupContext")) { if (ImGui.MenuItem("Select All")) - foreach (var opt in group) - Handle((SubMod)opt, true); + // ReSharper disable once PossibleMultipleEnumeration + foreach (var opt in optionEnumerator) + Handle(opt, true); if (ImGui.MenuItem("Unselect All")) - foreach (var opt in group) - Handle((SubMod)opt, false); + // ReSharper disable once PossibleMultipleEnumeration + foreach (var opt in optionEnumerator) + Handle(opt, false); ImGui.EndPopup(); } } diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index b002dedd..0dc694d8 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -324,7 +324,7 @@ public class ModPanelEditTab( ? mod.Description : optionIdx < 0 ? mod.Groups[groupIdx].Description - : mod.Groups[groupIdx][optionIdx].Description; + : mod.Groups[groupIdx].Options[optionIdx].Description; _oldDescription = _newDescription; _mod = mod; @@ -479,17 +479,24 @@ public class ModPanelEditTab( ImGui.TableSetupColumn("delete", ImGuiTableColumnFlags.WidthFixed, UiHelpers.IconButtonSize.X); ImGui.TableSetupColumn("priority", ImGuiTableColumnFlags.WidthFixed, 50 * UiHelpers.Scale); - var group = panel._mod.Groups[groupIdx]; - for (var optionIdx = 0; optionIdx < group.Count; ++optionIdx) - EditOption(panel, group, groupIdx, optionIdx); - + switch (panel._mod.Groups[groupIdx]) + { + case SingleModGroup single: + for (var optionIdx = 0; optionIdx < single.OptionData.Count; ++optionIdx) + EditOption(panel, single, groupIdx, optionIdx); + break; + case MultiModGroup multi: + for (var optionIdx = 0; optionIdx < multi.PrioritizedOptions.Count; ++optionIdx) + EditOption(panel, multi, groupIdx, optionIdx); + break; + } DrawNewOption(panel, groupIdx, UiHelpers.IconButtonSize); } /// Draw a line for a single option. private static void EditOption(ModPanelEditTab panel, IModGroup group, int groupIdx, int optionIdx) { - var option = group[optionIdx]; + var option = group.Options[optionIdx]; using var id = ImRaii.PushId(optionIdx); ImGui.TableNextColumn(); ImGui.AlignTextToFramePadding(); @@ -547,10 +554,16 @@ public class ModPanelEditTab( { var mod = panel._mod; var group = mod.Groups[groupIdx]; + var count = group switch + { + SingleModGroup single => single.OptionData.Count, + MultiModGroup multi => multi.PrioritizedOptions.Count, + _ => throw new Exception($"Dragging options to an option group of type {group.GetType()} is not supported."), + }; ImGui.TableNextColumn(); ImGui.AlignTextToFramePadding(); - ImGui.Selectable($"Option #{group.Count + 1}"); - Target(panel, group, groupIdx, group.Count); + ImGui.Selectable($"Option #{count + 1}"); + Target(panel, group, groupIdx, count); ImGui.TableNextColumn(); ImGui.TableNextColumn(); ImGui.SetNextItemWidth(-1); @@ -562,7 +575,7 @@ public class ModPanelEditTab( } ImGui.TableNextColumn(); - var canAddGroup = mod.Groups[groupIdx].Type != GroupType.Multi || mod.Groups[groupIdx].Count < IModGroup.MaxMultiOptions; + var canAddGroup = mod.Groups[groupIdx].Type != GroupType.Multi || count < IModGroup.MaxMultiOptions; var validName = _newOptionName.Length > 0 && _newOptionNameIdx == groupIdx; var tt = canAddGroup ? validName ? "Add a new option to this group." : "Please enter a name for the new option." @@ -588,7 +601,7 @@ public class ModPanelEditTab( _dragDropOptionIdx = optionIdx; } - ImGui.TextUnformatted($"Dragging option {group[optionIdx].Name} from group {group.Name}..."); + ImGui.TextUnformatted($"Dragging option {group.Options[optionIdx].Name} from group {group.Name}..."); } private static void Target(ModPanelEditTab panel, IModGroup group, int groupIdx, int optionIdx) @@ -611,12 +624,17 @@ public class ModPanelEditTab( var sourceGroupIdx = _dragDropGroupIdx; var sourceOption = _dragDropOptionIdx; var sourceGroup = panel._mod.Groups[sourceGroupIdx]; - var currentCount = group.Count; - var option = sourceGroup[sourceOption]; - var priority = sourceGroup switch + var currentCount = group switch { - MultiModGroup multi => multi.PrioritizedOptions[_dragDropOptionIdx].Priority, - _ => ModPriority.Default, + SingleModGroup single => single.OptionData.Count, + MultiModGroup multi => multi.PrioritizedOptions.Count, + _ => throw new Exception($"Dragging options to an option group of type {group.GetType()} is not supported."), + }; + var (option, priority) = sourceGroup switch + { + SingleModGroup single => (single.OptionData[_dragDropOptionIdx], ModPriority.Default), + MultiModGroup multi => multi.PrioritizedOptions[_dragDropOptionIdx], + _ => throw new Exception($"Dragging options from an option group of type {sourceGroup.GetType()} is not supported."), }; panel._delayedActions.Enqueue(() => { @@ -651,7 +669,7 @@ public class ModPanelEditTab( if (ImGui.Selectable(GroupTypeName(GroupType.Single), group.Type == GroupType.Single)) _modManager.OptionEditor.ChangeModGroupType(_mod, groupIdx, GroupType.Single); - var canSwitchToMulti = group.Count <= IModGroup.MaxMultiOptions; + var canSwitchToMulti = group.Options.Count <= IModGroup.MaxMultiOptions; using var style = ImRaii.PushStyle(ImGuiStyleVar.Alpha, 0.5f, !canSwitchToMulti); if (ImGui.Selectable(GroupTypeName(GroupType.Multi), group.Type == GroupType.Multi) && canSwitchToMulti) _modManager.OptionEditor.ChangeModGroupType(_mod, groupIdx, GroupType.Multi); diff --git a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs index 1107aa20..cb76088c 100644 --- a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs @@ -75,7 +75,7 @@ public class ModPanelSettingsTab : ITab { var useDummy = true; foreach (var (group, idx) in _selector.Selected!.Groups.WithIndex() - .Where(g => g.Value.Type == GroupType.Single && g.Value.Count > _config.SingleGroupRadioMax)) + .Where(g => g.Value.Type == GroupType.Single && g.Value.Options.Count > _config.SingleGroupRadioMax)) { ImGuiUtil.Dummy(UiHelpers.DefaultSpace, useDummy); useDummy = false; @@ -92,7 +92,7 @@ public class ModPanelSettingsTab : ITab case GroupType.Multi: DrawMultiGroup(group, idx); break; - case GroupType.Single when group.Count <= _config.SingleGroupRadioMax: + case GroupType.Single when group.Options.Count <= _config.SingleGroupRadioMax: DrawSingleGroupRadio(group, idx); break; } @@ -181,13 +181,14 @@ public class ModPanelSettingsTab : ITab using var id = ImRaii.PushId(groupIdx); var selectedOption = _empty ? group.DefaultSettings.AsIndex : _settings.Settings[groupIdx].AsIndex; ImGui.SetNextItemWidth(UiHelpers.InputTextWidth.X * 3 / 4); - using (var combo = ImRaii.Combo(string.Empty, group[selectedOption].Name)) + var options = group.Options; + using (var combo = ImRaii.Combo(string.Empty, options[selectedOption].Name)) { if (combo) - for (var idx2 = 0; idx2 < group.Count; ++idx2) + for (var idx2 = 0; idx2 < options.Count; ++idx2) { id.Push(idx2); - var option = group[idx2]; + var option = options[idx2]; if (ImGui.Selectable(option.Name, idx2 == selectedOption)) _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, Setting.Single(idx2)); @@ -213,18 +214,18 @@ public class ModPanelSettingsTab : ITab using var id = ImRaii.PushId(groupIdx); var selectedOption = _empty ? group.DefaultSettings.AsIndex : _settings.Settings[groupIdx].AsIndex; var minWidth = Widget.BeginFramedGroup(group.Name, group.Description); - - DrawCollapseHandling(group, minWidth, DrawOptions); + var options = group.Options; + DrawCollapseHandling(options, minWidth, DrawOptions); Widget.EndFramedGroup(); return; void DrawOptions() { - for (var idx = 0; idx < group.Count; ++idx) + for (var idx = 0; idx < group.Options.Count; ++idx) { using var i = ImRaii.PushId(idx); - var option = group[idx]; + var option = options[idx]; if (ImGui.RadioButton(option.Name, selectedOption == idx)) _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, Setting.Single(idx)); @@ -239,9 +240,9 @@ public class ModPanelSettingsTab : ITab } - private void DrawCollapseHandling(IModGroup group, float minWidth, Action draw) + private void DrawCollapseHandling(IReadOnlyList options, float minWidth, Action draw) { - if (group.Count <= _config.OptionGroupCollapsibleMin) + if (options.Count <= _config.OptionGroupCollapsibleMin) { draw(); } @@ -249,8 +250,8 @@ public class ModPanelSettingsTab : ITab { var collapseId = ImGui.GetID("Collapse"); var shown = ImGui.GetStateStorage().GetBool(collapseId, true); - var buttonTextShow = $"Show {group.Count} Options"; - var buttonTextHide = $"Hide {group.Count} Options"; + var buttonTextShow = $"Show {options.Count} Options"; + var buttonTextHide = $"Hide {options.Count} Options"; var buttonWidth = Math.Max(ImGui.CalcTextSize(buttonTextShow).X, ImGui.CalcTextSize(buttonTextHide).X) + 2 * ImGui.GetStyle().FramePadding.X; minWidth = Math.Max(buttonWidth, minWidth); @@ -274,7 +275,7 @@ public class ModPanelSettingsTab : ITab } else { - var optionWidth = group.Max(o => ImGui.CalcTextSize(o.Name).X) + var optionWidth = options.Max(o => ImGui.CalcTextSize(o.Name).X) + ImGui.GetStyle().ItemInnerSpacing.X + ImGui.GetFrameHeight() + ImGui.GetStyle().FramePadding.X; @@ -294,8 +295,8 @@ public class ModPanelSettingsTab : ITab using var id = ImRaii.PushId(groupIdx); var flags = _empty ? group.DefaultSettings : _settings.Settings[groupIdx]; var minWidth = Widget.BeginFramedGroup(group.Name, group.Description); - - DrawCollapseHandling(group, minWidth, DrawOptions); + var options = group.Options; + DrawCollapseHandling(options, minWidth, DrawOptions); Widget.EndFramedGroup(); var label = $"##multi{groupIdx}"; @@ -307,10 +308,10 @@ public class ModPanelSettingsTab : ITab void DrawOptions() { - for (var idx = 0; idx < group.Count; ++idx) + for (var idx = 0; idx < options.Count; ++idx) { using var i = ImRaii.PushId(idx); - var option = group[idx]; + var option = options[idx]; var setting = flags.HasFlag(idx); if (ImGui.Checkbox(option.Name, ref setting)) @@ -339,7 +340,7 @@ public class ModPanelSettingsTab : ITab ImGui.Separator(); if (ImGui.Selectable("Enable All")) _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, - Setting.AllBits(group.Count)); + Setting.AllBits(group.Options.Count)); if (ImGui.Selectable("Disable All")) _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, Setting.Zero); From 6b1743b776da300fbe1b80a16e662b66ea519a42 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 24 Apr 2024 23:04:04 +0200 Subject: [PATCH 129/433] This sucks so hard... --- Penumbra/Api/Api/ModSettingsApi.cs | 2 +- Penumbra/Import/TexToolsImporter.ModPack.cs | 6 +- Penumbra/Meta/MetaFileManager.cs | 14 +- Penumbra/Mods/Editor/DuplicateManager.cs | 6 +- Penumbra/Mods/Editor/FileRegistry.cs | 12 +- Penumbra/Mods/Editor/ModEditor.cs | 63 +-- Penumbra/Mods/Editor/ModFileCollection.cs | 16 +- Penumbra/Mods/Editor/ModFileEditor.cs | 22 +- Penumbra/Mods/Editor/ModMerger.cs | 79 ++- Penumbra/Mods/Editor/ModMetaEditor.cs | 4 +- Penumbra/Mods/Editor/ModNormalizer.cs | 38 +- Penumbra/Mods/Editor/ModSwapEditor.cs | 2 +- Penumbra/Mods/Manager/ModCacheManager.cs | 29 +- Penumbra/Mods/Manager/ModMigration.cs | 39 +- Penumbra/Mods/Manager/ModOptionEditor.cs | 144 +++--- Penumbra/Mods/Mod.cs | 19 +- Penumbra/Mods/ModCreator.cs | 41 +- Penumbra/Mods/Subclasses/IModDataContainer.cs | 48 ++ Penumbra/Mods/Subclasses/IModGroup.cs | 126 +++-- Penumbra/Mods/Subclasses/IModOption.cs | 2 + Penumbra/Mods/Subclasses/MultiModGroup.cs | 122 ++--- Penumbra/Mods/Subclasses/SingleModGroup.cs | 75 +-- Penumbra/Mods/Subclasses/SubMod.cs | 477 +++++++++++------- Penumbra/Mods/TemporaryMod.cs | 35 +- Penumbra/UI/AdvancedWindow/FileEditor.cs | 2 +- .../UI/AdvancedWindow/ModEditWindow.Files.cs | 15 +- .../UI/AdvancedWindow/ModEditWindow.Meta.cs | 2 +- .../ModEditWindow.Models.MdlTab.cs | 4 +- .../ModEditWindow.QuickImport.cs | 12 +- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 28 +- Penumbra/UI/AdvancedWindow/ModMergeTab.cs | 33 +- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 28 +- Penumbra/UI/ModsTab/ModPanelTabBar.cs | 2 +- 33 files changed, 852 insertions(+), 695 deletions(-) diff --git a/Penumbra/Api/Api/ModSettingsApi.cs b/Penumbra/Api/Api/ModSettingsApi.cs index 5ed26ce5..e145e027 100644 --- a/Penumbra/Api/Api/ModSettingsApi.cs +++ b/Penumbra/Api/Api/ModSettingsApi.cs @@ -201,7 +201,7 @@ public class ModSettingsApi : IPenumbraApiModSettings, IApiService, IDisposable { foreach (var name in optionNames) { - var optionIdx = multi.PrioritizedOptions.IndexOf(o => o.Mod.Name == name); + var optionIdx = multi.OptionData.IndexOf(o => o.Mod.Name == name); if (optionIdx < 0) return ApiHelpers.Return(PenumbraApiEc.OptionMissing, args); diff --git a/Penumbra/Import/TexToolsImporter.ModPack.cs b/Penumbra/Import/TexToolsImporter.ModPack.cs index 7d9388a9..b9cdda71 100644 --- a/Penumbra/Import/TexToolsImporter.ModPack.cs +++ b/Penumbra/Import/TexToolsImporter.ModPack.cs @@ -152,7 +152,7 @@ public partial class TexToolsImporter } // Iterate through all pages - var options = new List(); + var options = new List(); var groupPriority = ModPriority.Default; var groupNames = new HashSet(); foreach (var page in modList.ModPackPages) @@ -183,7 +183,7 @@ public partial class TexToolsImporter var optionFolder = ModCreator.NewSubFolderName(groupFolder, option.Name, _config.ReplaceNonAsciiOnImport) ?? new DirectoryInfo(Path.Combine(groupFolder.FullName, $"Option {i + optionIdx + 1}")); ExtractSimpleModList(optionFolder, option.ModsJsons); - options.Add(_modManager.Creator.CreateSubMod(_currentModDirectory, optionFolder, option)); + options.Add(_modManager.Creator.CreateSubMod(_currentModDirectory, optionFolder, option, new ModPriority(i))); if (option.IsChecked) defaultSettings = group.SelectionType == GroupType.Multi ? defaultSettings!.Value | Setting.Multi(i) @@ -203,7 +203,7 @@ public partial class TexToolsImporter { var option = group.OptionList[idx]; _currentOptionName = option.Name; - options.Insert(idx, SubMod.CreateForSaving(option.Name)); + options.Insert(idx, MultiSubMod.CreateForSaving(option.Name, option.Description, ModPriority.Default)); if (option.IsChecked) defaultSettings = Setting.Single(idx); } diff --git a/Penumbra/Meta/MetaFileManager.cs b/Penumbra/Meta/MetaFileManager.cs index b1823bd7..0e2e638b 100644 --- a/Penumbra/Meta/MetaFileManager.cs +++ b/Penumbra/Meta/MetaFileManager.cs @@ -50,17 +50,15 @@ public unsafe class MetaFileManager TexToolsMeta.WriteTexToolsMeta(this, mod.Default.Manipulations, mod.ModPath); foreach (var group in mod.Groups) { + if (group is not ITexToolsGroup texToolsGroup) + continue; + var dir = ModCreator.NewOptionDirectory(mod.ModPath, group.Name, Config.ReplaceNonAsciiOnImport); if (!dir.Exists) dir.Create(); - var optionEnumerator = group switch - { - SingleModGroup single => single.OptionData, - MultiModGroup multi => multi.PrioritizedOptions.Select(o => o.Mod), - _ => [], - }; - foreach (var option in optionEnumerator) + + foreach (var option in texToolsGroup.OptionData) { var optionDir = ModCreator.NewOptionDirectory(dir, option.Name, Config.ReplaceNonAsciiOnImport); if (!optionDir.Exists) @@ -99,7 +97,7 @@ public unsafe class MetaFileManager return; ResidentResources.Reload(); - if (collection?._cache == null) + if (collection._cache == null) CharacterUtility.ResetAll(); else collection._cache.Meta.SetFiles(); diff --git a/Penumbra/Mods/Editor/DuplicateManager.cs b/Penumbra/Mods/Editor/DuplicateManager.cs index 938199aa..92ec58b9 100644 --- a/Penumbra/Mods/Editor/DuplicateManager.cs +++ b/Penumbra/Mods/Editor/DuplicateManager.cs @@ -29,7 +29,7 @@ public class DuplicateManager(ModManager modManager, SaveService saveService, Co Worker = Task.Run(() => CheckDuplicates(filesTmp, _cancellationTokenSource.Token), _cancellationTokenSource.Token); } - public void DeleteDuplicates(ModFileCollection files, Mod mod, SubMod option, bool useModManager) + public void DeleteDuplicates(ModFileCollection files, Mod mod, IModDataContainer option, bool useModManager) { if (!Worker.IsCompleted || _duplicates.Count == 0) return; @@ -72,7 +72,7 @@ public class DuplicateManager(ModManager modManager, SaveService saveService, Co return; - void HandleSubMod(SubMod subMod, int groupIdx, int optionIdx) + void HandleSubMod(IModDataContainer subMod, int groupIdx, int optionIdx) { var changes = false; var dict = subMod.Files.ToDictionary(kvp => kvp.Key, @@ -86,7 +86,7 @@ public class DuplicateManager(ModManager modManager, SaveService saveService, Co } else { - subMod.FileData = dict; + subMod.Files = dict; saveService.ImmediateSaveSync(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); } } diff --git a/Penumbra/Mods/Editor/FileRegistry.cs b/Penumbra/Mods/Editor/FileRegistry.cs index 427c58ca..44d349ce 100644 --- a/Penumbra/Mods/Editor/FileRegistry.cs +++ b/Penumbra/Mods/Editor/FileRegistry.cs @@ -5,12 +5,12 @@ namespace Penumbra.Mods.Editor; public class FileRegistry : IEquatable { - public readonly List<(SubMod, Utf8GamePath)> SubModUsage = []; - public FullPath File { get; private init; } - public Utf8RelPath RelPath { get; private init; } - public long FileSize { get; private init; } - public int CurrentUsage; - public bool IsOnPlayer; + public readonly List<(IModDataContainer, Utf8GamePath)> SubModUsage = []; + public FullPath File { get; private init; } + public Utf8RelPath RelPath { get; private init; } + public long FileSize { get; private init; } + public int CurrentUsage; + public bool IsOnPlayer; public static bool FromFile(DirectoryInfo modPath, FileInfo file, [NotNullWhen(true)] out FileRegistry? registry) { diff --git a/Penumbra/Mods/Editor/ModEditor.cs b/Penumbra/Mods/Editor/ModEditor.cs index 0a96e0fd..1118f890 100644 --- a/Penumbra/Mods/Editor/ModEditor.cs +++ b/Penumbra/Mods/Editor/ModEditor.cs @@ -1,4 +1,3 @@ -using System; using OtterGui; using OtterGui.Compression; using Penumbra.Mods.Subclasses; @@ -25,20 +24,20 @@ public class ModEditor( public readonly MdlMaterialEditor MdlMaterialEditor = mdlMaterialEditor; public readonly FileCompactor Compactor = compactor; - public Mod? Mod { get; private set; } - public int GroupIdx { get; private set; } - public int OptionIdx { get; private set; } + public Mod? Mod { get; private set; } + public int GroupIdx { get; private set; } + public int DataIdx { get; private set; } - public IModGroup? Group { get; private set; } - public SubMod? Option { get; private set; } + public IModGroup? Group { get; private set; } + public IModDataContainer? Option { get; private set; } public void LoadMod(Mod mod) => LoadMod(mod, -1, 0); - public void LoadMod(Mod mod, int groupIdx, int optionIdx) + public void LoadMod(Mod mod, int groupIdx, int dataIdx) { Mod = mod; - LoadOption(groupIdx, optionIdx, true); + LoadOption(groupIdx, dataIdx, true); Files.UpdateAll(mod, Option!); SwapEditor.Revert(Option!); MetaEditor.Load(Mod!, Option!); @@ -46,9 +45,9 @@ public class ModEditor( MdlMaterialEditor.ScanModels(Mod!); } - public void LoadOption(int groupIdx, int optionIdx) + public void LoadOption(int groupIdx, int dataIdx) { - LoadOption(groupIdx, optionIdx, true); + LoadOption(groupIdx, dataIdx, true); SwapEditor.Revert(Option!); Files.UpdatePaths(Mod!, Option!); MetaEditor.Load(Mod!, Option!); @@ -57,44 +56,38 @@ public class ModEditor( } /// Load the correct option by indices for the currently loaded mod if possible, unload if not. - private void LoadOption(int groupIdx, int optionIdx, bool message) + private void LoadOption(int groupIdx, int dataIdx, bool message) { if (Mod != null && Mod.Groups.Count > groupIdx) { - if (groupIdx == -1 && optionIdx == 0) + if (groupIdx == -1 && dataIdx == 0) { - Group = null; - Option = Mod.Default; - GroupIdx = groupIdx; - OptionIdx = optionIdx; + Group = null; + Option = Mod.Default; + GroupIdx = groupIdx; + DataIdx = dataIdx; return; } if (groupIdx >= 0) { Group = Mod.Groups[groupIdx]; - switch(Group) + if (dataIdx >= 0 && dataIdx < Group.DataContainers.Count) { - case SingleModGroup single when optionIdx >= 0 && optionIdx < single.OptionData.Count: - Option = single.OptionData[optionIdx]; - GroupIdx = groupIdx; - OptionIdx = optionIdx; - return; - case MultiModGroup multi when optionIdx >= 0 && optionIdx < multi.PrioritizedOptions.Count: - Option = multi.PrioritizedOptions[optionIdx].Mod; - GroupIdx = groupIdx; - OptionIdx = optionIdx; - return; + Option = Group.DataContainers[dataIdx]; + GroupIdx = groupIdx; + DataIdx = dataIdx; + return; } } } - Group = null; - Option = Mod?.Default; - GroupIdx = -1; - OptionIdx = 0; + Group = null; + Option = Mod?.Default; + GroupIdx = -1; + DataIdx = 0; if (message) - Penumbra.Log.Error($"Loading invalid option {groupIdx} {optionIdx} for Mod {Mod?.Name ?? "Unknown"}."); + Penumbra.Log.Error($"Loading invalid option {groupIdx} {dataIdx} for Mod {Mod?.Name ?? "Unknown"}."); } public void Clear() @@ -111,7 +104,7 @@ public class ModEditor( => Clear(); /// Apply a option action to all available option in a mod, including the default option. - public static void ApplyToAllOptions(Mod mod, Action action) + public static void ApplyToAllOptions(Mod mod, Action action) { action(mod.Default, -1, 0); foreach (var (group, groupIdx) in mod.Groups.WithIndex()) @@ -123,8 +116,8 @@ public class ModEditor( action(single.OptionData[optionIdx], groupIdx, optionIdx); break; case MultiModGroup multi: - for (var optionIdx = 0; optionIdx < multi.PrioritizedOptions.Count; ++optionIdx) - action(multi.PrioritizedOptions[optionIdx].Mod, groupIdx, optionIdx); + for (var optionIdx = 0; optionIdx < multi.OptionData.Count; ++optionIdx) + action(multi.OptionData[optionIdx], groupIdx, optionIdx); break; } } diff --git a/Penumbra/Mods/Editor/ModFileCollection.cs b/Penumbra/Mods/Editor/ModFileCollection.cs index 9dd78217..ede35914 100644 --- a/Penumbra/Mods/Editor/ModFileCollection.cs +++ b/Penumbra/Mods/Editor/ModFileCollection.cs @@ -38,13 +38,13 @@ public class ModFileCollection : IDisposable public bool Ready { get; private set; } = true; - public void UpdateAll(Mod mod, SubMod option) + public void UpdateAll(Mod mod, IModDataContainer option) { UpdateFiles(mod, new CancellationToken()); UpdatePaths(mod, option, false, new CancellationToken()); } - public void UpdatePaths(Mod mod, SubMod option) + public void UpdatePaths(Mod mod, IModDataContainer option) => UpdatePaths(mod, option, true, new CancellationToken()); public void Clear() @@ -59,7 +59,7 @@ public class ModFileCollection : IDisposable public void ClearMissingFiles() => _missing.Clear(); - public void RemoveUsedPath(SubMod option, FileRegistry? file, Utf8GamePath gamePath) + public void RemoveUsedPath(IModDataContainer option, FileRegistry? file, Utf8GamePath gamePath) { _usedPaths.Remove(gamePath); if (file != null) @@ -69,10 +69,10 @@ public class ModFileCollection : IDisposable } } - public void RemoveUsedPath(SubMod option, FullPath file, Utf8GamePath gamePath) + public void RemoveUsedPath(IModDataContainer option, FullPath file, Utf8GamePath gamePath) => RemoveUsedPath(option, _available.FirstOrDefault(f => f.File.Equals(file)), gamePath); - public void AddUsedPath(SubMod option, FileRegistry? file, Utf8GamePath gamePath) + public void AddUsedPath(IModDataContainer option, FileRegistry? file, Utf8GamePath gamePath) { _usedPaths.Add(gamePath); if (file == null) @@ -82,7 +82,7 @@ public class ModFileCollection : IDisposable file.SubModUsage.Add((option, gamePath)); } - public void AddUsedPath(SubMod option, FullPath file, Utf8GamePath gamePath) + public void AddUsedPath(IModDataContainer option, FullPath file, Utf8GamePath gamePath) => AddUsedPath(option, _available.FirstOrDefault(f => f.File.Equals(file)), gamePath); public void ChangeUsedPath(FileRegistry file, int pathIdx, Utf8GamePath gamePath) @@ -154,14 +154,14 @@ public class ModFileCollection : IDisposable _usedPaths.Clear(); } - private void UpdatePaths(Mod mod, SubMod option, bool clearRegistries, CancellationToken tok) + private void UpdatePaths(Mod mod, IModDataContainer option, bool clearRegistries, CancellationToken tok) { tok.ThrowIfCancellationRequested(); ClearPaths(clearRegistries, tok); tok.ThrowIfCancellationRequested(); - foreach (var subMod in mod.AllSubMods) + foreach (var subMod in mod.AllDataContainers) { foreach (var (gamePath, file) in subMod.Files) { diff --git a/Penumbra/Mods/Editor/ModFileEditor.cs b/Penumbra/Mods/Editor/ModFileEditor.cs index 51615b05..11e35334 100644 --- a/Penumbra/Mods/Editor/ModFileEditor.cs +++ b/Penumbra/Mods/Editor/ModFileEditor.cs @@ -14,7 +14,7 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu Changes = false; } - public int Apply(Mod mod, SubMod option) + public int Apply(Mod mod, IModDataContainer option) { var dict = new Dictionary(); var num = 0; @@ -24,23 +24,23 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu num += dict.TryAdd(path.Item2, file.File) ? 0 : 1; } - var (groupIdx, optionIdx) = option.GetIndices(); - modManager.OptionEditor.OptionSetFiles(mod, groupIdx, optionIdx, dict); + var (groupIdx, dataIdx) = option.GetDataIndices(); + modManager.OptionEditor.OptionSetFiles(mod, groupIdx, dataIdx, dict); files.UpdatePaths(mod, option); Changes = false; return num; } - public void Revert(Mod mod, SubMod option) + public void Revert(Mod mod, IModDataContainer option) { files.UpdateAll(mod, option); Changes = false; } /// Remove all path redirections where the pointed-to file does not exist. - public void RemoveMissingPaths(Mod mod, SubMod option) + public void RemoveMissingPaths(Mod mod, IModDataContainer option) { - void HandleSubMod(SubMod subMod, int groupIdx, int optionIdx) + void HandleSubMod(IModDataContainer subMod, int groupIdx, int optionIdx) { var newDict = subMod.Files.Where(kvp => CheckAgainstMissing(mod, subMod, kvp.Value, kvp.Key, subMod == option)) .ToDictionary(kvp => kvp.Key, kvp => kvp.Value); @@ -62,7 +62,7 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu /// If path is empty, it will be deleted instead. /// If pathIdx is equal to the total number of paths, path will be added, otherwise replaced. /// - public bool SetGamePath(SubMod option, int fileIdx, int pathIdx, Utf8GamePath path) + public bool SetGamePath(IModDataContainer option, int fileIdx, int pathIdx, Utf8GamePath path) { if (!CanAddGamePath(path) || fileIdx < 0 || fileIdx > files.Available.Count) return false; @@ -85,7 +85,7 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu /// Transform a set of files to the appropriate game paths with the given number of folders skipped, /// and add them to the given option. /// - public int AddPathsToSelected(SubMod option, IEnumerable files1, int skipFolders = 0) + public int AddPathsToSelected(IModDataContainer option, IEnumerable files1, int skipFolders = 0) { var failed = 0; foreach (var file in files1) @@ -112,7 +112,7 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu } /// Remove all paths in the current option from the given files. - public void RemovePathsFromSelected(SubMod option, IEnumerable files1) + public void RemovePathsFromSelected(IModDataContainer option, IEnumerable files1) { foreach (var file in files1) { @@ -130,7 +130,7 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu } /// Delete all given files from your filesystem - public void DeleteFiles(Mod mod, SubMod option, IEnumerable files1) + public void DeleteFiles(Mod mod, IModDataContainer option, IEnumerable files1) { var deletions = 0; foreach (var file in files1) @@ -156,7 +156,7 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu } - private bool CheckAgainstMissing(Mod mod, SubMod option, FullPath file, Utf8GamePath key, bool removeUsed) + private bool CheckAgainstMissing(Mod mod, IModDataContainer option, FullPath file, Utf8GamePath key, bool removeUsed) { if (!files.Missing.Contains(file)) return true; diff --git a/Penumbra/Mods/Editor/ModMerger.cs b/Penumbra/Mods/Editor/ModMerger.cs index 74e9007c..541c84ae 100644 --- a/Penumbra/Mods/Editor/ModMerger.cs +++ b/Penumbra/Mods/Editor/ModMerger.cs @@ -1,6 +1,5 @@ using Dalamud.Interface.Internal.Notifications; using Dalamud.Utility; -using ImGuizmoNET; using OtterGui; using OtterGui.Classes; using Penumbra.Api.Enums; @@ -33,9 +32,9 @@ public class ModMerger : IDisposable private readonly Dictionary _fileToFile = []; private readonly HashSet _createdDirectories = []; private readonly HashSet _createdGroups = []; - private readonly HashSet _createdOptions = []; + private readonly HashSet _createdOptions = []; - public readonly HashSet SelectedOptions = []; + public readonly HashSet SelectedOptions = []; public readonly IReadOnlyList Warnings = []; public Exception? Error { get; private set; } @@ -94,7 +93,7 @@ public class ModMerger : IDisposable private void MergeWithOptions() { - MergeIntoOption(Enumerable.Repeat(MergeFromMod!.Default, 1), MergeToMod!.Default, false); + MergeIntoOption([MergeFromMod!.Default], MergeToMod!.Default, false); foreach (var originalGroup in MergeFromMod!.Groups) { @@ -105,20 +104,13 @@ public class ModMerger : IDisposable ((List)Warnings).Add( $"The merged group {group.Name} already existed, but has a different type {group.Type} than the original group of type {originalGroup.Type}."); - var optionEnumerator = group switch + foreach (var originalOption in group.DataContainers) { - SingleModGroup single => single.OptionData, - MultiModGroup multi => multi.PrioritizedOptions.Select(o => o.Mod), - _ => [], - }; - - foreach (var originalOption in optionEnumerator) - { - var (option, _, optionCreated) = _editor.FindOrAddOption(MergeToMod!, groupIdx, originalOption.Name); + var (option, _, optionCreated) = _editor.FindOrAddOption(MergeToMod!, groupIdx, originalOption.GetName()); if (optionCreated) { - _createdOptions.Add(option); - MergeIntoOption(Enumerable.Repeat(originalOption, 1), option, false); + _createdOptions.Add((IModDataOption)option); + MergeIntoOption([originalOption], (IModDataOption)option, false); } else { @@ -136,7 +128,7 @@ public class ModMerger : IDisposable if (groupName.Length == 0 && optionName.Length == 0) { CopyFiles(MergeToMod!.ModPath); - MergeIntoOption(MergeFromMod!.AllSubMods.Reverse(), MergeToMod!.Default, true); + MergeIntoOption(MergeFromMod!.AllDataContainers.Reverse(), MergeToMod!.Default, true); } else if (groupName.Length * optionName.Length == 0) { @@ -148,7 +140,7 @@ public class ModMerger : IDisposable _createdGroups.Add(groupIdx); var (option, _, optionCreated) = _editor.FindOrAddOption(MergeToMod!, groupIdx, optionName, SaveType.None); if (optionCreated) - _createdOptions.Add(option); + _createdOptions.Add((IModDataOption)option); var dir = ModCreator.NewOptionDirectory(MergeToMod!.ModPath, groupName, _config.ReplaceNonAsciiOnImport); if (!dir.Exists) _createdDirectories.Add(dir.FullName); @@ -156,14 +148,14 @@ public class ModMerger : IDisposable if (!dir.Exists) _createdDirectories.Add(dir.FullName); CopyFiles(dir); - MergeIntoOption(MergeFromMod!.AllSubMods.Reverse(), option, true); + MergeIntoOption(MergeFromMod!.AllDataContainers.Reverse(), (IModDataOption)option, true); } - private void MergeIntoOption(IEnumerable mergeOptions, SubMod option, bool fromFileToFile) + private void MergeIntoOption(IEnumerable mergeOptions, IModDataContainer option, bool fromFileToFile) { - var redirections = option.FileData.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); - var swaps = option.FileSwapData.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); - var manips = option.ManipulationData.ToHashSet(); + var redirections = option.Files.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); + var swaps = option.FileSwaps.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); + var manips = option.Manipulations.ToHashSet(); foreach (var originalOption in mergeOptions) { @@ -171,31 +163,31 @@ public class ModMerger : IDisposable { if (!manips.Add(manip)) throw new Exception( - $"Could not add meta manipulation {manip} from {originalOption.FullName} to {option.FullName} because another manipulation of the same data already exists in this option."); + $"Could not add meta manipulation {manip} from {originalOption.GetFullName()} to {option.GetFullName()} because another manipulation of the same data already exists in this option."); } foreach (var (swapA, swapB) in originalOption.FileSwaps) { if (!swaps.TryAdd(swapA, swapB)) throw new Exception( - $"Could not add file swap {swapB} -> {swapA} from {originalOption.FullName} to {option.FullName} because another swap of the key already exists."); + $"Could not add file swap {swapB} -> {swapA} from {originalOption.GetFullName()} to {option.GetFullName()} because another swap of the key already exists."); } foreach (var (gamePath, path) in originalOption.Files) { if (!GetFullPath(path, out var newFile)) throw new Exception( - $"Could not add file redirection {path} -> {gamePath} from {originalOption.FullName} to {option.FullName} because the file does not exist in the new mod."); + $"Could not add file redirection {path} -> {gamePath} from {originalOption.GetFullName()} to {option.GetFullName()} because the file does not exist in the new mod."); if (!redirections.TryAdd(gamePath, newFile)) throw new Exception( - $"Could not add file redirection {path} -> {gamePath} from {originalOption.FullName} to {option.FullName} because a redirection for the game path already exists."); + $"Could not add file redirection {path} -> {gamePath} from {originalOption.GetFullName()} to {option.GetFullName()} because a redirection for the game path already exists."); } } - var (groupIdx, optionIdx) = option.GetIndices(); - _editor.OptionSetFiles(MergeToMod!, groupIdx, optionIdx, redirections, SaveType.None); - _editor.OptionSetFileSwaps(MergeToMod!, groupIdx, optionIdx, swaps, SaveType.None); - _editor.OptionSetManipulations(MergeToMod!, groupIdx, optionIdx, manips, SaveType.ImmediateSync); + var (groupIdx, dataIdx) = option.GetDataIndices(); + _editor.OptionSetFiles(MergeToMod!, groupIdx, dataIdx, redirections, SaveType.None); + _editor.OptionSetFileSwaps(MergeToMod!, groupIdx, dataIdx, swaps, SaveType.None); + _editor.OptionSetManipulations(MergeToMod!, groupIdx, dataIdx, manips, SaveType.ImmediateSync); return; bool GetFullPath(FullPath input, out FullPath ret) @@ -270,30 +262,29 @@ public class ModMerger : IDisposable { var files = CopySubModFiles(mods[0], dir); _editor.OptionSetFiles(result, -1, 0, files); - _editor.OptionSetFileSwaps(result, -1, 0, mods[0].FileSwapData); - _editor.OptionSetManipulations(result, -1, 0, mods[0].ManipulationData); + _editor.OptionSetFileSwaps(result, -1, 0, mods[0].FileSwaps); + _editor.OptionSetManipulations(result, -1, 0, mods[0].Manipulations); } else { foreach (var originalOption in mods) { - if (originalOption.IsDefault) + if (originalOption.Group is not {} originalGroup) { var files = CopySubModFiles(mods[0], dir); _editor.OptionSetFiles(result, -1, 0, files); - _editor.OptionSetFileSwaps(result, -1, 0, mods[0].FileSwapData); - _editor.OptionSetManipulations(result, -1, 0, mods[0].ManipulationData); + _editor.OptionSetFileSwaps(result, -1, 0, mods[0].FileSwaps); + _editor.OptionSetManipulations(result, -1, 0, mods[0].Manipulations); } else { - var originalGroup = originalOption.Group; - var (group, groupIdx, _) = _editor.FindOrAddModGroup(result, originalGroup.Type, originalGroup.Name); - var (option, optionIdx, _) = _editor.FindOrAddOption(result, groupIdx, originalOption.Name); + var (group, groupIdx, _) = _editor.FindOrAddModGroup(result, originalGroup.Type, originalGroup.Name); + var (option, optionIdx, _) = _editor.FindOrAddOption(result, groupIdx, originalOption.GetName()); var folder = Path.Combine(dir.FullName, group.Name, option.Name); var files = CopySubModFiles(originalOption, new DirectoryInfo(folder)); _editor.OptionSetFiles(result, groupIdx, optionIdx, files); - _editor.OptionSetFileSwaps(result, groupIdx, optionIdx, originalOption.FileSwapData); - _editor.OptionSetManipulations(result, groupIdx, optionIdx, originalOption.ManipulationData); + _editor.OptionSetFileSwaps(result, groupIdx, optionIdx, originalOption.FileSwaps); + _editor.OptionSetManipulations(result, groupIdx, optionIdx, originalOption.Manipulations); } } } @@ -315,11 +306,11 @@ public class ModMerger : IDisposable } } - private static Dictionary CopySubModFiles(SubMod option, DirectoryInfo newMod) + private static Dictionary CopySubModFiles(IModDataContainer option, DirectoryInfo newMod) { - var ret = new Dictionary(option.FileData.Count); + var ret = new Dictionary(option.Files.Count); var parentPath = ((Mod)option.Mod).ModPath.FullName; - foreach (var (path, file) in option.FileData) + foreach (var (path, file) in option.Files) { var target = Path.GetRelativePath(parentPath, file.FullName); target = Path.Combine(newMod.FullName, target); @@ -348,7 +339,7 @@ public class ModMerger : IDisposable { foreach (var option in _createdOptions) { - var (groupIdx, optionIdx) = option.GetIndices(); + var (groupIdx, optionIdx) = option.GetOptionIndices(); _editor.DeleteOption(MergeToMod!, groupIdx, optionIdx); Penumbra.Log.Verbose($"[Merger] Removed option {option.FullName}."); } diff --git a/Penumbra/Mods/Editor/ModMetaEditor.cs b/Penumbra/Mods/Editor/ModMetaEditor.cs index a6218c6f..88e48f0f 100644 --- a/Penumbra/Mods/Editor/ModMetaEditor.cs +++ b/Penumbra/Mods/Editor/ModMetaEditor.cs @@ -103,7 +103,7 @@ public class ModMetaEditor(ModManager modManager) Changes = true; } - public void Load(Mod mod, SubMod currentOption) + public void Load(Mod mod, IModDataContainer currentOption) { OtherImcCount = 0; OtherEqpCount = 0; @@ -111,7 +111,7 @@ public class ModMetaEditor(ModManager modManager) OtherGmpCount = 0; OtherEstCount = 0; OtherRspCount = 0; - foreach (var option in mod.AllSubMods) + foreach (var option in mod.AllDataContainers) { if (option == currentOption) continue; diff --git a/Penumbra/Mods/Editor/ModNormalizer.cs b/Penumbra/Mods/Editor/ModNormalizer.cs index 9698fdcb..db00a1c7 100644 --- a/Penumbra/Mods/Editor/ModNormalizer.cs +++ b/Penumbra/Mods/Editor/ModNormalizer.cs @@ -1,3 +1,4 @@ +using System; using Dalamud.Interface.Internal.Notifications; using OtterGui; using OtterGui.Classes; @@ -168,27 +169,11 @@ public class ModNormalizer(ModManager _modManager, Configuration _config) foreach (var (group, groupIdx) in Mod.Groups.WithIndex()) { var groupDir = ModCreator.CreateModFolder(directory, group.Name, _config.ReplaceNonAsciiOnImport, true); - switch (group) - { - case SingleModGroup single: - _redirections[groupIdx + 1].EnsureCapacity(single.OptionData.Count); - for (var i = _redirections[groupIdx + 1].Count; i < single.OptionData.Count; ++i) - _redirections[groupIdx + 1].Add([]); - - foreach (var (option, optionIdx) in single.OptionData.WithIndex()) - HandleSubMod(groupDir, option, _redirections[groupIdx + 1][optionIdx]); - - break; - case MultiModGroup multi: - _redirections[groupIdx + 1].EnsureCapacity(multi.PrioritizedOptions.Count); - for (var i = _redirections[groupIdx + 1].Count; i < multi.PrioritizedOptions.Count; ++i) - _redirections[groupIdx + 1].Add([]); - - foreach (var ((option, _), optionIdx) in multi.PrioritizedOptions.WithIndex()) - HandleSubMod(groupDir, option, _redirections[groupIdx + 1][optionIdx]); - - break; - } + _redirections[groupIdx + 1].EnsureCapacity(group.DataContainers.Count); + for (var i = _redirections[groupIdx + 1].Count; i < group.DataContainers.Count; ++i) + _redirections[groupIdx + 1].Add([]); + foreach (var (data, dataIdx) in group.DataContainers.WithIndex()) + HandleSubMod(groupDir, data, _redirections[groupIdx + 1][dataIdx]); } return true; @@ -200,13 +185,14 @@ public class ModNormalizer(ModManager _modManager, Configuration _config) return false; - void HandleSubMod(DirectoryInfo groupDir, SubMod option, Dictionary newDict) + void HandleSubMod(DirectoryInfo groupDir, IModDataContainer option, Dictionary newDict) { - var optionDir = ModCreator.CreateModFolder(groupDir, option.Name, _config.ReplaceNonAsciiOnImport, true); + var name = option.GetName(); + var optionDir = ModCreator.CreateModFolder(groupDir, name, _config.ReplaceNonAsciiOnImport, true); newDict.Clear(); - newDict.EnsureCapacity(option.FileData.Count); - foreach (var (gamePath, fullPath) in option.FileData) + newDict.EnsureCapacity(option.Files.Count); + foreach (var (gamePath, fullPath) in option.Files) { var relPath = new Utf8RelPath(gamePath).ToString(); var newFullPath = Path.Combine(optionDir.FullName, relPath); @@ -300,7 +286,7 @@ public class ModNormalizer(ModManager _modManager, Configuration _config) _modManager.OptionEditor.OptionSetFiles(Mod, groupIdx, optionIdx, _redirections[groupIdx + 1][optionIdx]); break; case MultiModGroup multi: - foreach (var (_, optionIdx) in multi.PrioritizedOptions.WithIndex()) + foreach (var (_, optionIdx) in multi.OptionData.WithIndex()) _modManager.OptionEditor.OptionSetFiles(Mod, groupIdx, optionIdx, _redirections[groupIdx + 1][optionIdx]); break; } diff --git a/Penumbra/Mods/Editor/ModSwapEditor.cs b/Penumbra/Mods/Editor/ModSwapEditor.cs index 0d5f05a9..64788cf3 100644 --- a/Penumbra/Mods/Editor/ModSwapEditor.cs +++ b/Penumbra/Mods/Editor/ModSwapEditor.cs @@ -11,7 +11,7 @@ public class ModSwapEditor(ModManager modManager) public IReadOnlyDictionary Swaps => _swaps; - public void Revert(SubMod option) + public void Revert(IModDataContainer option) { _swaps.SetTo(option.FileSwaps); Changes = false; diff --git a/Penumbra/Mods/Manager/ModCacheManager.cs b/Penumbra/Mods/Manager/ModCacheManager.cs index df243781..4f9e8648 100644 --- a/Penumbra/Mods/Manager/ModCacheManager.cs +++ b/Penumbra/Mods/Manager/ModCacheManager.cs @@ -176,13 +176,13 @@ public class ModCacheManager : IDisposable } private static void UpdateFileCount(Mod mod) - => mod.TotalFileCount = mod.AllSubMods.Sum(s => s.Files.Count); + => mod.TotalFileCount = mod.AllDataContainers.Sum(s => s.Files.Count); private static void UpdateSwapCount(Mod mod) - => mod.TotalSwapCount = mod.AllSubMods.Sum(s => s.FileSwaps.Count); + => mod.TotalSwapCount = mod.AllDataContainers.Sum(s => s.FileSwaps.Count); private static void UpdateMetaCount(Mod mod) - => mod.TotalManipulations = mod.AllSubMods.Sum(s => s.Manipulations.Count); + => mod.TotalManipulations = mod.AllDataContainers.Sum(s => s.Manipulations.Count); private static void UpdateHasOptions(Mod mod) => mod.HasOptions = mod.Groups.Any(o => o.IsOption); @@ -194,10 +194,10 @@ public class ModCacheManager : IDisposable { var changedItems = (SortedList)mod.ChangedItems; changedItems.Clear(); - foreach (var gamePath in mod.AllSubMods.SelectMany(m => m.Files.Keys.Concat(m.FileSwaps.Keys))) + foreach (var gamePath in mod.AllDataContainers.SelectMany(m => m.Files.Keys.Concat(m.FileSwaps.Keys))) _identifier.Identify(changedItems, gamePath.ToString()); - foreach (var manip in mod.AllSubMods.SelectMany(m => m.Manipulations)) + foreach (var manip in mod.AllDataContainers.SelectMany(m => m.Manipulations)) ComputeChangedItems(_identifier, changedItems, manip); mod.LowerChangedItemsString = string.Join("\0", mod.ChangedItems.Keys.Select(k => k.ToLowerInvariant())); @@ -211,20 +211,11 @@ public class ModCacheManager : IDisposable mod.HasOptions = false; foreach (var group in mod.Groups) { - mod.HasOptions |= group.IsOption; - var optionEnumerator = group switch - { - SingleModGroup single => single.OptionData, - MultiModGroup multi => multi.PrioritizedOptions.Select(o => o.Mod), - _ => [], - }; - - foreach (var s in optionEnumerator) - { - mod.TotalFileCount += s.Files.Count; - mod.TotalSwapCount += s.FileSwaps.Count; - mod.TotalManipulations += s.Manipulations.Count; - } + mod.HasOptions |= group.IsOption; + var (files, swaps, manips) = group.GetCounts(); + mod.TotalFileCount += files; + mod.TotalSwapCount += swaps; + mod.TotalManipulations += manips; } } diff --git a/Penumbra/Mods/Manager/ModMigration.cs b/Penumbra/Mods/Manager/ModMigration.cs index 9c8ced89..8c4a5674 100644 --- a/Penumbra/Mods/Manager/ModMigration.cs +++ b/Penumbra/Mods/Manager/ModMigration.cs @@ -71,14 +71,14 @@ public static partial class ModMigration foreach (var unusedFile in mod.FindUnusedFiles().Where(f => !seenMetaFiles.Contains(f))) { if (unusedFile.ToGamePath(mod.ModPath, out var gamePath) - && !mod.Default.FileData.TryAdd(gamePath, unusedFile)) - Penumbra.Log.Error($"Could not add {gamePath} because it already points to {mod.Default.FileData[gamePath]}."); + && !mod.Default.Files.TryAdd(gamePath, unusedFile)) + Penumbra.Log.Error($"Could not add {gamePath} because it already points to {mod.Default.Files[gamePath]}."); } - mod.Default.FileSwapData.Clear(); - mod.Default.FileSwapData.EnsureCapacity(swaps.Count); + mod.Default.FileSwaps.Clear(); + mod.Default.FileSwaps.EnsureCapacity(swaps.Count); foreach (var (gamePath, swapPath) in swaps) - mod.Default.FileSwapData.Add(gamePath, swapPath); + mod.Default.FileSwaps.Add(gamePath, swapPath); creator.IncorporateMetaChanges(mod.Default, mod.ModPath, true); foreach (var (_, index) in mod.Groups.WithIndex()) @@ -134,7 +134,7 @@ public static partial class ModMigration }; mod.Groups.Add(newMultiGroup); foreach (var option in group.Options) - newMultiGroup.PrioritizedOptions.Add((SubModFromOption(creator, mod, newMultiGroup, option, seenMetaFiles), optionPriority++)); + newMultiGroup.OptionData.Add(SubModFromOption(creator, mod, newMultiGroup, option, optionPriority++, seenMetaFiles)); break; case GroupType.Single: @@ -158,22 +158,41 @@ public static partial class ModMigration } } - private static void AddFilesToSubMod(SubMod mod, DirectoryInfo basePath, OptionV0 option, HashSet seenMetaFiles) + private static void AddFilesToSubMod(IModDataContainer mod, DirectoryInfo basePath, OptionV0 option, HashSet seenMetaFiles) { foreach (var (relPath, gamePaths) in option.OptionFiles) { var fullPath = new FullPath(basePath, relPath); foreach (var gamePath in gamePaths) - mod.FileData.TryAdd(gamePath, fullPath); + mod.Files.TryAdd(gamePath, fullPath); if (fullPath.Extension is ".meta" or ".rgsp") seenMetaFiles.Add(fullPath); } } - private static SubMod SubModFromOption(ModCreator creator, Mod mod, IModGroup group, OptionV0 option, HashSet seenMetaFiles) + private static SingleSubMod SubModFromOption(ModCreator creator, Mod mod, SingleModGroup group, OptionV0 option, + HashSet seenMetaFiles) { - var subMod = new SubMod(mod, group) { Name = option.OptionName }; + var subMod = new SingleSubMod(mod, group) + { + Name = option.OptionName, + Description = option.OptionDesc, + }; + AddFilesToSubMod(subMod, mod.ModPath, option, seenMetaFiles); + creator.IncorporateMetaChanges(subMod, mod.ModPath, false); + return subMod; + } + + private static MultiSubMod SubModFromOption(ModCreator creator, Mod mod, MultiModGroup group, OptionV0 option, + ModPriority priority, HashSet seenMetaFiles) + { + var subMod = new MultiSubMod(mod, group) + { + Name = option.OptionName, + Description = option.OptionDesc, + Priority = priority, + }; AddFilesToSubMod(subMod, mod.ModPath, option, seenMetaFiles); creator.IncorporateMetaChanges(subMod, mod.ModPath, false); return subMod; diff --git a/Penumbra/Mods/Manager/ModOptionEditor.cs b/Penumbra/Mods/Manager/ModOptionEditor.cs index e78b6209..4d3a5717 100644 --- a/Penumbra/Mods/Manager/ModOptionEditor.cs +++ b/Penumbra/Mods/Manager/ModOptionEditor.cs @@ -1,4 +1,3 @@ -using System.Security.AccessControl; using Dalamud.Interface.Internal.Notifications; using OtterGui; using OtterGui.Classes; @@ -179,10 +178,10 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS switch (mod.Groups[groupIdx]) { case MultiModGroup multi: - if (multi.PrioritizedOptions[optionIdx].Priority == newPriority) + if (multi.OptionData[optionIdx].Priority == newPriority) return; - multi.PrioritizedOptions[optionIdx] = (multi.PrioritizedOptions[optionIdx].Mod, newPriority); + multi.OptionData[optionIdx].Priority = newPriority; saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); communicator.ModOptionChanged.Invoke(ModOptionChangeType.PriorityChanged, mod, groupIdx, optionIdx, -1); return; @@ -213,70 +212,62 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS } /// Add a new empty option of the given name for the given group if it does not exist already. - public (SubMod, int, bool) FindOrAddOption(Mod mod, int groupIdx, string newName, SaveType saveType = SaveType.Queue) + public (IModOption, int, bool) FindOrAddOption(Mod mod, int groupIdx, string newName, SaveType saveType = SaveType.Queue) { var group = mod.Groups[groupIdx]; - switch (group) - { - case SingleModGroup single: - { - var idx = single.OptionData.IndexOf(o => o.Name == newName); - if (idx >= 0) - return (single.OptionData[idx], idx, false); + var idx = group.Options.IndexOf(o => o.Name == newName); + if (idx >= 0) + return (group.Options[idx], idx, false); - idx = single.AddOption(mod, newName); - if (idx < 0) - throw new Exception($"Could not create new option with name {newName} in {group.Name}."); + idx = group.AddOption(mod, newName); + if (idx < 0) + throw new Exception($"Could not create new option with name {newName} in {group.Name}."); - saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, idx, -1); - return (single.OptionData[^1], single.OptionData.Count - 1, true); - } - case MultiModGroup multi: - { - var idx = multi.PrioritizedOptions.IndexOf(o => o.Mod.Name == newName); - if (idx >= 0) - return (multi.PrioritizedOptions[idx].Mod, idx, false); - - idx = multi.AddOption(mod, newName); - if (idx < 0) - throw new Exception($"Could not create new option with name {newName} in {group.Name}."); - - saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, idx, -1); - return (multi.PrioritizedOptions[^1].Mod, multi.PrioritizedOptions.Count - 1, true); - } - } - - throw new Exception($"{nameof(FindOrAddOption)} is not supported for mod groups of type {group.GetType()}."); + saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, idx, -1); + return (group.Options[idx], idx, true); } - /// Add an existing option to a given group with default priority. - public void AddOption(Mod mod, int groupIdx, SubMod option) - => AddOption(mod, groupIdx, option, ModPriority.Default); - - /// Add an existing option to a given group with a given priority. - public void AddOption(Mod mod, int groupIdx, SubMod option, ModPriority priority) + /// Add an existing option to a given group. + public void AddOption(Mod mod, int groupIdx, IModOption option) { var group = mod.Groups[groupIdx]; int idx; switch (group) { - case MultiModGroup { PrioritizedOptions.Count: >= IModGroup.MaxMultiOptions }: + case MultiModGroup { OptionData.Count: >= IModGroup.MaxMultiOptions }: Penumbra.Log.Error( $"Could not add option {option.Name} to {group.Name} for mod {mod.Name}, " + $"since only up to {IModGroup.MaxMultiOptions} options are supported in one group."); return; case SingleModGroup s: + { idx = s.OptionData.Count; - s.OptionData.Add(option); + var newOption = new SingleSubMod(s.Mod, s) + { + Name = option.Name, + Description = option.Description, + }; + if (option is IModDataContainer data) + IModDataContainer.Clone(data, newOption); + s.OptionData.Add(newOption); break; + } case MultiModGroup m: - idx = m.PrioritizedOptions.Count; - m.PrioritizedOptions.Add((option, priority)); + { + idx = m.OptionData.Count; + var newOption = new MultiSubMod(m.Mod, m) + { + Name = option.Name, + Description = option.Description, + Priority = option is MultiSubMod s ? s.Priority : ModPriority.Default, + }; + if (option is IModDataContainer data) + IModDataContainer.Clone(data, newOption); + m.OptionData.Add(newOption); break; - default: - return; + } + default: return; } saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); @@ -295,7 +286,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS break; case MultiModGroup m: - m.PrioritizedOptions.RemoveAt(optionIdx); + m.OptionData.RemoveAt(optionIdx); break; } @@ -315,59 +306,59 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS } /// Set the meta manipulations for a given option. Replaces existing manipulations. - public void OptionSetManipulations(Mod mod, int groupIdx, int optionIdx, HashSet manipulations, + public void OptionSetManipulations(Mod mod, int groupIdx, int dataContainerIdx, HashSet manipulations, SaveType saveType = SaveType.Queue) { - var subMod = GetSubMod(mod, groupIdx, optionIdx); + var subMod = GetSubMod(mod, groupIdx, dataContainerIdx); if (subMod.Manipulations.Count == manipulations.Count && subMod.Manipulations.All(m => manipulations.TryGetValue(m, out var old) && old.EntryEquals(m))) return; - communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, optionIdx, -1); - subMod.ManipulationData.SetTo(manipulations); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, dataContainerIdx, -1); + subMod.Manipulations.SetTo(manipulations); saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMetaChanged, mod, groupIdx, optionIdx, -1); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMetaChanged, mod, groupIdx, dataContainerIdx, -1); } /// Set the file redirections for a given option. Replaces existing redirections. - public void OptionSetFiles(Mod mod, int groupIdx, int optionIdx, IReadOnlyDictionary replacements, + public void OptionSetFiles(Mod mod, int groupIdx, int dataContainerIdx, IReadOnlyDictionary replacements, SaveType saveType = SaveType.Queue) { - var subMod = GetSubMod(mod, groupIdx, optionIdx); - if (subMod.FileData.SetEquals(replacements)) + var subMod = GetSubMod(mod, groupIdx, dataContainerIdx); + if (subMod.Files.SetEquals(replacements)) return; - communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, optionIdx, -1); - subMod.FileData.SetTo(replacements); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, dataContainerIdx, -1); + subMod.Files.SetTo(replacements); saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesChanged, mod, groupIdx, optionIdx, -1); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesChanged, mod, groupIdx, dataContainerIdx, -1); } /// Add additional file redirections to a given option, keeping already existing ones. Only fires an event if anything is actually added. - public void OptionAddFiles(Mod mod, int groupIdx, int optionIdx, IReadOnlyDictionary additions) + public void OptionAddFiles(Mod mod, int groupIdx, int dataContainerIdx, IReadOnlyDictionary additions) { - var subMod = GetSubMod(mod, groupIdx, optionIdx); - var oldCount = subMod.FileData.Count; - subMod.FileData.AddFrom(additions); - if (oldCount != subMod.FileData.Count) + var subMod = GetSubMod(mod, groupIdx, dataContainerIdx); + var oldCount = subMod.Files.Count; + subMod.Files.AddFrom(additions); + if (oldCount != subMod.Files.Count) { saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesAdded, mod, groupIdx, optionIdx, -1); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesAdded, mod, groupIdx, dataContainerIdx, -1); } } /// Set the file swaps for a given option. Replaces existing swaps. - public void OptionSetFileSwaps(Mod mod, int groupIdx, int optionIdx, IReadOnlyDictionary swaps, + public void OptionSetFileSwaps(Mod mod, int groupIdx, int dataContainerIdx, IReadOnlyDictionary swaps, SaveType saveType = SaveType.Queue) { - var subMod = GetSubMod(mod, groupIdx, optionIdx); - if (subMod.FileSwapData.SetEquals(swaps)) + var subMod = GetSubMod(mod, groupIdx, dataContainerIdx); + if (subMod.FileSwaps.SetEquals(swaps)) return; - communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, optionIdx, -1); - subMod.FileSwapData.SetTo(swaps); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, dataContainerIdx, -1); + subMod.FileSwaps.SetTo(swaps); saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionSwapsChanged, mod, groupIdx, optionIdx, -1); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionSwapsChanged, mod, groupIdx, dataContainerIdx, -1); } @@ -389,17 +380,12 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS } /// Get the correct option for the given group and option index. - private static SubMod GetSubMod(Mod mod, int groupIdx, int optionIdx) + private static IModDataContainer GetSubMod(Mod mod, int groupIdx, int dataContainerIdx) { - if (groupIdx == -1 && optionIdx == 0) + if (groupIdx == -1 && dataContainerIdx == 0) return mod.Default; - return mod.Groups[groupIdx] switch - { - SingleModGroup s => s.OptionData[optionIdx], - MultiModGroup m => m.PrioritizedOptions[optionIdx].Mod, - _ => throw new InvalidOperationException(), - }; + return mod.Groups[groupIdx].DataContainers[dataContainerIdx]; } } diff --git a/Penumbra/Mods/Mod.cs b/Penumbra/Mods/Mod.cs index 71f64205..5c02213e 100644 --- a/Penumbra/Mods/Mod.cs +++ b/Penumbra/Mods/Mod.cs @@ -38,7 +38,7 @@ public sealed class Mod : IMod internal Mod(DirectoryInfo modPath) { ModPath = modPath; - Default = SubMod.CreateDefault(this); + Default = new DefaultSubMod(this); } public override string ToString() @@ -61,8 +61,8 @@ public sealed class Mod : IMod // Options - public readonly SubMod Default; - public readonly List Groups = []; + public readonly DefaultSubMod Default; + public readonly List Groups = []; public AppliedModData GetData(ModSettings? settings = null) { @@ -77,21 +77,16 @@ public sealed class Mod : IMod group.AddData(config, dictRedirections, setManips); } - Default.AddData(dictRedirections, setManips); + Default.AddDataTo(dictRedirections, setManips); return new AppliedModData(dictRedirections, setManips); } - public IEnumerable AllSubMods - => Groups.SelectMany(o => o switch - { - SingleModGroup single => single.OptionData, - MultiModGroup multi => multi.PrioritizedOptions.Select(s => s.Mod), - _ => [], - }).Prepend(Default); + public IEnumerable AllDataContainers + => Groups.SelectMany(o => o.DataContainers).Prepend(Default); public List FindUnusedFiles() { - var modFiles = AllSubMods.SelectMany(o => o.Files) + var modFiles = AllDataContainers.SelectMany(o => o.Files) .Select(p => p.Value) .ToHashSet(); return ModPath.EnumerateDirectories() diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index 4d32f395..c1236037 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -112,10 +112,8 @@ public partial class ModCreator( var defaultFile = _saveService.FileNames.OptionGroupFile(mod, -1, Config.ReplaceNonAsciiOnImport); try { - if (!File.Exists(defaultFile)) - mod.Default.Load(mod.ModPath, new JObject(), out _); - else - mod.Default.Load(mod.ModPath, JObject.Parse(File.ReadAllText(defaultFile)), out _); + var jObject = File.Exists(defaultFile) ? JObject.Parse(File.ReadAllText(defaultFile)) : new JObject(); + IModDataContainer.Load(jObject, mod.Default, mod.ModPath); } catch (Exception e) { @@ -154,7 +152,7 @@ public partial class ModCreator( { var changes = false; List deleteList = new(); - foreach (var subMod in mod.AllSubMods) + foreach (var subMod in mod.AllDataContainers) { var (localChanges, localDeleteList) = IncorporateMetaChanges(subMod, mod.ModPath, false); changes |= localChanges; @@ -162,7 +160,7 @@ public partial class ModCreator( deleteList.AddRange(localDeleteList); } - SubMod.DeleteDeleteList(deleteList, delete); + IModDataContainer.DeleteDeleteList(deleteList, delete); if (!changes) return; @@ -176,10 +174,10 @@ public partial class ModCreator( /// If .meta or .rgsp files are encountered, parse them and incorporate their meta changes into the mod. /// If delete is true, the files are deleted afterwards. /// - public (bool Changes, List DeleteList) IncorporateMetaChanges(SubMod option, DirectoryInfo basePath, bool delete) + public (bool Changes, List DeleteList) IncorporateMetaChanges(IModDataContainer option, DirectoryInfo basePath, bool delete) { var deleteList = new List(); - var oldSize = option.ManipulationData.Count; + var oldSize = option.Manipulations.Count; var deleteString = delete ? "with deletion." : "without deletion."; foreach (var (key, file) in option.Files.ToList()) { @@ -189,7 +187,7 @@ public partial class ModCreator( { if (ext1 == ".meta" || ext2 == ".meta") { - option.FileData.Remove(key); + option.Files.Remove(key); if (!file.Exists) continue; @@ -198,11 +196,11 @@ public partial class ModCreator( Penumbra.Log.Verbose( $"Incorporating {file} as Metadata file of {meta.MetaManipulations.Count} manipulations {deleteString}"); deleteList.Add(file.FullName); - option.ManipulationData.UnionWith(meta.MetaManipulations); + option.Manipulations.UnionWith(meta.MetaManipulations); } else if (ext1 == ".rgsp" || ext2 == ".rgsp") { - option.FileData.Remove(key); + option.Files.Remove(key); if (!file.Exists) continue; @@ -212,7 +210,7 @@ public partial class ModCreator( $"Incorporating {file} as racial scaling file of {rgsp.MetaManipulations.Count} manipulations {deleteString}"); deleteList.Add(file.FullName); - option.ManipulationData.UnionWith(rgsp.MetaManipulations); + option.Manipulations.UnionWith(rgsp.MetaManipulations); } } catch (Exception e) @@ -221,8 +219,8 @@ public partial class ModCreator( } } - SubMod.DeleteDeleteList(deleteList, delete); - return (oldSize < option.ManipulationData.Count, deleteList); + IModDataContainer.DeleteDeleteList(deleteList, delete); + return (oldSize < option.Manipulations.Count, deleteList); } /// @@ -238,7 +236,7 @@ public partial class ModCreator( /// Create a file for an option group from given data. public void CreateOptionGroup(DirectoryInfo baseFolder, GroupType type, string name, - ModPriority priority, int index, Setting defaultSettings, string desc, IEnumerable subMods) + ModPriority priority, int index, Setting defaultSettings, string desc, IEnumerable subMods) { switch (type) { @@ -248,7 +246,7 @@ public partial class ModCreator( group.Description = desc; group.Priority = priority; group.DefaultSettings = defaultSettings; - group.PrioritizedOptions.AddRange(subMods.Select((s, idx) => (s, new ModPriority(idx)))); + group.OptionData.AddRange(subMods.Select(s => s.Clone(null!, group))); _saveService.ImmediateSaveSync(new ModSaveGroup(baseFolder, group, index, Config.ReplaceNonAsciiOnImport)); break; } @@ -258,7 +256,7 @@ public partial class ModCreator( group.Description = desc; group.Priority = priority; group.DefaultSettings = defaultSettings; - group.OptionData.AddRange(subMods); + group.OptionData.AddRange(subMods.Select(s => s.ConvertToSingle(null!, group))); _saveService.ImmediateSaveSync(new ModSaveGroup(baseFolder, group, index, Config.ReplaceNonAsciiOnImport)); break; } @@ -266,16 +264,15 @@ public partial class ModCreator( } /// Create the data for a given sub mod from its data and the folder it is based on. - public SubMod CreateSubMod(DirectoryInfo baseFolder, DirectoryInfo optionFolder, OptionList option) + public MultiSubMod CreateSubMod(DirectoryInfo baseFolder, DirectoryInfo optionFolder, OptionList option, ModPriority priority) { var list = optionFolder.EnumerateNonHiddenFiles() .Select(f => (Utf8GamePath.FromFile(f, optionFolder, out var gamePath, true), gamePath, new FullPath(f))) .Where(t => t.Item1); - var mod = SubMod.CreateForSaving(option.Name); - mod.Description = option.Description; + var mod = MultiSubMod.CreateForSaving(option.Name, option.Description, priority); foreach (var (_, gamePath, file) in list) - mod.FileData.TryAdd(gamePath, file); + mod.Files.TryAdd(gamePath, file); IncorporateMetaChanges(mod, baseFolder, true); return mod; @@ -292,7 +289,7 @@ public partial class ModCreator( foreach (var file in mod.FindUnusedFiles()) { if (Utf8GamePath.FromFile(new FileInfo(file.FullName), directory, out var gamePath, true)) - mod.Default.FileData.TryAdd(gamePath, file); + mod.Default.Files.TryAdd(gamePath, file); } IncorporateMetaChanges(mod.Default, directory, true); diff --git a/Penumbra/Mods/Subclasses/IModDataContainer.cs b/Penumbra/Mods/Subclasses/IModDataContainer.cs index d0b444b8..a26beb2a 100644 --- a/Penumbra/Mods/Subclasses/IModDataContainer.cs +++ b/Penumbra/Mods/Subclasses/IModDataContainer.cs @@ -1,12 +1,16 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; using Penumbra.String.Classes; namespace Penumbra.Mods.Subclasses; public interface IModDataContainer { + public IMod Mod { get; } + public IModGroup? Group { get; } + public Dictionary Files { get; set; } public Dictionary FileSwaps { get; set; } public HashSet Manipulations { get; set; } @@ -21,6 +25,32 @@ public interface IModDataContainer manipulations.UnionWith(Manipulations); } + public string GetName() + => this switch + { + IModOption o => o.FullName, + DefaultSubMod => DefaultSubMod.FullName, + _ => $"Container {GetDataIndices().DataIndex + 1}", + }; + + public string GetFullName() + => this switch + { + IModOption o => o.FullName, + DefaultSubMod => DefaultSubMod.FullName, + _ when Group != null => $"{Group.Name}: Container {GetDataIndices().DataIndex + 1}", + _ => $"Container {GetDataIndices().DataIndex + 1}", + }; + + public static void Clone(IModDataContainer from, IModDataContainer to) + { + to.Files = new Dictionary(from.Files); + to.FileSwaps = new Dictionary(from.FileSwaps); + to.Manipulations = [.. from.Manipulations]; + } + + public (int GroupIndex, int DataIndex) GetDataIndices(); + public static void Load(JToken json, IModDataContainer data, DirectoryInfo basePath) { data.Files.Clear(); @@ -77,4 +107,22 @@ public interface IModDataContainer serializer.Serialize(j, data.Manipulations); j.WriteEndObject(); } + + internal static void DeleteDeleteList(IEnumerable deleteList, bool delete) + { + if (!delete) + return; + + foreach (var file in deleteList) + { + try + { + File.Delete(file); + } + catch (Exception e) + { + Penumbra.Log.Error($"Could not delete incorporated meta file {file}:\n{e}"); + } + } + } } diff --git a/Penumbra/Mods/Subclasses/IModGroup.cs b/Penumbra/Mods/Subclasses/IModGroup.cs index a046ade0..5c500793 100644 --- a/Penumbra/Mods/Subclasses/IModGroup.cs +++ b/Penumbra/Mods/Subclasses/IModGroup.cs @@ -1,3 +1,4 @@ +using FFXIVClientStructs.FFXIV.Client.UI.Agent; using Newtonsoft.Json; using Penumbra.Api.Enums; using Penumbra.Meta.Manipulations; @@ -6,6 +7,11 @@ using Penumbra.String.Classes; namespace Penumbra.Mods.Subclasses; +public interface ITexToolsGroup +{ + public IReadOnlyList OptionData { get; } +} + public interface IModGroup { public const int MaxMultiOptions = 63; @@ -19,28 +25,89 @@ public interface IModGroup public FullPath? FindBestMatch(Utf8GamePath gamePath); public int AddOption(Mod mod, string name, string description = ""); - public bool ChangeOptionDescription(int optionIndex, string newDescription); - public bool ChangeOptionName(int optionIndex, string newName); - public IReadOnlyList Options { get; } - public bool IsOption { get; } + public IReadOnlyList Options { get; } + public IReadOnlyList DataContainers { get; } + public bool IsOption { get; } public IModGroup Convert(GroupType type); public bool MoveOption(int optionIdxFrom, int optionIdxTo); + public int GetIndex(); + public void AddData(Setting setting, Dictionary redirections, HashSet manipulations); /// Ensure that a value is valid for a group. public Setting FixSetting(Setting setting); + + public void WriteJson(JsonTextWriter jWriter, JsonSerializer serializer, DirectoryInfo? basePath = null); + + public bool ChangeOptionDescription(int optionIndex, string newDescription) + { + if (optionIndex < 0 || optionIndex >= Options.Count) + return false; + + var option = Options[optionIndex]; + if (option.Description == newDescription) + return false; + + option.Description = newDescription; + return true; + } + + public bool ChangeOptionName(int optionIndex, string newName) + { + if (optionIndex < 0 || optionIndex >= Options.Count) + return false; + + var option = Options[optionIndex]; + if (option.Name == newName) + return false; + + option.Name = newName; + return true; + } + + public static void WriteJsonBase(JsonTextWriter jWriter, IModGroup group) + { + jWriter.WriteStartObject(); + jWriter.WritePropertyName(nameof(group.Name)); + jWriter.WriteValue(group!.Name); + jWriter.WritePropertyName(nameof(group.Description)); + jWriter.WriteValue(group.Description); + jWriter.WritePropertyName(nameof(group.Priority)); + jWriter.WriteValue(group.Priority.Value); + jWriter.WritePropertyName(nameof(group.Type)); + jWriter.WriteValue(group.Type.ToString()); + jWriter.WritePropertyName(nameof(group.DefaultSettings)); + jWriter.WriteValue(group.DefaultSettings.Value); + } + + public (int Redirections, int Swaps, int Manips) GetCounts(); + + public static (int Redirections, int Swaps, int Manips) GetCountsBase(IModGroup group) + { + var redirectionCount = 0; + var swapCount = 0; + var manipCount = 0; + foreach (var option in group.DataContainers) + { + redirectionCount += option.Files.Count; + swapCount += option.FileSwaps.Count; + manipCount += option.Manipulations.Count; + } + + return (redirectionCount, swapCount, manipCount); + } } public readonly struct ModSaveGroup : ISavable { - private readonly DirectoryInfo _basePath; - private readonly IModGroup? _group; - private readonly int _groupIdx; - private readonly SubMod? _defaultMod; - private readonly bool _onlyAscii; + private readonly DirectoryInfo _basePath; + private readonly IModGroup? _group; + private readonly int _groupIdx; + private readonly DefaultSubMod? _defaultMod; + private readonly bool _onlyAscii; public ModSaveGroup(Mod mod, int groupIdx, bool onlyAscii) { @@ -61,7 +128,7 @@ public readonly struct ModSaveGroup : ISavable _onlyAscii = onlyAscii; } - public ModSaveGroup(DirectoryInfo basePath, SubMod @default, bool onlyAscii) + public ModSaveGroup(DirectoryInfo basePath, DefaultSubMod @default, bool onlyAscii) { _basePath = basePath; _groupIdx = -1; @@ -77,42 +144,11 @@ public readonly struct ModSaveGroup : ISavable using var j = new JsonTextWriter(writer); j.Formatting = Formatting.Indented; var serializer = new JsonSerializer { Formatting = Formatting.Indented }; + j.WriteStartObject(); if (_groupIdx >= 0) - { - j.WriteStartObject(); - j.WritePropertyName(nameof(_group.Name)); - j.WriteValue(_group!.Name); - j.WritePropertyName(nameof(_group.Description)); - j.WriteValue(_group.Description); - j.WritePropertyName(nameof(_group.Priority)); - j.WriteValue(_group.Priority.Value); - j.WritePropertyName(nameof(Type)); - j.WriteValue(_group.Type.ToString()); - j.WritePropertyName(nameof(_group.DefaultSettings)); - j.WriteValue(_group.DefaultSettings.Value); - switch (_group) - { - case SingleModGroup single: - j.WritePropertyName("Options"); - j.WriteStartArray(); - foreach (var option in single.OptionData) - SubMod.WriteSubMod(j, serializer, option, _basePath, null); - j.WriteEndArray(); - j.WriteEndObject(); - break; - case MultiModGroup multi: - j.WritePropertyName("Options"); - j.WriteStartArray(); - foreach (var (option, priority) in multi.PrioritizedOptions) - SubMod.WriteSubMod(j, serializer, option, _basePath, priority); - j.WriteEndArray(); - j.WriteEndObject(); - break; - } - } + _group!.WriteJson(j, serializer); else - { - SubMod.WriteSubMod(j, serializer, _defaultMod!, _basePath, null); - } + IModDataContainer.WriteModData(j, serializer, _defaultMod!, _basePath); + j.WriteEndObject(); } } diff --git a/Penumbra/Mods/Subclasses/IModOption.cs b/Penumbra/Mods/Subclasses/IModOption.cs index bb52a2cd..f66ce44e 100644 --- a/Penumbra/Mods/Subclasses/IModOption.cs +++ b/Penumbra/Mods/Subclasses/IModOption.cs @@ -15,6 +15,8 @@ public interface IModOption option.Description = json[nameof(Description)]?.ToObject() ?? string.Empty; } + public (int GroupIndex, int OptionIndex) GetOptionIndices(); + public static void WriteModOption(JsonWriter j, IModOption option) { j.WritePropertyName(nameof(Name)); diff --git a/Penumbra/Mods/Subclasses/MultiModGroup.cs b/Penumbra/Mods/Subclasses/MultiModGroup.cs index 4ec2c72a..f194350a 100644 --- a/Penumbra/Mods/Subclasses/MultiModGroup.cs +++ b/Penumbra/Mods/Subclasses/MultiModGroup.cs @@ -1,4 +1,5 @@ using Dalamud.Interface.Internal.Notifications; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui; using OtterGui.Classes; @@ -10,20 +11,30 @@ using Penumbra.String.Classes; namespace Penumbra.Mods.Subclasses; /// Groups that allow all available options to be selected at once. -public sealed class MultiModGroup(Mod mod) : IModGroup +public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup { public GroupType Type => GroupType.Multi; - public Mod Mod { get; set; } = mod; - public string Name { get; set; } = "Group"; - public string Description { get; set; } = "A non-exclusive group of settings."; - public ModPriority Priority { get; set; } - public Setting DefaultSettings { get; set; } + public Mod Mod { get; set; } = mod; + public string Name { get; set; } = "Group"; + public string Description { get; set; } = "A non-exclusive group of settings."; + public ModPriority Priority { get; set; } + public Setting DefaultSettings { get; set; } + public readonly List OptionData = []; + + public IReadOnlyList Options + => OptionData; + + public IReadOnlyList DataContainers + => OptionData; + + public bool IsOption + => OptionData.Count > 0; public FullPath? FindBestMatch(Utf8GamePath gamePath) - => PrioritizedOptions.OrderByDescending(o => o.Priority) - .SelectWhere(o => (o.Mod.FileData.TryGetValue(gamePath, out var file) || o.Mod.FileSwapData.TryGetValue(gamePath, out file), file)) + => OptionData.OrderByDescending(o => o.Priority) + .SelectWhere(o => (o.Files.TryGetValue(gamePath, out var file) || o.FileSwaps.TryGetValue(gamePath, out file), file)) .FirstOrDefault(); public int AddOption(Mod mod, string name, string description = "") @@ -32,49 +43,15 @@ public sealed class MultiModGroup(Mod mod) : IModGroup if (groupIdx < 0) return -1; - var subMod = new SubMod(mod, this) + var subMod = new MultiSubMod(mod, this) { Name = name, Description = description, }; - PrioritizedOptions.Add((subMod, ModPriority.Default)); - return PrioritizedOptions.Count - 1; + OptionData.Add(subMod); + return OptionData.Count - 1; } - public bool ChangeOptionDescription(int optionIndex, string newDescription) - { - if (optionIndex < 0 || optionIndex >= PrioritizedOptions.Count) - return false; - - var option = PrioritizedOptions[optionIndex].Mod; - if (option.Description == newDescription) - return false; - - option.Description = newDescription; - return true; - } - - public bool ChangeOptionName(int optionIndex, string newName) - { - if (optionIndex < 0 || optionIndex >= PrioritizedOptions.Count) - return false; - - var option = PrioritizedOptions[optionIndex].Mod; - if (option.Name == newName) - return false; - - option.Name = newName; - return true; - } - - public IReadOnlyList Options - => PrioritizedOptions.Select(p => p.Mod).ToArray(); - - public bool IsOption - => PrioritizedOptions.Count > 0; - - public readonly List<(SubMod Mod, ModPriority Priority)> PrioritizedOptions = []; - public static MultiModGroup? Load(Mod mod, JObject json, int groupIdx) { var ret = new MultiModGroup(mod) @@ -91,7 +68,7 @@ public sealed class MultiModGroup(Mod mod) : IModGroup if (options != null) foreach (var child in options.Children()) { - if (ret.PrioritizedOptions.Count == IModGroup.MaxMultiOptions) + if (ret.OptionData.Count == IModGroup.MaxMultiOptions) { Penumbra.Messager.NotificationMessage( $"Multi Group {ret.Name} in {mod.Name} has more than {IModGroup.MaxMultiOptions} options, ignoring excessive options.", @@ -99,9 +76,8 @@ public sealed class MultiModGroup(Mod mod) : IModGroup break; } - var subMod = new SubMod(mod, ret); - subMod.Load(mod.ModPath, child, out var priority); - ret.PrioritizedOptions.Add((subMod, priority)); + var subMod = new MultiSubMod(mod, ret, child); + ret.OptionData.Add(subMod); } ret.DefaultSettings = ret.FixSetting(ret.DefaultSettings); @@ -115,39 +91,68 @@ public sealed class MultiModGroup(Mod mod) : IModGroup { case GroupType.Multi: return this; case GroupType.Single: - var multi = new SingleModGroup(Mod) + var single = new SingleModGroup(Mod) { Name = Name, Description = Description, Priority = Priority, - DefaultSettings = DefaultSettings.TurnMulti(PrioritizedOptions.Count), + DefaultSettings = DefaultSettings.TurnMulti(OptionData.Count), }; - multi.OptionData.AddRange(PrioritizedOptions.Select(p => p.Mod)); - return multi; + single.OptionData.AddRange(OptionData.Select(o => o.ConvertToSingle(Mod, single))); + return single; default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } } public bool MoveOption(int optionIdxFrom, int optionIdxTo) { - if (!PrioritizedOptions.Move(optionIdxFrom, optionIdxTo)) + if (!OptionData.Move(optionIdxFrom, optionIdxTo)) return false; DefaultSettings = DefaultSettings.MoveBit(optionIdxFrom, optionIdxTo); return true; } + public int GetIndex() + { + var groupIndex = Mod.Groups.IndexOf(this); + if (groupIndex < 0) + throw new Exception($"Mod {Mod.Name} from Group {Name} does not contain this group."); + + return groupIndex; + } + public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) { - foreach (var (option, index) in PrioritizedOptions.WithIndex().OrderByDescending(o => o.Value.Priority)) + foreach (var (option, index) in OptionData.WithIndex().OrderByDescending(o => o.Value.Priority)) { if (setting.HasFlag(index)) - option.Mod.AddData(redirections, manipulations); + option.AddDataTo(redirections, manipulations); } } + public void WriteJson(JsonTextWriter jWriter, JsonSerializer serializer, DirectoryInfo? basePath = null) + { + IModGroup.WriteJsonBase(jWriter, this); + jWriter.WritePropertyName("Options"); + jWriter.WriteStartArray(); + foreach (var option in OptionData) + { + IModOption.WriteModOption(jWriter, option); + jWriter.WritePropertyName(nameof(option.Priority)); + jWriter.WriteValue(option.Priority.Value); + IModDataContainer.WriteModData(jWriter, serializer, option, basePath ?? Mod.ModPath); + } + + jWriter.WriteEndArray(); + jWriter.WriteEndObject(); + } + + public (int Redirections, int Swaps, int Manips) GetCounts() + => IModGroup.GetCountsBase(this); + public Setting FixSetting(Setting setting) - => new(setting.Value & ((1ul << PrioritizedOptions.Count) - 1)); + => new(setting.Value & ((1ul << OptionData.Count) - 1)); /// Create a group without a mod only for saving it in the creator. internal static MultiModGroup CreateForSaving(string name) @@ -155,4 +160,7 @@ public sealed class MultiModGroup(Mod mod) : IModGroup { Name = name, }; + + IReadOnlyList ITexToolsGroup.OptionData + => OptionData; } diff --git a/Penumbra/Mods/Subclasses/SingleModGroup.cs b/Penumbra/Mods/Subclasses/SingleModGroup.cs index 994a1f96..d1a3b6d1 100644 --- a/Penumbra/Mods/Subclasses/SingleModGroup.cs +++ b/Penumbra/Mods/Subclasses/SingleModGroup.cs @@ -1,3 +1,4 @@ +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui; using OtterGui.Filesystem; @@ -8,7 +9,7 @@ using Penumbra.String.Classes; namespace Penumbra.Mods.Subclasses; /// Groups that allow only one of their available options to be selected. -public sealed class SingleModGroup(Mod mod) : IModGroup +public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup { public GroupType Type => GroupType.Single; @@ -19,16 +20,19 @@ public sealed class SingleModGroup(Mod mod) : IModGroup public ModPriority Priority { get; set; } public Setting DefaultSettings { get; set; } - public readonly List OptionData = []; + public readonly List OptionData = []; + + IReadOnlyList ITexToolsGroup.OptionData + => OptionData; public FullPath? FindBestMatch(Utf8GamePath gamePath) => OptionData - .SelectWhere(m => (m.FileData.TryGetValue(gamePath, out var file) || m.FileSwapData.TryGetValue(gamePath, out file), file)) + .SelectWhere(m => (m.Files.TryGetValue(gamePath, out var file) || m.FileSwaps.TryGetValue(gamePath, out file), file)) .FirstOrDefault(); public int AddOption(Mod mod, string name, string description = "") { - var subMod = new SubMod(mod, this) + var subMod = new SingleSubMod(mod, this) { Name = name, Description = description, @@ -37,35 +41,12 @@ public sealed class SingleModGroup(Mod mod) : IModGroup return OptionData.Count - 1; } - public bool ChangeOptionDescription(int optionIndex, string newDescription) - { - if (optionIndex < 0 || optionIndex >= OptionData.Count) - return false; - - var option = OptionData[optionIndex]; - if (option.Description == newDescription) - return false; - - option.Description = newDescription; - return true; - } - - public bool ChangeOptionName(int optionIndex, string newName) - { - if (optionIndex < 0 || optionIndex >= OptionData.Count) - return false; - - var option = OptionData[optionIndex]; - if (option.Name == newName) - return false; - - option.Name = newName; - return true; - } - public IReadOnlyList Options => OptionData; + public IReadOnlyList DataContainers + => OptionData; + public bool IsOption => OptionData.Count > 1; @@ -85,8 +66,7 @@ public sealed class SingleModGroup(Mod mod) : IModGroup if (options != null) foreach (var child in options.Children()) { - var subMod = new SubMod(mod, ret); - subMod.Load(mod.ModPath, child, out _); + var subMod = new SingleSubMod(mod, ret, child); ret.OptionData.Add(subMod); } @@ -107,7 +87,7 @@ public sealed class SingleModGroup(Mod mod) : IModGroup Priority = Priority, DefaultSettings = Setting.Multi((int)DefaultSettings.Value), }; - multi.PrioritizedOptions.AddRange(OptionData.Select((o, i) => (o, new ModPriority(i)))); + multi.OptionData.AddRange(OptionData.Select((o, i) => o.ConvertToMulti(Mod, multi, new ModPriority(i)))); return multi; default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } @@ -137,12 +117,39 @@ public sealed class SingleModGroup(Mod mod) : IModGroup return true; } + public int GetIndex() + { + var groupIndex = Mod.Groups.IndexOf(this); + if (groupIndex < 0) + throw new Exception($"Mod {Mod.Name} from Group {Name} does not contain this group."); + + return groupIndex; + } + public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) - => OptionData[setting.AsIndex].AddData(redirections, manipulations); + => OptionData[setting.AsIndex].AddDataTo(redirections, manipulations); public Setting FixSetting(Setting setting) => OptionData.Count == 0 ? Setting.Zero : new Setting(Math.Min(setting.Value, (ulong)(OptionData.Count - 1))); + public (int Redirections, int Swaps, int Manips) GetCounts() + => IModGroup.GetCountsBase(this); + + public void WriteJson(JsonTextWriter jWriter, JsonSerializer serializer, DirectoryInfo? basePath = null) + { + IModGroup.WriteJsonBase(jWriter, this); + jWriter.WritePropertyName("Options"); + jWriter.WriteStartArray(); + foreach (var option in OptionData) + { + IModOption.WriteModOption(jWriter, option); + IModDataContainer.WriteModData(jWriter, serializer, option, basePath ?? Mod.ModPath); + } + + jWriter.WriteEndArray(); + jWriter.WriteEndObject(); + } + /// Create a group without a mod only for saving it in the creator. internal static SingleModGroup CreateForSaving(string name) => new(null!) diff --git a/Penumbra/Mods/Subclasses/SubMod.cs b/Penumbra/Mods/Subclasses/SubMod.cs index bc93fcc4..a2425eb7 100644 --- a/Penumbra/Mods/Subclasses/SubMod.cs +++ b/Penumbra/Mods/Subclasses/SubMod.cs @@ -7,7 +7,9 @@ using Penumbra.String.Classes; namespace Penumbra.Mods.Subclasses; -public class SingleSubMod(Mod mod, SingleModGroup group) : IModOption, IModDataContainer +public interface IModDataOption : IModOption, IModDataContainer; + +public class SingleSubMod(Mod mod, SingleModGroup group) : IModDataOption { internal readonly Mod Mod = mod; internal readonly SingleModGroup Group = group; @@ -19,12 +21,68 @@ public class SingleSubMod(Mod mod, SingleModGroup group) : IModOption, IModDataC public string Description { get; set; } = string.Empty; + IMod IModDataContainer.Mod + => Mod; + + IModGroup IModDataContainer.Group + => Group; + public Dictionary Files { get; set; } = []; public Dictionary FileSwaps { get; set; } = []; public HashSet Manipulations { get; set; } = []; + + public SingleSubMod(Mod mod, SingleModGroup group, JToken json) + : this(mod, group) + { + IModOption.Load(json, this); + IModDataContainer.Load(json, this, mod.ModPath); + } + + public SingleSubMod Clone(Mod mod, SingleModGroup group) + { + var ret = new SingleSubMod(mod, group) + { + Name = Name, + Description = Description, + }; + IModDataContainer.Clone(this, ret); + + return ret; + } + + public MultiSubMod ConvertToMulti(Mod mod, MultiModGroup group, ModPriority priority) + { + var ret = new MultiSubMod(mod, group) + { + Name = Name, + Description = Description, + Priority = priority, + }; + IModDataContainer.Clone(this, ret); + + return ret; + } + + public void AddDataTo(Dictionary redirections, HashSet manipulations) + => ((IModDataContainer)this).AddDataTo(redirections, manipulations); + + public (int GroupIndex, int DataIndex) GetDataIndices() + => (Group.GetIndex(), GetDataIndex()); + + public (int GroupIndex, int OptionIndex) GetOptionIndices() + => (Group.GetIndex(), GetDataIndex()); + + private int GetDataIndex() + { + var dataIndex = Group.DataContainers.IndexOf(this); + if (dataIndex < 0) + throw new Exception($"Group {Group.Name} from SubMod {Name} does not contain this SubMod."); + + return dataIndex; + } } -public class MultiSubMod(Mod mod, MultiModGroup group) : IModOption, IModDataContainer +public class MultiSubMod(Mod mod, MultiModGroup group) : IModDataOption { internal readonly Mod Mod = mod; internal readonly MultiModGroup Group = group; @@ -40,12 +98,76 @@ public class MultiSubMod(Mod mod, MultiModGroup group) : IModOption, IModDataCon public Dictionary Files { get; set; } = []; public Dictionary FileSwaps { get; set; } = []; public HashSet Manipulations { get; set; } = []; + + IMod IModDataContainer.Mod + => Mod; + + IModGroup IModDataContainer.Group + => Group; + + + public MultiSubMod(Mod mod, MultiModGroup group, JToken json) + : this(mod, group) + { + IModOption.Load(json, this); + IModDataContainer.Load(json, this, mod.ModPath); + Priority = json[nameof(IModGroup.Priority)]?.ToObject() ?? ModPriority.Default; + } + + public MultiSubMod Clone(Mod mod, MultiModGroup group) + { + var ret = new MultiSubMod(mod, group) + { + Name = Name, + Description = Description, + Priority = Priority, + }; + IModDataContainer.Clone(this, ret); + + return ret; + } + + public SingleSubMod ConvertToSingle(Mod mod, SingleModGroup group) + { + var ret = new SingleSubMod(mod, group) + { + Name = Name, + Description = Description, + }; + IModDataContainer.Clone(this, ret); + return ret; + } + + public void AddDataTo(Dictionary redirections, HashSet manipulations) + => ((IModDataContainer)this).AddDataTo(redirections, manipulations); + + public static MultiSubMod CreateForSaving(string name, string description, ModPriority priority) + => new(null!, null!) + { + Name = name, + Description = description, + Priority = priority, + }; + + public (int GroupIndex, int DataIndex) GetDataIndices() + => (Group.GetIndex(), GetDataIndex()); + + public (int GroupIndex, int OptionIndex) GetOptionIndices() + => (Group.GetIndex(), GetDataIndex()); + + private int GetDataIndex() + { + var dataIndex = Group.DataContainers.IndexOf(this); + if (dataIndex < 0) + throw new Exception($"Group {Group.Name} from SubMod {Name} does not contain this SubMod."); + + return dataIndex; + } } public class DefaultSubMod(IMod mod) : IModDataContainer { - public string FullName - => "Default Option"; + public const string FullName = "Default Option"; public string Description => string.Empty; @@ -55,183 +177,176 @@ public class DefaultSubMod(IMod mod) : IModDataContainer public Dictionary Files { get; set; } = []; public Dictionary FileSwaps { get; set; } = []; public HashSet Manipulations { get; set; } = []; + + IMod IModDataContainer.Mod + => Mod; + + IModGroup? IModDataContainer.Group + => null; + + + public DefaultSubMod(Mod mod, JToken json) + : this(mod) + { + IModDataContainer.Load(json, this, mod.ModPath); + } + + public void AddDataTo(Dictionary redirections, HashSet manipulations) + => ((IModDataContainer)this).AddDataTo(redirections, manipulations); + + public (int GroupIndex, int DataIndex) GetDataIndices() + => (-1, 0); } -/// -/// A sub mod is a collection of -/// - file replacements -/// - file swaps -/// - meta manipulations -/// that can be used either as an option or as the default data for a mod. -/// It can be loaded and reloaded from Json. -/// Nothing is checked for existence or validity when loading. -/// Objects are also not checked for uniqueness, the first appearance of a game path or meta path decides. -/// -public sealed class SubMod(IMod mod, IModGroup group) : IModOption -{ - public string Name { get; set; } = "Default"; - public string FullName - => Group == null ? "Default Option" : $"{Group.Name}: {Name}"; - - public string Description { get; set; } = string.Empty; - - internal readonly IMod Mod = mod; - internal readonly IModGroup? Group = group; - - internal (int GroupIdx, int OptionIdx) GetIndices() - { - if (IsDefault) - return (-1, 0); - - var groupIdx = Mod.Groups.IndexOf(Group); - if (groupIdx < 0) - throw new Exception($"Group {Group.Name} from SubMod {Name} is not contained in Mod {Mod.Name}."); - - return (groupIdx, GetOptionIndex()); - } - - private int GetOptionIndex() - { - var optionIndex = Group switch - { - null => 0, - SingleModGroup single => single.OptionData.IndexOf(this), - MultiModGroup multi => multi.PrioritizedOptions.IndexOf(p => p.Mod == this), - _ => throw new Exception($"Group {Group.Name} from SubMod {Name} has unknown type {typeof(Group)}"), - }; - if (optionIndex < 0) - throw new Exception($"Group {Group!.Name} from SubMod {Name} does not contain this SubMod."); - - return optionIndex; - } - - public static SubMod CreateDefault(IMod mod) - => new(mod, null!); - - [MemberNotNullWhen(false, nameof(Group))] - public bool IsDefault - => Group == null; - - public void AddData(Dictionary redirections, HashSet manipulations) - { - foreach (var (path, file) in Files) - redirections.TryAdd(path, file); - - foreach (var (path, file) in FileSwaps) - redirections.TryAdd(path, file); - manipulations.UnionWith(Manipulations); - } - - public Dictionary FileData = []; - public Dictionary FileSwapData = []; - public HashSet ManipulationData = []; - - public IReadOnlyDictionary Files - => FileData; - - public IReadOnlyDictionary FileSwaps - => FileSwapData; - - public IReadOnlySet Manipulations - => ManipulationData; - - public void Load(DirectoryInfo basePath, JToken json, out ModPriority priority) - { - FileData.Clear(); - FileSwapData.Clear(); - ManipulationData.Clear(); - - // Every option has a name, but priorities are only relevant for multi group options. - Name = json[nameof(Name)]?.ToObject() ?? string.Empty; - Description = json[nameof(Description)]?.ToObject() ?? string.Empty; - priority = json[nameof(IModGroup.Priority)]?.ToObject() ?? ModPriority.Default; - - var files = (JObject?)json[nameof(Files)]; - if (files != null) - foreach (var property in files.Properties()) - { - if (Utf8GamePath.FromString(property.Name, out var p, true)) - FileData.TryAdd(p, new FullPath(basePath, property.Value.ToObject())); - } - - var swaps = (JObject?)json[nameof(FileSwaps)]; - if (swaps != null) - foreach (var property in swaps.Properties()) - { - if (Utf8GamePath.FromString(property.Name, out var p, true)) - FileSwapData.TryAdd(p, new FullPath(property.Value.ToObject()!)); - } - - var manips = json[nameof(Manipulations)]; - if (manips != null) - foreach (var s in manips.Children().Select(c => c.ToObject()) - .Where(m => m.Validate())) - ManipulationData.Add(s); - } - - internal static void DeleteDeleteList(IEnumerable deleteList, bool delete) - { - if (!delete) - return; - - foreach (var file in deleteList) - { - try - { - File.Delete(file); - } - catch (Exception e) - { - Penumbra.Log.Error($"Could not delete incorporated meta file {file}:\n{e}"); - } - } - } - - /// Create a sub mod without a mod or group only for saving it in the creator. - internal static SubMod CreateForSaving(string name) - => new(null!, null!) - { - Name = name, - }; - - - public static void WriteSubMod(JsonWriter j, JsonSerializer serializer, SubMod mod, DirectoryInfo basePath, ModPriority? priority) - { - j.WriteStartObject(); - j.WritePropertyName(nameof(Name)); - j.WriteValue(mod.Name); - j.WritePropertyName(nameof(Description)); - j.WriteValue(mod.Description); - if (priority != null) - { - j.WritePropertyName(nameof(IModGroup.Priority)); - j.WriteValue(priority.Value.Value); - } - - j.WritePropertyName(nameof(mod.Files)); - j.WriteStartObject(); - foreach (var (gamePath, file) in mod.Files) - { - if (file.ToRelPath(basePath, out var relPath)) - { - j.WritePropertyName(gamePath.ToString()); - j.WriteValue(relPath.ToString()); - } - } - - j.WriteEndObject(); - j.WritePropertyName(nameof(mod.FileSwaps)); - j.WriteStartObject(); - foreach (var (gamePath, file) in mod.FileSwaps) - { - j.WritePropertyName(gamePath.ToString()); - j.WriteValue(file.ToString()); - } - - j.WriteEndObject(); - j.WritePropertyName(nameof(mod.Manipulations)); - serializer.Serialize(j, mod.Manipulations); - j.WriteEndObject(); - } -} +//public sealed class SubMod(IMod mod, IModGroup group) : IModOption +//{ +// public string Name { get; set; } = "Default"; +// +// public string FullName +// => Group == null ? "Default Option" : $"{Group.Name}: {Name}"; +// +// public string Description { get; set; } = string.Empty; +// +// internal readonly IMod Mod = mod; +// internal readonly IModGroup? Group = group; +// +// internal (int GroupIdx, int OptionIdx) GetIndices() +// { +// if (IsDefault) +// return (-1, 0); +// +// var groupIdx = Mod.Groups.IndexOf(Group); +// if (groupIdx < 0) +// throw new Exception($"Group {Group.Name} from SubMod {Name} is not contained in Mod {Mod.Name}."); +// +// return (groupIdx, GetOptionIndex()); +// } +// +// private int GetOptionIndex() +// { +// var optionIndex = Group switch +// { +// null => 0, +// SingleModGroup single => single.OptionData.IndexOf(this), +// MultiModGroup multi => multi.OptionData.IndexOf(p => p.Mod == this), +// _ => throw new Exception($"Group {Group.Name} from SubMod {Name} has unknown type {typeof(Group)}"), +// }; +// if (optionIndex < 0) +// throw new Exception($"Group {Group!.Name} from SubMod {Name} does not contain this SubMod."); +// +// return optionIndex; +// } +// +// public static SubMod CreateDefault(IMod mod) +// => new(mod, null!); +// +// [MemberNotNullWhen(false, nameof(Group))] +// public bool IsDefault +// => Group == null; +// +// public void AddData(Dictionary redirections, HashSet manipulations) +// { +// foreach (var (path, file) in Files) +// redirections.TryAdd(path, file); +// +// foreach (var (path, file) in FileSwaps) +// redirections.TryAdd(path, file); +// manipulations.UnionWith(Manipulations); +// } +// +// public Dictionary FileData = []; +// public Dictionary FileSwapData = []; +// public HashSet ManipulationData = []; +// +// public IReadOnlyDictionary Files +// => FileData; +// +// public IReadOnlyDictionary FileSwaps +// => FileSwapData; +// +// public IReadOnlySet Manipulations +// => ManipulationData; +// +// public void Load(DirectoryInfo basePath, JToken json, out ModPriority priority) +// { +// FileData.Clear(); +// FileSwapData.Clear(); +// ManipulationData.Clear(); +// +// // Every option has a name, but priorities are only relevant for multi group options. +// Name = json[nameof(Name)]?.ToObject() ?? string.Empty; +// Description = json[nameof(Description)]?.ToObject() ?? string.Empty; +// priority = json[nameof(IModGroup.Priority)]?.ToObject() ?? ModPriority.Default; +// +// var files = (JObject?)json[nameof(Files)]; +// if (files != null) +// foreach (var property in files.Properties()) +// { +// if (Utf8GamePath.FromString(property.Name, out var p, true)) +// FileData.TryAdd(p, new FullPath(basePath, property.Value.ToObject())); +// } +// +// var swaps = (JObject?)json[nameof(FileSwaps)]; +// if (swaps != null) +// foreach (var property in swaps.Properties()) +// { +// if (Utf8GamePath.FromString(property.Name, out var p, true)) +// FileSwapData.TryAdd(p, new FullPath(property.Value.ToObject()!)); +// } +// +// var manips = json[nameof(Manipulations)]; +// if (manips != null) +// foreach (var s in manips.Children().Select(c => c.ToObject()) +// .Where(m => m.Validate())) +// ManipulationData.Add(s); +// } +// +// +// /// Create a sub mod without a mod or group only for saving it in the creator. +// internal static SubMod CreateForSaving(string name) +// => new(null!, null!) +// { +// Name = name, +// }; +// +// +// public static void WriteSubMod(JsonWriter j, JsonSerializer serializer, SubMod mod, DirectoryInfo basePath, ModPriority? priority) +// { +// j.WriteStartObject(); +// j.WritePropertyName(nameof(Name)); +// j.WriteValue(mod.Name); +// j.WritePropertyName(nameof(Description)); +// j.WriteValue(mod.Description); +// if (priority != null) +// { +// j.WritePropertyName(nameof(IModGroup.Priority)); +// j.WriteValue(priority.Value.Value); +// } +// +// j.WritePropertyName(nameof(mod.Files)); +// j.WriteStartObject(); +// foreach (var (gamePath, file) in mod.Files) +// { +// if (file.ToRelPath(basePath, out var relPath)) +// { +// j.WritePropertyName(gamePath.ToString()); +// j.WriteValue(relPath.ToString()); +// } +// } +// +// j.WriteEndObject(); +// j.WritePropertyName(nameof(mod.FileSwaps)); +// j.WriteStartObject(); +// foreach (var (gamePath, file) in mod.FileSwaps) +// { +// j.WritePropertyName(gamePath.ToString()); +// j.WriteValue(file.ToString()); +// } +// +// j.WriteEndObject(); +// j.WritePropertyName(nameof(mod.Manipulations)); +// serializer.Serialize(j, mod.Manipulations); +// j.WriteEndObject(); +// } +//} diff --git a/Penumbra/Mods/TemporaryMod.cs b/Penumbra/Mods/TemporaryMod.cs index a599b3bb..393369d4 100644 --- a/Penumbra/Mods/TemporaryMod.cs +++ b/Penumbra/Mods/TemporaryMod.cs @@ -18,49 +18,46 @@ public class TemporaryMod : IMod public int TotalManipulations => Default.Manipulations.Count; - public readonly SubMod Default; + public readonly DefaultSubMod Default; public AppliedModData GetData(ModSettings? settings = null) { Dictionary dict; - if (Default.FileSwapData.Count == 0) + if (Default.FileSwaps.Count == 0) { - dict = Default.FileData; + dict = Default.Files; } - else if (Default.FileData.Count == 0) + else if (Default.Files.Count == 0) { - dict = Default.FileSwapData; + dict = Default.FileSwaps; } else { // Need to ensure uniqueness. - dict = new Dictionary(Default.FileData.Count + Default.FileSwaps.Count); - foreach (var (gamePath, file) in Default.FileData.Concat(Default.FileSwaps)) + dict = new Dictionary(Default.Files.Count + Default.FileSwaps.Count); + foreach (var (gamePath, file) in Default.Files.Concat(Default.FileSwaps)) dict.TryAdd(gamePath, file); } - return new AppliedModData(dict, Default.ManipulationData); + return new AppliedModData(dict, Default.Manipulations); } public IReadOnlyList Groups => Array.Empty(); - public IEnumerable AllSubMods - => [Default]; - public TemporaryMod() - => Default = SubMod.CreateDefault(this); + => Default = new(this); public void SetFile(Utf8GamePath gamePath, FullPath fullPath) - => Default.FileData[gamePath] = fullPath; + => Default.Files[gamePath] = fullPath; public bool SetManipulation(MetaManipulation manip) - => Default.ManipulationData.Remove(manip) | Default.ManipulationData.Add(manip); + => Default.Manipulations.Remove(manip) | Default.Manipulations.Add(manip); public void SetAll(Dictionary dict, HashSet manips) { - Default.FileData = dict; - Default.ManipulationData = manips; + Default.Files = dict; + Default.Manipulations = manips; } public static void SaveTempCollection(Configuration config, SaveService saveService, ModManager modManager, ModCollection collection, @@ -93,16 +90,16 @@ public class TemporaryMod : IMod { var target = Path.Combine(fileDir.FullName, Path.GetFileName(targetPath)); File.Copy(targetPath, target, true); - defaultMod.FileData[gamePath] = new FullPath(target); + defaultMod.Files[gamePath] = new FullPath(target); } else { - defaultMod.FileSwapData[gamePath] = new FullPath(targetPath); + defaultMod.FileSwaps[gamePath] = new FullPath(targetPath); } } foreach (var manip in collection.MetaCache?.Manipulations ?? Array.Empty()) - defaultMod.ManipulationData.Add(manip); + defaultMod.Manipulations.Add(manip); saveService.ImmediateSave(new ModSaveGroup(dir, defaultMod, config.ReplaceNonAsciiOnImport)); modManager.AddMod(dir); diff --git a/Penumbra/UI/AdvancedWindow/FileEditor.cs b/Penumbra/UI/AdvancedWindow/FileEditor.cs index c891d33a..503d64b7 100644 --- a/Penumbra/UI/AdvancedWindow/FileEditor.cs +++ b/Penumbra/UI/AdvancedWindow/FileEditor.cs @@ -305,7 +305,7 @@ public class FileEditor( UiHelpers.Text(gamePath.Path); ImGui.TableNextColumn(); using var color = ImRaii.PushColor(ImGuiCol.Text, ColorId.ItemId.Value()); - ImGui.TextUnformatted(option.FullName); + ImGui.TextUnformatted(option.GetFullName()); } } diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs index f765b47e..94f1d577 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs @@ -3,7 +3,6 @@ using ImGuiNET; using OtterGui; using OtterGui.Classes; using OtterGui.Raii; -using Penumbra.Mods; using Penumbra.Mods.Editor; using Penumbra.Mods.Subclasses; using Penumbra.String.Classes; @@ -79,7 +78,7 @@ public partial class ModEditWindow var file = f.RelPath.ToString(); return f.SubModUsage.Count == 0 ? Enumerable.Repeat((file, "Unused", string.Empty, 0x40000080u), 1) - : f.SubModUsage.Select(s => (file, s.Item2.ToString(), s.Item1.FullName, + : f.SubModUsage.Select(s => (file, s.Item2.ToString(), s.Item1.GetFullName(), _editor.Option! == s.Item1 && Mod!.HasOptions ? 0x40008000u : 0u)); }); @@ -148,13 +147,13 @@ public partial class ModEditWindow (string, int) GetMulti() { var groups = registry.SubModUsage.GroupBy(s => s.Item1).ToArray(); - return (string.Join("\n", groups.Select(g => g.Key.Name)), groups.Length); + return (string.Join("\n", groups.Select(g => g.Key.GetName())), groups.Length); } var (text, groupCount) = color switch { ColorId.ConflictingMod => (string.Empty, 0), - ColorId.NewMod => (registry.SubModUsage[0].Item1.Name, 1), + ColorId.NewMod => (registry.SubModUsage[0].Item1.GetName(), 1), ColorId.InheritedMod => GetMulti(), _ => (string.Empty, 0), }; @@ -192,7 +191,7 @@ public partial class ModEditWindow ImGuiUtil.RightAlign(rightText); } - private void PrintGamePath(int i, int j, FileRegistry registry, SubMod subMod, Utf8GamePath gamePath) + private void PrintGamePath(int i, int j, FileRegistry registry, IModDataContainer subMod, Utf8GamePath gamePath) { using var id = ImRaii.PushId(j); ImGui.TableNextColumn(); @@ -228,7 +227,7 @@ public partial class ModEditWindow } } - private void PrintNewGamePath(int i, FileRegistry registry, SubMod subMod) + private void PrintNewGamePath(int i, FileRegistry registry, IModDataContainer subMod) { var tmp = _fileIdx == i && _pathIdx == -1 ? _gamePathEdit : string.Empty; var pos = ImGui.GetCursorPosX() - ImGui.GetFrameHeight(); @@ -301,9 +300,9 @@ public partial class ModEditWindow tt = changes ? "Apply the current file setup to the currently selected option." : "No changes made."; if (ImGuiUtil.DrawDisabledButton("Apply Changes", Vector2.Zero, tt, !changes)) { - var failedFiles = _editor.FileEditor.Apply(_editor.Mod!, (SubMod)_editor.Option!); + var failedFiles = _editor.FileEditor.Apply(_editor.Mod!, _editor.Option!); if (failedFiles > 0) - Penumbra.Log.Information($"Failed to apply {failedFiles} file redirections to {_editor.Option!.FullName}."); + Penumbra.Log.Information($"Failed to apply {failedFiles} file redirections to {_editor.Option!.GetFullName()}."); } diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs index aad70cb3..92a9dd66 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs @@ -45,7 +45,7 @@ public partial class ModEditWindow var tt = setsEqual ? "No changes staged." : "Apply the currently staged changes to the option."; ImGui.NewLine(); if (ImGuiUtil.DrawDisabledButton("Apply Changes", Vector2.Zero, tt, setsEqual)) - _editor.MetaEditor.Apply(_editor.Mod!, _editor.GroupIdx, _editor.OptionIdx); + _editor.MetaEditor.Apply(_editor.Mod!, _editor.GroupIdx, _editor.DataIdx); ImGui.SameLine(); tt = setsEqual ? "No changes staged." : "Revert all currently staged changes."; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs index cca8fe10..6b9965b8 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs @@ -85,7 +85,7 @@ public partial class ModEditWindow { // TODO: Is it worth trying to order results based on option priorities for cases where more than one match is found? // NOTE: We're using case-insensitive comparisons, as option group paths in mods are stored in lower case, but the mod editor uses paths directly from the file system, which may be mixed case. - return mod.AllSubMods + return mod.AllDataContainers .SelectMany(m => m.Files.Concat(m.FileSwaps)) .Where(kv => kv.Value.FullName.Equals(path, StringComparison.OrdinalIgnoreCase)) .Select(kv => kv.Key) @@ -103,7 +103,7 @@ public partial class ModEditWindow return []; // Filter then prepend the current option to ensure it's chosen first. - return mod.AllSubMods + return mod.AllDataContainers .Where(subMod => subMod != option) .Prepend(option) .SelectMany(subMod => subMod.Manipulations) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs index 4ecacece..70854fe7 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs @@ -187,8 +187,8 @@ public partial class ModEditWindow if (editor == null) return new QuickImportAction(owner._editor, FallbackOptionName, gamePath); - var subMod = editor.Option; - var optionName = subMod!.FullName; + var subMod = editor.Option!; + var optionName = subMod is IModOption o ? o.FullName : FallbackOptionName; if (gamePath.IsEmpty || file == null || editor.FileEditor.Changes) return new QuickImportAction(editor, optionName, gamePath); @@ -199,7 +199,7 @@ public partial class ModEditWindow if (mod == null) return new QuickImportAction(editor, optionName, gamePath); - var (preferredPath, subDirs) = GetPreferredPath(mod, subMod, owner._config.ReplaceNonAsciiOnImport); + var (preferredPath, subDirs) = GetPreferredPath(mod, subMod as IModOption, owner._config.ReplaceNonAsciiOnImport); var targetPath = new FullPath(Path.Combine(preferredPath.FullName, gamePath.ToString())).FullName; if (File.Exists(targetPath)) return new QuickImportAction(editor, optionName, gamePath); @@ -222,16 +222,16 @@ public partial class ModEditWindow { fileRegistry, }, _subDirs); - _editor.FileEditor.Apply(_editor.Mod!, (SubMod)_editor.Option!); + _editor.FileEditor.Apply(_editor.Mod!, _editor.Option!); return fileRegistry; } - private static (DirectoryInfo, int) GetPreferredPath(Mod mod, SubMod subMod, bool replaceNonAscii) + private static (DirectoryInfo, int) GetPreferredPath(Mod mod, IModOption? subMod, bool replaceNonAscii) { var path = mod.ModPath; var subDirs = 0; - if (subMod == mod.Default) + if (subMod == null) return (path, subDirs); var name = subMod.Name; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index 3f5f6c37..21d14f78 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -77,10 +77,10 @@ public partial class ModEditWindow : Window, IDisposable _forceTextureStartPath = true; } - public void ChangeOption(SubMod? subMod) + public void ChangeOption(IModDataContainer? subMod) { - var (groupIdx, optionIdx) = subMod?.GetIndices() ?? (-1, 0); - _editor.LoadOption(groupIdx, optionIdx); + var (groupIdx, dataIdx) = subMod?.GetDataIndices() ?? (-1, 0); + _editor.LoadOption(groupIdx, dataIdx); } public void UpdateModels() @@ -111,7 +111,7 @@ public partial class ModEditWindow : Window, IDisposable }); var manipulations = 0; var subMods = 0; - var swaps = Mod!.AllSubMods.Sum(m => + var swaps = Mod!.AllDataContainers.Sum(m => { ++subMods; manipulations += m.Manipulations.Count; @@ -330,7 +330,7 @@ public partial class ModEditWindow : Window, IDisposable else if (ImGuiUtil.DrawDisabledButton("Re-Duplicate and Normalize Mod", Vector2.Zero, tt, !_allowReduplicate && !modifier)) { _editor.ModNormalizer.Normalize(Mod!); - _editor.ModNormalizer.Worker.ContinueWith(_ => _editor.LoadMod(Mod!, _editor.GroupIdx, _editor.OptionIdx), TaskScheduler.Default); + _editor.ModNormalizer.Worker.ContinueWith(_ => _editor.LoadMod(Mod!, _editor.GroupIdx, _editor.DataIdx), TaskScheduler.Default); } if (!_editor.Duplicates.Worker.IsCompleted) @@ -405,7 +405,7 @@ public partial class ModEditWindow : Window, IDisposable var width = new Vector2(ImGui.GetContentRegionAvail().X / 3, 0); var ret = false; if (ImGuiUtil.DrawDisabledButton(defaultOption, width, "Switch to the default option for the mod.\nThis resets unsaved changes.", - _editor.Option!.IsDefault)) + _editor.Option is DefaultSubMod)) { _editor.LoadOption(-1, 0); ret = true; @@ -414,7 +414,7 @@ public partial class ModEditWindow : Window, IDisposable ImGui.SameLine(); if (ImGuiUtil.DrawDisabledButton("Refresh Data", width, "Refresh data for the current option.\nThis resets unsaved changes.", false)) { - _editor.LoadMod(_editor.Mod!, _editor.GroupIdx, _editor.OptionIdx); + _editor.LoadMod(_editor.Mod!, _editor.GroupIdx, _editor.DataIdx); ret = true; } @@ -422,17 +422,17 @@ public partial class ModEditWindow : Window, IDisposable ImGui.SetNextItemWidth(width.X); style.Push(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale); using var color = ImRaii.PushColor(ImGuiCol.Border, ColorId.FolderLine.Value()); - using var combo = ImRaii.Combo("##optionSelector", _editor.Option.FullName); + using var combo = ImRaii.Combo("##optionSelector", _editor.Option!.GetFullName()); if (!combo) return ret; - foreach (var (option, idx) in Mod!.AllSubMods.WithIndex()) + foreach (var (option, idx) in Mod!.AllDataContainers.WithIndex()) { using var id = ImRaii.PushId(idx); - if (ImGui.Selectable(option.FullName, option == _editor.Option)) + if (ImGui.Selectable(option.GetFullName(), option == _editor.Option)) { - var (groupIdx, optionIdx) = option.GetIndices(); - _editor.LoadOption(groupIdx, optionIdx); + var (groupIdx, dataIdx) = option.GetDataIndices(); + _editor.LoadOption(groupIdx, dataIdx); ret = true; } } @@ -455,7 +455,7 @@ public partial class ModEditWindow : Window, IDisposable var tt = setsEqual ? "No changes staged." : "Apply the currently staged changes to the option."; ImGui.NewLine(); if (ImGuiUtil.DrawDisabledButton("Apply Changes", Vector2.Zero, tt, setsEqual)) - _editor.SwapEditor.Apply(_editor.Mod!, _editor.GroupIdx, _editor.OptionIdx); + _editor.SwapEditor.Apply(_editor.Mod!, _editor.GroupIdx, _editor.DataIdx); ImGui.SameLine(); tt = setsEqual ? "No changes staged." : "Revert all currently staged changes."; @@ -569,7 +569,7 @@ public partial class ModEditWindow : Window, IDisposable } if (Mod != null) - foreach (var option in Mod.AllSubMods) + foreach (var option in Mod.AllDataContainers) { foreach (var path in option.Files.Keys) { diff --git a/Penumbra/UI/AdvancedWindow/ModMergeTab.cs b/Penumbra/UI/AdvancedWindow/ModMergeTab.cs index c34c7ef0..bed31ab8 100644 --- a/Penumbra/UI/AdvancedWindow/ModMergeTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModMergeTab.cs @@ -50,8 +50,7 @@ public class ModMergeTab(ModMerger modMerger) ImGui.SameLine(); DrawCombo(size - ImGui.GetItemRectSize().X - ImGui.GetStyle().ItemSpacing.X); - var width = ImGui.GetItemRectSize(); - using (var g = ImRaii.Group()) + using (ImRaii.Group()) { using var disabled = ImRaii.Disabled(modMerger.MergeFromMod.HasOptions); var buttonWidth = (size - ImGui.GetStyle().ItemSpacing.X) / 2; @@ -124,13 +123,13 @@ public class ModMergeTab(ModMerger modMerger) ImGui.Dummy(Vector2.One); var buttonSize = new Vector2((size - 2 * ImGui.GetStyle().ItemSpacing.X) / 3, 0); if (ImGui.Button("Select All", buttonSize)) - modMerger.SelectedOptions.UnionWith(modMerger.MergeFromMod!.AllSubMods); + modMerger.SelectedOptions.UnionWith(modMerger.MergeFromMod!.AllDataContainers); ImGui.SameLine(); if (ImGui.Button("Unselect All", buttonSize)) modMerger.SelectedOptions.Clear(); ImGui.SameLine(); if (ImGui.Button("Invert Selection", buttonSize)) - modMerger.SelectedOptions.SymmetricExceptWith(modMerger.MergeFromMod!.AllSubMods); + modMerger.SelectedOptions.SymmetricExceptWith(modMerger.MergeFromMod!.AllDataContainers); DrawOptionTable(size); } @@ -144,7 +143,7 @@ public class ModMergeTab(ModMerger modMerger) private void DrawOptionTable(float size) { - var options = modMerger.MergeFromMod!.AllSubMods.ToList(); + var options = modMerger.MergeFromMod!.AllDataContainers.ToList(); var height = modMerger.Warnings.Count == 0 && modMerger.Error == null ? ImGui.GetContentRegionAvail().Y - 3 * ImGui.GetFrameHeightWithSpacing() : 8 * ImGui.GetFrameHeightWithSpacing(); @@ -176,47 +175,41 @@ public class ModMergeTab(ModMerger modMerger) if (ImGui.Checkbox("##check", ref selected)) Handle(option, selected); - if (option.IsDefault) + if (option.Group is not { } group) { - ImGuiUtil.DrawTableColumn(option.FullName); + ImGuiUtil.DrawTableColumn(option.GetFullName()); ImGui.TableNextColumn(); } else { - ImGuiUtil.DrawTableColumn(option.Name); - var group = option.Group; - var optionEnumerator = group switch - { - SingleModGroup single => single.OptionData, - MultiModGroup multi => multi.PrioritizedOptions.Select(o => o.Mod), - _ => [], - }; + ImGuiUtil.DrawTableColumn(option.GetName()); + ImGui.TableNextColumn(); ImGui.Selectable(group.Name, false); if (ImGui.BeginPopupContextItem("##groupContext")) { if (ImGui.MenuItem("Select All")) // ReSharper disable once PossibleMultipleEnumeration - foreach (var opt in optionEnumerator) + foreach (var opt in group.DataContainers) Handle(opt, true); if (ImGui.MenuItem("Unselect All")) // ReSharper disable once PossibleMultipleEnumeration - foreach (var opt in optionEnumerator) + foreach (var opt in group.DataContainers) Handle(opt, false); ImGui.EndPopup(); } } ImGui.TableNextColumn(); - ImGuiUtil.RightAlign(option.FileData.Count.ToString(), 3 * ImGuiHelpers.GlobalScale); + ImGuiUtil.RightAlign(option.Files.Count.ToString(), 3 * ImGuiHelpers.GlobalScale); ImGui.TableNextColumn(); - ImGuiUtil.RightAlign(option.FileSwapData.Count.ToString(), 3 * ImGuiHelpers.GlobalScale); + ImGuiUtil.RightAlign(option.FileSwaps.Count.ToString(), 3 * ImGuiHelpers.GlobalScale); ImGui.TableNextColumn(); ImGuiUtil.RightAlign(option.Manipulations.Count.ToString(), 3 * ImGuiHelpers.GlobalScale); continue; - void Handle(SubMod option2, bool selected2) + void Handle(IModDataContainer option2, bool selected2) { if (selected2) modMerger.SelectedOptions.Add(option2); diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index 0dc694d8..c05f1ac1 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -486,7 +486,7 @@ public class ModPanelEditTab( EditOption(panel, single, groupIdx, optionIdx); break; case MultiModGroup multi: - for (var optionIdx = 0; optionIdx < multi.PrioritizedOptions.Count; ++optionIdx) + for (var optionIdx = 0; optionIdx < multi.OptionData.Count; ++optionIdx) EditOption(panel, multi, groupIdx, optionIdx); break; } @@ -542,7 +542,7 @@ public class ModPanelEditTab( if (group is not MultiModGroup multi) return; - if (Input.Priority("##Priority", groupIdx, optionIdx, multi.PrioritizedOptions[optionIdx].Priority, out var priority, + if (Input.Priority("##Priority", groupIdx, optionIdx, multi.OptionData[optionIdx].Priority, out var priority, 50 * UiHelpers.Scale)) panel._modManager.OptionEditor.ChangeOptionPriority(panel._mod, groupIdx, optionIdx, priority); @@ -557,7 +557,7 @@ public class ModPanelEditTab( var count = group switch { SingleModGroup single => single.OptionData.Count, - MultiModGroup multi => multi.PrioritizedOptions.Count, + MultiModGroup multi => multi.OptionData.Count, _ => throw new Exception($"Dragging options to an option group of type {group.GetType()} is not supported."), }; ImGui.TableNextColumn(); @@ -591,6 +591,9 @@ public class ModPanelEditTab( // Handle drag and drop to move options inside a group or into another group. private static void Source(IModGroup group, int groupIdx, int optionIdx) { + if (group is not ITexToolsGroup) + return; + using var source = ImRaii.DragDropSource(); if (!source) return; @@ -606,6 +609,9 @@ public class ModPanelEditTab( private static void Target(ModPanelEditTab panel, IModGroup group, int groupIdx, int optionIdx) { + if (group is not ITexToolsGroup) + return; + using var target = ImRaii.DragDropTarget(); if (!target.Success || !ImGuiUtil.IsDropping(DragDropLabel)) return; @@ -624,22 +630,12 @@ public class ModPanelEditTab( var sourceGroupIdx = _dragDropGroupIdx; var sourceOption = _dragDropOptionIdx; var sourceGroup = panel._mod.Groups[sourceGroupIdx]; - var currentCount = group switch - { - SingleModGroup single => single.OptionData.Count, - MultiModGroup multi => multi.PrioritizedOptions.Count, - _ => throw new Exception($"Dragging options to an option group of type {group.GetType()} is not supported."), - }; - var (option, priority) = sourceGroup switch - { - SingleModGroup single => (single.OptionData[_dragDropOptionIdx], ModPriority.Default), - MultiModGroup multi => multi.PrioritizedOptions[_dragDropOptionIdx], - _ => throw new Exception($"Dragging options from an option group of type {sourceGroup.GetType()} is not supported."), - }; + var currentCount = group.DataContainers.Count; + var option = ((ITexToolsGroup) sourceGroup).OptionData[_dragDropOptionIdx]; panel._delayedActions.Enqueue(() => { panel._modManager.OptionEditor.DeleteOption(panel._mod, sourceGroupIdx, sourceOption); - panel._modManager.OptionEditor.AddOption(panel._mod, groupIdx, option, priority); + panel._modManager.OptionEditor.AddOption(panel._mod, groupIdx, option); panel._modManager.OptionEditor.MoveOption(panel._mod, groupIdx, currentCount, optionIdx); }); } diff --git a/Penumbra/UI/ModsTab/ModPanelTabBar.cs b/Penumbra/UI/ModsTab/ModPanelTabBar.cs index 02ec9a32..1aaa7741 100644 --- a/Penumbra/UI/ModsTab/ModPanelTabBar.cs +++ b/Penumbra/UI/ModsTab/ModPanelTabBar.cs @@ -114,7 +114,7 @@ public class ModPanelTabBar if (ImGui.TabItemButton("Advanced Editing", ImGuiTabItemFlags.Trailing | ImGuiTabItemFlags.NoTooltip)) { _modEditWindow.ChangeMod(mod); - _modEditWindow.ChangeOption((SubMod)mod.Default); + _modEditWindow.ChangeOption(mod.Default); _modEditWindow.IsOpen = true; } From 514121d8c133baf711c19a9ca1dfa585f6043f6d Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 24 Apr 2024 23:28:12 +0200 Subject: [PATCH 130/433] Reorder stuff. --- Penumbra/Api/Api/ModSettingsApi.cs | 3 +- Penumbra/Api/Api/TemporaryApi.cs | 2 +- Penumbra/Api/TempModManager.cs | 2 +- .../Cache/CollectionCacheManager.cs | 2 +- .../Collections/Manager/CollectionEditor.cs | 2 +- .../Collections/Manager/CollectionStorage.cs | 2 +- .../Manager/ModCollectionMigration.cs | 2 +- Penumbra/Collections/ModCollection.cs | 2 +- Penumbra/Collections/ModCollectionSave.cs | 2 +- Penumbra/Communication/ModSettingChanged.cs | 2 +- Penumbra/Import/TexToolsImporter.ModPack.cs | 4 +- Penumbra/Meta/MetaFileManager.cs | 2 +- Penumbra/Mods/Editor/DuplicateManager.cs | 3 +- Penumbra/Mods/Editor/FileRegistry.cs | 2 +- Penumbra/Mods/Editor/IMod.cs | 3 +- Penumbra/Mods/Editor/ModEditor.cs | 3 +- Penumbra/Mods/Editor/ModFileCollection.cs | 2 +- Penumbra/Mods/Editor/ModFileEditor.cs | 2 +- Penumbra/Mods/Editor/ModMerger.cs | 2 +- Penumbra/Mods/Editor/ModMetaEditor.cs | 2 +- Penumbra/Mods/Editor/ModNormalizer.cs | 3 +- Penumbra/Mods/Editor/ModSwapEditor.cs | 2 +- .../Mods/{Subclasses => Groups}/IModGroup.cs | 86 +---- Penumbra/Mods/Groups/ModSaveGroup.cs | 57 +++ .../{Subclasses => Groups}/MultiModGroup.cs | 30 +- .../{Subclasses => Groups}/SingleModGroup.cs | 28 +- Penumbra/Mods/ItemSwap/ItemSwapContainer.cs | 2 +- Penumbra/Mods/Manager/ModCacheManager.cs | 1 - Penumbra/Mods/Manager/ModMigration.cs | 4 +- Penumbra/Mods/Manager/ModOptionEditor.cs | 4 +- Penumbra/Mods/Mod.cs | 5 +- Penumbra/Mods/ModCreator.cs | 4 +- Penumbra/Mods/ModLocalData.cs | 20 +- Penumbra/Mods/ModMeta.cs | 24 +- .../{Subclasses => Settings}/ModPriority.cs | 2 +- .../{Subclasses => Settings}/ModSettings.cs | 5 +- .../Mods/{Subclasses => Settings}/Setting.cs | 2 +- .../{Subclasses => Settings}/SettingList.cs | 2 +- Penumbra/Mods/SubMods/DefaultSubMod.cs | 30 ++ .../IModDataContainer.cs | 27 +- .../{Subclasses => SubMods}/IModOption.cs | 8 +- Penumbra/Mods/SubMods/MultiSubMod.cs | 92 +++++ Penumbra/Mods/SubMods/SingleSubMod.cs | 82 ++++ Penumbra/Mods/Subclasses/SubMod.cs | 352 ------------------ Penumbra/Mods/TemporaryMod.cs | 6 +- Penumbra/Penumbra.csproj | 4 + Penumbra/Services/ConfigMigrationService.cs | 2 +- Penumbra/Services/SaveService.cs | 2 +- Penumbra/UI/AdvancedWindow/ItemSwapTab.cs | 3 +- .../UI/AdvancedWindow/ModEditWindow.Files.cs | 2 +- .../ModEditWindow.QuickImport.cs | 2 +- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 2 +- Penumbra/UI/AdvancedWindow/ModMergeTab.cs | 2 +- Penumbra/UI/ModsTab/ModFileSystemSelector.cs | 2 +- Penumbra/UI/ModsTab/ModPanelConflictsTab.cs | 2 +- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 3 +- Penumbra/UI/ModsTab/ModPanelSettingsTab.cs | 4 +- Penumbra/UI/ModsTab/ModPanelTabBar.cs | 1 - 58 files changed, 416 insertions(+), 539 deletions(-) rename Penumbra/Mods/{Subclasses => Groups}/IModGroup.cs (50%) create mode 100644 Penumbra/Mods/Groups/ModSaveGroup.cs rename Penumbra/Mods/{Subclasses => Groups}/MultiModGroup.cs (83%) rename Penumbra/Mods/{Subclasses => Groups}/SingleModGroup.cs (85%) rename Penumbra/Mods/{Subclasses => Settings}/ModPriority.cs (98%) rename Penumbra/Mods/{Subclasses => Settings}/ModSettings.cs (98%) rename Penumbra/Mods/{Subclasses => Settings}/Setting.cs (98%) rename Penumbra/Mods/{Subclasses => Settings}/SettingList.cs (97%) create mode 100644 Penumbra/Mods/SubMods/DefaultSubMod.cs rename Penumbra/Mods/{Subclasses => SubMods}/IModDataContainer.cs (80%) rename Penumbra/Mods/{Subclasses => SubMods}/IModOption.cs (72%) create mode 100644 Penumbra/Mods/SubMods/MultiSubMod.cs create mode 100644 Penumbra/Mods/SubMods/SingleSubMod.cs delete mode 100644 Penumbra/Mods/Subclasses/SubMod.cs diff --git a/Penumbra/Api/Api/ModSettingsApi.cs b/Penumbra/Api/Api/ModSettingsApi.cs index e145e027..039fbfa9 100644 --- a/Penumbra/Api/Api/ModSettingsApi.cs +++ b/Penumbra/Api/Api/ModSettingsApi.cs @@ -8,8 +8,9 @@ using Penumbra.Communication; using Penumbra.Interop.PathResolving; using Penumbra.Mods; using Penumbra.Mods.Editor; +using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; using Penumbra.Services; namespace Penumbra.Api.Api; diff --git a/Penumbra/Api/Api/TemporaryApi.cs b/Penumbra/Api/Api/TemporaryApi.cs index b4ffa8f4..38d080cc 100644 --- a/Penumbra/Api/Api/TemporaryApi.cs +++ b/Penumbra/Api/Api/TemporaryApi.cs @@ -5,7 +5,7 @@ using Penumbra.Collections.Manager; using Penumbra.GameData.Actors; using Penumbra.GameData.Interop; using Penumbra.Meta.Manipulations; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; using Penumbra.String.Classes; namespace Penumbra.Api.Api; diff --git a/Penumbra/Api/TempModManager.cs b/Penumbra/Api/TempModManager.cs index 7d682338..aee2b447 100644 --- a/Penumbra/Api/TempModManager.cs +++ b/Penumbra/Api/TempModManager.cs @@ -6,7 +6,7 @@ using Penumbra.Services; using Penumbra.String.Classes; using Penumbra.Collections.Manager; using Penumbra.Communication; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; namespace Penumbra.Api; diff --git a/Penumbra/Collections/Cache/CollectionCacheManager.cs b/Penumbra/Collections/Cache/CollectionCacheManager.cs index 4b5c4337..c1296414 100644 --- a/Penumbra/Collections/Cache/CollectionCacheManager.cs +++ b/Penumbra/Collections/Cache/CollectionCacheManager.cs @@ -8,7 +8,7 @@ using Penumbra.Interop.ResourceLoading; using Penumbra.Meta; using Penumbra.Mods; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; using Penumbra.Services; using Penumbra.String.Classes; diff --git a/Penumbra/Collections/Manager/CollectionEditor.cs b/Penumbra/Collections/Manager/CollectionEditor.cs index 4af19e6b..0243de1e 100644 --- a/Penumbra/Collections/Manager/CollectionEditor.cs +++ b/Penumbra/Collections/Manager/CollectionEditor.cs @@ -2,7 +2,7 @@ using OtterGui; using Penumbra.Api.Enums; using Penumbra.Mods; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; using Penumbra.Services; namespace Penumbra.Collections.Manager; diff --git a/Penumbra/Collections/Manager/CollectionStorage.cs b/Penumbra/Collections/Manager/CollectionStorage.cs index 2da2a569..4e2fb7b7 100644 --- a/Penumbra/Collections/Manager/CollectionStorage.cs +++ b/Penumbra/Collections/Manager/CollectionStorage.cs @@ -5,7 +5,7 @@ using Penumbra.Communication; using Penumbra.Mods; using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; using Penumbra.Services; namespace Penumbra.Collections.Manager; diff --git a/Penumbra/Collections/Manager/ModCollectionMigration.cs b/Penumbra/Collections/Manager/ModCollectionMigration.cs index 053f0a2b..89743aa2 100644 --- a/Penumbra/Collections/Manager/ModCollectionMigration.cs +++ b/Penumbra/Collections/Manager/ModCollectionMigration.cs @@ -1,6 +1,6 @@ using Penumbra.Mods; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; using Penumbra.Services; using Penumbra.Util; diff --git a/Penumbra/Collections/ModCollection.cs b/Penumbra/Collections/ModCollection.cs index e666b151..4580e37a 100644 --- a/Penumbra/Collections/ModCollection.cs +++ b/Penumbra/Collections/ModCollection.cs @@ -1,7 +1,7 @@ using Penumbra.Mods; using Penumbra.Mods.Manager; using Penumbra.Collections.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; using Penumbra.Services; namespace Penumbra.Collections; diff --git a/Penumbra/Collections/ModCollectionSave.cs b/Penumbra/Collections/ModCollectionSave.cs index acc38d83..e6bb069b 100644 --- a/Penumbra/Collections/ModCollectionSave.cs +++ b/Penumbra/Collections/ModCollectionSave.cs @@ -2,7 +2,7 @@ using Newtonsoft.Json.Linq; using Penumbra.Services; using Newtonsoft.Json; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; namespace Penumbra.Collections; diff --git a/Penumbra/Communication/ModSettingChanged.cs b/Penumbra/Communication/ModSettingChanged.cs index 968f78a7..a7da345b 100644 --- a/Penumbra/Communication/ModSettingChanged.cs +++ b/Penumbra/Communication/ModSettingChanged.cs @@ -4,7 +4,7 @@ using Penumbra.Api.Api; using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.Mods; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; namespace Penumbra.Communication; diff --git a/Penumbra/Import/TexToolsImporter.ModPack.cs b/Penumbra/Import/TexToolsImporter.ModPack.cs index b9cdda71..eb6d0b0c 100644 --- a/Penumbra/Import/TexToolsImporter.ModPack.cs +++ b/Penumbra/Import/TexToolsImporter.ModPack.cs @@ -3,7 +3,9 @@ using OtterGui; using Penumbra.Api.Enums; using Penumbra.Import.Structs; using Penumbra.Mods; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; using Penumbra.Util; using ZipArchive = SharpCompress.Archives.Zip.ZipArchive; diff --git a/Penumbra/Meta/MetaFileManager.cs b/Penumbra/Meta/MetaFileManager.cs index 0e2e638b..cd99396b 100644 --- a/Penumbra/Meta/MetaFileManager.cs +++ b/Penumbra/Meta/MetaFileManager.cs @@ -11,7 +11,7 @@ using Penumbra.Interop.Services; using Penumbra.Interop.Structs; using Penumbra.Meta.Files; using Penumbra.Mods; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Groups; using Penumbra.Services; using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManager; diff --git a/Penumbra/Mods/Editor/DuplicateManager.cs b/Penumbra/Mods/Editor/DuplicateManager.cs index 92ec58b9..31aacbe1 100644 --- a/Penumbra/Mods/Editor/DuplicateManager.cs +++ b/Penumbra/Mods/Editor/DuplicateManager.cs @@ -1,6 +1,7 @@ using OtterGui.Classes; +using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.String.Classes; diff --git a/Penumbra/Mods/Editor/FileRegistry.cs b/Penumbra/Mods/Editor/FileRegistry.cs index 44d349ce..a484c8c2 100644 --- a/Penumbra/Mods/Editor/FileRegistry.cs +++ b/Penumbra/Mods/Editor/FileRegistry.cs @@ -1,4 +1,4 @@ -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.SubMods; using Penumbra.String.Classes; namespace Penumbra.Mods.Editor; diff --git a/Penumbra/Mods/Editor/IMod.cs b/Penumbra/Mods/Editor/IMod.cs index c4c4be2f..d4c881e9 100644 --- a/Penumbra/Mods/Editor/IMod.cs +++ b/Penumbra/Mods/Editor/IMod.cs @@ -1,6 +1,7 @@ using OtterGui.Classes; using Penumbra.Meta.Manipulations; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Settings; using Penumbra.String.Classes; namespace Penumbra.Mods.Editor; diff --git a/Penumbra/Mods/Editor/ModEditor.cs b/Penumbra/Mods/Editor/ModEditor.cs index 1118f890..e1c5962f 100644 --- a/Penumbra/Mods/Editor/ModEditor.cs +++ b/Penumbra/Mods/Editor/ModEditor.cs @@ -1,6 +1,7 @@ using OtterGui; using OtterGui.Compression; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Groups; +using Penumbra.Mods.SubMods; namespace Penumbra.Mods.Editor; diff --git a/Penumbra/Mods/Editor/ModFileCollection.cs b/Penumbra/Mods/Editor/ModFileCollection.cs index ede35914..551d04cf 100644 --- a/Penumbra/Mods/Editor/ModFileCollection.cs +++ b/Penumbra/Mods/Editor/ModFileCollection.cs @@ -1,5 +1,5 @@ using OtterGui; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.SubMods; using Penumbra.String.Classes; namespace Penumbra.Mods.Editor; diff --git a/Penumbra/Mods/Editor/ModFileEditor.cs b/Penumbra/Mods/Editor/ModFileEditor.cs index 11e35334..00685c94 100644 --- a/Penumbra/Mods/Editor/ModFileEditor.cs +++ b/Penumbra/Mods/Editor/ModFileEditor.cs @@ -1,5 +1,5 @@ using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.String.Classes; diff --git a/Penumbra/Mods/Editor/ModMerger.cs b/Penumbra/Mods/Editor/ModMerger.cs index 541c84ae..0f629bc7 100644 --- a/Penumbra/Mods/Editor/ModMerger.cs +++ b/Penumbra/Mods/Editor/ModMerger.cs @@ -5,7 +5,7 @@ using OtterGui.Classes; using Penumbra.Api.Enums; using Penumbra.Communication; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.String.Classes; using Penumbra.UI.ModsTab; diff --git a/Penumbra/Mods/Editor/ModMetaEditor.cs b/Penumbra/Mods/Editor/ModMetaEditor.cs index 88e48f0f..dee700d5 100644 --- a/Penumbra/Mods/Editor/ModMetaEditor.cs +++ b/Penumbra/Mods/Editor/ModMetaEditor.cs @@ -1,6 +1,6 @@ using Penumbra.Meta.Manipulations; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.SubMods; namespace Penumbra.Mods.Editor; diff --git a/Penumbra/Mods/Editor/ModNormalizer.cs b/Penumbra/Mods/Editor/ModNormalizer.cs index db00a1c7..e2088b32 100644 --- a/Penumbra/Mods/Editor/ModNormalizer.cs +++ b/Penumbra/Mods/Editor/ModNormalizer.cs @@ -3,8 +3,9 @@ using Dalamud.Interface.Internal.Notifications; using OtterGui; using OtterGui.Classes; using OtterGui.Tasks; +using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.SubMods; using Penumbra.String.Classes; namespace Penumbra.Mods.Editor; diff --git a/Penumbra/Mods/Editor/ModSwapEditor.cs b/Penumbra/Mods/Editor/ModSwapEditor.cs index 64788cf3..3247cfdf 100644 --- a/Penumbra/Mods/Editor/ModSwapEditor.cs +++ b/Penumbra/Mods/Editor/ModSwapEditor.cs @@ -1,6 +1,6 @@ using Penumbra.Mods; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.SubMods; using Penumbra.String.Classes; using Penumbra.Util; diff --git a/Penumbra/Mods/Subclasses/IModGroup.cs b/Penumbra/Mods/Groups/IModGroup.cs similarity index 50% rename from Penumbra/Mods/Subclasses/IModGroup.cs rename to Penumbra/Mods/Groups/IModGroup.cs index 5c500793..dc5150cf 100644 --- a/Penumbra/Mods/Subclasses/IModGroup.cs +++ b/Penumbra/Mods/Groups/IModGroup.cs @@ -1,11 +1,11 @@ -using FFXIVClientStructs.FFXIV.Client.UI.Agent; using Newtonsoft.Json; using Penumbra.Api.Enums; using Penumbra.Meta.Manipulations; -using Penumbra.Services; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; using Penumbra.String.Classes; -namespace Penumbra.Mods.Subclasses; +namespace Penumbra.Mods.Groups; public interface ITexToolsGroup { @@ -16,22 +16,22 @@ public interface IModGroup { public const int MaxMultiOptions = 63; - public Mod Mod { get; } - public string Name { get; } - public string Description { get; } - public GroupType Type { get; } - public ModPriority Priority { get; set; } - public Setting DefaultSettings { get; set; } + public Mod Mod { get; } + public string Name { get; } + public string Description { get; } + public GroupType Type { get; } + public ModPriority Priority { get; set; } + public Setting DefaultSettings { get; set; } public FullPath? FindBestMatch(Utf8GamePath gamePath); - public int AddOption(Mod mod, string name, string description = ""); + public int AddOption(Mod mod, string name, string description = ""); - public IReadOnlyList Options { get; } + public IReadOnlyList Options { get; } public IReadOnlyList DataContainers { get; } - public bool IsOption { get; } + public bool IsOption { get; } public IModGroup Convert(GroupType type); - public bool MoveOption(int optionIdxFrom, int optionIdxTo); + public bool MoveOption(int optionIdxFrom, int optionIdxTo); public int GetIndex(); @@ -88,67 +88,15 @@ public interface IModGroup public static (int Redirections, int Swaps, int Manips) GetCountsBase(IModGroup group) { var redirectionCount = 0; - var swapCount = 0; - var manipCount = 0; + var swapCount = 0; + var manipCount = 0; foreach (var option in group.DataContainers) { redirectionCount += option.Files.Count; - swapCount += option.FileSwaps.Count; - manipCount += option.Manipulations.Count; + swapCount += option.FileSwaps.Count; + manipCount += option.Manipulations.Count; } return (redirectionCount, swapCount, manipCount); } } - -public readonly struct ModSaveGroup : ISavable -{ - private readonly DirectoryInfo _basePath; - private readonly IModGroup? _group; - private readonly int _groupIdx; - private readonly DefaultSubMod? _defaultMod; - private readonly bool _onlyAscii; - - public ModSaveGroup(Mod mod, int groupIdx, bool onlyAscii) - { - _basePath = mod.ModPath; - _groupIdx = groupIdx; - if (_groupIdx < 0) - _defaultMod = mod.Default; - else - _group = mod.Groups[_groupIdx]; - _onlyAscii = onlyAscii; - } - - public ModSaveGroup(DirectoryInfo basePath, IModGroup group, int groupIdx, bool onlyAscii) - { - _basePath = basePath; - _group = group; - _groupIdx = groupIdx; - _onlyAscii = onlyAscii; - } - - public ModSaveGroup(DirectoryInfo basePath, DefaultSubMod @default, bool onlyAscii) - { - _basePath = basePath; - _groupIdx = -1; - _defaultMod = @default; - _onlyAscii = onlyAscii; - } - - public string ToFilename(FilenameService fileNames) - => fileNames.OptionGroupFile(_basePath.FullName, _groupIdx, _group?.Name ?? string.Empty, _onlyAscii); - - public void Save(StreamWriter writer) - { - using var j = new JsonTextWriter(writer); - j.Formatting = Formatting.Indented; - var serializer = new JsonSerializer { Formatting = Formatting.Indented }; - j.WriteStartObject(); - if (_groupIdx >= 0) - _group!.WriteJson(j, serializer); - else - IModDataContainer.WriteModData(j, serializer, _defaultMod!, _basePath); - j.WriteEndObject(); - } -} diff --git a/Penumbra/Mods/Groups/ModSaveGroup.cs b/Penumbra/Mods/Groups/ModSaveGroup.cs new file mode 100644 index 00000000..ed81f42f --- /dev/null +++ b/Penumbra/Mods/Groups/ModSaveGroup.cs @@ -0,0 +1,57 @@ +using Newtonsoft.Json; +using Penumbra.Mods.SubMods; +using Penumbra.Services; + +namespace Penumbra.Mods.Groups; + +public readonly struct ModSaveGroup : ISavable +{ + private readonly DirectoryInfo _basePath; + private readonly IModGroup? _group; + private readonly int _groupIdx; + private readonly DefaultSubMod? _defaultMod; + private readonly bool _onlyAscii; + + public ModSaveGroup(Mod mod, int groupIdx, bool onlyAscii) + { + _basePath = mod.ModPath; + _groupIdx = groupIdx; + if (_groupIdx < 0) + _defaultMod = mod.Default; + else + _group = mod.Groups[_groupIdx]; + _onlyAscii = onlyAscii; + } + + public ModSaveGroup(DirectoryInfo basePath, IModGroup group, int groupIdx, bool onlyAscii) + { + _basePath = basePath; + _group = group; + _groupIdx = groupIdx; + _onlyAscii = onlyAscii; + } + + public ModSaveGroup(DirectoryInfo basePath, DefaultSubMod @default, bool onlyAscii) + { + _basePath = basePath; + _groupIdx = -1; + _defaultMod = @default; + _onlyAscii = onlyAscii; + } + + public string ToFilename(FilenameService fileNames) + => fileNames.OptionGroupFile(_basePath.FullName, _groupIdx, _group?.Name ?? string.Empty, _onlyAscii); + + public void Save(StreamWriter writer) + { + using var j = new JsonTextWriter(writer); + j.Formatting = Formatting.Indented; + var serializer = new JsonSerializer { Formatting = Formatting.Indented }; + j.WriteStartObject(); + if (_groupIdx >= 0) + _group!.WriteJson(j, serializer); + else + IModDataContainer.WriteModData(j, serializer, _defaultMod!, _basePath); + j.WriteEndObject(); + } +} diff --git a/Penumbra/Mods/Subclasses/MultiModGroup.cs b/Penumbra/Mods/Groups/MultiModGroup.cs similarity index 83% rename from Penumbra/Mods/Subclasses/MultiModGroup.cs rename to Penumbra/Mods/Groups/MultiModGroup.cs index f194350a..f39f2e70 100644 --- a/Penumbra/Mods/Subclasses/MultiModGroup.cs +++ b/Penumbra/Mods/Groups/MultiModGroup.cs @@ -6,9 +6,11 @@ using OtterGui.Classes; using OtterGui.Filesystem; using Penumbra.Api.Enums; using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; using Penumbra.String.Classes; -namespace Penumbra.Mods.Subclasses; +namespace Penumbra.Mods.Groups; /// Groups that allow all available options to be selected at once. public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup @@ -16,11 +18,11 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup public GroupType Type => GroupType.Multi; - public Mod Mod { get; set; } = mod; - public string Name { get; set; } = "Group"; - public string Description { get; set; } = "A non-exclusive group of settings."; - public ModPriority Priority { get; set; } - public Setting DefaultSettings { get; set; } + public Mod Mod { get; set; } = mod; + public string Name { get; set; } = "Group"; + public string Description { get; set; } = "A non-exclusive group of settings."; + public ModPriority Priority { get; set; } + public Setting DefaultSettings { get; set; } public readonly List OptionData = []; public IReadOnlyList Options @@ -45,7 +47,7 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup var subMod = new MultiSubMod(mod, this) { - Name = name, + Name = name, Description = description, }; OptionData.Add(subMod); @@ -56,9 +58,9 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup { var ret = new MultiModGroup(mod) { - Name = json[nameof(Name)]?.ToObject() ?? string.Empty, - Description = json[nameof(Description)]?.ToObject() ?? string.Empty, - Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, + Name = json[nameof(Name)]?.ToObject() ?? string.Empty, + Description = json[nameof(Description)]?.ToObject() ?? string.Empty, + Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, DefaultSettings = json[nameof(DefaultSettings)]?.ToObject() ?? Setting.Zero, }; if (ret.Name.Length == 0) @@ -93,9 +95,9 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup case GroupType.Single: var single = new SingleModGroup(Mod) { - Name = Name, - Description = Description, - Priority = Priority, + Name = Name, + Description = Description, + Priority = Priority, DefaultSettings = DefaultSettings.TurnMulti(OptionData.Count), }; single.OptionData.AddRange(OptionData.Select(o => o.ConvertToSingle(Mod, single))); @@ -152,7 +154,7 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup => IModGroup.GetCountsBase(this); public Setting FixSetting(Setting setting) - => new(setting.Value & ((1ul << OptionData.Count) - 1)); + => new(setting.Value & (1ul << OptionData.Count) - 1); /// Create a group without a mod only for saving it in the creator. internal static MultiModGroup CreateForSaving(string name) diff --git a/Penumbra/Mods/Subclasses/SingleModGroup.cs b/Penumbra/Mods/Groups/SingleModGroup.cs similarity index 85% rename from Penumbra/Mods/Subclasses/SingleModGroup.cs rename to Penumbra/Mods/Groups/SingleModGroup.cs index d1a3b6d1..6011185a 100644 --- a/Penumbra/Mods/Subclasses/SingleModGroup.cs +++ b/Penumbra/Mods/Groups/SingleModGroup.cs @@ -4,9 +4,11 @@ using OtterGui; using OtterGui.Filesystem; using Penumbra.Api.Enums; using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; using Penumbra.String.Classes; -namespace Penumbra.Mods.Subclasses; +namespace Penumbra.Mods.Groups; /// Groups that allow only one of their available options to be selected. public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup @@ -14,11 +16,11 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup public GroupType Type => GroupType.Single; - public Mod Mod { get; set; } = mod; - public string Name { get; set; } = "Option"; - public string Description { get; set; } = "A mutually exclusive group of settings."; - public ModPriority Priority { get; set; } - public Setting DefaultSettings { get; set; } + public Mod Mod { get; set; } = mod; + public string Name { get; set; } = "Option"; + public string Description { get; set; } = "A mutually exclusive group of settings."; + public ModPriority Priority { get; set; } + public Setting DefaultSettings { get; set; } public readonly List OptionData = []; @@ -34,7 +36,7 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup { var subMod = new SingleSubMod(mod, this) { - Name = name, + Name = name, Description = description, }; OptionData.Add(subMod); @@ -55,9 +57,9 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup var options = json["Options"]; var ret = new SingleModGroup(mod) { - Name = json[nameof(Name)]?.ToObject() ?? string.Empty, - Description = json[nameof(Description)]?.ToObject() ?? string.Empty, - Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, + Name = json[nameof(Name)]?.ToObject() ?? string.Empty, + Description = json[nameof(Description)]?.ToObject() ?? string.Empty, + Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, DefaultSettings = json[nameof(DefaultSettings)]?.ToObject() ?? Setting.Zero, }; if (ret.Name.Length == 0) @@ -82,9 +84,9 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup case GroupType.Multi: var multi = new MultiModGroup(Mod) { - Name = Name, - Description = Description, - Priority = Priority, + Name = Name, + Description = Description, + Priority = Priority, DefaultSettings = Setting.Multi((int)DefaultSettings.Value), }; multi.OptionData.AddRange(OptionData.Select((o, i) => o.ConvertToMulti(Mod, multi, new ModPriority(i)))); diff --git a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs index 21b9ef2c..1545811e 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs @@ -7,7 +7,7 @@ using Penumbra.String.Classes; using Penumbra.Meta; using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; namespace Penumbra.Mods.ItemSwap; diff --git a/Penumbra/Mods/Manager/ModCacheManager.cs b/Penumbra/Mods/Manager/ModCacheManager.cs index 4f9e8648..3ff1a333 100644 --- a/Penumbra/Mods/Manager/ModCacheManager.cs +++ b/Penumbra/Mods/Manager/ModCacheManager.cs @@ -2,7 +2,6 @@ using Penumbra.Communication; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.Meta.Manipulations; -using Penumbra.Mods.Subclasses; using Penumbra.Services; namespace Penumbra.Mods.Manager; diff --git a/Penumbra/Mods/Manager/ModMigration.cs b/Penumbra/Mods/Manager/ModMigration.cs index 8c4a5674..f160d5bd 100644 --- a/Penumbra/Mods/Manager/ModMigration.cs +++ b/Penumbra/Mods/Manager/ModMigration.cs @@ -2,7 +2,9 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui; using Penumbra.Api.Enums; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.String.Classes; diff --git a/Penumbra/Mods/Manager/ModOptionEditor.cs b/Penumbra/Mods/Manager/ModOptionEditor.cs index 4d3a5717..b66fec17 100644 --- a/Penumbra/Mods/Manager/ModOptionEditor.cs +++ b/Penumbra/Mods/Manager/ModOptionEditor.cs @@ -4,7 +4,9 @@ using OtterGui.Classes; using OtterGui.Filesystem; using Penumbra.Api.Enums; using Penumbra.Meta.Manipulations; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.String.Classes; using Penumbra.Util; diff --git a/Penumbra/Mods/Mod.cs b/Penumbra/Mods/Mod.cs index 5c02213e..fc84afcc 100644 --- a/Penumbra/Mods/Mod.cs +++ b/Penumbra/Mods/Mod.cs @@ -1,9 +1,10 @@ using OtterGui; using OtterGui.Classes; -using Penumbra.Collections.Cache; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Editor; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; using Penumbra.String.Classes; namespace Penumbra.Mods; diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index c1236037..e8113ee1 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -9,8 +9,10 @@ using Penumbra.GameData.Data; using Penumbra.Import; using Penumbra.Import.Structs; using Penumbra.Meta; +using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.String.Classes; diff --git a/Penumbra/Mods/ModLocalData.cs b/Penumbra/Mods/ModLocalData.cs index 51fe8d58..beda0dc7 100644 --- a/Penumbra/Mods/ModLocalData.cs +++ b/Penumbra/Mods/ModLocalData.cs @@ -5,29 +5,25 @@ using Penumbra.Services; namespace Penumbra.Mods; -public readonly struct ModLocalData : ISavable +public readonly struct ModLocalData(Mod mod) : ISavable { public const int FileVersion = 3; - private readonly Mod _mod; - - public ModLocalData(Mod mod) - => _mod = mod; - public string ToFilename(FilenameService fileNames) - => fileNames.LocalDataFile(_mod); + => fileNames.LocalDataFile(mod); public void Save(StreamWriter writer) { var jObject = new JObject { { nameof(FileVersion), JToken.FromObject(FileVersion) }, - { nameof(Mod.ImportDate), JToken.FromObject(_mod.ImportDate) }, - { nameof(Mod.LocalTags), JToken.FromObject(_mod.LocalTags) }, - { nameof(Mod.Note), JToken.FromObject(_mod.Note) }, - { nameof(Mod.Favorite), JToken.FromObject(_mod.Favorite) }, + { nameof(Mod.ImportDate), JToken.FromObject(mod.ImportDate) }, + { nameof(Mod.LocalTags), JToken.FromObject(mod.LocalTags) }, + { nameof(Mod.Note), JToken.FromObject(mod.Note) }, + { nameof(Mod.Favorite), JToken.FromObject(mod.Favorite) }, }; - using var jWriter = new JsonTextWriter(writer) { Formatting = Formatting.Indented }; + using var jWriter = new JsonTextWriter(writer); + jWriter.Formatting = Formatting.Indented; jObject.WriteTo(jWriter); } diff --git a/Penumbra/Mods/ModMeta.cs b/Penumbra/Mods/ModMeta.cs index d29cdb9c..870d6d4f 100644 --- a/Penumbra/Mods/ModMeta.cs +++ b/Penumbra/Mods/ModMeta.cs @@ -4,31 +4,27 @@ using Penumbra.Services; namespace Penumbra.Mods; -public readonly struct ModMeta : ISavable +public readonly struct ModMeta(Mod mod) : ISavable { public const uint FileVersion = 3; - private readonly Mod _mod; - - public ModMeta(Mod mod) - => _mod = mod; - public string ToFilename(FilenameService fileNames) - => fileNames.ModMetaPath(_mod); + => fileNames.ModMetaPath(mod); public void Save(StreamWriter writer) { var jObject = new JObject { { nameof(FileVersion), JToken.FromObject(FileVersion) }, - { nameof(Mod.Name), JToken.FromObject(_mod.Name) }, - { nameof(Mod.Author), JToken.FromObject(_mod.Author) }, - { nameof(Mod.Description), JToken.FromObject(_mod.Description) }, - { nameof(Mod.Version), JToken.FromObject(_mod.Version) }, - { nameof(Mod.Website), JToken.FromObject(_mod.Website) }, - { nameof(Mod.ModTags), JToken.FromObject(_mod.ModTags) }, + { nameof(Mod.Name), JToken.FromObject(mod.Name) }, + { nameof(Mod.Author), JToken.FromObject(mod.Author) }, + { nameof(Mod.Description), JToken.FromObject(mod.Description) }, + { nameof(Mod.Version), JToken.FromObject(mod.Version) }, + { nameof(Mod.Website), JToken.FromObject(mod.Website) }, + { nameof(Mod.ModTags), JToken.FromObject(mod.ModTags) }, }; - using var jWriter = new JsonTextWriter(writer) { Formatting = Formatting.Indented }; + using var jWriter = new JsonTextWriter(writer); + jWriter.Formatting = Formatting.Indented; jObject.WriteTo(jWriter); } } diff --git a/Penumbra/Mods/Subclasses/ModPriority.cs b/Penumbra/Mods/Settings/ModPriority.cs similarity index 98% rename from Penumbra/Mods/Subclasses/ModPriority.cs rename to Penumbra/Mods/Settings/ModPriority.cs index a99c12ed..993bd577 100644 --- a/Penumbra/Mods/Subclasses/ModPriority.cs +++ b/Penumbra/Mods/Settings/ModPriority.cs @@ -1,6 +1,6 @@ using Newtonsoft.Json; -namespace Penumbra.Mods.Subclasses; +namespace Penumbra.Mods.Settings; [JsonConverter(typeof(Converter))] public readonly record struct ModPriority(int Value) : diff --git a/Penumbra/Mods/Subclasses/ModSettings.cs b/Penumbra/Mods/Settings/ModSettings.cs similarity index 98% rename from Penumbra/Mods/Subclasses/ModSettings.cs rename to Penumbra/Mods/Settings/ModSettings.cs index 2ddabdb8..db9e0521 100644 --- a/Penumbra/Mods/Subclasses/ModSettings.cs +++ b/Penumbra/Mods/Settings/ModSettings.cs @@ -1,12 +1,11 @@ using OtterGui; using OtterGui.Filesystem; using Penumbra.Api.Enums; -using Penumbra.Meta.Manipulations; using Penumbra.Mods.Editor; +using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; -using Penumbra.String.Classes; -namespace Penumbra.Mods.Subclasses; +namespace Penumbra.Mods.Settings; /// Contains the settings for a given mod. public class ModSettings diff --git a/Penumbra/Mods/Subclasses/Setting.cs b/Penumbra/Mods/Settings/Setting.cs similarity index 98% rename from Penumbra/Mods/Subclasses/Setting.cs rename to Penumbra/Mods/Settings/Setting.cs index 18b1e4ca..231529b8 100644 --- a/Penumbra/Mods/Subclasses/Setting.cs +++ b/Penumbra/Mods/Settings/Setting.cs @@ -1,7 +1,7 @@ using Newtonsoft.Json; using OtterGui; -namespace Penumbra.Mods.Subclasses; +namespace Penumbra.Mods.Settings; [JsonConverter(typeof(Converter))] public readonly record struct Setting(ulong Value) diff --git a/Penumbra/Mods/Subclasses/SettingList.cs b/Penumbra/Mods/Settings/SettingList.cs similarity index 97% rename from Penumbra/Mods/Subclasses/SettingList.cs rename to Penumbra/Mods/Settings/SettingList.cs index ea1e447f..67b1b947 100644 --- a/Penumbra/Mods/Subclasses/SettingList.cs +++ b/Penumbra/Mods/Settings/SettingList.cs @@ -1,4 +1,4 @@ -namespace Penumbra.Mods.Subclasses; +namespace Penumbra.Mods.Settings; public class SettingList : List { diff --git a/Penumbra/Mods/SubMods/DefaultSubMod.cs b/Penumbra/Mods/SubMods/DefaultSubMod.cs new file mode 100644 index 00000000..ced0cd0d --- /dev/null +++ b/Penumbra/Mods/SubMods/DefaultSubMod.cs @@ -0,0 +1,30 @@ +using Newtonsoft.Json.Linq; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; +using Penumbra.Mods.Groups; +using Penumbra.String.Classes; + +namespace Penumbra.Mods.SubMods; + +public class DefaultSubMod(IMod mod) : IModDataContainer +{ + public const string FullName = "Default Option"; + + internal readonly IMod Mod = mod; + + public Dictionary Files { get; set; } = []; + public Dictionary FileSwaps { get; set; } = []; + public HashSet Manipulations { get; set; } = []; + + IMod IModDataContainer.Mod + => Mod; + + IModGroup? IModDataContainer.Group + => null; + + public void AddDataTo(Dictionary redirections, HashSet manipulations) + => ((IModDataContainer)this).AddDataTo(redirections, manipulations); + + public (int GroupIndex, int DataIndex) GetDataIndices() + => (-1, 0); +} diff --git a/Penumbra/Mods/Subclasses/IModDataContainer.cs b/Penumbra/Mods/SubMods/IModDataContainer.cs similarity index 80% rename from Penumbra/Mods/Subclasses/IModDataContainer.cs rename to Penumbra/Mods/SubMods/IModDataContainer.cs index a26beb2a..18b3b23f 100644 --- a/Penumbra/Mods/Subclasses/IModDataContainer.cs +++ b/Penumbra/Mods/SubMods/IModDataContainer.cs @@ -2,18 +2,19 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Editor; +using Penumbra.Mods.Groups; using Penumbra.String.Classes; -namespace Penumbra.Mods.Subclasses; +namespace Penumbra.Mods.SubMods; public interface IModDataContainer { - public IMod Mod { get; } + public IMod Mod { get; } public IModGroup? Group { get; } - public Dictionary Files { get; set; } - public Dictionary FileSwaps { get; set; } - public HashSet Manipulations { get; set; } + public Dictionary Files { get; set; } + public Dictionary FileSwaps { get; set; } + public HashSet Manipulations { get; set; } public void AddDataTo(Dictionary redirections, HashSet manipulations) { @@ -28,24 +29,24 @@ public interface IModDataContainer public string GetName() => this switch { - IModOption o => o.FullName, + IModOption o => o.FullName, DefaultSubMod => DefaultSubMod.FullName, - _ => $"Container {GetDataIndices().DataIndex + 1}", + _ => $"Container {GetDataIndices().DataIndex + 1}", }; public string GetFullName() => this switch { - IModOption o => o.FullName, - DefaultSubMod => DefaultSubMod.FullName, + IModOption o => o.FullName, + DefaultSubMod => DefaultSubMod.FullName, _ when Group != null => $"{Group.Name}: Container {GetDataIndices().DataIndex + 1}", - _ => $"Container {GetDataIndices().DataIndex + 1}", + _ => $"Container {GetDataIndices().DataIndex + 1}", }; public static void Clone(IModDataContainer from, IModDataContainer to) { - to.Files = new Dictionary(from.Files); - to.FileSwaps = new Dictionary(from.FileSwaps); + to.Files = new Dictionary(from.Files); + to.FileSwaps = new Dictionary(from.FileSwaps); to.Manipulations = [.. from.Manipulations]; } @@ -126,3 +127,5 @@ public interface IModDataContainer } } } + +public interface IModDataOption : IModOption, IModDataContainer; diff --git a/Penumbra/Mods/Subclasses/IModOption.cs b/Penumbra/Mods/SubMods/IModOption.cs similarity index 72% rename from Penumbra/Mods/Subclasses/IModOption.cs rename to Penumbra/Mods/SubMods/IModOption.cs index f66ce44e..f1ce1d4c 100644 --- a/Penumbra/Mods/Subclasses/IModOption.cs +++ b/Penumbra/Mods/SubMods/IModOption.cs @@ -1,17 +1,17 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; -namespace Penumbra.Mods.Subclasses; +namespace Penumbra.Mods.SubMods; public interface IModOption { - public string Name { get; set; } - public string FullName { get; } + public string Name { get; set; } + public string FullName { get; } public string Description { get; set; } public static void Load(JToken json, IModOption option) { - option.Name = json[nameof(Name)]?.ToObject() ?? string.Empty; + option.Name = json[nameof(Name)]?.ToObject() ?? string.Empty; option.Description = json[nameof(Description)]?.ToObject() ?? string.Empty; } diff --git a/Penumbra/Mods/SubMods/MultiSubMod.cs b/Penumbra/Mods/SubMods/MultiSubMod.cs new file mode 100644 index 00000000..00216b77 --- /dev/null +++ b/Penumbra/Mods/SubMods/MultiSubMod.cs @@ -0,0 +1,92 @@ +using Newtonsoft.Json.Linq; +using OtterGui; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Settings; +using Penumbra.String.Classes; + +namespace Penumbra.Mods.SubMods; + +public class MultiSubMod(Mod mod, MultiModGroup group) : IModDataOption +{ + internal readonly Mod Mod = mod; + internal readonly MultiModGroup Group = group; + + public string Name { get; set; } = "Option"; + + public string FullName + => $"{Group.Name}: {Name}"; + + public string Description { get; set; } = string.Empty; + public ModPriority Priority { get; set; } = ModPriority.Default; + + public Dictionary Files { get; set; } = []; + public Dictionary FileSwaps { get; set; } = []; + public HashSet Manipulations { get; set; } = []; + + IMod IModDataContainer.Mod + => Mod; + + IModGroup IModDataContainer.Group + => Group; + + + public MultiSubMod(Mod mod, MultiModGroup group, JToken json) + : this(mod, group) + { + IModOption.Load(json, this); + IModDataContainer.Load(json, this, mod.ModPath); + Priority = json[nameof(IModGroup.Priority)]?.ToObject() ?? ModPriority.Default; + } + + public MultiSubMod Clone(Mod mod, MultiModGroup group) + { + var ret = new MultiSubMod(mod, group) + { + Name = Name, + Description = Description, + Priority = Priority, + }; + IModDataContainer.Clone(this, ret); + + return ret; + } + + public SingleSubMod ConvertToSingle(Mod mod, SingleModGroup group) + { + var ret = new SingleSubMod(mod, group) + { + Name = Name, + Description = Description, + }; + IModDataContainer.Clone(this, ret); + return ret; + } + + public void AddDataTo(Dictionary redirections, HashSet manipulations) + => ((IModDataContainer)this).AddDataTo(redirections, manipulations); + + public static MultiSubMod CreateForSaving(string name, string description, ModPriority priority) + => new(null!, null!) + { + Name = name, + Description = description, + Priority = priority, + }; + + public (int GroupIndex, int DataIndex) GetDataIndices() + => (Group.GetIndex(), GetDataIndex()); + + public (int GroupIndex, int OptionIndex) GetOptionIndices() + => (Group.GetIndex(), GetDataIndex()); + + private int GetDataIndex() + { + var dataIndex = Group.DataContainers.IndexOf(this); + if (dataIndex < 0) + throw new Exception($"Group {Group.Name} from SubMod {Name} does not contain this SubMod."); + + return dataIndex; + } +} diff --git a/Penumbra/Mods/SubMods/SingleSubMod.cs b/Penumbra/Mods/SubMods/SingleSubMod.cs new file mode 100644 index 00000000..499ab192 --- /dev/null +++ b/Penumbra/Mods/SubMods/SingleSubMod.cs @@ -0,0 +1,82 @@ +using Newtonsoft.Json.Linq; +using OtterGui; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Settings; +using Penumbra.String.Classes; + +namespace Penumbra.Mods.SubMods; + +public class SingleSubMod(Mod mod, SingleModGroup group) : IModDataOption +{ + internal readonly Mod Mod = mod; + internal readonly SingleModGroup Group = group; + + public string Name { get; set; } = "Option"; + + public string FullName + => $"{Group.Name}: {Name}"; + + public string Description { get; set; } = string.Empty; + + IMod IModDataContainer.Mod + => Mod; + + IModGroup IModDataContainer.Group + => Group; + + public Dictionary Files { get; set; } = []; + public Dictionary FileSwaps { get; set; } = []; + public HashSet Manipulations { get; set; } = []; + + public SingleSubMod(Mod mod, SingleModGroup group, JToken json) + : this(mod, group) + { + IModOption.Load(json, this); + IModDataContainer.Load(json, this, mod.ModPath); + } + + public SingleSubMod Clone(Mod mod, SingleModGroup group) + { + var ret = new SingleSubMod(mod, group) + { + Name = Name, + Description = Description, + }; + IModDataContainer.Clone(this, ret); + + return ret; + } + + public MultiSubMod ConvertToMulti(Mod mod, MultiModGroup group, ModPriority priority) + { + var ret = new MultiSubMod(mod, group) + { + Name = Name, + Description = Description, + Priority = priority, + }; + IModDataContainer.Clone(this, ret); + + return ret; + } + + public void AddDataTo(Dictionary redirections, HashSet manipulations) + => ((IModDataContainer)this).AddDataTo(redirections, manipulations); + + public (int GroupIndex, int DataIndex) GetDataIndices() + => (Group.GetIndex(), GetDataIndex()); + + public (int GroupIndex, int OptionIndex) GetOptionIndices() + => (Group.GetIndex(), GetDataIndex()); + + private int GetDataIndex() + { + var dataIndex = Group.DataContainers.IndexOf(this); + if (dataIndex < 0) + throw new Exception($"Group {Group.Name} from SubMod {Name} does not contain this SubMod."); + + return dataIndex; + } +} diff --git a/Penumbra/Mods/Subclasses/SubMod.cs b/Penumbra/Mods/Subclasses/SubMod.cs deleted file mode 100644 index a2425eb7..00000000 --- a/Penumbra/Mods/Subclasses/SubMod.cs +++ /dev/null @@ -1,352 +0,0 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using OtterGui; -using Penumbra.Meta.Manipulations; -using Penumbra.Mods.Editor; -using Penumbra.String.Classes; - -namespace Penumbra.Mods.Subclasses; - -public interface IModDataOption : IModOption, IModDataContainer; - -public class SingleSubMod(Mod mod, SingleModGroup group) : IModDataOption -{ - internal readonly Mod Mod = mod; - internal readonly SingleModGroup Group = group; - - public string Name { get; set; } = "Option"; - - public string FullName - => $"{Group.Name}: {Name}"; - - public string Description { get; set; } = string.Empty; - - IMod IModDataContainer.Mod - => Mod; - - IModGroup IModDataContainer.Group - => Group; - - public Dictionary Files { get; set; } = []; - public Dictionary FileSwaps { get; set; } = []; - public HashSet Manipulations { get; set; } = []; - - public SingleSubMod(Mod mod, SingleModGroup group, JToken json) - : this(mod, group) - { - IModOption.Load(json, this); - IModDataContainer.Load(json, this, mod.ModPath); - } - - public SingleSubMod Clone(Mod mod, SingleModGroup group) - { - var ret = new SingleSubMod(mod, group) - { - Name = Name, - Description = Description, - }; - IModDataContainer.Clone(this, ret); - - return ret; - } - - public MultiSubMod ConvertToMulti(Mod mod, MultiModGroup group, ModPriority priority) - { - var ret = new MultiSubMod(mod, group) - { - Name = Name, - Description = Description, - Priority = priority, - }; - IModDataContainer.Clone(this, ret); - - return ret; - } - - public void AddDataTo(Dictionary redirections, HashSet manipulations) - => ((IModDataContainer)this).AddDataTo(redirections, manipulations); - - public (int GroupIndex, int DataIndex) GetDataIndices() - => (Group.GetIndex(), GetDataIndex()); - - public (int GroupIndex, int OptionIndex) GetOptionIndices() - => (Group.GetIndex(), GetDataIndex()); - - private int GetDataIndex() - { - var dataIndex = Group.DataContainers.IndexOf(this); - if (dataIndex < 0) - throw new Exception($"Group {Group.Name} from SubMod {Name} does not contain this SubMod."); - - return dataIndex; - } -} - -public class MultiSubMod(Mod mod, MultiModGroup group) : IModDataOption -{ - internal readonly Mod Mod = mod; - internal readonly MultiModGroup Group = group; - - public string Name { get; set; } = "Option"; - - public string FullName - => $"{Group.Name}: {Name}"; - - public string Description { get; set; } = string.Empty; - public ModPriority Priority { get; set; } = ModPriority.Default; - - public Dictionary Files { get; set; } = []; - public Dictionary FileSwaps { get; set; } = []; - public HashSet Manipulations { get; set; } = []; - - IMod IModDataContainer.Mod - => Mod; - - IModGroup IModDataContainer.Group - => Group; - - - public MultiSubMod(Mod mod, MultiModGroup group, JToken json) - : this(mod, group) - { - IModOption.Load(json, this); - IModDataContainer.Load(json, this, mod.ModPath); - Priority = json[nameof(IModGroup.Priority)]?.ToObject() ?? ModPriority.Default; - } - - public MultiSubMod Clone(Mod mod, MultiModGroup group) - { - var ret = new MultiSubMod(mod, group) - { - Name = Name, - Description = Description, - Priority = Priority, - }; - IModDataContainer.Clone(this, ret); - - return ret; - } - - public SingleSubMod ConvertToSingle(Mod mod, SingleModGroup group) - { - var ret = new SingleSubMod(mod, group) - { - Name = Name, - Description = Description, - }; - IModDataContainer.Clone(this, ret); - return ret; - } - - public void AddDataTo(Dictionary redirections, HashSet manipulations) - => ((IModDataContainer)this).AddDataTo(redirections, manipulations); - - public static MultiSubMod CreateForSaving(string name, string description, ModPriority priority) - => new(null!, null!) - { - Name = name, - Description = description, - Priority = priority, - }; - - public (int GroupIndex, int DataIndex) GetDataIndices() - => (Group.GetIndex(), GetDataIndex()); - - public (int GroupIndex, int OptionIndex) GetOptionIndices() - => (Group.GetIndex(), GetDataIndex()); - - private int GetDataIndex() - { - var dataIndex = Group.DataContainers.IndexOf(this); - if (dataIndex < 0) - throw new Exception($"Group {Group.Name} from SubMod {Name} does not contain this SubMod."); - - return dataIndex; - } -} - -public class DefaultSubMod(IMod mod) : IModDataContainer -{ - public const string FullName = "Default Option"; - - public string Description - => string.Empty; - - internal readonly IMod Mod = mod; - - public Dictionary Files { get; set; } = []; - public Dictionary FileSwaps { get; set; } = []; - public HashSet Manipulations { get; set; } = []; - - IMod IModDataContainer.Mod - => Mod; - - IModGroup? IModDataContainer.Group - => null; - - - public DefaultSubMod(Mod mod, JToken json) - : this(mod) - { - IModDataContainer.Load(json, this, mod.ModPath); - } - - public void AddDataTo(Dictionary redirections, HashSet manipulations) - => ((IModDataContainer)this).AddDataTo(redirections, manipulations); - - public (int GroupIndex, int DataIndex) GetDataIndices() - => (-1, 0); -} - - -//public sealed class SubMod(IMod mod, IModGroup group) : IModOption -//{ -// public string Name { get; set; } = "Default"; -// -// public string FullName -// => Group == null ? "Default Option" : $"{Group.Name}: {Name}"; -// -// public string Description { get; set; } = string.Empty; -// -// internal readonly IMod Mod = mod; -// internal readonly IModGroup? Group = group; -// -// internal (int GroupIdx, int OptionIdx) GetIndices() -// { -// if (IsDefault) -// return (-1, 0); -// -// var groupIdx = Mod.Groups.IndexOf(Group); -// if (groupIdx < 0) -// throw new Exception($"Group {Group.Name} from SubMod {Name} is not contained in Mod {Mod.Name}."); -// -// return (groupIdx, GetOptionIndex()); -// } -// -// private int GetOptionIndex() -// { -// var optionIndex = Group switch -// { -// null => 0, -// SingleModGroup single => single.OptionData.IndexOf(this), -// MultiModGroup multi => multi.OptionData.IndexOf(p => p.Mod == this), -// _ => throw new Exception($"Group {Group.Name} from SubMod {Name} has unknown type {typeof(Group)}"), -// }; -// if (optionIndex < 0) -// throw new Exception($"Group {Group!.Name} from SubMod {Name} does not contain this SubMod."); -// -// return optionIndex; -// } -// -// public static SubMod CreateDefault(IMod mod) -// => new(mod, null!); -// -// [MemberNotNullWhen(false, nameof(Group))] -// public bool IsDefault -// => Group == null; -// -// public void AddData(Dictionary redirections, HashSet manipulations) -// { -// foreach (var (path, file) in Files) -// redirections.TryAdd(path, file); -// -// foreach (var (path, file) in FileSwaps) -// redirections.TryAdd(path, file); -// manipulations.UnionWith(Manipulations); -// } -// -// public Dictionary FileData = []; -// public Dictionary FileSwapData = []; -// public HashSet ManipulationData = []; -// -// public IReadOnlyDictionary Files -// => FileData; -// -// public IReadOnlyDictionary FileSwaps -// => FileSwapData; -// -// public IReadOnlySet Manipulations -// => ManipulationData; -// -// public void Load(DirectoryInfo basePath, JToken json, out ModPriority priority) -// { -// FileData.Clear(); -// FileSwapData.Clear(); -// ManipulationData.Clear(); -// -// // Every option has a name, but priorities are only relevant for multi group options. -// Name = json[nameof(Name)]?.ToObject() ?? string.Empty; -// Description = json[nameof(Description)]?.ToObject() ?? string.Empty; -// priority = json[nameof(IModGroup.Priority)]?.ToObject() ?? ModPriority.Default; -// -// var files = (JObject?)json[nameof(Files)]; -// if (files != null) -// foreach (var property in files.Properties()) -// { -// if (Utf8GamePath.FromString(property.Name, out var p, true)) -// FileData.TryAdd(p, new FullPath(basePath, property.Value.ToObject())); -// } -// -// var swaps = (JObject?)json[nameof(FileSwaps)]; -// if (swaps != null) -// foreach (var property in swaps.Properties()) -// { -// if (Utf8GamePath.FromString(property.Name, out var p, true)) -// FileSwapData.TryAdd(p, new FullPath(property.Value.ToObject()!)); -// } -// -// var manips = json[nameof(Manipulations)]; -// if (manips != null) -// foreach (var s in manips.Children().Select(c => c.ToObject()) -// .Where(m => m.Validate())) -// ManipulationData.Add(s); -// } -// -// -// /// Create a sub mod without a mod or group only for saving it in the creator. -// internal static SubMod CreateForSaving(string name) -// => new(null!, null!) -// { -// Name = name, -// }; -// -// -// public static void WriteSubMod(JsonWriter j, JsonSerializer serializer, SubMod mod, DirectoryInfo basePath, ModPriority? priority) -// { -// j.WriteStartObject(); -// j.WritePropertyName(nameof(Name)); -// j.WriteValue(mod.Name); -// j.WritePropertyName(nameof(Description)); -// j.WriteValue(mod.Description); -// if (priority != null) -// { -// j.WritePropertyName(nameof(IModGroup.Priority)); -// j.WriteValue(priority.Value.Value); -// } -// -// j.WritePropertyName(nameof(mod.Files)); -// j.WriteStartObject(); -// foreach (var (gamePath, file) in mod.Files) -// { -// if (file.ToRelPath(basePath, out var relPath)) -// { -// j.WritePropertyName(gamePath.ToString()); -// j.WriteValue(relPath.ToString()); -// } -// } -// -// j.WriteEndObject(); -// j.WritePropertyName(nameof(mod.FileSwaps)); -// j.WriteStartObject(); -// foreach (var (gamePath, file) in mod.FileSwaps) -// { -// j.WritePropertyName(gamePath.ToString()); -// j.WriteValue(file.ToString()); -// } -// -// j.WriteEndObject(); -// j.WritePropertyName(nameof(mod.Manipulations)); -// serializer.Serialize(j, mod.Manipulations); -// j.WriteEndObject(); -// } -//} diff --git a/Penumbra/Mods/TemporaryMod.cs b/Penumbra/Mods/TemporaryMod.cs index 393369d4..d08c8b06 100644 --- a/Penumbra/Mods/TemporaryMod.cs +++ b/Penumbra/Mods/TemporaryMod.cs @@ -2,8 +2,10 @@ using OtterGui.Classes; using Penumbra.Collections; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Editor; +using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.String.Classes; @@ -46,7 +48,7 @@ public class TemporaryMod : IMod => Array.Empty(); public TemporaryMod() - => Default = new(this); + => Default = new DefaultSubMod(this); public void SetFile(Utf8GamePath gamePath, FullPath fullPath) => Default.Files[gamePath] = fullPath; diff --git a/Penumbra/Penumbra.csproj b/Penumbra/Penumbra.csproj index c8961579..0ec1fd44 100644 --- a/Penumbra/Penumbra.csproj +++ b/Penumbra/Penumbra.csproj @@ -93,6 +93,10 @@ + + + + diff --git a/Penumbra/Services/ConfigMigrationService.cs b/Penumbra/Services/ConfigMigrationService.cs index e775d81a..fafaa0e5 100644 --- a/Penumbra/Services/ConfigMigrationService.cs +++ b/Penumbra/Services/ConfigMigrationService.cs @@ -10,7 +10,7 @@ using Penumbra.Interop.Services; using Penumbra.Mods; using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; using Penumbra.UI; using Penumbra.UI.Classes; using Penumbra.UI.ResourceWatcher; diff --git a/Penumbra/Services/SaveService.cs b/Penumbra/Services/SaveService.cs index 801e0c1d..8d3cb641 100644 --- a/Penumbra/Services/SaveService.cs +++ b/Penumbra/Services/SaveService.cs @@ -2,7 +2,7 @@ using OtterGui.Classes; using OtterGui.Log; using OtterGui.Services; using Penumbra.Mods; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Groups; namespace Penumbra.Services; diff --git a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs index 5125a5b2..77bdb161 100644 --- a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs +++ b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs @@ -13,9 +13,10 @@ using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Meta; using Penumbra.Mods; +using Penumbra.Mods.Groups; using Penumbra.Mods.ItemSwap; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; using Penumbra.Services; using Penumbra.UI.Classes; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs index 94f1d577..107c56e6 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs @@ -4,7 +4,7 @@ using OtterGui; using OtterGui.Classes; using OtterGui.Raii; using Penumbra.Mods.Editor; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.SubMods; using Penumbra.String.Classes; using Penumbra.UI.Classes; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs index 70854fe7..55b7e748 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs @@ -8,7 +8,7 @@ using Penumbra.GameData.Files; using Penumbra.Interop.ResourceTree; using Penumbra.Mods; using Penumbra.Mods.Editor; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.SubMods; using Penumbra.String.Classes; namespace Penumbra.UI.AdvancedWindow; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index 21d14f78..dbb88fb7 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -21,7 +21,7 @@ using Penumbra.Meta; using Penumbra.Mods; using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.String; using Penumbra.String.Classes; diff --git a/Penumbra/UI/AdvancedWindow/ModMergeTab.cs b/Penumbra/UI/AdvancedWindow/ModMergeTab.cs index bed31ab8..5dad66b4 100644 --- a/Penumbra/UI/AdvancedWindow/ModMergeTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModMergeTab.cs @@ -4,7 +4,7 @@ using OtterGui; using OtterGui.Raii; using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.SubMods; using Penumbra.UI.Classes; namespace Penumbra.UI.AdvancedWindow; diff --git a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs index 11a2d96f..5b6cfa99 100644 --- a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs +++ b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs @@ -14,7 +14,7 @@ using Penumbra.Collections.Manager; using Penumbra.Communication; using Penumbra.Mods; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; using Penumbra.Services; using Penumbra.UI.Classes; using MessageService = Penumbra.Services.MessageService; diff --git a/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs b/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs index b2cfaf25..5e3aac48 100644 --- a/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs @@ -9,7 +9,7 @@ using Penumbra.Collections.Manager; using Penumbra.Meta.Manipulations; using Penumbra.Mods; using Penumbra.Mods.Editor; -using Penumbra.Mods.Subclasses; +using Penumbra.Mods.Settings; using Penumbra.String.Classes; using Penumbra.UI.Classes; diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index c05f1ac1..afbef45d 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -11,9 +11,10 @@ using Penumbra.Api.Enums; using Penumbra.Mods; using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; using Penumbra.Services; using Penumbra.UI.AdvancedWindow; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Settings; namespace Penumbra.UI.ModsTab; diff --git a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs index cb76088c..1326a763 100644 --- a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs @@ -8,8 +8,10 @@ using Penumbra.UI.Classes; using Dalamud.Interface.Components; using Penumbra.Collections.Manager; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; using Penumbra.Services; +using Penumbra.Mods.SubMods; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Settings; namespace Penumbra.UI.ModsTab; diff --git a/Penumbra/UI/ModsTab/ModPanelTabBar.cs b/Penumbra/UI/ModsTab/ModPanelTabBar.cs index 1aaa7741..8b09d8b9 100644 --- a/Penumbra/UI/ModsTab/ModPanelTabBar.cs +++ b/Penumbra/UI/ModsTab/ModPanelTabBar.cs @@ -5,7 +5,6 @@ using OtterGui.Raii; using OtterGui.Widgets; using Penumbra.Mods; using Penumbra.Mods.Manager; -using Penumbra.Mods.Subclasses; using Penumbra.UI.AdvancedWindow; namespace Penumbra.UI.ModsTab; From cd76c31d8ce73402b40e45d73dace053cc8a28b5 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 24 Apr 2024 23:41:55 +0200 Subject: [PATCH 131/433] Fix stack overflow. --- Penumbra/Mods/SubMods/DefaultSubMod.cs | 2 +- Penumbra/Mods/SubMods/IModDataContainer.cs | 8 ++++---- Penumbra/Mods/SubMods/MultiSubMod.cs | 2 +- Penumbra/Mods/SubMods/SingleSubMod.cs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Penumbra/Mods/SubMods/DefaultSubMod.cs b/Penumbra/Mods/SubMods/DefaultSubMod.cs index ced0cd0d..8b166505 100644 --- a/Penumbra/Mods/SubMods/DefaultSubMod.cs +++ b/Penumbra/Mods/SubMods/DefaultSubMod.cs @@ -23,7 +23,7 @@ public class DefaultSubMod(IMod mod) : IModDataContainer => null; public void AddDataTo(Dictionary redirections, HashSet manipulations) - => ((IModDataContainer)this).AddDataTo(redirections, manipulations); + => IModDataContainer.AddDataTo(this, redirections, manipulations); public (int GroupIndex, int DataIndex) GetDataIndices() => (-1, 0); diff --git a/Penumbra/Mods/SubMods/IModDataContainer.cs b/Penumbra/Mods/SubMods/IModDataContainer.cs index 18b3b23f..c9420821 100644 --- a/Penumbra/Mods/SubMods/IModDataContainer.cs +++ b/Penumbra/Mods/SubMods/IModDataContainer.cs @@ -16,14 +16,14 @@ public interface IModDataContainer public Dictionary FileSwaps { get; set; } public HashSet Manipulations { get; set; } - public void AddDataTo(Dictionary redirections, HashSet manipulations) + public static void AddDataTo(IModDataContainer container, Dictionary redirections, HashSet manipulations) { - foreach (var (path, file) in Files) + foreach (var (path, file) in container.Files) redirections.TryAdd(path, file); - foreach (var (path, file) in FileSwaps) + foreach (var (path, file) in container.FileSwaps) redirections.TryAdd(path, file); - manipulations.UnionWith(Manipulations); + manipulations.UnionWith(container.Manipulations); } public string GetName() diff --git a/Penumbra/Mods/SubMods/MultiSubMod.cs b/Penumbra/Mods/SubMods/MultiSubMod.cs index 00216b77..12dfcada 100644 --- a/Penumbra/Mods/SubMods/MultiSubMod.cs +++ b/Penumbra/Mods/SubMods/MultiSubMod.cs @@ -65,7 +65,7 @@ public class MultiSubMod(Mod mod, MultiModGroup group) : IModDataOption } public void AddDataTo(Dictionary redirections, HashSet manipulations) - => ((IModDataContainer)this).AddDataTo(redirections, manipulations); + => IModDataContainer.AddDataTo(this, redirections, manipulations); public static MultiSubMod CreateForSaving(string name, string description, ModPriority priority) => new(null!, null!) diff --git a/Penumbra/Mods/SubMods/SingleSubMod.cs b/Penumbra/Mods/SubMods/SingleSubMod.cs index 499ab192..ba91e271 100644 --- a/Penumbra/Mods/SubMods/SingleSubMod.cs +++ b/Penumbra/Mods/SubMods/SingleSubMod.cs @@ -63,7 +63,7 @@ public class SingleSubMod(Mod mod, SingleModGroup group) : IModDataOption } public void AddDataTo(Dictionary redirections, HashSet manipulations) - => ((IModDataContainer)this).AddDataTo(redirections, manipulations); + => IModDataContainer.AddDataTo(this, redirections, manipulations); public (int GroupIndex, int DataIndex) GetDataIndices() => (Group.GetIndex(), GetDataIndex()); From 72db023804f1d7e529b2bca49e5293b884469432 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 25 Apr 2024 17:58:32 +0200 Subject: [PATCH 132/433] Some cleanup. --- Penumbra/Mods/Groups/ModSaveGroup.cs | 2 +- Penumbra/Mods/Groups/MultiModGroup.cs | 6 +- Penumbra/Mods/Groups/SingleModGroup.cs | 6 +- Penumbra/Mods/Manager/ModOptionEditor.cs | 4 +- Penumbra/Mods/Mod.cs | 2 +- Penumbra/Mods/ModCreator.cs | 40 +++++--- Penumbra/Mods/SubMods/DefaultSubMod.cs | 6 +- Penumbra/Mods/SubMods/IModDataContainer.cs | 105 ++------------------- Penumbra/Mods/SubMods/IModOption.cs | 21 +---- Penumbra/Mods/SubMods/MultiSubMod.cs | 10 +- Penumbra/Mods/SubMods/SingleSubMod.cs | 10 +- Penumbra/Mods/SubMods/SubModHelpers.cs | 105 +++++++++++++++++++++ 12 files changed, 163 insertions(+), 154 deletions(-) create mode 100644 Penumbra/Mods/SubMods/SubModHelpers.cs diff --git a/Penumbra/Mods/Groups/ModSaveGroup.cs b/Penumbra/Mods/Groups/ModSaveGroup.cs index ed81f42f..e87fc012 100644 --- a/Penumbra/Mods/Groups/ModSaveGroup.cs +++ b/Penumbra/Mods/Groups/ModSaveGroup.cs @@ -51,7 +51,7 @@ public readonly struct ModSaveGroup : ISavable if (_groupIdx >= 0) _group!.WriteJson(j, serializer); else - IModDataContainer.WriteModData(j, serializer, _defaultMod!, _basePath); + SubModHelpers.WriteModContainer(j, serializer, _defaultMod!, _basePath); j.WriteEndObject(); } } diff --git a/Penumbra/Mods/Groups/MultiModGroup.cs b/Penumbra/Mods/Groups/MultiModGroup.cs index f39f2e70..1c8c769c 100644 --- a/Penumbra/Mods/Groups/MultiModGroup.cs +++ b/Penumbra/Mods/Groups/MultiModGroup.cs @@ -54,7 +54,7 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup return OptionData.Count - 1; } - public static MultiModGroup? Load(Mod mod, JObject json, int groupIdx) + public static MultiModGroup? Load(Mod mod, JObject json) { var ret = new MultiModGroup(mod) { @@ -140,10 +140,10 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup jWriter.WriteStartArray(); foreach (var option in OptionData) { - IModOption.WriteModOption(jWriter, option); + SubModHelpers.WriteModOption(jWriter, option); jWriter.WritePropertyName(nameof(option.Priority)); jWriter.WriteValue(option.Priority.Value); - IModDataContainer.WriteModData(jWriter, serializer, option, basePath ?? Mod.ModPath); + SubModHelpers.WriteModContainer(jWriter, serializer, option, basePath ?? Mod.ModPath); } jWriter.WriteEndArray(); diff --git a/Penumbra/Mods/Groups/SingleModGroup.cs b/Penumbra/Mods/Groups/SingleModGroup.cs index 6011185a..11542968 100644 --- a/Penumbra/Mods/Groups/SingleModGroup.cs +++ b/Penumbra/Mods/Groups/SingleModGroup.cs @@ -52,7 +52,7 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup public bool IsOption => OptionData.Count > 1; - public static SingleModGroup? Load(Mod mod, JObject json, int groupIdx) + public static SingleModGroup? Load(Mod mod, JObject json) { var options = json["Options"]; var ret = new SingleModGroup(mod) @@ -144,8 +144,8 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup jWriter.WriteStartArray(); foreach (var option in OptionData) { - IModOption.WriteModOption(jWriter, option); - IModDataContainer.WriteModData(jWriter, serializer, option, basePath ?? Mod.ModPath); + SubModHelpers.WriteModOption(jWriter, option); + SubModHelpers.WriteModContainer(jWriter, serializer, option, basePath ?? Mod.ModPath); } jWriter.WriteEndArray(); diff --git a/Penumbra/Mods/Manager/ModOptionEditor.cs b/Penumbra/Mods/Manager/ModOptionEditor.cs index b66fec17..a02c8d68 100644 --- a/Penumbra/Mods/Manager/ModOptionEditor.cs +++ b/Penumbra/Mods/Manager/ModOptionEditor.cs @@ -251,7 +251,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS Description = option.Description, }; if (option is IModDataContainer data) - IModDataContainer.Clone(data, newOption); + SubModHelpers.Clone(data, newOption); s.OptionData.Add(newOption); break; } @@ -265,7 +265,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS Priority = option is MultiSubMod s ? s.Priority : ModPriority.Default, }; if (option is IModDataContainer data) - IModDataContainer.Clone(data, newOption); + SubModHelpers.Clone(data, newOption); m.OptionData.Add(newOption); break; } diff --git a/Penumbra/Mods/Mod.cs b/Penumbra/Mods/Mod.cs index fc84afcc..6f6eb8ce 100644 --- a/Penumbra/Mods/Mod.cs +++ b/Penumbra/Mods/Mod.cs @@ -78,7 +78,7 @@ public sealed class Mod : IMod group.AddData(config, dictRedirections, setManips); } - Default.AddDataTo(dictRedirections, setManips); + Default.AddTo(dictRedirections, setManips); return new AppliedModData(dictRedirections, setManips); } diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index e8113ee1..0626bc9d 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -115,7 +115,7 @@ public partial class ModCreator( try { var jObject = File.Exists(defaultFile) ? JObject.Parse(File.ReadAllText(defaultFile)) : new JObject(); - IModDataContainer.Load(jObject, mod.Default, mod.ModPath); + SubModHelpers.LoadDataContainer(jObject, mod.Default, mod.ModPath); } catch (Exception e) { @@ -162,7 +162,7 @@ public partial class ModCreator( deleteList.AddRange(localDeleteList); } - IModDataContainer.DeleteDeleteList(deleteList, delete); + DeleteDeleteList(deleteList, delete); if (!changes) return; @@ -221,7 +221,7 @@ public partial class ModCreator( } } - IModDataContainer.DeleteDeleteList(deleteList, delete); + DeleteDeleteList(deleteList, delete); return (oldSize < option.Manipulations.Count, deleteList); } @@ -392,10 +392,8 @@ public partial class ModCreator( Penumbra.Log.Debug($"Writing the first {IModGroup.MaxMultiOptions} options to {Path.GetFileName(oldPath)} after split."); using (var oldFile = File.CreateText(oldPath)) { - using var j = new JsonTextWriter(oldFile) - { - Formatting = Formatting.Indented, - }; + using var j = new JsonTextWriter(oldFile); + j.Formatting = Formatting.Indented; json.WriteTo(j); } @@ -403,10 +401,8 @@ public partial class ModCreator( $"Writing the remaining {options.Count - IModGroup.MaxMultiOptions} options to {Path.GetFileName(newPath)} after split."); using (var newFile = File.CreateText(newPath)) { - using var j = new JsonTextWriter(newFile) - { - Formatting = Formatting.Indented, - }; + using var j = new JsonTextWriter(newFile); + j.Formatting = Formatting.Indented; clone.WriteTo(j); } @@ -436,8 +432,8 @@ public partial class ModCreator( var json = JObject.Parse(File.ReadAllText(file.FullName)); switch (json[nameof(Type)]?.ToObject() ?? GroupType.Single) { - case GroupType.Multi: return MultiModGroup.Load(mod, json, groupIdx); - case GroupType.Single: return SingleModGroup.Load(mod, json, groupIdx); + case GroupType.Multi: return MultiModGroup.Load(mod, json); + case GroupType.Single: return SingleModGroup.Load(mod, json); } } catch (Exception e) @@ -446,5 +442,23 @@ public partial class ModCreator( } return null; + } + + internal static void DeleteDeleteList(IEnumerable deleteList, bool delete) + { + if (!delete) + return; + + foreach (var file in deleteList) + { + try + { + File.Delete(file); + } + catch (Exception e) + { + Penumbra.Log.Error($"Could not delete incorporated meta file {file}:\n{e}"); + } + } } } diff --git a/Penumbra/Mods/SubMods/DefaultSubMod.cs b/Penumbra/Mods/SubMods/DefaultSubMod.cs index 8b166505..8b00e2ae 100644 --- a/Penumbra/Mods/SubMods/DefaultSubMod.cs +++ b/Penumbra/Mods/SubMods/DefaultSubMod.cs @@ -22,9 +22,9 @@ public class DefaultSubMod(IMod mod) : IModDataContainer IModGroup? IModDataContainer.Group => null; - public void AddDataTo(Dictionary redirections, HashSet manipulations) - => IModDataContainer.AddDataTo(this, redirections, manipulations); + public void AddTo(Dictionary redirections, HashSet manipulations) + => SubModHelpers.AddContainerTo(this, redirections, manipulations); public (int GroupIndex, int DataIndex) GetDataIndices() => (-1, 0); -} +} diff --git a/Penumbra/Mods/SubMods/IModDataContainer.cs b/Penumbra/Mods/SubMods/IModDataContainer.cs index c9420821..1e676816 100644 --- a/Penumbra/Mods/SubMods/IModDataContainer.cs +++ b/Penumbra/Mods/SubMods/IModDataContainer.cs @@ -1,5 +1,3 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Editor; using Penumbra.Mods.Groups; @@ -7,6 +5,7 @@ using Penumbra.String.Classes; namespace Penumbra.Mods.SubMods; + public interface IModDataContainer { public IMod Mod { get; } @@ -16,116 +15,24 @@ public interface IModDataContainer public Dictionary FileSwaps { get; set; } public HashSet Manipulations { get; set; } - public static void AddDataTo(IModDataContainer container, Dictionary redirections, HashSet manipulations) - { - foreach (var (path, file) in container.Files) - redirections.TryAdd(path, file); - - foreach (var (path, file) in container.FileSwaps) - redirections.TryAdd(path, file); - manipulations.UnionWith(container.Manipulations); - } - public string GetName() => this switch { - IModOption o => o.FullName, + IModOption o => o.FullName, DefaultSubMod => DefaultSubMod.FullName, - _ => $"Container {GetDataIndices().DataIndex + 1}", + _ => $"Container {GetDataIndices().DataIndex + 1}", }; public string GetFullName() => this switch { - IModOption o => o.FullName, - DefaultSubMod => DefaultSubMod.FullName, + IModOption o => o.FullName, + DefaultSubMod => DefaultSubMod.FullName, _ when Group != null => $"{Group.Name}: Container {GetDataIndices().DataIndex + 1}", - _ => $"Container {GetDataIndices().DataIndex + 1}", + _ => $"Container {GetDataIndices().DataIndex + 1}", }; - public static void Clone(IModDataContainer from, IModDataContainer to) - { - to.Files = new Dictionary(from.Files); - to.FileSwaps = new Dictionary(from.FileSwaps); - to.Manipulations = [.. from.Manipulations]; - } - public (int GroupIndex, int DataIndex) GetDataIndices(); - - public static void Load(JToken json, IModDataContainer data, DirectoryInfo basePath) - { - data.Files.Clear(); - data.FileSwaps.Clear(); - data.Manipulations.Clear(); - - var files = (JObject?)json[nameof(Files)]; - if (files != null) - foreach (var property in files.Properties()) - { - if (Utf8GamePath.FromString(property.Name, out var p, true)) - data.Files.TryAdd(p, new FullPath(basePath, property.Value.ToObject())); - } - - var swaps = (JObject?)json[nameof(FileSwaps)]; - if (swaps != null) - foreach (var property in swaps.Properties()) - { - if (Utf8GamePath.FromString(property.Name, out var p, true)) - data.FileSwaps.TryAdd(p, new FullPath(property.Value.ToObject()!)); - } - - var manips = json[nameof(Manipulations)]; - if (manips != null) - foreach (var s in manips.Children().Select(c => c.ToObject()) - .Where(m => m.Validate())) - data.Manipulations.Add(s); - } - - public static void WriteModData(JsonWriter j, JsonSerializer serializer, IModDataContainer data, DirectoryInfo basePath) - { - j.WritePropertyName(nameof(data.Files)); - j.WriteStartObject(); - foreach (var (gamePath, file) in data.Files) - { - if (file.ToRelPath(basePath, out var relPath)) - { - j.WritePropertyName(gamePath.ToString()); - j.WriteValue(relPath.ToString()); - } - } - - j.WriteEndObject(); - j.WritePropertyName(nameof(data.FileSwaps)); - j.WriteStartObject(); - foreach (var (gamePath, file) in data.FileSwaps) - { - j.WritePropertyName(gamePath.ToString()); - j.WriteValue(file.ToString()); - } - - j.WriteEndObject(); - j.WritePropertyName(nameof(data.Manipulations)); - serializer.Serialize(j, data.Manipulations); - j.WriteEndObject(); - } - - internal static void DeleteDeleteList(IEnumerable deleteList, bool delete) - { - if (!delete) - return; - - foreach (var file in deleteList) - { - try - { - File.Delete(file); - } - catch (Exception e) - { - Penumbra.Log.Error($"Could not delete incorporated meta file {file}:\n{e}"); - } - } - } } public interface IModDataOption : IModOption, IModDataContainer; diff --git a/Penumbra/Mods/SubMods/IModOption.cs b/Penumbra/Mods/SubMods/IModOption.cs index f1ce1d4c..83d632a0 100644 --- a/Penumbra/Mods/SubMods/IModOption.cs +++ b/Penumbra/Mods/SubMods/IModOption.cs @@ -1,27 +1,10 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; - namespace Penumbra.Mods.SubMods; public interface IModOption { - public string Name { get; set; } - public string FullName { get; } + public string Name { get; set; } + public string FullName { get; } public string Description { get; set; } - public static void Load(JToken json, IModOption option) - { - option.Name = json[nameof(Name)]?.ToObject() ?? string.Empty; - option.Description = json[nameof(Description)]?.ToObject() ?? string.Empty; - } - public (int GroupIndex, int OptionIndex) GetOptionIndices(); - - public static void WriteModOption(JsonWriter j, IModOption option) - { - j.WritePropertyName(nameof(Name)); - j.WriteValue(option.Name); - j.WritePropertyName(nameof(Description)); - j.WriteValue(option.Description); - } } diff --git a/Penumbra/Mods/SubMods/MultiSubMod.cs b/Penumbra/Mods/SubMods/MultiSubMod.cs index 12dfcada..ccb787a5 100644 --- a/Penumbra/Mods/SubMods/MultiSubMod.cs +++ b/Penumbra/Mods/SubMods/MultiSubMod.cs @@ -35,8 +35,8 @@ public class MultiSubMod(Mod mod, MultiModGroup group) : IModDataOption public MultiSubMod(Mod mod, MultiModGroup group, JToken json) : this(mod, group) { - IModOption.Load(json, this); - IModDataContainer.Load(json, this, mod.ModPath); + SubModHelpers.LoadOptionData(json, this); + SubModHelpers.LoadDataContainer(json, this, mod.ModPath); Priority = json[nameof(IModGroup.Priority)]?.ToObject() ?? ModPriority.Default; } @@ -48,7 +48,7 @@ public class MultiSubMod(Mod mod, MultiModGroup group) : IModDataOption Description = Description, Priority = Priority, }; - IModDataContainer.Clone(this, ret); + SubModHelpers.Clone(this, ret); return ret; } @@ -60,12 +60,12 @@ public class MultiSubMod(Mod mod, MultiModGroup group) : IModDataOption Name = Name, Description = Description, }; - IModDataContainer.Clone(this, ret); + SubModHelpers.Clone(this, ret); return ret; } public void AddDataTo(Dictionary redirections, HashSet manipulations) - => IModDataContainer.AddDataTo(this, redirections, manipulations); + => SubModHelpers.AddContainerTo(this, redirections, manipulations); public static MultiSubMod CreateForSaving(string name, string description, ModPriority priority) => new(null!, null!) diff --git a/Penumbra/Mods/SubMods/SingleSubMod.cs b/Penumbra/Mods/SubMods/SingleSubMod.cs index ba91e271..e6740a47 100644 --- a/Penumbra/Mods/SubMods/SingleSubMod.cs +++ b/Penumbra/Mods/SubMods/SingleSubMod.cs @@ -33,8 +33,8 @@ public class SingleSubMod(Mod mod, SingleModGroup group) : IModDataOption public SingleSubMod(Mod mod, SingleModGroup group, JToken json) : this(mod, group) { - IModOption.Load(json, this); - IModDataContainer.Load(json, this, mod.ModPath); + SubModHelpers.LoadOptionData(json, this); + SubModHelpers.LoadDataContainer(json, this, mod.ModPath); } public SingleSubMod Clone(Mod mod, SingleModGroup group) @@ -44,7 +44,7 @@ public class SingleSubMod(Mod mod, SingleModGroup group) : IModDataOption Name = Name, Description = Description, }; - IModDataContainer.Clone(this, ret); + SubModHelpers.Clone(this, ret); return ret; } @@ -57,13 +57,13 @@ public class SingleSubMod(Mod mod, SingleModGroup group) : IModDataOption Description = Description, Priority = priority, }; - IModDataContainer.Clone(this, ret); + SubModHelpers.Clone(this, ret); return ret; } public void AddDataTo(Dictionary redirections, HashSet manipulations) - => IModDataContainer.AddDataTo(this, redirections, manipulations); + => SubModHelpers.AddContainerTo(this, redirections, manipulations); public (int GroupIndex, int DataIndex) GetDataIndices() => (Group.GetIndex(), GetDataIndex()); diff --git a/Penumbra/Mods/SubMods/SubModHelpers.cs b/Penumbra/Mods/SubMods/SubModHelpers.cs new file mode 100644 index 00000000..9992b6e8 --- /dev/null +++ b/Penumbra/Mods/SubMods/SubModHelpers.cs @@ -0,0 +1,105 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.ItemSwap; +using Penumbra.String.Classes; + +namespace Penumbra.Mods.SubMods; + +public static class SubModHelpers +{ + /// Add all unique meta manipulations, file redirections and then file swaps from a ModDataContainer to the given sets. Skip any keys that are already contained. + public static void AddContainerTo(IModDataContainer container, Dictionary redirections, + HashSet manipulations) + { + foreach (var (path, file) in container.Files) + redirections.TryAdd(path, file); + + foreach (var (path, file) in container.FileSwaps) + redirections.TryAdd(path, file); + manipulations.UnionWith(container.Manipulations); + } + + /// Replace all data of with the data of . + public static void Clone(IModDataContainer from, IModDataContainer to) + { + to.Files = new Dictionary(from.Files); + to.FileSwaps = new Dictionary(from.FileSwaps); + to.Manipulations = [.. from.Manipulations]; + } + + /// Load all file redirections, file swaps and meta manipulations from a JToken of that option into a data container. + public static void LoadDataContainer(JToken json, IModDataContainer data, DirectoryInfo basePath) + { + data.Files.Clear(); + data.FileSwaps.Clear(); + data.Manipulations.Clear(); + + var files = (JObject?)json[nameof(data.Files)]; + if (files != null) + foreach (var property in files.Properties()) + { + if (Utf8GamePath.FromString(property.Name, out var p, true)) + data.Files.TryAdd(p, new FullPath(basePath, property.Value.ToObject())); + } + + var swaps = (JObject?)json[nameof(data.FileSwaps)]; + if (swaps != null) + foreach (var property in swaps.Properties()) + { + if (Utf8GamePath.FromString(property.Name, out var p, true)) + data.FileSwaps.TryAdd(p, new FullPath(property.Value.ToObject()!)); + } + + var manips = json[nameof(data.Manipulations)]; + if (manips != null) + foreach (var s in manips.Children().Select(c => c.ToObject()) + .Where(m => m.Validate())) + data.Manipulations.Add(s); + } + + /// Load the relevant data for a selectable option from a JToken of that option. + public static void LoadOptionData(JToken json, IModOption option) + { + option.Name = json[nameof(option.Name)]?.ToObject() ?? string.Empty; + option.Description = json[nameof(option.Description)]?.ToObject() ?? string.Empty; + } + + /// Write file redirections, file swaps and meta manipulations from a data container on a JsonWriter. + public static void WriteModContainer(JsonWriter j, JsonSerializer serializer, IModDataContainer data, DirectoryInfo basePath) + { + j.WritePropertyName(nameof(data.Files)); + j.WriteStartObject(); + foreach (var (gamePath, file) in data.Files) + { + if (file.ToRelPath(basePath, out var relPath)) + { + j.WritePropertyName(gamePath.ToString()); + j.WriteValue(relPath.ToString()); + } + } + + j.WriteEndObject(); + j.WritePropertyName(nameof(data.FileSwaps)); + j.WriteStartObject(); + foreach (var (gamePath, file) in data.FileSwaps) + { + j.WritePropertyName(gamePath.ToString()); + j.WriteValue(file.ToString()); + } + + j.WriteEndObject(); + j.WritePropertyName(nameof(data.Manipulations)); + serializer.Serialize(j, data.Manipulations); + j.WriteEndObject(); + } + + /// Write the data for a selectable mod option on a JsonWriter. + public static void WriteModOption(JsonWriter j, IModOption option) + { + j.WritePropertyName(nameof(option.Name)); + j.WriteValue(option.Name); + j.WritePropertyName(nameof(option.Description)); + j.WriteValue(option.Description); + } +} From 0fd14ffefc11475920e7a7f1cf00c70e8ba46e22 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 25 Apr 2024 18:14:21 +0200 Subject: [PATCH 133/433] More cleanup. --- Penumbra/Mods/SubMods/DefaultSubMod.cs | 7 ++- Penumbra/Mods/SubMods/IModDataContainer.cs | 21 +------ Penumbra/Mods/SubMods/MultiSubMod.cs | 64 ++++------------------ Penumbra/Mods/SubMods/OptionSubMod.cs | 57 +++++++++++++++++++ Penumbra/Mods/SubMods/SingleSubMod.cs | 64 ++++------------------ Penumbra/Mods/SubMods/SubModHelpers.cs | 5 +- 6 files changed, 89 insertions(+), 129 deletions(-) create mode 100644 Penumbra/Mods/SubMods/OptionSubMod.cs diff --git a/Penumbra/Mods/SubMods/DefaultSubMod.cs b/Penumbra/Mods/SubMods/DefaultSubMod.cs index 8b00e2ae..1eddcef6 100644 --- a/Penumbra/Mods/SubMods/DefaultSubMod.cs +++ b/Penumbra/Mods/SubMods/DefaultSubMod.cs @@ -1,4 +1,3 @@ -using Newtonsoft.Json.Linq; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Editor; using Penumbra.Mods.Groups; @@ -25,6 +24,12 @@ public class DefaultSubMod(IMod mod) : IModDataContainer public void AddTo(Dictionary redirections, HashSet manipulations) => SubModHelpers.AddContainerTo(this, redirections, manipulations); + public string GetName() + => FullName; + + public string GetFullName() + => FullName; + public (int GroupIndex, int DataIndex) GetDataIndices() => (-1, 0); } diff --git a/Penumbra/Mods/SubMods/IModDataContainer.cs b/Penumbra/Mods/SubMods/IModDataContainer.cs index 1e676816..a6ab491f 100644 --- a/Penumbra/Mods/SubMods/IModDataContainer.cs +++ b/Penumbra/Mods/SubMods/IModDataContainer.cs @@ -15,24 +15,7 @@ public interface IModDataContainer public Dictionary FileSwaps { get; set; } public HashSet Manipulations { get; set; } - public string GetName() - => this switch - { - IModOption o => o.FullName, - DefaultSubMod => DefaultSubMod.FullName, - _ => $"Container {GetDataIndices().DataIndex + 1}", - }; - - public string GetFullName() - => this switch - { - IModOption o => o.FullName, - DefaultSubMod => DefaultSubMod.FullName, - _ when Group != null => $"{Group.Name}: Container {GetDataIndices().DataIndex + 1}", - _ => $"Container {GetDataIndices().DataIndex + 1}", - }; - + public string GetName(); + public string GetFullName(); public (int GroupIndex, int DataIndex) GetDataIndices(); } - -public interface IModDataOption : IModOption, IModDataContainer; diff --git a/Penumbra/Mods/SubMods/MultiSubMod.cs b/Penumbra/Mods/SubMods/MultiSubMod.cs index ccb787a5..c43d4b9e 100644 --- a/Penumbra/Mods/SubMods/MultiSubMod.cs +++ b/Penumbra/Mods/SubMods/MultiSubMod.cs @@ -1,37 +1,13 @@ -using Newtonsoft.Json.Linq; -using OtterGui; -using Penumbra.Meta.Manipulations; -using Penumbra.Mods.Editor; +using Newtonsoft.Json.Linq; using Penumbra.Mods.Groups; using Penumbra.Mods.Settings; -using Penumbra.String.Classes; - -namespace Penumbra.Mods.SubMods; - -public class MultiSubMod(Mod mod, MultiModGroup group) : IModDataOption + +namespace Penumbra.Mods.SubMods; + +public class MultiSubMod(Mod mod, MultiModGroup group) : OptionSubMod(mod, group) { - internal readonly Mod Mod = mod; - internal readonly MultiModGroup Group = group; - - public string Name { get; set; } = "Option"; - - public string FullName - => $"{Group.Name}: {Name}"; - - public string Description { get; set; } = string.Empty; public ModPriority Priority { get; set; } = ModPriority.Default; - public Dictionary Files { get; set; } = []; - public Dictionary FileSwaps { get; set; } = []; - public HashSet Manipulations { get; set; } = []; - - IMod IModDataContainer.Mod - => Mod; - - IModGroup IModDataContainer.Group - => Group; - - public MultiSubMod(Mod mod, MultiModGroup group, JToken json) : this(mod, group) { @@ -44,9 +20,9 @@ public class MultiSubMod(Mod mod, MultiModGroup group) : IModDataOption { var ret = new MultiSubMod(mod, group) { - Name = Name, + Name = Name, Description = Description, - Priority = Priority, + Priority = Priority, }; SubModHelpers.Clone(this, ret); @@ -57,36 +33,18 @@ public class MultiSubMod(Mod mod, MultiModGroup group) : IModDataOption { var ret = new SingleSubMod(mod, group) { - Name = Name, + Name = Name, Description = Description, }; SubModHelpers.Clone(this, ret); return ret; } - public void AddDataTo(Dictionary redirections, HashSet manipulations) - => SubModHelpers.AddContainerTo(this, redirections, manipulations); - public static MultiSubMod CreateForSaving(string name, string description, ModPriority priority) => new(null!, null!) { - Name = name, + Name = name, Description = description, - Priority = priority, + Priority = priority, }; - - public (int GroupIndex, int DataIndex) GetDataIndices() - => (Group.GetIndex(), GetDataIndex()); - - public (int GroupIndex, int OptionIndex) GetOptionIndices() - => (Group.GetIndex(), GetDataIndex()); - - private int GetDataIndex() - { - var dataIndex = Group.DataContainers.IndexOf(this); - if (dataIndex < 0) - throw new Exception($"Group {Group.Name} from SubMod {Name} does not contain this SubMod."); - - return dataIndex; - } -} +} diff --git a/Penumbra/Mods/SubMods/OptionSubMod.cs b/Penumbra/Mods/SubMods/OptionSubMod.cs new file mode 100644 index 00000000..79c50e51 --- /dev/null +++ b/Penumbra/Mods/SubMods/OptionSubMod.cs @@ -0,0 +1,57 @@ +using OtterGui; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; +using Penumbra.Mods.Groups; +using Penumbra.String.Classes; + +namespace Penumbra.Mods.SubMods; + +public interface IModDataOption : IModDataContainer, IModOption; + +public abstract class OptionSubMod(Mod mod, T group) : IModDataOption + where T : IModGroup +{ + internal readonly Mod Mod = mod; + internal readonly IModGroup Group = group; + + public string Name { get; set; } = "Option"; + + public string FullName + => $"{Group!.Name}: {Name}"; + + public string Description { get; set; } = string.Empty; + + IMod IModDataContainer.Mod + => Mod; + + IModGroup IModDataContainer.Group + => Group; + + public Dictionary Files { get; set; } = []; + public Dictionary FileSwaps { get; set; } = []; + public HashSet Manipulations { get; set; } = []; + + public void AddDataTo(Dictionary redirections, HashSet manipulations) + => SubModHelpers.AddContainerTo(this, redirections, manipulations); + + public string GetName() + => Name; + + public string GetFullName() + => FullName; + + public (int GroupIndex, int DataIndex) GetDataIndices() + => (Group.GetIndex(), GetDataIndex()); + + public (int GroupIndex, int OptionIndex) GetOptionIndices() + => (Group.GetIndex(), GetDataIndex()); + + private int GetDataIndex() + { + var dataIndex = Group.DataContainers.IndexOf(this); + if (dataIndex < 0) + throw new Exception($"Group {Group.Name} from SubMod {Name} does not contain this SubMod."); + + return dataIndex; + } +} diff --git a/Penumbra/Mods/SubMods/SingleSubMod.cs b/Penumbra/Mods/SubMods/SingleSubMod.cs index e6740a47..5d68e401 100644 --- a/Penumbra/Mods/SubMods/SingleSubMod.cs +++ b/Penumbra/Mods/SubMods/SingleSubMod.cs @@ -1,37 +1,13 @@ -using Newtonsoft.Json.Linq; -using OtterGui; -using Penumbra.Meta.Manipulations; -using Penumbra.Mods.Editor; +using Newtonsoft.Json.Linq; using Penumbra.Mods.Groups; using Penumbra.Mods.Settings; -using Penumbra.String.Classes; - -namespace Penumbra.Mods.SubMods; - -public class SingleSubMod(Mod mod, SingleModGroup group) : IModDataOption + +namespace Penumbra.Mods.SubMods; + +public class SingleSubMod(Mod mod, SingleModGroup singleGroup) : OptionSubMod(mod, singleGroup) { - internal readonly Mod Mod = mod; - internal readonly SingleModGroup Group = group; - - public string Name { get; set; } = "Option"; - - public string FullName - => $"{Group.Name}: {Name}"; - - public string Description { get; set; } = string.Empty; - - IMod IModDataContainer.Mod - => Mod; - - IModGroup IModDataContainer.Group - => Group; - - public Dictionary Files { get; set; } = []; - public Dictionary FileSwaps { get; set; } = []; - public HashSet Manipulations { get; set; } = []; - - public SingleSubMod(Mod mod, SingleModGroup group, JToken json) - : this(mod, group) + public SingleSubMod(Mod mod, SingleModGroup singleGroup, JToken json) + : this(mod, singleGroup) { SubModHelpers.LoadOptionData(json, this); SubModHelpers.LoadDataContainer(json, this, mod.ModPath); @@ -41,7 +17,7 @@ public class SingleSubMod(Mod mod, SingleModGroup group) : IModDataOption { var ret = new SingleSubMod(mod, group) { - Name = Name, + Name = Name, Description = Description, }; SubModHelpers.Clone(this, ret); @@ -53,30 +29,12 @@ public class SingleSubMod(Mod mod, SingleModGroup group) : IModDataOption { var ret = new MultiSubMod(mod, group) { - Name = Name, + Name = Name, Description = Description, - Priority = priority, + Priority = priority, }; SubModHelpers.Clone(this, ret); return ret; } - - public void AddDataTo(Dictionary redirections, HashSet manipulations) - => SubModHelpers.AddContainerTo(this, redirections, manipulations); - - public (int GroupIndex, int DataIndex) GetDataIndices() - => (Group.GetIndex(), GetDataIndex()); - - public (int GroupIndex, int OptionIndex) GetOptionIndices() - => (Group.GetIndex(), GetDataIndex()); - - private int GetDataIndex() - { - var dataIndex = Group.DataContainers.IndexOf(this); - if (dataIndex < 0) - throw new Exception($"Group {Group.Name} from SubMod {Name} does not contain this SubMod."); - - return dataIndex; - } -} +} diff --git a/Penumbra/Mods/SubMods/SubModHelpers.cs b/Penumbra/Mods/SubMods/SubModHelpers.cs index 9992b6e8..2a09fbc3 100644 --- a/Penumbra/Mods/SubMods/SubModHelpers.cs +++ b/Penumbra/Mods/SubMods/SubModHelpers.cs @@ -1,7 +1,6 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Penumbra.Meta.Manipulations; -using Penumbra.Mods.ItemSwap; using Penumbra.String.Classes; namespace Penumbra.Mods.SubMods; @@ -92,9 +91,9 @@ public static class SubModHelpers j.WritePropertyName(nameof(data.Manipulations)); serializer.Serialize(j, data.Manipulations); j.WriteEndObject(); - } + } - /// Write the data for a selectable mod option on a JsonWriter. + /// Write the data for a selectable mod option on a JsonWriter. public static void WriteModOption(JsonWriter j, IModOption option) { j.WritePropertyName(nameof(option.Name)); From 06953c175dbb1e733612c02e8bf815ed82247d29 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 25 Apr 2024 18:18:57 +0200 Subject: [PATCH 134/433] mooooore --- Penumbra/Mods/Groups/IModGroup.cs | 15 --------------- Penumbra/Mods/Groups/ModSaveGroup.cs | 15 +++++++++++++++ Penumbra/Mods/Groups/MultiModGroup.cs | 4 +++- Penumbra/Mods/Groups/SingleModGroup.cs | 4 +++- Penumbra/Mods/SubMods/DefaultSubMod.cs | 20 ++++++++++---------- 5 files changed, 31 insertions(+), 27 deletions(-) diff --git a/Penumbra/Mods/Groups/IModGroup.cs b/Penumbra/Mods/Groups/IModGroup.cs index dc5150cf..0cabc9f3 100644 --- a/Penumbra/Mods/Groups/IModGroup.cs +++ b/Penumbra/Mods/Groups/IModGroup.cs @@ -68,21 +68,6 @@ public interface IModGroup return true; } - public static void WriteJsonBase(JsonTextWriter jWriter, IModGroup group) - { - jWriter.WriteStartObject(); - jWriter.WritePropertyName(nameof(group.Name)); - jWriter.WriteValue(group!.Name); - jWriter.WritePropertyName(nameof(group.Description)); - jWriter.WriteValue(group.Description); - jWriter.WritePropertyName(nameof(group.Priority)); - jWriter.WriteValue(group.Priority.Value); - jWriter.WritePropertyName(nameof(group.Type)); - jWriter.WriteValue(group.Type.ToString()); - jWriter.WritePropertyName(nameof(group.DefaultSettings)); - jWriter.WriteValue(group.DefaultSettings.Value); - } - public (int Redirections, int Swaps, int Manips) GetCounts(); public static (int Redirections, int Swaps, int Manips) GetCountsBase(IModGroup group) diff --git a/Penumbra/Mods/Groups/ModSaveGroup.cs b/Penumbra/Mods/Groups/ModSaveGroup.cs index e87fc012..92ccb36e 100644 --- a/Penumbra/Mods/Groups/ModSaveGroup.cs +++ b/Penumbra/Mods/Groups/ModSaveGroup.cs @@ -54,4 +54,19 @@ public readonly struct ModSaveGroup : ISavable SubModHelpers.WriteModContainer(j, serializer, _defaultMod!, _basePath); j.WriteEndObject(); } + + public static void WriteJsonBase(JsonTextWriter jWriter, IModGroup group) + { + jWriter.WriteStartObject(); + jWriter.WritePropertyName(nameof(group.Name)); + jWriter.WriteValue(group!.Name); + jWriter.WritePropertyName(nameof(group.Description)); + jWriter.WriteValue(group.Description); + jWriter.WritePropertyName(nameof(group.Priority)); + jWriter.WriteValue(group.Priority.Value); + jWriter.WritePropertyName(nameof(group.Type)); + jWriter.WriteValue(group.Type.ToString()); + jWriter.WritePropertyName(nameof(group.DefaultSettings)); + jWriter.WriteValue(group.DefaultSettings.Value); + } } diff --git a/Penumbra/Mods/Groups/MultiModGroup.cs b/Penumbra/Mods/Groups/MultiModGroup.cs index 1c8c769c..7e900ef5 100644 --- a/Penumbra/Mods/Groups/MultiModGroup.cs +++ b/Penumbra/Mods/Groups/MultiModGroup.cs @@ -135,15 +135,17 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup public void WriteJson(JsonTextWriter jWriter, JsonSerializer serializer, DirectoryInfo? basePath = null) { - IModGroup.WriteJsonBase(jWriter, this); + ModSaveGroup.WriteJsonBase(jWriter, this); jWriter.WritePropertyName("Options"); jWriter.WriteStartArray(); foreach (var option in OptionData) { + jWriter.WriteStartObject(); SubModHelpers.WriteModOption(jWriter, option); jWriter.WritePropertyName(nameof(option.Priority)); jWriter.WriteValue(option.Priority.Value); SubModHelpers.WriteModContainer(jWriter, serializer, option, basePath ?? Mod.ModPath); + jWriter.WriteEndObject(); } jWriter.WriteEndArray(); diff --git a/Penumbra/Mods/Groups/SingleModGroup.cs b/Penumbra/Mods/Groups/SingleModGroup.cs index 11542968..6aa9160e 100644 --- a/Penumbra/Mods/Groups/SingleModGroup.cs +++ b/Penumbra/Mods/Groups/SingleModGroup.cs @@ -139,13 +139,15 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup public void WriteJson(JsonTextWriter jWriter, JsonSerializer serializer, DirectoryInfo? basePath = null) { - IModGroup.WriteJsonBase(jWriter, this); + ModSaveGroup.WriteJsonBase(jWriter, this); jWriter.WritePropertyName("Options"); jWriter.WriteStartArray(); foreach (var option in OptionData) { + jWriter.WriteStartObject(); SubModHelpers.WriteModOption(jWriter, option); SubModHelpers.WriteModContainer(jWriter, serializer, option, basePath ?? Mod.ModPath); + jWriter.WriteEndObject(); } jWriter.WriteEndArray(); diff --git a/Penumbra/Mods/SubMods/DefaultSubMod.cs b/Penumbra/Mods/SubMods/DefaultSubMod.cs index 1eddcef6..980b805d 100644 --- a/Penumbra/Mods/SubMods/DefaultSubMod.cs +++ b/Penumbra/Mods/SubMods/DefaultSubMod.cs @@ -1,19 +1,19 @@ -using Penumbra.Meta.Manipulations; -using Penumbra.Mods.Editor; -using Penumbra.Mods.Groups; -using Penumbra.String.Classes; - -namespace Penumbra.Mods.SubMods; - +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; +using Penumbra.Mods.Groups; +using Penumbra.String.Classes; + +namespace Penumbra.Mods.SubMods; + public class DefaultSubMod(IMod mod) : IModDataContainer { public const string FullName = "Default Option"; internal readonly IMod Mod = mod; - public Dictionary Files { get; set; } = []; - public Dictionary FileSwaps { get; set; } = []; - public HashSet Manipulations { get; set; } = []; + public Dictionary Files { get; set; } = []; + public Dictionary FileSwaps { get; set; } = []; + public HashSet Manipulations { get; set; } = []; IMod IModDataContainer.Mod => Mod; From a72be22d3b295250716408fa0d01976f44a6e7b3 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 26 Apr 2024 10:56:06 +0200 Subject: [PATCH 135/433] Make sure HS image does not displace the settings entirely. --- Penumbra/UI/ModsTab/ModPanelHeader.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Penumbra/UI/ModsTab/ModPanelHeader.cs b/Penumbra/UI/ModsTab/ModPanelHeader.cs index 05f47809..a8b393b1 100644 --- a/Penumbra/UI/ModsTab/ModPanelHeader.cs +++ b/Penumbra/UI/ModsTab/ModPanelHeader.cs @@ -18,6 +18,7 @@ public class ModPanelHeader : IDisposable private readonly IFontHandle _nameFont; private readonly CommunicatorService _communicator; + private float _lastPreSettingsHeight = 0; public ModPanelHeader(DalamudPluginInterface pi, CommunicatorService communicator) { @@ -32,6 +33,11 @@ public class ModPanelHeader : IDisposable /// public void Draw() { + var height = ImGui.GetContentRegionAvail().Y; + var maxHeight = 3 * height / 4; + using var child = _lastPreSettingsHeight > maxHeight && _communicator.PreSettingsTabBarDraw.HasSubscribers + ? ImRaii.Child("HeaderChild", new Vector2(ImGui.GetContentRegionAvail().X, maxHeight), false) + : null; using (ImRaii.Group()) { var offset = DrawModName(); @@ -40,6 +46,7 @@ public class ModPanelHeader : IDisposable } _communicator.PreSettingsTabBarDraw.Invoke(_mod.Identifier, ImGui.GetItemRectSize().X, _nameWidth); + _lastPreSettingsHeight = ImGui.GetCursorPosY(); } /// @@ -48,6 +55,7 @@ public class ModPanelHeader : IDisposable /// public void UpdateModData(Mod mod) { + _lastPreSettingsHeight = 0; _mod = mod; // Name var name = $" {mod.Name} "; From e40c4999b6d18deab2bf98a60cd9cf5d1ac2b198 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 26 Apr 2024 10:56:36 +0200 Subject: [PATCH 136/433] Improve collection migration maybe. --- .../Collections/Manager/CollectionStorage.cs | 30 +++++++++++++++---- .../Manager/ModCollectionMigration.cs | 2 -- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Penumbra/Collections/Manager/CollectionStorage.cs b/Penumbra/Collections/Manager/CollectionStorage.cs index 4e2fb7b7..1fe5b227 100644 --- a/Penumbra/Collections/Manager/CollectionStorage.cs +++ b/Penumbra/Collections/Manager/CollectionStorage.cs @@ -184,21 +184,39 @@ public class CollectionStorage : IReadOnlyList, IDisposable { if (version >= 2) { - File.Move(file.FullName, correctName, false); - Penumbra.Messager.NotificationMessage($"Collection {file.Name} does not correspond to {collection.Identifier}, renamed.", - NotificationType.Warning); + try + { + File.Move(file.FullName, correctName, false); + Penumbra.Messager.NotificationMessage( + $"Collection {file.Name} does not correspond to {collection.Identifier}, renamed.", + NotificationType.Warning); + } + catch (Exception ex) + { + Penumbra.Messager.NotificationMessage( + $"Collection {file.Name} does not correspond to {collection.Identifier}, rename failed:\n{ex}", + NotificationType.Warning); + } } else { _saveService.ImmediateSaveSync(new ModCollectionSave(_modStorage, collection)); - File.Delete(file.FullName); - Penumbra.Log.Information($"Migrated collection {name} to Guid {id}."); + try + { + File.Move(file.FullName, file.FullName + ".bak", true); + Penumbra.Log.Information($"Migrated collection {name} to Guid {id} with backup of old file."); + } + catch (Exception ex) + { + Penumbra.Log.Information($"Migrated collection {name} to Guid {id}, rename of old file failed:\n{ex}"); + } } } catch (Exception e) { Penumbra.Messager.NotificationMessage(e, - $"Collection {file.Name} does not correspond to {collection.Identifier}, but could not rename.", NotificationType.Error); + $"Collection {file.Name} does not correspond to {collection.Identifier}, but could not rename.", + NotificationType.Error); } _collections.Add(collection); diff --git a/Penumbra/Collections/Manager/ModCollectionMigration.cs b/Penumbra/Collections/Manager/ModCollectionMigration.cs index 89743aa2..fe61285d 100644 --- a/Penumbra/Collections/Manager/ModCollectionMigration.cs +++ b/Penumbra/Collections/Manager/ModCollectionMigration.cs @@ -1,8 +1,6 @@ -using Penumbra.Mods; using Penumbra.Mods.Manager; using Penumbra.Mods.Settings; using Penumbra.Services; -using Penumbra.Util; namespace Penumbra.Collections.Manager; From 297be487b50066c4a79c4cff03af18fa452b0672 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 26 Apr 2024 10:57:09 +0200 Subject: [PATCH 137/433] More cleanup on groups. --- .../Communication/PreSettingsTabBarDraw.cs | 3 +- Penumbra/Mods/Groups/IModGroup.cs | 61 +++---------------- Penumbra/Mods/Groups/ModGroup.cs | 42 +++++++++++++ Penumbra/Mods/Groups/ModSaveGroup.cs | 2 +- Penumbra/Mods/Groups/MultiModGroup.cs | 6 +- Penumbra/Mods/Groups/SingleModGroup.cs | 6 +- Penumbra/Mods/Manager/ModOptionEditor.cs | 42 +++++-------- Penumbra/Mods/Manager/ModStorage.cs | 6 +- Penumbra/Mods/ModCreator.cs | 2 +- Penumbra/Mods/SubMods/DefaultSubMod.cs | 2 +- Penumbra/Mods/SubMods/MultiSubMod.cs | 8 +-- Penumbra/Mods/SubMods/OptionSubMod.cs | 2 +- Penumbra/Mods/SubMods/SingleSubMod.cs | 8 +-- .../SubMods/{SubModHelpers.cs => SubMod.cs} | 19 +++++- 14 files changed, 107 insertions(+), 102 deletions(-) create mode 100644 Penumbra/Mods/Groups/ModGroup.cs rename Penumbra/Mods/SubMods/{SubModHelpers.cs => SubMod.cs} (87%) diff --git a/Penumbra/Communication/PreSettingsTabBarDraw.cs b/Penumbra/Communication/PreSettingsTabBarDraw.cs index 8614bbbe..e1d67297 100644 --- a/Penumbra/Communication/PreSettingsTabBarDraw.cs +++ b/Penumbra/Communication/PreSettingsTabBarDraw.cs @@ -1,5 +1,6 @@ using OtterGui.Classes; using Penumbra.Api.Api; +using Penumbra.Api.IpcSubscribers; namespace Penumbra.Communication; @@ -15,7 +16,7 @@ public sealed class PreSettingsTabBarDraw() : EventWrapper + /// Default = 0, } } diff --git a/Penumbra/Mods/Groups/IModGroup.cs b/Penumbra/Mods/Groups/IModGroup.cs index 0cabc9f3..b13799cd 100644 --- a/Penumbra/Mods/Groups/IModGroup.cs +++ b/Penumbra/Mods/Groups/IModGroup.cs @@ -16,22 +16,22 @@ public interface IModGroup { public const int MaxMultiOptions = 63; - public Mod Mod { get; } - public string Name { get; } - public string Description { get; } - public GroupType Type { get; } - public ModPriority Priority { get; set; } - public Setting DefaultSettings { get; set; } + public Mod Mod { get; } + public string Name { get; } + public string Description { get; set; } + public GroupType Type { get; } + public ModPriority Priority { get; set; } + public Setting DefaultSettings { get; set; } public FullPath? FindBestMatch(Utf8GamePath gamePath); - public int AddOption(Mod mod, string name, string description = ""); + public int AddOption(Mod mod, string name, string description = ""); - public IReadOnlyList Options { get; } + public IReadOnlyList Options { get; } public IReadOnlyList DataContainers { get; } - public bool IsOption { get; } + public bool IsOption { get; } public IModGroup Convert(GroupType type); - public bool MoveOption(int optionIdxFrom, int optionIdxTo); + public bool MoveOption(int optionIdxFrom, int optionIdxTo); public int GetIndex(); @@ -42,46 +42,5 @@ public interface IModGroup public void WriteJson(JsonTextWriter jWriter, JsonSerializer serializer, DirectoryInfo? basePath = null); - public bool ChangeOptionDescription(int optionIndex, string newDescription) - { - if (optionIndex < 0 || optionIndex >= Options.Count) - return false; - - var option = Options[optionIndex]; - if (option.Description == newDescription) - return false; - - option.Description = newDescription; - return true; - } - - public bool ChangeOptionName(int optionIndex, string newName) - { - if (optionIndex < 0 || optionIndex >= Options.Count) - return false; - - var option = Options[optionIndex]; - if (option.Name == newName) - return false; - - option.Name = newName; - return true; - } - public (int Redirections, int Swaps, int Manips) GetCounts(); - - public static (int Redirections, int Swaps, int Manips) GetCountsBase(IModGroup group) - { - var redirectionCount = 0; - var swapCount = 0; - var manipCount = 0; - foreach (var option in group.DataContainers) - { - redirectionCount += option.Files.Count; - swapCount += option.FileSwaps.Count; - manipCount += option.Manipulations.Count; - } - - return (redirectionCount, swapCount, manipCount); - } } diff --git a/Penumbra/Mods/Groups/ModGroup.cs b/Penumbra/Mods/Groups/ModGroup.cs new file mode 100644 index 00000000..da302714 --- /dev/null +++ b/Penumbra/Mods/Groups/ModGroup.cs @@ -0,0 +1,42 @@ +using Penumbra.Api.Enums; +using Penumbra.Mods.Settings; + +namespace Penumbra.Mods.Groups; + +public static class ModGroup +{ + public static IModGroup Create(Mod mod, GroupType type, string name) + { + var maxPriority = mod.Groups.Count == 0 ? ModPriority.Default : mod.Groups.Max(o => o.Priority) + 1; + return type switch + { + GroupType.Single => new SingleModGroup(mod) + { + Name = name, + Priority = maxPriority, + }, + GroupType.Multi => new MultiModGroup(mod) + { + Name = name, + Priority = maxPriority, + }, + _ => throw new ArgumentOutOfRangeException(nameof(type), type, null), + }; + } + + + public static (int Redirections, int Swaps, int Manips) GetCountsBase(IModGroup group) + { + var redirectionCount = 0; + var swapCount = 0; + var manipCount = 0; + foreach (var option in group.DataContainers) + { + redirectionCount += option.Files.Count; + swapCount += option.FileSwaps.Count; + manipCount += option.Manipulations.Count; + } + + return (redirectionCount, swapCount, manipCount); + } +} diff --git a/Penumbra/Mods/Groups/ModSaveGroup.cs b/Penumbra/Mods/Groups/ModSaveGroup.cs index 92ccb36e..332879cb 100644 --- a/Penumbra/Mods/Groups/ModSaveGroup.cs +++ b/Penumbra/Mods/Groups/ModSaveGroup.cs @@ -51,7 +51,7 @@ public readonly struct ModSaveGroup : ISavable if (_groupIdx >= 0) _group!.WriteJson(j, serializer); else - SubModHelpers.WriteModContainer(j, serializer, _defaultMod!, _basePath); + SubMod.WriteModContainer(j, serializer, _defaultMod!, _basePath); j.WriteEndObject(); } diff --git a/Penumbra/Mods/Groups/MultiModGroup.cs b/Penumbra/Mods/Groups/MultiModGroup.cs index 7e900ef5..6b352f66 100644 --- a/Penumbra/Mods/Groups/MultiModGroup.cs +++ b/Penumbra/Mods/Groups/MultiModGroup.cs @@ -141,10 +141,10 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup foreach (var option in OptionData) { jWriter.WriteStartObject(); - SubModHelpers.WriteModOption(jWriter, option); + SubMod.WriteModOption(jWriter, option); jWriter.WritePropertyName(nameof(option.Priority)); jWriter.WriteValue(option.Priority.Value); - SubModHelpers.WriteModContainer(jWriter, serializer, option, basePath ?? Mod.ModPath); + SubMod.WriteModContainer(jWriter, serializer, option, basePath ?? Mod.ModPath); jWriter.WriteEndObject(); } @@ -153,7 +153,7 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup } public (int Redirections, int Swaps, int Manips) GetCounts() - => IModGroup.GetCountsBase(this); + => ModGroup.GetCountsBase(this); public Setting FixSetting(Setting setting) => new(setting.Value & (1ul << OptionData.Count) - 1); diff --git a/Penumbra/Mods/Groups/SingleModGroup.cs b/Penumbra/Mods/Groups/SingleModGroup.cs index 6aa9160e..ac85e2bc 100644 --- a/Penumbra/Mods/Groups/SingleModGroup.cs +++ b/Penumbra/Mods/Groups/SingleModGroup.cs @@ -135,7 +135,7 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup => OptionData.Count == 0 ? Setting.Zero : new Setting(Math.Min(setting.Value, (ulong)(OptionData.Count - 1))); public (int Redirections, int Swaps, int Manips) GetCounts() - => IModGroup.GetCountsBase(this); + => ModGroup.GetCountsBase(this); public void WriteJson(JsonTextWriter jWriter, JsonSerializer serializer, DirectoryInfo? basePath = null) { @@ -145,8 +145,8 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup foreach (var option in OptionData) { jWriter.WriteStartObject(); - SubModHelpers.WriteModOption(jWriter, option); - SubModHelpers.WriteModContainer(jWriter, serializer, option, basePath ?? Mod.ModPath); + SubMod.WriteModOption(jWriter, option); + SubMod.WriteModContainer(jWriter, serializer, option, basePath ?? Mod.ModPath); jWriter.WriteEndObject(); } diff --git a/Penumbra/Mods/Manager/ModOptionEditor.cs b/Penumbra/Mods/Manager/ModOptionEditor.cs index a02c8d68..c6122ea8 100644 --- a/Penumbra/Mods/Manager/ModOptionEditor.cs +++ b/Penumbra/Mods/Manager/ModOptionEditor.cs @@ -68,7 +68,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS return; saveService.ImmediateDelete(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - var _ = group switch + _ = group switch { SingleModGroup s => s.Name = newName, MultiModGroup m => m.Name = newName, @@ -85,21 +85,11 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS if (!VerifyFileName(mod, null, newName, true)) return; - var maxPriority = mod.Groups.Count == 0 ? ModPriority.Default : mod.Groups.Max(o => o.Priority) + 1; - - mod.Groups.Add(type == GroupType.Multi - ? new MultiModGroup(mod) - { - Name = newName, - Priority = maxPriority, - } - : new SingleModGroup(mod) - { - Name = newName, - Priority = maxPriority, - }); - saveService.Save(saveType, new ModSaveGroup(mod, mod.Groups.Count - 1, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupAdded, mod, mod.Groups.Count - 1, -1, -1); + var idx = mod.Groups.Count; + var group = ModGroup.Create(mod, type, newName); + mod.Groups.Add(group); + saveService.Save(saveType, new ModSaveGroup(mod, idx, config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupAdded, mod, idx, -1, -1); } /// Add a new mod, empty option group of the given type and name if it does not exist already. @@ -142,12 +132,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS if (group.Description == newDescription) return; - var _ = group switch - { - SingleModGroup s => s.Description = newDescription, - MultiModGroup m => m.Description = newDescription, - _ => newDescription, - }; + group.Description = newDescription; saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, mod, groupIdx, -1, -1); } @@ -155,9 +140,11 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS /// Change the description of the given option. public void ChangeOptionDescription(Mod mod, int groupIdx, int optionIdx, string newDescription) { - if (!mod.Groups[groupIdx].ChangeOptionDescription(optionIdx, newDescription)) + var option = mod.Groups[groupIdx].Options[optionIdx]; + if (option.Description == newDescription) return; + option.Description = newDescription; saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, mod, groupIdx, optionIdx, -1); } @@ -193,9 +180,12 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS /// Rename the given option. public void RenameOption(Mod mod, int groupIdx, int optionIdx, string newName) { - if (!mod.Groups[groupIdx].ChangeOptionName(optionIdx, newName)) + var option = mod.Groups[groupIdx].Options[optionIdx]; + if (option.Name == newName) return; + option.Name = newName; + saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, mod, groupIdx, optionIdx, -1); } @@ -251,7 +241,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS Description = option.Description, }; if (option is IModDataContainer data) - SubModHelpers.Clone(data, newOption); + SubMod.Clone(data, newOption); s.OptionData.Add(newOption); break; } @@ -265,7 +255,7 @@ public class ModOptionEditor(CommunicatorService communicator, SaveService saveS Priority = option is MultiSubMod s ? s.Priority : ModPriority.Default, }; if (option is IModDataContainer data) - SubModHelpers.Clone(data, newOption); + SubMod.Clone(data, newOption); m.OptionData.Add(newOption); break; } diff --git a/Penumbra/Mods/Manager/ModStorage.cs b/Penumbra/Mods/Manager/ModStorage.cs index 65b8ddd9..acb2c1ab 100644 --- a/Penumbra/Mods/Manager/ModStorage.cs +++ b/Penumbra/Mods/Manager/ModStorage.cs @@ -3,17 +3,13 @@ using OtterGui.Widgets; namespace Penumbra.Mods.Manager; -public class ModCombo : FilterComboCache +public class ModCombo(Func> generator) : FilterComboCache(generator, MouseWheelType.None, Penumbra.Log) { protected override bool IsVisible(int globalIndex, LowerString filter) => Items[globalIndex].Name.Contains(filter); protected override string ToString(Mod obj) => obj.Name.Text; - - public ModCombo(Func> generator) - : base(generator, MouseWheelType.None, Penumbra.Log) - { } } public class ModStorage : IReadOnlyList diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index 0626bc9d..40f943c8 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -115,7 +115,7 @@ public partial class ModCreator( try { var jObject = File.Exists(defaultFile) ? JObject.Parse(File.ReadAllText(defaultFile)) : new JObject(); - SubModHelpers.LoadDataContainer(jObject, mod.Default, mod.ModPath); + SubMod.LoadDataContainer(jObject, mod.Default, mod.ModPath); } catch (Exception e) { diff --git a/Penumbra/Mods/SubMods/DefaultSubMod.cs b/Penumbra/Mods/SubMods/DefaultSubMod.cs index 980b805d..1a234879 100644 --- a/Penumbra/Mods/SubMods/DefaultSubMod.cs +++ b/Penumbra/Mods/SubMods/DefaultSubMod.cs @@ -22,7 +22,7 @@ public class DefaultSubMod(IMod mod) : IModDataContainer => null; public void AddTo(Dictionary redirections, HashSet manipulations) - => SubModHelpers.AddContainerTo(this, redirections, manipulations); + => SubMod.AddContainerTo(this, redirections, manipulations); public string GetName() => FullName; diff --git a/Penumbra/Mods/SubMods/MultiSubMod.cs b/Penumbra/Mods/SubMods/MultiSubMod.cs index c43d4b9e..3bcaffab 100644 --- a/Penumbra/Mods/SubMods/MultiSubMod.cs +++ b/Penumbra/Mods/SubMods/MultiSubMod.cs @@ -11,8 +11,8 @@ public class MultiSubMod(Mod mod, MultiModGroup group) : OptionSubMod() ?? ModPriority.Default; } @@ -24,7 +24,7 @@ public class MultiSubMod(Mod mod, MultiModGroup group) : OptionSubMod(Mod mod, T group) : IModDataOption public HashSet Manipulations { get; set; } = []; public void AddDataTo(Dictionary redirections, HashSet manipulations) - => SubModHelpers.AddContainerTo(this, redirections, manipulations); + => SubMod.AddContainerTo(this, redirections, manipulations); public string GetName() => Name; diff --git a/Penumbra/Mods/SubMods/SingleSubMod.cs b/Penumbra/Mods/SubMods/SingleSubMod.cs index 5d68e401..98c56151 100644 --- a/Penumbra/Mods/SubMods/SingleSubMod.cs +++ b/Penumbra/Mods/SubMods/SingleSubMod.cs @@ -9,8 +9,8 @@ public class SingleSubMod(Mod mod, SingleModGroup singleGroup) : OptionSubMod group switch + { + SingleModGroup single => new SingleSubMod(group.Mod, single) + { + Name = name, + Description = description, + }, + MultiModGroup multi => new MultiSubMod(group.Mod, multi) + { + Name = name, + Description = description, + }, + _ => throw new ArgumentOutOfRangeException(nameof(group)), + }; + /// Add all unique meta manipulations, file redirections and then file swaps from a ModDataContainer to the given sets. Skip any keys that are already contained. public static void AddContainerTo(IModDataContainer container, Dictionary redirections, HashSet manipulations) From 616db0dcc3a43fae6faeadde80260fa625d71cc9 Mon Sep 17 00:00:00 2001 From: ackwell Date: Fri, 26 Apr 2024 21:23:31 +1000 Subject: [PATCH 138/433] Add mesh vertex element readout --- Penumbra/Import/Models/Export/MeshExporter.cs | 1 - .../UI/AdvancedWindow/ModEditWindow.Models.cs | 44 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Penumbra/Import/Models/Export/MeshExporter.cs b/Penumbra/Import/Models/Export/MeshExporter.cs index 19b06d55..219a046e 100644 --- a/Penumbra/Import/Models/Export/MeshExporter.cs +++ b/Penumbra/Import/Models/Export/MeshExporter.cs @@ -1,5 +1,4 @@ using System.Collections.Immutable; -using Lumina.Data.Parsing; using Lumina.Extensions; using OtterGui; using Penumbra.GameData.Files; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index 39924021..03b5169a 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -1,6 +1,7 @@ using Dalamud.Interface; using Dalamud.Interface.Components; using ImGuiNET; +using Lumina.Data.Parsing; using OtterGui; using OtterGui.Custom; using OtterGui.Raii; @@ -8,7 +9,6 @@ using OtterGui.Widgets; using Penumbra.GameData; using Penumbra.GameData.Files; using Penumbra.Import.Models; -using Penumbra.Mods; using Penumbra.String.Classes; using Penumbra.UI.Classes; @@ -421,6 +421,14 @@ public partial class ModEditWindow var file = tab.Mdl; var mesh = file.Meshes[meshIndex]; + // Vertex elements + ImGui.TableNextColumn(); + ImGui.AlignTextToFramePadding(); + ImGui.TextUnformatted("Vertex Elements"); + + ImGui.TableNextColumn(); + DrawVertexElementDetails(file.VertexDeclarations[meshIndex].VertexElements); + // Mesh material ImGui.TableNextColumn(); ImGui.AlignTextToFramePadding(); @@ -436,6 +444,40 @@ public partial class ModEditWindow return ret; } + private static void DrawVertexElementDetails(MdlStructs.VertexElement[] vertexElements) + { + using var node = ImRaii.TreeNode($"Click to expand"); + if (!node) + return; + + var flags = ImGuiTableFlags.SizingFixedFit + | ImGuiTableFlags.RowBg + | ImGuiTableFlags.Borders + | ImGuiTableFlags.NoHostExtendX; + using var table = ImRaii.Table(string.Empty, 4, flags); + if (!table) + return; + + ImGui.TableSetupColumn("Usage"); + ImGui.TableSetupColumn("Type"); + ImGui.TableSetupColumn("Stream"); + ImGui.TableSetupColumn("Offset"); + + ImGui.TableHeadersRow(); + + foreach (var element in vertexElements) + { + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{(MdlFile.VertexUsage)element.Usage}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{(MdlFile.VertexType)element.Type}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{element.Stream}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{element.Offset}"); + } + } + private static bool DrawMaterialCombo(MdlTab tab, int meshIndex, bool disabled) { var mesh = tab.Mdl.Meshes[meshIndex]; From 1e5ed1c41450ba0d8b3b5dd85a8571ee49b6dd71 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 26 Apr 2024 18:43:45 +0200 Subject: [PATCH 139/433] Now that was a lot of work. --- Penumbra.Api | 2 +- Penumbra/Api/Api/ModSettingsApi.cs | 3 +- .../Cache/CollectionCacheManager.cs | 4 +- .../Collections/Manager/CollectionStorage.cs | 6 +- Penumbra/Communication/ModOptionChanged.cs | 17 +- Penumbra/Import/TexToolsImporter.ModPack.cs | 2 +- Penumbra/Mods/Editor/DuplicateManager.cs | 13 +- Penumbra/Mods/Editor/ModEditor.cs | 46 +- Penumbra/Mods/Editor/ModFileEditor.cs | 9 +- Penumbra/Mods/Editor/ModMerger.cs | 69 +-- Penumbra/Mods/Editor/ModMetaEditor.cs | 4 +- Penumbra/Mods/Editor/ModNormalizer.cs | 8 +- Penumbra/Mods/Editor/ModSwapEditor.cs | 4 +- Penumbra/Mods/Groups/IModGroup.cs | 11 +- Penumbra/Mods/Groups/ImcModGroup.cs | 158 +++++++ Penumbra/Mods/Groups/ModGroup.cs | 15 + Penumbra/Mods/Groups/ModSaveGroup.cs | 65 ++- Penumbra/Mods/Groups/MultiModGroup.cs | 78 ++-- Penumbra/Mods/Groups/SingleModGroup.cs | 91 ++-- Penumbra/Mods/ItemSwap/ItemSwapContainer.cs | 11 +- Penumbra/Mods/Manager/ImcModGroupEditor.cs | 38 ++ Penumbra/Mods/Manager/ModCacheManager.cs | 4 +- Penumbra/Mods/Manager/ModGroupEditor.cs | 289 +++++++++++++ Penumbra/Mods/Manager/ModManager.cs | 9 +- Penumbra/Mods/Manager/ModMigration.cs | 16 +- Penumbra/Mods/Manager/ModOptionEditor.cs | 392 +++--------------- Penumbra/Mods/Manager/MultiModGroupEditor.cs | 84 ++++ Penumbra/Mods/Manager/SingleModGroupEditor.cs | 57 +++ Penumbra/Mods/ModCreator.cs | 20 +- Penumbra/Mods/Settings/ModSettings.cs | 44 +- Penumbra/Mods/Settings/Setting.cs | 28 ++ Penumbra/Mods/SubMods/IModOption.cs | 7 +- Penumbra/Mods/SubMods/ImcSubMod.cs | 32 ++ Penumbra/Mods/SubMods/MultiSubMod.cs | 20 +- Penumbra/Mods/SubMods/OptionSubMod.cs | 47 ++- Penumbra/Mods/SubMods/SingleSubMod.cs | 16 +- Penumbra/Mods/SubMods/SubMod.cs | 32 +- Penumbra/Penumbra.csproj | 10 +- Penumbra/Services/SaveService.cs | 9 +- Penumbra/Services/StaticServiceManager.cs | 1 - Penumbra/UI/AdvancedWindow/ItemSwapTab.cs | 35 +- .../UI/AdvancedWindow/ModEditWindow.Meta.cs | 6 +- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 4 +- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 132 +++--- 44 files changed, 1182 insertions(+), 766 deletions(-) create mode 100644 Penumbra/Mods/Groups/ImcModGroup.cs create mode 100644 Penumbra/Mods/Manager/ImcModGroupEditor.cs create mode 100644 Penumbra/Mods/Manager/ModGroupEditor.cs create mode 100644 Penumbra/Mods/Manager/MultiModGroupEditor.cs create mode 100644 Penumbra/Mods/Manager/SingleModGroupEditor.cs create mode 100644 Penumbra/Mods/SubMods/ImcSubMod.cs diff --git a/Penumbra.Api b/Penumbra.Api index 590629df..69d106b4 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit 590629df33f9ad92baddd1d65ec8c986f18d608a +Subproject commit 69d106b457eb0f73d4b4caf1234da5631fd6fbf0 diff --git a/Penumbra/Api/Api/ModSettingsApi.cs b/Penumbra/Api/Api/ModSettingsApi.cs index 039fbfa9..bfd134bb 100644 --- a/Penumbra/Api/Api/ModSettingsApi.cs +++ b/Penumbra/Api/Api/ModSettingsApi.cs @@ -11,6 +11,7 @@ using Penumbra.Mods.Editor; using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; using Penumbra.Services; namespace Penumbra.Api.Api; @@ -254,7 +255,7 @@ public class ModSettingsApi : IPenumbraApiModSettings, IApiService, IDisposable private void OnModSettingChange(ModCollection collection, ModSettingChange type, Mod? mod, Setting _1, int _2, bool inherited) => ModSettingChanged?.Invoke(type, collection.Id, mod?.ModPath.Name ?? string.Empty, inherited); - private void OnModOptionEdited(ModOptionChangeType type, Mod mod, int groupIndex, int optionIndex, int moveIndex) + private void OnModOptionEdited(ModOptionChangeType type, Mod mod, IModGroup? group, IModOption? option, IModDataContainer? container, int moveIndex) { switch (type) { diff --git a/Penumbra/Collections/Cache/CollectionCacheManager.cs b/Penumbra/Collections/Cache/CollectionCacheManager.cs index c1296414..9c104cef 100644 --- a/Penumbra/Collections/Cache/CollectionCacheManager.cs +++ b/Penumbra/Collections/Cache/CollectionCacheManager.cs @@ -7,8 +7,10 @@ using Penumbra.Communication; using Penumbra.Interop.ResourceLoading; using Penumbra.Meta; using Penumbra.Mods; +using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.String.Classes; @@ -257,7 +259,7 @@ public class CollectionCacheManager : IDisposable } /// Prepare Changes by removing mods from caches with collections or add or reload mods. - private void OnModOptionChange(ModOptionChangeType type, Mod mod, int groupIdx, int optionIdx, int movedToIdx) + private void OnModOptionChange(ModOptionChangeType type, Mod mod, IModGroup? group, IModOption? option, IModDataContainer? container, int movedToIdx) { if (type is ModOptionChangeType.PrepareChange) { diff --git a/Penumbra/Collections/Manager/CollectionStorage.cs b/Penumbra/Collections/Manager/CollectionStorage.cs index 1fe5b227..bfae2dc0 100644 --- a/Penumbra/Collections/Manager/CollectionStorage.cs +++ b/Penumbra/Collections/Manager/CollectionStorage.cs @@ -4,8 +4,10 @@ using OtterGui.Classes; using Penumbra.Communication; using Penumbra.Mods; using Penumbra.Mods.Editor; +using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; using Penumbra.Services; namespace Penumbra.Collections.Manager; @@ -290,7 +292,7 @@ public class CollectionStorage : IReadOnlyList, IDisposable } /// Save all collections where the mod has settings and the change requires saving. - private void OnModOptionChange(ModOptionChangeType type, Mod mod, int groupIdx, int optionIdx, int movedToIdx) + private void OnModOptionChange(ModOptionChangeType type, Mod mod, IModGroup? group, IModOption? option, IModDataContainer? container, int movedToIdx) { type.HandlingInfo(out var requiresSaving, out _, out _); if (!requiresSaving) @@ -298,7 +300,7 @@ public class CollectionStorage : IReadOnlyList, IDisposable foreach (var collection in this) { - if (collection.Settings[mod.Index]?.HandleChanges(type, mod, groupIdx, optionIdx, movedToIdx) ?? false) + if (collection.Settings[mod.Index]?.HandleChanges(type, mod, group, option, movedToIdx) ?? false) _saveService.QueueSave(new ModCollectionSave(_modStorage, collection)); } } diff --git a/Penumbra/Communication/ModOptionChanged.cs b/Penumbra/Communication/ModOptionChanged.cs index 0df58b5f..a20592ec 100644 --- a/Penumbra/Communication/ModOptionChanged.cs +++ b/Penumbra/Communication/ModOptionChanged.cs @@ -1,8 +1,10 @@ using OtterGui.Classes; -using Penumbra.Api; using Penumbra.Api.Api; using Penumbra.Mods; +using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; +using Penumbra.Mods.SubMods; +using static Penumbra.Communication.ModOptionChanged; namespace Penumbra.Communication; @@ -11,22 +13,23 @@ namespace Penumbra.Communication; /// /// Parameter is the type option change. /// Parameter is the changed mod. -/// Parameter is the index of the changed group inside the mod. -/// Parameter is the index of the changed option inside the group or -1 if it does not concern a specific option. -/// Parameter is the index of the group an option was moved to. +/// Parameter is the changed group inside the mod. +/// Parameter is the changed option inside the group or null if it does not concern a specific option. +/// Parameter is the changed data container inside the group or null if it does not concern a specific data container. +/// Parameter is the index of the group or option moved or deleted from. /// public sealed class ModOptionChanged() - : EventWrapper(nameof(ModOptionChanged)) + : EventWrapper(nameof(ModOptionChanged)) { public enum Priority { - /// + /// Api = int.MinValue, /// CollectionCacheManager = -100, - /// + /// ModCacheManager = 0, /// diff --git a/Penumbra/Import/TexToolsImporter.ModPack.cs b/Penumbra/Import/TexToolsImporter.ModPack.cs index eb6d0b0c..2b45ecbe 100644 --- a/Penumbra/Import/TexToolsImporter.ModPack.cs +++ b/Penumbra/Import/TexToolsImporter.ModPack.cs @@ -205,7 +205,7 @@ public partial class TexToolsImporter { var option = group.OptionList[idx]; _currentOptionName = option.Name; - options.Insert(idx, MultiSubMod.CreateForSaving(option.Name, option.Description, ModPriority.Default)); + options.Insert(idx, MultiSubMod.WithoutGroup(option.Name, option.Description, ModPriority.Default)); if (option.IsChecked) defaultSettings = Setting.Single(idx); } diff --git a/Penumbra/Mods/Editor/DuplicateManager.cs b/Penumbra/Mods/Editor/DuplicateManager.cs index 31aacbe1..84a832a2 100644 --- a/Penumbra/Mods/Editor/DuplicateManager.cs +++ b/Penumbra/Mods/Editor/DuplicateManager.cs @@ -60,7 +60,7 @@ public class DuplicateManager(ModManager modManager, SaveService saveService, Co private void HandleDuplicate(Mod mod, FullPath duplicate, FullPath remaining, bool useModManager) { - ModEditor.ApplyToAllOptions(mod, HandleSubMod); + ModEditor.ApplyToAllContainers(mod, HandleSubMod); try { @@ -73,7 +73,7 @@ public class DuplicateManager(ModManager modManager, SaveService saveService, Co return; - void HandleSubMod(IModDataContainer subMod, int groupIdx, int optionIdx) + void HandleSubMod(IModDataContainer subMod) { var changes = false; var dict = subMod.Files.ToDictionary(kvp => kvp.Key, @@ -82,14 +82,9 @@ public class DuplicateManager(ModManager modManager, SaveService saveService, Co return; if (useModManager) - { - modManager.OptionEditor.OptionSetFiles(mod, groupIdx, optionIdx, dict, SaveType.ImmediateSync); - } + modManager.OptionEditor.SetFiles(subMod, dict, SaveType.ImmediateSync); else - { - subMod.Files = dict; - saveService.ImmediateSaveSync(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - } + saveService.ImmediateSaveSync(new ModSaveGroup(mod.ModPath, subMod, config.ReplaceNonAsciiOnImport)); } } diff --git a/Penumbra/Mods/Editor/ModEditor.cs b/Penumbra/Mods/Editor/ModEditor.cs index e1c5962f..37524da1 100644 --- a/Penumbra/Mods/Editor/ModEditor.cs +++ b/Penumbra/Mods/Editor/ModEditor.cs @@ -29,8 +29,8 @@ public class ModEditor( public int GroupIdx { get; private set; } public int DataIdx { get; private set; } - public IModGroup? Group { get; private set; } - public IModDataContainer? Option { get; private set; } + public IModGroup? Group { get; private set; } + public IModDataContainer? Option { get; private set; } public void LoadMod(Mod mod) => LoadMod(mod, -1, 0); @@ -63,10 +63,10 @@ public class ModEditor( { if (groupIdx == -1 && dataIdx == 0) { - Group = null; - Option = Mod.Default; - GroupIdx = groupIdx; - DataIdx = dataIdx; + Group = null; + Option = Mod.Default; + GroupIdx = groupIdx; + DataIdx = dataIdx; return; } @@ -75,18 +75,18 @@ public class ModEditor( Group = Mod.Groups[groupIdx]; if (dataIdx >= 0 && dataIdx < Group.DataContainers.Count) { - Option = Group.DataContainers[dataIdx]; - GroupIdx = groupIdx; - DataIdx = dataIdx; + Option = Group.DataContainers[dataIdx]; + GroupIdx = groupIdx; + DataIdx = dataIdx; return; } } } - Group = null; - Option = Mod?.Default; - GroupIdx = -1; - DataIdx = 0; + Group = null; + Option = Mod?.Default; + GroupIdx = -1; + DataIdx = 0; if (message) Penumbra.Log.Error($"Loading invalid option {groupIdx} {dataIdx} for Mod {Mod?.Name ?? "Unknown"}."); } @@ -105,23 +105,11 @@ public class ModEditor( => Clear(); /// Apply a option action to all available option in a mod, including the default option. - public static void ApplyToAllOptions(Mod mod, Action action) + public static void ApplyToAllContainers(Mod mod, Action action) { - action(mod.Default, -1, 0); - foreach (var (group, groupIdx) in mod.Groups.WithIndex()) - { - switch (group) - { - case SingleModGroup single: - for (var optionIdx = 0; optionIdx < single.OptionData.Count; ++optionIdx) - action(single.OptionData[optionIdx], groupIdx, optionIdx); - break; - case MultiModGroup multi: - for (var optionIdx = 0; optionIdx < multi.OptionData.Count; ++optionIdx) - action(multi.OptionData[optionIdx], groupIdx, optionIdx); - break; - } - } + action(mod.Default); + foreach (var container in mod.Groups.SelectMany(g => g.DataContainers)) + action(container); } // Does not delete the base directory itself even if it is completely empty at the end. diff --git a/Penumbra/Mods/Editor/ModFileEditor.cs b/Penumbra/Mods/Editor/ModFileEditor.cs index 00685c94..e2c0b726 100644 --- a/Penumbra/Mods/Editor/ModFileEditor.cs +++ b/Penumbra/Mods/Editor/ModFileEditor.cs @@ -24,8 +24,7 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu num += dict.TryAdd(path.Item2, file.File) ? 0 : 1; } - var (groupIdx, dataIdx) = option.GetDataIndices(); - modManager.OptionEditor.OptionSetFiles(mod, groupIdx, dataIdx, dict); + modManager.OptionEditor.SetFiles(option, dict); files.UpdatePaths(mod, option); Changes = false; return num; @@ -40,15 +39,15 @@ public class ModFileEditor(ModFileCollection files, ModManager modManager, Commu /// Remove all path redirections where the pointed-to file does not exist. public void RemoveMissingPaths(Mod mod, IModDataContainer option) { - void HandleSubMod(IModDataContainer subMod, int groupIdx, int optionIdx) + void HandleSubMod(IModDataContainer subMod) { var newDict = subMod.Files.Where(kvp => CheckAgainstMissing(mod, subMod, kvp.Value, kvp.Key, subMod == option)) .ToDictionary(kvp => kvp.Key, kvp => kvp.Value); if (newDict.Count != subMod.Files.Count) - modManager.OptionEditor.OptionSetFiles(mod, groupIdx, optionIdx, newDict); + modManager.OptionEditor.SetFiles(subMod, newDict); } - ModEditor.ApplyToAllOptions(mod, HandleSubMod); + ModEditor.ApplyToAllContainers(mod, HandleSubMod); files.ClearMissingFiles(); } diff --git a/Penumbra/Mods/Editor/ModMerger.cs b/Penumbra/Mods/Editor/ModMerger.cs index 0f629bc7..3a6f4a81 100644 --- a/Penumbra/Mods/Editor/ModMerger.cs +++ b/Penumbra/Mods/Editor/ModMerger.cs @@ -16,7 +16,7 @@ public class ModMerger : IDisposable { private readonly Configuration _config; private readonly CommunicatorService _communicator; - private readonly ModOptionEditor _editor; + private readonly ModGroupEditor _editor; private readonly ModFileSystemSelector _selector; private readonly DuplicateManager _duplicates; private readonly ModManager _mods; @@ -32,14 +32,14 @@ public class ModMerger : IDisposable private readonly Dictionary _fileToFile = []; private readonly HashSet _createdDirectories = []; private readonly HashSet _createdGroups = []; - private readonly HashSet _createdOptions = []; + private readonly HashSet _createdOptions = []; public readonly HashSet SelectedOptions = []; public readonly IReadOnlyList Warnings = []; public Exception? Error { get; private set; } - public ModMerger(ModManager mods, ModOptionEditor editor, ModFileSystemSelector selector, DuplicateManager duplicates, + public ModMerger(ModManager mods, ModGroupEditor editor, ModFileSystemSelector selector, DuplicateManager duplicates, CommunicatorService communicator, ModCreator creator, Configuration config) { _editor = editor; @@ -100,22 +100,23 @@ public class ModMerger : IDisposable var (group, groupIdx, groupCreated) = _editor.FindOrAddModGroup(MergeToMod!, originalGroup.Type, originalGroup.Name); if (groupCreated) _createdGroups.Add(groupIdx); - if (group.Type != originalGroup.Type) - ((List)Warnings).Add( - $"The merged group {group.Name} already existed, but has a different type {group.Type} than the original group of type {originalGroup.Type}."); + if (group == null) + throw new Exception( + $"The merged group {originalGroup.Name} already existed, but had a different type than the original group of type {originalGroup.Type}."); foreach (var originalOption in group.DataContainers) { - var (option, _, optionCreated) = _editor.FindOrAddOption(MergeToMod!, groupIdx, originalOption.GetName()); + var (option, _, optionCreated) = _editor.FindOrAddOption(group, originalOption.GetName()); if (optionCreated) { - _createdOptions.Add((IModDataOption)option); - MergeIntoOption([originalOption], (IModDataOption)option, false); + _createdOptions.Add(option!); + // #TODO DataContainer <> Option. + MergeIntoOption([originalOption], (IModDataContainer)option!, false); } else { throw new Exception( - $"Could not merge {MergeFromMod!.Name} into {MergeToMod!.Name}: The option {option.FullName} already existed."); + $"Could not merge {MergeFromMod!.Name} into {MergeToMod!.Name}: The option {option!.FullName} already existed."); } } } @@ -138,9 +139,9 @@ public class ModMerger : IDisposable var (group, groupIdx, groupCreated) = _editor.FindOrAddModGroup(MergeToMod!, GroupType.Multi, groupName, SaveType.None); if (groupCreated) _createdGroups.Add(groupIdx); - var (option, _, optionCreated) = _editor.FindOrAddOption(MergeToMod!, groupIdx, optionName, SaveType.None); + var (option, _, optionCreated) = _editor.FindOrAddOption(group!, optionName, SaveType.None); if (optionCreated) - _createdOptions.Add((IModDataOption)option); + _createdOptions.Add(option!); var dir = ModCreator.NewOptionDirectory(MergeToMod!.ModPath, groupName, _config.ReplaceNonAsciiOnImport); if (!dir.Exists) _createdDirectories.Add(dir.FullName); @@ -148,7 +149,8 @@ public class ModMerger : IDisposable if (!dir.Exists) _createdDirectories.Add(dir.FullName); CopyFiles(dir); - MergeIntoOption(MergeFromMod!.AllDataContainers.Reverse(), (IModDataOption)option, true); + // #TODO DataContainer <> Option. + MergeIntoOption(MergeFromMod!.AllDataContainers.Reverse(), (IModDataContainer)option!, true); } private void MergeIntoOption(IEnumerable mergeOptions, IModDataContainer option, bool fromFileToFile) @@ -184,10 +186,9 @@ public class ModMerger : IDisposable } } - var (groupIdx, dataIdx) = option.GetDataIndices(); - _editor.OptionSetFiles(MergeToMod!, groupIdx, dataIdx, redirections, SaveType.None); - _editor.OptionSetFileSwaps(MergeToMod!, groupIdx, dataIdx, swaps, SaveType.None); - _editor.OptionSetManipulations(MergeToMod!, groupIdx, dataIdx, manips, SaveType.ImmediateSync); + _editor.SetFiles(option, redirections, SaveType.None); + _editor.SetFileSwaps(option, swaps, SaveType.None); + _editor.SetManipulations(option, manips, SaveType.ImmediateSync); return; bool GetFullPath(FullPath input, out FullPath ret) @@ -261,30 +262,31 @@ public class ModMerger : IDisposable if (mods.Count == 1) { var files = CopySubModFiles(mods[0], dir); - _editor.OptionSetFiles(result, -1, 0, files); - _editor.OptionSetFileSwaps(result, -1, 0, mods[0].FileSwaps); - _editor.OptionSetManipulations(result, -1, 0, mods[0].Manipulations); + _editor.SetFiles(result.Default, files); + _editor.SetFileSwaps(result.Default, mods[0].FileSwaps); + _editor.SetManipulations(result.Default, mods[0].Manipulations); } else { foreach (var originalOption in mods) { - if (originalOption.Group is not {} originalGroup) + if (originalOption.Group is not { } originalGroup) { var files = CopySubModFiles(mods[0], dir); - _editor.OptionSetFiles(result, -1, 0, files); - _editor.OptionSetFileSwaps(result, -1, 0, mods[0].FileSwaps); - _editor.OptionSetManipulations(result, -1, 0, mods[0].Manipulations); + _editor.SetFiles(result.Default, files); + _editor.SetFileSwaps(result.Default, mods[0].FileSwaps); + _editor.SetManipulations(result.Default, mods[0].Manipulations); } else { - var (group, groupIdx, _) = _editor.FindOrAddModGroup(result, originalGroup.Type, originalGroup.Name); - var (option, optionIdx, _) = _editor.FindOrAddOption(result, groupIdx, originalOption.GetName()); - var folder = Path.Combine(dir.FullName, group.Name, option.Name); + // TODO DataContainer <> Option. + var (group, _, _) = _editor.FindOrAddModGroup(result, originalGroup.Type, originalGroup.Name); + var (option, _, _) = _editor.FindOrAddOption(group!, originalOption.GetName()); + var folder = Path.Combine(dir.FullName, group!.Name, option!.Name); var files = CopySubModFiles(originalOption, new DirectoryInfo(folder)); - _editor.OptionSetFiles(result, groupIdx, optionIdx, files); - _editor.OptionSetFileSwaps(result, groupIdx, optionIdx, originalOption.FileSwaps); - _editor.OptionSetManipulations(result, groupIdx, optionIdx, originalOption.Manipulations); + _editor.SetFiles((IModDataContainer)option, files); + _editor.SetFileSwaps((IModDataContainer)option, originalOption.FileSwaps); + _editor.SetManipulations((IModDataContainer)option, originalOption.Manipulations); } } } @@ -339,16 +341,15 @@ public class ModMerger : IDisposable { foreach (var option in _createdOptions) { - var (groupIdx, optionIdx) = option.GetOptionIndices(); - _editor.DeleteOption(MergeToMod!, groupIdx, optionIdx); + _editor.DeleteOption(option); Penumbra.Log.Verbose($"[Merger] Removed option {option.FullName}."); } foreach (var group in _createdGroups) { var groupName = MergeToMod!.Groups[group]; - _editor.DeleteModGroup(MergeToMod!, group); - Penumbra.Log.Verbose($"[Merger] Removed option group {groupName}."); + _editor.DeleteModGroup(groupName); + Penumbra.Log.Verbose($"[Merger] Removed option group {groupName.Name}."); } foreach (var dir in _createdDirectories) diff --git a/Penumbra/Mods/Editor/ModMetaEditor.cs b/Penumbra/Mods/Editor/ModMetaEditor.cs index dee700d5..2f7fd04c 100644 --- a/Penumbra/Mods/Editor/ModMetaEditor.cs +++ b/Penumbra/Mods/Editor/ModMetaEditor.cs @@ -145,12 +145,12 @@ public class ModMetaEditor(ModManager modManager) Split(currentOption.Manipulations); } - public void Apply(Mod mod, int groupIdx, int optionIdx) + public void Apply(IModDataContainer container) { if (!Changes) return; - modManager.OptionEditor.OptionSetManipulations(mod, groupIdx, optionIdx, Recombine().ToHashSet()); + modManager.OptionEditor.SetManipulations(container, Recombine().ToHashSet()); Changes = false; } diff --git a/Penumbra/Mods/Editor/ModNormalizer.cs b/Penumbra/Mods/Editor/ModNormalizer.cs index e2088b32..437600c9 100644 --- a/Penumbra/Mods/Editor/ModNormalizer.cs +++ b/Penumbra/Mods/Editor/ModNormalizer.cs @@ -283,12 +283,12 @@ public class ModNormalizer(ModManager _modManager, Configuration _config) switch (group) { case SingleModGroup single: - foreach (var (_, optionIdx) in single.OptionData.WithIndex()) - _modManager.OptionEditor.OptionSetFiles(Mod, groupIdx, optionIdx, _redirections[groupIdx + 1][optionIdx]); + foreach (var (option, optionIdx) in single.OptionData.WithIndex()) + _modManager.OptionEditor.SetFiles(option, _redirections[groupIdx + 1][optionIdx]); break; case MultiModGroup multi: - foreach (var (_, optionIdx) in multi.OptionData.WithIndex()) - _modManager.OptionEditor.OptionSetFiles(Mod, groupIdx, optionIdx, _redirections[groupIdx + 1][optionIdx]); + foreach (var (option, optionIdx) in multi.OptionData.WithIndex()) + _modManager.OptionEditor.SetFiles(option, _redirections[groupIdx + 1][optionIdx]); break; } } diff --git a/Penumbra/Mods/Editor/ModSwapEditor.cs b/Penumbra/Mods/Editor/ModSwapEditor.cs index 3247cfdf..0250efae 100644 --- a/Penumbra/Mods/Editor/ModSwapEditor.cs +++ b/Penumbra/Mods/Editor/ModSwapEditor.cs @@ -17,12 +17,12 @@ public class ModSwapEditor(ModManager modManager) Changes = false; } - public void Apply(Mod mod, int groupIdx, int optionIdx) + public void Apply(IModDataContainer container) { if (!Changes) return; - modManager.OptionEditor.OptionSetFileSwaps(mod, groupIdx, optionIdx, _swaps); + modManager.OptionEditor.SetFileSwaps(container, _swaps); Changes = false; } diff --git a/Penumbra/Mods/Groups/IModGroup.cs b/Penumbra/Mods/Groups/IModGroup.cs index b13799cd..a268ba0f 100644 --- a/Penumbra/Mods/Groups/IModGroup.cs +++ b/Penumbra/Mods/Groups/IModGroup.cs @@ -9,7 +9,7 @@ namespace Penumbra.Mods.Groups; public interface ITexToolsGroup { - public IReadOnlyList OptionData { get; } + public IReadOnlyList OptionData { get; } } public interface IModGroup @@ -17,22 +17,19 @@ public interface IModGroup public const int MaxMultiOptions = 63; public Mod Mod { get; } - public string Name { get; } + public string Name { get; set; } public string Description { get; set; } public GroupType Type { get; } public ModPriority Priority { get; set; } public Setting DefaultSettings { get; set; } - public FullPath? FindBestMatch(Utf8GamePath gamePath); - public int AddOption(Mod mod, string name, string description = ""); + public FullPath? FindBestMatch(Utf8GamePath gamePath); + public IModOption? AddOption(string name, string description = ""); public IReadOnlyList Options { get; } public IReadOnlyList DataContainers { get; } public bool IsOption { get; } - public IModGroup Convert(GroupType type); - public bool MoveOption(int optionIdxFrom, int optionIdxTo); - public int GetIndex(); public void AddData(Setting setting, Dictionary redirections, HashSet manipulations); diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs new file mode 100644 index 00000000..e233f82e --- /dev/null +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -0,0 +1,158 @@ +using Newtonsoft.Json; +using Penumbra.Api.Enums; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; +using Penumbra.String.Classes; + +namespace Penumbra.Mods.Groups; + +public class ImcModGroup(Mod mod) : IModGroup +{ + public const int DisabledIndex = 30; + public const int NumAttributes = 10; + + public Mod Mod { get; } = mod; + public string Name { get; set; } = "Option"; + public string Description { get; set; } = "A single IMC manipulation."; + + public GroupType Type + => GroupType.Imc; + + public ModPriority Priority { get; set; } = ModPriority.Default; + public Setting DefaultSettings { get; set; } = Setting.Zero; + + public PrimaryId PrimaryId; + public SecondaryId SecondaryId; + public ObjectType ObjectType; + public BodySlot BodySlot; + public EquipSlot EquipSlot; + public Variant Variant; + + public ImcEntry DefaultEntry; + + public FullPath? FindBestMatch(Utf8GamePath gamePath) + => null; + + private bool _canBeDisabled = false; + + public bool CanBeDisabled + { + get => _canBeDisabled; + set + { + _canBeDisabled = value; + if (!value) + DefaultSettings = FixSetting(DefaultSettings); + } + } + + public IModOption? AddOption(string name, string description = "") + { + uint fullMask = GetFullMask(); + var firstUnset = (byte)BitOperations.TrailingZeroCount(~fullMask); + // All attributes handled. + if (firstUnset >= NumAttributes) + return null; + + var groupIdx = Mod.Groups.IndexOf(this); + if (groupIdx < 0) + return null; + + var subMod = new ImcSubMod(this) + { + Name = name, + Description = description, + AttributeIndex = firstUnset, + }; + OptionData.Add(subMod); + return subMod; + } + + public readonly List OptionData = []; + + public IReadOnlyList Options + => OptionData; + + public IReadOnlyList DataContainers + => []; + + public bool IsOption + => CanBeDisabled || OptionData.Count > 0; + + public int GetIndex() + => ModGroup.GetIndex(this); + + private ushort GetCurrentMask(Setting setting) + { + var mask = DefaultEntry.AttributeMask; + for (var i = 0; i < OptionData.Count; ++i) + { + if (!setting.HasFlag(i)) + continue; + + var option = OptionData[i]; + mask |= option.Attribute; + } + + return mask; + } + + private ushort GetFullMask() + => GetCurrentMask(Setting.AllBits(63)); + + private ImcManipulation GetManip(ushort mask) + => new(ObjectType, BodySlot, PrimaryId, SecondaryId.Id, Variant.Id, EquipSlot, + DefaultEntry with { AttributeMask = mask }); + + + public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) + { + if (CanBeDisabled && setting.HasFlag(DisabledIndex)) + return; + + var mask = GetCurrentMask(setting); + var imc = GetManip(mask); + manipulations.Add(imc); + } + + public Setting FixSetting(Setting setting) + => new(setting.Value & (((1ul << OptionData.Count) - 1) | (CanBeDisabled ? 1ul << DisabledIndex : 0))); + + public void WriteJson(JsonTextWriter jWriter, JsonSerializer serializer, DirectoryInfo? basePath = null) + { + ModSaveGroup.WriteJsonBase(jWriter, this); + jWriter.WritePropertyName(nameof(ObjectType)); + jWriter.WriteValue(ObjectType.ToString()); + jWriter.WritePropertyName(nameof(BodySlot)); + jWriter.WriteValue(BodySlot.ToString()); + jWriter.WritePropertyName(nameof(EquipSlot)); + jWriter.WriteValue(EquipSlot.ToString()); + jWriter.WritePropertyName(nameof(PrimaryId)); + jWriter.WriteValue(PrimaryId.Id); + jWriter.WritePropertyName(nameof(SecondaryId)); + jWriter.WriteValue(SecondaryId.Id); + jWriter.WritePropertyName(nameof(Variant)); + jWriter.WriteValue(Variant.Id); + jWriter.WritePropertyName(nameof(DefaultEntry)); + serializer.Serialize(jWriter, DefaultEntry); + jWriter.WritePropertyName("Options"); + jWriter.WriteStartArray(); + foreach (var option in OptionData) + { + jWriter.WriteStartObject(); + SubMod.WriteModOption(jWriter, option); + jWriter.WritePropertyName(nameof(option.AttributeIndex)); + jWriter.WriteValue(option.AttributeIndex); + jWriter.WriteEndObject(); + } + + jWriter.WriteEndArray(); + jWriter.WriteEndObject(); + } + + public (int Redirections, int Swaps, int Manips) GetCounts() + => (0, 0, 1); +} diff --git a/Penumbra/Mods/Groups/ModGroup.cs b/Penumbra/Mods/Groups/ModGroup.cs index da302714..8b55a035 100644 --- a/Penumbra/Mods/Groups/ModGroup.cs +++ b/Penumbra/Mods/Groups/ModGroup.cs @@ -5,6 +5,7 @@ namespace Penumbra.Mods.Groups; public static class ModGroup { + /// Create a new mod group based on the given type. public static IModGroup Create(Mod mod, GroupType type, string name) { var maxPriority = mod.Groups.Count == 0 ? ModPriority.Default : mod.Groups.Max(o => o.Priority) + 1; @@ -20,6 +21,11 @@ public static class ModGroup Name = name, Priority = maxPriority, }, + GroupType.Imc => new ImcModGroup(mod) + { + Name = name, + Priority = maxPriority, + }, _ => throw new ArgumentOutOfRangeException(nameof(type), type, null), }; } @@ -38,5 +44,14 @@ public static class ModGroup } return (redirectionCount, swapCount, manipCount); + } + + public static int GetIndex(IModGroup group) + { + var groupIndex = group.Mod.Groups.IndexOf(group); + if (groupIndex < 0) + throw new Exception($"Mod {group.Mod.Name} from Group {group.Name} does not contain this group."); + + return groupIndex; } } diff --git a/Penumbra/Mods/Groups/ModSaveGroup.cs b/Penumbra/Mods/Groups/ModSaveGroup.cs index 332879cb..efdcde09 100644 --- a/Penumbra/Mods/Groups/ModSaveGroup.cs +++ b/Penumbra/Mods/Groups/ModSaveGroup.cs @@ -1,9 +1,9 @@ -using Newtonsoft.Json; -using Penumbra.Mods.SubMods; -using Penumbra.Services; - -namespace Penumbra.Mods.Groups; - +using Newtonsoft.Json; +using Penumbra.Mods.SubMods; +using Penumbra.Services; + +namespace Penumbra.Mods.Groups; + public readonly struct ModSaveGroup : ISavable { private readonly DirectoryInfo _basePath; @@ -12,25 +12,21 @@ public readonly struct ModSaveGroup : ISavable private readonly DefaultSubMod? _defaultMod; private readonly bool _onlyAscii; - public ModSaveGroup(Mod mod, int groupIdx, bool onlyAscii) - { - _basePath = mod.ModPath; - _groupIdx = groupIdx; - if (_groupIdx < 0) - _defaultMod = mod.Default; - else - _group = mod.Groups[_groupIdx]; - _onlyAscii = onlyAscii; - } - - public ModSaveGroup(DirectoryInfo basePath, IModGroup group, int groupIdx, bool onlyAscii) + private ModSaveGroup(DirectoryInfo basePath, IModGroup group, int groupIndex, bool onlyAscii) { _basePath = basePath; _group = group; - _groupIdx = groupIdx; + _groupIdx = groupIndex; _onlyAscii = onlyAscii; } + public static ModSaveGroup WithoutMod(DirectoryInfo basePath, IModGroup group, int groupIndex, bool onlyAscii) + => new(basePath, group, groupIndex, onlyAscii); + + public ModSaveGroup(IModGroup group, bool onlyAscii) + : this(group.Mod.ModPath, group, group.GetIndex(), onlyAscii) + { } + public ModSaveGroup(DirectoryInfo basePath, DefaultSubMod @default, bool onlyAscii) { _basePath = basePath; @@ -39,6 +35,33 @@ public readonly struct ModSaveGroup : ISavable _onlyAscii = onlyAscii; } + public ModSaveGroup(DirectoryInfo basePath, IModDataContainer container, bool onlyAscii) + { + _basePath = basePath; + _defaultMod = container as DefaultSubMod; + _onlyAscii = onlyAscii; + if (_defaultMod == null) + { + _groupIdx = -1; + _group = null; + } + else + { + _group = container.Group!; + _groupIdx = _group.GetIndex(); + } + } + + public ModSaveGroup(IModDataContainer container, bool onlyAscii) + { + _basePath = (container.Mod as Mod)?.ModPath + ?? throw new Exception("Invalid save group from default data container without base path."); // Should not happen. + _defaultMod = null; + _onlyAscii = onlyAscii; + _group = container.Group!; + _groupIdx = _group.GetIndex(); + } + public string ToFilename(FilenameService fileNames) => fileNames.OptionGroupFile(_basePath.FullName, _groupIdx, _group?.Name ?? string.Empty, _onlyAscii); @@ -59,7 +82,7 @@ public readonly struct ModSaveGroup : ISavable { jWriter.WriteStartObject(); jWriter.WritePropertyName(nameof(group.Name)); - jWriter.WriteValue(group!.Name); + jWriter.WriteValue(group.Name); jWriter.WritePropertyName(nameof(group.Description)); jWriter.WriteValue(group.Description); jWriter.WritePropertyName(nameof(group.Priority)); @@ -69,4 +92,4 @@ public readonly struct ModSaveGroup : ISavable jWriter.WritePropertyName(nameof(group.DefaultSettings)); jWriter.WriteValue(group.DefaultSettings.Value); } -} +} diff --git a/Penumbra/Mods/Groups/MultiModGroup.cs b/Penumbra/Mods/Groups/MultiModGroup.cs index 6b352f66..a0034be0 100644 --- a/Penumbra/Mods/Groups/MultiModGroup.cs +++ b/Penumbra/Mods/Groups/MultiModGroup.cs @@ -3,7 +3,6 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui; using OtterGui.Classes; -using OtterGui.Filesystem; using Penumbra.Api.Enums; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Settings; @@ -18,11 +17,11 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup public GroupType Type => GroupType.Multi; - public Mod Mod { get; set; } = mod; - public string Name { get; set; } = "Group"; - public string Description { get; set; } = "A non-exclusive group of settings."; - public ModPriority Priority { get; set; } - public Setting DefaultSettings { get; set; } + public Mod Mod { get; } = mod; + public string Name { get; set; } = "Group"; + public string Description { get; set; } = "A non-exclusive group of settings."; + public ModPriority Priority { get; set; } + public Setting DefaultSettings { get; set; } public readonly List OptionData = []; public IReadOnlyList Options @@ -39,28 +38,28 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup .SelectWhere(o => (o.Files.TryGetValue(gamePath, out var file) || o.FileSwaps.TryGetValue(gamePath, out file), file)) .FirstOrDefault(); - public int AddOption(Mod mod, string name, string description = "") + public IModOption? AddOption(string name, string description = "") { - var groupIdx = mod.Groups.IndexOf(this); + var groupIdx = Mod.Groups.IndexOf(this); if (groupIdx < 0) - return -1; + return null; - var subMod = new MultiSubMod(mod, this) + var subMod = new MultiSubMod(this) { - Name = name, + Name = name, Description = description, }; OptionData.Add(subMod); - return OptionData.Count - 1; + return subMod; } public static MultiModGroup? Load(Mod mod, JObject json) { var ret = new MultiModGroup(mod) { - Name = json[nameof(Name)]?.ToObject() ?? string.Empty, - Description = json[nameof(Description)]?.ToObject() ?? string.Empty, - Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, + Name = json[nameof(Name)]?.ToObject() ?? string.Empty, + Description = json[nameof(Description)]?.ToObject() ?? string.Empty, + Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, DefaultSettings = json[nameof(DefaultSettings)]?.ToObject() ?? Setting.Zero, }; if (ret.Name.Length == 0) @@ -78,7 +77,7 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup break; } - var subMod = new MultiSubMod(mod, ret, child); + var subMod = new MultiSubMod(ret, child); ret.OptionData.Add(subMod); } @@ -87,42 +86,21 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup return ret; } - public IModGroup Convert(GroupType type) + public SingleModGroup ConvertToSingle() { - switch (type) + var single = new SingleModGroup(Mod) { - case GroupType.Multi: return this; - case GroupType.Single: - var single = new SingleModGroup(Mod) - { - Name = Name, - Description = Description, - Priority = Priority, - DefaultSettings = DefaultSettings.TurnMulti(OptionData.Count), - }; - single.OptionData.AddRange(OptionData.Select(o => o.ConvertToSingle(Mod, single))); - return single; - default: throw new ArgumentOutOfRangeException(nameof(type), type, null); - } - } - - public bool MoveOption(int optionIdxFrom, int optionIdxTo) - { - if (!OptionData.Move(optionIdxFrom, optionIdxTo)) - return false; - - DefaultSettings = DefaultSettings.MoveBit(optionIdxFrom, optionIdxTo); - return true; + Name = Name, + Description = Description, + Priority = Priority, + DefaultSettings = DefaultSettings.TurnMulti(OptionData.Count), + }; + single.OptionData.AddRange(OptionData.Select(o => o.ConvertToSingle(single))); + return single; } public int GetIndex() - { - var groupIndex = Mod.Groups.IndexOf(this); - if (groupIndex < 0) - throw new Exception($"Mod {Mod.Name} from Group {Name} does not contain this group."); - - return groupIndex; - } + => ModGroup.GetIndex(this); public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) { @@ -156,15 +134,15 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup => ModGroup.GetCountsBase(this); public Setting FixSetting(Setting setting) - => new(setting.Value & (1ul << OptionData.Count) - 1); + => new(setting.Value & ((1ul << OptionData.Count) - 1)); /// Create a group without a mod only for saving it in the creator. - internal static MultiModGroup CreateForSaving(string name) + internal static MultiModGroup WithoutMod(string name) => new(null!) { Name = name, }; - IReadOnlyList ITexToolsGroup.OptionData + IReadOnlyList ITexToolsGroup.OptionData => OptionData; } diff --git a/Penumbra/Mods/Groups/SingleModGroup.cs b/Penumbra/Mods/Groups/SingleModGroup.cs index ac85e2bc..0776c2af 100644 --- a/Penumbra/Mods/Groups/SingleModGroup.cs +++ b/Penumbra/Mods/Groups/SingleModGroup.cs @@ -1,7 +1,6 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui; -using OtterGui.Filesystem; using Penumbra.Api.Enums; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Settings; @@ -16,31 +15,28 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup public GroupType Type => GroupType.Single; - public Mod Mod { get; set; } = mod; - public string Name { get; set; } = "Option"; - public string Description { get; set; } = "A mutually exclusive group of settings."; - public ModPriority Priority { get; set; } - public Setting DefaultSettings { get; set; } + public Mod Mod { get; } = mod; + public string Name { get; set; } = "Option"; + public string Description { get; set; } = "A mutually exclusive group of settings."; + public ModPriority Priority { get; set; } + public Setting DefaultSettings { get; set; } public readonly List OptionData = []; - IReadOnlyList ITexToolsGroup.OptionData - => OptionData; - public FullPath? FindBestMatch(Utf8GamePath gamePath) => OptionData .SelectWhere(m => (m.Files.TryGetValue(gamePath, out var file) || m.FileSwaps.TryGetValue(gamePath, out file), file)) .FirstOrDefault(); - public int AddOption(Mod mod, string name, string description = "") + public IModOption AddOption(string name, string description = "") { - var subMod = new SingleSubMod(mod, this) + var subMod = new SingleSubMod(this) { - Name = name, + Name = name, Description = description, }; OptionData.Add(subMod); - return OptionData.Count - 1; + return subMod; } public IReadOnlyList Options @@ -57,9 +53,9 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup var options = json["Options"]; var ret = new SingleModGroup(mod) { - Name = json[nameof(Name)]?.ToObject() ?? string.Empty, - Description = json[nameof(Description)]?.ToObject() ?? string.Empty, - Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, + Name = json[nameof(Name)]?.ToObject() ?? string.Empty, + Description = json[nameof(Description)]?.ToObject() ?? string.Empty, + Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, DefaultSettings = json[nameof(DefaultSettings)]?.ToObject() ?? Setting.Zero, }; if (ret.Name.Length == 0) @@ -68,7 +64,7 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup if (options != null) foreach (var child in options.Children()) { - var subMod = new SingleSubMod(mod, ret, child); + var subMod = new SingleSubMod(ret, child); ret.OptionData.Add(subMod); } @@ -76,57 +72,21 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup return ret; } - public IModGroup Convert(GroupType type) + public MultiModGroup ConvertToMulti() { - switch (type) + var multi = new MultiModGroup(Mod) { - case GroupType.Single: return this; - case GroupType.Multi: - var multi = new MultiModGroup(Mod) - { - Name = Name, - Description = Description, - Priority = Priority, - DefaultSettings = Setting.Multi((int)DefaultSettings.Value), - }; - multi.OptionData.AddRange(OptionData.Select((o, i) => o.ConvertToMulti(Mod, multi, new ModPriority(i)))); - return multi; - default: throw new ArgumentOutOfRangeException(nameof(type), type, null); - } + Name = Name, + Description = Description, + Priority = Priority, + DefaultSettings = Setting.Multi((int)DefaultSettings.Value), + }; + multi.OptionData.AddRange(OptionData.Select((o, i) => o.ConvertToMulti(multi, new ModPriority(i)))); + return multi; } - public bool MoveOption(int optionIdxFrom, int optionIdxTo) - { - if (!OptionData.Move(optionIdxFrom, optionIdxTo)) - return false; - - var currentIndex = DefaultSettings.AsIndex; - // Update default settings with the move. - if (currentIndex == optionIdxFrom) - { - DefaultSettings = Setting.Single(optionIdxTo); - } - else if (optionIdxFrom < optionIdxTo) - { - if (currentIndex > optionIdxFrom && currentIndex <= optionIdxTo) - DefaultSettings = Setting.Single(currentIndex - 1); - } - else if (currentIndex < optionIdxFrom && currentIndex >= optionIdxTo) - { - DefaultSettings = Setting.Single(currentIndex + 1); - } - - return true; - } - - public int GetIndex() - { - var groupIndex = Mod.Groups.IndexOf(this); - if (groupIndex < 0) - throw new Exception($"Mod {Mod.Name} from Group {Name} does not contain this group."); - - return groupIndex; - } + public int GetIndex() + => ModGroup.GetIndex(this); public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) => OptionData[setting.AsIndex].AddDataTo(redirections, manipulations); @@ -160,4 +120,7 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup { Name = name, }; + + IReadOnlyList ITexToolsGroup.OptionData + => OptionData; } diff --git a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs index 1545811e..449405a0 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs @@ -1,3 +1,4 @@ +using OtterGui.Classes; using Penumbra.Collections; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; @@ -8,6 +9,7 @@ using Penumbra.Meta; using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; namespace Penumbra.Mods.ItemSwap; @@ -40,8 +42,7 @@ public class ItemSwapContainer NoSwaps, } - public bool WriteMod(ModManager manager, Mod mod, WriteType writeType = WriteType.NoSwaps, DirectoryInfo? directory = null, - int groupIndex = -1, int optionIndex = 0) + public bool WriteMod(ModManager manager, Mod mod, IModDataContainer container, WriteType writeType = WriteType.NoSwaps, DirectoryInfo? directory = null) { var convertedManips = new HashSet(Swaps.Count); var convertedFiles = new Dictionary(Swaps.Count); @@ -80,9 +81,9 @@ public class ItemSwapContainer } } - manager.OptionEditor.OptionSetFiles(mod, groupIndex, optionIndex, convertedFiles); - manager.OptionEditor.OptionSetFileSwaps(mod, groupIndex, optionIndex, convertedSwaps); - manager.OptionEditor.OptionSetManipulations(mod, groupIndex, optionIndex, convertedManips); + manager.OptionEditor.SetFiles(container, convertedFiles, SaveType.None); + manager.OptionEditor.SetFileSwaps(container, convertedSwaps, SaveType.None); + manager.OptionEditor.SetManipulations(container, convertedManips, SaveType.ImmediateSync); return true; } catch (Exception e) diff --git a/Penumbra/Mods/Manager/ImcModGroupEditor.cs b/Penumbra/Mods/Manager/ImcModGroupEditor.cs new file mode 100644 index 00000000..4e2b2194 --- /dev/null +++ b/Penumbra/Mods/Manager/ImcModGroupEditor.cs @@ -0,0 +1,38 @@ +using OtterGui.Classes; +using OtterGui.Filesystem; +using OtterGui.Services; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; +using Penumbra.Services; + +namespace Penumbra.Mods.Manager; + +public sealed class ImcModGroupEditor(CommunicatorService communicator, SaveService saveService, Configuration config) + : ModOptionEditor(communicator, saveService, config), IService +{ + protected override ImcModGroup CreateGroup(Mod mod, string newName, ModPriority priority, SaveType saveType = SaveType.ImmediateSync) + => new(mod) + { + Name = newName, + Priority = priority, + }; + + protected override ImcSubMod? CloneOption(ImcModGroup group, IModOption option) + => null; + + protected override void RemoveOption(ImcModGroup group, int optionIndex) + { + group.OptionData.RemoveAt(optionIndex); + group.DefaultSettings = group.FixSetting(group.DefaultSettings); + } + + protected override bool MoveOption(ImcModGroup group, int optionIdxFrom, int optionIdxTo) + { + if (!group.OptionData.Move(optionIdxFrom, optionIdxTo)) + return false; + + group.DefaultSettings = group.DefaultSettings.MoveBit(optionIdxFrom, optionIdxTo); + return true; + } +} diff --git a/Penumbra/Mods/Manager/ModCacheManager.cs b/Penumbra/Mods/Manager/ModCacheManager.cs index 3ff1a333..0669696f 100644 --- a/Penumbra/Mods/Manager/ModCacheManager.cs +++ b/Penumbra/Mods/Manager/ModCacheManager.cs @@ -2,6 +2,8 @@ using Penumbra.Communication; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Groups; +using Penumbra.Mods.SubMods; using Penumbra.Services; namespace Penumbra.Mods.Manager; @@ -103,7 +105,7 @@ public class ModCacheManager : IDisposable } } - private void OnModOptionChange(ModOptionChangeType type, Mod mod, int groupIdx, int _, int _2) + private void OnModOptionChange(ModOptionChangeType type, Mod mod, IModGroup? group, IModOption? option, IModDataContainer? container, int fromIdx) { switch (type) { diff --git a/Penumbra/Mods/Manager/ModGroupEditor.cs b/Penumbra/Mods/Manager/ModGroupEditor.cs new file mode 100644 index 00000000..9f41fa6f --- /dev/null +++ b/Penumbra/Mods/Manager/ModGroupEditor.cs @@ -0,0 +1,289 @@ +using System.Text.RegularExpressions; +using Dalamud.Interface.Internal.Notifications; +using OtterGui.Classes; +using OtterGui.Filesystem; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.GameData.Data; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; +using Penumbra.Services; +using Penumbra.String.Classes; +using Penumbra.Util; +using static FFXIVClientStructs.FFXIV.Client.UI.Misc.ConfigModule; + +namespace Penumbra.Mods.Manager; + +public enum ModOptionChangeType +{ + GroupRenamed, + GroupAdded, + GroupDeleted, + GroupMoved, + GroupTypeChanged, + PriorityChanged, + OptionAdded, + OptionDeleted, + OptionMoved, + OptionFilesChanged, + OptionFilesAdded, + OptionSwapsChanged, + OptionMetaChanged, + DisplayChange, + PrepareChange, + DefaultOptionChanged, +} + +public class ModGroupEditor( + SingleModGroupEditor singleEditor, + MultiModGroupEditor multiEditor, + ImcModGroupEditor imcEditor, + CommunicatorService Communicator, + SaveService SaveService, + Configuration Config) : IService +{ + public SingleModGroupEditor SingleEditor + => singleEditor; + + public MultiModGroupEditor MultiEditor + => multiEditor; + + public ImcModGroupEditor ImcEditor + => imcEditor; + + /// Change the settings stored as default options in a mod. + public void ChangeModGroupDefaultOption(IModGroup group, Setting defaultOption) + { + if (group.DefaultSettings == defaultOption) + return; + + group.DefaultSettings = defaultOption; + SaveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.DefaultOptionChanged, group.Mod, group, null, null, -1); + } + + /// Rename an option group if possible. + public void RenameModGroup(IModGroup group, string newName) + { + var oldName = group.Name; + if (oldName == newName || !VerifyFileName(group.Mod, group, newName, true)) + return; + + SaveService.ImmediateDelete(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + group.Name = newName; + SaveService.ImmediateSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupRenamed, group.Mod, group, null, null, -1); + } + + /// Delete a given option group. Fires an event to prepare before actually deleting. + public void DeleteModGroup(IModGroup group) + { + var mod = group.Mod; + var idx = group.GetIndex(); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, group, null, null, -1); + mod.Groups.RemoveAt(idx); + SaveService.SaveAllOptionGroups(mod, false, Config.ReplaceNonAsciiOnImport); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupDeleted, mod, null, null, null, idx); + } + + /// Move the index of a given option group. + public void MoveModGroup(IModGroup group, int groupIdxTo) + { + var mod = group.Mod; + var idxFrom = group.GetIndex(); + if (!mod.Groups.Move(idxFrom, groupIdxTo)) + return; + + SaveService.SaveAllOptionGroups(mod, false, Config.ReplaceNonAsciiOnImport); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupMoved, mod, group, null, null, idxFrom); + } + + /// Change the internal priority of the given option group. + public void ChangeGroupPriority(IModGroup group, ModPriority newPriority) + { + if (group.Priority == newPriority) + return; + + group.Priority = newPriority; + SaveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.PriorityChanged, group.Mod, group, null, null, -1); + } + + /// Change the description of the given option group. + public void ChangeGroupDescription(IModGroup group, string newDescription) + { + if (group.Description == newDescription) + return; + + group.Description = newDescription; + SaveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, group.Mod, group, null, null, -1); + } + + /// Rename the given option. + public void RenameOption(IModOption option, string newName) + { + if (option.Name == newName) + return; + + option.Name = newName; + SaveService.QueueSave(new ModSaveGroup(option.Group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, option.Mod, option.Group, option, null, -1); + } + + /// Change the description of the given option. + public void ChangeOptionDescription(IModOption option, string newDescription) + { + if (option.Description == newDescription) + return; + + option.Description = newDescription; + SaveService.QueueSave(new ModSaveGroup(option.Group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, option.Mod, option.Group, option, null, -1); + } + + /// Set the meta manipulations for a given option. Replaces existing manipulations. + public void SetManipulations(IModDataContainer subMod, HashSet manipulations, SaveType saveType = SaveType.Queue) + { + if (subMod.Manipulations.Count == manipulations.Count + && subMod.Manipulations.All(m => manipulations.TryGetValue(m, out var old) && old.EntryEquals(m))) + return; + + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); + subMod.Manipulations.SetTo(manipulations); + SaveService.Save(saveType, new ModSaveGroup(subMod, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMetaChanged, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); + } + + /// Set the file redirections for a given option. Replaces existing redirections. + public void SetFiles(IModDataContainer subMod, IReadOnlyDictionary replacements, SaveType saveType = SaveType.Queue) + { + if (subMod.Files.SetEquals(replacements)) + return; + + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); + subMod.Files.SetTo(replacements); + SaveService.Save(saveType, new ModSaveGroup(subMod, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesChanged, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); + } + + /// Add additional file redirections to a given option, keeping already existing ones. Only fires an event if anything is actually added. + public void AddFiles(IModDataContainer subMod, IReadOnlyDictionary additions) + { + var oldCount = subMod.Files.Count; + subMod.Files.AddFrom(additions); + if (oldCount != subMod.Files.Count) + { + SaveService.QueueSave(new ModSaveGroup(subMod, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesAdded, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); + } + } + + /// Set the file swaps for a given option. Replaces existing swaps. + public void SetFileSwaps(IModDataContainer subMod, IReadOnlyDictionary swaps, SaveType saveType = SaveType.Queue) + { + if (subMod.FileSwaps.SetEquals(swaps)) + return; + + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); + subMod.FileSwaps.SetTo(swaps); + SaveService.Save(saveType, new ModSaveGroup(subMod, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionSwapsChanged, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); + } + + /// Verify that a new option group name is unique in this mod. + public static bool VerifyFileName(Mod mod, IModGroup? group, string newName, bool message) + { + var path = newName.RemoveInvalidPathSymbols(); + if (path.Length != 0 + && !mod.Groups.Any(o => !ReferenceEquals(o, group) + && string.Equals(o.Name.RemoveInvalidPathSymbols(), path, StringComparison.OrdinalIgnoreCase))) + return true; + + if (message) + Penumbra.Messager.NotificationMessage( + $"Could not name option {newName} because option with same filename {path} already exists.", + NotificationType.Warning, false); + + return false; + } + + public void DeleteOption(IModOption option) + { + switch (option) + { + case SingleSubMod s: + SingleEditor.DeleteOption(s); + return; + case MultiSubMod m: + MultiEditor.DeleteOption(m); + return; + case ImcSubMod i: + ImcEditor.DeleteOption(i); + return; + } + } + + public IModOption? AddOption(IModGroup group, IModOption option) + => group switch + { + SingleModGroup s => SingleEditor.AddOption(s, option), + MultiModGroup m => MultiEditor.AddOption(m, option), + ImcModGroup i => ImcEditor.AddOption(i, option), + _ => null, + }; + + public IModOption? AddOption(IModGroup group, string newName) + => group switch + { + SingleModGroup s => SingleEditor.AddOption(s, newName), + MultiModGroup m => MultiEditor.AddOption(m, newName), + ImcModGroup i => ImcEditor.AddOption(i, newName), + _ => null, + }; + + public IModGroup? AddModGroup(Mod mod, GroupType type, string newName, SaveType saveType = SaveType.ImmediateSync) + => type switch + { + GroupType.Single => SingleEditor.AddModGroup(mod, newName, saveType), + GroupType.Multi => MultiEditor.AddModGroup(mod, newName, saveType), + GroupType.Imc => ImcEditor.AddModGroup(mod, newName, saveType), + _ => null, + }; + + public (IModGroup?, int, bool) FindOrAddModGroup(Mod mod, GroupType type, string name, SaveType saveType = SaveType.ImmediateSync) + => type switch + { + GroupType.Single => SingleEditor.FindOrAddModGroup(mod, name, saveType), + GroupType.Multi => MultiEditor.FindOrAddModGroup(mod, name, saveType), + GroupType.Imc => ImcEditor.FindOrAddModGroup(mod, name, saveType), + _ => (null, -1, false), + }; + + public (IModOption?, int, bool) FindOrAddOption(IModGroup group, string name, SaveType saveType = SaveType.ImmediateSync) + => group switch + { + SingleModGroup s => SingleEditor.FindOrAddOption(s, name, saveType), + MultiModGroup m => MultiEditor.FindOrAddOption(m, name, saveType), + ImcModGroup i => ImcEditor.FindOrAddOption(i, name, saveType), + _ => (null, -1, false), + }; + + public void MoveOption(IModOption option, int toIdx) + { + switch (option) + { + case SingleSubMod s: + SingleEditor.MoveOption(s, toIdx); + return; + case MultiSubMod m: + MultiEditor.MoveOption(m, toIdx); + return; + case ImcSubMod i: + ImcEditor.MoveOption(i, toIdx); + return; + } + } +} diff --git a/Penumbra/Mods/Manager/ModManager.cs b/Penumbra/Mods/Manager/ModManager.cs index d912e292..7a266a31 100644 --- a/Penumbra/Mods/Manager/ModManager.cs +++ b/Penumbra/Mods/Manager/ModManager.cs @@ -1,4 +1,3 @@ -using System.Security.AccessControl; using Penumbra.Communication; using Penumbra.Mods.Editor; using Penumbra.Services; @@ -32,14 +31,14 @@ public sealed class ModManager : ModStorage, IDisposable private readonly Configuration _config; private readonly CommunicatorService _communicator; - public readonly ModCreator Creator; - public readonly ModDataEditor DataEditor; - public readonly ModOptionEditor OptionEditor; + public readonly ModCreator Creator; + public readonly ModDataEditor DataEditor; + public readonly ModGroupEditor OptionEditor; public DirectoryInfo BasePath { get; private set; } = null!; public bool Valid { get; private set; } - public ModManager(Configuration config, CommunicatorService communicator, ModDataEditor dataEditor, ModOptionEditor optionEditor, + public ModManager(Configuration config, CommunicatorService communicator, ModDataEditor dataEditor, ModGroupEditor optionEditor, ModCreator creator) { _config = config; diff --git a/Penumbra/Mods/Manager/ModMigration.cs b/Penumbra/Mods/Manager/ModMigration.cs index f160d5bd..c7eb7cc5 100644 --- a/Penumbra/Mods/Manager/ModMigration.cs +++ b/Penumbra/Mods/Manager/ModMigration.cs @@ -83,8 +83,8 @@ public static partial class ModMigration mod.Default.FileSwaps.Add(gamePath, swapPath); creator.IncorporateMetaChanges(mod.Default, mod.ModPath, true); - foreach (var (_, index) in mod.Groups.WithIndex()) - saveService.ImmediateSave(new ModSaveGroup(mod, index, creator.Config.ReplaceNonAsciiOnImport)); + foreach (var group in mod.Groups) + saveService.ImmediateSave(new ModSaveGroup(group, creator.Config.ReplaceNonAsciiOnImport)); // Delete meta files. foreach (var file in seenMetaFiles.Where(f => f.Exists)) @@ -112,7 +112,7 @@ public static partial class ModMigration } fileVersion = 1; - saveService.ImmediateSave(new ModSaveGroup(mod, -1, creator.Config.ReplaceNonAsciiOnImport)); + saveService.ImmediateSave(new ModSaveGroup(mod.ModPath, mod.Default, creator.Config.ReplaceNonAsciiOnImport)); return true; } @@ -176,7 +176,7 @@ public static partial class ModMigration private static SingleSubMod SubModFromOption(ModCreator creator, Mod mod, SingleModGroup group, OptionV0 option, HashSet seenMetaFiles) { - var subMod = new SingleSubMod(mod, group) + var subMod = new SingleSubMod(group) { Name = option.OptionName, Description = option.OptionDesc, @@ -189,7 +189,7 @@ public static partial class ModMigration private static MultiSubMod SubModFromOption(ModCreator creator, Mod mod, MultiModGroup group, OptionV0 option, ModPriority priority, HashSet seenMetaFiles) { - var subMod = new MultiSubMod(mod, group) + var subMod = new MultiSubMod(group) { Name = option.OptionName, Description = option.OptionDesc, @@ -219,7 +219,7 @@ public static partial class ModMigration [JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))] public GroupType SelectionType = GroupType.Single; - public List Options = new(); + public List Options = []; public OptionGroupV0() { } @@ -236,12 +236,12 @@ public static partial class ModMigration var token = JToken.Load(reader); if (token.Type == JTokenType.Array) - return token.ToObject>() ?? new HashSet(); + return token.ToObject>() ?? []; var tmp = token.ToObject(); return tmp != null ? new HashSet { tmp } - : new HashSet(); + : []; } public override bool CanWrite diff --git a/Penumbra/Mods/Manager/ModOptionEditor.cs b/Penumbra/Mods/Manager/ModOptionEditor.cs index c6122ea8..7370a933 100644 --- a/Penumbra/Mods/Manager/ModOptionEditor.cs +++ b/Penumbra/Mods/Manager/ModOptionEditor.cs @@ -1,384 +1,122 @@ -using Dalamud.Interface.Internal.Notifications; using OtterGui; using OtterGui.Classes; -using OtterGui.Filesystem; -using Penumbra.Api.Enums; -using Penumbra.Meta.Manipulations; using Penumbra.Mods.Groups; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; -using Penumbra.String.Classes; -using Penumbra.Util; namespace Penumbra.Mods.Manager; -public enum ModOptionChangeType +public abstract class ModOptionEditor( + CommunicatorService communicator, + SaveService saveService, + Configuration config) + where TGroup : class, IModGroup + where TOption : class, IModOption { - GroupRenamed, - GroupAdded, - GroupDeleted, - GroupMoved, - GroupTypeChanged, - PriorityChanged, - OptionAdded, - OptionDeleted, - OptionMoved, - OptionFilesChanged, - OptionFilesAdded, - OptionSwapsChanged, - OptionMetaChanged, - DisplayChange, - PrepareChange, - DefaultOptionChanged, -} - -public class ModOptionEditor(CommunicatorService communicator, SaveService saveService, Configuration config) -{ - /// Change the type of a group given by mod and index to type, if possible. - public void ChangeModGroupType(Mod mod, int groupIdx, GroupType type) - { - var group = mod.Groups[groupIdx]; - if (group.Type == type) - return; - - mod.Groups[groupIdx] = group.Convert(type); - saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupTypeChanged, mod, groupIdx, -1, -1); - } - - /// Change the settings stored as default options in a mod. - public void ChangeModGroupDefaultOption(Mod mod, int groupIdx, Setting defaultOption) - { - var group = mod.Groups[groupIdx]; - if (group.DefaultSettings == defaultOption) - return; - - group.DefaultSettings = defaultOption; - saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.DefaultOptionChanged, mod, groupIdx, -1, -1); - } - - /// Rename an option group if possible. - public void RenameModGroup(Mod mod, int groupIdx, string newName) - { - var group = mod.Groups[groupIdx]; - var oldName = group.Name; - if (oldName == newName || !VerifyFileName(mod, group, newName, true)) - return; - - saveService.ImmediateDelete(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - _ = group switch - { - SingleModGroup s => s.Name = newName, - MultiModGroup m => m.Name = newName, - _ => newName, - }; - - saveService.ImmediateSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupRenamed, mod, groupIdx, -1, -1); - } + protected readonly CommunicatorService Communicator = communicator; + protected readonly SaveService SaveService = saveService; + protected readonly Configuration Config = config; /// Add a new, empty option group of the given type and name. - public void AddModGroup(Mod mod, GroupType type, string newName, SaveType saveType = SaveType.ImmediateSync) + public TGroup? AddModGroup(Mod mod, string newName, SaveType saveType = SaveType.ImmediateSync) { - if (!VerifyFileName(mod, null, newName, true)) - return; + if (!ModGroupEditor.VerifyFileName(mod, null, newName, true)) + return null; - var idx = mod.Groups.Count; - var group = ModGroup.Create(mod, type, newName); + var maxPriority = mod.Groups.Count == 0 ? ModPriority.Default : mod.Groups.Max(o => o.Priority) + 1; + var group = CreateGroup(mod, newName, maxPriority); mod.Groups.Add(group); - saveService.Save(saveType, new ModSaveGroup(mod, idx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupAdded, mod, idx, -1, -1); + SaveService.Save(saveType, new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupAdded, mod, group, null, null, -1); + return group; } /// Add a new mod, empty option group of the given type and name if it does not exist already. - public (IModGroup, int, bool) FindOrAddModGroup(Mod mod, GroupType type, string newName, SaveType saveType = SaveType.ImmediateSync) + public (TGroup, int, bool) FindOrAddModGroup(Mod mod, string newName, SaveType saveType = SaveType.ImmediateSync) { var idx = mod.Groups.IndexOf(g => g.Name == newName); if (idx >= 0) - return (mod.Groups[idx], idx, false); + { + var existingGroup = mod.Groups[idx] as TGroup + ?? throw new Exception($"Mod group with name {newName} exists, but is of the wrong type."); + return (existingGroup, idx, false); + } - AddModGroup(mod, type, newName, saveType); - if (mod.Groups[^1].Name != newName) + idx = mod.Groups.Count; + if (AddModGroup(mod, newName, saveType) is not { } group) throw new Exception($"Could not create new mod group with name {newName}."); - return (mod.Groups[^1], mod.Groups.Count - 1, true); - } - - /// Delete a given option group. Fires an event to prepare before actually deleting. - public void DeleteModGroup(Mod mod, int groupIdx) - { - communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, -1, -1); - mod.Groups.RemoveAt(groupIdx); - saveService.SaveAllOptionGroups(mod, false, config.ReplaceNonAsciiOnImport); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupDeleted, mod, groupIdx, -1, -1); - } - - /// Move the index of a given option group. - public void MoveModGroup(Mod mod, int groupIdxFrom, int groupIdxTo) - { - if (!mod.Groups.Move(groupIdxFrom, groupIdxTo)) - return; - - saveService.SaveAllOptionGroups(mod, false, config.ReplaceNonAsciiOnImport); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupMoved, mod, groupIdxFrom, -1, groupIdxTo); - } - - /// Change the description of the given option group. - public void ChangeGroupDescription(Mod mod, int groupIdx, string newDescription) - { - var group = mod.Groups[groupIdx]; - if (group.Description == newDescription) - return; - - group.Description = newDescription; - saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, mod, groupIdx, -1, -1); - } - - /// Change the description of the given option. - public void ChangeOptionDescription(Mod mod, int groupIdx, int optionIdx, string newDescription) - { - var option = mod.Groups[groupIdx].Options[optionIdx]; - if (option.Description == newDescription) - return; - - option.Description = newDescription; - saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, mod, groupIdx, optionIdx, -1); - } - - /// Change the internal priority of the given option group. - public void ChangeGroupPriority(Mod mod, int groupIdx, ModPriority newPriority) - { - var group = mod.Groups[groupIdx]; - if (group.Priority == newPriority) - return; - - group.Priority = newPriority; - saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.PriorityChanged, mod, groupIdx, -1, -1); - } - - /// Change the internal priority of the given option. - public void ChangeOptionPriority(Mod mod, int groupIdx, int optionIdx, ModPriority newPriority) - { - switch (mod.Groups[groupIdx]) - { - case MultiModGroup multi: - if (multi.OptionData[optionIdx].Priority == newPriority) - return; - - multi.OptionData[optionIdx].Priority = newPriority; - saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.PriorityChanged, mod, groupIdx, optionIdx, -1); - return; - } - } - - /// Rename the given option. - public void RenameOption(Mod mod, int groupIdx, int optionIdx, string newName) - { - var option = mod.Groups[groupIdx].Options[optionIdx]; - if (option.Name == newName) - return; - - option.Name = newName; - - saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, mod, groupIdx, optionIdx, -1); + return (group, idx, true); } /// Add a new empty option of the given name for the given group. - public int AddOption(Mod mod, int groupIdx, string newName, SaveType saveType = SaveType.Queue) + public TOption? AddOption(TGroup group, string newName, SaveType saveType = SaveType.Queue) { - var group = mod.Groups[groupIdx]; - var idx = group.AddOption(mod, newName); - if (idx < 0) - return -1; + if (group.AddOption(newName) is not TOption option) + return null; - saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, idx, -1); - return idx; + SaveService.Save(saveType, new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, group.Mod, group, option, null, -1); + return option; } /// Add a new empty option of the given name for the given group if it does not exist already. - public (IModOption, int, bool) FindOrAddOption(Mod mod, int groupIdx, string newName, SaveType saveType = SaveType.Queue) + public (TOption, int, bool) FindOrAddOption(TGroup group, string newName, SaveType saveType = SaveType.Queue) { - var group = mod.Groups[groupIdx]; - var idx = group.Options.IndexOf(o => o.Name == newName); + var idx = group.Options.IndexOf(o => o.Name == newName); if (idx >= 0) - return (group.Options[idx], idx, false); + { + var existingOption = group.Options[idx] as TOption + ?? throw new Exception($"Mod option with name {newName} exists, but is of the wrong type."); // Should never happen. + return (existingOption, idx, false); + } - idx = group.AddOption(mod, newName); - if (idx < 0) + if (AddOption(group, newName, saveType) is not { } option) throw new Exception($"Could not create new option with name {newName} in {group.Name}."); - saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, idx, -1); - return (group.Options[idx], idx, true); + return (option, idx, true); } /// Add an existing option to a given group. - public void AddOption(Mod mod, int groupIdx, IModOption option) + public TOption? AddOption(TGroup group, IModOption option) { - var group = mod.Groups[groupIdx]; - int idx; - switch (group) - { - case MultiModGroup { OptionData.Count: >= IModGroup.MaxMultiOptions }: - Penumbra.Log.Error( - $"Could not add option {option.Name} to {group.Name} for mod {mod.Name}, " - + $"since only up to {IModGroup.MaxMultiOptions} options are supported in one group."); - return; - case SingleModGroup s: - { - idx = s.OptionData.Count; - var newOption = new SingleSubMod(s.Mod, s) - { - Name = option.Name, - Description = option.Description, - }; - if (option is IModDataContainer data) - SubMod.Clone(data, newOption); - s.OptionData.Add(newOption); - break; - } - case MultiModGroup m: - { - idx = m.OptionData.Count; - var newOption = new MultiSubMod(m.Mod, m) - { - Name = option.Name, - Description = option.Description, - Priority = option is MultiSubMod s ? s.Priority : ModPriority.Default, - }; - if (option is IModDataContainer data) - SubMod.Clone(data, newOption); - m.OptionData.Add(newOption); - break; - } - default: return; - } + if (CloneOption(group, option) is not { } clonedOption) + return null; - saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, mod, groupIdx, idx, -1); + SaveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, group.Mod, group, clonedOption, null, -1); + return clonedOption; } /// Delete the given option from the given group. - public void DeleteOption(Mod mod, int groupIdx, int optionIdx) + public void DeleteOption(TOption option) { - var group = mod.Groups[groupIdx]; - communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, optionIdx, -1); - switch (group) - { - case SingleModGroup s: - s.OptionData.RemoveAt(optionIdx); - - break; - case MultiModGroup m: - m.OptionData.RemoveAt(optionIdx); - break; - } - - saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionDeleted, mod, groupIdx, optionIdx, -1); + var mod = option.Mod; + var group = option.Group; + var optionIdx = option.GetIndex(); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, group, option, null, -1); + RemoveOption((TGroup)group, optionIdx); + SaveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionDeleted, mod, group, null, null, optionIdx); } /// Move an option inside the given option group. - public void MoveOption(Mod mod, int groupIdx, int optionIdxFrom, int optionIdxTo) + public void MoveOption(TOption option, int optionIdxTo) { - var group = mod.Groups[groupIdx]; - if (!group.MoveOption(optionIdxFrom, optionIdxTo)) + var idx = option.GetIndex(); + var group = (TGroup)option.Group; + if (!MoveOption(group, idx, optionIdxTo)) return; - saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMoved, mod, groupIdx, optionIdxFrom, optionIdxTo); + SaveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMoved, group.Mod, group, option, null, idx); } - /// Set the meta manipulations for a given option. Replaces existing manipulations. - public void OptionSetManipulations(Mod mod, int groupIdx, int dataContainerIdx, HashSet manipulations, - SaveType saveType = SaveType.Queue) - { - var subMod = GetSubMod(mod, groupIdx, dataContainerIdx); - if (subMod.Manipulations.Count == manipulations.Count - && subMod.Manipulations.All(m => manipulations.TryGetValue(m, out var old) && old.EntryEquals(m))) - return; - - communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, dataContainerIdx, -1); - subMod.Manipulations.SetTo(manipulations); - saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMetaChanged, mod, groupIdx, dataContainerIdx, -1); - } - - /// Set the file redirections for a given option. Replaces existing redirections. - public void OptionSetFiles(Mod mod, int groupIdx, int dataContainerIdx, IReadOnlyDictionary replacements, - SaveType saveType = SaveType.Queue) - { - var subMod = GetSubMod(mod, groupIdx, dataContainerIdx); - if (subMod.Files.SetEquals(replacements)) - return; - - communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, dataContainerIdx, -1); - subMod.Files.SetTo(replacements); - saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesChanged, mod, groupIdx, dataContainerIdx, -1); - } - - /// Add additional file redirections to a given option, keeping already existing ones. Only fires an event if anything is actually added. - public void OptionAddFiles(Mod mod, int groupIdx, int dataContainerIdx, IReadOnlyDictionary additions) - { - var subMod = GetSubMod(mod, groupIdx, dataContainerIdx); - var oldCount = subMod.Files.Count; - subMod.Files.AddFrom(additions); - if (oldCount != subMod.Files.Count) - { - saveService.QueueSave(new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesAdded, mod, groupIdx, dataContainerIdx, -1); - } - } - - /// Set the file swaps for a given option. Replaces existing swaps. - public void OptionSetFileSwaps(Mod mod, int groupIdx, int dataContainerIdx, IReadOnlyDictionary swaps, - SaveType saveType = SaveType.Queue) - { - var subMod = GetSubMod(mod, groupIdx, dataContainerIdx); - if (subMod.FileSwaps.SetEquals(swaps)) - return; - - communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, groupIdx, dataContainerIdx, -1); - subMod.FileSwaps.SetTo(swaps); - saveService.Save(saveType, new ModSaveGroup(mod, groupIdx, config.ReplaceNonAsciiOnImport)); - communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionSwapsChanged, mod, groupIdx, dataContainerIdx, -1); - } - - - /// Verify that a new option group name is unique in this mod. - public static bool VerifyFileName(Mod mod, IModGroup? group, string newName, bool message) - { - var path = newName.RemoveInvalidPathSymbols(); - if (path.Length != 0 - && !mod.Groups.Any(o => !ReferenceEquals(o, group) - && string.Equals(o.Name.RemoveInvalidPathSymbols(), path, StringComparison.OrdinalIgnoreCase))) - return true; - - if (message) - Penumbra.Messager.NotificationMessage( - $"Could not name option {newName} because option with same filename {path} already exists.", - NotificationType.Warning, false); - - return false; - } - - /// Get the correct option for the given group and option index. - private static IModDataContainer GetSubMod(Mod mod, int groupIdx, int dataContainerIdx) - { - if (groupIdx == -1 && dataContainerIdx == 0) - return mod.Default; - - return mod.Groups[groupIdx].DataContainers[dataContainerIdx]; - } + protected abstract TGroup CreateGroup(Mod mod, string newName, ModPriority priority, SaveType saveType = SaveType.ImmediateSync); + protected abstract TOption? CloneOption(TGroup group, IModOption option); + protected abstract void RemoveOption(TGroup group, int optionIndex); + protected abstract bool MoveOption(TGroup group, int optionIdxFrom, int optionIdxTo); } public static class ModOptionChangeTypeExtension diff --git a/Penumbra/Mods/Manager/MultiModGroupEditor.cs b/Penumbra/Mods/Manager/MultiModGroupEditor.cs new file mode 100644 index 00000000..e6b2bac1 --- /dev/null +++ b/Penumbra/Mods/Manager/MultiModGroupEditor.cs @@ -0,0 +1,84 @@ +using OtterGui.Classes; +using OtterGui.Filesystem; +using OtterGui.Services; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; +using Penumbra.Services; + +namespace Penumbra.Mods.Manager; + +public sealed class MultiModGroupEditor(CommunicatorService communicator, SaveService saveService, Configuration config) + : ModOptionEditor(communicator, saveService, config), IService +{ + public void ChangeToSingle(MultiModGroup group) + { + var idx = group.GetIndex(); + var singleGroup = group.ConvertToSingle(); + group.Mod.Groups[idx] = singleGroup; + SaveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupTypeChanged, group.Mod, group, null, null, -1); + } + + /// Change the internal priority of the given option. + public void ChangeOptionPriority(MultiSubMod option, ModPriority newPriority) + { + if (option.Priority == newPriority) + return; + + option.Priority = newPriority; + SaveService.QueueSave(new ModSaveGroup(option.Group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.PriorityChanged, option.Mod, option.Group, option, null, -1); + } + + protected override MultiModGroup CreateGroup(Mod mod, string newName, ModPriority priority, SaveType saveType = SaveType.ImmediateSync) + => new(mod) + { + Name = newName, + Priority = priority, + }; + + protected override MultiSubMod? CloneOption(MultiModGroup group, IModOption option) + { + if (group.OptionData.Count >= IModGroup.MaxMultiOptions) + { + Penumbra.Log.Error( + $"Could not add option {option.Name} to {group.Name} for mod {group.Mod.Name}, " + + $"since only up to {IModGroup.MaxMultiOptions} options are supported in one group."); + return null; + } + + var newOption = new MultiSubMod(group) + { + Name = option.Name, + Description = option.Description, + }; + + if (option is IModDataContainer data) + { + SubMod.Clone(data, newOption); + if (option is MultiSubMod m) + newOption.Priority = m.Priority; + else + newOption.Priority = new ModPriority(group.OptionData.Max(o => o.Priority.Value) + 1); + } + + group.OptionData.Add(newOption); + return newOption; + } + + protected override void RemoveOption(MultiModGroup group, int optionIndex) + { + group.OptionData.RemoveAt(optionIndex); + group.DefaultSettings = group.DefaultSettings.RemoveBit(optionIndex); + } + + protected override bool MoveOption(MultiModGroup group, int optionIdxFrom, int optionIdxTo) + { + if (!group.OptionData.Move(optionIdxFrom, optionIdxTo)) + return false; + + group.DefaultSettings = group.DefaultSettings.MoveBit(optionIdxFrom, optionIdxTo); + return true; + } +} diff --git a/Penumbra/Mods/Manager/SingleModGroupEditor.cs b/Penumbra/Mods/Manager/SingleModGroupEditor.cs new file mode 100644 index 00000000..4999ff60 --- /dev/null +++ b/Penumbra/Mods/Manager/SingleModGroupEditor.cs @@ -0,0 +1,57 @@ +using OtterGui.Classes; +using OtterGui.Filesystem; +using OtterGui.Services; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; +using Penumbra.Services; + +namespace Penumbra.Mods.Manager; + +public sealed class SingleModGroupEditor(CommunicatorService communicator, SaveService saveService, Configuration config) + : ModOptionEditor(communicator, saveService, config), IService +{ + public void ChangeToMulti(SingleModGroup group) + { + var idx = group.GetIndex(); + var multiGroup = group.ConvertToMulti(); + group.Mod.Groups[idx] = multiGroup; + SaveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupTypeChanged, group.Mod, multiGroup, null, null, -1); + } + + protected override SingleModGroup CreateGroup(Mod mod, string newName, ModPriority priority, SaveType saveType = SaveType.ImmediateSync) + => new(mod) + { + Name = newName, + Priority = priority, + }; + + protected override SingleSubMod CloneOption(SingleModGroup group, IModOption option) + { + var newOption = new SingleSubMod(group) + { + Name = option.Name, + Description = option.Description, + }; + if (option is IModDataContainer data) + SubMod.Clone(data, newOption); + group.OptionData.Add(newOption); + return newOption; + } + + protected override void RemoveOption(SingleModGroup group, int optionIndex) + { + group.OptionData.RemoveAt(optionIndex); + group.DefaultSettings = group.DefaultSettings.RemoveSingle(optionIndex); + } + + protected override bool MoveOption(SingleModGroup group, int optionIdxFrom, int optionIdxTo) + { + if (!group.OptionData.Move(optionIdxFrom, optionIdxTo)) + return false; + + group.DefaultSettings = group.DefaultSettings.MoveSingle(optionIdxFrom, optionIdxTo); + return true; + } +} diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index 40f943c8..47261c6d 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -90,7 +90,7 @@ public partial class ModCreator( var changes = false; foreach (var file in _saveService.FileNames.GetOptionGroupFiles(mod)) { - var group = LoadModGroup(mod, file, mod.Groups.Count); + var group = LoadModGroup(mod, file); if (group != null && mod.Groups.All(g => g.Name != group.Name)) { changes = changes @@ -244,12 +244,12 @@ public partial class ModCreator( { case GroupType.Multi: { - var group = MultiModGroup.CreateForSaving(name); + var group = MultiModGroup.WithoutMod(name); group.Description = desc; group.Priority = priority; group.DefaultSettings = defaultSettings; - group.OptionData.AddRange(subMods.Select(s => s.Clone(null!, group))); - _saveService.ImmediateSaveSync(new ModSaveGroup(baseFolder, group, index, Config.ReplaceNonAsciiOnImport)); + group.OptionData.AddRange(subMods.Select(s => s.Clone(group))); + _saveService.ImmediateSaveSync(ModSaveGroup.WithoutMod(baseFolder, group, index, Config.ReplaceNonAsciiOnImport)); break; } case GroupType.Single: @@ -258,8 +258,8 @@ public partial class ModCreator( group.Description = desc; group.Priority = priority; group.DefaultSettings = defaultSettings; - group.OptionData.AddRange(subMods.Select(s => s.ConvertToSingle(null!, group))); - _saveService.ImmediateSaveSync(new ModSaveGroup(baseFolder, group, index, Config.ReplaceNonAsciiOnImport)); + group.OptionData.AddRange(subMods.Select(s => s.ConvertToSingle(group))); + _saveService.ImmediateSaveSync(ModSaveGroup.WithoutMod(baseFolder, group, index, Config.ReplaceNonAsciiOnImport)); break; } } @@ -272,7 +272,7 @@ public partial class ModCreator( .Select(f => (Utf8GamePath.FromFile(f, optionFolder, out var gamePath, true), gamePath, new FullPath(f))) .Where(t => t.Item1); - var mod = MultiSubMod.CreateForSaving(option.Name, option.Description, priority); + var mod = MultiSubMod.WithoutGroup(option.Name, option.Description, priority); foreach (var (_, gamePath, file) in list) mod.Files.TryAdd(gamePath, file); @@ -295,7 +295,7 @@ public partial class ModCreator( } IncorporateMetaChanges(mod.Default, directory, true); - _saveService.ImmediateSaveSync(new ModSaveGroup(mod, -1, Config.ReplaceNonAsciiOnImport)); + _saveService.ImmediateSaveSync(new ModSaveGroup(mod.ModPath, mod.Default, Config.ReplaceNonAsciiOnImport)); } /// Return the name of a new valid directory based on the base directory and the given name. @@ -422,7 +422,7 @@ public partial class ModCreator( /// Load an option group for a specific mod by its file and index. - private static IModGroup? LoadModGroup(Mod mod, FileInfo file, int groupIdx) + private static IModGroup? LoadModGroup(Mod mod, FileInfo file) { if (!File.Exists(file.FullName)) return null; @@ -442,7 +442,7 @@ public partial class ModCreator( } return null; - } + } internal static void DeleteDeleteList(IEnumerable deleteList, bool delete) { diff --git a/Penumbra/Mods/Settings/ModSettings.cs b/Penumbra/Mods/Settings/ModSettings.cs index db9e0521..39ee1860 100644 --- a/Penumbra/Mods/Settings/ModSettings.cs +++ b/Penumbra/Mods/Settings/ModSettings.cs @@ -4,6 +4,7 @@ using Penumbra.Api.Enums; using Penumbra.Mods.Editor; using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; +using Penumbra.Mods.SubMods; namespace Penumbra.Mods.Settings; @@ -45,63 +46,64 @@ public class ModSettings } // Automatically react to changes in a mods available options. - public bool HandleChanges(ModOptionChangeType type, Mod mod, int groupIdx, int optionIdx, int movedToIdx) + public bool HandleChanges(ModOptionChangeType type, Mod mod, IModGroup? group, IModOption? option, int fromIdx) { switch (type) { case ModOptionChangeType.GroupRenamed: return true; case ModOptionChangeType.GroupAdded: // Add new empty setting for new mod. - Settings.Insert(groupIdx, mod.Groups[groupIdx].DefaultSettings); + Settings.Insert(group!.GetIndex(), group.DefaultSettings); return true; case ModOptionChangeType.GroupDeleted: // Remove setting for deleted mod. - Settings.RemoveAt(groupIdx); + Settings.RemoveAt(fromIdx); return true; case ModOptionChangeType.GroupTypeChanged: { // Fix settings for a changed group type. // Single -> Multi: set single as enabled, rest as disabled // Multi -> Single: set the first enabled option or 0. - var group = mod.Groups[groupIdx]; - var config = Settings[groupIdx]; - Settings[groupIdx] = group.Type switch + var idx = group!.GetIndex(); + var config = Settings[idx]; + Settings[idx] = group.Type switch { GroupType.Single => config.TurnMulti(group.Options.Count), GroupType.Multi => Setting.Multi((int)config.Value), _ => config, }; - return config != Settings[groupIdx]; + return config != Settings[idx]; } case ModOptionChangeType.OptionDeleted: { // Single -> select the previous option if any. // Multi -> excise the corresponding bit. - var group = mod.Groups[groupIdx]; - var config = Settings[groupIdx]; - Settings[groupIdx] = group.Type switch + var groupIdx = group!.GetIndex(); + var config = Settings[groupIdx]; + Settings[groupIdx] = group!.Type switch { - GroupType.Single => config.AsIndex >= optionIdx - ? config.AsIndex > 1 ? Setting.Single(config.AsIndex - 1) : Setting.Zero - : config, - GroupType.Multi => config.RemoveBit(optionIdx), - _ => config, + GroupType.Single => config.RemoveSingle(fromIdx), + GroupType.Multi => config.RemoveBit(fromIdx), + GroupType.Imc => config.RemoveBit(fromIdx), + _ => config, }; return config != Settings[groupIdx]; } case ModOptionChangeType.GroupMoved: // Move the group the same way. - return Settings.Move(groupIdx, movedToIdx); + return Settings.Move(fromIdx, group!.GetIndex()); case ModOptionChangeType.OptionMoved: { // Single -> select the moved option if it was currently selected // Multi -> move the corresponding bit - var group = mod.Groups[groupIdx]; - var config = Settings[groupIdx]; - Settings[groupIdx] = group.Type switch + var groupIdx = group!.GetIndex(); + var toIdx = option!.GetIndex(); + var config = Settings[groupIdx]; + Settings[groupIdx] = group!.Type switch { - GroupType.Single => config.AsIndex == optionIdx ? Setting.Single(movedToIdx) : config, - GroupType.Multi => config.MoveBit(optionIdx, movedToIdx), + GroupType.Single => config.MoveSingle(fromIdx, toIdx), + GroupType.Multi => config.MoveBit(fromIdx, toIdx), + GroupType.Imc => config.MoveBit(fromIdx, toIdx), _ => config, }; return config != Settings[groupIdx]; diff --git a/Penumbra/Mods/Settings/Setting.cs b/Penumbra/Mods/Settings/Setting.cs index 231529b8..059cbf51 100644 --- a/Penumbra/Mods/Settings/Setting.cs +++ b/Penumbra/Mods/Settings/Setting.cs @@ -41,6 +41,34 @@ public readonly record struct Setting(ulong Value) public Setting TurnMulti(int count) => new(Math.Max((ulong)Math.Min(count - 1, BitOperations.TrailingZeroCount(Value)), 0)); + public Setting RemoveSingle(int singleIdx) + { + var settingIndex = AsIndex; + if (settingIndex >= singleIdx) + return settingIndex > 1 ? Single(settingIndex - 1) : Zero; + + return this; + } + + public Setting MoveSingle(int singleIdxFrom, int singleIdxTo) + { + var currentIndex = AsIndex; + if (currentIndex == singleIdxFrom) + return Single(singleIdxTo); + + if (singleIdxFrom < singleIdxTo) + { + if (currentIndex > singleIdxFrom && currentIndex <= singleIdxTo) + return Single(currentIndex - 1); + } + else if (currentIndex < singleIdxFrom && currentIndex >= singleIdxTo) + { + return Single(currentIndex + 1); + } + + return this; + } + public ModPriority AsPriority => new((int)(Value & 0xFFFFFFFF)); diff --git a/Penumbra/Mods/SubMods/IModOption.cs b/Penumbra/Mods/SubMods/IModOption.cs index 83d632a0..ecfcf91a 100644 --- a/Penumbra/Mods/SubMods/IModOption.cs +++ b/Penumbra/Mods/SubMods/IModOption.cs @@ -1,10 +1,15 @@ +using Penumbra.Mods.Groups; + namespace Penumbra.Mods.SubMods; public interface IModOption { + public Mod Mod { get; } + public IModGroup Group { get; } + public string Name { get; set; } public string FullName { get; } public string Description { get; set; } - public (int GroupIndex, int OptionIndex) GetOptionIndices(); + public int GetIndex(); } diff --git a/Penumbra/Mods/SubMods/ImcSubMod.cs b/Penumbra/Mods/SubMods/ImcSubMod.cs new file mode 100644 index 00000000..167c8a6c --- /dev/null +++ b/Penumbra/Mods/SubMods/ImcSubMod.cs @@ -0,0 +1,32 @@ +using Penumbra.Mods.Groups; + +namespace Penumbra.Mods.SubMods; + +public class ImcSubMod(ImcModGroup group) : IModOption +{ + public readonly ImcModGroup Group = group; + + public Mod Mod + => Group.Mod; + + public byte AttributeIndex; + + public ushort Attribute + => (ushort)(1 << AttributeIndex); + + Mod IModOption.Mod + => Mod; + + IModGroup IModOption.Group + => Group; + + public string Name { get; set; } = "Part"; + + public string FullName + => $"{Group.Name}: {Name}"; + + public string Description { get; set; } = string.Empty; + + public int GetIndex() + => SubMod.GetIndex(this); +} diff --git a/Penumbra/Mods/SubMods/MultiSubMod.cs b/Penumbra/Mods/SubMods/MultiSubMod.cs index 3bcaffab..c01dcce9 100644 --- a/Penumbra/Mods/SubMods/MultiSubMod.cs +++ b/Penumbra/Mods/SubMods/MultiSubMod.cs @@ -4,21 +4,21 @@ using Penumbra.Mods.Settings; namespace Penumbra.Mods.SubMods; -public class MultiSubMod(Mod mod, MultiModGroup group) : OptionSubMod(mod, group) +public class MultiSubMod(MultiModGroup group) : OptionSubMod(group) { public ModPriority Priority { get; set; } = ModPriority.Default; - public MultiSubMod(Mod mod, MultiModGroup group, JToken json) - : this(mod, group) + public MultiSubMod(MultiModGroup group, JToken json) + : this(group) { SubMod.LoadOptionData(json, this); - SubMod.LoadDataContainer(json, this, mod.ModPath); + SubMod.LoadDataContainer(json, this, group.Mod.ModPath); Priority = json[nameof(IModGroup.Priority)]?.ToObject() ?? ModPriority.Default; } - public MultiSubMod Clone(Mod mod, MultiModGroup group) + public MultiSubMod Clone(MultiModGroup group) { - var ret = new MultiSubMod(mod, group) + var ret = new MultiSubMod(group) { Name = Name, Description = Description, @@ -29,9 +29,9 @@ public class MultiSubMod(Mod mod, MultiModGroup group) : OptionSubMod new(null!, null!) + public static MultiSubMod WithoutGroup(string name, string description, ModPriority priority) + => new(null!) { Name = name, Description = description, diff --git a/Penumbra/Mods/SubMods/OptionSubMod.cs b/Penumbra/Mods/SubMods/OptionSubMod.cs index fbf03243..02d86af2 100644 --- a/Penumbra/Mods/SubMods/OptionSubMod.cs +++ b/Penumbra/Mods/SubMods/OptionSubMod.cs @@ -1,25 +1,26 @@ -using OtterGui; -using Penumbra.Meta.Manipulations; -using Penumbra.Mods.Editor; -using Penumbra.Mods.Groups; -using Penumbra.String.Classes; - +using OtterGui; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; +using Penumbra.Mods.Groups; +using Penumbra.String.Classes; + namespace Penumbra.Mods.SubMods; -public interface IModDataOption : IModDataContainer, IModOption; - -public abstract class OptionSubMod(Mod mod, T group) : IModDataOption - where T : IModGroup +public abstract class OptionSubMod(IModGroup group) : IModOption, IModDataContainer { - internal readonly Mod Mod = mod; - internal readonly IModGroup Group = group; + protected readonly IModGroup Group = group; - public string Name { get; set; } = "Option"; + public Mod Mod + => Group.Mod; + + public string Name { get; set; } = "Option"; + public string Description { get; set; } = string.Empty; public string FullName - => $"{Group!.Name}: {Name}"; + => $"{Group.Name}: {Name}"; - public string Description { get; set; } = string.Empty; + Mod IModOption.Mod + => Mod; IMod IModDataContainer.Mod => Mod; @@ -27,6 +28,9 @@ public abstract class OptionSubMod(Mod mod, T group) : IModDataOption IModGroup IModDataContainer.Group => Group; + IModGroup IModOption.Group + => Group; + public Dictionary Files { get; set; } = []; public Dictionary FileSwaps { get; set; } = []; public HashSet Manipulations { get; set; } = []; @@ -43,8 +47,8 @@ public abstract class OptionSubMod(Mod mod, T group) : IModDataOption public (int GroupIndex, int DataIndex) GetDataIndices() => (Group.GetIndex(), GetDataIndex()); - public (int GroupIndex, int OptionIndex) GetOptionIndices() - => (Group.GetIndex(), GetDataIndex()); + public int GetIndex() + => SubMod.GetIndex(this); private int GetDataIndex() { @@ -54,4 +58,11 @@ public abstract class OptionSubMod(Mod mod, T group) : IModDataOption return dataIndex; } -} +} + +public abstract class OptionSubMod(T group) : OptionSubMod(group) + where T : IModGroup +{ + public new T Group + => (T)base.Group; +} diff --git a/Penumbra/Mods/SubMods/SingleSubMod.cs b/Penumbra/Mods/SubMods/SingleSubMod.cs index 98c56151..675f37bc 100644 --- a/Penumbra/Mods/SubMods/SingleSubMod.cs +++ b/Penumbra/Mods/SubMods/SingleSubMod.cs @@ -4,18 +4,18 @@ using Penumbra.Mods.Settings; namespace Penumbra.Mods.SubMods; -public class SingleSubMod(Mod mod, SingleModGroup singleGroup) : OptionSubMod(mod, singleGroup) +public class SingleSubMod(SingleModGroup singleGroup) : OptionSubMod(singleGroup) { - public SingleSubMod(Mod mod, SingleModGroup singleGroup, JToken json) - : this(mod, singleGroup) + public SingleSubMod(SingleModGroup singleGroup, JToken json) + : this(singleGroup) { SubMod.LoadOptionData(json, this); - SubMod.LoadDataContainer(json, this, mod.ModPath); + SubMod.LoadDataContainer(json, this, singleGroup.Mod.ModPath); } - public SingleSubMod Clone(Mod mod, SingleModGroup group) + public SingleSubMod Clone(SingleModGroup group) { - var ret = new SingleSubMod(mod, group) + var ret = new SingleSubMod(group) { Name = Name, Description = Description, @@ -25,9 +25,9 @@ public class SingleSubMod(Mod mod, SingleModGroup singleGroup) : OptionSubMod group switch - { - SingleModGroup single => new SingleSubMod(group.Mod, single) - { - Name = name, - Description = description, - }, - MultiModGroup multi => new MultiSubMod(group.Mod, multi) - { - Name = name, - Description = description, - }, - _ => throw new ArgumentOutOfRangeException(nameof(group)), - }; + [MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)] + public static int GetIndex(IModOption option) + { + var dataIndex = option.Group.Options.IndexOf(option); + if (dataIndex < 0) + throw new Exception($"Group {option.Group.Name} from option {option.Name} does not contain this option."); + + return dataIndex; + } /// Add all unique meta manipulations, file redirections and then file swaps from a ModDataContainer to the given sets. Skip any keys that are already contained. + [MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)] public static void AddContainerTo(IModDataContainer container, Dictionary redirections, HashSet manipulations) { @@ -37,6 +32,7 @@ public static class SubMod } /// Replace all data of with the data of . + [MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)] public static void Clone(IModDataContainer from, IModDataContainer to) { to.Files = new Dictionary(from.Files); @@ -45,6 +41,7 @@ public static class SubMod } /// Load all file redirections, file swaps and meta manipulations from a JToken of that option into a data container. + [MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)] public static void LoadDataContainer(JToken json, IModDataContainer data, DirectoryInfo basePath) { data.Files.Clear(); @@ -75,6 +72,7 @@ public static class SubMod } /// Load the relevant data for a selectable option from a JToken of that option. + [MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)] public static void LoadOptionData(JToken json, IModOption option) { option.Name = json[nameof(option.Name)]?.ToObject() ?? string.Empty; @@ -82,6 +80,7 @@ public static class SubMod } /// Write file redirections, file swaps and meta manipulations from a data container on a JsonWriter. + [MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)] public static void WriteModContainer(JsonWriter j, JsonSerializer serializer, IModDataContainer data, DirectoryInfo basePath) { j.WritePropertyName(nameof(data.Files)); @@ -111,6 +110,7 @@ public static class SubMod } /// Write the data for a selectable mod option on a JsonWriter. + [MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)] public static void WriteModOption(JsonWriter j, IModOption option) { j.WritePropertyName(nameof(option.Name)); diff --git a/Penumbra/Penumbra.csproj b/Penumbra/Penumbra.csproj index 0ec1fd44..2d595ec1 100644 --- a/Penumbra/Penumbra.csproj +++ b/Penumbra/Penumbra.csproj @@ -23,6 +23,12 @@ PROFILING; + + + + + + PreserveNewest @@ -93,10 +99,6 @@ - - - - diff --git a/Penumbra/Services/SaveService.cs b/Penumbra/Services/SaveService.cs index 8d3cb641..eff3295d 100644 --- a/Penumbra/Services/SaveService.cs +++ b/Penumbra/Services/SaveService.cs @@ -34,8 +34,11 @@ public sealed class SaveService(Logger log, FrameworkManager framework, Filename } } - for (var i = 0; i < mod.Groups.Count - 1; ++i) - ImmediateSave(new ModSaveGroup(mod, i, onlyAscii)); - ImmediateSaveSync(new ModSaveGroup(mod, mod.Groups.Count - 1, onlyAscii)); + if (mod.Groups.Count > 0) + { + foreach (var group in mod.Groups.SkipLast(1)) + ImmediateSave(new ModSaveGroup(group, onlyAscii)); + ImmediateSaveSync(new ModSaveGroup(mod.Groups[^1], onlyAscii)); + } } } diff --git a/Penumbra/Services/StaticServiceManager.cs b/Penumbra/Services/StaticServiceManager.cs index e758aa35..5fa1a848 100644 --- a/Penumbra/Services/StaticServiceManager.cs +++ b/Penumbra/Services/StaticServiceManager.cs @@ -121,7 +121,6 @@ public static class StaticServiceManager private static ServiceManager AddMods(this ServiceManager services) => services.AddSingleton() .AddSingleton() - .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() diff --git a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs index 77bdb161..cd55beb0 100644 --- a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs +++ b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs @@ -17,6 +17,7 @@ using Penumbra.Mods.Groups; using Penumbra.Mods.ItemSwap; using Penumbra.Mods.Manager; using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.UI.Classes; @@ -264,9 +265,10 @@ public class ItemSwapTab : IDisposable, ITab return; _modManager.AddMod(newDir); - if (!_swapData.WriteMod(_modManager, _modManager[^1], + var mod = _modManager[^1]; + if (!_swapData.WriteMod(_modManager, mod, mod.Default, _useFileSwaps ? ItemSwapContainer.WriteType.UseSwaps : ItemSwapContainer.WriteType.NoSwaps)) - _modManager.DeleteMod(_modManager[^1]); + _modManager.DeleteMod(mod); } private void CreateOption() @@ -276,7 +278,7 @@ public class ItemSwapTab : IDisposable, ITab var groupCreated = false; var dirCreated = false; - var optionCreated = -1; + IModOption? createdOption = null; DirectoryInfo? optionFolderName = null; try { @@ -290,22 +292,22 @@ public class ItemSwapTab : IDisposable, ITab { if (_selectedGroup == null) { - _modManager.OptionEditor.AddModGroup(_mod, GroupType.Multi, _newGroupName); - _selectedGroup = _mod.Groups.Last(); + if (_modManager.OptionEditor.AddModGroup(_mod, GroupType.Multi, _newGroupName) is not { } group) + throw new Exception($"Failure creating option group."); + + _selectedGroup = group; groupCreated = true; } - var optionIdx = _modManager.OptionEditor.AddOption(_mod, _mod.Groups.IndexOf(_selectedGroup), _newOptionName); - if (optionIdx < 0) + if (_modManager.OptionEditor.AddOption(_selectedGroup, _newOptionName) is not { } option) throw new Exception($"Failure creating mod option."); - optionCreated = optionIdx; + createdOption = option; optionFolderName = Directory.CreateDirectory(optionFolderName.FullName); dirCreated = true; - if (!_swapData.WriteMod(_modManager, _mod, - _useFileSwaps ? ItemSwapContainer.WriteType.UseSwaps : ItemSwapContainer.WriteType.NoSwaps, - optionFolderName, - _mod.Groups.IndexOf(_selectedGroup), optionIdx)) + // #TODO ModOption <> DataContainer + if (!_swapData.WriteMod(_modManager, _mod, (IModDataContainer)option, + _useFileSwaps ? ItemSwapContainer.WriteType.UseSwaps : ItemSwapContainer.WriteType.NoSwaps, optionFolderName)) throw new Exception("Failure writing files for mod swap."); } } @@ -314,12 +316,12 @@ public class ItemSwapTab : IDisposable, ITab Penumbra.Messager.NotificationMessage(e, "Could not create new Swap Option.", NotificationType.Error, false); try { - if (optionCreated >= 0 && _selectedGroup != null) - _modManager.OptionEditor.DeleteOption(_mod, _mod.Groups.IndexOf(_selectedGroup), optionCreated); + if (createdOption != null) + _modManager.OptionEditor.DeleteOption(createdOption); if (groupCreated) { - _modManager.OptionEditor.DeleteModGroup(_mod, _mod.Groups.IndexOf(_selectedGroup!)); + _modManager.OptionEditor.DeleteModGroup(_selectedGroup!); _selectedGroup = null; } @@ -717,7 +719,8 @@ public class ItemSwapTab : IDisposable, ITab _dirty = true; } - private void OnModOptionChange(ModOptionChangeType type, Mod mod, int a, int b, int c) + private void OnModOptionChange(ModOptionChangeType type, Mod mod, IModGroup? group, IModOption? option, IModDataContainer? container, + int fromIdx) { if (type is ModOptionChangeType.PrepareChange or ModOptionChangeType.GroupAdded or ModOptionChangeType.OptionAdded || mod != _mod) return; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs index 92a9dd66..743310ea 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs @@ -27,7 +27,7 @@ public partial class ModEditWindow private const string GenderTooltip = "Gender"; private const string ObjectTypeTooltip = "Object Type"; private const string SecondaryIdTooltip = "Secondary ID"; - private const string PrimaryIDTooltip = "Primary ID"; + private const string PrimaryIdTooltipShort = "Primary ID"; private const string VariantIdTooltip = "Variant ID"; private const string EstTypeTooltip = "EST Type"; private const string RacialTribeTooltip = "Racial Tribe"; @@ -45,7 +45,7 @@ public partial class ModEditWindow var tt = setsEqual ? "No changes staged." : "Apply the currently staged changes to the option."; ImGui.NewLine(); if (ImGuiUtil.DrawDisabledButton("Apply Changes", Vector2.Zero, tt, setsEqual)) - _editor.MetaEditor.Apply(_editor.Mod!, _editor.GroupIdx, _editor.DataIdx); + _editor.MetaEditor.Apply(_editor.Option!); ImGui.SameLine(); tt = setsEqual ? "No changes staged." : "Revert all currently staged changes."; @@ -477,7 +477,7 @@ public partial class ModEditWindow ImGui.TableNextColumn(); ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); ImGui.TextUnformatted(meta.PrimaryId.ToString()); - ImGuiUtil.HoverTooltip(PrimaryIDTooltip); + ImGuiUtil.HoverTooltip(PrimaryIdTooltipShort); ImGui.TableNextColumn(); ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index dbb88fb7..6b48a048 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -455,7 +455,7 @@ public partial class ModEditWindow : Window, IDisposable var tt = setsEqual ? "No changes staged." : "Apply the currently staged changes to the option."; ImGui.NewLine(); if (ImGuiUtil.DrawDisabledButton("Apply Changes", Vector2.Zero, tt, setsEqual)) - _editor.SwapEditor.Apply(_editor.Mod!, _editor.GroupIdx, _editor.DataIdx); + _editor.SwapEditor.Apply(_editor.Option!); ImGui.SameLine(); tt = setsEqual ? "No changes staged." : "Revert all currently staged changes."; @@ -627,7 +627,7 @@ public partial class ModEditWindow : Window, IDisposable public void Dispose() { _communicator.ModPathChanged.Unsubscribe(OnModPathChange); - _editor?.Dispose(); + _editor.Dispose(); _materialTab.Dispose(); _modelTab.Dispose(); _shaderPackageTab.Dispose(); diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index afbef45d..fcd76a51 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -15,6 +15,7 @@ using Penumbra.Services; using Penumbra.UI.AdvancedWindow; using Penumbra.Mods.Groups; using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; namespace Penumbra.UI.ModsTab; @@ -248,13 +249,13 @@ public class ModPanelEditTab( ImGui.SameLine(); - var nameValid = ModOptionEditor.VerifyFileName(mod, null, _newGroupName, false); + var nameValid = ModGroupEditor.VerifyFileName(mod, null, _newGroupName, false); tt = nameValid ? "Add new option group to the mod." : "Can not add a group of this name."; if (!ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), UiHelpers.IconButtonSize, tt, !nameValid, true)) return; - modManager.OptionEditor.AddModGroup(mod, GroupType.Single, _newGroupName); + modManager.OptionEditor.SingleEditor.AddModGroup(mod, _newGroupName); Reset(); } } @@ -364,9 +365,9 @@ public class ModPanelEditTab( break; case >= 0: if (_newDescriptionOptionIdx < 0) - modManager.OptionEditor.ChangeGroupDescription(_mod, _newDescriptionIdx, _newDescription); + modManager.OptionEditor.ChangeGroupDescription(_mod.Groups[_newDescriptionIdx], _newDescription); else - modManager.OptionEditor.ChangeOptionDescription(_mod, _newDescriptionIdx, _newDescriptionOptionIdx, + modManager.OptionEditor.ChangeOptionDescription(_mod.Groups[_newDescriptionIdx].Options[_newDescriptionOptionIdx], _newDescription); break; @@ -396,18 +397,18 @@ public class ModPanelEditTab( .Push(ImGuiStyleVar.ItemSpacing, _itemSpacing); if (Input.Text("##Name", groupIdx, Input.None, group.Name, out var newGroupName, 256, UiHelpers.InputTextWidth.X)) - _modManager.OptionEditor.RenameModGroup(_mod, groupIdx, newGroupName); + _modManager.OptionEditor.RenameModGroup(group, newGroupName); ImGuiUtil.HoverTooltip("Group Name"); ImGui.SameLine(); if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Trash.ToIconString(), UiHelpers.IconButtonSize, "Delete this option group.\nHold Control while clicking to delete.", !ImGui.GetIO().KeyCtrl, true)) - _delayedActions.Enqueue(() => _modManager.OptionEditor.DeleteModGroup(_mod, groupIdx)); + _delayedActions.Enqueue(() => _modManager.OptionEditor.DeleteModGroup(group)); ImGui.SameLine(); if (Input.Priority("##Priority", groupIdx, Input.None, group.Priority, out var priority, 50 * UiHelpers.Scale)) - _modManager.OptionEditor.ChangeGroupPriority(_mod, groupIdx, priority); + _modManager.OptionEditor.ChangeGroupPriority(group, priority); ImGuiUtil.HoverTooltip("Group Priority"); @@ -417,7 +418,7 @@ public class ModPanelEditTab( var tt = groupIdx == 0 ? "Can not move this group further upwards." : $"Move this group up to group {groupIdx}."; if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.ArrowUp.ToIconString(), UiHelpers.IconButtonSize, tt, groupIdx == 0, true)) - _delayedActions.Enqueue(() => _modManager.OptionEditor.MoveModGroup(_mod, groupIdx, groupIdx - 1)); + _delayedActions.Enqueue(() => _modManager.OptionEditor.MoveModGroup(group, groupIdx - 1)); ImGui.SameLine(); tt = groupIdx == _mod.Groups.Count - 1 @@ -425,7 +426,7 @@ public class ModPanelEditTab( : $"Move this group down to group {groupIdx + 2}."; if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.ArrowDown.ToIconString(), UiHelpers.IconButtonSize, tt, groupIdx == _mod.Groups.Count - 1, true)) - _delayedActions.Enqueue(() => _modManager.OptionEditor.MoveModGroup(_mod, groupIdx, groupIdx + 1)); + _delayedActions.Enqueue(() => _modManager.OptionEditor.MoveModGroup(group, groupIdx + 1)); ImGui.SameLine(); @@ -452,17 +453,17 @@ public class ModPanelEditTab( { private const string DragDropLabel = "##DragOption"; - private static int _newOptionNameIdx = -1; - private static string _newOptionName = string.Empty; - private static int _dragDropGroupIdx = -1; - private static int _dragDropOptionIdx = -1; + private static int _newOptionNameIdx = -1; + private static string _newOptionName = string.Empty; + private static IModGroup? _dragDropGroup; + private static IModOption? _dragDropOption; public static void Reset() { - _newOptionNameIdx = -1; - _newOptionName = string.Empty; - _dragDropGroupIdx = -1; - _dragDropOptionIdx = -1; + _newOptionNameIdx = -1; + _newOptionName = string.Empty; + _dragDropGroup = null; + _dragDropOption = null; } public static void Draw(ModPanelEditTab panel, int groupIdx) @@ -482,7 +483,7 @@ public class ModPanelEditTab( switch (panel._mod.Groups[groupIdx]) { - case SingleModGroup single: + case SingleModGroup single: for (var optionIdx = 0; optionIdx < single.OptionData.Count; ++optionIdx) EditOption(panel, single, groupIdx, optionIdx); break; @@ -491,6 +492,7 @@ public class ModPanelEditTab( EditOption(panel, multi, groupIdx, optionIdx); break; } + DrawNewOption(panel, groupIdx, UiHelpers.IconButtonSize); } @@ -502,8 +504,8 @@ public class ModPanelEditTab( ImGui.TableNextColumn(); ImGui.AlignTextToFramePadding(); ImGui.Selectable($"Option #{optionIdx + 1}"); - Source(group, groupIdx, optionIdx); - Target(panel, group, groupIdx, optionIdx); + Source(option); + Target(panel, group, optionIdx); ImGui.TableNextColumn(); @@ -511,7 +513,7 @@ public class ModPanelEditTab( if (group.Type == GroupType.Single) { if (ImGui.RadioButton("##default", group.DefaultSettings.AsIndex == optionIdx)) - panel._modManager.OptionEditor.ChangeModGroupDefaultOption(panel._mod, groupIdx, Setting.Single(optionIdx)); + panel._modManager.OptionEditor.ChangeModGroupDefaultOption(group, Setting.Single(optionIdx)); ImGuiUtil.HoverTooltip($"Set {option.Name} as the default choice for this group."); } @@ -519,15 +521,14 @@ public class ModPanelEditTab( { var isDefaultOption = group.DefaultSettings.HasFlag(optionIdx); if (ImGui.Checkbox("##default", ref isDefaultOption)) - panel._modManager.OptionEditor.ChangeModGroupDefaultOption(panel._mod, groupIdx, - group.DefaultSettings.SetBit(optionIdx, isDefaultOption)); + panel._modManager.OptionEditor.ChangeModGroupDefaultOption(group, group.DefaultSettings.SetBit(optionIdx, isDefaultOption)); ImGuiUtil.HoverTooltip($"{(isDefaultOption ? "Disable" : "Enable")} {option.Name} per default in this group."); } ImGui.TableNextColumn(); if (Input.Text("##Name", groupIdx, optionIdx, option.Name, out var newOptionName, 256, -1)) - panel._modManager.OptionEditor.RenameOption(panel._mod, groupIdx, optionIdx, newOptionName); + panel._modManager.OptionEditor.RenameOption(option, newOptionName); ImGui.TableNextColumn(); if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Edit.ToIconString(), UiHelpers.IconButtonSize, "Edit option description.", @@ -537,15 +538,15 @@ public class ModPanelEditTab( ImGui.TableNextColumn(); if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Trash.ToIconString(), UiHelpers.IconButtonSize, "Delete this option.\nHold Control while clicking to delete.", !ImGui.GetIO().KeyCtrl, true)) - panel._delayedActions.Enqueue(() => panel._modManager.OptionEditor.DeleteOption(panel._mod, groupIdx, optionIdx)); + panel._delayedActions.Enqueue(() => panel._modManager.OptionEditor.DeleteOption(option)); ImGui.TableNextColumn(); - if (group is not MultiModGroup multi) + if (option is not MultiSubMod multi) return; - if (Input.Priority("##Priority", groupIdx, optionIdx, multi.OptionData[optionIdx].Priority, out var priority, + if (Input.Priority("##Priority", groupIdx, optionIdx, multi.Priority, out var priority, 50 * UiHelpers.Scale)) - panel._modManager.OptionEditor.ChangeOptionPriority(panel._mod, groupIdx, optionIdx, priority); + panel._modManager.OptionEditor.MultiEditor.ChangeOptionPriority(multi, priority); ImGuiUtil.HoverTooltip("Option priority."); } @@ -564,7 +565,7 @@ public class ModPanelEditTab( ImGui.TableNextColumn(); ImGui.AlignTextToFramePadding(); ImGui.Selectable($"Option #{count + 1}"); - Target(panel, group, groupIdx, count); + Target(panel, group, count); ImGui.TableNextColumn(); ImGui.TableNextColumn(); ImGui.SetNextItemWidth(-1); @@ -585,14 +586,14 @@ public class ModPanelEditTab( tt, !(canAddGroup && validName), true)) return; - panel._modManager.OptionEditor.AddOption(mod, groupIdx, _newOptionName); + panel._modManager.OptionEditor.AddOption(group, _newOptionName); _newOptionName = string.Empty; } // Handle drag and drop to move options inside a group or into another group. - private static void Source(IModGroup group, int groupIdx, int optionIdx) + private static void Source(IModOption option) { - if (group is not ITexToolsGroup) + if (option.Group is not ITexToolsGroup) return; using var source = ImRaii.DragDropSource(); @@ -601,14 +602,14 @@ public class ModPanelEditTab( if (ImGui.SetDragDropPayload(DragDropLabel, IntPtr.Zero, 0)) { - _dragDropGroupIdx = groupIdx; - _dragDropOptionIdx = optionIdx; + _dragDropGroup = option.Group; + _dragDropOption = option; } - ImGui.TextUnformatted($"Dragging option {group.Options[optionIdx].Name} from group {group.Name}..."); + ImGui.TextUnformatted($"Dragging option {option.Name} from group {option.Group.Name}..."); } - private static void Target(ModPanelEditTab panel, IModGroup group, int groupIdx, int optionIdx) + private static void Target(ModPanelEditTab panel, IModGroup group, int optionIdx) { if (group is not ITexToolsGroup) return; @@ -617,39 +618,53 @@ public class ModPanelEditTab( if (!target.Success || !ImGuiUtil.IsDropping(DragDropLabel)) return; - if (_dragDropGroupIdx >= 0 && _dragDropOptionIdx >= 0) + if (_dragDropGroup != null && _dragDropOption != null) { - if (_dragDropGroupIdx == groupIdx) + if (_dragDropGroup == group) { - var sourceOption = _dragDropOptionIdx; + var sourceOption = _dragDropOption; panel._delayedActions.Enqueue( - () => panel._modManager.OptionEditor.MoveOption(panel._mod, groupIdx, sourceOption, optionIdx)); + () => panel._modManager.OptionEditor.MoveOption(sourceOption, optionIdx)); } else { // Move from one group to another by deleting, then adding, then moving the option. - var sourceGroupIdx = _dragDropGroupIdx; - var sourceOption = _dragDropOptionIdx; - var sourceGroup = panel._mod.Groups[sourceGroupIdx]; - var currentCount = group.DataContainers.Count; - var option = ((ITexToolsGroup) sourceGroup).OptionData[_dragDropOptionIdx]; + var sourceOption = _dragDropOption; panel._delayedActions.Enqueue(() => { - panel._modManager.OptionEditor.DeleteOption(panel._mod, sourceGroupIdx, sourceOption); - panel._modManager.OptionEditor.AddOption(panel._mod, groupIdx, option); - panel._modManager.OptionEditor.MoveOption(panel._mod, groupIdx, currentCount, optionIdx); + panel._modManager.OptionEditor.DeleteOption(sourceOption); + if (panel._modManager.OptionEditor.AddOption(group, sourceOption) is { } newOption) + panel._modManager.OptionEditor.MoveOption(newOption, optionIdx); }); } } - _dragDropGroupIdx = -1; - _dragDropOptionIdx = -1; + _dragDropGroup = null; + _dragDropOption = null; } } /// Draw a combo to select single or multi group and switch between them. private void DrawGroupCombo(IModGroup group, int groupIdx) { + ImGui.SetNextItemWidth(UiHelpers.InputTextWidth.X - 2 * UiHelpers.IconButtonSize.X - 2 * ImGui.GetStyle().ItemSpacing.X); + using var combo = ImRaii.Combo("##GroupType", GroupTypeName(group.Type)); + if (!combo) + return; + + if (ImGui.Selectable(GroupTypeName(GroupType.Single), group.Type == GroupType.Single) && group is MultiModGroup m) + _modManager.OptionEditor.MultiEditor.ChangeToSingle(m); + + var canSwitchToMulti = group.Options.Count <= IModGroup.MaxMultiOptions; + using var style = ImRaii.PushStyle(ImGuiStyleVar.Alpha, 0.5f, !canSwitchToMulti); + if (ImGui.Selectable(GroupTypeName(GroupType.Multi), group.Type == GroupType.Multi) && canSwitchToMulti && group is SingleModGroup s) + _modManager.OptionEditor.SingleEditor.ChangeToMulti(s); + + style.Pop(); + if (!canSwitchToMulti) + ImGuiUtil.HoverTooltip($"Can not convert group to multi group since it has more than {IModGroup.MaxMultiOptions} options."); + return; + static string GroupTypeName(GroupType type) => type switch { @@ -657,23 +672,6 @@ public class ModPanelEditTab( GroupType.Multi => "Multi Group", _ => "Unknown", }; - - ImGui.SetNextItemWidth(UiHelpers.InputTextWidth.X - 2 * UiHelpers.IconButtonSize.X - 2 * ImGui.GetStyle().ItemSpacing.X); - using var combo = ImRaii.Combo("##GroupType", GroupTypeName(group.Type)); - if (!combo) - return; - - if (ImGui.Selectable(GroupTypeName(GroupType.Single), group.Type == GroupType.Single)) - _modManager.OptionEditor.ChangeModGroupType(_mod, groupIdx, GroupType.Single); - - var canSwitchToMulti = group.Options.Count <= IModGroup.MaxMultiOptions; - using var style = ImRaii.PushStyle(ImGuiStyleVar.Alpha, 0.5f, !canSwitchToMulti); - if (ImGui.Selectable(GroupTypeName(GroupType.Multi), group.Type == GroupType.Multi) && canSwitchToMulti) - _modManager.OptionEditor.ChangeModGroupType(_mod, groupIdx, GroupType.Multi); - - style.Pop(); - if (!canSwitchToMulti) - ImGuiUtil.HoverTooltip($"Can not convert group to multi group since it has more than {IModGroup.MaxMultiOptions} options."); } /// Handles input text and integers in separate fields without buffers for every single one. From cff617245356a3721d3e5849585e89856cbebcc5 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 27 Apr 2024 00:07:10 +0200 Subject: [PATCH 140/433] Move editors into folder. --- OtterGui | 2 +- Penumbra/Api/Api/ModSettingsApi.cs | 1 + .../Cache/CollectionCacheManager.cs | 1 + .../Collections/Manager/CollectionStorage.cs | 1 + Penumbra/Communication/ModOptionChanged.cs | 1 + Penumbra/Mods/Editor/ModMerger.cs | 1 + Penumbra/Mods/Manager/ModCacheManager.cs | 1 + Penumbra/Mods/Manager/ModManager.cs | 1 + .../{ => OptionEditor}/ImcModGroupEditor.cs | 4 +- .../{ => OptionEditor}/ModGroupEditor.cs | 37 +++++++------- .../{ => OptionEditor}/ModOptionEditor.cs | 50 +++++++++---------- .../{ => OptionEditor}/MultiModGroupEditor.cs | 8 +-- .../SingleModGroupEditor.cs | 8 +-- Penumbra/Mods/Settings/ModSettings.cs | 2 +- Penumbra/Penumbra.csproj | 2 +- Penumbra/UI/AdvancedWindow/ItemSwapTab.cs | 1 + Penumbra/UI/ModsTab/ModPanelEditTab.cs | 1 + Penumbra/packages.lock.json | 14 ++++-- 18 files changed, 74 insertions(+), 62 deletions(-) rename Penumbra/Mods/Manager/{ => OptionEditor}/ImcModGroupEditor.cs (91%) rename Penumbra/Mods/Manager/{ => OptionEditor}/ModGroupEditor.cs (88%) rename Penumbra/Mods/Manager/{ => OptionEditor}/ModOptionEditor.cs (73%) rename Penumbra/Mods/Manager/{ => OptionEditor}/MultiModGroupEditor.cs (92%) rename Penumbra/Mods/Manager/{ => OptionEditor}/SingleModGroupEditor.cs (90%) diff --git a/OtterGui b/OtterGui index 3460a817..20c4a6c5 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 3460a817fc5e01a6b60eb834c3c59031938388fc +Subproject commit 20c4a6c53103d9fa8dec63babc628c9d01f094c0 diff --git a/Penumbra/Api/Api/ModSettingsApi.cs b/Penumbra/Api/Api/ModSettingsApi.cs index bfd134bb..56b80e63 100644 --- a/Penumbra/Api/Api/ModSettingsApi.cs +++ b/Penumbra/Api/Api/ModSettingsApi.cs @@ -10,6 +10,7 @@ using Penumbra.Mods; using Penumbra.Mods.Editor; using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; +using Penumbra.Mods.Manager.OptionEditor; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; diff --git a/Penumbra/Collections/Cache/CollectionCacheManager.cs b/Penumbra/Collections/Cache/CollectionCacheManager.cs index 9c104cef..fb9ee9a3 100644 --- a/Penumbra/Collections/Cache/CollectionCacheManager.cs +++ b/Penumbra/Collections/Cache/CollectionCacheManager.cs @@ -9,6 +9,7 @@ using Penumbra.Meta; using Penumbra.Mods; using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; +using Penumbra.Mods.Manager.OptionEditor; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; diff --git a/Penumbra/Collections/Manager/CollectionStorage.cs b/Penumbra/Collections/Manager/CollectionStorage.cs index bfae2dc0..de5d0a14 100644 --- a/Penumbra/Collections/Manager/CollectionStorage.cs +++ b/Penumbra/Collections/Manager/CollectionStorage.cs @@ -6,6 +6,7 @@ using Penumbra.Mods; using Penumbra.Mods.Editor; using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; +using Penumbra.Mods.Manager.OptionEditor; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; diff --git a/Penumbra/Communication/ModOptionChanged.cs b/Penumbra/Communication/ModOptionChanged.cs index a20592ec..67f2c0c3 100644 --- a/Penumbra/Communication/ModOptionChanged.cs +++ b/Penumbra/Communication/ModOptionChanged.cs @@ -3,6 +3,7 @@ using Penumbra.Api.Api; using Penumbra.Mods; using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; +using Penumbra.Mods.Manager.OptionEditor; using Penumbra.Mods.SubMods; using static Penumbra.Communication.ModOptionChanged; diff --git a/Penumbra/Mods/Editor/ModMerger.cs b/Penumbra/Mods/Editor/ModMerger.cs index 3a6f4a81..d6e21076 100644 --- a/Penumbra/Mods/Editor/ModMerger.cs +++ b/Penumbra/Mods/Editor/ModMerger.cs @@ -5,6 +5,7 @@ using OtterGui.Classes; using Penumbra.Api.Enums; using Penumbra.Communication; using Penumbra.Mods.Manager; +using Penumbra.Mods.Manager.OptionEditor; using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.String.Classes; diff --git a/Penumbra/Mods/Manager/ModCacheManager.cs b/Penumbra/Mods/Manager/ModCacheManager.cs index 0669696f..59c88cf0 100644 --- a/Penumbra/Mods/Manager/ModCacheManager.cs +++ b/Penumbra/Mods/Manager/ModCacheManager.cs @@ -3,6 +3,7 @@ using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Groups; +using Penumbra.Mods.Manager.OptionEditor; using Penumbra.Mods.SubMods; using Penumbra.Services; diff --git a/Penumbra/Mods/Manager/ModManager.cs b/Penumbra/Mods/Manager/ModManager.cs index 7a266a31..adaca85e 100644 --- a/Penumbra/Mods/Manager/ModManager.cs +++ b/Penumbra/Mods/Manager/ModManager.cs @@ -1,5 +1,6 @@ using Penumbra.Communication; using Penumbra.Mods.Editor; +using Penumbra.Mods.Manager.OptionEditor; using Penumbra.Services; namespace Penumbra.Mods.Manager; diff --git a/Penumbra/Mods/Manager/ImcModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs similarity index 91% rename from Penumbra/Mods/Manager/ImcModGroupEditor.cs rename to Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs index 4e2b2194..1194f961 100644 --- a/Penumbra/Mods/Manager/ImcModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs @@ -6,7 +6,7 @@ using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; -namespace Penumbra.Mods.Manager; +namespace Penumbra.Mods.Manager.OptionEditor; public sealed class ImcModGroupEditor(CommunicatorService communicator, SaveService saveService, Configuration config) : ModOptionEditor(communicator, saveService, config), IService @@ -14,7 +14,7 @@ public sealed class ImcModGroupEditor(CommunicatorService communicator, SaveServ protected override ImcModGroup CreateGroup(Mod mod, string newName, ModPriority priority, SaveType saveType = SaveType.ImmediateSync) => new(mod) { - Name = newName, + Name = newName, Priority = priority, }; diff --git a/Penumbra/Mods/Manager/ModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs similarity index 88% rename from Penumbra/Mods/Manager/ModGroupEditor.cs rename to Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs index 9f41fa6f..b2b48ac0 100644 --- a/Penumbra/Mods/Manager/ModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs @@ -1,10 +1,8 @@ -using System.Text.RegularExpressions; using Dalamud.Interface.Internal.Notifications; using OtterGui.Classes; using OtterGui.Filesystem; using OtterGui.Services; using Penumbra.Api.Enums; -using Penumbra.GameData.Data; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Groups; using Penumbra.Mods.Settings; @@ -12,9 +10,8 @@ using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.String.Classes; using Penumbra.Util; -using static FFXIVClientStructs.FFXIV.Client.UI.Misc.ConfigModule; -namespace Penumbra.Mods.Manager; +namespace Penumbra.Mods.Manager.OptionEditor; public enum ModOptionChangeType { @@ -91,7 +88,7 @@ public class ModGroupEditor( /// Move the index of a given option group. public void MoveModGroup(IModGroup group, int groupIdxTo) { - var mod = group.Mod; + var mod = group.Mod; var idxFrom = group.GetIndex(); if (!mod.Groups.Move(idxFrom, groupIdxTo)) return; @@ -230,45 +227,45 @@ public class ModGroupEditor( => group switch { SingleModGroup s => SingleEditor.AddOption(s, option), - MultiModGroup m => MultiEditor.AddOption(m, option), - ImcModGroup i => ImcEditor.AddOption(i, option), - _ => null, + MultiModGroup m => MultiEditor.AddOption(m, option), + ImcModGroup i => ImcEditor.AddOption(i, option), + _ => null, }; public IModOption? AddOption(IModGroup group, string newName) => group switch { SingleModGroup s => SingleEditor.AddOption(s, newName), - MultiModGroup m => MultiEditor.AddOption(m, newName), - ImcModGroup i => ImcEditor.AddOption(i, newName), - _ => null, + MultiModGroup m => MultiEditor.AddOption(m, newName), + ImcModGroup i => ImcEditor.AddOption(i, newName), + _ => null, }; public IModGroup? AddModGroup(Mod mod, GroupType type, string newName, SaveType saveType = SaveType.ImmediateSync) => type switch { GroupType.Single => SingleEditor.AddModGroup(mod, newName, saveType), - GroupType.Multi => MultiEditor.AddModGroup(mod, newName, saveType), - GroupType.Imc => ImcEditor.AddModGroup(mod, newName, saveType), - _ => null, + GroupType.Multi => MultiEditor.AddModGroup(mod, newName, saveType), + GroupType.Imc => ImcEditor.AddModGroup(mod, newName, saveType), + _ => null, }; public (IModGroup?, int, bool) FindOrAddModGroup(Mod mod, GroupType type, string name, SaveType saveType = SaveType.ImmediateSync) => type switch { GroupType.Single => SingleEditor.FindOrAddModGroup(mod, name, saveType), - GroupType.Multi => MultiEditor.FindOrAddModGroup(mod, name, saveType), - GroupType.Imc => ImcEditor.FindOrAddModGroup(mod, name, saveType), - _ => (null, -1, false), + GroupType.Multi => MultiEditor.FindOrAddModGroup(mod, name, saveType), + GroupType.Imc => ImcEditor.FindOrAddModGroup(mod, name, saveType), + _ => (null, -1, false), }; public (IModOption?, int, bool) FindOrAddOption(IModGroup group, string name, SaveType saveType = SaveType.ImmediateSync) => group switch { SingleModGroup s => SingleEditor.FindOrAddOption(s, name, saveType), - MultiModGroup m => MultiEditor.FindOrAddOption(m, name, saveType), - ImcModGroup i => ImcEditor.FindOrAddOption(i, name, saveType), - _ => (null, -1, false), + MultiModGroup m => MultiEditor.FindOrAddOption(m, name, saveType), + ImcModGroup i => ImcEditor.FindOrAddOption(i, name, saveType), + _ => (null, -1, false), }; public void MoveOption(IModOption option, int toIdx) diff --git a/Penumbra/Mods/Manager/ModOptionEditor.cs b/Penumbra/Mods/Manager/OptionEditor/ModOptionEditor.cs similarity index 73% rename from Penumbra/Mods/Manager/ModOptionEditor.cs rename to Penumbra/Mods/Manager/OptionEditor/ModOptionEditor.cs index 7370a933..c067102e 100644 --- a/Penumbra/Mods/Manager/ModOptionEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/ModOptionEditor.cs @@ -5,7 +5,7 @@ using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; -namespace Penumbra.Mods.Manager; +namespace Penumbra.Mods.Manager.OptionEditor; public abstract class ModOptionEditor( CommunicatorService communicator, @@ -15,8 +15,8 @@ public abstract class ModOptionEditor( where TOption : class, IModOption { protected readonly CommunicatorService Communicator = communicator; - protected readonly SaveService SaveService = saveService; - protected readonly Configuration Config = config; + protected readonly SaveService SaveService = saveService; + protected readonly Configuration Config = config; /// Add a new, empty option group of the given type and name. public TGroup? AddModGroup(Mod mod, string newName, SaveType saveType = SaveType.ImmediateSync) @@ -25,7 +25,7 @@ public abstract class ModOptionEditor( return null; var maxPriority = mod.Groups.Count == 0 ? ModPriority.Default : mod.Groups.Max(o => o.Priority) + 1; - var group = CreateGroup(mod, newName, maxPriority); + var group = CreateGroup(mod, newName, maxPriority); mod.Groups.Add(group); SaveService.Save(saveType, new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupAdded, mod, group, null, null, -1); @@ -92,8 +92,8 @@ public abstract class ModOptionEditor( /// Delete the given option from the given group. public void DeleteOption(TOption option) { - var mod = option.Mod; - var group = option.Group; + var mod = option.Mod; + var group = option.Group; var optionIdx = option.GetIndex(); Communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, group, option, null, -1); RemoveOption((TGroup)group, optionIdx); @@ -104,7 +104,7 @@ public abstract class ModOptionEditor( /// Move an option inside the given option group. public void MoveOption(TOption option, int optionIdxTo) { - var idx = option.GetIndex(); + var idx = option.GetIndex(); var group = (TGroup)option.Group; if (!MoveOption(group, idx, optionIdxTo)) return; @@ -113,10 +113,10 @@ public abstract class ModOptionEditor( Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMoved, group.Mod, group, option, null, idx); } - protected abstract TGroup CreateGroup(Mod mod, string newName, ModPriority priority, SaveType saveType = SaveType.ImmediateSync); + protected abstract TGroup CreateGroup(Mod mod, string newName, ModPriority priority, SaveType saveType = SaveType.ImmediateSync); protected abstract TOption? CloneOption(TGroup group, IModOption option); - protected abstract void RemoveOption(TGroup group, int optionIndex); - protected abstract bool MoveOption(TGroup group, int optionIdxFrom, int optionIdxTo); + protected abstract void RemoveOption(TGroup group, int optionIndex); + protected abstract bool MoveOption(TGroup group, int optionIdxFrom, int optionIdxTo); } public static class ModOptionChangeTypeExtension @@ -132,22 +132,22 @@ public static class ModOptionChangeTypeExtension { (requiresSaving, requiresReloading, wasPrepared) = type switch { - ModOptionChangeType.GroupRenamed => (true, false, false), - ModOptionChangeType.GroupAdded => (true, false, false), - ModOptionChangeType.GroupDeleted => (true, true, false), - ModOptionChangeType.GroupMoved => (true, false, false), - ModOptionChangeType.GroupTypeChanged => (true, true, true), - ModOptionChangeType.PriorityChanged => (true, true, true), - ModOptionChangeType.OptionAdded => (true, true, true), - ModOptionChangeType.OptionDeleted => (true, true, false), - ModOptionChangeType.OptionMoved => (true, false, false), - ModOptionChangeType.OptionFilesChanged => (false, true, false), - ModOptionChangeType.OptionFilesAdded => (false, true, true), - ModOptionChangeType.OptionSwapsChanged => (false, true, false), - ModOptionChangeType.OptionMetaChanged => (false, true, false), - ModOptionChangeType.DisplayChange => (false, false, false), + ModOptionChangeType.GroupRenamed => (true, false, false), + ModOptionChangeType.GroupAdded => (true, false, false), + ModOptionChangeType.GroupDeleted => (true, true, false), + ModOptionChangeType.GroupMoved => (true, false, false), + ModOptionChangeType.GroupTypeChanged => (true, true, true), + ModOptionChangeType.PriorityChanged => (true, true, true), + ModOptionChangeType.OptionAdded => (true, true, true), + ModOptionChangeType.OptionDeleted => (true, true, false), + ModOptionChangeType.OptionMoved => (true, false, false), + ModOptionChangeType.OptionFilesChanged => (false, true, false), + ModOptionChangeType.OptionFilesAdded => (false, true, true), + ModOptionChangeType.OptionSwapsChanged => (false, true, false), + ModOptionChangeType.OptionMetaChanged => (false, true, false), + ModOptionChangeType.DisplayChange => (false, false, false), ModOptionChangeType.DefaultOptionChanged => (true, false, false), - _ => (false, false, false), + _ => (false, false, false), }; } } diff --git a/Penumbra/Mods/Manager/MultiModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/MultiModGroupEditor.cs similarity index 92% rename from Penumbra/Mods/Manager/MultiModGroupEditor.cs rename to Penumbra/Mods/Manager/OptionEditor/MultiModGroupEditor.cs index e6b2bac1..c48d2d40 100644 --- a/Penumbra/Mods/Manager/MultiModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/MultiModGroupEditor.cs @@ -6,14 +6,14 @@ using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; -namespace Penumbra.Mods.Manager; +namespace Penumbra.Mods.Manager.OptionEditor; public sealed class MultiModGroupEditor(CommunicatorService communicator, SaveService saveService, Configuration config) : ModOptionEditor(communicator, saveService, config), IService { public void ChangeToSingle(MultiModGroup group) { - var idx = group.GetIndex(); + var idx = group.GetIndex(); var singleGroup = group.ConvertToSingle(); group.Mod.Groups[idx] = singleGroup; SaveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); @@ -34,7 +34,7 @@ public sealed class MultiModGroupEditor(CommunicatorService communicator, SaveSe protected override MultiModGroup CreateGroup(Mod mod, string newName, ModPriority priority, SaveType saveType = SaveType.ImmediateSync) => new(mod) { - Name = newName, + Name = newName, Priority = priority, }; @@ -50,7 +50,7 @@ public sealed class MultiModGroupEditor(CommunicatorService communicator, SaveSe var newOption = new MultiSubMod(group) { - Name = option.Name, + Name = option.Name, Description = option.Description, }; diff --git a/Penumbra/Mods/Manager/SingleModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/SingleModGroupEditor.cs similarity index 90% rename from Penumbra/Mods/Manager/SingleModGroupEditor.cs rename to Penumbra/Mods/Manager/OptionEditor/SingleModGroupEditor.cs index 4999ff60..556416c6 100644 --- a/Penumbra/Mods/Manager/SingleModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/SingleModGroupEditor.cs @@ -6,14 +6,14 @@ using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; -namespace Penumbra.Mods.Manager; +namespace Penumbra.Mods.Manager.OptionEditor; public sealed class SingleModGroupEditor(CommunicatorService communicator, SaveService saveService, Configuration config) : ModOptionEditor(communicator, saveService, config), IService { public void ChangeToMulti(SingleModGroup group) { - var idx = group.GetIndex(); + var idx = group.GetIndex(); var multiGroup = group.ConvertToMulti(); group.Mod.Groups[idx] = multiGroup; SaveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); @@ -23,7 +23,7 @@ public sealed class SingleModGroupEditor(CommunicatorService communicator, SaveS protected override SingleModGroup CreateGroup(Mod mod, string newName, ModPriority priority, SaveType saveType = SaveType.ImmediateSync) => new(mod) { - Name = newName, + Name = newName, Priority = priority, }; @@ -31,7 +31,7 @@ public sealed class SingleModGroupEditor(CommunicatorService communicator, SaveS { var newOption = new SingleSubMod(group) { - Name = option.Name, + Name = option.Name, Description = option.Description, }; if (option is IModDataContainer data) diff --git a/Penumbra/Mods/Settings/ModSettings.cs b/Penumbra/Mods/Settings/ModSettings.cs index 39ee1860..7fe48365 100644 --- a/Penumbra/Mods/Settings/ModSettings.cs +++ b/Penumbra/Mods/Settings/ModSettings.cs @@ -3,7 +3,7 @@ using OtterGui.Filesystem; using Penumbra.Api.Enums; using Penumbra.Mods.Editor; using Penumbra.Mods.Groups; -using Penumbra.Mods.Manager; +using Penumbra.Mods.Manager.OptionEditor; using Penumbra.Mods.SubMods; namespace Penumbra.Mods.Settings; diff --git a/Penumbra/Penumbra.csproj b/Penumbra/Penumbra.csproj index 2d595ec1..2e53bd22 100644 --- a/Penumbra/Penumbra.csproj +++ b/Penumbra/Penumbra.csproj @@ -75,7 +75,7 @@ - + diff --git a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs index cd55beb0..4db0df47 100644 --- a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs +++ b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs @@ -16,6 +16,7 @@ using Penumbra.Mods; using Penumbra.Mods.Groups; using Penumbra.Mods.ItemSwap; using Penumbra.Mods.Manager; +using Penumbra.Mods.Manager.OptionEditor; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index fcd76a51..862852fa 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -16,6 +16,7 @@ using Penumbra.UI.AdvancedWindow; using Penumbra.Mods.Groups; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; +using Penumbra.Mods.Manager.OptionEditor; namespace Penumbra.UI.ModsTab; diff --git a/Penumbra/packages.lock.json b/Penumbra/packages.lock.json index 68e1b880..b431e595 100644 --- a/Penumbra/packages.lock.json +++ b/Penumbra/packages.lock.json @@ -34,9 +34,14 @@ }, "SixLabors.ImageSharp": { "type": "Direct", - "requested": "[3.1.3, )", - "resolved": "3.1.3", - "contentHash": "wybtaqZQ1ZRZ4ZeU+9h+PaSeV14nyiGKIy7qRbDfSHzHq4ybqyOcjoifeaYbiKLO1u+PVxLBuy7MF/DMmwwbfg==" + "requested": "[3.1.4, )", + "resolved": "3.1.4", + "contentHash": "lFIdxgGDA5iYkUMRFOze7BGLcdpoLFbR+a20kc1W7NepvzU7ejtxtWOg9RvgG7kb9tBoJ3ONYOK6kLil/dgF1w==" + }, + "JetBrains.Annotations": { + "type": "Transitive", + "resolved": "2023.3.0", + "contentHash": "PHfnvdBUdGaTVG9bR/GEfxgTwWM0Z97Y6X3710wiljELBISipSfF5okn/vz+C2gfO+ihoEyVPjaJwn8ZalVukA==" }, "Microsoft.Extensions.DependencyInjection": { "type": "Transitive", @@ -75,6 +80,7 @@ "ottergui": { "type": "Project", "dependencies": { + "JetBrains.Annotations": "[2023.3.0, )", "Microsoft.Extensions.DependencyInjection": "[8.0.0, )" } }, @@ -88,7 +94,7 @@ "type": "Project", "dependencies": { "OtterGui": "[1.0.0, )", - "Penumbra.Api": "[1.0.15, )", + "Penumbra.Api": "[5.0.0, )", "Penumbra.String": "[1.0.4, )" } }, From 9084b43e3ebef4e00471aa8839ed29aecd0fc25a Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 28 Apr 2024 12:01:54 +0200 Subject: [PATCH 141/433] Update GameData. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 9208c9c2..1b0b5469 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 9208c9c242244beeb3c1fb826582d72da09831af +Subproject commit 1b0b5469f792999e5b412d4f0c3ed77d9994d7b7 From 2e76148fba396e8c0e3fc0dc8ad632e0726e059d Mon Sep 17 00:00:00 2001 From: ackwell Date: Fri, 3 May 2024 21:14:40 +1000 Subject: [PATCH 142/433] Ensure materials end in .mtrl --- Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs index b8c0176a..b3460667 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs @@ -290,10 +290,13 @@ public partial class ModEditWindow /// While materials can be relative (`/mt_...`) or absolute (`bg/...`), /// they invariably must contain at least one directory seperator. /// Missing this can lead to a crash. + /// + /// They must also be at least one character (though this is enforced + /// by containing a `/`), and end with `.mtrl`. /// public bool ValidateMaterial(string material) { - return material.Contains('/'); + return material.Contains('/') && material.EndsWith(".mtrl"); } /// Remove the material given by the index. From c96adcf557597a712582b09eee62978ad479f2b7 Mon Sep 17 00:00:00 2001 From: ackwell Date: Fri, 3 May 2024 21:15:00 +1000 Subject: [PATCH 143/433] Prevent saving invalid files --- Penumbra/UI/AdvancedWindow/FileEditor.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Penumbra/UI/AdvancedWindow/FileEditor.cs b/Penumbra/UI/AdvancedWindow/FileEditor.cs index c891d33a..66f38bab 100644 --- a/Penumbra/UI/AdvancedWindow/FileEditor.cs +++ b/Penumbra/UI/AdvancedWindow/FileEditor.cs @@ -204,8 +204,9 @@ public class FileEditor( private void SaveButton() { + var canSave = _changed && _currentFile != null && _currentFile.Valid; if (ImGuiUtil.DrawDisabledButton("Save to File", Vector2.Zero, - $"Save the selected {fileType} file with all changes applied. This is not revertible.", !_changed)) + $"Save the selected {fileType} file with all changes applied. This is not revertible.", !canSave)) { compactor.WriteAllBytes(_currentPath!.File.FullName, _currentFile!.Write()); if (owner.Mod != null) From 078688454a5e67a05497f92b0a834e9c5daae594 Mon Sep 17 00:00:00 2001 From: ackwell Date: Fri, 3 May 2024 21:17:54 +1000 Subject: [PATCH 144/433] Show an invalid material count --- .../UI/AdvancedWindow/ModEditWindow.Models.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index 03b5169a..e075b592 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -293,7 +293,21 @@ public partial class ModEditWindow private bool DrawModelMaterialDetails(MdlTab tab, bool disabled) { - if (!ImGui.CollapsingHeader("Materials")) + var invalidMaterialCount = tab.Mdl.Materials.Count(material => !tab.ValidateMaterial(material)); + + var oldPos = ImGui.GetCursorPosY(); + var header = ImGui.CollapsingHeader("Materials"); + var newPos = ImGui.GetCursorPos(); + if (invalidMaterialCount > 0) + { + var text = $"{invalidMaterialCount} invalid material{(invalidMaterialCount > 1 ? "s" : "")}"; + var size = ImGui.CalcTextSize(text).X; + ImGui.SetCursorPos(new Vector2(ImGui.GetContentRegionAvail().X - size, oldPos + ImGui.GetStyle().FramePadding.Y)); + ImGuiUtil.TextColored(0xFF0000FF, text); + ImGui.SetCursorPos(newPos); + } + + if (!header) return false; using var table = ImRaii.Table(string.Empty, disabled ? 2 : 4, ImGuiTableFlags.SizingFixedFit); @@ -382,7 +396,8 @@ public partial class ModEditWindow { ImGuiComponents.HelpMarker( "Materials must be either relative (e.g. \"/filename.mtrl\")\n" - + "or absolute (e.g. \"bg/full/path/to/filename.mtrl\").", + + "or absolute (e.g. \"bg/full/path/to/filename.mtrl\"),\n" + + "and must end in \".mtrl\".", FontAwesomeIcon.TimesCircle); } From 9063d131bae492f5186b2a96d01e8135f7e2364f Mon Sep 17 00:00:00 2001 From: ackwell Date: Fri, 3 May 2024 21:41:35 +1000 Subject: [PATCH 145/433] Use validation logic for new material field --- .../UI/AdvancedWindow/ModEditWindow.Models.cs | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index e075b592..0be95c99 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -337,7 +337,7 @@ public partial class ModEditWindow ImGui.TableNextColumn(); ImGui.SetNextItemWidth(-1); ImGui.InputTextWithHint("##newMaterial", "Add new material...", ref _modelNewMaterial, Utf8GamePath.MaxGamePathLength, inputFlags); - var validName = _modelNewMaterial.Length > 0 && _modelNewMaterial[0] == '/'; + var validName = tab.ValidateMaterial(_modelNewMaterial); ImGui.TableNextColumn(); if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), UiHelpers.IconButtonSize, string.Empty, !validName, true)) { @@ -346,6 +346,8 @@ public partial class ModEditWindow _modelNewMaterial = string.Empty; } ImGui.TableNextColumn(); + if (!validName && _modelNewMaterial.Length > 0) + DrawInvalidMaterialMarker(); return ret; } @@ -392,18 +394,22 @@ public partial class ModEditWindow ImGui.TableNextColumn(); // Add markers to invalid materials. if (!tab.ValidateMaterial(temp)) - using (var colorHandle = ImRaii.PushColor(ImGuiCol.TextDisabled, 0xFF0000FF, true)) - { - ImGuiComponents.HelpMarker( - "Materials must be either relative (e.g. \"/filename.mtrl\")\n" - + "or absolute (e.g. \"bg/full/path/to/filename.mtrl\"),\n" - + "and must end in \".mtrl\".", - FontAwesomeIcon.TimesCircle); - } + DrawInvalidMaterialMarker(); return ret; } + private void DrawInvalidMaterialMarker() + { + using var colorHandle = ImRaii.PushColor(ImGuiCol.TextDisabled, 0xFF0000FF, true); + + ImGuiComponents.HelpMarker( + "Materials must be either relative (e.g. \"/filename.mtrl\")\n" + + "or absolute (e.g. \"bg/full/path/to/filename.mtrl\"),\n" + + "and must end in \".mtrl\".", + FontAwesomeIcon.TimesCircle); + } + private bool DrawModelLodDetails(MdlTab tab, int lodIndex, bool disabled) { using var lodNode = ImRaii.TreeNode($"Level of Detail #{lodIndex + 1}", ImGuiTreeNodeFlags.DefaultOpen); From 46a111d15287112a09ee3acf0b7cba0a30454ef2 Mon Sep 17 00:00:00 2001 From: ackwell Date: Fri, 3 May 2024 22:42:28 +1000 Subject: [PATCH 146/433] Fix marker --- Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index 0be95c99..a7d39c6e 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -401,13 +401,13 @@ public partial class ModEditWindow private void DrawInvalidMaterialMarker() { - using var colorHandle = ImRaii.PushColor(ImGuiCol.TextDisabled, 0xFF0000FF, true); + using (var font = ImRaii.PushFont(UiBuilder.IconFont)) + ImGuiUtil.TextColored(0xFF0000FF, FontAwesomeIcon.TimesCircle.ToIconString()); - ImGuiComponents.HelpMarker( + ImGuiUtil.HoverTooltip( "Materials must be either relative (e.g. \"/filename.mtrl\")\n" + "or absolute (e.g. \"bg/full/path/to/filename.mtrl\"),\n" - + "and must end in \".mtrl\".", - FontAwesomeIcon.TimesCircle); + + "and must end in \".mtrl\"."); } private bool DrawModelLodDetails(MdlTab tab, int lodIndex, bool disabled) From 36fc251d5b4265c9fb6d0af8c9cfc88233c71697 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 3 May 2024 18:52:52 +0200 Subject: [PATCH 147/433] Fix a bunch of issues. --- Penumbra.GameData | 2 +- Penumbra/Mods/Groups/IModGroup.cs | 19 +- Penumbra/Mods/Groups/ImcModGroup.cs | 4 +- Penumbra/Mods/Groups/ModSaveGroup.cs | 7 +- Penumbra/Mods/Groups/MultiModGroup.cs | 4 +- Penumbra/Mods/Groups/SingleModGroup.cs | 13 +- .../OptionEditor/MultiModGroupEditor.cs | 4 +- .../OptionEditor/SingleModGroupEditor.cs | 4 +- Penumbra/Mods/SubMods/SubMod.cs | 1 - Penumbra/Services/BackupService.cs | 1 + Penumbra/Services/StaticServiceManager.cs | 1 - Penumbra/UI/ModsTab/ModGroupDrawer.cs | 233 +++++++++++++++ Penumbra/UI/ModsTab/ModGroupEditDrawer.cs | 214 ++++++++++++++ Penumbra/UI/ModsTab/ModPanelSettingsTab.cs | 272 ++---------------- 14 files changed, 513 insertions(+), 266 deletions(-) create mode 100644 Penumbra/UI/ModsTab/ModGroupDrawer.cs create mode 100644 Penumbra/UI/ModsTab/ModGroupEditDrawer.cs diff --git a/Penumbra.GameData b/Penumbra.GameData index 1b0b5469..595ac572 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 1b0b5469f792999e5b412d4f0c3ed77d9994d7b7 +Subproject commit 595ac5722c9c400bea36110503ed2ae7b02d1489 diff --git a/Penumbra/Mods/Groups/IModGroup.cs b/Penumbra/Mods/Groups/IModGroup.cs index a268ba0f..d7138434 100644 --- a/Penumbra/Mods/Groups/IModGroup.cs +++ b/Penumbra/Mods/Groups/IModGroup.cs @@ -12,16 +12,23 @@ public interface ITexToolsGroup public IReadOnlyList OptionData { get; } } +public enum GroupDrawBehaviour +{ + SingleSelection, + MultiSelection, +} + public interface IModGroup { public const int MaxMultiOptions = 63; - public Mod Mod { get; } - public string Name { get; set; } - public string Description { get; set; } - public GroupType Type { get; } - public ModPriority Priority { get; set; } - public Setting DefaultSettings { get; set; } + public Mod Mod { get; } + public string Name { get; set; } + public string Description { get; set; } + public GroupType Type { get; } + public GroupDrawBehaviour Behaviour { get; } + public ModPriority Priority { get; set; } + public Setting DefaultSettings { get; set; } public FullPath? FindBestMatch(Utf8GamePath gamePath); public IModOption? AddOption(string name, string description = ""); diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs index e233f82e..e58d855a 100644 --- a/Penumbra/Mods/Groups/ImcModGroup.cs +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -21,6 +21,9 @@ public class ImcModGroup(Mod mod) : IModGroup public GroupType Type => GroupType.Imc; + public GroupDrawBehaviour Behaviour + => GroupDrawBehaviour.MultiSelection; + public ModPriority Priority { get; set; } = ModPriority.Default; public Setting DefaultSettings { get; set; } = Setting.Zero; @@ -150,7 +153,6 @@ public class ImcModGroup(Mod mod) : IModGroup } jWriter.WriteEndArray(); - jWriter.WriteEndObject(); } public (int Redirections, int Swaps, int Manips) GetCounts() diff --git a/Penumbra/Mods/Groups/ModSaveGroup.cs b/Penumbra/Mods/Groups/ModSaveGroup.cs index efdcde09..ed3c8857 100644 --- a/Penumbra/Mods/Groups/ModSaveGroup.cs +++ b/Penumbra/Mods/Groups/ModSaveGroup.cs @@ -56,10 +56,10 @@ public readonly struct ModSaveGroup : ISavable { _basePath = (container.Mod as Mod)?.ModPath ?? throw new Exception("Invalid save group from default data container without base path."); // Should not happen. - _defaultMod = null; + _defaultMod = container as DefaultSubMod; _onlyAscii = onlyAscii; - _group = container.Group!; - _groupIdx = _group.GetIndex(); + _group = container.Group; + _groupIdx = _group?.GetIndex() ?? -1; } public string ToFilename(FilenameService fileNames) @@ -80,7 +80,6 @@ public readonly struct ModSaveGroup : ISavable public static void WriteJsonBase(JsonTextWriter jWriter, IModGroup group) { - jWriter.WriteStartObject(); jWriter.WritePropertyName(nameof(group.Name)); jWriter.WriteValue(group.Name); jWriter.WritePropertyName(nameof(group.Description)); diff --git a/Penumbra/Mods/Groups/MultiModGroup.cs b/Penumbra/Mods/Groups/MultiModGroup.cs index a0034be0..7495c4b4 100644 --- a/Penumbra/Mods/Groups/MultiModGroup.cs +++ b/Penumbra/Mods/Groups/MultiModGroup.cs @@ -17,6 +17,9 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup public GroupType Type => GroupType.Multi; + public GroupDrawBehaviour Behaviour + => GroupDrawBehaviour.MultiSelection; + public Mod Mod { get; } = mod; public string Name { get; set; } = "Group"; public string Description { get; set; } = "A non-exclusive group of settings."; @@ -127,7 +130,6 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup } jWriter.WriteEndArray(); - jWriter.WriteEndObject(); } public (int Redirections, int Swaps, int Manips) GetCounts() diff --git a/Penumbra/Mods/Groups/SingleModGroup.cs b/Penumbra/Mods/Groups/SingleModGroup.cs index 0776c2af..459cec4a 100644 --- a/Penumbra/Mods/Groups/SingleModGroup.cs +++ b/Penumbra/Mods/Groups/SingleModGroup.cs @@ -15,7 +15,10 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup public GroupType Type => GroupType.Single; - public Mod Mod { get; } = mod; + public GroupDrawBehaviour Behaviour + => GroupDrawBehaviour.SingleSelection; + + public Mod Mod { get; } = mod; public string Name { get; set; } = "Option"; public string Description { get; set; } = "A mutually exclusive group of settings."; public ModPriority Priority { get; set; } @@ -89,7 +92,12 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup => ModGroup.GetIndex(this); public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) - => OptionData[setting.AsIndex].AddDataTo(redirections, manipulations); + { + if (!IsOption) + return; + + OptionData[setting.AsIndex].AddDataTo(redirections, manipulations); + } public Setting FixSetting(Setting setting) => OptionData.Count == 0 ? Setting.Zero : new Setting(Math.Min(setting.Value, (ulong)(OptionData.Count - 1))); @@ -111,7 +119,6 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup } jWriter.WriteEndArray(); - jWriter.WriteEndObject(); } /// Create a group without a mod only for saving it in the creator. diff --git a/Penumbra/Mods/Manager/OptionEditor/MultiModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/MultiModGroupEditor.cs index c48d2d40..74362325 100644 --- a/Penumbra/Mods/Manager/OptionEditor/MultiModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/MultiModGroupEditor.cs @@ -16,8 +16,8 @@ public sealed class MultiModGroupEditor(CommunicatorService communicator, SaveSe var idx = group.GetIndex(); var singleGroup = group.ConvertToSingle(); group.Mod.Groups[idx] = singleGroup; - SaveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupTypeChanged, group.Mod, group, null, null, -1); + SaveService.QueueSave(new ModSaveGroup(singleGroup, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupTypeChanged, singleGroup.Mod, singleGroup, null, null, -1); } /// Change the internal priority of the given option. diff --git a/Penumbra/Mods/Manager/OptionEditor/SingleModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/SingleModGroupEditor.cs index 556416c6..15a899a0 100644 --- a/Penumbra/Mods/Manager/OptionEditor/SingleModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/SingleModGroupEditor.cs @@ -16,8 +16,8 @@ public sealed class SingleModGroupEditor(CommunicatorService communicator, SaveS var idx = group.GetIndex(); var multiGroup = group.ConvertToMulti(); group.Mod.Groups[idx] = multiGroup; - SaveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupTypeChanged, group.Mod, multiGroup, null, null, -1); + SaveService.QueueSave(new ModSaveGroup(multiGroup, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupTypeChanged, multiGroup.Mod, multiGroup, null, null, -1); } protected override SingleModGroup CreateGroup(Mod mod, string newName, ModPriority priority, SaveType saveType = SaveType.ImmediateSync) diff --git a/Penumbra/Mods/SubMods/SubMod.cs b/Penumbra/Mods/SubMods/SubMod.cs index a50e397f..b984b570 100644 --- a/Penumbra/Mods/SubMods/SubMod.cs +++ b/Penumbra/Mods/SubMods/SubMod.cs @@ -106,7 +106,6 @@ public static class SubMod j.WriteEndObject(); j.WritePropertyName(nameof(data.Manipulations)); serializer.Serialize(j, data.Manipulations); - j.WriteEndObject(); } /// Write the data for a selectable mod option on a JsonWriter. diff --git a/Penumbra/Services/BackupService.cs b/Penumbra/Services/BackupService.cs index a542dab5..bd5b3bcc 100644 --- a/Penumbra/Services/BackupService.cs +++ b/Penumbra/Services/BackupService.cs @@ -29,6 +29,7 @@ public class BackupService : IAsyncService list.Add(new FileInfo(fileNames.ConfigFile)); list.Add(new FileInfo(fileNames.FilesystemFile)); list.Add(new FileInfo(fileNames.ActiveCollectionsFile)); + list.Add(new FileInfo(fileNames.PredefinedTagFile)); return list; } diff --git a/Penumbra/Services/StaticServiceManager.cs b/Penumbra/Services/StaticServiceManager.cs index 5fa1a848..19ae31a2 100644 --- a/Penumbra/Services/StaticServiceManager.cs +++ b/Penumbra/Services/StaticServiceManager.cs @@ -162,7 +162,6 @@ public static class StaticServiceManager .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() diff --git a/Penumbra/UI/ModsTab/ModGroupDrawer.cs b/Penumbra/UI/ModsTab/ModGroupDrawer.cs new file mode 100644 index 00000000..e9b0b396 --- /dev/null +++ b/Penumbra/UI/ModsTab/ModGroupDrawer.cs @@ -0,0 +1,233 @@ +using Dalamud.Interface.Components; +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Services; +using OtterGui.Widgets; +using Penumbra.Collections; +using Penumbra.Collections.Manager; +using Penumbra.Mods; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; + +namespace Penumbra.UI.ModsTab; + +public sealed class ModGroupDrawer(Configuration config, CollectionManager collectionManager) : IUiService +{ + private readonly List<(IModGroup, int)> _blockGroupCache = []; + + public void Draw(Mod mod, ModSettings settings) + { + if (mod.Groups.Count <= 0) + return; + + _blockGroupCache.Clear(); + var useDummy = true; + foreach (var (group, idx) in mod.Groups.WithIndex()) + { + if (!group.IsOption) + continue; + + switch (group.Behaviour) + { + case GroupDrawBehaviour.SingleSelection when group.Options.Count <= config.SingleGroupRadioMax: + case GroupDrawBehaviour.MultiSelection: + _blockGroupCache.Add((group, idx)); + break; + + case GroupDrawBehaviour.SingleSelection: + ImGuiUtil.Dummy(UiHelpers.DefaultSpace, useDummy); + useDummy = false; + DrawSingleGroupCombo(group, idx, settings == ModSettings.Empty ? group.DefaultSettings : settings.Settings[idx]); + break; + } + } + + useDummy = true; + foreach (var (group, idx) in _blockGroupCache) + { + ImGuiUtil.Dummy(UiHelpers.DefaultSpace, useDummy); + useDummy = false; + var option = settings == ModSettings.Empty ? group.DefaultSettings : settings.Settings[idx]; + if (group.Behaviour is GroupDrawBehaviour.MultiSelection) + DrawMultiGroup(group, idx, option); + else + DrawSingleGroupRadio(group, idx, option); + } + } + + /// + /// Draw a single group selector as a combo box. + /// If a description is provided, add a help marker besides it. + /// + private void DrawSingleGroupCombo(IModGroup group, int groupIdx, Setting setting) + { + using var id = ImRaii.PushId(groupIdx); + var selectedOption = setting.AsIndex; + ImGui.SetNextItemWidth(UiHelpers.InputTextWidth.X * 3 / 4); + var options = group.Options; + using (var combo = ImRaii.Combo(string.Empty, options[selectedOption].Name)) + { + if (combo) + for (var idx2 = 0; idx2 < options.Count; ++idx2) + { + id.Push(idx2); + var option = options[idx2]; + if (ImGui.Selectable(option.Name, idx2 == selectedOption)) + SetModSetting(group, groupIdx, Setting.Single(idx2)); + + if (option.Description.Length > 0) + ImGuiUtil.SelectableHelpMarker(option.Description); + + id.Pop(); + } + } + + ImGui.SameLine(); + if (group.Description.Length > 0) + ImGuiUtil.LabeledHelpMarker(group.Name, group.Description); + else + ImGui.TextUnformatted(group.Name); + } + + /// + /// Draw a single group selector as a set of radio buttons. + /// If a description is provided, add a help marker besides it. + /// + private void DrawSingleGroupRadio(IModGroup group, int groupIdx, Setting setting) + { + using var id = ImRaii.PushId(groupIdx); + var selectedOption = setting.AsIndex; + var minWidth = Widget.BeginFramedGroup(group.Name, group.Description); + var options = group.Options; + DrawCollapseHandling(options, minWidth, DrawOptions); + + Widget.EndFramedGroup(); + return; + + void DrawOptions() + { + for (var idx = 0; idx < group.Options.Count; ++idx) + { + using var i = ImRaii.PushId(idx); + var option = options[idx]; + if (ImGui.RadioButton(option.Name, selectedOption == idx)) + SetModSetting(group, groupIdx, Setting.Single(idx)); + + if (option.Description.Length <= 0) + continue; + + ImGui.SameLine(); + ImGuiComponents.HelpMarker(option.Description); + } + } + } + + /// + /// Draw a multi group selector as a bordered set of checkboxes. + /// If a description is provided, add a help marker in the title. + /// + private void DrawMultiGroup(IModGroup group, int groupIdx, Setting setting) + { + using var id = ImRaii.PushId(groupIdx); + var minWidth = Widget.BeginFramedGroup(group.Name, group.Description); + var options = group.Options; + DrawCollapseHandling(options, minWidth, DrawOptions); + + Widget.EndFramedGroup(); + var label = $"##multi{groupIdx}"; + if (ImGui.IsItemClicked(ImGuiMouseButton.Right)) + ImGui.OpenPopup($"##multi{groupIdx}"); + + DrawMultiPopup(group, groupIdx, label); + return; + + void DrawOptions() + { + for (var idx = 0; idx < options.Count; ++idx) + { + using var i = ImRaii.PushId(idx); + var option = options[idx]; + var enabled = setting.HasFlag(idx); + + if (ImGui.Checkbox(option.Name, ref enabled)) + SetModSetting(group, groupIdx, setting.SetBit(idx, enabled)); + + if (option.Description.Length > 0) + { + ImGui.SameLine(); + ImGuiComponents.HelpMarker(option.Description); + } + } + } + } + + private void DrawMultiPopup(IModGroup group, int groupIdx, string label) + { + using var style = ImRaii.PushStyle(ImGuiStyleVar.PopupBorderSize, 1); + using var popup = ImRaii.Popup(label); + if (!popup) + return; + + ImGui.TextUnformatted(group.Name); + ImGui.Separator(); + if (ImGui.Selectable("Enable All")) + SetModSetting(group, groupIdx, Setting.AllBits(group.Options.Count)); + + if (ImGui.Selectable("Disable All")) + SetModSetting(group, groupIdx, Setting.Zero); + } + + private void DrawCollapseHandling(IReadOnlyList options, float minWidth, Action draw) + { + if (options.Count <= config.OptionGroupCollapsibleMin) + { + draw(); + } + else + { + var collapseId = ImGui.GetID("Collapse"); + var shown = ImGui.GetStateStorage().GetBool(collapseId, true); + var buttonTextShow = $"Show {options.Count} Options"; + var buttonTextHide = $"Hide {options.Count} Options"; + var buttonWidth = Math.Max(ImGui.CalcTextSize(buttonTextShow).X, ImGui.CalcTextSize(buttonTextHide).X) + + 2 * ImGui.GetStyle().FramePadding.X; + minWidth = Math.Max(buttonWidth, minWidth); + if (shown) + { + var pos = ImGui.GetCursorPos(); + ImGui.Dummy(UiHelpers.IconButtonSize); + using (var _ = ImRaii.Group()) + { + draw(); + } + + + var width = Math.Max(ImGui.GetItemRectSize().X, minWidth); + var endPos = ImGui.GetCursorPos(); + ImGui.SetCursorPos(pos); + if (ImGui.Button(buttonTextHide, new Vector2(width, 0))) + ImGui.GetStateStorage().SetBool(collapseId, !shown); + + ImGui.SetCursorPos(endPos); + } + else + { + var optionWidth = options.Max(o => ImGui.CalcTextSize(o.Name).X) + + ImGui.GetStyle().ItemInnerSpacing.X + + ImGui.GetFrameHeight() + + ImGui.GetStyle().FramePadding.X; + var width = Math.Max(optionWidth, minWidth); + if (ImGui.Button(buttonTextShow, new Vector2(width, 0))) + ImGui.GetStateStorage().SetBool(collapseId, !shown); + } + } + } + + private ModCollection Current + => collectionManager.Active.Current; + + private void SetModSetting(IModGroup group, int groupIdx, Setting setting) + => collectionManager.Editor.SetModSetting(Current, group.Mod, groupIdx, setting); +} diff --git a/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs new file mode 100644 index 00000000..6b62d5b8 --- /dev/null +++ b/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs @@ -0,0 +1,214 @@ +using Dalamud.Interface; +using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.Utility; +using ImGuiNET; +using OtterGui; +using OtterGui.Classes; +using OtterGui.Raii; +using OtterGui.Services; +using Penumbra.Mods; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Manager; +using Penumbra.Mods.Manager.OptionEditor; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; +using Penumbra.Services; +using Penumbra.UI.Classes; + +namespace Penumbra.UI.ModsTab; + +public sealed class ModGroupEditDrawer(ModManager modManager, Configuration config, FilenameService filenames) : IUiService +{ + private Vector2 _buttonSize; + private float _priorityWidth; + private float _groupNameWidth; + private float _spacing; + + private string? _currentGroupName; + private ModPriority? _currentGroupPriority; + private IModGroup? _currentGroupEdited; + private bool _isGroupNameValid; + private IModGroup? _deleteGroup; + private IModGroup? _moveGroup; + private int _moveTo; + + private string? _currentOptionName; + private ModPriority? _currentOptionPriority; + private IModOption? _currentOptionEdited; + private IModOption? _deleteOption; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void SameLine() + => ImGui.SameLine(0, _spacing); + + public void Draw(Mod mod) + { + _buttonSize = new Vector2(ImGui.GetFrameHeight()); + _priorityWidth = 50 * ImGuiHelpers.GlobalScale; + _groupNameWidth = 350f * ImGuiHelpers.GlobalScale; + _spacing = ImGui.GetStyle().ItemInnerSpacing.X; + + + FinishGroupCleanup(); + } + + private void FinishGroupCleanup() + { + if (_deleteGroup != null) + { + modManager.OptionEditor.DeleteModGroup(_deleteGroup); + _deleteGroup = null; + } + + if (_deleteOption != null) + { + modManager.OptionEditor.DeleteOption(_deleteOption); + _deleteOption = null; + } + + if (_moveGroup != null) + { + modManager.OptionEditor.MoveModGroup(_moveGroup, _moveTo); + _moveGroup = null; + } + } + + private void DrawGroup(IModGroup group, int idx) + { + using var id = ImRaii.PushId(idx); + using var frame = ImRaii.FramedGroup($"Group #{idx + 1}"); + DrawGroupNameRow(group); + switch (group) + { + case SingleModGroup s: + DrawSingleGroup(s, idx); + break; + case MultiModGroup m: + DrawMultiGroup(m, idx); + break; + case ImcModGroup i: + DrawImcGroup(i, idx); + break; + } + } + + private void DrawGroupNameRow(IModGroup group) + { + DrawGroupName(group); + SameLine(); + DrawGroupDelete(group); + SameLine(); + DrawGroupPriority(group); + } + + private void DrawGroupName(IModGroup group) + { + var text = _currentGroupEdited == group ? _currentGroupName ?? group.Name : group.Name; + ImGui.SetNextItemWidth(_groupNameWidth); + using var border = ImRaii.PushFrameBorder(UiHelpers.ScaleX2, Colors.RegexWarningBorder, !_isGroupNameValid); + if (ImGui.InputText("##GroupName", ref text, 256)) + { + _currentGroupEdited = group; + _currentGroupName = text; + _isGroupNameValid = ModGroupEditor.VerifyFileName(group.Mod, group, text, false); + } + + if (ImGui.IsItemDeactivated()) + { + if (_currentGroupName != null && _isGroupNameValid) + modManager.OptionEditor.RenameModGroup(group, _currentGroupName); + _currentGroupName = null; + _currentGroupEdited = null; + _isGroupNameValid = true; + } + + var tt = _isGroupNameValid + ? "Group Name" + : "Current name can not be used for this group."; + ImGuiUtil.HoverTooltip(tt); + } + + private void DrawGroupDelete(IModGroup group) + { + var enabled = config.DeleteModModifier.IsActive(); + var tt = enabled + ? "Delete this option group." + : $"Delete this option group.\nHold {config.DeleteModModifier} while clicking to delete."; + + if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Trash.ToIconString(), _buttonSize, tt, !enabled, true)) + _deleteGroup = group; + } + + private void DrawGroupPriority(IModGroup group) + { + var priority = _currentGroupEdited == group + ? (_currentGroupPriority ?? group.Priority).Value + : group.Priority.Value; + ImGui.SetNextItemWidth(_priorityWidth); + if (ImGui.InputInt("##GroupPriority", ref priority, 0, 0)) + { + _currentGroupEdited = group; + _currentGroupPriority = new ModPriority(priority); + } + + if (ImGui.IsItemDeactivated()) + { + if (_currentGroupPriority.HasValue) + modManager.OptionEditor.ChangeGroupPriority(group, _currentGroupPriority.Value); + _currentGroupEdited = null; + _currentGroupPriority = null; + } + + ImGuiUtil.HoverTooltip("Group Priority"); + } + + private void DrawGroupMoveButtons(IModGroup group, int idx) + { + var isFirst = idx == 0; + var tt = isFirst ? "Can not move this group further upwards." : $"Move this group up to group {idx}."; + if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.ArrowUp.ToIconString(), UiHelpers.IconButtonSize, tt, isFirst, true)) + { + _moveGroup = group; + _moveTo = idx - 1; + } + + SameLine(); + var isLast = idx == group.Mod.Groups.Count - 1; + tt = isLast + ? "Can not move this group further downwards." + : $"Move this group down to group {idx + 2}."; + if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.ArrowDown.ToIconString(), UiHelpers.IconButtonSize, tt, isLast, true)) + { + _moveGroup = group; + _moveTo = idx + 1; + } + } + + private void DrawGroupOpenFile(IModGroup group, int idx) + { + var fileName = filenames.OptionGroupFile(group.Mod, idx, config.ReplaceNonAsciiOnImport); + var fileExists = File.Exists(fileName); + var tt = fileExists + ? $"Open the {group.Name} json file in the text editor of your choice." + : $"The {group.Name} json file does not exist."; + if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.FileExport.ToIconString(), UiHelpers.IconButtonSize, tt, !fileExists, true)) + try + { + Process.Start(new ProcessStartInfo(fileName) { UseShellExecute = true }); + } + catch (Exception e) + { + Penumbra.Messager.NotificationMessage(e, "Could not open editor.", NotificationType.Error); + } + } + + + private void DrawSingleGroup(SingleModGroup group, int idx) + { } + + private void DrawMultiGroup(MultiModGroup group, int idx) + { } + + private void DrawImcGroup(ImcModGroup group, int idx) + { } +} diff --git a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs index 1326a763..fc5311d9 100644 --- a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs @@ -1,51 +1,37 @@ using ImGuiNET; using OtterGui.Raii; using OtterGui; +using OtterGui.Services; +using OtterGui.Text; using OtterGui.Widgets; -using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.UI.Classes; -using Dalamud.Interface.Components; using Penumbra.Collections.Manager; using Penumbra.Mods.Manager; using Penumbra.Services; -using Penumbra.Mods.SubMods; -using Penumbra.Mods.Groups; using Penumbra.Mods.Settings; namespace Penumbra.UI.ModsTab; -public class ModPanelSettingsTab : ITab +public class ModPanelSettingsTab( + CollectionManager collectionManager, + ModManager modManager, + ModFileSystemSelector selector, + TutorialService tutorial, + CommunicatorService communicator, + ModGroupDrawer modGroupDrawer) + : ITab, IUiService { - private readonly Configuration _config; - private readonly CommunicatorService _communicator; - private readonly CollectionManager _collectionManager; - private readonly ModFileSystemSelector _selector; - private readonly TutorialService _tutorial; - private readonly ModManager _modManager; - private bool _inherited; private ModSettings _settings = null!; private ModCollection _collection = null!; - private bool _empty; private int? _currentPriority; - public ModPanelSettingsTab(CollectionManager collectionManager, ModManager modManager, ModFileSystemSelector selector, - TutorialService tutorial, CommunicatorService communicator, Configuration config) - { - _collectionManager = collectionManager; - _communicator = communicator; - _modManager = modManager; - _selector = selector; - _tutorial = tutorial; - _config = config; - } - public ReadOnlySpan Label => "Settings"u8; public void DrawHeader() - => _tutorial.OpenTutorial(BasicTutorialSteps.ModOptions); + => tutorial.OpenTutorial(BasicTutorialSteps.ModOptions); public void Reset() => _currentPriority = null; @@ -56,53 +42,24 @@ public class ModPanelSettingsTab : ITab if (!child) return; - _settings = _selector.SelectedSettings; - _collection = _selector.SelectedSettingCollection; - _inherited = _collection != _collectionManager.Active.Current; - _empty = _settings == ModSettings.Empty; - + _settings = selector.SelectedSettings; + _collection = selector.SelectedSettingCollection; + _inherited = _collection != collectionManager.Active.Current; DrawInheritedWarning(); UiHelpers.DefaultLineSpace(); - _communicator.PreSettingsPanelDraw.Invoke(_selector.Selected!.Identifier); + communicator.PreSettingsPanelDraw.Invoke(selector.Selected!.Identifier); DrawEnabledInput(); - _tutorial.OpenTutorial(BasicTutorialSteps.EnablingMods); + tutorial.OpenTutorial(BasicTutorialSteps.EnablingMods); ImGui.SameLine(); DrawPriorityInput(); - _tutorial.OpenTutorial(BasicTutorialSteps.Priority); + tutorial.OpenTutorial(BasicTutorialSteps.Priority); DrawRemoveSettings(); - _communicator.PostEnabledDraw.Invoke(_selector.Selected!.Identifier); - - if (_selector.Selected!.Groups.Count > 0) - { - var useDummy = true; - foreach (var (group, idx) in _selector.Selected!.Groups.WithIndex() - .Where(g => g.Value.Type == GroupType.Single && g.Value.Options.Count > _config.SingleGroupRadioMax)) - { - ImGuiUtil.Dummy(UiHelpers.DefaultSpace, useDummy); - useDummy = false; - DrawSingleGroupCombo(group, idx); - } - - useDummy = true; - foreach (var (group, idx) in _selector.Selected!.Groups.WithIndex().Where(g => g.Value.IsOption)) - { - ImGuiUtil.Dummy(UiHelpers.DefaultSpace, useDummy); - useDummy = false; - switch (group.Type) - { - case GroupType.Multi: - DrawMultiGroup(group, idx); - break; - case GroupType.Single when group.Options.Count <= _config.SingleGroupRadioMax: - DrawSingleGroupRadio(group, idx); - break; - } - } - } + communicator.PostEnabledDraw.Invoke(selector.Selected!.Identifier); + modGroupDrawer.Draw(selector.Selected!, _settings); UiHelpers.DefaultLineSpace(); - _communicator.PostSettingsPanelDraw.Invoke(_selector.Selected!.Identifier); + communicator.PostSettingsPanelDraw.Invoke(selector.Selected!.Identifier); } /// Draw a big red bar if the current setting is inherited. @@ -113,8 +70,8 @@ public class ModPanelSettingsTab : ITab using var color = ImRaii.PushColor(ImGuiCol.Button, Colors.PressEnterWarningBg); var width = new Vector2(ImGui.GetContentRegionAvail().X, 0); - if (ImGui.Button($"These settings are inherited from {_collection.Name}.", width)) - _collectionManager.Editor.SetModInheritance(_collectionManager.Active.Current, _selector.Selected!, false); + if (ImUtf8.Button($"These settings are inherited from {_collection.Name}.", width)) + collectionManager.Editor.SetModInheritance(collectionManager.Active.Current, selector.Selected!, false); ImGuiUtil.HoverTooltip("You can click this button to copy the current settings to the current selection.\n" + "You can also just change any setting, which will copy the settings with the single setting changed to the current selection."); @@ -127,8 +84,8 @@ public class ModPanelSettingsTab : ITab if (!ImGui.Checkbox("Enabled", ref enabled)) return; - _modManager.SetKnown(_selector.Selected!); - _collectionManager.Editor.SetModState(_collectionManager.Active.Current, _selector.Selected!, enabled); + modManager.SetKnown(selector.Selected!); + collectionManager.Editor.SetModState(collectionManager.Active.Current, selector.Selected!, enabled); } /// @@ -146,7 +103,8 @@ public class ModPanelSettingsTab : ITab if (ImGui.IsItemDeactivatedAfterEdit() && _currentPriority.HasValue) { if (_currentPriority != _settings.Priority.Value) - _collectionManager.Editor.SetModPriority(_collectionManager.Active.Current, _selector.Selected!, new ModPriority(_currentPriority.Value)); + collectionManager.Editor.SetModPriority(collectionManager.Active.Current, selector.Selected!, + new ModPriority(_currentPriority.Value)); _currentPriority = null; } @@ -162,189 +120,15 @@ public class ModPanelSettingsTab : ITab private void DrawRemoveSettings() { const string text = "Inherit Settings"; - if (_inherited || _empty) + if (_inherited || _settings == ModSettings.Empty) return; var scroll = ImGui.GetScrollMaxY() > 0 ? ImGui.GetStyle().ScrollbarSize : 0; ImGui.SameLine(ImGui.GetWindowWidth() - ImGui.CalcTextSize(text).X - ImGui.GetStyle().FramePadding.X * 2 - scroll); if (ImGui.Button(text)) - _collectionManager.Editor.SetModInheritance(_collectionManager.Active.Current, _selector.Selected!, true); + collectionManager.Editor.SetModInheritance(collectionManager.Active.Current, selector.Selected!, true); ImGuiUtil.HoverTooltip("Remove current settings from this collection so that it can inherit them.\n" + "If no inherited collection has settings for this mod, it will be disabled."); } - - /// - /// Draw a single group selector as a combo box. - /// If a description is provided, add a help marker besides it. - /// - private void DrawSingleGroupCombo(IModGroup group, int groupIdx) - { - using var id = ImRaii.PushId(groupIdx); - var selectedOption = _empty ? group.DefaultSettings.AsIndex : _settings.Settings[groupIdx].AsIndex; - ImGui.SetNextItemWidth(UiHelpers.InputTextWidth.X * 3 / 4); - var options = group.Options; - using (var combo = ImRaii.Combo(string.Empty, options[selectedOption].Name)) - { - if (combo) - for (var idx2 = 0; idx2 < options.Count; ++idx2) - { - id.Push(idx2); - var option = options[idx2]; - if (ImGui.Selectable(option.Name, idx2 == selectedOption)) - _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, - Setting.Single(idx2)); - - if (option.Description.Length > 0) - ImGuiUtil.SelectableHelpMarker(option.Description); - - id.Pop(); - } - } - - ImGui.SameLine(); - if (group.Description.Length > 0) - ImGuiUtil.LabeledHelpMarker(group.Name, group.Description); - else - ImGui.TextUnformatted(group.Name); - } - - // Draw a single group selector as a set of radio buttons. - // If a description is provided, add a help marker besides it. - private void DrawSingleGroupRadio(IModGroup group, int groupIdx) - { - using var id = ImRaii.PushId(groupIdx); - var selectedOption = _empty ? group.DefaultSettings.AsIndex : _settings.Settings[groupIdx].AsIndex; - var minWidth = Widget.BeginFramedGroup(group.Name, group.Description); - var options = group.Options; - DrawCollapseHandling(options, minWidth, DrawOptions); - - Widget.EndFramedGroup(); - return; - - void DrawOptions() - { - for (var idx = 0; idx < group.Options.Count; ++idx) - { - using var i = ImRaii.PushId(idx); - var option = options[idx]; - if (ImGui.RadioButton(option.Name, selectedOption == idx)) - _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, - Setting.Single(idx)); - - if (option.Description.Length <= 0) - continue; - - ImGui.SameLine(); - ImGuiComponents.HelpMarker(option.Description); - } - } - } - - - private void DrawCollapseHandling(IReadOnlyList options, float minWidth, Action draw) - { - if (options.Count <= _config.OptionGroupCollapsibleMin) - { - draw(); - } - else - { - var collapseId = ImGui.GetID("Collapse"); - var shown = ImGui.GetStateStorage().GetBool(collapseId, true); - var buttonTextShow = $"Show {options.Count} Options"; - var buttonTextHide = $"Hide {options.Count} Options"; - var buttonWidth = Math.Max(ImGui.CalcTextSize(buttonTextShow).X, ImGui.CalcTextSize(buttonTextHide).X) - + 2 * ImGui.GetStyle().FramePadding.X; - minWidth = Math.Max(buttonWidth, minWidth); - if (shown) - { - var pos = ImGui.GetCursorPos(); - ImGui.Dummy(UiHelpers.IconButtonSize); - using (var _ = ImRaii.Group()) - { - draw(); - } - - - var width = Math.Max(ImGui.GetItemRectSize().X, minWidth); - var endPos = ImGui.GetCursorPos(); - ImGui.SetCursorPos(pos); - if (ImGui.Button(buttonTextHide, new Vector2(width, 0))) - ImGui.GetStateStorage().SetBool(collapseId, !shown); - - ImGui.SetCursorPos(endPos); - } - else - { - var optionWidth = options.Max(o => ImGui.CalcTextSize(o.Name).X) - + ImGui.GetStyle().ItemInnerSpacing.X - + ImGui.GetFrameHeight() - + ImGui.GetStyle().FramePadding.X; - var width = Math.Max(optionWidth, minWidth); - if (ImGui.Button(buttonTextShow, new Vector2(width, 0))) - ImGui.GetStateStorage().SetBool(collapseId, !shown); - } - } - } - - /// - /// Draw a multi group selector as a bordered set of checkboxes. - /// If a description is provided, add a help marker in the title. - /// - private void DrawMultiGroup(IModGroup group, int groupIdx) - { - using var id = ImRaii.PushId(groupIdx); - var flags = _empty ? group.DefaultSettings : _settings.Settings[groupIdx]; - var minWidth = Widget.BeginFramedGroup(group.Name, group.Description); - var options = group.Options; - DrawCollapseHandling(options, minWidth, DrawOptions); - - Widget.EndFramedGroup(); - var label = $"##multi{groupIdx}"; - if (ImGui.IsItemClicked(ImGuiMouseButton.Right)) - ImGui.OpenPopup($"##multi{groupIdx}"); - - DrawMultiPopup(group, groupIdx, label); - return; - - void DrawOptions() - { - for (var idx = 0; idx < options.Count; ++idx) - { - using var i = ImRaii.PushId(idx); - var option = options[idx]; - var setting = flags.HasFlag(idx); - - if (ImGui.Checkbox(option.Name, ref setting)) - { - flags = flags.SetBit(idx, setting); - _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, flags); - } - - if (option.Description.Length > 0) - { - ImGui.SameLine(); - ImGuiComponents.HelpMarker(option.Description); - } - } - } - } - - private void DrawMultiPopup(IModGroup group, int groupIdx, string label) - { - using var style = ImRaii.PushStyle(ImGuiStyleVar.PopupBorderSize, 1); - using var popup = ImRaii.Popup(label); - if (!popup) - return; - - ImGui.TextUnformatted(group.Name); - ImGui.Separator(); - if (ImGui.Selectable("Enable All")) - _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, - Setting.AllBits(group.Options.Count)); - - if (ImGui.Selectable("Disable All")) - _collectionManager.Editor.SetModSetting(_collectionManager.Active.Current, _selector.Selected!, groupIdx, Setting.Zero); - } } From 7553b5da8ac44fe4d058934033a5aa18ff32ffa1 Mon Sep 17 00:00:00 2001 From: ackwell Date: Sat, 4 May 2024 04:01:28 +1000 Subject: [PATCH 148/433] Fix float imprecision on blend weights --- .../Import/Models/Import/VertexAttribute.cs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Penumbra/Import/Models/Import/VertexAttribute.cs b/Penumbra/Import/Models/Import/VertexAttribute.cs index b7f5dcf1..a4651776 100644 --- a/Penumbra/Import/Models/Import/VertexAttribute.cs +++ b/Penumbra/Import/Models/Import/VertexAttribute.cs @@ -138,7 +138,27 @@ public class VertexAttribute return new VertexAttribute( element, - index => BuildNByte4(values[index]) + index => { + // Blend weights are _very_ sensitive to float imprecision - a vertex sum being off + // by one, such as 256, is enough to cause a visible defect. To avoid this, we tweak + // the converted values to have the expected sum, preferencing values with minimal differences. + var originalValues = values[index]; + var byteValues = BuildNByte4(originalValues); + + var adjustment = 255 - byteValues.Select(value => (int)value).Sum(); + while (adjustment != 0) + { + var convertedValues = byteValues.Select(value => value * (1f / 255f)).ToArray(); + var closestIndex = Enumerable.Range(0, 4) + .Select(index => (index, delta: Math.Abs(originalValues[index] - convertedValues[index]))) + .MinBy(x => x.delta) + .index; + byteValues[closestIndex] = (byte)(byteValues[closestIndex] + Math.CopySign(1, adjustment)); + adjustment = 255 - byteValues.Select(value => (int)value).Sum(); + } + + return byteValues; + } ); } From 2a5df2dfb05469a15cf1386e70139a93eaad1d7d Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 4 May 2024 11:19:07 +0200 Subject: [PATCH 149/433] Create permanent backup before migrating collections. --- Penumbra/Configuration.cs | 2 +- Penumbra/Services/BackupService.cs | 14 ++++++++++++-- Penumbra/Services/ConfigMigrationService.cs | 16 +++++++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index 98668e8a..a065bc26 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -136,7 +136,7 @@ public class Configuration : IPluginConfiguration, ISavable /// Contains some default values or boundaries for config values. public static class Constants { - public const int CurrentVersion = 8; + public const int CurrentVersion = 9; public const float MaxAbsoluteSize = 600; public const int DefaultAbsoluteSize = 250; public const float MinAbsoluteSize = 50; diff --git a/Penumbra/Services/BackupService.cs b/Penumbra/Services/BackupService.cs index bd5b3bcc..88b99de1 100644 --- a/Penumbra/Services/BackupService.cs +++ b/Penumbra/Services/BackupService.cs @@ -7,6 +7,10 @@ namespace Penumbra.Services; public class BackupService : IAsyncService { + private readonly Logger _logger; + private readonly DirectoryInfo _configDirectory; + private readonly IReadOnlyList _fileNames; + /// public Task Awaiter { get; } @@ -17,10 +21,16 @@ public class BackupService : IAsyncService /// Start a backup process on the collected files. public BackupService(Logger logger, FilenameService fileNames) { - var files = PenumbraFiles(fileNames); - Awaiter = Task.Run(() => Backup.CreateAutomaticBackup(logger, new DirectoryInfo(fileNames.ConfigDirectory), files)); + _logger = logger; + _fileNames = PenumbraFiles(fileNames); + _configDirectory = new DirectoryInfo(fileNames.ConfigDirectory); + Awaiter = Task.Run(() => Backup.CreateAutomaticBackup(logger, new DirectoryInfo(fileNames.ConfigDirectory), _fileNames)); } + /// Create a permanent backup with a given name for migrations. + public void CreateMigrationBackup(string name) + => Backup.CreatePermanentBackup(_logger, _configDirectory, _fileNames, name); + /// Collect all relevant files for penumbra configuration. private static IReadOnlyList PenumbraFiles(FilenameService fileNames) { diff --git a/Penumbra/Services/ConfigMigrationService.cs b/Penumbra/Services/ConfigMigrationService.cs index fafaa0e5..1f6ac170 100644 --- a/Penumbra/Services/ConfigMigrationService.cs +++ b/Penumbra/Services/ConfigMigrationService.cs @@ -22,7 +22,7 @@ namespace Penumbra.Services; /// Contains everything to migrate from older versions of the config to the current, /// including deprecated fields. /// -public class ConfigMigrationService(SaveService saveService) : IService +public class ConfigMigrationService(SaveService saveService, BackupService backupService) : IService { private Configuration _config = null!; private JObject _data = null!; @@ -73,9 +73,23 @@ public class ConfigMigrationService(SaveService saveService) : IService Version5To6(); Version6To7(); Version7To8(); + Version8To9(); AddColors(config, true); } + // Migrate to ephemeral config. + private void Version8To9() + { + if (_config.Version != 8) + return; + + backupService.CreateMigrationBackup("pre_collection_identifiers"); + _config.Version = 9; + _config.Ephemeral.Version = 9; + _config.Save(); + _config.Ephemeral.Save(); + } + // Migrate to ephemeral config. private void Version7To8() { From d8dad91e899a4055e5fe19dbc1255333a07f9033 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 4 May 2024 11:24:01 +0200 Subject: [PATCH 150/433] Oop, not yet. --- Penumbra/UI/ModsTab/ModPanelSettingsTab.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs index fc5311d9..107a2d04 100644 --- a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs @@ -2,7 +2,6 @@ using ImGuiNET; using OtterGui.Raii; using OtterGui; using OtterGui.Services; -using OtterGui.Text; using OtterGui.Widgets; using Penumbra.Collections; using Penumbra.UI.Classes; @@ -70,7 +69,7 @@ public class ModPanelSettingsTab( using var color = ImRaii.PushColor(ImGuiCol.Button, Colors.PressEnterWarningBg); var width = new Vector2(ImGui.GetContentRegionAvail().X, 0); - if (ImUtf8.Button($"These settings are inherited from {_collection.Name}.", width)) + if (ImGui.Button($"These settings are inherited from {_collection.Name}.", width)) collectionManager.Editor.SetModInheritance(collectionManager.Active.Current, selector.Selected!, false); ImGuiUtil.HoverTooltip("You can click this button to copy the current settings to the current selection.\n" From 1f2f66b1141f83613c3799faf80439b1142bd571 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 4 May 2024 11:34:02 +0200 Subject: [PATCH 151/433] Meh. --- OtterGui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OtterGui b/OtterGui index 20c4a6c5..bc2afed8 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 20c4a6c53103d9fa8dec63babc628c9d01f094c0 +Subproject commit bc2afed8a873d1f9517eefe7a7296bc5b83e693b From bbbf65eb4c0c9d8dd77ac8f8101ad2ac86433c1c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 9 May 2024 15:49:49 +0200 Subject: [PATCH 152/433] Fix bug preventing deduplication. --- Penumbra/Mods/Editor/DuplicateManager.cs | 3 +++ Penumbra/Mods/Groups/ModSaveGroup.cs | 2 +- Penumbra/UI/Classes/CollectionSelectHeader.cs | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Penumbra/Mods/Editor/DuplicateManager.cs b/Penumbra/Mods/Editor/DuplicateManager.cs index 84a832a2..2e6dc1d6 100644 --- a/Penumbra/Mods/Editor/DuplicateManager.cs +++ b/Penumbra/Mods/Editor/DuplicateManager.cs @@ -84,7 +84,10 @@ public class DuplicateManager(ModManager modManager, SaveService saveService, Co if (useModManager) modManager.OptionEditor.SetFiles(subMod, dict, SaveType.ImmediateSync); else + { + subMod.Files = dict; saveService.ImmediateSaveSync(new ModSaveGroup(mod.ModPath, subMod, config.ReplaceNonAsciiOnImport)); + } } } diff --git a/Penumbra/Mods/Groups/ModSaveGroup.cs b/Penumbra/Mods/Groups/ModSaveGroup.cs index ed3c8857..7efc76a6 100644 --- a/Penumbra/Mods/Groups/ModSaveGroup.cs +++ b/Penumbra/Mods/Groups/ModSaveGroup.cs @@ -40,7 +40,7 @@ public readonly struct ModSaveGroup : ISavable _basePath = basePath; _defaultMod = container as DefaultSubMod; _onlyAscii = onlyAscii; - if (_defaultMod == null) + if (_defaultMod != null) { _groupIdx = -1; _group = null; diff --git a/Penumbra/UI/Classes/CollectionSelectHeader.cs b/Penumbra/UI/Classes/CollectionSelectHeader.cs index de2b6a34..bff0092a 100644 --- a/Penumbra/UI/Classes/CollectionSelectHeader.cs +++ b/Penumbra/UI/Classes/CollectionSelectHeader.cs @@ -89,7 +89,7 @@ public class CollectionSelectHeader var collection = _resolver.PlayerCollection(); return CheckCollection(collection) switch { - CollectionState.Empty => (collection, "None", "The base collection is configured to use no mods.", true), + CollectionState.Empty => (collection, "None", "The loaded player character is configured to use no mods.", true), CollectionState.Selected => (collection, collection.Name, "The collection configured to apply to the loaded player character is already selected as the current collection.", true), CollectionState.Available => (collection, collection.Name, From 32dbf419e254ca432055f6e4b08b684b5a03447b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 9 May 2024 19:58:35 +0200 Subject: [PATCH 153/433] Fix single group default options not applying. --- Penumbra/Mods/Editor/DuplicateManager.cs | 2 ++ Penumbra/Mods/Groups/SingleModGroup.cs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Penumbra/Mods/Editor/DuplicateManager.cs b/Penumbra/Mods/Editor/DuplicateManager.cs index 2e6dc1d6..47aa18dc 100644 --- a/Penumbra/Mods/Editor/DuplicateManager.cs +++ b/Penumbra/Mods/Editor/DuplicateManager.cs @@ -82,7 +82,9 @@ public class DuplicateManager(ModManager modManager, SaveService saveService, Co return; if (useModManager) + { modManager.OptionEditor.SetFiles(subMod, dict, SaveType.ImmediateSync); + } else { subMod.Files = dict; diff --git a/Penumbra/Mods/Groups/SingleModGroup.cs b/Penumbra/Mods/Groups/SingleModGroup.cs index 459cec4a..bc463c1e 100644 --- a/Penumbra/Mods/Groups/SingleModGroup.cs +++ b/Penumbra/Mods/Groups/SingleModGroup.cs @@ -93,7 +93,7 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) { - if (!IsOption) + if (OptionData.Count == 0) return; OptionData[setting.AsIndex].AddDataTo(redirections, manipulations); From d47d31b665ffa4cd14774f04888a6c50c7a37a72 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 14 May 2024 18:10:59 +0200 Subject: [PATCH 154/433] achieve feature parity... I think. --- OtterGui | 2 +- Penumbra/Mods/Groups/IModGroup.cs | 2 +- Penumbra/UI/ModsTab/DescriptionEditPopup.cs | 114 ++++++ Penumbra/UI/ModsTab/ModGroupEditDrawer.cs | 424 ++++++++++++++++---- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 420 +------------------ 5 files changed, 473 insertions(+), 489 deletions(-) create mode 100644 Penumbra/UI/ModsTab/DescriptionEditPopup.cs diff --git a/OtterGui b/OtterGui index bc2afed8..866389b3 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit bc2afed8a873d1f9517eefe7a7296bc5b83e693b +Subproject commit 866389b3988d9c4926a786f6c78ac9d5265591ac diff --git a/Penumbra/Mods/Groups/IModGroup.cs b/Penumbra/Mods/Groups/IModGroup.cs index d7138434..2ec60f7e 100644 --- a/Penumbra/Mods/Groups/IModGroup.cs +++ b/Penumbra/Mods/Groups/IModGroup.cs @@ -20,7 +20,7 @@ public enum GroupDrawBehaviour public interface IModGroup { - public const int MaxMultiOptions = 63; + public const int MaxMultiOptions = 32; public Mod Mod { get; } public string Name { get; set; } diff --git a/Penumbra/UI/ModsTab/DescriptionEditPopup.cs b/Penumbra/UI/ModsTab/DescriptionEditPopup.cs new file mode 100644 index 00000000..c284afc3 --- /dev/null +++ b/Penumbra/UI/ModsTab/DescriptionEditPopup.cs @@ -0,0 +1,114 @@ +using Dalamud.Interface.Utility; +using ImGuiNET; +using OtterGui.Services; +using OtterGui.Text; +using Penumbra.Mods; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Manager; +using Penumbra.Mods.SubMods; + +namespace Penumbra.UI.ModsTab; + +public class DescriptionEditPopup(ModManager modManager) : IUiService +{ + private static ReadOnlySpan PopupId + => "PenumbraEditDescription"u8; + + private bool _hasBeenEdited; + private string _description = string.Empty; + + private object? _current; + private bool _opened; + + public void Open(Mod mod) + { + _current = mod; + _opened = true; + _hasBeenEdited = false; + _description = mod.Description; + } + + public void Open(IModGroup group) + { + _current = group; + _opened = true; + _hasBeenEdited = false; + _description = group.Description; + } + + public void Open(IModOption option) + { + _current = option; + _opened = true; + _hasBeenEdited = false; + _description = option.Description; + } + + public void Draw() + { + if (_current == null) + return; + + if (_opened) + { + _opened = false; + ImUtf8.OpenPopup(PopupId); + } + + var inputSize = ImGuiHelpers.ScaledVector2(800); + using var popup = ImUtf8.Popup(PopupId); + if (!popup) + return; + + if (ImGui.IsWindowAppearing()) + ImGui.SetKeyboardFocusHere(); + + ImUtf8.InputMultiLineOnDeactivated("##editDescription"u8, ref _description, inputSize); + _hasBeenEdited |= ImGui.IsItemEdited(); + UiHelpers.DefaultLineSpace(); + + var buttonSize = new Vector2(ImUtf8.GlobalScale * 100, 0); + + var width = 2 * buttonSize.X + + 4 * ImUtf8.FramePadding.X + + ImUtf8.ItemSpacing.X; + + ImGui.SetCursorPosX((inputSize.X - width) / 2); + DrawSaveButton(buttonSize); + ImGui.SameLine(); + DrawCancelButton(buttonSize); + } + + private void DrawSaveButton(Vector2 buttonSize) + { + if (!ImUtf8.ButtonEx("Save"u8, _hasBeenEdited ? [] : "No changes made yet."u8, buttonSize, !_hasBeenEdited)) + return; + + switch (_current) + { + case Mod mod: + modManager.DataEditor.ChangeModDescription(mod, _description); + break; + case IModGroup group: + modManager.OptionEditor.ChangeGroupDescription(group, _description); + break; + case IModOption option: + modManager.OptionEditor.ChangeOptionDescription(option, _description); + break; + } + + _description = string.Empty; + _hasBeenEdited = false; + ImGui.CloseCurrentPopup(); + } + + private void DrawCancelButton(Vector2 buttonSize) + { + if (!ImUtf8.Button("Cancel"u8, buttonSize) && !ImGui.IsKeyPressed(ImGuiKey.Escape)) + return; + + _description = string.Empty; + _hasBeenEdited = false; + ImGui.CloseCurrentPopup(); + } +} diff --git a/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs index 6b62d5b8..5652fa98 100644 --- a/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs @@ -1,11 +1,12 @@ using Dalamud.Interface; using Dalamud.Interface.Internal.Notifications; -using Dalamud.Interface.Utility; using ImGuiNET; using OtterGui; using OtterGui.Classes; using OtterGui.Raii; using OtterGui.Services; +using OtterGui.Text; +using OtterGui.Text.EndObjects; using Penumbra.Mods; using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; @@ -17,87 +18,79 @@ using Penumbra.UI.Classes; namespace Penumbra.UI.ModsTab; -public sealed class ModGroupEditDrawer(ModManager modManager, Configuration config, FilenameService filenames) : IUiService +public sealed class ModGroupEditDrawer( + ModManager modManager, + Configuration config, + FilenameService filenames, + DescriptionEditPopup descriptionPopup) : IUiService { + private static ReadOnlySpan DragDropLabel + => "##DragOption"u8; + private Vector2 _buttonSize; + private Vector2 _availableWidth; private float _priorityWidth; private float _groupNameWidth; + private float _optionNameWidth; private float _spacing; + private Vector2 _optionIdxSelectable; + private bool _deleteEnabled; private string? _currentGroupName; private ModPriority? _currentGroupPriority; private IModGroup? _currentGroupEdited; - private bool _isGroupNameValid; - private IModGroup? _deleteGroup; - private IModGroup? _moveGroup; - private int _moveTo; + private bool _isGroupNameValid = true; - private string? _currentOptionName; - private ModPriority? _currentOptionPriority; - private IModOption? _currentOptionEdited; - private IModOption? _deleteOption; + private string? _newOptionName; + private IModGroup? _newOptionGroup; + private readonly Queue _actionQueue = new(); - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void SameLine() - => ImGui.SameLine(0, _spacing); + private IModGroup? _dragDropGroup; + private IModOption? _dragDropOption; public void Draw(Mod mod) { - _buttonSize = new Vector2(ImGui.GetFrameHeight()); - _priorityWidth = 50 * ImGuiHelpers.GlobalScale; - _groupNameWidth = 350f * ImGuiHelpers.GlobalScale; - _spacing = ImGui.GetStyle().ItemInnerSpacing.X; + PrepareStyle(); + using var id = ImUtf8.PushId("##GroupEdit"u8); + foreach (var (group, groupIdx) in mod.Groups.WithIndex()) + DrawGroup(group, groupIdx); - FinishGroupCleanup(); - } - - private void FinishGroupCleanup() - { - if (_deleteGroup != null) - { - modManager.OptionEditor.DeleteModGroup(_deleteGroup); - _deleteGroup = null; - } - - if (_deleteOption != null) - { - modManager.OptionEditor.DeleteOption(_deleteOption); - _deleteOption = null; - } - - if (_moveGroup != null) - { - modManager.OptionEditor.MoveModGroup(_moveGroup, _moveTo); - _moveGroup = null; - } + while (_actionQueue.TryDequeue(out var action)) + action.Invoke(); } private void DrawGroup(IModGroup group, int idx) { - using var id = ImRaii.PushId(idx); + using var id = ImUtf8.PushId(idx); using var frame = ImRaii.FramedGroup($"Group #{idx + 1}"); - DrawGroupNameRow(group); + DrawGroupNameRow(group, idx); switch (group) { case SingleModGroup s: - DrawSingleGroup(s, idx); + DrawSingleGroup(s); break; case MultiModGroup m: - DrawMultiGroup(m, idx); + DrawMultiGroup(m); break; case ImcModGroup i: - DrawImcGroup(i, idx); + DrawImcGroup(i); break; } } - private void DrawGroupNameRow(IModGroup group) + private void DrawGroupNameRow(IModGroup group, int idx) { DrawGroupName(group); - SameLine(); + ImUtf8.SameLineInner(); + DrawGroupMoveButtons(group, idx); + ImUtf8.SameLineInner(); + DrawGroupOpenFile(group, idx); + ImUtf8.SameLineInner(); + DrawGroupDescription(group); + ImUtf8.SameLineInner(); DrawGroupDelete(group); - SameLine(); + ImUtf8.SameLineInner(); DrawGroupPriority(group); } @@ -106,11 +99,11 @@ public sealed class ModGroupEditDrawer(ModManager modManager, Configuration conf var text = _currentGroupEdited == group ? _currentGroupName ?? group.Name : group.Name; ImGui.SetNextItemWidth(_groupNameWidth); using var border = ImRaii.PushFrameBorder(UiHelpers.ScaleX2, Colors.RegexWarningBorder, !_isGroupNameValid); - if (ImGui.InputText("##GroupName", ref text, 256)) + if (ImUtf8.InputText("##GroupName"u8, ref text)) { _currentGroupEdited = group; _currentGroupName = text; - _isGroupNameValid = ModGroupEditor.VerifyFileName(group.Mod, group, text, false); + _isGroupNameValid = text == group.Name || ModGroupEditor.VerifyFileName(group.Mod, group, text, false); } if (ImGui.IsItemDeactivated()) @@ -123,20 +116,21 @@ public sealed class ModGroupEditDrawer(ModManager modManager, Configuration conf } var tt = _isGroupNameValid - ? "Group Name" - : "Current name can not be used for this group."; - ImGuiUtil.HoverTooltip(tt); + ? "Change the Group name."u8 + : "Current name can not be used for this group."u8; + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, tt); } private void DrawGroupDelete(IModGroup group) { - var enabled = config.DeleteModModifier.IsActive(); - var tt = enabled - ? "Delete this option group." - : $"Delete this option group.\nHold {config.DeleteModModifier} while clicking to delete."; + if (ImUtf8.IconButton(FontAwesomeIcon.Trash, !_deleteEnabled)) + _actionQueue.Enqueue(() => modManager.OptionEditor.DeleteModGroup(group)); - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Trash.ToIconString(), _buttonSize, tt, !enabled, true)) - _deleteGroup = group; + if (_deleteEnabled) + ImUtf8.HoverTooltip("Delete this option group."u8); + else + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, + $"Delete this option group.\nHold {config.DeleteModModifier} while clicking to delete."); } private void DrawGroupPriority(IModGroup group) @@ -162,36 +156,41 @@ public sealed class ModGroupEditDrawer(ModManager modManager, Configuration conf ImGuiUtil.HoverTooltip("Group Priority"); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void DrawGroupDescription(IModGroup group) + { + if (ImUtf8.IconButton(FontAwesomeIcon.Edit, "Edit group description."u8)) + descriptionPopup.Open(group); + } + private void DrawGroupMoveButtons(IModGroup group, int idx) { var isFirst = idx == 0; - var tt = isFirst ? "Can not move this group further upwards." : $"Move this group up to group {idx}."; - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.ArrowUp.ToIconString(), UiHelpers.IconButtonSize, tt, isFirst, true)) - { - _moveGroup = group; - _moveTo = idx - 1; - } + if (ImUtf8.IconButton(FontAwesomeIcon.ArrowUp, isFirst)) + _actionQueue.Enqueue(() => modManager.OptionEditor.MoveModGroup(group, idx - 1)); - SameLine(); + if (isFirst) + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, "Can not move this group further upwards."u8); + else + ImUtf8.HoverTooltip($"Move this group up to group {idx}."); + + + ImUtf8.SameLineInner(); var isLast = idx == group.Mod.Groups.Count - 1; - tt = isLast - ? "Can not move this group further downwards." - : $"Move this group down to group {idx + 2}."; - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.ArrowDown.ToIconString(), UiHelpers.IconButtonSize, tt, isLast, true)) - { - _moveGroup = group; - _moveTo = idx + 1; - } + if (ImUtf8.IconButton(FontAwesomeIcon.ArrowDown, isLast)) + _actionQueue.Enqueue(() => modManager.OptionEditor.MoveModGroup(group, idx + 1)); + + if (isLast) + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, "Can not move this group further downwards."u8); + else + ImUtf8.HoverTooltip($"Move this group down to group {idx + 2}."); } private void DrawGroupOpenFile(IModGroup group, int idx) { var fileName = filenames.OptionGroupFile(group.Mod, idx, config.ReplaceNonAsciiOnImport); var fileExists = File.Exists(fileName); - var tt = fileExists - ? $"Open the {group.Name} json file in the text editor of your choice." - : $"The {group.Name} json file does not exist."; - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.FileExport.ToIconString(), UiHelpers.IconButtonSize, tt, !fileExists, true)) + if (ImUtf8.IconButton(FontAwesomeIcon.FileExport, !fileExists)) try { Process.Start(new ProcessStartInfo(fileName) { UseShellExecute = true }); @@ -200,15 +199,274 @@ public sealed class ModGroupEditDrawer(ModManager modManager, Configuration conf { Penumbra.Messager.NotificationMessage(e, "Could not open editor.", NotificationType.Error); } + + if (fileExists) + ImUtf8.HoverTooltip($"Open the {group.Name} json file in the text editor of your choice."); + else + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, $"The {group.Name} json file does not exist."); } + private void DrawSingleGroup(SingleModGroup group) + { + foreach (var (option, optionIdx) in group.OptionData.WithIndex()) + { + using var id = ImRaii.PushId(optionIdx); + DrawOptionPosition(group, option, optionIdx); - private void DrawSingleGroup(SingleModGroup group, int idx) - { } + ImUtf8.SameLineInner(); + DrawOptionDefaultSingleBehaviour(group, option, optionIdx); - private void DrawMultiGroup(MultiModGroup group, int idx) - { } + ImUtf8.SameLineInner(); + DrawOptionName(option); - private void DrawImcGroup(ImcModGroup group, int idx) - { } + ImUtf8.SameLineInner(); + DrawOptionDescription(option); + + ImUtf8.SameLineInner(); + DrawOptionDelete(option); + + ImUtf8.SameLineInner(); + ImGui.Dummy(new Vector2(_priorityWidth, 0)); + } + + DrawNewOption(group); + var convertible = group.Options.Count <= IModGroup.MaxMultiOptions; + if (ImUtf8.ButtonEx("Convert to Multi Group", _availableWidth, !convertible)) + _actionQueue.Enqueue(() => modManager.OptionEditor.SingleEditor.ChangeToMulti(group)); + if (!convertible) + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, + "Can not convert to multi group since maximum number of options is exceeded."u8); + } + + private void DrawMultiGroup(MultiModGroup group) + { + foreach (var (option, optionIdx) in group.OptionData.WithIndex()) + { + using var id = ImRaii.PushId(optionIdx); + DrawOptionPosition(group, option, optionIdx); + + ImUtf8.SameLineInner(); + DrawOptionDefaultMultiBehaviour(group, option, optionIdx); + + ImUtf8.SameLineInner(); + DrawOptionName(option); + + ImUtf8.SameLineInner(); + DrawOptionDescription(option); + + ImUtf8.SameLineInner(); + DrawOptionDelete(option); + + ImUtf8.SameLineInner(); + DrawOptionPriority(option); + } + + DrawNewOption(group); + if (ImUtf8.Button("Convert to Single Group"u8, _availableWidth)) + _actionQueue.Enqueue(() => modManager.OptionEditor.MultiEditor.ChangeToSingle(group)); + } + + private void DrawImcGroup(ImcModGroup group) + { + // TODO + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void DrawOptionPosition(IModGroup group, IModOption option, int optionIdx) + { + ImGui.AlignTextToFramePadding(); + ImUtf8.Selectable($"Option #{optionIdx + 1}", false, size: _optionIdxSelectable); + Target(group, optionIdx); + Source(option); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void DrawOptionDefaultSingleBehaviour(IModGroup group, IModOption option, int optionIdx) + { + var isDefaultOption = group.DefaultSettings.AsIndex == optionIdx; + if (ImUtf8.RadioButton("##default"u8, isDefaultOption)) + modManager.OptionEditor.ChangeModGroupDefaultOption(group, Setting.Single(optionIdx)); + ImUtf8.HoverTooltip($"Set {option.Name} as the default choice for this group."); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void DrawOptionDefaultMultiBehaviour(IModGroup group, IModOption option, int optionIdx) + { + var isDefaultOption = group.DefaultSettings.HasFlag(optionIdx); + if (ImUtf8.Checkbox("##default"u8, ref isDefaultOption)) + modManager.OptionEditor.ChangeModGroupDefaultOption(group, group.DefaultSettings.SetBit(optionIdx, isDefaultOption)); + ImUtf8.HoverTooltip($"{(isDefaultOption ? "Disable"u8 : "Enable"u8)} {option.Name} per default in this group."); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void DrawOptionDescription(IModOption option) + { + if (ImUtf8.IconButton(FontAwesomeIcon.Edit, "Edit option description."u8)) + descriptionPopup.Open(option); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void DrawOptionPriority(MultiSubMod option) + { + var priority = option.Priority.Value; + ImGui.SetNextItemWidth(_priorityWidth); + if (ImUtf8.InputScalarOnDeactivated("##Priority"u8, ref priority)) + modManager.OptionEditor.MultiEditor.ChangeOptionPriority(option, new ModPriority(priority)); + ImUtf8.HoverTooltip("Option priority inside the mod."u8); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void DrawOptionName(IModOption option) + { + var name = option.Name; + ImGui.SetNextItemWidth(_optionNameWidth); + if (ImUtf8.InputTextOnDeactivated("##Name"u8, ref name)) + modManager.OptionEditor.RenameOption(option, name); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void DrawOptionDelete(IModOption option) + { + if (ImUtf8.IconButton(FontAwesomeIcon.Trash, !_deleteEnabled)) + _actionQueue.Enqueue(() => modManager.OptionEditor.DeleteOption(option)); + + if (_deleteEnabled) + ImUtf8.HoverTooltip("Delete this option."u8); + else + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, + $"Delete this option.\nHold {config.DeleteModModifier} while clicking to delete."); + } + + private void DrawNewOption(SingleModGroup group) + { + var count = group.Options.Count; + if (count >= int.MaxValue) + return; + + DrawNewOptionBase(group, count); + + var validName = _newOptionName?.Length > 0; + if (ImUtf8.IconButton(FontAwesomeIcon.Plus, validName + ? "Add a new option to this group."u8 + : "Please enter a name for the new option."u8, !validName)) + { + modManager.OptionEditor.SingleEditor.AddOption(group, _newOptionName!); + _newOptionName = null; + } + } + + private void DrawNewOption(MultiModGroup group) + { + var count = group.Options.Count; + if (count >= IModGroup.MaxMultiOptions) + return; + + DrawNewOptionBase(group, count); + + var validName = _newOptionName?.Length > 0; + if (ImUtf8.IconButton(FontAwesomeIcon.Plus, validName + ? "Add a new option to this group."u8 + : "Please enter a name for the new option."u8, !validName)) + { + modManager.OptionEditor.MultiEditor.AddOption(group, _newOptionName!); + _newOptionName = null; + } + } + + private void DrawNewOption(ImcModGroup group) + { + // TODO + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void DrawNewOptionBase(IModGroup group, int count) + { + ImUtf8.Selectable($"Option #{count + 1}", false, size: _optionIdxSelectable); + Target(group, count); + + ImUtf8.SameLineInner(); + ImUtf8.IconDummy(); + + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(_optionNameWidth); + var newName = _newOptionGroup == group + ? _newOptionName ?? string.Empty + : string.Empty; + if (ImUtf8.InputText("##newOption"u8, ref newName, "Add new option..."u8)) + { + _newOptionName = newName; + _newOptionGroup = group; + } + + ImUtf8.SameLineInner(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void Source(IModOption option) + { + if (option.Group is not ITexToolsGroup) + return; + + using var source = ImUtf8.DragDropSource(); + if (!source) + return; + + if (!DragDropSource.SetPayload(DragDropLabel)) + { + _dragDropGroup = option.Group; + _dragDropOption = option; + } + + ImGui.TextUnformatted($"Dragging option {option.Name} from group {option.Group.Name}..."); + } + + private void Target(IModGroup group, int optionIdx) + { + if (group is not ITexToolsGroup) + return; + + if (_dragDropGroup != group && _dragDropGroup != null && group is MultiModGroup { Options.Count: >= IModGroup.MaxMultiOptions }) + return; + + using var target = ImRaii.DragDropTarget(); + if (!target.Success || !DragDropTarget.CheckPayload(DragDropLabel)) + return; + + if (_dragDropGroup != null && _dragDropOption != null) + { + if (_dragDropGroup == group) + { + var sourceOption = _dragDropOption; + _actionQueue.Enqueue(() => modManager.OptionEditor.MoveOption(sourceOption, optionIdx)); + } + else + { + // Move from one group to another by deleting, then adding, then moving the option. + var sourceOption = _dragDropOption; + _actionQueue.Enqueue(() => + { + modManager.OptionEditor.DeleteOption(sourceOption); + if (modManager.OptionEditor.AddOption(group, sourceOption) is { } newOption) + modManager.OptionEditor.MoveOption(newOption, optionIdx); + }); + } + } + + _dragDropGroup = null; + _dragDropOption = null; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void PrepareStyle() + { + var totalWidth = 400f * ImUtf8.GlobalScale; + _buttonSize = new Vector2(ImUtf8.FrameHeight); + _priorityWidth = 50 * ImUtf8.GlobalScale; + _availableWidth = new Vector2(totalWidth + 3 * _spacing + 2 * _buttonSize.X + _priorityWidth, 0); + _groupNameWidth = totalWidth - 3 * (_buttonSize.X + _spacing); + _spacing = ImGui.GetStyle().ItemInnerSpacing.X; + _optionIdxSelectable = ImUtf8.CalcTextSize("Option #88."u8); + _optionNameWidth = totalWidth - _optionIdxSelectable.X - _buttonSize.X - 2 * _spacing; + _deleteEnabled = config.DeleteModModifier.IsActive(); + } } diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index 862852fa..a5db15b6 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -1,21 +1,17 @@ using Dalamud.Interface; using Dalamud.Interface.Components; using Dalamud.Interface.Internal.Notifications; -using Dalamud.Interface.Utility; using ImGuiNET; using OtterGui; using OtterGui.Raii; using OtterGui.Widgets; using OtterGui.Classes; -using Penumbra.Api.Enums; using Penumbra.Mods; using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; using Penumbra.Services; using Penumbra.UI.AdvancedWindow; -using Penumbra.Mods.Groups; using Penumbra.Mods.Settings; -using Penumbra.Mods.SubMods; using Penumbra.Mods.Manager.OptionEditor; namespace Penumbra.UI.ModsTab; @@ -30,15 +26,13 @@ public class ModPanelEditTab( FilenameService filenames, ModExportManager modExportManager, Configuration config, - PredefinedTagManager predefinedTagManager) + PredefinedTagManager predefinedTagManager, + ModGroupEditDrawer groupEditDrawer, + DescriptionEditPopup descriptionPopup) : ITab { - private readonly ModManager _modManager = modManager; - private readonly TagButtons _modTags = new(); - private Vector2 _cellPadding = Vector2.Zero; - private Vector2 _itemSpacing = Vector2.Zero; private ModFileSystem.Leaf _leaf = null!; private Mod _mod = null!; @@ -54,9 +48,6 @@ public class ModPanelEditTab( _leaf = selector.SelectedLeaf!; _mod = selector.Selected!; - _cellPadding = ImGui.GetStyle().CellPadding with { X = 2 * UiHelpers.Scale }; - _itemSpacing = ImGui.GetStyle().CellPadding with { X = 4 * UiHelpers.Scale }; - EditButtons(); EditRegularMeta(); UiHelpers.DefaultLineSpace(); @@ -77,21 +68,18 @@ public class ModPanelEditTab( var tagIdx = _modTags.Draw("Mod Tags: ", "Edit tags by clicking them, or add new tags. Empty tags are removed.", _mod.ModTags, out var editedTag, rightEndOffset: sharedTagButtonOffset); if (tagIdx >= 0) - _modManager.DataEditor.ChangeModTag(_mod, tagIdx, editedTag); + modManager.DataEditor.ChangeModTag(_mod, tagIdx, editedTag); if (sharedTagsEnabled) predefinedTagManager.DrawAddFromSharedTagsAndUpdateTags(selector.Selected!.LocalTags, selector.Selected!.ModTags, false, selector.Selected!); UiHelpers.DefaultLineSpace(); - AddOptionGroup.Draw(filenames, _modManager, _mod, config.ReplaceNonAsciiOnImport); + AddOptionGroup.Draw(filenames, modManager, _mod, config.ReplaceNonAsciiOnImport); UiHelpers.DefaultLineSpace(); - for (var groupIdx = 0; groupIdx < _mod.Groups.Count; ++groupIdx) - EditGroup(groupIdx); - - EndActions(); - DescriptionEdit.DrawPopup(_modManager); + groupEditDrawer.Draw(_mod); + descriptionPopup.Draw(); } public void Reset() @@ -99,7 +87,6 @@ public class ModPanelEditTab( AddOptionGroup.Reset(); MoveDirectory.Reset(); Input.Reset(); - OptionTable.Reset(); } /// The general edit row for non-detailed mod edits. @@ -117,10 +104,10 @@ public class ModPanelEditTab( if (ImGuiUtil.DrawDisabledButton("Reload Mod", buttonSize, "Reload the current mod from its files.\n" + "If the mod directory or meta file do not exist anymore or if the new mod name is empty, the mod is deleted instead.", false)) - _modManager.ReloadMod(_mod); + modManager.ReloadMod(_mod); BackupButtons(buttonSize); - MoveDirectory.Draw(_modManager, _mod, buttonSize); + MoveDirectory.Draw(modManager, _mod, buttonSize); UiHelpers.DefaultLineSpace(); DrawUpdateBibo(buttonSize); @@ -169,7 +156,7 @@ public class ModPanelEditTab( : $"Exported mod \"{backup.Name}\" does not exist."; ImGui.SameLine(); if (ImGuiUtil.DrawDisabledButton("Restore From Export", buttonSize, tt, !backup.Exists || !config.DeleteModModifier.IsActive())) - backup.Restore(_modManager); + backup.Restore(modManager); if (backup.Exists) { ImGui.SameLine(); @@ -186,24 +173,24 @@ public class ModPanelEditTab( private void EditRegularMeta() { if (Input.Text("Name", Input.Name, Input.None, _mod.Name, out var newName, 256, UiHelpers.InputTextWidth.X)) - _modManager.DataEditor.ChangeModName(_mod, newName); + modManager.DataEditor.ChangeModName(_mod, newName); if (Input.Text("Author", Input.Author, Input.None, _mod.Author, out var newAuthor, 256, UiHelpers.InputTextWidth.X)) - _modManager.DataEditor.ChangeModAuthor(_mod, newAuthor); + modManager.DataEditor.ChangeModAuthor(_mod, newAuthor); if (Input.Text("Version", Input.Version, Input.None, _mod.Version, out var newVersion, 32, UiHelpers.InputTextWidth.X)) - _modManager.DataEditor.ChangeModVersion(_mod, newVersion); + modManager.DataEditor.ChangeModVersion(_mod, newVersion); if (Input.Text("Website", Input.Website, Input.None, _mod.Website, out var newWebsite, 256, UiHelpers.InputTextWidth.X)) - _modManager.DataEditor.ChangeModWebsite(_mod, newWebsite); + modManager.DataEditor.ChangeModWebsite(_mod, newWebsite); using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, new Vector2(UiHelpers.ScaleX3)); var reducedSize = new Vector2(UiHelpers.InputTextMinusButton3, 0); if (ImGui.Button("Edit Description", reducedSize)) - _delayedActions.Enqueue(() => DescriptionEdit.OpenPopup(_mod, Input.Description)); + descriptionPopup.Open(_mod); ImGui.SameLine(); var fileExists = File.Exists(filenames.ModMetaPath(_mod)); @@ -215,16 +202,6 @@ public class ModPanelEditTab( Process.Start(new ProcessStartInfo(filenames.ModMetaPath(_mod)) { UseShellExecute = true }); } - /// Do some edits outside of iterations. - private readonly Queue _delayedActions = new(); - - /// Delete a marked group or option outside of iteration. - private void EndActions() - { - while (_delayedActions.TryDequeue(out var action)) - action.Invoke(); - } - /// Text input to add a new option group at the end of the current groups. private static class AddOptionGroup { @@ -309,372 +286,6 @@ public class ModPanelEditTab( } } - /// Open a popup to edit a multi-line mod or option description. - private static class DescriptionEdit - { - private const string PopupName = "Edit Description"; - private static string _newDescription = string.Empty; - private static string _oldDescription = string.Empty; - private static int _newDescriptionIdx = -1; - private static int _newDescriptionOptionIdx = -1; - private static Mod? _mod; - - public static void OpenPopup(Mod mod, int groupIdx, int optionIdx = -1) - { - _newDescriptionIdx = groupIdx; - _newDescriptionOptionIdx = optionIdx; - _newDescription = groupIdx < 0 - ? mod.Description - : optionIdx < 0 - ? mod.Groups[groupIdx].Description - : mod.Groups[groupIdx].Options[optionIdx].Description; - _oldDescription = _newDescription; - - _mod = mod; - ImGui.OpenPopup(PopupName); - } - - public static void DrawPopup(ModManager modManager) - { - if (_mod == null) - return; - - using var popup = ImRaii.Popup(PopupName); - if (!popup) - return; - - if (ImGui.IsWindowAppearing()) - ImGui.SetKeyboardFocusHere(); - - ImGui.InputTextMultiline("##editDescription", ref _newDescription, 4096, ImGuiHelpers.ScaledVector2(800, 800)); - UiHelpers.DefaultLineSpace(); - - var buttonSize = ImGuiHelpers.ScaledVector2(100, 0); - var width = 2 * buttonSize.X - + 4 * ImGui.GetStyle().FramePadding.X - + ImGui.GetStyle().ItemSpacing.X; - ImGui.SetCursorPosX((800 * UiHelpers.Scale - width) / 2); - - var tooltip = _newDescription != _oldDescription ? string.Empty : "No changes made yet."; - - if (ImGuiUtil.DrawDisabledButton("Save", buttonSize, tooltip, tooltip.Length > 0)) - { - switch (_newDescriptionIdx) - { - case Input.Description: - modManager.DataEditor.ChangeModDescription(_mod, _newDescription); - break; - case >= 0: - if (_newDescriptionOptionIdx < 0) - modManager.OptionEditor.ChangeGroupDescription(_mod.Groups[_newDescriptionIdx], _newDescription); - else - modManager.OptionEditor.ChangeOptionDescription(_mod.Groups[_newDescriptionIdx].Options[_newDescriptionOptionIdx], - _newDescription); - - break; - } - - ImGui.CloseCurrentPopup(); - } - - ImGui.SameLine(); - if (!ImGui.Button("Cancel", buttonSize) - && !ImGui.IsKeyPressed(ImGuiKey.Escape)) - return; - - _newDescriptionIdx = Input.None; - _newDescription = string.Empty; - ImGui.CloseCurrentPopup(); - } - } - - private void EditGroup(int groupIdx) - { - var group = _mod.Groups[groupIdx]; - using var id = ImRaii.PushId(groupIdx); - using var frame = ImRaii.FramedGroup($"Group #{groupIdx + 1}"); - - using var style = ImRaii.PushStyle(ImGuiStyleVar.CellPadding, _cellPadding) - .Push(ImGuiStyleVar.ItemSpacing, _itemSpacing); - - if (Input.Text("##Name", groupIdx, Input.None, group.Name, out var newGroupName, 256, UiHelpers.InputTextWidth.X)) - _modManager.OptionEditor.RenameModGroup(group, newGroupName); - - ImGuiUtil.HoverTooltip("Group Name"); - ImGui.SameLine(); - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Trash.ToIconString(), UiHelpers.IconButtonSize, - "Delete this option group.\nHold Control while clicking to delete.", !ImGui.GetIO().KeyCtrl, true)) - _delayedActions.Enqueue(() => _modManager.OptionEditor.DeleteModGroup(group)); - - ImGui.SameLine(); - - if (Input.Priority("##Priority", groupIdx, Input.None, group.Priority, out var priority, 50 * UiHelpers.Scale)) - _modManager.OptionEditor.ChangeGroupPriority(group, priority); - - ImGuiUtil.HoverTooltip("Group Priority"); - - DrawGroupCombo(group, groupIdx); - ImGui.SameLine(); - - var tt = groupIdx == 0 ? "Can not move this group further upwards." : $"Move this group up to group {groupIdx}."; - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.ArrowUp.ToIconString(), UiHelpers.IconButtonSize, - tt, groupIdx == 0, true)) - _delayedActions.Enqueue(() => _modManager.OptionEditor.MoveModGroup(group, groupIdx - 1)); - - ImGui.SameLine(); - tt = groupIdx == _mod.Groups.Count - 1 - ? "Can not move this group further downwards." - : $"Move this group down to group {groupIdx + 2}."; - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.ArrowDown.ToIconString(), UiHelpers.IconButtonSize, - tt, groupIdx == _mod.Groups.Count - 1, true)) - _delayedActions.Enqueue(() => _modManager.OptionEditor.MoveModGroup(group, groupIdx + 1)); - - ImGui.SameLine(); - - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Edit.ToIconString(), UiHelpers.IconButtonSize, - "Edit group description.", false, true)) - _delayedActions.Enqueue(() => DescriptionEdit.OpenPopup(_mod, groupIdx)); - - ImGui.SameLine(); - var fileName = filenames.OptionGroupFile(_mod, groupIdx, config.ReplaceNonAsciiOnImport); - var fileExists = File.Exists(fileName); - tt = fileExists - ? $"Open the {group.Name} json file in the text editor of your choice." - : $"The {group.Name} json file does not exist."; - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.FileExport.ToIconString(), UiHelpers.IconButtonSize, tt, !fileExists, true)) - Process.Start(new ProcessStartInfo(fileName) { UseShellExecute = true }); - - UiHelpers.DefaultLineSpace(); - - OptionTable.Draw(this, groupIdx); - } - - /// Draw the table displaying all options and the add new option line. - private static class OptionTable - { - private const string DragDropLabel = "##DragOption"; - - private static int _newOptionNameIdx = -1; - private static string _newOptionName = string.Empty; - private static IModGroup? _dragDropGroup; - private static IModOption? _dragDropOption; - - public static void Reset() - { - _newOptionNameIdx = -1; - _newOptionName = string.Empty; - _dragDropGroup = null; - _dragDropOption = null; - } - - public static void Draw(ModPanelEditTab panel, int groupIdx) - { - using var table = ImRaii.Table(string.Empty, 6, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - var maxWidth = ImGui.CalcTextSize("Option #88.").X; - ImGui.TableSetupColumn("idx", ImGuiTableColumnFlags.WidthFixed, maxWidth); - ImGui.TableSetupColumn("default", ImGuiTableColumnFlags.WidthFixed, ImGui.GetFrameHeight()); - ImGui.TableSetupColumn("name", ImGuiTableColumnFlags.WidthFixed, - UiHelpers.InputTextWidth.X - maxWidth - 12 * UiHelpers.Scale - ImGui.GetFrameHeight() - UiHelpers.IconButtonSize.X); - ImGui.TableSetupColumn("description", ImGuiTableColumnFlags.WidthFixed, UiHelpers.IconButtonSize.X); - ImGui.TableSetupColumn("delete", ImGuiTableColumnFlags.WidthFixed, UiHelpers.IconButtonSize.X); - ImGui.TableSetupColumn("priority", ImGuiTableColumnFlags.WidthFixed, 50 * UiHelpers.Scale); - - switch (panel._mod.Groups[groupIdx]) - { - case SingleModGroup single: - for (var optionIdx = 0; optionIdx < single.OptionData.Count; ++optionIdx) - EditOption(panel, single, groupIdx, optionIdx); - break; - case MultiModGroup multi: - for (var optionIdx = 0; optionIdx < multi.OptionData.Count; ++optionIdx) - EditOption(panel, multi, groupIdx, optionIdx); - break; - } - - DrawNewOption(panel, groupIdx, UiHelpers.IconButtonSize); - } - - /// Draw a line for a single option. - private static void EditOption(ModPanelEditTab panel, IModGroup group, int groupIdx, int optionIdx) - { - var option = group.Options[optionIdx]; - using var id = ImRaii.PushId(optionIdx); - ImGui.TableNextColumn(); - ImGui.AlignTextToFramePadding(); - ImGui.Selectable($"Option #{optionIdx + 1}"); - Source(option); - Target(panel, group, optionIdx); - - ImGui.TableNextColumn(); - - - if (group.Type == GroupType.Single) - { - if (ImGui.RadioButton("##default", group.DefaultSettings.AsIndex == optionIdx)) - panel._modManager.OptionEditor.ChangeModGroupDefaultOption(group, Setting.Single(optionIdx)); - - ImGuiUtil.HoverTooltip($"Set {option.Name} as the default choice for this group."); - } - else - { - var isDefaultOption = group.DefaultSettings.HasFlag(optionIdx); - if (ImGui.Checkbox("##default", ref isDefaultOption)) - panel._modManager.OptionEditor.ChangeModGroupDefaultOption(group, group.DefaultSettings.SetBit(optionIdx, isDefaultOption)); - - ImGuiUtil.HoverTooltip($"{(isDefaultOption ? "Disable" : "Enable")} {option.Name} per default in this group."); - } - - ImGui.TableNextColumn(); - if (Input.Text("##Name", groupIdx, optionIdx, option.Name, out var newOptionName, 256, -1)) - panel._modManager.OptionEditor.RenameOption(option, newOptionName); - - ImGui.TableNextColumn(); - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Edit.ToIconString(), UiHelpers.IconButtonSize, "Edit option description.", - false, true)) - panel._delayedActions.Enqueue(() => DescriptionEdit.OpenPopup(panel._mod, groupIdx, optionIdx)); - - ImGui.TableNextColumn(); - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Trash.ToIconString(), UiHelpers.IconButtonSize, - "Delete this option.\nHold Control while clicking to delete.", !ImGui.GetIO().KeyCtrl, true)) - panel._delayedActions.Enqueue(() => panel._modManager.OptionEditor.DeleteOption(option)); - - ImGui.TableNextColumn(); - if (option is not MultiSubMod multi) - return; - - if (Input.Priority("##Priority", groupIdx, optionIdx, multi.Priority, out var priority, - 50 * UiHelpers.Scale)) - panel._modManager.OptionEditor.MultiEditor.ChangeOptionPriority(multi, priority); - - ImGuiUtil.HoverTooltip("Option priority."); - } - - /// Draw the line to add a new option. - private static void DrawNewOption(ModPanelEditTab panel, int groupIdx, Vector2 iconButtonSize) - { - var mod = panel._mod; - var group = mod.Groups[groupIdx]; - var count = group switch - { - SingleModGroup single => single.OptionData.Count, - MultiModGroup multi => multi.OptionData.Count, - _ => throw new Exception($"Dragging options to an option group of type {group.GetType()} is not supported."), - }; - ImGui.TableNextColumn(); - ImGui.AlignTextToFramePadding(); - ImGui.Selectable($"Option #{count + 1}"); - Target(panel, group, count); - ImGui.TableNextColumn(); - ImGui.TableNextColumn(); - ImGui.SetNextItemWidth(-1); - var tmp = _newOptionNameIdx == groupIdx ? _newOptionName : string.Empty; - if (ImGui.InputTextWithHint("##newOption", "Add new option...", ref tmp, 256)) - { - _newOptionName = tmp; - _newOptionNameIdx = groupIdx; - } - - ImGui.TableNextColumn(); - var canAddGroup = mod.Groups[groupIdx].Type != GroupType.Multi || count < IModGroup.MaxMultiOptions; - var validName = _newOptionName.Length > 0 && _newOptionNameIdx == groupIdx; - var tt = canAddGroup - ? validName ? "Add a new option to this group." : "Please enter a name for the new option." - : $"Can not add more than {IModGroup.MaxMultiOptions} options to a multi group."; - if (!ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconButtonSize, - tt, !(canAddGroup && validName), true)) - return; - - panel._modManager.OptionEditor.AddOption(group, _newOptionName); - _newOptionName = string.Empty; - } - - // Handle drag and drop to move options inside a group or into another group. - private static void Source(IModOption option) - { - if (option.Group is not ITexToolsGroup) - return; - - using var source = ImRaii.DragDropSource(); - if (!source) - return; - - if (ImGui.SetDragDropPayload(DragDropLabel, IntPtr.Zero, 0)) - { - _dragDropGroup = option.Group; - _dragDropOption = option; - } - - ImGui.TextUnformatted($"Dragging option {option.Name} from group {option.Group.Name}..."); - } - - private static void Target(ModPanelEditTab panel, IModGroup group, int optionIdx) - { - if (group is not ITexToolsGroup) - return; - - using var target = ImRaii.DragDropTarget(); - if (!target.Success || !ImGuiUtil.IsDropping(DragDropLabel)) - return; - - if (_dragDropGroup != null && _dragDropOption != null) - { - if (_dragDropGroup == group) - { - var sourceOption = _dragDropOption; - panel._delayedActions.Enqueue( - () => panel._modManager.OptionEditor.MoveOption(sourceOption, optionIdx)); - } - else - { - // Move from one group to another by deleting, then adding, then moving the option. - var sourceOption = _dragDropOption; - panel._delayedActions.Enqueue(() => - { - panel._modManager.OptionEditor.DeleteOption(sourceOption); - if (panel._modManager.OptionEditor.AddOption(group, sourceOption) is { } newOption) - panel._modManager.OptionEditor.MoveOption(newOption, optionIdx); - }); - } - } - - _dragDropGroup = null; - _dragDropOption = null; - } - } - - /// Draw a combo to select single or multi group and switch between them. - private void DrawGroupCombo(IModGroup group, int groupIdx) - { - ImGui.SetNextItemWidth(UiHelpers.InputTextWidth.X - 2 * UiHelpers.IconButtonSize.X - 2 * ImGui.GetStyle().ItemSpacing.X); - using var combo = ImRaii.Combo("##GroupType", GroupTypeName(group.Type)); - if (!combo) - return; - - if (ImGui.Selectable(GroupTypeName(GroupType.Single), group.Type == GroupType.Single) && group is MultiModGroup m) - _modManager.OptionEditor.MultiEditor.ChangeToSingle(m); - - var canSwitchToMulti = group.Options.Count <= IModGroup.MaxMultiOptions; - using var style = ImRaii.PushStyle(ImGuiStyleVar.Alpha, 0.5f, !canSwitchToMulti); - if (ImGui.Selectable(GroupTypeName(GroupType.Multi), group.Type == GroupType.Multi) && canSwitchToMulti && group is SingleModGroup s) - _modManager.OptionEditor.SingleEditor.ChangeToMulti(s); - - style.Pop(); - if (!canSwitchToMulti) - ImGuiUtil.HoverTooltip($"Can not convert group to multi group since it has more than {IModGroup.MaxMultiOptions} options."); - return; - - static string GroupTypeName(GroupType type) - => type switch - { - GroupType.Single => "Single Group", - GroupType.Multi => "Multi Group", - _ => "Unknown", - }; - } - /// Handles input text and integers in separate fields without buffers for every single one. private static class Input { @@ -705,6 +316,7 @@ public class ModPanelEditTab( { var tmp = field == _currentField && option == _optionIndex ? _currentEdit ?? oldValue : oldValue; ImGui.SetNextItemWidth(width); + if (ImGui.InputText(label, ref tmp, maxLength)) { _currentEdit = tmp; From df6eb3fdd2f4afac8b8b5e911b137ce0b9d54280 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 16 May 2024 18:30:40 +0200 Subject: [PATCH 155/433] Add some early support for IMC groups. --- OtterGui | 2 +- Penumbra/Collections/Cache/ImcCache.cs | 4 +- .../Import/TexToolsMeta.Deserialization.cs | 2 +- .../Meta/Manipulations/ImcManipulation.cs | 4 +- .../Meta/Manipulations/MetaManipulation.cs | 4 +- Penumbra/Mods/Groups/ImcModGroup.cs | 43 ++++ .../Manager/OptionEditor/ImcModGroupEditor.cs | 33 ++- .../Manager/OptionEditor/ModGroupEditor.cs | 2 +- Penumbra/Mods/ModCreator.cs | 1 + Penumbra/Mods/SubMods/ImcSubMod.cs | 7 + Penumbra/UI/AdvancedWindow/ItemSwapTab.cs | 4 +- .../UI/AdvancedWindow/ModEditWindow.Meta.cs | 80 +----- Penumbra/UI/Classes/Combos.cs | 40 ++- Penumbra/UI/ModsTab/ModGroupEditDrawer.cs | 238 +++++++++++++++++- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 42 +--- 15 files changed, 360 insertions(+), 146 deletions(-) diff --git a/OtterGui b/OtterGui index 866389b3..5028fba7 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 866389b3988d9c4926a786f6c78ac9d5265591ac +Subproject commit 5028fba767ca8febd75a1a5ebc312bd354efc81b diff --git a/Penumbra/Collections/Cache/ImcCache.cs b/Penumbra/Collections/Cache/ImcCache.cs index bc928360..843fe195 100644 --- a/Penumbra/Collections/Cache/ImcCache.cs +++ b/Penumbra/Collections/Cache/ImcCache.cs @@ -36,7 +36,7 @@ public readonly struct ImcCache : IDisposable public bool ApplyMod(MetaFileManager manager, ModCollection collection, ImcManipulation manip) { - if (!manip.Validate()) + if (!manip.Validate(true)) return false; var idx = _imcManipulations.FindIndex(p => p.Item1.Equals(manip)); @@ -77,7 +77,7 @@ public readonly struct ImcCache : IDisposable public bool RevertMod(MetaFileManager manager, ModCollection collection, ImcManipulation m) { - if (!m.Validate()) + if (!m.Validate(false)) return false; var idx = _imcManipulations.FindIndex(p => p.Item1.Equals(m)); diff --git a/Penumbra/Import/TexToolsMeta.Deserialization.cs b/Penumbra/Import/TexToolsMeta.Deserialization.cs index 64eff8ba..325c9143 100644 --- a/Penumbra/Import/TexToolsMeta.Deserialization.cs +++ b/Penumbra/Import/TexToolsMeta.Deserialization.cs @@ -120,7 +120,7 @@ public partial class TexToolsMeta { var imc = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, manip.SecondaryId, i, manip.EquipSlot, value); - if (imc.Validate()) + if (imc.Validate(true)) MetaManipulations.Add(imc); } diff --git a/Penumbra/Meta/Manipulations/ImcManipulation.cs b/Penumbra/Meta/Manipulations/ImcManipulation.cs index a1c4b5bf..45295990 100644 --- a/Penumbra/Meta/Manipulations/ImcManipulation.cs +++ b/Penumbra/Meta/Manipulations/ImcManipulation.cs @@ -146,7 +146,7 @@ public readonly struct ImcManipulation : IMetaManipulation public bool Apply(ImcFile file) => file.SetEntry(ImcFile.PartIndex(EquipSlot), Variant.Id, Entry); - public bool Validate() + public bool Validate(bool withMaterial) { switch (ObjectType) { @@ -178,7 +178,7 @@ public readonly struct ImcManipulation : IMetaManipulation break; } - if (Entry.MaterialId == 0) + if (withMaterial && Entry.MaterialId == 0) return false; return true; diff --git a/Penumbra/Meta/Manipulations/MetaManipulation.cs b/Penumbra/Meta/Manipulations/MetaManipulation.cs index e057d1a4..ed184d52 100644 --- a/Penumbra/Meta/Manipulations/MetaManipulation.cs +++ b/Penumbra/Meta/Manipulations/MetaManipulation.cs @@ -98,7 +98,7 @@ public readonly struct MetaManipulation : IEquatable, ICompara return; case ImcManipulation m: Imc = m; - ManipulationType = m.Validate() ? Type.Imc : Type.Unknown; + ManipulationType = m.Validate(true) ? Type.Imc : Type.Unknown; return; } } @@ -108,7 +108,7 @@ public readonly struct MetaManipulation : IEquatable, ICompara { return ManipulationType switch { - Type.Imc => Imc.Validate(), + Type.Imc => Imc.Validate(true), Type.Eqdp => Eqdp.Validate(), Type.Eqp => Eqp.Validate(), Type.Est => Est.Validate(), diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs index e58d855a..21d8abe0 100644 --- a/Penumbra/Mods/Groups/ImcModGroup.cs +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -1,4 +1,7 @@ +using Dalamud.Interface.Internal.Notifications; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using OtterGui.Classes; using Penumbra.Api.Enums; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; @@ -157,4 +160,44 @@ public class ImcModGroup(Mod mod) : IModGroup public (int Redirections, int Swaps, int Manips) GetCounts() => (0, 0, 1); + + public static ImcModGroup? Load(Mod mod, JObject json) + { + var options = json["Options"]; + var ret = new ImcModGroup(mod) + { + Name = json[nameof(Name)]?.ToObject() ?? string.Empty, + Description = json[nameof(Description)]?.ToObject() ?? string.Empty, + Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, + DefaultSettings = json[nameof(DefaultSettings)]?.ToObject() ?? Setting.Zero, + ObjectType = json[nameof(ObjectType)]?.ToObject() ?? ObjectType.Unknown, + BodySlot = json[nameof(BodySlot)]?.ToObject() ?? BodySlot.Unknown, + EquipSlot = json[nameof(EquipSlot)]?.ToObject() ?? EquipSlot.Unknown, + PrimaryId = new PrimaryId(json[nameof(PrimaryId)]?.ToObject() ?? 0), + SecondaryId = new SecondaryId(json[nameof(SecondaryId)]?.ToObject() ?? 0), + Variant = new Variant(json[nameof(Variant)]?.ToObject() ?? 0), + CanBeDisabled = json[nameof(CanBeDisabled)]?.ToObject() ?? false, + DefaultEntry = json[nameof(DefaultEntry)]?.ToObject() ?? new ImcEntry(), + }; + if (ret.Name.Length == 0) + return null; + + if (options != null) + foreach (var child in options.Children()) + { + var subMod = new ImcSubMod(ret, child); + ret.OptionData.Add(subMod); + } + + if (!new ImcManipulation(ret.ObjectType, ret.BodySlot, ret.PrimaryId, ret.SecondaryId.Id, ret.Variant.Id, ret.EquipSlot, + ret.DefaultEntry).Validate(true)) + { + Penumbra.Messager.NotificationMessage($"Could not add IMC group because the associated IMC Entry is invalid.", + NotificationType.Warning); + return null; + } + + ret.DefaultSettings = ret.FixSetting(ret.DefaultSettings); + return ret; + } } diff --git a/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs index 1194f961..f71547ba 100644 --- a/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs @@ -1,6 +1,7 @@ using OtterGui.Classes; using OtterGui.Filesystem; using OtterGui.Services; +using Penumbra.Meta.Manipulations; using Penumbra.Mods.Groups; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; @@ -11,13 +12,43 @@ namespace Penumbra.Mods.Manager.OptionEditor; public sealed class ImcModGroupEditor(CommunicatorService communicator, SaveService saveService, Configuration config) : ModOptionEditor(communicator, saveService, config), IService { + /// Add a new, empty imc group with the given manipulation data. + public ImcModGroup? AddModGroup(Mod mod, string newName, ImcManipulation manip, SaveType saveType = SaveType.ImmediateSync) + { + if (!ModGroupEditor.VerifyFileName(mod, null, newName, true)) + return null; + + var maxPriority = mod.Groups.Count == 0 ? ModPriority.Default : mod.Groups.Max(o => o.Priority) + 1; + var group = CreateGroup(mod, newName, manip, maxPriority); + mod.Groups.Add(group); + SaveService.Save(saveType, new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupAdded, mod, group, null, null, -1); + return group; + } + protected override ImcModGroup CreateGroup(Mod mod, string newName, ModPriority priority, SaveType saveType = SaveType.ImmediateSync) => new(mod) { - Name = newName, + Name = newName, Priority = priority, }; + + private static ImcModGroup CreateGroup(Mod mod, string newName, ImcManipulation manip, ModPriority priority, + SaveType saveType = SaveType.ImmediateSync) + => new(mod) + { + Name = newName, + Priority = priority, + ObjectType = manip.ObjectType, + EquipSlot = manip.EquipSlot, + BodySlot = manip.BodySlot, + PrimaryId = manip.PrimaryId, + SecondaryId = manip.SecondaryId.Id, + Variant = manip.Variant, + DefaultEntry = manip.Entry, + }; + protected override ImcSubMod? CloneOption(ImcModGroup group, IModOption option) => null; diff --git a/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs index b2b48ac0..3c00dcc1 100644 --- a/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs @@ -246,7 +246,7 @@ public class ModGroupEditor( { GroupType.Single => SingleEditor.AddModGroup(mod, newName, saveType), GroupType.Multi => MultiEditor.AddModGroup(mod, newName, saveType), - GroupType.Imc => ImcEditor.AddModGroup(mod, newName, saveType), + GroupType.Imc => ImcEditor.AddModGroup(mod, newName, default, saveType), _ => null, }; diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index 47261c6d..ed4245c4 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -434,6 +434,7 @@ public partial class ModCreator( { case GroupType.Multi: return MultiModGroup.Load(mod, json); case GroupType.Single: return SingleModGroup.Load(mod, json); + case GroupType.Imc: return ImcModGroup.Load(mod, json); } } catch (Exception e) diff --git a/Penumbra/Mods/SubMods/ImcSubMod.cs b/Penumbra/Mods/SubMods/ImcSubMod.cs index 167c8a6c..fca817aa 100644 --- a/Penumbra/Mods/SubMods/ImcSubMod.cs +++ b/Penumbra/Mods/SubMods/ImcSubMod.cs @@ -1,3 +1,4 @@ +using Newtonsoft.Json.Linq; using Penumbra.Mods.Groups; namespace Penumbra.Mods.SubMods; @@ -6,6 +7,12 @@ public class ImcSubMod(ImcModGroup group) : IModOption { public readonly ImcModGroup Group = group; + public ImcSubMod(ImcModGroup group, JToken json) + : this(group) + { + SubMod.LoadOptionData(json, this); + } + public Mod Mod => Group.Mod; diff --git a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs index 4db0df47..6010cdaf 100644 --- a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs +++ b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs @@ -639,11 +639,11 @@ public class ItemSwapTab : IDisposable, ITab ImGui.TextUnformatted(text); ImGui.TableNextColumn(); - _dirty |= Combos.Gender("##Gender", InputWidth, _currentGender, out _currentGender); + _dirty |= Combos.Gender("##Gender", _currentGender, out _currentGender, InputWidth); if (drawRace == 1) { ImGui.SameLine(); - _dirty |= Combos.Race("##Race", InputWidth, _currentRace, out _currentRace); + _dirty |= Combos.Race("##Race", _currentRace, out _currentRace, InputWidth); } else if (drawRace == 2) { diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs index 743310ea..55125375 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs @@ -10,6 +10,7 @@ using Penumbra.Meta.Files; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Editor; using Penumbra.UI.Classes; +using Penumbra.UI.ModsTab; namespace Penumbra.UI.AdvancedWindow; @@ -145,7 +146,7 @@ public partial class ModEditWindow ImGuiUtil.HoverTooltip(ModelSetIdTooltip); ImGui.TableNextColumn(); - if (Combos.EqpEquipSlot("##eqpSlot", 100, _new.Slot, out var slot)) + if (Combos.EqpEquipSlot("##eqpSlot", _new.Slot, out var slot)) _new = new EqpManipulation(ExpandedEqpFile.GetDefault(metaFileManager, setId), slot, _new.SetId); ImGuiUtil.HoverTooltip(EquipSlotTooltip); @@ -351,90 +352,31 @@ public partial class ModEditWindow // Identifier ImGui.TableNextColumn(); - if (Combos.ImcType("##imcType", _new.ObjectType, out var type)) - { - var equipSlot = type switch - { - ObjectType.Equipment => _new.EquipSlot.IsEquipment() ? _new.EquipSlot : EquipSlot.Head, - ObjectType.DemiHuman => _new.EquipSlot.IsEquipment() ? _new.EquipSlot : EquipSlot.Head, - ObjectType.Accessory => _new.EquipSlot.IsAccessory() ? _new.EquipSlot : EquipSlot.Ears, - _ => EquipSlot.Unknown, - }; - _new = new ImcManipulation(type, _new.BodySlot, _new.PrimaryId, _new.SecondaryId == 0 ? (ushort)1 : _new.SecondaryId, - _new.Variant.Id, equipSlot, _new.Entry); - } - - ImGuiUtil.HoverTooltip(ObjectTypeTooltip); + var change = MetaManipulationDrawer.DrawObjectType(ref _new); ImGui.TableNextColumn(); - if (IdInput("##imcId", IdWidth, _new.PrimaryId.Id, out var setId, 0, ushort.MaxValue, _new.PrimaryId <= 1)) - _new = new ImcManipulation(_new.ObjectType, _new.BodySlot, setId, _new.SecondaryId, _new.Variant.Id, _new.EquipSlot, _new.Entry) - .Copy(GetDefault(metaFileManager, _new) - ?? new ImcEntry()); - - ImGuiUtil.HoverTooltip(PrimaryIdTooltip); - + change |= MetaManipulationDrawer.DrawPrimaryId(ref _new); using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, new Vector2(3 * UiHelpers.Scale, ImGui.GetStyle().ItemSpacing.Y)); ImGui.TableNextColumn(); // Equipment and accessories are slightly different imcs than other types. - if (_new.ObjectType is ObjectType.Equipment) - { - if (Combos.EqpEquipSlot("##imcSlot", 100, _new.EquipSlot, out var slot)) - _new = new ImcManipulation(_new.ObjectType, _new.BodySlot, _new.PrimaryId, _new.SecondaryId, _new.Variant.Id, slot, - _new.Entry) - .Copy(GetDefault(metaFileManager, _new) - ?? new ImcEntry()); - - ImGuiUtil.HoverTooltip(EquipSlotTooltip); - } - else if (_new.ObjectType is ObjectType.Accessory) - { - if (Combos.AccessorySlot("##imcSlot", _new.EquipSlot, out var slot)) - _new = new ImcManipulation(_new.ObjectType, _new.BodySlot, _new.PrimaryId, _new.SecondaryId, _new.Variant.Id, slot, - _new.Entry) - .Copy(GetDefault(metaFileManager, _new) - ?? new ImcEntry()); - - ImGuiUtil.HoverTooltip(EquipSlotTooltip); - } + if (_new.ObjectType is ObjectType.Equipment or ObjectType.Accessory) + change |= MetaManipulationDrawer.DrawSlot(ref _new); else - { - if (IdInput("##imcId2", 100 * UiHelpers.Scale, _new.SecondaryId.Id, out var setId2, 0, ushort.MaxValue, false)) - _new = new ImcManipulation(_new.ObjectType, _new.BodySlot, _new.PrimaryId, setId2, _new.Variant.Id, _new.EquipSlot, - _new.Entry) - .Copy(GetDefault(metaFileManager, _new) - ?? new ImcEntry()); - - ImGuiUtil.HoverTooltip(SecondaryIdTooltip); - } + change |= MetaManipulationDrawer.DrawSecondaryId(ref _new); ImGui.TableNextColumn(); - if (IdInput("##imcVariant", SmallIdWidth, _new.Variant.Id, out var variant, 0, byte.MaxValue, false)) - _new = new ImcManipulation(_new.ObjectType, _new.BodySlot, _new.PrimaryId, _new.SecondaryId, variant, _new.EquipSlot, - _new.Entry).Copy(GetDefault(metaFileManager, _new) - ?? new ImcEntry()); - - ImGuiUtil.HoverTooltip(VariantIdTooltip); + change |= MetaManipulationDrawer.DrawVariant(ref _new); ImGui.TableNextColumn(); if (_new.ObjectType is ObjectType.DemiHuman) - { - if (Combos.EqpEquipSlot("##imcSlot", 70, _new.EquipSlot, out var slot)) - _new = new ImcManipulation(_new.ObjectType, _new.BodySlot, _new.PrimaryId, _new.SecondaryId, _new.Variant.Id, slot, - _new.Entry) - .Copy(GetDefault(metaFileManager, _new) - ?? new ImcEntry()); - - ImGuiUtil.HoverTooltip(EquipSlotTooltip); - } + change |= MetaManipulationDrawer.DrawSlot(ref _new, 70); else - { ImGui.Dummy(new Vector2(70 * UiHelpers.Scale, 0)); - } - + if (change) + _new = _new.Copy(GetDefault(metaFileManager, _new) ?? new ImcEntry()); // Values using var disabled = ImRaii.Disabled(); ImGui.TableNextColumn(); diff --git a/Penumbra/UI/Classes/Combos.cs b/Penumbra/UI/Classes/Combos.cs index 2cba7cf5..253bf0e0 100644 --- a/Penumbra/UI/Classes/Combos.cs +++ b/Penumbra/UI/Classes/Combos.cs @@ -8,41 +8,35 @@ namespace Penumbra.UI.Classes; public static class Combos { // Different combos to use with enums. - public static bool Race(string label, ModelRace current, out ModelRace race) - => Race(label, 100, current, out race); - - public static bool Race(string label, float unscaledWidth, ModelRace current, out ModelRace race) + public static bool Race(string label, ModelRace current, out ModelRace race, float unscaledWidth = 100) => ImGuiUtil.GenericEnumCombo(label, unscaledWidth * UiHelpers.Scale, current, out race, RaceEnumExtensions.ToName, 1); - public static bool Gender(string label, Gender current, out Gender gender) - => Gender(label, 120, current, out gender); + public static bool Gender(string label, Gender current, out Gender gender, float unscaledWidth = 120) + => ImGuiUtil.GenericEnumCombo(label, unscaledWidth, current, out gender, RaceEnumExtensions.ToName, 1); - public static bool Gender(string label, float unscaledWidth, Gender current, out Gender gender) - => ImGuiUtil.GenericEnumCombo(label, unscaledWidth * UiHelpers.Scale, current, out gender, RaceEnumExtensions.ToName, 1); - - public static bool EqdpEquipSlot(string label, EquipSlot current, out EquipSlot slot) - => ImGuiUtil.GenericEnumCombo(label, 100 * UiHelpers.Scale, current, out slot, EquipSlotExtensions.EqdpSlots, + public static bool EqdpEquipSlot(string label, EquipSlot current, out EquipSlot slot, float unscaledWidth = 100) + => ImGuiUtil.GenericEnumCombo(label, unscaledWidth * UiHelpers.Scale, current, out slot, EquipSlotExtensions.EqdpSlots, EquipSlotExtensions.ToName); - public static bool EqpEquipSlot(string label, float width, EquipSlot current, out EquipSlot slot) - => ImGuiUtil.GenericEnumCombo(label, width * UiHelpers.Scale, current, out slot, EquipSlotExtensions.EquipmentSlots, + public static bool EqpEquipSlot(string label, EquipSlot current, out EquipSlot slot, float unscaledWidth = 100) + => ImGuiUtil.GenericEnumCombo(label, unscaledWidth * UiHelpers.Scale, current, out slot, EquipSlotExtensions.EquipmentSlots, EquipSlotExtensions.ToName); - public static bool AccessorySlot(string label, EquipSlot current, out EquipSlot slot) - => ImGuiUtil.GenericEnumCombo(label, 100 * UiHelpers.Scale, current, out slot, EquipSlotExtensions.AccessorySlots, + public static bool AccessorySlot(string label, EquipSlot current, out EquipSlot slot, float unscaledWidth = 100) + => ImGuiUtil.GenericEnumCombo(label, unscaledWidth * UiHelpers.Scale, current, out slot, EquipSlotExtensions.AccessorySlots, EquipSlotExtensions.ToName); - public static bool SubRace(string label, SubRace current, out SubRace subRace) - => ImGuiUtil.GenericEnumCombo(label, 150 * UiHelpers.Scale, current, out subRace, RaceEnumExtensions.ToName, 1); + public static bool SubRace(string label, SubRace current, out SubRace subRace, float unscaledWidth = 150) + => ImGuiUtil.GenericEnumCombo(label, unscaledWidth * UiHelpers.Scale, current, out subRace, RaceEnumExtensions.ToName, 1); - public static bool RspAttribute(string label, RspAttribute current, out RspAttribute attribute) - => ImGuiUtil.GenericEnumCombo(label, 200 * UiHelpers.Scale, current, out attribute, + public static bool RspAttribute(string label, RspAttribute current, out RspAttribute attribute, float unscaledWidth = 200) + => ImGuiUtil.GenericEnumCombo(label, unscaledWidth * UiHelpers.Scale, current, out attribute, RspAttributeExtensions.ToFullString, 0, 1); - public static bool EstSlot(string label, EstManipulation.EstType current, out EstManipulation.EstType attribute) - => ImGuiUtil.GenericEnumCombo(label, 200 * UiHelpers.Scale, current, out attribute); + public static bool EstSlot(string label, EstManipulation.EstType current, out EstManipulation.EstType attribute, float unscaledWidth = 200) + => ImGuiUtil.GenericEnumCombo(label, unscaledWidth * UiHelpers.Scale, current, out attribute); - public static bool ImcType(string label, ObjectType current, out ObjectType type) - => ImGuiUtil.GenericEnumCombo(label, 110 * UiHelpers.Scale, current, out type, ObjectTypeExtensions.ValidImcTypes, + public static bool ImcType(string label, ObjectType current, out ObjectType type, float unscaledWidth = 110) + => ImGuiUtil.GenericEnumCombo(label, unscaledWidth * UiHelpers.Scale, current, out type, ObjectTypeExtensions.ValidImcTypes, ObjectTypeExtensions.ToName); } diff --git a/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs index 5652fa98..b7262f95 100644 --- a/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs @@ -1,12 +1,19 @@ using Dalamud.Interface; using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.Utility; using ImGuiNET; +using Lumina.Data.Files; using OtterGui; using OtterGui.Classes; using OtterGui.Raii; using OtterGui.Services; using OtterGui.Text; using OtterGui.Text.EndObjects; +using Penumbra.Api.Enums; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Meta; +using Penumbra.Meta.Manipulations; using Penumbra.Mods; using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; @@ -15,9 +22,236 @@ using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.UI.Classes; +using ImcFile = Penumbra.Meta.Files.ImcFile; namespace Penumbra.UI.ModsTab; +public static class MetaManipulationDrawer +{ + public static bool DrawObjectType(ref ImcManipulation manip, float width = 110) + { + var ret = Combos.ImcType("##imcType", manip.ObjectType, out var type, width); + ImUtf8.HoverTooltip("Object Type"u8); + + if (ret) + { + var equipSlot = type switch + { + ObjectType.Equipment => manip.EquipSlot.IsEquipment() ? manip.EquipSlot : EquipSlot.Head, + ObjectType.DemiHuman => manip.EquipSlot.IsEquipment() ? manip.EquipSlot : EquipSlot.Head, + ObjectType.Accessory => manip.EquipSlot.IsAccessory() ? manip.EquipSlot : EquipSlot.Ears, + _ => EquipSlot.Unknown, + }; + manip = new ImcManipulation(type, manip.BodySlot, manip.PrimaryId, manip.SecondaryId == 0 ? 1 : manip.SecondaryId, + manip.Variant.Id, equipSlot, manip.Entry); + } + + return ret; + } + + public static bool DrawPrimaryId(ref ImcManipulation manip, float unscaledWidth = 80) + { + var ret = IdInput("##imcPrimaryId"u8, unscaledWidth, manip.PrimaryId.Id, out var newId, 0, ushort.MaxValue, + manip.PrimaryId.Id <= 1); + ImUtf8.HoverTooltip("Primary ID - You can usually find this as the 'x####' part of an item path.\n"u8 + + "This should generally not be left <= 1 unless you explicitly want that."u8); + if (ret) + manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, newId, manip.SecondaryId, manip.Variant.Id, manip.EquipSlot, + manip.Entry); + return ret; + } + + public static bool DrawSecondaryId(ref ImcManipulation manip, float unscaledWidth = 100) + { + var ret = IdInput("##imcSecondaryId"u8, unscaledWidth, manip.SecondaryId.Id, out var newId, 0, ushort.MaxValue, false); + ImUtf8.HoverTooltip("Secondary ID"u8); + if (ret) + manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, newId, manip.Variant.Id, manip.EquipSlot, + manip.Entry); + return ret; + } + + public static bool DrawVariant(ref ImcManipulation manip, float unscaledWidth = 45) + { + var ret = IdInput("##imcVariant"u8, unscaledWidth, manip.Variant.Id, out var newId, 0, byte.MaxValue, false); + ImUtf8.HoverTooltip("Variant ID"u8); + if (ret) + manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, manip.SecondaryId, (byte)newId, manip.EquipSlot, + manip.Entry); + return ret; + } + + public static bool DrawSlot(ref ImcManipulation manip, float unscaledWidth = 100) + { + bool ret; + EquipSlot slot; + switch (manip.ObjectType) + { + case ObjectType.Equipment: + case ObjectType.DemiHuman: + ret = Combos.EqpEquipSlot("##slot", manip.EquipSlot, out slot, unscaledWidth); + break; + case ObjectType.Accessory: + ret = Combos.AccessorySlot("##slot", manip.EquipSlot, out slot, unscaledWidth); + break; + default: return false; + } + + ImUtf8.HoverTooltip("Equip Slot"u8); + if (ret) + manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, manip.SecondaryId, manip.Variant.Id, slot, + manip.Entry); + return ret; + } + + // A number input for ids with a optional max id of given width. + // Returns true if newId changed against currentId. + private static bool IdInput(ReadOnlySpan label, float unscaledWidth, ushort currentId, out ushort newId, int minId, int maxId, + bool border) + { + int tmp = currentId; + ImGui.SetNextItemWidth(unscaledWidth * ImUtf8.GlobalScale); + using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, UiHelpers.Scale, border); + using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.RegexWarningBorder, border); + if (ImUtf8.InputScalar(label, ref tmp)) + tmp = Math.Clamp(tmp, minId, maxId); + + newId = (ushort)tmp; + return newId != currentId; + } +} + +public class AddGroupDrawer : IUiService +{ + private string _groupName = string.Empty; + private bool _groupNameValid = false; + + private ImcManipulation _imcManip = new(EquipSlot.Head, 1, 1, new ImcEntry()); + private ImcEntry _defaultEntry; + private bool _imcFileExists; + private bool _entryExists; + private bool _entryInvalid; + private readonly MetaFileManager _metaManager; + private readonly ModManager _modManager; + + public AddGroupDrawer(MetaFileManager metaManager, ModManager modManager) + { + _metaManager = metaManager; + _modManager = modManager; + UpdateEntry(); + } + + public void Draw(Mod mod, float width) + { + DrawBasicGroups(mod, width); + DrawImcData(mod, width); + } + + private void UpdateEntry() + { + try + { + _defaultEntry = ImcFile.GetDefault(_metaManager, _imcManip.GamePath(), _imcManip.EquipSlot, _imcManip.Variant, + out _entryExists); + _imcFileExists = true; + } + catch (Exception) + { + _defaultEntry = new ImcEntry(); + _imcFileExists = false; + _entryExists = false; + } + + _imcManip = _imcManip.Copy(_entryExists ? _defaultEntry : new ImcEntry()); + _entryInvalid = !_imcManip.Validate(true); + } + + + private void DrawBasicGroups(Mod mod, float width) + { + ImGui.SetNextItemWidth(width); + if (ImUtf8.InputText("##name"u8, ref _groupName, "Enter New Name..."u8)) + _groupNameValid = ModGroupEditor.VerifyFileName(mod, null, _groupName, false); + + var buttonWidth = new Vector2((width - ImUtf8.ItemInnerSpacing.X) / 2, 0); + if (ImUtf8.ButtonEx("Add Single Group"u8, _groupNameValid + ? "Add a new single selection option group to this mod."u8 + : "Can not add a new group of this name."u8, + buttonWidth, !_groupNameValid)) + { + _modManager.OptionEditor.AddModGroup(mod, GroupType.Single, _groupName); + _groupName = string.Empty; + _groupNameValid = false; + } + + ImUtf8.SameLineInner(); + if (ImUtf8.ButtonEx("Add Multi Group"u8, _groupNameValid + ? "Add a new multi selection option group to this mod."u8 + : "Can not add a new group of this name."u8, + buttonWidth, !_groupNameValid)) + { + _modManager.OptionEditor.AddModGroup(mod, GroupType.Multi, _groupName); + _groupName = string.Empty; + _groupNameValid = false; + } + } + + private void DrawImcData(Mod mod, float width) + { + var halfWidth = (width - ImUtf8.ItemInnerSpacing.X) / 2 / ImUtf8.GlobalScale; + var change = MetaManipulationDrawer.DrawObjectType(ref _imcManip, halfWidth); + ImUtf8.SameLineInner(); + change |= MetaManipulationDrawer.DrawPrimaryId(ref _imcManip, halfWidth); + if (_imcManip.ObjectType is ObjectType.Weapon or ObjectType.Monster) + { + change |= MetaManipulationDrawer.DrawSecondaryId(ref _imcManip, halfWidth); + ImUtf8.SameLineInner(); + change |= MetaManipulationDrawer.DrawVariant(ref _imcManip, halfWidth); + } + else if (_imcManip.ObjectType is ObjectType.DemiHuman) + { + var quarterWidth = (halfWidth - ImUtf8.ItemInnerSpacing.X / ImUtf8.GlobalScale) / 2; + change |= MetaManipulationDrawer.DrawSecondaryId(ref _imcManip, halfWidth); + ImUtf8.SameLineInner(); + change |= MetaManipulationDrawer.DrawSlot(ref _imcManip, quarterWidth); + ImUtf8.SameLineInner(); + change |= MetaManipulationDrawer.DrawVariant(ref _imcManip, quarterWidth); + } + else + { + change |= MetaManipulationDrawer.DrawSlot(ref _imcManip, halfWidth); + ImUtf8.SameLineInner(); + change |= MetaManipulationDrawer.DrawVariant(ref _imcManip, halfWidth); + } + + if (change) + UpdateEntry(); + + var buttonWidth = new Vector2(halfWidth * ImUtf8.GlobalScale, 0); + + if (ImUtf8.ButtonEx("Add IMC Group"u8, !_groupNameValid + ? "Can not add a new group of this name."u8 + : _entryInvalid ? + "The associated IMC entry is invalid."u8 + : "Add a new multi selection option group to this mod."u8, + buttonWidth, !_groupNameValid || _entryInvalid)) + { + _modManager.OptionEditor.ImcEditor.AddModGroup(mod, _groupName, _imcManip); + _groupName = string.Empty; + _groupNameValid = false; + } + + if (_entryInvalid) + { + ImUtf8.SameLineInner(); + var text = _imcFileExists + ? "IMC Entry Does Not Exist" + : "IMC File Does Not Exist"; + ImGuiUtil.DrawTextButton(text, buttonWidth, Colors.PressEnterWarningBg); + } + } +} + public sealed class ModGroupEditDrawer( ModManager modManager, Configuration config, @@ -267,9 +501,7 @@ public sealed class ModGroupEditDrawer( } private void DrawImcGroup(ImcModGroup group) - { - // TODO - } + { } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void DrawOptionPosition(IModGroup group, IModOption option, int optionIdx) diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index a5db15b6..b7951c49 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -28,7 +28,8 @@ public class ModPanelEditTab( Configuration config, PredefinedTagManager predefinedTagManager, ModGroupEditDrawer groupEditDrawer, - DescriptionEditPopup descriptionPopup) + DescriptionEditPopup descriptionPopup, + AddGroupDrawer addGroupDrawer) : ITab { private readonly TagButtons _modTags = new(); @@ -75,7 +76,7 @@ public class ModPanelEditTab( selector.Selected!); UiHelpers.DefaultLineSpace(); - AddOptionGroup.Draw(filenames, modManager, _mod, config.ReplaceNonAsciiOnImport); + addGroupDrawer.Draw(_mod, UiHelpers.InputTextWidth.X); UiHelpers.DefaultLineSpace(); groupEditDrawer.Draw(_mod); @@ -84,7 +85,6 @@ public class ModPanelEditTab( public void Reset() { - AddOptionGroup.Reset(); MoveDirectory.Reset(); Input.Reset(); } @@ -202,42 +202,6 @@ public class ModPanelEditTab( Process.Start(new ProcessStartInfo(filenames.ModMetaPath(_mod)) { UseShellExecute = true }); } - /// Text input to add a new option group at the end of the current groups. - private static class AddOptionGroup - { - private static string _newGroupName = string.Empty; - - public static void Reset() - => _newGroupName = string.Empty; - - public static void Draw(FilenameService filenames, ModManager modManager, Mod mod, bool onlyAscii) - { - using var spacing = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, new Vector2(UiHelpers.ScaleX3)); - ImGui.SetNextItemWidth(UiHelpers.InputTextMinusButton3); - ImGui.InputTextWithHint("##newGroup", "Add new option group...", ref _newGroupName, 256); - ImGui.SameLine(); - var defaultFile = filenames.OptionGroupFile(mod, -1, onlyAscii); - var fileExists = File.Exists(defaultFile); - var tt = fileExists - ? "Open the default option json file in the text editor of your choice." - : "The default option json file does not exist."; - if (ImGuiUtil.DrawDisabledButton($"{FontAwesomeIcon.FileExport.ToIconString()}##defaultFile", UiHelpers.IconButtonSize, tt, - !fileExists, true)) - Process.Start(new ProcessStartInfo(defaultFile) { UseShellExecute = true }); - - ImGui.SameLine(); - - var nameValid = ModGroupEditor.VerifyFileName(mod, null, _newGroupName, false); - tt = nameValid ? "Add new option group to the mod." : "Can not add a group of this name."; - if (!ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), UiHelpers.IconButtonSize, - tt, !nameValid, true)) - return; - - modManager.OptionEditor.SingleEditor.AddModGroup(mod, _newGroupName); - Reset(); - } - } - /// A text input for the new directory name and a button to apply the move. private static class MoveDirectory { From bb56faa288be5bf200e0e44af93a649b382ef632 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 20 May 2024 18:28:40 +0200 Subject: [PATCH 156/433] Improvements. --- OtterGui | 2 +- Penumbra.GameData | 2 +- Penumbra/Mods/Groups/ImcModGroup.cs | 27 +- Penumbra/Mods/Manager/ModCacheManager.cs | 3 + .../Manager/OptionEditor/ImcAttributeCache.cs | 123 ++++++++ .../Manager/OptionEditor/ImcModGroupEditor.cs | 63 ++++ Penumbra/Mods/SubMods/ImcSubMod.cs | 7 +- Penumbra/UI/ModsTab/AddGroupDrawer.cs | 161 ++++++++++ Penumbra/UI/ModsTab/ModGroupEditDrawer.cs | 280 +++++++++--------- 9 files changed, 498 insertions(+), 170 deletions(-) create mode 100644 Penumbra/Mods/Manager/OptionEditor/ImcAttributeCache.cs create mode 100644 Penumbra/UI/ModsTab/AddGroupDrawer.cs diff --git a/OtterGui b/OtterGui index 5028fba7..462acb87 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 5028fba767ca8febd75a1a5ebc312bd354efc81b +Subproject commit 462acb87099650019996e4306d18cc70f76ca576 diff --git a/Penumbra.GameData b/Penumbra.GameData index 595ac572..5fa4d0e7 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 595ac5722c9c400bea36110503ed2ae7b02d1489 +Subproject commit 5fa4d0e7972423b73f8cf569bb2bfbeddd825c8a diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs index 21d8abe0..671d684f 100644 --- a/Penumbra/Mods/Groups/ImcModGroup.cs +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -14,8 +14,7 @@ namespace Penumbra.Mods.Groups; public class ImcModGroup(Mod mod) : IModGroup { - public const int DisabledIndex = 30; - public const int NumAttributes = 10; + public const int DisabledIndex = 60; public Mod Mod { get; } = mod; public string Name { get; set; } = "Option"; @@ -55,23 +54,20 @@ public class ImcModGroup(Mod mod) : IModGroup } } + public bool DefaultDisabled + => _canBeDisabled && DefaultSettings.HasFlag(DisabledIndex); + public IModOption? AddOption(string name, string description = "") { - uint fullMask = GetFullMask(); - var firstUnset = (byte)BitOperations.TrailingZeroCount(~fullMask); - // All attributes handled. - if (firstUnset >= NumAttributes) - return null; - var groupIdx = Mod.Groups.IndexOf(this); if (groupIdx < 0) return null; var subMod = new ImcSubMod(this) { - Name = name, - Description = description, - AttributeIndex = firstUnset, + Name = name, + Description = description, + AttributeMask = 0, }; OptionData.Add(subMod); return subMod; @@ -100,7 +96,7 @@ public class ImcModGroup(Mod mod) : IModGroup continue; var option = OptionData[i]; - mask |= option.Attribute; + mask |= option.AttributeMask; } return mask; @@ -109,11 +105,10 @@ public class ImcModGroup(Mod mod) : IModGroup private ushort GetFullMask() => GetCurrentMask(Setting.AllBits(63)); - private ImcManipulation GetManip(ushort mask) + public ImcManipulation GetManip(ushort mask) => new(ObjectType, BodySlot, PrimaryId, SecondaryId.Id, Variant.Id, EquipSlot, DefaultEntry with { AttributeMask = mask }); - public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) { if (CanBeDisabled && setting.HasFlag(DisabledIndex)) @@ -150,8 +145,8 @@ public class ImcModGroup(Mod mod) : IModGroup { jWriter.WriteStartObject(); SubMod.WriteModOption(jWriter, option); - jWriter.WritePropertyName(nameof(option.AttributeIndex)); - jWriter.WriteValue(option.AttributeIndex); + jWriter.WritePropertyName(nameof(option.AttributeMask)); + jWriter.WriteValue(option.AttributeMask); jWriter.WriteEndObject(); } diff --git a/Penumbra/Mods/Manager/ModCacheManager.cs b/Penumbra/Mods/Manager/ModCacheManager.cs index 59c88cf0..c6a723a0 100644 --- a/Penumbra/Mods/Manager/ModCacheManager.cs +++ b/Penumbra/Mods/Manager/ModCacheManager.cs @@ -202,6 +202,9 @@ public class ModCacheManager : IDisposable foreach (var manip in mod.AllDataContainers.SelectMany(m => m.Manipulations)) ComputeChangedItems(_identifier, changedItems, manip); + foreach(var imcGroup in mod.Groups.OfType()) + ComputeChangedItems(_identifier, changedItems, imcGroup.GetManip(0)); + mod.LowerChangedItemsString = string.Join("\0", mod.ChangedItems.Keys.Select(k => k.ToLowerInvariant())); } diff --git a/Penumbra/Mods/Manager/OptionEditor/ImcAttributeCache.cs b/Penumbra/Mods/Manager/OptionEditor/ImcAttributeCache.cs new file mode 100644 index 00000000..e1235c5b --- /dev/null +++ b/Penumbra/Mods/Manager/OptionEditor/ImcAttributeCache.cs @@ -0,0 +1,123 @@ +using OtterGui; +using Penumbra.GameData.Structs; +using Penumbra.Mods.Groups; +using Penumbra.Mods.SubMods; + +namespace Penumbra.Mods.Manager.OptionEditor; + +public unsafe ref struct ImcAttributeCache +{ + private fixed bool _canChange[ImcEntry.NumAttributes]; + private fixed byte _option[ImcEntry.NumAttributes]; + + /// Obtain the earliest unset flag, or 0 if none are unset. + public readonly ushort LowestUnsetMask; + + public ImcAttributeCache(ImcModGroup group) + { + for (var i = 0; i < ImcEntry.NumAttributes; ++i) + { + _canChange[i] = true; + _option[i] = byte.MaxValue; + + var flag = (ushort)(1 << i); + var set = (group.DefaultEntry.AttributeMask & flag) != 0; + if (set) + { + _canChange[i] = true; + _option[i] = byte.MaxValue - 1; + continue; + } + + foreach (var (option, idx) in group.OptionData.WithIndex()) + { + set = (option.AttributeMask & flag) != 0; + if (set) + { + _canChange[i] = option.AttributeMask != flag; + _option[i] = (byte)idx; + break; + } + } + + if (_option[i] == byte.MaxValue && LowestUnsetMask is 0) + LowestUnsetMask = flag; + } + } + + + /// Checks whether an attribute flag can be set by anything, i.e. if it might be the only flag for an option and thus could not be removed from that option. + public readonly bool CanChange(int idx) + => _canChange[idx]; + + /// Set a default attribute flag to a value if possible, remove it from its prior option if necessary, and return if anything changed. + public readonly bool Set(ImcModGroup group, int idx, bool value) + { + var flag = 1 << idx; + var oldMask = group.DefaultEntry.AttributeMask; + if (!value) + { + var newMask = (ushort)(oldMask & ~flag); + if (oldMask == newMask) + return false; + + group.DefaultEntry = group.DefaultEntry with { AttributeMask = newMask }; + return true; + } + + if (!_canChange[idx]) + return false; + + var mask = (ushort)(oldMask | flag); + if (oldMask == mask) + return false; + + group.DefaultEntry = group.DefaultEntry with { AttributeMask = mask }; + if (_option[idx] <= ImcEntry.NumAttributes) + { + var option = group.OptionData[_option[idx]]; + option.AttributeMask = (ushort)(option.AttributeMask & ~flag); + } + + return true; + } + + /// Set an attribute flag to a value if possible, remove it from its prior option or the default entry if necessary, and return if anything changed. + public readonly bool Set(ImcSubMod option, int idx, bool value) + { + if (!_canChange[idx]) + return false; + + var flag = 1 << idx; + var oldMask = option.AttributeMask; + if (!value) + { + var newMask = (ushort)(oldMask & ~flag); + if (oldMask == newMask) + return false; + + option.AttributeMask = newMask; + return true; + } + + var mask = (ushort)(oldMask | flag); + if (oldMask == mask) + return false; + + option.AttributeMask = mask; + if (_option[idx] <= ImcEntry.NumAttributes) + { + var oldOption = option.Group.OptionData[_option[idx]]; + oldOption.AttributeMask = (ushort)(oldOption.AttributeMask & ~flag); + } + else if (_option[idx] is byte.MaxValue - 1) + { + option.Group.DefaultEntry = option.Group.DefaultEntry with + { + AttributeMask = (ushort)(option.Group.DefaultEntry.AttributeMask & ~flag), + }; + } + + return true; + } +} diff --git a/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs index f71547ba..20021d29 100644 --- a/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs @@ -1,11 +1,13 @@ using OtterGui.Classes; using OtterGui.Filesystem; using OtterGui.Services; +using Penumbra.GameData.Structs; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Groups; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; +using static FFXIVClientStructs.FFXIV.Client.UI.Misc.ConfigModule; namespace Penumbra.Mods.Manager.OptionEditor; @@ -26,6 +28,67 @@ public sealed class ImcModGroupEditor(CommunicatorService communicator, SaveServ return group; } + public ImcSubMod? AddOption(ImcModGroup group, in ImcAttributeCache cache, string name, string description = "", + SaveType saveType = SaveType.Queue) + { + if (cache.LowestUnsetMask == 0) + return null; + + var subMod = new ImcSubMod(group) + { + Name = name, + Description = description, + AttributeMask = cache.LowestUnsetMask, + }; + group.OptionData.Add(subMod); + SaveService.Save(saveType, new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionAdded, group.Mod, group, subMod, null, -1); + return subMod; + } + + // Hide this method. + private new ImcSubMod? AddOption(ImcModGroup group, string name, SaveType saveType) + => null; + + public void ChangeDefaultAttribute(ImcModGroup group, in ImcAttributeCache cache, int idx, bool value, SaveType saveType = SaveType.Queue) + { + if (!cache.Set(group, idx, value)) + return; + + SaveService.Save(saveType, new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMetaChanged, group.Mod, group, null, null, -1); + } + + public void ChangeDefaultEntry(ImcModGroup group, in ImcEntry newEntry, SaveType saveType = SaveType.Queue) + { + var entry = newEntry with { AttributeMask = group.DefaultEntry.AttributeMask }; + if (entry.MaterialId == 0 || group.DefaultEntry.Equals(entry)) + return; + + group.DefaultEntry = entry; + SaveService.Save(saveType, new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMetaChanged, group.Mod, group, null, null, -1); + } + + public void ChangeOptionAttribute(ImcSubMod option, in ImcAttributeCache cache, int idx, bool value, SaveType saveType = SaveType.Queue) + { + if (!cache.Set(option, idx, value)) + return; + + SaveService.Save(saveType, new ModSaveGroup(option.Group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMetaChanged, option.Mod, option.Group, option, null, -1); + } + + public void ChangeCanBeDisabled(ImcModGroup group, bool canBeDisabled, SaveType saveType = SaveType.Queue) + { + if (group.CanBeDisabled == canBeDisabled) + return; + + group.CanBeDisabled = canBeDisabled; + SaveService.Save(saveType, new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMetaChanged, group.Mod, group, null, null, -1); + } + protected override ImcModGroup CreateGroup(Mod mod, string newName, ModPriority priority, SaveType saveType = SaveType.ImmediateSync) => new(mod) { diff --git a/Penumbra/Mods/SubMods/ImcSubMod.cs b/Penumbra/Mods/SubMods/ImcSubMod.cs index fca817aa..7f46bc95 100644 --- a/Penumbra/Mods/SubMods/ImcSubMod.cs +++ b/Penumbra/Mods/SubMods/ImcSubMod.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json.Linq; +using Penumbra.GameData.Structs; using Penumbra.Mods.Groups; namespace Penumbra.Mods.SubMods; @@ -11,15 +12,13 @@ public class ImcSubMod(ImcModGroup group) : IModOption : this(group) { SubMod.LoadOptionData(json, this); + AttributeMask = (ushort)((json[nameof(AttributeMask)]?.ToObject() ?? 0) & ImcEntry.AttributesMask); } public Mod Mod => Group.Mod; - public byte AttributeIndex; - - public ushort Attribute - => (ushort)(1 << AttributeIndex); + public ushort AttributeMask; Mod IModOption.Mod => Mod; diff --git a/Penumbra/UI/ModsTab/AddGroupDrawer.cs b/Penumbra/UI/ModsTab/AddGroupDrawer.cs new file mode 100644 index 00000000..79c1bf9d --- /dev/null +++ b/Penumbra/UI/ModsTab/AddGroupDrawer.cs @@ -0,0 +1,161 @@ +using ImGuiNET; +using OtterGui.Services; +using OtterGui.Text; +using Penumbra.Api.Enums; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Meta; +using Penumbra.Meta.Files; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods; +using Penumbra.Mods.Manager; +using Penumbra.Mods.Manager.OptionEditor; +using Penumbra.UI.Classes; + +namespace Penumbra.UI.ModsTab; + +public class AddGroupDrawer : IUiService +{ + private string _groupName = string.Empty; + private bool _groupNameValid = false; + + private ImcManipulation _imcManip = new(EquipSlot.Head, 1, 1, new ImcEntry()); + private ImcEntry _defaultEntry; + private bool _imcFileExists; + private bool _entryExists; + private bool _entryInvalid; + private readonly MetaFileManager _metaManager; + private readonly ModManager _modManager; + + public AddGroupDrawer(MetaFileManager metaManager, ModManager modManager) + { + _metaManager = metaManager; + _modManager = modManager; + UpdateEntry(); + } + + public void Draw(Mod mod, float width) + { + var buttonWidth = new Vector2((width - ImUtf8.ItemInnerSpacing.X) / 2, 0); + DrawBasicGroups(mod, width, buttonWidth); + DrawImcData(mod, buttonWidth); + } + + private void DrawBasicGroups(Mod mod, float width, Vector2 buttonWidth) + { + ImGui.SetNextItemWidth(width); + if (ImUtf8.InputText("##name"u8, ref _groupName, "Enter New Name..."u8)) + _groupNameValid = ModGroupEditor.VerifyFileName(mod, null, _groupName, false); + + DrawSingleGroupButton(mod, buttonWidth); + ImUtf8.SameLineInner(); + DrawMultiGroupButton(mod, buttonWidth); + } + + private void DrawSingleGroupButton(Mod mod, Vector2 width) + { + if (!ImUtf8.ButtonEx("Add Single Group"u8, _groupNameValid + ? "Add a new single selection option group to this mod."u8 + : "Can not add a new group of this name."u8, + width, !_groupNameValid)) + return; + + _modManager.OptionEditor.AddModGroup(mod, GroupType.Single, _groupName); + _groupName = string.Empty; + _groupNameValid = false; + } + + private void DrawMultiGroupButton(Mod mod, Vector2 width) + { + if (!ImUtf8.ButtonEx("Add Multi Group"u8, _groupNameValid + ? "Add a new multi selection option group to this mod."u8 + : "Can not add a new group of this name."u8, + width, !_groupNameValid)) + return; + + _modManager.OptionEditor.AddModGroup(mod, GroupType.Multi, _groupName); + _groupName = string.Empty; + _groupNameValid = false; + } + + private void DrawImcInput(float width) + { + var change = MetaManipulationDrawer.DrawObjectType(ref _imcManip, width); + ImUtf8.SameLineInner(); + change |= MetaManipulationDrawer.DrawPrimaryId(ref _imcManip, width); + if (_imcManip.ObjectType is ObjectType.Weapon or ObjectType.Monster) + { + change |= MetaManipulationDrawer.DrawSecondaryId(ref _imcManip, width); + ImUtf8.SameLineInner(); + change |= MetaManipulationDrawer.DrawVariant(ref _imcManip, width); + } + else if (_imcManip.ObjectType is ObjectType.DemiHuman) + { + var quarterWidth = (width - ImUtf8.ItemInnerSpacing.X / ImUtf8.GlobalScale) / 2; + change |= MetaManipulationDrawer.DrawSecondaryId(ref _imcManip, width); + ImUtf8.SameLineInner(); + change |= MetaManipulationDrawer.DrawSlot(ref _imcManip, quarterWidth); + ImUtf8.SameLineInner(); + change |= MetaManipulationDrawer.DrawVariant(ref _imcManip, quarterWidth); + } + else + { + change |= MetaManipulationDrawer.DrawSlot(ref _imcManip, width); + ImUtf8.SameLineInner(); + change |= MetaManipulationDrawer.DrawVariant(ref _imcManip, width); + } + + if (change) + UpdateEntry(); + } + + private void DrawImcData(Mod mod, Vector2 width) + { + var halfWidth = width.X / ImUtf8.GlobalScale; + DrawImcInput(halfWidth); + DrawImcButton(mod, width); + } + + private void DrawImcButton(Mod mod, Vector2 width) + { + if (ImUtf8.ButtonEx("Add IMC Group"u8, !_groupNameValid + ? "Can not add a new group of this name."u8 + : _entryInvalid + ? "The associated IMC entry is invalid."u8 + : "Add a new multi selection option group to this mod."u8, + width, !_groupNameValid || _entryInvalid)) + { + _modManager.OptionEditor.ImcEditor.AddModGroup(mod, _groupName, _imcManip); + _groupName = string.Empty; + _groupNameValid = false; + } + + if (_entryInvalid) + { + ImUtf8.SameLineInner(); + var text = _imcFileExists + ? "IMC Entry Does Not Exist"u8 + : "IMC File Does Not Exist"u8; + ImUtf8.TextFramed(text, Colors.PressEnterWarningBg, width); + } + } + + private void UpdateEntry() + { + try + { + _defaultEntry = ImcFile.GetDefault(_metaManager, _imcManip.GamePath(), _imcManip.EquipSlot, _imcManip.Variant, + out _entryExists); + _imcFileExists = true; + } + catch (Exception) + { + _defaultEntry = new ImcEntry(); + _imcFileExists = false; + _entryExists = false; + } + + _imcManip = _imcManip.Copy(_entryExists ? _defaultEntry : new ImcEntry()); + _entryInvalid = !_imcManip.Validate(true); + } +} diff --git a/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs index b7262f95..a94c25ea 100644 --- a/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs @@ -1,15 +1,12 @@ using Dalamud.Interface; using Dalamud.Interface.Internal.Notifications; -using Dalamud.Interface.Utility; using ImGuiNET; -using Lumina.Data.Files; using OtterGui; using OtterGui.Classes; using OtterGui.Raii; using OtterGui.Services; using OtterGui.Text; using OtterGui.Text.EndObjects; -using Penumbra.Api.Enums; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Meta; @@ -22,7 +19,6 @@ using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.UI.Classes; -using ImcFile = Penumbra.Meta.Files.ImcFile; namespace Penumbra.UI.ModsTab; @@ -104,8 +100,10 @@ public static class MetaManipulationDrawer return ret; } - // A number input for ids with a optional max id of given width. - // Returns true if newId changed against currentId. + /// + /// A number input for ids with an optional max id of given width. + /// Returns true if newId changed against currentId. + /// private static bool IdInput(ReadOnlySpan label, float unscaledWidth, ushort currentId, out ushort newId, int minId, int maxId, bool border) { @@ -121,142 +119,12 @@ public static class MetaManipulationDrawer } } -public class AddGroupDrawer : IUiService -{ - private string _groupName = string.Empty; - private bool _groupNameValid = false; - - private ImcManipulation _imcManip = new(EquipSlot.Head, 1, 1, new ImcEntry()); - private ImcEntry _defaultEntry; - private bool _imcFileExists; - private bool _entryExists; - private bool _entryInvalid; - private readonly MetaFileManager _metaManager; - private readonly ModManager _modManager; - - public AddGroupDrawer(MetaFileManager metaManager, ModManager modManager) - { - _metaManager = metaManager; - _modManager = modManager; - UpdateEntry(); - } - - public void Draw(Mod mod, float width) - { - DrawBasicGroups(mod, width); - DrawImcData(mod, width); - } - - private void UpdateEntry() - { - try - { - _defaultEntry = ImcFile.GetDefault(_metaManager, _imcManip.GamePath(), _imcManip.EquipSlot, _imcManip.Variant, - out _entryExists); - _imcFileExists = true; - } - catch (Exception) - { - _defaultEntry = new ImcEntry(); - _imcFileExists = false; - _entryExists = false; - } - - _imcManip = _imcManip.Copy(_entryExists ? _defaultEntry : new ImcEntry()); - _entryInvalid = !_imcManip.Validate(true); - } - - - private void DrawBasicGroups(Mod mod, float width) - { - ImGui.SetNextItemWidth(width); - if (ImUtf8.InputText("##name"u8, ref _groupName, "Enter New Name..."u8)) - _groupNameValid = ModGroupEditor.VerifyFileName(mod, null, _groupName, false); - - var buttonWidth = new Vector2((width - ImUtf8.ItemInnerSpacing.X) / 2, 0); - if (ImUtf8.ButtonEx("Add Single Group"u8, _groupNameValid - ? "Add a new single selection option group to this mod."u8 - : "Can not add a new group of this name."u8, - buttonWidth, !_groupNameValid)) - { - _modManager.OptionEditor.AddModGroup(mod, GroupType.Single, _groupName); - _groupName = string.Empty; - _groupNameValid = false; - } - - ImUtf8.SameLineInner(); - if (ImUtf8.ButtonEx("Add Multi Group"u8, _groupNameValid - ? "Add a new multi selection option group to this mod."u8 - : "Can not add a new group of this name."u8, - buttonWidth, !_groupNameValid)) - { - _modManager.OptionEditor.AddModGroup(mod, GroupType.Multi, _groupName); - _groupName = string.Empty; - _groupNameValid = false; - } - } - - private void DrawImcData(Mod mod, float width) - { - var halfWidth = (width - ImUtf8.ItemInnerSpacing.X) / 2 / ImUtf8.GlobalScale; - var change = MetaManipulationDrawer.DrawObjectType(ref _imcManip, halfWidth); - ImUtf8.SameLineInner(); - change |= MetaManipulationDrawer.DrawPrimaryId(ref _imcManip, halfWidth); - if (_imcManip.ObjectType is ObjectType.Weapon or ObjectType.Monster) - { - change |= MetaManipulationDrawer.DrawSecondaryId(ref _imcManip, halfWidth); - ImUtf8.SameLineInner(); - change |= MetaManipulationDrawer.DrawVariant(ref _imcManip, halfWidth); - } - else if (_imcManip.ObjectType is ObjectType.DemiHuman) - { - var quarterWidth = (halfWidth - ImUtf8.ItemInnerSpacing.X / ImUtf8.GlobalScale) / 2; - change |= MetaManipulationDrawer.DrawSecondaryId(ref _imcManip, halfWidth); - ImUtf8.SameLineInner(); - change |= MetaManipulationDrawer.DrawSlot(ref _imcManip, quarterWidth); - ImUtf8.SameLineInner(); - change |= MetaManipulationDrawer.DrawVariant(ref _imcManip, quarterWidth); - } - else - { - change |= MetaManipulationDrawer.DrawSlot(ref _imcManip, halfWidth); - ImUtf8.SameLineInner(); - change |= MetaManipulationDrawer.DrawVariant(ref _imcManip, halfWidth); - } - - if (change) - UpdateEntry(); - - var buttonWidth = new Vector2(halfWidth * ImUtf8.GlobalScale, 0); - - if (ImUtf8.ButtonEx("Add IMC Group"u8, !_groupNameValid - ? "Can not add a new group of this name."u8 - : _entryInvalid ? - "The associated IMC entry is invalid."u8 - : "Add a new multi selection option group to this mod."u8, - buttonWidth, !_groupNameValid || _entryInvalid)) - { - _modManager.OptionEditor.ImcEditor.AddModGroup(mod, _groupName, _imcManip); - _groupName = string.Empty; - _groupNameValid = false; - } - - if (_entryInvalid) - { - ImUtf8.SameLineInner(); - var text = _imcFileExists - ? "IMC Entry Does Not Exist" - : "IMC File Does Not Exist"; - ImGuiUtil.DrawTextButton(text, buttonWidth, Colors.PressEnterWarningBg); - } - } -} - public sealed class ModGroupEditDrawer( ModManager modManager, Configuration config, FilenameService filenames, - DescriptionEditPopup descriptionPopup) : IUiService + DescriptionEditPopup descriptionPopup, + MetaFileManager metaManager) : IUiService { private static ReadOnlySpan DragDropLabel => "##DragOption"u8; @@ -501,7 +369,111 @@ public sealed class ModGroupEditDrawer( } private void DrawImcGroup(ImcModGroup group) - { } + { + using (ImUtf8.Group()) + { + ImUtf8.Text("Object Type"u8); + if (group.ObjectType is ObjectType.Equipment or ObjectType.Accessory or ObjectType.DemiHuman) + ImUtf8.Text("Slot"u8); + ImUtf8.Text("Primary ID"); + if (group.ObjectType is not ObjectType.Equipment and not ObjectType.Accessory) + ImUtf8.Text("Secondary ID"); + ImUtf8.Text("Variant"u8); + + ImUtf8.TextFrameAligned("Material ID"u8); + ImUtf8.TextFrameAligned("Material Animation ID"u8); + ImUtf8.TextFrameAligned("Decal ID"u8); + ImUtf8.TextFrameAligned("VFX ID"u8); + ImUtf8.TextFrameAligned("Sound ID"u8); + ImUtf8.TextFrameAligned("Can Be Disabled"u8); + ImUtf8.TextFrameAligned("Default Attributes"u8); + } + + ImGui.SameLine(); + + var attributeCache = new ImcAttributeCache(group); + + using (ImUtf8.Group()) + { + ImUtf8.Text(group.ObjectType.ToName()); + if (group.ObjectType is ObjectType.Equipment or ObjectType.Accessory or ObjectType.DemiHuman) + ImUtf8.Text(group.EquipSlot.ToName()); + ImUtf8.Text($"{group.PrimaryId.Id}"); + if (group.ObjectType is not ObjectType.Equipment and not ObjectType.Accessory) + ImUtf8.Text($"{group.SecondaryId.Id}"); + ImUtf8.Text($"{group.Variant.Id}"); + + ImUtf8.TextFrameAligned($"{group.DefaultEntry.MaterialId}"); + ImUtf8.TextFrameAligned($"{group.DefaultEntry.MaterialAnimationId}"); + ImUtf8.TextFrameAligned($"{group.DefaultEntry.DecalId}"); + ImUtf8.TextFrameAligned($"{group.DefaultEntry.VfxId}"); + ImUtf8.TextFrameAligned($"{group.DefaultEntry.SoundId}"); + + var canBeDisabled = group.CanBeDisabled; + if (ImUtf8.Checkbox("##disabled"u8, ref canBeDisabled)) + modManager.OptionEditor.ImcEditor.ChangeCanBeDisabled(group, canBeDisabled, SaveType.Queue); + + var defaultDisabled = group.DefaultDisabled; + ImUtf8.SameLineInner(); + if (ImUtf8.Checkbox("##defaultDisabled"u8, ref defaultDisabled)) + modManager.OptionEditor.ChangeModGroupDefaultOption(group, + group.DefaultSettings.SetBit(ImcModGroup.DisabledIndex, defaultDisabled)); + + DrawAttributes(modManager.OptionEditor.ImcEditor, attributeCache, group.DefaultEntry.AttributeMask, group); + } + + + foreach (var (option, optionIdx) in group.OptionData.WithIndex()) + { + using var id = ImRaii.PushId(optionIdx); + DrawOptionPosition(group, option, optionIdx); + + ImUtf8.SameLineInner(); + DrawOptionDefaultMultiBehaviour(group, option, optionIdx); + + ImUtf8.SameLineInner(); + DrawOptionName(option); + + ImUtf8.SameLineInner(); + DrawOptionDescription(option); + + ImUtf8.SameLineInner(); + DrawOptionDelete(option); + + ImUtf8.SameLineInner(); + ImGui.Dummy(new Vector2(_priorityWidth, 0)); + + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + _optionIdxSelectable.X + ImUtf8.ItemInnerSpacing.X * 2 + ImUtf8.FrameHeight); + DrawAttributes(modManager.OptionEditor.ImcEditor, attributeCache, option.AttributeMask, option); + } + + DrawNewOption(group, attributeCache); + return; + + static void DrawAttributes(ImcModGroupEditor editor, in ImcAttributeCache cache, ushort mask, object data) + { + for (var i = 0; i < ImcEntry.NumAttributes; ++i) + { + using var id = ImRaii.PushId(i); + var value = (mask & (1 << i)) != 0; + using (ImRaii.Disabled(!cache.CanChange(i))) + { + if (ImUtf8.Checkbox(TerminatedByteString.Empty, ref value)) + { + if (data is ImcModGroup g) + editor.ChangeDefaultAttribute(g, cache, i, value); + else + editor.ChangeOptionAttribute((ImcSubMod)data, cache, i, value); + } + } + + ImUtf8.HoverTooltip($"{(char)('A' + i)}"); + if (i != 9) + ImUtf8.SameLineInner(); + } + } + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void DrawOptionPosition(IModGroup group, IModOption option, int optionIdx) @@ -575,14 +547,14 @@ public sealed class ModGroupEditDrawer( if (count >= int.MaxValue) return; - DrawNewOptionBase(group, count); + var name = DrawNewOptionBase(group, count); - var validName = _newOptionName?.Length > 0; + var validName = name.Length > 0; if (ImUtf8.IconButton(FontAwesomeIcon.Plus, validName ? "Add a new option to this group."u8 : "Please enter a name for the new option."u8, !validName)) { - modManager.OptionEditor.SingleEditor.AddOption(group, _newOptionName!); + modManager.OptionEditor.SingleEditor.AddOption(group, name); _newOptionName = null; } } @@ -593,25 +565,36 @@ public sealed class ModGroupEditDrawer( if (count >= IModGroup.MaxMultiOptions) return; - DrawNewOptionBase(group, count); + var name = DrawNewOptionBase(group, count); - var validName = _newOptionName?.Length > 0; + var validName = name.Length > 0; if (ImUtf8.IconButton(FontAwesomeIcon.Plus, validName ? "Add a new option to this group."u8 : "Please enter a name for the new option."u8, !validName)) { - modManager.OptionEditor.MultiEditor.AddOption(group, _newOptionName!); + modManager.OptionEditor.MultiEditor.AddOption(group, name); _newOptionName = null; } } - private void DrawNewOption(ImcModGroup group) + private void DrawNewOption(ImcModGroup group, in ImcAttributeCache cache) { - // TODO + if (cache.LowestUnsetMask == 0) + return; + + var name = DrawNewOptionBase(group, group.Options.Count); + var validName = name.Length > 0; + if (ImUtf8.IconButton(FontAwesomeIcon.Plus, validName + ? "Add a new option to this group."u8 + : "Please enter a name for the new option."u8, !validName)) + { + modManager.OptionEditor.ImcEditor.AddOption(group, cache, name); + _newOptionName = null; + } } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DrawNewOptionBase(IModGroup group, int count) + private string DrawNewOptionBase(IModGroup group, int count) { ImUtf8.Selectable($"Option #{count + 1}", false, size: _optionIdxSelectable); Target(group, count); @@ -631,6 +614,7 @@ public sealed class ModGroupEditDrawer( } ImUtf8.SameLineInner(); + return newName; } [MethodImpl(MethodImplOptions.AggressiveInlining)] From e85b84dafe543b3a2e8bcfe13ed665fbe92b4c93 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 21 May 2024 18:24:21 +0200 Subject: [PATCH 157/433] Add the option to omit mch offhands from changed items. --- Penumbra/Collections/Cache/CollectionCache.cs | 10 +- .../Cache/CollectionCacheManager.cs | 8 +- Penumbra/Configuration.cs | 25 ++-- Penumbra/Mods/Groups/IModGroup.cs | 2 + Penumbra/Mods/Groups/ImcModGroup.cs | 5 + Penumbra/Mods/Groups/MultiModGroup.cs | 8 ++ Penumbra/Mods/Groups/SingleModGroup.cs | 8 ++ Penumbra/Mods/Manager/ModCacheManager.cs | 92 ++------------ Penumbra/Mods/Manager/ModImportManager.cs | 19 +-- Penumbra/Mods/Mod.cs | 6 +- Penumbra/UI/ModsTab/ModGroupEditDrawer.cs | 3 +- Penumbra/UI/Tabs/SettingsTab.cs | 8 ++ Penumbra/Util/IdentifierExtensions.cs | 115 ++++++++++++++++++ 13 files changed, 192 insertions(+), 117 deletions(-) create mode 100644 Penumbra/Util/IdentifierExtensions.cs diff --git a/Penumbra/Collections/Cache/CollectionCache.cs b/Penumbra/Collections/Cache/CollectionCache.cs index ded1dc73..e2f20b46 100644 --- a/Penumbra/Collections/Cache/CollectionCache.cs +++ b/Penumbra/Collections/Cache/CollectionCache.cs @@ -6,6 +6,7 @@ using Penumbra.Communication; using Penumbra.Mods.Editor; using Penumbra.String.Classes; using Penumbra.Mods.Manager; +using Penumbra.Util; namespace Penumbra.Collections.Cache; @@ -252,8 +253,8 @@ public sealed class CollectionCache : IDisposable return mod.GetData(); var settings = _collection[mod.Index].Settings; - return settings is not { Enabled: true } - ? AppliedModData.Empty + return settings is not { Enabled: true } + ? AppliedModData.Empty : mod.GetData(settings); } @@ -439,9 +440,12 @@ public sealed class CollectionCache : IDisposable foreach (var (manip, mod) in Meta) { - ModCacheManager.ComputeChangedItems(identifier, items, manip); + identifier.MetaChangedItems(items, manip); AddItems(mod); } + + if (_manager.Config.HideMachinistOffhandFromChangedItems) + _changedItems.RemoveMachinistOffhands(); } catch (Exception e) { diff --git a/Penumbra/Collections/Cache/CollectionCacheManager.cs b/Penumbra/Collections/Cache/CollectionCacheManager.cs index fb9ee9a3..ca57c8b9 100644 --- a/Penumbra/Collections/Cache/CollectionCacheManager.cs +++ b/Penumbra/Collections/Cache/CollectionCacheManager.cs @@ -25,6 +25,7 @@ public class CollectionCacheManager : IDisposable private readonly ModStorage _modStorage; private readonly CollectionStorage _storage; private readonly ActiveCollections _active; + internal readonly Configuration Config; internal readonly ResolvedFileChanged ResolvedFileChanged; internal readonly MetaFileManager MetaFileManager; internal readonly ResourceLoader ResourceLoader; @@ -40,7 +41,8 @@ public class CollectionCacheManager : IDisposable => _storage.Where(c => c.HasCache); public CollectionCacheManager(FrameworkManager framework, CommunicatorService communicator, TempModManager tempMods, ModStorage modStorage, - MetaFileManager metaFileManager, ActiveCollections active, CollectionStorage storage, ResourceLoader resourceLoader) + MetaFileManager metaFileManager, ActiveCollections active, CollectionStorage storage, ResourceLoader resourceLoader, + Configuration config) { _framework = framework; _communicator = communicator; @@ -50,6 +52,7 @@ public class CollectionCacheManager : IDisposable _active = active; _storage = storage; ResourceLoader = resourceLoader; + Config = config; ResolvedFileChanged = _communicator.ResolvedFileChanged; if (!_active.Individuals.IsLoaded) @@ -260,7 +263,8 @@ public class CollectionCacheManager : IDisposable } /// Prepare Changes by removing mods from caches with collections or add or reload mods. - private void OnModOptionChange(ModOptionChangeType type, Mod mod, IModGroup? group, IModOption? option, IModDataContainer? container, int movedToIdx) + private void OnModOptionChange(ModOptionChangeType type, Mod mod, IModGroup? group, IModOption? option, IModDataContainer? container, + int movedToIdx) { if (type is ModOptionChangeType.PrepareChange) { diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index a065bc26..b81e84d8 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -41,18 +41,19 @@ public class Configuration : IPluginConfiguration, ISavable public bool HideUiWhenUiHidden { get; set; } = false; public bool UseDalamudUiTextureRedirection { get; set; } = true; - public bool UseCharacterCollectionInMainWindow { get; set; } = true; - public bool UseCharacterCollectionsInCards { get; set; } = true; - public bool UseCharacterCollectionInInspect { get; set; } = true; - public bool UseCharacterCollectionInTryOn { get; set; } = true; - public bool UseOwnerNameForCharacterCollection { get; set; } = true; - public bool UseNoModsInInspect { get; set; } = false; - public bool HideChangedItemFilters { get; set; } = false; - public bool ReplaceNonAsciiOnImport { get; set; } = false; - public bool HidePrioritiesInSelector { get; set; } = false; - public bool HideRedrawBar { get; set; } = false; - public RenameField ShowRename { get; set; } = RenameField.BothDataPrio; - public int OptionGroupCollapsibleMin { get; set; } = 5; + public bool UseCharacterCollectionInMainWindow { get; set; } = true; + public bool UseCharacterCollectionsInCards { get; set; } = true; + public bool UseCharacterCollectionInInspect { get; set; } = true; + public bool UseCharacterCollectionInTryOn { get; set; } = true; + public bool UseOwnerNameForCharacterCollection { get; set; } = true; + public bool UseNoModsInInspect { get; set; } = false; + public bool HideChangedItemFilters { get; set; } = false; + public bool ReplaceNonAsciiOnImport { get; set; } = false; + public bool HidePrioritiesInSelector { get; set; } = false; + public bool HideRedrawBar { get; set; } = false; + public bool HideMachinistOffhandFromChangedItems { get; set; } = true; + public RenameField ShowRename { get; set; } = RenameField.BothDataPrio; + public int OptionGroupCollapsibleMin { get; set; } = 5; public Vector2 MinimumSize = new(Constants.MinimumSizeX, Constants.MinimumSizeY); diff --git a/Penumbra/Mods/Groups/IModGroup.cs b/Penumbra/Mods/Groups/IModGroup.cs index 2ec60f7e..ab367532 100644 --- a/Penumbra/Mods/Groups/IModGroup.cs +++ b/Penumbra/Mods/Groups/IModGroup.cs @@ -1,5 +1,6 @@ using Newtonsoft.Json; using Penumbra.Api.Enums; +using Penumbra.GameData.Data; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; @@ -40,6 +41,7 @@ public interface IModGroup public int GetIndex(); public void AddData(Setting setting, Dictionary redirections, HashSet manipulations); + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems); /// Ensure that a value is valid for a group. public Setting FixSetting(Setting setting); diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs index 671d684f..173bf57e 100644 --- a/Penumbra/Mods/Groups/ImcModGroup.cs +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -3,12 +3,14 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui.Classes; using Penumbra.Api.Enums; +using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.String.Classes; +using Penumbra.Util; namespace Penumbra.Mods.Groups; @@ -119,6 +121,9 @@ public class ImcModGroup(Mod mod) : IModGroup manipulations.Add(imc); } + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + => identifier.MetaChangedItems(changedItems, GetManip(0)); + public Setting FixSetting(Setting setting) => new(setting.Value & (((1ul << OptionData.Count) - 1) | (CanBeDisabled ? 1ul << DisabledIndex : 0))); diff --git a/Penumbra/Mods/Groups/MultiModGroup.cs b/Penumbra/Mods/Groups/MultiModGroup.cs index 7495c4b4..f587fc8f 100644 --- a/Penumbra/Mods/Groups/MultiModGroup.cs +++ b/Penumbra/Mods/Groups/MultiModGroup.cs @@ -4,10 +4,12 @@ using Newtonsoft.Json.Linq; using OtterGui; using OtterGui.Classes; using Penumbra.Api.Enums; +using Penumbra.GameData.Data; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.String.Classes; +using Penumbra.Util; namespace Penumbra.Mods.Groups; @@ -114,6 +116,12 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup } } + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + { + foreach (var container in DataContainers) + identifier.AddChangedItems(container, changedItems); + } + public void WriteJson(JsonTextWriter jWriter, JsonSerializer serializer, DirectoryInfo? basePath = null) { ModSaveGroup.WriteJsonBase(jWriter, this); diff --git a/Penumbra/Mods/Groups/SingleModGroup.cs b/Penumbra/Mods/Groups/SingleModGroup.cs index bc463c1e..7a551322 100644 --- a/Penumbra/Mods/Groups/SingleModGroup.cs +++ b/Penumbra/Mods/Groups/SingleModGroup.cs @@ -2,10 +2,12 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui; using Penumbra.Api.Enums; +using Penumbra.GameData.Data; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.String.Classes; +using Penumbra.Util; namespace Penumbra.Mods.Groups; @@ -99,6 +101,12 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup OptionData[setting.AsIndex].AddDataTo(redirections, manipulations); } + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + { + foreach (var container in DataContainers) + identifier.AddChangedItems(container, changedItems); + } + public Setting FixSetting(Setting setting) => OptionData.Count == 0 ? Setting.Zero : new Setting(Math.Min(setting.Value, (ulong)(OptionData.Count - 1))); diff --git a/Penumbra/Mods/Manager/ModCacheManager.cs b/Penumbra/Mods/Manager/ModCacheManager.cs index c6a723a0..8ab8cf33 100644 --- a/Penumbra/Mods/Manager/ModCacheManager.cs +++ b/Penumbra/Mods/Manager/ModCacheManager.cs @@ -1,26 +1,27 @@ using Penumbra.Communication; using Penumbra.GameData.Data; -using Penumbra.GameData.Enums; -using Penumbra.Meta.Manipulations; using Penumbra.Mods.Groups; using Penumbra.Mods.Manager.OptionEditor; using Penumbra.Mods.SubMods; using Penumbra.Services; +using Penumbra.Util; namespace Penumbra.Mods.Manager; public class ModCacheManager : IDisposable { + private readonly Configuration _config; private readonly CommunicatorService _communicator; private readonly ObjectIdentification _identifier; private readonly ModStorage _modManager; private bool _updatingItems = false; - public ModCacheManager(CommunicatorService communicator, ObjectIdentification identifier, ModStorage modStorage) + public ModCacheManager(CommunicatorService communicator, ObjectIdentification identifier, ModStorage modStorage, Configuration config) { _communicator = communicator; _identifier = identifier; _modManager = modStorage; + _config = config; _communicator.ModOptionChanged.Subscribe(OnModOptionChange, ModOptionChanged.Priority.ModCacheManager); _communicator.ModPathChanged.Subscribe(OnModPathChange, ModPathChanged.Priority.ModCacheManager); @@ -38,75 +39,8 @@ public class ModCacheManager : IDisposable _communicator.ModDiscoveryFinished.Unsubscribe(OnModDiscoveryFinished); } - /// Compute the items changed by a given meta manipulation and put them into the changedItems dictionary. - public static void ComputeChangedItems(ObjectIdentification identifier, IDictionary changedItems, MetaManipulation manip) - { - switch (manip.ManipulationType) - { - case MetaManipulation.Type.Imc: - switch (manip.Imc.ObjectType) - { - case ObjectType.Equipment: - case ObjectType.Accessory: - identifier.Identify(changedItems, - GamePaths.Equipment.Mtrl.Path(manip.Imc.PrimaryId, GenderRace.MidlanderMale, manip.Imc.EquipSlot, manip.Imc.Variant, - "a")); - break; - case ObjectType.Weapon: - identifier.Identify(changedItems, - GamePaths.Weapon.Mtrl.Path(manip.Imc.PrimaryId, manip.Imc.SecondaryId, manip.Imc.Variant, "a")); - break; - case ObjectType.DemiHuman: - identifier.Identify(changedItems, - GamePaths.DemiHuman.Mtrl.Path(manip.Imc.PrimaryId, manip.Imc.SecondaryId, manip.Imc.EquipSlot, manip.Imc.Variant, - "a")); - break; - case ObjectType.Monster: - identifier.Identify(changedItems, - GamePaths.Monster.Mtrl.Path(manip.Imc.PrimaryId, manip.Imc.SecondaryId, manip.Imc.Variant, "a")); - break; - } - - break; - case MetaManipulation.Type.Eqdp: - identifier.Identify(changedItems, - GamePaths.Equipment.Mdl.Path(manip.Eqdp.SetId, Names.CombinedRace(manip.Eqdp.Gender, manip.Eqdp.Race), manip.Eqdp.Slot)); - break; - case MetaManipulation.Type.Eqp: - identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(manip.Eqp.SetId, GenderRace.MidlanderMale, manip.Eqp.Slot)); - break; - case MetaManipulation.Type.Est: - switch (manip.Est.Slot) - { - case EstManipulation.EstType.Hair: - changedItems.TryAdd($"Customization: {manip.Est.Race} {manip.Est.Gender} Hair (Hair) {manip.Est.SetId}", null); - break; - case EstManipulation.EstType.Face: - changedItems.TryAdd($"Customization: {manip.Est.Race} {manip.Est.Gender} Face (Face) {manip.Est.SetId}", null); - break; - case EstManipulation.EstType.Body: - identifier.Identify(changedItems, - GamePaths.Equipment.Mdl.Path(manip.Est.SetId, Names.CombinedRace(manip.Est.Gender, manip.Est.Race), - EquipSlot.Body)); - break; - case EstManipulation.EstType.Head: - identifier.Identify(changedItems, - GamePaths.Equipment.Mdl.Path(manip.Est.SetId, Names.CombinedRace(manip.Est.Gender, manip.Est.Race), - EquipSlot.Head)); - break; - } - - break; - case MetaManipulation.Type.Gmp: - identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(manip.Gmp.SetId, GenderRace.MidlanderMale, EquipSlot.Head)); - break; - case MetaManipulation.Type.Rsp: - changedItems.TryAdd($"{manip.Rsp.SubRace.ToName()} {manip.Rsp.Attribute.ToFullString()}", null); - break; - } - } - - private void OnModOptionChange(ModOptionChangeType type, Mod mod, IModGroup? group, IModOption? option, IModDataContainer? container, int fromIdx) + private void OnModOptionChange(ModOptionChangeType type, Mod mod, IModGroup? group, IModOption? option, IModDataContainer? container, + int fromIdx) { switch (type) { @@ -194,16 +128,14 @@ public class ModCacheManager : IDisposable private void UpdateChangedItems(Mod mod) { - var changedItems = (SortedList)mod.ChangedItems; - changedItems.Clear(); - foreach (var gamePath in mod.AllDataContainers.SelectMany(m => m.Files.Keys.Concat(m.FileSwaps.Keys))) - _identifier.Identify(changedItems, gamePath.ToString()); + mod.ChangedItems.Clear(); - foreach (var manip in mod.AllDataContainers.SelectMany(m => m.Manipulations)) - ComputeChangedItems(_identifier, changedItems, manip); + _identifier.AddChangedItems(mod.Default, mod.ChangedItems); + foreach (var group in mod.Groups) + group.AddChangedItems(_identifier, mod.ChangedItems); - foreach(var imcGroup in mod.Groups.OfType()) - ComputeChangedItems(_identifier, changedItems, imcGroup.GetManip(0)); + if (_config.HideMachinistOffhandFromChangedItems) + mod.ChangedItems.RemoveMachinistOffhands(); mod.LowerChangedItemsString = string.Join("\0", mod.ChangedItems.Keys.Select(k => k.ToLowerInvariant())); } diff --git a/Penumbra/Mods/Manager/ModImportManager.cs b/Penumbra/Mods/Manager/ModImportManager.cs index 73571ea4..c99b7d0e 100644 --- a/Penumbra/Mods/Manager/ModImportManager.cs +++ b/Penumbra/Mods/Manager/ModImportManager.cs @@ -5,12 +5,8 @@ using Penumbra.Mods.Editor; namespace Penumbra.Mods.Manager; -public class ModImportManager : IDisposable +public class ModImportManager(ModManager modManager, Configuration config, ModEditor modEditor) : IDisposable { - private readonly ModManager _modManager; - private readonly Configuration _config; - private readonly ModEditor _modEditor; - private readonly ConcurrentQueue _modsToUnpack = new(); /// Mods need to be added thread-safely outside of iteration. @@ -26,13 +22,6 @@ public class ModImportManager : IDisposable => _modsToAdd; - public ModImportManager(ModManager modManager, Configuration config, ModEditor modEditor) - { - _modManager = modManager; - _config = config; - _modEditor = modEditor; - } - public void TryUnpacking() { if (Importing || !_modsToUnpack.TryDequeue(out var newMods)) @@ -51,7 +40,7 @@ public class ModImportManager : IDisposable if (files.Length == 0) return; - _import = new TexToolsImporter(files.Length, files, AddNewMod, _config, _modEditor, _modManager, _modEditor.Compactor); + _import = new TexToolsImporter(files.Length, files, AddNewMod, config, modEditor, modManager, modEditor.Compactor); } public bool Importing @@ -87,8 +76,8 @@ public class ModImportManager : IDisposable return false; } - _modManager.AddMod(directory); - mod = _modManager.LastOrDefault(); + modManager.AddMod(directory); + mod = modManager.LastOrDefault(); return mod != null && mod.ModPath == directory; } diff --git a/Penumbra/Mods/Mod.cs b/Penumbra/Mods/Mod.cs index 6f6eb8ce..783ef3e6 100644 --- a/Penumbra/Mods/Mod.cs +++ b/Penumbra/Mods/Mod.cs @@ -62,8 +62,8 @@ public sealed class Mod : IMod // Options - public readonly DefaultSubMod Default; - public readonly List Groups = []; + public readonly DefaultSubMod Default; + public readonly List Groups = []; public AppliedModData GetData(ModSettings? settings = null) { @@ -99,7 +99,7 @@ public sealed class Mod : IMod } // Cache - public readonly IReadOnlyDictionary ChangedItems = new SortedList(); + public readonly SortedList ChangedItems = new(); public string LowerChangedItemsString { get; internal set; } = string.Empty; public string AllTagsLower { get; internal set; } = string.Empty; diff --git a/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs index a94c25ea..4ef1577f 100644 --- a/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs @@ -123,8 +123,7 @@ public sealed class ModGroupEditDrawer( ModManager modManager, Configuration config, FilenameService filenames, - DescriptionEditPopup descriptionPopup, - MetaFileManager metaManager) : IUiService + DescriptionEditPopup descriptionPopup) : IUiService { private static ReadOnlySpan DragDropLabel => "##DragOption"u8; diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index 439f7be4..30384538 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -428,6 +428,14 @@ public class SettingsTab : ITab _config.Ephemeral.Save(); } }); + Checkbox("Omit Machinist Offhands in Changed Items", + "Omits all Aetherotransformers (machinist offhands) in the changed items tabs because any change on them changes all of them at the moment.\n\n" + + "Changing this triggers a rediscovery of your mods so all changed items can be updated.", + _config.HideMachinistOffhandFromChangedItems, v => + { + _config.HideMachinistOffhandFromChangedItems = v; + _modManager.DiscoverMods(); + }); Checkbox("Hide Priority Numbers in Mod Selector", "Hides the bracketed non-zero priority numbers displayed in the mod selector when there is enough space for them.", _config.HidePrioritiesInSelector, v => _config.HidePrioritiesInSelector = v); diff --git a/Penumbra/Util/IdentifierExtensions.cs b/Penumbra/Util/IdentifierExtensions.cs new file mode 100644 index 00000000..392a5aba --- /dev/null +++ b/Penumbra/Util/IdentifierExtensions.cs @@ -0,0 +1,115 @@ +using OtterGui.Classes; +using Penumbra.GameData.Data; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; +using Penumbra.Mods.SubMods; + +namespace Penumbra.Util; + +public static class IdentifierExtensions +{ + /// Compute the items changed by a given meta manipulation and put them into the changedItems dictionary. + public static void MetaChangedItems(this ObjectIdentification identifier, IDictionary changedItems, + MetaManipulation manip) + { + switch (manip.ManipulationType) + { + case MetaManipulation.Type.Imc: + switch (manip.Imc.ObjectType) + { + case ObjectType.Equipment: + case ObjectType.Accessory: + identifier.Identify(changedItems, + GamePaths.Equipment.Mtrl.Path(manip.Imc.PrimaryId, GenderRace.MidlanderMale, manip.Imc.EquipSlot, manip.Imc.Variant, + "a")); + break; + case ObjectType.Weapon: + identifier.Identify(changedItems, + GamePaths.Weapon.Mtrl.Path(manip.Imc.PrimaryId, manip.Imc.SecondaryId, manip.Imc.Variant, "a")); + break; + case ObjectType.DemiHuman: + identifier.Identify(changedItems, + GamePaths.DemiHuman.Mtrl.Path(manip.Imc.PrimaryId, manip.Imc.SecondaryId, manip.Imc.EquipSlot, manip.Imc.Variant, + "a")); + break; + case ObjectType.Monster: + identifier.Identify(changedItems, + GamePaths.Monster.Mtrl.Path(manip.Imc.PrimaryId, manip.Imc.SecondaryId, manip.Imc.Variant, "a")); + break; + } + + break; + case MetaManipulation.Type.Eqdp: + identifier.Identify(changedItems, + GamePaths.Equipment.Mdl.Path(manip.Eqdp.SetId, Names.CombinedRace(manip.Eqdp.Gender, manip.Eqdp.Race), manip.Eqdp.Slot)); + break; + case MetaManipulation.Type.Eqp: + identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(manip.Eqp.SetId, GenderRace.MidlanderMale, manip.Eqp.Slot)); + break; + case MetaManipulation.Type.Est: + switch (manip.Est.Slot) + { + case EstManipulation.EstType.Hair: + changedItems.TryAdd($"Customization: {manip.Est.Race} {manip.Est.Gender} Hair (Hair) {manip.Est.SetId}", null); + break; + case EstManipulation.EstType.Face: + changedItems.TryAdd($"Customization: {manip.Est.Race} {manip.Est.Gender} Face (Face) {manip.Est.SetId}", null); + break; + case EstManipulation.EstType.Body: + identifier.Identify(changedItems, + GamePaths.Equipment.Mdl.Path(manip.Est.SetId, Names.CombinedRace(manip.Est.Gender, manip.Est.Race), + EquipSlot.Body)); + break; + case EstManipulation.EstType.Head: + identifier.Identify(changedItems, + GamePaths.Equipment.Mdl.Path(manip.Est.SetId, Names.CombinedRace(manip.Est.Gender, manip.Est.Race), + EquipSlot.Head)); + break; + } + + break; + case MetaManipulation.Type.Gmp: + identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(manip.Gmp.SetId, GenderRace.MidlanderMale, EquipSlot.Head)); + break; + case MetaManipulation.Type.Rsp: + changedItems.TryAdd($"{manip.Rsp.SubRace.ToName()} {manip.Rsp.Attribute.ToFullString()}", null); + break; + } + } + + public static void AddChangedItems(this ObjectIdentification identifier, IModDataContainer container, + IDictionary changedItems) + { + foreach (var gamePath in container.Files.Keys.Concat(container.FileSwaps.Keys)) + identifier.Identify(changedItems, gamePath.ToString()); + + foreach (var manip in container.Manipulations) + MetaChangedItems(identifier, changedItems, manip); + } + + public static void RemoveMachinistOffhands(this SortedList changedItems) + { + for (var i = 0; i < changedItems.Count; i++) + { + { + var value = changedItems.Values[i]; + if (value is EquipItem { Type: FullEquipType.GunOff }) + changedItems.RemoveAt(i--); + } + } + } + + public static void RemoveMachinistOffhands(this SortedList, object?)> changedItems) + { + for (var i = 0; i < changedItems.Count; i++) + { + { + var value = changedItems.Values[i].Item2; + if (value is EquipItem { Type: FullEquipType.GunOff }) + changedItems.RemoveAt(i--); + } + } + } +} From 2585de8b21e25fd62cf2295d58ac2520e526752b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 21 May 2024 22:01:20 +0200 Subject: [PATCH 158/433] Cleanup group drawing somewhat. --- Penumbra/Mods/Editor/ModNormalizer.cs | 16 +- Penumbra/Mods/Groups/IModGroup.cs | 3 + Penumbra/Mods/Groups/ImcModGroup.cs | 5 + Penumbra/Mods/Groups/MultiModGroup.cs | 4 + Penumbra/Mods/Groups/SingleModGroup.cs | 4 + .../Manager/OptionEditor/ModGroupEditor.cs | 94 +-- .../UI/ModsTab/{ => Groups}/AddGroupDrawer.cs | 32 +- .../UI/ModsTab/Groups/IModGroupEditDrawer.cs | 6 + .../ModsTab/Groups/ImcModGroupEditDrawer.cs | 138 ++++ .../UI/ModsTab/{ => Groups}/ModGroupDrawer.cs | 36 +- .../UI/ModsTab/Groups/ModGroupEditDrawer.cs | 360 +++++++++ .../ModsTab/Groups/MultiModGroupEditDrawer.cs | 63 ++ .../Groups/SingleModGroupEditDrawer.cs | 68 ++ Penumbra/UI/ModsTab/MetaManipulationDrawer.cs | 105 +++ Penumbra/UI/ModsTab/ModGroupEditDrawer.cs | 687 ------------------ Penumbra/UI/ModsTab/ModPanelEditTab.cs | 1 + Penumbra/UI/ModsTab/ModPanelSettingsTab.cs | 1 + 17 files changed, 841 insertions(+), 782 deletions(-) rename Penumbra/UI/ModsTab/{ => Groups}/AddGroupDrawer.cs (83%) create mode 100644 Penumbra/UI/ModsTab/Groups/IModGroupEditDrawer.cs create mode 100644 Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs rename Penumbra/UI/ModsTab/{ => Groups}/ModGroupDrawer.cs (85%) create mode 100644 Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs create mode 100644 Penumbra/UI/ModsTab/Groups/MultiModGroupEditDrawer.cs create mode 100644 Penumbra/UI/ModsTab/Groups/SingleModGroupEditDrawer.cs create mode 100644 Penumbra/UI/ModsTab/MetaManipulationDrawer.cs delete mode 100644 Penumbra/UI/ModsTab/ModGroupEditDrawer.cs diff --git a/Penumbra/Mods/Editor/ModNormalizer.cs b/Penumbra/Mods/Editor/ModNormalizer.cs index 437600c9..58e4fc08 100644 --- a/Penumbra/Mods/Editor/ModNormalizer.cs +++ b/Penumbra/Mods/Editor/ModNormalizer.cs @@ -1,4 +1,3 @@ -using System; using Dalamud.Interface.Internal.Notifications; using OtterGui; using OtterGui.Classes; @@ -279,19 +278,8 @@ public class ModNormalizer(ModManager _modManager, Configuration _config) private void ApplyRedirections() { foreach (var (group, groupIdx) in Mod.Groups.WithIndex()) - { - switch (group) - { - case SingleModGroup single: - foreach (var (option, optionIdx) in single.OptionData.WithIndex()) - _modManager.OptionEditor.SetFiles(option, _redirections[groupIdx + 1][optionIdx]); - break; - case MultiModGroup multi: - foreach (var (option, optionIdx) in multi.OptionData.WithIndex()) - _modManager.OptionEditor.SetFiles(option, _redirections[groupIdx + 1][optionIdx]); - break; - } - } + foreach (var (container, containerIdx) in group.DataContainers.WithIndex()) + _modManager.OptionEditor.SetFiles(container, _redirections[groupIdx + 1][containerIdx]); ++Step; } diff --git a/Penumbra/Mods/Groups/IModGroup.cs b/Penumbra/Mods/Groups/IModGroup.cs index ab367532..fcc8c093 100644 --- a/Penumbra/Mods/Groups/IModGroup.cs +++ b/Penumbra/Mods/Groups/IModGroup.cs @@ -5,6 +5,7 @@ using Penumbra.Meta.Manipulations; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.String.Classes; +using Penumbra.UI.ModsTab.Groups; namespace Penumbra.Mods.Groups; @@ -40,6 +41,8 @@ public interface IModGroup public int GetIndex(); + public IModGroupEditDrawer EditDrawer(ModGroupEditDrawer editDrawer); + public void AddData(Setting setting, Dictionary redirections, HashSet manipulations); public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems); diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs index 173bf57e..d2c41f34 100644 --- a/Penumbra/Mods/Groups/ImcModGroup.cs +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -10,6 +10,8 @@ using Penumbra.Meta.Manipulations; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.String.Classes; +using Penumbra.UI.ModsTab; +using Penumbra.UI.ModsTab.Groups; using Penumbra.Util; namespace Penumbra.Mods.Groups; @@ -89,6 +91,9 @@ public class ImcModGroup(Mod mod) : IModGroup public int GetIndex() => ModGroup.GetIndex(this); + public IModGroupEditDrawer EditDrawer(ModGroupEditDrawer editDrawer) + => new ImcModGroupEditDrawer(editDrawer, this); + private ushort GetCurrentMask(Setting setting) { var mask = DefaultEntry.AttributeMask; diff --git a/Penumbra/Mods/Groups/MultiModGroup.cs b/Penumbra/Mods/Groups/MultiModGroup.cs index f587fc8f..7fc9acb3 100644 --- a/Penumbra/Mods/Groups/MultiModGroup.cs +++ b/Penumbra/Mods/Groups/MultiModGroup.cs @@ -9,6 +9,7 @@ using Penumbra.Meta.Manipulations; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.String.Classes; +using Penumbra.UI.ModsTab.Groups; using Penumbra.Util; namespace Penumbra.Mods.Groups; @@ -107,6 +108,9 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup public int GetIndex() => ModGroup.GetIndex(this); + public IModGroupEditDrawer EditDrawer(ModGroupEditDrawer editDrawer) + => new MultiModGroupEditDrawer(editDrawer, this); + public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) { foreach (var (option, index) in OptionData.WithIndex().OrderByDescending(o => o.Value.Priority)) diff --git a/Penumbra/Mods/Groups/SingleModGroup.cs b/Penumbra/Mods/Groups/SingleModGroup.cs index 7a551322..4eec0746 100644 --- a/Penumbra/Mods/Groups/SingleModGroup.cs +++ b/Penumbra/Mods/Groups/SingleModGroup.cs @@ -7,6 +7,7 @@ using Penumbra.Meta.Manipulations; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.String.Classes; +using Penumbra.UI.ModsTab.Groups; using Penumbra.Util; namespace Penumbra.Mods.Groups; @@ -93,6 +94,9 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup public int GetIndex() => ModGroup.GetIndex(this); + public IModGroupEditDrawer EditDrawer(ModGroupEditDrawer editDrawer) + => new SingleModGroupEditDrawer(editDrawer, this); + public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) { if (OptionData.Count == 0) diff --git a/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs index 3c00dcc1..969ad3fa 100644 --- a/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs @@ -37,8 +37,8 @@ public class ModGroupEditor( SingleModGroupEditor singleEditor, MultiModGroupEditor multiEditor, ImcModGroupEditor imcEditor, - CommunicatorService Communicator, - SaveService SaveService, + CommunicatorService communicator, + SaveService saveService, Configuration Config) : IService { public SingleModGroupEditor SingleEditor @@ -57,8 +57,8 @@ public class ModGroupEditor( return; group.DefaultSettings = defaultOption; - SaveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.DefaultOptionChanged, group.Mod, group, null, null, -1); + saveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.DefaultOptionChanged, group.Mod, group, null, null, -1); } /// Rename an option group if possible. @@ -68,10 +68,10 @@ public class ModGroupEditor( if (oldName == newName || !VerifyFileName(group.Mod, group, newName, true)) return; - SaveService.ImmediateDelete(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + saveService.ImmediateDelete(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); group.Name = newName; - SaveService.ImmediateSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupRenamed, group.Mod, group, null, null, -1); + saveService.ImmediateSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupRenamed, group.Mod, group, null, null, -1); } /// Delete a given option group. Fires an event to prepare before actually deleting. @@ -79,22 +79,22 @@ public class ModGroupEditor( { var mod = group.Mod; var idx = group.GetIndex(); - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, group, null, null, -1); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, mod, group, null, null, -1); mod.Groups.RemoveAt(idx); - SaveService.SaveAllOptionGroups(mod, false, Config.ReplaceNonAsciiOnImport); - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupDeleted, mod, null, null, null, idx); + saveService.SaveAllOptionGroups(mod, false, Config.ReplaceNonAsciiOnImport); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupDeleted, mod, null, null, null, idx); } /// Move the index of a given option group. public void MoveModGroup(IModGroup group, int groupIdxTo) { - var mod = group.Mod; + var mod = group.Mod; var idxFrom = group.GetIndex(); if (!mod.Groups.Move(idxFrom, groupIdxTo)) return; - SaveService.SaveAllOptionGroups(mod, false, Config.ReplaceNonAsciiOnImport); - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupMoved, mod, group, null, null, idxFrom); + saveService.SaveAllOptionGroups(mod, false, Config.ReplaceNonAsciiOnImport); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupMoved, mod, group, null, null, idxFrom); } /// Change the internal priority of the given option group. @@ -104,8 +104,8 @@ public class ModGroupEditor( return; group.Priority = newPriority; - SaveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.PriorityChanged, group.Mod, group, null, null, -1); + saveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.PriorityChanged, group.Mod, group, null, null, -1); } /// Change the description of the given option group. @@ -115,8 +115,8 @@ public class ModGroupEditor( return; group.Description = newDescription; - SaveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, group.Mod, group, null, null, -1); + saveService.QueueSave(new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, group.Mod, group, null, null, -1); } /// Rename the given option. @@ -126,8 +126,8 @@ public class ModGroupEditor( return; option.Name = newName; - SaveService.QueueSave(new ModSaveGroup(option.Group, Config.ReplaceNonAsciiOnImport)); - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, option.Mod, option.Group, option, null, -1); + saveService.QueueSave(new ModSaveGroup(option.Group, Config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, option.Mod, option.Group, option, null, -1); } /// Change the description of the given option. @@ -137,8 +137,8 @@ public class ModGroupEditor( return; option.Description = newDescription; - SaveService.QueueSave(new ModSaveGroup(option.Group, Config.ReplaceNonAsciiOnImport)); - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, option.Mod, option.Group, option, null, -1); + saveService.QueueSave(new ModSaveGroup(option.Group, Config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.DisplayChange, option.Mod, option.Group, option, null, -1); } /// Set the meta manipulations for a given option. Replaces existing manipulations. @@ -148,10 +148,10 @@ public class ModGroupEditor( && subMod.Manipulations.All(m => manipulations.TryGetValue(m, out var old) && old.EntryEquals(m))) return; - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); subMod.Manipulations.SetTo(manipulations); - SaveService.Save(saveType, new ModSaveGroup(subMod, Config.ReplaceNonAsciiOnImport)); - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMetaChanged, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); + saveService.Save(saveType, new ModSaveGroup(subMod, Config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMetaChanged, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); } /// Set the file redirections for a given option. Replaces existing redirections. @@ -160,10 +160,10 @@ public class ModGroupEditor( if (subMod.Files.SetEquals(replacements)) return; - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); subMod.Files.SetTo(replacements); - SaveService.Save(saveType, new ModSaveGroup(subMod, Config.ReplaceNonAsciiOnImport)); - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesChanged, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); + saveService.Save(saveType, new ModSaveGroup(subMod, Config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesChanged, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); } /// Add additional file redirections to a given option, keeping already existing ones. Only fires an event if anything is actually added. @@ -173,8 +173,8 @@ public class ModGroupEditor( subMod.Files.AddFrom(additions); if (oldCount != subMod.Files.Count) { - SaveService.QueueSave(new ModSaveGroup(subMod, Config.ReplaceNonAsciiOnImport)); - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesAdded, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); + saveService.QueueSave(new ModSaveGroup(subMod, Config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesAdded, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); } } @@ -184,10 +184,10 @@ public class ModGroupEditor( if (subMod.FileSwaps.SetEquals(swaps)) return; - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); subMod.FileSwaps.SetTo(swaps); - SaveService.Save(saveType, new ModSaveGroup(subMod, Config.ReplaceNonAsciiOnImport)); - Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionSwapsChanged, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); + saveService.Save(saveType, new ModSaveGroup(subMod, Config.ReplaceNonAsciiOnImport)); + communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionSwapsChanged, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); } /// Verify that a new option group name is unique in this mod. @@ -227,45 +227,45 @@ public class ModGroupEditor( => group switch { SingleModGroup s => SingleEditor.AddOption(s, option), - MultiModGroup m => MultiEditor.AddOption(m, option), - ImcModGroup i => ImcEditor.AddOption(i, option), - _ => null, + MultiModGroup m => MultiEditor.AddOption(m, option), + ImcModGroup i => ImcEditor.AddOption(i, option), + _ => null, }; public IModOption? AddOption(IModGroup group, string newName) => group switch { SingleModGroup s => SingleEditor.AddOption(s, newName), - MultiModGroup m => MultiEditor.AddOption(m, newName), - ImcModGroup i => ImcEditor.AddOption(i, newName), - _ => null, + MultiModGroup m => MultiEditor.AddOption(m, newName), + ImcModGroup i => ImcEditor.AddOption(i, newName), + _ => null, }; public IModGroup? AddModGroup(Mod mod, GroupType type, string newName, SaveType saveType = SaveType.ImmediateSync) => type switch { GroupType.Single => SingleEditor.AddModGroup(mod, newName, saveType), - GroupType.Multi => MultiEditor.AddModGroup(mod, newName, saveType), - GroupType.Imc => ImcEditor.AddModGroup(mod, newName, default, saveType), - _ => null, + GroupType.Multi => MultiEditor.AddModGroup(mod, newName, saveType), + GroupType.Imc => ImcEditor.AddModGroup(mod, newName, default, saveType), + _ => null, }; public (IModGroup?, int, bool) FindOrAddModGroup(Mod mod, GroupType type, string name, SaveType saveType = SaveType.ImmediateSync) => type switch { GroupType.Single => SingleEditor.FindOrAddModGroup(mod, name, saveType), - GroupType.Multi => MultiEditor.FindOrAddModGroup(mod, name, saveType), - GroupType.Imc => ImcEditor.FindOrAddModGroup(mod, name, saveType), - _ => (null, -1, false), + GroupType.Multi => MultiEditor.FindOrAddModGroup(mod, name, saveType), + GroupType.Imc => ImcEditor.FindOrAddModGroup(mod, name, saveType), + _ => (null, -1, false), }; public (IModOption?, int, bool) FindOrAddOption(IModGroup group, string name, SaveType saveType = SaveType.ImmediateSync) => group switch { SingleModGroup s => SingleEditor.FindOrAddOption(s, name, saveType), - MultiModGroup m => MultiEditor.FindOrAddOption(m, name, saveType), - ImcModGroup i => ImcEditor.FindOrAddOption(i, name, saveType), - _ => (null, -1, false), + MultiModGroup m => MultiEditor.FindOrAddOption(m, name, saveType), + ImcModGroup i => ImcEditor.FindOrAddOption(i, name, saveType), + _ => (null, -1, false), }; public void MoveOption(IModOption option, int toIdx) diff --git a/Penumbra/UI/ModsTab/AddGroupDrawer.cs b/Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs similarity index 83% rename from Penumbra/UI/ModsTab/AddGroupDrawer.cs rename to Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs index 79c1bf9d..06cb4154 100644 --- a/Penumbra/UI/ModsTab/AddGroupDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs @@ -12,25 +12,25 @@ using Penumbra.Mods.Manager; using Penumbra.Mods.Manager.OptionEditor; using Penumbra.UI.Classes; -namespace Penumbra.UI.ModsTab; +namespace Penumbra.UI.ModsTab.Groups; public class AddGroupDrawer : IUiService { - private string _groupName = string.Empty; - private bool _groupNameValid = false; + private string _groupName = string.Empty; + private bool _groupNameValid = false; - private ImcManipulation _imcManip = new(EquipSlot.Head, 1, 1, new ImcEntry()); - private ImcEntry _defaultEntry; - private bool _imcFileExists; - private bool _entryExists; - private bool _entryInvalid; + private ImcManipulation _imcManip = new(EquipSlot.Head, 1, 1, new ImcEntry()); + private ImcEntry _defaultEntry; + private bool _imcFileExists; + private bool _entryExists; + private bool _entryInvalid; private readonly MetaFileManager _metaManager; - private readonly ModManager _modManager; + private readonly ModManager _modManager; public AddGroupDrawer(MetaFileManager metaManager, ModManager modManager) { _metaManager = metaManager; - _modManager = modManager; + _modManager = modManager; UpdateEntry(); } @@ -61,7 +61,7 @@ public class AddGroupDrawer : IUiService return; _modManager.OptionEditor.AddModGroup(mod, GroupType.Single, _groupName); - _groupName = string.Empty; + _groupName = string.Empty; _groupNameValid = false; } @@ -74,7 +74,7 @@ public class AddGroupDrawer : IUiService return; _modManager.OptionEditor.AddModGroup(mod, GroupType.Multi, _groupName); - _groupName = string.Empty; + _groupName = string.Empty; _groupNameValid = false; } @@ -126,7 +126,7 @@ public class AddGroupDrawer : IUiService width, !_groupNameValid || _entryInvalid)) { _modManager.OptionEditor.ImcEditor.AddModGroup(mod, _groupName, _imcManip); - _groupName = string.Empty; + _groupName = string.Empty; _groupNameValid = false; } @@ -150,12 +150,12 @@ public class AddGroupDrawer : IUiService } catch (Exception) { - _defaultEntry = new ImcEntry(); + _defaultEntry = new ImcEntry(); _imcFileExists = false; - _entryExists = false; + _entryExists = false; } - _imcManip = _imcManip.Copy(_entryExists ? _defaultEntry : new ImcEntry()); + _imcManip = _imcManip.Copy(_entryExists ? _defaultEntry : new ImcEntry()); _entryInvalid = !_imcManip.Validate(true); } } diff --git a/Penumbra/UI/ModsTab/Groups/IModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/IModGroupEditDrawer.cs new file mode 100644 index 00000000..d7114147 --- /dev/null +++ b/Penumbra/UI/ModsTab/Groups/IModGroupEditDrawer.cs @@ -0,0 +1,6 @@ +namespace Penumbra.UI.ModsTab.Groups; + +public interface IModGroupEditDrawer +{ + public void Draw(); +} diff --git a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs new file mode 100644 index 00000000..2418c5cb --- /dev/null +++ b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs @@ -0,0 +1,138 @@ +using Dalamud.Interface; +using ImGuiNET; +using OtterGui; +using OtterGui.Classes; +using OtterGui.Raii; +using OtterGui.Text; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Manager.OptionEditor; +using Penumbra.Mods.SubMods; + +namespace Penumbra.UI.ModsTab.Groups; + +public readonly struct ImcModGroupEditDrawer(ModGroupEditDrawer editor, ImcModGroup group) : IModGroupEditDrawer +{ + public void Draw() + { + using (ImUtf8.Group()) + { + ImUtf8.Text("Object Type"u8); + if (group.ObjectType is ObjectType.Equipment or ObjectType.Accessory or ObjectType.DemiHuman) + ImUtf8.Text("Slot"u8); + ImUtf8.Text("Primary ID"); + if (group.ObjectType is not ObjectType.Equipment and not ObjectType.Accessory) + ImUtf8.Text("Secondary ID"); + ImUtf8.Text("Variant"u8); + + ImUtf8.TextFrameAligned("Material ID"u8); + ImUtf8.TextFrameAligned("Material Animation ID"u8); + ImUtf8.TextFrameAligned("Decal ID"u8); + ImUtf8.TextFrameAligned("VFX ID"u8); + ImUtf8.TextFrameAligned("Sound ID"u8); + ImUtf8.TextFrameAligned("Can Be Disabled"u8); + ImUtf8.TextFrameAligned("Default Attributes"u8); + } + + ImGui.SameLine(); + + var attributeCache = new ImcAttributeCache(group); + + using (ImUtf8.Group()) + { + ImUtf8.Text(group.ObjectType.ToName()); + if (group.ObjectType is ObjectType.Equipment or ObjectType.Accessory or ObjectType.DemiHuman) + ImUtf8.Text(group.EquipSlot.ToName()); + ImUtf8.Text($"{group.PrimaryId.Id}"); + if (group.ObjectType is not ObjectType.Equipment and not ObjectType.Accessory) + ImUtf8.Text($"{group.SecondaryId.Id}"); + ImUtf8.Text($"{group.Variant.Id}"); + + ImUtf8.TextFrameAligned($"{group.DefaultEntry.MaterialId}"); + ImUtf8.TextFrameAligned($"{group.DefaultEntry.MaterialAnimationId}"); + ImUtf8.TextFrameAligned($"{group.DefaultEntry.DecalId}"); + ImUtf8.TextFrameAligned($"{group.DefaultEntry.VfxId}"); + ImUtf8.TextFrameAligned($"{group.DefaultEntry.SoundId}"); + + var canBeDisabled = group.CanBeDisabled; + if (ImUtf8.Checkbox("##disabled"u8, ref canBeDisabled)) + editor.ModManager.OptionEditor.ImcEditor.ChangeCanBeDisabled(group, canBeDisabled, SaveType.Queue); + + var defaultDisabled = group.DefaultDisabled; + ImUtf8.SameLineInner(); + if (ImUtf8.Checkbox("##defaultDisabled"u8, ref defaultDisabled)) + editor.ModManager.OptionEditor.ChangeModGroupDefaultOption(group, + group.DefaultSettings.SetBit(ImcModGroup.DisabledIndex, defaultDisabled)); + + DrawAttributes(editor.ModManager.OptionEditor.ImcEditor, attributeCache, group.DefaultEntry.AttributeMask, group); + } + + + foreach (var (option, optionIdx) in group.OptionData.WithIndex()) + { + using var id = ImRaii.PushId(optionIdx); + editor.DrawOptionPosition(group, option, optionIdx); + + ImUtf8.SameLineInner(); + editor.DrawOptionDefaultMultiBehaviour(group, option, optionIdx); + + ImUtf8.SameLineInner(); + editor.DrawOptionName(option); + + ImUtf8.SameLineInner(); + editor.DrawOptionDescription(option); + + ImUtf8.SameLineInner(); + editor.DrawOptionDelete(option); + + ImUtf8.SameLineInner(); + ImGui.Dummy(new Vector2(editor.PriorityWidth, 0)); + + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + editor.OptionIdxSelectable.X + ImUtf8.ItemInnerSpacing.X * 2 + ImUtf8.FrameHeight); + DrawAttributes(editor.ModManager.OptionEditor.ImcEditor, attributeCache, option.AttributeMask, option); + } + + DrawNewOption(attributeCache); + return; + + static void DrawAttributes(ImcModGroupEditor editor, in ImcAttributeCache cache, ushort mask, object data) + { + for (var i = 0; i < ImcEntry.NumAttributes; ++i) + { + using var id = ImRaii.PushId(i); + var value = (mask & 1 << i) != 0; + using (ImRaii.Disabled(!cache.CanChange(i))) + { + if (ImUtf8.Checkbox(TerminatedByteString.Empty, ref value)) + { + if (data is ImcModGroup g) + editor.ChangeDefaultAttribute(g, cache, i, value); + else + editor.ChangeOptionAttribute((ImcSubMod)data, cache, i, value); + } + } + + ImUtf8.HoverTooltip($"{(char)('A' + i)}"); + if (i != 9) + ImUtf8.SameLineInner(); + } + } + } + + private void DrawNewOption(in ImcAttributeCache cache) + { + if (cache.LowestUnsetMask == 0) + return; + + var name = editor.DrawNewOptionBase(group, group.Options.Count); + var validName = name.Length > 0; + if (ImUtf8.IconButton(FontAwesomeIcon.Plus, validName + ? "Add a new option to this group."u8 + : "Please enter a name for the new option."u8, !validName)) + { + editor.ModManager.OptionEditor.ImcEditor.AddOption(group, cache, name); + editor.NewOptionName = null; + } + } +} diff --git a/Penumbra/UI/ModsTab/ModGroupDrawer.cs b/Penumbra/UI/ModsTab/Groups/ModGroupDrawer.cs similarity index 85% rename from Penumbra/UI/ModsTab/ModGroupDrawer.cs rename to Penumbra/UI/ModsTab/Groups/ModGroupDrawer.cs index e9b0b396..dec77430 100644 --- a/Penumbra/UI/ModsTab/ModGroupDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/ModGroupDrawer.cs @@ -11,7 +11,7 @@ using Penumbra.Mods.Groups; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; -namespace Penumbra.UI.ModsTab; +namespace Penumbra.UI.ModsTab.Groups; public sealed class ModGroupDrawer(Configuration config, CollectionManager collectionManager) : IUiService { @@ -63,8 +63,8 @@ public sealed class ModGroupDrawer(Configuration config, CollectionManager colle /// private void DrawSingleGroupCombo(IModGroup group, int groupIdx, Setting setting) { - using var id = ImRaii.PushId(groupIdx); - var selectedOption = setting.AsIndex; + using var id = ImRaii.PushId(groupIdx); + var selectedOption = setting.AsIndex; ImGui.SetNextItemWidth(UiHelpers.InputTextWidth.X * 3 / 4); var options = group.Options; using (var combo = ImRaii.Combo(string.Empty, options[selectedOption].Name)) @@ -97,10 +97,10 @@ public sealed class ModGroupDrawer(Configuration config, CollectionManager colle /// private void DrawSingleGroupRadio(IModGroup group, int groupIdx, Setting setting) { - using var id = ImRaii.PushId(groupIdx); - var selectedOption = setting.AsIndex; - var minWidth = Widget.BeginFramedGroup(group.Name, group.Description); - var options = group.Options; + using var id = ImRaii.PushId(groupIdx); + var selectedOption = setting.AsIndex; + var minWidth = Widget.BeginFramedGroup(group.Name, group.Description); + var options = group.Options; DrawCollapseHandling(options, minWidth, DrawOptions); Widget.EndFramedGroup(); @@ -110,8 +110,8 @@ public sealed class ModGroupDrawer(Configuration config, CollectionManager colle { for (var idx = 0; idx < group.Options.Count; ++idx) { - using var i = ImRaii.PushId(idx); - var option = options[idx]; + using var i = ImRaii.PushId(idx); + var option = options[idx]; if (ImGui.RadioButton(option.Name, selectedOption == idx)) SetModSetting(group, groupIdx, Setting.Single(idx)); @@ -130,9 +130,9 @@ public sealed class ModGroupDrawer(Configuration config, CollectionManager colle /// private void DrawMultiGroup(IModGroup group, int groupIdx, Setting setting) { - using var id = ImRaii.PushId(groupIdx); - var minWidth = Widget.BeginFramedGroup(group.Name, group.Description); - var options = group.Options; + using var id = ImRaii.PushId(groupIdx); + var minWidth = Widget.BeginFramedGroup(group.Name, group.Description); + var options = group.Options; DrawCollapseHandling(options, minWidth, DrawOptions); Widget.EndFramedGroup(); @@ -147,9 +147,9 @@ public sealed class ModGroupDrawer(Configuration config, CollectionManager colle { for (var idx = 0; idx < options.Count; ++idx) { - using var i = ImRaii.PushId(idx); - var option = options[idx]; - var enabled = setting.HasFlag(idx); + using var i = ImRaii.PushId(idx); + var option = options[idx]; + var enabled = setting.HasFlag(idx); if (ImGui.Checkbox(option.Name, ref enabled)) SetModSetting(group, groupIdx, setting.SetBit(idx, enabled)); @@ -187,8 +187,8 @@ public sealed class ModGroupDrawer(Configuration config, CollectionManager colle } else { - var collapseId = ImGui.GetID("Collapse"); - var shown = ImGui.GetStateStorage().GetBool(collapseId, true); + var collapseId = ImGui.GetID("Collapse"); + var shown = ImGui.GetStateStorage().GetBool(collapseId, true); var buttonTextShow = $"Show {options.Count} Options"; var buttonTextHide = $"Hide {options.Count} Options"; var buttonWidth = Math.Max(ImGui.CalcTextSize(buttonTextShow).X, ImGui.CalcTextSize(buttonTextHide).X) @@ -204,7 +204,7 @@ public sealed class ModGroupDrawer(Configuration config, CollectionManager colle } - var width = Math.Max(ImGui.GetItemRectSize().X, minWidth); + var width = Math.Max(ImGui.GetItemRectSize().X, minWidth); var endPos = ImGui.GetCursorPos(); ImGui.SetCursorPos(pos); if (ImGui.Button(buttonTextHide, new Vector2(width, 0))) diff --git a/Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs new file mode 100644 index 00000000..e7d70922 --- /dev/null +++ b/Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs @@ -0,0 +1,360 @@ +using Dalamud.Interface; +using Dalamud.Interface.Internal.Notifications; +using ImGuiNET; +using OtterGui; +using OtterGui.Classes; +using OtterGui.Raii; +using OtterGui.Services; +using OtterGui.Text; +using OtterGui.Text.EndObjects; +using Penumbra.Mods; +using Penumbra.Mods.Groups; +using Penumbra.Mods.Manager; +using Penumbra.Mods.Manager.OptionEditor; +using Penumbra.Mods.Settings; +using Penumbra.Mods.SubMods; +using Penumbra.Services; +using Penumbra.UI.Classes; + +namespace Penumbra.UI.ModsTab.Groups; + +public sealed class ModGroupEditDrawer( + ModManager modManager, + Configuration config, + FilenameService filenames, + DescriptionEditPopup descriptionPopup) : IUiService +{ + private static ReadOnlySpan DragDropLabel + => "##DragOption"u8; + + internal readonly ModManager ModManager = modManager; + internal readonly Queue ActionQueue = new(); + + internal Vector2 OptionIdxSelectable; + internal Vector2 AvailableWidth; + internal float PriorityWidth; + + internal string? NewOptionName; + private IModGroup? _newOptionGroup; + + private Vector2 _buttonSize; + private float _groupNameWidth; + private float _optionNameWidth; + private float _spacing; + private bool _deleteEnabled; + + private string? _currentGroupName; + private ModPriority? _currentGroupPriority; + private IModGroup? _currentGroupEdited; + private bool _isGroupNameValid = true; + + private IModGroup? _dragDropGroup; + private IModOption? _dragDropOption; + + public void Draw(Mod mod) + { + PrepareStyle(); + + using var id = ImUtf8.PushId("##GroupEdit"u8); + foreach (var (group, groupIdx) in mod.Groups.WithIndex()) + DrawGroup(group, groupIdx); + + while (ActionQueue.TryDequeue(out var action)) + action.Invoke(); + } + + private void DrawGroup(IModGroup group, int idx) + { + using var id = ImUtf8.PushId(idx); + using var frame = ImRaii.FramedGroup($"Group #{idx + 1}"); + DrawGroupNameRow(group, idx); + group.EditDrawer(this).Draw(); + } + + private void DrawGroupNameRow(IModGroup group, int idx) + { + DrawGroupName(group); + ImUtf8.SameLineInner(); + DrawGroupMoveButtons(group, idx); + ImUtf8.SameLineInner(); + DrawGroupOpenFile(group, idx); + ImUtf8.SameLineInner(); + DrawGroupDescription(group); + ImUtf8.SameLineInner(); + DrawGroupDelete(group); + ImUtf8.SameLineInner(); + DrawGroupPriority(group); + } + + private void DrawGroupName(IModGroup group) + { + var text = _currentGroupEdited == group ? _currentGroupName ?? group.Name : group.Name; + ImGui.SetNextItemWidth(_groupNameWidth); + using var border = ImRaii.PushFrameBorder(UiHelpers.ScaleX2, Colors.RegexWarningBorder, !_isGroupNameValid); + if (ImUtf8.InputText("##GroupName"u8, ref text)) + { + _currentGroupEdited = group; + _currentGroupName = text; + _isGroupNameValid = text == group.Name || ModGroupEditor.VerifyFileName(group.Mod, group, text, false); + } + + if (ImGui.IsItemDeactivated()) + { + if (_currentGroupName != null && _isGroupNameValid) + ModManager.OptionEditor.RenameModGroup(group, _currentGroupName); + _currentGroupName = null; + _currentGroupEdited = null; + _isGroupNameValid = true; + } + + var tt = _isGroupNameValid + ? "Change the Group name."u8 + : "Current name can not be used for this group."u8; + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, tt); + } + + private void DrawGroupDelete(IModGroup group) + { + if (ImUtf8.IconButton(FontAwesomeIcon.Trash, !_deleteEnabled)) + ActionQueue.Enqueue(() => ModManager.OptionEditor.DeleteModGroup(group)); + + if (_deleteEnabled) + ImUtf8.HoverTooltip("Delete this option group."u8); + else + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, + $"Delete this option group.\nHold {config.DeleteModModifier} while clicking to delete."); + } + + private void DrawGroupPriority(IModGroup group) + { + var priority = _currentGroupEdited == group + ? (_currentGroupPriority ?? group.Priority).Value + : group.Priority.Value; + ImGui.SetNextItemWidth(PriorityWidth); + if (ImGui.InputInt("##GroupPriority", ref priority, 0, 0)) + { + _currentGroupEdited = group; + _currentGroupPriority = new ModPriority(priority); + } + + if (ImGui.IsItemDeactivated()) + { + if (_currentGroupPriority.HasValue) + ModManager.OptionEditor.ChangeGroupPriority(group, _currentGroupPriority.Value); + _currentGroupEdited = null; + _currentGroupPriority = null; + } + + ImGuiUtil.HoverTooltip("Group Priority"); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void DrawGroupDescription(IModGroup group) + { + if (ImUtf8.IconButton(FontAwesomeIcon.Edit, "Edit group description."u8)) + descriptionPopup.Open(group); + } + + private void DrawGroupMoveButtons(IModGroup group, int idx) + { + var isFirst = idx == 0; + if (ImUtf8.IconButton(FontAwesomeIcon.ArrowUp, isFirst)) + ActionQueue.Enqueue(() => ModManager.OptionEditor.MoveModGroup(group, idx - 1)); + + if (isFirst) + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, "Can not move this group further upwards."u8); + else + ImUtf8.HoverTooltip($"Move this group up to group {idx}."); + + + ImUtf8.SameLineInner(); + var isLast = idx == group.Mod.Groups.Count - 1; + if (ImUtf8.IconButton(FontAwesomeIcon.ArrowDown, isLast)) + ActionQueue.Enqueue(() => ModManager.OptionEditor.MoveModGroup(group, idx + 1)); + + if (isLast) + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, "Can not move this group further downwards."u8); + else + ImUtf8.HoverTooltip($"Move this group down to group {idx + 2}."); + } + + private void DrawGroupOpenFile(IModGroup group, int idx) + { + var fileName = filenames.OptionGroupFile(group.Mod, idx, config.ReplaceNonAsciiOnImport); + var fileExists = File.Exists(fileName); + if (ImUtf8.IconButton(FontAwesomeIcon.FileExport, !fileExists)) + try + { + Process.Start(new ProcessStartInfo(fileName) { UseShellExecute = true }); + } + catch (Exception e) + { + Penumbra.Messager.NotificationMessage(e, "Could not open editor.", NotificationType.Error); + } + + if (fileExists) + ImUtf8.HoverTooltip($"Open the {group.Name} json file in the text editor of your choice."); + else + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, $"The {group.Name} json file does not exist."); + } + + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal void DrawOptionPosition(IModGroup group, IModOption option, int optionIdx) + { + ImGui.AlignTextToFramePadding(); + ImUtf8.Selectable($"Option #{optionIdx + 1}", false, size: OptionIdxSelectable); + Target(group, optionIdx); + Source(option); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal void DrawOptionDefaultSingleBehaviour(IModGroup group, IModOption option, int optionIdx) + { + var isDefaultOption = group.DefaultSettings.AsIndex == optionIdx; + if (ImUtf8.RadioButton("##default"u8, isDefaultOption)) + ModManager.OptionEditor.ChangeModGroupDefaultOption(group, Setting.Single(optionIdx)); + ImUtf8.HoverTooltip($"Set {option.Name} as the default choice for this group."); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal void DrawOptionDefaultMultiBehaviour(IModGroup group, IModOption option, int optionIdx) + { + var isDefaultOption = group.DefaultSettings.HasFlag(optionIdx); + if (ImUtf8.Checkbox("##default"u8, ref isDefaultOption)) + ModManager.OptionEditor.ChangeModGroupDefaultOption(group, group.DefaultSettings.SetBit(optionIdx, isDefaultOption)); + ImUtf8.HoverTooltip($"{(isDefaultOption ? "Disable"u8 : "Enable"u8)} {option.Name} per default in this group."); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal void DrawOptionDescription(IModOption option) + { + if (ImUtf8.IconButton(FontAwesomeIcon.Edit, "Edit option description."u8)) + descriptionPopup.Open(option); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal void DrawOptionPriority(MultiSubMod option) + { + var priority = option.Priority.Value; + ImGui.SetNextItemWidth(PriorityWidth); + if (ImUtf8.InputScalarOnDeactivated("##Priority"u8, ref priority)) + ModManager.OptionEditor.MultiEditor.ChangeOptionPriority(option, new ModPriority(priority)); + ImUtf8.HoverTooltip("Option priority inside the mod."u8); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal void DrawOptionName(IModOption option) + { + var name = option.Name; + ImGui.SetNextItemWidth(_optionNameWidth); + if (ImUtf8.InputTextOnDeactivated("##Name"u8, ref name)) + ModManager.OptionEditor.RenameOption(option, name); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal void DrawOptionDelete(IModOption option) + { + if (ImUtf8.IconButton(FontAwesomeIcon.Trash, !_deleteEnabled)) + ActionQueue.Enqueue(() => ModManager.OptionEditor.DeleteOption(option)); + + if (_deleteEnabled) + ImUtf8.HoverTooltip("Delete this option."u8); + else + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, + $"Delete this option.\nHold {config.DeleteModModifier} while clicking to delete."); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal string DrawNewOptionBase(IModGroup group, int count) + { + ImUtf8.Selectable($"Option #{count + 1}", false, size: OptionIdxSelectable); + Target(group, count); + + ImUtf8.SameLineInner(); + ImUtf8.IconDummy(); + + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(_optionNameWidth); + var newName = _newOptionGroup == group + ? NewOptionName ?? string.Empty + : string.Empty; + if (ImUtf8.InputText("##newOption"u8, ref newName, "Add new option..."u8)) + { + NewOptionName = newName; + _newOptionGroup = group; + } + + ImUtf8.SameLineInner(); + return newName; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void Source(IModOption option) + { + if (option.Group is not ITexToolsGroup) + return; + + using var source = ImUtf8.DragDropSource(); + if (!source) + return; + + if (!DragDropSource.SetPayload(DragDropLabel)) + { + _dragDropGroup = option.Group; + _dragDropOption = option; + } + + ImGui.TextUnformatted($"Dragging option {option.Name} from group {option.Group.Name}..."); + } + + private void Target(IModGroup group, int optionIdx) + { + if (group is not ITexToolsGroup) + return; + + if (_dragDropGroup != group && _dragDropGroup != null && group is MultiModGroup { Options.Count: >= IModGroup.MaxMultiOptions }) + return; + + using var target = ImRaii.DragDropTarget(); + if (!target.Success || !DragDropTarget.CheckPayload(DragDropLabel)) + return; + + if (_dragDropGroup != null && _dragDropOption != null) + { + if (_dragDropGroup == group) + { + var sourceOption = _dragDropOption; + ActionQueue.Enqueue(() => ModManager.OptionEditor.MoveOption(sourceOption, optionIdx)); + } + else + { + // Move from one group to another by deleting, then adding, then moving the option. + var sourceOption = _dragDropOption; + ActionQueue.Enqueue(() => + { + ModManager.OptionEditor.DeleteOption(sourceOption); + if (ModManager.OptionEditor.AddOption(group, sourceOption) is { } newOption) + ModManager.OptionEditor.MoveOption(newOption, optionIdx); + }); + } + } + + _dragDropGroup = null; + _dragDropOption = null; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void PrepareStyle() + { + var totalWidth = 400f * ImUtf8.GlobalScale; + _buttonSize = new Vector2(ImUtf8.FrameHeight); + PriorityWidth = 50 * ImUtf8.GlobalScale; + AvailableWidth = new Vector2(totalWidth + 3 * _spacing + 2 * _buttonSize.X + PriorityWidth, 0); + _groupNameWidth = totalWidth - 3 * (_buttonSize.X + _spacing); + _spacing = ImGui.GetStyle().ItemInnerSpacing.X; + OptionIdxSelectable = ImUtf8.CalcTextSize("Option #88."u8); + _optionNameWidth = totalWidth - OptionIdxSelectable.X - _buttonSize.X - 2 * _spacing; + _deleteEnabled = config.DeleteModModifier.IsActive(); + } +} diff --git a/Penumbra/UI/ModsTab/Groups/MultiModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/MultiModGroupEditDrawer.cs new file mode 100644 index 00000000..e6701a03 --- /dev/null +++ b/Penumbra/UI/ModsTab/Groups/MultiModGroupEditDrawer.cs @@ -0,0 +1,63 @@ +using Dalamud.Interface; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Text; +using Penumbra.Mods.Groups; + +namespace Penumbra.UI.ModsTab.Groups; + +public readonly struct MultiModGroupEditDrawer(ModGroupEditDrawer editor, MultiModGroup group) : IModGroupEditDrawer +{ + public void Draw() + { + foreach (var (option, optionIdx) in group.OptionData.WithIndex()) + { + using var id = ImRaii.PushId(optionIdx); + editor.DrawOptionPosition(group, option, optionIdx); + + ImUtf8.SameLineInner(); + editor.DrawOptionDefaultMultiBehaviour(group, option, optionIdx); + + ImUtf8.SameLineInner(); + editor.DrawOptionName(option); + + ImUtf8.SameLineInner(); + editor.DrawOptionDescription(option); + + ImUtf8.SameLineInner(); + editor.DrawOptionDelete(option); + + ImUtf8.SameLineInner(); + editor.DrawOptionPriority(option); + } + + DrawNewOption(); + DrawConvertButton(); + } + + private void DrawConvertButton() + { + var g = group; + var e = editor.ModManager.OptionEditor.MultiEditor; + if (ImUtf8.Button("Convert to Single Group"u8, editor.AvailableWidth)) + editor.ActionQueue.Enqueue(() => e.ChangeToSingle(g)); + } + + private void DrawNewOption() + { + var count = group.Options.Count; + if (count >= IModGroup.MaxMultiOptions) + return; + + var name = editor.DrawNewOptionBase(group, count); + + var validName = name.Length > 0; + if (ImUtf8.IconButton(FontAwesomeIcon.Plus, validName + ? "Add a new option to this group."u8 + : "Please enter a name for the new option."u8, !validName)) + { + editor.ModManager.OptionEditor.MultiEditor.AddOption(group, name); + editor.NewOptionName = null; + } + } +} diff --git a/Penumbra/UI/ModsTab/Groups/SingleModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/SingleModGroupEditDrawer.cs new file mode 100644 index 00000000..75fbc63a --- /dev/null +++ b/Penumbra/UI/ModsTab/Groups/SingleModGroupEditDrawer.cs @@ -0,0 +1,68 @@ +using Dalamud.Interface; +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Text; +using Penumbra.Mods.Groups; + +namespace Penumbra.UI.ModsTab.Groups; + +public readonly struct SingleModGroupEditDrawer(ModGroupEditDrawer editor, SingleModGroup group) : IModGroupEditDrawer +{ + public void Draw() + { + foreach (var (option, optionIdx) in group.OptionData.WithIndex()) + { + using var id = ImRaii.PushId(optionIdx); + editor.DrawOptionPosition(group, option, optionIdx); + + ImUtf8.SameLineInner(); + editor.DrawOptionDefaultSingleBehaviour(group, option, optionIdx); + + ImUtf8.SameLineInner(); + editor.DrawOptionName(option); + + ImUtf8.SameLineInner(); + editor.DrawOptionDescription(option); + + ImUtf8.SameLineInner(); + editor.DrawOptionDelete(option); + + ImUtf8.SameLineInner(); + ImGui.Dummy(new Vector2(editor.PriorityWidth, 0)); + } + + DrawNewOption(); + DrawConvertButton(); + } + + private void DrawConvertButton() + { + var convertible = group.Options.Count <= IModGroup.MaxMultiOptions; + var g = group; + var e = editor.ModManager.OptionEditor.SingleEditor; + if (ImUtf8.ButtonEx("Convert to Multi Group", editor.AvailableWidth, !convertible)) + editor.ActionQueue.Enqueue(() => e.ChangeToMulti(g)); + if (!convertible) + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, + "Can not convert to multi group since maximum number of options is exceeded."u8); + } + + private void DrawNewOption() + { + var count = group.Options.Count; + if (count >= int.MaxValue) + return; + + var name = editor.DrawNewOptionBase(group, count); + + var validName = name.Length > 0; + if (ImUtf8.IconButton(FontAwesomeIcon.Plus, validName + ? "Add a new option to this group."u8 + : "Please enter a name for the new option."u8, !validName)) + { + editor.ModManager.OptionEditor.SingleEditor.AddOption(group, name); + editor.NewOptionName = null; + } + } +} diff --git a/Penumbra/UI/ModsTab/MetaManipulationDrawer.cs b/Penumbra/UI/ModsTab/MetaManipulationDrawer.cs new file mode 100644 index 00000000..1f2273b5 --- /dev/null +++ b/Penumbra/UI/ModsTab/MetaManipulationDrawer.cs @@ -0,0 +1,105 @@ +using ImGuiNET; +using OtterGui.Raii; +using OtterGui.Text; +using Penumbra.GameData.Enums; +using Penumbra.Meta.Manipulations; +using Penumbra.UI.Classes; + +namespace Penumbra.UI.ModsTab; + +public static class MetaManipulationDrawer +{ + public static bool DrawObjectType(ref ImcManipulation manip, float width = 110) + { + var ret = Combos.ImcType("##imcType", manip.ObjectType, out var type, width); + ImUtf8.HoverTooltip("Object Type"u8); + + if (ret) + { + var equipSlot = type switch + { + ObjectType.Equipment => manip.EquipSlot.IsEquipment() ? manip.EquipSlot : EquipSlot.Head, + ObjectType.DemiHuman => manip.EquipSlot.IsEquipment() ? manip.EquipSlot : EquipSlot.Head, + ObjectType.Accessory => manip.EquipSlot.IsAccessory() ? manip.EquipSlot : EquipSlot.Ears, + _ => EquipSlot.Unknown, + }; + manip = new ImcManipulation(type, manip.BodySlot, manip.PrimaryId, manip.SecondaryId == 0 ? 1 : manip.SecondaryId, + manip.Variant.Id, equipSlot, manip.Entry); + } + + return ret; + } + + public static bool DrawPrimaryId(ref ImcManipulation manip, float unscaledWidth = 80) + { + var ret = IdInput("##imcPrimaryId"u8, unscaledWidth, manip.PrimaryId.Id, out var newId, 0, ushort.MaxValue, + manip.PrimaryId.Id <= 1); + ImUtf8.HoverTooltip("Primary ID - You can usually find this as the 'x####' part of an item path.\n"u8 + + "This should generally not be left <= 1 unless you explicitly want that."u8); + if (ret) + manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, newId, manip.SecondaryId, manip.Variant.Id, manip.EquipSlot, + manip.Entry); + return ret; + } + + public static bool DrawSecondaryId(ref ImcManipulation manip, float unscaledWidth = 100) + { + var ret = IdInput("##imcSecondaryId"u8, unscaledWidth, manip.SecondaryId.Id, out var newId, 0, ushort.MaxValue, false); + ImUtf8.HoverTooltip("Secondary ID"u8); + if (ret) + manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, newId, manip.Variant.Id, manip.EquipSlot, + manip.Entry); + return ret; + } + + public static bool DrawVariant(ref ImcManipulation manip, float unscaledWidth = 45) + { + var ret = IdInput("##imcVariant"u8, unscaledWidth, manip.Variant.Id, out var newId, 0, byte.MaxValue, false); + ImUtf8.HoverTooltip("Variant ID"u8); + if (ret) + manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, manip.SecondaryId, (byte)newId, manip.EquipSlot, + manip.Entry); + return ret; + } + + public static bool DrawSlot(ref ImcManipulation manip, float unscaledWidth = 100) + { + bool ret; + EquipSlot slot; + switch (manip.ObjectType) + { + case ObjectType.Equipment: + case ObjectType.DemiHuman: + ret = Combos.EqpEquipSlot("##slot", manip.EquipSlot, out slot, unscaledWidth); + break; + case ObjectType.Accessory: + ret = Combos.AccessorySlot("##slot", manip.EquipSlot, out slot, unscaledWidth); + break; + default: return false; + } + + ImUtf8.HoverTooltip("Equip Slot"u8); + if (ret) + manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, manip.SecondaryId, manip.Variant.Id, slot, + manip.Entry); + return ret; + } + + /// + /// A number input for ids with an optional max id of given width. + /// Returns true if newId changed against currentId. + /// + private static bool IdInput(ReadOnlySpan label, float unscaledWidth, ushort currentId, out ushort newId, int minId, int maxId, + bool border) + { + int tmp = currentId; + ImGui.SetNextItemWidth(unscaledWidth * ImUtf8.GlobalScale); + using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, UiHelpers.Scale, border); + using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.RegexWarningBorder, border); + if (ImUtf8.InputScalar(label, ref tmp)) + tmp = Math.Clamp(tmp, minId, maxId); + + newId = (ushort)tmp; + return newId != currentId; + } +} diff --git a/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs deleted file mode 100644 index 4ef1577f..00000000 --- a/Penumbra/UI/ModsTab/ModGroupEditDrawer.cs +++ /dev/null @@ -1,687 +0,0 @@ -using Dalamud.Interface; -using Dalamud.Interface.Internal.Notifications; -using ImGuiNET; -using OtterGui; -using OtterGui.Classes; -using OtterGui.Raii; -using OtterGui.Services; -using OtterGui.Text; -using OtterGui.Text.EndObjects; -using Penumbra.GameData.Enums; -using Penumbra.GameData.Structs; -using Penumbra.Meta; -using Penumbra.Meta.Manipulations; -using Penumbra.Mods; -using Penumbra.Mods.Groups; -using Penumbra.Mods.Manager; -using Penumbra.Mods.Manager.OptionEditor; -using Penumbra.Mods.Settings; -using Penumbra.Mods.SubMods; -using Penumbra.Services; -using Penumbra.UI.Classes; - -namespace Penumbra.UI.ModsTab; - -public static class MetaManipulationDrawer -{ - public static bool DrawObjectType(ref ImcManipulation manip, float width = 110) - { - var ret = Combos.ImcType("##imcType", manip.ObjectType, out var type, width); - ImUtf8.HoverTooltip("Object Type"u8); - - if (ret) - { - var equipSlot = type switch - { - ObjectType.Equipment => manip.EquipSlot.IsEquipment() ? manip.EquipSlot : EquipSlot.Head, - ObjectType.DemiHuman => manip.EquipSlot.IsEquipment() ? manip.EquipSlot : EquipSlot.Head, - ObjectType.Accessory => manip.EquipSlot.IsAccessory() ? manip.EquipSlot : EquipSlot.Ears, - _ => EquipSlot.Unknown, - }; - manip = new ImcManipulation(type, manip.BodySlot, manip.PrimaryId, manip.SecondaryId == 0 ? 1 : manip.SecondaryId, - manip.Variant.Id, equipSlot, manip.Entry); - } - - return ret; - } - - public static bool DrawPrimaryId(ref ImcManipulation manip, float unscaledWidth = 80) - { - var ret = IdInput("##imcPrimaryId"u8, unscaledWidth, manip.PrimaryId.Id, out var newId, 0, ushort.MaxValue, - manip.PrimaryId.Id <= 1); - ImUtf8.HoverTooltip("Primary ID - You can usually find this as the 'x####' part of an item path.\n"u8 - + "This should generally not be left <= 1 unless you explicitly want that."u8); - if (ret) - manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, newId, manip.SecondaryId, manip.Variant.Id, manip.EquipSlot, - manip.Entry); - return ret; - } - - public static bool DrawSecondaryId(ref ImcManipulation manip, float unscaledWidth = 100) - { - var ret = IdInput("##imcSecondaryId"u8, unscaledWidth, manip.SecondaryId.Id, out var newId, 0, ushort.MaxValue, false); - ImUtf8.HoverTooltip("Secondary ID"u8); - if (ret) - manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, newId, manip.Variant.Id, manip.EquipSlot, - manip.Entry); - return ret; - } - - public static bool DrawVariant(ref ImcManipulation manip, float unscaledWidth = 45) - { - var ret = IdInput("##imcVariant"u8, unscaledWidth, manip.Variant.Id, out var newId, 0, byte.MaxValue, false); - ImUtf8.HoverTooltip("Variant ID"u8); - if (ret) - manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, manip.SecondaryId, (byte)newId, manip.EquipSlot, - manip.Entry); - return ret; - } - - public static bool DrawSlot(ref ImcManipulation manip, float unscaledWidth = 100) - { - bool ret; - EquipSlot slot; - switch (manip.ObjectType) - { - case ObjectType.Equipment: - case ObjectType.DemiHuman: - ret = Combos.EqpEquipSlot("##slot", manip.EquipSlot, out slot, unscaledWidth); - break; - case ObjectType.Accessory: - ret = Combos.AccessorySlot("##slot", manip.EquipSlot, out slot, unscaledWidth); - break; - default: return false; - } - - ImUtf8.HoverTooltip("Equip Slot"u8); - if (ret) - manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, manip.SecondaryId, manip.Variant.Id, slot, - manip.Entry); - return ret; - } - - /// - /// A number input for ids with an optional max id of given width. - /// Returns true if newId changed against currentId. - /// - private static bool IdInput(ReadOnlySpan label, float unscaledWidth, ushort currentId, out ushort newId, int minId, int maxId, - bool border) - { - int tmp = currentId; - ImGui.SetNextItemWidth(unscaledWidth * ImUtf8.GlobalScale); - using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, UiHelpers.Scale, border); - using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.RegexWarningBorder, border); - if (ImUtf8.InputScalar(label, ref tmp)) - tmp = Math.Clamp(tmp, minId, maxId); - - newId = (ushort)tmp; - return newId != currentId; - } -} - -public sealed class ModGroupEditDrawer( - ModManager modManager, - Configuration config, - FilenameService filenames, - DescriptionEditPopup descriptionPopup) : IUiService -{ - private static ReadOnlySpan DragDropLabel - => "##DragOption"u8; - - private Vector2 _buttonSize; - private Vector2 _availableWidth; - private float _priorityWidth; - private float _groupNameWidth; - private float _optionNameWidth; - private float _spacing; - private Vector2 _optionIdxSelectable; - private bool _deleteEnabled; - - private string? _currentGroupName; - private ModPriority? _currentGroupPriority; - private IModGroup? _currentGroupEdited; - private bool _isGroupNameValid = true; - - private string? _newOptionName; - private IModGroup? _newOptionGroup; - private readonly Queue _actionQueue = new(); - - private IModGroup? _dragDropGroup; - private IModOption? _dragDropOption; - - public void Draw(Mod mod) - { - PrepareStyle(); - - using var id = ImUtf8.PushId("##GroupEdit"u8); - foreach (var (group, groupIdx) in mod.Groups.WithIndex()) - DrawGroup(group, groupIdx); - - while (_actionQueue.TryDequeue(out var action)) - action.Invoke(); - } - - private void DrawGroup(IModGroup group, int idx) - { - using var id = ImUtf8.PushId(idx); - using var frame = ImRaii.FramedGroup($"Group #{idx + 1}"); - DrawGroupNameRow(group, idx); - switch (group) - { - case SingleModGroup s: - DrawSingleGroup(s); - break; - case MultiModGroup m: - DrawMultiGroup(m); - break; - case ImcModGroup i: - DrawImcGroup(i); - break; - } - } - - private void DrawGroupNameRow(IModGroup group, int idx) - { - DrawGroupName(group); - ImUtf8.SameLineInner(); - DrawGroupMoveButtons(group, idx); - ImUtf8.SameLineInner(); - DrawGroupOpenFile(group, idx); - ImUtf8.SameLineInner(); - DrawGroupDescription(group); - ImUtf8.SameLineInner(); - DrawGroupDelete(group); - ImUtf8.SameLineInner(); - DrawGroupPriority(group); - } - - private void DrawGroupName(IModGroup group) - { - var text = _currentGroupEdited == group ? _currentGroupName ?? group.Name : group.Name; - ImGui.SetNextItemWidth(_groupNameWidth); - using var border = ImRaii.PushFrameBorder(UiHelpers.ScaleX2, Colors.RegexWarningBorder, !_isGroupNameValid); - if (ImUtf8.InputText("##GroupName"u8, ref text)) - { - _currentGroupEdited = group; - _currentGroupName = text; - _isGroupNameValid = text == group.Name || ModGroupEditor.VerifyFileName(group.Mod, group, text, false); - } - - if (ImGui.IsItemDeactivated()) - { - if (_currentGroupName != null && _isGroupNameValid) - modManager.OptionEditor.RenameModGroup(group, _currentGroupName); - _currentGroupName = null; - _currentGroupEdited = null; - _isGroupNameValid = true; - } - - var tt = _isGroupNameValid - ? "Change the Group name."u8 - : "Current name can not be used for this group."u8; - ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, tt); - } - - private void DrawGroupDelete(IModGroup group) - { - if (ImUtf8.IconButton(FontAwesomeIcon.Trash, !_deleteEnabled)) - _actionQueue.Enqueue(() => modManager.OptionEditor.DeleteModGroup(group)); - - if (_deleteEnabled) - ImUtf8.HoverTooltip("Delete this option group."u8); - else - ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, - $"Delete this option group.\nHold {config.DeleteModModifier} while clicking to delete."); - } - - private void DrawGroupPriority(IModGroup group) - { - var priority = _currentGroupEdited == group - ? (_currentGroupPriority ?? group.Priority).Value - : group.Priority.Value; - ImGui.SetNextItemWidth(_priorityWidth); - if (ImGui.InputInt("##GroupPriority", ref priority, 0, 0)) - { - _currentGroupEdited = group; - _currentGroupPriority = new ModPriority(priority); - } - - if (ImGui.IsItemDeactivated()) - { - if (_currentGroupPriority.HasValue) - modManager.OptionEditor.ChangeGroupPriority(group, _currentGroupPriority.Value); - _currentGroupEdited = null; - _currentGroupPriority = null; - } - - ImGuiUtil.HoverTooltip("Group Priority"); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DrawGroupDescription(IModGroup group) - { - if (ImUtf8.IconButton(FontAwesomeIcon.Edit, "Edit group description."u8)) - descriptionPopup.Open(group); - } - - private void DrawGroupMoveButtons(IModGroup group, int idx) - { - var isFirst = idx == 0; - if (ImUtf8.IconButton(FontAwesomeIcon.ArrowUp, isFirst)) - _actionQueue.Enqueue(() => modManager.OptionEditor.MoveModGroup(group, idx - 1)); - - if (isFirst) - ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, "Can not move this group further upwards."u8); - else - ImUtf8.HoverTooltip($"Move this group up to group {idx}."); - - - ImUtf8.SameLineInner(); - var isLast = idx == group.Mod.Groups.Count - 1; - if (ImUtf8.IconButton(FontAwesomeIcon.ArrowDown, isLast)) - _actionQueue.Enqueue(() => modManager.OptionEditor.MoveModGroup(group, idx + 1)); - - if (isLast) - ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, "Can not move this group further downwards."u8); - else - ImUtf8.HoverTooltip($"Move this group down to group {idx + 2}."); - } - - private void DrawGroupOpenFile(IModGroup group, int idx) - { - var fileName = filenames.OptionGroupFile(group.Mod, idx, config.ReplaceNonAsciiOnImport); - var fileExists = File.Exists(fileName); - if (ImUtf8.IconButton(FontAwesomeIcon.FileExport, !fileExists)) - try - { - Process.Start(new ProcessStartInfo(fileName) { UseShellExecute = true }); - } - catch (Exception e) - { - Penumbra.Messager.NotificationMessage(e, "Could not open editor.", NotificationType.Error); - } - - if (fileExists) - ImUtf8.HoverTooltip($"Open the {group.Name} json file in the text editor of your choice."); - else - ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, $"The {group.Name} json file does not exist."); - } - - private void DrawSingleGroup(SingleModGroup group) - { - foreach (var (option, optionIdx) in group.OptionData.WithIndex()) - { - using var id = ImRaii.PushId(optionIdx); - DrawOptionPosition(group, option, optionIdx); - - ImUtf8.SameLineInner(); - DrawOptionDefaultSingleBehaviour(group, option, optionIdx); - - ImUtf8.SameLineInner(); - DrawOptionName(option); - - ImUtf8.SameLineInner(); - DrawOptionDescription(option); - - ImUtf8.SameLineInner(); - DrawOptionDelete(option); - - ImUtf8.SameLineInner(); - ImGui.Dummy(new Vector2(_priorityWidth, 0)); - } - - DrawNewOption(group); - var convertible = group.Options.Count <= IModGroup.MaxMultiOptions; - if (ImUtf8.ButtonEx("Convert to Multi Group", _availableWidth, !convertible)) - _actionQueue.Enqueue(() => modManager.OptionEditor.SingleEditor.ChangeToMulti(group)); - if (!convertible) - ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, - "Can not convert to multi group since maximum number of options is exceeded."u8); - } - - private void DrawMultiGroup(MultiModGroup group) - { - foreach (var (option, optionIdx) in group.OptionData.WithIndex()) - { - using var id = ImRaii.PushId(optionIdx); - DrawOptionPosition(group, option, optionIdx); - - ImUtf8.SameLineInner(); - DrawOptionDefaultMultiBehaviour(group, option, optionIdx); - - ImUtf8.SameLineInner(); - DrawOptionName(option); - - ImUtf8.SameLineInner(); - DrawOptionDescription(option); - - ImUtf8.SameLineInner(); - DrawOptionDelete(option); - - ImUtf8.SameLineInner(); - DrawOptionPriority(option); - } - - DrawNewOption(group); - if (ImUtf8.Button("Convert to Single Group"u8, _availableWidth)) - _actionQueue.Enqueue(() => modManager.OptionEditor.MultiEditor.ChangeToSingle(group)); - } - - private void DrawImcGroup(ImcModGroup group) - { - using (ImUtf8.Group()) - { - ImUtf8.Text("Object Type"u8); - if (group.ObjectType is ObjectType.Equipment or ObjectType.Accessory or ObjectType.DemiHuman) - ImUtf8.Text("Slot"u8); - ImUtf8.Text("Primary ID"); - if (group.ObjectType is not ObjectType.Equipment and not ObjectType.Accessory) - ImUtf8.Text("Secondary ID"); - ImUtf8.Text("Variant"u8); - - ImUtf8.TextFrameAligned("Material ID"u8); - ImUtf8.TextFrameAligned("Material Animation ID"u8); - ImUtf8.TextFrameAligned("Decal ID"u8); - ImUtf8.TextFrameAligned("VFX ID"u8); - ImUtf8.TextFrameAligned("Sound ID"u8); - ImUtf8.TextFrameAligned("Can Be Disabled"u8); - ImUtf8.TextFrameAligned("Default Attributes"u8); - } - - ImGui.SameLine(); - - var attributeCache = new ImcAttributeCache(group); - - using (ImUtf8.Group()) - { - ImUtf8.Text(group.ObjectType.ToName()); - if (group.ObjectType is ObjectType.Equipment or ObjectType.Accessory or ObjectType.DemiHuman) - ImUtf8.Text(group.EquipSlot.ToName()); - ImUtf8.Text($"{group.PrimaryId.Id}"); - if (group.ObjectType is not ObjectType.Equipment and not ObjectType.Accessory) - ImUtf8.Text($"{group.SecondaryId.Id}"); - ImUtf8.Text($"{group.Variant.Id}"); - - ImUtf8.TextFrameAligned($"{group.DefaultEntry.MaterialId}"); - ImUtf8.TextFrameAligned($"{group.DefaultEntry.MaterialAnimationId}"); - ImUtf8.TextFrameAligned($"{group.DefaultEntry.DecalId}"); - ImUtf8.TextFrameAligned($"{group.DefaultEntry.VfxId}"); - ImUtf8.TextFrameAligned($"{group.DefaultEntry.SoundId}"); - - var canBeDisabled = group.CanBeDisabled; - if (ImUtf8.Checkbox("##disabled"u8, ref canBeDisabled)) - modManager.OptionEditor.ImcEditor.ChangeCanBeDisabled(group, canBeDisabled, SaveType.Queue); - - var defaultDisabled = group.DefaultDisabled; - ImUtf8.SameLineInner(); - if (ImUtf8.Checkbox("##defaultDisabled"u8, ref defaultDisabled)) - modManager.OptionEditor.ChangeModGroupDefaultOption(group, - group.DefaultSettings.SetBit(ImcModGroup.DisabledIndex, defaultDisabled)); - - DrawAttributes(modManager.OptionEditor.ImcEditor, attributeCache, group.DefaultEntry.AttributeMask, group); - } - - - foreach (var (option, optionIdx) in group.OptionData.WithIndex()) - { - using var id = ImRaii.PushId(optionIdx); - DrawOptionPosition(group, option, optionIdx); - - ImUtf8.SameLineInner(); - DrawOptionDefaultMultiBehaviour(group, option, optionIdx); - - ImUtf8.SameLineInner(); - DrawOptionName(option); - - ImUtf8.SameLineInner(); - DrawOptionDescription(option); - - ImUtf8.SameLineInner(); - DrawOptionDelete(option); - - ImUtf8.SameLineInner(); - ImGui.Dummy(new Vector2(_priorityWidth, 0)); - - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + _optionIdxSelectable.X + ImUtf8.ItemInnerSpacing.X * 2 + ImUtf8.FrameHeight); - DrawAttributes(modManager.OptionEditor.ImcEditor, attributeCache, option.AttributeMask, option); - } - - DrawNewOption(group, attributeCache); - return; - - static void DrawAttributes(ImcModGroupEditor editor, in ImcAttributeCache cache, ushort mask, object data) - { - for (var i = 0; i < ImcEntry.NumAttributes; ++i) - { - using var id = ImRaii.PushId(i); - var value = (mask & (1 << i)) != 0; - using (ImRaii.Disabled(!cache.CanChange(i))) - { - if (ImUtf8.Checkbox(TerminatedByteString.Empty, ref value)) - { - if (data is ImcModGroup g) - editor.ChangeDefaultAttribute(g, cache, i, value); - else - editor.ChangeOptionAttribute((ImcSubMod)data, cache, i, value); - } - } - - ImUtf8.HoverTooltip($"{(char)('A' + i)}"); - if (i != 9) - ImUtf8.SameLineInner(); - } - } - } - - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DrawOptionPosition(IModGroup group, IModOption option, int optionIdx) - { - ImGui.AlignTextToFramePadding(); - ImUtf8.Selectable($"Option #{optionIdx + 1}", false, size: _optionIdxSelectable); - Target(group, optionIdx); - Source(option); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DrawOptionDefaultSingleBehaviour(IModGroup group, IModOption option, int optionIdx) - { - var isDefaultOption = group.DefaultSettings.AsIndex == optionIdx; - if (ImUtf8.RadioButton("##default"u8, isDefaultOption)) - modManager.OptionEditor.ChangeModGroupDefaultOption(group, Setting.Single(optionIdx)); - ImUtf8.HoverTooltip($"Set {option.Name} as the default choice for this group."); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DrawOptionDefaultMultiBehaviour(IModGroup group, IModOption option, int optionIdx) - { - var isDefaultOption = group.DefaultSettings.HasFlag(optionIdx); - if (ImUtf8.Checkbox("##default"u8, ref isDefaultOption)) - modManager.OptionEditor.ChangeModGroupDefaultOption(group, group.DefaultSettings.SetBit(optionIdx, isDefaultOption)); - ImUtf8.HoverTooltip($"{(isDefaultOption ? "Disable"u8 : "Enable"u8)} {option.Name} per default in this group."); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DrawOptionDescription(IModOption option) - { - if (ImUtf8.IconButton(FontAwesomeIcon.Edit, "Edit option description."u8)) - descriptionPopup.Open(option); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DrawOptionPriority(MultiSubMod option) - { - var priority = option.Priority.Value; - ImGui.SetNextItemWidth(_priorityWidth); - if (ImUtf8.InputScalarOnDeactivated("##Priority"u8, ref priority)) - modManager.OptionEditor.MultiEditor.ChangeOptionPriority(option, new ModPriority(priority)); - ImUtf8.HoverTooltip("Option priority inside the mod."u8); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DrawOptionName(IModOption option) - { - var name = option.Name; - ImGui.SetNextItemWidth(_optionNameWidth); - if (ImUtf8.InputTextOnDeactivated("##Name"u8, ref name)) - modManager.OptionEditor.RenameOption(option, name); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DrawOptionDelete(IModOption option) - { - if (ImUtf8.IconButton(FontAwesomeIcon.Trash, !_deleteEnabled)) - _actionQueue.Enqueue(() => modManager.OptionEditor.DeleteOption(option)); - - if (_deleteEnabled) - ImUtf8.HoverTooltip("Delete this option."u8); - else - ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, - $"Delete this option.\nHold {config.DeleteModModifier} while clicking to delete."); - } - - private void DrawNewOption(SingleModGroup group) - { - var count = group.Options.Count; - if (count >= int.MaxValue) - return; - - var name = DrawNewOptionBase(group, count); - - var validName = name.Length > 0; - if (ImUtf8.IconButton(FontAwesomeIcon.Plus, validName - ? "Add a new option to this group."u8 - : "Please enter a name for the new option."u8, !validName)) - { - modManager.OptionEditor.SingleEditor.AddOption(group, name); - _newOptionName = null; - } - } - - private void DrawNewOption(MultiModGroup group) - { - var count = group.Options.Count; - if (count >= IModGroup.MaxMultiOptions) - return; - - var name = DrawNewOptionBase(group, count); - - var validName = name.Length > 0; - if (ImUtf8.IconButton(FontAwesomeIcon.Plus, validName - ? "Add a new option to this group."u8 - : "Please enter a name for the new option."u8, !validName)) - { - modManager.OptionEditor.MultiEditor.AddOption(group, name); - _newOptionName = null; - } - } - - private void DrawNewOption(ImcModGroup group, in ImcAttributeCache cache) - { - if (cache.LowestUnsetMask == 0) - return; - - var name = DrawNewOptionBase(group, group.Options.Count); - var validName = name.Length > 0; - if (ImUtf8.IconButton(FontAwesomeIcon.Plus, validName - ? "Add a new option to this group."u8 - : "Please enter a name for the new option."u8, !validName)) - { - modManager.OptionEditor.ImcEditor.AddOption(group, cache, name); - _newOptionName = null; - } - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private string DrawNewOptionBase(IModGroup group, int count) - { - ImUtf8.Selectable($"Option #{count + 1}", false, size: _optionIdxSelectable); - Target(group, count); - - ImUtf8.SameLineInner(); - ImUtf8.IconDummy(); - - ImUtf8.SameLineInner(); - ImGui.SetNextItemWidth(_optionNameWidth); - var newName = _newOptionGroup == group - ? _newOptionName ?? string.Empty - : string.Empty; - if (ImUtf8.InputText("##newOption"u8, ref newName, "Add new option..."u8)) - { - _newOptionName = newName; - _newOptionGroup = group; - } - - ImUtf8.SameLineInner(); - return newName; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void Source(IModOption option) - { - if (option.Group is not ITexToolsGroup) - return; - - using var source = ImUtf8.DragDropSource(); - if (!source) - return; - - if (!DragDropSource.SetPayload(DragDropLabel)) - { - _dragDropGroup = option.Group; - _dragDropOption = option; - } - - ImGui.TextUnformatted($"Dragging option {option.Name} from group {option.Group.Name}..."); - } - - private void Target(IModGroup group, int optionIdx) - { - if (group is not ITexToolsGroup) - return; - - if (_dragDropGroup != group && _dragDropGroup != null && group is MultiModGroup { Options.Count: >= IModGroup.MaxMultiOptions }) - return; - - using var target = ImRaii.DragDropTarget(); - if (!target.Success || !DragDropTarget.CheckPayload(DragDropLabel)) - return; - - if (_dragDropGroup != null && _dragDropOption != null) - { - if (_dragDropGroup == group) - { - var sourceOption = _dragDropOption; - _actionQueue.Enqueue(() => modManager.OptionEditor.MoveOption(sourceOption, optionIdx)); - } - else - { - // Move from one group to another by deleting, then adding, then moving the option. - var sourceOption = _dragDropOption; - _actionQueue.Enqueue(() => - { - modManager.OptionEditor.DeleteOption(sourceOption); - if (modManager.OptionEditor.AddOption(group, sourceOption) is { } newOption) - modManager.OptionEditor.MoveOption(newOption, optionIdx); - }); - } - } - - _dragDropGroup = null; - _dragDropOption = null; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void PrepareStyle() - { - var totalWidth = 400f * ImUtf8.GlobalScale; - _buttonSize = new Vector2(ImUtf8.FrameHeight); - _priorityWidth = 50 * ImUtf8.GlobalScale; - _availableWidth = new Vector2(totalWidth + 3 * _spacing + 2 * _buttonSize.X + _priorityWidth, 0); - _groupNameWidth = totalWidth - 3 * (_buttonSize.X + _spacing); - _spacing = ImGui.GetStyle().ItemInnerSpacing.X; - _optionIdxSelectable = ImUtf8.CalcTextSize("Option #88."u8); - _optionNameWidth = totalWidth - _optionIdxSelectable.X - _buttonSize.X - 2 * _spacing; - _deleteEnabled = config.DeleteModModifier.IsActive(); - } -} diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index b7951c49..125f539e 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -13,6 +13,7 @@ using Penumbra.Services; using Penumbra.UI.AdvancedWindow; using Penumbra.Mods.Settings; using Penumbra.Mods.Manager.OptionEditor; +using Penumbra.UI.ModsTab.Groups; namespace Penumbra.UI.ModsTab; diff --git a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs index 107a2d04..7e3b8a95 100644 --- a/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelSettingsTab.cs @@ -9,6 +9,7 @@ using Penumbra.Collections.Manager; using Penumbra.Mods.Manager; using Penumbra.Services; using Penumbra.Mods.Settings; +using Penumbra.UI.ModsTab.Groups; namespace Penumbra.UI.ModsTab; From c06d5b08715924aba67910f7e0fe718b4f83b274 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 23 May 2024 16:57:16 +0200 Subject: [PATCH 159/433] Update for new gamedata. --- Penumbra.GameData | 2 +- Penumbra/Import/TexToolsMeta.Deserialization.cs | 4 +--- Penumbra/Meta/Files/CmpFile.cs | 6 +++--- Penumbra/Meta/Files/EqpGmpFile.cs | 6 +++--- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 5fa4d0e7..e8220a0a 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 5fa4d0e7972423b73f8cf569bb2bfbeddd825c8a +Subproject commit e8220a0a74e9480330e98ed7ca462353434b9649 diff --git a/Penumbra/Import/TexToolsMeta.Deserialization.cs b/Penumbra/Import/TexToolsMeta.Deserialization.cs index 325c9143..f062ae25 100644 --- a/Penumbra/Import/TexToolsMeta.Deserialization.cs +++ b/Penumbra/Import/TexToolsMeta.Deserialization.cs @@ -57,9 +57,7 @@ public partial class TexToolsMeta if (data == null) return; - using var reader = new BinaryReader(new MemoryStream(data)); - var value = (GmpEntry)reader.ReadUInt32(); - value.UnknownTotal = reader.ReadByte(); + var value = GmpEntry.FromTexToolsMeta(data.AsSpan(0, 5)); var def = ExpandedGmpFile.GetDefault(_metaFileManager, metaFileInfo.PrimaryId); if (_keepDefault || value != def) MetaManipulations.Add(new GmpManipulation(value, metaFileInfo.PrimaryId)); diff --git a/Penumbra/Meta/Files/CmpFile.cs b/Penumbra/Meta/Files/CmpFile.cs index 8a6040ec..b265a5e8 100644 --- a/Penumbra/Meta/Files/CmpFile.cs +++ b/Penumbra/Meta/Files/CmpFile.cs @@ -19,8 +19,8 @@ public sealed unsafe class CmpFile : MetaBaseFile public float this[SubRace subRace, RspAttribute attribute] { - get => *(float*)(Data + RacialScalingStart + ToRspIndex(subRace) * RspEntry.ByteSize + (int)attribute * 4); - set => *(float*)(Data + RacialScalingStart + ToRspIndex(subRace) * RspEntry.ByteSize + (int)attribute * 4) = value; + get => *(float*)(Data + RacialScalingStart + ToRspIndex(subRace) * RspData.ByteSize + (int)attribute * 4); + set => *(float*)(Data + RacialScalingStart + ToRspIndex(subRace) * RspData.ByteSize + (int)attribute * 4) = value; } public override void Reset() @@ -42,7 +42,7 @@ public sealed unsafe class CmpFile : MetaBaseFile public static float GetDefault(MetaFileManager manager, SubRace subRace, RspAttribute attribute) { var data = (byte*)manager.CharacterUtility.DefaultResource(InternalIndex).Address; - return *(float*)(data + RacialScalingStart + ToRspIndex(subRace) * RspEntry.ByteSize + (int)attribute * 4); + return *(float*)(data + RacialScalingStart + ToRspIndex(subRace) * RspData.ByteSize + (int)attribute * 4); } private static int ToRspIndex(SubRace subRace) diff --git a/Penumbra/Meta/Files/EqpGmpFile.cs b/Penumbra/Meta/Files/EqpGmpFile.cs index 97f57703..70067c2b 100644 --- a/Penumbra/Meta/Files/EqpGmpFile.cs +++ b/Penumbra/Meta/Files/EqpGmpFile.cs @@ -157,12 +157,12 @@ public sealed class ExpandedGmpFile : ExpandedEqpGmpBase, IEnumerable public GmpEntry this[PrimaryId idx] { - get => (GmpEntry)GetInternal(idx); - set => SetInternal(idx, (ulong)value); + get => new() { Value = GetInternal(idx) }; + set => SetInternal(idx, value.Value); } public static GmpEntry GetDefault(MetaFileManager manager, PrimaryId primaryIdx) - => (GmpEntry)GetDefaultInternal(manager, InternalIndex, primaryIdx, (ulong)GmpEntry.Default); + => new() { Value = GetDefaultInternal(manager, InternalIndex, primaryIdx, GmpEntry.Default.Value) }; public void Reset(IEnumerable entries) { From dfdd5167a84c5a9c1aeb5280eb3fb91d895a613b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 23 May 2024 17:24:42 +0200 Subject: [PATCH 160/433] Remove auto descriptions from newly generated option groups. --- Penumbra/Mods/Groups/ImcModGroup.cs | 2 +- Penumbra/Mods/Groups/MultiModGroup.cs | 2 +- Penumbra/Mods/Groups/SingleModGroup.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs index d2c41f34..cf228889 100644 --- a/Penumbra/Mods/Groups/ImcModGroup.cs +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -22,7 +22,7 @@ public class ImcModGroup(Mod mod) : IModGroup public Mod Mod { get; } = mod; public string Name { get; set; } = "Option"; - public string Description { get; set; } = "A single IMC manipulation."; + public string Description { get; set; } = string.Empty; public GroupType Type => GroupType.Imc; diff --git a/Penumbra/Mods/Groups/MultiModGroup.cs b/Penumbra/Mods/Groups/MultiModGroup.cs index 7fc9acb3..38c0ef15 100644 --- a/Penumbra/Mods/Groups/MultiModGroup.cs +++ b/Penumbra/Mods/Groups/MultiModGroup.cs @@ -25,7 +25,7 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup public Mod Mod { get; } = mod; public string Name { get; set; } = "Group"; - public string Description { get; set; } = "A non-exclusive group of settings."; + public string Description { get; set; } = string.Empty; public ModPriority Priority { get; set; } public Setting DefaultSettings { get; set; } public readonly List OptionData = []; diff --git a/Penumbra/Mods/Groups/SingleModGroup.cs b/Penumbra/Mods/Groups/SingleModGroup.cs index 4eec0746..49190e34 100644 --- a/Penumbra/Mods/Groups/SingleModGroup.cs +++ b/Penumbra/Mods/Groups/SingleModGroup.cs @@ -23,7 +23,7 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup public Mod Mod { get; } = mod; public string Name { get; set; } = "Option"; - public string Description { get; set; } = "A mutually exclusive group of settings."; + public string Description { get; set; } = string.Empty; public ModPriority Priority { get; set; } public Setting DefaultSettings { get; set; } From 7df9ddcb995b1f4b86abcfb827a63fb5488e6c1f Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 23 May 2024 17:25:27 +0200 Subject: [PATCH 161/433] Re-Add button to open default mod json. --- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index 125f539e..468e97b9 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -35,8 +35,8 @@ public class ModPanelEditTab( { private readonly TagButtons _modTags = new(); - private ModFileSystem.Leaf _leaf = null!; - private Mod _mod = null!; + private ModFileSystem.Leaf _leaf = null!; + private Mod _mod = null!; public ReadOnlySpan Label => "Edit Mod"u8; @@ -193,6 +193,7 @@ public class ModPanelEditTab( if (ImGui.Button("Edit Description", reducedSize)) descriptionPopup.Open(_mod); + ImGui.SameLine(); var fileExists = File.Exists(filenames.ModMetaPath(_mod)); var tt = fileExists @@ -201,8 +202,22 @@ public class ModPanelEditTab( if (ImGuiUtil.DrawDisabledButton($"{FontAwesomeIcon.FileExport.ToIconString()}##metaFile", UiHelpers.IconButtonSize, tt, !fileExists, true)) Process.Start(new ProcessStartInfo(filenames.ModMetaPath(_mod)) { UseShellExecute = true }); + + DrawOpenDefaultMod(); } + private void DrawOpenDefaultMod() + { + var file = filenames.OptionGroupFile(_mod, -1, false); + var fileExists = File.Exists(file); + var tt = fileExists + ? "Open the default mod data file in the text editor of your choice." + : "The default mod data file does not exist."; + if (ImGuiUtil.DrawDisabledButton("Open Default Data", UiHelpers.InputTextWidth, tt, !fileExists)) + Process.Start(new ProcessStartInfo(file) { UseShellExecute = true }); + } + + /// A text input for the new directory name and a button to apply the move. private static class MoveDirectory { From fca1bf9d946dc505c1834ccef6feb206ee3039f4 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 23 May 2024 22:30:42 +0200 Subject: [PATCH 162/433] Add ImcIdentifier. --- .../Meta/Manipulations/IMetaIdentifier.cs | 16 ++ Penumbra/Meta/Manipulations/Imc.cs | 187 ++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 Penumbra/Meta/Manipulations/IMetaIdentifier.cs create mode 100644 Penumbra/Meta/Manipulations/Imc.cs diff --git a/Penumbra/Meta/Manipulations/IMetaIdentifier.cs b/Penumbra/Meta/Manipulations/IMetaIdentifier.cs new file mode 100644 index 00000000..4ad6bd3d --- /dev/null +++ b/Penumbra/Meta/Manipulations/IMetaIdentifier.cs @@ -0,0 +1,16 @@ +using Newtonsoft.Json.Linq; +using Penumbra.GameData.Data; +using Penumbra.Interop.Structs; + +namespace Penumbra.Meta.Manipulations; + +public interface IMetaIdentifier +{ + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems); + + public MetaIndex FileIndex(); + + public bool Validate(); + + public JObject AddToJson(JObject jObj); +} diff --git a/Penumbra/Meta/Manipulations/Imc.cs b/Penumbra/Meta/Manipulations/Imc.cs new file mode 100644 index 00000000..f0101be2 --- /dev/null +++ b/Penumbra/Meta/Manipulations/Imc.cs @@ -0,0 +1,187 @@ +using Penumbra.GameData.Data; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Interop.Structs; +using Penumbra.String.Classes; + +namespace Penumbra.Meta.Manipulations; + +public readonly record struct ImcIdentifier( + PrimaryId PrimaryId, + Variant Variant, + ObjectType ObjectType, + SecondaryId SecondaryId, + EquipSlot EquipSlot, + BodySlot BodySlot) : IMetaIdentifier, IComparable +{ + public ImcIdentifier(EquipSlot slot, PrimaryId primaryId, ushort variant) + : this(primaryId, (Variant)Math.Clamp(variant, (ushort)0, byte.MaxValue), + slot.IsAccessory() ? ObjectType.Accessory : ObjectType.Equipment, 0, slot, + variant > byte.MaxValue ? BodySlot.Body : BodySlot.Unknown) + { } + + public ImcIdentifier(EquipSlot slot, PrimaryId primaryId, Variant variant) + : this(primaryId, variant, slot.IsAccessory() ? ObjectType.Accessory : ObjectType.Equipment, 0, slot, BodySlot.Unknown) + { } + + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + { + var path = ObjectType switch + { + ObjectType.Equipment or ObjectType.Accessory => GamePaths.Equipment.Mtrl.Path(PrimaryId, GenderRace.MidlanderMale, EquipSlot, + Variant, + "a"), + ObjectType.Weapon => GamePaths.Weapon.Mtrl.Path(PrimaryId, SecondaryId.Id, Variant, "a"), + ObjectType.DemiHuman => GamePaths.DemiHuman.Mtrl.Path(PrimaryId, SecondaryId.Id, EquipSlot, Variant, + "a"), + ObjectType.Monster => GamePaths.Monster.Mtrl.Path(PrimaryId, SecondaryId.Id, Variant, "a"), + _ => string.Empty, + }; + if (path.Length == 0) + return; + + identifier.Identify(changedItems, path); + } + + public Utf8GamePath GamePath() + { + return ObjectType switch + { + ObjectType.Accessory => Utf8GamePath.FromString(GamePaths.Accessory.Imc.Path(PrimaryId), out var p) ? p : Utf8GamePath.Empty, + ObjectType.Equipment => Utf8GamePath.FromString(GamePaths.Equipment.Imc.Path(PrimaryId), out var p) ? p : Utf8GamePath.Empty, + ObjectType.DemiHuman => Utf8GamePath.FromString(GamePaths.DemiHuman.Imc.Path(PrimaryId, SecondaryId.Id), out var p) + ? p + : Utf8GamePath.Empty, + ObjectType.Monster => Utf8GamePath.FromString(GamePaths.Monster.Imc.Path(PrimaryId, SecondaryId.Id), out var p) + ? p + : Utf8GamePath.Empty, + ObjectType.Weapon => Utf8GamePath.FromString(GamePaths.Weapon.Imc.Path(PrimaryId, SecondaryId.Id), out var p) + ? p + : Utf8GamePath.Empty, + _ => throw new NotImplementedException(), + }; + } + + public MetaIndex FileIndex() + => (MetaIndex)(-1); + + public override string ToString() + => ObjectType is ObjectType.Equipment or ObjectType.Accessory + ? $"Imc - {PrimaryId} - {EquipSlot.ToName()} - {Variant}" + : $"Imc - {PrimaryId} - {ObjectType.ToName()} - {SecondaryId} - {BodySlot} - {Variant}"; + + public bool Validate() + { + switch (ObjectType) + { + case ObjectType.Accessory: + case ObjectType.Equipment: + if (BodySlot is not BodySlot.Unknown) + return false; + if (!EquipSlot.IsEquipment() && !EquipSlot.IsAccessory()) + return false; + if (SecondaryId != 0) + return false; + + break; + case ObjectType.DemiHuman: + if (BodySlot is not BodySlot.Unknown) + return false; + if (!EquipSlot.IsEquipment() && !EquipSlot.IsAccessory()) + return false; + + break; + default: + if (!Enum.IsDefined(BodySlot)) + return false; + if (EquipSlot is not EquipSlot.Unknown) + return false; + if (!Enum.IsDefined(ObjectType)) + return false; + + break; + } + + return true; + } + + public int CompareTo(ImcIdentifier other) + { + var o = ObjectType.CompareTo(other.ObjectType); + if (o != 0) + return o; + + var i = PrimaryId.Id.CompareTo(other.PrimaryId.Id); + if (i != 0) + return i; + + if (ObjectType is ObjectType.Equipment or ObjectType.Accessory) + { + var e = EquipSlot.CompareTo(other.EquipSlot); + return e != 0 ? e : Variant.Id.CompareTo(other.Variant.Id); + } + + if (ObjectType is ObjectType.DemiHuman) + { + var e = EquipSlot.CompareTo(other.EquipSlot); + if (e != 0) + return e; + } + + var s = SecondaryId.Id.CompareTo(other.SecondaryId.Id); + if (s != 0) + return s; + + var b = BodySlot.CompareTo(other.BodySlot); + return b != 0 ? b : Variant.Id.CompareTo(other.Variant.Id); + } + + public static ImcIdentifier? FromJson(JObject jObj) + { + var objectType = jObj["PrimaryId"]?.ToObject() ?? ObjectType.Unknown; + var primaryId = new PrimaryId(jObj["PrimaryId"]?.ToObject() ?? 0); + var variant = jObj["Variant"]?.ToObject() ?? 0; + if (variant > byte.MaxValue) + return null; + + ImcIdentifier ret; + switch (objectType) + { + case ObjectType.Equipment: + case ObjectType.Accessory: + { + var slot = jObj["EquipSlot"]?.ToObject() ?? EquipSlot.Unknown; + ret = new ImcIdentifier(slot, primaryId, variant); + break; + } + case ObjectType.DemiHuman: + { + var secondaryId = new SecondaryId(jObj["SecondaryId"]?.ToObject() ?? 0); + var slot = jObj["Slot"]?.ToObject() ?? EquipSlot.Unknown; + ret = new ImcIdentifier(primaryId, (Variant)variant, objectType, secondaryId, slot, BodySlot.Unknown); + break; + } + + case ObjectType.Monster: + case ObjectType.Weapon: + { + var secondaryId = new SecondaryId(jObj["SecondaryId"]?.ToObject() ?? 0); + ret = new ImcIdentifier(primaryId, (Variant)variant, objectType, secondaryId, EquipSlot.Unknown, BodySlot.Body); + break; + } + default: return null; + } + + return ret.Validate() ? ret : null; + } + + public JObject AddToJson(JObject jObj) + { + var (gender, race) = GenderRace.Split(); + jObj["Gender"] = gender.ToString(); + jObj["Race"] = race.ToString(); + jObj["SetId"] = SetId.Id.ToString(); + jObj["Slot"] = Slot.ToString(); + return jObj; + } +} From 992cdff58d3a82c64dc166147de7de4a10be87f6 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 23 May 2024 22:31:39 +0200 Subject: [PATCH 163/433] Improve some IMC things. --- OtterGui | 2 +- Penumbra/Meta/Manipulations/Imc.cs | 14 +- .../UI/AdvancedWindow/ModEditWindow.Meta.cs | 92 +++----- Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs | 18 +- Penumbra/UI/ModsTab/ImcManipulationDrawer.cs | 221 ++++++++++++++++++ Penumbra/UI/ModsTab/MetaManipulationDrawer.cs | 105 --------- 6 files changed, 267 insertions(+), 185 deletions(-) create mode 100644 Penumbra/UI/ModsTab/ImcManipulationDrawer.cs delete mode 100644 Penumbra/UI/ModsTab/MetaManipulationDrawer.cs diff --git a/OtterGui b/OtterGui index 462acb87..1d936516 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 462acb87099650019996e4306d18cc70f76ca576 +Subproject commit 1d9365164655a7cb38172e1311e15e19b1def6db diff --git a/Penumbra/Meta/Manipulations/Imc.cs b/Penumbra/Meta/Manipulations/Imc.cs index f0101be2..9b123df1 100644 --- a/Penumbra/Meta/Manipulations/Imc.cs +++ b/Penumbra/Meta/Manipulations/Imc.cs @@ -1,3 +1,4 @@ +using Newtonsoft.Json.Linq; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; @@ -134,7 +135,7 @@ public readonly record struct ImcIdentifier( var b = BodySlot.CompareTo(other.BodySlot); return b != 0 ? b : Variant.Id.CompareTo(other.Variant.Id); - } + } public static ImcIdentifier? FromJson(JObject jObj) { @@ -177,11 +178,12 @@ public readonly record struct ImcIdentifier( public JObject AddToJson(JObject jObj) { - var (gender, race) = GenderRace.Split(); - jObj["Gender"] = gender.ToString(); - jObj["Race"] = race.ToString(); - jObj["SetId"] = SetId.Id.ToString(); - jObj["Slot"] = Slot.ToString(); + jObj["ObjectType"] = ObjectType.ToString(); + jObj["PrimaryId"] = PrimaryId.Id; + jObj["PrimaryId"] = SecondaryId.Id; + jObj["Variant"] = Variant.Id; + jObj["EquipSlot"] = EquipSlot.ToString(); + jObj["BodySlot"] = BodySlot.ToString(); return jObj; } } diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs index 55125375..99889360 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs @@ -352,26 +352,26 @@ public partial class ModEditWindow // Identifier ImGui.TableNextColumn(); - var change = MetaManipulationDrawer.DrawObjectType(ref _new); + var change = ImcManipulationDrawer.DrawObjectType(ref _new); ImGui.TableNextColumn(); - change |= MetaManipulationDrawer.DrawPrimaryId(ref _new); + change |= ImcManipulationDrawer.DrawPrimaryId(ref _new); using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, new Vector2(3 * UiHelpers.Scale, ImGui.GetStyle().ItemSpacing.Y)); ImGui.TableNextColumn(); // Equipment and accessories are slightly different imcs than other types. if (_new.ObjectType is ObjectType.Equipment or ObjectType.Accessory) - change |= MetaManipulationDrawer.DrawSlot(ref _new); + change |= ImcManipulationDrawer.DrawSlot(ref _new); else - change |= MetaManipulationDrawer.DrawSecondaryId(ref _new); + change |= ImcManipulationDrawer.DrawSecondaryId(ref _new); ImGui.TableNextColumn(); - change |= MetaManipulationDrawer.DrawVariant(ref _new); + change |= ImcManipulationDrawer.DrawVariant(ref _new); ImGui.TableNextColumn(); if (_new.ObjectType is ObjectType.DemiHuman) - change |= MetaManipulationDrawer.DrawSlot(ref _new, 70); + change |= ImcManipulationDrawer.DrawSlot(ref _new, 70); else ImGui.Dummy(new Vector2(70 * UiHelpers.Scale, 0)); @@ -379,32 +379,20 @@ public partial class ModEditWindow _new = _new.Copy(GetDefault(metaFileManager, _new) ?? new ImcEntry()); // Values using var disabled = ImRaii.Disabled(); - ImGui.TableNextColumn(); - IntDragInput("##imcMaterialId", "Material ID", SmallIdWidth, defaultEntry.Value.MaterialId, defaultEntry.Value.MaterialId, out _, - 1, byte.MaxValue, 0f); - ImGui.SameLine(); - IntDragInput("##imcMaterialAnimId", "Material Animation ID", SmallIdWidth, defaultEntry.Value.MaterialAnimationId, - defaultEntry.Value.MaterialAnimationId, out _, 0, byte.MaxValue, 0.01f); - ImGui.TableNextColumn(); - IntDragInput("##imcDecalId", "Decal ID", SmallIdWidth, defaultEntry.Value.DecalId, defaultEntry.Value.DecalId, out _, 0, - byte.MaxValue, 0f); - ImGui.SameLine(); - IntDragInput("##imcVfxId", "VFX ID", SmallIdWidth, defaultEntry.Value.VfxId, defaultEntry.Value.VfxId, out _, 0, byte.MaxValue, - 0f); - ImGui.SameLine(); - IntDragInput("##imcSoundId", "Sound ID", SmallIdWidth, defaultEntry.Value.SoundId, defaultEntry.Value.SoundId, out _, 0, 0b111111, - 0f); - ImGui.TableNextColumn(); - for (var i = 0; i < 10; ++i) - { - using var id = ImRaii.PushId(i); - var flag = 1 << i; - Checkmark("##attribute", $"{(char)('A' + i)}", (defaultEntry.Value.AttributeMask & flag) != 0, - (defaultEntry.Value.AttributeMask & flag) != 0, out _); - ImGui.SameLine(); - } - ImGui.NewLine(); + var entry = defaultEntry.Value; + ImGui.TableNextColumn(); + ImcManipulationDrawer.DrawMaterialId(entry, ref entry, false); + ImGui.SameLine(); + ImcManipulationDrawer.DrawMaterialAnimationId(entry, ref entry, false); + ImGui.TableNextColumn(); + ImcManipulationDrawer.DrawDecalId(entry, ref entry, false); + ImGui.SameLine(); + ImcManipulationDrawer.DrawVfxId(entry, ref entry, false); + ImGui.SameLine(); + ImcManipulationDrawer.DrawSoundId(entry, ref entry, false); + ImGui.TableNextColumn(); + ImcManipulationDrawer.DrawAttributes(entry, ref entry); } public static void Draw(MetaFileManager metaFileManager, ImcManipulation meta, ModEditor editor, Vector2 iconSize) @@ -452,46 +440,22 @@ public partial class ModEditWindow new Vector2(3 * UiHelpers.Scale, ImGui.GetStyle().ItemSpacing.Y)); ImGui.TableNextColumn(); var defaultEntry = GetDefault(metaFileManager, meta) ?? new ImcEntry(); - if (IntDragInput("##imcMaterialId", $"Material ID\nDefault Value: {defaultEntry.MaterialId}", SmallIdWidth, meta.Entry.MaterialId, - defaultEntry.MaterialId, out var materialId, 1, byte.MaxValue, 0.01f)) - editor.MetaEditor.Change(meta.Copy(meta.Entry with { MaterialId = (byte)materialId })); + var newEntry = meta.Entry; + var changes = ImcManipulationDrawer.DrawMaterialId(defaultEntry, ref newEntry, true); ImGui.SameLine(); - if (IntDragInput("##imcMaterialAnimId", $"Material Animation ID\nDefault Value: {defaultEntry.MaterialAnimationId}", SmallIdWidth, - meta.Entry.MaterialAnimationId, defaultEntry.MaterialAnimationId, out var materialAnimId, 0, byte.MaxValue, 0.01f)) - editor.MetaEditor.Change(meta.Copy(meta.Entry with { MaterialAnimationId = (byte)materialAnimId })); - + changes |= ImcManipulationDrawer.DrawMaterialAnimationId(defaultEntry, ref newEntry, true); ImGui.TableNextColumn(); - if (IntDragInput("##imcDecalId", $"Decal ID\nDefault Value: {defaultEntry.DecalId}", SmallIdWidth, meta.Entry.DecalId, - defaultEntry.DecalId, out var decalId, 0, byte.MaxValue, 0.01f)) - editor.MetaEditor.Change(meta.Copy(meta.Entry with { DecalId = (byte)decalId })); - + changes |= ImcManipulationDrawer.DrawDecalId(defaultEntry, ref newEntry, true); ImGui.SameLine(); - if (IntDragInput("##imcVfxId", $"VFX ID\nDefault Value: {defaultEntry.VfxId}", SmallIdWidth, meta.Entry.VfxId, defaultEntry.VfxId, - out var vfxId, 0, byte.MaxValue, 0.01f)) - editor.MetaEditor.Change(meta.Copy(meta.Entry with { VfxId = (byte)vfxId })); - + changes |= ImcManipulationDrawer.DrawVfxId(defaultEntry, ref newEntry, true); ImGui.SameLine(); - if (IntDragInput("##imcSoundId", $"Sound ID\nDefault Value: {defaultEntry.SoundId}", SmallIdWidth, meta.Entry.SoundId, - defaultEntry.SoundId, out var soundId, 0, 0b111111, 0.01f)) - editor.MetaEditor.Change(meta.Copy(meta.Entry with { SoundId = (byte)soundId })); - + changes |= ImcManipulationDrawer.DrawSoundId(defaultEntry, ref newEntry, true); ImGui.TableNextColumn(); - for (var i = 0; i < 10; ++i) - { - using var id = ImRaii.PushId(i); - var flag = 1 << i; - if (Checkmark("##attribute", $"{(char)('A' + i)}", (meta.Entry.AttributeMask & flag) != 0, - (defaultEntry.AttributeMask & flag) != 0, out var val)) - { - var attributes = val ? meta.Entry.AttributeMask | flag : meta.Entry.AttributeMask & ~flag; - editor.MetaEditor.Change(meta.Copy(meta.Entry with { AttributeMask = (ushort)attributes })); - } + changes |= ImcManipulationDrawer.DrawAttributes(defaultEntry, ref newEntry); - ImGui.SameLine(); - } - - ImGui.NewLine(); + if (changes) + editor.MetaEditor.Change(meta.Copy(newEntry)); } } diff --git a/Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs b/Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs index 06cb4154..2d80d3df 100644 --- a/Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs @@ -80,29 +80,29 @@ public class AddGroupDrawer : IUiService private void DrawImcInput(float width) { - var change = MetaManipulationDrawer.DrawObjectType(ref _imcManip, width); + var change = ImcManipulationDrawer.DrawObjectType(ref _imcManip, width); ImUtf8.SameLineInner(); - change |= MetaManipulationDrawer.DrawPrimaryId(ref _imcManip, width); + change |= ImcManipulationDrawer.DrawPrimaryId(ref _imcManip, width); if (_imcManip.ObjectType is ObjectType.Weapon or ObjectType.Monster) { - change |= MetaManipulationDrawer.DrawSecondaryId(ref _imcManip, width); + change |= ImcManipulationDrawer.DrawSecondaryId(ref _imcManip, width); ImUtf8.SameLineInner(); - change |= MetaManipulationDrawer.DrawVariant(ref _imcManip, width); + change |= ImcManipulationDrawer.DrawVariant(ref _imcManip, width); } else if (_imcManip.ObjectType is ObjectType.DemiHuman) { var quarterWidth = (width - ImUtf8.ItemInnerSpacing.X / ImUtf8.GlobalScale) / 2; - change |= MetaManipulationDrawer.DrawSecondaryId(ref _imcManip, width); + change |= ImcManipulationDrawer.DrawSecondaryId(ref _imcManip, width); ImUtf8.SameLineInner(); - change |= MetaManipulationDrawer.DrawSlot(ref _imcManip, quarterWidth); + change |= ImcManipulationDrawer.DrawSlot(ref _imcManip, quarterWidth); ImUtf8.SameLineInner(); - change |= MetaManipulationDrawer.DrawVariant(ref _imcManip, quarterWidth); + change |= ImcManipulationDrawer.DrawVariant(ref _imcManip, quarterWidth); } else { - change |= MetaManipulationDrawer.DrawSlot(ref _imcManip, width); + change |= ImcManipulationDrawer.DrawSlot(ref _imcManip, width); ImUtf8.SameLineInner(); - change |= MetaManipulationDrawer.DrawVariant(ref _imcManip, width); + change |= ImcManipulationDrawer.DrawVariant(ref _imcManip, width); } if (change) diff --git a/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs b/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs new file mode 100644 index 00000000..5873119e --- /dev/null +++ b/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs @@ -0,0 +1,221 @@ +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Text; +using OtterGui.Text.HelperObjects; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Meta.Manipulations; +using Penumbra.UI.Classes; + +namespace Penumbra.UI.ModsTab; + +public static class ImcManipulationDrawer +{ + public static bool DrawObjectType(ref ImcManipulation manip, float width = 110) + { + var ret = Combos.ImcType("##imcType", manip.ObjectType, out var type, width); + ImUtf8.HoverTooltip("Object Type"u8); + + if (ret) + { + var equipSlot = type switch + { + ObjectType.Equipment => manip.EquipSlot.IsEquipment() ? manip.EquipSlot : EquipSlot.Head, + ObjectType.DemiHuman => manip.EquipSlot.IsEquipment() ? manip.EquipSlot : EquipSlot.Head, + ObjectType.Accessory => manip.EquipSlot.IsAccessory() ? manip.EquipSlot : EquipSlot.Ears, + _ => EquipSlot.Unknown, + }; + manip = new ImcManipulation(type, manip.BodySlot, manip.PrimaryId, manip.SecondaryId == 0 ? 1 : manip.SecondaryId, + manip.Variant.Id, equipSlot, manip.Entry); + } + + return ret; + } + + public static bool DrawPrimaryId(ref ImcManipulation manip, float unscaledWidth = 80) + { + var ret = IdInput("##imcPrimaryId"u8, unscaledWidth, manip.PrimaryId.Id, out var newId, 0, ushort.MaxValue, + manip.PrimaryId.Id <= 1); + ImUtf8.HoverTooltip("Primary ID - You can usually find this as the 'x####' part of an item path.\n"u8 + + "This should generally not be left <= 1 unless you explicitly want that."u8); + if (ret) + manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, newId, manip.SecondaryId, manip.Variant.Id, manip.EquipSlot, + manip.Entry); + return ret; + } + + public static bool DrawSecondaryId(ref ImcManipulation manip, float unscaledWidth = 100) + { + var ret = IdInput("##imcSecondaryId"u8, unscaledWidth, manip.SecondaryId.Id, out var newId, 0, ushort.MaxValue, false); + ImUtf8.HoverTooltip("Secondary ID"u8); + if (ret) + manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, newId, manip.Variant.Id, manip.EquipSlot, + manip.Entry); + return ret; + } + + public static bool DrawVariant(ref ImcManipulation manip, float unscaledWidth = 45) + { + var ret = IdInput("##imcVariant"u8, unscaledWidth, manip.Variant.Id, out var newId, 0, byte.MaxValue, false); + ImUtf8.HoverTooltip("Variant ID"u8); + if (ret) + manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, manip.SecondaryId, (byte)newId, manip.EquipSlot, + manip.Entry); + return ret; + } + + public static bool DrawSlot(ref ImcManipulation manip, float unscaledWidth = 100) + { + bool ret; + EquipSlot slot; + switch (manip.ObjectType) + { + case ObjectType.Equipment: + case ObjectType.DemiHuman: + ret = Combos.EqpEquipSlot("##slot", manip.EquipSlot, out slot, unscaledWidth); + break; + case ObjectType.Accessory: + ret = Combos.AccessorySlot("##slot", manip.EquipSlot, out slot, unscaledWidth); + break; + default: return false; + } + + ImUtf8.HoverTooltip("Equip Slot"u8); + if (ret) + manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, manip.SecondaryId, manip.Variant.Id, slot, + manip.Entry); + return ret; + } + + public static bool DrawMaterialId(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault, float unscaledWidth = 45) + { + if (!DragInput("##materialId"u8, "Material ID"u8, unscaledWidth * ImUtf8.GlobalScale, entry.MaterialId, defaultEntry.MaterialId, + out var newValue, (byte)1, byte.MaxValue, 0.01f, addDefault)) + return false; + + entry = entry with { MaterialId = newValue }; + return true; + } + + public static bool DrawMaterialAnimationId(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault, float unscaledWidth = 45) + { + if (!DragInput("##mAnimId"u8, "Material Animation ID"u8, unscaledWidth * ImUtf8.GlobalScale, entry.MaterialAnimationId, + defaultEntry.MaterialAnimationId, out var newValue, (byte)0, byte.MaxValue, 0.01f, addDefault)) + return false; + + entry = entry with { MaterialAnimationId = newValue }; + return true; + } + + public static bool DrawDecalId(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault, float unscaledWidth = 45) + { + if (!DragInput("##decalId"u8, "Decal ID"u8, unscaledWidth * ImUtf8.GlobalScale, entry.DecalId, defaultEntry.DecalId, out var newValue, + (byte)0, byte.MaxValue, 0.01f, addDefault)) + return false; + + entry = entry with { DecalId = newValue }; + return true; + } + + public static bool DrawVfxId(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault, float unscaledWidth = 45) + { + if (!DragInput("##vfxId"u8, "VFX ID"u8, unscaledWidth * ImUtf8.GlobalScale, entry.VfxId, defaultEntry.VfxId, out var newValue, (byte)0, + byte.MaxValue, 0.01f, addDefault)) + return false; + + entry = entry with { VfxId = newValue }; + return true; + } + + public static bool DrawSoundId(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault, float unscaledWidth = 45) + { + if (!DragInput("##soundId"u8, "Sound ID"u8, unscaledWidth * ImUtf8.GlobalScale, entry.SoundId, defaultEntry.SoundId, out var newValue, + (byte)0, byte.MaxValue, 0.01f, addDefault)) + return false; + + entry = entry with { SoundId = newValue }; + return true; + } + + public static bool DrawAttributes(ImcEntry defaultEntry, ref ImcEntry entry) + { + var changes = false; + for (var i = 0; i < ImcEntry.NumAttributes; ++i) + { + using var id = ImRaii.PushId(i); + var flag = 1 << i; + var value = (entry.AttributeMask & flag) != 0; + var def = (defaultEntry.AttributeMask & flag) != 0; + if (Checkmark("##attribute"u8, "ABCDEFGHIJ"u8.Slice(i, 1), value, def, out var newValue)) + { + var newMask = (ushort)(newValue ? entry.AttributeMask | flag : entry.AttributeMask & ~flag); + entry = entry with { AttributeMask = newMask }; + changes = true; + } + + if (i < ImcEntry.NumAttributes - 1) + ImGui.SameLine(); + } + + return changes; + } + + + /// + /// A number input for ids with an optional max id of given width. + /// Returns true if newId changed against currentId. + /// + private static bool IdInput(ReadOnlySpan label, float unscaledWidth, ushort currentId, out ushort newId, int minId, int maxId, + bool border) + { + int tmp = currentId; + ImGui.SetNextItemWidth(unscaledWidth * ImUtf8.GlobalScale); + using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, UiHelpers.Scale, border); + using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.RegexWarningBorder, border); + if (ImUtf8.InputScalar(label, ref tmp)) + tmp = Math.Clamp(tmp, minId, maxId); + + newId = (ushort)tmp; + return newId != currentId; + } + + /// + /// A dragging int input of given width that compares against a default value, shows a tooltip and clamps against min and max. + /// Returns true if newValue changed against currentValue. + /// + private static bool DragInput(ReadOnlySpan label, ReadOnlySpan tooltip, float width, T currentValue, T defaultValue, + out T newValue, T minValue, T maxValue, float speed, bool addDefault) where T : unmanaged, INumber + { + newValue = currentValue; + using var color = ImRaii.PushColor(ImGuiCol.FrameBg, + defaultValue > currentValue ? ColorId.DecreasedMetaValue.Value() : ColorId.IncreasedMetaValue.Value(), + defaultValue != currentValue); + ImGui.SetNextItemWidth(width); + if (ImUtf8.DragScalar(label, ref newValue, minValue, maxValue, speed)) + newValue = newValue <= minValue ? minValue : newValue >= maxValue ? maxValue : newValue; + + if (addDefault) + ImUtf8.HoverTooltip($"{tooltip}\nDefault Value: {defaultValue}"); + else + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, tooltip); + + return newValue != currentValue; + } + + /// + /// A checkmark that compares against a default value and shows a tooltip. + /// Returns true if newValue is changed against currentValue. + /// + private static bool Checkmark(ReadOnlySpan label, ReadOnlySpan tooltip, bool currentValue, bool defaultValue, + out bool newValue) + { + using var color = ImRaii.PushColor(ImGuiCol.FrameBg, + defaultValue ? ColorId.DecreasedMetaValue.Value() : ColorId.IncreasedMetaValue.Value(), + defaultValue != currentValue); + newValue = currentValue; + ImUtf8.Checkbox(label, ref newValue); + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, tooltip); + return newValue != currentValue; + } +} diff --git a/Penumbra/UI/ModsTab/MetaManipulationDrawer.cs b/Penumbra/UI/ModsTab/MetaManipulationDrawer.cs deleted file mode 100644 index 1f2273b5..00000000 --- a/Penumbra/UI/ModsTab/MetaManipulationDrawer.cs +++ /dev/null @@ -1,105 +0,0 @@ -using ImGuiNET; -using OtterGui.Raii; -using OtterGui.Text; -using Penumbra.GameData.Enums; -using Penumbra.Meta.Manipulations; -using Penumbra.UI.Classes; - -namespace Penumbra.UI.ModsTab; - -public static class MetaManipulationDrawer -{ - public static bool DrawObjectType(ref ImcManipulation manip, float width = 110) - { - var ret = Combos.ImcType("##imcType", manip.ObjectType, out var type, width); - ImUtf8.HoverTooltip("Object Type"u8); - - if (ret) - { - var equipSlot = type switch - { - ObjectType.Equipment => manip.EquipSlot.IsEquipment() ? manip.EquipSlot : EquipSlot.Head, - ObjectType.DemiHuman => manip.EquipSlot.IsEquipment() ? manip.EquipSlot : EquipSlot.Head, - ObjectType.Accessory => manip.EquipSlot.IsAccessory() ? manip.EquipSlot : EquipSlot.Ears, - _ => EquipSlot.Unknown, - }; - manip = new ImcManipulation(type, manip.BodySlot, manip.PrimaryId, manip.SecondaryId == 0 ? 1 : manip.SecondaryId, - manip.Variant.Id, equipSlot, manip.Entry); - } - - return ret; - } - - public static bool DrawPrimaryId(ref ImcManipulation manip, float unscaledWidth = 80) - { - var ret = IdInput("##imcPrimaryId"u8, unscaledWidth, manip.PrimaryId.Id, out var newId, 0, ushort.MaxValue, - manip.PrimaryId.Id <= 1); - ImUtf8.HoverTooltip("Primary ID - You can usually find this as the 'x####' part of an item path.\n"u8 - + "This should generally not be left <= 1 unless you explicitly want that."u8); - if (ret) - manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, newId, manip.SecondaryId, manip.Variant.Id, manip.EquipSlot, - manip.Entry); - return ret; - } - - public static bool DrawSecondaryId(ref ImcManipulation manip, float unscaledWidth = 100) - { - var ret = IdInput("##imcSecondaryId"u8, unscaledWidth, manip.SecondaryId.Id, out var newId, 0, ushort.MaxValue, false); - ImUtf8.HoverTooltip("Secondary ID"u8); - if (ret) - manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, newId, manip.Variant.Id, manip.EquipSlot, - manip.Entry); - return ret; - } - - public static bool DrawVariant(ref ImcManipulation manip, float unscaledWidth = 45) - { - var ret = IdInput("##imcVariant"u8, unscaledWidth, manip.Variant.Id, out var newId, 0, byte.MaxValue, false); - ImUtf8.HoverTooltip("Variant ID"u8); - if (ret) - manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, manip.SecondaryId, (byte)newId, manip.EquipSlot, - manip.Entry); - return ret; - } - - public static bool DrawSlot(ref ImcManipulation manip, float unscaledWidth = 100) - { - bool ret; - EquipSlot slot; - switch (manip.ObjectType) - { - case ObjectType.Equipment: - case ObjectType.DemiHuman: - ret = Combos.EqpEquipSlot("##slot", manip.EquipSlot, out slot, unscaledWidth); - break; - case ObjectType.Accessory: - ret = Combos.AccessorySlot("##slot", manip.EquipSlot, out slot, unscaledWidth); - break; - default: return false; - } - - ImUtf8.HoverTooltip("Equip Slot"u8); - if (ret) - manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, manip.SecondaryId, manip.Variant.Id, slot, - manip.Entry); - return ret; - } - - /// - /// A number input for ids with an optional max id of given width. - /// Returns true if newId changed against currentId. - /// - private static bool IdInput(ReadOnlySpan label, float unscaledWidth, ushort currentId, out ushort newId, int minId, int maxId, - bool border) - { - int tmp = currentId; - ImGui.SetNextItemWidth(unscaledWidth * ImUtf8.GlobalScale); - using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, UiHelpers.Scale, border); - using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.RegexWarningBorder, border); - if (ImUtf8.InputScalar(label, ref tmp)) - tmp = Math.Clamp(tmp, minId, maxId); - - newId = (ushort)tmp; - return newId != currentId; - } -} From 125e5628ecbf2e42d75e8c499db39bdeef3ba668 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 23 May 2024 23:24:37 +0200 Subject: [PATCH 164/433] Fix fuckup. --- Penumbra/Mods/Groups/ModSaveGroup.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Mods/Groups/ModSaveGroup.cs b/Penumbra/Mods/Groups/ModSaveGroup.cs index 7efc76a6..05437e3d 100644 --- a/Penumbra/Mods/Groups/ModSaveGroup.cs +++ b/Penumbra/Mods/Groups/ModSaveGroup.cs @@ -72,7 +72,7 @@ public readonly struct ModSaveGroup : ISavable var serializer = new JsonSerializer { Formatting = Formatting.Indented }; j.WriteStartObject(); if (_groupIdx >= 0) - _group!.WriteJson(j, serializer); + _group!.WriteJson(j, serializer, _basePath); else SubMod.WriteModContainer(j, serializer, _defaultMod!, _basePath); j.WriteEndObject(); From 65627b5002c2b1ff9ed6b73cd5d1e7efb7ef2963 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 24 May 2024 16:14:48 +0200 Subject: [PATCH 165/433] Fix a weird age-old bug apparently? --- Penumbra/Collections/Cache/CollectionCacheManager.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Penumbra/Collections/Cache/CollectionCacheManager.cs b/Penumbra/Collections/Cache/CollectionCacheManager.cs index ca57c8b9..ae424b94 100644 --- a/Penumbra/Collections/Cache/CollectionCacheManager.cs +++ b/Penumbra/Collections/Cache/CollectionCacheManager.cs @@ -274,17 +274,17 @@ public class CollectionCacheManager : IDisposable return; } - type.HandlingInfo(out _, out var recomputeList, out var reload); + type.HandlingInfo(out _, out var recomputeList, out var justAdd); if (!recomputeList) return; foreach (var collection in _storage.Where(collection => collection.HasCache && collection[mod.Index].Settings is { Enabled: true })) { - if (reload) - collection._cache!.ReloadMod(mod, true); - else + if (justAdd) collection._cache!.AddMod(mod, true); + else + collection._cache!.ReloadMod(mod, true); } } From 4743acf76786518307782677d6d38c48faeb0b95 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 24 May 2024 16:15:04 +0200 Subject: [PATCH 166/433] Make IMC handling even better. --- Penumbra.GameData | 2 +- Penumbra/Meta/ImcChecker.cs | 36 +++ Penumbra/Meta/Manipulations/Imc.cs | 24 +- .../Meta/Manipulations/ImcManipulation.cs | 169 ++++-------- Penumbra/Meta/MetaFileManager.cs | 2 + Penumbra/Mods/Groups/ImcModGroup.cs | 251 ++++++++++-------- .../Manager/OptionEditor/ImcModGroupEditor.cs | 16 +- .../Manager/OptionEditor/ModGroupEditor.cs | 2 +- Penumbra/Mods/SubMods/ImcSubMod.cs | 12 +- Penumbra/Services/StaticServiceManager.cs | 3 +- .../UI/AdvancedWindow/ModEditWindow.Meta.cs | 65 ++--- Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs | 68 ++--- .../ModsTab/Groups/ImcModGroupEditDrawer.cs | 161 +++++------ .../UI/ModsTab/Groups/ModGroupEditDrawer.cs | 32 ++- Penumbra/UI/ModsTab/ImcManipulationDrawer.cs | 53 ++-- 15 files changed, 437 insertions(+), 459 deletions(-) create mode 100644 Penumbra/Meta/ImcChecker.cs diff --git a/Penumbra.GameData b/Penumbra.GameData index e8220a0a..ec35e664 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit e8220a0a74e9480330e98ed7ca462353434b9649 +Subproject commit ec35e66499eb388b4e7917e4fae4615218d33335 diff --git a/Penumbra/Meta/ImcChecker.cs b/Penumbra/Meta/ImcChecker.cs new file mode 100644 index 00000000..14486e21 --- /dev/null +++ b/Penumbra/Meta/ImcChecker.cs @@ -0,0 +1,36 @@ +using Penumbra.GameData.Structs; +using Penumbra.Meta.Files; +using Penumbra.Meta.Manipulations; + +namespace Penumbra.Meta; + +public class ImcChecker(MetaFileManager metaFileManager) +{ + public readonly record struct CachedEntry(ImcEntry Entry, bool FileExists, bool VariantExists); + + private readonly Dictionary _cachedDefaultEntries = new(); + + public CachedEntry GetDefaultEntry(ImcIdentifier identifier, bool storeCache) + { + if (_cachedDefaultEntries.TryGetValue(identifier, out var entry)) + return entry; + + try + { + var e = ImcFile.GetDefault(metaFileManager, identifier.GamePath(), identifier.EquipSlot, identifier.Variant, out var entryExists); + entry = new CachedEntry(e, true, entryExists); + } + catch (Exception) + { + entry = new CachedEntry(default, false, false); + } + + if (storeCache) + _cachedDefaultEntries.Add(identifier, entry); + return entry; + } + + public CachedEntry GetDefaultEntry(ImcManipulation imcManip, bool storeCache) + => GetDefaultEntry(new ImcIdentifier(imcManip.PrimaryId, imcManip.Variant, imcManip.ObjectType, imcManip.SecondaryId.Id, + imcManip.EquipSlot, imcManip.BodySlot), storeCache); +} diff --git a/Penumbra/Meta/Manipulations/Imc.cs b/Penumbra/Meta/Manipulations/Imc.cs index 9b123df1..fef86520 100644 --- a/Penumbra/Meta/Manipulations/Imc.cs +++ b/Penumbra/Meta/Manipulations/Imc.cs @@ -15,6 +15,8 @@ public readonly record struct ImcIdentifier( EquipSlot EquipSlot, BodySlot BodySlot) : IMetaIdentifier, IComparable { + public static readonly ImcIdentifier Default = new(EquipSlot.Body, 1, (Variant)1); + public ImcIdentifier(EquipSlot slot, PrimaryId primaryId, ushort variant) : this(primaryId, (Variant)Math.Clamp(variant, (ushort)0, byte.MaxValue), slot.IsAccessory() ? ObjectType.Accessory : ObjectType.Equipment, 0, slot, @@ -25,6 +27,9 @@ public readonly record struct ImcIdentifier( : this(primaryId, variant, slot.IsAccessory() ? ObjectType.Accessory : ObjectType.Equipment, 0, slot, BodySlot.Unknown) { } + public ImcManipulation ToManipulation(ImcEntry entry) + => new(ObjectType, BodySlot, PrimaryId, SecondaryId.Id, Variant.Id, EquipSlot, entry); + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) { var path = ObjectType switch @@ -137,9 +142,12 @@ public readonly record struct ImcIdentifier( return b != 0 ? b : Variant.Id.CompareTo(other.Variant.Id); } - public static ImcIdentifier? FromJson(JObject jObj) + public static ImcIdentifier? FromJson(JObject? jObj) { - var objectType = jObj["PrimaryId"]?.ToObject() ?? ObjectType.Unknown; + if (jObj == null) + return null; + + var objectType = jObj["ObjectType"]?.ToObject() ?? ObjectType.Unknown; var primaryId = new PrimaryId(jObj["PrimaryId"]?.ToObject() ?? 0); var variant = jObj["Variant"]?.ToObject() ?? 0; if (variant > byte.MaxValue) @@ -178,12 +186,12 @@ public readonly record struct ImcIdentifier( public JObject AddToJson(JObject jObj) { - jObj["ObjectType"] = ObjectType.ToString(); - jObj["PrimaryId"] = PrimaryId.Id; - jObj["PrimaryId"] = SecondaryId.Id; - jObj["Variant"] = Variant.Id; - jObj["EquipSlot"] = EquipSlot.ToString(); - jObj["BodySlot"] = BodySlot.ToString(); + jObj["ObjectType"] = ObjectType.ToString(); + jObj["PrimaryId"] = PrimaryId.Id; + jObj["SecondaryId"] = SecondaryId.Id; + jObj["Variant"] = Variant.Id; + jObj["EquipSlot"] = EquipSlot.ToString(); + jObj["BodySlot"] = BodySlot.ToString(); return jObj; } } diff --git a/Penumbra/Meta/Manipulations/ImcManipulation.cs b/Penumbra/Meta/Manipulations/ImcManipulation.cs index 45295990..945aab04 100644 --- a/Penumbra/Meta/Manipulations/ImcManipulation.cs +++ b/Penumbra/Meta/Manipulations/ImcManipulation.cs @@ -12,171 +12,96 @@ namespace Penumbra.Meta.Manipulations; [StructLayout(LayoutKind.Sequential, Pack = 1)] public readonly struct ImcManipulation : IMetaManipulation { - public ImcEntry Entry { get; private init; } - public PrimaryId PrimaryId { get; private init; } - public PrimaryId SecondaryId { get; private init; } - public Variant Variant { get; private init; } + [JsonIgnore] + public ImcIdentifier Identifier { get; private init; } + + public ImcEntry Entry { get; private init; } + + + public PrimaryId PrimaryId + => Identifier.PrimaryId; + + public SecondaryId SecondaryId + => Identifier.SecondaryId; + + public Variant Variant + => Identifier.Variant; [JsonConverter(typeof(StringEnumConverter))] - public ObjectType ObjectType { get; private init; } + public ObjectType ObjectType + => Identifier.ObjectType; [JsonConverter(typeof(StringEnumConverter))] - public EquipSlot EquipSlot { get; private init; } + public EquipSlot EquipSlot + => Identifier.EquipSlot; [JsonConverter(typeof(StringEnumConverter))] - public BodySlot BodySlot { get; private init; } + public BodySlot BodySlot + => Identifier.BodySlot; public ImcManipulation(EquipSlot equipSlot, ushort variant, PrimaryId primaryId, ImcEntry entry) + : this(new ImcIdentifier(equipSlot, primaryId, variant), entry) + { } + + public ImcManipulation(ImcIdentifier identifier, ImcEntry entry) { - Entry = entry; - PrimaryId = primaryId; - Variant = (Variant)Math.Clamp(variant, (ushort)0, byte.MaxValue); - SecondaryId = 0; - ObjectType = equipSlot.IsAccessory() ? ObjectType.Accessory : ObjectType.Equipment; - EquipSlot = equipSlot; - BodySlot = variant > byte.MaxValue ? BodySlot.Body : BodySlot.Unknown; + Identifier = identifier; + Entry = entry; } + // Variants were initially ushorts but got shortened to bytes. // There are still some manipulations around that have values > 255 for variant, // so we change the unused value to something nonsensical in that case, just so they do not compare equal, // and clamp the variant to 255. [JsonConstructor] - internal ImcManipulation(ObjectType objectType, BodySlot bodySlot, PrimaryId primaryId, PrimaryId secondaryId, ushort variant, + internal ImcManipulation(ObjectType objectType, BodySlot bodySlot, PrimaryId primaryId, SecondaryId secondaryId, ushort variant, EquipSlot equipSlot, ImcEntry entry) { - Entry = entry; - ObjectType = objectType; - PrimaryId = primaryId; - Variant = (Variant)Math.Clamp(variant, (ushort)0, byte.MaxValue); - - if (objectType is ObjectType.Accessory or ObjectType.Equipment) + Entry = entry; + var v = (Variant)Math.Clamp(variant, (ushort)0, byte.MaxValue); + Identifier = objectType switch { - BodySlot = variant > byte.MaxValue ? BodySlot.Body : BodySlot.Unknown; - SecondaryId = 0; - EquipSlot = equipSlot; - } - else if (objectType is ObjectType.DemiHuman) - { - BodySlot = variant > byte.MaxValue ? BodySlot.Body : BodySlot.Unknown; - SecondaryId = secondaryId; - EquipSlot = equipSlot == EquipSlot.Unknown ? EquipSlot.Head : equipSlot; - } - else - { - BodySlot = bodySlot; - SecondaryId = secondaryId; - EquipSlot = variant > byte.MaxValue ? EquipSlot.All : EquipSlot.Unknown; - } + ObjectType.Accessory or ObjectType.Equipment => new ImcIdentifier(primaryId, v, objectType, 0, equipSlot, + variant > byte.MaxValue ? BodySlot.Body : BodySlot.Unknown), + ObjectType.DemiHuman => new ImcIdentifier(primaryId, v, objectType, secondaryId, + equipSlot == EquipSlot.Unknown ? EquipSlot.Head : equipSlot, variant > byte.MaxValue ? BodySlot.Body : BodySlot.Unknown), + _ => new ImcIdentifier(primaryId, v, objectType, secondaryId, equipSlot == EquipSlot.Unknown ? EquipSlot.Head : equipSlot, + bodySlot), + }; } public ImcManipulation Copy(ImcEntry entry) - => new(ObjectType, BodySlot, PrimaryId, SecondaryId, Variant.Id, EquipSlot, entry); + => new(Identifier, entry); public override string ToString() - => ObjectType is ObjectType.Equipment or ObjectType.Accessory - ? $"Imc - {PrimaryId} - {EquipSlot} - {Variant}" - : $"Imc - {PrimaryId} - {ObjectType} - {SecondaryId} - {BodySlot} - {Variant}"; + => Identifier.ToString(); public bool Equals(ImcManipulation other) - => PrimaryId == other.PrimaryId - && Variant == other.Variant - && SecondaryId == other.SecondaryId - && ObjectType == other.ObjectType - && EquipSlot == other.EquipSlot - && BodySlot == other.BodySlot; + => Identifier == other.Identifier; public override bool Equals(object? obj) => obj is ImcManipulation other && Equals(other); public override int GetHashCode() - => HashCode.Combine(PrimaryId, Variant, SecondaryId, (int)ObjectType, (int)EquipSlot, (int)BodySlot); + => Identifier.GetHashCode(); public int CompareTo(ImcManipulation other) - { - var o = ObjectType.CompareTo(other.ObjectType); - if (o != 0) - return o; - - var i = PrimaryId.Id.CompareTo(other.PrimaryId.Id); - if (i != 0) - return i; - - if (ObjectType is ObjectType.Equipment or ObjectType.Accessory) - { - var e = EquipSlot.CompareTo(other.EquipSlot); - return e != 0 ? e : Variant.Id.CompareTo(other.Variant.Id); - } - - if (ObjectType is ObjectType.DemiHuman) - { - var e = EquipSlot.CompareTo(other.EquipSlot); - if (e != 0) - return e; - } - - var s = SecondaryId.Id.CompareTo(other.SecondaryId.Id); - if (s != 0) - return s; - - var b = BodySlot.CompareTo(other.BodySlot); - return b != 0 ? b : Variant.Id.CompareTo(other.Variant.Id); - } + => Identifier.CompareTo(other.Identifier); public MetaIndex FileIndex() - => (MetaIndex)(-1); + => Identifier.FileIndex(); public Utf8GamePath GamePath() - { - return ObjectType switch - { - ObjectType.Accessory => Utf8GamePath.FromString(GamePaths.Accessory.Imc.Path(PrimaryId), out var p) ? p : Utf8GamePath.Empty, - ObjectType.Equipment => Utf8GamePath.FromString(GamePaths.Equipment.Imc.Path(PrimaryId), out var p) ? p : Utf8GamePath.Empty, - ObjectType.DemiHuman => Utf8GamePath.FromString(GamePaths.DemiHuman.Imc.Path(PrimaryId, SecondaryId), out var p) - ? p - : Utf8GamePath.Empty, - ObjectType.Monster => Utf8GamePath.FromString(GamePaths.Monster.Imc.Path(PrimaryId, SecondaryId), out var p) - ? p - : Utf8GamePath.Empty, - ObjectType.Weapon => Utf8GamePath.FromString(GamePaths.Weapon.Imc.Path(PrimaryId, SecondaryId), out var p) ? p : Utf8GamePath.Empty, - _ => throw new NotImplementedException(), - }; - } + => Identifier.GamePath(); public bool Apply(ImcFile file) => file.SetEntry(ImcFile.PartIndex(EquipSlot), Variant.Id, Entry); public bool Validate(bool withMaterial) { - switch (ObjectType) - { - case ObjectType.Accessory: - case ObjectType.Equipment: - if (BodySlot is not BodySlot.Unknown) - return false; - if (!EquipSlot.IsEquipment() && !EquipSlot.IsAccessory()) - return false; - if (SecondaryId != 0) - return false; - - break; - case ObjectType.DemiHuman: - if (BodySlot is not BodySlot.Unknown) - return false; - if (!EquipSlot.IsEquipment() && !EquipSlot.IsAccessory()) - return false; - - break; - default: - if (!Enum.IsDefined(BodySlot)) - return false; - if (EquipSlot is not EquipSlot.Unknown) - return false; - if (!Enum.IsDefined(ObjectType)) - return false; - - break; - } + if (!Identifier.Validate()) + return false; if (withMaterial && Entry.MaterialId == 0) return false; diff --git a/Penumbra/Meta/MetaFileManager.cs b/Penumbra/Meta/MetaFileManager.cs index cd99396b..40fceb07 100644 --- a/Penumbra/Meta/MetaFileManager.cs +++ b/Penumbra/Meta/MetaFileManager.cs @@ -27,6 +27,7 @@ public unsafe class MetaFileManager internal readonly ValidityChecker ValidityChecker; internal readonly ObjectIdentification Identifier; internal readonly FileCompactor Compactor; + internal readonly ImcChecker ImcChecker; public MetaFileManager(CharacterUtility characterUtility, ResidentResourceManager residentResources, IDataManager gameData, ActiveCollectionData activeCollections, Configuration config, ValidityChecker validityChecker, ObjectIdentification identifier, @@ -40,6 +41,7 @@ public unsafe class MetaFileManager ValidityChecker = validityChecker; Identifier = identifier; Compactor = compactor; + ImcChecker = new ImcChecker(this); interop.InitializeFromAttributes(this); } diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs index cf228889..e0d70aa6 100644 --- a/Penumbra/Mods/Groups/ImcModGroup.cs +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -1,25 +1,21 @@ using Dalamud.Interface.Internal.Notifications; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using OtterGui; using OtterGui.Classes; using Penumbra.Api.Enums; using Penumbra.GameData.Data; -using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.String.Classes; -using Penumbra.UI.ModsTab; using Penumbra.UI.ModsTab.Groups; -using Penumbra.Util; namespace Penumbra.Mods.Groups; public class ImcModGroup(Mod mod) : IModGroup { - public const int DisabledIndex = 60; - public Mod Mod { get; } = mod; public string Name { get; set; } = "Option"; public string Description { get; set; } = string.Empty; @@ -33,33 +29,35 @@ public class ImcModGroup(Mod mod) : IModGroup public ModPriority Priority { get; set; } = ModPriority.Default; public Setting DefaultSettings { get; set; } = Setting.Zero; - public PrimaryId PrimaryId; - public SecondaryId SecondaryId; - public ObjectType ObjectType; - public BodySlot BodySlot; - public EquipSlot EquipSlot; - public Variant Variant; - - public ImcEntry DefaultEntry; + public ImcIdentifier Identifier; + public ImcEntry DefaultEntry; public FullPath? FindBestMatch(Utf8GamePath gamePath) => null; - private bool _canBeDisabled = false; + private bool _canBeDisabled; public bool CanBeDisabled { - get => _canBeDisabled; + get => OptionData.Any(m => m.IsDisableSubMod); set { _canBeDisabled = value; if (!value) + { + OptionData.RemoveAll(m => m.IsDisableSubMod); DefaultSettings = FixSetting(DefaultSettings); + } + else + { + if (!OptionData.Any(m => m.IsDisableSubMod)) + OptionData.Add(ImcSubMod.DisableSubMod(this)); + } } } public bool DefaultDisabled - => _canBeDisabled && DefaultSettings.HasFlag(DisabledIndex); + => IsDisabled(DefaultSettings); public IModOption? AddOption(string name, string description = "") { @@ -86,7 +84,7 @@ public class ImcModGroup(Mod mod) : IModGroup => []; public bool IsOption - => CanBeDisabled || OptionData.Count > 0; + => OptionData.Count > 0; public int GetIndex() => ModGroup.GetIndex(this); @@ -94,6 +92,128 @@ public class ImcModGroup(Mod mod) : IModGroup public IModGroupEditDrawer EditDrawer(ModGroupEditDrawer editDrawer) => new ImcModGroupEditDrawer(editDrawer, this); + public ImcManipulation GetManip(ushort mask) + => new(Identifier.ObjectType, Identifier.BodySlot, Identifier.PrimaryId, Identifier.SecondaryId.Id, Identifier.Variant.Id, + Identifier.EquipSlot, DefaultEntry with { AttributeMask = mask }); + + public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) + { + if (IsDisabled(setting)) + return; + + var mask = GetCurrentMask(setting); + var imc = GetManip(mask); + manipulations.Add(imc); + } + + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + => Identifier.AddChangedItems(identifier, changedItems); + + public Setting FixSetting(Setting setting) + => new(setting.Value & ((1ul << OptionData.Count) - 1)); + + public void WriteJson(JsonTextWriter jWriter, JsonSerializer serializer, DirectoryInfo? basePath = null) + { + ModSaveGroup.WriteJsonBase(jWriter, this); + var jObj = Identifier.AddToJson(new JObject()); + jWriter.WritePropertyName(nameof(Identifier)); + jObj.WriteTo(jWriter); + jWriter.WritePropertyName(nameof(DefaultEntry)); + serializer.Serialize(jWriter, DefaultEntry); + jWriter.WritePropertyName("Options"); + jWriter.WriteStartArray(); + foreach (var option in OptionData) + { + jWriter.WriteStartObject(); + SubMod.WriteModOption(jWriter, option); + if (option.IsDisableSubMod) + { + jWriter.WritePropertyName(nameof(option.IsDisableSubMod)); + jWriter.WriteValue(true); + } + else + { + jWriter.WritePropertyName(nameof(option.AttributeMask)); + jWriter.WriteValue(option.AttributeMask); + } + + jWriter.WriteEndObject(); + } + + jWriter.WriteEndArray(); + } + + public (int Redirections, int Swaps, int Manips) GetCounts() + => (0, 0, 1); + + public static ImcModGroup? Load(Mod mod, JObject json) + { + var options = json["Options"]; + var identifier = ImcIdentifier.FromJson(json[nameof(Identifier)] as JObject); + var ret = new ImcModGroup(mod) + { + Name = json[nameof(Name)]?.ToObject() ?? string.Empty, + Description = json[nameof(Description)]?.ToObject() ?? string.Empty, + Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, + DefaultEntry = json[nameof(DefaultEntry)]?.ToObject() ?? new ImcEntry(), + }; + if (ret.Name.Length == 0) + return null; + + if (!identifier.HasValue || ret.DefaultEntry.MaterialId == 0) + { + Penumbra.Messager.NotificationMessage($"Could not add IMC group {ret.Name} because the associated IMC Entry is invalid.", + NotificationType.Warning); + return null; + } + + var rollingMask = ret.DefaultEntry.AttributeMask; + if (options != null) + foreach (var child in options.Children()) + { + var subMod = new ImcSubMod(ret, child); + + if (subMod.IsDisableSubMod) + ret._canBeDisabled = true; + + if (subMod.IsDisableSubMod && ret.OptionData.FirstOrDefault(m => m.IsDisableSubMod) is { } disable) + { + Penumbra.Messager.NotificationMessage( + $"Could not add IMC option {subMod.Name} to {ret.Name} because it already contains {disable.Name} as disable option.", + NotificationType.Warning); + } + else if ((subMod.AttributeMask & rollingMask) != 0) + { + Penumbra.Messager.NotificationMessage( + $"Could not add IMC option {subMod.Name} to {ret.Name} because it contains attributes already in use.", + NotificationType.Warning); + } + else + { + rollingMask |= subMod.AttributeMask; + ret.OptionData.Add(subMod); + } + } + + ret.Identifier = identifier.Value; + ret.DefaultSettings = json[nameof(DefaultSettings)]?.ToObject() ?? Setting.Zero; + ret.DefaultSettings = ret.FixSetting(ret.DefaultSettings); + return ret; + } + + private bool IsDisabled(Setting setting) + { + if (!CanBeDisabled) + return false; + + var idx = OptionData.IndexOf(m => m.IsDisableSubMod); + if (idx >= 0) + return setting.HasFlag(idx); + + Penumbra.Log.Warning($"A IMC Group should be able to be disabled, but does not contain a disable option."); + return false; + } + private ushort GetCurrentMask(Setting setting) { var mask = DefaultEntry.AttributeMask; @@ -108,101 +228,4 @@ public class ImcModGroup(Mod mod) : IModGroup return mask; } - - private ushort GetFullMask() - => GetCurrentMask(Setting.AllBits(63)); - - public ImcManipulation GetManip(ushort mask) - => new(ObjectType, BodySlot, PrimaryId, SecondaryId.Id, Variant.Id, EquipSlot, - DefaultEntry with { AttributeMask = mask }); - - public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) - { - if (CanBeDisabled && setting.HasFlag(DisabledIndex)) - return; - - var mask = GetCurrentMask(setting); - var imc = GetManip(mask); - manipulations.Add(imc); - } - - public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) - => identifier.MetaChangedItems(changedItems, GetManip(0)); - - public Setting FixSetting(Setting setting) - => new(setting.Value & (((1ul << OptionData.Count) - 1) | (CanBeDisabled ? 1ul << DisabledIndex : 0))); - - public void WriteJson(JsonTextWriter jWriter, JsonSerializer serializer, DirectoryInfo? basePath = null) - { - ModSaveGroup.WriteJsonBase(jWriter, this); - jWriter.WritePropertyName(nameof(ObjectType)); - jWriter.WriteValue(ObjectType.ToString()); - jWriter.WritePropertyName(nameof(BodySlot)); - jWriter.WriteValue(BodySlot.ToString()); - jWriter.WritePropertyName(nameof(EquipSlot)); - jWriter.WriteValue(EquipSlot.ToString()); - jWriter.WritePropertyName(nameof(PrimaryId)); - jWriter.WriteValue(PrimaryId.Id); - jWriter.WritePropertyName(nameof(SecondaryId)); - jWriter.WriteValue(SecondaryId.Id); - jWriter.WritePropertyName(nameof(Variant)); - jWriter.WriteValue(Variant.Id); - jWriter.WritePropertyName(nameof(DefaultEntry)); - serializer.Serialize(jWriter, DefaultEntry); - jWriter.WritePropertyName("Options"); - jWriter.WriteStartArray(); - foreach (var option in OptionData) - { - jWriter.WriteStartObject(); - SubMod.WriteModOption(jWriter, option); - jWriter.WritePropertyName(nameof(option.AttributeMask)); - jWriter.WriteValue(option.AttributeMask); - jWriter.WriteEndObject(); - } - - jWriter.WriteEndArray(); - } - - public (int Redirections, int Swaps, int Manips) GetCounts() - => (0, 0, 1); - - public static ImcModGroup? Load(Mod mod, JObject json) - { - var options = json["Options"]; - var ret = new ImcModGroup(mod) - { - Name = json[nameof(Name)]?.ToObject() ?? string.Empty, - Description = json[nameof(Description)]?.ToObject() ?? string.Empty, - Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, - DefaultSettings = json[nameof(DefaultSettings)]?.ToObject() ?? Setting.Zero, - ObjectType = json[nameof(ObjectType)]?.ToObject() ?? ObjectType.Unknown, - BodySlot = json[nameof(BodySlot)]?.ToObject() ?? BodySlot.Unknown, - EquipSlot = json[nameof(EquipSlot)]?.ToObject() ?? EquipSlot.Unknown, - PrimaryId = new PrimaryId(json[nameof(PrimaryId)]?.ToObject() ?? 0), - SecondaryId = new SecondaryId(json[nameof(SecondaryId)]?.ToObject() ?? 0), - Variant = new Variant(json[nameof(Variant)]?.ToObject() ?? 0), - CanBeDisabled = json[nameof(CanBeDisabled)]?.ToObject() ?? false, - DefaultEntry = json[nameof(DefaultEntry)]?.ToObject() ?? new ImcEntry(), - }; - if (ret.Name.Length == 0) - return null; - - if (options != null) - foreach (var child in options.Children()) - { - var subMod = new ImcSubMod(ret, child); - ret.OptionData.Add(subMod); - } - - if (!new ImcManipulation(ret.ObjectType, ret.BodySlot, ret.PrimaryId, ret.SecondaryId.Id, ret.Variant.Id, ret.EquipSlot, - ret.DefaultEntry).Validate(true)) - { - Penumbra.Messager.NotificationMessage($"Could not add IMC group because the associated IMC Entry is invalid.", - NotificationType.Warning); - return null; - } - - ret.DefaultSettings = ret.FixSetting(ret.DefaultSettings); - return ret; - } } diff --git a/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs index 20021d29..f9fd532f 100644 --- a/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs @@ -7,7 +7,6 @@ using Penumbra.Mods.Groups; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; -using static FFXIVClientStructs.FFXIV.Client.UI.Misc.ConfigModule; namespace Penumbra.Mods.Manager.OptionEditor; @@ -15,13 +14,13 @@ public sealed class ImcModGroupEditor(CommunicatorService communicator, SaveServ : ModOptionEditor(communicator, saveService, config), IService { /// Add a new, empty imc group with the given manipulation data. - public ImcModGroup? AddModGroup(Mod mod, string newName, ImcManipulation manip, SaveType saveType = SaveType.ImmediateSync) + public ImcModGroup? AddModGroup(Mod mod, string newName, ImcIdentifier identifier, ImcEntry defaultEntry, SaveType saveType = SaveType.ImmediateSync) { if (!ModGroupEditor.VerifyFileName(mod, null, newName, true)) return null; var maxPriority = mod.Groups.Count == 0 ? ModPriority.Default : mod.Groups.Max(o => o.Priority) + 1; - var group = CreateGroup(mod, newName, manip, maxPriority); + var group = CreateGroup(mod, newName, identifier, defaultEntry, maxPriority); mod.Groups.Add(group); SaveService.Save(saveType, new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); Communicator.ModOptionChanged.Invoke(ModOptionChangeType.GroupAdded, mod, group, null, null, -1); @@ -97,19 +96,14 @@ public sealed class ImcModGroupEditor(CommunicatorService communicator, SaveServ }; - private static ImcModGroup CreateGroup(Mod mod, string newName, ImcManipulation manip, ModPriority priority, + private static ImcModGroup CreateGroup(Mod mod, string newName, ImcIdentifier identifier, ImcEntry defaultEntry, ModPriority priority, SaveType saveType = SaveType.ImmediateSync) => new(mod) { Name = newName, Priority = priority, - ObjectType = manip.ObjectType, - EquipSlot = manip.EquipSlot, - BodySlot = manip.BodySlot, - PrimaryId = manip.PrimaryId, - SecondaryId = manip.SecondaryId.Id, - Variant = manip.Variant, - DefaultEntry = manip.Entry, + Identifier = identifier, + DefaultEntry = defaultEntry, }; protected override ImcSubMod? CloneOption(ImcModGroup group, IModOption option) diff --git a/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs index 969ad3fa..e1db0ccf 100644 --- a/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs @@ -246,7 +246,7 @@ public class ModGroupEditor( { GroupType.Single => SingleEditor.AddModGroup(mod, newName, saveType), GroupType.Multi => MultiEditor.AddModGroup(mod, newName, saveType), - GroupType.Imc => ImcEditor.AddModGroup(mod, newName, default, saveType), + GroupType.Imc => ImcEditor.AddModGroup(mod, newName, default, default, saveType), _ => null, }; diff --git a/Penumbra/Mods/SubMods/ImcSubMod.cs b/Penumbra/Mods/SubMods/ImcSubMod.cs index 7f46bc95..c5c8f002 100644 --- a/Penumbra/Mods/SubMods/ImcSubMod.cs +++ b/Penumbra/Mods/SubMods/ImcSubMod.cs @@ -12,13 +12,23 @@ public class ImcSubMod(ImcModGroup group) : IModOption : this(group) { SubMod.LoadOptionData(json, this); - AttributeMask = (ushort)((json[nameof(AttributeMask)]?.ToObject() ?? 0) & ImcEntry.AttributesMask); + AttributeMask = (ushort)((json[nameof(AttributeMask)]?.ToObject() ?? 0) & ImcEntry.AttributesMask); + IsDisableSubMod = json[nameof(IsDisableSubMod)]?.ToObject() ?? false; } + public static ImcSubMod DisableSubMod(ImcModGroup group) + => new(group) + { + Name = "Disable", + AttributeMask = 0, + IsDisableSubMod = true, + }; + public Mod Mod => Group.Mod; public ushort AttributeMask; + public bool IsDisableSubMod { get; private init; } Mod IModOption.Mod => Mod; diff --git a/Penumbra/Services/StaticServiceManager.cs b/Penumbra/Services/StaticServiceManager.cs index 19ae31a2..0c6648ba 100644 --- a/Penumbra/Services/StaticServiceManager.cs +++ b/Penumbra/Services/StaticServiceManager.cs @@ -101,7 +101,8 @@ public static class StaticServiceManager .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton(); + .AddSingleton() + .AddSingleton(p => p.GetRequiredService().ImcChecker); private static ServiceManager AddConfiguration(this ServiceManager services) => services.AddSingleton() diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs index 99889360..68933c9e 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs @@ -19,9 +19,6 @@ public partial class ModEditWindow private const string ModelSetIdTooltip = "Model Set ID - You can usually find this as the 'e####' part of an item path.\nThis should generally not be left <= 1 unless you explicitly want that."; - private const string PrimaryIdTooltip = - "Primary ID - You can usually find this as the 'x####' part of an item path.\nThis should generally not be left <= 1 unless you explicitly want that."; - private const string ModelSetIdTooltipShort = "Model Set ID"; private const string EquipSlotTooltip = "Equip Slot"; private const string ModelRaceTooltip = "Model Race"; @@ -316,7 +313,7 @@ public partial class ModEditWindow private static class ImcRow { - private static ImcManipulation _new = new(EquipSlot.Head, 1, 1, new ImcEntry()); + private static ImcIdentifier _newIdentifier = ImcIdentifier.Default; private static float IdWidth => 80 * UiHelpers.Scale; @@ -324,75 +321,60 @@ public partial class ModEditWindow private static float SmallIdWidth => 45 * UiHelpers.Scale; - /// Convert throwing to null-return if the file does not exist. - private static ImcEntry? GetDefault(MetaFileManager metaFileManager, ImcManipulation imc) - { - try - { - return ImcFile.GetDefault(metaFileManager, imc.GamePath(), imc.EquipSlot, imc.Variant, out _); - } - catch - { - return null; - } - } - public static void DrawNew(MetaFileManager metaFileManager, ModEditor editor, Vector2 iconSize) { ImGui.TableNextColumn(); CopyToClipboardButton("Copy all current IMC manipulations to clipboard.", iconSize, editor.MetaEditor.Imc.Select(m => (MetaManipulation)m)); ImGui.TableNextColumn(); - var defaultEntry = GetDefault(metaFileManager, _new); - var canAdd = defaultEntry != null && editor.MetaEditor.CanAdd(_new); - var tt = canAdd ? "Stage this edit." : defaultEntry == null ? "This IMC file does not exist." : "This entry is already edited."; - defaultEntry ??= new ImcEntry(); + var (defaultEntry, fileExists, _) = metaFileManager.ImcChecker.GetDefaultEntry(_newIdentifier, true); + var manip = (MetaManipulation)new ImcManipulation(_newIdentifier, defaultEntry); + var canAdd = fileExists && editor.MetaEditor.CanAdd(manip); + var tt = canAdd ? "Stage this edit." : !fileExists ? "This IMC file does not exist." : "This entry is already edited."; if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) - editor.MetaEditor.Add(_new.Copy(defaultEntry.Value)); + editor.MetaEditor.Add(manip); // Identifier ImGui.TableNextColumn(); - var change = ImcManipulationDrawer.DrawObjectType(ref _new); + var change = ImcManipulationDrawer.DrawObjectType(ref _newIdentifier); ImGui.TableNextColumn(); - change |= ImcManipulationDrawer.DrawPrimaryId(ref _new); + change |= ImcManipulationDrawer.DrawPrimaryId(ref _newIdentifier); using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, new Vector2(3 * UiHelpers.Scale, ImGui.GetStyle().ItemSpacing.Y)); ImGui.TableNextColumn(); // Equipment and accessories are slightly different imcs than other types. - if (_new.ObjectType is ObjectType.Equipment or ObjectType.Accessory) - change |= ImcManipulationDrawer.DrawSlot(ref _new); + if (_newIdentifier.ObjectType is ObjectType.Equipment or ObjectType.Accessory) + change |= ImcManipulationDrawer.DrawSlot(ref _newIdentifier); else - change |= ImcManipulationDrawer.DrawSecondaryId(ref _new); + change |= ImcManipulationDrawer.DrawSecondaryId(ref _newIdentifier); ImGui.TableNextColumn(); - change |= ImcManipulationDrawer.DrawVariant(ref _new); + change |= ImcManipulationDrawer.DrawVariant(ref _newIdentifier); ImGui.TableNextColumn(); - if (_new.ObjectType is ObjectType.DemiHuman) - change |= ImcManipulationDrawer.DrawSlot(ref _new, 70); + if (_newIdentifier.ObjectType is ObjectType.DemiHuman) + change |= ImcManipulationDrawer.DrawSlot(ref _newIdentifier, 70); else ImGui.Dummy(new Vector2(70 * UiHelpers.Scale, 0)); if (change) - _new = _new.Copy(GetDefault(metaFileManager, _new) ?? new ImcEntry()); + defaultEntry = metaFileManager.ImcChecker.GetDefaultEntry(_newIdentifier, true).Entry; // Values using var disabled = ImRaii.Disabled(); - - var entry = defaultEntry.Value; ImGui.TableNextColumn(); - ImcManipulationDrawer.DrawMaterialId(entry, ref entry, false); + ImcManipulationDrawer.DrawMaterialId(defaultEntry, ref defaultEntry, false); ImGui.SameLine(); - ImcManipulationDrawer.DrawMaterialAnimationId(entry, ref entry, false); + ImcManipulationDrawer.DrawMaterialAnimationId(defaultEntry, ref defaultEntry, false); ImGui.TableNextColumn(); - ImcManipulationDrawer.DrawDecalId(entry, ref entry, false); + ImcManipulationDrawer.DrawDecalId(defaultEntry, ref defaultEntry, false); ImGui.SameLine(); - ImcManipulationDrawer.DrawVfxId(entry, ref entry, false); + ImcManipulationDrawer.DrawVfxId(defaultEntry, ref defaultEntry, false); ImGui.SameLine(); - ImcManipulationDrawer.DrawSoundId(entry, ref entry, false); + ImcManipulationDrawer.DrawSoundId(defaultEntry, ref defaultEntry, false); ImGui.TableNextColumn(); - ImcManipulationDrawer.DrawAttributes(entry, ref entry); + ImcManipulationDrawer.DrawAttributes(defaultEntry, ref defaultEntry); } public static void Draw(MetaFileManager metaFileManager, ImcManipulation meta, ModEditor editor, Vector2 iconSize) @@ -439,10 +421,9 @@ public partial class ModEditWindow using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, new Vector2(3 * UiHelpers.Scale, ImGui.GetStyle().ItemSpacing.Y)); ImGui.TableNextColumn(); - var defaultEntry = GetDefault(metaFileManager, meta) ?? new ImcEntry(); + var defaultEntry = metaFileManager.ImcChecker.GetDefaultEntry(meta.Identifier, true).Entry; var newEntry = meta.Entry; - - var changes = ImcManipulationDrawer.DrawMaterialId(defaultEntry, ref newEntry, true); + var changes = ImcManipulationDrawer.DrawMaterialId(defaultEntry, ref newEntry, true); ImGui.SameLine(); changes |= ImcManipulationDrawer.DrawMaterialAnimationId(defaultEntry, ref newEntry, true); ImGui.TableNextColumn(); diff --git a/Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs b/Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs index 2d80d3df..3ac10cd0 100644 --- a/Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs @@ -5,7 +5,6 @@ using Penumbra.Api.Enums; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Meta; -using Penumbra.Meta.Files; using Penumbra.Meta.Manipulations; using Penumbra.Mods; using Penumbra.Mods.Manager; @@ -17,20 +16,20 @@ namespace Penumbra.UI.ModsTab.Groups; public class AddGroupDrawer : IUiService { private string _groupName = string.Empty; - private bool _groupNameValid = false; + private bool _groupNameValid; - private ImcManipulation _imcManip = new(EquipSlot.Head, 1, 1, new ImcEntry()); - private ImcEntry _defaultEntry; - private bool _imcFileExists; - private bool _entryExists; - private bool _entryInvalid; - private readonly MetaFileManager _metaManager; - private readonly ModManager _modManager; + private ImcIdentifier _imcIdentifier = ImcIdentifier.Default; + private ImcEntry _defaultEntry; + private bool _imcFileExists; + private bool _entryExists; + private bool _entryInvalid; + private readonly ImcChecker _imcChecker; + private readonly ModManager _modManager; - public AddGroupDrawer(MetaFileManager metaManager, ModManager modManager) + public AddGroupDrawer(ModManager modManager, ImcChecker imcChecker) { - _metaManager = metaManager; _modManager = modManager; + _imcChecker = imcChecker; UpdateEntry(); } @@ -61,7 +60,7 @@ public class AddGroupDrawer : IUiService return; _modManager.OptionEditor.AddModGroup(mod, GroupType.Single, _groupName); - _groupName = string.Empty; + _groupName = string.Empty; _groupNameValid = false; } @@ -74,35 +73,35 @@ public class AddGroupDrawer : IUiService return; _modManager.OptionEditor.AddModGroup(mod, GroupType.Multi, _groupName); - _groupName = string.Empty; + _groupName = string.Empty; _groupNameValid = false; } private void DrawImcInput(float width) { - var change = ImcManipulationDrawer.DrawObjectType(ref _imcManip, width); + var change = ImcManipulationDrawer.DrawObjectType(ref _imcIdentifier, width); ImUtf8.SameLineInner(); - change |= ImcManipulationDrawer.DrawPrimaryId(ref _imcManip, width); - if (_imcManip.ObjectType is ObjectType.Weapon or ObjectType.Monster) + change |= ImcManipulationDrawer.DrawPrimaryId(ref _imcIdentifier, width); + if (_imcIdentifier.ObjectType is ObjectType.Weapon or ObjectType.Monster) { - change |= ImcManipulationDrawer.DrawSecondaryId(ref _imcManip, width); + change |= ImcManipulationDrawer.DrawSecondaryId(ref _imcIdentifier, width); ImUtf8.SameLineInner(); - change |= ImcManipulationDrawer.DrawVariant(ref _imcManip, width); + change |= ImcManipulationDrawer.DrawVariant(ref _imcIdentifier, width); } - else if (_imcManip.ObjectType is ObjectType.DemiHuman) + else if (_imcIdentifier.ObjectType is ObjectType.DemiHuman) { var quarterWidth = (width - ImUtf8.ItemInnerSpacing.X / ImUtf8.GlobalScale) / 2; - change |= ImcManipulationDrawer.DrawSecondaryId(ref _imcManip, width); + change |= ImcManipulationDrawer.DrawSecondaryId(ref _imcIdentifier, width); ImUtf8.SameLineInner(); - change |= ImcManipulationDrawer.DrawSlot(ref _imcManip, quarterWidth); + change |= ImcManipulationDrawer.DrawSlot(ref _imcIdentifier, quarterWidth); ImUtf8.SameLineInner(); - change |= ImcManipulationDrawer.DrawVariant(ref _imcManip, quarterWidth); + change |= ImcManipulationDrawer.DrawVariant(ref _imcIdentifier, quarterWidth); } else { - change |= ImcManipulationDrawer.DrawSlot(ref _imcManip, width); + change |= ImcManipulationDrawer.DrawSlot(ref _imcIdentifier, width); ImUtf8.SameLineInner(); - change |= ImcManipulationDrawer.DrawVariant(ref _imcManip, width); + change |= ImcManipulationDrawer.DrawVariant(ref _imcIdentifier, width); } if (change) @@ -125,8 +124,8 @@ public class AddGroupDrawer : IUiService : "Add a new multi selection option group to this mod."u8, width, !_groupNameValid || _entryInvalid)) { - _modManager.OptionEditor.ImcEditor.AddModGroup(mod, _groupName, _imcManip); - _groupName = string.Empty; + _modManager.OptionEditor.ImcEditor.AddModGroup(mod, _groupName, _imcIdentifier, _defaultEntry); + _groupName = string.Empty; _groupNameValid = false; } @@ -142,20 +141,7 @@ public class AddGroupDrawer : IUiService private void UpdateEntry() { - try - { - _defaultEntry = ImcFile.GetDefault(_metaManager, _imcManip.GamePath(), _imcManip.EquipSlot, _imcManip.Variant, - out _entryExists); - _imcFileExists = true; - } - catch (Exception) - { - _defaultEntry = new ImcEntry(); - _imcFileExists = false; - _entryExists = false; - } - - _imcManip = _imcManip.Copy(_entryExists ? _defaultEntry : new ImcEntry()); - _entryInvalid = !_imcManip.Validate(true); + (_defaultEntry, _imcFileExists, _entryExists) = _imcChecker.GetDefaultEntry(_imcIdentifier, false); + _entryInvalid = !_imcIdentifier.Validate() || _defaultEntry.MaterialId == 0 || !_entryExists; } } diff --git a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs index 2418c5cb..045149c9 100644 --- a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs @@ -1,10 +1,8 @@ using Dalamud.Interface; using ImGuiNET; using OtterGui; -using OtterGui.Classes; using OtterGui.Raii; using OtterGui.Text; -using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Mods.Groups; using Penumbra.Mods.Manager.OptionEditor; @@ -16,59 +14,75 @@ public readonly struct ImcModGroupEditDrawer(ModGroupEditDrawer editor, ImcModGr { public void Draw() { + var identifier = group.Identifier; + var defaultEntry = editor.ImcChecker.GetDefaultEntry(identifier, true).Entry; + var entry = group.DefaultEntry; + var changes = false; + + ImUtf8.TextFramed(identifier.ToString(), 0, editor.AvailableWidth, borderColor: ImGui.GetColorU32(ImGuiCol.Border)); + using (ImUtf8.Group()) { - ImUtf8.Text("Object Type"u8); - if (group.ObjectType is ObjectType.Equipment or ObjectType.Accessory or ObjectType.DemiHuman) - ImUtf8.Text("Slot"u8); - ImUtf8.Text("Primary ID"); - if (group.ObjectType is not ObjectType.Equipment and not ObjectType.Accessory) - ImUtf8.Text("Secondary ID"); - ImUtf8.Text("Variant"u8); - ImUtf8.TextFrameAligned("Material ID"u8); - ImUtf8.TextFrameAligned("Material Animation ID"u8); - ImUtf8.TextFrameAligned("Decal ID"u8); ImUtf8.TextFrameAligned("VFX ID"u8); + ImUtf8.TextFrameAligned("Decal ID"u8); + } + + ImGui.SameLine(); + using (ImUtf8.Group()) + { + changes |= ImcManipulationDrawer.DrawMaterialId(defaultEntry, ref entry, true); + changes |= ImcManipulationDrawer.DrawVfxId(defaultEntry, ref entry, true); + changes |= ImcManipulationDrawer.DrawDecalId(defaultEntry, ref entry, true); + } + + ImGui.SameLine(0, editor.PriorityWidth); + using (ImUtf8.Group()) + { + ImUtf8.TextFrameAligned("Material Animation ID"u8); ImUtf8.TextFrameAligned("Sound ID"u8); ImUtf8.TextFrameAligned("Can Be Disabled"u8); - ImUtf8.TextFrameAligned("Default Attributes"u8); } ImGui.SameLine(); + using (ImUtf8.Group()) + { + changes |= ImcManipulationDrawer.DrawMaterialAnimationId(defaultEntry, ref entry, true); + changes |= ImcManipulationDrawer.DrawSoundId(defaultEntry, ref entry, true); + var canBeDisabled = group.CanBeDisabled; + if (ImUtf8.Checkbox("##disabled"u8, ref canBeDisabled)) + editor.ModManager.OptionEditor.ImcEditor.ChangeCanBeDisabled(group, canBeDisabled); + } + + if (changes) + editor.ModManager.OptionEditor.ImcEditor.ChangeDefaultEntry(group, entry); + + ImGui.Dummy(Vector2.Zero); + DrawOptions(); var attributeCache = new ImcAttributeCache(group); + DrawNewOption(attributeCache); + ImGui.Dummy(Vector2.Zero); + using (ImUtf8.Group()) { - ImUtf8.Text(group.ObjectType.ToName()); - if (group.ObjectType is ObjectType.Equipment or ObjectType.Accessory or ObjectType.DemiHuman) - ImUtf8.Text(group.EquipSlot.ToName()); - ImUtf8.Text($"{group.PrimaryId.Id}"); - if (group.ObjectType is not ObjectType.Equipment and not ObjectType.Accessory) - ImUtf8.Text($"{group.SecondaryId.Id}"); - ImUtf8.Text($"{group.Variant.Id}"); - - ImUtf8.TextFrameAligned($"{group.DefaultEntry.MaterialId}"); - ImUtf8.TextFrameAligned($"{group.DefaultEntry.MaterialAnimationId}"); - ImUtf8.TextFrameAligned($"{group.DefaultEntry.DecalId}"); - ImUtf8.TextFrameAligned($"{group.DefaultEntry.VfxId}"); - ImUtf8.TextFrameAligned($"{group.DefaultEntry.SoundId}"); - - var canBeDisabled = group.CanBeDisabled; - if (ImUtf8.Checkbox("##disabled"u8, ref canBeDisabled)) - editor.ModManager.OptionEditor.ImcEditor.ChangeCanBeDisabled(group, canBeDisabled, SaveType.Queue); - - var defaultDisabled = group.DefaultDisabled; - ImUtf8.SameLineInner(); - if (ImUtf8.Checkbox("##defaultDisabled"u8, ref defaultDisabled)) - editor.ModManager.OptionEditor.ChangeModGroupDefaultOption(group, - group.DefaultSettings.SetBit(ImcModGroup.DisabledIndex, defaultDisabled)); - - DrawAttributes(editor.ModManager.OptionEditor.ImcEditor, attributeCache, group.DefaultEntry.AttributeMask, group); + ImUtf8.TextFrameAligned("Default Attributes"u8); + foreach (var option in group.OptionData.Where(o => !o.IsDisableSubMod)) + ImUtf8.TextFrameAligned(option.Name); } + ImUtf8.SameLineInner(); + using (ImUtf8.Group()) + { + DrawAttributes(editor.ModManager.OptionEditor.ImcEditor, attributeCache, group.DefaultEntry.AttributeMask, group); + foreach (var option in group.OptionData.Where(o => !o.IsDisableSubMod)) + DrawAttributes(editor.ModManager.OptionEditor.ImcEditor, attributeCache, option.AttributeMask, option); + } + } + private void DrawOptions() + { foreach (var (option, optionIdx) in group.OptionData.WithIndex()) { using var id = ImRaii.PushId(optionIdx); @@ -83,56 +97,51 @@ public readonly struct ImcModGroupEditDrawer(ModGroupEditDrawer editor, ImcModGr ImUtf8.SameLineInner(); editor.DrawOptionDescription(option); - ImUtf8.SameLineInner(); - editor.DrawOptionDelete(option); - - ImUtf8.SameLineInner(); - ImGui.Dummy(new Vector2(editor.PriorityWidth, 0)); - - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + editor.OptionIdxSelectable.X + ImUtf8.ItemInnerSpacing.X * 2 + ImUtf8.FrameHeight); - DrawAttributes(editor.ModManager.OptionEditor.ImcEditor, attributeCache, option.AttributeMask, option); - } - - DrawNewOption(attributeCache); - return; - - static void DrawAttributes(ImcModGroupEditor editor, in ImcAttributeCache cache, ushort mask, object data) - { - for (var i = 0; i < ImcEntry.NumAttributes; ++i) + if (!option.IsDisableSubMod) { - using var id = ImRaii.PushId(i); - var value = (mask & 1 << i) != 0; - using (ImRaii.Disabled(!cache.CanChange(i))) - { - if (ImUtf8.Checkbox(TerminatedByteString.Empty, ref value)) - { - if (data is ImcModGroup g) - editor.ChangeDefaultAttribute(g, cache, i, value); - else - editor.ChangeOptionAttribute((ImcSubMod)data, cache, i, value); - } - } - - ImUtf8.HoverTooltip($"{(char)('A' + i)}"); - if (i != 9) - ImUtf8.SameLineInner(); + ImUtf8.SameLineInner(); + editor.DrawOptionDelete(option); } } } private void DrawNewOption(in ImcAttributeCache cache) { - if (cache.LowestUnsetMask == 0) - return; - - var name = editor.DrawNewOptionBase(group, group.Options.Count); + var dis = cache.LowestUnsetMask == 0; + var name = editor.DrawNewOptionBase(group, group.Options.Count); var validName = name.Length > 0; - if (ImUtf8.IconButton(FontAwesomeIcon.Plus, validName + var tt = dis + ? "No Free Attribute Slots for New Options..."u8 + : validName ? "Add a new option to this group."u8 - : "Please enter a name for the new option."u8, !validName)) + : "Please enter a name for the new option."u8; + if (ImUtf8.IconButton(FontAwesomeIcon.Plus, tt, !validName || dis)) { editor.ModManager.OptionEditor.ImcEditor.AddOption(group, cache, name); editor.NewOptionName = null; } } + + private static void DrawAttributes(ImcModGroupEditor editor, in ImcAttributeCache cache, ushort mask, object data) + { + for (var i = 0; i < ImcEntry.NumAttributes; ++i) + { + using var id = ImRaii.PushId(i); + var value = (mask & (1 << i)) != 0; + using (ImRaii.Disabled(!cache.CanChange(i))) + { + if (ImUtf8.Checkbox(TerminatedByteString.Empty, ref value)) + { + if (data is ImcModGroup g) + editor.ChangeDefaultAttribute(g, cache, i, value); + else + editor.ChangeOptionAttribute((ImcSubMod)data, cache, i, value); + } + } + + ImUtf8.HoverTooltip("ABCDEFGHIJ"u8.Slice(i, 1)); + if (i != 9) + ImUtf8.SameLineInner(); + } + } } diff --git a/Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs index e7d70922..e8a27a74 100644 --- a/Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs @@ -7,6 +7,7 @@ using OtterGui.Raii; using OtterGui.Services; using OtterGui.Text; using OtterGui.Text.EndObjects; +using Penumbra.Meta; using Penumbra.Mods; using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; @@ -22,11 +23,16 @@ public sealed class ModGroupEditDrawer( ModManager modManager, Configuration config, FilenameService filenames, - DescriptionEditPopup descriptionPopup) : IUiService + DescriptionEditPopup descriptionPopup, + ImcChecker imcChecker) : IUiService { - private static ReadOnlySpan DragDropLabel - => "##DragOption"u8; + private static ReadOnlySpan AcrossGroupsLabel + => "##DragOptionAcross"u8; + private static ReadOnlySpan InsideGroupLabel + => "##DragOptionInside"u8; + + internal readonly ImcChecker ImcChecker = imcChecker; internal readonly ModManager ModManager = modManager; internal readonly Queue ActionQueue = new(); @@ -50,6 +56,7 @@ public sealed class ModGroupEditDrawer( private IModGroup? _dragDropGroup; private IModOption? _dragDropOption; + private bool _draggingAcross; public void Draw(Mod mod) { @@ -292,32 +299,30 @@ public sealed class ModGroupEditDrawer( [MethodImpl(MethodImplOptions.AggressiveInlining)] private void Source(IModOption option) { - if (option.Group is not ITexToolsGroup) - return; - using var source = ImUtf8.DragDropSource(); if (!source) return; - if (!DragDropSource.SetPayload(DragDropLabel)) + var across = option.Group is ITexToolsGroup; + + if (!DragDropSource.SetPayload(across ? AcrossGroupsLabel : InsideGroupLabel)) { _dragDropGroup = option.Group; _dragDropOption = option; + _draggingAcross = across; } - ImGui.TextUnformatted($"Dragging option {option.Name} from group {option.Group.Name}..."); + ImUtf8.Text($"Dragging option {option.Name} from group {option.Group.Name}..."); } private void Target(IModGroup group, int optionIdx) { - if (group is not ITexToolsGroup) - return; - - if (_dragDropGroup != group && _dragDropGroup != null && group is MultiModGroup { Options.Count: >= IModGroup.MaxMultiOptions }) + if (_dragDropGroup != group + && (!_draggingAcross || (_dragDropGroup != null && group is MultiModGroup { Options.Count: >= IModGroup.MaxMultiOptions }))) return; using var target = ImRaii.DragDropTarget(); - if (!target.Success || !DragDropTarget.CheckPayload(DragDropLabel)) + if (!target.Success || !DragDropTarget.CheckPayload(_draggingAcross ? AcrossGroupsLabel : InsideGroupLabel)) return; if (_dragDropGroup != null && _dragDropOption != null) @@ -342,6 +347,7 @@ public sealed class ModGroupEditDrawer( _dragDropGroup = null; _dragDropOption = null; + _draggingAcross = false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs b/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs index 5873119e..c14652ac 100644 --- a/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs +++ b/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs @@ -1,8 +1,6 @@ using ImGuiNET; -using OtterGui; using OtterGui.Raii; using OtterGui.Text; -using OtterGui.Text.HelperObjects; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Meta.Manipulations; @@ -12,79 +10,78 @@ namespace Penumbra.UI.ModsTab; public static class ImcManipulationDrawer { - public static bool DrawObjectType(ref ImcManipulation manip, float width = 110) + public static bool DrawObjectType(ref ImcIdentifier identifier, float width = 110) { - var ret = Combos.ImcType("##imcType", manip.ObjectType, out var type, width); + var ret = Combos.ImcType("##imcType", identifier.ObjectType, out var type, width); ImUtf8.HoverTooltip("Object Type"u8); if (ret) { var equipSlot = type switch { - ObjectType.Equipment => manip.EquipSlot.IsEquipment() ? manip.EquipSlot : EquipSlot.Head, - ObjectType.DemiHuman => manip.EquipSlot.IsEquipment() ? manip.EquipSlot : EquipSlot.Head, - ObjectType.Accessory => manip.EquipSlot.IsAccessory() ? manip.EquipSlot : EquipSlot.Ears, + ObjectType.Equipment => identifier.EquipSlot.IsEquipment() ? identifier.EquipSlot : EquipSlot.Head, + ObjectType.DemiHuman => identifier.EquipSlot.IsEquipment() ? identifier.EquipSlot : EquipSlot.Head, + ObjectType.Accessory => identifier.EquipSlot.IsAccessory() ? identifier.EquipSlot : EquipSlot.Ears, _ => EquipSlot.Unknown, }; - manip = new ImcManipulation(type, manip.BodySlot, manip.PrimaryId, manip.SecondaryId == 0 ? 1 : manip.SecondaryId, - manip.Variant.Id, equipSlot, manip.Entry); + identifier = identifier with + { + EquipSlot = equipSlot, + SecondaryId = identifier.SecondaryId == 0 ? 1 : identifier.SecondaryId, + }; } return ret; } - public static bool DrawPrimaryId(ref ImcManipulation manip, float unscaledWidth = 80) + public static bool DrawPrimaryId(ref ImcIdentifier identifier, float unscaledWidth = 80) { - var ret = IdInput("##imcPrimaryId"u8, unscaledWidth, manip.PrimaryId.Id, out var newId, 0, ushort.MaxValue, - manip.PrimaryId.Id <= 1); + var ret = IdInput("##imcPrimaryId"u8, unscaledWidth, identifier.PrimaryId.Id, out var newId, 0, ushort.MaxValue, + identifier.PrimaryId.Id <= 1); ImUtf8.HoverTooltip("Primary ID - You can usually find this as the 'x####' part of an item path.\n"u8 + "This should generally not be left <= 1 unless you explicitly want that."u8); if (ret) - manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, newId, manip.SecondaryId, manip.Variant.Id, manip.EquipSlot, - manip.Entry); + identifier = identifier with { PrimaryId = newId }; return ret; } - public static bool DrawSecondaryId(ref ImcManipulation manip, float unscaledWidth = 100) + public static bool DrawSecondaryId(ref ImcIdentifier identifier, float unscaledWidth = 100) { - var ret = IdInput("##imcSecondaryId"u8, unscaledWidth, manip.SecondaryId.Id, out var newId, 0, ushort.MaxValue, false); + var ret = IdInput("##imcSecondaryId"u8, unscaledWidth, identifier.SecondaryId.Id, out var newId, 0, ushort.MaxValue, false); ImUtf8.HoverTooltip("Secondary ID"u8); if (ret) - manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, newId, manip.Variant.Id, manip.EquipSlot, - manip.Entry); + identifier = identifier with { SecondaryId = newId }; return ret; } - public static bool DrawVariant(ref ImcManipulation manip, float unscaledWidth = 45) + public static bool DrawVariant(ref ImcIdentifier identifier, float unscaledWidth = 45) { - var ret = IdInput("##imcVariant"u8, unscaledWidth, manip.Variant.Id, out var newId, 0, byte.MaxValue, false); + var ret = IdInput("##imcVariant"u8, unscaledWidth, identifier.Variant.Id, out var newId, 0, byte.MaxValue, false); ImUtf8.HoverTooltip("Variant ID"u8); if (ret) - manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, manip.SecondaryId, (byte)newId, manip.EquipSlot, - manip.Entry); + identifier = identifier with { Variant = (byte)newId }; return ret; } - public static bool DrawSlot(ref ImcManipulation manip, float unscaledWidth = 100) + public static bool DrawSlot(ref ImcIdentifier identifier, float unscaledWidth = 100) { bool ret; EquipSlot slot; - switch (manip.ObjectType) + switch (identifier.ObjectType) { case ObjectType.Equipment: case ObjectType.DemiHuman: - ret = Combos.EqpEquipSlot("##slot", manip.EquipSlot, out slot, unscaledWidth); + ret = Combos.EqpEquipSlot("##slot", identifier.EquipSlot, out slot, unscaledWidth); break; case ObjectType.Accessory: - ret = Combos.AccessorySlot("##slot", manip.EquipSlot, out slot, unscaledWidth); + ret = Combos.AccessorySlot("##slot", identifier.EquipSlot, out slot, unscaledWidth); break; default: return false; } ImUtf8.HoverTooltip("Equip Slot"u8); if (ret) - manip = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, manip.SecondaryId, manip.Variant.Id, slot, - manip.Entry); + identifier = identifier with { EquipSlot = slot }; return ret; } From bad1f45ab91f54576da1041577eaeca83cb653ef Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 24 May 2024 17:34:56 +0200 Subject: [PATCH 167/433] Use different hooking method for EQP entries. --- Penumbra.GameData | 2 +- Penumbra/Interop/Hooks/Meta/EqpHook.cs | 34 ++++++++++++++++++ Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs | 5 +-- .../Interop/Hooks/Meta/GetEqpIndirect2.cs | 36 +++++++++++++++++++ .../Interop/Hooks/Meta/ModelLoadComplete.cs | 4 ++- Penumbra/Interop/Hooks/Meta/UpdateModel.cs | 6 ++-- Penumbra/Interop/PathResolving/MetaState.cs | 1 + 7 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 Penumbra/Interop/Hooks/Meta/EqpHook.cs create mode 100644 Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs diff --git a/Penumbra.GameData b/Penumbra.GameData index ec35e664..539d1387 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit ec35e66499eb388b4e7917e4fae4615218d33335 +Subproject commit 539d138700543e7c2c6c918f9f68e33228111e4d diff --git a/Penumbra/Interop/Hooks/Meta/EqpHook.cs b/Penumbra/Interop/Hooks/Meta/EqpHook.cs new file mode 100644 index 00000000..457b9428 --- /dev/null +++ b/Penumbra/Interop/Hooks/Meta/EqpHook.cs @@ -0,0 +1,34 @@ +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using OtterGui.Services; +using Penumbra.GameData.Structs; +using Penumbra.Interop.PathResolving; + +namespace Penumbra.Interop.Hooks.Meta; + +public unsafe class EqpHook : FastHook +{ + public delegate void Delegate(CharacterUtility* utility, EqpEntry* flags, CharacterArmor* armor); + + private readonly MetaState _metaState; + + public EqpHook(HookManager hooks, MetaState metaState) + { + _metaState = metaState; + Task = hooks.CreateHook("GetEqpFlags", "E8 ?? ?? ?? ?? 0F B6 44 24 ?? C0 E8", Detour, true); + } + + private void Detour(CharacterUtility* utility, EqpEntry* flags, CharacterArmor* armor) + { + if (_metaState.EqpCollection.Valid) + { + using var eqp = _metaState.ResolveEqpData(_metaState.EqpCollection.ModCollection); + Task.Result.Original(utility, flags, armor); + } + else + { + Task.Result.Original(utility, flags, armor); + } + + Penumbra.Log.Excessive($"[GetEqpFlags] Invoked on 0x{(nint)utility:X} with 0x{(ulong)armor:X}, returned 0x{(ulong)*flags:X16}."); + } +} diff --git a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs index 8ffc050f..beae6acc 100644 --- a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs +++ b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs @@ -1,5 +1,6 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; +using Penumbra.Collections; using Penumbra.GameData; using Penumbra.Interop.PathResolving; @@ -28,8 +29,8 @@ public sealed unsafe class GetEqpIndirect : FastHook return; Penumbra.Log.Excessive($"[Get EQP Indirect] Invoked on {(nint)drawObject:X}."); - var collection = _collectionResolver.IdentifyCollection(drawObject, true); - using var eqp = _metaState.ResolveEqpData(collection.ModCollection); + _metaState.EqpCollection = _collectionResolver.IdentifyCollection(drawObject, true); Task.Result.Original(drawObject); + _metaState.EqpCollection = ResolveData.Invalid; } } diff --git a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs new file mode 100644 index 00000000..89aaa9b0 --- /dev/null +++ b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs @@ -0,0 +1,36 @@ +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using OtterGui.Services; +using Penumbra.Collections; +using Penumbra.GameData; +using Penumbra.Interop.PathResolving; + +namespace Penumbra.Interop.Hooks.Meta; + +public sealed unsafe class GetEqpIndirect2 : FastHook +{ + private readonly CollectionResolver _collectionResolver; + private readonly MetaState _metaState; + + public GetEqpIndirect2(HookManager hooks, CollectionResolver collectionResolver, MetaState metaState) + { + _collectionResolver = collectionResolver; + _metaState = metaState; + Task = hooks.CreateHook("Get EQP Indirect 2", Sigs.GetEqpIndirect2, Detour, true); + } + + public delegate void Delegate(DrawObject* drawObject); + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + private void Detour(DrawObject* drawObject) + { + // Shortcut because this is also called all the time. + // Same thing is checked at the beginning of the original function. + if (((*(uint*)((nint)drawObject + Offsets.GetEqpIndirect2Skip) >> 0x12) & 1) == 0) + return; + + Penumbra.Log.Excessive($"[Get EQP Indirect 2] Invoked on {(nint)drawObject:X}."); + _metaState.EqpCollection = _collectionResolver.IdentifyCollection(drawObject, true); + Task.Result.Original(drawObject); + _metaState.EqpCollection = ResolveData.Invalid; + } +} diff --git a/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs b/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs index 9f191fdd..10c12594 100644 --- a/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs +++ b/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs @@ -1,5 +1,6 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; +using Penumbra.Collections; using Penumbra.Interop.PathResolving; namespace Penumbra.Interop.Hooks.Meta; @@ -23,8 +24,9 @@ public sealed unsafe class ModelLoadComplete : FastHook Penumbra.Log.Excessive($"[Update Model] Invoked on {(nint)drawObject:X}."); var collection = _collectionResolver.IdentifyCollection(drawObject, true); - using var eqp = _metaState.ResolveEqpData(collection.ModCollection); using var eqdp = _metaState.ResolveEqdpData(collection.ModCollection, MetaState.GetDrawObjectGenderRace((nint)drawObject), true, true); - Task.Result.Original.Invoke(drawObject); + _metaState.EqpCollection = collection; + Task.Result.Original(drawObject); + _metaState.EqpCollection = ResolveData.Invalid; } } diff --git a/Penumbra/Interop/PathResolving/MetaState.cs b/Penumbra/Interop/PathResolving/MetaState.cs index 5f07ffc5..6fa5c263 100644 --- a/Penumbra/Interop/PathResolving/MetaState.cs +++ b/Penumbra/Interop/PathResolving/MetaState.cs @@ -46,6 +46,7 @@ public sealed unsafe class MetaState : IDisposable private readonly CreateCharacterBase _createCharacterBase; public ResolveData CustomizeChangeCollection = ResolveData.Invalid; + public ResolveData EqpCollection = ResolveData.Invalid; private ResolveData _lastCreatedCollection = ResolveData.Invalid; private DisposableContainer _characterBaseCreateMetaChanges = DisposableContainer.Empty; From e32e314863e46fb4b806bc9b2c3adf86a5ec2d0e Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 24 May 2024 17:51:47 +0200 Subject: [PATCH 168/433] Add initial changelog for next release. --- Penumbra/UI/Changelog.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Penumbra/UI/Changelog.cs b/Penumbra/UI/Changelog.cs index 8b00ab8d..add16f94 100644 --- a/Penumbra/UI/Changelog.cs +++ b/Penumbra/UI/Changelog.cs @@ -48,10 +48,34 @@ public class PenumbraChangelog Add8_3_0(Changelog); Add1_0_0_0(Changelog); Add1_0_2_0(Changelog); + Add1_0_3_0(Changelog); } #region Changelogs + private static void Add1_0_3_0(Changelog log) + => log.NextVersion("Version 1.0.3.0") + .RegisterHighlight("Collections now have associated GUIDs as identifiers instead of their names, so they can now be renamed.") + .RegisterEntry("Migrating those collections may introduce issues, please let me know as soon as possible if you encounter any.", 1) + .RegisterEntry("A permanent (non-rolling) backup should be created before the migration in case of any issues.", 1) + .RegisterHighlight("A total rework of how options and groups are handled internally, and introduction of the first new group type, the IMC Group.") + .RegisterEntry("Mod Creators can add a IMC Group to their mod that controls a single IMC Manipulation, so they can provide options for the separate attributes for it.", 1) + .RegisterEntry("This makes it a lot easier to have combined options: No need for 'A', 'B' and 'AB', you can just define 'A' and 'B' and skip their combinations", 1) + .RegisterHighlight("Added a field to rename mods directly from the mod selector context menu, instead of moving them in the filesystem.") + .RegisterEntry("You can choose which rename field (none, either one or both) to display in the settings.", 1) + .RegisterEntry("You can now paste your current clipboard text into the mod selector filter with a simple right-click as long as it is not focused.") + .RegisterHighlight("Added the option to display VFX for accessories if added via IMC edits, which the game does not do inherently (by Ocealot).") + .RegisterEntry("Added support for reading and writing the new material and model file formats from the benchmark.") + .RegisterEntry("Added the option to hide Machinist Offhands from the Changed Items tabs (because any change to it changes ALL of them), which is on by default.") + .RegisterEntry("Removed the auto-generated descriptions for newly created groups in Penumbra.") + .RegisterEntry("Made some improvements to the Advanced Editing window, for example a much better and more performant Hex Viewer for unstructured data was added.") + .RegisterEntry("Made a lot of further improvements on Model import/export (by ackwell).") + .RegisterEntry("Reworked the API and IPC structure heavily.") + .RegisterEntry("Worked around the UI IPC possibly displacing all settings when the drawn additions became too big.") + .RegisterEntry("Fixed an issue with merging and deduplicating mods.") + .RegisterEntry("Fixed a crash when scanning for mods without access rights to the folder.") + .RegisterEntry("Made plugin conform to Dalamud requirements by adding a punchline and another button to open the menu from the installer."); + private static void Add1_0_2_0(Changelog log) => log.NextVersion("Version 1.0.2.0") .RegisterEntry("Updated to .net8 and XIV 6.58, using some new framework facilities to improve performance and stability.") From f9527970cb610f8d8529478e9674d1bd32548bf9 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 25 May 2024 00:43:02 +0200 Subject: [PATCH 169/433] Fix missing ID push for imc attributes. --- Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs index 045149c9..b10f123c 100644 --- a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs @@ -76,8 +76,11 @@ public readonly struct ImcModGroupEditDrawer(ModGroupEditDrawer editor, ImcModGr using (ImUtf8.Group()) { DrawAttributes(editor.ModManager.OptionEditor.ImcEditor, attributeCache, group.DefaultEntry.AttributeMask, group); - foreach (var option in group.OptionData.Where(o => !o.IsDisableSubMod)) + foreach (var (option, idx) in group.OptionData.WithIndex().Where(o => !o.Value.IsDisableSubMod)) + { + using var id = ImUtf8.PushId(idx); DrawAttributes(editor.ModManager.OptionEditor.ImcEditor, attributeCache, option.AttributeMask, option); + } } } From ed083f2a4ca375bf50eede01d72796408c320d7c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 26 May 2024 13:30:35 +0200 Subject: [PATCH 170/433] Add support for Global EQP Changes. --- Penumbra.GameData | 2 +- Penumbra/Collections/Cache/CollectionCache.cs | 1 - Penumbra/Collections/Cache/MetaCache.cs | 48 ++++-- .../Collections/ModCollection.Cache.Access.cs | 4 + Penumbra/Import/TexToolsMeta.Export.cs | 3 + Penumbra/Import/TexToolsMeta.cs | 2 +- Penumbra/Interop/Hooks/Meta/EqpHook.cs | 1 + Penumbra/Meta/Manipulations/GlobalEqpCache.cs | 80 +++++++++ .../Manipulations/GlobalEqpManipulation.cs | 50 ++++++ Penumbra/Meta/Manipulations/GlobalEqpType.cs | 61 +++++++ .../Meta/Manipulations/MetaManipulation.cs | 156 ++++++++++-------- Penumbra/Mods/Editor/ModMetaEditor.cs | 97 ++++++----- Penumbra/Mods/SubMods/IModDataContainer.cs | 13 +- .../UI/AdvancedWindow/ModEditWindow.Meta.cs | 66 ++++++++ Penumbra/Util/IdentifierExtensions.cs | 20 +++ 15 files changed, 471 insertions(+), 133 deletions(-) create mode 100644 Penumbra/Meta/Manipulations/GlobalEqpCache.cs create mode 100644 Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs create mode 100644 Penumbra/Meta/Manipulations/GlobalEqpType.cs diff --git a/Penumbra.GameData b/Penumbra.GameData index 539d1387..07cc26f1 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 539d138700543e7c2c6c918f9f68e33228111e4d +Subproject commit 07cc26f196984a44711b3bc4c412947d863288bd diff --git a/Penumbra/Collections/Cache/CollectionCache.cs b/Penumbra/Collections/Cache/CollectionCache.cs index e2f20b46..4d8d0b4a 100644 --- a/Penumbra/Collections/Cache/CollectionCache.cs +++ b/Penumbra/Collections/Cache/CollectionCache.cs @@ -5,7 +5,6 @@ using Penumbra.Mods; using Penumbra.Communication; using Penumbra.Mods.Editor; using Penumbra.String.Classes; -using Penumbra.Mods.Manager; using Penumbra.Util; namespace Penumbra.Collections.Cache; diff --git a/Penumbra/Collections/Cache/MetaCache.cs b/Penumbra/Collections/Cache/MetaCache.cs index 4c147c3c..f42b72fc 100644 --- a/Penumbra/Collections/Cache/MetaCache.cs +++ b/Penumbra/Collections/Cache/MetaCache.cs @@ -4,7 +4,6 @@ using Penumbra.Interop.Services; using Penumbra.Interop.Structs; using Penumbra.Meta; using Penumbra.Meta.Manipulations; -using Penumbra.Mods; using Penumbra.Mods.Editor; using Penumbra.String.Classes; @@ -14,13 +13,14 @@ public class MetaCache : IDisposable, IEnumerable _manipulations = new(); - private EqpCache _eqpCache = new(); - private readonly EqdpCache _eqdpCache = new(); - private EstCache _estCache = new(); - private GmpCache _gmpCache = new(); - private CmpCache _cmpCache = new(); - private readonly ImcCache _imcCache = new(); + private readonly Dictionary _manipulations = new(); + private EqpCache _eqpCache = new(); + private readonly EqdpCache _eqdpCache = new(); + private EstCache _estCache = new(); + private GmpCache _gmpCache = new(); + private CmpCache _cmpCache = new(); + private readonly ImcCache _imcCache = new(); + private GlobalEqpCache _globalEqpCache = new(); public bool TryGetValue(MetaManipulation manip, [NotNullWhen(true)] out IMod? mod) { @@ -69,6 +69,7 @@ public class MetaCache : IDisposable, IEnumerable _estCache.TemporarilySetFiles(_manager, type); + public unsafe EqpEntry ApplyGlobalEqp(EqpEntry baseEntry, CharacterArmor* armor) + => _globalEqpCache.Apply(baseEntry, armor); + /// Try to obtain a manipulated IMC file. public bool GetImcFile(Utf8GamePath path, [NotNullWhen(true)] out Meta.Files.ImcFile? file) @@ -193,8 +204,8 @@ public class MetaCache : IDisposable, IEnumerable _eqpCache.ApplyMod(_manager, manip.Eqp), - MetaManipulation.Type.Eqdp => _eqdpCache.ApplyMod(_manager, manip.Eqdp), - MetaManipulation.Type.Est => _estCache.ApplyMod(_manager, manip.Est), - MetaManipulation.Type.Gmp => _gmpCache.ApplyMod(_manager, manip.Gmp), - MetaManipulation.Type.Rsp => _cmpCache.ApplyMod(_manager, manip.Rsp), - MetaManipulation.Type.Imc => _imcCache.ApplyMod(_manager, _collection, manip.Imc), - MetaManipulation.Type.Unknown => false, - _ => false, + MetaManipulation.Type.Eqp => _eqpCache.ApplyMod(_manager, manip.Eqp), + MetaManipulation.Type.Eqdp => _eqdpCache.ApplyMod(_manager, manip.Eqdp), + MetaManipulation.Type.Est => _estCache.ApplyMod(_manager, manip.Est), + MetaManipulation.Type.Gmp => _gmpCache.ApplyMod(_manager, manip.Gmp), + MetaManipulation.Type.Rsp => _cmpCache.ApplyMod(_manager, manip.Rsp), + MetaManipulation.Type.Imc => _imcCache.ApplyMod(_manager, _collection, manip.Imc), + MetaManipulation.Type.GlobalEqp => false, + MetaManipulation.Type.Unknown => false, + _ => false, } ? 1 : 0; diff --git a/Penumbra/Collections/ModCollection.Cache.Access.cs b/Penumbra/Collections/ModCollection.Cache.Access.cs index b073e731..3f3733e0 100644 --- a/Penumbra/Collections/ModCollection.Cache.Access.cs +++ b/Penumbra/Collections/ModCollection.Cache.Access.cs @@ -8,6 +8,7 @@ using Penumbra.String.Classes; using Penumbra.Collections.Cache; using Penumbra.Interop.Services; using Penumbra.Mods.Editor; +using Penumbra.GameData.Structs; namespace Penumbra.Collections; @@ -114,4 +115,7 @@ public partial class ModCollection public MetaList.MetaReverter TemporarilySetEstFile(CharacterUtility utility, EstManipulation.EstType type) => _cache?.Meta.TemporarilySetEstFile(type) ?? utility.TemporarilyResetResource((MetaIndex)type); + + public unsafe EqpEntry ApplyGlobalEqp(EqpEntry baseEntry, CharacterArmor* armor) + => _cache?.Meta.ApplyGlobalEqp(baseEntry, armor) ?? baseEntry; } diff --git a/Penumbra/Import/TexToolsMeta.Export.cs b/Penumbra/Import/TexToolsMeta.Export.cs index 90ffaf60..03bdbd90 100644 --- a/Penumbra/Import/TexToolsMeta.Export.cs +++ b/Penumbra/Import/TexToolsMeta.Export.cs @@ -187,6 +187,9 @@ public partial class TexToolsMeta b.Write(manip.Gmp.Entry.UnknownTotal); } + break; + case MetaManipulation.Type.GlobalEqp: + // Not Supported break; } diff --git a/Penumbra/Import/TexToolsMeta.cs b/Penumbra/Import/TexToolsMeta.cs index 83b430fb..25e00bd7 100644 --- a/Penumbra/Import/TexToolsMeta.cs +++ b/Penumbra/Import/TexToolsMeta.cs @@ -44,7 +44,7 @@ public partial class TexToolsMeta var headerStart = reader.ReadUInt32(); reader.BaseStream.Seek(headerStart, SeekOrigin.Begin); - List<(MetaManipulation.Type type, uint offset, int size)> entries = new(); + List<(MetaManipulation.Type type, uint offset, int size)> entries = []; for (var i = 0; i < numHeaders; ++i) { var currentOffset = reader.BaseStream.Position; diff --git a/Penumbra/Interop/Hooks/Meta/EqpHook.cs b/Penumbra/Interop/Hooks/Meta/EqpHook.cs index 457b9428..6663c211 100644 --- a/Penumbra/Interop/Hooks/Meta/EqpHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqpHook.cs @@ -23,6 +23,7 @@ public unsafe class EqpHook : FastHook { using var eqp = _metaState.ResolveEqpData(_metaState.EqpCollection.ModCollection); Task.Result.Original(utility, flags, armor); + *flags = _metaState.EqpCollection.ModCollection.ApplyGlobalEqp(*flags, armor); } else { diff --git a/Penumbra/Meta/Manipulations/GlobalEqpCache.cs b/Penumbra/Meta/Manipulations/GlobalEqpCache.cs new file mode 100644 index 00000000..48ffb308 --- /dev/null +++ b/Penumbra/Meta/Manipulations/GlobalEqpCache.cs @@ -0,0 +1,80 @@ +using OtterGui.Services; +using Penumbra.GameData.Structs; + +namespace Penumbra.Meta.Manipulations; + +public struct GlobalEqpCache : IService +{ + private readonly HashSet _doNotHideEarrings = []; + private readonly HashSet _doNotHideNecklace = []; + private readonly HashSet _doNotHideBracelets = []; + private readonly HashSet _doNotHideRingL = []; + private readonly HashSet _doNotHideRingR = []; + private bool _doNotHideVieraHats; + private bool _doNotHideHrothgarHats; + + public GlobalEqpCache() + { } + + public void Clear() + { + _doNotHideEarrings.Clear(); + _doNotHideNecklace.Clear(); + _doNotHideBracelets.Clear(); + _doNotHideRingL.Clear(); + _doNotHideRingR.Clear(); + _doNotHideHrothgarHats = false; + _doNotHideVieraHats = false; + } + + public unsafe EqpEntry Apply(EqpEntry original, CharacterArmor* armor) + { + if (_doNotHideVieraHats) + original |= EqpEntry.HeadShowVieraHat; + + if (_doNotHideHrothgarHats) + original |= EqpEntry.HeadShowHrothgarHat; + + if (_doNotHideEarrings.Contains(armor[5].Set)) + original |= EqpEntry.HeadShowEarrings | EqpEntry.HeadShowEarringsAura | EqpEntry.HeadShowEarringsHuman; + + if (_doNotHideNecklace.Contains(armor[6].Set)) + original |= EqpEntry.BodyShowNecklace | EqpEntry.HeadShowNecklace; + + if (_doNotHideBracelets.Contains(armor[7].Set)) + original |= EqpEntry.BodyShowBracelet | EqpEntry.HandShowBracelet; + + if (_doNotHideBracelets.Contains(armor[8].Set)) + original |= EqpEntry.HandShowRingR; + + if (_doNotHideBracelets.Contains(armor[9].Set)) + original |= EqpEntry.HandShowRingL; + return original; + } + + public bool Add(GlobalEqpManipulation manipulation) + => manipulation.Type switch + { + GlobalEqpType.DoNotHideEarrings => _doNotHideEarrings.Add(manipulation.Condition), + GlobalEqpType.DoNotHideNecklace => _doNotHideNecklace.Add(manipulation.Condition), + GlobalEqpType.DoNotHideBracelets => _doNotHideBracelets.Add(manipulation.Condition), + GlobalEqpType.DoNotHideRingR => _doNotHideRingR.Add(manipulation.Condition), + GlobalEqpType.DoNotHideRingL => _doNotHideRingL.Add(manipulation.Condition), + GlobalEqpType.DoNotHideHrothgarHats => !_doNotHideHrothgarHats && (_doNotHideHrothgarHats = true), + GlobalEqpType.DoNotHideVieraHats => !_doNotHideVieraHats && (_doNotHideVieraHats = true), + _ => false, + }; + + public bool Remove(GlobalEqpManipulation manipulation) + => manipulation.Type switch + { + GlobalEqpType.DoNotHideEarrings => _doNotHideEarrings.Remove(manipulation.Condition), + GlobalEqpType.DoNotHideNecklace => _doNotHideNecklace.Remove(manipulation.Condition), + GlobalEqpType.DoNotHideBracelets => _doNotHideBracelets.Remove(manipulation.Condition), + GlobalEqpType.DoNotHideRingR => _doNotHideRingR.Remove(manipulation.Condition), + GlobalEqpType.DoNotHideRingL => _doNotHideRingL.Remove(manipulation.Condition), + GlobalEqpType.DoNotHideHrothgarHats => _doNotHideHrothgarHats && !(_doNotHideHrothgarHats = false), + GlobalEqpType.DoNotHideVieraHats => _doNotHideVieraHats && !(_doNotHideVieraHats = false), + _ => false, + }; +} diff --git a/Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs b/Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs new file mode 100644 index 00000000..ada543dc --- /dev/null +++ b/Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs @@ -0,0 +1,50 @@ +using Penumbra.GameData.Structs; +using Penumbra.Interop.Structs; + +namespace Penumbra.Meta.Manipulations; + +public readonly struct GlobalEqpManipulation : IMetaManipulation +{ + public GlobalEqpType Type { get; init; } + public PrimaryId Condition { get; init; } + + public bool Validate() + { + if (!Enum.IsDefined(Type)) + return false; + + if (Type is GlobalEqpType.DoNotHideVieraHats or GlobalEqpType.DoNotHideHrothgarHats) + return Condition == 0; + + return Condition != 0; + } + + + public bool Equals(GlobalEqpManipulation other) + => Type == other.Type + && Condition.Equals(other.Condition); + + public int CompareTo(GlobalEqpManipulation other) + { + var typeComp = Type.CompareTo(other); + return typeComp != 0 ? typeComp : Condition.Id.CompareTo(other.Condition.Id); + } + + public override bool Equals(object? obj) + => obj is GlobalEqpManipulation other && Equals(other); + + public override int GetHashCode() + => HashCode.Combine((int)Type, Condition); + + public static bool operator ==(GlobalEqpManipulation left, GlobalEqpManipulation right) + => left.Equals(right); + + public static bool operator !=(GlobalEqpManipulation left, GlobalEqpManipulation right) + => !left.Equals(right); + + public override string ToString() + => $"Global EQP - {Type}{(Condition != 0 ? $" - {Condition.Id}" : string.Empty)}"; + + public MetaIndex FileIndex() + => (MetaIndex)(-1); +} diff --git a/Penumbra/Meta/Manipulations/GlobalEqpType.cs b/Penumbra/Meta/Manipulations/GlobalEqpType.cs new file mode 100644 index 00000000..57d99d56 --- /dev/null +++ b/Penumbra/Meta/Manipulations/GlobalEqpType.cs @@ -0,0 +1,61 @@ +namespace Penumbra.Meta.Manipulations; + +public enum GlobalEqpType +{ + DoNotHideEarrings, + DoNotHideNecklace, + DoNotHideBracelets, + DoNotHideRingR, + DoNotHideRingL, + DoNotHideHrothgarHats, + DoNotHideVieraHats, +} + +public static class GlobalEqpExtensions +{ + public static bool HasCondition(this GlobalEqpType type) + => type switch + { + GlobalEqpType.DoNotHideEarrings => true, + GlobalEqpType.DoNotHideNecklace => true, + GlobalEqpType.DoNotHideBracelets => true, + GlobalEqpType.DoNotHideRingR => true, + GlobalEqpType.DoNotHideRingL => true, + GlobalEqpType.DoNotHideHrothgarHats => false, + GlobalEqpType.DoNotHideVieraHats => false, + _ => false, + }; + + + public static ReadOnlySpan ToName(this GlobalEqpType type) + => type switch + { + GlobalEqpType.DoNotHideEarrings => "Do Not Hide Earrings"u8, + GlobalEqpType.DoNotHideNecklace => "Do Not Hide Necklaces"u8, + GlobalEqpType.DoNotHideBracelets => "Do Not Hide Bracelets"u8, + GlobalEqpType.DoNotHideRingR => "Do Not Hide Rings (Right Finger)"u8, + GlobalEqpType.DoNotHideRingL => "Do Not Hide Rings (Left Finger)"u8, + GlobalEqpType.DoNotHideHrothgarHats => "Do Not Hide Hats for Hrothgar"u8, + GlobalEqpType.DoNotHideVieraHats => "Do Not Hide Hats for Viera"u8, + _ => "\0"u8, + }; + + public static ReadOnlySpan ToDescription(this GlobalEqpType type) + => type switch + { + GlobalEqpType.DoNotHideEarrings => "Prevents the game from hiding earrings through other models when a specific earring is worn."u8, + GlobalEqpType.DoNotHideNecklace => + "Prevents the game from hiding necklaces through other models when a specific necklace is worn."u8, + GlobalEqpType.DoNotHideBracelets => + "Prevents the game from hiding bracelets through other models when a specific bracelet is worn."u8, + GlobalEqpType.DoNotHideRingR => + "Prevents the game from hiding rings worn on the right finger through other models when a specific ring is worn on the right finger."u8, + GlobalEqpType.DoNotHideRingL => + "Prevents the game from hiding rings worn on the left finger through other models when a specific ring is worn on the left finger."u8, + GlobalEqpType.DoNotHideHrothgarHats => + "Prevents the game from hiding any hats for Hrothgar that are normally flagged to not display on them."u8, + GlobalEqpType.DoNotHideVieraHats => + "Prevents the game from hiding any hats for Viera that are normally flagged to not display on them."u8, + _ => "\0"u8, + }; +} diff --git a/Penumbra/Meta/Manipulations/MetaManipulation.cs b/Penumbra/Meta/Manipulations/MetaManipulation.cs index ed184d52..f22de809 100644 --- a/Penumbra/Meta/Manipulations/MetaManipulation.cs +++ b/Penumbra/Meta/Manipulations/MetaManipulation.cs @@ -21,13 +21,14 @@ public readonly struct MetaManipulation : IEquatable, ICompara public enum Type : byte { - Unknown = 0, - Imc = 1, - Eqdp = 2, - Eqp = 3, - Est = 4, - Gmp = 5, - Rsp = 6, + Unknown = 0, + Imc = 1, + Eqdp = 2, + Eqp = 3, + Est = 4, + Gmp = 5, + Rsp = 6, + GlobalEqp = 7, } [FieldOffset(0)] @@ -54,6 +55,10 @@ public readonly struct MetaManipulation : IEquatable, ICompara [JsonIgnore] public readonly ImcManipulation Imc = default; + [FieldOffset(0)] + [JsonIgnore] + public readonly GlobalEqpManipulation GlobalEqp = default; + [FieldOffset(15)] [JsonConverter(typeof(StringEnumConverter))] [JsonProperty("Type")] @@ -63,14 +68,15 @@ public readonly struct MetaManipulation : IEquatable, ICompara { get => ManipulationType switch { - Type.Unknown => null, - Type.Imc => Imc, - Type.Eqdp => Eqdp, - Type.Eqp => Eqp, - Type.Est => Est, - Type.Gmp => Gmp, - Type.Rsp => Rsp, - _ => null, + Type.Unknown => null, + Type.Imc => Imc, + Type.Eqdp => Eqdp, + Type.Eqp => Eqp, + Type.Est => Est, + Type.Gmp => Gmp, + Type.Rsp => Rsp, + Type.GlobalEqp => GlobalEqp, + _ => null, }; init { @@ -100,6 +106,10 @@ public readonly struct MetaManipulation : IEquatable, ICompara Imc = m; ManipulationType = m.Validate(true) ? Type.Imc : Type.Unknown; return; + case GlobalEqpManipulation m: + GlobalEqp = m; + ManipulationType = m.Validate() ? Type.GlobalEqp : Type.Unknown; + return; } } } @@ -108,13 +118,14 @@ public readonly struct MetaManipulation : IEquatable, ICompara { return ManipulationType switch { - Type.Imc => Imc.Validate(true), - Type.Eqdp => Eqdp.Validate(), - Type.Eqp => Eqp.Validate(), - Type.Est => Est.Validate(), - Type.Gmp => Gmp.Validate(), - Type.Rsp => Rsp.Validate(), - _ => false, + Type.Imc => Imc.Validate(true), + Type.Eqdp => Eqdp.Validate(), + Type.Eqp => Eqp.Validate(), + Type.Est => Est.Validate(), + Type.Gmp => Gmp.Validate(), + Type.Rsp => Rsp.Validate(), + Type.GlobalEqp => GlobalEqp.Validate(), + _ => false, }; } @@ -154,6 +165,12 @@ public readonly struct MetaManipulation : IEquatable, ICompara ManipulationType = Type.Imc; } + public MetaManipulation(GlobalEqpManipulation eqp) + { + GlobalEqp = eqp; + ManipulationType = Type.GlobalEqp; + } + public static implicit operator MetaManipulation(EqpManipulation eqp) => new(eqp); @@ -172,6 +189,9 @@ public readonly struct MetaManipulation : IEquatable, ICompara public static implicit operator MetaManipulation(ImcManipulation imc) => new(imc); + public static implicit operator MetaManipulation(GlobalEqpManipulation eqp) + => new(eqp); + public bool EntryEquals(MetaManipulation other) { if (ManipulationType != other.ManipulationType) @@ -179,13 +199,14 @@ public readonly struct MetaManipulation : IEquatable, ICompara return ManipulationType switch { - Type.Eqp => Eqp.Entry.Equals(other.Eqp.Entry), - Type.Gmp => Gmp.Entry.Equals(other.Gmp.Entry), - Type.Eqdp => Eqdp.Entry.Equals(other.Eqdp.Entry), - Type.Est => Est.Entry.Equals(other.Est.Entry), - Type.Rsp => Rsp.Entry.Equals(other.Rsp.Entry), - Type.Imc => Imc.Entry.Equals(other.Imc.Entry), - _ => throw new ArgumentOutOfRangeException(), + Type.Eqp => Eqp.Entry.Equals(other.Eqp.Entry), + Type.Gmp => Gmp.Entry.Equals(other.Gmp.Entry), + Type.Eqdp => Eqdp.Entry.Equals(other.Eqdp.Entry), + Type.Est => Est.Entry.Equals(other.Est.Entry), + Type.Rsp => Rsp.Entry.Equals(other.Rsp.Entry), + Type.Imc => Imc.Entry.Equals(other.Imc.Entry), + Type.GlobalEqp => true, + _ => throw new ArgumentOutOfRangeException(), }; } @@ -196,13 +217,14 @@ public readonly struct MetaManipulation : IEquatable, ICompara return ManipulationType switch { - Type.Eqp => Eqp.Equals(other.Eqp), - Type.Gmp => Gmp.Equals(other.Gmp), - Type.Eqdp => Eqdp.Equals(other.Eqdp), - Type.Est => Est.Equals(other.Est), - Type.Rsp => Rsp.Equals(other.Rsp), - Type.Imc => Imc.Equals(other.Imc), - _ => false, + Type.Eqp => Eqp.Equals(other.Eqp), + Type.Gmp => Gmp.Equals(other.Gmp), + Type.Eqdp => Eqdp.Equals(other.Eqdp), + Type.Est => Est.Equals(other.Est), + Type.Rsp => Rsp.Equals(other.Rsp), + Type.Imc => Imc.Equals(other.Imc), + Type.GlobalEqp => GlobalEqp.Equals(other.GlobalEqp), + _ => false, }; } @@ -213,13 +235,14 @@ public readonly struct MetaManipulation : IEquatable, ICompara return ManipulationType switch { - Type.Eqp => Eqp.Copy(other.Eqp.Entry), - Type.Gmp => Gmp.Copy(other.Gmp.Entry), - Type.Eqdp => Eqdp.Copy(other.Eqdp), - Type.Est => Est.Copy(other.Est.Entry), - Type.Rsp => Rsp.Copy(other.Rsp.Entry), - Type.Imc => Imc.Copy(other.Imc.Entry), - _ => throw new ArgumentOutOfRangeException(), + Type.Eqp => Eqp.Copy(other.Eqp.Entry), + Type.Gmp => Gmp.Copy(other.Gmp.Entry), + Type.Eqdp => Eqdp.Copy(other.Eqdp), + Type.Est => Est.Copy(other.Est.Entry), + Type.Rsp => Rsp.Copy(other.Rsp.Entry), + Type.Imc => Imc.Copy(other.Imc.Entry), + Type.GlobalEqp => GlobalEqp, + _ => throw new ArgumentOutOfRangeException(), }; } @@ -229,13 +252,14 @@ public readonly struct MetaManipulation : IEquatable, ICompara public override int GetHashCode() => ManipulationType switch { - Type.Eqp => Eqp.GetHashCode(), - Type.Gmp => Gmp.GetHashCode(), - Type.Eqdp => Eqdp.GetHashCode(), - Type.Est => Est.GetHashCode(), - Type.Rsp => Rsp.GetHashCode(), - Type.Imc => Imc.GetHashCode(), - _ => 0, + Type.Eqp => Eqp.GetHashCode(), + Type.Gmp => Gmp.GetHashCode(), + Type.Eqdp => Eqdp.GetHashCode(), + Type.Est => Est.GetHashCode(), + Type.Rsp => Rsp.GetHashCode(), + Type.Imc => Imc.GetHashCode(), + Type.GlobalEqp => GlobalEqp.GetHashCode(), + _ => 0, }; public unsafe int CompareTo(MetaManipulation other) @@ -249,13 +273,14 @@ public readonly struct MetaManipulation : IEquatable, ICompara public override string ToString() => ManipulationType switch { - Type.Eqp => Eqp.ToString(), - Type.Gmp => Gmp.ToString(), - Type.Eqdp => Eqdp.ToString(), - Type.Est => Est.ToString(), - Type.Rsp => Rsp.ToString(), - Type.Imc => Imc.ToString(), - _ => "Invalid", + Type.Eqp => Eqp.ToString(), + Type.Gmp => Gmp.ToString(), + Type.Eqdp => Eqdp.ToString(), + Type.Est => Est.ToString(), + Type.Rsp => Rsp.ToString(), + Type.Imc => Imc.ToString(), + Type.GlobalEqp => GlobalEqp.ToString(), + _ => "Invalid", }; public string EntryToString() @@ -263,14 +288,15 @@ public readonly struct MetaManipulation : IEquatable, ICompara { Type.Imc => $"{Imc.Entry.DecalId}-{Imc.Entry.MaterialId}-{Imc.Entry.VfxId}-{Imc.Entry.SoundId}-{Imc.Entry.MaterialAnimationId}-{Imc.Entry.AttributeMask}", - Type.Eqdp => $"{(ushort)Eqdp.Entry:X}", - Type.Eqp => $"{(ulong)Eqp.Entry:X}", - Type.Est => $"{Est.Entry}", - Type.Gmp => $"{Gmp.Entry.Value}", - Type.Rsp => $"{Rsp.Entry}", - _ => string.Empty, - }; - + Type.Eqdp => $"{(ushort)Eqdp.Entry:X}", + Type.Eqp => $"{(ulong)Eqp.Entry:X}", + Type.Est => $"{Est.Entry}", + Type.Gmp => $"{Gmp.Entry.Value}", + Type.Rsp => $"{Rsp.Entry}", + Type.GlobalEqp => string.Empty, + _ => string.Empty, + }; + public static bool operator ==(MetaManipulation left, MetaManipulation right) => left.Equals(right); diff --git a/Penumbra/Mods/Editor/ModMetaEditor.cs b/Penumbra/Mods/Editor/ModMetaEditor.cs index 2f7fd04c..829161f5 100644 --- a/Penumbra/Mods/Editor/ModMetaEditor.cs +++ b/Penumbra/Mods/Editor/ModMetaEditor.cs @@ -6,19 +6,21 @@ namespace Penumbra.Mods.Editor; public class ModMetaEditor(ModManager modManager) { - private readonly HashSet _imc = []; - private readonly HashSet _eqp = []; - private readonly HashSet _eqdp = []; - private readonly HashSet _gmp = []; - private readonly HashSet _est = []; - private readonly HashSet _rsp = []; + private readonly HashSet _imc = []; + private readonly HashSet _eqp = []; + private readonly HashSet _eqdp = []; + private readonly HashSet _gmp = []; + private readonly HashSet _est = []; + private readonly HashSet _rsp = []; + private readonly HashSet _globalEqp = []; - public int OtherImcCount { get; private set; } - public int OtherEqpCount { get; private set; } - public int OtherEqdpCount { get; private set; } - public int OtherGmpCount { get; private set; } - public int OtherEstCount { get; private set; } - public int OtherRspCount { get; private set; } + public int OtherImcCount { get; private set; } + public int OtherEqpCount { get; private set; } + public int OtherEqdpCount { get; private set; } + public int OtherGmpCount { get; private set; } + public int OtherEstCount { get; private set; } + public int OtherRspCount { get; private set; } + public int OtherGlobalEqpCount { get; private set; } public bool Changes { get; private set; } @@ -40,17 +42,21 @@ public class ModMetaEditor(ModManager modManager) public IReadOnlySet Rsp => _rsp; + public IReadOnlySet GlobalEqp + => _globalEqp; + public bool CanAdd(MetaManipulation m) { return m.ManipulationType switch { - MetaManipulation.Type.Imc => !_imc.Contains(m.Imc), - MetaManipulation.Type.Eqdp => !_eqdp.Contains(m.Eqdp), - MetaManipulation.Type.Eqp => !_eqp.Contains(m.Eqp), - MetaManipulation.Type.Est => !_est.Contains(m.Est), - MetaManipulation.Type.Gmp => !_gmp.Contains(m.Gmp), - MetaManipulation.Type.Rsp => !_rsp.Contains(m.Rsp), - _ => false, + MetaManipulation.Type.Imc => !_imc.Contains(m.Imc), + MetaManipulation.Type.Eqdp => !_eqdp.Contains(m.Eqdp), + MetaManipulation.Type.Eqp => !_eqp.Contains(m.Eqp), + MetaManipulation.Type.Est => !_est.Contains(m.Est), + MetaManipulation.Type.Gmp => !_gmp.Contains(m.Gmp), + MetaManipulation.Type.Rsp => !_rsp.Contains(m.Rsp), + MetaManipulation.Type.GlobalEqp => !_globalEqp.Contains(m.GlobalEqp), + _ => false, }; } @@ -58,13 +64,14 @@ public class ModMetaEditor(ModManager modManager) { var added = m.ManipulationType switch { - MetaManipulation.Type.Imc => _imc.Add(m.Imc), - MetaManipulation.Type.Eqdp => _eqdp.Add(m.Eqdp), - MetaManipulation.Type.Eqp => _eqp.Add(m.Eqp), - MetaManipulation.Type.Est => _est.Add(m.Est), - MetaManipulation.Type.Gmp => _gmp.Add(m.Gmp), - MetaManipulation.Type.Rsp => _rsp.Add(m.Rsp), - _ => false, + MetaManipulation.Type.Imc => _imc.Add(m.Imc), + MetaManipulation.Type.Eqdp => _eqdp.Add(m.Eqdp), + MetaManipulation.Type.Eqp => _eqp.Add(m.Eqp), + MetaManipulation.Type.Est => _est.Add(m.Est), + MetaManipulation.Type.Gmp => _gmp.Add(m.Gmp), + MetaManipulation.Type.Rsp => _rsp.Add(m.Rsp), + MetaManipulation.Type.GlobalEqp => _globalEqp.Add(m.GlobalEqp), + _ => false, }; Changes |= added; return added; @@ -74,13 +81,14 @@ public class ModMetaEditor(ModManager modManager) { var deleted = m.ManipulationType switch { - MetaManipulation.Type.Imc => _imc.Remove(m.Imc), - MetaManipulation.Type.Eqdp => _eqdp.Remove(m.Eqdp), - MetaManipulation.Type.Eqp => _eqp.Remove(m.Eqp), - MetaManipulation.Type.Est => _est.Remove(m.Est), - MetaManipulation.Type.Gmp => _gmp.Remove(m.Gmp), - MetaManipulation.Type.Rsp => _rsp.Remove(m.Rsp), - _ => false, + MetaManipulation.Type.Imc => _imc.Remove(m.Imc), + MetaManipulation.Type.Eqdp => _eqdp.Remove(m.Eqdp), + MetaManipulation.Type.Eqp => _eqp.Remove(m.Eqp), + MetaManipulation.Type.Est => _est.Remove(m.Est), + MetaManipulation.Type.Gmp => _gmp.Remove(m.Gmp), + MetaManipulation.Type.Rsp => _rsp.Remove(m.Rsp), + MetaManipulation.Type.GlobalEqp => _globalEqp.Remove(m.GlobalEqp), + _ => false, }; Changes |= deleted; return deleted; @@ -100,17 +108,19 @@ public class ModMetaEditor(ModManager modManager) _gmp.Clear(); _est.Clear(); _rsp.Clear(); + _globalEqp.Clear(); Changes = true; } public void Load(Mod mod, IModDataContainer currentOption) { - OtherImcCount = 0; - OtherEqpCount = 0; - OtherEqdpCount = 0; - OtherGmpCount = 0; - OtherEstCount = 0; - OtherRspCount = 0; + OtherImcCount = 0; + OtherEqpCount = 0; + OtherEqdpCount = 0; + OtherGmpCount = 0; + OtherEstCount = 0; + OtherRspCount = 0; + OtherGlobalEqpCount = 0; foreach (var option in mod.AllDataContainers) { if (option == currentOption) @@ -138,6 +148,9 @@ public class ModMetaEditor(ModManager modManager) case MetaManipulation.Type.Rsp: ++OtherRspCount; break; + case MetaManipulation.Type.GlobalEqp: + ++OtherGlobalEqpCount; + break; } } } @@ -179,6 +192,9 @@ public class ModMetaEditor(ModManager modManager) case MetaManipulation.Type.Rsp: _rsp.Add(manip.Rsp); break; + case MetaManipulation.Type.GlobalEqp: + _globalEqp.Add(manip.GlobalEqp); + break; } } @@ -191,5 +207,6 @@ public class ModMetaEditor(ModManager modManager) .Concat(_eqp.Select(m => (MetaManipulation)m)) .Concat(_est.Select(m => (MetaManipulation)m)) .Concat(_gmp.Select(m => (MetaManipulation)m)) - .Concat(_rsp.Select(m => (MetaManipulation)m)); + .Concat(_rsp.Select(m => (MetaManipulation)m)) + .Concat(_globalEqp.Select(m => (MetaManipulation)m)); } diff --git a/Penumbra/Mods/SubMods/IModDataContainer.cs b/Penumbra/Mods/SubMods/IModDataContainer.cs index a6ab491f..7f7ef4a6 100644 --- a/Penumbra/Mods/SubMods/IModDataContainer.cs +++ b/Penumbra/Mods/SubMods/IModDataContainer.cs @@ -5,17 +5,16 @@ using Penumbra.String.Classes; namespace Penumbra.Mods.SubMods; - public interface IModDataContainer { - public IMod Mod { get; } + public IMod Mod { get; } public IModGroup? Group { get; } - public Dictionary Files { get; set; } - public Dictionary FileSwaps { get; set; } - public HashSet Manipulations { get; set; } + public Dictionary Files { get; set; } + public Dictionary FileSwaps { get; set; } + public HashSet Manipulations { get; set; } - public string GetName(); - public string GetFullName(); + public string GetName(); + public string GetFullName(); public (int GroupIndex, int DataIndex) GetDataIndices(); } diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs index 68933c9e..7cf75c03 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs @@ -2,6 +2,7 @@ using Dalamud.Interface; using ImGuiNET; using OtterGui; using OtterGui.Raii; +using OtterGui.Text; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.Structs; @@ -75,6 +76,8 @@ public partial class ModEditWindow _editor.MetaEditor.OtherGmpCount); DrawEditHeader(_editor.MetaEditor.Rsp, "Racial Scaling Edits (RSP)###RSP", 5, RspRow.Draw, RspRow.DrawNew, _editor.MetaEditor.OtherRspCount); + DrawEditHeader(_editor.MetaEditor.GlobalEqp, "Global Equipment Parameter Edits Edits (Global EQP)###GEQP", 4, GlobalEqpRow.Draw, + GlobalEqpRow.DrawNew, _editor.MetaEditor.OtherGlobalEqpCount); } @@ -702,6 +705,69 @@ public partial class ModEditWindow } } + private static class GlobalEqpRow + { + private static GlobalEqpManipulation _new = new() + { + Type = GlobalEqpType.DoNotHideEarrings, + Condition = 1, + }; + + public static void DrawNew(MetaFileManager metaFileManager, ModEditor editor, Vector2 iconSize) + { + ImGui.TableNextColumn(); + CopyToClipboardButton("Copy all current global EQP manipulations to clipboard.", iconSize, + editor.MetaEditor.GlobalEqp.Select(m => (MetaManipulation)m)); + ImGui.TableNextColumn(); + var canAdd = editor.MetaEditor.CanAdd(_new); + var tt = canAdd ? "Stage this edit." : "This entry is already manipulated."; + if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) + editor.MetaEditor.Add(_new); + + // Identifier + ImGui.TableNextColumn(); + ImGui.SetNextItemWidth(250 * ImUtf8.GlobalScale); + using (var combo = ImUtf8.Combo("##geqpType"u8, _new.Type.ToName())) + { + if (combo) + foreach (var type in Enum.GetValues()) + { + if (ImUtf8.Selectable(type.ToName(), type == _new.Type)) + _new = new GlobalEqpManipulation + { + Type = type, + Condition = type.HasCondition() ? _new.Type.HasCondition() ? _new.Condition : 1 : 0, + }; + ImUtf8.HoverTooltip(type.ToDescription()); + } + } + + ImUtf8.HoverTooltip(_new.Type.ToDescription()); + + ImGui.TableNextColumn(); + if (!_new.Type.HasCondition()) + return; + + if (IdInput("##geqpCond", 100 * ImUtf8.GlobalScale, _new.Condition.Id, out var newId, 1, ushort.MaxValue, _new.Condition.Id <= 1)) + _new = _new with { Condition = newId }; + } + + public static void Draw(MetaFileManager metaFileManager, GlobalEqpManipulation meta, ModEditor editor, Vector2 iconSize) + { + DrawMetaButtons(meta, editor, iconSize); + ImGui.TableNextColumn(); + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); + ImUtf8.Text(meta.Type.ToName()); + ImUtf8.HoverTooltip(meta.Type.ToDescription()); + ImGui.TableNextColumn(); + if (meta.Type.HasCondition()) + { + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); + ImUtf8.Text($"{meta.Condition.Id}"); + } + } + } + // A number input for ids with a optional max id of given width. // Returns true if newId changed against currentId. private static bool IdInput(string label, float width, ushort currentId, out ushort newId, int minId, int maxId, bool border) diff --git a/Penumbra/Util/IdentifierExtensions.cs b/Penumbra/Util/IdentifierExtensions.cs index 392a5aba..7368c7c8 100644 --- a/Penumbra/Util/IdentifierExtensions.cs +++ b/Penumbra/Util/IdentifierExtensions.cs @@ -76,6 +76,26 @@ public static class IdentifierExtensions case MetaManipulation.Type.Rsp: changedItems.TryAdd($"{manip.Rsp.SubRace.ToName()} {manip.Rsp.Attribute.ToFullString()}", null); break; + case MetaManipulation.Type.GlobalEqp: + var path = manip.GlobalEqp.Type switch + { + GlobalEqpType.DoNotHideEarrings => GamePaths.Accessory.Mdl.Path(manip.GlobalEqp.Condition, GenderRace.MidlanderMale, + EquipSlot.Ears), + GlobalEqpType.DoNotHideNecklace => GamePaths.Accessory.Mdl.Path(manip.GlobalEqp.Condition, GenderRace.MidlanderMale, + EquipSlot.Neck), + GlobalEqpType.DoNotHideBracelets => GamePaths.Accessory.Mdl.Path(manip.GlobalEqp.Condition, GenderRace.MidlanderMale, + EquipSlot.Wrists), + GlobalEqpType.DoNotHideRingR => GamePaths.Accessory.Mdl.Path(manip.GlobalEqp.Condition, GenderRace.MidlanderMale, + EquipSlot.RFinger), + GlobalEqpType.DoNotHideRingL => GamePaths.Accessory.Mdl.Path(manip.GlobalEqp.Condition, GenderRace.MidlanderMale, + EquipSlot.LFinger), + GlobalEqpType.DoNotHideHrothgarHats => string.Empty, + GlobalEqpType.DoNotHideVieraHats => string.Empty, + _ => string.Empty, + }; + if (path.Length > 0) + identifier.Identify(changedItems, path); + break; } } From cd133bddbba7241138829ce7f7f7a45a8e50b98f Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 26 May 2024 14:50:22 +0200 Subject: [PATCH 171/433] Update Changelog. --- Penumbra/UI/Changelog.cs | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/Penumbra/UI/Changelog.cs b/Penumbra/UI/Changelog.cs index add16f94..2b2cfa99 100644 --- a/Penumbra/UI/Changelog.cs +++ b/Penumbra/UI/Changelog.cs @@ -55,26 +55,46 @@ public class PenumbraChangelog private static void Add1_0_3_0(Changelog log) => log.NextVersion("Version 1.0.3.0") + .RegisterImportant( + "This update comes, again, with a lot of very heavy backend changes (collections and groups) and thus may introduce new issues.") .RegisterHighlight("Collections now have associated GUIDs as identifiers instead of their names, so they can now be renamed.") .RegisterEntry("Migrating those collections may introduce issues, please let me know as soon as possible if you encounter any.", 1) - .RegisterEntry("A permanent (non-rolling) backup should be created before the migration in case of any issues.", 1) - .RegisterHighlight("A total rework of how options and groups are handled internally, and introduction of the first new group type, the IMC Group.") - .RegisterEntry("Mod Creators can add a IMC Group to their mod that controls a single IMC Manipulation, so they can provide options for the separate attributes for it.", 1) - .RegisterEntry("This makes it a lot easier to have combined options: No need for 'A', 'B' and 'AB', you can just define 'A' and 'B' and skip their combinations", 1) - .RegisterHighlight("Added a field to rename mods directly from the mod selector context menu, instead of moving them in the filesystem.") + .RegisterEntry("A permanent (non-rolling) backup should be created before the migration in case of any issues.", 1) + .RegisterHighlight( + "A total rework of how options and groups are handled internally, and introduction of the first new group type, the IMC Group.") + .RegisterEntry( + "Mod Creators can add a IMC Group to their mod that controls a single IMC Manipulation, so they can provide options for the separate attributes for it.", + 1) + .RegisterEntry( + "This makes it a lot easier to have combined options: No need for 'A', 'B' and 'AB', you can just define 'A' and 'B' and skip their combinations", + 1) + .RegisterHighlight("A new type of Meta Manipulation was added, 'Global EQP Manipulation'.") + .RegisterEntry( + "Global EQP Manipulations allow accessories to make other equipment pieces not hide them, e.g. whenever a character is wearing a specific Bracelet, neither body nor hand items will ever hide bracelets.", + 1) + .RegisterEntry( + "This can be used if something like a jacket or a stole is put onto an accessory to prevent it from being hidden in general.", + 1) + .RegisterHighlight( + "Added a field to rename mods directly from the mod selector context menu, instead of moving them in the filesystem.") .RegisterEntry("You can choose which rename field (none, either one or both) to display in the settings.", 1) - .RegisterEntry("You can now paste your current clipboard text into the mod selector filter with a simple right-click as long as it is not focused.") - .RegisterHighlight("Added the option to display VFX for accessories if added via IMC edits, which the game does not do inherently (by Ocealot).") + .RegisterEntry( + "You can now paste your current clipboard text into the mod selector filter with a simple right-click as long as it is not focused.") + .RegisterHighlight( + "Added the option to display VFX for accessories if added via IMC edits, which the game does not do inherently (by Ocealot).") .RegisterEntry("Added support for reading and writing the new material and model file formats from the benchmark.") - .RegisterEntry("Added the option to hide Machinist Offhands from the Changed Items tabs (because any change to it changes ALL of them), which is on by default.") + .RegisterEntry( + "Added the option to hide Machinist Offhands from the Changed Items tabs (because any change to it changes ALL of them), which is on by default.") .RegisterEntry("Removed the auto-generated descriptions for newly created groups in Penumbra.") - .RegisterEntry("Made some improvements to the Advanced Editing window, for example a much better and more performant Hex Viewer for unstructured data was added.") + .RegisterEntry( + "Made some improvements to the Advanced Editing window, for example a much better and more performant Hex Viewer for unstructured data was added.") .RegisterEntry("Made a lot of further improvements on Model import/export (by ackwell).") .RegisterEntry("Reworked the API and IPC structure heavily.") .RegisterEntry("Worked around the UI IPC possibly displacing all settings when the drawn additions became too big.") .RegisterEntry("Fixed an issue with merging and deduplicating mods.") .RegisterEntry("Fixed a crash when scanning for mods without access rights to the folder.") - .RegisterEntry("Made plugin conform to Dalamud requirements by adding a punchline and another button to open the menu from the installer."); + .RegisterEntry( + "Made plugin conform to Dalamud requirements by adding a punchline and another button to open the menu from the installer."); private static void Add1_0_2_0(Changelog log) => log.NextVersion("Version 1.0.2.0") From 1d230050c214df15aeb83e7f14b110a3e836a4eb Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 26 May 2024 15:41:27 +0200 Subject: [PATCH 172/433] Fix typo and rename geqp options. --- Penumbra/Meta/Manipulations/GlobalEqpType.cs | 14 +++++++------- Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Penumbra/Meta/Manipulations/GlobalEqpType.cs b/Penumbra/Meta/Manipulations/GlobalEqpType.cs index 57d99d56..d57af1d9 100644 --- a/Penumbra/Meta/Manipulations/GlobalEqpType.cs +++ b/Penumbra/Meta/Manipulations/GlobalEqpType.cs @@ -30,13 +30,13 @@ public static class GlobalEqpExtensions public static ReadOnlySpan ToName(this GlobalEqpType type) => type switch { - GlobalEqpType.DoNotHideEarrings => "Do Not Hide Earrings"u8, - GlobalEqpType.DoNotHideNecklace => "Do Not Hide Necklaces"u8, - GlobalEqpType.DoNotHideBracelets => "Do Not Hide Bracelets"u8, - GlobalEqpType.DoNotHideRingR => "Do Not Hide Rings (Right Finger)"u8, - GlobalEqpType.DoNotHideRingL => "Do Not Hide Rings (Left Finger)"u8, - GlobalEqpType.DoNotHideHrothgarHats => "Do Not Hide Hats for Hrothgar"u8, - GlobalEqpType.DoNotHideVieraHats => "Do Not Hide Hats for Viera"u8, + GlobalEqpType.DoNotHideEarrings => "Always Show Earrings"u8, + GlobalEqpType.DoNotHideNecklace => "Always Show Necklaces"u8, + GlobalEqpType.DoNotHideBracelets => "Always Show Bracelets"u8, + GlobalEqpType.DoNotHideRingR => "Always Show Rings (Right Finger)"u8, + GlobalEqpType.DoNotHideRingL => "Always Show Rings (Left Finger)"u8, + GlobalEqpType.DoNotHideHrothgarHats => "Always Show Hats for Hrothgar"u8, + GlobalEqpType.DoNotHideVieraHats => "Always Show Hats for Viera"u8, _ => "\0"u8, }; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs index 7cf75c03..6f542377 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs @@ -76,7 +76,7 @@ public partial class ModEditWindow _editor.MetaEditor.OtherGmpCount); DrawEditHeader(_editor.MetaEditor.Rsp, "Racial Scaling Edits (RSP)###RSP", 5, RspRow.Draw, RspRow.DrawNew, _editor.MetaEditor.OtherRspCount); - DrawEditHeader(_editor.MetaEditor.GlobalEqp, "Global Equipment Parameter Edits Edits (Global EQP)###GEQP", 4, GlobalEqpRow.Draw, + DrawEditHeader(_editor.MetaEditor.GlobalEqp, "Global Equipment Parameter Edits (Global EQP)###GEQP", 4, GlobalEqpRow.Draw, GlobalEqpRow.DrawNew, _editor.MetaEditor.OtherGlobalEqpCount); } From ca777ba1bf6369f7fd6202f40620d882d56b4e86 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 27 May 2024 17:43:13 +0200 Subject: [PATCH 173/433] Update GameData. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 07cc26f1..b8282970 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 07cc26f196984a44711b3bc4c412947d863288bd +Subproject commit b8282970ee78a2c085e740f60450fecf7ea58b9c From fe266dca314f873422ec3f87a4769cbdc7dd520a Mon Sep 17 00:00:00 2001 From: Actions User Date: Mon, 27 May 2024 15:45:29 +0000 Subject: [PATCH 174/433] [CI] Updating repo.json for testing_1.0.3.0 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 232afaa0..2327420d 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.0.1.0", - "TestingAssemblyVersion": "1.0.2.6", + "TestingAssemblyVersion": "1.0.3.0", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -18,7 +18,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.2.6/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.3.0/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From f11cefcec1e21d416d25f7cbb1a6f31d8a19705f Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 28 May 2024 00:11:31 +0200 Subject: [PATCH 175/433] Fix best match fullpath returning broken FullPath instead of nullopt. --- Penumbra/Mods/Groups/MultiModGroup.cs | 11 ++++++++--- Penumbra/Mods/Groups/SingleModGroup.cs | 10 +++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Penumbra/Mods/Groups/MultiModGroup.cs b/Penumbra/Mods/Groups/MultiModGroup.cs index 38c0ef15..7816d628 100644 --- a/Penumbra/Mods/Groups/MultiModGroup.cs +++ b/Penumbra/Mods/Groups/MultiModGroup.cs @@ -4,6 +4,7 @@ using Newtonsoft.Json.Linq; using OtterGui; using OtterGui.Classes; using Penumbra.Api.Enums; +using Penumbra.GameData; using Penumbra.GameData.Data; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Settings; @@ -40,9 +41,13 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup => OptionData.Count > 0; public FullPath? FindBestMatch(Utf8GamePath gamePath) - => OptionData.OrderByDescending(o => o.Priority) - .SelectWhere(o => (o.Files.TryGetValue(gamePath, out var file) || o.FileSwaps.TryGetValue(gamePath, out file), file)) - .FirstOrDefault(); + { + foreach (var path in OptionData.OrderByDescending(o => o.Priority) + .SelectWhere(o => (o.Files.TryGetValue(gamePath, out var file) || o.FileSwaps.TryGetValue(gamePath, out file), file))) + return path; + + return null; + } public IModOption? AddOption(string name, string description = "") { diff --git a/Penumbra/Mods/Groups/SingleModGroup.cs b/Penumbra/Mods/Groups/SingleModGroup.cs index 49190e34..a6ebd846 100644 --- a/Penumbra/Mods/Groups/SingleModGroup.cs +++ b/Penumbra/Mods/Groups/SingleModGroup.cs @@ -30,9 +30,13 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup public readonly List OptionData = []; public FullPath? FindBestMatch(Utf8GamePath gamePath) - => OptionData - .SelectWhere(m => (m.Files.TryGetValue(gamePath, out var file) || m.FileSwaps.TryGetValue(gamePath, out file), file)) - .FirstOrDefault(); + { + foreach (var path in OptionData + .SelectWhere(m => (m.Files.TryGetValue(gamePath, out var file) || m.FileSwaps.TryGetValue(gamePath, out file), file))) + return path; + + return null; + } public IModOption AddOption(string name, string description = "") { From b30de460e729c4d0eb6ccdfcab298738f10ef54a Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 28 May 2024 00:11:54 +0200 Subject: [PATCH 176/433] Fix ColorTable preview with legacy color tables. --- Penumbra.GameData | 2 +- .../Import/Models/Export/MaterialExporter.cs | 4 ++-- .../LiveColorTablePreviewer.cs | 2 +- .../ModEditWindow.Materials.ColorTable.cs | 13 ++++++------ .../ModEditWindow.Materials.MtrlTab.cs | 21 +++++++++++-------- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index b8282970..b83ce830 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit b8282970ee78a2c085e740f60450fecf7ea58b9c +Subproject commit b83ce830919ca56e8b066d48d588c889df3af39b diff --git a/Penumbra/Import/Models/Export/MaterialExporter.cs b/Penumbra/Import/Models/Export/MaterialExporter.cs index 73a5e725..5df9e1c1 100644 --- a/Penumbra/Import/Models/Export/MaterialExporter.cs +++ b/Penumbra/Import/Models/Export/MaterialExporter.cs @@ -49,7 +49,7 @@ public class MaterialExporter private static MaterialBuilder BuildCharacter(Material material, string name) { // Build the textures from the color table. - var table = material.Mtrl.Table; + var table = new LegacyColorTable(material.Mtrl.Table); var normal = material.Textures[TextureUsage.SamplerNormal]; @@ -103,7 +103,7 @@ public class MaterialExporter // TODO: It feels a little silly to request the entire normal here when extracting the normal only needs some of the components. // As a future refactor, it would be neat to accept a single-channel field here, and then do composition of other stuff later. - private readonly struct ProcessCharacterNormalOperation(Image normal, ColorTable table) : IRowOperation + private readonly struct ProcessCharacterNormalOperation(Image normal, LegacyColorTable table) : IRowOperation { public Image Normal { get; } = normal.Clone(); public Image BaseColor { get; } = new(normal.Width, normal.Height); diff --git a/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs b/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs index f211e0bc..8e75a895 100644 --- a/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs +++ b/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs @@ -8,7 +8,7 @@ namespace Penumbra.Interop.MaterialPreview; public sealed unsafe class LiveColorTablePreviewer : LiveMaterialPreviewerBase { public const int TextureWidth = 4; - public const int TextureHeight = GameData.Files.MaterialStructs.ColorTable.NumUsedRows; + public const int TextureHeight = GameData.Files.MaterialStructs.LegacyColorTable.NumUsedRows; public const int TextureLength = TextureWidth * TextureHeight * 4; private readonly IFramework _framework; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs index 54c0eff6..15bd7cc9 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs @@ -116,7 +116,7 @@ public partial class ModEditWindow { var ret = false; if (tab.Mtrl.HasDyeTable) - for (var i = 0; i < ColorTable.NumUsedRows; ++i) + for (var i = 0; i < LegacyColorTable.NumUsedRows; ++i) ret |= tab.Mtrl.ApplyDyeTemplate(_stainService.StmFile, i, dyeId, 0); tab.UpdateColorTablePreview(); @@ -170,6 +170,7 @@ public partial class ModEditWindow } } + [SkipLocalsInit] private static unsafe void ColorTableCopyClipboardButton(ColorTable.Row row, ColorDyeTable.Row dye) { if (!ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Clipboard.ToIconString(), ImGui.GetFrameHeight() * Vector2.One, @@ -178,11 +179,11 @@ public partial class ModEditWindow try { - var data = new byte[ColorTable.Row.Size + 2]; + Span data = stackalloc byte[ColorTable.Row.Size + ColorDyeTable.Row.Size]; fixed (byte* ptr = data) { - MemoryUtility.MemCpyUnchecked(ptr, &row, ColorTable.Row.Size); - MemoryUtility.MemCpyUnchecked(ptr + ColorTable.Row.Size, &dye, 2); + MemoryUtility.MemCpyUnchecked(ptr, &row, ColorTable.Row.Size); + MemoryUtility.MemCpyUnchecked(ptr + ColorTable.Row.Size, &dye, ColorDyeTable.Row.Size); } var text = Convert.ToBase64String(data); @@ -218,7 +219,7 @@ public partial class ModEditWindow { var text = ImGui.GetClipboardText(); var data = Convert.FromBase64String(text); - if (data.Length != ColorTable.Row.Size + 2 + if (data.Length != ColorTable.Row.Size + ColorDyeTable.Row.Size || !tab.Mtrl.HasTable) return false; @@ -349,7 +350,7 @@ public partial class ModEditWindow ImGui.TableNextColumn(); tmpFloat = row.GlossStrength; ImGui.SetNextItemWidth(floatSize); - float glossStrengthMin = ImGui.GetIO().KeyCtrl ? 0.0f : HalfEpsilon; + var glossStrengthMin = ImGui.GetIO().KeyCtrl ? 0.0f : HalfEpsilon; if (ImGui.DragFloat("##GlossStrength", ref tmpFloat, Math.Max(0.1f, tmpFloat * 0.025f), glossStrengthMin, HalfMaxValue, "%.1f") && FixFloat(ref tmpFloat, row.GlossStrength)) { diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs index 9421493e..56e9482b 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs @@ -455,7 +455,8 @@ public partial class ModEditWindow { UnbindFromMaterialInstances(); - var instances = MaterialInfo.FindMaterials(_edit._resourceTreeFactory.GetLocalPlayerRelatedCharacters().Select(ch => ch.Address), FilePath); + var instances = MaterialInfo.FindMaterials(_edit._resourceTreeFactory.GetLocalPlayerRelatedCharacters().Select(ch => ch.Address), + FilePath); var foundMaterials = new HashSet(); foreach (var materialInfo in instances) @@ -596,13 +597,13 @@ public partial class ModEditWindow if (!Mtrl.HasTable) return; - var row = Mtrl.Table[rowIdx]; + var row = new LegacyColorTable.Row(Mtrl.Table[rowIdx]); if (Mtrl.HasDyeTable) { var stm = _edit._stainService.StmFile; - var dye = Mtrl.DyeTable[rowIdx]; + var dye = new LegacyColorDyeTable.Row(Mtrl.DyeTable[rowIdx]); if (stm.TryGetValue(dye.Template, _edit._stainService.StainCombo.CurrentSelection.Key, out var dyes)) - row.ApplyDyeTemplate(dye, dyes, default); + row.ApplyDyeTemplate(dye, dyes); } if (HighlightedColorTableRow == rowIdx) @@ -624,17 +625,18 @@ public partial class ModEditWindow if (!Mtrl.HasTable) return; - var rows = Mtrl.Table; + var rows = new LegacyColorTable(Mtrl.Table); + var dyeRows = new LegacyColorDyeTable(Mtrl.DyeTable); if (Mtrl.HasDyeTable) { var stm = _edit._stainService.StmFile; var stainId = (StainId)_edit._stainService.StainCombo.CurrentSelection.Key; - for (var i = 0; i < ColorTable.NumUsedRows; ++i) + for (var i = 0; i < LegacyColorTable.NumUsedRows; ++i) { ref var row = ref rows[i]; - var dye = Mtrl.DyeTable[i]; + var dye = dyeRows[i]; if (stm.TryGetValue(dye.Template, stainId, out var dyes)) - row.ApplyDyeTemplate(dye, dyes, default); + row.ApplyDyeTemplate(dye, dyes); } } @@ -643,12 +645,13 @@ public partial class ModEditWindow foreach (var previewer in ColorTablePreviewers) { + // TODO: Dawntrail rows.AsHalves().CopyTo(previewer.ColorTable); previewer.ScheduleUpdate(); } } - private static void ApplyHighlight(ref ColorTable.Row row, float time) + private static void ApplyHighlight(ref LegacyColorTable.Row row, float time) { var level = (MathF.Sin(time * 2.0f * MathF.PI) + 2.0f) / 3.0f / 255.0f; var baseColor = ColorId.InGameHighlight.Value(); From eb2a9b810922f409f8630e73a75c63353f1e709f Mon Sep 17 00:00:00 2001 From: Actions User Date: Mon, 27 May 2024 22:13:55 +0000 Subject: [PATCH 177/433] [CI] Updating repo.json for testing_1.0.3.1 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 2327420d..1cf9c104 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.0.1.0", - "TestingAssemblyVersion": "1.0.3.0", + "TestingAssemblyVersion": "1.0.3.1", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -18,7 +18,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.3.0/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.3.1/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 255d11974fcad8893e009766b9c8254be12ec9eb Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 28 May 2024 11:20:17 +0200 Subject: [PATCH 178/433] Fix IMC Stupid. --- Penumbra/UI/AdvancedWindow/FileEditor.cs | 2 +- Penumbra/UI/ModsTab/ImcManipulationDrawer.cs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Penumbra/UI/AdvancedWindow/FileEditor.cs b/Penumbra/UI/AdvancedWindow/FileEditor.cs index 01bf112b..c95884c6 100644 --- a/Penumbra/UI/AdvancedWindow/FileEditor.cs +++ b/Penumbra/UI/AdvancedWindow/FileEditor.cs @@ -204,7 +204,7 @@ public class FileEditor( private void SaveButton() { - var canSave = _changed && _currentFile != null && _currentFile.Valid; + var canSave = _changed && _currentFile is { Valid: true }; if (ImGuiUtil.DrawDisabledButton("Save to File", Vector2.Zero, $"Save the selected {fileType} file with all changes applied. This is not revertible.", !canSave)) { diff --git a/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs b/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs index c14652ac..694ae11c 100644 --- a/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs +++ b/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs @@ -26,6 +26,7 @@ public static class ImcManipulationDrawer }; identifier = identifier with { + ObjectType = type, EquipSlot = equipSlot, SecondaryId = identifier.SecondaryId == 0 ? 1 : identifier.SecondaryId, }; From 5d1b17f96d159276e3470e462b56fbd2b101dd6e Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 28 May 2024 12:51:12 +0200 Subject: [PATCH 179/433] Fix mdl imports not being savable. --- Penumbra.GameData | 2 +- Penumbra/Import/Models/Import/ModelImporter.cs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index b83ce830..f2cea65b 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit b83ce830919ca56e8b066d48d588c889df3af39b +Subproject commit f2cea65b83b2d6cb0d03339e8f76aed8102a41d5 diff --git a/Penumbra/Import/Models/Import/ModelImporter.cs b/Penumbra/Import/Models/Import/ModelImporter.cs index eedd12ab..a141d754 100644 --- a/Penumbra/Import/Models/Import/ModelImporter.cs +++ b/Penumbra/Import/Models/Import/ModelImporter.cs @@ -104,6 +104,7 @@ public partial class ModelImporter(ModelRoot model, IoNotifier notifier) Radius = 1, BoneBoundingBoxes = Enumerable.Repeat(MdlFile.EmptyBoundingBox, _bones.Count).ToArray(), RemainingData = [.._vertexBuffer, ..indexBuffer], + Valid = true, }; } From f5d6ac8bdbd43edf11f5d7ed41d7f587c49590d6 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 28 May 2024 12:51:28 +0200 Subject: [PATCH 180/433] Fix Remove Assignment being visible for base and interface. --- Penumbra/Collections/Manager/CollectionType.cs | 3 +++ Penumbra/UI/CollectionTab/CollectionPanel.cs | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Penumbra/Collections/Manager/CollectionType.cs b/Penumbra/Collections/Manager/CollectionType.cs index 8c51fd90..c25413b8 100644 --- a/Penumbra/Collections/Manager/CollectionType.cs +++ b/Penumbra/Collections/Manager/CollectionType.cs @@ -107,6 +107,9 @@ public static class CollectionTypeExtensions public static bool IsSpecial(this CollectionType collectionType) => collectionType < CollectionType.Default; + public static bool CanBeRemoved(this CollectionType collectionType) + => collectionType.IsSpecial() || collectionType is CollectionType.Individual; + public static readonly (CollectionType, string, string)[] Special = Enum.GetValues() .Where(IsSpecial) .Select(s => (s, s.ToName(), s.ToDescription())) diff --git a/Penumbra/UI/CollectionTab/CollectionPanel.cs b/Penumbra/UI/CollectionTab/CollectionPanel.cs index 8625335e..bb22e6a7 100644 --- a/Penumbra/UI/CollectionTab/CollectionPanel.cs +++ b/Penumbra/UI/CollectionTab/CollectionPanel.cs @@ -287,7 +287,7 @@ public sealed class CollectionPanel : IDisposable _active.SetCollection(ModCollection.Empty, type, _active.Individuals.GetGroup(identifier)); } - if (collection != null) + if (collection != null && type.CanBeRemoved()) { using var color = ImRaii.PushColor(ImGuiCol.Text, Colors.RegexWarningBorder); if (ImGui.MenuItem("Remove this assignment.")) From 8891ea057086094e56220b60989741d7fc36dfc7 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 28 May 2024 12:53:02 +0200 Subject: [PATCH 181/433] Fix imc identifiers setting equip slot to something where they should not. --- Penumbra/Meta/Manipulations/ImcManipulation.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Penumbra/Meta/Manipulations/ImcManipulation.cs b/Penumbra/Meta/Manipulations/ImcManipulation.cs index 945aab04..eb3720c9 100644 --- a/Penumbra/Meta/Manipulations/ImcManipulation.cs +++ b/Penumbra/Meta/Manipulations/ImcManipulation.cs @@ -64,10 +64,8 @@ public readonly struct ImcManipulation : IMetaManipulation { ObjectType.Accessory or ObjectType.Equipment => new ImcIdentifier(primaryId, v, objectType, 0, equipSlot, variant > byte.MaxValue ? BodySlot.Body : BodySlot.Unknown), - ObjectType.DemiHuman => new ImcIdentifier(primaryId, v, objectType, secondaryId, - equipSlot == EquipSlot.Unknown ? EquipSlot.Head : equipSlot, variant > byte.MaxValue ? BodySlot.Body : BodySlot.Unknown), - _ => new ImcIdentifier(primaryId, v, objectType, secondaryId, equipSlot == EquipSlot.Unknown ? EquipSlot.Head : equipSlot, - bodySlot), + ObjectType.DemiHuman => new ImcIdentifier(primaryId, v, objectType, secondaryId, equipSlot, variant > byte.MaxValue ? BodySlot.Body : BodySlot.Unknown), + _ => new ImcIdentifier(primaryId, v, objectType, secondaryId, equipSlot, bodySlot == BodySlot.Unknown ? BodySlot.Body : BodySlot.Unknown), }; } From 09742e2e50d24acfe62e4876451f9be556b457fe Mon Sep 17 00:00:00 2001 From: Actions User Date: Tue, 28 May 2024 10:56:23 +0000 Subject: [PATCH 182/433] [CI] Updating repo.json for testing_1.0.3.2 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 1cf9c104..a996e2d0 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.0.1.0", - "TestingAssemblyVersion": "1.0.3.1", + "TestingAssemblyVersion": "1.0.3.2", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -18,7 +18,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.3.1/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.3.2/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From b2e1bff782f5aff12766ecbe82bfa743242e0705 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 30 May 2024 17:18:39 +0200 Subject: [PATCH 183/433] Consolidate path-data encoding into a single file and make it neater. --- Penumbra/Collections/Cache/ImcCache.cs | 16 +- .../Collections/Manager/CollectionStorage.cs | 64 ++++++- .../Manager/TempCollectionManager.cs | 3 +- Penumbra/Collections/ModCollection.cs | 27 +-- .../Interop/PathResolving/PathDataHandler.cs | 162 ++++++++++++++++++ .../Interop/PathResolving/PathResolver.cs | 52 +++--- .../Interop/PathResolving/SubfileHelper.cs | 19 +- .../ResourceLoading/CreateFileWHook.cs | 3 +- .../Interop/ResourceLoading/ResourceLoader.cs | 24 ++- .../Interop/ResourceTree/ResolveContext.cs | 11 +- Penumbra/Mods/TemporaryMod.cs | 7 +- Penumbra/Services/ConfigMigrationService.cs | 3 +- Penumbra/Services/CrashHandlerService.cs | 4 +- .../UI/ResourceWatcher/ResourceWatcher.cs | 2 +- 14 files changed, 302 insertions(+), 95 deletions(-) create mode 100644 Penumbra/Interop/PathResolving/PathDataHandler.cs diff --git a/Penumbra/Collections/Cache/ImcCache.cs b/Penumbra/Collections/Cache/ImcCache.cs index 843fe195..33b366d3 100644 --- a/Penumbra/Collections/Cache/ImcCache.cs +++ b/Penumbra/Collections/Cache/ImcCache.cs @@ -1,3 +1,4 @@ +using Penumbra.Interop.PathResolving; using Penumbra.Meta; using Penumbra.Meta.Files; using Penumbra.Meta.Manipulations; @@ -7,8 +8,8 @@ namespace Penumbra.Collections.Cache; public readonly struct ImcCache : IDisposable { - private readonly Dictionary _imcFiles = new(); - private readonly List<(ImcManipulation, ImcFile)> _imcManipulations = new(); + private readonly Dictionary _imcFiles = []; + private readonly List<(ImcManipulation, ImcFile)> _imcManipulations = []; public ImcCache() { } @@ -17,10 +18,10 @@ public readonly struct ImcCache : IDisposable { if (fromFullCompute) foreach (var path in _imcFiles.Keys) - collection._cache!.ForceFileSync(path, CreateImcPath(collection, path)); + collection._cache!.ForceFileSync(path, PathDataHandler.CreateImc(path.Path, collection)); else foreach (var path in _imcFiles.Keys) - collection._cache!.ForceFile(path, CreateImcPath(collection, path)); + collection._cache!.ForceFile(path, PathDataHandler.CreateImc(path.Path, collection)); } public void Reset(ModCollection collection) @@ -57,7 +58,7 @@ public readonly struct ImcCache : IDisposable return false; _imcFiles[path] = file; - var fullPath = CreateImcPath(collection, path); + var fullPath = PathDataHandler.CreateImc(file.Path.Path, collection); collection._cache!.ForceFile(path, fullPath); return true; @@ -100,7 +101,7 @@ public readonly struct ImcCache : IDisposable if (!manip.Apply(file)) return false; - var fullPath = CreateImcPath(collection, file.Path); + var fullPath = PathDataHandler.CreateImc(file.Path.Path, collection); collection._cache!.ForceFile(file.Path, fullPath); return true; @@ -115,9 +116,6 @@ public readonly struct ImcCache : IDisposable _imcManipulations.Clear(); } - private static FullPath CreateImcPath(ModCollection collection, Utf8GamePath path) - => new($"|{collection.Id.OptimizedString()}_{collection.ChangeCounter}|{path}"); - public bool GetImcFile(Utf8GamePath path, [NotNullWhen(true)] out ImcFile? file) => _imcFiles.TryGetValue(path, out file); } diff --git a/Penumbra/Collections/Manager/CollectionStorage.cs b/Penumbra/Collections/Manager/CollectionStorage.cs index de5d0a14..39068e87 100644 --- a/Penumbra/Collections/Manager/CollectionStorage.cs +++ b/Penumbra/Collections/Manager/CollectionStorage.cs @@ -1,3 +1,4 @@ +using System; using Dalamud.Interface.Internal.Notifications; using OtterGui; using OtterGui.Classes; @@ -10,23 +11,71 @@ using Penumbra.Mods.Manager.OptionEditor; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; +using Penumbra.UI.CollectionTab; namespace Penumbra.Collections.Manager; +/// A contiguously incrementing ID managed by the CollectionCreator. +public readonly record struct LocalCollectionId(int Id) : IAdditionOperators +{ + public static readonly LocalCollectionId Zero = new(0); + + public static LocalCollectionId operator +(LocalCollectionId left, int right) + => new(left.Id + right); +} + public class CollectionStorage : IReadOnlyList, IDisposable { private readonly CommunicatorService _communicator; private readonly SaveService _saveService; private readonly ModStorage _modStorage; + public ModCollection Create(string name, int index, ModCollection? duplicate) + { + var newCollection = duplicate?.Duplicate(name, CurrentCollectionId, index) + ?? ModCollection.CreateEmpty(name, CurrentCollectionId, index, _modStorage.Count); + _collectionsByLocal[CurrentCollectionId] = newCollection; + CurrentCollectionId += 1; + return newCollection; + } + + public ModCollection CreateFromData(Guid id, string name, int version, Dictionary allSettings, + IReadOnlyList inheritances) + { + var newCollection = ModCollection.CreateFromData(_saveService, _modStorage, id, name, CurrentCollectionId, version, Count, allSettings, + inheritances); + _collectionsByLocal[CurrentCollectionId] = newCollection; + CurrentCollectionId += 1; + return newCollection; + } + + public ModCollection CreateTemporary(string name, int index, int globalChangeCounter) + { + var newCollection = ModCollection.CreateTemporary(name, CurrentCollectionId, index, globalChangeCounter); + _collectionsByLocal[CurrentCollectionId] = newCollection; + CurrentCollectionId += 1; + return newCollection; + } + + public void Delete(ModCollection collection) + => _collectionsByLocal.Remove(collection.LocalId); + /// The empty collection is always available at Index 0. private readonly List _collections = [ ModCollection.Empty, ]; + /// A list of all collections ever created still existing by their local id. + private readonly Dictionary + _collectionsByLocal = new() { [LocalCollectionId.Zero] = ModCollection.Empty }; + + public readonly ModCollection DefaultNamed; + /// Incremented by 1 because the empty collection gets Zero. + public LocalCollectionId CurrentCollectionId { get; private set; } = LocalCollectionId.Zero + 1; + /// Default enumeration skips the empty collection. public IEnumerator GetEnumerator() => _collections.Skip(1).GetEnumerator(); @@ -69,6 +118,10 @@ public class CollectionStorage : IReadOnlyList, IDisposable return ByName(identifier, out collection); } + /// Find a collection by its local ID if it still exists, otherwise returns the empty collection. + public ModCollection ByLocalId(LocalCollectionId localId) + => _collectionsByLocal.TryGetValue(localId, out var coll) ? coll : ModCollection.Empty; + public CollectionStorage(CommunicatorService communicator, SaveService saveService, ModStorage modStorage) { _communicator = communicator; @@ -100,10 +153,7 @@ public class CollectionStorage : IReadOnlyList, IDisposable /// public bool AddCollection(string name, ModCollection? duplicate) { - var newCollection = duplicate?.Duplicate(name, _collections.Count) - ?? ModCollection.CreateEmpty(name, _collections.Count, _modStorage.Count); - _collections.Add(newCollection); - + var newCollection = Create(name, _collections.Count, duplicate); _saveService.ImmediateSave(new ModCollectionSave(_modStorage, newCollection)); Penumbra.Messager.NotificationMessage($"Created new collection {newCollection.AnonymizedName}.", NotificationType.Success, false); _communicator.CollectionChange.Invoke(CollectionType.Inactive, null, newCollection, string.Empty); @@ -132,6 +182,7 @@ public class CollectionStorage : IReadOnlyList, IDisposable // Update indices. for (var i = collection.Index; i < Count; ++i) _collections[i].Index = i; + _collectionsByLocal.Remove(collection.LocalId); Penumbra.Messager.NotificationMessage($"Deleted collection {collection.AnonymizedName}.", NotificationType.Success, false); _communicator.CollectionChange.Invoke(CollectionType.Inactive, collection, null, string.Empty); @@ -180,7 +231,7 @@ public class CollectionStorage : IReadOnlyList, IDisposable continue; } - var collection = ModCollection.CreateFromData(_saveService, _modStorage, id, name, version, Count, settings, inheritance); + var collection = CreateFromData(id, name, version, settings, inheritance); var correctName = _saveService.FileNames.CollectionFile(collection); if (file.FullName != correctName) try @@ -293,7 +344,8 @@ public class CollectionStorage : IReadOnlyList, IDisposable } /// Save all collections where the mod has settings and the change requires saving. - private void OnModOptionChange(ModOptionChangeType type, Mod mod, IModGroup? group, IModOption? option, IModDataContainer? container, int movedToIdx) + private void OnModOptionChange(ModOptionChangeType type, Mod mod, IModGroup? group, IModOption? option, IModDataContainer? container, + int movedToIdx) { type.HandlingInfo(out var requiresSaving, out _, out _); if (!requiresSaving) diff --git a/Penumbra/Collections/Manager/TempCollectionManager.cs b/Penumbra/Collections/Manager/TempCollectionManager.cs index de08c6a2..ce438a6b 100644 --- a/Penumbra/Collections/Manager/TempCollectionManager.cs +++ b/Penumbra/Collections/Manager/TempCollectionManager.cs @@ -52,7 +52,7 @@ public class TempCollectionManager : IDisposable { if (GlobalChangeCounter == int.MaxValue) GlobalChangeCounter = 0; - var collection = ModCollection.CreateTemporary(name, ~Count, GlobalChangeCounter++); + var collection = _storage.CreateTemporary(name, ~Count, GlobalChangeCounter++); Penumbra.Log.Debug($"Creating temporary collection {collection.Name} with {collection.Id}."); if (_customCollections.TryAdd(collection.Id, collection)) { @@ -72,6 +72,7 @@ public class TempCollectionManager : IDisposable return false; } + _storage.Delete(collection); Penumbra.Log.Debug($"Deleted temporary collection {collection.Id}."); GlobalChangeCounter += Math.Max(collection.ChangeCounter + 1 - GlobalChangeCounter, 0); for (var i = 0; i < Collections.Count; ++i) diff --git a/Penumbra/Collections/ModCollection.cs b/Penumbra/Collections/ModCollection.cs index 4580e37a..9286d459 100644 --- a/Penumbra/Collections/ModCollection.cs +++ b/Penumbra/Collections/ModCollection.cs @@ -25,13 +25,15 @@ public partial class ModCollection /// Create the always available Empty Collection that will always sit at index 0, /// can not be deleted and does never create a cache. /// - public static readonly ModCollection Empty = new(Guid.Empty, EmptyCollectionName, 0, 0, CurrentVersion, [], [], []); + public static readonly ModCollection Empty = new(Guid.Empty, EmptyCollectionName, LocalCollectionId.Zero, 0, 0, CurrentVersion, [], [], []); /// The name of a collection. public string Name { get; set; } public Guid Id { get; } + public LocalCollectionId LocalId { get; } + public string Identifier => Id.ToString(); @@ -117,19 +119,20 @@ public partial class ModCollection /// /// Constructor for duplication. Deep copies all settings and parent collections and adds the new collection to their children lists. /// - public ModCollection Duplicate(string name, int index) + public ModCollection Duplicate(string name, LocalCollectionId localId, int index) { Debug.Assert(index > 0, "Collection duplicated with non-positive index."); - return new ModCollection(Guid.NewGuid(), name, index, 0, CurrentVersion, Settings.Select(s => s?.DeepCopy()).ToList(), + return new ModCollection(Guid.NewGuid(), name, localId, index, 0, CurrentVersion, Settings.Select(s => s?.DeepCopy()).ToList(), [.. DirectlyInheritsFrom], UnusedSettings.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.DeepCopy())); } /// Constructor for reading from files. - public static ModCollection CreateFromData(SaveService saver, ModStorage mods, Guid id, string name, int version, int index, + public static ModCollection CreateFromData(SaveService saver, ModStorage mods, Guid id, string name, LocalCollectionId localId, int version, + int index, Dictionary allSettings, IReadOnlyList inheritances) { Debug.Assert(index > 0, "Collection read with non-positive index."); - var ret = new ModCollection(id, name, index, 0, version, [], [], allSettings) + var ret = new ModCollection(id, name, localId, index, 0, version, [], [], allSettings) { InheritanceByName = inheritances, }; @@ -139,18 +142,19 @@ public partial class ModCollection } /// Constructor for temporary collections. - public static ModCollection CreateTemporary(string name, int index, int changeCounter) + public static ModCollection CreateTemporary(string name, LocalCollectionId localId, int index, int changeCounter) { Debug.Assert(index < 0, "Temporary collection created with non-negative index."); - var ret = new ModCollection(Guid.NewGuid(), name, index, changeCounter, CurrentVersion, [], [], []); + var ret = new ModCollection(Guid.NewGuid(), name, localId, index, changeCounter, CurrentVersion, [], [], []); return ret; } /// Constructor for empty collections. - public static ModCollection CreateEmpty(string name, int index, int modCount) + public static ModCollection CreateEmpty(string name, LocalCollectionId localId, int index, int modCount) { Debug.Assert(index >= 0, "Empty collection created with negative index."); - return new ModCollection(Guid.NewGuid(), name, index, 0, CurrentVersion, Enumerable.Repeat((ModSettings?)null, modCount).ToList(), [], + return new ModCollection(Guid.NewGuid(), name, localId, index, 0, CurrentVersion, + Enumerable.Repeat((ModSettings?)null, modCount).ToList(), [], []); } @@ -199,11 +203,12 @@ public partial class ModCollection saver.ImmediateSave(new ModCollectionSave(mods, this)); } - private ModCollection(Guid id, string name, int index, int changeCounter, int version, List appliedSettings, - List inheritsFrom, Dictionary settings) + private ModCollection(Guid id, string name, LocalCollectionId localId, int index, int changeCounter, int version, + List appliedSettings, List inheritsFrom, Dictionary settings) { Name = name; Id = id; + LocalId = localId; Index = index; ChangeCounter = changeCounter; Settings = appliedSettings; diff --git a/Penumbra/Interop/PathResolving/PathDataHandler.cs b/Penumbra/Interop/PathResolving/PathDataHandler.cs new file mode 100644 index 00000000..5627e015 --- /dev/null +++ b/Penumbra/Interop/PathResolving/PathDataHandler.cs @@ -0,0 +1,162 @@ +using Penumbra.Collections; +using Penumbra.Collections.Manager; +using Penumbra.String; +using Penumbra.String.Classes; + +namespace Penumbra.Interop.PathResolving; + +public static class PathDataHandler +{ + public static readonly ushort Discriminator = (ushort)(Environment.TickCount >> 12); + private static readonly string DiscriminatorString = $"{Discriminator:X4}"; + private const int MinimumLength = 8; + + /// Additional Data encoded in a path. + /// The local ID of the collection. + /// The change counter of that collection when this file was loaded. + /// The CRC32 of the originally requested path, only used for materials. + /// A discriminator to differ between multiple loads of Penumbra. + public readonly record struct AdditionalPathData( + LocalCollectionId Collection, + int ChangeCounter, + int OriginalPathCrc32, + ushort Discriminator) + { + public static readonly AdditionalPathData Invalid = new(LocalCollectionId.Zero, 0, 0, PathDataHandler.Discriminator); + + /// Any collection but the empty collection can appear. In particular, they can be negative for temporary collections. + public bool Valid + => Collection.Id != 0; + } + + /// Create the encoding path for an IMC file. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static FullPath CreateImc(ByteString path, ModCollection collection) + => CreateBase(path, collection); + + /// Create the encoding path for a TMB file. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static FullPath CreateTmb(ByteString path, ModCollection collection) + => CreateBase(path, collection); + + /// Create the encoding path for an AVFX file. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static FullPath CreateAvfx(ByteString path, ModCollection collection) + => CreateBase(path, collection); + + /// Create the encoding path for a MTRL file. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static FullPath CreateMtrl(ByteString path, ModCollection collection, Utf8GamePath originalPath) + => new($"|{collection.LocalId.Id}_{collection.ChangeCounter}_{originalPath.Path.Crc32:X8}_{DiscriminatorString}|{path}"); + + /// The base function shared by most file types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static FullPath CreateBase(ByteString path, ModCollection collection) + => new($"|{collection.LocalId.Id}_{collection.ChangeCounter}_{DiscriminatorString}|{path}"); + + /// Read an additional data blurb and parse it into usable data for all file types but Materials. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool Read(ReadOnlySpan additionalData, out AdditionalPathData data) + => ReadBase(additionalData, out data, out _); + + /// Read an additional data blurb and parse it into usable data for Materials. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool ReadMtrl(ReadOnlySpan additionalData, out AdditionalPathData data) + { + if (!ReadBase(additionalData, out data, out var remaining)) + return false; + + if (!int.TryParse(remaining, out var crc32)) + return false; + + data = data with { OriginalPathCrc32 = crc32 }; + return true; + } + + /// Parse the common attributes of an additional data blurb and return remaining data if there is any. + private static bool ReadBase(ReadOnlySpan additionalData, out AdditionalPathData data, out ReadOnlySpan remainingData) + { + data = AdditionalPathData.Invalid; + remainingData = []; + + // At least (\d_\d_\x\x\x\x) + if (additionalData.Length < MinimumLength) + return false; + + // Fetch discriminator, constant length. + var discriminatorSpan = additionalData[^4..]; + if (!ushort.TryParse(discriminatorSpan, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out var discriminator)) + return false; + + additionalData = additionalData[..^5]; + var collectionSplit = additionalData.IndexOf((byte)'_'); + if (collectionSplit == -1) + return false; + + var collectionSpan = additionalData[..collectionSplit]; + additionalData = additionalData[(collectionSplit + 1)..]; + + if (!int.TryParse(collectionSpan, out var id)) + return false; + + var changeCounterSpan = additionalData; + var changeCounterSplit = additionalData.IndexOf((byte)'_'); + if (changeCounterSplit != -1) + { + changeCounterSpan = additionalData[..changeCounterSplit]; + remainingData = additionalData[(changeCounterSplit + 1)..]; + } + + if (!int.TryParse(changeCounterSpan, out var changeCounter)) + return false; + + data = new AdditionalPathData(new LocalCollectionId(id), changeCounter, 0, discriminator); + return true; + } + + /// Split a given span into the actual path and the additional data blurb. Returns true if a blurb exists. + public static bool Split(ReadOnlySpan text, out ReadOnlySpan path, out ReadOnlySpan data) + { + if (text.IsEmpty || text[0] is not (byte)'|') + { + path = text; + data = []; + return false; + } + + var endIdx = text[1..].IndexOf((byte)'|'); + if (endIdx++ < 0) + { + path = text; + data = []; + return false; + } + + data = text.Slice(1, endIdx - 1); + path = ++endIdx == text.Length ? [] : text[endIdx..]; + return true; + } + + /// + public static bool Split(ReadOnlySpan text, out ReadOnlySpan path, out ReadOnlySpan data) + { + if (text.Length == 0 || text[0] is not '|') + { + path = text; + data = []; + return false; + } + + var endIdx = text[1..].IndexOf('|'); + if (endIdx++ < 0) + { + path = text; + data = []; + return false; + } + + data = text.Slice(1, endIdx - 1); + path = ++endIdx >= text.Length ? [] : text[endIdx..]; + return true; + } +} diff --git a/Penumbra/Interop/PathResolving/PathResolver.cs b/Penumbra/Interop/PathResolving/PathResolver.cs index 5c3d8d19..e5c75327 100644 --- a/Penumbra/Interop/PathResolving/PathResolver.cs +++ b/Penumbra/Interop/PathResolving/PathResolver.cs @@ -13,11 +13,10 @@ namespace Penumbra.Interop.PathResolving; public class PathResolver : IDisposable { - private readonly PerformanceTracker _performance; - private readonly Configuration _config; - private readonly CollectionManager _collectionManager; - private readonly TempCollectionManager _tempCollections; - private readonly ResourceLoader _loader; + private readonly PerformanceTracker _performance; + private readonly Configuration _config; + private readonly CollectionManager _collectionManager; + private readonly ResourceLoader _loader; private readonly SubfileHelper _subfileHelper; private readonly PathState _pathState; @@ -25,14 +24,12 @@ public class PathResolver : IDisposable private readonly GameState _gameState; private readonly CollectionResolver _collectionResolver; - public unsafe PathResolver(PerformanceTracker performance, Configuration config, CollectionManager collectionManager, - TempCollectionManager tempCollections, ResourceLoader loader, SubfileHelper subfileHelper, - PathState pathState, MetaState metaState, CollectionResolver collectionResolver, GameState gameState) + public unsafe PathResolver(PerformanceTracker performance, Configuration config, CollectionManager collectionManager, ResourceLoader loader, + SubfileHelper subfileHelper, PathState pathState, MetaState metaState, CollectionResolver collectionResolver, GameState gameState) { _performance = performance; _config = config; _collectionManager = collectionManager; - _tempCollections = tempCollections; _subfileHelper = subfileHelper; _pathState = pathState; _metaState = metaState; @@ -43,9 +40,12 @@ public class PathResolver : IDisposable _loader.FileLoaded += ImcLoadResource; } - /// Obtain a temporary or permanent collection by name. - public bool CollectionById(Guid id, [NotNullWhen(true)] out ModCollection? collection) - => _tempCollections.CollectionById(id, out collection) || _collectionManager.Storage.ById(id, out collection); + /// Obtain a temporary or permanent collection by local ID. + public bool CollectionByLocalId(LocalCollectionId id, out ModCollection collection) + { + collection = _collectionManager.Storage.ByLocalId(id); + return collection != ModCollection.Empty; + } /// Try to resolve the given game path to the replaced path. public (FullPath?, ResolveData) ResolvePath(Utf8GamePath path, ResourceCategory category, ResourceType resourceType) @@ -113,7 +113,7 @@ public class PathResolver : IDisposable // so that the functions loading tex and shpk can find that path and use its collection. // We also need to handle defaulted materials against a non-default collection. var path = resolved == null ? gamePath.Path : resolved.Value.InternalName; - SubfileHelper.HandleCollection(resolveData, path, nonDefault, type, resolved, out var pair); + SubfileHelper.HandleCollection(resolveData, path, nonDefault, type, resolved, gamePath, out var pair); return pair; } @@ -131,23 +131,21 @@ public class PathResolver : IDisposable } /// After loading an IMC file, replace its contents with the modded IMC file. - private unsafe void ImcLoadResource(ResourceHandle* resource, ByteString path, bool returnValue, bool custom, ByteString additionalData) + private unsafe void ImcLoadResource(ResourceHandle* resource, ByteString path, bool returnValue, bool custom, + ReadOnlySpan additionalData) { - if (resource->FileType != ResourceType.Imc) + if (resource->FileType != ResourceType.Imc + || !PathDataHandler.Read(additionalData, out var data) + || data.Discriminator != PathDataHandler.Discriminator + || !Utf8GamePath.FromByteString(path, out var gamePath) + || !CollectionByLocalId(data.Collection, out var collection) + || !collection.HasCache + || !collection.GetImcFile(gamePath, out var file)) return; - var lastUnderscore = additionalData.LastIndexOf((byte)'_'); - var idString = lastUnderscore == -1 ? additionalData : additionalData.Substring(0, lastUnderscore); - if (Utf8GamePath.FromByteString(path, out var gamePath) - && GuidExtensions.FromOptimizedString(idString.Span, out var id) - && CollectionById(id, out var collection) - && collection.HasCache - && collection.GetImcFile(gamePath, out var file)) - { - file.Replace(resource); - Penumbra.Log.Verbose( - $"[ResourceLoader] Loaded {gamePath} from file and replaced with IMC from collection {collection.AnonymizedName}."); - } + file.Replace(resource); + Penumbra.Log.Verbose( + $"[ResourceLoader] Loaded {gamePath} from file and replaced with IMC from collection {collection.AnonymizedName}."); } /// Resolve a path from the interface collection. diff --git a/Penumbra/Interop/PathResolving/SubfileHelper.cs b/Penumbra/Interop/PathResolving/SubfileHelper.cs index 844baaa9..793ea20b 100644 --- a/Penumbra/Interop/PathResolving/SubfileHelper.cs +++ b/Penumbra/Interop/PathResolving/SubfileHelper.cs @@ -66,21 +66,18 @@ public sealed unsafe class SubfileHelper : IDisposable, IReadOnlyCollection Materials, TMB, and AVFX need to be set per collection so they can load their sub files independently from each other. + /// Materials, TMB, and AVFX need to be set per collection, so they can load their sub files independently of each other. public static void HandleCollection(ResolveData resolveData, ByteString path, bool nonDefault, ResourceType type, FullPath? resolved, - out (FullPath?, ResolveData) data) + Utf8GamePath originalPath, out (FullPath?, ResolveData) data) { if (nonDefault) - switch (type) + resolved = type switch { - case ResourceType.Mtrl: - case ResourceType.Avfx: - case ResourceType.Tmb: - var fullPath = new FullPath($"|{resolveData.ModCollection.Id.OptimizedString()}_{resolveData.ModCollection.ChangeCounter}|{path}"); - data = (fullPath, resolveData); - return; - } - + ResourceType.Mtrl => PathDataHandler.CreateMtrl(path, resolveData.ModCollection, originalPath), + ResourceType.Avfx => PathDataHandler.CreateAvfx(path, resolveData.ModCollection), + ResourceType.Tmb => PathDataHandler.CreateTmb(path, resolveData.ModCollection), + _ => resolved, + }; data = (resolved, resolveData); } diff --git a/Penumbra/Interop/ResourceLoading/CreateFileWHook.cs b/Penumbra/Interop/ResourceLoading/CreateFileWHook.cs index 8a5e779b..bde640d2 100644 --- a/Penumbra/Interop/ResourceLoading/CreateFileWHook.cs +++ b/Penumbra/Interop/ResourceLoading/CreateFileWHook.cs @@ -1,5 +1,6 @@ using Dalamud.Hooking; using Dalamud.Plugin.Services; +using OtterGui.Services; using Penumbra.String; using Penumbra.String.Classes; using Penumbra.String.Functions; @@ -11,7 +12,7 @@ namespace Penumbra.Interop.ResourceLoading; /// we use the fixed size buffers of their formats to only store pointers to the actual path instead. /// Then we translate the stored pointer to the path in CreateFileW, if the prefix matches. /// -public unsafe class CreateFileWHook : IDisposable +public unsafe class CreateFileWHook : IDisposable, IRequiredService { public const int Size = 28; diff --git a/Penumbra/Interop/ResourceLoading/ResourceLoader.cs b/Penumbra/Interop/ResourceLoading/ResourceLoader.cs index 6c2c83b3..7b49beab 100644 --- a/Penumbra/Interop/ResourceLoading/ResourceLoader.cs +++ b/Penumbra/Interop/ResourceLoading/ResourceLoader.cs @@ -1,6 +1,7 @@ using FFXIVClientStructs.FFXIV.Client.System.Resource; using Penumbra.Api.Enums; using Penumbra.Collections; +using Penumbra.Interop.PathResolving; using Penumbra.Interop.SafeHandles; using Penumbra.Interop.Structs; using Penumbra.String; @@ -17,8 +18,7 @@ public unsafe class ResourceLoader : IDisposable private ResolveData _resolvedData = ResolveData.Invalid; - public ResourceLoader(ResourceService resources, FileReadService fileReadService, TexMdlService texMdlService, - CreateFileWHook _) + public ResourceLoader(ResourceService resources, FileReadService fileReadService, TexMdlService texMdlService) { _resources = resources; _fileReadService = fileReadService; @@ -54,7 +54,7 @@ public unsafe class ResourceLoader : IDisposable /// Reset the ResolvePath function to always return null. public void ResetResolvePath() - => ResolvePath = (_1, _2, _3) => (null, ResolveData.Invalid); + => ResolvePath = (_, _, _) => (null, ResolveData.Invalid); public delegate void ResourceLoadedDelegate(ResourceHandle* handle, Utf8GamePath originalPath, FullPath? manipulatedPath, ResolveData resolveData); @@ -67,7 +67,7 @@ public unsafe class ResourceLoader : IDisposable public event ResourceLoadedDelegate? ResourceLoaded; public delegate void FileLoadedDelegate(ResourceHandle* resource, ByteString path, bool returnValue, bool custom, - ByteString additionalData); + ReadOnlySpan additionalData); /// /// Event fired whenever a resource is newly loaded. @@ -132,19 +132,17 @@ public unsafe class ResourceLoader : IDisposable // Paths starting with a '|' are handled separately to allow for special treatment. // They are expected to also have a closing '|'. - if (gamePath.Path[0] != (byte)'|') + if (!PathDataHandler.Split(gamePath.Path.Span, out var actualPath, out var data)) { - returnValue = DefaultLoadResource(gamePath.Path, fileDescriptor, priority, isSync, ByteString.Empty); + returnValue = DefaultLoadResource(gamePath.Path, fileDescriptor, priority, isSync, []); return; } - // Split the path into the special-treatment part (between the first and second '|') - // and the actual path. - var split = gamePath.Path.Split((byte)'|', 3, false); - fileDescriptor->ResourceHandle->FileNameData = split[2].Path; - fileDescriptor->ResourceHandle->FileNameLength = split[2].Length; + var path = ByteString.FromSpanUnsafe(actualPath, gamePath.Path.IsNullTerminated, gamePath.Path.IsAsciiLowerCase, gamePath.Path.IsAscii); + fileDescriptor->ResourceHandle->FileNameData = path.Path; + fileDescriptor->ResourceHandle->FileNameLength = path.Length; MtrlForceSync(fileDescriptor, ref isSync); - returnValue = DefaultLoadResource(split[2], fileDescriptor, priority, isSync, split[1]); + returnValue = DefaultLoadResource(path, fileDescriptor, priority, isSync, data); // Return original resource handle path so that they can be loaded separately. fileDescriptor->ResourceHandle->FileNameData = gamePath.Path.Path; fileDescriptor->ResourceHandle->FileNameLength = gamePath.Path.Length; @@ -153,7 +151,7 @@ public unsafe class ResourceLoader : IDisposable /// Load a resource by its path. If it is rooted, it will be loaded from the drive, otherwise from the SqPack. private byte DefaultLoadResource(ByteString gamePath, SeFileDescriptor* fileDescriptor, int priority, - bool isSync, ByteString additionalData) + bool isSync, ReadOnlySpan additionalData) { if (Utf8GamePath.IsRooted(gamePath)) { diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.cs b/Penumbra/Interop/ResourceTree/ResolveContext.cs index 615ef2b0..7c8da41f 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.cs @@ -9,6 +9,7 @@ using Penumbra.Collections; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; +using Penumbra.Interop.PathResolving; using Penumbra.Interop.Services; using Penumbra.String; using Penumbra.String.Classes; @@ -373,14 +374,8 @@ internal unsafe partial record ResolveContext( if (name.IsEmpty) return ByteString.Empty; - if (stripPrefix && name[0] == (byte)'|') - { - var pos = name.IndexOf((byte)'|', 1); - if (pos < 0) - return ByteString.Empty; - - name = name.Substring(pos + 1); - } + if (stripPrefix && PathDataHandler.Split(name.Span, out var path, out _)) + name = ByteString.FromSpanUnsafe(path, name.IsNullTerminated, name.IsAsciiLowerCase, name.IsAscii); return name; } diff --git a/Penumbra/Mods/TemporaryMod.cs b/Penumbra/Mods/TemporaryMod.cs index d08c8b06..6e6e72ab 100644 --- a/Penumbra/Mods/TemporaryMod.cs +++ b/Penumbra/Mods/TemporaryMod.cs @@ -1,5 +1,6 @@ using OtterGui.Classes; using Penumbra.Collections; +using Penumbra.Interop.PathResolving; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Editor; using Penumbra.Mods.Groups; @@ -83,10 +84,8 @@ public class TemporaryMod : IMod } var targetPath = fullPath.Path.FullName; - if (fullPath.Path.Name.StartsWith('|')) - { - targetPath = targetPath.Split('|', 3, StringSplitOptions.RemoveEmptyEntries).Last(); - } + if (PathDataHandler.Split(fullPath.Path.FullName, out var actualPath, out _)) + targetPath = actualPath.ToString(); if (Path.IsPathRooted(targetPath)) { diff --git a/Penumbra/Services/ConfigMigrationService.cs b/Penumbra/Services/ConfigMigrationService.cs index 1f6ac170..70b05a73 100644 --- a/Penumbra/Services/ConfigMigrationService.cs +++ b/Penumbra/Services/ConfigMigrationService.cs @@ -379,7 +379,8 @@ public class ConfigMigrationService(SaveService saveService, BackupService backu dict = dict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value with { Priority = maxPriority - kvp.Value.Priority }); var emptyStorage = new ModStorage(); - var collection = ModCollection.CreateFromData(saveService, emptyStorage, Guid.NewGuid(), ModCollection.DefaultCollectionName, 0, 1, dict, []); + // Only used for saving and immediately discarded, so the local collection id here is irrelevant. + var collection = ModCollection.CreateFromData(saveService, emptyStorage, Guid.NewGuid(), ModCollection.DefaultCollectionName, LocalCollectionId.Zero, 0, 1, dict, []); saveService.ImmediateSaveSync(new ModCollectionSave(emptyStorage, collection)); } catch (Exception e) diff --git a/Penumbra/Services/CrashHandlerService.cs b/Penumbra/Services/CrashHandlerService.cs index 6805e7db..1239578b 100644 --- a/Penumbra/Services/CrashHandlerService.cs +++ b/Penumbra/Services/CrashHandlerService.cs @@ -7,6 +7,7 @@ using Penumbra.Communication; using Penumbra.CrashHandler; using Penumbra.CrashHandler.Buffers; using Penumbra.GameData.Actors; +using Penumbra.Interop.PathResolving; using Penumbra.Interop.ResourceLoading; using Penumbra.Interop.Structs; using Penumbra.String; @@ -286,8 +287,7 @@ public sealed class CrashHandlerService : IDisposable, IService try { - var dashIdx = manipulatedPath.Value.InternalName[0] == (byte)'|' ? manipulatedPath.Value.InternalName.IndexOf((byte)'|', 1) : -1; - if (dashIdx >= 0 && !Utf8GamePath.IsRooted(manipulatedPath.Value.InternalName.Substring(dashIdx + 1))) + if (PathDataHandler.Split(manipulatedPath.Value.FullName, out var actualPath, out _) && Path.IsPathRooted(actualPath)) return; var name = GetActorName(resolveData.AssociatedGameObject); diff --git a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs index d5ff1abd..65a8fe76 100644 --- a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs +++ b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs @@ -236,7 +236,7 @@ public sealed class ResourceWatcher : IDisposable, ITab _newRecords.Enqueue(record); } - private unsafe void OnFileLoaded(ResourceHandle* resource, ByteString path, bool success, bool custom, ByteString _) + private unsafe void OnFileLoaded(ResourceHandle* resource, ByteString path, bool success, bool custom, ReadOnlySpan _) { if (_ephemeral.EnableResourceLogging && FilterMatch(path, out var match)) Penumbra.Log.Information( From a6661f15e87ad8b8b15d386943d08e20001c9848 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Thu, 30 May 2024 20:46:04 +0200 Subject: [PATCH 184/433] Display the additional path data in ResourceTree --- .../Interop/MaterialPreview/MaterialInfo.cs | 11 +++++--- .../ResolveContext.PathResolution.cs | 2 +- .../Interop/ResourceTree/ResolveContext.cs | 27 +++++++------------ Penumbra/Interop/ResourceTree/ResourceNode.cs | 4 +++ .../UI/AdvancedWindow/ResourceTreeViewer.cs | 8 ++++-- 5 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Penumbra/Interop/MaterialPreview/MaterialInfo.cs b/Penumbra/Interop/MaterialPreview/MaterialInfo.cs index 61e7c764..f7e6caf0 100644 --- a/Penumbra/Interop/MaterialPreview/MaterialInfo.cs +++ b/Penumbra/Interop/MaterialPreview/MaterialInfo.cs @@ -3,8 +3,9 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Render; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using Penumbra.GameData.Interop; using Penumbra.GameData.Structs; -using Penumbra.Interop.ResourceTree; +using Penumbra.Interop.PathResolving; using Penumbra.String; +using static Penumbra.Interop.Structs.StructExtensions; using Model = Penumbra.GameData.Interop.Model; namespace Penumbra.Interop.MaterialPreview; @@ -78,8 +79,12 @@ public readonly record struct MaterialInfo(ObjectIndex ObjectIndex, DrawObjectTy continue; var mtrlHandle = material->MaterialResourceHandle; - var path = ResolveContext.GetResourceHandlePath(&mtrlHandle->ResourceHandle); - if (path == needle) + if (mtrlHandle == null) + continue; + + PathDataHandler.Split(mtrlHandle->ResourceHandle.FileName.AsSpan(), out var path, out _); + var fileName = ByteString.FromSpanUnsafe(path, true); + if (fileName == needle) result.Add(new MaterialInfo(index, type, i, j)); } } diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs index d5b4fa39..236c7051 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs @@ -155,7 +155,7 @@ internal partial record ResolveContext var imcFileData = imc->GetDataSpan(); if (imcFileData.IsEmpty) { - Penumbra.Log.Warning($"IMC resource handle with path {GetResourceHandlePath(imc, false)} doesn't have a valid data span"); + Penumbra.Log.Warning($"IMC resource handle with path {imc->FileName.AsByteString()} doesn't have a valid data span"); return variant.Id; } diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.cs b/Penumbra/Interop/ResourceTree/ResolveContext.cs index 7c8da41f..e38bf4f6 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.cs @@ -111,12 +111,18 @@ internal unsafe partial record ResolveContext( if (resourceHandle == null) throw new ArgumentNullException(nameof(resourceHandle)); - var fullPath = Utf8GamePath.FromByteString(GetResourceHandlePath(resourceHandle), out var p) ? new FullPath(p) : FullPath.Empty; + var fileName = resourceHandle->FileName.AsSpan(); + var additionalData = ByteString.Empty; + if (PathDataHandler.Split(fileName, out fileName, out var data)) + additionalData = ByteString.FromSpanUnsafe(data, false).Clone(); + + var fullPath = Utf8GamePath.FromSpan(fileName, out var p) ? new FullPath(p.Clone()) : FullPath.Empty; var node = new ResourceNode(type, objectAddress, (nint)resourceHandle, GetResourceHandleLength(resourceHandle), this) { - GamePath = gamePath, - FullPath = fullPath, + GamePath = gamePath, + FullPath = fullPath, + AdditionalData = additionalData, }; if (autoAdd) Global.Nodes.Add((gamePath, (nint)resourceHandle), node); @@ -365,21 +371,6 @@ internal unsafe partial record ResolveContext( return i >= 0 && i < array.Length ? array[i] : null; } - internal static ByteString GetResourceHandlePath(ResourceHandle* handle, bool stripPrefix = true) - { - if (handle == null) - return ByteString.Empty; - - var name = handle->FileName.AsByteString(); - if (name.IsEmpty) - return ByteString.Empty; - - if (stripPrefix && PathDataHandler.Split(name.Span, out var path, out _)) - name = ByteString.FromSpanUnsafe(path, name.IsNullTerminated, name.IsAsciiLowerCase, name.IsAscii); - - return name; - } - private static ulong GetResourceHandleLength(ResourceHandle* handle) { if (handle == null) diff --git a/Penumbra/Interop/ResourceTree/ResourceNode.cs b/Penumbra/Interop/ResourceTree/ResourceNode.cs index 7ec75893..e74edb91 100644 --- a/Penumbra/Interop/ResourceTree/ResourceNode.cs +++ b/Penumbra/Interop/ResourceTree/ResourceNode.cs @@ -1,4 +1,5 @@ using Penumbra.Api.Enums; +using Penumbra.String; using Penumbra.String.Classes; using ChangedItemIcon = Penumbra.UI.ChangedItemDrawer.ChangedItemIcon; @@ -15,6 +16,7 @@ public class ResourceNode : ICloneable public readonly nint ResourceHandle; public Utf8GamePath[] PossibleGamePaths; public FullPath FullPath; + public ByteString AdditionalData; public readonly ulong Length; public readonly List Children; internal ResolveContext? ResolveContext; @@ -40,6 +42,7 @@ public class ResourceNode : ICloneable ObjectAddress = objectAddress; ResourceHandle = resourceHandle; PossibleGamePaths = Array.Empty(); + AdditionalData = ByteString.Empty; Length = length; Children = new List(); ResolveContext = resolveContext; @@ -56,6 +59,7 @@ public class ResourceNode : ICloneable ResourceHandle = other.ResourceHandle; PossibleGamePaths = other.PossibleGamePaths; FullPath = other.FullPath; + AdditionalData = other.AdditionalData; Length = other.Length; Children = other.Children; ResolveContext = other.ResolveContext; diff --git a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs index d31f3e52..3ada77df 100644 --- a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs +++ b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs @@ -5,6 +5,7 @@ using OtterGui.Raii; using OtterGui; using Penumbra.Interop.ResourceTree; using Penumbra.UI.Classes; +using Penumbra.String; namespace Penumbra.UI.AdvancedWindow; @@ -177,6 +178,9 @@ public class ResourceTreeViewer return NodeVisibility.Hidden; } + string GetAdditionalDataSuffix(ByteString data) + => !debugMode || data.IsEmpty ? string.Empty : $"\n\nAdditional Data: {data}"; + foreach (var (resourceNode, index) in resourceNodes.WithIndex()) { var visibility = GetNodeVisibility(resourceNode); @@ -260,13 +264,13 @@ public class ResourceTreeViewer ImGui.Selectable(resourceNode.FullPath.ToPath(), false, 0, new Vector2(ImGui.GetContentRegionAvail().X, cellHeight)); if (ImGui.IsItemClicked()) ImGui.SetClipboardText(resourceNode.FullPath.ToPath()); - ImGuiUtil.HoverTooltip($"{resourceNode.FullPath.ToPath()}\n\nClick to copy to clipboard."); + ImGuiUtil.HoverTooltip($"{resourceNode.FullPath.ToPath()}\n\nClick to copy to clipboard.{GetAdditionalDataSuffix(resourceNode.AdditionalData)}"); } else { ImGui.Selectable("(unavailable)", false, ImGuiSelectableFlags.Disabled, new Vector2(ImGui.GetContentRegionAvail().X, cellHeight)); - ImGuiUtil.HoverTooltip("The actual path to this file is unavailable.\nIt may be managed by another plug-in."); + ImGuiUtil.HoverTooltip($"The actual path to this file is unavailable.\nIt may be managed by another plug-in.{GetAdditionalDataSuffix(resourceNode.AdditionalData)}"); } mutedColor.Dispose(); From f4bdbcac5338f6bb7deb5e021ca2e4d6236de739 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Thu, 30 May 2024 23:09:26 +0200 Subject: [PATCH 185/433] Make Resource Trees honor Incognito Mode --- Penumbra/Interop/ResourceTree/ResourceTree.cs | 26 ++++++----- .../ResourceTree/ResourceTreeFactory.cs | 43 ++++++++++--------- Penumbra/Services/StaticServiceManager.cs | 2 + Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 6 +-- .../UI/AdvancedWindow/ResourceTreeViewer.cs | 17 +++++--- .../ResourceTreeViewerFactory.cs | 13 ++++++ Penumbra/UI/ChangedItemDrawer.cs | 7 ++- Penumbra/UI/CollectionTab/CollectionPanel.cs | 11 +++-- .../UI/CollectionTab/CollectionSelector.cs | 8 ++-- Penumbra/UI/CollectionTab/InheritanceUi.cs | 4 +- Penumbra/UI/IncognitoService.cs | 29 +++++++++++++ Penumbra/UI/Tabs/CollectionsTab.cs | 25 ++++------- Penumbra/UI/Tabs/OnScreenTab.cs | 7 +-- 13 files changed, 123 insertions(+), 75 deletions(-) create mode 100644 Penumbra/UI/AdvancedWindow/ResourceTreeViewerFactory.cs create mode 100644 Penumbra/UI/IncognitoService.cs diff --git a/Penumbra/Interop/ResourceTree/ResourceTree.cs b/Penumbra/Interop/ResourceTree/ResourceTree.cs index dac86e44..fc8c805a 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTree.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTree.cs @@ -15,6 +15,7 @@ namespace Penumbra.Interop.ResourceTree; public class ResourceTree { public readonly string Name; + public readonly string AnonymizedName; public readonly int GameObjectIndex; public readonly nint GameObjectAddress; public readonly nint DrawObjectAddress; @@ -22,6 +23,7 @@ public class ResourceTree public readonly bool PlayerRelated; public readonly bool Networked; public readonly string CollectionName; + public readonly string AnonymizedCollectionName; public readonly List Nodes; public readonly HashSet FlatNodes; @@ -29,18 +31,20 @@ public class ResourceTree public CustomizeData CustomizeData; public GenderRace RaceCode; - public ResourceTree(string name, int gameObjectIndex, nint gameObjectAddress, nint drawObjectAddress, bool localPlayerRelated, bool playerRelated, bool networked, string collectionName) + public ResourceTree(string name, string anonymizedName, int gameObjectIndex, nint gameObjectAddress, nint drawObjectAddress, bool localPlayerRelated, bool playerRelated, bool networked, string collectionName, string anonymizedCollectionName) { - Name = name; - GameObjectIndex = gameObjectIndex; - GameObjectAddress = gameObjectAddress; - DrawObjectAddress = drawObjectAddress; - LocalPlayerRelated = localPlayerRelated; - Networked = networked; - PlayerRelated = playerRelated; - CollectionName = collectionName; - Nodes = new List(); - FlatNodes = new HashSet(); + Name = name; + AnonymizedName = anonymizedName; + GameObjectIndex = gameObjectIndex; + GameObjectAddress = gameObjectAddress; + DrawObjectAddress = drawObjectAddress; + LocalPlayerRelated = localPlayerRelated; + Networked = networked; + PlayerRelated = playerRelated; + CollectionName = collectionName; + AnonymizedCollectionName = anonymizedCollectionName; + Nodes = new List(); + FlatNodes = new HashSet(); } public void ProcessPostfix(Action action) diff --git a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs index ae7187f0..a722e344 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs @@ -72,10 +72,10 @@ public class ResourceTreeFactory( return null; var localPlayerRelated = cache.IsLocalPlayerRelated(character); - var (name, related) = GetCharacterName(character, cache); + var (name, anonymizedName, related) = GetCharacterName(character); var networked = character.ObjectId != Dalamud.Game.ClientState.Objects.Types.GameObject.InvalidGameObjectId; - var tree = new ResourceTree(name, character.ObjectIndex, (nint)gameObjStruct, (nint)drawObjStruct, localPlayerRelated, related, - networked, collectionResolveData.ModCollection.Name); + var tree = new ResourceTree(name, anonymizedName, character.ObjectIndex, (nint)gameObjStruct, (nint)drawObjStruct, localPlayerRelated, related, + networked, collectionResolveData.ModCollection.Name, collectionResolveData.ModCollection.AnonymizedName); var globalContext = new GlobalResolveContext(identifier, collectionResolveData.ModCollection, cache, (flags & Flags.WithUiData) != 0); using (var _ = pathState.EnterInternalResolve()) @@ -157,27 +157,30 @@ public class ResourceTreeFactory( } } - private unsafe (string Name, bool PlayerRelated) GetCharacterName(Dalamud.Game.ClientState.Objects.Types.Character character, - TreeBuildCache cache) + private unsafe (string Name, string AnonymizedName, bool PlayerRelated) GetCharacterName(Dalamud.Game.ClientState.Objects.Types.Character character) { var identifier = actors.FromObject((GameObject*)character.Address, out var owner, true, false, false); - switch (identifier.Type) - { - case IdentifierType.Player: return (identifier.PlayerName.ToString(), true); - case IdentifierType.Owned: - var ownerChara = objects.Objects.CreateObjectReference(owner) as Dalamud.Game.ClientState.Objects.Types.Character; - if (ownerChara != null) - { - var ownerName = GetCharacterName(ownerChara, cache); - return ($"[{ownerName.Name}] {character.Name} ({identifier.Kind.ToName()})", ownerName.PlayerRelated); - } - - break; - } - - return ($"{character.Name} ({identifier.Kind.ToName()})", false); + var identifierStr = identifier.ToString(); + return (identifierStr, identifier.Incognito(identifierStr), IsPlayerRelated(identifier, owner)); } + private unsafe bool IsPlayerRelated(Dalamud.Game.ClientState.Objects.Types.Character? character) + { + if (character == null) + return false; + + var identifier = actors.FromObject((GameObject*)character.Address, out var owner, true, false, false); + return IsPlayerRelated(identifier, owner); + } + + private bool IsPlayerRelated(ActorIdentifier identifier, Actor owner) + => identifier.Type switch + { + IdentifierType.Player => true, + IdentifierType.Owned => IsPlayerRelated(objects.Objects.CreateObjectReference(owner) as Dalamud.Game.ClientState.Objects.Types.Character), + _ => false, + }; + [Flags] public enum Flags { diff --git a/Penumbra/Services/StaticServiceManager.cs b/Penumbra/Services/StaticServiceManager.cs index 0c6648ba..146d7ee0 100644 --- a/Penumbra/Services/StaticServiceManager.cs +++ b/Penumbra/Services/StaticServiceManager.cs @@ -149,6 +149,7 @@ public static class StaticServiceManager private static ServiceManager AddInterface(this ServiceManager services) => services.AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() @@ -181,6 +182,7 @@ public static class StaticServiceManager .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton(p => new Diagnostics(p)); private static ServiceManager AddModEditor(this ServiceManager services) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index 6b48a048..af01047b 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -585,7 +585,8 @@ public partial class ModEditWindow : Window, IDisposable Configuration config, ModEditor editor, ResourceTreeFactory resourceTreeFactory, MetaFileManager metaFileManager, StainService stainService, ActiveCollections activeCollections, ModMergeTab modMergeTab, CommunicatorService communicator, TextureManager textures, ModelManager models, IDragDropManager dragDropManager, - ChangedItemDrawer changedItemDrawer, ObjectManager objects, IFramework framework, CharacterBaseDestructor characterBaseDestructor) + ResourceTreeViewerFactory resourceTreeViewerFactory, ObjectManager objects, IFramework framework, + CharacterBaseDestructor characterBaseDestructor) : base(WindowBaseLabel) { _performance = performance; @@ -618,8 +619,7 @@ public partial class ModEditWindow : Window, IDisposable _center = new CombinedTexture(_left, _right); _textureSelectCombo = new TextureDrawer.PathSelectCombo(textures, editor, () => GetPlayerResourcesOfType(ResourceType.Tex)); _resourceTreeFactory = resourceTreeFactory; - _quickImportViewer = - new ResourceTreeViewer(_config, resourceTreeFactory, changedItemDrawer, 2, OnQuickImportRefresh, DrawQuickImportActions); + _quickImportViewer = resourceTreeViewerFactory.Create(2, OnQuickImportRefresh, DrawQuickImportActions); _communicator.ModPathChanged.Subscribe(OnModPathChange, ModPathChanged.Priority.ModEditWindow); IsOpen = _config is { OpenWindowAtStart: true, Ephemeral.AdvancedEditingOpen: true }; } diff --git a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs index 3ada77df..c0c49e47 100644 --- a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs +++ b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs @@ -6,6 +6,7 @@ using OtterGui; using Penumbra.Interop.ResourceTree; using Penumbra.UI.Classes; using Penumbra.String; +using Penumbra.UI.Tabs; namespace Penumbra.UI.AdvancedWindow; @@ -17,6 +18,7 @@ public class ResourceTreeViewer private readonly Configuration _config; private readonly ResourceTreeFactory _treeFactory; private readonly ChangedItemDrawer _changedItemDrawer; + private readonly IncognitoService _incognito; private readonly int _actionCapacity; private readonly Action _onRefresh; private readonly Action _drawActions; @@ -29,11 +31,12 @@ public class ResourceTreeViewer private Task? _task; public ResourceTreeViewer(Configuration config, ResourceTreeFactory treeFactory, ChangedItemDrawer changedItemDrawer, - int actionCapacity, Action onRefresh, Action drawActions) + IncognitoService incognito, int actionCapacity, Action onRefresh, Action drawActions) { _config = config; _treeFactory = treeFactory; _changedItemDrawer = changedItemDrawer; + _incognito = incognito; _actionCapacity = actionCapacity; _onRefresh = onRefresh; _drawActions = drawActions; @@ -75,7 +78,7 @@ public class ResourceTreeViewer using (var c = ImRaii.PushColor(ImGuiCol.Text, CategoryColor(category).Value())) { - var isOpen = ImGui.CollapsingHeader($"{tree.Name}##{index}", index == 0 ? ImGuiTreeNodeFlags.DefaultOpen : 0); + var isOpen = ImGui.CollapsingHeader($"{(_incognito.IncognitoMode ? tree.AnonymizedName : tree.Name)}###{index}", index == 0 ? ImGuiTreeNodeFlags.DefaultOpen : 0); if (debugMode) { using var _ = ImRaii.PushFont(UiBuilder.MonoFont); @@ -89,7 +92,7 @@ public class ResourceTreeViewer using var id = ImRaii.PushId(index); - ImGui.TextUnformatted($"Collection: {tree.CollectionName}"); + ImGui.TextUnformatted($"Collection: {(_incognito.IncognitoMode ? tree.AnonymizedCollectionName : tree.CollectionName)}"); using var table = ImRaii.Table("##ResourceTree", _actionCapacity > 0 ? 4 : 3, ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg); @@ -137,10 +140,14 @@ public class ResourceTreeViewer ImGui.SameLine(0, checkPadding); - _changedItemDrawer.DrawTypeFilter(ref _typeFilter, -yOffset); + ImGui.SetCursorPosY(ImGui.GetCursorPosY() - yOffset); + using (ImRaii.Child("##typeFilter", new Vector2(ImGui.GetContentRegionAvail().X, ChangedItemDrawer.TypeFilterIconSize.Y))) + _changedItemDrawer.DrawTypeFilter(ref _typeFilter); - ImGui.SetNextItemWidth(ImGui.GetContentRegionAvail().X); + ImGui.SetNextItemWidth(ImGui.GetContentRegionAvail().X - checkSpacing - ImGui.GetFrameHeightWithSpacing()); ImGui.InputTextWithHint("##TreeNameFilter", "Filter by Character/Entity Name...", ref _nameFilter, 128); + ImGui.SameLine(0, checkSpacing); + _incognito.DrawToggle(); } private Task RefreshCharacterList() diff --git a/Penumbra/UI/AdvancedWindow/ResourceTreeViewerFactory.cs b/Penumbra/UI/AdvancedWindow/ResourceTreeViewerFactory.cs new file mode 100644 index 00000000..91dab6cb --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/ResourceTreeViewerFactory.cs @@ -0,0 +1,13 @@ +using Penumbra.Interop.ResourceTree; + +namespace Penumbra.UI.AdvancedWindow; + +public class ResourceTreeViewerFactory( + Configuration config, + ResourceTreeFactory treeFactory, + ChangedItemDrawer changedItemDrawer, + IncognitoService incognito) +{ + public ResourceTreeViewer Create(int actionCapacity, Action onRefresh, Action drawActions) + => new(config, treeFactory, changedItemDrawer, incognito, actionCapacity, onRefresh, drawActions); +} diff --git a/Penumbra/UI/ChangedItemDrawer.cs b/Penumbra/UI/ChangedItemDrawer.cs index 638afef0..29a1f291 100644 --- a/Penumbra/UI/ChangedItemDrawer.cs +++ b/Penumbra/UI/ChangedItemDrawer.cs @@ -212,7 +212,7 @@ public class ChangedItemDrawer : IDisposable return; var typeFilter = _config.Ephemeral.ChangedItemFilter; - if (DrawTypeFilter(ref typeFilter, 0.0f)) + if (DrawTypeFilter(ref typeFilter)) { _config.Ephemeral.ChangedItemFilter = typeFilter; _config.Ephemeral.Save(); @@ -220,7 +220,7 @@ public class ChangedItemDrawer : IDisposable } /// Draw a header line with the different icon types to filter them. - public bool DrawTypeFilter(ref ChangedItemIcon typeFilter, float yOffset) + public bool DrawTypeFilter(ref ChangedItemIcon typeFilter) { var ret = false; using var _ = ImRaii.PushId("ChangedItemIconFilter"); @@ -233,7 +233,6 @@ public class ChangedItemDrawer : IDisposable var ret = false; var icon = _icons[type]; var flag = typeFilter.HasFlag(type); - ImGui.SetCursorPosY(ImGui.GetCursorPosY() + yOffset); ImGui.Image(icon.ImGuiHandle, size, Vector2.Zero, Vector2.One, flag ? Vector4.One : new Vector4(0.6f, 0.3f, 0.3f, 1f)); if (ImGui.IsItemClicked(ImGuiMouseButton.Left)) { @@ -267,7 +266,7 @@ public class ChangedItemDrawer : IDisposable ImGui.SameLine(); } - ImGui.SetCursorPos(new Vector2(ImGui.GetContentRegionMax().X - size.X, ImGui.GetCursorPosY() + yOffset)); + ImGui.SetCursorPosX(ImGui.GetContentRegionMax().X - size.X); ImGui.Image(_icons[AllFlags].ImGuiHandle, size, Vector2.Zero, Vector2.One, typeFilter == 0 ? new Vector4(0.6f, 0.3f, 0.3f, 1f) : typeFilter == AllFlags ? new Vector4(0.75f, 0.75f, 0.75f, 1f) : new Vector4(0.5f, 0.5f, 1f, 1f)); diff --git a/Penumbra/UI/CollectionTab/CollectionPanel.cs b/Penumbra/UI/CollectionTab/CollectionPanel.cs index bb22e6a7..cb4dbe20 100644 --- a/Penumbra/UI/CollectionTab/CollectionPanel.cs +++ b/Penumbra/UI/CollectionTab/CollectionPanel.cs @@ -31,6 +31,7 @@ public sealed class CollectionPanel : IDisposable private readonly InheritanceUi _inheritanceUi; private readonly ModStorage _mods; private readonly FilenameService _fileNames; + private readonly IncognitoService _incognito; private readonly IFontHandle _nameFont; private static readonly IReadOnlyDictionary Buttons = CreateButtons(); @@ -41,7 +42,8 @@ public sealed class CollectionPanel : IDisposable private int _draggedIndividualAssignment = -1; public CollectionPanel(DalamudPluginInterface pi, CommunicatorService communicator, CollectionManager manager, - CollectionSelector selector, ActorManager actors, ITargetManager targets, ModStorage mods, FilenameService fileNames) + CollectionSelector selector, ActorManager actors, ITargetManager targets, ModStorage mods, FilenameService fileNames, + IncognitoService incognito) { _collections = manager.Storage; _active = manager.Active; @@ -50,8 +52,9 @@ public sealed class CollectionPanel : IDisposable _targets = targets; _mods = mods; _fileNames = fileNames; + _incognito = incognito; _individualAssignmentUi = new IndividualAssignmentUi(communicator, actors, manager); - _inheritanceUi = new InheritanceUi(manager, _selector); + _inheritanceUi = new InheritanceUi(manager, incognito); _nameFont = pi.UiBuilder.FontAtlas.NewGameFontHandle(new GameFontStyle(GameFontFamilyAndSize.Jupiter23)); } @@ -415,7 +418,7 @@ public sealed class CollectionPanel : IDisposable /// Respect incognito mode for names of identifiers. private string Name(ActorIdentifier id, string? name) - => _selector.IncognitoMode && id.Type is IdentifierType.Player or IdentifierType.Owned + => _incognito.IncognitoMode && id.Type is IdentifierType.Player or IdentifierType.Owned ? id.Incognito(name) : name ?? id.ToString(); @@ -423,7 +426,7 @@ public sealed class CollectionPanel : IDisposable private string Name(ModCollection? collection) => collection == null ? "Unassigned" : collection == ModCollection.Empty ? "Use No Mods" : - _selector.IncognitoMode ? collection.AnonymizedName : collection.Name; + _incognito.IncognitoMode ? collection.AnonymizedName : collection.Name; private void DrawIndividualButton(string intro, Vector2 width, string tooltip, char suffix, params ActorIdentifier[] identifiers) { diff --git a/Penumbra/UI/CollectionTab/CollectionSelector.cs b/Penumbra/UI/CollectionTab/CollectionSelector.cs index fac85d4d..24d3f591 100644 --- a/Penumbra/UI/CollectionTab/CollectionSelector.cs +++ b/Penumbra/UI/CollectionTab/CollectionSelector.cs @@ -17,13 +17,12 @@ public sealed class CollectionSelector : ItemSelector, IDisposabl private readonly CollectionStorage _storage; private readonly ActiveCollections _active; private readonly TutorialService _tutorial; + private readonly IncognitoService _incognito; private ModCollection? _dragging; - public bool IncognitoMode; - public CollectionSelector(Configuration config, CommunicatorService communicator, CollectionStorage storage, ActiveCollections active, - TutorialService tutorial) + TutorialService tutorial, IncognitoService incognito) : base([], Flags.Delete | Flags.Add | Flags.Duplicate | Flags.Filter) { _config = config; @@ -31,6 +30,7 @@ public sealed class CollectionSelector : ItemSelector, IDisposabl _storage = storage; _active = active; _tutorial = tutorial; + _incognito = incognito; _communicator.CollectionChange.Subscribe(OnCollectionChange, CollectionChange.Priority.CollectionSelector); // Set items. @@ -109,7 +109,7 @@ public sealed class CollectionSelector : ItemSelector, IDisposabl } private string Name(ModCollection collection) - => IncognitoMode ? collection.AnonymizedName : collection.Name; + => _incognito.IncognitoMode ? collection.AnonymizedName : collection.Name; private void OnCollectionChange(CollectionType type, ModCollection? old, ModCollection? @new, string _3) { diff --git a/Penumbra/UI/CollectionTab/InheritanceUi.cs b/Penumbra/UI/CollectionTab/InheritanceUi.cs index 2290592d..418fe52c 100644 --- a/Penumbra/UI/CollectionTab/InheritanceUi.cs +++ b/Penumbra/UI/CollectionTab/InheritanceUi.cs @@ -9,7 +9,7 @@ using Penumbra.UI.Classes; namespace Penumbra.UI.CollectionTab; -public class InheritanceUi(CollectionManager collectionManager, CollectionSelector selector) : IUiService +public class InheritanceUi(CollectionManager collectionManager, IncognitoService incognito) : IUiService { private const int InheritedCollectionHeight = 9; private const string InheritanceDragDropLabel = "##InheritanceMove"; @@ -312,5 +312,5 @@ public class InheritanceUi(CollectionManager collectionManager, CollectionSelect } private string Name(ModCollection collection) - => selector.IncognitoMode ? collection.AnonymizedName : collection.Name; + => incognito.IncognitoMode ? collection.AnonymizedName : collection.Name; } diff --git a/Penumbra/UI/IncognitoService.cs b/Penumbra/UI/IncognitoService.cs new file mode 100644 index 00000000..d4b1828f --- /dev/null +++ b/Penumbra/UI/IncognitoService.cs @@ -0,0 +1,29 @@ +using Dalamud.Interface.Utility; +using Dalamud.Interface; +using ImGuiNET; +using OtterGui; +using Penumbra.UI.Classes; +using OtterGui.Raii; + +namespace Penumbra.UI; + +public class IncognitoService(TutorialService tutorial) +{ + public bool IncognitoMode; + + public void DrawToggle(float? buttonWidth = null) + { + using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale); + using var color = ImRaii.PushColor(ImGuiCol.Text, ColorId.FolderExpanded.Value()) + .Push(ImGuiCol.Border, ColorId.FolderExpanded.Value()); + if (ImGuiUtil.DrawDisabledButton( + $"{(IncognitoMode ? FontAwesomeIcon.Eye : FontAwesomeIcon.EyeSlash).ToIconString()}###IncognitoMode", + new Vector2(buttonWidth ?? ImGui.GetFrameHeightWithSpacing(), ImGui.GetFrameHeight()), string.Empty, false, true)) + IncognitoMode = !IncognitoMode; + var hovered = ImGui.IsItemHovered(); + tutorial.OpenTutorial(BasicTutorialSteps.Incognito); + color.Pop(2); + if (hovered) + ImGui.SetTooltip(IncognitoMode ? "Toggle incognito mode off." : "Toggle incognito mode on."); + } +} diff --git a/Penumbra/UI/Tabs/CollectionsTab.cs b/Penumbra/UI/Tabs/CollectionsTab.cs index fe1471b3..1eaece50 100644 --- a/Penumbra/UI/Tabs/CollectionsTab.cs +++ b/Penumbra/UI/Tabs/CollectionsTab.cs @@ -21,6 +21,7 @@ public sealed class CollectionsTab : IDisposable, ITab private readonly CollectionSelector _selector; private readonly CollectionPanel _panel; private readonly TutorialService _tutorial; + private readonly IncognitoService _incognito; public enum PanelMode { @@ -40,13 +41,14 @@ public sealed class CollectionsTab : IDisposable, ITab } } - public CollectionsTab(DalamudPluginInterface pi, Configuration configuration, CommunicatorService communicator, + public CollectionsTab(DalamudPluginInterface pi, Configuration configuration, CommunicatorService communicator, IncognitoService incognito, CollectionManager collectionManager, ModStorage modStorage, ActorManager actors, ITargetManager targets, TutorialService tutorial, FilenameService fileNames) { - _config = configuration.Ephemeral; - _tutorial = tutorial; - _selector = new CollectionSelector(configuration, communicator, collectionManager.Storage, collectionManager.Active, _tutorial); - _panel = new CollectionPanel(pi, communicator, collectionManager, _selector, actors, targets, modStorage, fileNames); + _config = configuration.Ephemeral; + _tutorial = tutorial; + _incognito = incognito; + _selector = new CollectionSelector(configuration, communicator, collectionManager.Storage, collectionManager.Active, _tutorial, incognito); + _panel = new CollectionPanel(pi, communicator, collectionManager, _selector, actors, targets, modStorage, fileNames, incognito); } public void Dispose() @@ -116,18 +118,7 @@ public sealed class CollectionsTab : IDisposable, ITab _tutorial.OpenTutorial(BasicTutorialSteps.CollectionDetails); ImGui.SameLine(); - style.Push(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale); - color.Push(ImGuiCol.Text, ColorId.FolderExpanded.Value()) - .Push(ImGuiCol.Border, ColorId.FolderExpanded.Value()); - if (ImGuiUtil.DrawDisabledButton( - $"{(_selector.IncognitoMode ? FontAwesomeIcon.Eye : FontAwesomeIcon.EyeSlash).ToIconString()}###IncognitoMode", - buttonSize with { X = withSpacing }, string.Empty, false, true)) - _selector.IncognitoMode = !_selector.IncognitoMode; - var hovered = ImGui.IsItemHovered(); - _tutorial.OpenTutorial(BasicTutorialSteps.Incognito); - color.Pop(2); - if (hovered) - ImGui.SetTooltip(_selector.IncognitoMode ? "Toggle incognito mode off." : "Toggle incognito mode on."); + _incognito.DrawToggle(withSpacing); } private void DrawPanel() diff --git a/Penumbra/UI/Tabs/OnScreenTab.cs b/Penumbra/UI/Tabs/OnScreenTab.cs index 09772d8e..787e07a1 100644 --- a/Penumbra/UI/Tabs/OnScreenTab.cs +++ b/Penumbra/UI/Tabs/OnScreenTab.cs @@ -1,18 +1,15 @@ using OtterGui.Widgets; -using Penumbra.Interop.ResourceTree; using Penumbra.UI.AdvancedWindow; namespace Penumbra.UI.Tabs; public class OnScreenTab : ITab { - private readonly Configuration _config; private readonly ResourceTreeViewer _viewer; - public OnScreenTab(Configuration config, ResourceTreeFactory treeFactory, ChangedItemDrawer changedItemDrawer) + public OnScreenTab(ResourceTreeViewerFactory resourceTreeViewerFactory) { - _config = config; - _viewer = new ResourceTreeViewer(_config, treeFactory, changedItemDrawer, 0, delegate { }, delegate { }); + _viewer = resourceTreeViewerFactory.Create(0, delegate { }, delegate { }); } public ReadOnlySpan Label From c7046ec0069bef1ee9d98602589740ed0d3a74bb Mon Sep 17 00:00:00 2001 From: Exter-N Date: Fri, 31 May 2024 01:05:05 +0200 Subject: [PATCH 186/433] ResourceTree: Add name/path filter --- Penumbra/Interop/ResourceTree/ResourceNode.cs | 2 - .../ResourceTree/ResourceTreeFactory.cs | 3 - .../UI/AdvancedWindow/ResourceTreeViewer.cs | 74 +++++++++++++++---- 3 files changed, 60 insertions(+), 19 deletions(-) diff --git a/Penumbra/Interop/ResourceTree/ResourceNode.cs b/Penumbra/Interop/ResourceTree/ResourceNode.cs index e74edb91..9c911791 100644 --- a/Penumbra/Interop/ResourceTree/ResourceNode.cs +++ b/Penumbra/Interop/ResourceTree/ResourceNode.cs @@ -10,7 +10,6 @@ public class ResourceNode : ICloneable public string? Name; public string? FallbackName; public ChangedItemIcon Icon; - public ChangedItemIcon DescendentIcons; public readonly ResourceType Type; public readonly nint ObjectAddress; public readonly nint ResourceHandle; @@ -53,7 +52,6 @@ public class ResourceNode : ICloneable Name = other.Name; FallbackName = other.FallbackName; Icon = other.Icon; - DescendentIcons = other.DescendentIcons; Type = other.Type; ObjectAddress = other.ObjectAddress; ResourceHandle = other.ResourceHandle; diff --git a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs index a722e344..5a190e52 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs @@ -116,9 +116,6 @@ public class ResourceTreeFactory( { if (node.Name == parent?.Name) node.Name = null; - - if (parent != null) - parent.DescendentIcons |= node.Icon | node.DescendentIcons; }); } diff --git a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs index c0c49e47..5f376b26 100644 --- a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs +++ b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs @@ -24,9 +24,12 @@ public class ResourceTreeViewer private readonly Action _drawActions; private readonly HashSet _unfolded; + private readonly Dictionary _filterCache; + private TreeCategory _categoryFilter; private ChangedItemDrawer.ChangedItemIcon _typeFilter; private string _nameFilter; + private string _nodeFilter; private Task? _task; @@ -40,11 +43,14 @@ public class ResourceTreeViewer _actionCapacity = actionCapacity; _onRefresh = onRefresh; _drawActions = drawActions; - _unfolded = new HashSet(); + _unfolded = []; + + _filterCache = []; _categoryFilter = AllCategories; _typeFilter = ChangedItemDrawer.AllFlags; _nameFilter = string.Empty; + _nodeFilter = string.Empty; } public void Draw() @@ -107,7 +113,7 @@ public class ResourceTreeViewer (_actionCapacity - 1) * 3 * ImGuiHelpers.GlobalScale + _actionCapacity * ImGui.GetFrameHeight()); ImGui.TableHeadersRow(); - DrawNodes(tree.Nodes, 0, unchecked(tree.DrawObjectAddress * 31)); + DrawNodes(tree.Nodes, 0, unchecked(tree.DrawObjectAddress * 31), 0); } } } @@ -140,14 +146,22 @@ public class ResourceTreeViewer ImGui.SameLine(0, checkPadding); + var filterChanged = false; ImGui.SetCursorPosY(ImGui.GetCursorPosY() - yOffset); using (ImRaii.Child("##typeFilter", new Vector2(ImGui.GetContentRegionAvail().X, ChangedItemDrawer.TypeFilterIconSize.Y))) - _changedItemDrawer.DrawTypeFilter(ref _typeFilter); + filterChanged |= _changedItemDrawer.DrawTypeFilter(ref _typeFilter); - ImGui.SetNextItemWidth(ImGui.GetContentRegionAvail().X - checkSpacing - ImGui.GetFrameHeightWithSpacing()); - ImGui.InputTextWithHint("##TreeNameFilter", "Filter by Character/Entity Name...", ref _nameFilter, 128); + var fieldWidth = (ImGui.GetContentRegionAvail().X - checkSpacing * 2.0f - ImGui.GetFrameHeightWithSpacing()) / 2.0f; + ImGui.SetNextItemWidth(fieldWidth); + filterChanged |= ImGui.InputTextWithHint("##TreeNameFilter", "Filter by Character/Entity Name...", ref _nameFilter, 128); + ImGui.SameLine(0, checkSpacing); + ImGui.SetNextItemWidth(fieldWidth); + filterChanged |= ImGui.InputTextWithHint("##NodeFilter", "Filter by Item/Part Name or Path...", ref _nodeFilter, 128); ImGui.SameLine(0, checkSpacing); _incognito.DrawToggle(); + + if (filterChanged) + _filterCache.Clear(); } private Task RefreshCharacterList() @@ -161,36 +175,68 @@ public class ResourceTreeViewer } finally { + _filterCache.Clear(); _unfolded.Clear(); _onRefresh(); } }); - private void DrawNodes(IEnumerable resourceNodes, int level, nint pathHash) + private void DrawNodes(IEnumerable resourceNodes, int level, nint pathHash, ChangedItemDrawer.ChangedItemIcon parentFilterIcon) { var debugMode = _config.DebugMode; var frameHeight = ImGui.GetFrameHeight(); var cellHeight = _actionCapacity > 0 ? frameHeight : 0.0f; - NodeVisibility GetNodeVisibility(ResourceNode node) + bool MatchesFilter(ResourceNode node, ChangedItemDrawer.ChangedItemIcon filterIcon) + { + if (!_typeFilter.HasFlag(filterIcon)) + return false; + + if (_nodeFilter.Length == 0) + return true; + + return node.Name != null && node.Name.Contains(_nodeFilter, StringComparison.OrdinalIgnoreCase) + || node.FullPath.FullName.Contains(_nodeFilter, StringComparison.OrdinalIgnoreCase) + || node.FullPath.InternalName.ToString().Contains(_nodeFilter, StringComparison.OrdinalIgnoreCase) + || Array.Exists(node.PossibleGamePaths, path => path.Path.ToString().Contains(_nodeFilter, StringComparison.OrdinalIgnoreCase)); + } + + NodeVisibility CalculateNodeVisibility(nint nodePathHash, ResourceNode node, ChangedItemDrawer.ChangedItemIcon parentFilterIcon) { if (node.Internal && !debugMode) return NodeVisibility.Hidden; - if (_typeFilter.HasFlag(node.Icon)) + var filterIcon = node.Icon != 0 ? node.Icon : parentFilterIcon; + if (MatchesFilter(node, filterIcon)) return NodeVisibility.Visible; - if ((_typeFilter & node.DescendentIcons) != 0) - return NodeVisibility.DescendentsOnly; + + foreach (var child in node.Children) + { + if (GetNodeVisibility(unchecked(nodePathHash * 31 + child.ResourceHandle), child, filterIcon) != NodeVisibility.Hidden) + return NodeVisibility.DescendentsOnly; + } return NodeVisibility.Hidden; } + NodeVisibility GetNodeVisibility(nint nodePathHash, ResourceNode node, ChangedItemDrawer.ChangedItemIcon parentFilterIcon) + { + if (!_filterCache.TryGetValue(nodePathHash, out var visibility)) + { + visibility = CalculateNodeVisibility(nodePathHash, node, parentFilterIcon); + _filterCache.Add(nodePathHash, visibility); + } + return visibility; + } + string GetAdditionalDataSuffix(ByteString data) => !debugMode || data.IsEmpty ? string.Empty : $"\n\nAdditional Data: {data}"; foreach (var (resourceNode, index) in resourceNodes.WithIndex()) { - var visibility = GetNodeVisibility(resourceNode); + var nodePathHash = unchecked(pathHash + resourceNode.ResourceHandle); + + var visibility = GetNodeVisibility(nodePathHash, resourceNode, parentFilterIcon); if (visibility == NodeVisibility.Hidden) continue; @@ -199,14 +245,14 @@ public class ResourceTreeViewer using var mutedColor = ImRaii.PushColor(ImGuiCol.Text, textColorInternal, resourceNode.Internal); - var nodePathHash = unchecked(pathHash + resourceNode.ResourceHandle); + var filterIcon = resourceNode.Icon != 0 ? resourceNode.Icon : parentFilterIcon; using var id = ImRaii.PushId(index); ImGui.TableNextColumn(); var unfolded = _unfolded.Contains(nodePathHash); using (var indent = ImRaii.PushIndent(level)) { - var hasVisibleChildren = resourceNode.Children.Any(child => GetNodeVisibility(child) != NodeVisibility.Hidden); + var hasVisibleChildren = resourceNode.Children.Any(child => GetNodeVisibility(unchecked(nodePathHash * 31 + child.ResourceHandle), child, filterIcon) != NodeVisibility.Hidden); var unfoldable = hasVisibleChildren && visibility != NodeVisibility.DescendentsOnly; if (unfoldable) { @@ -291,7 +337,7 @@ public class ResourceTreeViewer } if (unfolded) - DrawNodes(resourceNode.Children, level + 1, unchecked(nodePathHash * 31)); + DrawNodes(resourceNode.Children, level + 1, unchecked(nodePathHash * 31), filterIcon); } } From 81fdbf6ccff729daedc003eba80b3969a4f799f2 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 31 May 2024 16:59:51 +0200 Subject: [PATCH 187/433] Small cleanup. --- Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs index e8a27a74..b8faadf7 100644 --- a/Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs @@ -321,8 +321,8 @@ public sealed class ModGroupEditDrawer( && (!_draggingAcross || (_dragDropGroup != null && group is MultiModGroup { Options.Count: >= IModGroup.MaxMultiOptions }))) return; - using var target = ImRaii.DragDropTarget(); - if (!target.Success || !DragDropTarget.CheckPayload(_draggingAcross ? AcrossGroupsLabel : InsideGroupLabel)) + using var target = ImUtf8.DragDropTarget(); + if (!target.IsDropping(_draggingAcross ? AcrossGroupsLabel : InsideGroupLabel)) return; if (_dragDropGroup != null && _dragDropOption != null) From 67bb95f6e64232480ced89d33318edc976206e63 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 31 May 2024 17:00:40 +0200 Subject: [PATCH 188/433] Update submodules. --- OtterGui | 2 +- Penumbra.GameData | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/OtterGui b/OtterGui index 1d936516..0b5afffd 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 1d9365164655a7cb38172e1311e15e19b1def6db +Subproject commit 0b5afffda19d3e16aec9e8682d18c8f11f67f1c6 diff --git a/Penumbra.GameData b/Penumbra.GameData index f2cea65b..29b71cf7 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit f2cea65b83b2d6cb0d03339e8f76aed8102a41d5 +Subproject commit 29b71cf7b3cc68995d38f0954fa38c4b9500a81d From f61bd8bb8a5cd2bb45156803d1eddd8b8d74b8f9 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 31 May 2024 19:37:24 +0200 Subject: [PATCH 189/433] Update Changelog and improve metamanipulation display in advanced editing. --- Penumbra/Mods/Editor/ModMetaEditor.cs | 58 +++++++------------ .../UI/AdvancedWindow/ModEditWindow.Meta.cs | 30 ++++++---- Penumbra/UI/Changelog.cs | 47 ++++++++------- 3 files changed, 64 insertions(+), 71 deletions(-) diff --git a/Penumbra/Mods/Editor/ModMetaEditor.cs b/Penumbra/Mods/Editor/ModMetaEditor.cs index 829161f5..86d5e02e 100644 --- a/Penumbra/Mods/Editor/ModMetaEditor.cs +++ b/Penumbra/Mods/Editor/ModMetaEditor.cs @@ -1,3 +1,4 @@ +using System.Collections.Frozen; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Manager; using Penumbra.Mods.SubMods; @@ -14,13 +15,19 @@ public class ModMetaEditor(ModManager modManager) private readonly HashSet _rsp = []; private readonly HashSet _globalEqp = []; - public int OtherImcCount { get; private set; } - public int OtherEqpCount { get; private set; } - public int OtherEqdpCount { get; private set; } - public int OtherGmpCount { get; private set; } - public int OtherEstCount { get; private set; } - public int OtherRspCount { get; private set; } - public int OtherGlobalEqpCount { get; private set; } + public sealed class OtherOptionData : List + { + public int TotalCount; + + public new void Clear() + { + TotalCount = 0; + base.Clear(); + } + } + + public readonly FrozenDictionary OtherData = + Enum.GetValues().ToFrozenDictionary(t => t, _ => new OtherOptionData()); public bool Changes { get; private set; } @@ -114,13 +121,9 @@ public class ModMetaEditor(ModManager modManager) public void Load(Mod mod, IModDataContainer currentOption) { - OtherImcCount = 0; - OtherEqpCount = 0; - OtherEqdpCount = 0; - OtherGmpCount = 0; - OtherEstCount = 0; - OtherRspCount = 0; - OtherGlobalEqpCount = 0; + foreach (var type in Enum.GetValues()) + OtherData[type].Clear(); + foreach (var option in mod.AllDataContainers) { if (option == currentOption) @@ -128,30 +131,9 @@ public class ModMetaEditor(ModManager modManager) foreach (var manip in option.Manipulations) { - switch (manip.ManipulationType) - { - case MetaManipulation.Type.Imc: - ++OtherImcCount; - break; - case MetaManipulation.Type.Eqdp: - ++OtherEqdpCount; - break; - case MetaManipulation.Type.Eqp: - ++OtherEqpCount; - break; - case MetaManipulation.Type.Est: - ++OtherEstCount; - break; - case MetaManipulation.Type.Gmp: - ++OtherGmpCount; - break; - case MetaManipulation.Type.Rsp: - ++OtherRspCount; - break; - case MetaManipulation.Type.GlobalEqp: - ++OtherGlobalEqpCount; - break; - } + var data = OtherData[manip.ManipulationType]; + ++data.TotalCount; + data.Add(option.GetFullName()); } } diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs index 6f542377..a2a6925a 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs @@ -66,37 +66,45 @@ public partial class ModEditWindow return; DrawEditHeader(_editor.MetaEditor.Eqp, "Equipment Parameter Edits (EQP)###EQP", 5, EqpRow.Draw, EqpRow.DrawNew, - _editor.MetaEditor.OtherEqpCount); + _editor.MetaEditor.OtherData[MetaManipulation.Type.Eqp]); DrawEditHeader(_editor.MetaEditor.Eqdp, "Racial Model Edits (EQDP)###EQDP", 7, EqdpRow.Draw, EqdpRow.DrawNew, - _editor.MetaEditor.OtherEqdpCount); - DrawEditHeader(_editor.MetaEditor.Imc, "Variant Edits (IMC)###IMC", 10, ImcRow.Draw, ImcRow.DrawNew, _editor.MetaEditor.OtherImcCount); + _editor.MetaEditor.OtherData[MetaManipulation.Type.Eqdp]); + DrawEditHeader(_editor.MetaEditor.Imc, "Variant Edits (IMC)###IMC", 10, ImcRow.Draw, ImcRow.DrawNew, + _editor.MetaEditor.OtherData[MetaManipulation.Type.Imc]); DrawEditHeader(_editor.MetaEditor.Est, "Extra Skeleton Parameters (EST)###EST", 7, EstRow.Draw, EstRow.DrawNew, - _editor.MetaEditor.OtherEstCount); + _editor.MetaEditor.OtherData[MetaManipulation.Type.Est]); DrawEditHeader(_editor.MetaEditor.Gmp, "Visor/Gimmick Edits (GMP)###GMP", 7, GmpRow.Draw, GmpRow.DrawNew, - _editor.MetaEditor.OtherGmpCount); + _editor.MetaEditor.OtherData[MetaManipulation.Type.Gmp]); DrawEditHeader(_editor.MetaEditor.Rsp, "Racial Scaling Edits (RSP)###RSP", 5, RspRow.Draw, RspRow.DrawNew, - _editor.MetaEditor.OtherRspCount); + _editor.MetaEditor.OtherData[MetaManipulation.Type.Rsp]); DrawEditHeader(_editor.MetaEditor.GlobalEqp, "Global Equipment Parameter Edits (Global EQP)###GEQP", 4, GlobalEqpRow.Draw, - GlobalEqpRow.DrawNew, _editor.MetaEditor.OtherGlobalEqpCount); + GlobalEqpRow.DrawNew, _editor.MetaEditor.OtherData[MetaManipulation.Type.GlobalEqp]); } /// The headers for the different meta changes all have basically the same structure for different types. private void DrawEditHeader(IReadOnlyCollection items, string label, int numColumns, - Action draw, - Action drawNew, int otherCount) + Action draw, Action drawNew, + ModMetaEditor.OtherOptionData otherOptionData) { const ImGuiTableFlags flags = ImGuiTableFlags.RowBg | ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.BordersInnerV; var oldPos = ImGui.GetCursorPosY(); var header = ImGui.CollapsingHeader($"{items.Count} {label}"); var newPos = ImGui.GetCursorPos(); - if (otherCount > 0) + if (otherOptionData.TotalCount > 0) { - var text = $"{otherCount} Edits in other Options"; + var text = $"{otherOptionData.TotalCount} Edits in other Options"; var size = ImGui.CalcTextSize(text).X; ImGui.SetCursorPos(new Vector2(ImGui.GetContentRegionAvail().X - size, oldPos + ImGui.GetStyle().FramePadding.Y)); ImGuiUtil.TextColored(ColorId.RedundantAssignment.Value() | 0xFF000000, text); + if (ImGui.IsItemHovered()) + { + using var tt = ImUtf8.Tooltip(); + foreach (var name in otherOptionData) + ImUtf8.Text(name); + } + ImGui.SetCursorPos(newPos); } diff --git a/Penumbra/UI/Changelog.cs b/Penumbra/UI/Changelog.cs index 2b2cfa99..3f5a446a 100644 --- a/Penumbra/UI/Changelog.cs +++ b/Penumbra/UI/Changelog.cs @@ -47,19 +47,26 @@ public class PenumbraChangelog Add8_2_0(Changelog); Add8_3_0(Changelog); Add1_0_0_0(Changelog); - Add1_0_2_0(Changelog); - Add1_0_3_0(Changelog); + AddDummy(Changelog); + AddDummy(Changelog); + Add1_1_0_0(Changelog); } #region Changelogs - private static void Add1_0_3_0(Changelog log) - => log.NextVersion("Version 1.0.3.0") + private static void Add1_1_0_0(Changelog log) + => log.NextVersion("Version 1.1.0.0") .RegisterImportant( "This update comes, again, with a lot of very heavy backend changes (collections and groups) and thus may introduce new issues.") + .RegisterEntry("Updated to .net8 and XIV 6.58, using some new framework facilities to improve performance and stability.") + .RegisterHighlight( + "Added an experimental crash handler that is supposed to write a Penumbra.log file when the game crashes, containing Penumbra-specific information.") + .RegisterEntry("This is disabled by default. It can be enabled in Advanced Settings.", 1) .RegisterHighlight("Collections now have associated GUIDs as identifiers instead of their names, so they can now be renamed.") .RegisterEntry("Migrating those collections may introduce issues, please let me know as soon as possible if you encounter any.", 1) .RegisterEntry("A permanent (non-rolling) backup should be created before the migration in case of any issues.", 1) + .RegisterHighlight( + "Added predefined tags that can be setup in the Settings tab and can be more easily applied or removed from mods. (by DZD)") .RegisterHighlight( "A total rework of how options and groups are handled internally, and introduction of the first new group type, the IMC Group.") .RegisterEntry( @@ -75,9 +82,14 @@ public class PenumbraChangelog .RegisterEntry( "This can be used if something like a jacket or a stole is put onto an accessory to prevent it from being hidden in general.", 1) + .RegisterEntry( + "The first empty option in a single-select option group imported from a TTMP will now keep its location instead of being moved to the first option.") + .RegisterEntry("Further empty options are still removed.", 1) .RegisterHighlight( "Added a field to rename mods directly from the mod selector context menu, instead of moving them in the filesystem.") .RegisterEntry("You can choose which rename field (none, either one or both) to display in the settings.", 1) + .RegisterEntry("Added the characterglass.shpk shader file to special shader treatment to fix issues when replacing it. (By Ny)") + .RegisterEntry("Made it more obvious if a user has not set their root directory yet.") .RegisterEntry( "You can now paste your current clipboard text into the mod selector filter with a simple right-click as long as it is not focused.") .RegisterHighlight( @@ -88,29 +100,17 @@ public class PenumbraChangelog .RegisterEntry("Removed the auto-generated descriptions for newly created groups in Penumbra.") .RegisterEntry( "Made some improvements to the Advanced Editing window, for example a much better and more performant Hex Viewer for unstructured data was added.") - .RegisterEntry("Made a lot of further improvements on Model import/export (by ackwell).") + .RegisterEntry("Various improvements to model import/export by ackwell (throughout all patches).") + .RegisterEntry("Hovering over meta manipulations in other options in the advanced editing window now shows a list of those options.") .RegisterEntry("Reworked the API and IPC structure heavily.") + .RegisterImportant("This means some plugins interacting with Penumbra may not work correctly until they update.", 1) .RegisterEntry("Worked around the UI IPC possibly displacing all settings when the drawn additions became too big.") + .RegisterEntry("Fixed an issue where reloading a mod did not ensure settings for that mod being correct afterwards.") + .RegisterEntry("Fixed some issues with the file sizes of compressed files.") .RegisterEntry("Fixed an issue with merging and deduplicating mods.") .RegisterEntry("Fixed a crash when scanning for mods without access rights to the folder.") .RegisterEntry( - "Made plugin conform to Dalamud requirements by adding a punchline and another button to open the menu from the installer."); - - private static void Add1_0_2_0(Changelog log) - => log.NextVersion("Version 1.0.2.0") - .RegisterEntry("Updated to .net8 and XIV 6.58, using some new framework facilities to improve performance and stability.") - .RegisterHighlight( - "Added an experimental crash handler that is supposed to write a Penumbra.log file when the game crashes, containing Penumbra-specific information.") - .RegisterEntry("Various improvements to model import/export by ackwell (throughout all patches).") - .RegisterHighlight( - "Added predefined tags that can be setup in the Settings tab and can be more easily applied or removed from mods. (by DZD)") - .RegisterEntry( - "The first empty option in a single-select option group imported from a TTMP will now keep its location instead of being moved to the first option.") - .RegisterEntry("Further empty options are still removed.", 1) - .RegisterEntry("Made it more obvious if a user has not set their root directory yet.") - .RegisterEntry("Added the characterglass.shpk shader file to special shader treatment to fix issues when replacing it. (By Ny)") - .RegisterEntry("Fixed some issues with the file sizes of compressed files.") - .RegisterEntry("Fixed an issue where reloading a mod did not ensure settings for that mod being correct afterwards.") + "Made plugin conform to Dalamud requirements by adding a punchline and another button to open the menu from the installer.") .RegisterEntry("Added an option to automatically redraw the player character when saving files. (1.0.0.8)") .RegisterEntry("Fixed issue with manipulating mods not triggering some events. (1.0.0.7)") .RegisterEntry("Fixed issue with temporary mods not triggering some events. (1.0.0.6)") @@ -762,6 +762,9 @@ public class PenumbraChangelog #endregion + private static void AddDummy(Changelog log) + => log.NextVersion(string.Empty); + private (int, ChangeLogDisplayType) ConfigData() => (_config.Ephemeral.LastSeenVersion, _config.ChangeLogDisplayType); From ce11bec985770af2f9bfdaf68f8e265823dfa38a Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 31 May 2024 22:55:22 +0200 Subject: [PATCH 190/433] Use strings for global eqp. --- Penumbra/Meta/Manipulations/GlobalEqpType.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Penumbra/Meta/Manipulations/GlobalEqpType.cs b/Penumbra/Meta/Manipulations/GlobalEqpType.cs index d57af1d9..1a7396f9 100644 --- a/Penumbra/Meta/Manipulations/GlobalEqpType.cs +++ b/Penumbra/Meta/Manipulations/GlobalEqpType.cs @@ -1,5 +1,9 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; + namespace Penumbra.Meta.Manipulations; +[JsonConverter(typeof(StringEnumConverter))] public enum GlobalEqpType { DoNotHideEarrings, From b79600ea1489dab7cb3dc7dc5e2c28708de6ea3f Mon Sep 17 00:00:00 2001 From: Actions User Date: Sat, 1 Jun 2024 09:54:26 +0000 Subject: [PATCH 191/433] [CI] Updating repo.json for 1.1.0.0 --- repo.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/repo.json b/repo.json index a996e2d0..54e6b8b4 100644 --- a/repo.json +++ b/repo.json @@ -5,8 +5,8 @@ "Punchline": "Runtime mod loader and manager.", "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", - "AssemblyVersion": "1.0.1.0", - "TestingAssemblyVersion": "1.0.3.2", + "AssemblyVersion": "1.1.0.0", + "TestingAssemblyVersion": "1.1.0.0", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -17,9 +17,9 @@ "LoadPriority": 69420, "LoadRequiredState": 2, "LoadSync": true, - "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.0.3.2/Penumbra.zip", - "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.0.1.0/Penumbra.zip", + "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.0/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.0/Penumbra.zip", + "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } ] From 24d4e9fac6b15dd700a01cc6ec05b224d508b7f9 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 1 Jun 2024 18:13:46 +0200 Subject: [PATCH 192/433] Fix collections not being added on creation. --- Penumbra/Collections/Manager/CollectionStorage.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Penumbra/Collections/Manager/CollectionStorage.cs b/Penumbra/Collections/Manager/CollectionStorage.cs index 39068e87..68bd08cb 100644 --- a/Penumbra/Collections/Manager/CollectionStorage.cs +++ b/Penumbra/Collections/Manager/CollectionStorage.cs @@ -154,6 +154,7 @@ public class CollectionStorage : IReadOnlyList, IDisposable public bool AddCollection(string name, ModCollection? duplicate) { var newCollection = Create(name, _collections.Count, duplicate); + _collections.Add(newCollection); _saveService.ImmediateSave(new ModCollectionSave(_modStorage, newCollection)); Penumbra.Messager.NotificationMessage($"Created new collection {newCollection.AnonymizedName}.", NotificationType.Success, false); _communicator.CollectionChange.Invoke(CollectionType.Inactive, null, newCollection, string.Empty); From 331b7fbc1de13181aba85099518a14665ec0130e Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 1 Jun 2024 18:14:08 +0200 Subject: [PATCH 193/433] Fix other options displaying the same option multiple times. --- Penumbra/Mods/Editor/ModMetaEditor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Mods/Editor/ModMetaEditor.cs b/Penumbra/Mods/Editor/ModMetaEditor.cs index 86d5e02e..86853755 100644 --- a/Penumbra/Mods/Editor/ModMetaEditor.cs +++ b/Penumbra/Mods/Editor/ModMetaEditor.cs @@ -15,7 +15,7 @@ public class ModMetaEditor(ModManager modManager) private readonly HashSet _rsp = []; private readonly HashSet _globalEqp = []; - public sealed class OtherOptionData : List + public sealed class OtherOptionData : HashSet { public int TotalCount; From aba68cfb925a0dc97dd9fa6cc5be3f0fe8310f7a Mon Sep 17 00:00:00 2001 From: Actions User Date: Sat, 1 Jun 2024 16:18:13 +0000 Subject: [PATCH 194/433] [CI] Updating repo.json for 1.1.0.1 --- repo.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/repo.json b/repo.json index 54e6b8b4..3f5d7262 100644 --- a/repo.json +++ b/repo.json @@ -5,8 +5,8 @@ "Punchline": "Runtime mod loader and manager.", "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", - "AssemblyVersion": "1.1.0.0", - "TestingAssemblyVersion": "1.1.0.0", + "AssemblyVersion": "1.1.0.1", + "TestingAssemblyVersion": "1.1.0.1", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -17,9 +17,9 @@ "LoadPriority": 69420, "LoadRequiredState": 2, "LoadSync": true, - "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.0/Penumbra.zip", - "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.0/Penumbra.zip", + "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.1/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.1/Penumbra.zip", + "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.1/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } ] From 5101b73fdcb9db2e4cd992d70b0d74bf66316616 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 1 Jun 2024 20:28:43 +0200 Subject: [PATCH 195/433] Fix issue with creating unnamed collections. --- Penumbra/Collections/Manager/CollectionStorage.cs | 3 +++ Penumbra/UI/CollectionTab/CollectionSelector.cs | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Penumbra/Collections/Manager/CollectionStorage.cs b/Penumbra/Collections/Manager/CollectionStorage.cs index 68bd08cb..f6287320 100644 --- a/Penumbra/Collections/Manager/CollectionStorage.cs +++ b/Penumbra/Collections/Manager/CollectionStorage.cs @@ -153,6 +153,9 @@ public class CollectionStorage : IReadOnlyList, IDisposable /// public bool AddCollection(string name, ModCollection? duplicate) { + if (name.Length == 0) + return false; + var newCollection = Create(name, _collections.Count, duplicate); _collections.Add(newCollection); _saveService.ImmediateSave(new ModCollectionSave(_modStorage, newCollection)); diff --git a/Penumbra/UI/CollectionTab/CollectionSelector.cs b/Penumbra/UI/CollectionTab/CollectionSelector.cs index fac85d4d..cecb41d7 100644 --- a/Penumbra/UI/CollectionTab/CollectionSelector.cs +++ b/Penumbra/UI/CollectionTab/CollectionSelector.cs @@ -109,7 +109,7 @@ public sealed class CollectionSelector : ItemSelector, IDisposabl } private string Name(ModCollection collection) - => IncognitoMode ? collection.AnonymizedName : collection.Name; + => IncognitoMode || collection.Name.Length == 0 ? collection.AnonymizedName : collection.Name; private void OnCollectionChange(CollectionType type, ModCollection? old, ModCollection? @new, string _3) { From e7cf9d35c9ca7208423c95bc59fbfcddaa415df9 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 1 Jun 2024 23:33:21 +0200 Subject: [PATCH 196/433] Add GetChangedItems for Mods. --- Penumbra.Api | 2 +- Penumbra/Api/Api/ModsApi.cs | 9 +++-- Penumbra/Api/Api/PenumbraApi.cs | 2 +- Penumbra/Api/IpcProviders.cs | 1 + Penumbra/Api/IpcTester/ModsIpcTester.cs | 44 +++++++++++++++++++------ 5 files changed, 44 insertions(+), 14 deletions(-) diff --git a/Penumbra.Api b/Penumbra.Api index 69d106b4..f1e4e520 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit 69d106b457eb0f73d4b4caf1234da5631fd6fbf0 +Subproject commit f1e4e520daaa8f23e5c8b71d55e5992b8f6768e2 diff --git a/Penumbra/Api/Api/ModsApi.cs b/Penumbra/Api/Api/ModsApi.cs index c1e0c684..16dd8be9 100644 --- a/Penumbra/Api/Api/ModsApi.cs +++ b/Penumbra/Api/Api/ModsApi.cs @@ -106,8 +106,8 @@ public class ModsApi : IPenumbraApiMods, IApiService, IDisposable var fullPath = leaf.FullName(); var isDefault = ModFileSystem.ModHasDefaultPath(mod, fullPath); - var isNameDefault = isDefault || ModFileSystem.ModHasDefaultPath(mod, leaf.Name); - return (PenumbraApiEc.Success, fullPath, !isDefault, !isNameDefault ); + var isNameDefault = isDefault || ModFileSystem.ModHasDefaultPath(mod, leaf.Name); + return (PenumbraApiEc.Success, fullPath, !isDefault, !isNameDefault); } public PenumbraApiEc SetModPath(string modDirectory, string modName, string newPath) @@ -129,4 +129,9 @@ public class ModsApi : IPenumbraApiMods, IApiService, IDisposable return PenumbraApiEc.PathRenameFailed; } } + + public Dictionary GetChangedItems(string modDirectory, string modName) + => _modManager.TryGetMod(modDirectory, modName, out var mod) + ? mod.ChangedItems.ToDictionary(kvp => kvp.Key, kvp => kvp.Value) + : []; } diff --git a/Penumbra/Api/Api/PenumbraApi.cs b/Penumbra/Api/Api/PenumbraApi.cs index 1d5b1537..0400c694 100644 --- a/Penumbra/Api/Api/PenumbraApi.cs +++ b/Penumbra/Api/Api/PenumbraApi.cs @@ -22,7 +22,7 @@ public class PenumbraApi( } public (int Breaking, int Feature) ApiVersion - => (5, 0); + => (5, 1); public bool Valid { get; private set; } = true; public IPenumbraApiCollection Collection { get; } = collection; diff --git a/Penumbra/Api/IpcProviders.cs b/Penumbra/Api/IpcProviders.cs index ebf71176..6b146c39 100644 --- a/Penumbra/Api/IpcProviders.cs +++ b/Penumbra/Api/IpcProviders.cs @@ -49,6 +49,7 @@ public sealed class IpcProviders : IDisposable, IApiService IpcSubscribers.ModMoved.Provider(pi, api.Mods), IpcSubscribers.GetModPath.Provider(pi, api.Mods), IpcSubscribers.SetModPath.Provider(pi, api.Mods), + IpcSubscribers.GetChangedItems.Provider(pi, api.Mods), IpcSubscribers.GetAvailableModSettings.Provider(pi, api.ModSettings), IpcSubscribers.GetCurrentModSettings.Provider(pi, api.ModSettings), diff --git a/Penumbra/Api/IpcTester/ModsIpcTester.cs b/Penumbra/Api/IpcTester/ModsIpcTester.cs index 43f397e5..2be51a80 100644 --- a/Penumbra/Api/IpcTester/ModsIpcTester.cs +++ b/Penumbra/Api/IpcTester/ModsIpcTester.cs @@ -3,6 +3,7 @@ using Dalamud.Plugin; using ImGuiNET; using OtterGui.Raii; using OtterGui.Services; +using OtterGui.Text; using Penumbra.Api.Enums; using Penumbra.Api.Helpers; using Penumbra.Api.IpcSubscribers; @@ -13,16 +14,17 @@ public class ModsIpcTester : IUiService, IDisposable { private readonly DalamudPluginInterface _pi; - private string _modDirectory = string.Empty; - private string _modName = string.Empty; - private string _pathInput = string.Empty; - private string _newInstallPath = string.Empty; - private PenumbraApiEc _lastReloadEc; - private PenumbraApiEc _lastAddEc; - private PenumbraApiEc _lastDeleteEc; - private PenumbraApiEc _lastSetPathEc; - private PenumbraApiEc _lastInstallEc; - private Dictionary _mods = []; + private string _modDirectory = string.Empty; + private string _modName = string.Empty; + private string _pathInput = string.Empty; + private string _newInstallPath = string.Empty; + private PenumbraApiEc _lastReloadEc; + private PenumbraApiEc _lastAddEc; + private PenumbraApiEc _lastDeleteEc; + private PenumbraApiEc _lastSetPathEc; + private PenumbraApiEc _lastInstallEc; + private Dictionary _mods = []; + private Dictionary _changedItems = []; public readonly EventSubscriber DeleteSubscriber; public readonly EventSubscriber AddSubscriber; @@ -120,6 +122,14 @@ public class ModsIpcTester : IUiService, IDisposable ImGui.SameLine(); ImGui.TextUnformatted(_lastDeleteEc.ToString()); + IpcTester.DrawIntro(GetChangedItems.Label, "Get Changed Items"); + DrawChangedItemsPopup(); + if (ImUtf8.Button("Get##ChangedItems"u8)) + { + _changedItems = new GetChangedItems(_pi).Invoke(_modDirectory, _modName); + ImUtf8.OpenPopup("ChangedItems"u8); + } + IpcTester.DrawIntro(GetModPath.Label, "Current Path"); var (ec, path, def, nameDef) = new GetModPath(_pi).Invoke(_modDirectory, _modName); ImGui.TextUnformatted($"{path} ({(def ? "Custom" : "Default")} Path, {(nameDef ? "Custom" : "Default")} Name) [{ec}]"); @@ -157,4 +167,18 @@ public class ModsIpcTester : IUiService, IDisposable if (ImGui.Button("Close", -Vector2.UnitX) || !ImGui.IsWindowFocused()) ImGui.CloseCurrentPopup(); } + + private void DrawChangedItemsPopup() + { + ImGui.SetNextWindowSize(ImGuiHelpers.ScaledVector2(500, 500)); + using var p = ImUtf8.Popup("ChangedItems"u8); + if (!p) + return; + + foreach (var (name, data) in _changedItems) + ImUtf8.Text($"{name}: {data}"); + + if (ImUtf8.Button("Close"u8, -Vector2.UnitX) || !ImGui.IsWindowFocused()) + ImGui.CloseCurrentPopup(); + } } From cfa58ee19672a335592e5d06bad26a71aa58aee6 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 1 Jun 2024 23:42:04 +0200 Subject: [PATCH 197/433] Fix global EQP rings checking bracelets instead. --- Penumbra/Meta/Manipulations/GlobalEqpCache.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Penumbra/Meta/Manipulations/GlobalEqpCache.cs b/Penumbra/Meta/Manipulations/GlobalEqpCache.cs index 48ffb308..26eb1d05 100644 --- a/Penumbra/Meta/Manipulations/GlobalEqpCache.cs +++ b/Penumbra/Meta/Manipulations/GlobalEqpCache.cs @@ -44,10 +44,10 @@ public struct GlobalEqpCache : IService if (_doNotHideBracelets.Contains(armor[7].Set)) original |= EqpEntry.BodyShowBracelet | EqpEntry.HandShowBracelet; - if (_doNotHideBracelets.Contains(armor[8].Set)) + if (_doNotHideRingR.Contains(armor[8].Set)) original |= EqpEntry.HandShowRingR; - if (_doNotHideBracelets.Contains(armor[9].Set)) + if (_doNotHideRingL.Contains(armor[9].Set)) original |= EqpEntry.HandShowRingL; return original; } From ef9d81c061c659c0b43643991b15e8be44b068af Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 1 Jun 2024 23:42:17 +0200 Subject: [PATCH 198/433] Fix mod merger. --- Penumbra/Mods/Editor/ModMerger.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Mods/Editor/ModMerger.cs b/Penumbra/Mods/Editor/ModMerger.cs index d6e21076..32a207ff 100644 --- a/Penumbra/Mods/Editor/ModMerger.cs +++ b/Penumbra/Mods/Editor/ModMerger.cs @@ -37,7 +37,7 @@ public class ModMerger : IDisposable public readonly HashSet SelectedOptions = []; - public readonly IReadOnlyList Warnings = []; + public readonly IReadOnlyList Warnings = new List(); public Exception? Error { get; private set; } public ModMerger(ModManager mods, ModGroupEditor editor, ModFileSystemSelector selector, DuplicateManager duplicates, From 2e6473dc096363901c5665f79eb8afb750c2eb7c Mon Sep 17 00:00:00 2001 From: Actions User Date: Sat, 1 Jun 2024 21:44:53 +0000 Subject: [PATCH 199/433] [CI] Updating repo.json for 1.1.0.2 --- repo.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/repo.json b/repo.json index 3f5d7262..5e9e5b37 100644 --- a/repo.json +++ b/repo.json @@ -5,8 +5,8 @@ "Punchline": "Runtime mod loader and manager.", "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", - "AssemblyVersion": "1.1.0.1", - "TestingAssemblyVersion": "1.1.0.1", + "AssemblyVersion": "1.1.0.2", + "TestingAssemblyVersion": "1.1.0.2", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -17,9 +17,9 @@ "LoadPriority": 69420, "LoadRequiredState": 2, "LoadSync": true, - "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.1/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.1/Penumbra.zip", - "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.1/Penumbra.zip", + "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.2/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.2/Penumbra.zip", + "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } ] From 3deda68eeca875ebd6403359f52877ec7d53b55a Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 2 Jun 2024 01:03:51 +0200 Subject: [PATCH 200/433] Small updates. --- OtterGui | 2 +- Penumbra.GameData | 2 +- Penumbra/Services/StaticServiceManager.cs | 2 -- .../UI/AdvancedWindow/ResourceTreeViewer.cs | 2 +- .../ResourceTreeViewerFactory.cs | 3 +- Penumbra/UI/IncognitoService.cs | 29 +++++++++---------- .../ModsTab/Groups/ImcModGroupEditDrawer.cs | 2 +- .../ModsTab/Groups/MultiModGroupEditDrawer.cs | 2 +- .../Groups/SingleModGroupEditDrawer.cs | 2 +- 9 files changed, 21 insertions(+), 25 deletions(-) diff --git a/OtterGui b/OtterGui index 0b5afffd..becacbca 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 0b5afffda19d3e16aec9e8682d18c8f11f67f1c6 +Subproject commit becacbca4f35595d16ff40dc9639cfa24be3461f diff --git a/Penumbra.GameData b/Penumbra.GameData index 29b71cf7..fed687b5 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 29b71cf7b3cc68995d38f0954fa38c4b9500a81d +Subproject commit fed687b536b7c709484db251b690b8821c5ef403 diff --git a/Penumbra/Services/StaticServiceManager.cs b/Penumbra/Services/StaticServiceManager.cs index 146d7ee0..0c6648ba 100644 --- a/Penumbra/Services/StaticServiceManager.cs +++ b/Penumbra/Services/StaticServiceManager.cs @@ -149,7 +149,6 @@ public static class StaticServiceManager private static ServiceManager AddInterface(this ServiceManager services) => services.AddSingleton() .AddSingleton() - .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() @@ -182,7 +181,6 @@ public static class StaticServiceManager .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton() .AddSingleton(p => new Diagnostics(p)); private static ServiceManager AddModEditor(this ServiceManager services) diff --git a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs index 5f376b26..7315f136 100644 --- a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs +++ b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs @@ -158,7 +158,7 @@ public class ResourceTreeViewer ImGui.SetNextItemWidth(fieldWidth); filterChanged |= ImGui.InputTextWithHint("##NodeFilter", "Filter by Item/Part Name or Path...", ref _nodeFilter, 128); ImGui.SameLine(0, checkSpacing); - _incognito.DrawToggle(); + _incognito.DrawToggle(ImGui.GetFrameHeightWithSpacing()); if (filterChanged) _filterCache.Clear(); diff --git a/Penumbra/UI/AdvancedWindow/ResourceTreeViewerFactory.cs b/Penumbra/UI/AdvancedWindow/ResourceTreeViewerFactory.cs index 91dab6cb..ea64c0bf 100644 --- a/Penumbra/UI/AdvancedWindow/ResourceTreeViewerFactory.cs +++ b/Penumbra/UI/AdvancedWindow/ResourceTreeViewerFactory.cs @@ -1,3 +1,4 @@ +using OtterGui.Services; using Penumbra.Interop.ResourceTree; namespace Penumbra.UI.AdvancedWindow; @@ -6,7 +7,7 @@ public class ResourceTreeViewerFactory( Configuration config, ResourceTreeFactory treeFactory, ChangedItemDrawer changedItemDrawer, - IncognitoService incognito) + IncognitoService incognito) : IService { public ResourceTreeViewer Create(int actionCapacity, Action onRefresh, Action drawActions) => new(config, treeFactory, changedItemDrawer, incognito, actionCapacity, onRefresh, drawActions); diff --git a/Penumbra/UI/IncognitoService.cs b/Penumbra/UI/IncognitoService.cs index d4b1828f..d58ea1ec 100644 --- a/Penumbra/UI/IncognitoService.cs +++ b/Penumbra/UI/IncognitoService.cs @@ -1,29 +1,26 @@ -using Dalamud.Interface.Utility; using Dalamud.Interface; -using ImGuiNET; -using OtterGui; using Penumbra.UI.Classes; using OtterGui.Raii; +using OtterGui.Services; +using OtterGui.Text; namespace Penumbra.UI; -public class IncognitoService(TutorialService tutorial) +public class IncognitoService(TutorialService tutorial) : IService { public bool IncognitoMode; - public void DrawToggle(float? buttonWidth = null) + public void DrawToggle(float width) { - using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale); - using var color = ImRaii.PushColor(ImGuiCol.Text, ColorId.FolderExpanded.Value()) - .Push(ImGuiCol.Border, ColorId.FolderExpanded.Value()); - if (ImGuiUtil.DrawDisabledButton( - $"{(IncognitoMode ? FontAwesomeIcon.Eye : FontAwesomeIcon.EyeSlash).ToIconString()}###IncognitoMode", - new Vector2(buttonWidth ?? ImGui.GetFrameHeightWithSpacing(), ImGui.GetFrameHeight()), string.Empty, false, true)) - IncognitoMode = !IncognitoMode; - var hovered = ImGui.IsItemHovered(); + var color = ColorId.FolderExpanded.Value(); + using (ImRaii.PushFrameBorder(ImUtf8.GlobalScale, color)) + { + var tt = IncognitoMode ? "Toggle incognito mode off."u8 : "Toggle incognito mode on."u8; + var icon = IncognitoMode ? FontAwesomeIcon.EyeSlash : FontAwesomeIcon.Eye; + if (ImUtf8.IconButton(icon, tt, new Vector2(width, ImUtf8.FrameHeight), false, color)) + IncognitoMode = !IncognitoMode; + } + tutorial.OpenTutorial(BasicTutorialSteps.Incognito); - color.Pop(2); - if (hovered) - ImGui.SetTooltip(IncognitoMode ? "Toggle incognito mode off." : "Toggle incognito mode on."); } } diff --git a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs index b10f123c..b129d275 100644 --- a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs @@ -118,7 +118,7 @@ public readonly struct ImcModGroupEditDrawer(ModGroupEditDrawer editor, ImcModGr : validName ? "Add a new option to this group."u8 : "Please enter a name for the new option."u8; - if (ImUtf8.IconButton(FontAwesomeIcon.Plus, tt, !validName || dis)) + if (ImUtf8.IconButton(FontAwesomeIcon.Plus, tt, default, !validName || dis)) { editor.ModManager.OptionEditor.ImcEditor.AddOption(group, cache, name); editor.NewOptionName = null; diff --git a/Penumbra/UI/ModsTab/Groups/MultiModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/MultiModGroupEditDrawer.cs index e6701a03..f0275853 100644 --- a/Penumbra/UI/ModsTab/Groups/MultiModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/MultiModGroupEditDrawer.cs @@ -54,7 +54,7 @@ public readonly struct MultiModGroupEditDrawer(ModGroupEditDrawer editor, MultiM var validName = name.Length > 0; if (ImUtf8.IconButton(FontAwesomeIcon.Plus, validName ? "Add a new option to this group."u8 - : "Please enter a name for the new option."u8, !validName)) + : "Please enter a name for the new option."u8, default, !validName)) { editor.ModManager.OptionEditor.MultiEditor.AddOption(group, name); editor.NewOptionName = null; diff --git a/Penumbra/UI/ModsTab/Groups/SingleModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/SingleModGroupEditDrawer.cs index 75fbc63a..be2dbd73 100644 --- a/Penumbra/UI/ModsTab/Groups/SingleModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/SingleModGroupEditDrawer.cs @@ -59,7 +59,7 @@ public readonly struct SingleModGroupEditDrawer(ModGroupEditDrawer editor, Singl var validName = name.Length > 0; if (ImUtf8.IconButton(FontAwesomeIcon.Plus, validName ? "Add a new option to this group."u8 - : "Please enter a name for the new option."u8, !validName)) + : "Please enter a name for the new option."u8, default, !validName)) { editor.ModManager.OptionEditor.SingleEditor.AddOption(group, name); editor.NewOptionName = null; From 137b752196154960e2687be65728ae9d60bd913b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 2 Jun 2024 01:53:29 +0200 Subject: [PATCH 201/433] Fix Dye Preview not applying. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index fed687b5..ad12ddce 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit fed687b536b7c709484db251b690b8821c5ef403 +Subproject commit ad12ddcef38a9ed4e4dd7424d748f41c4b97db10 From 05d010a281de9e494c389bcb34fdad35ba9fe13d Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 2 Jun 2024 12:08:49 +0200 Subject: [PATCH 202/433] Add some functionality to allow an IMC group to add apply to all variants. --- Penumbra/Collections/Cache/ImcCache.cs | 2 +- .../Import/TexToolsMeta.Deserialization.cs | 2 +- Penumbra/Import/TexToolsMeta.Export.cs | 2 +- Penumbra/Meta/Files/ImcFile.cs | 21 +++++----- Penumbra/Meta/ImcChecker.cs | 40 ++++++++++++++++++- Penumbra/Meta/Manipulations/Imc.cs | 37 +++++++++-------- Penumbra/Mods/Groups/ImcModGroup.cs | 31 ++++++++++---- Penumbra/Mods/ItemSwap/EquipmentSwap.cs | 6 +-- .../Manager/OptionEditor/ImcModGroupEditor.cs | 14 ++++++- .../ModsTab/Groups/ImcModGroupEditDrawer.cs | 8 +++- 10 files changed, 117 insertions(+), 46 deletions(-) diff --git a/Penumbra/Collections/Cache/ImcCache.cs b/Penumbra/Collections/Cache/ImcCache.cs index 33b366d3..7990122a 100644 --- a/Penumbra/Collections/Cache/ImcCache.cs +++ b/Penumbra/Collections/Cache/ImcCache.cs @@ -51,7 +51,7 @@ public readonly struct ImcCache : IDisposable try { if (!_imcFiles.TryGetValue(path, out var file)) - file = new ImcFile(manager, manip); + file = new ImcFile(manager, manip.Identifier); _imcManipulations[idx] = (manip, file); if (!manip.Apply(file)) diff --git a/Penumbra/Import/TexToolsMeta.Deserialization.cs b/Penumbra/Import/TexToolsMeta.Deserialization.cs index f062ae25..554cf848 100644 --- a/Penumbra/Import/TexToolsMeta.Deserialization.cs +++ b/Penumbra/Import/TexToolsMeta.Deserialization.cs @@ -110,7 +110,7 @@ public partial class TexToolsMeta var manip = new ImcManipulation(metaFileInfo.PrimaryType, metaFileInfo.SecondaryType, metaFileInfo.PrimaryId, metaFileInfo.SecondaryId, i, metaFileInfo.EquipSlot, new ImcEntry()); - var def = new ImcFile(_metaFileManager, manip); + var def = new ImcFile(_metaFileManager, manip.Identifier); var partIdx = ImcFile.PartIndex(manip.EquipSlot); // Gets turned to unknown for things without equip, and unknown turns to 0. foreach (var value in values) { diff --git a/Penumbra/Import/TexToolsMeta.Export.cs b/Penumbra/Import/TexToolsMeta.Export.cs index 03bdbd90..09bd2c12 100644 --- a/Penumbra/Import/TexToolsMeta.Export.cs +++ b/Penumbra/Import/TexToolsMeta.Export.cs @@ -133,7 +133,7 @@ public partial class TexToolsMeta { case MetaManipulation.Type.Imc: var allManips = manips.ToList(); - var baseFile = new ImcFile(manager, allManips[0].Imc); + var baseFile = new ImcFile(manager, allManips[0].Imc.Identifier); foreach (var manip in allManips) manip.Imc.Apply(baseFile); diff --git a/Penumbra/Meta/Files/ImcFile.cs b/Penumbra/Meta/Files/ImcFile.cs index 68d3f5b3..5d704cf8 100644 --- a/Penumbra/Meta/Files/ImcFile.cs +++ b/Penumbra/Meta/Files/ImcFile.cs @@ -9,20 +9,20 @@ namespace Penumbra.Meta.Files; public class ImcException : Exception { - public readonly ImcManipulation Manipulation; - public readonly string GamePath; + public readonly ImcIdentifier Identifier; + public readonly string GamePath; - public ImcException(ImcManipulation manip, Utf8GamePath path) + public ImcException(ImcIdentifier identifier, Utf8GamePath path) { - Manipulation = manip; - GamePath = path.ToString(); + Identifier = identifier; + GamePath = path.ToString(); } public override string Message => "Could not obtain default Imc File.\n" + " Either the default file does not exist (possibly for offhand files from TexTools) or the installation is corrupted.\n" + $" Game Path: {GamePath}\n" - + $" Manipulation: {Manipulation}"; + + $" Manipulation: {Identifier}"; } public unsafe class ImcFile : MetaBaseFile @@ -142,13 +142,14 @@ public unsafe class ImcFile : MetaBaseFile } } - public ImcFile(MetaFileManager manager, ImcManipulation manip) + public ImcFile(MetaFileManager manager, ImcIdentifier identifier) : base(manager, 0) { - Path = manip.GamePath(); - var file = manager.GameData.GetFile(Path.ToString()); + var path = identifier.GamePathString(); + Path = Utf8GamePath.FromString(path, out var p) ? p : Utf8GamePath.Empty; + var file = manager.GameData.GetFile(path); if (file == null) - throw new ImcException(manip, Path); + throw new ImcException(identifier, Path); fixed (byte* ptr = file.Data) { diff --git a/Penumbra/Meta/ImcChecker.cs b/Penumbra/Meta/ImcChecker.cs index 14486e21..650919a3 100644 --- a/Penumbra/Meta/ImcChecker.cs +++ b/Penumbra/Meta/ImcChecker.cs @@ -4,11 +4,32 @@ using Penumbra.Meta.Manipulations; namespace Penumbra.Meta; -public class ImcChecker(MetaFileManager metaFileManager) +public class ImcChecker { + private static readonly Dictionary VariantCounts = []; + private static MetaFileManager? _dataManager; + + + public static int GetVariantCount(ImcIdentifier identifier) + { + if (VariantCounts.TryGetValue(identifier, out var count)) + return count; + + count = GetFile(identifier)?.Count ?? 0; + VariantCounts[identifier] = count; + return count; + } + public readonly record struct CachedEntry(ImcEntry Entry, bool FileExists, bool VariantExists); private readonly Dictionary _cachedDefaultEntries = new(); + private readonly MetaFileManager _metaFileManager; + + public ImcChecker(MetaFileManager metaFileManager) + { + _metaFileManager = metaFileManager; + _dataManager = metaFileManager; + } public CachedEntry GetDefaultEntry(ImcIdentifier identifier, bool storeCache) { @@ -17,7 +38,7 @@ public class ImcChecker(MetaFileManager metaFileManager) try { - var e = ImcFile.GetDefault(metaFileManager, identifier.GamePath(), identifier.EquipSlot, identifier.Variant, out var entryExists); + var e = ImcFile.GetDefault(_metaFileManager, identifier.GamePath(), identifier.EquipSlot, identifier.Variant, out var entryExists); entry = new CachedEntry(e, true, entryExists); } catch (Exception) @@ -33,4 +54,19 @@ public class ImcChecker(MetaFileManager metaFileManager) public CachedEntry GetDefaultEntry(ImcManipulation imcManip, bool storeCache) => GetDefaultEntry(new ImcIdentifier(imcManip.PrimaryId, imcManip.Variant, imcManip.ObjectType, imcManip.SecondaryId.Id, imcManip.EquipSlot, imcManip.BodySlot), storeCache); + + private static ImcFile? GetFile(ImcIdentifier identifier) + { + if (_dataManager == null) + return null; + + try + { + return new ImcFile(_dataManager, identifier); + } + catch + { + return null; + } + } } diff --git a/Penumbra/Meta/Manipulations/Imc.cs b/Penumbra/Meta/Manipulations/Imc.cs index fef86520..2a2f4c03 100644 --- a/Penumbra/Meta/Manipulations/Imc.cs +++ b/Penumbra/Meta/Manipulations/Imc.cs @@ -31,12 +31,16 @@ public readonly record struct ImcIdentifier( => new(ObjectType, BodySlot, PrimaryId, SecondaryId.Id, Variant.Id, EquipSlot, entry); public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + => AddChangedItems(identifier, changedItems, false); + + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems, bool allVariants) { var path = ObjectType switch { - ObjectType.Equipment or ObjectType.Accessory => GamePaths.Equipment.Mtrl.Path(PrimaryId, GenderRace.MidlanderMale, EquipSlot, - Variant, - "a"), + ObjectType.Equipment when allVariants => GamePaths.Equipment.Mdl.Path(PrimaryId, GenderRace.MidlanderMale, EquipSlot), + ObjectType.Equipment => GamePaths.Equipment.Mtrl.Path(PrimaryId, GenderRace.MidlanderMale, EquipSlot, Variant, "a"), + ObjectType.Accessory when allVariants => GamePaths.Accessory.Mdl.Path(PrimaryId, GenderRace.MidlanderMale, EquipSlot), + ObjectType.Accessory => GamePaths.Accessory.Mtrl.Path(PrimaryId, GenderRace.MidlanderMale, EquipSlot, Variant, "a"), ObjectType.Weapon => GamePaths.Weapon.Mtrl.Path(PrimaryId, SecondaryId.Id, Variant, "a"), ObjectType.DemiHuman => GamePaths.DemiHuman.Mtrl.Path(PrimaryId, SecondaryId.Id, EquipSlot, Variant, "a"), @@ -49,24 +53,19 @@ public readonly record struct ImcIdentifier( identifier.Identify(changedItems, path); } - public Utf8GamePath GamePath() - { - return ObjectType switch + public string GamePathString() + => ObjectType switch { - ObjectType.Accessory => Utf8GamePath.FromString(GamePaths.Accessory.Imc.Path(PrimaryId), out var p) ? p : Utf8GamePath.Empty, - ObjectType.Equipment => Utf8GamePath.FromString(GamePaths.Equipment.Imc.Path(PrimaryId), out var p) ? p : Utf8GamePath.Empty, - ObjectType.DemiHuman => Utf8GamePath.FromString(GamePaths.DemiHuman.Imc.Path(PrimaryId, SecondaryId.Id), out var p) - ? p - : Utf8GamePath.Empty, - ObjectType.Monster => Utf8GamePath.FromString(GamePaths.Monster.Imc.Path(PrimaryId, SecondaryId.Id), out var p) - ? p - : Utf8GamePath.Empty, - ObjectType.Weapon => Utf8GamePath.FromString(GamePaths.Weapon.Imc.Path(PrimaryId, SecondaryId.Id), out var p) - ? p - : Utf8GamePath.Empty, - _ => throw new NotImplementedException(), + ObjectType.Accessory => GamePaths.Accessory.Imc.Path(PrimaryId), + ObjectType.Equipment => GamePaths.Equipment.Imc.Path(PrimaryId), + ObjectType.DemiHuman => GamePaths.DemiHuman.Imc.Path(PrimaryId, SecondaryId.Id), + ObjectType.Monster => GamePaths.Monster.Imc.Path(PrimaryId, SecondaryId.Id), + ObjectType.Weapon => GamePaths.Weapon.Imc.Path(PrimaryId, SecondaryId.Id), + _ => string.Empty, }; - } + + public Utf8GamePath GamePath() + => Utf8GamePath.FromString(GamePathString(), out var p) ? p : Utf8GamePath.Empty; public MetaIndex FileIndex() => (MetaIndex)(-1); diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs index e0d70aa6..b336203d 100644 --- a/Penumbra/Mods/Groups/ImcModGroup.cs +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -6,6 +6,7 @@ using OtterGui.Classes; using Penumbra.Api.Enums; using Penumbra.GameData.Data; using Penumbra.GameData.Structs; +using Penumbra.Meta; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; @@ -31,6 +32,8 @@ public class ImcModGroup(Mod mod) : IModGroup public ImcIdentifier Identifier; public ImcEntry DefaultEntry; + public bool AllVariants; + public FullPath? FindBestMatch(Utf8GamePath gamePath) => null; @@ -39,7 +42,7 @@ public class ImcModGroup(Mod mod) : IModGroup public bool CanBeDisabled { - get => OptionData.Any(m => m.IsDisableSubMod); + get => _canBeDisabled; set { _canBeDisabled = value; @@ -92,8 +95,8 @@ public class ImcModGroup(Mod mod) : IModGroup public IModGroupEditDrawer EditDrawer(ModGroupEditDrawer editDrawer) => new ImcModGroupEditDrawer(editDrawer, this); - public ImcManipulation GetManip(ushort mask) - => new(Identifier.ObjectType, Identifier.BodySlot, Identifier.PrimaryId, Identifier.SecondaryId.Id, Identifier.Variant.Id, + public ImcManipulation GetManip(ushort mask, Variant variant) + => new(Identifier.ObjectType, Identifier.BodySlot, Identifier.PrimaryId, Identifier.SecondaryId.Id, variant.Id, Identifier.EquipSlot, DefaultEntry with { AttributeMask = mask }); public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) @@ -102,12 +105,23 @@ public class ImcModGroup(Mod mod) : IModGroup return; var mask = GetCurrentMask(setting); - var imc = GetManip(mask); - manipulations.Add(imc); + if (AllVariants) + { + var count = ImcChecker.GetVariantCount(Identifier); + if (count == 0) + manipulations.Add(GetManip(mask, Identifier.Variant)); + else + for (var i = 0; i <= count; ++i) + manipulations.Add(GetManip(mask, (Variant)i)); + } + else + { + manipulations.Add(GetManip(mask, Identifier.Variant)); + } } public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) - => Identifier.AddChangedItems(identifier, changedItems); + => Identifier.AddChangedItems(identifier, changedItems, AllVariants); public Setting FixSetting(Setting setting) => new(setting.Value & ((1ul << OptionData.Count) - 1)); @@ -120,6 +134,8 @@ public class ImcModGroup(Mod mod) : IModGroup jObj.WriteTo(jWriter); jWriter.WritePropertyName(nameof(DefaultEntry)); serializer.Serialize(jWriter, DefaultEntry); + jWriter.WritePropertyName(nameof(AllVariants)); + jWriter.WriteValue(AllVariants); jWriter.WritePropertyName("Options"); jWriter.WriteStartArray(); foreach (var option in OptionData) @@ -156,6 +172,7 @@ public class ImcModGroup(Mod mod) : IModGroup Description = json[nameof(Description)]?.ToObject() ?? string.Empty, Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, DefaultEntry = json[nameof(DefaultEntry)]?.ToObject() ?? new ImcEntry(), + AllVariants = json[nameof(AllVariants)]?.ToObject() ?? false, }; if (ret.Name.Length == 0) return null; @@ -210,7 +227,7 @@ public class ImcModGroup(Mod mod) : IModGroup if (idx >= 0) return setting.HasFlag(idx); - Penumbra.Log.Warning($"A IMC Group should be able to be disabled, but does not contain a disable option."); + Penumbra.Log.Warning("A IMC Group should be able to be disabled, but does not contain a disable option."); return false; } diff --git a/Penumbra/Mods/ItemSwap/EquipmentSwap.cs b/Penumbra/Mods/ItemSwap/EquipmentSwap.cs index 5a5181a5..ea4ef7b1 100644 --- a/Penumbra/Mods/ItemSwap/EquipmentSwap.cs +++ b/Penumbra/Mods/ItemSwap/EquipmentSwap.cs @@ -51,7 +51,7 @@ public static class EquipmentSwap var (imcFileFrom, variants, affectedItems) = GetVariants(manager, identifier, slotFrom, idFrom, idTo, variantFrom); var imcManip = new ImcManipulation(slotTo, variantTo.Id, idTo.Id, default); - var imcFileTo = new ImcFile(manager, imcManip); + var imcFileTo = new ImcFile(manager, imcManip.Identifier); var skipFemale = false; var skipMale = false; var mtrlVariantTo = manips(imcManip.Copy(imcFileTo.GetEntry(ImcFile.PartIndex(slotTo), variantTo.Id))).Imc.Entry.MaterialId; @@ -121,7 +121,7 @@ public static class EquipmentSwap { (var imcFileFrom, var variants, affectedItems) = GetVariants(manager, identifier, slot, idFrom, idTo, variantFrom); var imcManip = new ImcManipulation(slot, variantTo.Id, idTo, default); - var imcFileTo = new ImcFile(manager, imcManip); + var imcFileTo = new ImcFile(manager, imcManip.Identifier); var isAccessory = slot.IsAccessory(); var estType = slot switch @@ -250,7 +250,7 @@ public static class EquipmentSwap PrimaryId idFrom, PrimaryId idTo, Variant variantFrom) { var entry = new ImcManipulation(slotFrom, variantFrom.Id, idFrom, default); - var imc = new ImcFile(manager, entry); + var imc = new ImcFile(manager, entry.Identifier); EquipItem[] items; Variant[] variants; if (idFrom == idTo) diff --git a/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs index f9fd532f..4aae45a2 100644 --- a/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/ImcModGroupEditor.cs @@ -2,6 +2,7 @@ using OtterGui.Classes; using OtterGui.Filesystem; using OtterGui.Services; using Penumbra.GameData.Structs; +using Penumbra.Meta; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Groups; using Penumbra.Mods.Settings; @@ -14,7 +15,8 @@ public sealed class ImcModGroupEditor(CommunicatorService communicator, SaveServ : ModOptionEditor(communicator, saveService, config), IService { /// Add a new, empty imc group with the given manipulation data. - public ImcModGroup? AddModGroup(Mod mod, string newName, ImcIdentifier identifier, ImcEntry defaultEntry, SaveType saveType = SaveType.ImmediateSync) + public ImcModGroup? AddModGroup(Mod mod, string newName, ImcIdentifier identifier, ImcEntry defaultEntry, + SaveType saveType = SaveType.ImmediateSync) { if (!ModGroupEditor.VerifyFileName(mod, null, newName, true)) return null; @@ -78,6 +80,16 @@ public sealed class ImcModGroupEditor(CommunicatorService communicator, SaveServ Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMetaChanged, option.Mod, option.Group, option, null, -1); } + public void ChangeAllVariants(ImcModGroup group, bool allVariants, SaveType saveType = SaveType.Queue) + { + if (group.AllVariants == allVariants) + return; + + group.AllVariants = allVariants; + SaveService.Save(saveType, new ModSaveGroup(group, Config.ReplaceNonAsciiOnImport)); + Communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionMetaChanged, group.Mod, group, null, null, -1); + } + public void ChangeCanBeDisabled(ImcModGroup group, bool canBeDisabled, SaveType saveType = SaveType.Queue) { if (group.CanBeDisabled == canBeDisabled) diff --git a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs index b129d275..d346e05c 100644 --- a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs @@ -19,7 +19,13 @@ public readonly struct ImcModGroupEditDrawer(ModGroupEditDrawer editor, ImcModGr var entry = group.DefaultEntry; var changes = false; - ImUtf8.TextFramed(identifier.ToString(), 0, editor.AvailableWidth, borderColor: ImGui.GetColorU32(ImGuiCol.Border)); + var width = editor.AvailableWidth.X - ImUtf8.ItemInnerSpacing.X - ImUtf8.CalcTextSize("All Variants"u8).X; + ImUtf8.TextFramed(identifier.ToString(), 0, new Vector2(width, 0), borderColor: ImGui.GetColorU32(ImGuiCol.Border)); + ImUtf8.SameLineInner(); + var allVariants = group.AllVariants; + if (ImUtf8.Checkbox("All Variants"u8, ref allVariants)) + editor.ModManager.OptionEditor.ImcEditor.ChangeAllVariants(group, allVariants); + ImUtf8.HoverTooltip("Make this group overwrite all corresponding variants for this identifier, not just the one specified."u8); using (ImUtf8.Group()) { From b63935e81ed45d562a1a898ba5361f6233516798 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 2 Jun 2024 12:09:05 +0200 Subject: [PATCH 203/433] Fix issue with accessory vfx hook. --- Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs index 4e24ba39..8118343d 100644 --- a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs +++ b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs @@ -182,7 +182,7 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable private nint ResolveVfxHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint slotIndex, nint unkOutParam) { - if (slotIndex <= 4) + if (slotIndex is <= 4 or >= 10) return ResolveVfx(drawObject, pathBuffer, pathBufferSize, slotIndex, unkOutParam); var changedEquipData = ((Human*)drawObject)->ChangedEquipData; From 63b3a02e95b00dcbed78f55da4db6ba2ce874d23 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 3 Jun 2024 17:45:22 +0200 Subject: [PATCH 204/433] Fix issue with crash handler and collections not saving on rename. --- OtterGui | 2 +- Penumbra.CrashHandler/CrashData.cs | 4 +- .../Collections/Manager/CollectionStorage.cs | 1 + Penumbra/Communication/ModSettingChanged.cs | 2 +- Penumbra/Services/CrashHandlerService.cs | 2 +- Penumbra/UI/CollectionTab/CollectionPanel.cs | 68 ++++++++----------- .../UI/CollectionTab/CollectionSelector.cs | 22 +++--- Penumbra/UI/Tabs/CollectionsTab.cs | 8 +-- Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs | 2 +- 9 files changed, 52 insertions(+), 59 deletions(-) diff --git a/OtterGui b/OtterGui index becacbca..5de708b2 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit becacbca4f35595d16ff40dc9639cfa24be3461f +Subproject commit 5de708b27ed45c9cdead71742c7061ad9ce64323 diff --git a/Penumbra.CrashHandler/CrashData.cs b/Penumbra.CrashHandler/CrashData.cs index cdac103f..dd75f46e 100644 --- a/Penumbra.CrashHandler/CrashData.cs +++ b/Penumbra.CrashHandler/CrashData.cs @@ -55,7 +55,7 @@ public class CrashData /// The last vfx function invoked before this crash data was generated. public VfxFuncInvokedEntry? LastVfxFuncInvoked - => LastVfxFuncsInvoked.Count == 0 ? default : LastVfxFuncsInvoked[0]; + => LastVFXFuncsInvoked.Count == 0 ? default : LastVFXFuncsInvoked[0]; /// A collection of the last few characters loaded before this crash data was generated. public List LastCharactersLoaded { get; set; } = []; @@ -64,5 +64,5 @@ public class CrashData public List LastModdedFilesLoaded { get; set; } = []; /// A collection of the last few vfx functions invoked before this crash data was generated. - public List LastVfxFuncsInvoked { get; set; } = []; + public List LastVFXFuncsInvoked { get; set; } = []; } diff --git a/Penumbra/Collections/Manager/CollectionStorage.cs b/Penumbra/Collections/Manager/CollectionStorage.cs index f6287320..67de3a03 100644 --- a/Penumbra/Collections/Manager/CollectionStorage.cs +++ b/Penumbra/Collections/Manager/CollectionStorage.cs @@ -181,6 +181,7 @@ public class CollectionStorage : IReadOnlyList, IDisposable return false; } + Delete(collection); _saveService.ImmediateDelete(new ModCollectionSave(_modStorage, collection)); _collections.RemoveAt(collection.Index); // Update indices. diff --git a/Penumbra/Communication/ModSettingChanged.cs b/Penumbra/Communication/ModSettingChanged.cs index a7da345b..7fda2f35 100644 --- a/Penumbra/Communication/ModSettingChanged.cs +++ b/Penumbra/Communication/ModSettingChanged.cs @@ -24,7 +24,7 @@ public sealed class ModSettingChanged() { public enum Priority { - /// + /// Api = int.MinValue, /// diff --git a/Penumbra/Services/CrashHandlerService.cs b/Penumbra/Services/CrashHandlerService.cs index 1239578b..25c6cf57 100644 --- a/Penumbra/Services/CrashHandlerService.cs +++ b/Penumbra/Services/CrashHandlerService.cs @@ -287,7 +287,7 @@ public sealed class CrashHandlerService : IDisposable, IService try { - if (PathDataHandler.Split(manipulatedPath.Value.FullName, out var actualPath, out _) && Path.IsPathRooted(actualPath)) + if (PathDataHandler.Split(manipulatedPath.Value.FullName, out var actualPath, out _) && !Path.IsPathRooted(actualPath)) return; var name = GetActorName(resolveData.AssociatedGameObject); diff --git a/Penumbra/UI/CollectionTab/CollectionPanel.cs b/Penumbra/UI/CollectionTab/CollectionPanel.cs index cb4dbe20..082b78b8 100644 --- a/Penumbra/UI/CollectionTab/CollectionPanel.cs +++ b/Penumbra/UI/CollectionTab/CollectionPanel.cs @@ -20,19 +20,23 @@ using Penumbra.UI.Classes; namespace Penumbra.UI.CollectionTab; -public sealed class CollectionPanel : IDisposable +public sealed class CollectionPanel( + DalamudPluginInterface pi, + CommunicatorService communicator, + CollectionManager manager, + CollectionSelector selector, + ActorManager actors, + ITargetManager targets, + ModStorage mods, + SaveService saveService, + IncognitoService incognito) + : IDisposable { - private readonly CollectionStorage _collections; - private readonly ActiveCollections _active; - private readonly CollectionSelector _selector; - private readonly ActorManager _actors; - private readonly ITargetManager _targets; - private readonly IndividualAssignmentUi _individualAssignmentUi; - private readonly InheritanceUi _inheritanceUi; - private readonly ModStorage _mods; - private readonly FilenameService _fileNames; - private readonly IncognitoService _incognito; - private readonly IFontHandle _nameFont; + private readonly CollectionStorage _collections = manager.Storage; + private readonly ActiveCollections _active = manager.Active; + private readonly IndividualAssignmentUi _individualAssignmentUi = new(communicator, actors, manager); + private readonly InheritanceUi _inheritanceUi = new(manager, incognito); + private readonly IFontHandle _nameFont = pi.UiBuilder.FontAtlas.NewGameFontHandle(new GameFontStyle(GameFontFamilyAndSize.Jupiter23)); private static readonly IReadOnlyDictionary Buttons = CreateButtons(); private static readonly IReadOnlyList<(CollectionType, bool, bool, string, uint)> AdvancedTree = CreateTree(); @@ -41,23 +45,6 @@ public sealed class CollectionPanel : IDisposable private int _draggedIndividualAssignment = -1; - public CollectionPanel(DalamudPluginInterface pi, CommunicatorService communicator, CollectionManager manager, - CollectionSelector selector, ActorManager actors, ITargetManager targets, ModStorage mods, FilenameService fileNames, - IncognitoService incognito) - { - _collections = manager.Storage; - _active = manager.Active; - _selector = selector; - _actors = actors; - _targets = targets; - _mods = mods; - _fileNames = fileNames; - _incognito = incognito; - _individualAssignmentUi = new IndividualAssignmentUi(communicator, actors, manager); - _inheritanceUi = new InheritanceUi(manager, incognito); - _nameFont = pi.UiBuilder.FontAtlas.NewGameFontHandle(new GameFontStyle(GameFontFamilyAndSize.Jupiter23)); - } - public void Dispose() { _individualAssignmentUi.Dispose(); @@ -237,17 +224,22 @@ public sealed class CollectionPanel : IDisposable var name = _newName ?? collection.Name; var identifier = collection.Identifier; var width = ImGui.GetContentRegionAvail().X; - var fileName = _fileNames.CollectionFile(collection); + var fileName = saveService.FileNames.CollectionFile(collection); ImGui.SetNextItemWidth(width); if (ImGui.InputText("##name", ref name, 128)) _newName = name; - if (ImGui.IsItemDeactivatedAfterEdit() && _newName != null) + if (ImGui.IsItemDeactivatedAfterEdit() && _newName != null && _newName != collection.Name) { collection.Name = _newName; - _newName = null; + saveService.QueueSave(new ModCollectionSave(mods, collection)); + selector.RestoreCollections(); + _newName = null; } else if (ImGui.IsItemDeactivated()) + { _newName = null; + } + using (ImRaii.PushFont(UiBuilder.MonoFont)) { if (ImGui.Button(collection.Identifier, new Vector2(width, 0))) @@ -329,7 +321,7 @@ public sealed class CollectionPanel : IDisposable DrawIndividualDragTarget(text, id); if (!invalid) { - _selector.DragTargetAssignment(type, id); + selector.DragTargetAssignment(type, id); var name = Name(collection); var size = ImGui.CalcTextSize(name); var textPos = ImGui.GetItemRectMax() - size - ImGui.GetStyle().FramePadding; @@ -418,7 +410,7 @@ public sealed class CollectionPanel : IDisposable /// Respect incognito mode for names of identifiers. private string Name(ActorIdentifier id, string? name) - => _incognito.IncognitoMode && id.Type is IdentifierType.Player or IdentifierType.Owned + => incognito.IncognitoMode && id.Type is IdentifierType.Player or IdentifierType.Owned ? id.Incognito(name) : name ?? id.ToString(); @@ -426,7 +418,7 @@ public sealed class CollectionPanel : IDisposable private string Name(ModCollection? collection) => collection == null ? "Unassigned" : collection == ModCollection.Empty ? "Use No Mods" : - _incognito.IncognitoMode ? collection.AnonymizedName : collection.Name; + incognito.IncognitoMode ? collection.AnonymizedName : collection.Name; private void DrawIndividualButton(string intro, Vector2 width, string tooltip, char suffix, params ActorIdentifier[] identifiers) { @@ -445,11 +437,11 @@ public sealed class CollectionPanel : IDisposable } private void DrawCurrentCharacter(Vector2 width) - => DrawIndividualButton("Current Character", width, string.Empty, 'c', _actors.GetCurrentPlayer()); + => DrawIndividualButton("Current Character", width, string.Empty, 'c', actors.GetCurrentPlayer()); private void DrawCurrentTarget(Vector2 width) => DrawIndividualButton("Current Target", width, string.Empty, 't', - _actors.FromObject(_targets.Target, false, true, true)); + actors.FromObject(targets.Target, false, true, true)); private void DrawNewPlayer(Vector2 width) => DrawIndividualButton("New Player", width, _individualAssignmentUi.PlayerTooltip, 'p', @@ -610,7 +602,7 @@ public sealed class CollectionPanel : IDisposable ImGui.TableSetupColumn("State", ImGuiTableColumnFlags.WidthFixed, 1.75f * ImGui.GetFrameHeight()); ImGui.TableSetupColumn("Priority", ImGuiTableColumnFlags.WidthFixed, 2.5f * ImGui.GetFrameHeight()); ImGui.TableHeadersRow(); - foreach (var (mod, (settings, parent)) in _mods.Select(m => (m, collection[m.Index])) + foreach (var (mod, (settings, parent)) in mods.Select(m => (m, collection[m.Index])) .Where(t => t.Item2.Settings != null) .OrderBy(t => t.m.Name)) { diff --git a/Penumbra/UI/CollectionTab/CollectionSelector.cs b/Penumbra/UI/CollectionTab/CollectionSelector.cs index c14baf5b..024873bf 100644 --- a/Penumbra/UI/CollectionTab/CollectionSelector.cs +++ b/Penumbra/UI/CollectionTab/CollectionSelector.cs @@ -44,7 +44,9 @@ public sealed class CollectionSelector : ItemSelector, IDisposabl if (idx < 0 || idx >= Items.Count) return false; - return _storage.RemoveCollection(Items[idx]); + // Always return false since we handle the selection update ourselves. + _storage.RemoveCollection(Items[idx]); + return false; } protected override bool DeleteButtonEnabled() @@ -111,6 +113,15 @@ public sealed class CollectionSelector : ItemSelector, IDisposabl private string Name(ModCollection collection) => _incognito.IncognitoMode || collection.Name.Length == 0 ? collection.AnonymizedName : collection.Name; + public void RestoreCollections() + { + Items.Clear(); + foreach (var c in _storage.OrderBy(c => c.Name)) + Items.Add(c); + SetFilterDirty(); + SetCurrent(_active.Current); + } + private void OnCollectionChange(CollectionType type, ModCollection? old, ModCollection? @new, string _3) { switch (type) @@ -122,14 +133,7 @@ public sealed class CollectionSelector : ItemSelector, IDisposabl SetFilterDirty(); return; case CollectionType.Inactive: - Items.Clear(); - foreach (var c in _storage.OrderBy(c => c.Name)) - Items.Add(c); - - if (old == Current) - ClearCurrentSelection(); - else - TryRestoreCurrent(); + RestoreCollections(); SetFilterDirty(); return; default: diff --git a/Penumbra/UI/Tabs/CollectionsTab.cs b/Penumbra/UI/Tabs/CollectionsTab.cs index 1eaece50..fabf7561 100644 --- a/Penumbra/UI/Tabs/CollectionsTab.cs +++ b/Penumbra/UI/Tabs/CollectionsTab.cs @@ -1,16 +1,12 @@ using Dalamud.Game.ClientState.Objects; -using Dalamud.Interface; -using Dalamud.Interface.Utility; using Dalamud.Plugin; using ImGuiNET; -using OtterGui; using OtterGui.Raii; using OtterGui.Widgets; using Penumbra.Collections.Manager; using Penumbra.GameData.Actors; using Penumbra.Mods.Manager; using Penumbra.Services; -using Penumbra.UI.Classes; using Penumbra.UI.CollectionTab; namespace Penumbra.UI.Tabs; @@ -42,13 +38,13 @@ public sealed class CollectionsTab : IDisposable, ITab } public CollectionsTab(DalamudPluginInterface pi, Configuration configuration, CommunicatorService communicator, IncognitoService incognito, - CollectionManager collectionManager, ModStorage modStorage, ActorManager actors, ITargetManager targets, TutorialService tutorial, FilenameService fileNames) + CollectionManager collectionManager, ModStorage modStorage, ActorManager actors, ITargetManager targets, TutorialService tutorial, SaveService saveService) { _config = configuration.Ephemeral; _tutorial = tutorial; _incognito = incognito; _selector = new CollectionSelector(configuration, communicator, collectionManager.Storage, collectionManager.Active, _tutorial, incognito); - _panel = new CollectionPanel(pi, communicator, collectionManager, _selector, actors, targets, modStorage, fileNames, incognito); + _panel = new CollectionPanel(pi, communicator, collectionManager, _selector, actors, targets, modStorage, saveService, incognito); } public void Dispose() diff --git a/Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs b/Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs index 4649e548..94c6cbd6 100644 --- a/Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs +++ b/Penumbra/UI/Tabs/Debug/CrashDataExtensions.cs @@ -96,7 +96,7 @@ public static class CrashDataExtensions if (!table) return; - ImGuiClip.ClippedDraw(data.LastVfxFuncsInvoked, vfx => + ImGuiClip.ClippedDraw(data.LastVFXFuncsInvoked, vfx => { ImGuiUtil.DrawTableColumn(vfx.Age.ToString(CultureInfo.InvariantCulture)); ImGuiUtil.DrawTableColumn(vfx.ThreadId.ToString()); From aeb2db9f5d7f390407e4ab760a55b5eb6a4a53ae Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 3 Jun 2024 17:46:45 +0200 Subject: [PATCH 205/433] Add tooltip to global eqp condition. --- Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs index a2a6925a..d4049bd9 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs @@ -758,6 +758,7 @@ public partial class ModEditWindow if (IdInput("##geqpCond", 100 * ImUtf8.GlobalScale, _new.Condition.Id, out var newId, 1, ushort.MaxValue, _new.Condition.Id <= 1)) _new = _new with { Condition = newId }; + ImUtf8.HoverTooltip("The Model ID for the item that should not be hidden."u8); } public static void Draw(MetaFileManager metaFileManager, GlobalEqpManipulation meta, ModEditor editor, Vector2 iconSize) From 699ae8e1fb5939c8b7c8afe1614d73ba70d6317d Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 3 Jun 2024 17:47:11 +0200 Subject: [PATCH 206/433] Fix issue with collection settings being set to negative value for some reason. --- Penumbra/Mods/Settings/Setting.cs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Penumbra/Mods/Settings/Setting.cs b/Penumbra/Mods/Settings/Setting.cs index 059cbf51..e8ad103c 100644 --- a/Penumbra/Mods/Settings/Setting.cs +++ b/Penumbra/Mods/Settings/Setting.cs @@ -85,6 +85,24 @@ public readonly record struct Setting(ulong Value) public override Setting ReadJson(JsonReader reader, Type objectType, Setting existingValue, bool hasExistingValue, JsonSerializer serializer) - => new(serializer.Deserialize(reader)); + { + try + { + return new Setting(serializer.Deserialize(reader)); + } + catch (Exception e) + { + Penumbra.Log.Warning($"Could not deserialize setting {reader.Value} to unsigned long:\n{e}"); + try + { + return new Setting((ulong)serializer.Deserialize(reader)); + } + catch + { + Penumbra.Log.Warning($"Could not deserialize setting {reader.Value} to long:\n{e}"); + return Zero; + } + } + } } } From 87fec7783eb3d416f2ffb5aa3cdc1b2c78acfc75 Mon Sep 17 00:00:00 2001 From: ackwell Date: Tue, 4 Jun 2024 12:10:45 +1000 Subject: [PATCH 207/433] Fix blend weight adjustment getting stuck on near-bounds values --- Penumbra/Import/Models/Import/VertexAttribute.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Penumbra/Import/Models/Import/VertexAttribute.cs b/Penumbra/Import/Models/Import/VertexAttribute.cs index a4651776..af401ec1 100644 --- a/Penumbra/Import/Models/Import/VertexAttribute.cs +++ b/Penumbra/Import/Models/Import/VertexAttribute.cs @@ -150,6 +150,12 @@ public class VertexAttribute { var convertedValues = byteValues.Select(value => value * (1f / 255f)).ToArray(); var closestIndex = Enumerable.Range(0, 4) + .Where(index => { + var byteValue = byteValues[index]; + if (adjustment < 0) return byteValue > 0; + if (adjustment > 0) return byteValue < 255; + return true; + }) .Select(index => (index, delta: Math.Abs(originalValues[index] - convertedValues[index]))) .MinBy(x => x.delta) .index; From 48dd4bcadb2a43e0cef94e89022da1fd802cec3a Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 4 Jun 2024 15:53:35 +0200 Subject: [PATCH 208/433] Bleh. --- Penumbra/UI/FileDialogService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/UI/FileDialogService.cs b/Penumbra/UI/FileDialogService.cs index e5b0fa19..88c0b00f 100644 --- a/Penumbra/UI/FileDialogService.cs +++ b/Penumbra/UI/FileDialogService.cs @@ -101,7 +101,7 @@ public class FileDialogService : IDisposable private static string HandleRoot(string path) { - if (path.Length == 2 && path[1] == ':') + if (path is [_, ':']) return path + '\\'; return path; From 03bfbcc3095042bdf7eccadc2494074bfe4e5189 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 5 Jun 2024 10:36:38 +0200 Subject: [PATCH 209/433] Fidx wrong group --- Penumbra/Mods/Editor/ModMerger.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Mods/Editor/ModMerger.cs b/Penumbra/Mods/Editor/ModMerger.cs index 32a207ff..8d47051c 100644 --- a/Penumbra/Mods/Editor/ModMerger.cs +++ b/Penumbra/Mods/Editor/ModMerger.cs @@ -105,7 +105,7 @@ public class ModMerger : IDisposable throw new Exception( $"The merged group {originalGroup.Name} already existed, but had a different type than the original group of type {originalGroup.Type}."); - foreach (var originalOption in group.DataContainers) + foreach (var originalOption in originalGroup.DataContainers) { var (option, _, optionCreated) = _editor.FindOrAddOption(group, originalOption.GetName()); if (optionCreated) From ceed8531af0da494a9bd3909835e2f8a245491f7 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 6 Jun 2024 16:49:39 +0200 Subject: [PATCH 210/433] Fix GMP Entry edit. --- Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs index d4049bd9..0783dd98 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs @@ -640,7 +640,7 @@ public partial class ModEditWindow ImGui.SameLine(); if (IntDragInput("##gmpUnkB", $"Animation Type B?\nDefault Value: {defaultEntry.UnknownB}", UnkWidth, meta.Entry.UnknownB, defaultEntry.UnknownB, out var unkB, 0, 15, 0.01f)) - editor.MetaEditor.Change(meta.Copy(meta.Entry with { UnknownA = (byte)unkB })); + editor.MetaEditor.Change(meta.Copy(meta.Entry with { UnknownB = (byte)unkB })); } } From 2e9f1844546476713634c2343e4db2020544faad Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 6 Jun 2024 17:26:25 +0200 Subject: [PATCH 211/433] Introduce Identifiers and strong entry types for each meta manipulation and use them in the manipulations. --- Penumbra/Collections/Cache/EstCache.cs | 38 +++--- Penumbra/Collections/Cache/MetaCache.cs | 4 +- .../Collections/ModCollection.Cache.Access.cs | 2 +- Penumbra/Import/Models/ModelManager.cs | 16 +-- .../Import/TexToolsMeta.Deserialization.cs | 12 +- Penumbra/Import/TexToolsMeta.Export.cs | 12 +- Penumbra/Import/TexToolsMeta.Rgsp.cs | 4 +- .../Hooks/Resources/ResolvePathHooksBase.cs | 8 +- .../ResolveContext.PathResolution.cs | 14 +-- Penumbra/Meta/Files/CmpFile.cs | 11 +- Penumbra/Meta/Files/EstFile.cs | 44 +++---- Penumbra/Meta/Manipulations/Eqdp.cs | 87 ++++++++++++++ .../Meta/Manipulations/EqdpManipulation.cs | 23 ++-- Penumbra/Meta/Manipulations/Eqp.cs | 72 +++++++++++ .../Meta/Manipulations/EqpManipulation.cs | 14 ++- Penumbra/Meta/Manipulations/Est.cs | 113 ++++++++++++++++++ .../Meta/Manipulations/EstManipulation.cs | 36 +++--- Penumbra/Meta/Manipulations/Gmp.cs | 39 ++++++ .../Meta/Manipulations/GmpManipulation.cs | 12 +- Penumbra/Meta/Manipulations/Rsp.cs | 52 ++++++++ .../Meta/Manipulations/RspManipulation.cs | 22 ++-- Penumbra/Mods/ItemSwap/EquipmentSwap.cs | 6 +- Penumbra/Mods/ItemSwap/ItemSwap.cs | 16 +-- Penumbra/Mods/ItemSwap/ItemSwapContainer.cs | 6 +- .../UI/AdvancedWindow/ModEditWindow.Meta.cs | 23 ++-- Penumbra/UI/Classes/Combos.cs | 2 +- Penumbra/Util/IdentifierExtensions.cs | 8 +- 27 files changed, 533 insertions(+), 163 deletions(-) create mode 100644 Penumbra/Meta/Manipulations/Eqdp.cs create mode 100644 Penumbra/Meta/Manipulations/Eqp.cs create mode 100644 Penumbra/Meta/Manipulations/Est.cs create mode 100644 Penumbra/Meta/Manipulations/Gmp.cs create mode 100644 Penumbra/Meta/Manipulations/Rsp.cs diff --git a/Penumbra/Collections/Cache/EstCache.cs b/Penumbra/Collections/Cache/EstCache.cs index 2552cd4a..3a0b4695 100644 --- a/Penumbra/Collections/Cache/EstCache.cs +++ b/Penumbra/Collections/Cache/EstCache.cs @@ -48,33 +48,33 @@ public struct EstCache : IDisposable } } - public MetaList.MetaReverter TemporarilySetFiles(MetaFileManager manager, EstManipulation.EstType type) + public MetaList.MetaReverter TemporarilySetFiles(MetaFileManager manager, EstType type) { var (file, idx) = type switch { - EstManipulation.EstType.Face => (_estFaceFile, MetaIndex.FaceEst), - EstManipulation.EstType.Hair => (_estHairFile, MetaIndex.HairEst), - EstManipulation.EstType.Body => (_estBodyFile, MetaIndex.BodyEst), - EstManipulation.EstType.Head => (_estHeadFile, MetaIndex.HeadEst), + EstType.Face => (_estFaceFile, MetaIndex.FaceEst), + EstType.Hair => (_estHairFile, MetaIndex.HairEst), + EstType.Body => (_estBodyFile, MetaIndex.BodyEst), + EstType.Head => (_estHeadFile, MetaIndex.HeadEst), _ => (null, 0), }; return manager.TemporarilySetFile(file, idx); } - private readonly EstFile? GetEstFile(EstManipulation.EstType type) + private readonly EstFile? GetEstFile(EstType type) { return type switch { - EstManipulation.EstType.Face => _estFaceFile, - EstManipulation.EstType.Hair => _estHairFile, - EstManipulation.EstType.Body => _estBodyFile, - EstManipulation.EstType.Head => _estHeadFile, + EstType.Face => _estFaceFile, + EstType.Hair => _estHairFile, + EstType.Body => _estBodyFile, + EstType.Head => _estHeadFile, _ => null, }; } - internal ushort GetEstEntry(MetaFileManager manager, EstManipulation.EstType type, GenderRace genderRace, PrimaryId primaryId) + internal EstEntry GetEstEntry(MetaFileManager manager, EstType type, GenderRace genderRace, PrimaryId primaryId) { var file = GetEstFile(type); return file != null @@ -96,10 +96,10 @@ public struct EstCache : IDisposable _estManipulations.AddOrReplace(m); var file = m.Slot switch { - EstManipulation.EstType.Hair => _estHairFile ??= new EstFile(manager, EstManipulation.EstType.Hair), - EstManipulation.EstType.Face => _estFaceFile ??= new EstFile(manager, EstManipulation.EstType.Face), - EstManipulation.EstType.Body => _estBodyFile ??= new EstFile(manager, EstManipulation.EstType.Body), - EstManipulation.EstType.Head => _estHeadFile ??= new EstFile(manager, EstManipulation.EstType.Head), + EstType.Hair => _estHairFile ??= new EstFile(manager, EstType.Hair), + EstType.Face => _estFaceFile ??= new EstFile(manager, EstType.Face), + EstType.Body => _estBodyFile ??= new EstFile(manager, EstType.Body), + EstType.Head => _estHeadFile ??= new EstFile(manager, EstType.Head), _ => throw new ArgumentOutOfRangeException(), }; return m.Apply(file); @@ -114,10 +114,10 @@ public struct EstCache : IDisposable var manip = new EstManipulation(m.Gender, m.Race, m.Slot, m.SetId, def); var file = m.Slot switch { - EstManipulation.EstType.Hair => _estHairFile!, - EstManipulation.EstType.Face => _estFaceFile!, - EstManipulation.EstType.Body => _estBodyFile!, - EstManipulation.EstType.Head => _estHeadFile!, + EstType.Hair => _estHairFile!, + EstType.Face => _estFaceFile!, + EstType.Body => _estBodyFile!, + EstType.Head => _estHeadFile!, _ => throw new ArgumentOutOfRangeException(), }; return manip.Apply(file); diff --git a/Penumbra/Collections/Cache/MetaCache.cs b/Penumbra/Collections/Cache/MetaCache.cs index f42b72fc..fbca9c0e 100644 --- a/Penumbra/Collections/Cache/MetaCache.cs +++ b/Penumbra/Collections/Cache/MetaCache.cs @@ -188,7 +188,7 @@ public class MetaCache : IDisposable, IEnumerable _cmpCache.TemporarilySetFiles(_manager); - public MetaList.MetaReverter TemporarilySetEstFile(EstManipulation.EstType type) + public MetaList.MetaReverter TemporarilySetEstFile(EstType type) => _estCache.TemporarilySetFiles(_manager, type); public unsafe EqpEntry ApplyGlobalEqp(EqpEntry baseEntry, CharacterArmor* armor) @@ -208,7 +208,7 @@ public class MetaCache : IDisposable, IEnumerable _estCache.GetEstEntry(_manager, type, genderRace, primaryId); /// Use this when CharacterUtility becomes ready. diff --git a/Penumbra/Collections/ModCollection.Cache.Access.cs b/Penumbra/Collections/ModCollection.Cache.Access.cs index 3f3733e0..484d4dd2 100644 --- a/Penumbra/Collections/ModCollection.Cache.Access.cs +++ b/Penumbra/Collections/ModCollection.Cache.Access.cs @@ -112,7 +112,7 @@ public partial class ModCollection => _cache?.Meta.TemporarilySetCmpFile() ?? utility.TemporarilyResetResource(MetaIndex.HumanCmp); - public MetaList.MetaReverter TemporarilySetEstFile(CharacterUtility utility, EstManipulation.EstType type) + public MetaList.MetaReverter TemporarilySetEstFile(CharacterUtility utility, EstType type) => _cache?.Meta.TemporarilySetEstFile(type) ?? utility.TemporarilyResetResource((MetaIndex)type); diff --git a/Penumbra/Import/Models/ModelManager.cs b/Penumbra/Import/Models/ModelManager.cs index 485a76a7..fdd28ef1 100644 --- a/Penumbra/Import/Models/ModelManager.cs +++ b/Penumbra/Import/Models/ModelManager.cs @@ -63,16 +63,16 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect return info.ObjectType switch { ObjectType.Equipment when info.EquipSlot.ToSlot() is EquipSlot.Body - => [baseSkeleton, ..ResolveEstSkeleton(EstManipulation.EstType.Body, info, estManipulations)], + => [baseSkeleton, ..ResolveEstSkeleton(EstType.Body, info, estManipulations)], ObjectType.Equipment when info.EquipSlot.ToSlot() is EquipSlot.Head - => [baseSkeleton, ..ResolveEstSkeleton(EstManipulation.EstType.Head, info, estManipulations)], + => [baseSkeleton, ..ResolveEstSkeleton(EstType.Head, info, estManipulations)], ObjectType.Equipment => [baseSkeleton], ObjectType.Accessory => [baseSkeleton], ObjectType.Character when info.BodySlot is BodySlot.Body or BodySlot.Tail => [baseSkeleton], ObjectType.Character when info.BodySlot is BodySlot.Hair - => [baseSkeleton, ..ResolveEstSkeleton(EstManipulation.EstType.Hair, info, estManipulations)], + => [baseSkeleton, ..ResolveEstSkeleton(EstType.Hair, info, estManipulations)], ObjectType.Character when info.BodySlot is BodySlot.Face or BodySlot.Ear - => [baseSkeleton, ..ResolveEstSkeleton(EstManipulation.EstType.Face, info, estManipulations)], + => [baseSkeleton, ..ResolveEstSkeleton(EstType.Face, info, estManipulations)], ObjectType.Character => throw new Exception($"Currently unsupported human model type \"{info.BodySlot}\"."), ObjectType.DemiHuman => [GamePaths.DemiHuman.Sklb.Path(info.PrimaryId)], ObjectType.Monster => [GamePaths.Monster.Sklb.Path(info.PrimaryId)], @@ -81,7 +81,7 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect }; } - private string[] ResolveEstSkeleton(EstManipulation.EstType type, GameObjectInfo info, EstManipulation[] estManipulations) + private string[] ResolveEstSkeleton(EstType type, GameObjectInfo info, EstManipulation[] estManipulations) { // Try to find an EST entry from the manipulations provided. var (gender, race) = info.GenderRace.Split(); @@ -96,13 +96,13 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect // Try to use an entry from provided manipulations, falling back to the current collection. var targetId = modEst?.Entry ?? collections.Current.MetaCache?.GetEstEntry(type, info.GenderRace, info.PrimaryId) - ?? 0; + ?? EstEntry.Zero; // If there's no entries, we can assume that there's no additional skeleton. - if (targetId == 0) + if (targetId == EstEntry.Zero) return []; - return [GamePaths.Skeleton.Sklb.Path(info.GenderRace, EstManipulation.ToName(type), targetId)]; + return [GamePaths.Skeleton.Sklb.Path(info.GenderRace, EstManipulation.ToName(type), targetId.AsId)]; } /// Try to resolve the absolute path to a .mtrl from the potentially-partial path provided by a model. diff --git a/Penumbra/Import/TexToolsMeta.Deserialization.cs b/Penumbra/Import/TexToolsMeta.Deserialization.cs index 554cf848..f6157747 100644 --- a/Penumbra/Import/TexToolsMeta.Deserialization.cs +++ b/Penumbra/Import/TexToolsMeta.Deserialization.cs @@ -75,14 +75,14 @@ public partial class TexToolsMeta { var gr = (GenderRace)reader.ReadUInt16(); var id = reader.ReadUInt16(); - var value = reader.ReadUInt16(); + var value = new EstEntry(reader.ReadUInt16()); var type = (metaFileInfo.SecondaryType, metaFileInfo.EquipSlot) switch { - (BodySlot.Face, _) => EstManipulation.EstType.Face, - (BodySlot.Hair, _) => EstManipulation.EstType.Hair, - (_, EquipSlot.Head) => EstManipulation.EstType.Head, - (_, EquipSlot.Body) => EstManipulation.EstType.Body, - _ => (EstManipulation.EstType)0, + (BodySlot.Face, _) => EstType.Face, + (BodySlot.Hair, _) => EstType.Hair, + (_, EquipSlot.Head) => EstType.Head, + (_, EquipSlot.Body) => EstType.Body, + _ => (EstType)0, }; if (!gr.IsValid() || type == 0) continue; diff --git a/Penumbra/Import/TexToolsMeta.Export.cs b/Penumbra/Import/TexToolsMeta.Export.cs index 09bd2c12..4fb56df6 100644 --- a/Penumbra/Import/TexToolsMeta.Export.cs +++ b/Penumbra/Import/TexToolsMeta.Export.cs @@ -66,7 +66,7 @@ public partial class TexToolsMeta foreach (var attribute in attributes) { var value = list.TryGetValue(attribute, out var tmp) ? tmp.Entry : CmpFile.GetDefault(manager, race, attribute); - b.Write(value); + b.Write(value.Value); } } @@ -176,7 +176,7 @@ public partial class TexToolsMeta { b.Write((ushort)Names.CombinedRace(manip.Est.Gender, manip.Est.Race)); b.Write(manip.Est.SetId.Id); - b.Write(manip.Est.Entry); + b.Write(manip.Est.Entry.Value); } break; @@ -239,10 +239,10 @@ public partial class TexToolsMeta var raceCode = Names.CombinedRace(manip.Gender, manip.Race).ToRaceCode(); return manip.Slot switch { - EstManipulation.EstType.Hair => $"chara/human/c{raceCode}/obj/hair/h{manip.SetId:D4}/c{raceCode}h{manip.SetId:D4}_hir.meta", - EstManipulation.EstType.Face => $"chara/human/c{raceCode}/obj/face/h{manip.SetId:D4}/c{raceCode}f{manip.SetId:D4}_fac.meta", - EstManipulation.EstType.Body => $"chara/equipment/e{manip.SetId:D4}/e{manip.SetId:D4}_{EquipSlot.Body.ToSuffix()}.meta", - EstManipulation.EstType.Head => $"chara/equipment/e{manip.SetId:D4}/e{manip.SetId:D4}_{EquipSlot.Head.ToSuffix()}.meta", + EstType.Hair => $"chara/human/c{raceCode}/obj/hair/h{manip.SetId:D4}/c{raceCode}h{manip.SetId:D4}_hir.meta", + EstType.Face => $"chara/human/c{raceCode}/obj/face/h{manip.SetId:D4}/c{raceCode}f{manip.SetId:D4}_fac.meta", + EstType.Body => $"chara/equipment/e{manip.SetId:D4}/e{manip.SetId:D4}_{EquipSlot.Body.ToSuffix()}.meta", + EstType.Head => $"chara/equipment/e{manip.SetId:D4}/e{manip.SetId:D4}_{EquipSlot.Head.ToSuffix()}.meta", _ => throw new ArgumentOutOfRangeException(), }; } diff --git a/Penumbra/Import/TexToolsMeta.Rgsp.cs b/Penumbra/Import/TexToolsMeta.Rgsp.cs index 51faa175..71b9165f 100644 --- a/Penumbra/Import/TexToolsMeta.Rgsp.cs +++ b/Penumbra/Import/TexToolsMeta.Rgsp.cs @@ -46,8 +46,8 @@ public partial class TexToolsMeta void Add(RspAttribute attribute, float value) { var def = CmpFile.GetDefault(manager, subRace, attribute); - if (keepDefault || value != def) - ret.MetaManipulations.Add(new RspManipulation(subRace, attribute, value)); + if (keepDefault || value != def.Value) + ret.MetaManipulations.Add(new RspManipulation(subRace, attribute, new RspEntry(value))); } if (gender == 1) diff --git a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs index 8118343d..9a68160b 100644 --- a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs +++ b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs @@ -212,10 +212,10 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable if (_parent.InInternalResolve) return DisposableContainer.Empty; - return new DisposableContainer(data.ModCollection.TemporarilySetEstFile(_parent.CharacterUtility, EstManipulation.EstType.Face), - data.ModCollection.TemporarilySetEstFile(_parent.CharacterUtility, EstManipulation.EstType.Body), - data.ModCollection.TemporarilySetEstFile(_parent.CharacterUtility, EstManipulation.EstType.Hair), - data.ModCollection.TemporarilySetEstFile(_parent.CharacterUtility, EstManipulation.EstType.Head)); + return new DisposableContainer(data.ModCollection.TemporarilySetEstFile(_parent.CharacterUtility, EstType.Face), + data.ModCollection.TemporarilySetEstFile(_parent.CharacterUtility, EstType.Body), + data.ModCollection.TemporarilySetEstFile(_parent.CharacterUtility, EstType.Hair), + data.ModCollection.TemporarilySetEstFile(_parent.CharacterUtility, EstType.Head)); } diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs index 236c7051..2b87e688 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs @@ -250,30 +250,30 @@ internal partial record ResolveContext _ => 0, }; } - return ResolveHumanExtraSkeletonData(characterRaceCode, EstManipulation.EstType.Face, faceId); + return ResolveHumanExtraSkeletonData(characterRaceCode, EstType.Face, faceId); case 2: - return ResolveHumanExtraSkeletonData(characterRaceCode, EstManipulation.EstType.Hair, human->HairId); + return ResolveHumanExtraSkeletonData(characterRaceCode, EstType.Hair, human->HairId); case 3: - return ResolveHumanEquipmentSkeletonData(EquipSlot.Head, EstManipulation.EstType.Head); + return ResolveHumanEquipmentSkeletonData(EquipSlot.Head, EstType.Head); case 4: - return ResolveHumanEquipmentSkeletonData(EquipSlot.Body, EstManipulation.EstType.Body); + return ResolveHumanEquipmentSkeletonData(EquipSlot.Body, EstType.Body); default: return (0, string.Empty, 0); } } - private unsafe (GenderRace RaceCode, string Slot, PrimaryId Set) ResolveHumanEquipmentSkeletonData(EquipSlot slot, EstManipulation.EstType type) + private unsafe (GenderRace RaceCode, string Slot, PrimaryId Set) ResolveHumanEquipmentSkeletonData(EquipSlot slot, EstType type) { var human = (Human*)CharacterBase; var equipment = ((CharacterArmor*)&human->Head)[slot.ToIndex()]; return ResolveHumanExtraSkeletonData(ResolveEqdpRaceCode(slot, equipment.Set), type, equipment.Set); } - private (GenderRace RaceCode, string Slot, PrimaryId Set) ResolveHumanExtraSkeletonData(GenderRace raceCode, EstManipulation.EstType type, PrimaryId primary) + private (GenderRace RaceCode, string Slot, PrimaryId Set) ResolveHumanExtraSkeletonData(GenderRace raceCode, EstType type, PrimaryId primary) { var metaCache = Global.Collection.MetaCache; var skeletonSet = metaCache?.GetEstEntry(type, raceCode, primary) ?? default; - return (raceCode, EstManipulation.ToName(type), skeletonSet); + return (raceCode, EstManipulation.ToName(type), skeletonSet.AsId); } private unsafe Utf8GamePath ResolveSkeletonPathNative(uint partialSkeletonIndex) diff --git a/Penumbra/Meta/Files/CmpFile.cs b/Penumbra/Meta/Files/CmpFile.cs index b265a5e8..96cda496 100644 --- a/Penumbra/Meta/Files/CmpFile.cs +++ b/Penumbra/Meta/Files/CmpFile.cs @@ -2,6 +2,7 @@ using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.Structs; using Penumbra.Interop.Services; +using Penumbra.Meta.Manipulations; using Penumbra.String.Functions; namespace Penumbra.Meta.Files; @@ -17,10 +18,10 @@ public sealed unsafe class CmpFile : MetaBaseFile private const int RacialScalingStart = 0x2A800; - public float this[SubRace subRace, RspAttribute attribute] + public RspEntry this[SubRace subRace, RspAttribute attribute] { - get => *(float*)(Data + RacialScalingStart + ToRspIndex(subRace) * RspData.ByteSize + (int)attribute * 4); - set => *(float*)(Data + RacialScalingStart + ToRspIndex(subRace) * RspData.ByteSize + (int)attribute * 4) = value; + get => *(RspEntry*)(Data + RacialScalingStart + ToRspIndex(subRace) * RspData.ByteSize + (int)attribute * 4); + set => *(RspEntry*)(Data + RacialScalingStart + ToRspIndex(subRace) * RspData.ByteSize + (int)attribute * 4) = value; } public override void Reset() @@ -39,10 +40,10 @@ public sealed unsafe class CmpFile : MetaBaseFile Reset(); } - public static float GetDefault(MetaFileManager manager, SubRace subRace, RspAttribute attribute) + public static RspEntry GetDefault(MetaFileManager manager, SubRace subRace, RspAttribute attribute) { var data = (byte*)manager.CharacterUtility.DefaultResource(InternalIndex).Address; - return *(float*)(data + RacialScalingStart + ToRspIndex(subRace) * RspData.ByteSize + (int)attribute * 4); + return *(RspEntry*)(data + RacialScalingStart + ToRspIndex(subRace) * RspData.ByteSize + (int)attribute * 4); } private static int ToRspIndex(SubRace subRace) diff --git a/Penumbra/Meta/Files/EstFile.cs b/Penumbra/Meta/Files/EstFile.cs index af441b22..ee38ea1e 100644 --- a/Penumbra/Meta/Files/EstFile.cs +++ b/Penumbra/Meta/Files/EstFile.cs @@ -34,26 +34,26 @@ public sealed unsafe class EstFile : MetaBaseFile Removed, } - public ushort this[GenderRace genderRace, ushort setId] + public EstEntry this[GenderRace genderRace, PrimaryId setId] { get { var (idx, exists) = FindEntry(genderRace, setId); if (!exists) - return 0; + return EstEntry.Zero; - return *(ushort*)(Data + EntryDescSize * (Count + 1) + EntrySize * idx); + return *(EstEntry*)(Data + EntryDescSize * (Count + 1) + EntrySize * idx); } set => SetEntry(genderRace, setId, value); } - private void InsertEntry(int idx, GenderRace genderRace, ushort setId, ushort skeletonId) + private void InsertEntry(int idx, GenderRace genderRace, PrimaryId setId, EstEntry skeletonId) { if (Length < Size + EntryDescSize + EntrySize) ResizeResources(Length + IncreaseSize); var control = (Info*)(Data + 4); - var entries = (ushort*)(control + Count); + var entries = (EstEntry*)(control + Count); for (var i = Count - 1; i >= idx; --i) entries[i + 3] = entries[i]; @@ -94,10 +94,10 @@ public sealed unsafe class EstFile : MetaBaseFile [StructLayout(LayoutKind.Sequential, Size = 4)] private struct Info : IComparable { - public readonly ushort SetId; + public readonly PrimaryId SetId; public readonly GenderRace GenderRace; - public Info(GenderRace gr, ushort setId) + public Info(GenderRace gr, PrimaryId setId) { GenderRace = gr; SetId = setId; @@ -106,42 +106,42 @@ public sealed unsafe class EstFile : MetaBaseFile public int CompareTo(Info other) { var genderRaceComparison = GenderRace.CompareTo(other.GenderRace); - return genderRaceComparison != 0 ? genderRaceComparison : SetId.CompareTo(other.SetId); + return genderRaceComparison != 0 ? genderRaceComparison : SetId.Id.CompareTo(other.SetId.Id); } } - private static (int, bool) FindEntry(ReadOnlySpan data, GenderRace genderRace, ushort setId) + private static (int, bool) FindEntry(ReadOnlySpan data, GenderRace genderRace, PrimaryId setId) { var idx = data.BinarySearch(new Info(genderRace, setId)); return idx < 0 ? (~idx, false) : (idx, true); } - private (int, bool) FindEntry(GenderRace genderRace, ushort setId) + private (int, bool) FindEntry(GenderRace genderRace, PrimaryId setId) { var span = new ReadOnlySpan(Data + 4, Count); return FindEntry(span, genderRace, setId); } - public EstEntryChange SetEntry(GenderRace genderRace, ushort setId, ushort skeletonId) + public EstEntryChange SetEntry(GenderRace genderRace, PrimaryId setId, EstEntry skeletonId) { var (idx, exists) = FindEntry(genderRace, setId); if (exists) { - var value = *(ushort*)(Data + 4 * (Count + 1) + 2 * idx); + var value = *(EstEntry*)(Data + 4 * (Count + 1) + 2 * idx); if (value == skeletonId) return EstEntryChange.Unchanged; - if (skeletonId == 0) + if (skeletonId == EstEntry.Zero) { RemoveEntry(idx); return EstEntryChange.Removed; } - *(ushort*)(Data + 4 * (Count + 1) + 2 * idx) = skeletonId; + *(EstEntry*)(Data + 4 * (Count + 1) + 2 * idx) = skeletonId; return EstEntryChange.Changed; } - if (skeletonId == 0) + if (skeletonId == EstEntry.Zero) return EstEntryChange.Unchanged; InsertEntry(idx, genderRace, setId, skeletonId); @@ -156,7 +156,7 @@ public sealed unsafe class EstFile : MetaBaseFile MemoryUtility.MemSet(Data + length, 0, Length - length); } - public EstFile(MetaFileManager manager, EstManipulation.EstType estType) + public EstFile(MetaFileManager manager, EstType estType) : base(manager, (MetaIndex)estType) { var length = DefaultData.Length; @@ -164,24 +164,24 @@ public sealed unsafe class EstFile : MetaBaseFile Reset(); } - public ushort GetDefault(GenderRace genderRace, ushort setId) + public EstEntry GetDefault(GenderRace genderRace, PrimaryId setId) => GetDefault(Manager, Index, genderRace, setId); - public static ushort GetDefault(MetaFileManager manager, CharacterUtility.InternalIndex index, GenderRace genderRace, PrimaryId primaryId) + public static EstEntry GetDefault(MetaFileManager manager, CharacterUtility.InternalIndex index, GenderRace genderRace, PrimaryId primaryId) { var data = (byte*)manager.CharacterUtility.DefaultResource(index).Address; var count = *(int*)data; var span = new ReadOnlySpan(data + 4, count); var (idx, found) = FindEntry(span, genderRace, primaryId.Id); if (!found) - return 0; + return EstEntry.Zero; - return *(ushort*)(data + 4 + count * EntryDescSize + idx * EntrySize); + return *(EstEntry*)(data + 4 + count * EntryDescSize + idx * EntrySize); } - public static ushort GetDefault(MetaFileManager manager, MetaIndex metaIndex, GenderRace genderRace, PrimaryId primaryId) + public static EstEntry GetDefault(MetaFileManager manager, MetaIndex metaIndex, GenderRace genderRace, PrimaryId primaryId) => GetDefault(manager, CharacterUtility.ReverseIndices[(int)metaIndex], genderRace, primaryId); - public static ushort GetDefault(MetaFileManager manager, EstManipulation.EstType estType, GenderRace genderRace, PrimaryId primaryId) + public static EstEntry GetDefault(MetaFileManager manager, EstType estType, GenderRace genderRace, PrimaryId primaryId) => GetDefault(manager, (MetaIndex)estType, genderRace, primaryId); } diff --git a/Penumbra/Meta/Manipulations/Eqdp.cs b/Penumbra/Meta/Manipulations/Eqdp.cs new file mode 100644 index 00000000..6d6942e6 --- /dev/null +++ b/Penumbra/Meta/Manipulations/Eqdp.cs @@ -0,0 +1,87 @@ +using Newtonsoft.Json.Linq; +using Penumbra.GameData.Data; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Interop.Structs; + +namespace Penumbra.Meta.Manipulations; + +public readonly record struct EqdpIdentifier(PrimaryId SetId, EquipSlot Slot, GenderRace GenderRace) + : IMetaIdentifier, IComparable +{ + public ModelRace Race + => GenderRace.Split().Item2; + + public Gender Gender + => GenderRace.Split().Item1; + + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + => identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(SetId, GenderRace, Slot)); + + public MetaIndex FileIndex() + => CharacterUtilityData.EqdpIdx(GenderRace, Slot.IsAccessory()); + + public override string ToString() + => $"Eqdp - {SetId} - {Slot.ToName()} - {GenderRace.ToName()}"; + + public bool Validate() + { + var mask = Eqdp.Mask(Slot); + if (mask == 0) + return false; + + if (FileIndex() == (MetaIndex)(-1)) + return false; + + // No check for set id. + return true; + } + + public int CompareTo(EqdpIdentifier other) + { + var gr = GenderRace.CompareTo(other.GenderRace); + if (gr != 0) + return gr; + + var set = SetId.Id.CompareTo(other.SetId.Id); + if (set != 0) + return set; + + return Slot.CompareTo(other.Slot); + } + + public static EqdpIdentifier? FromJson(JObject jObj) + { + var gender = jObj["Gender"]?.ToObject() ?? Gender.Unknown; + var race = jObj["Race"]?.ToObject() ?? ModelRace.Unknown; + var setId = new PrimaryId(jObj["SetId"]?.ToObject() ?? 0); + var slot = jObj["Slot"]?.ToObject() ?? EquipSlot.Unknown; + var ret = new EqdpIdentifier(setId, slot, Names.CombinedRace(gender, race)); + return ret.Validate() ? ret : null; + } + + public JObject AddToJson(JObject jObj) + { + var (gender, race) = GenderRace.Split(); + jObj["Gender"] = gender.ToString(); + jObj["Race"] = race.ToString(); + jObj["SetId"] = SetId.Id.ToString(); + jObj["Slot"] = Slot.ToString(); + return jObj; + } +} + +public readonly record struct InternalEqdpEntry(bool Model, bool Material) +{ + private InternalEqdpEntry((bool, bool) val) + : this(val.Item1, val.Item2) + { } + + public InternalEqdpEntry(EqdpEntry entry, EquipSlot slot) + : this(entry.ToBits(slot)) + { } + + + public EqdpEntry ToEntry(EquipSlot slot) + => Eqdp.FromSlotAndBits(slot, Model, Material); +} diff --git a/Penumbra/Meta/Manipulations/EqdpManipulation.cs b/Penumbra/Meta/Manipulations/EqdpManipulation.cs index 0426dfce..2c01ce3f 100644 --- a/Penumbra/Meta/Manipulations/EqdpManipulation.cs +++ b/Penumbra/Meta/Manipulations/EqdpManipulation.cs @@ -10,27 +10,30 @@ namespace Penumbra.Meta.Manipulations; [StructLayout(LayoutKind.Sequential, Pack = 1)] public readonly struct EqdpManipulation : IMetaManipulation { - public EqdpEntry Entry { get; private init; } + [JsonIgnore] + public EqdpIdentifier Identifier { get; private init; } + public EqdpEntry Entry { get; private init; } [JsonConverter(typeof(StringEnumConverter))] - public Gender Gender { get; private init; } + public Gender Gender + => Identifier.Gender; [JsonConverter(typeof(StringEnumConverter))] - public ModelRace Race { get; private init; } + public ModelRace Race + => Identifier.Race; - public PrimaryId SetId { get; private init; } + public PrimaryId SetId + => Identifier.SetId; [JsonConverter(typeof(StringEnumConverter))] - public EquipSlot Slot { get; private init; } + public EquipSlot Slot + => Identifier.Slot; [JsonConstructor] public EqdpManipulation(EqdpEntry entry, EquipSlot slot, Gender gender, ModelRace race, PrimaryId setId) { - Gender = gender; - Race = race; - SetId = setId; - Slot = slot; - Entry = Eqdp.Mask(Slot) & entry; + Identifier = new EqdpIdentifier(setId, slot, Names.CombinedRace(gender, race)); + Entry = Eqdp.Mask(Slot) & entry; } public EqdpManipulation Copy(EqdpManipulation entry) diff --git a/Penumbra/Meta/Manipulations/Eqp.cs b/Penumbra/Meta/Manipulations/Eqp.cs new file mode 100644 index 00000000..572dc203 --- /dev/null +++ b/Penumbra/Meta/Manipulations/Eqp.cs @@ -0,0 +1,72 @@ +using Newtonsoft.Json.Linq; +using Penumbra.GameData.Data; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Interop.Structs; + +namespace Penumbra.Meta.Manipulations; + +public readonly record struct EqpIdentifier(PrimaryId SetId, EquipSlot Slot) : IMetaIdentifier, IComparable +{ + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + => identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(SetId, GenderRace.MidlanderMale, Slot)); + + public MetaIndex FileIndex() + => MetaIndex.Eqp; + + public override string ToString() + => $"Eqp - {SetId} - {Slot}"; + + public bool Validate() + { + var mask = Eqp.Mask(Slot); + if (mask == 0) + return false; + + // No check for set id. + return true; + } + + public int CompareTo(EqpIdentifier other) + { + var set = SetId.Id.CompareTo(other.SetId.Id); + if (set != 0) + return set; + + return Slot.CompareTo(other.Slot); + } + + public static EqpIdentifier? FromJson(JObject jObj) + { + var setId = new PrimaryId(jObj["SetId"]?.ToObject() ?? 0); + var slot = jObj["Slot"]?.ToObject() ?? EquipSlot.Unknown; + var ret = new EqpIdentifier(setId, slot); + return ret.Validate() ? ret : null; + } + + public JObject AddToJson(JObject jObj) + { + jObj["SetId"] = SetId.Id.ToString(); + jObj["Slot"] = Slot.ToString(); + return jObj; + } +} + +public readonly record struct EqpEntryInternal(uint Value) +{ + public EqpEntryInternal(EqpEntry entry, EquipSlot slot) + : this(GetValue(entry, slot)) + { } + + public EqpEntry ToEntry(EquipSlot slot) + { + var (offset, mask) = Eqp.OffsetAndMask(slot); + return (EqpEntry)((ulong)Value << offset) & mask; + } + + private static uint GetValue(EqpEntry entry, EquipSlot slot) + { + var (offset, mask) = Eqp.OffsetAndMask(slot); + return (uint)((ulong)(entry & mask) >> offset); + } +} diff --git a/Penumbra/Meta/Manipulations/EqpManipulation.cs b/Penumbra/Meta/Manipulations/EqpManipulation.cs index d59938b6..3bced096 100644 --- a/Penumbra/Meta/Manipulations/EqpManipulation.cs +++ b/Penumbra/Meta/Manipulations/EqpManipulation.cs @@ -5,7 +5,6 @@ using Penumbra.GameData.Structs; using Penumbra.Interop.Structs; using Penumbra.Meta.Files; using Penumbra.Util; -using SharpCompress.Common; namespace Penumbra.Meta.Manipulations; @@ -15,17 +14,20 @@ public readonly struct EqpManipulation : IMetaManipulation [JsonConverter(typeof(ForceNumericFlagEnumConverter))] public EqpEntry Entry { get; private init; } - public PrimaryId SetId { get; private init; } + public EqpIdentifier Identifier { get; private init; } + + public PrimaryId SetId + => Identifier.SetId; [JsonConverter(typeof(StringEnumConverter))] - public EquipSlot Slot { get; private init; } + public EquipSlot Slot + => Identifier.Slot; [JsonConstructor] public EqpManipulation(EqpEntry entry, EquipSlot slot, PrimaryId setId) { - Slot = slot; - SetId = setId; - Entry = Eqp.Mask(slot) & entry; + Identifier = new EqpIdentifier(setId, slot); + Entry = Eqp.Mask(slot) & entry; } public EqpManipulation Copy(EqpEntry entry) diff --git a/Penumbra/Meta/Manipulations/Est.cs b/Penumbra/Meta/Manipulations/Est.cs new file mode 100644 index 00000000..9f878f97 --- /dev/null +++ b/Penumbra/Meta/Manipulations/Est.cs @@ -0,0 +1,113 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Penumbra.GameData.Data; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Interop.Structs; + +namespace Penumbra.Meta.Manipulations; + +public enum EstType : byte +{ + Hair = MetaIndex.HairEst, + Face = MetaIndex.FaceEst, + Body = MetaIndex.BodyEst, + Head = MetaIndex.HeadEst, +} + +public readonly record struct EstIdentifier(PrimaryId SetId, EstType Slot, GenderRace GenderRace) + : IMetaIdentifier, IComparable +{ + public ModelRace Race + => GenderRace.Split().Item2; + + public Gender Gender + => GenderRace.Split().Item1; + + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + { + switch (Slot) + { + case EstType.Hair: + changedItems.TryAdd( + $"Customization: {GenderRace.Split().Item2.ToName()} {GenderRace.Split().Item1.ToName()} Hair (Hair) {SetId}", null); + break; + case EstType.Face: + changedItems.TryAdd( + $"Customization: {GenderRace.Split().Item2.ToName()} {GenderRace.Split().Item1.ToName()} Face (Face) {SetId}", null); + break; + case EstType.Body: + identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(SetId, GenderRace, EquipSlot.Body)); + break; + case EstType.Head: + identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(SetId, GenderRace, EquipSlot.Head)); + break; + } + } + + public MetaIndex FileIndex() + => (MetaIndex)Slot; + + public override string ToString() + => $"Est - {SetId} - {Slot} - {GenderRace.ToName()}"; + + public bool Validate() + { + if (!Enum.IsDefined(Slot)) + return false; + if (GenderRace is GenderRace.Unknown || !Enum.IsDefined(GenderRace)) + return false; + + // No known check for set id. + return true; + } + + public int CompareTo(EstIdentifier other) + { + var gr = GenderRace.CompareTo(other.GenderRace); + if (gr != 0) + return gr; + + var id = SetId.Id.CompareTo(other.SetId.Id); + return id != 0 ? id : Slot.CompareTo(other.Slot); + } + + public static EstIdentifier? FromJson(JObject jObj) + { + var gender = jObj["Gender"]?.ToObject() ?? Gender.Unknown; + var race = jObj["Race"]?.ToObject() ?? ModelRace.Unknown; + var setId = new PrimaryId(jObj["SetId"]?.ToObject() ?? 0); + var slot = jObj["Slot"]?.ToObject() ?? 0; + var ret = new EstIdentifier(setId, slot, Names.CombinedRace(gender, race)); + return ret.Validate() ? ret : null; + } + + public JObject AddToJson(JObject jObj) + { + var (gender, race) = GenderRace.Split(); + jObj["Gender"] = gender.ToString(); + jObj["Race"] = race.ToString(); + jObj["SetId"] = SetId.Id.ToString(); + jObj["Slot"] = Slot.ToString(); + return jObj; + } +} + +[JsonConverter(typeof(Converter))] +public readonly record struct EstEntry(ushort Value) +{ + public static readonly EstEntry Zero = new(0); + + public PrimaryId AsId + => new(Value); + + private class Converter : JsonConverter + { + public override void WriteJson(JsonWriter writer, EstEntry value, JsonSerializer serializer) + => serializer.Serialize(writer, value.Value); + + public override EstEntry ReadJson(JsonReader reader, Type objectType, EstEntry existingValue, bool hasExistingValue, + JsonSerializer serializer) + => new(serializer.Deserialize(reader)); + } +} diff --git a/Penumbra/Meta/Manipulations/EstManipulation.cs b/Penumbra/Meta/Manipulations/EstManipulation.cs index d3c92ad3..c3f9792f 100644 --- a/Penumbra/Meta/Manipulations/EstManipulation.cs +++ b/Penumbra/Meta/Manipulations/EstManipulation.cs @@ -10,14 +10,6 @@ namespace Penumbra.Meta.Manipulations; [StructLayout(LayoutKind.Sequential, Pack = 1)] public readonly struct EstManipulation : IMetaManipulation { - public enum EstType : byte - { - Hair = MetaIndex.HairEst, - Face = MetaIndex.FaceEst, - Body = MetaIndex.BodyEst, - Head = MetaIndex.HeadEst, - } - public static string ToName(EstType type) => type switch { @@ -28,31 +20,33 @@ public readonly struct EstManipulation : IMetaManipulation _ => "unk", }; - public ushort Entry { get; private init; } // SkeletonIdx. + public EstIdentifier Identifier { get; private init; } + public EstEntry Entry { get; private init; } [JsonConverter(typeof(StringEnumConverter))] - public Gender Gender { get; private init; } + public Gender Gender + => Identifier.Gender; [JsonConverter(typeof(StringEnumConverter))] - public ModelRace Race { get; private init; } + public ModelRace Race + => Identifier.Race; - public PrimaryId SetId { get; private init; } + public PrimaryId SetId + => Identifier.SetId; [JsonConverter(typeof(StringEnumConverter))] - public EstType Slot { get; private init; } + public EstType Slot + => Identifier.Slot; [JsonConstructor] - public EstManipulation(Gender gender, ModelRace race, EstType slot, PrimaryId setId, ushort entry) + public EstManipulation(Gender gender, ModelRace race, EstType slot, PrimaryId setId, EstEntry entry) { - Entry = entry; - Gender = gender; - Race = race; - SetId = setId; - Slot = slot; + Entry = entry; + Identifier = new EstIdentifier(setId, slot, Names.CombinedRace(gender, race)); } - public EstManipulation Copy(ushort entry) + public EstManipulation Copy(EstEntry entry) => new(Gender, Race, Slot, SetId, entry); @@ -111,3 +105,5 @@ public readonly struct EstManipulation : IMetaManipulation return true; } } + + diff --git a/Penumbra/Meta/Manipulations/Gmp.cs b/Penumbra/Meta/Manipulations/Gmp.cs new file mode 100644 index 00000000..1b7c70ba --- /dev/null +++ b/Penumbra/Meta/Manipulations/Gmp.cs @@ -0,0 +1,39 @@ +using Newtonsoft.Json.Linq; +using Penumbra.GameData.Data; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Interop.Structs; + +namespace Penumbra.Meta.Manipulations; + +public readonly record struct GmpIdentifier(PrimaryId SetId) : IMetaIdentifier, IComparable +{ + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + => identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(SetId, GenderRace.MidlanderMale, EquipSlot.Head)); + + public MetaIndex FileIndex() + => MetaIndex.Gmp; + + public override string ToString() + => $"Gmp - {SetId}"; + + public bool Validate() + // No known conditions. + => true; + + public int CompareTo(GmpIdentifier other) + => SetId.Id.CompareTo(other.SetId.Id); + + public static GmpIdentifier? FromJson(JObject jObj) + { + var setId = new PrimaryId(jObj["SetId"]?.ToObject() ?? 0); + var ret = new GmpIdentifier(setId); + return ret.Validate() ? ret : null; + } + + public JObject AddToJson(JObject jObj) + { + jObj["SetId"] = SetId.Id.ToString(); + return jObj; + } +} diff --git a/Penumbra/Meta/Manipulations/GmpManipulation.cs b/Penumbra/Meta/Manipulations/GmpManipulation.cs index ee58295d..0b2a9f4b 100644 --- a/Penumbra/Meta/Manipulations/GmpManipulation.cs +++ b/Penumbra/Meta/Manipulations/GmpManipulation.cs @@ -8,14 +8,18 @@ namespace Penumbra.Meta.Manipulations; [StructLayout(LayoutKind.Sequential, Pack = 1)] public readonly struct GmpManipulation : IMetaManipulation { - public GmpEntry Entry { get; private init; } - public PrimaryId SetId { get; private init; } + public GmpIdentifier Identifier { get; private init; } + + public GmpEntry Entry { get; private init; } + + public PrimaryId SetId + => Identifier.SetId; [JsonConstructor] public GmpManipulation(GmpEntry entry, PrimaryId setId) { - Entry = entry; - SetId = setId; + Entry = entry; + Identifier = new GmpIdentifier(setId); } public GmpManipulation Copy(GmpEntry entry) diff --git a/Penumbra/Meta/Manipulations/Rsp.cs b/Penumbra/Meta/Manipulations/Rsp.cs new file mode 100644 index 00000000..29cdfd71 --- /dev/null +++ b/Penumbra/Meta/Manipulations/Rsp.cs @@ -0,0 +1,52 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Penumbra.GameData.Data; +using Penumbra.GameData.Enums; +using Penumbra.Interop.Structs; + +namespace Penumbra.Meta.Manipulations; + +public readonly record struct RspIdentifier(SubRace SubRace, RspAttribute Attribute) : IMetaIdentifier +{ + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + => changedItems.TryAdd($"{SubRace.ToName()} {Attribute.ToFullString()}", null); + + public MetaIndex FileIndex() + => throw new NotImplementedException(); + + public bool Validate() + => throw new NotImplementedException(); + + public JObject AddToJson(JObject jObj) + => throw new NotImplementedException(); +} + +[JsonConverter(typeof(Converter))] +public readonly record struct RspEntry(float Value) : IComparisonOperators +{ + public const float MinValue = 0.01f; + public const float MaxValue = 512f; + public static readonly RspEntry One = new(1f); + + private class Converter : JsonConverter + { + public override void WriteJson(JsonWriter writer, RspEntry value, JsonSerializer serializer) + => serializer.Serialize(writer, value.Value); + + public override RspEntry ReadJson(JsonReader reader, Type objectType, RspEntry existingValue, bool hasExistingValue, + JsonSerializer serializer) + => new(serializer.Deserialize(reader)); + } + + public static bool operator >(RspEntry left, RspEntry right) + => left.Value > right.Value; + + public static bool operator >=(RspEntry left, RspEntry right) + => left.Value >= right.Value; + + public static bool operator <(RspEntry left, RspEntry right) + => left.Value < right.Value; + + public static bool operator <=(RspEntry left, RspEntry right) + => left.Value <= right.Value; +} diff --git a/Penumbra/Meta/Manipulations/RspManipulation.cs b/Penumbra/Meta/Manipulations/RspManipulation.cs index 7e5e3fcb..04691c9f 100644 --- a/Penumbra/Meta/Manipulations/RspManipulation.cs +++ b/Penumbra/Meta/Manipulations/RspManipulation.cs @@ -9,25 +9,25 @@ namespace Penumbra.Meta.Manipulations; [StructLayout(LayoutKind.Sequential, Pack = 1)] public readonly struct RspManipulation : IMetaManipulation { - public const float MinValue = 0.01f; - public const float MaxValue = 512f; - public float Entry { get; private init; } + public RspIdentifier Identifier { get; private init; } + public RspEntry Entry { get; private init; } [JsonConverter(typeof(StringEnumConverter))] - public SubRace SubRace { get; private init; } + public SubRace SubRace + => Identifier.SubRace; [JsonConverter(typeof(StringEnumConverter))] - public RspAttribute Attribute { get; private init; } + public RspAttribute Attribute + => Identifier.Attribute; [JsonConstructor] - public RspManipulation(SubRace subRace, RspAttribute attribute, float entry) + public RspManipulation(SubRace subRace, RspAttribute attribute, RspEntry entry) { - Entry = entry; - SubRace = subRace; - Attribute = attribute; + Entry = entry; + Identifier = new RspIdentifier(subRace, attribute); } - public RspManipulation Copy(float entry) + public RspManipulation Copy(RspEntry entry) => new(SubRace, Attribute, entry); public override string ToString() @@ -68,7 +68,7 @@ public readonly struct RspManipulation : IMetaManipulation return false; if (!Enum.IsDefined(Attribute)) return false; - if (Entry is < MinValue or > MaxValue) + if (Entry.Value is < RspEntry.MinValue or > RspEntry.MaxValue) return false; return true; diff --git a/Penumbra/Mods/ItemSwap/EquipmentSwap.cs b/Penumbra/Mods/ItemSwap/EquipmentSwap.cs index ea4ef7b1..3efee857 100644 --- a/Penumbra/Mods/ItemSwap/EquipmentSwap.cs +++ b/Penumbra/Mods/ItemSwap/EquipmentSwap.cs @@ -126,9 +126,9 @@ public static class EquipmentSwap var isAccessory = slot.IsAccessory(); var estType = slot switch { - EquipSlot.Head => EstManipulation.EstType.Head, - EquipSlot.Body => EstManipulation.EstType.Body, - _ => (EstManipulation.EstType)0, + EquipSlot.Head => EstType.Head, + EquipSlot.Body => EstType.Body, + _ => (EstType)0, }; var skipFemale = false; diff --git a/Penumbra/Mods/ItemSwap/ItemSwap.cs b/Penumbra/Mods/ItemSwap/ItemSwap.cs index b269d89c..7fac52c1 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwap.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwap.cs @@ -133,23 +133,23 @@ public static class ItemSwap } - public static FileSwap CreatePhyb(MetaFileManager manager, Func redirections, EstManipulation.EstType type, - GenderRace race, ushort estEntry) + public static FileSwap CreatePhyb(MetaFileManager manager, Func redirections, EstType type, + GenderRace race, EstEntry estEntry) { - var phybPath = GamePaths.Skeleton.Phyb.Path(race, EstManipulation.ToName(type), estEntry); + var phybPath = GamePaths.Skeleton.Phyb.Path(race, EstManipulation.ToName(type), estEntry.AsId); return FileSwap.CreateSwap(manager, ResourceType.Phyb, redirections, phybPath, phybPath); } - public static FileSwap CreateSklb(MetaFileManager manager, Func redirections, EstManipulation.EstType type, - GenderRace race, ushort estEntry) + public static FileSwap CreateSklb(MetaFileManager manager, Func redirections, EstType type, + GenderRace race, EstEntry estEntry) { - var sklbPath = GamePaths.Skeleton.Sklb.Path(race, EstManipulation.ToName(type), estEntry); + var sklbPath = GamePaths.Skeleton.Sklb.Path(race, EstManipulation.ToName(type), estEntry.AsId); return FileSwap.CreateSwap(manager, ResourceType.Sklb, redirections, sklbPath, sklbPath); } /// metaChanges is not manipulated, but IReadOnlySet does not support TryGetValue. public static MetaSwap? CreateEst(MetaFileManager manager, Func redirections, - Func manips, EstManipulation.EstType type, + Func manips, EstType type, GenderRace genderRace, PrimaryId idFrom, PrimaryId idTo, bool ownMdl) { if (type == 0) @@ -160,7 +160,7 @@ public static class ItemSwap var toDefault = new EstManipulation(gender, race, type, idTo, EstFile.GetDefault(manager, type, genderRace, idTo)); var est = new MetaSwap(manips, fromDefault, toDefault); - if (ownMdl && est.SwapApplied.Est.Entry >= 2) + if (ownMdl && est.SwapApplied.Est.Entry.Value >= 2) { var phyb = CreatePhyb(manager, redirections, type, genderRace, est.SwapApplied.Est.Entry); est.ChildSwaps.Add(phyb); diff --git a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs index 449405a0..48d687d0 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs @@ -152,9 +152,9 @@ public class ItemSwapContainer var mdl = CustomizationSwap.CreateMdl(manager, pathResolver, slot, race, from, to); var type = slot switch { - BodySlot.Hair => EstManipulation.EstType.Hair, - BodySlot.Face => EstManipulation.EstType.Face, - _ => (EstManipulation.EstType)0, + BodySlot.Hair => EstType.Hair, + BodySlot.Face => EstType.Face, + _ => (EstType)0, }; var metaResolver = MetaResolver(collection); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs index 0783dd98..b0a74637 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs @@ -453,7 +453,7 @@ public partial class ModEditWindow private static class EstRow { - private static EstManipulation _new = new(Gender.Male, ModelRace.Midlander, EstManipulation.EstType.Body, 1, 0); + private static EstManipulation _new = new(Gender.Male, ModelRace.Midlander, EstType.Body, 1, EstEntry.Zero); private static float IdWidth => 100 * UiHelpers.Scale; @@ -510,7 +510,7 @@ public partial class ModEditWindow // Values using var disabled = ImRaii.Disabled(); ImGui.TableNextColumn(); - IntDragInput("##estSkeleton", "Skeleton Index", IdWidth, _new.Entry, defaultEntry, out _, 0, ushort.MaxValue, 0.05f); + IntDragInput("##estSkeleton", "Skeleton Index", IdWidth, _new.Entry.Value, defaultEntry.Value, out _, 0, ushort.MaxValue, 0.05f); } public static void Draw(MetaFileManager metaFileManager, EstManipulation meta, ModEditor editor, Vector2 iconSize) @@ -538,9 +538,9 @@ public partial class ModEditWindow // Values var defaultEntry = EstFile.GetDefault(metaFileManager, meta.Slot, Names.CombinedRace(meta.Gender, meta.Race), meta.SetId); ImGui.TableNextColumn(); - if (IntDragInput("##estSkeleton", $"Skeleton Index\nDefault Value: {defaultEntry}", IdWidth, meta.Entry, defaultEntry, + if (IntDragInput("##estSkeleton", $"Skeleton Index\nDefault Value: {defaultEntry}", IdWidth, meta.Entry.Value, defaultEntry.Value, out var entry, 0, ushort.MaxValue, 0.05f)) - editor.MetaEditor.Change(meta.Copy((ushort)entry)); + editor.MetaEditor.Change(meta.Copy(new EstEntry((ushort)entry))); } } @@ -646,7 +646,7 @@ public partial class ModEditWindow private static class RspRow { - private static RspManipulation _new = new(SubRace.Midlander, RspAttribute.MaleMinSize, 1f); + private static RspManipulation _new = new(SubRace.Midlander, RspAttribute.MaleMinSize, RspEntry.One); private static float FloatWidth => 150 * UiHelpers.Scale; @@ -680,7 +680,8 @@ public partial class ModEditWindow using var disabled = ImRaii.Disabled(); ImGui.TableNextColumn(); ImGui.SetNextItemWidth(FloatWidth); - ImGui.DragFloat("##rspValue", ref defaultEntry, 0f); + var value = defaultEntry.Value; + ImGui.DragFloat("##rspValue", ref value, 0f); } public static void Draw(MetaFileManager metaFileManager, RspManipulation meta, ModEditor editor, Vector2 iconSize) @@ -699,15 +700,15 @@ public partial class ModEditWindow ImGui.TableNextColumn(); // Values - var def = CmpFile.GetDefault(metaFileManager, meta.SubRace, meta.Attribute); - var value = meta.Entry; + var def = CmpFile.GetDefault(metaFileManager, meta.SubRace, meta.Attribute).Value; + var value = meta.Entry.Value; ImGui.SetNextItemWidth(FloatWidth); using var color = ImRaii.PushColor(ImGuiCol.FrameBg, def < value ? ColorId.IncreasedMetaValue.Value() : ColorId.DecreasedMetaValue.Value(), def != value); - if (ImGui.DragFloat("##rspValue", ref value, 0.001f, RspManipulation.MinValue, RspManipulation.MaxValue) - && value is >= RspManipulation.MinValue and <= RspManipulation.MaxValue) - editor.MetaEditor.Change(meta.Copy(value)); + if (ImGui.DragFloat("##rspValue", ref value, 0.001f, RspEntry.MinValue, RspEntry.MaxValue) + && value is >= RspEntry.MinValue and <= RspEntry.MaxValue) + editor.MetaEditor.Change(meta.Copy(new RspEntry(value))); ImGuiUtil.HoverTooltip($"Default Value: {def:0.###}"); } diff --git a/Penumbra/UI/Classes/Combos.cs b/Penumbra/UI/Classes/Combos.cs index 253bf0e0..234f7a3e 100644 --- a/Penumbra/UI/Classes/Combos.cs +++ b/Penumbra/UI/Classes/Combos.cs @@ -33,7 +33,7 @@ public static class Combos => ImGuiUtil.GenericEnumCombo(label, unscaledWidth * UiHelpers.Scale, current, out attribute, RspAttributeExtensions.ToFullString, 0, 1); - public static bool EstSlot(string label, EstManipulation.EstType current, out EstManipulation.EstType attribute, float unscaledWidth = 200) + public static bool EstSlot(string label, EstType current, out EstType attribute, float unscaledWidth = 200) => ImGuiUtil.GenericEnumCombo(label, unscaledWidth * UiHelpers.Scale, current, out attribute); public static bool ImcType(string label, ObjectType current, out ObjectType type, float unscaledWidth = 110) diff --git a/Penumbra/Util/IdentifierExtensions.cs b/Penumbra/Util/IdentifierExtensions.cs index 7368c7c8..0647ea8e 100644 --- a/Penumbra/Util/IdentifierExtensions.cs +++ b/Penumbra/Util/IdentifierExtensions.cs @@ -51,18 +51,18 @@ public static class IdentifierExtensions case MetaManipulation.Type.Est: switch (manip.Est.Slot) { - case EstManipulation.EstType.Hair: + case EstType.Hair: changedItems.TryAdd($"Customization: {manip.Est.Race} {manip.Est.Gender} Hair (Hair) {manip.Est.SetId}", null); break; - case EstManipulation.EstType.Face: + case EstType.Face: changedItems.TryAdd($"Customization: {manip.Est.Race} {manip.Est.Gender} Face (Face) {manip.Est.SetId}", null); break; - case EstManipulation.EstType.Body: + case EstType.Body: identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(manip.Est.SetId, Names.CombinedRace(manip.Est.Gender, manip.Est.Race), EquipSlot.Body)); break; - case EstManipulation.EstType.Head: + case EstType.Head: identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(manip.Est.SetId, Names.CombinedRace(manip.Est.Gender, manip.Est.Race), EquipSlot.Head)); From 50a7e7efb7f0ea41583ab04b1dd6dd16ca2f992e Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 7 Jun 2024 16:34:09 +0200 Subject: [PATCH 212/433] Add more filter options. --- OtterGui | 2 +- .../Meta/Manipulations/ImcManipulation.cs | 1 - Penumbra/UI/ModsTab/ModFileSystemSelector.cs | 106 ++++---------- .../UI/ModsTab/ModSearchStringSplitter.cs | 138 ++++++++++++++++++ 4 files changed, 171 insertions(+), 76 deletions(-) create mode 100644 Penumbra/UI/ModsTab/ModSearchStringSplitter.cs diff --git a/OtterGui b/OtterGui index 5de708b2..ac176daf 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 5de708b27ed45c9cdead71742c7061ad9ce64323 +Subproject commit ac176daf068f42d0b04a77dbc149f68a425fd460 diff --git a/Penumbra/Meta/Manipulations/ImcManipulation.cs b/Penumbra/Meta/Manipulations/ImcManipulation.cs index eb3720c9..5065a06e 100644 --- a/Penumbra/Meta/Manipulations/ImcManipulation.cs +++ b/Penumbra/Meta/Manipulations/ImcManipulation.cs @@ -1,6 +1,5 @@ using Newtonsoft.Json; using Newtonsoft.Json.Converters; -using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.Structs; diff --git a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs index 5b6cfa99..58f0b615 100644 --- a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs +++ b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs @@ -23,17 +23,20 @@ namespace Penumbra.UI.ModsTab; public sealed class ModFileSystemSelector : FileSystemSelector { - private readonly CommunicatorService _communicator; - private readonly MessageService _messager; - private readonly Configuration _config; - private readonly FileDialogService _fileDialog; - private readonly ModManager _modManager; - private readonly CollectionManager _collectionManager; - private readonly TutorialService _tutorial; - private readonly ModImportManager _modImportManager; - private readonly IDragDropManager _dragDrop; - public ModSettings SelectedSettings { get; private set; } = ModSettings.Empty; - public ModCollection SelectedSettingCollection { get; private set; } = ModCollection.Empty; + private readonly CommunicatorService _communicator; + private readonly MessageService _messager; + private readonly Configuration _config; + private readonly FileDialogService _fileDialog; + private readonly ModManager _modManager; + private readonly CollectionManager _collectionManager; + private readonly TutorialService _tutorial; + private readonly ModImportManager _modImportManager; + private readonly IDragDropManager _dragDrop; + private readonly ModSearchStringSplitter Filter = new(); + + public ModSettings SelectedSettings { get; private set; } = ModSettings.Empty; + public ModCollection SelectedSettingCollection { get; private set; } = ModCollection.Empty; + public ModFileSystemSelector(IKeyState keyState, CommunicatorService communicator, ModFileSystem fileSystem, ModManager modManager, CollectionManager collectionManager, Configuration config, TutorialService tutorial, FileDialogService fileDialog, @@ -568,78 +571,49 @@ public sealed class ModFileSystemSelector : FileSystemSelector Appropriately identify and set the string filter and its type. protected override bool ChangeFilter(string filterValue) { - (_modFilter, _filterType) = filterValue.Length switch - { - 0 => (LowerString.Empty, -1), - > 1 when filterValue[1] == ':' => - filterValue[0] switch - { - 'n' => filterValue.Length == 2 ? (LowerString.Empty, -1) : (new LowerString(filterValue[2..]), 1), - 'N' => filterValue.Length == 2 ? (LowerString.Empty, -1) : (new LowerString(filterValue[2..]), 1), - 'a' => filterValue.Length == 2 ? (LowerString.Empty, -1) : ParseFilter(filterValue, 2), - 'A' => filterValue.Length == 2 ? (LowerString.Empty, -1) : ParseFilter(filterValue, 2), - 'c' => filterValue.Length == 2 ? (LowerString.Empty, -1) : ParseFilter(filterValue, 3), - 'C' => filterValue.Length == 2 ? (LowerString.Empty, -1) : ParseFilter(filterValue, 3), - 't' => filterValue.Length == 2 ? (LowerString.Empty, -1) : ParseFilter(filterValue, 4), - 'T' => filterValue.Length == 2 ? (LowerString.Empty, -1) : ParseFilter(filterValue, 4), - 's' => filterValue.Length == 2 ? (LowerString.Empty, -1) : ParseFilter(filterValue, 5), - 'S' => filterValue.Length == 2 ? (LowerString.Empty, -1) : ParseFilter(filterValue, 5), - _ => (new LowerString(filterValue), 0), - }, - _ => (new LowerString(filterValue), 0), - }; - + Filter.Parse(filterValue); return true; } - private const int EmptyOffset = 128; - - private (LowerString, int) ParseFilter(string value, int id) - { - value = value[2..]; - var lower = new LowerString(value); - if (id == 5 && !ChangedItemDrawer.TryParsePartial(lower.Lower, out _slotFilter)) - _slotFilter = 0; - - return (lower, lower.Lower is "none" ? id + EmptyOffset : id); - } - - /// /// Check the state filter for a specific pair of has/has-not flags. /// Uses count == 0 to check for has-not and count != 0 for has. /// Returns true if it should be filtered and false if not. /// private bool CheckFlags(int count, ModFilter hasNoFlag, ModFilter hasFlag) - { - return count switch + => count switch { 0 when _stateFilter.HasFlag(hasNoFlag) => false, 0 => true, _ when _stateFilter.HasFlag(hasFlag) => false, _ => true, }; - } /// /// The overwritten filter method also computes the state. @@ -653,7 +627,7 @@ public sealed class ModFileSystemSelector : FileSystemSelector 0 && !f.FullName().Contains(FilterValue, IgnoreCase); + || !Filter.IsVisible(f); } return ApplyFiltersAndState((ModFileSystem.Leaf)path, out state); @@ -661,23 +635,7 @@ public sealed class ModFileSystemSelector : FileSystemSelector Apply the string filters. private bool ApplyStringFilters(ModFileSystem.Leaf leaf, Mod mod) - { - return _filterType switch - { - -1 => false, - 0 => !(leaf.FullName().Contains(_modFilter.Lower, IgnoreCase) || mod.Name.Contains(_modFilter)), - 1 => !mod.Name.Contains(_modFilter), - 2 => !mod.Author.Contains(_modFilter), - 3 => !mod.LowerChangedItemsString.Contains(_modFilter.Lower), - 4 => !mod.AllTagsLower.Contains(_modFilter.Lower), - 5 => mod.ChangedItems.All(p => (ChangedItemDrawer.GetCategoryIcon(p.Key, p.Value) & _slotFilter) == 0), - 2 + EmptyOffset => !mod.Author.IsEmpty, - 3 + EmptyOffset => mod.LowerChangedItemsString.Length > 0, - 4 + EmptyOffset => mod.AllTagsLower.Length > 0, - 5 + EmptyOffset => mod.ChangedItems.Count == 0, - _ => false, // Should never happen - }; - } + => !Filter.IsVisible(leaf); /// Only get the text color for a mod if no filters are set. private ColorId GetTextColor(Mod mod, ModSettings? settings, ModCollection collection) diff --git a/Penumbra/UI/ModsTab/ModSearchStringSplitter.cs b/Penumbra/UI/ModsTab/ModSearchStringSplitter.cs new file mode 100644 index 00000000..1ea70731 --- /dev/null +++ b/Penumbra/UI/ModsTab/ModSearchStringSplitter.cs @@ -0,0 +1,138 @@ +using OtterGui.Filesystem; +using OtterGui.Filesystem.Selector; +using Penumbra.Mods; +using Penumbra.Mods.Manager; + +namespace Penumbra.UI.ModsTab; + +public enum ModSearchType : byte +{ + Default = 0, + ChangedItem, + Tag, + Name, + Author, + Category, +} + +public sealed class ModSearchStringSplitter : SearchStringSplitter.Leaf, ModSearchStringSplitter.Entry> +{ + public readonly struct Entry : ISplitterEntry + { + public string Needle { get; init; } + public ModSearchType Type { get; init; } + public ChangedItemDrawer.ChangedItemIcon IconFilter { get; init; } + + public bool Contains(Entry other) + { + if (Type != other.Type) + return false; + if (Type is ModSearchType.Category) + return IconFilter == other.IconFilter; + + return Needle.Contains(other.Needle); + } + } + + protected override bool ConvertToken(char token, out ModSearchType val) + { + val = token switch + { + 'c' or 'C' => ModSearchType.ChangedItem, + 't' or 'T' => ModSearchType.Tag, + 'n' or 'N' => ModSearchType.Name, + 'a' or 'A' => ModSearchType.Author, + 's' or 'S' => ModSearchType.Category, + _ => ModSearchType.Default, + }; + return val is not ModSearchType.Default; + } + + protected override bool AllowsNone(ModSearchType val) + => val switch + { + ModSearchType.Author => true, + ModSearchType.ChangedItem => true, + ModSearchType.Tag => true, + ModSearchType.Category => true, + _ => false, + }; + + protected override void PostProcessing() + { + base.PostProcessing(); + HandleList(General); + HandleList(Forced); + HandleList(Negated); + return; + + static void HandleList(List list) + { + for (var i = 0; i < list.Count; ++i) + { + var entry = list[i]; + if (entry.Type is not ModSearchType.Category) + continue; + + if (ChangedItemDrawer.TryParsePartial(entry.Needle, out var icon)) + list[i] = entry with + { + IconFilter = icon, + Needle = string.Empty, + }; + else + list.RemoveAt(i--); + } + } + } + + public bool IsVisible(ModFileSystem.Folder folder) + { + switch (State) + { + case FilterState.NoFilters: return true; + case FilterState.NoMatches: return false; + } + + var fullName = folder.FullName(); + return Forced.All(i => MatchesName(i, folder.Name, fullName)) + && !Negated.Any(i => MatchesName(i, folder.Name, fullName)) + && (General.Count == 0 || General.Any(i => MatchesName(i, folder.Name, fullName))); + } + + protected override bool Matches(Entry entry, ModFileSystem.Leaf leaf) + => entry.Type switch + { + ModSearchType.Default => leaf.FullName().AsSpan().Contains(entry.Needle, StringComparison.OrdinalIgnoreCase) + || leaf.Value.Name.Lower.AsSpan().Contains(entry.Needle, StringComparison.Ordinal), + ModSearchType.ChangedItem => leaf.Value.LowerChangedItemsString.AsSpan().Contains(entry.Needle, StringComparison.Ordinal), + ModSearchType.Tag => leaf.Value.AllTagsLower.AsSpan().Contains(entry.Needle, StringComparison.Ordinal), + ModSearchType.Name => leaf.Value.Name.Lower.AsSpan().Contains(entry.Needle, StringComparison.Ordinal), + ModSearchType.Author => leaf.Value.Author.Lower.AsSpan().Contains(entry.Needle, StringComparison.Ordinal), + ModSearchType.Category => leaf.Value.ChangedItems.Any(p + => (ChangedItemDrawer.GetCategoryIcon(p.Key, p.Value) & entry.IconFilter) != 0), + _ => true, + }; + + protected override bool MatchesNone(ModSearchType type, bool negated, ModFileSystem.Leaf haystack) + => type switch + { + ModSearchType.Author when negated => !haystack.Value.Author.IsEmpty, + ModSearchType.Author => haystack.Value.Author.IsEmpty, + ModSearchType.ChangedItem when negated => haystack.Value.LowerChangedItemsString.Length > 0, + ModSearchType.ChangedItem => haystack.Value.LowerChangedItemsString.Length == 0, + ModSearchType.Tag when negated => haystack.Value.AllTagsLower.Length > 0, + ModSearchType.Tag => haystack.Value.AllTagsLower.Length == 0, + ModSearchType.Category when negated => haystack.Value.ChangedItems.Count > 0, + ModSearchType.Category => haystack.Value.ChangedItems.Count == 0, + _ => true, + }; + + private static bool MatchesName(Entry entry, ReadOnlySpan name, ReadOnlySpan fullName) + => entry.Type switch + { + ModSearchType.Default => fullName.Contains(entry.Needle, StringComparison.OrdinalIgnoreCase), + ModSearchType.Name => name.Contains(entry.Needle, StringComparison.OrdinalIgnoreCase), + _ => false, + }; +} From 102e7335a7303526a6ae71c5f89194538c8c9f56 Mon Sep 17 00:00:00 2001 From: Actions User Date: Fri, 7 Jun 2024 14:40:25 +0000 Subject: [PATCH 213/433] [CI] Updating repo.json for testing_1.1.0.3 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 5e9e5b37..5d6a9ed8 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.0.2", - "TestingAssemblyVersion": "1.1.0.2", + "TestingAssemblyVersion": "1.1.0.3", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -18,7 +18,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.2/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.1.0.3/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From de0309bfa7abcc925209086cbf88360bfaee1ee0 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 8 Jun 2024 12:45:12 +0200 Subject: [PATCH 214/433] Fix issue with ImcGroup settings and IPC. --- Penumbra/Api/Api/ModSettingsApi.cs | 27 +++++++++++++------------- Penumbra/Mods/Manager/ModDataEditor.cs | 6 ++---- Penumbra/Mods/Settings/ModSettings.cs | 4 ++-- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/Penumbra/Api/Api/ModSettingsApi.cs b/Penumbra/Api/Api/ModSettingsApi.cs index 56b80e63..e046ce30 100644 --- a/Penumbra/Api/Api/ModSettingsApi.cs +++ b/Penumbra/Api/Api/ModSettingsApi.cs @@ -77,10 +77,10 @@ public class ModSettingsApi : IPenumbraApiModSettings, IApiService, IDisposable if (!_collectionManager.Storage.ById(collectionId, out var collection)) return (PenumbraApiEc.CollectionMissing, null); - var settings = collection.Id == Guid.Empty - ? null - : ignoreInheritance - ? collection.Settings[mod.Index] + var settings = collection.Id == Guid.Empty + ? null + : ignoreInheritance + ? collection.Settings[mod.Index] : collection[mod.Index].Settings; if (settings == null) return (PenumbraApiEc.Success, null); @@ -160,11 +160,11 @@ public class ModSettingsApi : IPenumbraApiModSettings, IApiService, IDisposable if (optionIdx < 0) return ApiHelpers.Return(PenumbraApiEc.OptionMissing, args); - var setting = mod.Groups[groupIdx] switch + var setting = mod.Groups[groupIdx].Behaviour switch { - MultiModGroup => Setting.Multi(optionIdx), - SingleModGroup => Setting.Single(optionIdx), - _ => Setting.Zero, + GroupDrawBehaviour.MultiSelection => Setting.Multi(optionIdx), + GroupDrawBehaviour.SingleSelection => Setting.Single(optionIdx), + _ => Setting.Zero, }; var ret = _collectionEditor.SetModSetting(collection, mod, groupIdx, setting) ? PenumbraApiEc.Success @@ -191,20 +191,20 @@ public class ModSettingsApi : IPenumbraApiModSettings, IApiService, IDisposable var setting = Setting.Zero; switch (mod.Groups[groupIdx]) { - case SingleModGroup single: + case { Behaviour: GroupDrawBehaviour.SingleSelection } single: { - var optionIdx = optionNames.Count == 0 ? -1 : single.OptionData.IndexOf(o => o.Name == optionNames[^1]); + var optionIdx = optionNames.Count == 0 ? -1 : single.Options.IndexOf(o => o.Name == optionNames[^1]); if (optionIdx < 0) return ApiHelpers.Return(PenumbraApiEc.OptionMissing, args); setting = Setting.Single(optionIdx); break; } - case MultiModGroup multi: + case { Behaviour: GroupDrawBehaviour.MultiSelection } multi: { foreach (var name in optionNames) { - var optionIdx = multi.OptionData.IndexOf(o => o.Mod.Name == name); + var optionIdx = multi.Options.IndexOf(o => o.Name == name); if (optionIdx < 0) return ApiHelpers.Return(PenumbraApiEc.OptionMissing, args); @@ -256,7 +256,8 @@ public class ModSettingsApi : IPenumbraApiModSettings, IApiService, IDisposable private void OnModSettingChange(ModCollection collection, ModSettingChange type, Mod? mod, Setting _1, int _2, bool inherited) => ModSettingChanged?.Invoke(type, collection.Id, mod?.ModPath.Name ?? string.Empty, inherited); - private void OnModOptionEdited(ModOptionChangeType type, Mod mod, IModGroup? group, IModOption? option, IModDataContainer? container, int moveIndex) + private void OnModOptionEdited(ModOptionChangeType type, Mod mod, IModGroup? group, IModOption? option, IModDataContainer? container, + int moveIndex) { switch (type) { diff --git a/Penumbra/Mods/Manager/ModDataEditor.cs b/Penumbra/Mods/Manager/ModDataEditor.cs index e0af6f36..c7c7c2cc 100644 --- a/Penumbra/Mods/Manager/ModDataEditor.cs +++ b/Penumbra/Mods/Manager/ModDataEditor.cs @@ -50,7 +50,6 @@ public class ModDataEditor(SaveService saveService, CommunicatorService communic var save = true; if (File.Exists(dataFile)) { - save = false; try { var text = File.ReadAllText(dataFile); @@ -60,6 +59,7 @@ public class ModDataEditor(SaveService saveService, CommunicatorService communic favorite = json[nameof(Mod.Favorite)]?.Value() ?? favorite; note = json[nameof(Mod.Note)]?.Value() ?? note; localTags = json[nameof(Mod.LocalTags)]?.Values().OfType() ?? localTags; + save = false; } catch (Exception e) { @@ -239,7 +239,6 @@ public class ModDataEditor(SaveService saveService, CommunicatorService communic mod.Favorite = state; saveService.QueueSave(new ModLocalData(mod)); - ; communicatorService.ModDataChanged.Invoke(ModDataChangeType.Favorite, mod, null); } @@ -250,7 +249,6 @@ public class ModDataEditor(SaveService saveService, CommunicatorService communic mod.Note = newNote; saveService.QueueSave(new ModLocalData(mod)); - ; communicatorService.ModDataChanged.Invoke(ModDataChangeType.Favorite, mod, null); } @@ -260,7 +258,7 @@ public class ModDataEditor(SaveService saveService, CommunicatorService communic if (tagIdx < 0 || tagIdx > which.Count) return; - ModDataChangeType flags = 0; + ModDataChangeType flags; if (tagIdx == which.Count) { flags = ModLocalData.UpdateTags(mod, local ? null : which.Append(newTag), local ? which.Append(newTag) : null); diff --git a/Penumbra/Mods/Settings/ModSettings.cs b/Penumbra/Mods/Settings/ModSettings.cs index 7fe48365..25e4805d 100644 --- a/Penumbra/Mods/Settings/ModSettings.cs +++ b/Penumbra/Mods/Settings/ModSettings.cs @@ -185,10 +185,10 @@ public class ModSettings switch (mod.Groups[idx]) { - case SingleModGroup single when setting.Value < (ulong)single.Options.Count: + case { Behaviour: GroupDrawBehaviour.SingleSelection } single when setting.Value < (ulong)single.Options.Count: dict.Add(single.Name, [single.Options[setting.AsIndex].Name]); break; - case MultiModGroup multi: + case { Behaviour: GroupDrawBehaviour.MultiSelection } multi: var list = multi.Options.WithIndex().Where(p => setting.HasFlag(p.Index)).Select(p => p.Value.Name).ToList(); dict.Add(multi.Name, list); break; From e884b269a9ca3f2ea4c6ef63cf7da9e53d1353cb Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 8 Jun 2024 14:55:42 +0200 Subject: [PATCH 215/433] Add a version field to mod group files. --- Penumbra/Mods/Groups/ModSaveGroup.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Penumbra/Mods/Groups/ModSaveGroup.cs b/Penumbra/Mods/Groups/ModSaveGroup.cs index 05437e3d..c82c67c7 100644 --- a/Penumbra/Mods/Groups/ModSaveGroup.cs +++ b/Penumbra/Mods/Groups/ModSaveGroup.cs @@ -6,6 +6,8 @@ namespace Penumbra.Mods.Groups; public readonly struct ModSaveGroup : ISavable { + public const int CurrentVersion = 0; + private readonly DirectoryInfo _basePath; private readonly IModGroup? _group; private readonly int _groupIdx; @@ -71,6 +73,8 @@ public readonly struct ModSaveGroup : ISavable j.Formatting = Formatting.Indented; var serializer = new JsonSerializer { Formatting = Formatting.Indented }; j.WriteStartObject(); + j.WritePropertyName("Version"); + j.WriteValue(CurrentVersion); if (_groupIdx >= 0) _group!.WriteJson(j, serializer, _basePath); else From 159942f29c229f3dafef7275c553cc9f7e2183b6 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 8 Jun 2024 20:33:07 +0200 Subject: [PATCH 216/433] Add by-name identification in the lobby. --- .../PathResolving/CollectionResolver.cs | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/Penumbra/Interop/PathResolving/CollectionResolver.cs b/Penumbra/Interop/PathResolving/CollectionResolver.cs index aea58304..ed111794 100644 --- a/Penumbra/Interop/PathResolving/CollectionResolver.cs +++ b/Penumbra/Interop/PathResolving/CollectionResolver.cs @@ -1,5 +1,6 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using FFXIVClientStructs.FFXIV.Client.UI.Agent; using OtterGui.Services; using Penumbra.Collections; using Penumbra.Collections.Manager; @@ -7,6 +8,8 @@ using Penumbra.GameData.Actors; using Penumbra.GameData.DataContainers; using Penumbra.GameData.Enums; using Penumbra.GameData.Interop; +using Penumbra.GameData.Structs; +using Penumbra.String; using Penumbra.Util; using Character = FFXIVClientStructs.FFXIV.Client.Game.Character.Character; using GameObject = FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject; @@ -94,14 +97,8 @@ public sealed unsafe class CollectionResolver( public bool IsModelHuman(uint modelCharaId) => humanModels.IsHuman(modelCharaId); - /// Return whether the given character has a human model. - public bool IsModelHuman(Character* character) - => character != null && IsModelHuman((uint)character->CharacterData.ModelCharaId); - /// - /// Used if on the Login screen. Names are populated after actors are drawn, - /// so it is not possible to fetch names from the ui list. - /// Actors are also not named. So use Yourself > Players > Racial > Default. + /// Used if on the Login screen. /// private bool LoginScreen(GameObject* gameObject, out ResolveData ret) { @@ -114,6 +111,27 @@ public sealed unsafe class CollectionResolver( } var notYetReady = false; + var lobby = AgentLobby.Instance(); + if (lobby != null) + { + var span = lobby->LobbyData.CharaSelectEntries.Span; + // The lobby uses the first 8 cutscene actors. + var idx = gameObject->ObjectIndex - ObjectIndex.CutsceneStart.Index; + if (idx >= 0 && idx < span.Length && span[idx].Value != null) + { + var item = span[idx].Value; + var identifier = actors.CreatePlayer(new ByteString(item->Name), item->HomeWorldId); + Penumbra.Log.Verbose( + $"Identified {identifier.Incognito(null)} in cutscene for actor {idx + 200} at 0x{(ulong)gameObject:X} of race {(gameObject->IsCharacter() ? ((Character*)gameObject)->DrawData.CustomizeData.Race.ToString() : "Unknown")}."); + if (identifier.IsValid && CollectionByIdentifier(identifier) is { } coll) + { + // Do not add this to caches because game objects are reused for different draw objects. + ret = coll.ToResolveData(gameObject); + return true; + } + } + } + var collection = collectionManager.Active.ByType(CollectionType.Yourself) ?? CollectionByAttributes(gameObject, ref notYetReady) ?? collectionManager.Active.Default; @@ -189,7 +207,7 @@ public sealed unsafe class CollectionResolver( return null; // Only handle human models. - + if (!IsModelHuman((uint)actor.AsCharacter->CharacterData.ModelCharaId)) return null; From f51fc2cafd985cf3b3cca605f74b85639cf8036f Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 9 Jun 2024 22:05:19 +0200 Subject: [PATCH 217/433] Allow root directory overwriting with case sensitivity. --- Penumbra/Mods/Manager/ModManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Mods/Manager/ModManager.cs b/Penumbra/Mods/Manager/ModManager.cs index adaca85e..010cad19 100644 --- a/Penumbra/Mods/Manager/ModManager.cs +++ b/Penumbra/Mods/Manager/ModManager.cs @@ -266,7 +266,7 @@ public sealed class ModManager : ModStorage, IDisposable /// private void SetBaseDirectory(string newPath, bool firstTime) { - if (!firstTime && string.Equals(newPath, _config.ModDirectory, StringComparison.OrdinalIgnoreCase)) + if (!firstTime && string.Equals(newPath, _config.ModDirectory, StringComparison.Ordinal)) return; if (newPath.Length == 0) From f6b35497c5610acc00663ea31e9c69190ff3e357 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 9 Jun 2024 22:05:37 +0200 Subject: [PATCH 218/433] Change path comparison for AddMod. --- Penumbra/Api/Api/ModsApi.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Api/Api/ModsApi.cs b/Penumbra/Api/Api/ModsApi.cs index 16dd8be9..548831d5 100644 --- a/Penumbra/Api/Api/ModsApi.cs +++ b/Penumbra/Api/Api/ModsApi.cs @@ -75,7 +75,7 @@ public class ModsApi : IPenumbraApiMods, IApiService, IDisposable if (!dir.Exists) return ApiHelpers.Return(PenumbraApiEc.FileMissing, args); - if (_modManager.BasePath.FullName != dir.Parent?.FullName) + if (dir.Parent == null || Path.GetFullPath(_modManager.BasePath.FullName) != Path.GetFullPath(dir.Parent.FullName)) return ApiHelpers.Return(PenumbraApiEc.InvalidArgument, args); _modManager.AddMod(dir); From f7adc83d631936bf76446af1205a333baa260c44 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 9 Jun 2024 22:06:18 +0200 Subject: [PATCH 219/433] Fix issue with preview of file in advanced model editing. --- .../UI/AdvancedWindow/ModEditWindow.Models.cs | 110 ++++++++++-------- 1 file changed, 61 insertions(+), 49 deletions(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index a7d39c6e..bbed64b7 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -1,5 +1,4 @@ using Dalamud.Interface; -using Dalamud.Interface.Components; using ImGuiNET; using Lumina.Data.Parsing; using OtterGui; @@ -27,44 +26,56 @@ public partial class ModEditWindow private readonly FileEditor _modelTab; private readonly ModelManager _models; + private class LoadedData + { + public MdlFile LastFile = null!; + public readonly List SubMeshAttributeTags = []; + public long[] LodTriCount = []; + } + private string _modelNewMaterial = string.Empty; - private readonly List _subMeshAttributeTagWidgets = []; + + private readonly LoadedData _main = new(); + private readonly LoadedData _preview = new(); + private string _customPath = string.Empty; private Utf8GamePath _customGamePath = Utf8GamePath.Empty; - private MdlFile _lastFile = null!; - private long[] _lodTriCount = []; - private void UpdateFile(MdlFile file, bool force) + + + private LoadedData UpdateFile(MdlFile file, bool force, bool disabled) { - if (file == _lastFile && !force) - return; + var data = disabled ? _preview : _main; + if (file == data.LastFile && !force) + return data; - _lastFile = file; + data.LastFile = file; var subMeshTotal = file.Meshes.Aggregate(0, (count, mesh) => count + mesh.SubMeshCount); - if (_subMeshAttributeTagWidgets.Count != subMeshTotal) + if (data.SubMeshAttributeTags.Count != subMeshTotal) { - _subMeshAttributeTagWidgets.Clear(); - _subMeshAttributeTagWidgets.AddRange( + data.SubMeshAttributeTags.Clear(); + data.SubMeshAttributeTags.AddRange( Enumerable.Range(0, subMeshTotal).Select(_ => new TagButtons()) ); } - _lodTriCount = Enumerable.Range(0, file.Lods.Length).Select(l => GetTriangleCountForLod(file, l)).ToArray(); + data.LodTriCount = Enumerable.Range(0, file.Lods.Length).Select(l => GetTriangleCountForLod(file, l)).ToArray(); + return data; } private bool DrawModelPanel(MdlTab tab, bool disabled) { var ret = tab.Dirty; - UpdateFile(tab.Mdl, ret); + var data = UpdateFile(tab.Mdl, ret, disabled); DrawImportExport(tab, disabled); ret |= DrawModelMaterialDetails(tab, disabled); - if (ImGui.CollapsingHeader($"Meshes ({_lastFile.Meshes.Length})###meshes")) - for (var i = 0; i < _lastFile.LodCount; ++i) + if (ImGui.CollapsingHeader($"Meshes ({data.LastFile.Meshes.Length})###meshes")) + for (var i = 0; i < data.LastFile.LodCount; ++i) ret |= DrawModelLodDetails(tab, i, disabled); - ret |= DrawOtherModelDetails(disabled); + ret |= DrawOtherModelDetails(data); return !disabled && ret; } @@ -98,7 +109,7 @@ public partial class ModEditWindow return true; }); - using (var frame = ImRaii.FramedGroup("Import", size, headerPreIcon: FontAwesomeIcon.FileImport)) + using (ImRaii.FramedGroup("Import", size, headerPreIcon: FontAwesomeIcon.FileImport)) { ImGui.Checkbox("Keep current materials", ref tab.ImportKeepMaterials); ImGui.Checkbox("Keep current attributes", ref tab.ImportKeepAttributes); @@ -232,7 +243,7 @@ public partial class ModEditWindow if (!ImGui.InputTextWithHint("##customInput", "Enter custom game path...", ref _customPath, 256)) return; - if (!Utf8GamePath.FromString(_customPath, out _customGamePath, false)) + if (!Utf8GamePath.FromString(_customPath, out _customGamePath)) _customGamePath = Utf8GamePath.Empty; } @@ -399,9 +410,9 @@ public partial class ModEditWindow return ret; } - private void DrawInvalidMaterialMarker() + private static void DrawInvalidMaterialMarker() { - using (var font = ImRaii.PushFont(UiBuilder.IconFont)) + using (ImRaii.PushFont(UiBuilder.IconFont)) ImGuiUtil.TextColored(0xFF0000FF, FontAwesomeIcon.TimesCircle.ToIconString()); ImGuiUtil.HoverTooltip( @@ -534,7 +545,8 @@ public partial class ModEditWindow ImGui.TextUnformatted($"Attributes #{subMeshOffset + 1}"); ImGui.TableNextColumn(); - var widget = _subMeshAttributeTagWidgets[subMeshIndex]; + var data = disabled ? _preview : _main; + var widget = data.SubMeshAttributeTags[subMeshIndex]; var attributes = tab.GetSubMeshAttributes(subMeshIndex); if (attributes == null) @@ -555,7 +567,7 @@ public partial class ModEditWindow return true; } - private bool DrawOtherModelDetails(bool _) + private bool DrawOtherModelDetails(LoadedData data) { using var header = ImRaii.CollapsingHeader("Further Content"); if (!header) @@ -566,44 +578,44 @@ public partial class ModEditWindow if (table) { ImGuiUtil.DrawTableColumn("Version"); - ImGuiUtil.DrawTableColumn($"0x{_lastFile.Version:X}"); + ImGuiUtil.DrawTableColumn($"0x{data.LastFile.Version:X}"); ImGuiUtil.DrawTableColumn("Radius"); - ImGuiUtil.DrawTableColumn(_lastFile.Radius.ToString(CultureInfo.InvariantCulture)); + ImGuiUtil.DrawTableColumn(data.LastFile.Radius.ToString(CultureInfo.InvariantCulture)); ImGuiUtil.DrawTableColumn("Model Clip Out Distance"); - ImGuiUtil.DrawTableColumn(_lastFile.ModelClipOutDistance.ToString(CultureInfo.InvariantCulture)); + ImGuiUtil.DrawTableColumn(data.LastFile.ModelClipOutDistance.ToString(CultureInfo.InvariantCulture)); ImGuiUtil.DrawTableColumn("Shadow Clip Out Distance"); - ImGuiUtil.DrawTableColumn(_lastFile.ShadowClipOutDistance.ToString(CultureInfo.InvariantCulture)); + ImGuiUtil.DrawTableColumn(data.LastFile.ShadowClipOutDistance.ToString(CultureInfo.InvariantCulture)); ImGuiUtil.DrawTableColumn("LOD Count"); - ImGuiUtil.DrawTableColumn(_lastFile.LodCount.ToString()); + ImGuiUtil.DrawTableColumn(data.LastFile.LodCount.ToString()); ImGuiUtil.DrawTableColumn("Enable Index Buffer Streaming"); - ImGuiUtil.DrawTableColumn(_lastFile.EnableIndexBufferStreaming.ToString()); + ImGuiUtil.DrawTableColumn(data.LastFile.EnableIndexBufferStreaming.ToString()); ImGuiUtil.DrawTableColumn("Enable Edge Geometry"); - ImGuiUtil.DrawTableColumn(_lastFile.EnableEdgeGeometry.ToString()); + ImGuiUtil.DrawTableColumn(data.LastFile.EnableEdgeGeometry.ToString()); ImGuiUtil.DrawTableColumn("Flags 1"); - ImGuiUtil.DrawTableColumn(_lastFile.Flags1.ToString()); + ImGuiUtil.DrawTableColumn(data.LastFile.Flags1.ToString()); ImGuiUtil.DrawTableColumn("Flags 2"); - ImGuiUtil.DrawTableColumn(_lastFile.Flags2.ToString()); + ImGuiUtil.DrawTableColumn(data.LastFile.Flags2.ToString()); ImGuiUtil.DrawTableColumn("Vertex Declarations"); - ImGuiUtil.DrawTableColumn(_lastFile.VertexDeclarations.Length.ToString()); + ImGuiUtil.DrawTableColumn(data.LastFile.VertexDeclarations.Length.ToString()); ImGuiUtil.DrawTableColumn("Bone Bounding Boxes"); - ImGuiUtil.DrawTableColumn(_lastFile.BoneBoundingBoxes.Length.ToString()); + ImGuiUtil.DrawTableColumn(data.LastFile.BoneBoundingBoxes.Length.ToString()); ImGuiUtil.DrawTableColumn("Bone Tables"); - ImGuiUtil.DrawTableColumn(_lastFile.BoneTables.Length.ToString()); + ImGuiUtil.DrawTableColumn(data.LastFile.BoneTables.Length.ToString()); ImGuiUtil.DrawTableColumn("Element IDs"); - ImGuiUtil.DrawTableColumn(_lastFile.ElementIds.Length.ToString()); + ImGuiUtil.DrawTableColumn(data.LastFile.ElementIds.Length.ToString()); ImGuiUtil.DrawTableColumn("Extra LoDs"); - ImGuiUtil.DrawTableColumn(_lastFile.ExtraLods.Length.ToString()); + ImGuiUtil.DrawTableColumn(data.LastFile.ExtraLods.Length.ToString()); ImGuiUtil.DrawTableColumn("Meshes"); - ImGuiUtil.DrawTableColumn(_lastFile.Meshes.Length.ToString()); + ImGuiUtil.DrawTableColumn(data.LastFile.Meshes.Length.ToString()); ImGuiUtil.DrawTableColumn("Shape Meshes"); - ImGuiUtil.DrawTableColumn(_lastFile.ShapeMeshes.Length.ToString()); + ImGuiUtil.DrawTableColumn(data.LastFile.ShapeMeshes.Length.ToString()); ImGuiUtil.DrawTableColumn("LoDs"); - ImGuiUtil.DrawTableColumn(_lastFile.Lods.Length.ToString()); + ImGuiUtil.DrawTableColumn(data.LastFile.Lods.Length.ToString()); ImGuiUtil.DrawTableColumn("Vertex Declarations"); - ImGuiUtil.DrawTableColumn(_lastFile.VertexDeclarations.Length.ToString()); + ImGuiUtil.DrawTableColumn(data.LastFile.VertexDeclarations.Length.ToString()); ImGuiUtil.DrawTableColumn("Stack Size"); - ImGuiUtil.DrawTableColumn(_lastFile.StackSize.ToString()); - foreach (var (triCount, lod) in _lodTriCount.WithIndex()) + ImGuiUtil.DrawTableColumn(data.LastFile.StackSize.ToString()); + foreach (var (triCount, lod) in data.LodTriCount.WithIndex()) { ImGuiUtil.DrawTableColumn($"LOD #{lod + 1} Triangle Count"); ImGuiUtil.DrawTableColumn(triCount.ToString()); @@ -614,36 +626,36 @@ public partial class ModEditWindow using (var materials = ImRaii.TreeNode("Materials", ImGuiTreeNodeFlags.DefaultOpen)) { if (materials) - foreach (var material in _lastFile.Materials) + foreach (var material in data.LastFile.Materials) ImRaii.TreeNode(material, ImGuiTreeNodeFlags.Leaf).Dispose(); } using (var attributes = ImRaii.TreeNode("Attributes", ImGuiTreeNodeFlags.DefaultOpen)) { if (attributes) - foreach (var attribute in _lastFile.Attributes) + foreach (var attribute in data.LastFile.Attributes) ImRaii.TreeNode(attribute, ImGuiTreeNodeFlags.Leaf).Dispose(); } using (var bones = ImRaii.TreeNode("Bones", ImGuiTreeNodeFlags.DefaultOpen)) { if (bones) - foreach (var bone in _lastFile.Bones) + foreach (var bone in data.LastFile.Bones) ImRaii.TreeNode(bone, ImGuiTreeNodeFlags.Leaf).Dispose(); } using (var shapes = ImRaii.TreeNode("Shapes", ImGuiTreeNodeFlags.DefaultOpen)) { if (shapes) - foreach (var shape in _lastFile.Shapes) + foreach (var shape in data.LastFile.Shapes) ImRaii.TreeNode(shape.ShapeName, ImGuiTreeNodeFlags.Leaf).Dispose(); } - if (_lastFile.RemainingData.Length > 0) + if (data.LastFile.RemainingData.Length > 0) { - using var t = ImRaii.TreeNode($"Additional Data (Size: {_lastFile.RemainingData.Length})###AdditionalData"); + using var t = ImRaii.TreeNode($"Additional Data (Size: {data.LastFile.RemainingData.Length})###AdditionalData"); if (t) - Widget.DrawHexViewer(_lastFile.RemainingData); + Widget.DrawHexViewer(data.LastFile.RemainingData); } return false; From ecd5752d16d14f64ed15775a43459b2006c78593 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 9 Jun 2024 22:12:55 +0200 Subject: [PATCH 220/433] Make imc attribute letter tooltip appear on disabled. --- Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs index d346e05c..5c8edce6 100644 --- a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs @@ -148,7 +148,7 @@ public readonly struct ImcModGroupEditDrawer(ModGroupEditDrawer editor, ImcModGr } } - ImUtf8.HoverTooltip("ABCDEFGHIJ"u8.Slice(i, 1)); + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, "ABCDEFGHIJ"u8.Slice(i, 1)); if (i != 9) ImUtf8.SameLineInner(); } From 30a87e3f402495517d067235f8235fbf46c3ba81 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 11 Jun 2024 12:28:33 +0200 Subject: [PATCH 221/433] Update valid world check. --- OtterGui | 2 +- Penumbra.GameData | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/OtterGui b/OtterGui index ac176daf..e95c0f04 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit ac176daf068f42d0b04a77dbc149f68a425fd460 +Subproject commit e95c0f04edc7e85aea67498fd8bf495a7fe6d3c8 diff --git a/Penumbra.GameData b/Penumbra.GameData index ad12ddce..6aeae346 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit ad12ddcef38a9ed4e4dd7424d748f41c4b97db10 +Subproject commit 6aeae346332a255b7575ccfca554afb0f3cf1494 From 863a7edf0ee57c2e5abadb59cad8cd4d85e5272d Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 11 Jun 2024 12:50:13 +0200 Subject: [PATCH 222/433] Add changelog. --- Penumbra/Penumbra.cs | 1 - Penumbra/UI/Changelog.cs | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index 42be0aa3..3bbfdf65 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -20,7 +20,6 @@ using ChangedItemHover = Penumbra.Communication.ChangedItemHover; using OtterGui.Tasks; using Penumbra.GameData.Enums; using Penumbra.UI; -using IPenumbraApi = Penumbra.Api.Api.IPenumbraApi; using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManager; namespace Penumbra; diff --git a/Penumbra/UI/Changelog.cs b/Penumbra/UI/Changelog.cs index 3f5a446a..184633f2 100644 --- a/Penumbra/UI/Changelog.cs +++ b/Penumbra/UI/Changelog.cs @@ -50,10 +50,39 @@ public class PenumbraChangelog AddDummy(Changelog); AddDummy(Changelog); Add1_1_0_0(Changelog); - } - + Add1_1_1_0(Changelog); + } + #region Changelogs + private static void Add1_1_1_0(Changelog log) + => log.NextVersion("Version 1.1.1.0") + .RegisterHighlight("Filtering for mods is now tokenized and can filter for multiple things at once, or exclude specific things.") + .RegisterEntry("Hover over the filter to see the new available options in the tooltip.", 1) + .RegisterEntry("Be aware that the tokenization changed the prior behavior slightly.", 1) + .RegisterEntry("This is open to improvements, if you have any ideas, let me know!", 1) + .RegisterHighlight("Added initial identification of characters in the login-screen by name.") + .RegisterEntry("Those characters can not be redrawn and re-use some things, so this may not always behave as expected, but should work in general. Let me know if you encounter edge cases!", 1) + .RegisterEntry("Added functionality for IMC groups to apply to all variants for a model instead of a specific one.") + .RegisterEntry("Improved the resource tree view with filters and incognito mode. (by Ny)") + .RegisterEntry("Added a tooltip to the global EQP condition.") + .RegisterEntry("Fixed the new worlds not being identified correctly because Square Enix could not be bothered to turn them public.") + .RegisterEntry("Fixed model import getting stuck when doing weight adjustments. (by ackwell)") + .RegisterEntry("Fixed an issue with dye previews in the material editor not applying.") + .RegisterEntry("Fixed an issue with collections not saving on renames.") + .RegisterEntry("Fixed an issue parsing collections with settings set to negative values, which should now be set to 0.") + .RegisterEntry("Fixed an issue with the accessory VFX addition.") + .RegisterEntry("Fixed an issue with GMP animation type entries.") + .RegisterEntry("Fixed another issue with the mod merger.") + .RegisterEntry("Fixed an issue with IMC groups and IPC.") + .RegisterEntry("Fixed some issues with the capitalization of the root directory.") + .RegisterEntry("Fixed IMC attribute tooltips not appearing for disabled checkboxes.") + .RegisterEntry("Added GetChangedItems IPC for single mods. (1.1.0.2)") + .RegisterEntry("Fixed an issue with creating unnamed collections. (1.1.0.2)") + .RegisterEntry("Fixed an issue with the mod merger. (1.1.0.2)") + .RegisterEntry("Fixed the global EQP entry for rings checking for bracelets instead of rings. (1.1.0.2)") + .RegisterEntry("Fixed an issue with newly created collections not being added to the collection list. (1.1.0.1)"); + private static void Add1_1_0_0(Changelog log) => log.NextVersion("Version 1.1.0.0") .RegisterImportant( From c8ea33f8dddff7a8ee7bc92791a54c3d20c45f12 Mon Sep 17 00:00:00 2001 From: Actions User Date: Tue, 11 Jun 2024 10:52:49 +0000 Subject: [PATCH 223/433] [CI] Updating repo.json for 1.1.1.0 --- repo.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/repo.json b/repo.json index 5d6a9ed8..05de7ec7 100644 --- a/repo.json +++ b/repo.json @@ -5,8 +5,8 @@ "Punchline": "Runtime mod loader and manager.", "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", - "AssemblyVersion": "1.1.0.2", - "TestingAssemblyVersion": "1.1.0.3", + "AssemblyVersion": "1.1.1.0", + "TestingAssemblyVersion": "1.1.1.0", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -17,9 +17,9 @@ "LoadPriority": 69420, "LoadRequiredState": 2, "LoadSync": true, - "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.1.0.3/Penumbra.zip", - "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.0.2/Penumbra.zip", + "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.0/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.0/Penumbra.zip", + "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } ] From 447735f6091db767d5d06a39cb8959b3aaebc279 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 11 Jun 2024 16:32:29 +0200 Subject: [PATCH 224/433] Add a configuration to disable showing mods in the lobby and at the aesthetician. --- Penumbra/Configuration.cs | 1 + .../Interop/PathResolving/CollectionResolver.cs | 13 +++++++++++++ Penumbra/UI/Tabs/SettingsTab.cs | 3 +++ 3 files changed, 17 insertions(+) diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index b81e84d8..02286cc7 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -41,6 +41,7 @@ public class Configuration : IPluginConfiguration, ISavable public bool HideUiWhenUiHidden { get; set; } = false; public bool UseDalamudUiTextureRedirection { get; set; } = true; + public bool ShowModsInLobby { get; set; } = true; public bool UseCharacterCollectionInMainWindow { get; set; } = true; public bool UseCharacterCollectionsInCards { get; set; } = true; public bool UseCharacterCollectionInInspect { get; set; } = true; diff --git a/Penumbra/Interop/PathResolving/CollectionResolver.cs b/Penumbra/Interop/PathResolving/CollectionResolver.cs index ed111794..b42571ac 100644 --- a/Penumbra/Interop/PathResolving/CollectionResolver.cs +++ b/Penumbra/Interop/PathResolving/CollectionResolver.cs @@ -1,6 +1,7 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using FFXIVClientStructs.FFXIV.Client.UI.Agent; +using Microsoft.VisualBasic; using OtterGui.Services; using Penumbra.Collections; using Penumbra.Collections.Manager; @@ -110,6 +111,12 @@ public sealed unsafe class CollectionResolver( return false; } + if (!config.ShowModsInLobby) + { + ret = ModCollection.Empty.ToResolveData(gameObject); + return true; + } + var notYetReady = false; var lobby = AgentLobby.Instance(); if (lobby != null) @@ -148,6 +155,12 @@ public sealed unsafe class CollectionResolver( return false; } + if (!config.ShowModsInLobby) + { + ret = ModCollection.Empty.ToResolveData(gameObject); + return true; + } + var player = actors.GetCurrentPlayer(); var notYetReady = false; var collection = (player.IsValid ? CollectionByIdentifier(player) : null) diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index 30384538..9989f90a 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -449,6 +449,9 @@ public class SettingsTab : ITab Checkbox("Use Interface Collection for other Plugin UIs", "Use the collection assigned to your interface for other plugins requesting UI-textures and icons through Dalamud.", _dalamudSubstitutionProvider.Enabled, _dalamudSubstitutionProvider.Set); + Checkbox($"Use {TutorialService.AssignedCollections} in Lobby", + "If this is disabled, no mods are applied to characters in the lobby or at the aesthetician.", + _config.ShowModsInLobby, v => _config.ShowModsInLobby = v); Checkbox($"Use {TutorialService.AssignedCollections} in Character Window", "Use the individual collection for your characters name or the Your Character collection in your main character window, if it is set.", _config.UseCharacterCollectionInMainWindow, v => _config.UseCharacterCollectionInMainWindow = v); From 2346b7588a49049012f240eec2c1cad912c4b86c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 14 Jun 2024 13:39:25 +0200 Subject: [PATCH 225/433] Fix GMP bug. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 6aeae346..0a2e2650 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 6aeae346332a255b7575ccfca554afb0f3cf1494 +Subproject commit 0a2e2650d693d1bba267498f96112682cc09dbab From 532e8a0936acd38cdfe1701bc62c3f57d06c58ff Mon Sep 17 00:00:00 2001 From: Actions User Date: Fri, 14 Jun 2024 12:11:16 +0000 Subject: [PATCH 226/433] [CI] Updating repo.json for 1.1.1.1 --- repo.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/repo.json b/repo.json index 05de7ec7..318aafc2 100644 --- a/repo.json +++ b/repo.json @@ -5,8 +5,8 @@ "Punchline": "Runtime mod loader and manager.", "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", - "AssemblyVersion": "1.1.1.0", - "TestingAssemblyVersion": "1.1.1.0", + "AssemblyVersion": "1.1.1.1", + "TestingAssemblyVersion": "1.1.1.1", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -17,9 +17,9 @@ "LoadPriority": 69420, "LoadRequiredState": 2, "LoadSync": true, - "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.0/Penumbra.zip", - "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.0/Penumbra.zip", + "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.1/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.1/Penumbra.zip", + "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.1/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } ] From b1a059038221960133882e7b45dc697efd694981 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 14 Jun 2024 23:08:04 +0200 Subject: [PATCH 227/433] Make modmerger file lookup case insensitive. --- Penumbra/Mods/Editor/ModMerger.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Penumbra/Mods/Editor/ModMerger.cs b/Penumbra/Mods/Editor/ModMerger.cs index 8d47051c..74f182d3 100644 --- a/Penumbra/Mods/Editor/ModMerger.cs +++ b/Penumbra/Mods/Editor/ModMerger.cs @@ -196,7 +196,7 @@ public class ModMerger : IDisposable { if (fromFileToFile) { - if (!_fileToFile.TryGetValue(input.FullName, out var s)) + if (!_fileToFile.TryGetValue(input.FullName.ToLowerInvariant(), out var s)) { ret = input; return false; @@ -238,7 +238,7 @@ public class ModMerger : IDisposable Directory.CreateDirectory(finalDir); file.CopyTo(path); Penumbra.Log.Verbose($"[Merger] Copied file {file.FullName} to {path}."); - _fileToFile.Add(file.FullName, path); + _fileToFile.Add(file.FullName.ToLowerInvariant(), path); } } From ec207bdba2c01448250f6cbc3dc8e980a720fac1 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 15 Jun 2024 20:48:37 +0200 Subject: [PATCH 228/433] Force saves independent of manipulations for swaps and merges. --- Penumbra/Mods/Editor/ModMerger.cs | 18 +++++++++++------- Penumbra/Mods/ItemSwap/ItemSwapContainer.cs | 3 ++- .../Manager/OptionEditor/ModGroupEditor.cs | 4 ++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Penumbra/Mods/Editor/ModMerger.cs b/Penumbra/Mods/Editor/ModMerger.cs index 74f182d3..f5fc9cd7 100644 --- a/Penumbra/Mods/Editor/ModMerger.cs +++ b/Penumbra/Mods/Editor/ModMerger.cs @@ -189,7 +189,8 @@ public class ModMerger : IDisposable _editor.SetFiles(option, redirections, SaveType.None); _editor.SetFileSwaps(option, swaps, SaveType.None); - _editor.SetManipulations(option, manips, SaveType.ImmediateSync); + _editor.SetManipulations(option, manips, SaveType.None); + _editor.ForceSave(option, SaveType.ImmediateSync); return; bool GetFullPath(FullPath input, out FullPath ret) @@ -263,9 +264,10 @@ public class ModMerger : IDisposable if (mods.Count == 1) { var files = CopySubModFiles(mods[0], dir); - _editor.SetFiles(result.Default, files); - _editor.SetFileSwaps(result.Default, mods[0].FileSwaps); - _editor.SetManipulations(result.Default, mods[0].Manipulations); + _editor.SetFiles(result.Default, files, SaveType.None); + _editor.SetFileSwaps(result.Default, mods[0].FileSwaps, SaveType.None); + _editor.SetManipulations(result.Default, mods[0].Manipulations, SaveType.None); + _editor.ForceSave(result.Default); } else { @@ -277,6 +279,7 @@ public class ModMerger : IDisposable _editor.SetFiles(result.Default, files); _editor.SetFileSwaps(result.Default, mods[0].FileSwaps); _editor.SetManipulations(result.Default, mods[0].Manipulations); + _editor.ForceSave(result.Default); } else { @@ -285,9 +288,10 @@ public class ModMerger : IDisposable var (option, _, _) = _editor.FindOrAddOption(group!, originalOption.GetName()); var folder = Path.Combine(dir.FullName, group!.Name, option!.Name); var files = CopySubModFiles(originalOption, new DirectoryInfo(folder)); - _editor.SetFiles((IModDataContainer)option, files); - _editor.SetFileSwaps((IModDataContainer)option, originalOption.FileSwaps); - _editor.SetManipulations((IModDataContainer)option, originalOption.Manipulations); + _editor.SetFiles((IModDataContainer)option, files, SaveType.None); + _editor.SetFileSwaps((IModDataContainer)option, originalOption.FileSwaps, SaveType.None); + _editor.SetManipulations((IModDataContainer)option, originalOption.Manipulations, SaveType.None); + _editor.ForceSave((IModDataContainer)option); } } } diff --git a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs index 48d687d0..af5b2d3a 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs @@ -83,7 +83,8 @@ public class ItemSwapContainer manager.OptionEditor.SetFiles(container, convertedFiles, SaveType.None); manager.OptionEditor.SetFileSwaps(container, convertedSwaps, SaveType.None); - manager.OptionEditor.SetManipulations(container, convertedManips, SaveType.ImmediateSync); + manager.OptionEditor.SetManipulations(container, convertedManips, SaveType.None); + manager.OptionEditor.ForceSave(container, SaveType.ImmediateSync); return true; } catch (Exception e) diff --git a/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs index e1db0ccf..55e01015 100644 --- a/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs @@ -166,6 +166,10 @@ public class ModGroupEditor( communicator.ModOptionChanged.Invoke(ModOptionChangeType.OptionFilesChanged, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); } + /// Forces a file save of the given container's group. + public void ForceSave(IModDataContainer subMod, SaveType saveType = SaveType.Queue) + => saveService.Save(saveType, new ModSaveGroup(subMod, Config.ReplaceNonAsciiOnImport)); + /// Add additional file redirections to a given option, keeping already existing ones. Only fires an event if anything is actually added. public void AddFiles(IModDataContainer subMod, IReadOnlyDictionary additions) { From b3f87624949c9c48de7bc847782601577bd86336 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 17 Jun 2024 14:50:07 +0200 Subject: [PATCH 229/433] Fix some crash handler issues --- Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs | 2 ++ Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs | 2 ++ Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs b/Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs index 3446530a..11dc52db 100644 --- a/Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs +++ b/Penumbra.CrashHandler/Buffers/AnimationInvocationBuffer.cs @@ -55,8 +55,10 @@ internal sealed class AnimationInvocationBuffer : MemoryMappedBuffer, IAnimation accessor.Write(16, characterAddress); var span = GetSpan(accessor, 24, 16); collectionId.TryWriteBytes(span); + accessor.SafeMemoryMappedViewHandle.ReleasePointer(); span = GetSpan(accessor, 40); WriteSpan(characterName, span); + accessor.SafeMemoryMappedViewHandle.ReleasePointer(); } } diff --git a/Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs b/Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs index 4036455d..a48fe846 100644 --- a/Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs +++ b/Penumbra.CrashHandler/Buffers/CharacterBaseBuffer.cs @@ -38,8 +38,10 @@ internal sealed class CharacterBaseBuffer : MemoryMappedBuffer, ICharacterBaseBu accessor.Write(12, characterAddress); var span = GetSpan(accessor, 20, 16); collectionId.TryWriteBytes(span); + accessor.SafeMemoryMappedViewHandle.ReleasePointer(); span = GetSpan(accessor, 36); WriteSpan(characterName, span); + accessor.SafeMemoryMappedViewHandle.ReleasePointer(); } } diff --git a/Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs b/Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs index 03f63ba4..ac507e7f 100644 --- a/Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs +++ b/Penumbra.CrashHandler/Buffers/ModdedFileBuffer.cs @@ -44,12 +44,16 @@ internal sealed class ModdedFileBuffer : MemoryMappedBuffer, IModdedFileBufferWr accessor.Write(12, characterAddress); var span = GetSpan(accessor, 20, 16); collectionId.TryWriteBytes(span); + accessor.SafeMemoryMappedViewHandle.ReleasePointer(); span = GetSpan(accessor, 36, 80); WriteSpan(characterName, span); + accessor.SafeMemoryMappedViewHandle.ReleasePointer(); span = GetSpan(accessor, 116, 260); WriteSpan(requestedFileName, span); + accessor.SafeMemoryMappedViewHandle.ReleasePointer(); span = GetSpan(accessor, 376); WriteSpan(actualFileName, span); + accessor.SafeMemoryMappedViewHandle.ReleasePointer(); } } From 250c4034e04323b025cd39cd4641be8e04d431ce Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 17 Jun 2024 14:50:33 +0200 Subject: [PATCH 230/433] Improve root directory behavior and AddMods. --- Penumbra/Api/Api/ModsApi.cs | 4 +++- Penumbra/Mods/Manager/ModManager.cs | 16 +++++++++------- Penumbra/UI/Tabs/SettingsTab.cs | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Penumbra/Api/Api/ModsApi.cs b/Penumbra/Api/Api/ModsApi.cs index 548831d5..60b00d37 100644 --- a/Penumbra/Api/Api/ModsApi.cs +++ b/Penumbra/Api/Api/ModsApi.cs @@ -75,7 +75,9 @@ public class ModsApi : IPenumbraApiMods, IApiService, IDisposable if (!dir.Exists) return ApiHelpers.Return(PenumbraApiEc.FileMissing, args); - if (dir.Parent == null || Path.GetFullPath(_modManager.BasePath.FullName) != Path.GetFullPath(dir.Parent.FullName)) + if (dir.Parent == null + || Path.TrimEndingDirectorySeparator(Path.GetFullPath(_modManager.BasePath.FullName)) + != Path.TrimEndingDirectorySeparator(Path.GetFullPath(dir.Parent.FullName))) return ApiHelpers.Return(PenumbraApiEc.InvalidArgument, args); _modManager.AddMod(dir); diff --git a/Penumbra/Mods/Manager/ModManager.cs b/Penumbra/Mods/Manager/ModManager.cs index 010cad19..62b54865 100644 --- a/Penumbra/Mods/Manager/ModManager.cs +++ b/Penumbra/Mods/Manager/ModManager.cs @@ -47,15 +47,15 @@ public sealed class ModManager : ModStorage, IDisposable DataEditor = dataEditor; OptionEditor = optionEditor; Creator = creator; - SetBaseDirectory(config.ModDirectory, true); + SetBaseDirectory(config.ModDirectory, true, out _); _communicator.ModPathChanged.Subscribe(OnModPathChange, ModPathChanged.Priority.ModManager); DiscoverMods(); } /// Change the mod base directory and discover available mods. - public void DiscoverMods(string newDir) + public void DiscoverMods(string newDir, out string resultNewDir) { - SetBaseDirectory(newDir, false); + SetBaseDirectory(newDir, false, out resultNewDir); DiscoverMods(); } @@ -264,8 +264,9 @@ public sealed class ModManager : ModStorage, IDisposable /// If its not the first time, check if it is the same directory as before. /// Also checks if the directory is available and tries to create it if it is not. /// - private void SetBaseDirectory(string newPath, bool firstTime) + private void SetBaseDirectory(string newPath, bool firstTime, out string resultNewDir) { + resultNewDir = newPath; if (!firstTime && string.Equals(newPath, _config.ModDirectory, StringComparison.Ordinal)) return; @@ -278,7 +279,7 @@ public sealed class ModManager : ModStorage, IDisposable } else { - var newDir = new DirectoryInfo(newPath); + var newDir = new DirectoryInfo(Path.TrimEndingDirectorySeparator(newPath)); if (!newDir.Exists) try { @@ -290,8 +291,9 @@ public sealed class ModManager : ModStorage, IDisposable Penumbra.Log.Error($"Could not create specified mod directory {newDir.FullName}:\n{e}"); } - BasePath = newDir; - Valid = Directory.Exists(newDir.FullName); + BasePath = newDir; + Valid = Directory.Exists(newDir.FullName); + resultNewDir = BasePath.FullName; if (!firstTime && _config.ModDirectory != BasePath.FullName) TriggerModDirectoryChange(BasePath.FullName, Valid); } diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index 9989f90a..0de4f790 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -269,7 +269,7 @@ public class SettingsTab : ITab if (_config.ModDirectory != _newModDirectory && _newModDirectory.Length != 0 && DrawPressEnterWarning(_newModDirectory, _config.ModDirectory, pos, save, selected)) - _modManager.DiscoverMods(_newModDirectory); + _modManager.DiscoverMods(_newModDirectory, out _newModDirectory); } /// Draw the Open Directory and Rediscovery buttons. From d7b60206d77610d2d0ba62ca280e93470698c94e Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 8 Jun 2024 14:55:10 +0200 Subject: [PATCH 231/433] Improve meta manipulation handling a bit. --- Penumbra/Api/Api/TemporaryApi.cs | 4 +- Penumbra/Api/TempModManager.cs | 2 +- .../Meta/Manipulations/EqdpManipulation.cs | 17 +-- .../Meta/Manipulations/EqpManipulation.cs | 17 +-- .../Meta/Manipulations/EstManipulation.cs | 15 +- .../Manipulations/GlobalEqpManipulation.cs | 31 +++- .../Meta/Manipulations/GmpManipulation.cs | 15 +- Penumbra/Meta/Manipulations/MetaDictionary.cs | 140 ++++++++++++++++++ Penumbra/Meta/Manipulations/Rsp.cs | 28 +++- .../Meta/Manipulations/RspManipulation.cs | 27 ++-- Penumbra/Mods/SubMods/DefaultSubMod.cs | 2 +- Penumbra/Mods/SubMods/IModDataContainer.cs | 6 +- Penumbra/Mods/SubMods/OptionSubMod.cs | 2 +- Penumbra/Mods/TemporaryMod.cs | 2 +- 14 files changed, 242 insertions(+), 66 deletions(-) create mode 100644 Penumbra/Meta/Manipulations/MetaDictionary.cs diff --git a/Penumbra/Api/Api/TemporaryApi.cs b/Penumbra/Api/Api/TemporaryApi.cs index 38d080cc..09a9b7c4 100644 --- a/Penumbra/Api/Api/TemporaryApi.cs +++ b/Penumbra/Api/Api/TemporaryApi.cs @@ -160,7 +160,7 @@ public class TemporaryApi( /// Only returns true if all conversions are successful and distinct. /// private static bool ConvertManips(string manipString, - [NotNullWhen(true)] out HashSet? manips) + [NotNullWhen(true)] out MetaDictionary? manips) { if (manipString.Length == 0) { @@ -174,7 +174,7 @@ public class TemporaryApi( return false; } - manips = new HashSet(manipArray!.Length); + manips = new MetaDictionary(manipArray!.Length); foreach (var manip in manipArray.Where(m => m.Validate())) { if (manips.Add(manip)) diff --git a/Penumbra/Api/TempModManager.cs b/Penumbra/Api/TempModManager.cs index aee2b447..cbb07436 100644 --- a/Penumbra/Api/TempModManager.cs +++ b/Penumbra/Api/TempModManager.cs @@ -43,7 +43,7 @@ public class TempModManager : IDisposable => _modsForAllCollections; public RedirectResult Register(string tag, ModCollection? collection, Dictionary dict, - HashSet manips, ModPriority priority) + MetaDictionary manips, ModPriority priority) { var mod = GetOrCreateMod(tag, collection, priority, out var created); Penumbra.Log.Verbose($"{(created ? "Created" : "Changed")} temporary Mod {mod.Name}."); diff --git a/Penumbra/Meta/Manipulations/EqdpManipulation.cs b/Penumbra/Meta/Manipulations/EqdpManipulation.cs index 2c01ce3f..8c5f27e5 100644 --- a/Penumbra/Meta/Manipulations/EqdpManipulation.cs +++ b/Penumbra/Meta/Manipulations/EqdpManipulation.cs @@ -8,11 +8,12 @@ using Penumbra.Meta.Files; namespace Penumbra.Meta.Manipulations; [StructLayout(LayoutKind.Sequential, Pack = 1)] -public readonly struct EqdpManipulation : IMetaManipulation +public readonly struct EqdpManipulation(EqdpIdentifier identifier, EqdpEntry entry) : IMetaManipulation { [JsonIgnore] - public EqdpIdentifier Identifier { get; private init; } - public EqdpEntry Entry { get; private init; } + public EqdpIdentifier Identifier { get; } = identifier; + + public EqdpEntry Entry { get; } = entry; [JsonConverter(typeof(StringEnumConverter))] public Gender Gender @@ -31,20 +32,18 @@ public readonly struct EqdpManipulation : IMetaManipulation [JsonConstructor] public EqdpManipulation(EqdpEntry entry, EquipSlot slot, Gender gender, ModelRace race, PrimaryId setId) - { - Identifier = new EqdpIdentifier(setId, slot, Names.CombinedRace(gender, race)); - Entry = Eqdp.Mask(Slot) & entry; - } + : this(new EqdpIdentifier(setId, slot, Names.CombinedRace(gender, race)), Eqdp.Mask(slot) & entry) + { } public EqdpManipulation Copy(EqdpManipulation entry) { if (entry.Slot != Slot) { var (bit1, bit2) = entry.Entry.ToBits(entry.Slot); - return new EqdpManipulation(Eqdp.FromSlotAndBits(Slot, bit1, bit2), Slot, Gender, Race, SetId); + return new EqdpManipulation(Identifier, Eqdp.FromSlotAndBits(Slot, bit1, bit2)); } - return new EqdpManipulation(entry.Entry, Slot, Gender, Race, SetId); + return new EqdpManipulation(Identifier, entry.Entry); } public EqdpManipulation Copy(EqdpEntry entry) diff --git a/Penumbra/Meta/Manipulations/EqpManipulation.cs b/Penumbra/Meta/Manipulations/EqpManipulation.cs index 3bced096..eef21d12 100644 --- a/Penumbra/Meta/Manipulations/EqpManipulation.cs +++ b/Penumbra/Meta/Manipulations/EqpManipulation.cs @@ -9,12 +9,13 @@ using Penumbra.Util; namespace Penumbra.Meta.Manipulations; [StructLayout(LayoutKind.Sequential, Pack = 1)] -public readonly struct EqpManipulation : IMetaManipulation +public readonly struct EqpManipulation(EqpIdentifier identifier, EqpEntry entry) : IMetaManipulation { - [JsonConverter(typeof(ForceNumericFlagEnumConverter))] - public EqpEntry Entry { get; private init; } + [JsonIgnore] + public EqpIdentifier Identifier { get; } = identifier; - public EqpIdentifier Identifier { get; private init; } + [JsonConverter(typeof(ForceNumericFlagEnumConverter))] + public EqpEntry Entry { get; } = entry; public PrimaryId SetId => Identifier.SetId; @@ -25,13 +26,11 @@ public readonly struct EqpManipulation : IMetaManipulation [JsonConstructor] public EqpManipulation(EqpEntry entry, EquipSlot slot, PrimaryId setId) - { - Identifier = new EqpIdentifier(setId, slot); - Entry = Eqp.Mask(slot) & entry; - } + : this(new EqpIdentifier(setId, slot), Eqp.Mask(slot) & entry) + { } public EqpManipulation Copy(EqpEntry entry) - => new(entry, Slot, SetId); + => new(Identifier, entry); public override string ToString() => $"Eqp - {SetId} - {Slot}"; diff --git a/Penumbra/Meta/Manipulations/EstManipulation.cs b/Penumbra/Meta/Manipulations/EstManipulation.cs index c3f9792f..09abbaa5 100644 --- a/Penumbra/Meta/Manipulations/EstManipulation.cs +++ b/Penumbra/Meta/Manipulations/EstManipulation.cs @@ -8,7 +8,7 @@ using Penumbra.Meta.Files; namespace Penumbra.Meta.Manipulations; [StructLayout(LayoutKind.Sequential, Pack = 1)] -public readonly struct EstManipulation : IMetaManipulation +public readonly struct EstManipulation(EstIdentifier identifier, EstEntry entry) : IMetaManipulation { public static string ToName(EstType type) => type switch @@ -20,8 +20,9 @@ public readonly struct EstManipulation : IMetaManipulation _ => "unk", }; - public EstIdentifier Identifier { get; private init; } - public EstEntry Entry { get; private init; } + [JsonIgnore] + public EstIdentifier Identifier { get; } = identifier; + public EstEntry Entry { get; } = entry; [JsonConverter(typeof(StringEnumConverter))] public Gender Gender @@ -41,13 +42,11 @@ public readonly struct EstManipulation : IMetaManipulation [JsonConstructor] public EstManipulation(Gender gender, ModelRace race, EstType slot, PrimaryId setId, EstEntry entry) - { - Entry = entry; - Identifier = new EstIdentifier(setId, slot, Names.CombinedRace(gender, race)); - } + : this(new EstIdentifier(setId, slot, Names.CombinedRace(gender, race)), entry) + { } public EstManipulation Copy(EstEntry entry) - => new(Gender, Race, Slot, SetId, entry); + => new(Identifier, entry); public override string ToString() diff --git a/Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs b/Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs index ada543dc..94c892e2 100644 --- a/Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs +++ b/Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs @@ -1,9 +1,11 @@ +using Newtonsoft.Json.Linq; +using Penumbra.GameData.Data; using Penumbra.GameData.Structs; using Penumbra.Interop.Structs; namespace Penumbra.Meta.Manipulations; -public readonly struct GlobalEqpManipulation : IMetaManipulation +public readonly struct GlobalEqpManipulation : IMetaManipulation, IMetaIdentifier { public GlobalEqpType Type { get; init; } public PrimaryId Condition { get; init; } @@ -19,6 +21,28 @@ public readonly struct GlobalEqpManipulation : IMetaManipulation() ?? (GlobalEqpType)100; + var condition = jObj[nameof(Condition)]?.ToObject() ?? 0; + var ret = new GlobalEqpManipulation + { + Type = type, + Condition = condition, + }; + return ret.Validate() ? ret : null; + } + public bool Equals(GlobalEqpManipulation other) => Type == other.Type @@ -45,6 +69,9 @@ public readonly struct GlobalEqpManipulation : IMetaManipulation $"Global EQP - {Type}{(Condition != 0 ? $" - {Condition.Id}" : string.Empty)}"; + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + { } + public MetaIndex FileIndex() - => (MetaIndex)(-1); + => MetaIndex.Eqp; } diff --git a/Penumbra/Meta/Manipulations/GmpManipulation.cs b/Penumbra/Meta/Manipulations/GmpManipulation.cs index 0b2a9f4b..431f6325 100644 --- a/Penumbra/Meta/Manipulations/GmpManipulation.cs +++ b/Penumbra/Meta/Manipulations/GmpManipulation.cs @@ -6,24 +6,23 @@ using Penumbra.Meta.Files; namespace Penumbra.Meta.Manipulations; [StructLayout(LayoutKind.Sequential, Pack = 1)] -public readonly struct GmpManipulation : IMetaManipulation +public readonly struct GmpManipulation(GmpIdentifier identifier, GmpEntry entry) : IMetaManipulation { - public GmpIdentifier Identifier { get; private init; } + [JsonIgnore] + public GmpIdentifier Identifier { get; } = identifier; - public GmpEntry Entry { get; private init; } + public GmpEntry Entry { get; } = entry; public PrimaryId SetId => Identifier.SetId; [JsonConstructor] public GmpManipulation(GmpEntry entry, PrimaryId setId) - { - Entry = entry; - Identifier = new GmpIdentifier(setId); - } + : this(new GmpIdentifier(setId), entry) + { } public GmpManipulation Copy(GmpEntry entry) - => new(entry, SetId); + => new(Identifier, entry); public override string ToString() => $"Gmp - {SetId}"; diff --git a/Penumbra/Meta/Manipulations/MetaDictionary.cs b/Penumbra/Meta/Manipulations/MetaDictionary.cs new file mode 100644 index 00000000..65252c5d --- /dev/null +++ b/Penumbra/Meta/Manipulations/MetaDictionary.cs @@ -0,0 +1,140 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Penumbra.GameData.Structs; +using ImcEntry = Penumbra.GameData.Structs.ImcEntry; + +namespace Penumbra.Meta.Manipulations; + +[JsonConverter(typeof(Converter))] +public sealed class MetaDictionary : HashSet +{ + public MetaDictionary() + { } + + public MetaDictionary(int capacity) + : base(capacity) + { } + + private class Converter : JsonConverter + { + public override void WriteJson(JsonWriter writer, MetaDictionary? value, JsonSerializer serializer) + { + if (value is null) + { + writer.WriteNull(); + return; + } + + writer.WriteStartArray(); + foreach (var item in value) + { + writer.WriteStartObject(); + writer.WritePropertyName("Type"); + writer.WriteValue(item.ManipulationType.ToString()); + writer.WritePropertyName("Manipulation"); + serializer.Serialize(writer, item.Manipulation); + writer.WriteEndObject(); + } + + writer.WriteEndArray(); + } + + public override MetaDictionary ReadJson(JsonReader reader, Type objectType, MetaDictionary? existingValue, bool hasExistingValue, + JsonSerializer serializer) + { + var dict = existingValue ?? []; + dict.Clear(); + var jObj = JArray.Load(reader); + foreach (var item in jObj) + { + var type = item["Type"]?.ToObject() ?? MetaManipulation.Type.Unknown; + if (type is MetaManipulation.Type.Unknown) + { + Penumbra.Log.Warning($"Invalid Meta Manipulation Type {type} encountered."); + continue; + } + + if (item["Manipulation"] is not JObject manip) + { + Penumbra.Log.Warning($"Manipulation of type {type} does not contain manipulation data."); + continue; + } + + switch (type) + { + case MetaManipulation.Type.Imc: + { + var identifier = ImcIdentifier.FromJson(manip); + var entry = manip["Entry"]?.ToObject(); + if (identifier.HasValue && entry.HasValue) + dict.Add(new MetaManipulation(new ImcManipulation(identifier.Value, entry.Value))); + else + Penumbra.Log.Warning("Invalid IMC Manipulation encountered."); + break; + } + case MetaManipulation.Type.Eqdp: + { + var identifier = EqdpIdentifier.FromJson(manip); + var entry = (EqdpEntry?)manip["Entry"]?.ToObject(); + if (identifier.HasValue && entry.HasValue) + dict.Add(new MetaManipulation(new EqdpManipulation(identifier.Value, entry.Value))); + else + Penumbra.Log.Warning("Invalid EQDP Manipulation encountered."); + break; + } + case MetaManipulation.Type.Eqp: + { + var identifier = EqpIdentifier.FromJson(manip); + var entry = (EqpEntry?)manip["Entry"]?.ToObject(); + if (identifier.HasValue && entry.HasValue) + dict.Add(new MetaManipulation(new EqpManipulation(identifier.Value, entry.Value))); + else + Penumbra.Log.Warning("Invalid EQP Manipulation encountered."); + break; + } + case MetaManipulation.Type.Est: + { + var identifier = EstIdentifier.FromJson(manip); + var entry = manip["Entry"]?.ToObject(); + if (identifier.HasValue && entry.HasValue) + dict.Add(new MetaManipulation(new EstManipulation(identifier.Value, entry.Value))); + else + Penumbra.Log.Warning("Invalid EST Manipulation encountered."); + break; + } + case MetaManipulation.Type.Gmp: + { + var identifier = GmpIdentifier.FromJson(manip); + var entry = manip["Entry"]?.ToObject(); + if (identifier.HasValue && entry.HasValue) + dict.Add(new MetaManipulation(new GmpManipulation(identifier.Value, entry.Value))); + else + Penumbra.Log.Warning("Invalid GMP Manipulation encountered."); + break; + } + case MetaManipulation.Type.Rsp: + { + var identifier = RspIdentifier.FromJson(manip); + var entry = manip["Entry"]?.ToObject(); + if (identifier.HasValue && entry.HasValue) + dict.Add(new MetaManipulation(new RspManipulation(identifier.Value, entry.Value))); + else + Penumbra.Log.Warning("Invalid RSP Manipulation encountered."); + break; + } + case MetaManipulation.Type.GlobalEqp: + { + var identifier = GlobalEqpManipulation.FromJson(manip); + if (identifier.HasValue) + dict.Add(new MetaManipulation(identifier.Value)); + else + Penumbra.Log.Warning("Invalid Global EQP Manipulation encountered."); + break; + } + } + } + + return dict; + } + } +} diff --git a/Penumbra/Meta/Manipulations/Rsp.cs b/Penumbra/Meta/Manipulations/Rsp.cs index 29cdfd71..ca7cb1c5 100644 --- a/Penumbra/Meta/Manipulations/Rsp.cs +++ b/Penumbra/Meta/Manipulations/Rsp.cs @@ -1,3 +1,4 @@ +using Lumina.Excel.GeneratedSheets; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Penumbra.GameData.Data; @@ -12,13 +13,31 @@ public readonly record struct RspIdentifier(SubRace SubRace, RspAttribute Attrib => changedItems.TryAdd($"{SubRace.ToName()} {Attribute.ToFullString()}", null); public MetaIndex FileIndex() - => throw new NotImplementedException(); + => MetaIndex.HumanCmp; public bool Validate() - => throw new NotImplementedException(); + => SubRace is not SubRace.Unknown + && Enum.IsDefined(SubRace) + && Attribute is not RspAttribute.NumAttributes + && Enum.IsDefined(Attribute); public JObject AddToJson(JObject jObj) - => throw new NotImplementedException(); + { + jObj["SubRace"] = SubRace.ToString(); + jObj["Attribute"] = Attribute.ToString(); + return jObj; + } + + public static RspIdentifier? FromJson(JObject? jObj) + { + if (jObj == null) + return null; + + var subRace = jObj["SubRace"]?.ToObject() ?? SubRace.Unknown; + var attribute = jObj["Attribute"]?.ToObject() ?? RspAttribute.NumAttributes; + var ret = new RspIdentifier(subRace, attribute); + return ret.Validate() ? ret : null; + } } [JsonConverter(typeof(Converter))] @@ -28,6 +47,9 @@ public readonly record struct RspEntry(float Value) : IComparisonOperators Value is >= MinValue and <= MaxValue; + private class Converter : JsonConverter { public override void WriteJson(JsonWriter writer, RspEntry value, JsonSerializer serializer) diff --git a/Penumbra/Meta/Manipulations/RspManipulation.cs b/Penumbra/Meta/Manipulations/RspManipulation.cs index 04691c9f..e2282c41 100644 --- a/Penumbra/Meta/Manipulations/RspManipulation.cs +++ b/Penumbra/Meta/Manipulations/RspManipulation.cs @@ -7,10 +7,12 @@ using Penumbra.Meta.Files; namespace Penumbra.Meta.Manipulations; [StructLayout(LayoutKind.Sequential, Pack = 1)] -public readonly struct RspManipulation : IMetaManipulation +public readonly struct RspManipulation(RspIdentifier identifier, RspEntry entry) : IMetaManipulation { - public RspIdentifier Identifier { get; private init; } - public RspEntry Entry { get; private init; } + [JsonIgnore] + public RspIdentifier Identifier { get; } = identifier; + + public RspEntry Entry { get; } = entry; [JsonConverter(typeof(StringEnumConverter))] public SubRace SubRace @@ -22,13 +24,11 @@ public readonly struct RspManipulation : IMetaManipulation [JsonConstructor] public RspManipulation(SubRace subRace, RspAttribute attribute, RspEntry entry) - { - Entry = entry; - Identifier = new RspIdentifier(subRace, attribute); - } + : this(new RspIdentifier(subRace, attribute), entry) + { } public RspManipulation Copy(RspEntry entry) - => new(SubRace, Attribute, entry); + => new(Identifier, entry); public override string ToString() => $"Rsp - {SubRace.ToName()} - {Attribute.ToFullString()}"; @@ -63,14 +63,5 @@ public readonly struct RspManipulation : IMetaManipulation } public bool Validate() - { - if (SubRace is SubRace.Unknown || !Enum.IsDefined(SubRace)) - return false; - if (!Enum.IsDefined(Attribute)) - return false; - if (Entry.Value is < RspEntry.MinValue or > RspEntry.MaxValue) - return false; - - return true; - } + => Identifier.Validate() && Entry.Validate(); } diff --git a/Penumbra/Mods/SubMods/DefaultSubMod.cs b/Penumbra/Mods/SubMods/DefaultSubMod.cs index 1a234879..5a300a48 100644 --- a/Penumbra/Mods/SubMods/DefaultSubMod.cs +++ b/Penumbra/Mods/SubMods/DefaultSubMod.cs @@ -13,7 +13,7 @@ public class DefaultSubMod(IMod mod) : IModDataContainer public Dictionary Files { get; set; } = []; public Dictionary FileSwaps { get; set; } = []; - public HashSet Manipulations { get; set; } = []; + public MetaDictionary Manipulations { get; set; } = []; IMod IModDataContainer.Mod => Mod; diff --git a/Penumbra/Mods/SubMods/IModDataContainer.cs b/Penumbra/Mods/SubMods/IModDataContainer.cs index 7f7ef4a6..1a89ec17 100644 --- a/Penumbra/Mods/SubMods/IModDataContainer.cs +++ b/Penumbra/Mods/SubMods/IModDataContainer.cs @@ -10,9 +10,9 @@ public interface IModDataContainer public IMod Mod { get; } public IModGroup? Group { get; } - public Dictionary Files { get; set; } - public Dictionary FileSwaps { get; set; } - public HashSet Manipulations { get; set; } + public Dictionary Files { get; set; } + public Dictionary FileSwaps { get; set; } + public MetaDictionary Manipulations { get; set; } public string GetName(); public string GetFullName(); diff --git a/Penumbra/Mods/SubMods/OptionSubMod.cs b/Penumbra/Mods/SubMods/OptionSubMod.cs index 02d86af2..378f6dc8 100644 --- a/Penumbra/Mods/SubMods/OptionSubMod.cs +++ b/Penumbra/Mods/SubMods/OptionSubMod.cs @@ -33,7 +33,7 @@ public abstract class OptionSubMod(IModGroup group) : IModOption, IModDataContai public Dictionary Files { get; set; } = []; public Dictionary FileSwaps { get; set; } = []; - public HashSet Manipulations { get; set; } = []; + public MetaDictionary Manipulations { get; set; } = []; public void AddDataTo(Dictionary redirections, HashSet manipulations) => SubMod.AddContainerTo(this, redirections, manipulations); diff --git a/Penumbra/Mods/TemporaryMod.cs b/Penumbra/Mods/TemporaryMod.cs index 6e6e72ab..e0a03c92 100644 --- a/Penumbra/Mods/TemporaryMod.cs +++ b/Penumbra/Mods/TemporaryMod.cs @@ -57,7 +57,7 @@ public class TemporaryMod : IMod public bool SetManipulation(MetaManipulation manip) => Default.Manipulations.Remove(manip) | Default.Manipulations.Add(manip); - public void SetAll(Dictionary dict, HashSet manips) + public void SetAll(Dictionary dict, MetaDictionary manips) { Default.Files = dict; Default.Manipulations = manips; From 94fdd848b718ef2c7e932faff6b108f7ed7287d8 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 8 Jun 2024 16:06:37 +0200 Subject: [PATCH 232/433] Expand on MetaDictionary to use separate dictionaries. --- Penumbra/Api/Api/TemporaryApi.cs | 4 +- Penumbra/Meta/Manipulations/MetaDictionary.cs | 244 +++++++++++++++++- Penumbra/Mods/Editor/IMod.cs | 2 +- Penumbra/Mods/Editor/ModMerger.cs | 2 +- Penumbra/Mods/Editor/ModMetaEditor.cs | 2 +- Penumbra/Mods/Groups/IModGroup.cs | 2 +- Penumbra/Mods/Groups/ImcModGroup.cs | 2 +- Penumbra/Mods/Groups/MultiModGroup.cs | 2 +- Penumbra/Mods/Groups/SingleModGroup.cs | 2 +- Penumbra/Mods/ItemSwap/ItemSwapContainer.cs | 21 +- .../Manager/OptionEditor/ModGroupEditor.cs | 2 +- Penumbra/Mods/Mod.cs | 2 +- Penumbra/Mods/SubMods/DefaultSubMod.cs | 2 +- Penumbra/Mods/SubMods/OptionSubMod.cs | 2 +- Penumbra/Mods/SubMods/SubMod.cs | 2 +- 15 files changed, 257 insertions(+), 36 deletions(-) diff --git a/Penumbra/Api/Api/TemporaryApi.cs b/Penumbra/Api/Api/TemporaryApi.cs index 09a9b7c4..995ec388 100644 --- a/Penumbra/Api/Api/TemporaryApi.cs +++ b/Penumbra/Api/Api/TemporaryApi.cs @@ -174,8 +174,8 @@ public class TemporaryApi( return false; } - manips = new MetaDictionary(manipArray!.Length); - foreach (var manip in manipArray.Where(m => m.Validate())) + manips = []; + foreach (var manip in manipArray!.Where(m => m.Validate())) { if (manips.Add(manip)) continue; diff --git a/Penumbra/Meta/Manipulations/MetaDictionary.cs b/Penumbra/Meta/Manipulations/MetaDictionary.cs index 65252c5d..b0b7f011 100644 --- a/Penumbra/Meta/Manipulations/MetaDictionary.cs +++ b/Penumbra/Meta/Manipulations/MetaDictionary.cs @@ -1,19 +1,237 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Penumbra.GameData.Structs; +using Penumbra.Util; using ImcEntry = Penumbra.GameData.Structs.ImcEntry; namespace Penumbra.Meta.Manipulations; [JsonConverter(typeof(Converter))] -public sealed class MetaDictionary : HashSet +public sealed class MetaDictionary : IEnumerable { - public MetaDictionary() - { } + private readonly Dictionary _imc = []; + private readonly Dictionary _eqp = []; + private readonly Dictionary _eqdp = []; + private readonly Dictionary _est = []; + private readonly Dictionary _rsp = []; + private readonly Dictionary _gmp = []; + private readonly HashSet _globalEqp = []; - public MetaDictionary(int capacity) - : base(capacity) - { } + public int Count { get; private set; } + + public void Clear() + { + _imc.Clear(); + _eqp.Clear(); + _eqdp.Clear(); + _est.Clear(); + _rsp.Clear(); + _gmp.Clear(); + _globalEqp.Clear(); + } + + public IEnumerator GetEnumerator() + => _imc.Select(kvp => new MetaManipulation(new ImcManipulation(kvp.Key, kvp.Value))) + .Concat(_eqp.Select(kvp => new MetaManipulation(new EqpManipulation(kvp.Key, kvp.Value)))) + .Concat(_eqdp.Select(kvp => new MetaManipulation(new EqdpManipulation(kvp.Key, kvp.Value)))) + .Concat(_est.Select(kvp => new MetaManipulation(new EstManipulation(kvp.Key, kvp.Value)))) + .Concat(_rsp.Select(kvp => new MetaManipulation(new RspManipulation(kvp.Key, kvp.Value)))) + .Concat(_gmp.Select(kvp => new MetaManipulation(new GmpManipulation(kvp.Key, kvp.Value)))) + .Concat(_globalEqp.Select(manip => new MetaManipulation(manip))).GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() + => GetEnumerator(); + + public bool Add(MetaManipulation manip) + { + var ret = manip.ManipulationType switch + { + MetaManipulation.Type.Imc => _imc.TryAdd(manip.Imc.Identifier, manip.Imc.Entry), + MetaManipulation.Type.Eqdp => _eqdp.TryAdd(manip.Eqdp.Identifier, manip.Eqdp.Entry), + MetaManipulation.Type.Eqp => _eqp.TryAdd(manip.Eqp.Identifier, manip.Eqp.Entry), + MetaManipulation.Type.Est => _est.TryAdd(manip.Est.Identifier, manip.Est.Entry), + MetaManipulation.Type.Gmp => _gmp.TryAdd(manip.Gmp.Identifier, manip.Gmp.Entry), + MetaManipulation.Type.Rsp => _rsp.TryAdd(manip.Rsp.Identifier, manip.Rsp.Entry), + MetaManipulation.Type.GlobalEqp => _globalEqp.Add(manip.GlobalEqp), + _ => false, + }; + + if (ret) + ++Count; + return ret; + } + + public bool TryAdd(ImcIdentifier identifier, ImcEntry entry) + { + if (!_imc.TryAdd(identifier, entry)) + return false; + + ++Count; + return true; + } + + public bool TryAdd(EqpIdentifier identifier, EqpEntry entry) + { + if (!_eqp.TryAdd(identifier, entry)) + return false; + + ++Count; + return true; + } + + public bool TryAdd(EqdpIdentifier identifier, EqdpEntry entry) + { + if (!_eqdp.TryAdd(identifier, entry)) + return false; + + ++Count; + return true; + } + + public bool TryAdd(EstIdentifier identifier, EstEntry entry) + { + if (!_est.TryAdd(identifier, entry)) + return false; + + ++Count; + return true; + } + + public bool TryAdd(GmpIdentifier identifier, GmpEntry entry) + { + if (!_gmp.TryAdd(identifier, entry)) + return false; + + ++Count; + return true; + } + + public bool TryAdd(RspIdentifier identifier, RspEntry entry) + { + if (!_rsp.TryAdd(identifier, entry)) + return false; + + ++Count; + return true; + } + + public bool TryAdd(GlobalEqpManipulation identifier) + { + if (!_globalEqp.Add(identifier)) + return false; + + ++Count; + return true; + } + + public bool Remove(MetaManipulation manip) + { + var ret = manip.ManipulationType switch + { + MetaManipulation.Type.Imc => _imc.Remove(manip.Imc.Identifier), + MetaManipulation.Type.Eqdp => _eqdp.Remove(manip.Eqdp.Identifier), + MetaManipulation.Type.Eqp => _eqp.Remove(manip.Eqp.Identifier), + MetaManipulation.Type.Est => _est.Remove(manip.Est.Identifier), + MetaManipulation.Type.Gmp => _gmp.Remove(manip.Gmp.Identifier), + MetaManipulation.Type.Rsp => _rsp.Remove(manip.Rsp.Identifier), + MetaManipulation.Type.GlobalEqp => _globalEqp.Remove(manip.GlobalEqp), + _ => false, + }; + if (ret) + --Count; + return ret; + } + + public void UnionWith(IEnumerable manips) + { + foreach (var manip in manips) + Add(manip); + } + + public bool TryGetValue(MetaManipulation identifier, out MetaManipulation oldValue) + { + switch (identifier.ManipulationType) + { + case MetaManipulation.Type.Imc: + if (_imc.TryGetValue(identifier.Imc.Identifier, out var oldImc)) + { + oldValue = new MetaManipulation(new ImcManipulation(identifier.Imc.Identifier, oldImc)); + return true; + } + + break; + case MetaManipulation.Type.Eqdp: + if (_eqp.TryGetValue(identifier.Eqp.Identifier, out var oldEqdp)) + { + oldValue = new MetaManipulation(new EqpManipulation(identifier.Eqp.Identifier, oldEqdp)); + return true; + } + + break; + case MetaManipulation.Type.Eqp: + if (_eqdp.TryGetValue(identifier.Eqdp.Identifier, out var oldEqp)) + { + oldValue = new MetaManipulation(new EqdpManipulation(identifier.Eqdp.Identifier, oldEqp)); + return true; + } + + break; + case MetaManipulation.Type.Est: + if (_est.TryGetValue(identifier.Est.Identifier, out var oldEst)) + { + oldValue = new MetaManipulation(new EstManipulation(identifier.Est.Identifier, oldEst)); + return true; + } + + break; + case MetaManipulation.Type.Gmp: + if (_gmp.TryGetValue(identifier.Gmp.Identifier, out var oldGmp)) + { + oldValue = new MetaManipulation(new GmpManipulation(identifier.Gmp.Identifier, oldGmp)); + return true; + } + + break; + case MetaManipulation.Type.Rsp: + if (_rsp.TryGetValue(identifier.Rsp.Identifier, out var oldRsp)) + { + oldValue = new MetaManipulation(new RspManipulation(identifier.Rsp.Identifier, oldRsp)); + return true; + } + + break; + case MetaManipulation.Type.GlobalEqp: + if (_globalEqp.TryGetValue(identifier.GlobalEqp, out var oldGlobalEqp)) + { + oldValue = new MetaManipulation(oldGlobalEqp); + return true; + } + + break; + } + + oldValue = default; + return false; + } + + public void SetTo(MetaDictionary other) + { + _imc.SetTo(other._imc); + _eqp.SetTo(other._eqp); + _eqdp.SetTo(other._eqdp); + _est.SetTo(other._est); + _rsp.SetTo(other._rsp); + _gmp.SetTo(other._gmp); + _globalEqp.SetTo(other._globalEqp); + Count = _imc.Count + _eqp.Count + _eqdp.Count + _est.Count + _rsp.Count + _gmp.Count + _globalEqp.Count; + } + + public MetaDictionary Clone() + { + var ret = new MetaDictionary(); + ret.SetTo(this); + return ret; + } private class Converter : JsonConverter { @@ -67,7 +285,7 @@ public sealed class MetaDictionary : HashSet var identifier = ImcIdentifier.FromJson(manip); var entry = manip["Entry"]?.ToObject(); if (identifier.HasValue && entry.HasValue) - dict.Add(new MetaManipulation(new ImcManipulation(identifier.Value, entry.Value))); + dict.TryAdd(identifier.Value, entry.Value); else Penumbra.Log.Warning("Invalid IMC Manipulation encountered."); break; @@ -77,7 +295,7 @@ public sealed class MetaDictionary : HashSet var identifier = EqdpIdentifier.FromJson(manip); var entry = (EqdpEntry?)manip["Entry"]?.ToObject(); if (identifier.HasValue && entry.HasValue) - dict.Add(new MetaManipulation(new EqdpManipulation(identifier.Value, entry.Value))); + dict.TryAdd(identifier.Value, entry.Value); else Penumbra.Log.Warning("Invalid EQDP Manipulation encountered."); break; @@ -87,7 +305,7 @@ public sealed class MetaDictionary : HashSet var identifier = EqpIdentifier.FromJson(manip); var entry = (EqpEntry?)manip["Entry"]?.ToObject(); if (identifier.HasValue && entry.HasValue) - dict.Add(new MetaManipulation(new EqpManipulation(identifier.Value, entry.Value))); + dict.TryAdd(identifier.Value, entry.Value); else Penumbra.Log.Warning("Invalid EQP Manipulation encountered."); break; @@ -97,7 +315,7 @@ public sealed class MetaDictionary : HashSet var identifier = EstIdentifier.FromJson(manip); var entry = manip["Entry"]?.ToObject(); if (identifier.HasValue && entry.HasValue) - dict.Add(new MetaManipulation(new EstManipulation(identifier.Value, entry.Value))); + dict.TryAdd(identifier.Value, entry.Value); else Penumbra.Log.Warning("Invalid EST Manipulation encountered."); break; @@ -107,7 +325,7 @@ public sealed class MetaDictionary : HashSet var identifier = GmpIdentifier.FromJson(manip); var entry = manip["Entry"]?.ToObject(); if (identifier.HasValue && entry.HasValue) - dict.Add(new MetaManipulation(new GmpManipulation(identifier.Value, entry.Value))); + dict.TryAdd(identifier.Value, entry.Value); else Penumbra.Log.Warning("Invalid GMP Manipulation encountered."); break; @@ -117,7 +335,7 @@ public sealed class MetaDictionary : HashSet var identifier = RspIdentifier.FromJson(manip); var entry = manip["Entry"]?.ToObject(); if (identifier.HasValue && entry.HasValue) - dict.Add(new MetaManipulation(new RspManipulation(identifier.Value, entry.Value))); + dict.TryAdd(identifier.Value, entry.Value); else Penumbra.Log.Warning("Invalid RSP Manipulation encountered."); break; @@ -126,7 +344,7 @@ public sealed class MetaDictionary : HashSet { var identifier = GlobalEqpManipulation.FromJson(manip); if (identifier.HasValue) - dict.Add(new MetaManipulation(identifier.Value)); + dict.TryAdd(identifier.Value); else Penumbra.Log.Warning("Invalid Global EQP Manipulation encountered."); break; diff --git a/Penumbra/Mods/Editor/IMod.cs b/Penumbra/Mods/Editor/IMod.cs index d4c881e9..06c31846 100644 --- a/Penumbra/Mods/Editor/IMod.cs +++ b/Penumbra/Mods/Editor/IMod.cs @@ -8,7 +8,7 @@ namespace Penumbra.Mods.Editor; public record struct AppliedModData( Dictionary FileRedirections, - HashSet Manipulations) + MetaDictionary Manipulations) { public static readonly AppliedModData Empty = new([], []); } diff --git a/Penumbra/Mods/Editor/ModMerger.cs b/Penumbra/Mods/Editor/ModMerger.cs index f5fc9cd7..4faced80 100644 --- a/Penumbra/Mods/Editor/ModMerger.cs +++ b/Penumbra/Mods/Editor/ModMerger.cs @@ -158,7 +158,7 @@ public class ModMerger : IDisposable { var redirections = option.Files.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); var swaps = option.FileSwaps.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); - var manips = option.Manipulations.ToHashSet(); + var manips = option.Manipulations.Clone(); foreach (var originalOption in mergeOptions) { diff --git a/Penumbra/Mods/Editor/ModMetaEditor.cs b/Penumbra/Mods/Editor/ModMetaEditor.cs index 86853755..45d9f8a1 100644 --- a/Penumbra/Mods/Editor/ModMetaEditor.cs +++ b/Penumbra/Mods/Editor/ModMetaEditor.cs @@ -145,7 +145,7 @@ public class ModMetaEditor(ModManager modManager) if (!Changes) return; - modManager.OptionEditor.SetManipulations(container, Recombine().ToHashSet()); + modManager.OptionEditor.SetManipulations(container, [..Recombine()]); Changes = false; } diff --git a/Penumbra/Mods/Groups/IModGroup.cs b/Penumbra/Mods/Groups/IModGroup.cs index fcc8c093..00f47e25 100644 --- a/Penumbra/Mods/Groups/IModGroup.cs +++ b/Penumbra/Mods/Groups/IModGroup.cs @@ -43,7 +43,7 @@ public interface IModGroup public IModGroupEditDrawer EditDrawer(ModGroupEditDrawer editDrawer); - public void AddData(Setting setting, Dictionary redirections, HashSet manipulations); + public void AddData(Setting setting, Dictionary redirections, MetaDictionary manipulations); public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems); /// Ensure that a value is valid for a group. diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs index b336203d..383bc9fd 100644 --- a/Penumbra/Mods/Groups/ImcModGroup.cs +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -99,7 +99,7 @@ public class ImcModGroup(Mod mod) : IModGroup => new(Identifier.ObjectType, Identifier.BodySlot, Identifier.PrimaryId, Identifier.SecondaryId.Id, variant.Id, Identifier.EquipSlot, DefaultEntry with { AttributeMask = mask }); - public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) + public void AddData(Setting setting, Dictionary redirections, MetaDictionary manipulations) { if (IsDisabled(setting)) return; diff --git a/Penumbra/Mods/Groups/MultiModGroup.cs b/Penumbra/Mods/Groups/MultiModGroup.cs index 7816d628..220d0a7c 100644 --- a/Penumbra/Mods/Groups/MultiModGroup.cs +++ b/Penumbra/Mods/Groups/MultiModGroup.cs @@ -116,7 +116,7 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup public IModGroupEditDrawer EditDrawer(ModGroupEditDrawer editDrawer) => new MultiModGroupEditDrawer(editDrawer, this); - public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) + public void AddData(Setting setting, Dictionary redirections, MetaDictionary manipulations) { foreach (var (option, index) in OptionData.WithIndex().OrderByDescending(o => o.Value.Priority)) { diff --git a/Penumbra/Mods/Groups/SingleModGroup.cs b/Penumbra/Mods/Groups/SingleModGroup.cs index a6ebd846..a559d609 100644 --- a/Penumbra/Mods/Groups/SingleModGroup.cs +++ b/Penumbra/Mods/Groups/SingleModGroup.cs @@ -101,7 +101,7 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup public IModGroupEditDrawer EditDrawer(ModGroupEditDrawer editDrawer) => new SingleModGroupEditDrawer(editDrawer, this); - public void AddData(Setting setting, Dictionary redirections, HashSet manipulations) + public void AddData(Setting setting, Dictionary redirections, MetaDictionary manipulations) { if (OptionData.Count == 0) return; diff --git a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs index af5b2d3a..67a5d007 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs @@ -23,7 +23,7 @@ public class ItemSwapContainer public IReadOnlyDictionary ModRedirections => _appliedModData.FileRedirections; - public IReadOnlySet ModManipulations + public MetaDictionary ModManipulations => _appliedModData.Manipulations; public readonly List Swaps = []; @@ -42,9 +42,10 @@ public class ItemSwapContainer NoSwaps, } - public bool WriteMod(ModManager manager, Mod mod, IModDataContainer container, WriteType writeType = WriteType.NoSwaps, DirectoryInfo? directory = null) + public bool WriteMod(ModManager manager, Mod mod, IModDataContainer container, WriteType writeType = WriteType.NoSwaps, + DirectoryInfo? directory = null) { - var convertedManips = new HashSet(Swaps.Count); + var convertedManips = new MetaDictionary(); var convertedFiles = new Dictionary(Swaps.Count); var convertedSwaps = new Dictionary(Swaps.Count); directory ??= mod.ModPath; @@ -98,13 +99,9 @@ public class ItemSwapContainer { Clear(); if (mod == null || mod.Index < 0) - { - _appliedModData = AppliedModData.Empty; - } + _appliedModData = AppliedModData.Empty; else - { _appliedModData = ModSettings.GetResolveData(mod, settings); - } } public ItemSwapContainer(MetaFileManager manager, ObjectIdentification identifier) @@ -121,7 +118,13 @@ public class ItemSwapContainer private Func MetaResolver(ModCollection? collection) { - var set = collection?.MetaCache?.Manipulations.ToHashSet() ?? _appliedModData.Manipulations; + if (collection?.MetaCache?.Manipulations is { } cache) + { + MetaDictionary dict = [.. cache]; + return m => dict.TryGetValue(m, out var a) ? a : m; + } + + var set = _appliedModData.Manipulations; return m => set.TryGetValue(m, out var a) ? a : m; } diff --git a/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs index 55e01015..01092862 100644 --- a/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs @@ -142,7 +142,7 @@ public class ModGroupEditor( } /// Set the meta manipulations for a given option. Replaces existing manipulations. - public void SetManipulations(IModDataContainer subMod, HashSet manipulations, SaveType saveType = SaveType.Queue) + public void SetManipulations(IModDataContainer subMod, MetaDictionary manipulations, SaveType saveType = SaveType.Queue) { if (subMod.Manipulations.Count == manipulations.Count && subMod.Manipulations.All(m => manipulations.TryGetValue(m, out var old) && old.EntryEquals(m))) diff --git a/Penumbra/Mods/Mod.cs b/Penumbra/Mods/Mod.cs index 783ef3e6..a7f87dcd 100644 --- a/Penumbra/Mods/Mod.cs +++ b/Penumbra/Mods/Mod.cs @@ -71,7 +71,7 @@ public sealed class Mod : IMod return AppliedModData.Empty; var dictRedirections = new Dictionary(TotalFileCount); - var setManips = new HashSet(TotalManipulations); + var setManips = new MetaDictionary(); foreach (var (group, groupIndex) in Groups.WithIndex().OrderByDescending(g => g.Value.Priority)) { var config = settings.Settings[groupIndex]; diff --git a/Penumbra/Mods/SubMods/DefaultSubMod.cs b/Penumbra/Mods/SubMods/DefaultSubMod.cs index 5a300a48..dcd33610 100644 --- a/Penumbra/Mods/SubMods/DefaultSubMod.cs +++ b/Penumbra/Mods/SubMods/DefaultSubMod.cs @@ -21,7 +21,7 @@ public class DefaultSubMod(IMod mod) : IModDataContainer IModGroup? IModDataContainer.Group => null; - public void AddTo(Dictionary redirections, HashSet manipulations) + public void AddTo(Dictionary redirections, MetaDictionary manipulations) => SubMod.AddContainerTo(this, redirections, manipulations); public string GetName() diff --git a/Penumbra/Mods/SubMods/OptionSubMod.cs b/Penumbra/Mods/SubMods/OptionSubMod.cs index 378f6dc8..ed7b6ff8 100644 --- a/Penumbra/Mods/SubMods/OptionSubMod.cs +++ b/Penumbra/Mods/SubMods/OptionSubMod.cs @@ -35,7 +35,7 @@ public abstract class OptionSubMod(IModGroup group) : IModOption, IModDataContai public Dictionary FileSwaps { get; set; } = []; public MetaDictionary Manipulations { get; set; } = []; - public void AddDataTo(Dictionary redirections, HashSet manipulations) + public void AddDataTo(Dictionary redirections, MetaDictionary manipulations) => SubMod.AddContainerTo(this, redirections, manipulations); public string GetName() diff --git a/Penumbra/Mods/SubMods/SubMod.cs b/Penumbra/Mods/SubMods/SubMod.cs index b984b570..06a924c8 100644 --- a/Penumbra/Mods/SubMods/SubMod.cs +++ b/Penumbra/Mods/SubMods/SubMod.cs @@ -21,7 +21,7 @@ public static class SubMod /// Add all unique meta manipulations, file redirections and then file swaps from a ModDataContainer to the given sets. Skip any keys that are already contained. [MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)] public static void AddContainerTo(IModDataContainer container, Dictionary redirections, - HashSet manipulations) + MetaDictionary manipulations) { foreach (var (path, file) in container.Files) redirections.TryAdd(path, file); From 13156a58e92ab64965879f994eb9f1aec77d8f12 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 8 Jun 2024 16:09:34 +0200 Subject: [PATCH 233/433] Remove unused functions. --- Penumbra/Mods/TemporaryMod.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Penumbra/Mods/TemporaryMod.cs b/Penumbra/Mods/TemporaryMod.cs index e0a03c92..a715f786 100644 --- a/Penumbra/Mods/TemporaryMod.cs +++ b/Penumbra/Mods/TemporaryMod.cs @@ -51,12 +51,6 @@ public class TemporaryMod : IMod public TemporaryMod() => Default = new DefaultSubMod(this); - public void SetFile(Utf8GamePath gamePath, FullPath fullPath) - => Default.Files[gamePath] = fullPath; - - public bool SetManipulation(MetaManipulation manip) - => Default.Manipulations.Remove(manip) | Default.Manipulations.Add(manip); - public void SetAll(Dictionary dict, MetaDictionary manips) { Default.Files = dict; From e0339160e908276a97963fd498f9e785a88ee690 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 8 Jun 2024 16:25:08 +0200 Subject: [PATCH 234/433] Start removing MetaManipulation functions. --- Penumbra/Meta/Manipulations/MetaDictionary.cs | 40 +++++++++---------- .../Manager/OptionEditor/ModGroupEditor.cs | 3 +- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/Penumbra/Meta/Manipulations/MetaDictionary.cs b/Penumbra/Meta/Manipulations/MetaDictionary.cs index b0b7f011..b9d7990d 100644 --- a/Penumbra/Meta/Manipulations/MetaDictionary.cs +++ b/Penumbra/Meta/Manipulations/MetaDictionary.cs @@ -124,28 +124,28 @@ public sealed class MetaDictionary : IEnumerable return true; } - public bool Remove(MetaManipulation manip) + public void UnionWith(MetaDictionary manips) { - var ret = manip.ManipulationType switch - { - MetaManipulation.Type.Imc => _imc.Remove(manip.Imc.Identifier), - MetaManipulation.Type.Eqdp => _eqdp.Remove(manip.Eqdp.Identifier), - MetaManipulation.Type.Eqp => _eqp.Remove(manip.Eqp.Identifier), - MetaManipulation.Type.Est => _est.Remove(manip.Est.Identifier), - MetaManipulation.Type.Gmp => _gmp.Remove(manip.Gmp.Identifier), - MetaManipulation.Type.Rsp => _rsp.Remove(manip.Rsp.Identifier), - MetaManipulation.Type.GlobalEqp => _globalEqp.Remove(manip.GlobalEqp), - _ => false, - }; - if (ret) - --Count; - return ret; - } + foreach (var (identifier, entry) in manips._imc) + TryAdd(identifier, entry); - public void UnionWith(IEnumerable manips) - { - foreach (var manip in manips) - Add(manip); + foreach (var (identifier, entry) in manips._eqp) + TryAdd(identifier, entry); + + foreach (var (identifier, entry) in manips._eqdp) + TryAdd(identifier, entry); + + foreach (var (identifier, entry) in manips._gmp) + TryAdd(identifier, entry); + + foreach (var (identifier, entry) in manips._rsp) + TryAdd(identifier, entry); + + foreach (var (identifier, entry) in manips._est) + TryAdd(identifier, entry); + + foreach (var identifier in manips._globalEqp) + TryAdd(identifier); } public bool TryGetValue(MetaManipulation identifier, out MetaManipulation oldValue) diff --git a/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs index 01092862..594ec9d2 100644 --- a/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs @@ -144,8 +144,7 @@ public class ModGroupEditor( /// Set the meta manipulations for a given option. Replaces existing manipulations. public void SetManipulations(IModDataContainer subMod, MetaDictionary manipulations, SaveType saveType = SaveType.Queue) { - if (subMod.Manipulations.Count == manipulations.Count - && subMod.Manipulations.All(m => manipulations.TryGetValue(m, out var old) && old.EntryEquals(m))) + if (subMod.Manipulations.Equals(manipulations)) return; communicator.ModOptionChanged.Invoke(ModOptionChangeType.PrepareChange, (Mod)subMod.Mod, subMod.Group, null, subMod, -1); From 5ca9e63a2a5f1f44dd9c5859578c3684bc6df4e6 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 8 Jun 2024 16:34:51 +0200 Subject: [PATCH 235/433] Use internal entries. --- Penumbra/Meta/Manipulations/Eqdp.cs | 6 +- Penumbra/Meta/Manipulations/MetaDictionary.cs | 70 ++++++++++++------- Penumbra/Mods/ModCreator.cs | 4 +- 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/Penumbra/Meta/Manipulations/Eqdp.cs b/Penumbra/Meta/Manipulations/Eqdp.cs index 6d6942e6..a986d475 100644 --- a/Penumbra/Meta/Manipulations/Eqdp.cs +++ b/Penumbra/Meta/Manipulations/Eqdp.cs @@ -71,13 +71,13 @@ public readonly record struct EqdpIdentifier(PrimaryId SetId, EquipSlot Slot, Ge } } -public readonly record struct InternalEqdpEntry(bool Model, bool Material) +public readonly record struct EqdpEntryInternal(bool Model, bool Material) { - private InternalEqdpEntry((bool, bool) val) + private EqdpEntryInternal((bool, bool) val) : this(val.Item1, val.Item2) { } - public InternalEqdpEntry(EqdpEntry entry, EquipSlot slot) + public EqdpEntryInternal(EqdpEntry entry, EquipSlot slot) : this(entry.ToBits(slot)) { } diff --git a/Penumbra/Meta/Manipulations/MetaDictionary.cs b/Penumbra/Meta/Manipulations/MetaDictionary.cs index b9d7990d..941cdf34 100644 --- a/Penumbra/Meta/Manipulations/MetaDictionary.cs +++ b/Penumbra/Meta/Manipulations/MetaDictionary.cs @@ -9,13 +9,13 @@ namespace Penumbra.Meta.Manipulations; [JsonConverter(typeof(Converter))] public sealed class MetaDictionary : IEnumerable { - private readonly Dictionary _imc = []; - private readonly Dictionary _eqp = []; - private readonly Dictionary _eqdp = []; - private readonly Dictionary _est = []; - private readonly Dictionary _rsp = []; - private readonly Dictionary _gmp = []; - private readonly HashSet _globalEqp = []; + private readonly Dictionary _imc = []; + private readonly Dictionary _eqp = []; + private readonly Dictionary _eqdp = []; + private readonly Dictionary _est = []; + private readonly Dictionary _rsp = []; + private readonly Dictionary _gmp = []; + private readonly HashSet _globalEqp = []; public int Count { get; private set; } @@ -30,10 +30,20 @@ public sealed class MetaDictionary : IEnumerable _globalEqp.Clear(); } + public bool Equals(MetaDictionary other) + => Count == other.Count + && _imc.SetEquals(other._imc) + && _eqp.SetEquals(other._eqp) + && _eqdp.SetEquals(other._eqdp) + && _est.SetEquals(other._est) + && _rsp.SetEquals(other._rsp) + && _gmp.SetEquals(other._gmp) + && _globalEqp.SetEquals(other._globalEqp); + public IEnumerator GetEnumerator() => _imc.Select(kvp => new MetaManipulation(new ImcManipulation(kvp.Key, kvp.Value))) - .Concat(_eqp.Select(kvp => new MetaManipulation(new EqpManipulation(kvp.Key, kvp.Value)))) - .Concat(_eqdp.Select(kvp => new MetaManipulation(new EqdpManipulation(kvp.Key, kvp.Value)))) + .Concat(_eqp.Select(kvp => new MetaManipulation(new EqpManipulation(kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))))) + .Concat(_eqdp.Select(kvp => new MetaManipulation(new EqdpManipulation(kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))))) .Concat(_est.Select(kvp => new MetaManipulation(new EstManipulation(kvp.Key, kvp.Value)))) .Concat(_rsp.Select(kvp => new MetaManipulation(new RspManipulation(kvp.Key, kvp.Value)))) .Concat(_gmp.Select(kvp => new MetaManipulation(new GmpManipulation(kvp.Key, kvp.Value)))) @@ -47,8 +57,8 @@ public sealed class MetaDictionary : IEnumerable var ret = manip.ManipulationType switch { MetaManipulation.Type.Imc => _imc.TryAdd(manip.Imc.Identifier, manip.Imc.Entry), - MetaManipulation.Type.Eqdp => _eqdp.TryAdd(manip.Eqdp.Identifier, manip.Eqdp.Entry), - MetaManipulation.Type.Eqp => _eqp.TryAdd(manip.Eqp.Identifier, manip.Eqp.Entry), + MetaManipulation.Type.Eqdp => _eqdp.TryAdd(manip.Eqdp.Identifier, new EqdpEntryInternal(manip.Eqdp.Entry, manip.Eqdp.Slot)), + MetaManipulation.Type.Eqp => _eqp.TryAdd(manip.Eqp.Identifier, new EqpEntryInternal(manip.Eqp.Entry, manip.Eqp.Slot)), MetaManipulation.Type.Est => _est.TryAdd(manip.Est.Identifier, manip.Est.Entry), MetaManipulation.Type.Gmp => _gmp.TryAdd(manip.Gmp.Identifier, manip.Gmp.Entry), MetaManipulation.Type.Rsp => _rsp.TryAdd(manip.Rsp.Identifier, manip.Rsp.Entry), @@ -71,22 +81,10 @@ public sealed class MetaDictionary : IEnumerable } public bool TryAdd(EqpIdentifier identifier, EqpEntry entry) - { - if (!_eqp.TryAdd(identifier, entry)) - return false; - - ++Count; - return true; - } + => TryAdd(identifier, new EqpEntryInternal(entry, identifier.Slot)); public bool TryAdd(EqdpIdentifier identifier, EqdpEntry entry) - { - if (!_eqdp.TryAdd(identifier, entry)) - return false; - - ++Count; - return true; - } + => TryAdd(identifier, new EqdpEntryInternal(entry, identifier.Slot)); public bool TryAdd(EstIdentifier identifier, EstEntry entry) { @@ -163,7 +161,7 @@ public sealed class MetaDictionary : IEnumerable case MetaManipulation.Type.Eqdp: if (_eqp.TryGetValue(identifier.Eqp.Identifier, out var oldEqdp)) { - oldValue = new MetaManipulation(new EqpManipulation(identifier.Eqp.Identifier, oldEqdp)); + oldValue = new MetaManipulation(new EqpManipulation(identifier.Eqp.Identifier, oldEqdp.ToEntry(identifier.Eqp.Slot))); return true; } @@ -171,7 +169,7 @@ public sealed class MetaDictionary : IEnumerable case MetaManipulation.Type.Eqp: if (_eqdp.TryGetValue(identifier.Eqdp.Identifier, out var oldEqp)) { - oldValue = new MetaManipulation(new EqdpManipulation(identifier.Eqdp.Identifier, oldEqp)); + oldValue = new MetaManipulation(new EqdpManipulation(identifier.Eqdp.Identifier, oldEqp.ToEntry(identifier.Eqdp.Slot))); return true; } @@ -355,4 +353,22 @@ public sealed class MetaDictionary : IEnumerable return dict; } } + + private bool TryAdd(EqpIdentifier identifier, EqpEntryInternal entry) + { + if (!_eqp.TryAdd(identifier, entry)) + return false; + + ++Count; + return true; + } + + private bool TryAdd(EqdpIdentifier identifier, EqdpEntryInternal entry) + { + if (!_eqdp.TryAdd(identifier, entry)) + return false; + + ++Count; + return true; + } } diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index ed4245c4..0035fd41 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -198,7 +198,7 @@ public partial class ModCreator( Penumbra.Log.Verbose( $"Incorporating {file} as Metadata file of {meta.MetaManipulations.Count} manipulations {deleteString}"); deleteList.Add(file.FullName); - option.Manipulations.UnionWith(meta.MetaManipulations); + option.Manipulations.UnionWith([.. meta.MetaManipulations]); } else if (ext1 == ".rgsp" || ext2 == ".rgsp") { @@ -212,7 +212,7 @@ public partial class ModCreator( $"Incorporating {file} as racial scaling file of {rgsp.MetaManipulations.Count} manipulations {deleteString}"); deleteList.Add(file.FullName); - option.Manipulations.UnionWith(rgsp.MetaManipulations); + option.Manipulations.UnionWith([.. rgsp.MetaManipulations]); } } catch (Exception e) From 0445ed0ef9ed456a45ac100007f1e847ecd2e68e Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 8 Jun 2024 19:09:46 +0200 Subject: [PATCH 236/433] Remove TryGetValue(MetaManipulation) from MetaDictionary. --- Penumbra/Meta/Files/EqdpFile.cs | 4 + Penumbra/Meta/Files/EqpGmpFile.cs | 4 + Penumbra/Meta/Files/EstFile.cs | 3 + Penumbra/Meta/Files/ImcFile.cs | 3 + Penumbra/Meta/Manipulations/Eqdp.cs | 5 +- Penumbra/Meta/Manipulations/MetaDictionary.cs | 85 ++++-------- Penumbra/Mods/ItemSwap/EquipmentSwap.cs | 131 +++++++++--------- Penumbra/Mods/ItemSwap/ItemSwap.cs | 21 ++- Penumbra/Mods/ItemSwap/ItemSwapContainer.cs | 23 ++- Penumbra/Mods/ItemSwap/Swaps.cs | 82 +++++++---- Penumbra/UI/AdvancedWindow/ItemSwapTab.cs | 4 +- 11 files changed, 182 insertions(+), 183 deletions(-) diff --git a/Penumbra/Meta/Files/EqdpFile.cs b/Penumbra/Meta/Files/EqdpFile.cs index c76c4efd..e46e82e9 100644 --- a/Penumbra/Meta/Files/EqdpFile.cs +++ b/Penumbra/Meta/Files/EqdpFile.cs @@ -2,6 +2,7 @@ using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.Services; using Penumbra.Interop.Structs; +using Penumbra.Meta.Manipulations; using Penumbra.String.Functions; namespace Penumbra.Meta.Files; @@ -126,4 +127,7 @@ public sealed unsafe class ExpandedEqdpFile : MetaBaseFile public static EqdpEntry GetDefault(MetaFileManager manager, GenderRace raceCode, bool accessory, PrimaryId primaryId) => GetDefault(manager, CharacterUtility.ReverseIndices[(int)CharacterUtilityData.EqdpIdx(raceCode, accessory)], primaryId); + + public static EqdpEntry GetDefault(MetaFileManager manager, EqdpIdentifier identifier) + => GetDefault(manager, CharacterUtility.ReverseIndices[(int)identifier.FileIndex()], identifier.SetId); } diff --git a/Penumbra/Meta/Files/EqpGmpFile.cs b/Penumbra/Meta/Files/EqpGmpFile.cs index 70067c2b..17541c4f 100644 --- a/Penumbra/Meta/Files/EqpGmpFile.cs +++ b/Penumbra/Meta/Files/EqpGmpFile.cs @@ -1,6 +1,7 @@ using Penumbra.GameData.Structs; using Penumbra.Interop.Services; using Penumbra.Interop.Structs; +using Penumbra.Meta.Manipulations; using Penumbra.String.Functions; namespace Penumbra.Meta.Files; @@ -164,6 +165,9 @@ public sealed class ExpandedGmpFile : ExpandedEqpGmpBase, IEnumerable public static GmpEntry GetDefault(MetaFileManager manager, PrimaryId primaryIdx) => new() { Value = GetDefaultInternal(manager, InternalIndex, primaryIdx, GmpEntry.Default.Value) }; + public static GmpEntry GetDefault(MetaFileManager manager, GmpIdentifier identifier) + => new() { Value = GetDefaultInternal(manager, InternalIndex, identifier.SetId, GmpEntry.Default.Value) }; + public void Reset(IEnumerable entries) { foreach (var entry in entries) diff --git a/Penumbra/Meta/Files/EstFile.cs b/Penumbra/Meta/Files/EstFile.cs index ee38ea1e..f3860416 100644 --- a/Penumbra/Meta/Files/EstFile.cs +++ b/Penumbra/Meta/Files/EstFile.cs @@ -184,4 +184,7 @@ public sealed unsafe class EstFile : MetaBaseFile public static EstEntry GetDefault(MetaFileManager manager, EstType estType, GenderRace genderRace, PrimaryId primaryId) => GetDefault(manager, (MetaIndex)estType, genderRace, primaryId); + + public static EstEntry GetDefault(MetaFileManager manager, EstIdentifier identifier) + => GetDefault(manager, identifier.FileIndex(), identifier.GenderRace, identifier.SetId); } diff --git a/Penumbra/Meta/Files/ImcFile.cs b/Penumbra/Meta/Files/ImcFile.cs index 5d704cf8..892f5b44 100644 --- a/Penumbra/Meta/Files/ImcFile.cs +++ b/Penumbra/Meta/Files/ImcFile.cs @@ -65,6 +65,9 @@ public unsafe class ImcFile : MetaBaseFile return ptr == null ? new ImcEntry() : *ptr; } + public ImcEntry GetEntry(EquipSlot slot, Variant variantIdx) + => GetEntry(PartIndex(slot), variantIdx); + public ImcEntry GetEntry(int partIdx, Variant variantIdx, out bool exists) { var ptr = VariantPtr(Data, partIdx, variantIdx); diff --git a/Penumbra/Meta/Manipulations/Eqdp.cs b/Penumbra/Meta/Manipulations/Eqdp.cs index a986d475..6306f419 100644 --- a/Penumbra/Meta/Manipulations/Eqdp.cs +++ b/Penumbra/Meta/Manipulations/Eqdp.cs @@ -71,7 +71,7 @@ public readonly record struct EqdpIdentifier(PrimaryId SetId, EquipSlot Slot, Ge } } -public readonly record struct EqdpEntryInternal(bool Model, bool Material) +public readonly record struct EqdpEntryInternal(bool Material, bool Model) { private EqdpEntryInternal((bool, bool) val) : this(val.Item1, val.Item2) @@ -81,7 +81,6 @@ public readonly record struct EqdpEntryInternal(bool Model, bool Material) : this(entry.ToBits(slot)) { } - public EqdpEntry ToEntry(EquipSlot slot) - => Eqdp.FromSlotAndBits(slot, Model, Material); + => Eqdp.FromSlotAndBits(slot, Material, Model); } diff --git a/Penumbra/Meta/Manipulations/MetaDictionary.cs b/Penumbra/Meta/Manipulations/MetaDictionary.cs index 941cdf34..51149e3b 100644 --- a/Penumbra/Meta/Manipulations/MetaDictionary.cs +++ b/Penumbra/Meta/Manipulations/MetaDictionary.cs @@ -52,6 +52,19 @@ public sealed class MetaDictionary : IEnumerable IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + public bool Add(IMetaIdentifier identifier, object entry) + => identifier switch + { + EqdpIdentifier eqdpIdentifier => entry is EqdpEntryInternal e && Add(eqdpIdentifier, e), + EqpIdentifier eqpIdentifier => entry is EqpEntryInternal e && Add(eqpIdentifier, e), + EstIdentifier estIdentifier => entry is EstEntry e && Add(estIdentifier, e), + GlobalEqpManipulation globalEqpManipulation => Add(globalEqpManipulation), + GmpIdentifier gmpIdentifier => entry is GmpEntry e && Add(gmpIdentifier, e), + ImcIdentifier imcIdentifier => entry is ImcEntry e && Add(imcIdentifier, e), + RspIdentifier rspIdentifier => entry is RspEntry e && Add(rspIdentifier, e), + _ => false, + }; + public bool Add(MetaManipulation manip) { var ret = manip.ManipulationType switch @@ -146,71 +159,23 @@ public sealed class MetaDictionary : IEnumerable TryAdd(identifier); } - public bool TryGetValue(MetaManipulation identifier, out MetaManipulation oldValue) - { - switch (identifier.ManipulationType) - { - case MetaManipulation.Type.Imc: - if (_imc.TryGetValue(identifier.Imc.Identifier, out var oldImc)) - { - oldValue = new MetaManipulation(new ImcManipulation(identifier.Imc.Identifier, oldImc)); - return true; - } + public bool TryGetValue(EstIdentifier identifier, out EstEntry value) + => _est.TryGetValue(identifier, out value); - break; - case MetaManipulation.Type.Eqdp: - if (_eqp.TryGetValue(identifier.Eqp.Identifier, out var oldEqdp)) - { - oldValue = new MetaManipulation(new EqpManipulation(identifier.Eqp.Identifier, oldEqdp.ToEntry(identifier.Eqp.Slot))); - return true; - } + public bool TryGetValue(EqpIdentifier identifier, out EqpEntryInternal value) + => _eqp.TryGetValue(identifier, out value); - break; - case MetaManipulation.Type.Eqp: - if (_eqdp.TryGetValue(identifier.Eqdp.Identifier, out var oldEqp)) - { - oldValue = new MetaManipulation(new EqdpManipulation(identifier.Eqdp.Identifier, oldEqp.ToEntry(identifier.Eqdp.Slot))); - return true; - } + public bool TryGetValue(EqdpIdentifier identifier, out EqdpEntryInternal value) + => _eqdp.TryGetValue(identifier, out value); - break; - case MetaManipulation.Type.Est: - if (_est.TryGetValue(identifier.Est.Identifier, out var oldEst)) - { - oldValue = new MetaManipulation(new EstManipulation(identifier.Est.Identifier, oldEst)); - return true; - } + public bool TryGetValue(GmpIdentifier identifier, out GmpEntry value) + => _gmp.TryGetValue(identifier, out value); - break; - case MetaManipulation.Type.Gmp: - if (_gmp.TryGetValue(identifier.Gmp.Identifier, out var oldGmp)) - { - oldValue = new MetaManipulation(new GmpManipulation(identifier.Gmp.Identifier, oldGmp)); - return true; - } + public bool TryGetValue(RspIdentifier identifier, out RspEntry value) + => _rsp.TryGetValue(identifier, out value); - break; - case MetaManipulation.Type.Rsp: - if (_rsp.TryGetValue(identifier.Rsp.Identifier, out var oldRsp)) - { - oldValue = new MetaManipulation(new RspManipulation(identifier.Rsp.Identifier, oldRsp)); - return true; - } - - break; - case MetaManipulation.Type.GlobalEqp: - if (_globalEqp.TryGetValue(identifier.GlobalEqp, out var oldGlobalEqp)) - { - oldValue = new MetaManipulation(oldGlobalEqp); - return true; - } - - break; - } - - oldValue = default; - return false; - } + public bool TryGetValue(ImcIdentifier identifier, out ImcEntry value) + => _imc.TryGetValue(identifier, out value); public void SetTo(MetaDictionary other) { diff --git a/Penumbra/Mods/ItemSwap/EquipmentSwap.cs b/Penumbra/Mods/ItemSwap/EquipmentSwap.cs index 3efee857..e42a1d31 100644 --- a/Penumbra/Mods/ItemSwap/EquipmentSwap.cs +++ b/Penumbra/Mods/ItemSwap/EquipmentSwap.cs @@ -1,4 +1,4 @@ -using Penumbra.Api.Enums; +using Penumbra.Api.Enums; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.GameData.Files; @@ -16,32 +16,19 @@ public static class EquipmentSwap private static EquipSlot[] ConvertSlots(EquipSlot slot, bool rFinger, bool lFinger) { if (slot != EquipSlot.RFinger) - return new[] - { - slot, - }; + return [slot]; return rFinger ? lFinger - ? new[] - { - EquipSlot.RFinger, - EquipSlot.LFinger, - } - : new[] - { - EquipSlot.RFinger, - } + ? [EquipSlot.RFinger, EquipSlot.LFinger] + : [EquipSlot.RFinger] : lFinger - ? new[] - { - EquipSlot.LFinger, - } - : Array.Empty(); + ? [EquipSlot.LFinger] + : []; } public static EquipItem[] CreateTypeSwap(MetaFileManager manager, ObjectIdentification identifier, List swaps, - Func redirections, Func manips, + Func redirections, MetaDictionary manips, EquipSlot slotFrom, EquipItem itemFrom, EquipSlot slotTo, EquipItem itemTo) { LookupItem(itemFrom, out var actualSlotFrom, out var idFrom, out var variantFrom); @@ -50,11 +37,14 @@ public static class EquipmentSwap throw new ItemSwap.InvalidItemTypeException(); var (imcFileFrom, variants, affectedItems) = GetVariants(manager, identifier, slotFrom, idFrom, idTo, variantFrom); - var imcManip = new ImcManipulation(slotTo, variantTo.Id, idTo.Id, default); - var imcFileTo = new ImcFile(manager, imcManip.Identifier); + var imcIdentifierTo = new ImcIdentifier(slotTo, idTo, variantTo); + var imcFileTo = new ImcFile(manager, imcIdentifierTo); + var imcEntry = manips.TryGetValue(imcIdentifierTo, out var entry) + ? entry + : imcFileTo.GetEntry(imcIdentifierTo.EquipSlot, imcIdentifierTo.Variant); + var mtrlVariantTo = imcEntry.MaterialId; var skipFemale = false; var skipMale = false; - var mtrlVariantTo = manips(imcManip.Copy(imcFileTo.GetEntry(ImcFile.PartIndex(slotTo), variantTo.Id))).Imc.Entry.MaterialId; foreach (var gr in Enum.GetValues()) { switch (gr.Split().Item1) @@ -99,7 +89,7 @@ public static class EquipmentSwap } public static EquipItem[] CreateItemSwap(MetaFileManager manager, ObjectIdentification identifier, List swaps, - Func redirections, Func manips, EquipItem itemFrom, + Func redirections, MetaDictionary manips, EquipItem itemFrom, EquipItem itemTo, bool rFinger = true, bool lFinger = true) { // Check actual ids, variants and slots. We only support using the same slot. @@ -120,8 +110,12 @@ public static class EquipmentSwap foreach (var slot in ConvertSlots(slotFrom, rFinger, lFinger)) { (var imcFileFrom, var variants, affectedItems) = GetVariants(manager, identifier, slot, idFrom, idTo, variantFrom); - var imcManip = new ImcManipulation(slot, variantTo.Id, idTo, default); - var imcFileTo = new ImcFile(manager, imcManip.Identifier); + var imcIdentifierTo = new ImcIdentifier(slotTo, idTo, variantTo); + var imcFileTo = new ImcFile(manager, imcIdentifierTo); + var imcEntry = manips.TryGetValue(imcIdentifierTo, out var entry) + ? entry + : imcFileTo.GetEntry(imcIdentifierTo.EquipSlot, imcIdentifierTo.Variant); + var mtrlVariantTo = imcEntry.MaterialId; var isAccessory = slot.IsAccessory(); var estType = slot switch @@ -133,7 +127,6 @@ public static class EquipmentSwap var skipFemale = false; var skipMale = false; - var mtrlVariantTo = manips(imcManip.Copy(imcFileTo.GetEntry(ImcFile.PartIndex(slot), variantTo))).Imc.Entry.MaterialId; foreach (var gr in Enum.GetValues()) { switch (gr.Split().Item1) @@ -154,7 +147,7 @@ public static class EquipmentSwap if (eqdp != null) swaps.Add(eqdp); - var ownMdl = eqdp?.SwapApplied.Eqdp.Entry.ToBits(slot).Item2 ?? false; + var ownMdl = eqdp?.SwapToModdedEntry.Model ?? false; var est = ItemSwap.CreateEst(manager, redirections, manips, estType, gr, idFrom, idTo, ownMdl); if (est != null) swaps.Add(est); @@ -184,22 +177,22 @@ public static class EquipmentSwap return affectedItems; } - public static MetaSwap? CreateEqdp(MetaFileManager manager, Func redirections, - Func manips, EquipSlot slot, GenderRace gr, PrimaryId idFrom, - PrimaryId idTo, byte mtrlTo) + public static MetaSwap? CreateEqdp(MetaFileManager manager, Func redirections, + MetaDictionary manips, EquipSlot slot, GenderRace gr, PrimaryId idFrom, PrimaryId idTo, byte mtrlTo) => CreateEqdp(manager, redirections, manips, slot, slot, gr, idFrom, idTo, mtrlTo); - public static MetaSwap? CreateEqdp(MetaFileManager manager, Func redirections, - Func manips, EquipSlot slotFrom, EquipSlot slotTo, GenderRace gr, PrimaryId idFrom, + public static MetaSwap? CreateEqdp(MetaFileManager manager, Func redirections, + MetaDictionary manips, EquipSlot slotFrom, EquipSlot slotTo, GenderRace gr, PrimaryId idFrom, PrimaryId idTo, byte mtrlTo) { - var (gender, race) = gr.Split(); - var eqdpFrom = new EqdpManipulation(ExpandedEqdpFile.GetDefault(manager, gr, slotFrom.IsAccessory(), idFrom), slotFrom, gender, - race, idFrom); - var eqdpTo = new EqdpManipulation(ExpandedEqdpFile.GetDefault(manager, gr, slotTo.IsAccessory(), idTo), slotTo, gender, race, - idTo); - var meta = new MetaSwap(manips, eqdpFrom, eqdpTo); - var (ownMtrl, ownMdl) = meta.SwapApplied.Eqdp.Entry.ToBits(slotFrom); + var eqdpFromIdentifier = new EqdpIdentifier(idFrom, slotFrom, gr); + var eqdpToIdentifier = new EqdpIdentifier(idFrom, slotFrom, gr); + var eqdpFromDefault = new EqdpEntryInternal(ExpandedEqdpFile.GetDefault(manager, eqdpFromIdentifier), slotFrom); + var eqdpToDefault = new EqdpEntryInternal(ExpandedEqdpFile.GetDefault(manager, eqdpToIdentifier), slotTo); + var meta = new MetaSwap(i => manips.TryGetValue(i, out var e) ? e : null, eqdpFromIdentifier, + eqdpFromDefault, eqdpToIdentifier, + eqdpToDefault); + var (ownMtrl, ownMdl) = meta.SwapToModdedEntry; if (ownMdl) { var mdl = CreateMdl(manager, redirections, slotFrom, slotTo, gr, idFrom, idTo, mtrlTo); @@ -270,38 +263,39 @@ public static class EquipmentSwap return (imc, variants, items); } - public static MetaSwap? CreateGmp(MetaFileManager manager, Func manips, EquipSlot slot, PrimaryId idFrom, - PrimaryId idTo) + public static MetaSwap? CreateGmp(MetaFileManager manager, MetaDictionary manips, + EquipSlot slot, PrimaryId idFrom, PrimaryId idTo) { if (slot is not EquipSlot.Head) return null; - var manipFrom = new GmpManipulation(ExpandedGmpFile.GetDefault(manager, idFrom), idFrom); - var manipTo = new GmpManipulation(ExpandedGmpFile.GetDefault(manager, idTo), idTo); - return new MetaSwap(manips, manipFrom, manipTo); + var manipFromIdentifier = new GmpIdentifier(idFrom); + var manipToIdentifier = new GmpIdentifier(idTo); + var manipFromDefault = ExpandedGmpFile.GetDefault(manager, manipFromIdentifier); + var manipToDefault = ExpandedGmpFile.GetDefault(manager, manipToIdentifier); + return new MetaSwap(i => manips.TryGetValue(i, out var e) ? e : null, manipFromIdentifier, manipFromDefault, manipToIdentifier, manipToDefault); } - public static MetaSwap CreateImc(MetaFileManager manager, Func redirections, - Func manips, EquipSlot slot, - PrimaryId idFrom, PrimaryId idTo, Variant variantFrom, Variant variantTo, ImcFile imcFileFrom, ImcFile imcFileTo) + public static MetaSwap CreateImc(MetaFileManager manager, Func redirections, + MetaDictionary manips, EquipSlot slot, PrimaryId idFrom, PrimaryId idTo, Variant variantFrom, Variant variantTo, + ImcFile imcFileFrom, ImcFile imcFileTo) => CreateImc(manager, redirections, manips, slot, slot, idFrom, idTo, variantFrom, variantTo, imcFileFrom, imcFileTo); - public static MetaSwap CreateImc(MetaFileManager manager, Func redirections, - Func manips, - EquipSlot slotFrom, EquipSlot slotTo, PrimaryId idFrom, PrimaryId idTo, + public static MetaSwap CreateImc(MetaFileManager manager, Func redirections, + MetaDictionary manips, EquipSlot slotFrom, EquipSlot slotTo, PrimaryId idFrom, PrimaryId idTo, Variant variantFrom, Variant variantTo, ImcFile imcFileFrom, ImcFile imcFileTo) { - var entryFrom = imcFileFrom.GetEntry(ImcFile.PartIndex(slotFrom), variantFrom); - var entryTo = imcFileTo.GetEntry(ImcFile.PartIndex(slotTo), variantTo); - var manipulationFrom = new ImcManipulation(slotFrom, variantFrom.Id, idFrom, entryFrom); - var manipulationTo = new ImcManipulation(slotTo, variantTo.Id, idTo, entryTo); - var imc = new MetaSwap(manips, manipulationFrom, manipulationTo); + var manipFromIdentifier = new ImcIdentifier(slotFrom, idFrom, variantFrom); + var manipToIdentifier = new ImcIdentifier(slotTo, idTo, variantTo); + var manipFromDefault = imcFileFrom.GetEntry(ImcFile.PartIndex(slotFrom), variantFrom); + var manipToDefault = imcFileTo.GetEntry(ImcFile.PartIndex(slotTo), variantTo); + var imc = new MetaSwap(i => manips.TryGetValue(i, out var e) ? e : null, manipFromIdentifier, manipFromDefault, manipToIdentifier, manipToDefault); - var decal = CreateDecal(manager, redirections, imc.SwapToModded.Imc.Entry.DecalId); + var decal = CreateDecal(manager, redirections, imc.SwapToModdedEntry.DecalId); if (decal != null) imc.ChildSwaps.Add(decal); - var avfx = CreateAvfx(manager, redirections, idFrom, idTo, imc.SwapToModded.Imc.Entry.VfxId); + var avfx = CreateAvfx(manager, redirections, idFrom, idTo, imc.SwapToModdedEntry.VfxId); if (avfx != null) imc.ChildSwaps.Add(avfx); @@ -322,7 +316,8 @@ public static class EquipmentSwap // Example: Abyssos Helm / Body - public static FileSwap? CreateAvfx(MetaFileManager manager, Func redirections, PrimaryId idFrom, PrimaryId idTo, byte vfxId) + public static FileSwap? CreateAvfx(MetaFileManager manager, Func redirections, PrimaryId idFrom, PrimaryId idTo, + byte vfxId) { if (vfxId == 0) return null; @@ -340,17 +335,18 @@ public static class EquipmentSwap return avfx; } - public static MetaSwap? CreateEqp(MetaFileManager manager, Func manips, EquipSlot slot, PrimaryId idFrom, - PrimaryId idTo) + public static MetaSwap? CreateEqp(MetaFileManager manager, MetaDictionary manips, + EquipSlot slot, PrimaryId idFrom, PrimaryId idTo) { if (slot.IsAccessory()) return null; - var eqpValueFrom = ExpandedEqpFile.GetDefault(manager, idFrom); - var eqpValueTo = ExpandedEqpFile.GetDefault(manager, idTo); - var eqpFrom = new EqpManipulation(eqpValueFrom, slot, idFrom); - var eqpTo = new EqpManipulation(eqpValueTo, slot, idFrom); - return new MetaSwap(manips, eqpFrom, eqpTo); + var manipFromIdentifier = new EqpIdentifier(idFrom, slot); + var manipToIdentifier = new EqpIdentifier(idTo, slot); + var manipFromDefault = new EqpEntryInternal(ExpandedEqpFile.GetDefault(manager, idFrom), slot); + var manipToDefault = new EqpEntryInternal(ExpandedEqpFile.GetDefault(manager, idTo), slot); + return new MetaSwap(i => manips.TryGetValue(i, out var e) ? e : null, manipFromIdentifier, + manipFromDefault, manipToIdentifier, manipToDefault); } public static FileSwap? CreateMtrl(MetaFileManager manager, Func redirections, EquipSlot slot, PrimaryId idFrom, @@ -397,7 +393,8 @@ public static class EquipmentSwap return mtrl; } - public static FileSwap CreateTex(MetaFileManager manager, Func redirections, char prefix, PrimaryId idFrom, PrimaryId idTo, + public static FileSwap CreateTex(MetaFileManager manager, Func redirections, char prefix, PrimaryId idFrom, + PrimaryId idTo, ref MtrlFile.Texture texture, ref bool dataWasChanged) => CreateTex(manager, redirections, prefix, EquipSlot.Unknown, EquipSlot.Unknown, idFrom, idTo, ref texture, ref dataWasChanged); diff --git a/Penumbra/Mods/ItemSwap/ItemSwap.cs b/Penumbra/Mods/ItemSwap/ItemSwap.cs index 7fac52c1..efd8080c 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwap.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwap.cs @@ -147,24 +147,23 @@ public static class ItemSwap return FileSwap.CreateSwap(manager, ResourceType.Sklb, redirections, sklbPath, sklbPath); } - /// metaChanges is not manipulated, but IReadOnlySet does not support TryGetValue. - public static MetaSwap? CreateEst(MetaFileManager manager, Func redirections, - Func manips, EstType type, - GenderRace genderRace, PrimaryId idFrom, PrimaryId idTo, bool ownMdl) + public static MetaSwap? CreateEst(MetaFileManager manager, Func redirections, + MetaDictionary manips, EstType type, GenderRace genderRace, PrimaryId idFrom, PrimaryId idTo, bool ownMdl) { if (type == 0) return null; - var (gender, race) = genderRace.Split(); - var fromDefault = new EstManipulation(gender, race, type, idFrom, EstFile.GetDefault(manager, type, genderRace, idFrom)); - var toDefault = new EstManipulation(gender, race, type, idTo, EstFile.GetDefault(manager, type, genderRace, idTo)); - var est = new MetaSwap(manips, fromDefault, toDefault); + var manipFromIdentifier = new EstIdentifier(idFrom, type, genderRace); + var manipToIdentifier = new EstIdentifier(idTo, type, genderRace); + var manipFromDefault = EstFile.GetDefault(manager, manipFromIdentifier); + var manipToDefault = EstFile.GetDefault(manager, manipToIdentifier); + var est = new MetaSwap(i => manips.TryGetValue(i, out var e) ? e : null, manipFromIdentifier, manipFromDefault, manipToIdentifier, manipToDefault); - if (ownMdl && est.SwapApplied.Est.Entry.Value >= 2) + if (ownMdl && est.SwapToModdedEntry.Value >= 2) { - var phyb = CreatePhyb(manager, redirections, type, genderRace, est.SwapApplied.Est.Entry); + var phyb = CreatePhyb(manager, redirections, type, genderRace, est.SwapToModdedEntry); est.ChildSwaps.Add(phyb); - var sklb = CreateSklb(manager, redirections, type, genderRace, est.SwapApplied.Est.Entry); + var sklb = CreateSklb(manager, redirections, type, genderRace, est.SwapToModdedEntry); est.ChildSwaps.Add(sklb); } else if (est.SwapAppliedIsDefault) diff --git a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs index 67a5d007..021ee665 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs @@ -74,9 +74,9 @@ public class ItemSwapContainer } break; - case MetaSwap meta: + case IMetaSwap meta: if (!meta.SwapAppliedIsDefault) - convertedManips.Add(meta.SwapApplied); + convertedManips.Add(meta.SwapFromIdentifier, meta.SwapToModdedEntry); break; } @@ -116,17 +116,10 @@ public class ItemSwapContainer ? p => collection.ResolvePath(p) ?? new FullPath(p) : p => ModRedirections.TryGetValue(p, out var path) ? path : new FullPath(p); - private Func MetaResolver(ModCollection? collection) - { - if (collection?.MetaCache?.Manipulations is { } cache) - { - MetaDictionary dict = [.. cache]; - return m => dict.TryGetValue(m, out var a) ? a : m; - } - - var set = _appliedModData.Manipulations; - return m => set.TryGetValue(m, out var a) ? a : m; - } + private MetaDictionary MetaResolver(ModCollection? collection) + => collection?.MetaCache?.Manipulations is { } cache + ? [.. cache] + : _appliedModData.Manipulations; public EquipItem[] LoadEquipment(EquipItem from, EquipItem to, ModCollection? collection = null, bool useRightRing = true, bool useLeftRing = true) @@ -161,8 +154,8 @@ public class ItemSwapContainer _ => (EstType)0, }; - var metaResolver = MetaResolver(collection); - var est = ItemSwap.CreateEst(manager, pathResolver, metaResolver, type, race, from, to, true); + var estResolver = MetaResolver(collection); + var est = ItemSwap.CreateEst(manager, pathResolver, estResolver, type, race, from, to, true); Swaps.Add(mdl); if (est != null) diff --git a/Penumbra/Mods/ItemSwap/Swaps.cs b/Penumbra/Mods/ItemSwap/Swaps.cs index 27935ffb..36c54203 100644 --- a/Penumbra/Mods/ItemSwap/Swaps.cs +++ b/Penumbra/Mods/ItemSwap/Swaps.cs @@ -1,58 +1,91 @@ -using Penumbra.Api.Enums; +using Penumbra.Api.Enums; using Penumbra.GameData.Files; using Penumbra.Meta.Manipulations; using Penumbra.String.Classes; using Penumbra.Meta; using static Penumbra.Mods.ItemSwap.ItemSwap; -using Penumbra.Services; namespace Penumbra.Mods.ItemSwap; public class Swap { /// Any further swaps belonging specifically to this tree of changes. - public readonly List ChildSwaps = new(); + public readonly List ChildSwaps = []; public IEnumerable WithChildren() => ChildSwaps.SelectMany(c => c.WithChildren()).Prepend(this); } -public sealed class MetaSwap : Swap +public interface IMetaSwap { + public IMetaIdentifier SwapFromIdentifier { get; } + public IMetaIdentifier SwapToIdentifier { get; } + + public object SwapFromDefaultEntry { get; } + public object SwapToDefaultEntry { get; } + public object SwapToModdedEntry { get; } + + public bool SwapToIsDefault { get; } + public bool SwapAppliedIsDefault { get; } +} + +public sealed class MetaSwap : Swap, IMetaSwap + where TIdentifier : unmanaged, IMetaIdentifier + where TEntry : unmanaged, IEquatable +{ + public TIdentifier SwapFromIdentifier; + public TIdentifier SwapToIdentifier; + /// The default value of a specific meta manipulation that needs to be redirected. - public MetaManipulation SwapFrom; + public TEntry SwapFromDefaultEntry; /// The default value of the same Meta entry of the redirected item. - public MetaManipulation SwapToDefault; + public TEntry SwapToDefaultEntry; /// The modded value of the same Meta entry of the redirected item, or the same as SwapToDefault if unmodded. - public MetaManipulation SwapToModded; + public TEntry SwapToModdedEntry; - /// The modded value applied to the specific meta manipulation target before redirection. - public MetaManipulation SwapApplied; - - /// Whether SwapToModded equals SwapToDefault. - public bool SwapToIsDefault; + /// Whether SwapToModdedEntry equals SwapToDefaultEntry. + public bool SwapToIsDefault { get; } /// Whether the applied meta manipulation does not change anything against the default. - public bool SwapAppliedIsDefault; + public bool SwapAppliedIsDefault { get; } /// /// Create a new MetaSwap from the original meta identifier and the target meta identifier. /// - /// A function that converts the given manipulation to the modded one. - /// The original meta identifier with its default value. - /// The target meta identifier with its default value. - public MetaSwap(Func manipulations, MetaManipulation manipFrom, MetaManipulation manipTo) + /// A function that obtains a modded meta entry if it exists. + /// The original meta identifier. + /// The default value for the original meta identifier. + /// The target meta identifier. + /// The default value for the target meta identifier. + public MetaSwap(Func manipulations, TIdentifier manipFromIdentifier, TEntry manipFromEntry, + TIdentifier manipToIdentifier, TEntry manipToEntry) { - SwapFrom = manipFrom; - SwapToDefault = manipTo; + SwapFromIdentifier = manipFromIdentifier; + SwapToIdentifier = manipToIdentifier; + SwapFromDefaultEntry = manipFromEntry; + SwapToDefaultEntry = manipToEntry; - SwapToModded = manipulations(manipTo); - SwapToIsDefault = manipTo.EntryEquals(SwapToModded); - SwapApplied = SwapFrom.WithEntryOf(SwapToModded); - SwapAppliedIsDefault = SwapApplied.EntryEquals(SwapFrom); + SwapToModdedEntry = manipulations(SwapToIdentifier) ?? SwapToDefaultEntry; + SwapToIsDefault = SwapToModdedEntry.Equals(SwapToDefaultEntry); + SwapAppliedIsDefault = SwapToModdedEntry.Equals(SwapFromDefaultEntry); } + + IMetaIdentifier IMetaSwap.SwapFromIdentifier + => SwapFromIdentifier; + + IMetaIdentifier IMetaSwap.SwapToIdentifier + => SwapToIdentifier; + + object IMetaSwap.SwapFromDefaultEntry + => SwapFromDefaultEntry; + + object IMetaSwap.SwapToDefaultEntry + => SwapToDefaultEntry; + + object IMetaSwap.SwapToModdedEntry + => SwapToModdedEntry; } public sealed class FileSwap : Swap @@ -113,8 +146,7 @@ public sealed class FileSwap : Swap /// A full swap container with the actual file in memory. /// True if everything could be read correctly, false otherwise. public static FileSwap CreateSwap(MetaFileManager manager, ResourceType type, Func redirections, - string swapFromRequest, string swapToRequest, - string? swapFromPreChange = null) + string swapFromRequest, string swapToRequest, string? swapFromPreChange = null) { var swap = new FileSwap { diff --git a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs index 6010cdaf..62115dd6 100644 --- a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs +++ b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs @@ -240,7 +240,7 @@ public class ItemSwapTab : IDisposable, ITab { return swap switch { - MetaSwap meta => $"{meta.SwapFrom}: {meta.SwapFrom.EntryToString()} -> {meta.SwapApplied.EntryToString()}", + IMetaSwap meta => $"{meta.SwapFromIdentifier}: {meta.SwapFromDefaultEntry} -> {meta.SwapToModdedEntry}", FileSwap file => $"{file.Type}: {file.SwapFromRequestPath} -> {file.SwapToModded.FullName}{(file.DataWasChanged ? " (EDITED)" : string.Empty)}", _ => string.Empty, @@ -410,7 +410,7 @@ public class ItemSwapTab : IDisposable, ITab private ImRaii.IEndObject DrawTab(SwapType newTab) { - using var tab = ImRaii.TabItem(newTab is SwapType.BetweenSlots ? "Between Slots" : newTab.ToString()); + var tab = ImRaii.TabItem(newTab is SwapType.BetweenSlots ? "Between Slots" : newTab.ToString()); if (tab) { _dirty |= _lastTab != newTab; From d9b63320f07b5600bdd71b3420fd740e0f44e6d4 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 8 Jun 2024 20:46:29 +0200 Subject: [PATCH 237/433] Some small fixes, parse directly into MetaDictionary. --- Penumbra/Api/Api/TemporaryApi.cs | 24 ++++--------------- Penumbra/Api/IpcTester/TemporaryIpcTester.cs | 3 ++- Penumbra/Meta/Manipulations/MetaDictionary.cs | 16 ++++++------- Penumbra/Mods/ItemSwap/ItemSwapContainer.cs | 2 +- Penumbra/UI/Tabs/Debug/DebugTab.cs | 2 +- 5 files changed, 17 insertions(+), 30 deletions(-) diff --git a/Penumbra/Api/Api/TemporaryApi.cs b/Penumbra/Api/Api/TemporaryApi.cs index 995ec388..49958a0d 100644 --- a/Penumbra/Api/Api/TemporaryApi.cs +++ b/Penumbra/Api/Api/TemporaryApi.cs @@ -159,8 +159,7 @@ public class TemporaryApi( /// The empty string is treated as an empty set. /// Only returns true if all conversions are successful and distinct. /// - private static bool ConvertManips(string manipString, - [NotNullWhen(true)] out MetaDictionary? manips) + private static bool ConvertManips(string manipString, [NotNullWhen(true)] out MetaDictionary? manips) { if (manipString.Length == 0) { @@ -168,23 +167,10 @@ public class TemporaryApi( return true; } - if (Functions.FromCompressedBase64(manipString, out var manipArray) != MetaManipulation.CurrentVersion) - { - manips = null; - return false; - } + if (Functions.FromCompressedBase64(manipString, out manips!) == MetaManipulation.CurrentVersion) + return true; - manips = []; - foreach (var manip in manipArray!.Where(m => m.Validate())) - { - if (manips.Add(manip)) - continue; - - Penumbra.Log.Warning($"Manipulation {manip} {manip.EntryToString()} is invalid and was skipped."); - manips = null; - return false; - } - - return true; + manips = null; + return false; } } diff --git a/Penumbra/Api/IpcTester/TemporaryIpcTester.cs b/Penumbra/Api/IpcTester/TemporaryIpcTester.cs index a8405eb2..15601867 100644 --- a/Penumbra/Api/IpcTester/TemporaryIpcTester.cs +++ b/Penumbra/Api/IpcTester/TemporaryIpcTester.cs @@ -4,6 +4,7 @@ using ImGuiNET; using OtterGui; using OtterGui.Raii; using OtterGui.Services; +using OtterGui.Text; using Penumbra.Api.Enums; using Penumbra.Api.IpcSubscribers; using Penumbra.Collections.Manager; @@ -49,7 +50,7 @@ public class TemporaryIpcTester( ImGui.InputTextWithHint("##tempMod", "Temporary Mod Name...", ref _tempModName, 32); ImGui.InputTextWithHint("##tempGame", "Game Path...", ref _tempGamePath, 256); ImGui.InputTextWithHint("##tempFile", "File Path...", ref _tempFilePath, 256); - ImGui.InputTextWithHint("##tempManip", "Manipulation Base64 String...", ref _tempManipulation, 256); + ImUtf8.InputText("##tempManip"u8, ref _tempManipulation, "Manipulation Base64 String..."u8); ImGui.Checkbox("Force Character Collection Overwrite", ref _forceOverwrite); using var table = ImRaii.Table(string.Empty, 3, ImGuiTableFlags.SizingFixedFit); diff --git a/Penumbra/Meta/Manipulations/MetaDictionary.cs b/Penumbra/Meta/Manipulations/MetaDictionary.cs index 51149e3b..3ce54afb 100644 --- a/Penumbra/Meta/Manipulations/MetaDictionary.cs +++ b/Penumbra/Meta/Manipulations/MetaDictionary.cs @@ -52,16 +52,16 @@ public sealed class MetaDictionary : IEnumerable IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - public bool Add(IMetaIdentifier identifier, object entry) + public bool TryAdd(IMetaIdentifier identifier, object entry) => identifier switch { - EqdpIdentifier eqdpIdentifier => entry is EqdpEntryInternal e && Add(eqdpIdentifier, e), - EqpIdentifier eqpIdentifier => entry is EqpEntryInternal e && Add(eqpIdentifier, e), - EstIdentifier estIdentifier => entry is EstEntry e && Add(estIdentifier, e), - GlobalEqpManipulation globalEqpManipulation => Add(globalEqpManipulation), - GmpIdentifier gmpIdentifier => entry is GmpEntry e && Add(gmpIdentifier, e), - ImcIdentifier imcIdentifier => entry is ImcEntry e && Add(imcIdentifier, e), - RspIdentifier rspIdentifier => entry is RspEntry e && Add(rspIdentifier, e), + EqdpIdentifier eqdpIdentifier => entry is EqdpEntryInternal e && TryAdd(eqdpIdentifier, e), + EqpIdentifier eqpIdentifier => entry is EqpEntryInternal e && TryAdd(eqpIdentifier, e), + EstIdentifier estIdentifier => entry is EstEntry e && TryAdd(estIdentifier, e), + GlobalEqpManipulation globalEqpManipulation => TryAdd(globalEqpManipulation), + GmpIdentifier gmpIdentifier => entry is GmpEntry e && TryAdd(gmpIdentifier, e), + ImcIdentifier imcIdentifier => entry is ImcEntry e && TryAdd(imcIdentifier, e), + RspIdentifier rspIdentifier => entry is RspEntry e && TryAdd(rspIdentifier, e), _ => false, }; diff --git a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs index 021ee665..b0b588b4 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs @@ -76,7 +76,7 @@ public class ItemSwapContainer break; case IMetaSwap meta: if (!meta.SwapAppliedIsDefault) - convertedManips.Add(meta.SwapFromIdentifier, meta.SwapToModdedEntry); + convertedManips.TryAdd(meta.SwapFromIdentifier, meta.SwapToModdedEntry); break; } diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index 1813a7e3..396029d5 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -432,7 +432,7 @@ public class DebugTab : Window, ITab foreach (var obj in _objects) { - ImGuiUtil.DrawTableColumn($"{((GameObject*)obj.Address)->ObjectIndex}"); + ImGuiUtil.DrawTableColumn(obj.Address == nint.Zero ? $"{((GameObject*)obj.Address)->ObjectIndex}" : "NULL"); ImGuiUtil.DrawTableColumn($"0x{obj.Address:X}"); ImGuiUtil.DrawTableColumn(obj.Address == nint.Zero ? string.Empty From 196ca2ce393ba160c1bc7c29f9375feeb64f1b1f Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 8 Jun 2024 21:15:40 +0200 Subject: [PATCH 238/433] Remove all usages of Add(MetaManipulation) --- Penumbra/Meta/Manipulations/MetaDictionary.cs | 137 +++++++++++------- Penumbra/Mods/ItemSwap/ItemSwapContainer.cs | 54 ++++--- Penumbra/Mods/SubMods/SubMod.cs | 6 +- Penumbra/Mods/TemporaryMod.cs | 4 +- 4 files changed, 121 insertions(+), 80 deletions(-) diff --git a/Penumbra/Meta/Manipulations/MetaDictionary.cs b/Penumbra/Meta/Manipulations/MetaDictionary.cs index 3ce54afb..1dc6496e 100644 --- a/Penumbra/Meta/Manipulations/MetaDictionary.cs +++ b/Penumbra/Meta/Manipulations/MetaDictionary.cs @@ -52,38 +52,6 @@ public sealed class MetaDictionary : IEnumerable IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - public bool TryAdd(IMetaIdentifier identifier, object entry) - => identifier switch - { - EqdpIdentifier eqdpIdentifier => entry is EqdpEntryInternal e && TryAdd(eqdpIdentifier, e), - EqpIdentifier eqpIdentifier => entry is EqpEntryInternal e && TryAdd(eqpIdentifier, e), - EstIdentifier estIdentifier => entry is EstEntry e && TryAdd(estIdentifier, e), - GlobalEqpManipulation globalEqpManipulation => TryAdd(globalEqpManipulation), - GmpIdentifier gmpIdentifier => entry is GmpEntry e && TryAdd(gmpIdentifier, e), - ImcIdentifier imcIdentifier => entry is ImcEntry e && TryAdd(imcIdentifier, e), - RspIdentifier rspIdentifier => entry is RspEntry e && TryAdd(rspIdentifier, e), - _ => false, - }; - - public bool Add(MetaManipulation manip) - { - var ret = manip.ManipulationType switch - { - MetaManipulation.Type.Imc => _imc.TryAdd(manip.Imc.Identifier, manip.Imc.Entry), - MetaManipulation.Type.Eqdp => _eqdp.TryAdd(manip.Eqdp.Identifier, new EqdpEntryInternal(manip.Eqdp.Entry, manip.Eqdp.Slot)), - MetaManipulation.Type.Eqp => _eqp.TryAdd(manip.Eqp.Identifier, new EqpEntryInternal(manip.Eqp.Entry, manip.Eqp.Slot)), - MetaManipulation.Type.Est => _est.TryAdd(manip.Est.Identifier, manip.Est.Entry), - MetaManipulation.Type.Gmp => _gmp.TryAdd(manip.Gmp.Identifier, manip.Gmp.Entry), - MetaManipulation.Type.Rsp => _rsp.TryAdd(manip.Rsp.Identifier, manip.Rsp.Entry), - MetaManipulation.Type.GlobalEqp => _globalEqp.Add(manip.GlobalEqp), - _ => false, - }; - - if (ret) - ++Count; - return ret; - } - public bool TryAdd(ImcIdentifier identifier, ImcEntry entry) { if (!_imc.TryAdd(identifier, entry)) @@ -93,9 +61,29 @@ public sealed class MetaDictionary : IEnumerable return true; } + + public bool TryAdd(EqpIdentifier identifier, EqpEntryInternal entry) + { + if (!_eqp.TryAdd(identifier, entry)) + return false; + + ++Count; + return true; + } + public bool TryAdd(EqpIdentifier identifier, EqpEntry entry) => TryAdd(identifier, new EqpEntryInternal(entry, identifier.Slot)); + + public bool TryAdd(EqdpIdentifier identifier, EqdpEntryInternal entry) + { + if (!_eqdp.TryAdd(identifier, entry)) + return false; + + ++Count; + return true; + } + public bool TryAdd(EqdpIdentifier identifier, EqdpEntry entry) => TryAdd(identifier, new EqdpEntryInternal(entry, identifier.Slot)); @@ -159,6 +147,73 @@ public sealed class MetaDictionary : IEnumerable TryAdd(identifier); } + /// Try to merge all manipulations from manips into this, and return the first failure, if any. + public bool MergeForced(MetaDictionary manips, out IMetaIdentifier failedIdentifier) + { + foreach (var (identifier, entry) in manips._imc) + { + if (!TryAdd(identifier, entry)) + { + failedIdentifier = identifier; + return false; + } + } + + foreach (var (identifier, entry) in manips._eqp) + { + if (!TryAdd(identifier, entry)) + { + failedIdentifier = identifier; + return false; + } + } + + foreach (var (identifier, entry) in manips._eqdp) + { + if (!TryAdd(identifier, entry)) + { + failedIdentifier = identifier; + return false; + } + } + + foreach (var (identifier, entry) in manips._gmp) + { + if (!TryAdd(identifier, entry)) + { + failedIdentifier = identifier; + return false; + } + } + + foreach (var (identifier, entry) in manips._rsp) + { + if (!TryAdd(identifier, entry)) + { + failedIdentifier = identifier; + return false; + } + } + + foreach (var (identifier, entry) in manips._est) + { + if (!TryAdd(identifier, entry)) + { + failedIdentifier = identifier; + return false; + } + } + + foreach (var identifier in manips._globalEqp) + { + if (!TryAdd(identifier)) + { + failedIdentifier = identifier; + return false; + } + } + } + public bool TryGetValue(EstIdentifier identifier, out EstEntry value) => _est.TryGetValue(identifier, out value); @@ -318,22 +373,4 @@ public sealed class MetaDictionary : IEnumerable return dict; } } - - private bool TryAdd(EqpIdentifier identifier, EqpEntryInternal entry) - { - if (!_eqp.TryAdd(identifier, entry)) - return false; - - ++Count; - return true; - } - - private bool TryAdd(EqdpIdentifier identifier, EqdpEntryInternal entry) - { - if (!_eqdp.TryAdd(identifier, entry)) - return false; - - ++Count; - return true; - } } diff --git a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs index b0b588b4..72a6005d 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs @@ -53,32 +53,38 @@ public class ItemSwapContainer { foreach (var swap in Swaps.SelectMany(s => s.WithChildren())) { - switch (swap) + if (swap is FileSwap file) { - case FileSwap file: - // Skip, nothing to do - if (file.SwapToModdedEqualsOriginal) - continue; + // Skip, nothing to do + if (file.SwapToModdedEqualsOriginal) + continue; - if (writeType == WriteType.UseSwaps && file.SwapToModdedExistsInGame && !file.DataWasChanged) - { - convertedSwaps.TryAdd(file.SwapFromRequestPath, file.SwapToModded); - } - else - { - var path = file.GetNewPath(directory.FullName); - var bytes = file.FileData.Write(); - Directory.CreateDirectory(Path.GetDirectoryName(path)!); - _manager.Compactor.WriteAllBytes(path, bytes); - convertedFiles.TryAdd(file.SwapFromRequestPath, new FullPath(path)); - } - - break; - case IMetaSwap meta: - if (!meta.SwapAppliedIsDefault) - convertedManips.TryAdd(meta.SwapFromIdentifier, meta.SwapToModdedEntry); - - break; + if (writeType == WriteType.UseSwaps && file.SwapToModdedExistsInGame && !file.DataWasChanged) + { + convertedSwaps.TryAdd(file.SwapFromRequestPath, file.SwapToModded); + } + else + { + var path = file.GetNewPath(directory.FullName); + var bytes = file.FileData.Write(); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + _manager.Compactor.WriteAllBytes(path, bytes); + convertedFiles.TryAdd(file.SwapFromRequestPath, new FullPath(path)); + } + } + else if (swap is IMetaSwap { SwapAppliedIsDefault: false }) + { + // @formatter:off + _ = swap switch + { + MetaSwap meta => convertedManips.TryAdd(meta.SwapFromIdentifier, meta.SwapToModdedEntry), + MetaSwap meta => convertedManips.TryAdd(meta.SwapFromIdentifier, meta.SwapToModdedEntry), + MetaSwap meta => convertedManips.TryAdd(meta.SwapFromIdentifier, meta.SwapToModdedEntry), + MetaSwapmeta => convertedManips.TryAdd(meta.SwapFromIdentifier, meta.SwapToModdedEntry), + MetaSwapmeta => convertedManips.TryAdd(meta.SwapFromIdentifier, meta.SwapToModdedEntry), + _ => false, + }; + // @formatter:on } } diff --git a/Penumbra/Mods/SubMods/SubMod.cs b/Penumbra/Mods/SubMods/SubMod.cs index 06a924c8..40fd2e75 100644 --- a/Penumbra/Mods/SubMods/SubMod.cs +++ b/Penumbra/Mods/SubMods/SubMod.cs @@ -64,11 +64,9 @@ public static class SubMod data.FileSwaps.TryAdd(p, new FullPath(property.Value.ToObject()!)); } - var manips = json[nameof(data.Manipulations)]; + var manips = json[nameof(data.Manipulations)]?.ToObject(); if (manips != null) - foreach (var s in manips.Children().Select(c => c.ToObject()) - .Where(m => m.Validate())) - data.Manipulations.Add(s); + data.Manipulations.UnionWith(manips); } /// Load the relevant data for a selectable option from a JToken of that option. diff --git a/Penumbra/Mods/TemporaryMod.cs b/Penumbra/Mods/TemporaryMod.cs index a715f786..61ed4528 100644 --- a/Penumbra/Mods/TemporaryMod.cs +++ b/Penumbra/Mods/TemporaryMod.cs @@ -93,8 +93,8 @@ public class TemporaryMod : IMod } } - foreach (var manip in collection.MetaCache?.Manipulations ?? Array.Empty()) - defaultMod.Manipulations.Add(manip); + MetaDictionary manips = [.. collection.MetaCache?.Manipulations ?? []]; + defaultMod.Manipulations.UnionWith(manips); saveService.ImmediateSave(new ModSaveGroup(dir, defaultMod, config.ReplaceNonAsciiOnImport)); modManager.AddMod(dir); From 361082813b59c125949c3a3c092945a2844626e3 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 11 Jun 2024 12:23:08 +0200 Subject: [PATCH 239/433] tmp --- Penumbra/Meta/Manipulations/MetaDictionary.cs | 198 +++++-- .../Meta/Manipulations/MetaManipulation.cs | 142 ++++- Penumbra/Mods/Editor/ModMerger.cs | 9 +- Penumbra/Mods/Editor/ModMetaEditor.cs | 167 +----- Penumbra/Mods/Groups/ImcModGroup.cs | 14 +- Penumbra/Mods/ItemSwap/ItemSwapContainer.cs | 2 +- Penumbra/Mods/ModCreator.cs | 6 +- Penumbra/Mods/SubMods/SubMod.cs | 2 +- Penumbra/Mods/TemporaryMod.cs | 3 +- Penumbra/Services/StaticServiceManager.cs | 1 - .../UI/AdvancedWindow/Meta/ImcMetaDrawer.cs | 353 ++++++++++++ .../UI/AdvancedWindow/ModEditWindow.Meta.cs | 522 ++++++++++-------- Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs | 19 +- .../ModsTab/Groups/ImcModGroupEditDrawer.cs | 11 +- Penumbra/UI/ModsTab/ImcManipulationDrawer.cs | 207 +------ Penumbra/Util/DictionaryExtensions.cs | 12 + 16 files changed, 1008 insertions(+), 660 deletions(-) create mode 100644 Penumbra/UI/AdvancedWindow/Meta/ImcMetaDrawer.cs diff --git a/Penumbra/Meta/Manipulations/MetaDictionary.cs b/Penumbra/Meta/Manipulations/MetaDictionary.cs index 1dc6496e..236157ae 100644 --- a/Penumbra/Meta/Manipulations/MetaDictionary.cs +++ b/Penumbra/Meta/Manipulations/MetaDictionary.cs @@ -7,7 +7,7 @@ using ImcEntry = Penumbra.GameData.Structs.ImcEntry; namespace Penumbra.Meta.Manipulations; [JsonConverter(typeof(Converter))] -public sealed class MetaDictionary : IEnumerable +public class MetaDictionary : IEnumerable { private readonly Dictionary _imc = []; private readonly Dictionary _eqp = []; @@ -17,8 +17,37 @@ public sealed class MetaDictionary : IEnumerable private readonly Dictionary _gmp = []; private readonly HashSet _globalEqp = []; + public IReadOnlyDictionary Imc + => _imc; + public int Count { get; private set; } + public int GetCount(MetaManipulation.Type type) + => type switch + { + MetaManipulation.Type.Imc => _imc.Count, + MetaManipulation.Type.Eqdp => _eqdp.Count, + MetaManipulation.Type.Eqp => _eqp.Count, + MetaManipulation.Type.Est => _est.Count, + MetaManipulation.Type.Gmp => _gmp.Count, + MetaManipulation.Type.Rsp => _rsp.Count, + MetaManipulation.Type.GlobalEqp => _globalEqp.Count, + _ => 0, + }; + + public bool CanAdd(IMetaIdentifier identifier) + => identifier switch + { + EqdpIdentifier eqdpIdentifier => !_eqdp.ContainsKey(eqdpIdentifier), + EqpIdentifier eqpIdentifier => !_eqp.ContainsKey(eqpIdentifier), + EstIdentifier estIdentifier => !_est.ContainsKey(estIdentifier), + GlobalEqpManipulation globalEqpManipulation => !_globalEqp.Contains(globalEqpManipulation), + GmpIdentifier gmpIdentifier => !_gmp.ContainsKey(gmpIdentifier), + ImcIdentifier imcIdentifier => !_imc.ContainsKey(imcIdentifier), + RspIdentifier rspIdentifier => !_rsp.ContainsKey(rspIdentifier), + _ => false, + }; + public void Clear() { _imc.Clear(); @@ -123,6 +152,68 @@ public sealed class MetaDictionary : IEnumerable return true; } + public bool Update(ImcIdentifier identifier, ImcEntry entry) + { + if (!_imc.ContainsKey(identifier)) + return false; + + _imc[identifier] = entry; + return true; + } + + + public bool Update(EqpIdentifier identifier, EqpEntryInternal entry) + { + if (!_eqp.ContainsKey(identifier)) + return false; + + _eqp[identifier] = entry; + return true; + } + + public bool Update(EqpIdentifier identifier, EqpEntry entry) + => Update(identifier, new EqpEntryInternal(entry, identifier.Slot)); + + + public bool Update(EqdpIdentifier identifier, EqdpEntryInternal entry) + { + if (!_eqdp.ContainsKey(identifier)) + return false; + + _eqdp[identifier] = entry; + return true; + } + + public bool Update(EqdpIdentifier identifier, EqdpEntry entry) + => Update(identifier, new EqdpEntryInternal(entry, identifier.Slot)); + + public bool Update(EstIdentifier identifier, EstEntry entry) + { + if (!_est.ContainsKey(identifier)) + return false; + + _est[identifier] = entry; + return true; + } + + public bool Update(GmpIdentifier identifier, GmpEntry entry) + { + if (!_gmp.ContainsKey(identifier)) + return false; + + _gmp[identifier] = entry; + return true; + } + + public bool Update(RspIdentifier identifier, RspEntry entry) + { + if (!_rsp.ContainsKey(identifier)) + return false; + + _rsp[identifier] = entry; + return true; + } + public void UnionWith(MetaDictionary manips) { foreach (var (identifier, entry) in manips._imc) @@ -148,70 +239,52 @@ public sealed class MetaDictionary : IEnumerable } /// Try to merge all manipulations from manips into this, and return the first failure, if any. - public bool MergeForced(MetaDictionary manips, out IMetaIdentifier failedIdentifier) + public bool MergeForced(MetaDictionary manips, out IMetaIdentifier? failedIdentifier) { - foreach (var (identifier, entry) in manips._imc) + foreach (var (identifier, _) in manips._imc.Where(kvp => !TryAdd(kvp.Key, kvp.Value))) { - if (!TryAdd(identifier, entry)) - { - failedIdentifier = identifier; - return false; - } + failedIdentifier = identifier; + return false; } - foreach (var (identifier, entry) in manips._eqp) + foreach (var (identifier, _) in manips._eqp.Where(kvp => !TryAdd(kvp.Key, kvp.Value))) { - if (!TryAdd(identifier, entry)) - { - failedIdentifier = identifier; - return false; - } + failedIdentifier = identifier; + return false; } - foreach (var (identifier, entry) in manips._eqdp) + foreach (var (identifier, _) in manips._eqdp.Where(kvp => !TryAdd(kvp.Key, kvp.Value))) { - if (!TryAdd(identifier, entry)) - { - failedIdentifier = identifier; - return false; - } + failedIdentifier = identifier; + return false; } - foreach (var (identifier, entry) in manips._gmp) + foreach (var (identifier, _) in manips._gmp.Where(kvp => !TryAdd(kvp.Key, kvp.Value))) { - if (!TryAdd(identifier, entry)) - { - failedIdentifier = identifier; - return false; - } + failedIdentifier = identifier; + return false; } - foreach (var (identifier, entry) in manips._rsp) + foreach (var (identifier, _) in manips._rsp.Where(kvp => !TryAdd(kvp.Key, kvp.Value))) { - if (!TryAdd(identifier, entry)) - { - failedIdentifier = identifier; - return false; - } + failedIdentifier = identifier; + return false; } - foreach (var (identifier, entry) in manips._est) + foreach (var (identifier, _) in manips._est.Where(kvp => !TryAdd(kvp.Key, kvp.Value))) { - if (!TryAdd(identifier, entry)) - { - failedIdentifier = identifier; - return false; - } + failedIdentifier = identifier; + return false; } - foreach (var identifier in manips._globalEqp) + foreach (var identifier in manips._globalEqp.Where(identifier => !TryAdd(identifier))) { - if (!TryAdd(identifier)) - { - failedIdentifier = identifier; - return false; - } + failedIdentifier = identifier; + return false; } + + failedIdentifier = default; + return false; } public bool TryGetValue(EstIdentifier identifier, out EstEntry value) @@ -244,6 +317,18 @@ public sealed class MetaDictionary : IEnumerable Count = _imc.Count + _eqp.Count + _eqdp.Count + _est.Count + _rsp.Count + _gmp.Count + _globalEqp.Count; } + public void UpdateTo(MetaDictionary other) + { + _imc.UpdateTo(other._imc); + _eqp.UpdateTo(other._eqp); + _eqdp.UpdateTo(other._eqdp); + _est.UpdateTo(other._est); + _rsp.UpdateTo(other._rsp); + _gmp.UpdateTo(other._gmp); + _globalEqp.UnionWith(other._globalEqp); + Count = _imc.Count + _eqp.Count + _eqdp.Count + _est.Count + _rsp.Count + _gmp.Count + _globalEqp.Count; + } + public MetaDictionary Clone() { var ret = new MetaDictionary(); @@ -251,6 +336,31 @@ public sealed class MetaDictionary : IEnumerable return ret; } + private static void WriteJson(JsonWriter writer, JsonSerializer serializer, IMetaIdentifier identifier, object entry) + { + var type = identifier switch + { + ImcIdentifier => "Imc", + EqdpIdentifier => "Eqdp", + EqpIdentifier => "Eqp", + EstIdentifier => "Est", + GmpIdentifier => "Gmp", + RspIdentifier => "Rsp", + GlobalEqpManipulation => "GlobalEqp", + _ => string.Empty, + }; + + if (type.Length == 0) + return; + + writer.WriteStartObject(); + writer.WritePropertyName("Type"); + writer.WriteValue(type); + writer.WritePropertyName("Manipulation"); + + writer.WriteEndObject(); + } + private class Converter : JsonConverter { public override void WriteJson(JsonWriter writer, MetaDictionary? value, JsonSerializer serializer) diff --git a/Penumbra/Meta/Manipulations/MetaManipulation.cs b/Penumbra/Meta/Manipulations/MetaManipulation.cs index f22de809..b80681d2 100644 --- a/Penumbra/Meta/Manipulations/MetaManipulation.cs +++ b/Penumbra/Meta/Manipulations/MetaManipulation.cs @@ -1,9 +1,148 @@ +using Dalamud.Interface; +using ImGuiNET; using Newtonsoft.Json; using Newtonsoft.Json.Converters; +using OtterGui; +using Penumbra.GameData.Enums; using Penumbra.Interop.Structs; +using Penumbra.Mods.Editor; using Penumbra.String.Functions; +using Penumbra.UI; +using Penumbra.UI.ModsTab; -namespace Penumbra.Meta.Manipulations; +namespace Penumbra.Meta.Manipulations; + +#if false +private static class ImcRow +{ + private static ImcIdentifier _newIdentifier = ImcIdentifier.Default; + + private static float IdWidth + => 80 * UiHelpers.Scale; + + private static float SmallIdWidth + => 45 * UiHelpers.Scale; + + public static void DrawNew(MetaFileManager metaFileManager, ModEditor editor, Vector2 iconSize) + { + ImGui.TableNextColumn(); + CopyToClipboardButton("Copy all current IMC manipulations to clipboard.", iconSize, + editor.MetaEditor.Imc.Select(m => (MetaManipulation)m)); + ImGui.TableNextColumn(); + var (defaultEntry, fileExists, _) = metaFileManager.ImcChecker.GetDefaultEntry(_newIdentifier, true); + var manip = (MetaManipulation)new ImcManipulation(_newIdentifier, defaultEntry); + var canAdd = fileExists && editor.MetaEditor.CanAdd(manip); + var tt = canAdd ? "Stage this edit." : !fileExists ? "This IMC file does not exist." : "This entry is already edited."; + if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) + editor.MetaEditor.Add(manip); + + // Identifier + ImGui.TableNextColumn(); + var change = ImcManipulationDrawer.DrawObjectType(ref _newIdentifier); + + ImGui.TableNextColumn(); + change |= ImcManipulationDrawer.DrawPrimaryId(ref _newIdentifier); + using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, + new Vector2(3 * UiHelpers.Scale, ImGui.GetStyle().ItemSpacing.Y)); + + ImGui.TableNextColumn(); + // Equipment and accessories are slightly different imcs than other types. + if (_newIdentifier.ObjectType is ObjectType.Equipment or ObjectType.Accessory) + change |= ImcManipulationDrawer.DrawSlot(ref _newIdentifier); + else + change |= ImcManipulationDrawer.DrawSecondaryId(ref _newIdentifier); + + ImGui.TableNextColumn(); + change |= ImcManipulationDrawer.DrawVariant(ref _newIdentifier); + + ImGui.TableNextColumn(); + if (_newIdentifier.ObjectType is ObjectType.DemiHuman) + change |= ImcManipulationDrawer.DrawSlot(ref _newIdentifier, 70); + else + ImUtf8.ScaledDummy(new Vector2(70 * UiHelpers.Scale, 0)); + + if (change) + defaultEntry = metaFileManager.ImcChecker.GetDefaultEntry(_newIdentifier, true).Entry; + // Values + using var disabled = ImRaii.Disabled(); + ImGui.TableNextColumn(); + ImcManipulationDrawer.DrawMaterialId(defaultEntry, ref defaultEntry, false); + ImGui.SameLine(); + ImcManipulationDrawer.DrawMaterialAnimationId(defaultEntry, ref defaultEntry, false); + ImGui.TableNextColumn(); + ImcManipulationDrawer.DrawDecalId(defaultEntry, ref defaultEntry, false); + ImGui.SameLine(); + ImcManipulationDrawer.DrawVfxId(defaultEntry, ref defaultEntry, false); + ImGui.SameLine(); + ImcManipulationDrawer.DrawSoundId(defaultEntry, ref defaultEntry, false); + ImGui.TableNextColumn(); + ImcManipulationDrawer.DrawAttributes(defaultEntry, ref defaultEntry); + } + + public static void Draw(MetaFileManager metaFileManager, ImcManipulation meta, ModEditor editor, Vector2 iconSize) + { + DrawMetaButtons(meta, editor, iconSize); + + // Identifier + ImGui.TableNextColumn(); + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); + ImGui.TextUnformatted(meta.ObjectType.ToName()); + ImGuiUtil.HoverTooltip(ObjectTypeTooltip); + ImGui.TableNextColumn(); + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); + ImGui.TextUnformatted(meta.PrimaryId.ToString()); + ImGuiUtil.HoverTooltip(PrimaryIdTooltipShort); + + ImGui.TableNextColumn(); + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); + if (meta.ObjectType is ObjectType.Equipment or ObjectType.Accessory) + { + ImGui.TextUnformatted(meta.EquipSlot.ToName()); + ImGuiUtil.HoverTooltip(EquipSlotTooltip); + } + else + { + ImGui.TextUnformatted(meta.SecondaryId.ToString()); + ImGuiUtil.HoverTooltip(SecondaryIdTooltip); + } + + ImGui.TableNextColumn(); + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); + ImGui.TextUnformatted(meta.Variant.ToString()); + ImGuiUtil.HoverTooltip(VariantIdTooltip); + + ImGui.TableNextColumn(); + ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); + if (meta.ObjectType is ObjectType.DemiHuman) + { + ImGui.TextUnformatted(meta.EquipSlot.ToName()); + ImGuiUtil.HoverTooltip(EquipSlotTooltip); + } + + // Values + using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, + new Vector2(3 * UiHelpers.Scale, ImGui.GetStyle().ItemSpacing.Y)); + ImGui.TableNextColumn(); + var defaultEntry = metaFileManager.ImcChecker.GetDefaultEntry(meta.Identifier, true).Entry; + var newEntry = meta.Entry; + var changes = ImcManipulationDrawer.DrawMaterialId(defaultEntry, ref newEntry, true); + ImGui.SameLine(); + changes |= ImcManipulationDrawer.DrawMaterialAnimationId(defaultEntry, ref newEntry, true); + ImGui.TableNextColumn(); + changes |= ImcManipulationDrawer.DrawDecalId(defaultEntry, ref newEntry, true); + ImGui.SameLine(); + changes |= ImcManipulationDrawer.DrawVfxId(defaultEntry, ref newEntry, true); + ImGui.SameLine(); + changes |= ImcManipulationDrawer.DrawSoundId(defaultEntry, ref newEntry, true); + ImGui.TableNextColumn(); + changes |= ImcManipulationDrawer.DrawAttributes(defaultEntry, ref newEntry); + + if (changes) + editor.MetaEditor.Change(meta.Copy(newEntry)); + } +} + +#endif public interface IMetaManipulation { @@ -315,3 +454,4 @@ public readonly struct MetaManipulation : IEquatable, ICompara public static bool operator >=(MetaManipulation left, MetaManipulation right) => left.CompareTo(right) >= 0; } + diff --git a/Penumbra/Mods/Editor/ModMerger.cs b/Penumbra/Mods/Editor/ModMerger.cs index 4faced80..2df76838 100644 --- a/Penumbra/Mods/Editor/ModMerger.cs +++ b/Penumbra/Mods/Editor/ModMerger.cs @@ -162,12 +162,9 @@ public class ModMerger : IDisposable foreach (var originalOption in mergeOptions) { - foreach (var manip in originalOption.Manipulations) - { - if (!manips.Add(manip)) - throw new Exception( - $"Could not add meta manipulation {manip} from {originalOption.GetFullName()} to {option.GetFullName()} because another manipulation of the same data already exists in this option."); - } + if (!manips.MergeForced(originalOption.Manipulations, out var failed)) + throw new Exception( + $"Could not add meta manipulation {failed} from {originalOption.GetFullName()} to {option.GetFullName()} because another manipulation of the same data already exists in this option."); foreach (var (swapA, swapB) in originalOption.FileSwaps) { diff --git a/Penumbra/Mods/Editor/ModMetaEditor.cs b/Penumbra/Mods/Editor/ModMetaEditor.cs index 45d9f8a1..42171378 100644 --- a/Penumbra/Mods/Editor/ModMetaEditor.cs +++ b/Penumbra/Mods/Editor/ModMetaEditor.cs @@ -1,24 +1,24 @@ using System.Collections.Frozen; +using OtterGui.Services; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Manager; using Penumbra.Mods.SubMods; namespace Penumbra.Mods.Editor; -public class ModMetaEditor(ModManager modManager) +public class ModMetaEditor(ModManager modManager) : MetaDictionary, IService { - private readonly HashSet _imc = []; - private readonly HashSet _eqp = []; - private readonly HashSet _eqdp = []; - private readonly HashSet _gmp = []; - private readonly HashSet _est = []; - private readonly HashSet _rsp = []; - private readonly HashSet _globalEqp = []; - public sealed class OtherOptionData : HashSet { public int TotalCount; + public void Add(string name, int count) + { + if (count > 0) + Add(name); + TotalCount += count; + } + public new void Clear() { TotalCount = 0; @@ -31,91 +31,9 @@ public class ModMetaEditor(ModManager modManager) public bool Changes { get; private set; } - public IReadOnlySet Imc - => _imc; - - public IReadOnlySet Eqp - => _eqp; - - public IReadOnlySet Eqdp - => _eqdp; - - public IReadOnlySet Gmp - => _gmp; - - public IReadOnlySet Est - => _est; - - public IReadOnlySet Rsp - => _rsp; - - public IReadOnlySet GlobalEqp - => _globalEqp; - - public bool CanAdd(MetaManipulation m) + public new void Clear() { - return m.ManipulationType switch - { - MetaManipulation.Type.Imc => !_imc.Contains(m.Imc), - MetaManipulation.Type.Eqdp => !_eqdp.Contains(m.Eqdp), - MetaManipulation.Type.Eqp => !_eqp.Contains(m.Eqp), - MetaManipulation.Type.Est => !_est.Contains(m.Est), - MetaManipulation.Type.Gmp => !_gmp.Contains(m.Gmp), - MetaManipulation.Type.Rsp => !_rsp.Contains(m.Rsp), - MetaManipulation.Type.GlobalEqp => !_globalEqp.Contains(m.GlobalEqp), - _ => false, - }; - } - - public bool Add(MetaManipulation m) - { - var added = m.ManipulationType switch - { - MetaManipulation.Type.Imc => _imc.Add(m.Imc), - MetaManipulation.Type.Eqdp => _eqdp.Add(m.Eqdp), - MetaManipulation.Type.Eqp => _eqp.Add(m.Eqp), - MetaManipulation.Type.Est => _est.Add(m.Est), - MetaManipulation.Type.Gmp => _gmp.Add(m.Gmp), - MetaManipulation.Type.Rsp => _rsp.Add(m.Rsp), - MetaManipulation.Type.GlobalEqp => _globalEqp.Add(m.GlobalEqp), - _ => false, - }; - Changes |= added; - return added; - } - - public bool Delete(MetaManipulation m) - { - var deleted = m.ManipulationType switch - { - MetaManipulation.Type.Imc => _imc.Remove(m.Imc), - MetaManipulation.Type.Eqdp => _eqdp.Remove(m.Eqdp), - MetaManipulation.Type.Eqp => _eqp.Remove(m.Eqp), - MetaManipulation.Type.Est => _est.Remove(m.Est), - MetaManipulation.Type.Gmp => _gmp.Remove(m.Gmp), - MetaManipulation.Type.Rsp => _rsp.Remove(m.Rsp), - MetaManipulation.Type.GlobalEqp => _globalEqp.Remove(m.GlobalEqp), - _ => false, - }; - Changes |= deleted; - return deleted; - } - - public bool Change(MetaManipulation m) - => Delete(m) && Add(m); - - public bool Set(MetaManipulation m) - => Delete(m) | Add(m); - - public void Clear() - { - _imc.Clear(); - _eqp.Clear(); - _eqdp.Clear(); - _gmp.Clear(); - _est.Clear(); - _rsp.Clear(); - _globalEqp.Clear(); + base.Clear(); Changes = true; } @@ -129,15 +47,19 @@ public class ModMetaEditor(ModManager modManager) if (option == currentOption) continue; - foreach (var manip in option.Manipulations) - { - var data = OtherData[manip.ManipulationType]; - ++data.TotalCount; - data.Add(option.GetFullName()); - } + var name = option.GetFullName(); + OtherData[MetaManipulation.Type.Imc].Add(name, option.Manipulations.GetCount(MetaManipulation.Type.Imc)); + OtherData[MetaManipulation.Type.Eqp].Add(name, option.Manipulations.GetCount(MetaManipulation.Type.Eqp)); + OtherData[MetaManipulation.Type.Eqdp].Add(name, option.Manipulations.GetCount(MetaManipulation.Type.Eqdp)); + OtherData[MetaManipulation.Type.Gmp].Add(name, option.Manipulations.GetCount(MetaManipulation.Type.Gmp)); + OtherData[MetaManipulation.Type.Est].Add(name, option.Manipulations.GetCount(MetaManipulation.Type.Est)); + OtherData[MetaManipulation.Type.Rsp].Add(name, option.Manipulations.GetCount(MetaManipulation.Type.Rsp)); + OtherData[MetaManipulation.Type.GlobalEqp].Add(name, option.Manipulations.GetCount(MetaManipulation.Type.GlobalEqp)); } - Split(currentOption.Manipulations); + Clear(); + UnionWith(currentOption.Manipulations); + Changes = false; } public void Apply(IModDataContainer container) @@ -145,50 +67,7 @@ public class ModMetaEditor(ModManager modManager) if (!Changes) return; - modManager.OptionEditor.SetManipulations(container, [..Recombine()]); + modManager.OptionEditor.SetManipulations(container, this); Changes = false; } - - private void Split(IEnumerable manips) - { - Clear(); - foreach (var manip in manips) - { - switch (manip.ManipulationType) - { - case MetaManipulation.Type.Imc: - _imc.Add(manip.Imc); - break; - case MetaManipulation.Type.Eqdp: - _eqdp.Add(manip.Eqdp); - break; - case MetaManipulation.Type.Eqp: - _eqp.Add(manip.Eqp); - break; - case MetaManipulation.Type.Est: - _est.Add(manip.Est); - break; - case MetaManipulation.Type.Gmp: - _gmp.Add(manip.Gmp); - break; - case MetaManipulation.Type.Rsp: - _rsp.Add(manip.Rsp); - break; - case MetaManipulation.Type.GlobalEqp: - _globalEqp.Add(manip.GlobalEqp); - break; - } - } - - Changes = false; - } - - public IEnumerable Recombine() - => _imc.Select(m => (MetaManipulation)m) - .Concat(_eqdp.Select(m => (MetaManipulation)m)) - .Concat(_eqp.Select(m => (MetaManipulation)m)) - .Concat(_est.Select(m => (MetaManipulation)m)) - .Concat(_gmp.Select(m => (MetaManipulation)m)) - .Concat(_rsp.Select(m => (MetaManipulation)m)) - .Concat(_globalEqp.Select(m => (MetaManipulation)m)); } diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs index 383bc9fd..c52828c0 100644 --- a/Penumbra/Mods/Groups/ImcModGroup.cs +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -95,28 +95,28 @@ public class ImcModGroup(Mod mod) : IModGroup public IModGroupEditDrawer EditDrawer(ModGroupEditDrawer editDrawer) => new ImcModGroupEditDrawer(editDrawer, this); - public ImcManipulation GetManip(ushort mask, Variant variant) - => new(Identifier.ObjectType, Identifier.BodySlot, Identifier.PrimaryId, Identifier.SecondaryId.Id, variant.Id, - Identifier.EquipSlot, DefaultEntry with { AttributeMask = mask }); + public ImcEntry GetEntry(ushort mask) + => DefaultEntry with { AttributeMask = mask }; public void AddData(Setting setting, Dictionary redirections, MetaDictionary manipulations) { if (IsDisabled(setting)) return; - var mask = GetCurrentMask(setting); + var mask = GetCurrentMask(setting); + var entry = GetEntry(mask); if (AllVariants) { var count = ImcChecker.GetVariantCount(Identifier); if (count == 0) - manipulations.Add(GetManip(mask, Identifier.Variant)); + manipulations.TryAdd(Identifier, entry); else for (var i = 0; i <= count; ++i) - manipulations.Add(GetManip(mask, (Variant)i)); + manipulations.TryAdd(Identifier with { Variant = (Variant)i }, entry); } else { - manipulations.Add(GetManip(mask, Identifier.Variant)); + manipulations.TryAdd(Identifier, entry); } } diff --git a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs index 72a6005d..8328edea 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs @@ -124,7 +124,7 @@ public class ItemSwapContainer private MetaDictionary MetaResolver(ModCollection? collection) => collection?.MetaCache?.Manipulations is { } cache - ? [.. cache] + ? [] // [.. cache] TODO : _appliedModData.Manipulations; public EquipItem[] LoadEquipment(EquipItem from, EquipItem to, ModCollection? collection = null, bool useRightRing = true, diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index 0035fd41..e8ca3199 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -198,7 +198,8 @@ public partial class ModCreator( Penumbra.Log.Verbose( $"Incorporating {file} as Metadata file of {meta.MetaManipulations.Count} manipulations {deleteString}"); deleteList.Add(file.FullName); - option.Manipulations.UnionWith([.. meta.MetaManipulations]); + // TODO + option.Manipulations.UnionWith([]);//[.. meta.MetaManipulations]); } else if (ext1 == ".rgsp" || ext2 == ".rgsp") { @@ -212,7 +213,8 @@ public partial class ModCreator( $"Incorporating {file} as racial scaling file of {rgsp.MetaManipulations.Count} manipulations {deleteString}"); deleteList.Add(file.FullName); - option.Manipulations.UnionWith([.. rgsp.MetaManipulations]); + // TODO + option.Manipulations.UnionWith([]);//[.. rgsp.MetaManipulations]); } } catch (Exception e) diff --git a/Penumbra/Mods/SubMods/SubMod.cs b/Penumbra/Mods/SubMods/SubMod.cs index 40fd2e75..a8c37369 100644 --- a/Penumbra/Mods/SubMods/SubMod.cs +++ b/Penumbra/Mods/SubMods/SubMod.cs @@ -37,7 +37,7 @@ public static class SubMod { to.Files = new Dictionary(from.Files); to.FileSwaps = new Dictionary(from.FileSwaps); - to.Manipulations = [.. from.Manipulations]; + to.Manipulations = from.Manipulations.Clone(); } /// Load all file redirections, file swaps and meta manipulations from a JToken of that option into a data container. diff --git a/Penumbra/Mods/TemporaryMod.cs b/Penumbra/Mods/TemporaryMod.cs index 61ed4528..91c4c5df 100644 --- a/Penumbra/Mods/TemporaryMod.cs +++ b/Penumbra/Mods/TemporaryMod.cs @@ -93,7 +93,8 @@ public class TemporaryMod : IMod } } - MetaDictionary manips = [.. collection.MetaCache?.Manipulations ?? []]; + // TODO + MetaDictionary manips = []; // [.. collection.MetaCache?.Manipulations ?? []]; defaultMod.Manipulations.UnionWith(manips); saveService.ImmediateSave(new ModSaveGroup(dir, defaultMod, config.ReplaceNonAsciiOnImport)); diff --git a/Penumbra/Services/StaticServiceManager.cs b/Penumbra/Services/StaticServiceManager.cs index 0c6648ba..35e36349 100644 --- a/Penumbra/Services/StaticServiceManager.cs +++ b/Penumbra/Services/StaticServiceManager.cs @@ -188,7 +188,6 @@ public static class StaticServiceManager .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() diff --git a/Penumbra/UI/AdvancedWindow/Meta/ImcMetaDrawer.cs b/Penumbra/UI/AdvancedWindow/Meta/ImcMetaDrawer.cs new file mode 100644 index 00000000..d9a8c27c --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Meta/ImcMetaDrawer.cs @@ -0,0 +1,353 @@ +using Dalamud.Interface; +using ImGuiNET; +using OtterGui.Raii; +using OtterGui.Services; +using OtterGui.Text; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Meta; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; +using Penumbra.UI.Classes; + +namespace Penumbra.UI.AdvancedWindow.Meta; + +public sealed class ImcMetaDrawer(ModEditor editor, MetaFileManager metaFiles) + : MetaDrawer(editor, metaFiles), IService +{ + private bool _fileExists; + + private const string ModelSetIdTooltipShort = "Model Set ID"; + private const string EquipSlotTooltip = "Equip Slot"; + private const string ModelRaceTooltip = "Model Race"; + private const string GenderTooltip = "Gender"; + private const string ObjectTypeTooltip = "Object Type"; + private const string SecondaryIdTooltip = "Secondary ID"; + private const string PrimaryIdTooltipShort = "Primary ID"; + private const string VariantIdTooltip = "Variant ID"; + private const string EstTypeTooltip = "EST Type"; + private const string RacialTribeTooltip = "Racial Tribe"; + private const string ScalingTypeTooltip = "Scaling Type"; + + protected override void Initialize() + { + Identifier = ImcIdentifier.Default; + UpdateEntry(); + } + + private void UpdateEntry() + => (Entry, _fileExists, _) = MetaFiles.ImcChecker.GetDefaultEntry(Identifier, true); + + protected override void DrawNew() + { + ImGui.TableNextColumn(); + // Copy To Clipboard + ImGui.TableNextColumn(); + var canAdd = _fileExists && Editor.MetaEditor.CanAdd(Identifier); + var tt = canAdd ? "Stage this edit."u8 : !_fileExists ? "This IMC file does not exist."u8 : "This entry is already edited."u8; + if (ImUtf8.IconButton(FontAwesomeIcon.Plus, tt, disabled: !canAdd)) + Editor.MetaEditor.TryAdd(Identifier, Entry); + + if (DrawIdentifier(ref Identifier)) + UpdateEntry(); + + using var disabled = ImRaii.Disabled(); + DrawEntry(Entry, ref Entry, false); + } + + protected override void DrawEntry(ImcIdentifier identifier, ImcEntry entry) + { + const uint frameColor = 0; + // Meta Buttons + + ImGui.TableNextColumn(); + ImUtf8.TextFramed(identifier.ObjectType.ToName(), frameColor); + ImUtf8.HoverTooltip("Object Type"u8); + + ImGui.TableNextColumn(); + ImUtf8.TextFramed($"{identifier.PrimaryId.Id}", frameColor); + ImUtf8.HoverTooltip("Primary ID"); + + ImGui.TableNextColumn(); + if (identifier.ObjectType is ObjectType.Equipment or ObjectType.Accessory) + { + ImUtf8.TextFramed(identifier.EquipSlot.ToName(), frameColor); + ImUtf8.HoverTooltip("Equip Slot"u8); + } + else + { + ImUtf8.TextFramed($"{identifier.SecondaryId.Id}", frameColor); + ImUtf8.HoverTooltip("Secondary ID"u8); + } + + ImGui.TableNextColumn(); + ImUtf8.TextFramed($"{identifier.Variant.Id}", frameColor); + ImUtf8.HoverTooltip("Variant"u8); + + ImGui.TableNextColumn(); + if (identifier.ObjectType is ObjectType.DemiHuman) + { + ImUtf8.TextFramed(identifier.EquipSlot.ToName(), frameColor); + ImUtf8.HoverTooltip("Equip Slot"u8); + } + + var defaultEntry = MetaFiles.ImcChecker.GetDefaultEntry(identifier, true).Entry; + if (DrawEntry(defaultEntry, ref entry, true)) + Editor.MetaEditor.Update(identifier, entry); + } + + private static bool DrawIdentifier(ref ImcIdentifier identifier) + { + ImGui.TableNextColumn(); + var change = DrawObjectType(ref identifier); + + ImGui.TableNextColumn(); + change |= DrawPrimaryId(ref identifier); + + ImGui.TableNextColumn(); + if (identifier.ObjectType is ObjectType.Equipment or ObjectType.Accessory) + change |= DrawSlot(ref identifier); + else + change |= DrawSecondaryId(ref identifier); + + ImGui.TableNextColumn(); + change |= DrawVariant(ref identifier); + + ImGui.TableNextColumn(); + if (identifier.ObjectType is ObjectType.DemiHuman) + change |= DrawSlot(ref identifier, 70f); + else + ImUtf8.ScaledDummy(70f); + return change; + } + + private static bool DrawEntry(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault) + { + ImGui.TableNextColumn(); + var change = DrawMaterialId(defaultEntry, ref entry, addDefault); + ImUtf8.SameLineInner(); + change |= DrawMaterialAnimationId(defaultEntry, ref entry, addDefault); + + ImGui.TableNextColumn(); + change |= DrawDecalId(defaultEntry, ref entry, addDefault); + ImUtf8.SameLineInner(); + change |= DrawVfxId(defaultEntry, ref entry, addDefault); + ImUtf8.SameLineInner(); + change |= DrawSoundId(defaultEntry, ref entry, addDefault); + + ImGui.TableNextColumn(); + change |= DrawAttributes(defaultEntry, ref entry); + return change; + } + + + protected override IEnumerable<(ImcIdentifier, ImcEntry)> Enumerate() + => Editor.MetaEditor.Imc.Select(kvp => (kvp.Key, kvp.Value)); + + public static bool DrawObjectType(ref ImcIdentifier identifier, float width = 110) + { + var ret = Combos.ImcType("##imcType", identifier.ObjectType, out var type, width); + ImUtf8.HoverTooltip("Object Type"u8); + + if (ret) + { + var equipSlot = type switch + { + ObjectType.Equipment => identifier.EquipSlot.IsEquipment() ? identifier.EquipSlot : EquipSlot.Head, + ObjectType.DemiHuman => identifier.EquipSlot.IsEquipment() ? identifier.EquipSlot : EquipSlot.Head, + ObjectType.Accessory => identifier.EquipSlot.IsAccessory() ? identifier.EquipSlot : EquipSlot.Ears, + _ => EquipSlot.Unknown, + }; + identifier = identifier with + { + ObjectType = type, + EquipSlot = equipSlot, + SecondaryId = identifier.SecondaryId == 0 ? 1 : identifier.SecondaryId, + }; + } + + return ret; + } + + public static bool DrawPrimaryId(ref ImcIdentifier identifier, float unscaledWidth = 80) + { + var ret = IdInput("##imcPrimaryId"u8, unscaledWidth, identifier.PrimaryId.Id, out var newId, 0, ushort.MaxValue, + identifier.PrimaryId.Id <= 1); + ImUtf8.HoverTooltip("Primary ID - You can usually find this as the 'x####' part of an item path.\n"u8 + + "This should generally not be left <= 1 unless you explicitly want that."u8); + if (ret) + identifier = identifier with { PrimaryId = newId }; + return ret; + } + + public static bool DrawSecondaryId(ref ImcIdentifier identifier, float unscaledWidth = 100) + { + var ret = IdInput("##imcSecondaryId"u8, unscaledWidth, identifier.SecondaryId.Id, out var newId, 0, ushort.MaxValue, false); + ImUtf8.HoverTooltip("Secondary ID"u8); + if (ret) + identifier = identifier with { SecondaryId = newId }; + return ret; + } + + public static bool DrawVariant(ref ImcIdentifier identifier, float unscaledWidth = 45) + { + var ret = IdInput("##imcVariant"u8, unscaledWidth, identifier.Variant.Id, out var newId, 0, byte.MaxValue, false); + ImUtf8.HoverTooltip("Variant ID"u8); + if (ret) + identifier = identifier with { Variant = (byte)newId }; + return ret; + } + + public static bool DrawSlot(ref ImcIdentifier identifier, float unscaledWidth = 100) + { + bool ret; + EquipSlot slot; + switch (identifier.ObjectType) + { + case ObjectType.Equipment: + case ObjectType.DemiHuman: + ret = Combos.EqpEquipSlot("##slot", identifier.EquipSlot, out slot, unscaledWidth); + break; + case ObjectType.Accessory: + ret = Combos.AccessorySlot("##slot", identifier.EquipSlot, out slot, unscaledWidth); + break; + default: return false; + } + + ImUtf8.HoverTooltip("Equip Slot"u8); + if (ret) + identifier = identifier with { EquipSlot = slot }; + return ret; + } + + public static bool DrawMaterialId(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault, float unscaledWidth = 45) + { + if (!DragInput("##materialId"u8, "Material ID"u8, unscaledWidth * ImUtf8.GlobalScale, entry.MaterialId, defaultEntry.MaterialId, + out var newValue, (byte)1, byte.MaxValue, 0.01f, addDefault)) + return false; + + entry = entry with { MaterialId = newValue }; + return true; + } + + public static bool DrawMaterialAnimationId(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault, float unscaledWidth = 45) + { + if (!DragInput("##mAnimId"u8, "Material Animation ID"u8, unscaledWidth * ImUtf8.GlobalScale, entry.MaterialAnimationId, + defaultEntry.MaterialAnimationId, out var newValue, (byte)0, byte.MaxValue, 0.01f, addDefault)) + return false; + + entry = entry with { MaterialAnimationId = newValue }; + return true; + } + + public static bool DrawDecalId(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault, float unscaledWidth = 45) + { + if (!DragInput("##decalId"u8, "Decal ID"u8, unscaledWidth * ImUtf8.GlobalScale, entry.DecalId, defaultEntry.DecalId, out var newValue, + (byte)0, byte.MaxValue, 0.01f, addDefault)) + return false; + + entry = entry with { DecalId = newValue }; + return true; + } + + public static bool DrawVfxId(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault, float unscaledWidth = 45) + { + if (!DragInput("##vfxId"u8, "VFX ID"u8, unscaledWidth * ImUtf8.GlobalScale, entry.VfxId, defaultEntry.VfxId, out var newValue, (byte)0, + byte.MaxValue, 0.01f, addDefault)) + return false; + + entry = entry with { VfxId = newValue }; + return true; + } + + public static bool DrawSoundId(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault, float unscaledWidth = 45) + { + if (!DragInput("##soundId"u8, "Sound ID"u8, unscaledWidth * ImUtf8.GlobalScale, entry.SoundId, defaultEntry.SoundId, out var newValue, + (byte)0, byte.MaxValue, 0.01f, addDefault)) + return false; + + entry = entry with { SoundId = newValue }; + return true; + } + + public static bool DrawAttributes(ImcEntry defaultEntry, ref ImcEntry entry) + { + var changes = false; + for (var i = 0; i < ImcEntry.NumAttributes; ++i) + { + using var id = ImRaii.PushId(i); + var flag = 1 << i; + var value = (entry.AttributeMask & flag) != 0; + var def = (defaultEntry.AttributeMask & flag) != 0; + if (Checkmark("##attribute"u8, "ABCDEFGHIJ"u8.Slice(i, 1), value, def, out var newValue)) + { + var newMask = (ushort)(newValue ? entry.AttributeMask | flag : entry.AttributeMask & ~flag); + entry = entry with { AttributeMask = newMask }; + changes = true; + } + + if (i < ImcEntry.NumAttributes - 1) + ImUtf8.SameLineInner(); + } + + return changes; + } + + + /// + /// A number input for ids with an optional max id of given width. + /// Returns true if newId changed against currentId. + /// + private static bool IdInput(ReadOnlySpan label, float unscaledWidth, ushort currentId, out ushort newId, int minId, int maxId, + bool border) + { + int tmp = currentId; + ImGui.SetNextItemWidth(unscaledWidth * ImUtf8.GlobalScale); + using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, UiHelpers.Scale, border); + using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.RegexWarningBorder, border); + if (ImUtf8.InputScalar(label, ref tmp)) + tmp = Math.Clamp(tmp, minId, maxId); + + newId = (ushort)tmp; + return newId != currentId; + } + + /// + /// A dragging int input of given width that compares against a default value, shows a tooltip and clamps against min and max. + /// Returns true if newValue changed against currentValue. + /// + private static bool DragInput(ReadOnlySpan label, ReadOnlySpan tooltip, float width, T currentValue, T defaultValue, + out T newValue, T minValue, T maxValue, float speed, bool addDefault) where T : unmanaged, INumber + { + newValue = currentValue; + using var color = ImRaii.PushColor(ImGuiCol.FrameBg, + defaultValue > currentValue ? ColorId.DecreasedMetaValue.Value() : ColorId.IncreasedMetaValue.Value(), + defaultValue != currentValue); + ImGui.SetNextItemWidth(width); + if (ImUtf8.DragScalar(label, ref newValue, minValue, maxValue, speed)) + newValue = newValue <= minValue ? minValue : newValue >= maxValue ? maxValue : newValue; + + if (addDefault) + ImUtf8.HoverTooltip($"{tooltip}\nDefault Value: {defaultValue}"); + else + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, tooltip); + + return newValue != currentValue; + } + + /// + /// A checkmark that compares against a default value and shows a tooltip. + /// Returns true if newValue is changed against currentValue. + /// + private static bool Checkmark(ReadOnlySpan label, ReadOnlySpan tooltip, bool currentValue, bool defaultValue, + out bool newValue) + { + using var color = ImRaii.PushColor(ImGuiCol.FrameBg, + defaultValue ? ColorId.DecreasedMetaValue.Value() : ColorId.IncreasedMetaValue.Value(), + defaultValue != currentValue); + newValue = currentValue; + ImUtf8.Checkbox(label, ref newValue); + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, tooltip); + return newValue != currentValue; + } +} diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs index b0a74637..50862eec 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs @@ -1,10 +1,11 @@ +using System.Reflection.Emit; using Dalamud.Interface; using ImGuiNET; using OtterGui; using OtterGui.Raii; using OtterGui.Text; +using OtterGui.Text.EndObjects; using Penumbra.GameData.Enums; -using Penumbra.GameData.Structs; using Penumbra.Interop.Structs; using Penumbra.Meta; using Penumbra.Meta.Files; @@ -20,17 +21,7 @@ public partial class ModEditWindow private const string ModelSetIdTooltip = "Model Set ID - You can usually find this as the 'e####' part of an item path.\nThis should generally not be left <= 1 unless you explicitly want that."; - private const string ModelSetIdTooltipShort = "Model Set ID"; - private const string EquipSlotTooltip = "Equip Slot"; - private const string ModelRaceTooltip = "Model Race"; - private const string GenderTooltip = "Gender"; - private const string ObjectTypeTooltip = "Object Type"; - private const string SecondaryIdTooltip = "Secondary ID"; - private const string PrimaryIdTooltipShort = "Primary ID"; - private const string VariantIdTooltip = "Variant ID"; - private const string EstTypeTooltip = "EST Type"; - private const string RacialTribeTooltip = "Racial Tribe"; - private const string ScalingTypeTooltip = "Scaling Type"; + private void DrawMetaTab() { @@ -56,7 +47,7 @@ public partial class ModEditWindow ImGui.SameLine(); SetFromClipboardButton(); ImGui.SameLine(); - CopyToClipboardButton("Copy all current manipulations to clipboard.", _iconSize, _editor.MetaEditor.Recombine()); + CopyToClipboardButton("Copy all current manipulations to clipboard.", _iconSize, _editor.MetaEditor); ImGui.SameLine(); if (ImGui.Button("Write as TexTools Files")) _metaFileManager.WriteAllTexToolsMeta(Mod!); @@ -65,71 +56,103 @@ public partial class ModEditWindow if (!child) return; - DrawEditHeader(_editor.MetaEditor.Eqp, "Equipment Parameter Edits (EQP)###EQP", 5, EqpRow.Draw, EqpRow.DrawNew, - _editor.MetaEditor.OtherData[MetaManipulation.Type.Eqp]); - DrawEditHeader(_editor.MetaEditor.Eqdp, "Racial Model Edits (EQDP)###EQDP", 7, EqdpRow.Draw, EqdpRow.DrawNew, - _editor.MetaEditor.OtherData[MetaManipulation.Type.Eqdp]); - DrawEditHeader(_editor.MetaEditor.Imc, "Variant Edits (IMC)###IMC", 10, ImcRow.Draw, ImcRow.DrawNew, - _editor.MetaEditor.OtherData[MetaManipulation.Type.Imc]); - DrawEditHeader(_editor.MetaEditor.Est, "Extra Skeleton Parameters (EST)###EST", 7, EstRow.Draw, EstRow.DrawNew, - _editor.MetaEditor.OtherData[MetaManipulation.Type.Est]); - DrawEditHeader(_editor.MetaEditor.Gmp, "Visor/Gimmick Edits (GMP)###GMP", 7, GmpRow.Draw, GmpRow.DrawNew, - _editor.MetaEditor.OtherData[MetaManipulation.Type.Gmp]); - DrawEditHeader(_editor.MetaEditor.Rsp, "Racial Scaling Edits (RSP)###RSP", 5, RspRow.Draw, RspRow.DrawNew, - _editor.MetaEditor.OtherData[MetaManipulation.Type.Rsp]); - DrawEditHeader(_editor.MetaEditor.GlobalEqp, "Global Equipment Parameter Edits (Global EQP)###GEQP", 4, GlobalEqpRow.Draw, - GlobalEqpRow.DrawNew, _editor.MetaEditor.OtherData[MetaManipulation.Type.GlobalEqp]); + DrawEditHeader(MetaManipulation.Type.Eqp); + DrawEditHeader(MetaManipulation.Type.Eqdp); + DrawEditHeader(MetaManipulation.Type.Imc); + DrawEditHeader(MetaManipulation.Type.Est); + DrawEditHeader(MetaManipulation.Type.Gmp); + DrawEditHeader(MetaManipulation.Type.Rsp); + DrawEditHeader(MetaManipulation.Type.GlobalEqp); } - - /// The headers for the different meta changes all have basically the same structure for different types. - private void DrawEditHeader(IReadOnlyCollection items, string label, int numColumns, - Action draw, Action drawNew, - ModMetaEditor.OtherOptionData otherOptionData) - { - const ImGuiTableFlags flags = ImGuiTableFlags.RowBg | ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.BordersInnerV; - - var oldPos = ImGui.GetCursorPosY(); - var header = ImGui.CollapsingHeader($"{items.Count} {label}"); - var newPos = ImGui.GetCursorPos(); - if (otherOptionData.TotalCount > 0) + private static ReadOnlySpan Label(MetaManipulation.Type type) + => type switch { - var text = $"{otherOptionData.TotalCount} Edits in other Options"; - var size = ImGui.CalcTextSize(text).X; - ImGui.SetCursorPos(new Vector2(ImGui.GetContentRegionAvail().X - size, oldPos + ImGui.GetStyle().FramePadding.Y)); - ImGuiUtil.TextColored(ColorId.RedundantAssignment.Value() | 0xFF000000, text); - if (ImGui.IsItemHovered()) - { - using var tt = ImUtf8.Tooltip(); - foreach (var name in otherOptionData) - ImUtf8.Text(name); - } + MetaManipulation.Type.Imc => "Variant Edits (IMC)###IMC"u8, + MetaManipulation.Type.Eqdp => "Racial Model Edits (EQDP)###EQDP"u8, + MetaManipulation.Type.Eqp => "Equipment Parameter Edits (EQP)###EQP"u8, + MetaManipulation.Type.Est => "Extra Skeleton Parameters (EST)###EST"u8, + MetaManipulation.Type.Gmp => "Visor/Gimmick Edits (GMP)###GMP"u8, + MetaManipulation.Type.Rsp => "Racial Scaling Edits (RSP)###RSP"u8, + MetaManipulation.Type.GlobalEqp => "Global Equipment Parameter Edits (Global EQP)###GEQP"u8, + _ => "\0"u8, + }; - ImGui.SetCursorPos(newPos); - } + private static int ColumnCount(MetaManipulation.Type type) + => type switch + { + MetaManipulation.Type.Imc => 10, + MetaManipulation.Type.Eqdp => 7, + MetaManipulation.Type.Eqp => 5, + MetaManipulation.Type.Est => 7, + MetaManipulation.Type.Gmp => 7, + MetaManipulation.Type.Rsp => 5, + MetaManipulation.Type.GlobalEqp => 4, + _ => 0, + }; + private void DrawEditHeader(MetaManipulation.Type type) + { + var oldPos = ImGui.GetCursorPosY(); + var header = ImUtf8.CollapsingHeader($"{_editor.MetaEditor.GetCount(type)} {Label(type)}"); + DrawOtherOptionData(type, oldPos, ImGui.GetCursorPos()); if (!header) return; - using (var table = ImRaii.Table(label, numColumns, flags)) - { - if (table) - { - drawNew(_metaFileManager, _editor, _iconSize); - foreach (var (item, index) in items.ToArray().WithIndex()) - { - using var id = ImRaii.PushId(index); - draw(_metaFileManager, item, _editor, _iconSize); - } - } - } + DrawTable(type); + } + private IMetaDrawer? Drawer(MetaManipulation.Type type) + => type switch + { + //MetaManipulation.Type.Imc => expr, + //MetaManipulation.Type.Eqdp => expr, + //MetaManipulation.Type.Eqp => expr, + //MetaManipulation.Type.Est => expr, + //MetaManipulation.Type.Gmp => expr, + //MetaManipulation.Type.Rsp => expr, + //MetaManipulation.Type.GlobalEqp => expr, + _ => null, + }; + + private void DrawTable(MetaManipulation.Type type) + { + const ImGuiTableFlags flags = ImGuiTableFlags.RowBg | ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.BordersInnerV; + using var table = ImUtf8.Table(Label(type), ColumnCount(type), flags); + if (!table) + return; + + if (Drawer(type) is not { } drawer) + return; + + drawer.Draw(); ImGui.NewLine(); } + private void DrawOtherOptionData(MetaManipulation.Type type, float oldPos, Vector2 newPos) + { + var otherOptionData = _editor.MetaEditor.OtherData[type]; + if (otherOptionData.TotalCount <= 0) + return; + + var text = $"{otherOptionData.TotalCount} Edits in other Options"; + var size = ImGui.CalcTextSize(text).X; + ImGui.SetCursorPos(new Vector2(ImGui.GetContentRegionAvail().X - size, oldPos + ImGui.GetStyle().FramePadding.Y)); + ImGuiUtil.TextColored(ColorId.RedundantAssignment.Value() | 0xFF000000, text); + if (ImGui.IsItemHovered()) + { + using var tt = ImUtf8.Tooltip(); + foreach (var name in otherOptionData) + ImUtf8.Text(name); + } + + ImGui.SetCursorPos(newPos); + } + +#if false private static class EqpRow { - private static EqpManipulation _new = new(Eqp.DefaultEntry, EquipSlot.Head, 1); + private static EqpIdentifier _newIdentifier = new(1, EquipSlot.Body); private static float IdWidth => 100 * UiHelpers.Scale; @@ -140,8 +163,8 @@ public partial class ModEditWindow CopyToClipboardButton("Copy all current EQP manipulations to clipboard.", iconSize, editor.MetaEditor.Eqp.Select(m => (MetaManipulation)m)); ImGui.TableNextColumn(); - var canAdd = editor.MetaEditor.CanAdd(_new); - var tt = canAdd ? "Stage this edit." : "This entry is already edited."; + var canAdd = editor.MetaEditor.CanAdd(_new); + var tt = canAdd ? "Stage this edit." : "This entry is already edited."; var defaultEntry = ExpandedEqpFile.GetDefault(metaFileManager, _new.SetId); if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) editor.MetaEditor.Add(_new.Copy(defaultEntry)); @@ -197,7 +220,7 @@ public partial class ModEditWindow var idx = 0; foreach (var flag in Eqp.EqpAttributes[meta.Slot]) { - using var id = ImRaii.PushId(idx++); + using var id = ImRaii.PushId(idx++); var defaultValue = defaultEntry.HasFlag(flag); var currentValue = meta.Entry.HasFlag(flag); if (Checkmark("##eqp", flag.ToLocalName(), currentValue, defaultValue, out var value)) @@ -209,8 +232,6 @@ public partial class ModEditWindow ImGui.NewLine(); } } - - private static class EqdpRow { private static EqdpManipulation _new = new(EqdpEntry.Invalid, EquipSlot.Head, Gender.Male, ModelRace.Midlander, 1); @@ -224,9 +245,9 @@ public partial class ModEditWindow CopyToClipboardButton("Copy all current EQDP manipulations to clipboard.", iconSize, editor.MetaEditor.Eqdp.Select(m => (MetaManipulation)m)); ImGui.TableNextColumn(); - var raceCode = Names.CombinedRace(_new.Gender, _new.Race); + var raceCode = Names.CombinedRace(_new.Gender, _new.Race); var validRaceCode = CharacterUtilityData.EqdpIdx(raceCode, false) >= 0; - var canAdd = validRaceCode && editor.MetaEditor.CanAdd(_new); + var canAdd = validRaceCode && editor.MetaEditor.CanAdd(_new); var tt = canAdd ? "Stage this edit." : validRaceCode ? "This entry is already edited." : "This combination of race and gender can not be used."; var defaultEntry = validRaceCode @@ -311,7 +332,7 @@ public partial class ModEditWindow var defaultEntry = ExpandedEqdpFile.GetDefault(metaFileManager, Names.CombinedRace(meta.Gender, meta.Race), meta.Slot.IsAccessory(), meta.SetId); var (defaultBit1, defaultBit2) = defaultEntry.ToBits(meta.Slot); - var (bit1, bit2) = meta.Entry.ToBits(meta.Slot); + var (bit1, bit2) = meta.Entry.ToBits(meta.Slot); ImGui.TableNextColumn(); if (Checkmark("Material##eqdpCheck1", string.Empty, bit1, defaultBit1, out var newBit1)) editor.MetaEditor.Change(meta.Copy(Eqdp.FromSlotAndBits(meta.Slot, newBit1, bit2))); @@ -321,136 +342,7 @@ public partial class ModEditWindow editor.MetaEditor.Change(meta.Copy(Eqdp.FromSlotAndBits(meta.Slot, bit1, newBit2))); } } - - private static class ImcRow - { - private static ImcIdentifier _newIdentifier = ImcIdentifier.Default; - - private static float IdWidth - => 80 * UiHelpers.Scale; - - private static float SmallIdWidth - => 45 * UiHelpers.Scale; - - public static void DrawNew(MetaFileManager metaFileManager, ModEditor editor, Vector2 iconSize) - { - ImGui.TableNextColumn(); - CopyToClipboardButton("Copy all current IMC manipulations to clipboard.", iconSize, - editor.MetaEditor.Imc.Select(m => (MetaManipulation)m)); - ImGui.TableNextColumn(); - var (defaultEntry, fileExists, _) = metaFileManager.ImcChecker.GetDefaultEntry(_newIdentifier, true); - var manip = (MetaManipulation)new ImcManipulation(_newIdentifier, defaultEntry); - var canAdd = fileExists && editor.MetaEditor.CanAdd(manip); - var tt = canAdd ? "Stage this edit." : !fileExists ? "This IMC file does not exist." : "This entry is already edited."; - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) - editor.MetaEditor.Add(manip); - - // Identifier - ImGui.TableNextColumn(); - var change = ImcManipulationDrawer.DrawObjectType(ref _newIdentifier); - - ImGui.TableNextColumn(); - change |= ImcManipulationDrawer.DrawPrimaryId(ref _newIdentifier); - using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, - new Vector2(3 * UiHelpers.Scale, ImGui.GetStyle().ItemSpacing.Y)); - - ImGui.TableNextColumn(); - // Equipment and accessories are slightly different imcs than other types. - if (_newIdentifier.ObjectType is ObjectType.Equipment or ObjectType.Accessory) - change |= ImcManipulationDrawer.DrawSlot(ref _newIdentifier); - else - change |= ImcManipulationDrawer.DrawSecondaryId(ref _newIdentifier); - - ImGui.TableNextColumn(); - change |= ImcManipulationDrawer.DrawVariant(ref _newIdentifier); - - ImGui.TableNextColumn(); - if (_newIdentifier.ObjectType is ObjectType.DemiHuman) - change |= ImcManipulationDrawer.DrawSlot(ref _newIdentifier, 70); - else - ImGui.Dummy(new Vector2(70 * UiHelpers.Scale, 0)); - - if (change) - defaultEntry = metaFileManager.ImcChecker.GetDefaultEntry(_newIdentifier, true).Entry; - // Values - using var disabled = ImRaii.Disabled(); - ImGui.TableNextColumn(); - ImcManipulationDrawer.DrawMaterialId(defaultEntry, ref defaultEntry, false); - ImGui.SameLine(); - ImcManipulationDrawer.DrawMaterialAnimationId(defaultEntry, ref defaultEntry, false); - ImGui.TableNextColumn(); - ImcManipulationDrawer.DrawDecalId(defaultEntry, ref defaultEntry, false); - ImGui.SameLine(); - ImcManipulationDrawer.DrawVfxId(defaultEntry, ref defaultEntry, false); - ImGui.SameLine(); - ImcManipulationDrawer.DrawSoundId(defaultEntry, ref defaultEntry, false); - ImGui.TableNextColumn(); - ImcManipulationDrawer.DrawAttributes(defaultEntry, ref defaultEntry); - } - - public static void Draw(MetaFileManager metaFileManager, ImcManipulation meta, ModEditor editor, Vector2 iconSize) - { - DrawMetaButtons(meta, editor, iconSize); - - // Identifier - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.ObjectType.ToName()); - ImGuiUtil.HoverTooltip(ObjectTypeTooltip); - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.PrimaryId.ToString()); - ImGuiUtil.HoverTooltip(PrimaryIdTooltipShort); - - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - if (meta.ObjectType is ObjectType.Equipment or ObjectType.Accessory) - { - ImGui.TextUnformatted(meta.EquipSlot.ToName()); - ImGuiUtil.HoverTooltip(EquipSlotTooltip); - } - else - { - ImGui.TextUnformatted(meta.SecondaryId.ToString()); - ImGuiUtil.HoverTooltip(SecondaryIdTooltip); - } - - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.Variant.ToString()); - ImGuiUtil.HoverTooltip(VariantIdTooltip); - - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - if (meta.ObjectType is ObjectType.DemiHuman) - { - ImGui.TextUnformatted(meta.EquipSlot.ToName()); - ImGuiUtil.HoverTooltip(EquipSlotTooltip); - } - - // Values - using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, - new Vector2(3 * UiHelpers.Scale, ImGui.GetStyle().ItemSpacing.Y)); - ImGui.TableNextColumn(); - var defaultEntry = metaFileManager.ImcChecker.GetDefaultEntry(meta.Identifier, true).Entry; - var newEntry = meta.Entry; - var changes = ImcManipulationDrawer.DrawMaterialId(defaultEntry, ref newEntry, true); - ImGui.SameLine(); - changes |= ImcManipulationDrawer.DrawMaterialAnimationId(defaultEntry, ref newEntry, true); - ImGui.TableNextColumn(); - changes |= ImcManipulationDrawer.DrawDecalId(defaultEntry, ref newEntry, true); - ImGui.SameLine(); - changes |= ImcManipulationDrawer.DrawVfxId(defaultEntry, ref newEntry, true); - ImGui.SameLine(); - changes |= ImcManipulationDrawer.DrawSoundId(defaultEntry, ref newEntry, true); - ImGui.TableNextColumn(); - changes |= ImcManipulationDrawer.DrawAttributes(defaultEntry, ref newEntry); - - if (changes) - editor.MetaEditor.Change(meta.Copy(newEntry)); - } - } - + private static class EstRow { private static EstManipulation _new = new(Gender.Male, ModelRace.Midlander, EstType.Body, 1, EstEntry.Zero); @@ -464,8 +356,8 @@ public partial class ModEditWindow CopyToClipboardButton("Copy all current EST manipulations to clipboard.", iconSize, editor.MetaEditor.Est.Select(m => (MetaManipulation)m)); ImGui.TableNextColumn(); - var canAdd = editor.MetaEditor.CanAdd(_new); - var tt = canAdd ? "Stage this edit." : "This entry is already edited."; + var canAdd = editor.MetaEditor.CanAdd(_new); + var tt = canAdd ? "Stage this edit." : "This entry is already edited."; var defaultEntry = EstFile.GetDefault(metaFileManager, _new.Slot, Names.CombinedRace(_new.Gender, _new.Race), _new.SetId); if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) editor.MetaEditor.Add(_new.Copy(defaultEntry)); @@ -538,12 +430,11 @@ public partial class ModEditWindow // Values var defaultEntry = EstFile.GetDefault(metaFileManager, meta.Slot, Names.CombinedRace(meta.Gender, meta.Race), meta.SetId); ImGui.TableNextColumn(); - if (IntDragInput("##estSkeleton", $"Skeleton Index\nDefault Value: {defaultEntry}", IdWidth, meta.Entry.Value, defaultEntry.Value, + if (IntDragInput("##estSkeleton", $"Skeleton Index\nDefault Value: {defaultEntry}", IdWidth, meta.Entry.Value, defaultEntry.Value, out var entry, 0, ushort.MaxValue, 0.05f)) editor.MetaEditor.Change(meta.Copy(new EstEntry((ushort)entry))); } } - private static class GmpRow { private static GmpManipulation _new = new(GmpEntry.Default, 1); @@ -563,8 +454,8 @@ public partial class ModEditWindow CopyToClipboardButton("Copy all current GMP manipulations to clipboard.", iconSize, editor.MetaEditor.Gmp.Select(m => (MetaManipulation)m)); ImGui.TableNextColumn(); - var canAdd = editor.MetaEditor.CanAdd(_new); - var tt = canAdd ? "Stage this edit." : "This entry is already edited."; + var canAdd = editor.MetaEditor.CanAdd(_new); + var tt = canAdd ? "Stage this edit." : "This entry is already edited."; var defaultEntry = ExpandedGmpFile.GetDefault(metaFileManager, _new.SetId); if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) editor.MetaEditor.Add(_new.Copy(defaultEntry)); @@ -643,7 +534,6 @@ public partial class ModEditWindow editor.MetaEditor.Change(meta.Copy(meta.Entry with { UnknownB = (byte)unkB })); } } - private static class RspRow { private static RspManipulation _new = new(SubRace.Midlander, RspAttribute.MaleMinSize, RspEntry.One); @@ -657,8 +547,8 @@ public partial class ModEditWindow CopyToClipboardButton("Copy all current RSP manipulations to clipboard.", iconSize, editor.MetaEditor.Rsp.Select(m => (MetaManipulation)m)); ImGui.TableNextColumn(); - var canAdd = editor.MetaEditor.CanAdd(_new); - var tt = canAdd ? "Stage this edit." : "This entry is already edited."; + var canAdd = editor.MetaEditor.CanAdd(_new); + var tt = canAdd ? "Stage this edit." : "This entry is already edited."; var defaultEntry = CmpFile.GetDefault(metaFileManager, _new.SubRace, _new.Attribute); if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) editor.MetaEditor.Add(_new.Copy(defaultEntry)); @@ -700,7 +590,7 @@ public partial class ModEditWindow ImGui.TableNextColumn(); // Values - var def = CmpFile.GetDefault(metaFileManager, meta.SubRace, meta.Attribute).Value; + var def = CmpFile.GetDefault(metaFileManager, meta.SubRace, meta.Attribute).Value; var value = meta.Entry.Value; ImGui.SetNextItemWidth(FloatWidth); using var color = ImRaii.PushColor(ImGuiCol.FrameBg, @@ -713,12 +603,11 @@ public partial class ModEditWindow ImGuiUtil.HoverTooltip($"Default Value: {def:0.###}"); } } - private static class GlobalEqpRow { private static GlobalEqpManipulation _new = new() { - Type = GlobalEqpType.DoNotHideEarrings, + Type = GlobalEqpType.DoNotHideEarrings, Condition = 1, }; @@ -729,7 +618,7 @@ public partial class ModEditWindow editor.MetaEditor.GlobalEqp.Select(m => (MetaManipulation)m)); ImGui.TableNextColumn(); var canAdd = editor.MetaEditor.CanAdd(_new); - var tt = canAdd ? "Stage this edit." : "This entry is already manipulated."; + var tt = canAdd ? "Stage this edit." : "This entry is already manipulated."; if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) editor.MetaEditor.Add(_new); @@ -744,7 +633,7 @@ public partial class ModEditWindow if (ImUtf8.Selectable(type.ToName(), type == _new.Type)) _new = new GlobalEqpManipulation { - Type = type, + Type = type, Condition = type.HasCondition() ? _new.Type.HasCondition() ? _new.Condition : 1 : 0, }; ImUtf8.HoverTooltip(type.ToDescription()); @@ -777,6 +666,7 @@ public partial class ModEditWindow } } } +#endif // A number input for ids with a optional max id of given width. // Returns true if newId changed against currentId. @@ -824,7 +714,7 @@ public partial class ModEditWindow return newValue != currentValue; } - private static void CopyToClipboardButton(string tooltip, Vector2 iconSize, IEnumerable manipulations) + private static void CopyToClipboardButton(string tooltip, Vector2 iconSize, MetaDictionary manipulations) { if (!ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Clipboard.ToIconString(), iconSize, tooltip, false, true)) return; @@ -840,10 +730,9 @@ public partial class ModEditWindow { var clipboard = ImGuiUtil.GetClipboardText(); - var version = Functions.FromCompressedBase64(clipboard, out var manips); + var version = Functions.FromCompressedBase64(clipboard, out var manips); if (version == MetaManipulation.CurrentVersion && manips != null) - foreach (var manip in manips.Where(m => m.ManipulationType != MetaManipulation.Type.Unknown)) - _editor.MetaEditor.Set(manip); + _editor.MetaEditor.UpdateTo(manips); } ImGuiUtil.HoverTooltip( @@ -855,13 +744,9 @@ public partial class ModEditWindow if (ImGui.Button("Set from Clipboard")) { var clipboard = ImGuiUtil.GetClipboardText(); - var version = Functions.FromCompressedBase64(clipboard, out var manips); + var version = Functions.FromCompressedBase64(clipboard, out var manips); if (version == MetaManipulation.CurrentVersion && manips != null) - { - _editor.MetaEditor.Clear(); - foreach (var manip in manips.Where(m => m.ManipulationType != MetaManipulation.Type.Unknown)) - _editor.MetaEditor.Set(manip); - } + _editor.MetaEditor.SetTo(manips); } ImGuiUtil.HoverTooltip( @@ -870,11 +755,184 @@ public partial class ModEditWindow private static void DrawMetaButtons(MetaManipulation meta, ModEditor editor, Vector2 iconSize) { - ImGui.TableNextColumn(); - CopyToClipboardButton("Copy this manipulation to clipboard.", iconSize, Array.Empty().Append(meta)); - - ImGui.TableNextColumn(); - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Trash.ToIconString(), iconSize, "Delete this meta manipulation.", false, true)) - editor.MetaEditor.Delete(meta); + //ImGui.TableNextColumn(); + //CopyToClipboardButton("Copy this manipulation to clipboard.", iconSize, Array.Empty().Append(meta)); + // + //ImGui.TableNextColumn(); + //if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Trash.ToIconString(), iconSize, "Delete this meta manipulation.", false, true)) + // editor.MetaEditor.Delete(meta); } +} + + +public interface IMetaDrawer +{ + public void Draw(); } + + + + +public abstract class MetaDrawer(ModEditor editor, MetaFileManager metaFiles) : IMetaDrawer + where TIdentifier : unmanaged, IMetaIdentifier + where TEntry : unmanaged +{ + protected readonly ModEditor Editor = editor; + protected readonly MetaFileManager MetaFiles = metaFiles; + protected TIdentifier Identifier; + protected TEntry Entry; + private bool _initialized; + + public void Draw() + { + if (!_initialized) + { + Initialize(); + _initialized = true; + } + + DrawNew(); + foreach (var ((identifier, entry), idx) in Enumerate().WithIndex()) + { + using var id = ImUtf8.PushId(idx); + DrawEntry(identifier, entry); + } + } + + protected abstract void DrawNew(); + protected abstract void Initialize(); + protected abstract void DrawEntry(TIdentifier identifier, TEntry entry); + + protected abstract IEnumerable<(TIdentifier, TEntry)> Enumerate(); +} + + +#if false +public sealed class GmpMetaDrawer(ModEditor editor) : MetaDrawer, IService +{ + protected override void Initialize() + { + Identifier = new GmpIdentifier(1, EquipSlot.Body); + UpdateEntry(); + } + + private void UpdateEntry() + => Entry = ExpandedEqpFile.GetDefault(metaManager, Identifier.SetId); + + protected override void DrawNew() + { } + + protected override void DrawEntry(GmpIdentifier identifier, GmpEntry entry) + { } + + protected override IEnumerable<(GmpIdentifier, GmpEntry)> Enumerate() + => editor.MetaEditor.Eqp.Select(kvp => (kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))); +} + +public sealed class EstMetaDrawer(ModEditor editor) : MetaDrawer, IService +{ + protected override void Initialize() + { + Identifier = new EqpIdentifier(1, EquipSlot.Body); + UpdateEntry(); + } + + private void UpdateEntry() + => Entry = ExpandedEqpFile.GetDefault(metaManager, Identifier.SetId); + + protected override void DrawNew() + { } + + protected override void DrawEntry(EstIdentifier identifier, EstEntry entry) + { } + + protected override IEnumerable<(EstIdentifier, EstEntry)> Enumerate() + => editor.MetaEditor.Eqp.Select(kvp => (kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))); +} + +public sealed class EqdpMetaDrawer(ModEditor editor) : MetaDrawer, IService +{ + protected override void Initialize() + { + Identifier = new EqdpIdentifier(1, EquipSlot.Body); + UpdateEntry(); + } + + private void UpdateEntry() + => Entry = ExpandedEqpFile.GetDefault(metaManager, Identifier.SetId); + + protected override void DrawNew() + { } + + protected override void DrawEntry(EqdpIdentifier identifier, EqdpEntry entry) + { } + + protected override IEnumerable<(EqdpIdentifier, EqdpEntry)> Enumerate() + => editor.MetaEditor.Eqp.Select(kvp => (kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))); +} + +public sealed class EqpMetaDrawer(ModEditor editor, MetaFileManager metaManager) : MetaDrawer, IService +{ + protected override void Initialize() + { + Identifier = new EqpIdentifier(1, EquipSlot.Body); + UpdateEntry(); + } + + private void UpdateEntry() + => Entry = ExpandedEqpFile.GetDefault(metaManager, Identifier.SetId); + + protected override void DrawNew() + { } + + protected override void DrawEntry(EqpIdentifier identifier, EqpEntry entry) + { } + + protected override IEnumerable<(EqpIdentifier, EqpEntry)> Enumerate() + => editor.MetaEditor.Eqp.Select(kvp => (kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))); +} + +public sealed class RspMetaDrawer(ModEditor editor) : MetaDrawer, IService +{ + protected override void Initialize() + { + Identifier = new RspIdentifier(1, EquipSlot.Body); + UpdateEntry(); + } + + private void UpdateEntry() + => Entry = ExpandedEqpFile.GetDefault(metaManager, Identifier.SetId); + + protected override void DrawNew() + { } + + protected override void DrawEntry(RspIdentifier identifier, RspEntry entry) + { } + + protected override IEnumerable<(RspIdentifier, RspEntry)> Enumerate() + => editor.MetaEditor.Eqp.Select(kvp => (kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))); +} + + + +public sealed class GlobalEqpMetaDrawer(ModEditor editor) : MetaDrawer, IService +{ + protected override void Initialize() + { + Identifier = new EqpIdentifier(1, EquipSlot.Body); + UpdateEntry(); + } + + private void UpdateEntry() + => Entry = ExpandedEqpFile.GetDefault(metaManager, Identifier.SetId); + + protected override void DrawNew() + { } + + protected override void DrawEntry(GlobalEqpManipulation identifier, byte _) + { } + + protected override IEnumerable<(GlobalEqpManipulation, byte)> Enumerate() + => editor.MetaEditor.Eqp.Select(kvp => (kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))); +} +#endif diff --git a/Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs b/Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs index 3ac10cd0..689571f3 100644 --- a/Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/AddGroupDrawer.cs @@ -9,6 +9,7 @@ using Penumbra.Meta.Manipulations; using Penumbra.Mods; using Penumbra.Mods.Manager; using Penumbra.Mods.Manager.OptionEditor; +using Penumbra.UI.AdvancedWindow.Meta; using Penumbra.UI.Classes; namespace Penumbra.UI.ModsTab.Groups; @@ -79,29 +80,29 @@ public class AddGroupDrawer : IUiService private void DrawImcInput(float width) { - var change = ImcManipulationDrawer.DrawObjectType(ref _imcIdentifier, width); + var change = ImcMetaDrawer.DrawObjectType(ref _imcIdentifier, width); ImUtf8.SameLineInner(); - change |= ImcManipulationDrawer.DrawPrimaryId(ref _imcIdentifier, width); + change |= ImcMetaDrawer.DrawPrimaryId(ref _imcIdentifier, width); if (_imcIdentifier.ObjectType is ObjectType.Weapon or ObjectType.Monster) { - change |= ImcManipulationDrawer.DrawSecondaryId(ref _imcIdentifier, width); + change |= ImcMetaDrawer.DrawSecondaryId(ref _imcIdentifier, width); ImUtf8.SameLineInner(); - change |= ImcManipulationDrawer.DrawVariant(ref _imcIdentifier, width); + change |= ImcMetaDrawer.DrawVariant(ref _imcIdentifier, width); } else if (_imcIdentifier.ObjectType is ObjectType.DemiHuman) { var quarterWidth = (width - ImUtf8.ItemInnerSpacing.X / ImUtf8.GlobalScale) / 2; - change |= ImcManipulationDrawer.DrawSecondaryId(ref _imcIdentifier, width); + change |= ImcMetaDrawer.DrawSecondaryId(ref _imcIdentifier, width); ImUtf8.SameLineInner(); - change |= ImcManipulationDrawer.DrawSlot(ref _imcIdentifier, quarterWidth); + change |= ImcMetaDrawer.DrawSlot(ref _imcIdentifier, quarterWidth); ImUtf8.SameLineInner(); - change |= ImcManipulationDrawer.DrawVariant(ref _imcIdentifier, quarterWidth); + change |= ImcMetaDrawer.DrawVariant(ref _imcIdentifier, quarterWidth); } else { - change |= ImcManipulationDrawer.DrawSlot(ref _imcIdentifier, width); + change |= ImcMetaDrawer.DrawSlot(ref _imcIdentifier, width); ImUtf8.SameLineInner(); - change |= ImcManipulationDrawer.DrawVariant(ref _imcIdentifier, width); + change |= ImcMetaDrawer.DrawVariant(ref _imcIdentifier, width); } if (change) diff --git a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs index 5c8edce6..5d10febd 100644 --- a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs @@ -7,6 +7,7 @@ using Penumbra.GameData.Structs; using Penumbra.Mods.Groups; using Penumbra.Mods.Manager.OptionEditor; using Penumbra.Mods.SubMods; +using Penumbra.UI.AdvancedWindow.Meta; namespace Penumbra.UI.ModsTab.Groups; @@ -37,9 +38,9 @@ public readonly struct ImcModGroupEditDrawer(ModGroupEditDrawer editor, ImcModGr ImGui.SameLine(); using (ImUtf8.Group()) { - changes |= ImcManipulationDrawer.DrawMaterialId(defaultEntry, ref entry, true); - changes |= ImcManipulationDrawer.DrawVfxId(defaultEntry, ref entry, true); - changes |= ImcManipulationDrawer.DrawDecalId(defaultEntry, ref entry, true); + changes |= ImcMetaDrawer.DrawMaterialId(defaultEntry, ref entry, true); + changes |= ImcMetaDrawer.DrawVfxId(defaultEntry, ref entry, true); + changes |= ImcMetaDrawer.DrawDecalId(defaultEntry, ref entry, true); } ImGui.SameLine(0, editor.PriorityWidth); @@ -54,8 +55,8 @@ public readonly struct ImcModGroupEditDrawer(ModGroupEditDrawer editor, ImcModGr using (ImUtf8.Group()) { - changes |= ImcManipulationDrawer.DrawMaterialAnimationId(defaultEntry, ref entry, true); - changes |= ImcManipulationDrawer.DrawSoundId(defaultEntry, ref entry, true); + changes |= ImcMetaDrawer.DrawMaterialAnimationId(defaultEntry, ref entry, true); + changes |= ImcMetaDrawer.DrawSoundId(defaultEntry, ref entry, true); var canBeDisabled = group.CanBeDisabled; if (ImUtf8.Checkbox("##disabled"u8, ref canBeDisabled)) editor.ModManager.OptionEditor.ImcEditor.ChangeCanBeDisabled(group, canBeDisabled); diff --git a/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs b/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs index 694ae11c..1291f568 100644 --- a/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs +++ b/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs @@ -10,210 +10,5 @@ namespace Penumbra.UI.ModsTab; public static class ImcManipulationDrawer { - public static bool DrawObjectType(ref ImcIdentifier identifier, float width = 110) - { - var ret = Combos.ImcType("##imcType", identifier.ObjectType, out var type, width); - ImUtf8.HoverTooltip("Object Type"u8); - - if (ret) - { - var equipSlot = type switch - { - ObjectType.Equipment => identifier.EquipSlot.IsEquipment() ? identifier.EquipSlot : EquipSlot.Head, - ObjectType.DemiHuman => identifier.EquipSlot.IsEquipment() ? identifier.EquipSlot : EquipSlot.Head, - ObjectType.Accessory => identifier.EquipSlot.IsAccessory() ? identifier.EquipSlot : EquipSlot.Ears, - _ => EquipSlot.Unknown, - }; - identifier = identifier with - { - ObjectType = type, - EquipSlot = equipSlot, - SecondaryId = identifier.SecondaryId == 0 ? 1 : identifier.SecondaryId, - }; - } - - return ret; - } - - public static bool DrawPrimaryId(ref ImcIdentifier identifier, float unscaledWidth = 80) - { - var ret = IdInput("##imcPrimaryId"u8, unscaledWidth, identifier.PrimaryId.Id, out var newId, 0, ushort.MaxValue, - identifier.PrimaryId.Id <= 1); - ImUtf8.HoverTooltip("Primary ID - You can usually find this as the 'x####' part of an item path.\n"u8 - + "This should generally not be left <= 1 unless you explicitly want that."u8); - if (ret) - identifier = identifier with { PrimaryId = newId }; - return ret; - } - - public static bool DrawSecondaryId(ref ImcIdentifier identifier, float unscaledWidth = 100) - { - var ret = IdInput("##imcSecondaryId"u8, unscaledWidth, identifier.SecondaryId.Id, out var newId, 0, ushort.MaxValue, false); - ImUtf8.HoverTooltip("Secondary ID"u8); - if (ret) - identifier = identifier with { SecondaryId = newId }; - return ret; - } - - public static bool DrawVariant(ref ImcIdentifier identifier, float unscaledWidth = 45) - { - var ret = IdInput("##imcVariant"u8, unscaledWidth, identifier.Variant.Id, out var newId, 0, byte.MaxValue, false); - ImUtf8.HoverTooltip("Variant ID"u8); - if (ret) - identifier = identifier with { Variant = (byte)newId }; - return ret; - } - - public static bool DrawSlot(ref ImcIdentifier identifier, float unscaledWidth = 100) - { - bool ret; - EquipSlot slot; - switch (identifier.ObjectType) - { - case ObjectType.Equipment: - case ObjectType.DemiHuman: - ret = Combos.EqpEquipSlot("##slot", identifier.EquipSlot, out slot, unscaledWidth); - break; - case ObjectType.Accessory: - ret = Combos.AccessorySlot("##slot", identifier.EquipSlot, out slot, unscaledWidth); - break; - default: return false; - } - - ImUtf8.HoverTooltip("Equip Slot"u8); - if (ret) - identifier = identifier with { EquipSlot = slot }; - return ret; - } - - public static bool DrawMaterialId(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault, float unscaledWidth = 45) - { - if (!DragInput("##materialId"u8, "Material ID"u8, unscaledWidth * ImUtf8.GlobalScale, entry.MaterialId, defaultEntry.MaterialId, - out var newValue, (byte)1, byte.MaxValue, 0.01f, addDefault)) - return false; - - entry = entry with { MaterialId = newValue }; - return true; - } - - public static bool DrawMaterialAnimationId(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault, float unscaledWidth = 45) - { - if (!DragInput("##mAnimId"u8, "Material Animation ID"u8, unscaledWidth * ImUtf8.GlobalScale, entry.MaterialAnimationId, - defaultEntry.MaterialAnimationId, out var newValue, (byte)0, byte.MaxValue, 0.01f, addDefault)) - return false; - - entry = entry with { MaterialAnimationId = newValue }; - return true; - } - - public static bool DrawDecalId(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault, float unscaledWidth = 45) - { - if (!DragInput("##decalId"u8, "Decal ID"u8, unscaledWidth * ImUtf8.GlobalScale, entry.DecalId, defaultEntry.DecalId, out var newValue, - (byte)0, byte.MaxValue, 0.01f, addDefault)) - return false; - - entry = entry with { DecalId = newValue }; - return true; - } - - public static bool DrawVfxId(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault, float unscaledWidth = 45) - { - if (!DragInput("##vfxId"u8, "VFX ID"u8, unscaledWidth * ImUtf8.GlobalScale, entry.VfxId, defaultEntry.VfxId, out var newValue, (byte)0, - byte.MaxValue, 0.01f, addDefault)) - return false; - - entry = entry with { VfxId = newValue }; - return true; - } - - public static bool DrawSoundId(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault, float unscaledWidth = 45) - { - if (!DragInput("##soundId"u8, "Sound ID"u8, unscaledWidth * ImUtf8.GlobalScale, entry.SoundId, defaultEntry.SoundId, out var newValue, - (byte)0, byte.MaxValue, 0.01f, addDefault)) - return false; - - entry = entry with { SoundId = newValue }; - return true; - } - - public static bool DrawAttributes(ImcEntry defaultEntry, ref ImcEntry entry) - { - var changes = false; - for (var i = 0; i < ImcEntry.NumAttributes; ++i) - { - using var id = ImRaii.PushId(i); - var flag = 1 << i; - var value = (entry.AttributeMask & flag) != 0; - var def = (defaultEntry.AttributeMask & flag) != 0; - if (Checkmark("##attribute"u8, "ABCDEFGHIJ"u8.Slice(i, 1), value, def, out var newValue)) - { - var newMask = (ushort)(newValue ? entry.AttributeMask | flag : entry.AttributeMask & ~flag); - entry = entry with { AttributeMask = newMask }; - changes = true; - } - - if (i < ImcEntry.NumAttributes - 1) - ImGui.SameLine(); - } - - return changes; - } - - - /// - /// A number input for ids with an optional max id of given width. - /// Returns true if newId changed against currentId. - /// - private static bool IdInput(ReadOnlySpan label, float unscaledWidth, ushort currentId, out ushort newId, int minId, int maxId, - bool border) - { - int tmp = currentId; - ImGui.SetNextItemWidth(unscaledWidth * ImUtf8.GlobalScale); - using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, UiHelpers.Scale, border); - using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.RegexWarningBorder, border); - if (ImUtf8.InputScalar(label, ref tmp)) - tmp = Math.Clamp(tmp, minId, maxId); - - newId = (ushort)tmp; - return newId != currentId; - } - - /// - /// A dragging int input of given width that compares against a default value, shows a tooltip and clamps against min and max. - /// Returns true if newValue changed against currentValue. - /// - private static bool DragInput(ReadOnlySpan label, ReadOnlySpan tooltip, float width, T currentValue, T defaultValue, - out T newValue, T minValue, T maxValue, float speed, bool addDefault) where T : unmanaged, INumber - { - newValue = currentValue; - using var color = ImRaii.PushColor(ImGuiCol.FrameBg, - defaultValue > currentValue ? ColorId.DecreasedMetaValue.Value() : ColorId.IncreasedMetaValue.Value(), - defaultValue != currentValue); - ImGui.SetNextItemWidth(width); - if (ImUtf8.DragScalar(label, ref newValue, minValue, maxValue, speed)) - newValue = newValue <= minValue ? minValue : newValue >= maxValue ? maxValue : newValue; - - if (addDefault) - ImUtf8.HoverTooltip($"{tooltip}\nDefault Value: {defaultValue}"); - else - ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, tooltip); - - return newValue != currentValue; - } - - /// - /// A checkmark that compares against a default value and shows a tooltip. - /// Returns true if newValue is changed against currentValue. - /// - private static bool Checkmark(ReadOnlySpan label, ReadOnlySpan tooltip, bool currentValue, bool defaultValue, - out bool newValue) - { - using var color = ImRaii.PushColor(ImGuiCol.FrameBg, - defaultValue ? ColorId.DecreasedMetaValue.Value() : ColorId.IncreasedMetaValue.Value(), - defaultValue != currentValue); - newValue = currentValue; - ImUtf8.Checkbox(label, ref newValue); - ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, tooltip); - return newValue != currentValue; - } + } diff --git a/Penumbra/Util/DictionaryExtensions.cs b/Penumbra/Util/DictionaryExtensions.cs index abf715e6..f7aa5598 100644 --- a/Penumbra/Util/DictionaryExtensions.cs +++ b/Penumbra/Util/DictionaryExtensions.cs @@ -45,6 +45,18 @@ public static class DictionaryExtensions lhs.Add(key, value); } + /// Set all entries in the right-hand dictionary to the same values in the left-hand dictionary, ensuring capacity beforehand. + public static void UpdateTo(this Dictionary lhs, IReadOnlyDictionary rhs) + where TKey : notnull + { + if (ReferenceEquals(lhs, rhs)) + return; + + lhs.EnsureCapacity(rhs.Count); + foreach (var (key, value) in rhs) + lhs[key] = value; + } + /// Set one set to the other, deleting previous entries and ensuring capacity beforehand. public static void SetTo(this HashSet lhs, IReadOnlySet rhs) { From 3170edfeb659f0362d46f56fde9c000ee54ba0ee Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 14 Jun 2024 13:38:36 +0200 Subject: [PATCH 240/433] Get rid off all MetaManipulation things. --- Penumbra/Api/Api/MetaApi.cs | 28 +- Penumbra/Api/Api/TemporaryApi.cs | 4 +- Penumbra/Api/IpcTester/TemporaryIpcTester.cs | 8 +- Penumbra/Collections/Cache/CmpCache.cs | 56 -- Penumbra/Collections/Cache/CollectionCache.cs | 40 +- .../Collections/Cache/CollectionModData.cs | 12 +- Penumbra/Collections/Cache/EqdpCache.cs | 85 +- Penumbra/Collections/Cache/EqpCache.cs | 77 +- Penumbra/Collections/Cache/EstCache.cs | 136 ++- .../Cache}/GlobalEqpCache.cs | 44 +- Penumbra/Collections/Cache/GmpCache.cs | 74 +- Penumbra/Collections/Cache/IMetaCache.cs | 89 ++ Penumbra/Collections/Cache/ImcCache.cs | 153 ++-- Penumbra/Collections/Cache/MetaCache.cs | 269 +++--- Penumbra/Collections/Cache/RspCache.cs | 81 ++ Penumbra/Import/Models/ModelManager.cs | 23 +- .../Import/TexToolsMeta.Deserialization.cs | 61 +- Penumbra/Import/TexToolsMeta.Export.cs | 328 ++++--- Penumbra/Import/TexToolsMeta.Rgsp.cs | 17 +- Penumbra/Import/TexToolsMeta.cs | 25 +- .../PathResolving/CollectionResolver.cs | 1 - .../ResolveContext.PathResolution.cs | 2 +- Penumbra/Meta/ImcChecker.cs | 4 - Penumbra/Meta/Manipulations/Eqdp.cs | 9 + .../Meta/Manipulations/EqdpManipulation.cs | 110 --- Penumbra/Meta/Manipulations/Eqp.cs | 3 + .../Meta/Manipulations/EqpManipulation.cs | 80 -- Penumbra/Meta/Manipulations/Est.cs | 16 + .../Meta/Manipulations/EstManipulation.cs | 108 --- .../Manipulations/GlobalEqpManipulation.cs | 26 +- Penumbra/Meta/Manipulations/Gmp.cs | 3 + .../Meta/Manipulations/GmpManipulation.cs | 58 -- .../Meta/Manipulations/IMetaIdentifier.cs | 16 + Penumbra/Meta/Manipulations/Imc.cs | 6 +- .../Meta/Manipulations/ImcManipulation.cs | 108 --- Penumbra/Meta/Manipulations/MetaDictionary.cs | 338 +++++-- .../Meta/Manipulations/MetaManipulation.cs | 457 ---------- Penumbra/Meta/Manipulations/Rsp.cs | 3 + .../Meta/Manipulations/RspManipulation.cs | 67 -- Penumbra/Mods/Editor/IMod.cs | 2 +- Penumbra/Mods/Editor/ModMetaEditor.cs | 24 +- Penumbra/Mods/ItemSwap/EquipmentSwap.cs | 41 +- Penumbra/Mods/ItemSwap/ItemSwap.cs | 29 +- Penumbra/Mods/ItemSwap/ItemSwapContainer.cs | 4 +- Penumbra/Mods/ModCreator.cs | 6 +- Penumbra/Mods/SubMods/DefaultSubMod.cs | 2 +- Penumbra/Mods/SubMods/OptionSubMod.cs | 2 +- Penumbra/Mods/TemporaryMod.cs | 3 +- .../UI/AdvancedWindow/Meta/EqdpMetaDrawer.cs | 159 ++++ .../UI/AdvancedWindow/Meta/EqpMetaDrawer.cs | 134 +++ .../UI/AdvancedWindow/Meta/EstMetaDrawer.cs | 147 +++ .../Meta/GlobalEqpMetaDrawer.cs | 111 +++ .../UI/AdvancedWindow/Meta/GmpMetaDrawer.cs | 148 +++ .../UI/AdvancedWindow/Meta/ImcMetaDrawer.cs | 164 ++-- Penumbra/UI/AdvancedWindow/Meta/MetaDrawer.cs | 154 ++++ .../UI/AdvancedWindow/Meta/MetaDrawers.cs | 35 + .../UI/AdvancedWindow/Meta/RspMetaDrawer.cs | 112 +++ .../UI/AdvancedWindow/ModEditWindow.Meta.cs | 847 +----------------- .../ModEditWindow.Models.MdlTab.cs | 6 +- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 4 +- Penumbra/UI/ModsTab/ModPanelConflictsTab.cs | 6 +- Penumbra/UI/Tabs/EffectiveTab.cs | 10 +- Penumbra/Util/IdentifierExtensions.cs | 94 +- 63 files changed, 2422 insertions(+), 2847 deletions(-) delete mode 100644 Penumbra/Collections/Cache/CmpCache.cs rename Penumbra/{Meta/Manipulations => Collections/Cache}/GlobalEqpCache.cs (75%) create mode 100644 Penumbra/Collections/Cache/IMetaCache.cs create mode 100644 Penumbra/Collections/Cache/RspCache.cs delete mode 100644 Penumbra/Meta/Manipulations/EqdpManipulation.cs delete mode 100644 Penumbra/Meta/Manipulations/EqpManipulation.cs delete mode 100644 Penumbra/Meta/Manipulations/EstManipulation.cs delete mode 100644 Penumbra/Meta/Manipulations/GmpManipulation.cs delete mode 100644 Penumbra/Meta/Manipulations/ImcManipulation.cs delete mode 100644 Penumbra/Meta/Manipulations/MetaManipulation.cs delete mode 100644 Penumbra/Meta/Manipulations/RspManipulation.cs create mode 100644 Penumbra/UI/AdvancedWindow/Meta/EqdpMetaDrawer.cs create mode 100644 Penumbra/UI/AdvancedWindow/Meta/EqpMetaDrawer.cs create mode 100644 Penumbra/UI/AdvancedWindow/Meta/EstMetaDrawer.cs create mode 100644 Penumbra/UI/AdvancedWindow/Meta/GlobalEqpMetaDrawer.cs create mode 100644 Penumbra/UI/AdvancedWindow/Meta/GmpMetaDrawer.cs create mode 100644 Penumbra/UI/AdvancedWindow/Meta/MetaDrawer.cs create mode 100644 Penumbra/UI/AdvancedWindow/Meta/MetaDrawers.cs create mode 100644 Penumbra/UI/AdvancedWindow/Meta/RspMetaDrawer.cs diff --git a/Penumbra/Api/Api/MetaApi.cs b/Penumbra/Api/Api/MetaApi.cs index c467df58..ce1a9def 100644 --- a/Penumbra/Api/Api/MetaApi.cs +++ b/Penumbra/Api/Api/MetaApi.cs @@ -1,5 +1,8 @@ +using Newtonsoft.Json.Linq; using OtterGui; using OtterGui.Services; +using Penumbra.Collections; +using Penumbra.GameData.Structs; using Penumbra.Interop.PathResolving; using Penumbra.Meta.Manipulations; @@ -7,17 +10,34 @@ namespace Penumbra.Api.Api; public class MetaApi(CollectionResolver collectionResolver, ApiHelpers helpers) : IPenumbraApiMeta, IApiService { + public const int CurrentVersion = 0; + public string GetPlayerMetaManipulations() { var collection = collectionResolver.PlayerCollection(); - var set = collection.MetaCache?.Manipulations.ToArray() ?? []; - return Functions.ToCompressedBase64(set, MetaManipulation.CurrentVersion); + return CompressMetaManipulations(collection); } public string GetMetaManipulations(int gameObjectIdx) { helpers.AssociatedCollection(gameObjectIdx, out var collection); - var set = collection.MetaCache?.Manipulations.ToArray() ?? []; - return Functions.ToCompressedBase64(set, MetaManipulation.CurrentVersion); + return CompressMetaManipulations(collection); + } + + internal static string CompressMetaManipulations(ModCollection collection) + { + var array = new JArray(); + if (collection.MetaCache is { } cache) + { + MetaDictionary.SerializeTo(array, cache.GlobalEqp.Select(kvp => kvp.Key)); + MetaDictionary.SerializeTo(array, cache.Imc.Select(kvp => new KeyValuePair(kvp.Key, kvp.Value.Entry))); + MetaDictionary.SerializeTo(array, cache.Eqp.Select(kvp => new KeyValuePair(kvp.Key, kvp.Value.Entry))); + MetaDictionary.SerializeTo(array, cache.Eqdp.Select(kvp => new KeyValuePair(kvp.Key, kvp.Value.Entry))); + MetaDictionary.SerializeTo(array, cache.Est.Select(kvp => new KeyValuePair(kvp.Key, kvp.Value.Entry))); + MetaDictionary.SerializeTo(array, cache.Rsp.Select(kvp => new KeyValuePair(kvp.Key, kvp.Value.Entry))); + MetaDictionary.SerializeTo(array, cache.Gmp.Select(kvp => new KeyValuePair(kvp.Key, kvp.Value.Entry))); + } + + return Functions.ToCompressedBase64(array, CurrentVersion); } } diff --git a/Penumbra/Api/Api/TemporaryApi.cs b/Penumbra/Api/Api/TemporaryApi.cs index 49958a0d..0894a8e5 100644 --- a/Penumbra/Api/Api/TemporaryApi.cs +++ b/Penumbra/Api/Api/TemporaryApi.cs @@ -163,11 +163,11 @@ public class TemporaryApi( { if (manipString.Length == 0) { - manips = []; + manips = new MetaDictionary(); return true; } - if (Functions.FromCompressedBase64(manipString, out manips!) == MetaManipulation.CurrentVersion) + if (Functions.FromCompressedBase64(manipString, out manips!) == MetaApi.CurrentVersion) return true; manips = null; diff --git a/Penumbra/Api/IpcTester/TemporaryIpcTester.cs b/Penumbra/Api/IpcTester/TemporaryIpcTester.cs index 15601867..0aa6821c 100644 --- a/Penumbra/Api/IpcTester/TemporaryIpcTester.cs +++ b/Penumbra/Api/IpcTester/TemporaryIpcTester.cs @@ -5,6 +5,7 @@ using OtterGui; using OtterGui.Raii; using OtterGui.Services; using OtterGui.Text; +using Penumbra.Api.Api; using Penumbra.Api.Enums; using Penumbra.Api.IpcSubscribers; using Penumbra.Collections.Manager; @@ -102,8 +103,7 @@ public class TemporaryIpcTester( && copyCollection is { HasCache: true }) { var files = copyCollection.ResolvedFiles.ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.Path.ToString()); - var manips = Functions.ToCompressedBase64(copyCollection.MetaCache?.Manipulations.ToArray() ?? Array.Empty(), - MetaManipulation.CurrentVersion); + var manips = MetaApi.CompressMetaManipulations(copyCollection); _lastTempError = new AddTemporaryMod(pi).Invoke(_tempModName, guid, files, manips, 999); } @@ -188,8 +188,8 @@ public class TemporaryIpcTester( if (ImGui.IsItemHovered()) { using var tt = ImRaii.Tooltip(); - foreach (var manip in mod.Default.Manipulations) - ImGui.TextUnformatted(manip.ToString()); + foreach (var identifier in mod.Default.Manipulations.Identifiers) + ImGui.TextUnformatted(identifier.ToString()); } } } diff --git a/Penumbra/Collections/Cache/CmpCache.cs b/Penumbra/Collections/Cache/CmpCache.cs deleted file mode 100644 index 470cadd4..00000000 --- a/Penumbra/Collections/Cache/CmpCache.cs +++ /dev/null @@ -1,56 +0,0 @@ -using OtterGui.Filesystem; -using Penumbra.Interop.Services; -using Penumbra.Interop.Structs; -using Penumbra.Meta; -using Penumbra.Meta.Files; -using Penumbra.Meta.Manipulations; - -namespace Penumbra.Collections.Cache; - -public struct CmpCache : IDisposable -{ - private CmpFile? _cmpFile = null; - private readonly List _cmpManipulations = new(); - - public CmpCache() - { } - - public void SetFiles(MetaFileManager manager) - => manager.SetFile(_cmpFile, MetaIndex.HumanCmp); - - public MetaList.MetaReverter TemporarilySetFiles(MetaFileManager manager) - => manager.TemporarilySetFile(_cmpFile, MetaIndex.HumanCmp); - - public void Reset() - { - if (_cmpFile == null) - return; - - _cmpFile.Reset(_cmpManipulations.Select(m => (m.SubRace, m.Attribute))); - _cmpManipulations.Clear(); - } - - public bool ApplyMod(MetaFileManager manager, RspManipulation manip) - { - _cmpManipulations.AddOrReplace(manip); - _cmpFile ??= new CmpFile(manager); - return manip.Apply(_cmpFile); - } - - public bool RevertMod(MetaFileManager manager, RspManipulation manip) - { - if (!_cmpManipulations.Remove(manip)) - return false; - - var def = CmpFile.GetDefault(manager, manip.SubRace, manip.Attribute); - manip = new RspManipulation(manip.SubRace, manip.Attribute, def); - return manip.Apply(_cmpFile!); - } - - public void Dispose() - { - _cmpFile?.Dispose(); - _cmpFile = null; - _cmpManipulations.Clear(); - } -} diff --git a/Penumbra/Collections/Cache/CollectionCache.cs b/Penumbra/Collections/Cache/CollectionCache.cs index 4d8d0b4a..fd801d3b 100644 --- a/Penumbra/Collections/Cache/CollectionCache.cs +++ b/Penumbra/Collections/Cache/CollectionCache.cs @@ -36,7 +36,7 @@ public sealed class CollectionCache : IDisposable => ConflictDict.Values; public SingleArray Conflicts(IMod mod) - => ConflictDict.TryGetValue(mod, out var c) ? c : new SingleArray(); + => ConflictDict.TryGetValue(mod, out SingleArray c) ? c : new SingleArray(); private int _changedItemsSaveCounter = -1; @@ -233,8 +233,20 @@ public sealed class CollectionCache : IDisposable foreach (var (path, file) in files.FileRedirections) AddFile(path, file, mod); - foreach (var manip in files.Manipulations) - AddManipulation(manip, mod); + foreach (var (identifier, entry) in files.Manipulations.Eqp) + AddManipulation(mod, identifier, entry); + foreach (var (identifier, entry) in files.Manipulations.Eqdp) + AddManipulation(mod, identifier, entry); + foreach (var (identifier, entry) in files.Manipulations.Est) + AddManipulation(mod, identifier, entry); + foreach (var (identifier, entry) in files.Manipulations.Gmp) + AddManipulation(mod, identifier, entry); + foreach (var (identifier, entry) in files.Manipulations.Rsp) + AddManipulation(mod, identifier, entry); + foreach (var (identifier, entry) in files.Manipulations.Imc) + AddManipulation(mod, identifier, entry); + foreach (var identifier in files.Manipulations.GlobalEqp) + AddManipulation(mod, identifier, null!); if (addMetaChanges) { @@ -342,7 +354,7 @@ public sealed class CollectionCache : IDisposable foreach (var conflict in tmpConflicts) { if (data is Utf8GamePath path && conflict.Conflicts.RemoveAll(p => p is Utf8GamePath x && x.Equals(path)) > 0 - || data is MetaManipulation meta && conflict.Conflicts.RemoveAll(m => m is MetaManipulation x && x.Equals(meta)) > 0) + || data is IMetaIdentifier meta && conflict.Conflicts.RemoveAll(m => m.Equals(meta)) > 0) AddConflict(data, addedMod, conflict.Mod2); } @@ -374,12 +386,12 @@ public sealed class CollectionCache : IDisposable // For different mods, higher mod priority takes precedence before option group priority, // which takes precedence before option priority, which takes precedence before ordering. // Inside the same mod, conflicts are not recorded. - private void AddManipulation(MetaManipulation manip, IMod mod) + private void AddManipulation(IMod mod, IMetaIdentifier identifier, object entry) { - if (!Meta.TryGetValue(manip, out var existingMod)) + if (!Meta.TryGetMod(identifier, out var existingMod)) { - Meta.ApplyMod(manip, mod); - ModData.AddManip(mod, manip); + Meta.ApplyMod(mod, identifier, entry); + ModData.AddManip(mod, identifier); return; } @@ -387,11 +399,11 @@ public sealed class CollectionCache : IDisposable if (mod == existingMod) return; - if (AddConflict(manip, mod, existingMod)) + if (AddConflict(identifier, mod, existingMod)) { - ModData.RemoveManip(existingMod, manip); - Meta.ApplyMod(manip, mod); - ModData.AddManip(mod, manip); + ModData.RemoveManip(existingMod, identifier); + Meta.ApplyMod(mod, identifier, entry); + ModData.AddManip(mod, identifier); } } @@ -437,9 +449,9 @@ public sealed class CollectionCache : IDisposable AddItems(modPath.Mod); } - foreach (var (manip, mod) in Meta) + foreach (var (manip, mod) in Meta.IdentifierSources) { - identifier.MetaChangedItems(items, manip); + manip.AddChangedItems(identifier, items); AddItems(mod); } diff --git a/Penumbra/Collections/Cache/CollectionModData.cs b/Penumbra/Collections/Cache/CollectionModData.cs index d0a3bc76..295191d2 100644 --- a/Penumbra/Collections/Cache/CollectionModData.cs +++ b/Penumbra/Collections/Cache/CollectionModData.cs @@ -9,12 +9,12 @@ namespace Penumbra.Collections.Cache; /// public class CollectionModData { - private readonly Dictionary, HashSet)> _data = new(); + private readonly Dictionary, HashSet)> _data = new(); - public IEnumerable<(IMod, IReadOnlySet, IReadOnlySet)> Data - => _data.Select(kvp => (kvp.Key, (IReadOnlySet)kvp.Value.Item1, (IReadOnlySet)kvp.Value.Item2)); + public IEnumerable<(IMod, IReadOnlySet, IReadOnlySet)> Data + => _data.Select(kvp => (kvp.Key, (IReadOnlySet)kvp.Value.Item1, (IReadOnlySet)kvp.Value.Item2)); - public (IReadOnlyCollection Paths, IReadOnlyCollection Manipulations) RemoveMod(IMod mod) + public (IReadOnlyCollection Paths, IReadOnlyCollection Manipulations) RemoveMod(IMod mod) { if (_data.Remove(mod, out var data)) return data; @@ -35,7 +35,7 @@ public class CollectionModData } } - public void AddManip(IMod mod, MetaManipulation manipulation) + public void AddManip(IMod mod, IMetaIdentifier manipulation) { if (_data.TryGetValue(mod, out var data)) { @@ -54,7 +54,7 @@ public class CollectionModData _data.Remove(mod); } - public void RemoveManip(IMod mod, MetaManipulation manip) + public void RemoveManip(IMod mod, IMetaIdentifier manip) { if (_data.TryGetValue(mod, out var data) && data.Item2.Remove(manip) && data.Item1.Count == 0 && data.Item2.Count == 0) _data.Remove(mod); diff --git a/Penumbra/Collections/Cache/EqdpCache.cs b/Penumbra/Collections/Cache/EqdpCache.cs index a0f27c23..f3475c7e 100644 --- a/Penumbra/Collections/Cache/EqdpCache.cs +++ b/Penumbra/Collections/Cache/EqdpCache.cs @@ -1,5 +1,5 @@ +using FFXIVClientStructs.FFXIV.Client.Graphics.Render; using OtterGui; -using OtterGui.Filesystem; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.Services; @@ -10,28 +10,38 @@ using Penumbra.Meta.Manipulations; namespace Penumbra.Collections.Cache; -public readonly struct EqdpCache : IDisposable +public sealed class EqdpCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - private readonly ExpandedEqdpFile?[] _eqdpFiles = new ExpandedEqdpFile[CharacterUtilityData.EqdpIndices.Length]; // TODO: female Hrothgar - private readonly List _eqdpManipulations = new(); + private readonly ExpandedEqdpFile?[] _eqdpFiles = new ExpandedEqdpFile[CharacterUtilityData.EqdpIndices.Length]; // TODO: female Hrothgar - public EqdpCache() - { } - - public void SetFiles(MetaFileManager manager) + public override void SetFiles() { for (var i = 0; i < CharacterUtilityData.EqdpIndices.Length; ++i) - manager.SetFile(_eqdpFiles[i], CharacterUtilityData.EqdpIndices[i]); + Manager.SetFile(_eqdpFiles[i], CharacterUtilityData.EqdpIndices[i]); } - public void SetFile(MetaFileManager manager, MetaIndex index) + public void SetFile(MetaIndex index) { var i = CharacterUtilityData.EqdpIndices.IndexOf(index); if (i != -1) - manager.SetFile(_eqdpFiles[i], index); + Manager.SetFile(_eqdpFiles[i], index); } - public MetaList.MetaReverter? TemporarilySetFiles(MetaFileManager manager, GenderRace genderRace, bool accessory) + public override void ResetFiles() + => Manager.SetFile(null, MetaIndex.Eqp); + + protected override void IncorporateChangesInternal() + { + foreach (var (identifier, (_, entry)) in this) + Apply(GetFile(identifier)!, identifier, entry); + + Penumbra.Log.Verbose($"{Collection.AnonymizedName}: Loaded {Count} delayed EQDP manipulations."); + } + + public ExpandedEqdpFile? EqdpFile(GenderRace race, bool accessory) + => _eqdpFiles[Array.IndexOf(CharacterUtilityData.EqdpIndices, CharacterUtilityData.EqdpIdx(race, accessory))]; // TODO: female Hrothgar + + public MetaList.MetaReverter? TemporarilySetFile(GenderRace genderRace, bool accessory) { var idx = CharacterUtilityData.EqdpIdx(genderRace, accessory); if (idx < 0) @@ -47,44 +57,44 @@ public readonly struct EqdpCache : IDisposable return null; } - return manager.TemporarilySetFile(_eqdpFiles[i], idx); + return Manager.TemporarilySetFile(_eqdpFiles[i], idx); } - public void Reset() + public override void Reset() { foreach (var file in _eqdpFiles.OfType()) { var relevant = CharacterUtility.RelevantIndices[file.Index.Value]; - file.Reset(_eqdpManipulations.Where(m => m.FileIndex() == relevant).Select(m => (PrimaryId)m.SetId)); + file.Reset(Keys.Where(m => m.FileIndex() == relevant).Select(m => m.SetId)); } - _eqdpManipulations.Clear(); + Clear(); } - public bool ApplyMod(MetaFileManager manager, EqdpManipulation manip) + protected override void ApplyModInternal(EqdpIdentifier identifier, EqdpEntry entry) { - _eqdpManipulations.AddOrReplace(manip); - var file = _eqdpFiles[Array.IndexOf(CharacterUtilityData.EqdpIndices, manip.FileIndex())] ??= - new ExpandedEqdpFile(manager, Names.CombinedRace(manip.Gender, manip.Race), manip.Slot.IsAccessory()); // TODO: female Hrothgar - return manip.Apply(file); + if (GetFile(identifier) is { } file) + Apply(file, identifier, entry); } - public bool RevertMod(MetaFileManager manager, EqdpManipulation manip) + protected override void RevertModInternal(EqdpIdentifier identifier) { - if (!_eqdpManipulations.Remove(manip)) + if (GetFile(identifier) is { } file) + Apply(file, identifier, ExpandedEqdpFile.GetDefault(Manager, identifier)); + } + + public static bool Apply(ExpandedEqdpFile file, EqdpIdentifier identifier, EqdpEntry entry) + { + var origEntry = file[identifier.SetId]; + var mask = Eqdp.Mask(identifier.Slot); + if ((origEntry & mask) == entry) return false; - var def = ExpandedEqdpFile.GetDefault(manager, Names.CombinedRace(manip.Gender, manip.Race), manip.Slot.IsAccessory(), manip.SetId); - var file = _eqdpFiles[Array.IndexOf(CharacterUtilityData.EqdpIndices, manip.FileIndex())]!; - manip = new EqdpManipulation(def, manip.Slot, manip.Gender, manip.Race, manip.SetId); - return manip.Apply(file); + file[identifier.SetId] = (entry & ~mask) | origEntry; + return true; } - public ExpandedEqdpFile? EqdpFile(GenderRace race, bool accessory) - => _eqdpFiles - [Array.IndexOf(CharacterUtilityData.EqdpIndices, CharacterUtilityData.EqdpIdx(race, accessory))]; // TODO: female Hrothgar - - public void Dispose() + protected override void Dispose(bool _) { for (var i = 0; i < _eqdpFiles.Length; ++i) { @@ -92,6 +102,15 @@ public readonly struct EqdpCache : IDisposable _eqdpFiles[i] = null; } - _eqdpManipulations.Clear(); + Clear(); + } + + private ExpandedEqdpFile? GetFile(EqdpIdentifier identifier) + { + if (!Manager.CharacterUtility.Ready) + return null; + + var index = Array.IndexOf(CharacterUtilityData.EqdpIndices, identifier.FileIndex()); + return _eqdpFiles[index] ??= new ExpandedEqdpFile(Manager, identifier.GenderRace, identifier.Slot.IsAccessory()); } } diff --git a/Penumbra/Collections/Cache/EqpCache.cs b/Penumbra/Collections/Cache/EqpCache.cs index 972ee5a5..599ae588 100644 --- a/Penumbra/Collections/Cache/EqpCache.cs +++ b/Penumbra/Collections/Cache/EqpCache.cs @@ -1,4 +1,4 @@ -using OtterGui.Filesystem; +using Penumbra.GameData.Structs; using Penumbra.Interop.Services; using Penumbra.Interop.Structs; using Penumbra.Meta; @@ -7,54 +7,77 @@ using Penumbra.Meta.Manipulations; namespace Penumbra.Collections.Cache; -public struct EqpCache : IDisposable +public sealed class EqpCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - private ExpandedEqpFile? _eqpFile = null; - private readonly List _eqpManipulations = new(); + private ExpandedEqpFile? _eqpFile; - public EqpCache() - { } + public override void SetFiles() + => Manager.SetFile(_eqpFile, MetaIndex.Eqp); - public void SetFiles(MetaFileManager manager) - => manager.SetFile(_eqpFile, MetaIndex.Eqp); + public override void ResetFiles() + => Manager.SetFile(null, MetaIndex.Eqp); - public static void ResetFiles(MetaFileManager manager) - => manager.SetFile(null, MetaIndex.Eqp); + protected override void IncorporateChangesInternal() + { + if (GetFile() is not { } file) + return; - public MetaList.MetaReverter TemporarilySetFiles(MetaFileManager manager) - => manager.TemporarilySetFile(_eqpFile, MetaIndex.Eqp); + foreach (var (identifier, (_, entry)) in this) + Apply(file, identifier, entry); - public void Reset() + Penumbra.Log.Verbose($"{Collection.AnonymizedName}: Loaded {Count} delayed EQP manipulations."); + } + + public MetaList.MetaReverter TemporarilySetFile() + => Manager.TemporarilySetFile(_eqpFile, MetaIndex.Eqp); + + public override void Reset() { if (_eqpFile == null) return; - _eqpFile.Reset(_eqpManipulations.Select(m => m.SetId)); - _eqpManipulations.Clear(); + _eqpFile.Reset(Keys.Select(identifier => identifier.SetId)); + Clear(); } - public bool ApplyMod(MetaFileManager manager, EqpManipulation manip) + protected override void ApplyModInternal(EqpIdentifier identifier, EqpEntry entry) { - _eqpManipulations.AddOrReplace(manip); - _eqpFile ??= new ExpandedEqpFile(manager); - return manip.Apply(_eqpFile); + if (GetFile() is { } file) + Apply(file, identifier, entry); } - public bool RevertMod(MetaFileManager manager, EqpManipulation manip) + protected override void RevertModInternal(EqpIdentifier identifier) { - var idx = _eqpManipulations.FindIndex(manip.Equals); - if (idx < 0) + if (GetFile() is { } file) + Apply(file, identifier, ExpandedEqpFile.GetDefault(Manager, identifier.SetId)); + } + + public static bool Apply(ExpandedEqpFile file, EqpIdentifier identifier, EqpEntry entry) + { + var origEntry = file[identifier.SetId]; + var mask = Eqp.Mask(identifier.Slot); + if ((origEntry & mask) == entry) return false; - var def = ExpandedEqpFile.GetDefault(manager, manip.SetId); - manip = new EqpManipulation(def, manip.Slot, manip.SetId); - return manip.Apply(_eqpFile!); + file[identifier.SetId] = (origEntry & ~mask) | entry; + return true; } - public void Dispose() + protected override void Dispose(bool _) { _eqpFile?.Dispose(); _eqpFile = null; - _eqpManipulations.Clear(); + Clear(); + } + + private ExpandedEqpFile? GetFile() + { + if (_eqpFile != null) + return _eqpFile; + + if (!Manager.CharacterUtility.Ready) + return null; + + return _eqpFile = new ExpandedEqpFile(Manager); } } diff --git a/Penumbra/Collections/Cache/EstCache.cs b/Penumbra/Collections/Cache/EstCache.cs index 3a0b4695..412dd322 100644 --- a/Penumbra/Collections/Cache/EstCache.cs +++ b/Penumbra/Collections/Cache/EstCache.cs @@ -1,6 +1,3 @@ -using OtterGui.Filesystem; -using Penumbra.GameData.Enums; -using Penumbra.GameData.Structs; using Penumbra.Interop.Services; using Penumbra.Interop.Structs; using Penumbra.Meta; @@ -9,46 +6,41 @@ using Penumbra.Meta.Manipulations; namespace Penumbra.Collections.Cache; -public struct EstCache : IDisposable +public sealed class EstCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - private EstFile? _estFaceFile = null; - private EstFile? _estHairFile = null; - private EstFile? _estBodyFile = null; - private EstFile? _estHeadFile = null; + private EstFile? _estFaceFile; + private EstFile? _estHairFile; + private EstFile? _estBodyFile; + private EstFile? _estHeadFile; - private readonly List _estManipulations = new(); - - public EstCache() - { } - - public void SetFiles(MetaFileManager manager) + public override void SetFiles() { - manager.SetFile(_estFaceFile, MetaIndex.FaceEst); - manager.SetFile(_estHairFile, MetaIndex.HairEst); - manager.SetFile(_estBodyFile, MetaIndex.BodyEst); - manager.SetFile(_estHeadFile, MetaIndex.HeadEst); + Manager.SetFile(_estFaceFile, MetaIndex.FaceEst); + Manager.SetFile(_estHairFile, MetaIndex.HairEst); + Manager.SetFile(_estBodyFile, MetaIndex.BodyEst); + Manager.SetFile(_estHeadFile, MetaIndex.HeadEst); } - public void SetFile(MetaFileManager manager, MetaIndex index) + public void SetFile(MetaIndex index) { switch (index) { case MetaIndex.FaceEst: - manager.SetFile(_estFaceFile, MetaIndex.FaceEst); + Manager.SetFile(_estFaceFile, MetaIndex.FaceEst); break; case MetaIndex.HairEst: - manager.SetFile(_estHairFile, MetaIndex.HairEst); + Manager.SetFile(_estHairFile, MetaIndex.HairEst); break; case MetaIndex.BodyEst: - manager.SetFile(_estBodyFile, MetaIndex.BodyEst); + Manager.SetFile(_estBodyFile, MetaIndex.BodyEst); break; case MetaIndex.HeadEst: - manager.SetFile(_estHeadFile, MetaIndex.HeadEst); + Manager.SetFile(_estHeadFile, MetaIndex.HeadEst); break; } } - public MetaList.MetaReverter TemporarilySetFiles(MetaFileManager manager, EstType type) + public MetaList.MetaReverter TemporarilySetFiles(EstType type) { var (file, idx) = type switch { @@ -56,74 +48,65 @@ public struct EstCache : IDisposable EstType.Hair => (_estHairFile, MetaIndex.HairEst), EstType.Body => (_estBodyFile, MetaIndex.BodyEst), EstType.Head => (_estHeadFile, MetaIndex.HeadEst), - _ => (null, 0), + _ => (null, 0), }; - return manager.TemporarilySetFile(file, idx); + return Manager.TemporarilySetFile(file, idx); } - private readonly EstFile? GetEstFile(EstType type) + public override void ResetFiles() + => Manager.SetFile(null, MetaIndex.Eqp); + + protected override void IncorporateChangesInternal() { - return type switch - { - EstType.Face => _estFaceFile, - EstType.Hair => _estHairFile, - EstType.Body => _estBodyFile, - EstType.Head => _estHeadFile, - _ => null, - }; + if (!Manager.CharacterUtility.Ready) + return; + + foreach (var (identifier, (_, entry)) in this) + Apply(GetFile(identifier)!, identifier, entry); + Penumbra.Log.Verbose($"{Collection.AnonymizedName}: Loaded {Count} delayed EST manipulations."); } - internal EstEntry GetEstEntry(MetaFileManager manager, EstType type, GenderRace genderRace, PrimaryId primaryId) + public EstEntry GetEstEntry(EstIdentifier identifier) { - var file = GetEstFile(type); + var file = GetFile(identifier); return file != null - ? file[genderRace, primaryId.Id] - : EstFile.GetDefault(manager, type, genderRace, primaryId); + ? file[identifier.GenderRace, identifier.SetId] + : EstFile.GetDefault(Manager, identifier); } - public void Reset() + public override void Reset() { _estFaceFile?.Reset(); _estHairFile?.Reset(); _estBodyFile?.Reset(); _estHeadFile?.Reset(); - _estManipulations.Clear(); + Clear(); } - public bool ApplyMod(MetaFileManager manager, EstManipulation m) + protected override void ApplyModInternal(EstIdentifier identifier, EstEntry entry) { - _estManipulations.AddOrReplace(m); - var file = m.Slot switch - { - EstType.Hair => _estHairFile ??= new EstFile(manager, EstType.Hair), - EstType.Face => _estFaceFile ??= new EstFile(manager, EstType.Face), - EstType.Body => _estBodyFile ??= new EstFile(manager, EstType.Body), - EstType.Head => _estHeadFile ??= new EstFile(manager, EstType.Head), - _ => throw new ArgumentOutOfRangeException(), - }; - return m.Apply(file); + if (GetFile(identifier) is { } file) + Apply(file, identifier, entry); } - public bool RevertMod(MetaFileManager manager, EstManipulation m) + protected override void RevertModInternal(EstIdentifier identifier) { - if (!_estManipulations.Remove(m)) - return false; - - var def = EstFile.GetDefault(manager, m.Slot, Names.CombinedRace(m.Gender, m.Race), m.SetId); - var manip = new EstManipulation(m.Gender, m.Race, m.Slot, m.SetId, def); - var file = m.Slot switch - { - EstType.Hair => _estHairFile!, - EstType.Face => _estFaceFile!, - EstType.Body => _estBodyFile!, - EstType.Head => _estHeadFile!, - _ => throw new ArgumentOutOfRangeException(), - }; - return manip.Apply(file); + if (GetFile(identifier) is { } file) + Apply(file, identifier, EstFile.GetDefault(Manager, identifier.Slot, identifier.GenderRace, identifier.SetId)); } - public void Dispose() + public static bool Apply(EstFile file, EstIdentifier identifier, EstEntry entry) + => file.SetEntry(identifier.GenderRace, identifier.SetId, entry) switch + { + EstFile.EstEntryChange.Unchanged => false, + EstFile.EstEntryChange.Changed => true, + EstFile.EstEntryChange.Added => true, + EstFile.EstEntryChange.Removed => true, + _ => false, + }; + + protected override void Dispose(bool _) { _estFaceFile?.Dispose(); _estHairFile?.Dispose(); @@ -133,6 +116,21 @@ public struct EstCache : IDisposable _estHairFile = null; _estBodyFile = null; _estHeadFile = null; - _estManipulations.Clear(); + Clear(); + } + + private EstFile? GetFile(EstIdentifier identifier) + { + if (Manager.CharacterUtility.Ready) + return null; + + return identifier.Slot switch + { + EstType.Hair => _estHairFile ??= new EstFile(Manager, EstType.Hair), + EstType.Face => _estFaceFile ??= new EstFile(Manager, EstType.Face), + EstType.Body => _estBodyFile ??= new EstFile(Manager, EstType.Body), + EstType.Head => _estHeadFile ??= new EstFile(Manager, EstType.Head), + _ => null, + }; } } diff --git a/Penumbra/Meta/Manipulations/GlobalEqpCache.cs b/Penumbra/Collections/Cache/GlobalEqpCache.cs similarity index 75% rename from Penumbra/Meta/Manipulations/GlobalEqpCache.cs rename to Penumbra/Collections/Cache/GlobalEqpCache.cs index 26eb1d05..1c80b47d 100644 --- a/Penumbra/Meta/Manipulations/GlobalEqpCache.cs +++ b/Penumbra/Collections/Cache/GlobalEqpCache.cs @@ -1,9 +1,11 @@ using OtterGui.Services; using Penumbra.GameData.Structs; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; -namespace Penumbra.Meta.Manipulations; +namespace Penumbra.Collections.Cache; -public struct GlobalEqpCache : IService +public class GlobalEqpCache : Dictionary, IService { private readonly HashSet _doNotHideEarrings = []; private readonly HashSet _doNotHideNecklace = []; @@ -13,11 +15,9 @@ public struct GlobalEqpCache : IService private bool _doNotHideVieraHats; private bool _doNotHideHrothgarHats; - public GlobalEqpCache() - { } - - public void Clear() + public new void Clear() { + base.Clear(); _doNotHideEarrings.Clear(); _doNotHideNecklace.Clear(); _doNotHideBracelets.Clear(); @@ -29,6 +29,9 @@ public struct GlobalEqpCache : IService public unsafe EqpEntry Apply(EqpEntry original, CharacterArmor* armor) { + if (Count == 0) + return original; + if (_doNotHideVieraHats) original |= EqpEntry.HeadShowVieraHat; @@ -52,8 +55,13 @@ public struct GlobalEqpCache : IService return original; } - public bool Add(GlobalEqpManipulation manipulation) - => manipulation.Type switch + public bool ApplyMod(IMod mod, GlobalEqpManipulation manipulation) + { + if (Remove(manipulation, out var oldMod) && oldMod == mod) + return false; + + this[manipulation] = mod; + _ = manipulation.Type switch { GlobalEqpType.DoNotHideEarrings => _doNotHideEarrings.Add(manipulation.Condition), GlobalEqpType.DoNotHideNecklace => _doNotHideNecklace.Add(manipulation.Condition), @@ -61,12 +69,18 @@ public struct GlobalEqpCache : IService GlobalEqpType.DoNotHideRingR => _doNotHideRingR.Add(manipulation.Condition), GlobalEqpType.DoNotHideRingL => _doNotHideRingL.Add(manipulation.Condition), GlobalEqpType.DoNotHideHrothgarHats => !_doNotHideHrothgarHats && (_doNotHideHrothgarHats = true), - GlobalEqpType.DoNotHideVieraHats => !_doNotHideVieraHats && (_doNotHideVieraHats = true), - _ => false, + GlobalEqpType.DoNotHideVieraHats => !_doNotHideVieraHats && (_doNotHideVieraHats = true), + _ => false, }; + return true; + } - public bool Remove(GlobalEqpManipulation manipulation) - => manipulation.Type switch + public bool RevertMod(GlobalEqpManipulation manipulation, [NotNullWhen(true)] out IMod? mod) + { + if (!Remove(manipulation, out mod)) + return false; + + _ = manipulation.Type switch { GlobalEqpType.DoNotHideEarrings => _doNotHideEarrings.Remove(manipulation.Condition), GlobalEqpType.DoNotHideNecklace => _doNotHideNecklace.Remove(manipulation.Condition), @@ -74,7 +88,9 @@ public struct GlobalEqpCache : IService GlobalEqpType.DoNotHideRingR => _doNotHideRingR.Remove(manipulation.Condition), GlobalEqpType.DoNotHideRingL => _doNotHideRingL.Remove(manipulation.Condition), GlobalEqpType.DoNotHideHrothgarHats => _doNotHideHrothgarHats && !(_doNotHideHrothgarHats = false), - GlobalEqpType.DoNotHideVieraHats => _doNotHideVieraHats && !(_doNotHideVieraHats = false), - _ => false, + GlobalEqpType.DoNotHideVieraHats => _doNotHideVieraHats && !(_doNotHideVieraHats = false), + _ => false, }; + return true; + } } diff --git a/Penumbra/Collections/Cache/GmpCache.cs b/Penumbra/Collections/Cache/GmpCache.cs index 0a713867..1475ffd5 100644 --- a/Penumbra/Collections/Cache/GmpCache.cs +++ b/Penumbra/Collections/Cache/GmpCache.cs @@ -1,4 +1,4 @@ -using OtterGui.Filesystem; +using Penumbra.GameData.Structs; using Penumbra.Interop.Services; using Penumbra.Interop.Structs; using Penumbra.Meta; @@ -7,50 +7,76 @@ using Penumbra.Meta.Manipulations; namespace Penumbra.Collections.Cache; -public struct GmpCache : IDisposable +public sealed class GmpCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - private ExpandedGmpFile? _gmpFile = null; - private readonly List _gmpManipulations = new(); + private ExpandedGmpFile? _gmpFile; - public GmpCache() - { } + public override void SetFiles() + => Manager.SetFile(_gmpFile, MetaIndex.Gmp); - public void SetFiles(MetaFileManager manager) - => manager.SetFile(_gmpFile, MetaIndex.Gmp); + public override void ResetFiles() + => Manager.SetFile(null, MetaIndex.Gmp); - public MetaList.MetaReverter TemporarilySetFiles(MetaFileManager manager) - => manager.TemporarilySetFile(_gmpFile, MetaIndex.Gmp); + protected override void IncorporateChangesInternal() + { + if (GetFile() is not { } file) + return; - public void Reset() + foreach (var (identifier, (_, entry)) in this) + Apply(file, identifier, entry); + + Penumbra.Log.Verbose($"{Collection.AnonymizedName}: Loaded {Count} delayed GMP manipulations."); + } + + public MetaList.MetaReverter TemporarilySetFile() + => Manager.TemporarilySetFile(_gmpFile, MetaIndex.Gmp); + + public override void Reset() { if (_gmpFile == null) return; - _gmpFile.Reset(_gmpManipulations.Select(m => m.SetId)); - _gmpManipulations.Clear(); + _gmpFile.Reset(Keys.Select(identifier => identifier.SetId)); + Clear(); } - public bool ApplyMod(MetaFileManager manager, GmpManipulation manip) + protected override void ApplyModInternal(GmpIdentifier identifier, GmpEntry entry) { - _gmpManipulations.AddOrReplace(manip); - _gmpFile ??= new ExpandedGmpFile(manager); - return manip.Apply(_gmpFile); + if (GetFile() is { } file) + Apply(file, identifier, entry); } - public bool RevertMod(MetaFileManager manager, GmpManipulation manip) + protected override void RevertModInternal(GmpIdentifier identifier) { - if (!_gmpManipulations.Remove(manip)) + if (GetFile() is { } file) + Apply(file, identifier, ExpandedGmpFile.GetDefault(Manager, identifier.SetId)); + } + + public static bool Apply(ExpandedGmpFile file, GmpIdentifier identifier, GmpEntry entry) + { + var origEntry = file[identifier.SetId]; + if (entry == origEntry) return false; - var def = ExpandedGmpFile.GetDefault(manager, manip.SetId); - manip = new GmpManipulation(def, manip.SetId); - return manip.Apply(_gmpFile!); + file[identifier.SetId] = entry; + return true; } - public void Dispose() + protected override void Dispose(bool _) { _gmpFile?.Dispose(); _gmpFile = null; - _gmpManipulations.Clear(); + Clear(); + } + + private ExpandedGmpFile? GetFile() + { + if (_gmpFile != null) + return _gmpFile; + + if (!Manager.CharacterUtility.Ready) + return null; + + return _gmpFile = new ExpandedGmpFile(Manager); } } diff --git a/Penumbra/Collections/Cache/IMetaCache.cs b/Penumbra/Collections/Cache/IMetaCache.cs new file mode 100644 index 00000000..218c1840 --- /dev/null +++ b/Penumbra/Collections/Cache/IMetaCache.cs @@ -0,0 +1,89 @@ +using Penumbra.Meta; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; + +namespace Penumbra.Collections.Cache; + +public interface IMetaCache : IDisposable +{ + public void SetFiles(); + public void Reset(); + public void ResetFiles(); + + public int Count { get; } +} + +public abstract class MetaCacheBase + : Dictionary, IMetaCache + where TIdentifier : unmanaged, IMetaIdentifier + where TEntry : unmanaged +{ + public MetaCacheBase(MetaFileManager manager, ModCollection collection) + { + Manager = manager; + Collection = collection; + if (!Manager.CharacterUtility.Ready) + Manager.CharacterUtility.LoadingFinished += IncorporateChanges; + } + + protected readonly MetaFileManager Manager; + protected readonly ModCollection Collection; + + public void Dispose() + { + Manager.CharacterUtility.LoadingFinished -= IncorporateChanges; + Dispose(true); + } + + public abstract void SetFiles(); + public abstract void Reset(); + public abstract void ResetFiles(); + + public bool ApplyMod(IMod source, TIdentifier identifier, TEntry entry) + { + lock (this) + { + if (TryGetValue(identifier, out var pair) && pair.Source == source && EqualityComparer.Default.Equals(pair.Entry, entry)) + return false; + + this[identifier] = (source, entry); + } + + ApplyModInternal(identifier, entry); + return true; + } + + public bool RevertMod(TIdentifier identifier, [NotNullWhen(true)] out IMod? mod) + { + lock (this) + { + if (!Remove(identifier, out var pair)) + { + mod = null; + return false; + } + + mod = pair.Source; + } + + RevertModInternal(identifier); + return true; + } + + private void IncorporateChanges() + { + lock (this) + { + IncorporateChangesInternal(); + } + if (Manager.ActiveCollections.Default == Collection && Manager.Config.EnableMods) + SetFiles(); + } + + protected abstract void ApplyModInternal(TIdentifier identifier, TEntry entry); + protected abstract void RevertModInternal(TIdentifier identifier); + protected abstract void IncorporateChangesInternal(); + + protected virtual void Dispose(bool _) + { } +} diff --git a/Penumbra/Collections/Cache/ImcCache.cs b/Penumbra/Collections/Cache/ImcCache.cs index 7990122a..3d05e793 100644 --- a/Penumbra/Collections/Cache/ImcCache.cs +++ b/Penumbra/Collections/Cache/ImcCache.cs @@ -1,3 +1,6 @@ +using FFXIVClientStructs.FFXIV.Client.Graphics.Render; +using Penumbra.Collections.Manager; +using Penumbra.GameData.Structs; using Penumbra.Interop.PathResolving; using Penumbra.Meta; using Penumbra.Meta.Files; @@ -6,116 +9,132 @@ using Penumbra.String.Classes; namespace Penumbra.Collections.Cache; -public readonly struct ImcCache : IDisposable +public sealed class ImcCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - private readonly Dictionary _imcFiles = []; - private readonly List<(ImcManipulation, ImcFile)> _imcManipulations = []; + private readonly Dictionary)> _imcFiles = []; - public ImcCache() - { } + public override void SetFiles() + => SetFiles(false); - public void SetFiles(ModCollection collection, bool fromFullCompute) + public bool GetFile(Utf8GamePath path, [NotNullWhen(true)] out ImcFile? file) + { + if (!_imcFiles.TryGetValue(path, out var p)) + { + file = null; + return false; + } + + file = p.Item1; + return true; + } + + public void SetFiles(bool fromFullCompute) { if (fromFullCompute) - foreach (var path in _imcFiles.Keys) - collection._cache!.ForceFileSync(path, PathDataHandler.CreateImc(path.Path, collection)); + foreach (var (path, _) in _imcFiles) + Collection._cache!.ForceFileSync(path, PathDataHandler.CreateImc(path.Path, Collection)); else - foreach (var path in _imcFiles.Keys) - collection._cache!.ForceFile(path, PathDataHandler.CreateImc(path.Path, collection)); + foreach (var (path, _) in _imcFiles) + Collection._cache!.ForceFile(path, PathDataHandler.CreateImc(path.Path, Collection)); } - public void Reset(ModCollection collection) + public override void ResetFiles() { - foreach (var (path, file) in _imcFiles) + foreach (var (path, _) in _imcFiles) + Collection._cache!.ForceFile(path, FullPath.Empty); + } + + protected override void IncorporateChangesInternal() + { + if (!Manager.CharacterUtility.Ready) + return; + + foreach (var (identifier, (_, entry)) in this) + ApplyFile(identifier, entry); + + Penumbra.Log.Verbose($"{Collection.AnonymizedName}: Loaded {Count} delayed IMC manipulations."); + } + + + public override void Reset() + { + foreach (var (path, (file, set)) in _imcFiles) { - collection._cache!.RemovePath(path); + Collection._cache!.RemovePath(path); file.Reset(); + set.Clear(); } - _imcManipulations.Clear(); + Clear(); } - public bool ApplyMod(MetaFileManager manager, ModCollection collection, ImcManipulation manip) + protected override void ApplyModInternal(ImcIdentifier identifier, ImcEntry entry) { - if (!manip.Validate(true)) - return false; + if (Manager.CharacterUtility.Ready) + ApplyFile(identifier, entry); + } - var idx = _imcManipulations.FindIndex(p => p.Item1.Equals(manip)); - if (idx < 0) - { - idx = _imcManipulations.Count; - _imcManipulations.Add((manip, null!)); - } - - var path = manip.GamePath(); + private void ApplyFile(ImcIdentifier identifier, ImcEntry entry) + { + var path = identifier.GamePath(); try { - if (!_imcFiles.TryGetValue(path, out var file)) - file = new ImcFile(manager, manip.Identifier); + if (!_imcFiles.TryGetValue(path, out var pair)) + pair = (new ImcFile(Manager, identifier), []); - _imcManipulations[idx] = (manip, file); - if (!manip.Apply(file)) - return false; - _imcFiles[path] = file; - var fullPath = PathDataHandler.CreateImc(file.Path.Path, collection); - collection._cache!.ForceFile(path, fullPath); + if (!Apply(pair.Item1, identifier, entry)) + return; - return true; + pair.Item2.Add(identifier); + _imcFiles[path] = pair; + var fullPath = PathDataHandler.CreateImc(pair.Item1.Path.Path, Collection); + Collection._cache!.ForceFile(path, fullPath); } catch (ImcException e) { - manager.ValidityChecker.ImcExceptions.Add(e); + Manager.ValidityChecker.ImcExceptions.Add(e); Penumbra.Log.Error(e.ToString()); } catch (Exception e) { - Penumbra.Log.Error($"Could not apply IMC Manipulation {manip}:\n{e}"); + Penumbra.Log.Error($"Could not apply IMC Manipulation {identifier}:\n{e}"); } - - return false; } - public bool RevertMod(MetaFileManager manager, ModCollection collection, ImcManipulation m) + protected override void RevertModInternal(ImcIdentifier identifier) { - if (!m.Validate(false)) - return false; + var path = identifier.GamePath(); + if (!_imcFiles.TryGetValue(path, out var pair)) + return; - var idx = _imcManipulations.FindIndex(p => p.Item1.Equals(m)); - if (idx < 0) - return false; + if (!pair.Item2.Remove(identifier)) + return; - var (_, file) = _imcManipulations[idx]; - _imcManipulations.RemoveAt(idx); - - if (_imcManipulations.All(p => !ReferenceEquals(p.Item2, file))) + if (pair.Item2.Count == 0) { - _imcFiles.Remove(file.Path); - collection._cache!.ForceFile(file.Path, FullPath.Empty); - file.Dispose(); - return true; + _imcFiles.Remove(path); + Collection._cache!.ForceFile(pair.Item1.Path, FullPath.Empty); + pair.Item1.Dispose(); + return; } - var def = ImcFile.GetDefault(manager, file.Path, m.EquipSlot, m.Variant.Id, out _); - var manip = m.Copy(def); - if (!manip.Apply(file)) - return false; + var def = ImcFile.GetDefault(Manager, pair.Item1.Path, identifier.EquipSlot, identifier.Variant, out _); + if (!Apply(pair.Item1, identifier, def)) + return; - var fullPath = PathDataHandler.CreateImc(file.Path.Path, collection); - collection._cache!.ForceFile(file.Path, fullPath); - - return true; + var fullPath = PathDataHandler.CreateImc(pair.Item1.Path.Path, Collection); + Collection._cache!.ForceFile(pair.Item1.Path, fullPath); } - public void Dispose() + public static bool Apply(ImcFile file, ImcIdentifier identifier, ImcEntry entry) + => file.SetEntry(ImcFile.PartIndex(identifier.EquipSlot), identifier.Variant.Id, entry); + + protected override void Dispose(bool _) { - foreach (var file in _imcFiles.Values) + foreach (var (_, (file, _)) in _imcFiles) file.Dispose(); - + Clear(); _imcFiles.Clear(); - _imcManipulations.Clear(); } - - public bool GetImcFile(Utf8GamePath path, [NotNullWhen(true)] out ImcFile? file) - => _imcFiles.TryGetValue(path, out file); } diff --git a/Penumbra/Collections/Cache/MetaCache.cs b/Penumbra/Collections/Cache/MetaCache.cs index fbca9c0e..bc6ef34d 100644 --- a/Penumbra/Collections/Cache/MetaCache.cs +++ b/Penumbra/Collections/Cache/MetaCache.cs @@ -1,3 +1,4 @@ +using System.IO.Pipes; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.Services; @@ -9,238 +10,174 @@ using Penumbra.String.Classes; namespace Penumbra.Collections.Cache; -public class MetaCache : IDisposable, IEnumerable> +public class MetaCache(MetaFileManager manager, ModCollection collection) { - private readonly MetaFileManager _manager; - private readonly ModCollection _collection; - private readonly Dictionary _manipulations = new(); - private EqpCache _eqpCache = new(); - private readonly EqdpCache _eqdpCache = new(); - private EstCache _estCache = new(); - private GmpCache _gmpCache = new(); - private CmpCache _cmpCache = new(); - private readonly ImcCache _imcCache = new(); - private GlobalEqpCache _globalEqpCache = new(); - - public bool TryGetValue(MetaManipulation manip, [NotNullWhen(true)] out IMod? mod) - { - lock (_manipulations) - { - return _manipulations.TryGetValue(manip, out mod); - } - } + public readonly EqpCache Eqp = new(manager, collection); + public readonly EqdpCache Eqdp = new(manager, collection); + public readonly EstCache Est = new(manager, collection); + public readonly GmpCache Gmp = new(manager, collection); + public readonly RspCache Rsp = new(manager, collection); + public readonly ImcCache Imc = new(manager, collection); + public readonly GlobalEqpCache GlobalEqp = new(); public int Count - => _manipulations.Count; + => Eqp.Count + Eqdp.Count + Est.Count + Gmp.Count + Rsp.Count + Imc.Count + GlobalEqp.Count; - public IReadOnlyCollection Manipulations - => _manipulations.Keys; - - public IEnumerator> GetEnumerator() - => _manipulations.GetEnumerator(); - - IEnumerator IEnumerable.GetEnumerator() - => GetEnumerator(); - - public MetaCache(MetaFileManager manager, ModCollection collection) - { - _manager = manager; - _collection = collection; - if (!_manager.CharacterUtility.Ready) - _manager.CharacterUtility.LoadingFinished += ApplyStoredManipulations; - } + public IEnumerable<(IMetaIdentifier, IMod)> IdentifierSources + => Eqp.Select(kvp => ((IMetaIdentifier)kvp.Key, kvp.Value.Source)) + .Concat(Eqdp.Select(kvp => ((IMetaIdentifier)kvp.Key, kvp.Value.Source))) + .Concat(Est.Select(kvp => ((IMetaIdentifier)kvp.Key, kvp.Value.Source))) + .Concat(Gmp.Select(kvp => ((IMetaIdentifier)kvp.Key, kvp.Value.Source))) + .Concat(Rsp.Select(kvp => ((IMetaIdentifier)kvp.Key, kvp.Value.Source))) + .Concat(Imc.Select(kvp => ((IMetaIdentifier)kvp.Key, kvp.Value.Source))) + .Concat(GlobalEqp.Select(kvp => ((IMetaIdentifier)kvp.Key, kvp.Value))); public void SetFiles() { - _eqpCache.SetFiles(_manager); - _eqdpCache.SetFiles(_manager); - _estCache.SetFiles(_manager); - _gmpCache.SetFiles(_manager); - _cmpCache.SetFiles(_manager); - _imcCache.SetFiles(_collection, false); + Eqp.SetFiles(); + Eqdp.SetFiles(); + Est.SetFiles(); + Gmp.SetFiles(); + Rsp.SetFiles(); + Imc.SetFiles(false); } public void Reset() { - _eqpCache.Reset(); - _eqdpCache.Reset(); - _estCache.Reset(); - _gmpCache.Reset(); - _cmpCache.Reset(); - _imcCache.Reset(_collection); - _manipulations.Clear(); - _globalEqpCache.Clear(); + Eqp.Reset(); + Eqdp.Reset(); + Est.Reset(); + Gmp.Reset(); + Rsp.Reset(); + Imc.Reset(); + GlobalEqp.Clear(); } public void Dispose() { - _manager.CharacterUtility.LoadingFinished -= ApplyStoredManipulations; - _eqpCache.Dispose(); - _eqdpCache.Dispose(); - _estCache.Dispose(); - _gmpCache.Dispose(); - _cmpCache.Dispose(); - _imcCache.Dispose(); - _manipulations.Clear(); + Eqp.Dispose(); + Eqdp.Dispose(); + Est.Dispose(); + Gmp.Dispose(); + Rsp.Dispose(); + Imc.Dispose(); } + public bool TryGetMod(IMetaIdentifier identifier, [NotNullWhen(true)] out IMod? mod) + { + mod = null; + return identifier switch + { + EqdpIdentifier i => Eqdp.TryGetValue(i, out var p) && Convert(p, out mod), + EqpIdentifier i => Eqp.TryGetValue(i, out var p) && Convert(p, out mod), + EstIdentifier i => Est.TryGetValue(i, out var p) && Convert(p, out mod), + GmpIdentifier i => Gmp.TryGetValue(i, out var p) && Convert(p, out mod), + ImcIdentifier i => Imc.TryGetValue(i, out var p) && Convert(p, out mod), + RspIdentifier i => Rsp.TryGetValue(i, out var p) && Convert(p, out mod), + GlobalEqpManipulation i => GlobalEqp.TryGetValue(i, out mod), + _ => false, + }; + + static bool Convert((IMod, T) pair, out IMod mod) + { + mod = pair.Item1; + return true; + } + } + + public bool RevertMod(IMetaIdentifier identifier, [NotNullWhen(true)] out IMod? mod) + => identifier switch + { + EqdpIdentifier i => Eqdp.RevertMod(i, out mod), + EqpIdentifier i => Eqp.RevertMod(i, out mod), + EstIdentifier i => Est.RevertMod(i, out mod), + GmpIdentifier i => Gmp.RevertMod(i, out mod), + ImcIdentifier i => Imc.RevertMod(i, out mod), + RspIdentifier i => Rsp.RevertMod(i, out mod), + GlobalEqpManipulation i => GlobalEqp.RevertMod(i, out mod), + _ => (mod = null) != null, + }; + + public bool ApplyMod(IMod mod, IMetaIdentifier identifier, object entry) + => identifier switch + { + EqdpIdentifier i when entry is EqdpEntry e => Eqdp.ApplyMod(mod, i, e), + EqdpIdentifier i when entry is EqdpEntryInternal e => Eqdp.ApplyMod(mod, i, e.ToEntry(i.Slot)), + EqpIdentifier i when entry is EqpEntry e => Eqp.ApplyMod(mod, i, e), + EqpIdentifier i when entry is EqpEntryInternal e => Eqp.ApplyMod(mod, i, e.ToEntry(i.Slot)), + EstIdentifier i when entry is EstEntry e => Est.ApplyMod(mod, i, e), + GmpIdentifier i when entry is GmpEntry e => Gmp.ApplyMod(mod, i, e), + ImcIdentifier i when entry is ImcEntry e => Imc.ApplyMod(mod, i, e), + RspIdentifier i when entry is RspEntry e => Rsp.ApplyMod(mod, i, e), + GlobalEqpManipulation i => GlobalEqp.ApplyMod(mod, i), + _ => false, + }; + ~MetaCache() => Dispose(); - public bool ApplyMod(MetaManipulation manip, IMod mod) - { - lock (_manipulations) - { - if (_manipulations.ContainsKey(manip)) - _manipulations.Remove(manip); - - _manipulations[manip] = mod; - } - - if (manip.ManipulationType is MetaManipulation.Type.GlobalEqp) - return _globalEqpCache.Add(manip.GlobalEqp); - - if (!_manager.CharacterUtility.Ready) - return true; - - // Imc manipulations do not require character utility, - // but they do require the file space to be ready. - return manip.ManipulationType switch - { - MetaManipulation.Type.Eqp => _eqpCache.ApplyMod(_manager, manip.Eqp), - MetaManipulation.Type.Eqdp => _eqdpCache.ApplyMod(_manager, manip.Eqdp), - MetaManipulation.Type.Est => _estCache.ApplyMod(_manager, manip.Est), - MetaManipulation.Type.Gmp => _gmpCache.ApplyMod(_manager, manip.Gmp), - MetaManipulation.Type.Rsp => _cmpCache.ApplyMod(_manager, manip.Rsp), - MetaManipulation.Type.Imc => _imcCache.ApplyMod(_manager, _collection, manip.Imc), - MetaManipulation.Type.Unknown => false, - _ => false, - }; - } - - public bool RevertMod(MetaManipulation manip, [NotNullWhen(true)] out IMod? mod) - { - lock (_manipulations) - { - var ret = _manipulations.Remove(manip, out mod); - - if (manip.ManipulationType is MetaManipulation.Type.GlobalEqp) - return _globalEqpCache.Remove(manip.GlobalEqp); - - if (!_manager.CharacterUtility.Ready) - return ret; - } - - // Imc manipulations do not require character utility, - // but they do require the file space to be ready. - return manip.ManipulationType switch - { - MetaManipulation.Type.Eqp => _eqpCache.RevertMod(_manager, manip.Eqp), - MetaManipulation.Type.Eqdp => _eqdpCache.RevertMod(_manager, manip.Eqdp), - MetaManipulation.Type.Est => _estCache.RevertMod(_manager, manip.Est), - MetaManipulation.Type.Gmp => _gmpCache.RevertMod(_manager, manip.Gmp), - MetaManipulation.Type.Rsp => _cmpCache.RevertMod(_manager, manip.Rsp), - MetaManipulation.Type.Imc => _imcCache.RevertMod(_manager, _collection, manip.Imc), - MetaManipulation.Type.Unknown => false, - _ => false, - }; - } - /// Set a single file. public void SetFile(MetaIndex metaIndex) { switch (metaIndex) { case MetaIndex.Eqp: - _eqpCache.SetFiles(_manager); + Eqp.SetFiles(); break; case MetaIndex.Gmp: - _gmpCache.SetFiles(_manager); + Gmp.SetFiles(); break; case MetaIndex.HumanCmp: - _cmpCache.SetFiles(_manager); + Rsp.SetFiles(); break; case MetaIndex.FaceEst: case MetaIndex.HairEst: case MetaIndex.HeadEst: case MetaIndex.BodyEst: - _estCache.SetFile(_manager, metaIndex); + Est.SetFile(metaIndex); break; default: - _eqdpCache.SetFile(_manager, metaIndex); + Eqdp.SetFile(metaIndex); break; } } /// Set the currently relevant IMC files for the collection cache. public void SetImcFiles(bool fromFullCompute) - => _imcCache.SetFiles(_collection, fromFullCompute); + => Imc.SetFiles(fromFullCompute); public MetaList.MetaReverter TemporarilySetEqpFile() - => _eqpCache.TemporarilySetFiles(_manager); + => Eqp.TemporarilySetFile(); public MetaList.MetaReverter? TemporarilySetEqdpFile(GenderRace genderRace, bool accessory) - => _eqdpCache.TemporarilySetFiles(_manager, genderRace, accessory); + => Eqdp.TemporarilySetFile(genderRace, accessory); public MetaList.MetaReverter TemporarilySetGmpFile() - => _gmpCache.TemporarilySetFiles(_manager); + => Gmp.TemporarilySetFile(); public MetaList.MetaReverter TemporarilySetCmpFile() - => _cmpCache.TemporarilySetFiles(_manager); + => Rsp.TemporarilySetFile(); public MetaList.MetaReverter TemporarilySetEstFile(EstType type) - => _estCache.TemporarilySetFiles(_manager, type); + => Est.TemporarilySetFiles(type); public unsafe EqpEntry ApplyGlobalEqp(EqpEntry baseEntry, CharacterArmor* armor) - => _globalEqpCache.Apply(baseEntry, armor); + => GlobalEqp.Apply(baseEntry, armor); /// Try to obtain a manipulated IMC file. public bool GetImcFile(Utf8GamePath path, [NotNullWhen(true)] out Meta.Files.ImcFile? file) - => _imcCache.GetImcFile(path, out file); + => Imc.GetFile(path, out file); internal EqdpEntry GetEqdpEntry(GenderRace race, bool accessory, PrimaryId primaryId) { - var eqdpFile = _eqdpCache.EqdpFile(race, accessory); + var eqdpFile = Eqdp.EqdpFile(race, accessory); if (eqdpFile != null) return primaryId.Id < eqdpFile.Count ? eqdpFile[primaryId] : default; - return Meta.Files.ExpandedEqdpFile.GetDefault(_manager, race, accessory, primaryId); + return Meta.Files.ExpandedEqdpFile.GetDefault(manager, race, accessory, primaryId); } internal EstEntry GetEstEntry(EstType type, GenderRace genderRace, PrimaryId primaryId) - => _estCache.GetEstEntry(_manager, type, genderRace, primaryId); - - /// Use this when CharacterUtility becomes ready. - private void ApplyStoredManipulations() - { - if (!_manager.CharacterUtility.Ready) - return; - - var loaded = 0; - lock (_manipulations) - { - foreach (var manip in Manipulations) - { - loaded += manip.ManipulationType switch - { - MetaManipulation.Type.Eqp => _eqpCache.ApplyMod(_manager, manip.Eqp), - MetaManipulation.Type.Eqdp => _eqdpCache.ApplyMod(_manager, manip.Eqdp), - MetaManipulation.Type.Est => _estCache.ApplyMod(_manager, manip.Est), - MetaManipulation.Type.Gmp => _gmpCache.ApplyMod(_manager, manip.Gmp), - MetaManipulation.Type.Rsp => _cmpCache.ApplyMod(_manager, manip.Rsp), - MetaManipulation.Type.Imc => _imcCache.ApplyMod(_manager, _collection, manip.Imc), - MetaManipulation.Type.GlobalEqp => false, - MetaManipulation.Type.Unknown => false, - _ => false, - } - ? 1 - : 0; - } - } - - _manager.ApplyDefaultFiles(_collection); - _manager.CharacterUtility.LoadingFinished -= ApplyStoredManipulations; - Penumbra.Log.Debug($"{_collection.AnonymizedName}: Loaded {loaded} delayed meta manipulations."); - } + => Est.GetEstEntry(new EstIdentifier(primaryId, type, genderRace)); } diff --git a/Penumbra/Collections/Cache/RspCache.cs b/Penumbra/Collections/Cache/RspCache.cs new file mode 100644 index 00000000..3889d6f1 --- /dev/null +++ b/Penumbra/Collections/Cache/RspCache.cs @@ -0,0 +1,81 @@ +using Penumbra.Interop.Services; +using Penumbra.Interop.Structs; +using Penumbra.Meta; +using Penumbra.Meta.Files; +using Penumbra.Meta.Manipulations; + +namespace Penumbra.Collections.Cache; + +public sealed class RspCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) +{ + private CmpFile? _cmpFile; + + public override void SetFiles() + => Manager.SetFile(_cmpFile, MetaIndex.HumanCmp); + + public override void ResetFiles() + => Manager.SetFile(null, MetaIndex.HumanCmp); + + protected override void IncorporateChangesInternal() + { + if (GetFile() is not { } file) + return; + + foreach (var (identifier, (_, entry)) in this) + Apply(file, identifier, entry); + + Penumbra.Log.Verbose($"{Collection.AnonymizedName}: Loaded {Count} delayed RSP manipulations."); + } + + public MetaList.MetaReverter TemporarilySetFile() + => Manager.TemporarilySetFile(_cmpFile, MetaIndex.HumanCmp); + + public override void Reset() + { + if (_cmpFile == null) + return; + + _cmpFile.Reset(Keys.Select(identifier => (identifier.SubRace, identifier.Attribute))); + Clear(); + } + + protected override void ApplyModInternal(RspIdentifier identifier, RspEntry entry) + { + if (GetFile() is { } file) + Apply(file, identifier, entry); + } + + protected override void RevertModInternal(RspIdentifier identifier) + { + if (GetFile() is { } file) + Apply(file, identifier, CmpFile.GetDefault(Manager, identifier.SubRace, identifier.Attribute)); + } + + public static bool Apply(CmpFile file, RspIdentifier identifier, RspEntry entry) + { + var value = file[identifier.SubRace, identifier.Attribute]; + if (value == entry) + return false; + + file[identifier.SubRace, identifier.Attribute] = entry; + return true; + } + + protected override void Dispose(bool _) + { + _cmpFile?.Dispose(); + _cmpFile = null; + Clear(); + } + + private CmpFile? GetFile() + { + if (_cmpFile != null) + return _cmpFile; + + if (!Manager.CharacterUtility.Ready) + return null; + + return _cmpFile = new CmpFile(Manager); + } +} diff --git a/Penumbra/Import/Models/ModelManager.cs b/Penumbra/Import/Models/ModelManager.cs index fdd28ef1..9fa77784 100644 --- a/Penumbra/Import/Models/ModelManager.cs +++ b/Penumbra/Import/Models/ModelManager.cs @@ -37,7 +37,8 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect _tasks.Clear(); } - public Task ExportToGltf(in ExportConfig config, MdlFile mdl, IEnumerable sklbPaths, Func read, string outputPath) + public Task ExportToGltf(in ExportConfig config, MdlFile mdl, IEnumerable sklbPaths, Func read, + string outputPath) => EnqueueWithResult( new ExportToGltfAction(this, config, mdl, sklbPaths, read, outputPath), action => action.Notifier @@ -52,7 +53,7 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect /// Try to find the .sklb paths for a .mdl file. /// .mdl file to look up the skeletons for. /// Modified extra skeleton template parameters. - public string[] ResolveSklbsForMdl(string mdlPath, EstManipulation[] estManipulations) + public string[] ResolveSklbsForMdl(string mdlPath, KeyValuePair[] estManipulations) { var info = parser.GetFileInfo(mdlPath); if (info.FileType is not FileType.Model) @@ -81,20 +82,18 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect }; } - private string[] ResolveEstSkeleton(EstType type, GameObjectInfo info, EstManipulation[] estManipulations) + private string[] ResolveEstSkeleton(EstType type, GameObjectInfo info, KeyValuePair[] estManipulations) { // Try to find an EST entry from the manipulations provided. - var (gender, race) = info.GenderRace.Split(); var modEst = estManipulations - .FirstOrNull(est => - est.Gender == gender - && est.Race == race - && est.Slot == type - && est.SetId == info.PrimaryId + .FirstOrNull( + est => est.Key.GenderRace == info.GenderRace + && est.Key.Slot == type + && est.Key.SetId == info.PrimaryId ); // Try to use an entry from provided manipulations, falling back to the current collection. - var targetId = modEst?.Entry + var targetId = modEst?.Value ?? collections.Current.MetaCache?.GetEstEntry(type, info.GenderRace, info.PrimaryId) ?? EstEntry.Zero; @@ -102,7 +101,7 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect if (targetId == EstEntry.Zero) return []; - return [GamePaths.Skeleton.Sklb.Path(info.GenderRace, EstManipulation.ToName(type), targetId.AsId)]; + return [GamePaths.Skeleton.Sklb.Path(info.GenderRace, type.ToName(), targetId.AsId)]; } /// Try to resolve the absolute path to a .mtrl from the potentially-partial path provided by a model. @@ -250,9 +249,11 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect var path = manager.ResolveMtrlPath(relativePath, notifier); if (path == null) return null; + var bytes = read(path); if (bytes == null) return null; + var mtrl = new MtrlFile(bytes); return new MaterialExporter.Material diff --git a/Penumbra/Import/TexToolsMeta.Deserialization.cs b/Penumbra/Import/TexToolsMeta.Deserialization.cs index f6157747..1f970dfe 100644 --- a/Penumbra/Import/TexToolsMeta.Deserialization.cs +++ b/Penumbra/Import/TexToolsMeta.Deserialization.cs @@ -13,15 +13,15 @@ public partial class TexToolsMeta private void DeserializeEqpEntry(MetaFileInfo metaFileInfo, byte[]? data) { // Eqp can only be valid for equipment. - if (data == null || !metaFileInfo.EquipSlot.IsEquipment()) + var mask = Eqp.Mask(metaFileInfo.EquipSlot); + if (data == null || mask == 0) return; - var value = Eqp.FromSlotAndBytes(metaFileInfo.EquipSlot, data); - var def = new EqpManipulation(ExpandedEqpFile.GetDefault(_metaFileManager, metaFileInfo.PrimaryId), metaFileInfo.EquipSlot, - metaFileInfo.PrimaryId); - var manip = new EqpManipulation(value, metaFileInfo.EquipSlot, metaFileInfo.PrimaryId); - if (_keepDefault || def.Entry != manip.Entry) - MetaManipulations.Add(manip); + var identifier = new EqpIdentifier(metaFileInfo.PrimaryId, metaFileInfo.EquipSlot); + var value = Eqp.FromSlotAndBytes(metaFileInfo.EquipSlot, data) & mask; + var def = ExpandedEqpFile.GetDefault(_metaFileManager, metaFileInfo.PrimaryId) & mask; + if (_keepDefault || def != value) + MetaManipulations.TryAdd(identifier, value); } // Deserialize and check Eqdp Entries and add them to the list if they are non-default. @@ -40,14 +40,12 @@ public partial class TexToolsMeta if (!gr.IsValid() || !metaFileInfo.EquipSlot.IsEquipment() && !metaFileInfo.EquipSlot.IsAccessory()) continue; - var value = Eqdp.FromSlotAndBits(metaFileInfo.EquipSlot, (byteValue & 1) == 1, (byteValue & 2) == 2); - var def = new EqdpManipulation( - ExpandedEqdpFile.GetDefault(_metaFileManager, gr, metaFileInfo.EquipSlot.IsAccessory(), metaFileInfo.PrimaryId), - metaFileInfo.EquipSlot, - gr.Split().Item1, gr.Split().Item2, metaFileInfo.PrimaryId); - var manip = new EqdpManipulation(value, metaFileInfo.EquipSlot, gr.Split().Item1, gr.Split().Item2, metaFileInfo.PrimaryId); - if (_keepDefault || def.Entry != manip.Entry) - MetaManipulations.Add(manip); + var identifier = new EqdpIdentifier(metaFileInfo.PrimaryId, metaFileInfo.EquipSlot, gr); + var mask = Eqdp.Mask(metaFileInfo.EquipSlot); + var value = Eqdp.FromSlotAndBits(metaFileInfo.EquipSlot, (byteValue & 1) == 1, (byteValue & 2) == 2) & mask; + var def = ExpandedEqdpFile.GetDefault(_metaFileManager, gr, metaFileInfo.EquipSlot.IsAccessory(), metaFileInfo.PrimaryId) & mask; + if (_keepDefault || def != value) + MetaManipulations.TryAdd(identifier, value); } } @@ -57,10 +55,10 @@ public partial class TexToolsMeta if (data == null) return; - var value = GmpEntry.FromTexToolsMeta(data.AsSpan(0, 5)); - var def = ExpandedGmpFile.GetDefault(_metaFileManager, metaFileInfo.PrimaryId); + var value = GmpEntry.FromTexToolsMeta(data.AsSpan(0, 5)); + var def = ExpandedGmpFile.GetDefault(_metaFileManager, metaFileInfo.PrimaryId); if (_keepDefault || value != def) - MetaManipulations.Add(new GmpManipulation(value, metaFileInfo.PrimaryId)); + MetaManipulations.TryAdd(new GmpIdentifier(metaFileInfo.PrimaryId), value); } // Deserialize and check Est Entries and add them to the list if they are non-default. @@ -74,7 +72,7 @@ public partial class TexToolsMeta for (var i = 0; i < num; ++i) { var gr = (GenderRace)reader.ReadUInt16(); - var id = reader.ReadUInt16(); + var id = (PrimaryId)reader.ReadUInt16(); var value = new EstEntry(reader.ReadUInt16()); var type = (metaFileInfo.SecondaryType, metaFileInfo.EquipSlot) switch { @@ -87,9 +85,10 @@ public partial class TexToolsMeta if (!gr.IsValid() || type == 0) continue; - var def = EstFile.GetDefault(_metaFileManager, type, gr, id); + var identifier = new EstIdentifier(id, type, gr); + var def = EstFile.GetDefault(_metaFileManager, type, gr, id); if (_keepDefault || def != value) - MetaManipulations.Add(new EstManipulation(gr.Split().Item1, gr.Split().Item2, type, id, value)); + MetaManipulations.TryAdd(identifier, value); } } @@ -107,20 +106,16 @@ public partial class TexToolsMeta ushort i = 0; try { - var manip = new ImcManipulation(metaFileInfo.PrimaryType, metaFileInfo.SecondaryType, metaFileInfo.PrimaryId, - metaFileInfo.SecondaryId, i, metaFileInfo.EquipSlot, - new ImcEntry()); - var def = new ImcFile(_metaFileManager, manip.Identifier); - var partIdx = ImcFile.PartIndex(manip.EquipSlot); // Gets turned to unknown for things without equip, and unknown turns to 0. + var identifier = new ImcIdentifier(metaFileInfo.PrimaryId, 0, metaFileInfo.PrimaryType, metaFileInfo.SecondaryId, + metaFileInfo.EquipSlot, metaFileInfo.SecondaryType); + var file = new ImcFile(_metaFileManager, identifier); + var partIdx = ImcFile.PartIndex(identifier.EquipSlot); // Gets turned to unknown for things without equip, and unknown turns to 0. foreach (var value in values) { - if (_keepDefault || !value.Equals(def.GetEntry(partIdx, (Variant)i))) - { - var imc = new ImcManipulation(manip.ObjectType, manip.BodySlot, manip.PrimaryId, manip.SecondaryId, i, manip.EquipSlot, - value); - if (imc.Validate(true)) - MetaManipulations.Add(imc); - } + identifier = identifier with { Variant = (Variant)i }; + var def = file.GetEntry(partIdx, (Variant)i); + if (_keepDefault || def != value && identifier.Validate()) + MetaManipulations.TryAdd(identifier, value); ++i; } diff --git a/Penumbra/Import/TexToolsMeta.Export.cs b/Penumbra/Import/TexToolsMeta.Export.cs index 4fb56df6..9cce60e3 100644 --- a/Penumbra/Import/TexToolsMeta.Export.cs +++ b/Penumbra/Import/TexToolsMeta.Export.cs @@ -1,3 +1,4 @@ +using Penumbra.Collections.Cache; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Meta; @@ -8,7 +9,7 @@ namespace Penumbra.Import; public partial class TexToolsMeta { - public static void WriteTexToolsMeta(MetaFileManager manager, IEnumerable manipulations, DirectoryInfo basePath) + public static void WriteTexToolsMeta(MetaFileManager manager, MetaDictionary manipulations, DirectoryInfo basePath) { var files = ConvertToTexTools(manager, manipulations); @@ -27,49 +28,81 @@ public partial class TexToolsMeta } } - public static Dictionary ConvertToTexTools(MetaFileManager manager, IEnumerable manips) + public static Dictionary ConvertToTexTools(MetaFileManager manager, MetaDictionary manips) { var ret = new Dictionary(); - foreach (var group in manips.GroupBy(ManipToPath)) + foreach (var group in manips.Rsp.GroupBy(ManipToPath)) { if (group.Key.Length == 0) continue; - var bytes = group.Key.EndsWith(".rgsp") - ? WriteRgspFile(manager, group.Key, group) - : WriteMetaFile(manager, group.Key, group); + var bytes = WriteRgspFile(manager, group); if (bytes.Length == 0) continue; ret.Add(group.Key, bytes); } + foreach (var (file, dict) in SplitByFile(manips)) + { + var bytes = WriteMetaFile(manager, file, dict); + if (bytes.Length == 0) + continue; + + ret.Add(file, bytes); + } + return ret; } - private static byte[] WriteRgspFile(MetaFileManager manager, string path, IEnumerable manips) + private static Dictionary SplitByFile(MetaDictionary manips) { - var list = manips.GroupBy(m => m.Rsp.Attribute).ToDictionary(m => m.Key, m => m.Last().Rsp); + var ret = new Dictionary(); + foreach (var (identifier, key) in manips.Imc) + GetDict(ManipToPath(identifier)).TryAdd(identifier, key); + + foreach (var (identifier, key) in manips.Eqp) + GetDict(ManipToPath(identifier)).TryAdd(identifier, key); + + foreach (var (identifier, key) in manips.Eqdp) + GetDict(ManipToPath(identifier)).TryAdd(identifier, key); + + foreach (var (identifier, key) in manips.Est) + GetDict(ManipToPath(identifier)).TryAdd(identifier, key); + + foreach (var (identifier, key) in manips.Gmp) + GetDict(ManipToPath(identifier)).TryAdd(identifier, key); + + ret.Remove(string.Empty); + + return ret; + + MetaDictionary GetDict(string path) + { + if (!ret.TryGetValue(path, out var dict)) + { + dict = new MetaDictionary(); + ret.Add(path, dict); + } + + return dict; + } + } + + private static byte[] WriteRgspFile(MetaFileManager manager, IEnumerable> manips) + { + var list = manips.GroupBy(m => m.Key.Attribute).ToDictionary(g => g.Key, g => g.Last()); using var m = new MemoryStream(45); using var b = new BinaryWriter(m); // Version b.Write(byte.MaxValue); b.Write((ushort)2); - var race = list.First().Value.SubRace; - var gender = list.First().Value.Attribute.ToGender(); + var race = list.First().Value.Key.SubRace; + var gender = list.First().Value.Key.Attribute.ToGender(); b.Write((byte)(race - 1)); // offset by one due to Unknown b.Write((byte)(gender - 1)); // offset by one due to Unknown - void Add(params RspAttribute[] attributes) - { - foreach (var attribute in attributes) - { - var value = list.TryGetValue(attribute, out var tmp) ? tmp.Entry : CmpFile.GetDefault(manager, race, attribute); - b.Write(value.Value); - } - } - if (gender == Gender.Male) { Add(RspAttribute.MaleMinSize, RspAttribute.MaleMaxSize, RspAttribute.MaleMinTail, RspAttribute.MaleMaxTail); @@ -82,12 +115,24 @@ public partial class TexToolsMeta } return m.GetBuffer(); + + void Add(params RspAttribute[] attributes) + { + foreach (var attribute in attributes) + { + var value = list.TryGetValue(attribute, out var tmp) ? tmp.Value : CmpFile.GetDefault(manager, race, attribute); + b.Write(value.Value); + } + } } - private static byte[] WriteMetaFile(MetaFileManager manager, string path, IEnumerable manips) + private static byte[] WriteMetaFile(MetaFileManager manager, string path, MetaDictionary manips) { - var filteredManips = manips.GroupBy(m => m.ManipulationType).ToDictionary(p => p.Key, p => p.Select(x => x)); - + var headerCount = (manips.Imc.Count > 0 ? 1 : 0) + + (manips.Eqp.Count > 0 ? 1 : 0) + + (manips.Eqdp.Count > 0 ? 1 : 0) + + (manips.Est.Count > 0 ? 1 : 0) + + (manips.Gmp.Count > 0 ? 1 : 0); using var m = new MemoryStream(); using var b = new BinaryWriter(m); @@ -101,7 +146,7 @@ public partial class TexToolsMeta b.Write((byte)0); // Number of Headers - b.Write((uint)filteredManips.Count); + b.Write((uint)headerCount); // Current TT Size of Headers b.Write((uint)12); @@ -109,88 +154,44 @@ public partial class TexToolsMeta var headerStart = b.BaseStream.Position + 4; b.Write((uint)headerStart); - var offset = (uint)(b.BaseStream.Position + 12 * filteredManips.Count); - foreach (var (header, data) in filteredManips) - { - b.Write((uint)header); - b.Write(offset); - - var size = WriteData(manager, b, offset, header, data); - b.Write(size); - offset += size; - } + var offset = (uint)(b.BaseStream.Position + 12 * manips.Count); + offset += WriteData(manager, b, offset, manips.Imc); + offset += WriteData(b, offset, manips.Eqdp); + offset += WriteData(b, offset, manips.Eqp); + offset += WriteData(b, offset, manips.Est); + offset += WriteData(b, offset, manips.Gmp); return m.ToArray(); } - private static uint WriteData(MetaFileManager manager, BinaryWriter b, uint offset, MetaManipulation.Type type, - IEnumerable manips) + private static uint WriteData(MetaFileManager manager, BinaryWriter b, uint offset, IReadOnlyDictionary manips) { + if (manips.Count == 0) + return 0; + + b.Write((uint)MetaManipulationType.Imc); + b.Write(offset); + var oldPos = b.BaseStream.Position; b.Seek((int)offset, SeekOrigin.Begin); - switch (type) + var refIdentifier = manips.First().Key; + var baseFile = new ImcFile(manager, refIdentifier); + foreach (var (identifier, entry) in manips) + ImcCache.Apply(baseFile, identifier, entry); + + var partIdx = refIdentifier.ObjectType is ObjectType.Equipment or ObjectType.Accessory + ? ImcFile.PartIndex(refIdentifier.EquipSlot) + : 0; + + for (var i = 0; i <= baseFile.Count; ++i) { - case MetaManipulation.Type.Imc: - var allManips = manips.ToList(); - var baseFile = new ImcFile(manager, allManips[0].Imc.Identifier); - foreach (var manip in allManips) - manip.Imc.Apply(baseFile); - - var partIdx = allManips[0].Imc.ObjectType is ObjectType.Equipment or ObjectType.Accessory - ? ImcFile.PartIndex(allManips[0].Imc.EquipSlot) - : 0; - - for (var i = 0; i <= baseFile.Count; ++i) - { - var entry = baseFile.GetEntry(partIdx, (Variant)i); - b.Write(entry.MaterialId); - b.Write(entry.DecalId); - b.Write(entry.AttributeAndSound); - b.Write(entry.VfxId); - b.Write(entry.MaterialAnimationId); - } - - break; - case MetaManipulation.Type.Eqdp: - foreach (var manip in manips) - { - b.Write((uint)Names.CombinedRace(manip.Eqdp.Gender, manip.Eqdp.Race)); - var entry = (byte)(((uint)manip.Eqdp.Entry >> Eqdp.Offset(manip.Eqdp.Slot)) & 0x03); - b.Write(entry); - } - - break; - case MetaManipulation.Type.Eqp: - foreach (var manip in manips) - { - var bytes = BitConverter.GetBytes((ulong)manip.Eqp.Entry); - var (numBytes, byteOffset) = Eqp.BytesAndOffset(manip.Eqp.Slot); - for (var i = byteOffset; i < numBytes + byteOffset; ++i) - b.Write(bytes[i]); - } - - break; - case MetaManipulation.Type.Est: - foreach (var manip in manips) - { - b.Write((ushort)Names.CombinedRace(manip.Est.Gender, manip.Est.Race)); - b.Write(manip.Est.SetId.Id); - b.Write(manip.Est.Entry.Value); - } - - break; - case MetaManipulation.Type.Gmp: - foreach (var manip in manips) - { - b.Write((uint)manip.Gmp.Entry.Value); - b.Write(manip.Gmp.Entry.UnknownTotal); - } - - break; - case MetaManipulation.Type.GlobalEqp: - // Not Supported - break; + var entry = baseFile.GetEntry(partIdx, (Variant)i); + b.Write(entry.MaterialId); + b.Write(entry.DecalId); + b.Write(entry.AttributeAndSound); + b.Write(entry.VfxId); + b.Write(entry.MaterialAnimationId); } var size = b.BaseStream.Position - offset; @@ -198,19 +199,98 @@ public partial class TexToolsMeta return (uint)size; } - private static string ManipToPath(MetaManipulation manip) - => manip.ManipulationType switch - { - MetaManipulation.Type.Imc => ManipToPath(manip.Imc), - MetaManipulation.Type.Eqdp => ManipToPath(manip.Eqdp), - MetaManipulation.Type.Eqp => ManipToPath(manip.Eqp), - MetaManipulation.Type.Est => ManipToPath(manip.Est), - MetaManipulation.Type.Gmp => ManipToPath(manip.Gmp), - MetaManipulation.Type.Rsp => ManipToPath(manip.Rsp), - _ => string.Empty, - }; + private static uint WriteData(BinaryWriter b, uint offset, IReadOnlyDictionary manips) + { + if (manips.Count == 0) + return 0; - private static string ManipToPath(ImcManipulation manip) + b.Write((uint)MetaManipulationType.Eqdp); + b.Write(offset); + + var oldPos = b.BaseStream.Position; + b.Seek((int)offset, SeekOrigin.Begin); + + foreach (var (identifier, entry) in manips) + { + b.Write((uint)identifier.GenderRace); + b.Write(entry.AsByte); + } + + var size = b.BaseStream.Position - offset; + b.Seek((int)oldPos, SeekOrigin.Begin); + return (uint)size; + } + + private static uint WriteData(BinaryWriter b, uint offset, + IReadOnlyDictionary manips) + { + if (manips.Count == 0) + return 0; + + b.Write((uint)MetaManipulationType.Imc); + b.Write(offset); + + var oldPos = b.BaseStream.Position; + b.Seek((int)offset, SeekOrigin.Begin); + + foreach (var (identifier, entry) in manips) + { + var numBytes = Eqp.BytesAndOffset(identifier.Slot).Item1; + for (var i = 0; i < numBytes; ++i) + b.Write((byte)(entry.Value >> (8 * i))); + } + + var size = b.BaseStream.Position - offset; + b.Seek((int)oldPos, SeekOrigin.Begin); + return (uint)size; + } + + private static uint WriteData(BinaryWriter b, uint offset, IReadOnlyDictionary manips) + { + if (manips.Count == 0) + return 0; + + b.Write((uint)MetaManipulationType.Imc); + b.Write(offset); + + var oldPos = b.BaseStream.Position; + b.Seek((int)offset, SeekOrigin.Begin); + + foreach (var (identifier, entry) in manips) + { + b.Write((ushort)identifier.GenderRace); + b.Write(identifier.SetId.Id); + b.Write(entry.Value); + } + + var size = b.BaseStream.Position - offset; + b.Seek((int)oldPos, SeekOrigin.Begin); + return (uint)size; + } + + private static uint WriteData(BinaryWriter b, uint offset, IReadOnlyDictionary manips) + { + if (manips.Count == 0) + return 0; + + b.Write((uint)MetaManipulationType.Imc); + b.Write(offset); + + var oldPos = b.BaseStream.Position; + b.Seek((int)offset, SeekOrigin.Begin); + + foreach (var entry in manips.Values) + { + b.Write((uint)entry.Value); + b.Write(entry.UnknownTotal); + } + + var size = b.BaseStream.Position - offset; + b.Seek((int)oldPos, SeekOrigin.Begin); + return (uint)size; + } + + private static string ManipToPath(ImcIdentifier manip) { var path = manip.GamePath().ToString(); var replacement = manip.ObjectType switch @@ -224,33 +304,33 @@ public partial class TexToolsMeta return path.Replace(".imc", replacement); } - private static string ManipToPath(EqdpManipulation manip) + private static string ManipToPath(EqdpIdentifier manip) => manip.Slot.IsAccessory() - ? $"chara/accessory/a{manip.SetId:D4}/a{manip.SetId:D4}_{manip.Slot.ToSuffix()}.meta" - : $"chara/equipment/e{manip.SetId:D4}/e{manip.SetId:D4}_{manip.Slot.ToSuffix()}.meta"; + ? $"chara/accessory/a{manip.SetId.Id:D4}/a{manip.SetId.Id:D4}_{manip.Slot.ToSuffix()}.meta" + : $"chara/equipment/e{manip.SetId.Id:D4}/e{manip.SetId.Id:D4}_{manip.Slot.ToSuffix()}.meta"; - private static string ManipToPath(EqpManipulation manip) + private static string ManipToPath(EqpIdentifier manip) => manip.Slot.IsAccessory() - ? $"chara/accessory/a{manip.SetId:D4}/a{manip.SetId:D4}_{manip.Slot.ToSuffix()}.meta" - : $"chara/equipment/e{manip.SetId:D4}/e{manip.SetId:D4}_{manip.Slot.ToSuffix()}.meta"; + ? $"chara/accessory/a{manip.SetId.Id:D4}/a{manip.SetId.Id:D4}_{manip.Slot.ToSuffix()}.meta" + : $"chara/equipment/e{manip.SetId.Id:D4}/e{manip.SetId.Id:D4}_{manip.Slot.ToSuffix()}.meta"; - private static string ManipToPath(EstManipulation manip) + private static string ManipToPath(EstIdentifier manip) { var raceCode = Names.CombinedRace(manip.Gender, manip.Race).ToRaceCode(); return manip.Slot switch { - EstType.Hair => $"chara/human/c{raceCode}/obj/hair/h{manip.SetId:D4}/c{raceCode}h{manip.SetId:D4}_hir.meta", - EstType.Face => $"chara/human/c{raceCode}/obj/face/h{manip.SetId:D4}/c{raceCode}f{manip.SetId:D4}_fac.meta", - EstType.Body => $"chara/equipment/e{manip.SetId:D4}/e{manip.SetId:D4}_{EquipSlot.Body.ToSuffix()}.meta", - EstType.Head => $"chara/equipment/e{manip.SetId:D4}/e{manip.SetId:D4}_{EquipSlot.Head.ToSuffix()}.meta", - _ => throw new ArgumentOutOfRangeException(), + EstType.Hair => $"chara/human/c{raceCode}/obj/hair/h{manip.SetId.Id:D4}/c{raceCode}h{manip.SetId.Id:D4}_hir.meta", + EstType.Face => $"chara/human/c{raceCode}/obj/face/h{manip.SetId.Id:D4}/c{raceCode}f{manip.SetId.Id:D4}_fac.meta", + EstType.Body => $"chara/equipment/e{manip.SetId.Id:D4}/e{manip.SetId.Id:D4}_{EquipSlot.Body.ToSuffix()}.meta", + EstType.Head => $"chara/equipment/e{manip.SetId.Id:D4}/e{manip.SetId.Id:D4}_{EquipSlot.Head.ToSuffix()}.meta", + _ => throw new ArgumentOutOfRangeException(), }; } - private static string ManipToPath(GmpManipulation manip) - => $"chara/equipment/e{manip.SetId:D4}/e{manip.SetId:D4}_{EquipSlot.Head.ToSuffix()}.meta"; + private static string ManipToPath(GmpIdentifier manip) + => $"chara/equipment/e{manip.SetId.Id:D4}/e{manip.SetId.Id:D4}_{EquipSlot.Head.ToSuffix()}.meta"; - private static string ManipToPath(RspManipulation manip) - => $"chara/xls/charamake/rgsp/{(int)manip.SubRace - 1}-{(int)manip.Attribute.ToGender() - 1}.rgsp"; + private static string ManipToPath(KeyValuePair manip) + => $"chara/xls/charamake/rgsp/{(int)manip.Key.SubRace - 1}-{(int)manip.Key.Attribute.ToGender() - 1}.rgsp"; } diff --git a/Penumbra/Import/TexToolsMeta.Rgsp.cs b/Penumbra/Import/TexToolsMeta.Rgsp.cs index 71b9165f..7b0bb5a8 100644 --- a/Penumbra/Import/TexToolsMeta.Rgsp.cs +++ b/Penumbra/Import/TexToolsMeta.Rgsp.cs @@ -42,14 +42,6 @@ public partial class TexToolsMeta return Invalid; } - // Add the given values to the manipulations if they are not default. - void Add(RspAttribute attribute, float value) - { - var def = CmpFile.GetDefault(manager, subRace, attribute); - if (keepDefault || value != def.Value) - ret.MetaManipulations.Add(new RspManipulation(subRace, attribute, new RspEntry(value))); - } - if (gender == 1) { Add(RspAttribute.FemaleMinSize, br.ReadSingle()); @@ -73,5 +65,14 @@ public partial class TexToolsMeta } return ret; + + // Add the given values to the manipulations if they are not default. + void Add(RspAttribute attribute, float value) + { + var identifier = new RspIdentifier(subRace, attribute); + var def = CmpFile.GetDefault(manager, subRace, attribute); + if (keepDefault || value != def.Value) + ret.MetaManipulations.TryAdd(identifier, new RspEntry(value)); + } } } diff --git a/Penumbra/Import/TexToolsMeta.cs b/Penumbra/Import/TexToolsMeta.cs index 25e00bd7..c4a8e81f 100644 --- a/Penumbra/Import/TexToolsMeta.cs +++ b/Penumbra/Import/TexToolsMeta.cs @@ -1,4 +1,3 @@ -using Penumbra.GameData; using Penumbra.GameData.Data; using Penumbra.Import.Structs; using Penumbra.Meta; @@ -22,10 +21,10 @@ public partial class TexToolsMeta public static readonly TexToolsMeta Invalid = new(null!, string.Empty, 0); // The info class determines the files or table locations the changes need to apply to from the filename. - public readonly uint Version; - public readonly string FilePath; - public readonly List MetaManipulations = new(); - private readonly bool _keepDefault = false; + public readonly uint Version; + public readonly string FilePath; + public readonly MetaDictionary MetaManipulations = new(); + private readonly bool _keepDefault; private readonly MetaFileManager _metaFileManager; @@ -44,18 +43,18 @@ public partial class TexToolsMeta var headerStart = reader.ReadUInt32(); reader.BaseStream.Seek(headerStart, SeekOrigin.Begin); - List<(MetaManipulation.Type type, uint offset, int size)> entries = []; + List<(MetaManipulationType type, uint offset, int size)> entries = []; for (var i = 0; i < numHeaders; ++i) { var currentOffset = reader.BaseStream.Position; - var type = (MetaManipulation.Type)reader.ReadUInt32(); + var type = (MetaManipulationType)reader.ReadUInt32(); var offset = reader.ReadUInt32(); var size = reader.ReadInt32(); entries.Add((type, offset, size)); reader.BaseStream.Seek(currentOffset + headerSize, SeekOrigin.Begin); } - byte[]? ReadEntry(MetaManipulation.Type type) + byte[]? ReadEntry(MetaManipulationType type) { var idx = entries.FindIndex(t => t.type == type); if (idx < 0) @@ -65,11 +64,11 @@ public partial class TexToolsMeta return reader.ReadBytes(entries[idx].size); } - DeserializeEqpEntry(metaInfo, ReadEntry(MetaManipulation.Type.Eqp)); - DeserializeGmpEntry(metaInfo, ReadEntry(MetaManipulation.Type.Gmp)); - DeserializeEqdpEntries(metaInfo, ReadEntry(MetaManipulation.Type.Eqdp)); - DeserializeEstEntries(metaInfo, ReadEntry(MetaManipulation.Type.Est)); - DeserializeImcEntries(metaInfo, ReadEntry(MetaManipulation.Type.Imc)); + DeserializeEqpEntry(metaInfo, ReadEntry(MetaManipulationType.Eqp)); + DeserializeGmpEntry(metaInfo, ReadEntry(MetaManipulationType.Gmp)); + DeserializeEqdpEntries(metaInfo, ReadEntry(MetaManipulationType.Eqdp)); + DeserializeEstEntries(metaInfo, ReadEntry(MetaManipulationType.Est)); + DeserializeImcEntries(metaInfo, ReadEntry(MetaManipulationType.Imc)); } catch (Exception e) { diff --git a/Penumbra/Interop/PathResolving/CollectionResolver.cs b/Penumbra/Interop/PathResolving/CollectionResolver.cs index b42571ac..bc474952 100644 --- a/Penumbra/Interop/PathResolving/CollectionResolver.cs +++ b/Penumbra/Interop/PathResolving/CollectionResolver.cs @@ -1,7 +1,6 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using FFXIVClientStructs.FFXIV.Client.UI.Agent; -using Microsoft.VisualBasic; using OtterGui.Services; using Penumbra.Collections; using Penumbra.Collections.Manager; diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs index 2b87e688..4dfefd96 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs @@ -273,7 +273,7 @@ internal partial record ResolveContext { var metaCache = Global.Collection.MetaCache; var skeletonSet = metaCache?.GetEstEntry(type, raceCode, primary) ?? default; - return (raceCode, EstManipulation.ToName(type), skeletonSet.AsId); + return (raceCode, type.ToName(), skeletonSet.AsId); } private unsafe Utf8GamePath ResolveSkeletonPathNative(uint partialSkeletonIndex) diff --git a/Penumbra/Meta/ImcChecker.cs b/Penumbra/Meta/ImcChecker.cs index 650919a3..751113a0 100644 --- a/Penumbra/Meta/ImcChecker.cs +++ b/Penumbra/Meta/ImcChecker.cs @@ -51,10 +51,6 @@ public class ImcChecker return entry; } - public CachedEntry GetDefaultEntry(ImcManipulation imcManip, bool storeCache) - => GetDefaultEntry(new ImcIdentifier(imcManip.PrimaryId, imcManip.Variant, imcManip.ObjectType, imcManip.SecondaryId.Id, - imcManip.EquipSlot, imcManip.BodySlot), storeCache); - private static ImcFile? GetFile(ImcIdentifier identifier) { if (_dataManager == null) diff --git a/Penumbra/Meta/Manipulations/Eqdp.cs b/Penumbra/Meta/Manipulations/Eqdp.cs index 6306f419..3f856bd2 100644 --- a/Penumbra/Meta/Manipulations/Eqdp.cs +++ b/Penumbra/Meta/Manipulations/Eqdp.cs @@ -69,10 +69,16 @@ public readonly record struct EqdpIdentifier(PrimaryId SetId, EquipSlot Slot, Ge jObj["Slot"] = Slot.ToString(); return jObj; } + + public MetaManipulationType Type + => MetaManipulationType.Eqdp; } public readonly record struct EqdpEntryInternal(bool Material, bool Model) { + public byte AsByte + => (byte)(Material ? Model ? 3 : 1 : Model ? 2 : 0); + private EqdpEntryInternal((bool, bool) val) : this(val.Item1, val.Item2) { } @@ -83,4 +89,7 @@ public readonly record struct EqdpEntryInternal(bool Material, bool Model) public EqdpEntry ToEntry(EquipSlot slot) => Eqdp.FromSlotAndBits(slot, Material, Model); + + public override string ToString() + => $"Material: {Material}, Model: {Model}"; } diff --git a/Penumbra/Meta/Manipulations/EqdpManipulation.cs b/Penumbra/Meta/Manipulations/EqdpManipulation.cs deleted file mode 100644 index 8c5f27e5..00000000 --- a/Penumbra/Meta/Manipulations/EqdpManipulation.cs +++ /dev/null @@ -1,110 +0,0 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; -using Penumbra.GameData.Enums; -using Penumbra.GameData.Structs; -using Penumbra.Interop.Structs; -using Penumbra.Meta.Files; - -namespace Penumbra.Meta.Manipulations; - -[StructLayout(LayoutKind.Sequential, Pack = 1)] -public readonly struct EqdpManipulation(EqdpIdentifier identifier, EqdpEntry entry) : IMetaManipulation -{ - [JsonIgnore] - public EqdpIdentifier Identifier { get; } = identifier; - - public EqdpEntry Entry { get; } = entry; - - [JsonConverter(typeof(StringEnumConverter))] - public Gender Gender - => Identifier.Gender; - - [JsonConverter(typeof(StringEnumConverter))] - public ModelRace Race - => Identifier.Race; - - public PrimaryId SetId - => Identifier.SetId; - - [JsonConverter(typeof(StringEnumConverter))] - public EquipSlot Slot - => Identifier.Slot; - - [JsonConstructor] - public EqdpManipulation(EqdpEntry entry, EquipSlot slot, Gender gender, ModelRace race, PrimaryId setId) - : this(new EqdpIdentifier(setId, slot, Names.CombinedRace(gender, race)), Eqdp.Mask(slot) & entry) - { } - - public EqdpManipulation Copy(EqdpManipulation entry) - { - if (entry.Slot != Slot) - { - var (bit1, bit2) = entry.Entry.ToBits(entry.Slot); - return new EqdpManipulation(Identifier, Eqdp.FromSlotAndBits(Slot, bit1, bit2)); - } - - return new EqdpManipulation(Identifier, entry.Entry); - } - - public EqdpManipulation Copy(EqdpEntry entry) - => new(entry, Slot, Gender, Race, SetId); - - public override string ToString() - => $"Eqdp - {SetId} - {Slot} - {Race.ToName()} - {Gender.ToName()}"; - - public bool Equals(EqdpManipulation other) - => Gender == other.Gender - && Race == other.Race - && SetId == other.SetId - && Slot == other.Slot; - - public override bool Equals(object? obj) - => obj is EqdpManipulation other && Equals(other); - - public override int GetHashCode() - => HashCode.Combine((int)Gender, (int)Race, SetId, (int)Slot); - - public int CompareTo(EqdpManipulation other) - { - var r = Race.CompareTo(other.Race); - if (r != 0) - return r; - - var g = Gender.CompareTo(other.Gender); - if (g != 0) - return g; - - var set = SetId.Id.CompareTo(other.SetId.Id); - return set != 0 ? set : Slot.CompareTo(other.Slot); - } - - public MetaIndex FileIndex() - => CharacterUtilityData.EqdpIdx(Names.CombinedRace(Gender, Race), Slot.IsAccessory()); - - public bool Apply(ExpandedEqdpFile file) - { - var entry = file[SetId]; - var mask = Eqdp.Mask(Slot); - if ((entry & mask) == Entry) - return false; - - file[SetId] = (entry & ~mask) | Entry; - return true; - } - - public bool Validate() - { - var mask = Eqdp.Mask(Slot); - if (mask == 0) - return false; - - if ((mask & Entry) != Entry) - return false; - - if (FileIndex() == (MetaIndex)(-1)) - return false; - - // No check for set id. - return true; - } -} diff --git a/Penumbra/Meta/Manipulations/Eqp.cs b/Penumbra/Meta/Manipulations/Eqp.cs index 572dc203..ec4dd6e7 100644 --- a/Penumbra/Meta/Manipulations/Eqp.cs +++ b/Penumbra/Meta/Manipulations/Eqp.cs @@ -50,6 +50,9 @@ public readonly record struct EqpIdentifier(PrimaryId SetId, EquipSlot Slot) : I jObj["Slot"] = Slot.ToString(); return jObj; } + + public MetaManipulationType Type + => MetaManipulationType.Eqp; } public readonly record struct EqpEntryInternal(uint Value) diff --git a/Penumbra/Meta/Manipulations/EqpManipulation.cs b/Penumbra/Meta/Manipulations/EqpManipulation.cs deleted file mode 100644 index eef21d12..00000000 --- a/Penumbra/Meta/Manipulations/EqpManipulation.cs +++ /dev/null @@ -1,80 +0,0 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; -using Penumbra.GameData.Enums; -using Penumbra.GameData.Structs; -using Penumbra.Interop.Structs; -using Penumbra.Meta.Files; -using Penumbra.Util; - -namespace Penumbra.Meta.Manipulations; - -[StructLayout(LayoutKind.Sequential, Pack = 1)] -public readonly struct EqpManipulation(EqpIdentifier identifier, EqpEntry entry) : IMetaManipulation -{ - [JsonIgnore] - public EqpIdentifier Identifier { get; } = identifier; - - [JsonConverter(typeof(ForceNumericFlagEnumConverter))] - public EqpEntry Entry { get; } = entry; - - public PrimaryId SetId - => Identifier.SetId; - - [JsonConverter(typeof(StringEnumConverter))] - public EquipSlot Slot - => Identifier.Slot; - - [JsonConstructor] - public EqpManipulation(EqpEntry entry, EquipSlot slot, PrimaryId setId) - : this(new EqpIdentifier(setId, slot), Eqp.Mask(slot) & entry) - { } - - public EqpManipulation Copy(EqpEntry entry) - => new(Identifier, entry); - - public override string ToString() - => $"Eqp - {SetId} - {Slot}"; - - public bool Equals(EqpManipulation other) - => Slot == other.Slot - && SetId == other.SetId; - - public override bool Equals(object? obj) - => obj is EqpManipulation other && Equals(other); - - public override int GetHashCode() - => HashCode.Combine((int)Slot, SetId); - - public int CompareTo(EqpManipulation other) - { - var set = SetId.Id.CompareTo(other.SetId.Id); - return set != 0 ? set : Slot.CompareTo(other.Slot); - } - - public MetaIndex FileIndex() - => MetaIndex.Eqp; - - public bool Apply(ExpandedEqpFile file) - { - var entry = file[SetId]; - var mask = Eqp.Mask(Slot); - if ((entry & mask) == Entry) - return false; - - file[SetId] = (entry & ~mask) | Entry; - return true; - } - - public bool Validate() - { - var mask = Eqp.Mask(Slot); - if (mask == 0) - return false; - if ((Entry & mask) != Entry) - return false; - - // No check for set id. - - return true; - } -} diff --git a/Penumbra/Meta/Manipulations/Est.cs b/Penumbra/Meta/Manipulations/Est.cs index 9f878f97..2955dba4 100644 --- a/Penumbra/Meta/Manipulations/Est.cs +++ b/Penumbra/Meta/Manipulations/Est.cs @@ -91,6 +91,9 @@ public readonly record struct EstIdentifier(PrimaryId SetId, EstType Slot, Gende jObj["Slot"] = Slot.ToString(); return jObj; } + + public MetaManipulationType Type + => MetaManipulationType.Est; } [JsonConverter(typeof(Converter))] @@ -111,3 +114,16 @@ public readonly record struct EstEntry(ushort Value) => new(serializer.Deserialize(reader)); } } + +public static class EstTypeExtension +{ + public static string ToName(this EstType type) + => type switch + { + EstType.Hair => "hair", + EstType.Face => "face", + EstType.Body => "top", + EstType.Head => "met", + _ => "unk", + }; +} diff --git a/Penumbra/Meta/Manipulations/EstManipulation.cs b/Penumbra/Meta/Manipulations/EstManipulation.cs deleted file mode 100644 index 09abbaa5..00000000 --- a/Penumbra/Meta/Manipulations/EstManipulation.cs +++ /dev/null @@ -1,108 +0,0 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; -using Penumbra.GameData.Enums; -using Penumbra.GameData.Structs; -using Penumbra.Interop.Structs; -using Penumbra.Meta.Files; - -namespace Penumbra.Meta.Manipulations; - -[StructLayout(LayoutKind.Sequential, Pack = 1)] -public readonly struct EstManipulation(EstIdentifier identifier, EstEntry entry) : IMetaManipulation -{ - public static string ToName(EstType type) - => type switch - { - EstType.Hair => "hair", - EstType.Face => "face", - EstType.Body => "top", - EstType.Head => "met", - _ => "unk", - }; - - [JsonIgnore] - public EstIdentifier Identifier { get; } = identifier; - public EstEntry Entry { get; } = entry; - - [JsonConverter(typeof(StringEnumConverter))] - public Gender Gender - => Identifier.Gender; - - [JsonConverter(typeof(StringEnumConverter))] - public ModelRace Race - => Identifier.Race; - - public PrimaryId SetId - => Identifier.SetId; - - [JsonConverter(typeof(StringEnumConverter))] - public EstType Slot - => Identifier.Slot; - - - [JsonConstructor] - public EstManipulation(Gender gender, ModelRace race, EstType slot, PrimaryId setId, EstEntry entry) - : this(new EstIdentifier(setId, slot, Names.CombinedRace(gender, race)), entry) - { } - - public EstManipulation Copy(EstEntry entry) - => new(Identifier, entry); - - - public override string ToString() - => $"Est - {SetId} - {Slot} - {Race.ToName()} {Gender.ToName()}"; - - public bool Equals(EstManipulation other) - => Gender == other.Gender - && Race == other.Race - && SetId == other.SetId - && Slot == other.Slot; - - public override bool Equals(object? obj) - => obj is EstManipulation other && Equals(other); - - public override int GetHashCode() - => HashCode.Combine((int)Gender, (int)Race, SetId, (int)Slot); - - public int CompareTo(EstManipulation other) - { - var r = Race.CompareTo(other.Race); - if (r != 0) - return r; - - var g = Gender.CompareTo(other.Gender); - if (g != 0) - return g; - - var s = Slot.CompareTo(other.Slot); - return s != 0 ? s : SetId.Id.CompareTo(other.SetId.Id); - } - - public MetaIndex FileIndex() - => (MetaIndex)Slot; - - public bool Apply(EstFile file) - { - return file.SetEntry(Names.CombinedRace(Gender, Race), SetId.Id, Entry) switch - { - EstFile.EstEntryChange.Unchanged => false, - EstFile.EstEntryChange.Changed => true, - EstFile.EstEntryChange.Added => true, - EstFile.EstEntryChange.Removed => true, - _ => throw new ArgumentOutOfRangeException(), - }; - } - - public bool Validate() - { - if (!Enum.IsDefined(Slot)) - return false; - if (Names.CombinedRace(Gender, Race) == GenderRace.Unknown) - return false; - - // No known check for set id or entry. - return true; - } -} - - diff --git a/Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs b/Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs index 94c892e2..2b88d962 100644 --- a/Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs +++ b/Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs @@ -1,11 +1,12 @@ using Newtonsoft.Json.Linq; using Penumbra.GameData.Data; +using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.Structs; namespace Penumbra.Meta.Manipulations; -public readonly struct GlobalEqpManipulation : IMetaManipulation, IMetaIdentifier +public readonly struct GlobalEqpManipulation : IMetaIdentifier { public GlobalEqpType Type { get; init; } public PrimaryId Condition { get; init; } @@ -70,8 +71,29 @@ public readonly struct GlobalEqpManipulation : IMetaManipulation $"Global EQP - {Type}{(Condition != 0 ? $" - {Condition.Id}" : string.Empty)}"; public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) - { } + { + var path = Type switch + { + GlobalEqpType.DoNotHideEarrings => GamePaths.Accessory.Mdl.Path(Condition, GenderRace.MidlanderMale, EquipSlot.Ears), + GlobalEqpType.DoNotHideNecklace => GamePaths.Accessory.Mdl.Path(Condition, GenderRace.MidlanderMale, EquipSlot.Neck), + GlobalEqpType.DoNotHideBracelets => GamePaths.Accessory.Mdl.Path(Condition, GenderRace.MidlanderMale, EquipSlot.Wrists), + GlobalEqpType.DoNotHideRingR => GamePaths.Accessory.Mdl.Path(Condition, GenderRace.MidlanderMale, EquipSlot.RFinger), + GlobalEqpType.DoNotHideRingL => GamePaths.Accessory.Mdl.Path(Condition, GenderRace.MidlanderMale, EquipSlot.LFinger), + GlobalEqpType.DoNotHideHrothgarHats => string.Empty, + GlobalEqpType.DoNotHideVieraHats => string.Empty, + _ => string.Empty, + }; + if (path.Length > 0) + identifier.Identify(changedItems, path); + else if (Type is GlobalEqpType.DoNotHideVieraHats) + changedItems["All Hats for Viera"] = null; + else if (Type is GlobalEqpType.DoNotHideHrothgarHats) + changedItems["All Hats for Hrothgar"] = null; + } public MetaIndex FileIndex() => MetaIndex.Eqp; + + MetaManipulationType IMetaIdentifier.Type + => MetaManipulationType.GlobalEqp; } diff --git a/Penumbra/Meta/Manipulations/Gmp.cs b/Penumbra/Meta/Manipulations/Gmp.cs index 1b7c70ba..a6fcf58b 100644 --- a/Penumbra/Meta/Manipulations/Gmp.cs +++ b/Penumbra/Meta/Manipulations/Gmp.cs @@ -36,4 +36,7 @@ public readonly record struct GmpIdentifier(PrimaryId SetId) : IMetaIdentifier, jObj["SetId"] = SetId.Id.ToString(); return jObj; } + + public MetaManipulationType Type + => MetaManipulationType.Gmp; } diff --git a/Penumbra/Meta/Manipulations/GmpManipulation.cs b/Penumbra/Meta/Manipulations/GmpManipulation.cs deleted file mode 100644 index 431f6325..00000000 --- a/Penumbra/Meta/Manipulations/GmpManipulation.cs +++ /dev/null @@ -1,58 +0,0 @@ -using Newtonsoft.Json; -using Penumbra.GameData.Structs; -using Penumbra.Interop.Structs; -using Penumbra.Meta.Files; - -namespace Penumbra.Meta.Manipulations; - -[StructLayout(LayoutKind.Sequential, Pack = 1)] -public readonly struct GmpManipulation(GmpIdentifier identifier, GmpEntry entry) : IMetaManipulation -{ - [JsonIgnore] - public GmpIdentifier Identifier { get; } = identifier; - - public GmpEntry Entry { get; } = entry; - - public PrimaryId SetId - => Identifier.SetId; - - [JsonConstructor] - public GmpManipulation(GmpEntry entry, PrimaryId setId) - : this(new GmpIdentifier(setId), entry) - { } - - public GmpManipulation Copy(GmpEntry entry) - => new(Identifier, entry); - - public override string ToString() - => $"Gmp - {SetId}"; - - public bool Equals(GmpManipulation other) - => SetId == other.SetId; - - public override bool Equals(object? obj) - => obj is GmpManipulation other && Equals(other); - - public override int GetHashCode() - => SetId.GetHashCode(); - - public int CompareTo(GmpManipulation other) - => SetId.Id.CompareTo(other.SetId.Id); - - public MetaIndex FileIndex() - => MetaIndex.Gmp; - - public bool Apply(ExpandedGmpFile file) - { - var entry = file[SetId]; - if (entry == Entry) - return false; - - file[SetId] = Entry; - return true; - } - - public bool Validate() - // No known conditions. - => true; -} diff --git a/Penumbra/Meta/Manipulations/IMetaIdentifier.cs b/Penumbra/Meta/Manipulations/IMetaIdentifier.cs index 4ad6bd3d..5707ffca 100644 --- a/Penumbra/Meta/Manipulations/IMetaIdentifier.cs +++ b/Penumbra/Meta/Manipulations/IMetaIdentifier.cs @@ -4,6 +4,18 @@ using Penumbra.Interop.Structs; namespace Penumbra.Meta.Manipulations; +public enum MetaManipulationType : byte +{ + Unknown = 0, + Imc = 1, + Eqdp = 2, + Eqp = 3, + Est = 4, + Gmp = 5, + Rsp = 6, + GlobalEqp = 7, +} + public interface IMetaIdentifier { public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems); @@ -13,4 +25,8 @@ public interface IMetaIdentifier public bool Validate(); public JObject AddToJson(JObject jObj); + + public MetaManipulationType Type { get; } + + public string ToString(); } diff --git a/Penumbra/Meta/Manipulations/Imc.cs b/Penumbra/Meta/Manipulations/Imc.cs index 2a2f4c03..44c60942 100644 --- a/Penumbra/Meta/Manipulations/Imc.cs +++ b/Penumbra/Meta/Manipulations/Imc.cs @@ -27,9 +27,6 @@ public readonly record struct ImcIdentifier( : this(primaryId, variant, slot.IsAccessory() ? ObjectType.Accessory : ObjectType.Equipment, 0, slot, BodySlot.Unknown) { } - public ImcManipulation ToManipulation(ImcEntry entry) - => new(ObjectType, BodySlot, PrimaryId, SecondaryId.Id, Variant.Id, EquipSlot, entry); - public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) => AddChangedItems(identifier, changedItems, false); @@ -193,4 +190,7 @@ public readonly record struct ImcIdentifier( jObj["BodySlot"] = BodySlot.ToString(); return jObj; } + + public MetaManipulationType Type + => MetaManipulationType.Imc; } diff --git a/Penumbra/Meta/Manipulations/ImcManipulation.cs b/Penumbra/Meta/Manipulations/ImcManipulation.cs deleted file mode 100644 index 5065a06e..00000000 --- a/Penumbra/Meta/Manipulations/ImcManipulation.cs +++ /dev/null @@ -1,108 +0,0 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; -using Penumbra.GameData.Enums; -using Penumbra.GameData.Structs; -using Penumbra.Interop.Structs; -using Penumbra.Meta.Files; -using Penumbra.String.Classes; - -namespace Penumbra.Meta.Manipulations; - -[StructLayout(LayoutKind.Sequential, Pack = 1)] -public readonly struct ImcManipulation : IMetaManipulation -{ - [JsonIgnore] - public ImcIdentifier Identifier { get; private init; } - - public ImcEntry Entry { get; private init; } - - - public PrimaryId PrimaryId - => Identifier.PrimaryId; - - public SecondaryId SecondaryId - => Identifier.SecondaryId; - - public Variant Variant - => Identifier.Variant; - - [JsonConverter(typeof(StringEnumConverter))] - public ObjectType ObjectType - => Identifier.ObjectType; - - [JsonConverter(typeof(StringEnumConverter))] - public EquipSlot EquipSlot - => Identifier.EquipSlot; - - [JsonConverter(typeof(StringEnumConverter))] - public BodySlot BodySlot - => Identifier.BodySlot; - - public ImcManipulation(EquipSlot equipSlot, ushort variant, PrimaryId primaryId, ImcEntry entry) - : this(new ImcIdentifier(equipSlot, primaryId, variant), entry) - { } - - public ImcManipulation(ImcIdentifier identifier, ImcEntry entry) - { - Identifier = identifier; - Entry = entry; - } - - - // Variants were initially ushorts but got shortened to bytes. - // There are still some manipulations around that have values > 255 for variant, - // so we change the unused value to something nonsensical in that case, just so they do not compare equal, - // and clamp the variant to 255. - [JsonConstructor] - internal ImcManipulation(ObjectType objectType, BodySlot bodySlot, PrimaryId primaryId, SecondaryId secondaryId, ushort variant, - EquipSlot equipSlot, ImcEntry entry) - { - Entry = entry; - var v = (Variant)Math.Clamp(variant, (ushort)0, byte.MaxValue); - Identifier = objectType switch - { - ObjectType.Accessory or ObjectType.Equipment => new ImcIdentifier(primaryId, v, objectType, 0, equipSlot, - variant > byte.MaxValue ? BodySlot.Body : BodySlot.Unknown), - ObjectType.DemiHuman => new ImcIdentifier(primaryId, v, objectType, secondaryId, equipSlot, variant > byte.MaxValue ? BodySlot.Body : BodySlot.Unknown), - _ => new ImcIdentifier(primaryId, v, objectType, secondaryId, equipSlot, bodySlot == BodySlot.Unknown ? BodySlot.Body : BodySlot.Unknown), - }; - } - - public ImcManipulation Copy(ImcEntry entry) - => new(Identifier, entry); - - public override string ToString() - => Identifier.ToString(); - - public bool Equals(ImcManipulation other) - => Identifier == other.Identifier; - - public override bool Equals(object? obj) - => obj is ImcManipulation other && Equals(other); - - public override int GetHashCode() - => Identifier.GetHashCode(); - - public int CompareTo(ImcManipulation other) - => Identifier.CompareTo(other.Identifier); - - public MetaIndex FileIndex() - => Identifier.FileIndex(); - - public Utf8GamePath GamePath() - => Identifier.GamePath(); - - public bool Apply(ImcFile file) - => file.SetEntry(ImcFile.PartIndex(EquipSlot), Variant.Id, Entry); - - public bool Validate(bool withMaterial) - { - if (!Identifier.Validate()) - return false; - - if (withMaterial && Entry.MaterialId == 0) - return false; - - return true; - } -} diff --git a/Penumbra/Meta/Manipulations/MetaDictionary.cs b/Penumbra/Meta/Manipulations/MetaDictionary.cs index 236157ae..5a51df83 100644 --- a/Penumbra/Meta/Manipulations/MetaDictionary.cs +++ b/Penumbra/Meta/Manipulations/MetaDictionary.cs @@ -1,5 +1,6 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using Penumbra.Collections.Cache; using Penumbra.GameData.Structs; using Penumbra.Util; using ImcEntry = Penumbra.GameData.Structs.ImcEntry; @@ -7,7 +8,7 @@ using ImcEntry = Penumbra.GameData.Structs.ImcEntry; namespace Penumbra.Meta.Manipulations; [JsonConverter(typeof(Converter))] -public class MetaDictionary : IEnumerable +public class MetaDictionary { private readonly Dictionary _imc = []; private readonly Dictionary _eqp = []; @@ -20,32 +21,50 @@ public class MetaDictionary : IEnumerable public IReadOnlyDictionary Imc => _imc; + public IReadOnlyDictionary Eqp + => _eqp; + + public IReadOnlyDictionary Eqdp + => _eqdp; + + public IReadOnlyDictionary Est + => _est; + + public IReadOnlyDictionary Gmp + => _gmp; + + public IReadOnlyDictionary Rsp + => _rsp; + + public IReadOnlySet GlobalEqp + => _globalEqp; + public int Count { get; private set; } - public int GetCount(MetaManipulation.Type type) + public int GetCount(MetaManipulationType type) => type switch { - MetaManipulation.Type.Imc => _imc.Count, - MetaManipulation.Type.Eqdp => _eqdp.Count, - MetaManipulation.Type.Eqp => _eqp.Count, - MetaManipulation.Type.Est => _est.Count, - MetaManipulation.Type.Gmp => _gmp.Count, - MetaManipulation.Type.Rsp => _rsp.Count, - MetaManipulation.Type.GlobalEqp => _globalEqp.Count, - _ => 0, + MetaManipulationType.Imc => _imc.Count, + MetaManipulationType.Eqdp => _eqdp.Count, + MetaManipulationType.Eqp => _eqp.Count, + MetaManipulationType.Est => _est.Count, + MetaManipulationType.Gmp => _gmp.Count, + MetaManipulationType.Rsp => _rsp.Count, + MetaManipulationType.GlobalEqp => _globalEqp.Count, + _ => 0, }; - public bool CanAdd(IMetaIdentifier identifier) + public bool Contains(IMetaIdentifier identifier) => identifier switch { - EqdpIdentifier eqdpIdentifier => !_eqdp.ContainsKey(eqdpIdentifier), - EqpIdentifier eqpIdentifier => !_eqp.ContainsKey(eqpIdentifier), - EstIdentifier estIdentifier => !_est.ContainsKey(estIdentifier), - GlobalEqpManipulation globalEqpManipulation => !_globalEqp.Contains(globalEqpManipulation), - GmpIdentifier gmpIdentifier => !_gmp.ContainsKey(gmpIdentifier), - ImcIdentifier imcIdentifier => !_imc.ContainsKey(imcIdentifier), - RspIdentifier rspIdentifier => !_rsp.ContainsKey(rspIdentifier), - _ => false, + EqdpIdentifier i => _eqdp.ContainsKey(i), + EqpIdentifier i => _eqp.ContainsKey(i), + EstIdentifier i => _est.ContainsKey(i), + GlobalEqpManipulation i => _globalEqp.Contains(i), + GmpIdentifier i => _gmp.ContainsKey(i), + ImcIdentifier i => _imc.ContainsKey(i), + RspIdentifier i => _rsp.ContainsKey(i), + _ => false, }; public void Clear() @@ -69,17 +88,16 @@ public class MetaDictionary : IEnumerable && _gmp.SetEquals(other._gmp) && _globalEqp.SetEquals(other._globalEqp); - public IEnumerator GetEnumerator() - => _imc.Select(kvp => new MetaManipulation(new ImcManipulation(kvp.Key, kvp.Value))) - .Concat(_eqp.Select(kvp => new MetaManipulation(new EqpManipulation(kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))))) - .Concat(_eqdp.Select(kvp => new MetaManipulation(new EqdpManipulation(kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))))) - .Concat(_est.Select(kvp => new MetaManipulation(new EstManipulation(kvp.Key, kvp.Value)))) - .Concat(_rsp.Select(kvp => new MetaManipulation(new RspManipulation(kvp.Key, kvp.Value)))) - .Concat(_gmp.Select(kvp => new MetaManipulation(new GmpManipulation(kvp.Key, kvp.Value)))) - .Concat(_globalEqp.Select(manip => new MetaManipulation(manip))).GetEnumerator(); + public IEnumerable Identifiers + => _imc.Keys.Cast() + .Concat(_eqdp.Keys.Cast()) + .Concat(_eqp.Keys.Cast()) + .Concat(_est.Keys.Cast()) + .Concat(_gmp.Keys.Cast()) + .Concat(_rsp.Keys.Cast()) + .Concat(_globalEqp.Cast()); - IEnumerator IEnumerable.GetEnumerator() - => GetEnumerator(); + #region TryAdd public bool TryAdd(ImcIdentifier identifier, ImcEntry entry) { @@ -90,7 +108,6 @@ public class MetaDictionary : IEnumerable return true; } - public bool TryAdd(EqpIdentifier identifier, EqpEntryInternal entry) { if (!_eqp.TryAdd(identifier, entry)) @@ -103,7 +120,6 @@ public class MetaDictionary : IEnumerable public bool TryAdd(EqpIdentifier identifier, EqpEntry entry) => TryAdd(identifier, new EqpEntryInternal(entry, identifier.Slot)); - public bool TryAdd(EqdpIdentifier identifier, EqdpEntryInternal entry) { if (!_eqdp.TryAdd(identifier, entry)) @@ -152,6 +168,10 @@ public class MetaDictionary : IEnumerable return true; } + #endregion + + #region Update + public bool Update(ImcIdentifier identifier, ImcEntry entry) { if (!_imc.ContainsKey(identifier)) @@ -161,7 +181,6 @@ public class MetaDictionary : IEnumerable return true; } - public bool Update(EqpIdentifier identifier, EqpEntryInternal entry) { if (!_eqp.ContainsKey(identifier)) @@ -174,7 +193,6 @@ public class MetaDictionary : IEnumerable public bool Update(EqpIdentifier identifier, EqpEntry entry) => Update(identifier, new EqpEntryInternal(entry, identifier.Slot)); - public bool Update(EqdpIdentifier identifier, EqdpEntryInternal entry) { if (!_eqdp.ContainsKey(identifier)) @@ -214,6 +232,50 @@ public class MetaDictionary : IEnumerable return true; } + #endregion + + #region TryGetValue + + public bool TryGetValue(EstIdentifier identifier, out EstEntry value) + => _est.TryGetValue(identifier, out value); + + public bool TryGetValue(EqpIdentifier identifier, out EqpEntryInternal value) + => _eqp.TryGetValue(identifier, out value); + + public bool TryGetValue(EqdpIdentifier identifier, out EqdpEntryInternal value) + => _eqdp.TryGetValue(identifier, out value); + + public bool TryGetValue(GmpIdentifier identifier, out GmpEntry value) + => _gmp.TryGetValue(identifier, out value); + + public bool TryGetValue(RspIdentifier identifier, out RspEntry value) + => _rsp.TryGetValue(identifier, out value); + + public bool TryGetValue(ImcIdentifier identifier, out ImcEntry value) + => _imc.TryGetValue(identifier, out value); + + #endregion + + public bool Remove(IMetaIdentifier identifier) + { + var ret = identifier switch + { + EqdpIdentifier i => _eqdp.Remove(i), + EqpIdentifier i => _eqp.Remove(i), + EstIdentifier i => _est.Remove(i), + GlobalEqpManipulation i => _globalEqp.Remove(i), + GmpIdentifier i => _gmp.Remove(i), + ImcIdentifier i => _imc.Remove(i), + RspIdentifier i => _rsp.Remove(i), + _ => false, + }; + if (ret) + --Count; + return ret; + } + + #region Merging + public void UnionWith(MetaDictionary manips) { foreach (var (identifier, entry) in manips._imc) @@ -287,24 +349,6 @@ public class MetaDictionary : IEnumerable return false; } - public bool TryGetValue(EstIdentifier identifier, out EstEntry value) - => _est.TryGetValue(identifier, out value); - - public bool TryGetValue(EqpIdentifier identifier, out EqpEntryInternal value) - => _eqp.TryGetValue(identifier, out value); - - public bool TryGetValue(EqdpIdentifier identifier, out EqdpEntryInternal value) - => _eqdp.TryGetValue(identifier, out value); - - public bool TryGetValue(GmpIdentifier identifier, out GmpEntry value) - => _gmp.TryGetValue(identifier, out value); - - public bool TryGetValue(RspIdentifier identifier, out RspEntry value) - => _rsp.TryGetValue(identifier, out value); - - public bool TryGetValue(ImcIdentifier identifier, out ImcEntry value) - => _imc.TryGetValue(identifier, out value); - public void SetTo(MetaDictionary other) { _imc.SetTo(other._imc); @@ -329,6 +373,8 @@ public class MetaDictionary : IEnumerable Count = _imc.Count + _eqp.Count + _eqdp.Count + _est.Count + _rsp.Count + _gmp.Count + _globalEqp.Count; } + #endregion + public MetaDictionary Clone() { var ret = new MetaDictionary(); @@ -336,29 +382,124 @@ public class MetaDictionary : IEnumerable return ret; } - private static void WriteJson(JsonWriter writer, JsonSerializer serializer, IMetaIdentifier identifier, object entry) - { - var type = identifier switch + public static JObject Serialize(EqpIdentifier identifier, EqpEntryInternal entry) + => Serialize(identifier, entry.ToEntry(identifier.Slot)); + + public static JObject Serialize(EqpIdentifier identifier, EqpEntry entry) + => new() { - ImcIdentifier => "Imc", - EqdpIdentifier => "Eqdp", - EqpIdentifier => "Eqp", - EstIdentifier => "Est", - GmpIdentifier => "Gmp", - RspIdentifier => "Rsp", - GlobalEqpManipulation => "GlobalEqp", - _ => string.Empty, + ["Type"] = MetaManipulationType.Eqp.ToString(), + ["Manipulation"] = identifier.AddToJson(new JObject + { + ["Entry"] = (ulong)entry, + }), }; - if (type.Length == 0) - return; + public static JObject Serialize(EqdpIdentifier identifier, EqdpEntryInternal entry) + => Serialize(identifier, entry.ToEntry(identifier.Slot)); - writer.WriteStartObject(); - writer.WritePropertyName("Type"); - writer.WriteValue(type); - writer.WritePropertyName("Manipulation"); + public static JObject Serialize(EqdpIdentifier identifier, EqdpEntry entry) + => new() + { + ["Type"] = MetaManipulationType.Eqdp.ToString(), + ["Manipulation"] = identifier.AddToJson(new JObject + { + ["Entry"] = (ushort)entry, + }), + }; - writer.WriteEndObject(); + public static JObject Serialize(EstIdentifier identifier, EstEntry entry) + => new() + { + ["Type"] = MetaManipulationType.Est.ToString(), + ["Manipulation"] = identifier.AddToJson(new JObject + { + ["Entry"] = entry.Value, + }), + }; + + public static JObject Serialize(GmpIdentifier identifier, GmpEntry entry) + => new() + { + ["Type"] = MetaManipulationType.Gmp.ToString(), + ["Manipulation"] = identifier.AddToJson(new JObject + { + ["Entry"] = JObject.FromObject(entry), + }), + }; + + public static JObject Serialize(ImcIdentifier identifier, ImcEntry entry) + => new() + { + ["Type"] = MetaManipulationType.Imc.ToString(), + ["Manipulation"] = identifier.AddToJson(new JObject + { + ["Entry"] = JObject.FromObject(entry), + }), + }; + + public static JObject Serialize(RspIdentifier identifier, RspEntry entry) + => new() + { + ["Type"] = MetaManipulationType.Rsp.ToString(), + ["Manipulation"] = identifier.AddToJson(new JObject + { + ["Entry"] = entry.Value, + }), + }; + + public static JObject Serialize(GlobalEqpManipulation identifier) + => new() + { + ["Type"] = MetaManipulationType.GlobalEqp.ToString(), + ["Manipulation"] = identifier.AddToJson(new JObject()), + }; + + public static JObject? Serialize(TIdentifier identifier, TEntry entry) + where TIdentifier : unmanaged, IMetaIdentifier + where TEntry : unmanaged + { + if (typeof(TIdentifier) == typeof(EqpIdentifier) && typeof(TEntry) == typeof(EqpEntryInternal)) + return Serialize(Unsafe.As(ref identifier), Unsafe.As(ref entry)); + if (typeof(TIdentifier) == typeof(EqpIdentifier) && typeof(TEntry) == typeof(EqpEntry)) + return Serialize(Unsafe.As(ref identifier), Unsafe.As(ref entry)); + if (typeof(TIdentifier) == typeof(EqdpIdentifier) && typeof(TEntry) == typeof(EqdpEntryInternal)) + return Serialize(Unsafe.As(ref identifier), Unsafe.As(ref entry)); + if (typeof(TIdentifier) == typeof(EqdpIdentifier) && typeof(TEntry) == typeof(EqdpEntry)) + return Serialize(Unsafe.As(ref identifier), Unsafe.As(ref entry)); + if (typeof(TIdentifier) == typeof(EstIdentifier) && typeof(TEntry) == typeof(EstEntry)) + return Serialize(Unsafe.As(ref identifier), Unsafe.As(ref entry)); + if (typeof(TIdentifier) == typeof(GmpIdentifier) && typeof(TEntry) == typeof(GmpEntry)) + return Serialize(Unsafe.As(ref identifier), Unsafe.As(ref entry)); + if (typeof(TIdentifier) == typeof(RspIdentifier) && typeof(TEntry) == typeof(RspEntry)) + return Serialize(Unsafe.As(ref identifier), Unsafe.As(ref entry)); + if (typeof(TIdentifier) == typeof(ImcIdentifier) && typeof(TEntry) == typeof(ImcEntry)) + return Serialize(Unsafe.As(ref identifier), Unsafe.As(ref entry)); + if (typeof(TIdentifier) == typeof(GlobalEqpManipulation)) + return Serialize(Unsafe.As(ref identifier)); + + return null; + } + + public static JArray SerializeTo(JArray array, IEnumerable> manipulations) + where TIdentifier : unmanaged, IMetaIdentifier + where TEntry : unmanaged + { + foreach (var (identifier, entry) in manipulations) + { + if (Serialize(identifier, entry) is { } jObj) + array.Add(jObj); + } + + return array; + } + + public static JArray SerializeTo(JArray array, IEnumerable manipulations) + { + foreach (var manip in manipulations) + array.Add(Serialize(manip)); + + return array; } private class Converter : JsonConverter @@ -371,30 +512,27 @@ public class MetaDictionary : IEnumerable return; } - writer.WriteStartArray(); - foreach (var item in value) - { - writer.WriteStartObject(); - writer.WritePropertyName("Type"); - writer.WriteValue(item.ManipulationType.ToString()); - writer.WritePropertyName("Manipulation"); - serializer.Serialize(writer, item.Manipulation); - writer.WriteEndObject(); - } - - writer.WriteEndArray(); + var array = new JArray(); + SerializeTo(array, value._imc); + SerializeTo(array, value._eqp); + SerializeTo(array, value._eqdp); + SerializeTo(array, value._est); + SerializeTo(array, value._rsp); + SerializeTo(array, value._gmp); + SerializeTo(array, value._globalEqp); + array.WriteTo(writer); } public override MetaDictionary ReadJson(JsonReader reader, Type objectType, MetaDictionary? existingValue, bool hasExistingValue, JsonSerializer serializer) { - var dict = existingValue ?? []; + var dict = existingValue ?? new MetaDictionary(); dict.Clear(); var jObj = JArray.Load(reader); foreach (var item in jObj) { - var type = item["Type"]?.ToObject() ?? MetaManipulation.Type.Unknown; - if (type is MetaManipulation.Type.Unknown) + var type = item["Type"]?.ToObject() ?? MetaManipulationType.Unknown; + if (type is MetaManipulationType.Unknown) { Penumbra.Log.Warning($"Invalid Meta Manipulation Type {type} encountered."); continue; @@ -408,7 +546,7 @@ public class MetaDictionary : IEnumerable switch (type) { - case MetaManipulation.Type.Imc: + case MetaManipulationType.Imc: { var identifier = ImcIdentifier.FromJson(manip); var entry = manip["Entry"]?.ToObject(); @@ -418,7 +556,7 @@ public class MetaDictionary : IEnumerable Penumbra.Log.Warning("Invalid IMC Manipulation encountered."); break; } - case MetaManipulation.Type.Eqdp: + case MetaManipulationType.Eqdp: { var identifier = EqdpIdentifier.FromJson(manip); var entry = (EqdpEntry?)manip["Entry"]?.ToObject(); @@ -428,7 +566,7 @@ public class MetaDictionary : IEnumerable Penumbra.Log.Warning("Invalid EQDP Manipulation encountered."); break; } - case MetaManipulation.Type.Eqp: + case MetaManipulationType.Eqp: { var identifier = EqpIdentifier.FromJson(manip); var entry = (EqpEntry?)manip["Entry"]?.ToObject(); @@ -438,7 +576,7 @@ public class MetaDictionary : IEnumerable Penumbra.Log.Warning("Invalid EQP Manipulation encountered."); break; } - case MetaManipulation.Type.Est: + case MetaManipulationType.Est: { var identifier = EstIdentifier.FromJson(manip); var entry = manip["Entry"]?.ToObject(); @@ -448,7 +586,7 @@ public class MetaDictionary : IEnumerable Penumbra.Log.Warning("Invalid EST Manipulation encountered."); break; } - case MetaManipulation.Type.Gmp: + case MetaManipulationType.Gmp: { var identifier = GmpIdentifier.FromJson(manip); var entry = manip["Entry"]?.ToObject(); @@ -458,7 +596,7 @@ public class MetaDictionary : IEnumerable Penumbra.Log.Warning("Invalid GMP Manipulation encountered."); break; } - case MetaManipulation.Type.Rsp: + case MetaManipulationType.Rsp: { var identifier = RspIdentifier.FromJson(manip); var entry = manip["Entry"]?.ToObject(); @@ -468,7 +606,7 @@ public class MetaDictionary : IEnumerable Penumbra.Log.Warning("Invalid RSP Manipulation encountered."); break; } - case MetaManipulation.Type.GlobalEqp: + case MetaManipulationType.GlobalEqp: { var identifier = GlobalEqpManipulation.FromJson(manip); if (identifier.HasValue) @@ -483,4 +621,22 @@ public class MetaDictionary : IEnumerable return dict; } } + + public MetaDictionary() + { } + + public MetaDictionary(MetaCache? cache) + { + if (cache == null) + return; + + _imc = cache.Imc.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Entry); + _eqp = cache.Eqp.ToDictionary(kvp => kvp.Key, kvp => new EqpEntryInternal(kvp.Value.Entry, kvp.Key.Slot)); + _eqdp = cache.Eqdp.ToDictionary(kvp => kvp.Key, kvp => new EqdpEntryInternal(kvp.Value.Entry, kvp.Key.Slot)); + _est = cache.Est.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Entry); + _gmp = cache.Gmp.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Entry); + _rsp = cache.Rsp.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Entry); + _globalEqp = cache.GlobalEqp.Select(kvp => kvp.Key).ToHashSet(); + Count = cache.Count; + } } diff --git a/Penumbra/Meta/Manipulations/MetaManipulation.cs b/Penumbra/Meta/Manipulations/MetaManipulation.cs deleted file mode 100644 index b80681d2..00000000 --- a/Penumbra/Meta/Manipulations/MetaManipulation.cs +++ /dev/null @@ -1,457 +0,0 @@ -using Dalamud.Interface; -using ImGuiNET; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; -using OtterGui; -using Penumbra.GameData.Enums; -using Penumbra.Interop.Structs; -using Penumbra.Mods.Editor; -using Penumbra.String.Functions; -using Penumbra.UI; -using Penumbra.UI.ModsTab; - -namespace Penumbra.Meta.Manipulations; - -#if false -private static class ImcRow -{ - private static ImcIdentifier _newIdentifier = ImcIdentifier.Default; - - private static float IdWidth - => 80 * UiHelpers.Scale; - - private static float SmallIdWidth - => 45 * UiHelpers.Scale; - - public static void DrawNew(MetaFileManager metaFileManager, ModEditor editor, Vector2 iconSize) - { - ImGui.TableNextColumn(); - CopyToClipboardButton("Copy all current IMC manipulations to clipboard.", iconSize, - editor.MetaEditor.Imc.Select(m => (MetaManipulation)m)); - ImGui.TableNextColumn(); - var (defaultEntry, fileExists, _) = metaFileManager.ImcChecker.GetDefaultEntry(_newIdentifier, true); - var manip = (MetaManipulation)new ImcManipulation(_newIdentifier, defaultEntry); - var canAdd = fileExists && editor.MetaEditor.CanAdd(manip); - var tt = canAdd ? "Stage this edit." : !fileExists ? "This IMC file does not exist." : "This entry is already edited."; - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) - editor.MetaEditor.Add(manip); - - // Identifier - ImGui.TableNextColumn(); - var change = ImcManipulationDrawer.DrawObjectType(ref _newIdentifier); - - ImGui.TableNextColumn(); - change |= ImcManipulationDrawer.DrawPrimaryId(ref _newIdentifier); - using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, - new Vector2(3 * UiHelpers.Scale, ImGui.GetStyle().ItemSpacing.Y)); - - ImGui.TableNextColumn(); - // Equipment and accessories are slightly different imcs than other types. - if (_newIdentifier.ObjectType is ObjectType.Equipment or ObjectType.Accessory) - change |= ImcManipulationDrawer.DrawSlot(ref _newIdentifier); - else - change |= ImcManipulationDrawer.DrawSecondaryId(ref _newIdentifier); - - ImGui.TableNextColumn(); - change |= ImcManipulationDrawer.DrawVariant(ref _newIdentifier); - - ImGui.TableNextColumn(); - if (_newIdentifier.ObjectType is ObjectType.DemiHuman) - change |= ImcManipulationDrawer.DrawSlot(ref _newIdentifier, 70); - else - ImUtf8.ScaledDummy(new Vector2(70 * UiHelpers.Scale, 0)); - - if (change) - defaultEntry = metaFileManager.ImcChecker.GetDefaultEntry(_newIdentifier, true).Entry; - // Values - using var disabled = ImRaii.Disabled(); - ImGui.TableNextColumn(); - ImcManipulationDrawer.DrawMaterialId(defaultEntry, ref defaultEntry, false); - ImGui.SameLine(); - ImcManipulationDrawer.DrawMaterialAnimationId(defaultEntry, ref defaultEntry, false); - ImGui.TableNextColumn(); - ImcManipulationDrawer.DrawDecalId(defaultEntry, ref defaultEntry, false); - ImGui.SameLine(); - ImcManipulationDrawer.DrawVfxId(defaultEntry, ref defaultEntry, false); - ImGui.SameLine(); - ImcManipulationDrawer.DrawSoundId(defaultEntry, ref defaultEntry, false); - ImGui.TableNextColumn(); - ImcManipulationDrawer.DrawAttributes(defaultEntry, ref defaultEntry); - } - - public static void Draw(MetaFileManager metaFileManager, ImcManipulation meta, ModEditor editor, Vector2 iconSize) - { - DrawMetaButtons(meta, editor, iconSize); - - // Identifier - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.ObjectType.ToName()); - ImGuiUtil.HoverTooltip(ObjectTypeTooltip); - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.PrimaryId.ToString()); - ImGuiUtil.HoverTooltip(PrimaryIdTooltipShort); - - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - if (meta.ObjectType is ObjectType.Equipment or ObjectType.Accessory) - { - ImGui.TextUnformatted(meta.EquipSlot.ToName()); - ImGuiUtil.HoverTooltip(EquipSlotTooltip); - } - else - { - ImGui.TextUnformatted(meta.SecondaryId.ToString()); - ImGuiUtil.HoverTooltip(SecondaryIdTooltip); - } - - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.Variant.ToString()); - ImGuiUtil.HoverTooltip(VariantIdTooltip); - - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - if (meta.ObjectType is ObjectType.DemiHuman) - { - ImGui.TextUnformatted(meta.EquipSlot.ToName()); - ImGuiUtil.HoverTooltip(EquipSlotTooltip); - } - - // Values - using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, - new Vector2(3 * UiHelpers.Scale, ImGui.GetStyle().ItemSpacing.Y)); - ImGui.TableNextColumn(); - var defaultEntry = metaFileManager.ImcChecker.GetDefaultEntry(meta.Identifier, true).Entry; - var newEntry = meta.Entry; - var changes = ImcManipulationDrawer.DrawMaterialId(defaultEntry, ref newEntry, true); - ImGui.SameLine(); - changes |= ImcManipulationDrawer.DrawMaterialAnimationId(defaultEntry, ref newEntry, true); - ImGui.TableNextColumn(); - changes |= ImcManipulationDrawer.DrawDecalId(defaultEntry, ref newEntry, true); - ImGui.SameLine(); - changes |= ImcManipulationDrawer.DrawVfxId(defaultEntry, ref newEntry, true); - ImGui.SameLine(); - changes |= ImcManipulationDrawer.DrawSoundId(defaultEntry, ref newEntry, true); - ImGui.TableNextColumn(); - changes |= ImcManipulationDrawer.DrawAttributes(defaultEntry, ref newEntry); - - if (changes) - editor.MetaEditor.Change(meta.Copy(newEntry)); - } -} - -#endif - -public interface IMetaManipulation -{ - public MetaIndex FileIndex(); -} - -public interface IMetaManipulation - : IMetaManipulation, IComparable, IEquatable where T : struct -{ } - -[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 16)] -public readonly struct MetaManipulation : IEquatable, IComparable -{ - public const int CurrentVersion = 0; - - public enum Type : byte - { - Unknown = 0, - Imc = 1, - Eqdp = 2, - Eqp = 3, - Est = 4, - Gmp = 5, - Rsp = 6, - GlobalEqp = 7, - } - - [FieldOffset(0)] - [JsonIgnore] - public readonly EqpManipulation Eqp = default; - - [FieldOffset(0)] - [JsonIgnore] - public readonly GmpManipulation Gmp = default; - - [FieldOffset(0)] - [JsonIgnore] - public readonly EqdpManipulation Eqdp = default; - - [FieldOffset(0)] - [JsonIgnore] - public readonly EstManipulation Est = default; - - [FieldOffset(0)] - [JsonIgnore] - public readonly RspManipulation Rsp = default; - - [FieldOffset(0)] - [JsonIgnore] - public readonly ImcManipulation Imc = default; - - [FieldOffset(0)] - [JsonIgnore] - public readonly GlobalEqpManipulation GlobalEqp = default; - - [FieldOffset(15)] - [JsonConverter(typeof(StringEnumConverter))] - [JsonProperty("Type")] - public readonly Type ManipulationType; - - public object? Manipulation - { - get => ManipulationType switch - { - Type.Unknown => null, - Type.Imc => Imc, - Type.Eqdp => Eqdp, - Type.Eqp => Eqp, - Type.Est => Est, - Type.Gmp => Gmp, - Type.Rsp => Rsp, - Type.GlobalEqp => GlobalEqp, - _ => null, - }; - init - { - switch (value) - { - case EqpManipulation m: - Eqp = m; - ManipulationType = m.Validate() ? Type.Eqp : Type.Unknown; - return; - case EqdpManipulation m: - Eqdp = m; - ManipulationType = m.Validate() ? Type.Eqdp : Type.Unknown; - return; - case GmpManipulation m: - Gmp = m; - ManipulationType = m.Validate() ? Type.Gmp : Type.Unknown; - return; - case EstManipulation m: - Est = m; - ManipulationType = m.Validate() ? Type.Est : Type.Unknown; - return; - case RspManipulation m: - Rsp = m; - ManipulationType = m.Validate() ? Type.Rsp : Type.Unknown; - return; - case ImcManipulation m: - Imc = m; - ManipulationType = m.Validate(true) ? Type.Imc : Type.Unknown; - return; - case GlobalEqpManipulation m: - GlobalEqp = m; - ManipulationType = m.Validate() ? Type.GlobalEqp : Type.Unknown; - return; - } - } - } - - public bool Validate() - { - return ManipulationType switch - { - Type.Imc => Imc.Validate(true), - Type.Eqdp => Eqdp.Validate(), - Type.Eqp => Eqp.Validate(), - Type.Est => Est.Validate(), - Type.Gmp => Gmp.Validate(), - Type.Rsp => Rsp.Validate(), - Type.GlobalEqp => GlobalEqp.Validate(), - _ => false, - }; - } - - public MetaManipulation(EqpManipulation eqp) - { - Eqp = eqp; - ManipulationType = Type.Eqp; - } - - public MetaManipulation(GmpManipulation gmp) - { - Gmp = gmp; - ManipulationType = Type.Gmp; - } - - public MetaManipulation(EqdpManipulation eqdp) - { - Eqdp = eqdp; - ManipulationType = Type.Eqdp; - } - - public MetaManipulation(EstManipulation est) - { - Est = est; - ManipulationType = Type.Est; - } - - public MetaManipulation(RspManipulation rsp) - { - Rsp = rsp; - ManipulationType = Type.Rsp; - } - - public MetaManipulation(ImcManipulation imc) - { - Imc = imc; - ManipulationType = Type.Imc; - } - - public MetaManipulation(GlobalEqpManipulation eqp) - { - GlobalEqp = eqp; - ManipulationType = Type.GlobalEqp; - } - - public static implicit operator MetaManipulation(EqpManipulation eqp) - => new(eqp); - - public static implicit operator MetaManipulation(GmpManipulation gmp) - => new(gmp); - - public static implicit operator MetaManipulation(EqdpManipulation eqdp) - => new(eqdp); - - public static implicit operator MetaManipulation(EstManipulation est) - => new(est); - - public static implicit operator MetaManipulation(RspManipulation rsp) - => new(rsp); - - public static implicit operator MetaManipulation(ImcManipulation imc) - => new(imc); - - public static implicit operator MetaManipulation(GlobalEqpManipulation eqp) - => new(eqp); - - public bool EntryEquals(MetaManipulation other) - { - if (ManipulationType != other.ManipulationType) - return false; - - return ManipulationType switch - { - Type.Eqp => Eqp.Entry.Equals(other.Eqp.Entry), - Type.Gmp => Gmp.Entry.Equals(other.Gmp.Entry), - Type.Eqdp => Eqdp.Entry.Equals(other.Eqdp.Entry), - Type.Est => Est.Entry.Equals(other.Est.Entry), - Type.Rsp => Rsp.Entry.Equals(other.Rsp.Entry), - Type.Imc => Imc.Entry.Equals(other.Imc.Entry), - Type.GlobalEqp => true, - _ => throw new ArgumentOutOfRangeException(), - }; - } - - public bool Equals(MetaManipulation other) - { - if (ManipulationType != other.ManipulationType) - return false; - - return ManipulationType switch - { - Type.Eqp => Eqp.Equals(other.Eqp), - Type.Gmp => Gmp.Equals(other.Gmp), - Type.Eqdp => Eqdp.Equals(other.Eqdp), - Type.Est => Est.Equals(other.Est), - Type.Rsp => Rsp.Equals(other.Rsp), - Type.Imc => Imc.Equals(other.Imc), - Type.GlobalEqp => GlobalEqp.Equals(other.GlobalEqp), - _ => false, - }; - } - - public MetaManipulation WithEntryOf(MetaManipulation other) - { - if (ManipulationType != other.ManipulationType) - return this; - - return ManipulationType switch - { - Type.Eqp => Eqp.Copy(other.Eqp.Entry), - Type.Gmp => Gmp.Copy(other.Gmp.Entry), - Type.Eqdp => Eqdp.Copy(other.Eqdp), - Type.Est => Est.Copy(other.Est.Entry), - Type.Rsp => Rsp.Copy(other.Rsp.Entry), - Type.Imc => Imc.Copy(other.Imc.Entry), - Type.GlobalEqp => GlobalEqp, - _ => throw new ArgumentOutOfRangeException(), - }; - } - - public override bool Equals(object? obj) - => obj is MetaManipulation other && Equals(other); - - public override int GetHashCode() - => ManipulationType switch - { - Type.Eqp => Eqp.GetHashCode(), - Type.Gmp => Gmp.GetHashCode(), - Type.Eqdp => Eqdp.GetHashCode(), - Type.Est => Est.GetHashCode(), - Type.Rsp => Rsp.GetHashCode(), - Type.Imc => Imc.GetHashCode(), - Type.GlobalEqp => GlobalEqp.GetHashCode(), - _ => 0, - }; - - public unsafe int CompareTo(MetaManipulation other) - { - fixed (MetaManipulation* lhs = &this) - { - return MemoryUtility.MemCmpUnchecked(lhs, &other, sizeof(MetaManipulation)); - } - } - - public override string ToString() - => ManipulationType switch - { - Type.Eqp => Eqp.ToString(), - Type.Gmp => Gmp.ToString(), - Type.Eqdp => Eqdp.ToString(), - Type.Est => Est.ToString(), - Type.Rsp => Rsp.ToString(), - Type.Imc => Imc.ToString(), - Type.GlobalEqp => GlobalEqp.ToString(), - _ => "Invalid", - }; - - public string EntryToString() - => ManipulationType switch - { - Type.Imc => - $"{Imc.Entry.DecalId}-{Imc.Entry.MaterialId}-{Imc.Entry.VfxId}-{Imc.Entry.SoundId}-{Imc.Entry.MaterialAnimationId}-{Imc.Entry.AttributeMask}", - Type.Eqdp => $"{(ushort)Eqdp.Entry:X}", - Type.Eqp => $"{(ulong)Eqp.Entry:X}", - Type.Est => $"{Est.Entry}", - Type.Gmp => $"{Gmp.Entry.Value}", - Type.Rsp => $"{Rsp.Entry}", - Type.GlobalEqp => string.Empty, - _ => string.Empty, - }; - - public static bool operator ==(MetaManipulation left, MetaManipulation right) - => left.Equals(right); - - public static bool operator !=(MetaManipulation left, MetaManipulation right) - => !(left == right); - - public static bool operator <(MetaManipulation left, MetaManipulation right) - => left.CompareTo(right) < 0; - - public static bool operator <=(MetaManipulation left, MetaManipulation right) - => left.CompareTo(right) <= 0; - - public static bool operator >(MetaManipulation left, MetaManipulation right) - => left.CompareTo(right) > 0; - - public static bool operator >=(MetaManipulation left, MetaManipulation right) - => left.CompareTo(right) >= 0; -} - diff --git a/Penumbra/Meta/Manipulations/Rsp.cs b/Penumbra/Meta/Manipulations/Rsp.cs index ca7cb1c5..73d1d7e5 100644 --- a/Penumbra/Meta/Manipulations/Rsp.cs +++ b/Penumbra/Meta/Manipulations/Rsp.cs @@ -38,6 +38,9 @@ public readonly record struct RspIdentifier(SubRace SubRace, RspAttribute Attrib var ret = new RspIdentifier(subRace, attribute); return ret.Validate() ? ret : null; } + + public MetaManipulationType Type + => MetaManipulationType.Rsp; } [JsonConverter(typeof(Converter))] diff --git a/Penumbra/Meta/Manipulations/RspManipulation.cs b/Penumbra/Meta/Manipulations/RspManipulation.cs deleted file mode 100644 index e2282c41..00000000 --- a/Penumbra/Meta/Manipulations/RspManipulation.cs +++ /dev/null @@ -1,67 +0,0 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; -using Penumbra.GameData.Enums; -using Penumbra.Interop.Structs; -using Penumbra.Meta.Files; - -namespace Penumbra.Meta.Manipulations; - -[StructLayout(LayoutKind.Sequential, Pack = 1)] -public readonly struct RspManipulation(RspIdentifier identifier, RspEntry entry) : IMetaManipulation -{ - [JsonIgnore] - public RspIdentifier Identifier { get; } = identifier; - - public RspEntry Entry { get; } = entry; - - [JsonConverter(typeof(StringEnumConverter))] - public SubRace SubRace - => Identifier.SubRace; - - [JsonConverter(typeof(StringEnumConverter))] - public RspAttribute Attribute - => Identifier.Attribute; - - [JsonConstructor] - public RspManipulation(SubRace subRace, RspAttribute attribute, RspEntry entry) - : this(new RspIdentifier(subRace, attribute), entry) - { } - - public RspManipulation Copy(RspEntry entry) - => new(Identifier, entry); - - public override string ToString() - => $"Rsp - {SubRace.ToName()} - {Attribute.ToFullString()}"; - - public bool Equals(RspManipulation other) - => SubRace == other.SubRace - && Attribute == other.Attribute; - - public override bool Equals(object? obj) - => obj is RspManipulation other && Equals(other); - - public override int GetHashCode() - => HashCode.Combine((int)SubRace, (int)Attribute); - - public int CompareTo(RspManipulation other) - { - var s = SubRace.CompareTo(other.SubRace); - return s != 0 ? s : Attribute.CompareTo(other.Attribute); - } - - public MetaIndex FileIndex() - => MetaIndex.HumanCmp; - - public bool Apply(CmpFile file) - { - var value = file[SubRace, Attribute]; - if (value == Entry) - return false; - - file[SubRace, Attribute] = Entry; - return true; - } - - public bool Validate() - => Identifier.Validate() && Entry.Validate(); -} diff --git a/Penumbra/Mods/Editor/IMod.cs b/Penumbra/Mods/Editor/IMod.cs index 06c31846..3da38829 100644 --- a/Penumbra/Mods/Editor/IMod.cs +++ b/Penumbra/Mods/Editor/IMod.cs @@ -10,7 +10,7 @@ public record struct AppliedModData( Dictionary FileRedirections, MetaDictionary Manipulations) { - public static readonly AppliedModData Empty = new([], []); + public static readonly AppliedModData Empty = new([], new MetaDictionary()); } public interface IMod diff --git a/Penumbra/Mods/Editor/ModMetaEditor.cs b/Penumbra/Mods/Editor/ModMetaEditor.cs index 42171378..bacf4122 100644 --- a/Penumbra/Mods/Editor/ModMetaEditor.cs +++ b/Penumbra/Mods/Editor/ModMetaEditor.cs @@ -26,20 +26,20 @@ public class ModMetaEditor(ModManager modManager) : MetaDictionary, IService } } - public readonly FrozenDictionary OtherData = - Enum.GetValues().ToFrozenDictionary(t => t, _ => new OtherOptionData()); + public readonly FrozenDictionary OtherData = + Enum.GetValues().ToFrozenDictionary(t => t, _ => new OtherOptionData()); - public bool Changes { get; private set; } + public bool Changes { get; set; } public new void Clear() { + Changes = Count > 0; base.Clear(); - Changes = true; } public void Load(Mod mod, IModDataContainer currentOption) { - foreach (var type in Enum.GetValues()) + foreach (var type in Enum.GetValues()) OtherData[type].Clear(); foreach (var option in mod.AllDataContainers) @@ -48,13 +48,13 @@ public class ModMetaEditor(ModManager modManager) : MetaDictionary, IService continue; var name = option.GetFullName(); - OtherData[MetaManipulation.Type.Imc].Add(name, option.Manipulations.GetCount(MetaManipulation.Type.Imc)); - OtherData[MetaManipulation.Type.Eqp].Add(name, option.Manipulations.GetCount(MetaManipulation.Type.Eqp)); - OtherData[MetaManipulation.Type.Eqdp].Add(name, option.Manipulations.GetCount(MetaManipulation.Type.Eqdp)); - OtherData[MetaManipulation.Type.Gmp].Add(name, option.Manipulations.GetCount(MetaManipulation.Type.Gmp)); - OtherData[MetaManipulation.Type.Est].Add(name, option.Manipulations.GetCount(MetaManipulation.Type.Est)); - OtherData[MetaManipulation.Type.Rsp].Add(name, option.Manipulations.GetCount(MetaManipulation.Type.Rsp)); - OtherData[MetaManipulation.Type.GlobalEqp].Add(name, option.Manipulations.GetCount(MetaManipulation.Type.GlobalEqp)); + OtherData[MetaManipulationType.Imc].Add(name, option.Manipulations.GetCount(MetaManipulationType.Imc)); + OtherData[MetaManipulationType.Eqp].Add(name, option.Manipulations.GetCount(MetaManipulationType.Eqp)); + OtherData[MetaManipulationType.Eqdp].Add(name, option.Manipulations.GetCount(MetaManipulationType.Eqdp)); + OtherData[MetaManipulationType.Gmp].Add(name, option.Manipulations.GetCount(MetaManipulationType.Gmp)); + OtherData[MetaManipulationType.Est].Add(name, option.Manipulations.GetCount(MetaManipulationType.Est)); + OtherData[MetaManipulationType.Rsp].Add(name, option.Manipulations.GetCount(MetaManipulationType.Rsp)); + OtherData[MetaManipulationType.GlobalEqp].Add(name, option.Manipulations.GetCount(MetaManipulationType.GlobalEqp)); } Clear(); diff --git a/Penumbra/Mods/ItemSwap/EquipmentSwap.cs b/Penumbra/Mods/ItemSwap/EquipmentSwap.cs index e42a1d31..b7827c47 100644 --- a/Penumbra/Mods/ItemSwap/EquipmentSwap.cs +++ b/Penumbra/Mods/ItemSwap/EquipmentSwap.cs @@ -125,8 +125,8 @@ public static class EquipmentSwap _ => (EstType)0, }; - var skipFemale = false; - var skipMale = false; + var skipFemale = false; + var skipMale = false; foreach (var gr in Enum.GetValues()) { switch (gr.Split().Item1) @@ -242,8 +242,8 @@ public static class EquipmentSwap private static (ImcFile, Variant[], EquipItem[]) GetVariants(MetaFileManager manager, ObjectIdentification identifier, EquipSlot slotFrom, PrimaryId idFrom, PrimaryId idTo, Variant variantFrom) { - var entry = new ImcManipulation(slotFrom, variantFrom.Id, idFrom, default); - var imc = new ImcFile(manager, entry.Identifier); + var ident = new ImcIdentifier(slotFrom, idFrom, variantFrom); + var imc = new ImcFile(manager, ident); EquipItem[] items; Variant[] variants; if (idFrom == idTo) @@ -273,7 +273,8 @@ public static class EquipmentSwap var manipToIdentifier = new GmpIdentifier(idTo); var manipFromDefault = ExpandedGmpFile.GetDefault(manager, manipFromIdentifier); var manipToDefault = ExpandedGmpFile.GetDefault(manager, manipToIdentifier); - return new MetaSwap(i => manips.TryGetValue(i, out var e) ? e : null, manipFromIdentifier, manipFromDefault, manipToIdentifier, manipToDefault); + return new MetaSwap(i => manips.TryGetValue(i, out var e) ? e : null, manipFromIdentifier, manipFromDefault, + manipToIdentifier, manipToDefault); } public static MetaSwap CreateImc(MetaFileManager manager, Func redirections, @@ -286,16 +287,17 @@ public static class EquipmentSwap Variant variantFrom, Variant variantTo, ImcFile imcFileFrom, ImcFile imcFileTo) { var manipFromIdentifier = new ImcIdentifier(slotFrom, idFrom, variantFrom); - var manipToIdentifier = new ImcIdentifier(slotTo, idTo, variantTo); - var manipFromDefault = imcFileFrom.GetEntry(ImcFile.PartIndex(slotFrom), variantFrom); - var manipToDefault = imcFileTo.GetEntry(ImcFile.PartIndex(slotTo), variantTo); - var imc = new MetaSwap(i => manips.TryGetValue(i, out var e) ? e : null, manipFromIdentifier, manipFromDefault, manipToIdentifier, manipToDefault); + var manipToIdentifier = new ImcIdentifier(slotTo, idTo, variantTo); + var manipFromDefault = imcFileFrom.GetEntry(ImcFile.PartIndex(slotFrom), variantFrom); + var manipToDefault = imcFileTo.GetEntry(ImcFile.PartIndex(slotTo), variantTo); + var imc = new MetaSwap(i => manips.TryGetValue(i, out var e) ? e : null, manipFromIdentifier, manipFromDefault, + manipToIdentifier, manipToDefault); var decal = CreateDecal(manager, redirections, imc.SwapToModdedEntry.DecalId); if (decal != null) imc.ChildSwaps.Add(decal); - var avfx = CreateAvfx(manager, redirections, idFrom, idTo, imc.SwapToModdedEntry.VfxId); + var avfx = CreateAvfx(manager, redirections, slotFrom, slotTo, idFrom, idTo, imc.SwapToModdedEntry.VfxId); if (avfx != null) imc.ChildSwaps.Add(avfx); @@ -316,19 +318,21 @@ public static class EquipmentSwap // Example: Abyssos Helm / Body - public static FileSwap? CreateAvfx(MetaFileManager manager, Func redirections, PrimaryId idFrom, PrimaryId idTo, + public static FileSwap? CreateAvfx(MetaFileManager manager, Func redirections, EquipSlot slotFrom, EquipSlot slotTo, + PrimaryId idFrom, PrimaryId idTo, byte vfxId) { if (vfxId == 0) return null; var vfxPathFrom = GamePaths.Equipment.Avfx.Path(idFrom, vfxId); - var vfxPathTo = GamePaths.Equipment.Avfx.Path(idTo, vfxId); - var avfx = FileSwap.CreateSwap(manager, ResourceType.Avfx, redirections, vfxPathFrom, vfxPathTo); + vfxPathFrom = ItemSwap.ReplaceType(vfxPathFrom, slotFrom, slotTo, idFrom); + var vfxPathTo = GamePaths.Equipment.Avfx.Path(idTo, vfxId); + var avfx = FileSwap.CreateSwap(manager, ResourceType.Avfx, redirections, vfxPathFrom, vfxPathTo); foreach (ref var filePath in avfx.AsAvfx()!.Textures.AsSpan()) { - var atex = CreateAtex(manager, redirections, ref filePath, ref avfx.DataWasChanged); + var atex = CreateAtex(manager, redirections, slotFrom, slotTo, idFrom, ref filePath, ref avfx.DataWasChanged); avfx.ChildSwaps.Add(atex); } @@ -394,8 +398,7 @@ public static class EquipmentSwap } public static FileSwap CreateTex(MetaFileManager manager, Func redirections, char prefix, PrimaryId idFrom, - PrimaryId idTo, - ref MtrlFile.Texture texture, ref bool dataWasChanged) + PrimaryId idTo, ref MtrlFile.Texture texture, ref bool dataWasChanged) => CreateTex(manager, redirections, prefix, EquipSlot.Unknown, EquipSlot.Unknown, idFrom, idTo, ref texture, ref dataWasChanged); public static FileSwap CreateTex(MetaFileManager manager, Func redirections, char prefix, EquipSlot slotFrom, @@ -404,6 +407,7 @@ public static class EquipmentSwap var addedDashes = GamePaths.Tex.HandleDx11Path(texture, out var path); var newPath = ItemSwap.ReplaceAnyId(path, prefix, idFrom); newPath = ItemSwap.ReplaceSlot(newPath, slotTo, slotFrom, slotTo != slotFrom); + newPath = ItemSwap.ReplaceType(newPath, slotFrom, slotTo, idFrom); newPath = ItemSwap.AddSuffix(newPath, ".tex", $"_{Path.GetFileName(texture.Path).GetStableHashCode():x8}"); if (newPath != path) { @@ -421,11 +425,12 @@ public static class EquipmentSwap return FileSwap.CreateSwap(manager, ResourceType.Shpk, redirections, path, path); } - public static FileSwap CreateAtex(MetaFileManager manager, Func redirections, ref string filePath, - ref bool dataWasChanged) + public static FileSwap CreateAtex(MetaFileManager manager, Func redirections, EquipSlot slotFrom, EquipSlot slotTo, + PrimaryId idFrom, ref string filePath, ref bool dataWasChanged) { var oldPath = filePath; filePath = ItemSwap.AddSuffix(filePath, ".atex", $"_{Path.GetFileName(filePath).GetStableHashCode():x8}"); + filePath = ItemSwap.ReplaceType(filePath, slotFrom, slotTo, idFrom); dataWasChanged = true; return FileSwap.CreateSwap(manager, ResourceType.Atex, redirections, filePath, oldPath, oldPath); diff --git a/Penumbra/Mods/ItemSwap/ItemSwap.cs b/Penumbra/Mods/ItemSwap/ItemSwap.cs index efd8080c..1f4c5e7a 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwap.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwap.cs @@ -136,14 +136,14 @@ public static class ItemSwap public static FileSwap CreatePhyb(MetaFileManager manager, Func redirections, EstType type, GenderRace race, EstEntry estEntry) { - var phybPath = GamePaths.Skeleton.Phyb.Path(race, EstManipulation.ToName(type), estEntry.AsId); + var phybPath = GamePaths.Skeleton.Phyb.Path(race, type.ToName(), estEntry.AsId); return FileSwap.CreateSwap(manager, ResourceType.Phyb, redirections, phybPath, phybPath); } public static FileSwap CreateSklb(MetaFileManager manager, Func redirections, EstType type, GenderRace race, EstEntry estEntry) { - var sklbPath = GamePaths.Skeleton.Sklb.Path(race, EstManipulation.ToName(type), estEntry.AsId); + var sklbPath = GamePaths.Skeleton.Sklb.Path(race, type.ToName(), estEntry.AsId); return FileSwap.CreateSwap(manager, ResourceType.Sklb, redirections, sklbPath, sklbPath); } @@ -154,10 +154,11 @@ public static class ItemSwap return null; var manipFromIdentifier = new EstIdentifier(idFrom, type, genderRace); - var manipToIdentifier = new EstIdentifier(idTo, type, genderRace); - var manipFromDefault = EstFile.GetDefault(manager, manipFromIdentifier); - var manipToDefault = EstFile.GetDefault(manager, manipToIdentifier); - var est = new MetaSwap(i => manips.TryGetValue(i, out var e) ? e : null, manipFromIdentifier, manipFromDefault, manipToIdentifier, manipToDefault); + var manipToIdentifier = new EstIdentifier(idTo, type, genderRace); + var manipFromDefault = EstFile.GetDefault(manager, manipFromIdentifier); + var manipToDefault = EstFile.GetDefault(manager, manipToIdentifier); + var est = new MetaSwap(i => manips.TryGetValue(i, out var e) ? e : null, manipFromIdentifier, manipFromDefault, + manipToIdentifier, manipToDefault); if (ownMdl && est.SwapToModdedEntry.Value >= 2) { @@ -215,6 +216,22 @@ public static class ItemSwap ? path.Replace($"_{from.ToSuffix()}_", $"_{to.ToSuffix()}_") : path; + public static string ReplaceType(string path, EquipSlot from, EquipSlot to, PrimaryId idFrom) + { + var isAccessoryFrom = from.IsAccessory(); + if (isAccessoryFrom == to.IsAccessory()) + return path; + + if (isAccessoryFrom) + { + path = path.Replace("accessory/a", "equipment/e"); + return path.Replace($"a{idFrom.Id:D4}", $"e{idFrom.Id:D4}"); + } + + path = path.Replace("equipment/e", "accessory/a"); + return path.Replace($"e{idFrom.Id:D4}", $"a{idFrom.Id:D4}"); + } + public static string ReplaceRace(string path, GenderRace from, GenderRace to, bool condition = true) => ReplaceId(path, 'c', (ushort)from, (ushort)to, condition); diff --git a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs index 8328edea..d2deb9ef 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwapContainer.cs @@ -123,8 +123,8 @@ public class ItemSwapContainer : p => ModRedirections.TryGetValue(p, out var path) ? path : new FullPath(p); private MetaDictionary MetaResolver(ModCollection? collection) - => collection?.MetaCache?.Manipulations is { } cache - ? [] // [.. cache] TODO + => collection?.MetaCache is { } cache + ? new MetaDictionary(cache) : _appliedModData.Manipulations; public EquipItem[] LoadEquipment(EquipItem from, EquipItem to, ModCollection? collection = null, bool useRightRing = true, diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index e8ca3199..ed4245c4 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -198,8 +198,7 @@ public partial class ModCreator( Penumbra.Log.Verbose( $"Incorporating {file} as Metadata file of {meta.MetaManipulations.Count} manipulations {deleteString}"); deleteList.Add(file.FullName); - // TODO - option.Manipulations.UnionWith([]);//[.. meta.MetaManipulations]); + option.Manipulations.UnionWith(meta.MetaManipulations); } else if (ext1 == ".rgsp" || ext2 == ".rgsp") { @@ -213,8 +212,7 @@ public partial class ModCreator( $"Incorporating {file} as racial scaling file of {rgsp.MetaManipulations.Count} manipulations {deleteString}"); deleteList.Add(file.FullName); - // TODO - option.Manipulations.UnionWith([]);//[.. rgsp.MetaManipulations]); + option.Manipulations.UnionWith(rgsp.MetaManipulations); } } catch (Exception e) diff --git a/Penumbra/Mods/SubMods/DefaultSubMod.cs b/Penumbra/Mods/SubMods/DefaultSubMod.cs index dcd33610..3840468f 100644 --- a/Penumbra/Mods/SubMods/DefaultSubMod.cs +++ b/Penumbra/Mods/SubMods/DefaultSubMod.cs @@ -13,7 +13,7 @@ public class DefaultSubMod(IMod mod) : IModDataContainer public Dictionary Files { get; set; } = []; public Dictionary FileSwaps { get; set; } = []; - public MetaDictionary Manipulations { get; set; } = []; + public MetaDictionary Manipulations { get; set; } = new(); IMod IModDataContainer.Mod => Mod; diff --git a/Penumbra/Mods/SubMods/OptionSubMod.cs b/Penumbra/Mods/SubMods/OptionSubMod.cs index ed7b6ff8..8fac52d8 100644 --- a/Penumbra/Mods/SubMods/OptionSubMod.cs +++ b/Penumbra/Mods/SubMods/OptionSubMod.cs @@ -33,7 +33,7 @@ public abstract class OptionSubMod(IModGroup group) : IModOption, IModDataContai public Dictionary Files { get; set; } = []; public Dictionary FileSwaps { get; set; } = []; - public MetaDictionary Manipulations { get; set; } = []; + public MetaDictionary Manipulations { get; set; } = new(); public void AddDataTo(Dictionary redirections, MetaDictionary manipulations) => SubMod.AddContainerTo(this, redirections, manipulations); diff --git a/Penumbra/Mods/TemporaryMod.cs b/Penumbra/Mods/TemporaryMod.cs index 91c4c5df..e1cf9f2b 100644 --- a/Penumbra/Mods/TemporaryMod.cs +++ b/Penumbra/Mods/TemporaryMod.cs @@ -93,8 +93,7 @@ public class TemporaryMod : IMod } } - // TODO - MetaDictionary manips = []; // [.. collection.MetaCache?.Manipulations ?? []]; + var manips = new MetaDictionary(collection.MetaCache); defaultMod.Manipulations.UnionWith(manips); saveService.ImmediateSave(new ModSaveGroup(dir, defaultMod, config.ReplaceNonAsciiOnImport)); diff --git a/Penumbra/UI/AdvancedWindow/Meta/EqdpMetaDrawer.cs b/Penumbra/UI/AdvancedWindow/Meta/EqdpMetaDrawer.cs new file mode 100644 index 00000000..b1ac93f1 --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Meta/EqdpMetaDrawer.cs @@ -0,0 +1,159 @@ +using Dalamud.Interface; +using Dalamud.Interface.Utility.Raii; +using ImGuiNET; +using OtterGui.Services; +using OtterGui.Text; +using Penumbra.GameData.Enums; +using Penumbra.Interop.Structs; +using Penumbra.Meta; +using Penumbra.Meta.Files; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; +using Penumbra.UI.Classes; + +namespace Penumbra.UI.AdvancedWindow.Meta; + +public sealed class EqdpMetaDrawer(ModMetaEditor editor, MetaFileManager metaFiles) + : MetaDrawer(editor, metaFiles), IService +{ + public override ReadOnlySpan Label + => "Racial Model Edits (EQDP)###EQDP"u8; + + public override int NumColumns + => 7; + + protected override void Initialize() + { + Identifier = new EqdpIdentifier(1, EquipSlot.Head, GenderRace.MidlanderMale); + UpdateEntry(); + } + + private void UpdateEntry() + => Entry = new EqdpEntryInternal(ExpandedEqdpFile.GetDefault(MetaFiles, Identifier), Identifier.Slot); + + protected override void DrawNew() + { + ImGui.TableNextColumn(); + CopyToClipboardButton("Copy all current EQDP manipulations to clipboard."u8, MetaDictionary.SerializeTo([], Editor.Eqdp)); + + ImGui.TableNextColumn(); + var validRaceCode = CharacterUtilityData.EqdpIdx(Identifier.GenderRace, false) >= 0; + var canAdd = validRaceCode && !Editor.Contains(Identifier); + var tt = canAdd ? "Stage this edit."u8 : + validRaceCode ? "This entry is already edited."u8 : "This combination of race and gender can not be used."u8; + if (ImUtf8.IconButton(FontAwesomeIcon.Plus, tt, disabled: !canAdd)) + Editor.Changes |= Editor.TryAdd(Identifier, Entry); + + if (DrawIdentifierInput(ref Identifier)) + UpdateEntry(); + + DrawEntry(Entry, ref Entry, true); + } + + protected override void DrawEntry(EqdpIdentifier identifier, EqdpEntryInternal entry) + { + DrawMetaButtons(identifier, entry); + DrawIdentifier(identifier); + + var defaultEntry = new EqdpEntryInternal(ExpandedEqdpFile.GetDefault(MetaFiles, identifier), identifier.Slot); + if (DrawEntry(defaultEntry, ref entry, false)) + Editor.Changes |= Editor.Update(identifier, entry); + } + + protected override IEnumerable<(EqdpIdentifier, EqdpEntryInternal)> Enumerate() + => Editor.Eqdp.Select(kvp => (kvp.Key, kvp.Value)); + + private static bool DrawIdentifierInput(ref EqdpIdentifier identifier) + { + ImGui.TableNextColumn(); + var changes = DrawPrimaryId(ref identifier); + + ImGui.TableNextColumn(); + changes |= DrawRace(ref identifier); + + ImGui.TableNextColumn(); + changes |= DrawGender(ref identifier); + + ImGui.TableNextColumn(); + changes |= DrawEquipSlot(ref identifier); + return changes; + } + + private static void DrawIdentifier(EqdpIdentifier identifier) + { + ImGui.TableNextColumn(); + ImUtf8.TextFramed($"{identifier.SetId.Id}", FrameColor); + ImUtf8.HoverTooltip("Model Set ID"u8); + + ImGui.TableNextColumn(); + ImUtf8.TextFramed(identifier.Gender.ToName(), FrameColor); + ImUtf8.HoverTooltip("Model Race"u8); + + ImGui.TableNextColumn(); + ImUtf8.TextFramed(identifier.Race.ToName(), FrameColor); + ImUtf8.HoverTooltip("Gender"u8); + + ImGui.TableNextColumn(); + ImUtf8.TextFramed(identifier.Slot.ToName(), FrameColor); + ImUtf8.HoverTooltip("Equip Slot"u8); + } + + private static bool DrawEntry(EqdpEntryInternal defaultEntry, ref EqdpEntryInternal entry, bool disabled) + { + var changes = false; + using var dis = ImRaii.Disabled(disabled); + ImGui.TableNextColumn(); + if (Checkmark("Material##eqdp"u8, "\0"u8, entry.Material, defaultEntry.Material, out var newMaterial)) + { + entry = entry with { Material = newMaterial }; + changes = true; + } + + ImGui.SameLine(); + if (Checkmark("Model##eqdp"u8, "\0"u8, entry.Model, defaultEntry.Model, out var newModel)) + { + entry = entry with { Material = newModel }; + changes = true; + } + + return changes; + } + + public static bool DrawPrimaryId(ref EqdpIdentifier identifier, float unscaledWidth = 100) + { + var ret = IdInput("##eqdpPrimaryId"u8, unscaledWidth, identifier.SetId.Id, out var setId, 0, ExpandedEqpGmpBase.Count - 1, + identifier.SetId.Id <= 1); + ImUtf8.HoverTooltip( + "Model Set ID - You can usually find this as the 'e####' part of an item path.\nThis should generally not be left <= 1 unless you explicitly want that."u8); + if (ret) + identifier = identifier with { SetId = setId }; + return ret; + } + + public static bool DrawRace(ref EqdpIdentifier identifier, float unscaledWidth = 100) + { + var ret = Combos.Race("##eqdpRace", identifier.Race, out var race, unscaledWidth); + ImUtf8.HoverTooltip("Model Race"u8); + if (ret) + identifier = identifier with { GenderRace = Names.CombinedRace(identifier.Gender, race) }; + return ret; + } + + public static bool DrawGender(ref EqdpIdentifier identifier, float unscaledWidth = 120) + { + var ret = Combos.Gender("##eqdpGender", identifier.Gender, out var gender, unscaledWidth); + ImUtf8.HoverTooltip("Gender"u8); + if (ret) + identifier = identifier with { GenderRace = Names.CombinedRace(gender, identifier.Race) }; + return ret; + } + + public static bool DrawEquipSlot(ref EqdpIdentifier identifier, float unscaledWidth = 100) + { + var ret = Combos.EqdpEquipSlot("##eqdpSlot", identifier.Slot, out var slot, unscaledWidth); + ImUtf8.HoverTooltip("Equip Slot"u8); + if (ret) + identifier = identifier with { Slot = slot }; + return ret; + } +} diff --git a/Penumbra/UI/AdvancedWindow/Meta/EqpMetaDrawer.cs b/Penumbra/UI/AdvancedWindow/Meta/EqpMetaDrawer.cs new file mode 100644 index 00000000..56c06bc9 --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Meta/EqpMetaDrawer.cs @@ -0,0 +1,134 @@ +using Dalamud.Interface; +using ImGuiNET; +using OtterGui.Raii; +using OtterGui.Services; +using OtterGui.Text; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Meta; +using Penumbra.Meta.Files; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; +using Penumbra.UI.Classes; + +namespace Penumbra.UI.AdvancedWindow.Meta; + +public sealed class EqpMetaDrawer(ModMetaEditor editor, MetaFileManager metaFiles) + : MetaDrawer(editor, metaFiles), IService +{ + public override ReadOnlySpan Label + => "Equipment Parameter Edits (EQP)###EQP"u8; + + public override int NumColumns + => 5; + + protected override void Initialize() + { + Identifier = new EqpIdentifier(1, EquipSlot.Body); + UpdateEntry(); + } + + private void UpdateEntry() + => Entry = new EqpEntryInternal(ExpandedEqpFile.GetDefault(MetaFiles, Identifier.SetId), Identifier.Slot); + + protected override void DrawNew() + { + ImGui.TableNextColumn(); + CopyToClipboardButton("Copy all current EQP manipulations to clipboard."u8, MetaDictionary.SerializeTo([], Editor.Eqp)); + + ImGui.TableNextColumn(); + var canAdd = !Editor.Contains(Identifier); + var tt = canAdd ? "Stage this edit."u8 : "This entry is already edited."u8; + if (ImUtf8.IconButton(FontAwesomeIcon.Plus, tt, disabled: !canAdd)) + Editor.Changes |= Editor.TryAdd(Identifier, Entry); + + if (DrawIdentifierInput(ref Identifier)) + UpdateEntry(); + + DrawEntry(Identifier.Slot, Entry, ref Entry, true); + } + + protected override void DrawEntry(EqpIdentifier identifier, EqpEntryInternal entry) + { + DrawMetaButtons(identifier, entry); + DrawIdentifier(identifier); + + var defaultEntry = new EqpEntryInternal(ExpandedEqpFile.GetDefault(MetaFiles, identifier.SetId), identifier.Slot); + if (DrawEntry(identifier.Slot, defaultEntry, ref entry, false)) + Editor.Changes |= Editor.Update(identifier, entry); + } + + protected override IEnumerable<(EqpIdentifier, EqpEntryInternal)> Enumerate() + => Editor.Eqp.Select(kvp => (kvp.Key, kvp.Value)); + + private static bool DrawIdentifierInput(ref EqpIdentifier identifier) + { + ImGui.TableNextColumn(); + var changes = DrawPrimaryId(ref identifier); + + ImGui.TableNextColumn(); + changes |= DrawEquipSlot(ref identifier); + return changes; + } + + private static void DrawIdentifier(EqpIdentifier identifier) + { + ImGui.TableNextColumn(); + ImUtf8.TextFramed($"{identifier.SetId.Id}", FrameColor); + ImUtf8.HoverTooltip("Model Set ID"u8); + + ImGui.TableNextColumn(); + ImUtf8.TextFramed(identifier.Slot.ToName(), FrameColor); + ImUtf8.HoverTooltip("Equip Slot"u8); + } + + private static bool DrawEntry(EquipSlot slot, EqpEntryInternal defaultEntry, ref EqpEntryInternal entry, bool disabled) + { + var changes = false; + using var dis = ImRaii.Disabled(disabled); + ImGui.TableNextColumn(); + var offset = Eqp.OffsetAndMask(slot).Item1; + DrawBox(ref entry, 0); + for (var i = 1; i < Eqp.EqpAttributes[slot].Count; ++i) + { + ImUtf8.SameLineInner(); + DrawBox(ref entry, i); + } + + return changes; + + void DrawBox(ref EqpEntryInternal entry, int i) + { + using var id = ImUtf8.PushId(i); + var flag = 1u << i; + var eqpFlag = (EqpEntry)((ulong)flag << offset); + var defaultValue = (flag & defaultEntry.Value) != 0; + var value = (flag & entry.Value) != 0; + if (Checkmark("##eqp"u8, eqpFlag.ToLocalName(), value, defaultValue, out var newValue)) + { + entry = new EqpEntryInternal(newValue ? entry.Value | flag : entry.Value & ~flag); + changes = true; + } + } + } + + public static bool DrawPrimaryId(ref EqpIdentifier identifier, float unscaledWidth = 100) + { + var ret = IdInput("##eqpPrimaryId"u8, unscaledWidth, identifier.SetId.Id, out var setId, 0, ExpandedEqpGmpBase.Count - 1, + identifier.SetId.Id <= 1); + ImUtf8.HoverTooltip( + "Model Set ID - You can usually find this as the 'e####' part of an item path.\nThis should generally not be left <= 1 unless you explicitly want that."u8); + if (ret) + identifier = identifier with { SetId = setId }; + return ret; + } + + public static bool DrawEquipSlot(ref EqpIdentifier identifier, float unscaledWidth = 100) + { + var ret = Combos.EqpEquipSlot("##eqpSlot", identifier.Slot, out var slot, unscaledWidth); + ImUtf8.HoverTooltip("Equip Slot"u8); + if (ret) + identifier = identifier with { Slot = slot }; + return ret; + } +} diff --git a/Penumbra/UI/AdvancedWindow/Meta/EstMetaDrawer.cs b/Penumbra/UI/AdvancedWindow/Meta/EstMetaDrawer.cs new file mode 100644 index 00000000..5c3c5df5 --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Meta/EstMetaDrawer.cs @@ -0,0 +1,147 @@ +using Dalamud.Interface; +using Dalamud.Interface.Utility.Raii; +using ImGuiNET; +using OtterGui.Services; +using OtterGui.Text; +using Penumbra.GameData.Enums; +using Penumbra.Meta; +using Penumbra.Meta.Files; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; +using Penumbra.UI.Classes; + +namespace Penumbra.UI.AdvancedWindow.Meta; + +public sealed class EstMetaDrawer(ModMetaEditor editor, MetaFileManager metaFiles) + : MetaDrawer(editor, metaFiles), IService +{ + public override ReadOnlySpan Label + => "Extra Skeleton Parameters (EST)###EST"u8; + + public override int NumColumns + => 7; + + protected override void Initialize() + { + Identifier = new EstIdentifier(1, EstType.Hair, GenderRace.MidlanderMale); + UpdateEntry(); + } + + private void UpdateEntry() + => Entry = EstFile.GetDefault(MetaFiles, Identifier.Slot, Identifier.GenderRace, Identifier.SetId); + + protected override void DrawNew() + { + ImGui.TableNextColumn(); + CopyToClipboardButton("Copy all current EST manipulations to clipboard."u8, MetaDictionary.SerializeTo([], Editor.Est)); + + ImGui.TableNextColumn(); + var canAdd = !Editor.Contains(Identifier); + var tt = canAdd ? "Stage this edit."u8 : "This entry is already edited."u8; + if (ImUtf8.IconButton(FontAwesomeIcon.Plus, tt, disabled: !canAdd)) + Editor.Changes |= Editor.TryAdd(Identifier, Entry); + + if (DrawIdentifierInput(ref Identifier)) + UpdateEntry(); + + DrawEntry(Entry, ref Entry, true); + } + + protected override void DrawEntry(EstIdentifier identifier, EstEntry entry) + { + DrawMetaButtons(identifier, entry); + DrawIdentifier(identifier); + + var defaultEntry = EstFile.GetDefault(MetaFiles, identifier.Slot, identifier.GenderRace, identifier.SetId); + if (DrawEntry(defaultEntry, ref entry, false)) + Editor.Changes |= Editor.Update(identifier, entry); + } + + protected override IEnumerable<(EstIdentifier, EstEntry)> Enumerate() + => Editor.Est.Select(kvp => (kvp.Key, kvp.Value)); + + private static bool DrawIdentifierInput(ref EstIdentifier identifier) + { + ImGui.TableNextColumn(); + var changes = DrawPrimaryId(ref identifier); + + ImGui.TableNextColumn(); + changes |= DrawRace(ref identifier); + + ImGui.TableNextColumn(); + changes |= DrawGender(ref identifier); + + ImGui.TableNextColumn(); + changes |= DrawSlot(ref identifier); + + return changes; + } + + private static void DrawIdentifier(EstIdentifier identifier) + { + ImGui.TableNextColumn(); + ImUtf8.TextFramed($"{identifier.SetId.Id}", FrameColor); + ImUtf8.HoverTooltip("Model Set ID"u8); + + ImGui.TableNextColumn(); + ImUtf8.TextFramed(identifier.Race.ToName(), FrameColor); + ImUtf8.HoverTooltip("Model Race"u8); + + ImGui.TableNextColumn(); + ImUtf8.TextFramed(identifier.Gender.ToName(), FrameColor); + ImUtf8.HoverTooltip("Gender"u8); + + ImGui.TableNextColumn(); + ImUtf8.TextFramed(identifier.Slot.ToString(), FrameColor); + ImUtf8.HoverTooltip("Extra Skeleton Type"u8); + } + + private static bool DrawEntry(EstEntry defaultEntry, ref EstEntry entry, bool disabled) + { + using var dis = ImRaii.Disabled(disabled); + ImGui.TableNextColumn(); + var ret = DragInput("##estValue"u8, [], 100f * ImUtf8.GlobalScale, entry.Value, defaultEntry.Value, out var newValue, (ushort)0, + ushort.MaxValue, 0.05f, !disabled); + if (ret) + entry = new EstEntry(newValue); + return ret; + } + + public static bool DrawPrimaryId(ref EstIdentifier identifier, float unscaledWidth = 100) + { + var ret = IdInput("##estPrimaryId"u8, unscaledWidth, identifier.SetId.Id, out var setId, 0, ExpandedEqpGmpBase.Count - 1, + identifier.SetId.Id <= 1); + ImUtf8.HoverTooltip( + "Model Set ID - You can usually find this as the 'e####' part of an item path.\nThis should generally not be left <= 1 unless you explicitly want that."u8); + if (ret) + identifier = identifier with { SetId = setId }; + return ret; + } + + public static bool DrawRace(ref EstIdentifier identifier, float unscaledWidth = 100) + { + var ret = Combos.Race("##estRace", identifier.Race, out var race, unscaledWidth); + ImUtf8.HoverTooltip("Model Race"u8); + if (ret) + identifier = identifier with { GenderRace = Names.CombinedRace(identifier.Gender, race) }; + return ret; + } + + public static bool DrawGender(ref EstIdentifier identifier, float unscaledWidth = 120) + { + var ret = Combos.Gender("##estGender", identifier.Gender, out var gender, unscaledWidth); + ImUtf8.HoverTooltip("Gender"u8); + if (ret) + identifier = identifier with { GenderRace = Names.CombinedRace(gender, identifier.Race) }; + return ret; + } + + public static bool DrawSlot(ref EstIdentifier identifier, float unscaledWidth = 200) + { + var ret = Combos.EstSlot("##estSlot", identifier.Slot, out var slot, unscaledWidth); + ImUtf8.HoverTooltip("Extra Skeleton Type"u8); + if (ret) + identifier = identifier with { Slot = slot }; + return ret; + } +} diff --git a/Penumbra/UI/AdvancedWindow/Meta/GlobalEqpMetaDrawer.cs b/Penumbra/UI/AdvancedWindow/Meta/GlobalEqpMetaDrawer.cs new file mode 100644 index 00000000..130831a0 --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Meta/GlobalEqpMetaDrawer.cs @@ -0,0 +1,111 @@ +using Dalamud.Interface; +using ImGuiNET; +using OtterGui.Services; +using OtterGui.Text; +using Penumbra.Meta; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; + +namespace Penumbra.UI.AdvancedWindow.Meta; + +public sealed class GlobalEqpMetaDrawer(ModMetaEditor editor, MetaFileManager metaFiles) + : MetaDrawer(editor, metaFiles), IService +{ + public override ReadOnlySpan Label + => "Global Equipment Parameter Edits (Global EQP)###GEQP"u8; + + public override int NumColumns + => 4; + + protected override void Initialize() + { + Identifier = new GlobalEqpManipulation() + { + Condition = 1, + Type = GlobalEqpType.DoNotHideEarrings, + }; + } + + protected override void DrawNew() + { + ImGui.TableNextColumn(); + CopyToClipboardButton("Copy all current global EQP manipulations to clipboard."u8, MetaDictionary.SerializeTo([], Editor.GlobalEqp)); + + ImGui.TableNextColumn(); + var canAdd = !Editor.Contains(Identifier); + var tt = canAdd ? "Stage this edit."u8 : "This entry is already edited."u8; + if (ImUtf8.IconButton(FontAwesomeIcon.Plus, tt, disabled: !canAdd)) + Editor.Changes |= Editor.TryAdd(Identifier); + + DrawIdentifierInput(ref Identifier); + } + + protected override void DrawEntry(GlobalEqpManipulation identifier, byte _) + { + DrawMetaButtons(identifier, 0); + DrawIdentifier(identifier); + } + + protected override IEnumerable<(GlobalEqpManipulation, byte)> Enumerate() + => Editor.GlobalEqp.Select(identifier => (identifier, (byte)0)); + + private static void DrawIdentifierInput(ref GlobalEqpManipulation identifier) + { + ImGui.TableNextColumn(); + DrawType(ref identifier); + + ImGui.TableNextColumn(); + if (identifier.Type.HasCondition()) + DrawCondition(ref identifier); + else + ImUtf8.ScaledDummy(100); + } + + private static void DrawIdentifier(GlobalEqpManipulation identifier) + { + ImGui.TableNextColumn(); + ImUtf8.TextFramed(identifier.Type.ToName(), FrameColor); + ImUtf8.HoverTooltip("Global EQP Type"u8); + + ImGui.TableNextColumn(); + if (identifier.Type.HasCondition()) + { + ImUtf8.TextFramed($"{identifier.Condition.Id}", FrameColor); + ImUtf8.HoverTooltip("Conditional Model ID"u8); + } + } + + public static bool DrawType(ref GlobalEqpManipulation identifier, float unscaledWidth = 250) + { + ImGui.SetNextItemWidth(unscaledWidth * ImUtf8.GlobalScale); + using var combo = ImUtf8.Combo("##geqpType"u8, identifier.Type.ToName()); + if (!combo) + return false; + + var ret = false; + foreach (var type in Enum.GetValues()) + { + if (ImUtf8.Selectable(type.ToName(), type == identifier.Type)) + { + identifier = new GlobalEqpManipulation + { + Type = type, + Condition = type.HasCondition() ? identifier.Type.HasCondition() ? identifier.Condition : 1 : 0, + }; + ret = true; + } + + ImUtf8.HoverTooltip(type.ToDescription()); + } + + return ret; + } + + public static void DrawCondition(ref GlobalEqpManipulation identifier, float unscaledWidth = 100) + { + if (IdInput("##geqpCond"u8, unscaledWidth, identifier.Condition.Id, out var newId, 1, ushort.MaxValue, + identifier.Condition.Id <= 1)) + identifier = identifier with { Condition = newId }; + ImUtf8.HoverTooltip("The Model ID for the item that should not be hidden."u8); + } +} diff --git a/Penumbra/UI/AdvancedWindow/Meta/GmpMetaDrawer.cs b/Penumbra/UI/AdvancedWindow/Meta/GmpMetaDrawer.cs new file mode 100644 index 00000000..87ed21dc --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Meta/GmpMetaDrawer.cs @@ -0,0 +1,148 @@ +using Dalamud.Interface; +using Dalamud.Interface.Utility.Raii; +using ImGuiNET; +using OtterGui.Services; +using OtterGui.Text; +using Penumbra.GameData.Structs; +using Penumbra.Meta.Files; +using Penumbra.Meta; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; + +namespace Penumbra.UI.AdvancedWindow.Meta; + +public sealed class GmpMetaDrawer(ModMetaEditor editor, MetaFileManager metaFiles) + : MetaDrawer(editor, metaFiles), IService +{ + public override ReadOnlySpan Label + => "Visor/Gimmick Edits (GMP)###GMP"u8; + + public override int NumColumns + => 7; + + protected override void Initialize() + { + Identifier = new GmpIdentifier(1); + UpdateEntry(); + } + + private void UpdateEntry() + => Entry = ExpandedGmpFile.GetDefault(MetaFiles, Identifier.SetId); + + protected override void DrawNew() + { + ImGui.TableNextColumn(); + CopyToClipboardButton("Copy all current Gmp manipulations to clipboard."u8, MetaDictionary.SerializeTo([], Editor.Gmp)); + + ImGui.TableNextColumn(); + var canAdd = !Editor.Contains(Identifier); + var tt = canAdd ? "Stage this edit."u8 : "This entry is already edited."u8; + if (ImUtf8.IconButton(FontAwesomeIcon.Plus, tt, disabled: !canAdd)) + Editor.Changes |= Editor.TryAdd(Identifier, Entry); + + if (DrawIdentifierInput(ref Identifier)) + UpdateEntry(); + + DrawEntry(Entry, ref Entry, true); + } + + protected override void DrawEntry(GmpIdentifier identifier, GmpEntry entry) + { + DrawMetaButtons(identifier, entry); + DrawIdentifier(identifier); + + var defaultEntry = ExpandedGmpFile.GetDefault(MetaFiles, identifier.SetId); + if (DrawEntry(defaultEntry, ref entry, false)) + Editor.Changes |= Editor.Update(identifier, entry); + } + + protected override IEnumerable<(GmpIdentifier, GmpEntry)> Enumerate() + => Editor.Gmp.Select(kvp => (kvp.Key, kvp.Value)); + + private static bool DrawIdentifierInput(ref GmpIdentifier identifier) + { + ImGui.TableNextColumn(); + return DrawPrimaryId(ref identifier); + } + + private static void DrawIdentifier(GmpIdentifier identifier) + { + ImGui.TableNextColumn(); + ImUtf8.TextFramed($"{identifier.SetId.Id}", FrameColor); + ImUtf8.HoverTooltip("Model Set ID"u8); + } + + private static bool DrawEntry(GmpEntry defaultEntry, ref GmpEntry entry, bool disabled) + { + using var dis = ImRaii.Disabled(disabled); + ImGui.TableNextColumn(); + var changes = false; + if (Checkmark("##gmpEnabled"u8, "Gimmick Enabled", entry.Enabled, defaultEntry.Enabled, out var enabled)) + { + entry = entry with { Enabled = enabled }; + changes = true; + } + + ImGui.TableNextColumn(); + if (Checkmark("##gmpAnimated"u8, "Gimmick Animated", entry.Animated, defaultEntry.Animated, out var animated)) + { + entry = entry with { Animated = animated }; + changes = true; + } + + var rotationWidth = 75 * ImUtf8.GlobalScale; + ImGui.TableNextColumn(); + if (DragInput("##gmpRotationA"u8, "Rotation A in Degrees"u8, rotationWidth, entry.RotationA, defaultEntry.RotationA, out var rotationA, + (ushort)0, (ushort)360, 0.05f, !disabled)) + { + entry = entry with { RotationA = rotationA }; + changes = true; + } + + ImUtf8.SameLineInner(); + if (DragInput("##gmpRotationB"u8, "Rotation B in Degrees"u8, rotationWidth, entry.RotationB, defaultEntry.RotationB, out var rotationB, + (ushort)0, (ushort)360, 0.05f, !disabled)) + { + entry = entry with { RotationB = rotationB }; + changes = true; + } + + ImUtf8.SameLineInner(); + if (DragInput("##gmpRotationC"u8, "Rotation C in Degrees"u8, rotationWidth, entry.RotationC, defaultEntry.RotationC, out var rotationC, + (ushort)0, (ushort)360, 0.05f, !disabled)) + { + entry = entry with { RotationC = rotationC }; + changes = true; + } + + var unkWidth = 50 * ImUtf8.GlobalScale; + ImGui.TableNextColumn(); + if (DragInput("##gmpUnkA"u8, "Animation Type A?"u8, unkWidth, entry.UnknownA, defaultEntry.UnknownA, out var unknownA, + (byte)0, (byte)15, 0.01f, !disabled)) + { + entry = entry with { UnknownA = unknownA }; + changes = true; + } + + ImUtf8.SameLineInner(); + if (DragInput("##gmpUnkB"u8, "Animation Type B?"u8, unkWidth, entry.UnknownB, defaultEntry.UnknownB, out var unknownB, + (byte)0, (byte)15, 0.01f, !disabled)) + { + entry = entry with { UnknownB = unknownB }; + changes = true; + } + + return changes; + } + + public static bool DrawPrimaryId(ref GmpIdentifier identifier, float unscaledWidth = 100) + { + var ret = IdInput("##gmpPrimaryId"u8, unscaledWidth, identifier.SetId.Id, out var setId, 1, ExpandedEqpGmpBase.Count - 1, + identifier.SetId.Id <= 1); + ImUtf8.HoverTooltip( + "Model Set ID - You can usually find this as the 'e####' part of an item path.\nThis should generally not be left <= 1 unless you explicitly want that."u8); + if (ret) + identifier = new GmpIdentifier(setId); + return ret; + } +} diff --git a/Penumbra/UI/AdvancedWindow/Meta/ImcMetaDrawer.cs b/Penumbra/UI/AdvancedWindow/Meta/ImcMetaDrawer.cs index d9a8c27c..58f626fc 100644 --- a/Penumbra/UI/AdvancedWindow/Meta/ImcMetaDrawer.cs +++ b/Penumbra/UI/AdvancedWindow/Meta/ImcMetaDrawer.cs @@ -12,22 +12,16 @@ using Penumbra.UI.Classes; namespace Penumbra.UI.AdvancedWindow.Meta; -public sealed class ImcMetaDrawer(ModEditor editor, MetaFileManager metaFiles) +public sealed class ImcMetaDrawer(ModMetaEditor editor, MetaFileManager metaFiles) : MetaDrawer(editor, metaFiles), IService { - private bool _fileExists; + public override ReadOnlySpan Label + => "Variant Edits (IMC)###IMC"u8; - private const string ModelSetIdTooltipShort = "Model Set ID"; - private const string EquipSlotTooltip = "Equip Slot"; - private const string ModelRaceTooltip = "Model Race"; - private const string GenderTooltip = "Gender"; - private const string ObjectTypeTooltip = "Object Type"; - private const string SecondaryIdTooltip = "Secondary ID"; - private const string PrimaryIdTooltipShort = "Primary ID"; - private const string VariantIdTooltip = "Variant ID"; - private const string EstTypeTooltip = "EST Type"; - private const string RacialTribeTooltip = "Racial Tribe"; - private const string ScalingTypeTooltip = "Scaling Type"; + public override int NumColumns + => 10; + + private bool _fileExists; protected override void Initialize() { @@ -41,14 +35,14 @@ public sealed class ImcMetaDrawer(ModEditor editor, MetaFileManager metaFiles) protected override void DrawNew() { ImGui.TableNextColumn(); - // Copy To Clipboard + CopyToClipboardButton("Copy all current IMC manipulations to clipboard."u8, MetaDictionary.SerializeTo([], Editor.Imc)); ImGui.TableNextColumn(); - var canAdd = _fileExists && Editor.MetaEditor.CanAdd(Identifier); + var canAdd = _fileExists && !Editor.Contains(Identifier); var tt = canAdd ? "Stage this edit."u8 : !_fileExists ? "This IMC file does not exist."u8 : "This entry is already edited."u8; if (ImUtf8.IconButton(FontAwesomeIcon.Plus, tt, disabled: !canAdd)) - Editor.MetaEditor.TryAdd(Identifier, Entry); + Editor.Changes |= Editor.TryAdd(Identifier, Entry); - if (DrawIdentifier(ref Identifier)) + if (DrawIdentifierInput(ref Identifier)) UpdateEntry(); using var disabled = ImRaii.Disabled(); @@ -57,46 +51,15 @@ public sealed class ImcMetaDrawer(ModEditor editor, MetaFileManager metaFiles) protected override void DrawEntry(ImcIdentifier identifier, ImcEntry entry) { - const uint frameColor = 0; - // Meta Buttons - - ImGui.TableNextColumn(); - ImUtf8.TextFramed(identifier.ObjectType.ToName(), frameColor); - ImUtf8.HoverTooltip("Object Type"u8); - - ImGui.TableNextColumn(); - ImUtf8.TextFramed($"{identifier.PrimaryId.Id}", frameColor); - ImUtf8.HoverTooltip("Primary ID"); - - ImGui.TableNextColumn(); - if (identifier.ObjectType is ObjectType.Equipment or ObjectType.Accessory) - { - ImUtf8.TextFramed(identifier.EquipSlot.ToName(), frameColor); - ImUtf8.HoverTooltip("Equip Slot"u8); - } - else - { - ImUtf8.TextFramed($"{identifier.SecondaryId.Id}", frameColor); - ImUtf8.HoverTooltip("Secondary ID"u8); - } - - ImGui.TableNextColumn(); - ImUtf8.TextFramed($"{identifier.Variant.Id}", frameColor); - ImUtf8.HoverTooltip("Variant"u8); - - ImGui.TableNextColumn(); - if (identifier.ObjectType is ObjectType.DemiHuman) - { - ImUtf8.TextFramed(identifier.EquipSlot.ToName(), frameColor); - ImUtf8.HoverTooltip("Equip Slot"u8); - } + DrawMetaButtons(identifier, entry); + DrawIdentifier(identifier); var defaultEntry = MetaFiles.ImcChecker.GetDefaultEntry(identifier, true).Entry; if (DrawEntry(defaultEntry, ref entry, true)) - Editor.MetaEditor.Update(identifier, entry); + Editor.Changes |= Editor.Update(identifier, entry); } - private static bool DrawIdentifier(ref ImcIdentifier identifier) + private static bool DrawIdentifierInput(ref ImcIdentifier identifier) { ImGui.TableNextColumn(); var change = DrawObjectType(ref identifier); @@ -121,6 +84,41 @@ public sealed class ImcMetaDrawer(ModEditor editor, MetaFileManager metaFiles) return change; } + private static void DrawIdentifier(ImcIdentifier identifier) + { + ImGui.TableNextColumn(); + ImUtf8.TextFramed(identifier.ObjectType.ToName(), FrameColor); + ImUtf8.HoverTooltip("Object Type"u8); + + ImGui.TableNextColumn(); + ImUtf8.TextFramed($"{identifier.PrimaryId.Id}", FrameColor); + ImUtf8.HoverTooltip("Primary ID"); + + ImGui.TableNextColumn(); + if (identifier.ObjectType is ObjectType.Equipment or ObjectType.Accessory) + { + ImUtf8.TextFramed(identifier.EquipSlot.ToName(), FrameColor); + ImUtf8.HoverTooltip("Equip Slot"u8); + } + else + { + ImUtf8.TextFramed($"{identifier.SecondaryId.Id}", FrameColor); + ImUtf8.HoverTooltip("Secondary ID"u8); + } + + ImGui.TableNextColumn(); + ImUtf8.TextFramed($"{identifier.Variant.Id}", FrameColor); + ImUtf8.HoverTooltip("Variant"u8); + + ImGui.TableNextColumn(); + if (identifier.ObjectType is ObjectType.DemiHuman) + { + ImUtf8.TextFramed(identifier.EquipSlot.ToName(), FrameColor); + ImUtf8.HoverTooltip("Equip Slot"u8); + } + + } + private static bool DrawEntry(ImcEntry defaultEntry, ref ImcEntry entry, bool addDefault) { ImGui.TableNextColumn(); @@ -142,7 +140,7 @@ public sealed class ImcMetaDrawer(ModEditor editor, MetaFileManager metaFiles) protected override IEnumerable<(ImcIdentifier, ImcEntry)> Enumerate() - => Editor.MetaEditor.Imc.Select(kvp => (kvp.Key, kvp.Value)); + => Editor.Imc.Select(kvp => (kvp.Key, kvp.Value)); public static bool DrawObjectType(ref ImcIdentifier identifier, float width = 110) { @@ -270,7 +268,7 @@ public sealed class ImcMetaDrawer(ModEditor editor, MetaFileManager metaFiles) return true; } - public static bool DrawAttributes(ImcEntry defaultEntry, ref ImcEntry entry) + private static bool DrawAttributes(ImcEntry defaultEntry, ref ImcEntry entry) { var changes = false; for (var i = 0; i < ImcEntry.NumAttributes; ++i) @@ -292,62 +290,4 @@ public sealed class ImcMetaDrawer(ModEditor editor, MetaFileManager metaFiles) return changes; } - - - /// - /// A number input for ids with an optional max id of given width. - /// Returns true if newId changed against currentId. - /// - private static bool IdInput(ReadOnlySpan label, float unscaledWidth, ushort currentId, out ushort newId, int minId, int maxId, - bool border) - { - int tmp = currentId; - ImGui.SetNextItemWidth(unscaledWidth * ImUtf8.GlobalScale); - using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, UiHelpers.Scale, border); - using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.RegexWarningBorder, border); - if (ImUtf8.InputScalar(label, ref tmp)) - tmp = Math.Clamp(tmp, minId, maxId); - - newId = (ushort)tmp; - return newId != currentId; - } - - /// - /// A dragging int input of given width that compares against a default value, shows a tooltip and clamps against min and max. - /// Returns true if newValue changed against currentValue. - /// - private static bool DragInput(ReadOnlySpan label, ReadOnlySpan tooltip, float width, T currentValue, T defaultValue, - out T newValue, T minValue, T maxValue, float speed, bool addDefault) where T : unmanaged, INumber - { - newValue = currentValue; - using var color = ImRaii.PushColor(ImGuiCol.FrameBg, - defaultValue > currentValue ? ColorId.DecreasedMetaValue.Value() : ColorId.IncreasedMetaValue.Value(), - defaultValue != currentValue); - ImGui.SetNextItemWidth(width); - if (ImUtf8.DragScalar(label, ref newValue, minValue, maxValue, speed)) - newValue = newValue <= minValue ? minValue : newValue >= maxValue ? maxValue : newValue; - - if (addDefault) - ImUtf8.HoverTooltip($"{tooltip}\nDefault Value: {defaultValue}"); - else - ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, tooltip); - - return newValue != currentValue; - } - - /// - /// A checkmark that compares against a default value and shows a tooltip. - /// Returns true if newValue is changed against currentValue. - /// - private static bool Checkmark(ReadOnlySpan label, ReadOnlySpan tooltip, bool currentValue, bool defaultValue, - out bool newValue) - { - using var color = ImRaii.PushColor(ImGuiCol.FrameBg, - defaultValue ? ColorId.DecreasedMetaValue.Value() : ColorId.IncreasedMetaValue.Value(), - defaultValue != currentValue); - newValue = currentValue; - ImUtf8.Checkbox(label, ref newValue); - ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, tooltip); - return newValue != currentValue; - } } diff --git a/Penumbra/UI/AdvancedWindow/Meta/MetaDrawer.cs b/Penumbra/UI/AdvancedWindow/Meta/MetaDrawer.cs new file mode 100644 index 00000000..229526c4 --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Meta/MetaDrawer.cs @@ -0,0 +1,154 @@ +using Dalamud.Interface; +using ImGuiNET; +using Newtonsoft.Json.Linq; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Text; +using Penumbra.Api.Api; +using Penumbra.Meta; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; +using Penumbra.UI.Classes; + +namespace Penumbra.UI.AdvancedWindow.Meta; + +public interface IMetaDrawer +{ + public ReadOnlySpan Label { get; } + public int NumColumns { get; } + public void Draw(); +} + +public abstract class MetaDrawer(ModMetaEditor editor, MetaFileManager metaFiles) : IMetaDrawer + where TIdentifier : unmanaged, IMetaIdentifier + where TEntry : unmanaged +{ + protected const uint FrameColor = 0; + + protected readonly ModMetaEditor Editor = editor; + protected readonly MetaFileManager MetaFiles = metaFiles; + protected TIdentifier Identifier; + protected TEntry Entry; + private bool _initialized; + + public void Draw() + { + if (!_initialized) + { + Initialize(); + _initialized = true; + } + + using var id = ImUtf8.PushId((int)Identifier.Type); + DrawNew(); + foreach (var ((identifier, entry), idx) in Enumerate().WithIndex()) + { + id.Push(idx); + DrawEntry(identifier, entry); + id.Pop(); + } + } + + public abstract ReadOnlySpan Label { get; } + public abstract int NumColumns { get; } + + protected abstract void DrawNew(); + protected abstract void Initialize(); + protected abstract void DrawEntry(TIdentifier identifier, TEntry entry); + + protected abstract IEnumerable<(TIdentifier, TEntry)> Enumerate(); + + + /// + /// A number input for ids with an optional max id of given width. + /// Returns true if newId changed against currentId. + /// + protected static bool IdInput(ReadOnlySpan label, float unscaledWidth, ushort currentId, out ushort newId, int minId, int maxId, + bool border) + { + int tmp = currentId; + ImGui.SetNextItemWidth(unscaledWidth * ImUtf8.GlobalScale); + using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, UiHelpers.Scale, border); + using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.RegexWarningBorder, border); + if (ImUtf8.InputScalar(label, ref tmp)) + tmp = Math.Clamp(tmp, minId, maxId); + + newId = (ushort)tmp; + return newId != currentId; + } + + /// + /// A dragging int input of given width that compares against a default value, shows a tooltip and clamps against min and max. + /// Returns true if newValue changed against currentValue. + /// + protected static bool DragInput(ReadOnlySpan label, ReadOnlySpan tooltip, float width, T currentValue, T defaultValue, + out T newValue, T minValue, T maxValue, float speed, bool addDefault) where T : unmanaged, INumber + { + newValue = currentValue; + using var color = ImRaii.PushColor(ImGuiCol.FrameBg, + defaultValue > currentValue ? ColorId.DecreasedMetaValue.Value() : ColorId.IncreasedMetaValue.Value(), + defaultValue != currentValue); + ImGui.SetNextItemWidth(width); + if (ImUtf8.DragScalar(label, ref newValue, minValue, maxValue, speed)) + newValue = newValue <= minValue ? minValue : newValue >= maxValue ? maxValue : newValue; + + if (addDefault) + ImUtf8.HoverTooltip($"{tooltip}\nDefault Value: {defaultValue}"); + else + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, tooltip); + + return newValue != currentValue; + } + + /// + /// A checkmark that compares against a default value and shows a tooltip. + /// Returns true if newValue is changed against currentValue. + /// + protected static bool Checkmark(ReadOnlySpan label, ReadOnlySpan tooltip, bool currentValue, bool defaultValue, + out bool newValue) + { + using var color = ImRaii.PushColor(ImGuiCol.FrameBg, + defaultValue ? ColorId.DecreasedMetaValue.Value() : ColorId.IncreasedMetaValue.Value(), + defaultValue != currentValue); + newValue = currentValue; + ImUtf8.Checkbox(label, ref newValue); + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, tooltip); + return newValue != currentValue; + } + + /// + /// A checkmark that compares against a default value and shows a tooltip. + /// Returns true if newValue is changed against currentValue. + /// + protected static bool Checkmark(ReadOnlySpan label, ReadOnlySpan tooltip, bool currentValue, bool defaultValue, + out bool newValue) + { + using var color = ImRaii.PushColor(ImGuiCol.FrameBg, + defaultValue ? ColorId.DecreasedMetaValue.Value() : ColorId.IncreasedMetaValue.Value(), + defaultValue != currentValue); + newValue = currentValue; + ImUtf8.Checkbox(label, ref newValue); + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, tooltip); + return newValue != currentValue; + } + + protected void DrawMetaButtons(TIdentifier identifier, TEntry entry) + { + ImGui.TableNextColumn(); + CopyToClipboardButton("Copy this manipulation to clipboard."u8, new JArray { MetaDictionary.Serialize(identifier, entry)! }); + + ImGui.TableNextColumn(); + if (ImUtf8.IconButton(FontAwesomeIcon.Trash, "Delete this meta manipulation."u8)) + Editor.Changes |= Editor.Remove(identifier); + } + + protected void CopyToClipboardButton(ReadOnlySpan tooltip, JToken? manipulations) + { + if (!ImUtf8.IconButton(FontAwesomeIcon.Clipboard, tooltip)) + return; + + var text = Functions.ToCompressedBase64(manipulations, MetaApi.CurrentVersion); + if (text.Length > 0) + ImGui.SetClipboardText(text); + } +} diff --git a/Penumbra/UI/AdvancedWindow/Meta/MetaDrawers.cs b/Penumbra/UI/AdvancedWindow/Meta/MetaDrawers.cs new file mode 100644 index 00000000..b3dd9299 --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Meta/MetaDrawers.cs @@ -0,0 +1,35 @@ +using OtterGui.Services; +using Penumbra.Meta.Manipulations; + +namespace Penumbra.UI.AdvancedWindow.Meta; + +public class MetaDrawers( + EqdpMetaDrawer eqdp, + EqpMetaDrawer eqp, + EstMetaDrawer est, + GlobalEqpMetaDrawer globalEqp, + GmpMetaDrawer gmp, + ImcMetaDrawer imc, + RspMetaDrawer rsp) : IService +{ + public readonly EqdpMetaDrawer Eqdp = eqdp; + public readonly EqpMetaDrawer Eqp = eqp; + public readonly EstMetaDrawer Est = est; + public readonly GmpMetaDrawer Gmp = gmp; + public readonly RspMetaDrawer Rsp = rsp; + public readonly ImcMetaDrawer Imc = imc; + public readonly GlobalEqpMetaDrawer GlobalEqp = globalEqp; + + public IMetaDrawer? Get(MetaManipulationType type) + => type switch + { + MetaManipulationType.Imc => Imc, + MetaManipulationType.Eqdp => Eqdp, + MetaManipulationType.Eqp => Eqp, + MetaManipulationType.Est => Est, + MetaManipulationType.Gmp => Gmp, + MetaManipulationType.Rsp => Rsp, + MetaManipulationType.GlobalEqp => GlobalEqp, + _ => null, + }; +} diff --git a/Penumbra/UI/AdvancedWindow/Meta/RspMetaDrawer.cs b/Penumbra/UI/AdvancedWindow/Meta/RspMetaDrawer.cs new file mode 100644 index 00000000..2b7904ce --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Meta/RspMetaDrawer.cs @@ -0,0 +1,112 @@ +using Dalamud.Interface; +using Dalamud.Interface.Utility.Raii; +using ImGuiNET; +using OtterGui.Services; +using OtterGui.Text; +using Penumbra.GameData.Enums; +using Penumbra.Meta; +using Penumbra.Meta.Files; +using Penumbra.Meta.Manipulations; +using Penumbra.Mods.Editor; +using Penumbra.UI.Classes; + +namespace Penumbra.UI.AdvancedWindow.Meta; + +public sealed class RspMetaDrawer(ModMetaEditor editor, MetaFileManager metaFiles) + : MetaDrawer(editor, metaFiles), IService +{ + public override ReadOnlySpan Label + => "Racial Scaling Edits (RSP)###RSP"u8; + + public override int NumColumns + => 5; + + protected override void Initialize() + { + Identifier = new RspIdentifier(SubRace.Midlander, RspAttribute.MaleMinSize); + UpdateEntry(); + } + + private void UpdateEntry() + => Entry = CmpFile.GetDefault(MetaFiles, Identifier.SubRace, Identifier.Attribute); + + protected override void DrawNew() + { + ImGui.TableNextColumn(); + CopyToClipboardButton("Copy all current RSP manipulations to clipboard."u8, MetaDictionary.SerializeTo([], Editor.Rsp)); + + ImGui.TableNextColumn(); + var canAdd = !Editor.Contains(Identifier); + var tt = canAdd ? "Stage this edit."u8 : "This entry is already edited."u8; + if (ImUtf8.IconButton(FontAwesomeIcon.Plus, tt, disabled: !canAdd)) + Editor.Changes |= Editor.TryAdd(Identifier, Entry); + + if (DrawIdentifierInput(ref Identifier)) + UpdateEntry(); + + DrawEntry(Entry, ref Entry, true); + } + + protected override void DrawEntry(RspIdentifier identifier, RspEntry entry) + { + DrawMetaButtons(identifier, entry); + DrawIdentifier(identifier); + + var defaultEntry = CmpFile.GetDefault(MetaFiles, identifier.SubRace, identifier.Attribute); + if (DrawEntry(defaultEntry, ref entry, false)) + Editor.Changes |= Editor.Update(identifier, entry); + } + + protected override IEnumerable<(RspIdentifier, RspEntry)> Enumerate() + => Editor.Rsp.Select(kvp => (kvp.Key, kvp.Value)); + + private static bool DrawIdentifierInput(ref RspIdentifier identifier) + { + ImGui.TableNextColumn(); + var changes = DrawSubRace(ref identifier); + + ImGui.TableNextColumn(); + changes |= DrawAttribute(ref identifier); + return changes; + } + + private static void DrawIdentifier(RspIdentifier identifier) + { + ImGui.TableNextColumn(); + ImUtf8.TextFramed(identifier.SubRace.ToName(), FrameColor); + ImUtf8.HoverTooltip("Model Set ID"u8); + + ImGui.TableNextColumn(); + ImUtf8.TextFramed(identifier.Attribute.ToFullString(), FrameColor); + ImUtf8.HoverTooltip("Equip Slot"u8); + } + + private static bool DrawEntry(RspEntry defaultEntry, ref RspEntry entry, bool disabled) + { + using var dis = ImRaii.Disabled(disabled); + ImGui.TableNextColumn(); + var ret = DragInput("##rspValue"u8, [], ImUtf8.GlobalScale * 150, defaultEntry.Value, entry.Value, out var newValue, + RspEntry.MinValue, RspEntry.MaxValue, 0.001f, !disabled); + if (ret) + entry = new RspEntry(newValue); + return ret; + } + + public static bool DrawSubRace(ref RspIdentifier identifier, float unscaledWidth = 150) + { + var ret = Combos.SubRace("##rspSubRace", identifier.SubRace, out var subRace, unscaledWidth); + ImUtf8.HoverTooltip("Racial Clan"u8); + if (ret) + identifier = identifier with { SubRace = subRace }; + return ret; + } + + public static bool DrawAttribute(ref RspIdentifier identifier, float unscaledWidth = 200) + { + var ret = Combos.RspAttribute("##rspAttribute", identifier.Attribute, out var attribute, unscaledWidth); + ImUtf8.HoverTooltip("Scaling Attribute"u8); + if (ret) + identifier = identifier with { Attribute = attribute }; + return ret; + } +} diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs index 50862eec..3ec6a4d5 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Meta.cs @@ -1,27 +1,18 @@ -using System.Reflection.Emit; using Dalamud.Interface; using ImGuiNET; using OtterGui; using OtterGui.Raii; using OtterGui.Text; -using OtterGui.Text.EndObjects; -using Penumbra.GameData.Enums; -using Penumbra.Interop.Structs; -using Penumbra.Meta; -using Penumbra.Meta.Files; +using Penumbra.Api.Api; using Penumbra.Meta.Manipulations; -using Penumbra.Mods.Editor; +using Penumbra.UI.AdvancedWindow.Meta; using Penumbra.UI.Classes; -using Penumbra.UI.ModsTab; namespace Penumbra.UI.AdvancedWindow; public partial class ModEditWindow { - private const string ModelSetIdTooltip = - "Model Set ID - You can usually find this as the 'e####' part of an item path.\nThis should generally not be left <= 1 unless you explicitly want that."; - - + private readonly MetaDrawers _metaDrawers; private void DrawMetaTab() { @@ -56,80 +47,42 @@ public partial class ModEditWindow if (!child) return; - DrawEditHeader(MetaManipulation.Type.Eqp); - DrawEditHeader(MetaManipulation.Type.Eqdp); - DrawEditHeader(MetaManipulation.Type.Imc); - DrawEditHeader(MetaManipulation.Type.Est); - DrawEditHeader(MetaManipulation.Type.Gmp); - DrawEditHeader(MetaManipulation.Type.Rsp); - DrawEditHeader(MetaManipulation.Type.GlobalEqp); + DrawEditHeader(MetaManipulationType.Eqp); + DrawEditHeader(MetaManipulationType.Eqdp); + DrawEditHeader(MetaManipulationType.Imc); + DrawEditHeader(MetaManipulationType.Est); + DrawEditHeader(MetaManipulationType.Gmp); + DrawEditHeader(MetaManipulationType.Rsp); + DrawEditHeader(MetaManipulationType.GlobalEqp); } - private static ReadOnlySpan Label(MetaManipulation.Type type) - => type switch - { - MetaManipulation.Type.Imc => "Variant Edits (IMC)###IMC"u8, - MetaManipulation.Type.Eqdp => "Racial Model Edits (EQDP)###EQDP"u8, - MetaManipulation.Type.Eqp => "Equipment Parameter Edits (EQP)###EQP"u8, - MetaManipulation.Type.Est => "Extra Skeleton Parameters (EST)###EST"u8, - MetaManipulation.Type.Gmp => "Visor/Gimmick Edits (GMP)###GMP"u8, - MetaManipulation.Type.Rsp => "Racial Scaling Edits (RSP)###RSP"u8, - MetaManipulation.Type.GlobalEqp => "Global Equipment Parameter Edits (Global EQP)###GEQP"u8, - _ => "\0"u8, - }; - - private static int ColumnCount(MetaManipulation.Type type) - => type switch - { - MetaManipulation.Type.Imc => 10, - MetaManipulation.Type.Eqdp => 7, - MetaManipulation.Type.Eqp => 5, - MetaManipulation.Type.Est => 7, - MetaManipulation.Type.Gmp => 7, - MetaManipulation.Type.Rsp => 5, - MetaManipulation.Type.GlobalEqp => 4, - _ => 0, - }; - - private void DrawEditHeader(MetaManipulation.Type type) + private void DrawEditHeader(MetaManipulationType type) { + var drawer = _metaDrawers.Get(type); + if (drawer == null) + return; + var oldPos = ImGui.GetCursorPosY(); - var header = ImUtf8.CollapsingHeader($"{_editor.MetaEditor.GetCount(type)} {Label(type)}"); + var header = ImUtf8.CollapsingHeader($"{_editor.MetaEditor.GetCount(type)} {drawer.Label}"); DrawOtherOptionData(type, oldPos, ImGui.GetCursorPos()); if (!header) return; - DrawTable(type); + DrawTable(drawer); } - private IMetaDrawer? Drawer(MetaManipulation.Type type) - => type switch - { - //MetaManipulation.Type.Imc => expr, - //MetaManipulation.Type.Eqdp => expr, - //MetaManipulation.Type.Eqp => expr, - //MetaManipulation.Type.Est => expr, - //MetaManipulation.Type.Gmp => expr, - //MetaManipulation.Type.Rsp => expr, - //MetaManipulation.Type.GlobalEqp => expr, - _ => null, - }; - - private void DrawTable(MetaManipulation.Type type) + private static void DrawTable(IMetaDrawer drawer) { const ImGuiTableFlags flags = ImGuiTableFlags.RowBg | ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.BordersInnerV; - using var table = ImUtf8.Table(Label(type), ColumnCount(type), flags); + using var table = ImUtf8.Table(drawer.Label, drawer.NumColumns, flags); if (!table) return; - if (Drawer(type) is not { } drawer) - return; - drawer.Draw(); ImGui.NewLine(); } - private void DrawOtherOptionData(MetaManipulation.Type type, float oldPos, Vector2 newPos) + private void DrawOtherOptionData(MetaManipulationType type, float oldPos, Vector2 newPos) { var otherOptionData = _editor.MetaEditor.OtherData[type]; if (otherOptionData.TotalCount <= 0) @@ -149,577 +102,12 @@ public partial class ModEditWindow ImGui.SetCursorPos(newPos); } -#if false - private static class EqpRow - { - private static EqpIdentifier _newIdentifier = new(1, EquipSlot.Body); - - private static float IdWidth - => 100 * UiHelpers.Scale; - - public static void DrawNew(MetaFileManager metaFileManager, ModEditor editor, Vector2 iconSize) - { - ImGui.TableNextColumn(); - CopyToClipboardButton("Copy all current EQP manipulations to clipboard.", iconSize, - editor.MetaEditor.Eqp.Select(m => (MetaManipulation)m)); - ImGui.TableNextColumn(); - var canAdd = editor.MetaEditor.CanAdd(_new); - var tt = canAdd ? "Stage this edit." : "This entry is already edited."; - var defaultEntry = ExpandedEqpFile.GetDefault(metaFileManager, _new.SetId); - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) - editor.MetaEditor.Add(_new.Copy(defaultEntry)); - - // Identifier - ImGui.TableNextColumn(); - if (IdInput("##eqpId", IdWidth, _new.SetId.Id, out var setId, 1, ExpandedEqpGmpBase.Count - 1, _new.SetId <= 1)) - _new = new EqpManipulation(ExpandedEqpFile.GetDefault(metaFileManager, setId), _new.Slot, setId); - - ImGuiUtil.HoverTooltip(ModelSetIdTooltip); - - ImGui.TableNextColumn(); - if (Combos.EqpEquipSlot("##eqpSlot", _new.Slot, out var slot)) - _new = new EqpManipulation(ExpandedEqpFile.GetDefault(metaFileManager, setId), slot, _new.SetId); - - ImGuiUtil.HoverTooltip(EquipSlotTooltip); - - // Values - using var disabled = ImRaii.Disabled(); - ImGui.TableNextColumn(); - using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, - new Vector2(3 * UiHelpers.Scale, ImGui.GetStyle().ItemSpacing.Y)); - foreach (var flag in Eqp.EqpAttributes[_new.Slot]) - { - var value = defaultEntry.HasFlag(flag); - Checkmark("##eqp", flag.ToLocalName(), value, value, out _); - ImGui.SameLine(); - } - - ImGui.NewLine(); - } - - public static void Draw(MetaFileManager metaFileManager, EqpManipulation meta, ModEditor editor, Vector2 iconSize) - { - DrawMetaButtons(meta, editor, iconSize); - - // Identifier - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.SetId.ToString()); - ImGuiUtil.HoverTooltip(ModelSetIdTooltipShort); - var defaultEntry = ExpandedEqpFile.GetDefault(metaFileManager, meta.SetId); - - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.Slot.ToName()); - ImGuiUtil.HoverTooltip(EquipSlotTooltip); - - // Values - ImGui.TableNextColumn(); - using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, - new Vector2(3 * UiHelpers.Scale, ImGui.GetStyle().ItemSpacing.Y)); - var idx = 0; - foreach (var flag in Eqp.EqpAttributes[meta.Slot]) - { - using var id = ImRaii.PushId(idx++); - var defaultValue = defaultEntry.HasFlag(flag); - var currentValue = meta.Entry.HasFlag(flag); - if (Checkmark("##eqp", flag.ToLocalName(), currentValue, defaultValue, out var value)) - editor.MetaEditor.Change(meta.Copy(value ? meta.Entry | flag : meta.Entry & ~flag)); - - ImGui.SameLine(); - } - - ImGui.NewLine(); - } - } - private static class EqdpRow - { - private static EqdpManipulation _new = new(EqdpEntry.Invalid, EquipSlot.Head, Gender.Male, ModelRace.Midlander, 1); - - private static float IdWidth - => 100 * UiHelpers.Scale; - - public static void DrawNew(MetaFileManager metaFileManager, ModEditor editor, Vector2 iconSize) - { - ImGui.TableNextColumn(); - CopyToClipboardButton("Copy all current EQDP manipulations to clipboard.", iconSize, - editor.MetaEditor.Eqdp.Select(m => (MetaManipulation)m)); - ImGui.TableNextColumn(); - var raceCode = Names.CombinedRace(_new.Gender, _new.Race); - var validRaceCode = CharacterUtilityData.EqdpIdx(raceCode, false) >= 0; - var canAdd = validRaceCode && editor.MetaEditor.CanAdd(_new); - var tt = canAdd ? "Stage this edit." : - validRaceCode ? "This entry is already edited." : "This combination of race and gender can not be used."; - var defaultEntry = validRaceCode - ? ExpandedEqdpFile.GetDefault(metaFileManager, Names.CombinedRace(_new.Gender, _new.Race), _new.Slot.IsAccessory(), _new.SetId) - : 0; - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) - editor.MetaEditor.Add(_new.Copy(defaultEntry)); - - // Identifier - ImGui.TableNextColumn(); - if (IdInput("##eqdpId", IdWidth, _new.SetId.Id, out var setId, 0, ExpandedEqpGmpBase.Count - 1, _new.SetId <= 1)) - { - var newDefaultEntry = ExpandedEqdpFile.GetDefault(metaFileManager, Names.CombinedRace(_new.Gender, _new.Race), - _new.Slot.IsAccessory(), setId); - _new = new EqdpManipulation(newDefaultEntry, _new.Slot, _new.Gender, _new.Race, setId); - } - - ImGuiUtil.HoverTooltip(ModelSetIdTooltip); - - ImGui.TableNextColumn(); - if (Combos.Race("##eqdpRace", _new.Race, out var race)) - { - var newDefaultEntry = ExpandedEqdpFile.GetDefault(metaFileManager, Names.CombinedRace(_new.Gender, race), - _new.Slot.IsAccessory(), _new.SetId); - _new = new EqdpManipulation(newDefaultEntry, _new.Slot, _new.Gender, race, _new.SetId); - } - - ImGuiUtil.HoverTooltip(ModelRaceTooltip); - - ImGui.TableNextColumn(); - if (Combos.Gender("##eqdpGender", _new.Gender, out var gender)) - { - var newDefaultEntry = ExpandedEqdpFile.GetDefault(metaFileManager, Names.CombinedRace(gender, _new.Race), - _new.Slot.IsAccessory(), _new.SetId); - _new = new EqdpManipulation(newDefaultEntry, _new.Slot, gender, _new.Race, _new.SetId); - } - - ImGuiUtil.HoverTooltip(GenderTooltip); - - ImGui.TableNextColumn(); - if (Combos.EqdpEquipSlot("##eqdpSlot", _new.Slot, out var slot)) - { - var newDefaultEntry = ExpandedEqdpFile.GetDefault(metaFileManager, Names.CombinedRace(_new.Gender, _new.Race), - slot.IsAccessory(), _new.SetId); - _new = new EqdpManipulation(newDefaultEntry, slot, _new.Gender, _new.Race, _new.SetId); - } - - ImGuiUtil.HoverTooltip(EquipSlotTooltip); - - // Values - using var disabled = ImRaii.Disabled(); - ImGui.TableNextColumn(); - var (bit1, bit2) = defaultEntry.ToBits(_new.Slot); - Checkmark("Material##eqdpCheck1", string.Empty, bit1, bit1, out _); - ImGui.SameLine(); - Checkmark("Model##eqdpCheck2", string.Empty, bit2, bit2, out _); - } - - public static void Draw(MetaFileManager metaFileManager, EqdpManipulation meta, ModEditor editor, Vector2 iconSize) - { - DrawMetaButtons(meta, editor, iconSize); - - // Identifier - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.SetId.ToString()); - ImGuiUtil.HoverTooltip(ModelSetIdTooltipShort); - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.Race.ToName()); - ImGuiUtil.HoverTooltip(ModelRaceTooltip); - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.Gender.ToName()); - ImGuiUtil.HoverTooltip(GenderTooltip); - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.Slot.ToName()); - ImGuiUtil.HoverTooltip(EquipSlotTooltip); - - // Values - var defaultEntry = ExpandedEqdpFile.GetDefault(metaFileManager, Names.CombinedRace(meta.Gender, meta.Race), meta.Slot.IsAccessory(), - meta.SetId); - var (defaultBit1, defaultBit2) = defaultEntry.ToBits(meta.Slot); - var (bit1, bit2) = meta.Entry.ToBits(meta.Slot); - ImGui.TableNextColumn(); - if (Checkmark("Material##eqdpCheck1", string.Empty, bit1, defaultBit1, out var newBit1)) - editor.MetaEditor.Change(meta.Copy(Eqdp.FromSlotAndBits(meta.Slot, newBit1, bit2))); - - ImGui.SameLine(); - if (Checkmark("Model##eqdpCheck2", string.Empty, bit2, defaultBit2, out var newBit2)) - editor.MetaEditor.Change(meta.Copy(Eqdp.FromSlotAndBits(meta.Slot, bit1, newBit2))); - } - } - - private static class EstRow - { - private static EstManipulation _new = new(Gender.Male, ModelRace.Midlander, EstType.Body, 1, EstEntry.Zero); - - private static float IdWidth - => 100 * UiHelpers.Scale; - - public static void DrawNew(MetaFileManager metaFileManager, ModEditor editor, Vector2 iconSize) - { - ImGui.TableNextColumn(); - CopyToClipboardButton("Copy all current EST manipulations to clipboard.", iconSize, - editor.MetaEditor.Est.Select(m => (MetaManipulation)m)); - ImGui.TableNextColumn(); - var canAdd = editor.MetaEditor.CanAdd(_new); - var tt = canAdd ? "Stage this edit." : "This entry is already edited."; - var defaultEntry = EstFile.GetDefault(metaFileManager, _new.Slot, Names.CombinedRace(_new.Gender, _new.Race), _new.SetId); - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) - editor.MetaEditor.Add(_new.Copy(defaultEntry)); - - // Identifier - ImGui.TableNextColumn(); - if (IdInput("##estId", IdWidth, _new.SetId.Id, out var setId, 0, ExpandedEqpGmpBase.Count - 1, _new.SetId <= 1)) - { - var newDefaultEntry = EstFile.GetDefault(metaFileManager, _new.Slot, Names.CombinedRace(_new.Gender, _new.Race), setId); - _new = new EstManipulation(_new.Gender, _new.Race, _new.Slot, setId, newDefaultEntry); - } - - ImGuiUtil.HoverTooltip(ModelSetIdTooltip); - - ImGui.TableNextColumn(); - if (Combos.Race("##estRace", _new.Race, out var race)) - { - var newDefaultEntry = EstFile.GetDefault(metaFileManager, _new.Slot, Names.CombinedRace(_new.Gender, race), _new.SetId); - _new = new EstManipulation(_new.Gender, race, _new.Slot, _new.SetId, newDefaultEntry); - } - - ImGuiUtil.HoverTooltip(ModelRaceTooltip); - - ImGui.TableNextColumn(); - if (Combos.Gender("##estGender", _new.Gender, out var gender)) - { - var newDefaultEntry = EstFile.GetDefault(metaFileManager, _new.Slot, Names.CombinedRace(gender, _new.Race), _new.SetId); - _new = new EstManipulation(gender, _new.Race, _new.Slot, _new.SetId, newDefaultEntry); - } - - ImGuiUtil.HoverTooltip(GenderTooltip); - - ImGui.TableNextColumn(); - if (Combos.EstSlot("##estSlot", _new.Slot, out var slot)) - { - var newDefaultEntry = EstFile.GetDefault(metaFileManager, slot, Names.CombinedRace(_new.Gender, _new.Race), _new.SetId); - _new = new EstManipulation(_new.Gender, _new.Race, slot, _new.SetId, newDefaultEntry); - } - - ImGuiUtil.HoverTooltip(EstTypeTooltip); - - // Values - using var disabled = ImRaii.Disabled(); - ImGui.TableNextColumn(); - IntDragInput("##estSkeleton", "Skeleton Index", IdWidth, _new.Entry.Value, defaultEntry.Value, out _, 0, ushort.MaxValue, 0.05f); - } - - public static void Draw(MetaFileManager metaFileManager, EstManipulation meta, ModEditor editor, Vector2 iconSize) - { - DrawMetaButtons(meta, editor, iconSize); - - // Identifier - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.SetId.ToString()); - ImGuiUtil.HoverTooltip(ModelSetIdTooltipShort); - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.Race.ToName()); - ImGuiUtil.HoverTooltip(ModelRaceTooltip); - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.Gender.ToName()); - ImGuiUtil.HoverTooltip(GenderTooltip); - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.Slot.ToString()); - ImGuiUtil.HoverTooltip(EstTypeTooltip); - - // Values - var defaultEntry = EstFile.GetDefault(metaFileManager, meta.Slot, Names.CombinedRace(meta.Gender, meta.Race), meta.SetId); - ImGui.TableNextColumn(); - if (IntDragInput("##estSkeleton", $"Skeleton Index\nDefault Value: {defaultEntry}", IdWidth, meta.Entry.Value, defaultEntry.Value, - out var entry, 0, ushort.MaxValue, 0.05f)) - editor.MetaEditor.Change(meta.Copy(new EstEntry((ushort)entry))); - } - } - private static class GmpRow - { - private static GmpManipulation _new = new(GmpEntry.Default, 1); - - private static float RotationWidth - => 75 * UiHelpers.Scale; - - private static float UnkWidth - => 50 * UiHelpers.Scale; - - private static float IdWidth - => 100 * UiHelpers.Scale; - - public static void DrawNew(MetaFileManager metaFileManager, ModEditor editor, Vector2 iconSize) - { - ImGui.TableNextColumn(); - CopyToClipboardButton("Copy all current GMP manipulations to clipboard.", iconSize, - editor.MetaEditor.Gmp.Select(m => (MetaManipulation)m)); - ImGui.TableNextColumn(); - var canAdd = editor.MetaEditor.CanAdd(_new); - var tt = canAdd ? "Stage this edit." : "This entry is already edited."; - var defaultEntry = ExpandedGmpFile.GetDefault(metaFileManager, _new.SetId); - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) - editor.MetaEditor.Add(_new.Copy(defaultEntry)); - - // Identifier - ImGui.TableNextColumn(); - if (IdInput("##gmpId", IdWidth, _new.SetId.Id, out var setId, 1, ExpandedEqpGmpBase.Count - 1, _new.SetId <= 1)) - _new = new GmpManipulation(ExpandedGmpFile.GetDefault(metaFileManager, setId), setId); - - ImGuiUtil.HoverTooltip(ModelSetIdTooltip); - - // Values - using var disabled = ImRaii.Disabled(); - ImGui.TableNextColumn(); - Checkmark("##gmpEnabled", "Gimmick Enabled", defaultEntry.Enabled, defaultEntry.Enabled, out _); - ImGui.TableNextColumn(); - Checkmark("##gmpAnimated", "Gimmick Animated", defaultEntry.Animated, defaultEntry.Animated, out _); - ImGui.TableNextColumn(); - IntDragInput("##gmpRotationA", "Rotation A in Degrees", RotationWidth, defaultEntry.RotationA, defaultEntry.RotationA, out _, 0, - 360, 0f); - ImGui.SameLine(); - IntDragInput("##gmpRotationB", "Rotation B in Degrees", RotationWidth, defaultEntry.RotationB, defaultEntry.RotationB, out _, 0, - 360, 0f); - ImGui.SameLine(); - IntDragInput("##gmpRotationC", "Rotation C in Degrees", RotationWidth, defaultEntry.RotationC, defaultEntry.RotationC, out _, 0, - 360, 0f); - ImGui.TableNextColumn(); - IntDragInput("##gmpUnkA", "Animation Type A?", UnkWidth, defaultEntry.UnknownA, defaultEntry.UnknownA, out _, 0, 15, 0f); - ImGui.SameLine(); - IntDragInput("##gmpUnkB", "Animation Type B?", UnkWidth, defaultEntry.UnknownB, defaultEntry.UnknownB, out _, 0, 15, 0f); - } - - public static void Draw(MetaFileManager metaFileManager, GmpManipulation meta, ModEditor editor, Vector2 iconSize) - { - DrawMetaButtons(meta, editor, iconSize); - - // Identifier - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.SetId.ToString()); - ImGuiUtil.HoverTooltip(ModelSetIdTooltipShort); - - // Values - var defaultEntry = ExpandedGmpFile.GetDefault(metaFileManager, meta.SetId); - ImGui.TableNextColumn(); - if (Checkmark("##gmpEnabled", "Gimmick Enabled", meta.Entry.Enabled, defaultEntry.Enabled, out var enabled)) - editor.MetaEditor.Change(meta.Copy(meta.Entry with { Enabled = enabled })); - - ImGui.TableNextColumn(); - if (Checkmark("##gmpAnimated", "Gimmick Animated", meta.Entry.Animated, defaultEntry.Animated, out var animated)) - editor.MetaEditor.Change(meta.Copy(meta.Entry with { Animated = animated })); - - ImGui.TableNextColumn(); - if (IntDragInput("##gmpRotationA", $"Rotation A in Degrees\nDefault Value: {defaultEntry.RotationA}", RotationWidth, - meta.Entry.RotationA, defaultEntry.RotationA, out var rotationA, 0, 360, 0.05f)) - editor.MetaEditor.Change(meta.Copy(meta.Entry with { RotationA = (ushort)rotationA })); - - ImGui.SameLine(); - if (IntDragInput("##gmpRotationB", $"Rotation B in Degrees\nDefault Value: {defaultEntry.RotationB}", RotationWidth, - meta.Entry.RotationB, defaultEntry.RotationB, out var rotationB, 0, 360, 0.05f)) - editor.MetaEditor.Change(meta.Copy(meta.Entry with { RotationB = (ushort)rotationB })); - - ImGui.SameLine(); - if (IntDragInput("##gmpRotationC", $"Rotation C in Degrees\nDefault Value: {defaultEntry.RotationC}", RotationWidth, - meta.Entry.RotationC, defaultEntry.RotationC, out var rotationC, 0, 360, 0.05f)) - editor.MetaEditor.Change(meta.Copy(meta.Entry with { RotationC = (ushort)rotationC })); - - ImGui.TableNextColumn(); - if (IntDragInput("##gmpUnkA", $"Animation Type A?\nDefault Value: {defaultEntry.UnknownA}", UnkWidth, meta.Entry.UnknownA, - defaultEntry.UnknownA, out var unkA, 0, 15, 0.01f)) - editor.MetaEditor.Change(meta.Copy(meta.Entry with { UnknownA = (byte)unkA })); - - ImGui.SameLine(); - if (IntDragInput("##gmpUnkB", $"Animation Type B?\nDefault Value: {defaultEntry.UnknownB}", UnkWidth, meta.Entry.UnknownB, - defaultEntry.UnknownB, out var unkB, 0, 15, 0.01f)) - editor.MetaEditor.Change(meta.Copy(meta.Entry with { UnknownB = (byte)unkB })); - } - } - private static class RspRow - { - private static RspManipulation _new = new(SubRace.Midlander, RspAttribute.MaleMinSize, RspEntry.One); - - private static float FloatWidth - => 150 * UiHelpers.Scale; - - public static void DrawNew(MetaFileManager metaFileManager, ModEditor editor, Vector2 iconSize) - { - ImGui.TableNextColumn(); - CopyToClipboardButton("Copy all current RSP manipulations to clipboard.", iconSize, - editor.MetaEditor.Rsp.Select(m => (MetaManipulation)m)); - ImGui.TableNextColumn(); - var canAdd = editor.MetaEditor.CanAdd(_new); - var tt = canAdd ? "Stage this edit." : "This entry is already edited."; - var defaultEntry = CmpFile.GetDefault(metaFileManager, _new.SubRace, _new.Attribute); - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) - editor.MetaEditor.Add(_new.Copy(defaultEntry)); - - // Identifier - ImGui.TableNextColumn(); - if (Combos.SubRace("##rspSubRace", _new.SubRace, out var subRace)) - _new = new RspManipulation(subRace, _new.Attribute, CmpFile.GetDefault(metaFileManager, subRace, _new.Attribute)); - - ImGuiUtil.HoverTooltip(RacialTribeTooltip); - - ImGui.TableNextColumn(); - if (Combos.RspAttribute("##rspAttribute", _new.Attribute, out var attribute)) - _new = new RspManipulation(_new.SubRace, attribute, CmpFile.GetDefault(metaFileManager, subRace, attribute)); - - ImGuiUtil.HoverTooltip(ScalingTypeTooltip); - - // Values - using var disabled = ImRaii.Disabled(); - ImGui.TableNextColumn(); - ImGui.SetNextItemWidth(FloatWidth); - var value = defaultEntry.Value; - ImGui.DragFloat("##rspValue", ref value, 0f); - } - - public static void Draw(MetaFileManager metaFileManager, RspManipulation meta, ModEditor editor, Vector2 iconSize) - { - DrawMetaButtons(meta, editor, iconSize); - - // Identifier - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.SubRace.ToName()); - ImGuiUtil.HoverTooltip(RacialTribeTooltip); - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImGui.TextUnformatted(meta.Attribute.ToFullString()); - ImGuiUtil.HoverTooltip(ScalingTypeTooltip); - ImGui.TableNextColumn(); - - // Values - var def = CmpFile.GetDefault(metaFileManager, meta.SubRace, meta.Attribute).Value; - var value = meta.Entry.Value; - ImGui.SetNextItemWidth(FloatWidth); - using var color = ImRaii.PushColor(ImGuiCol.FrameBg, - def < value ? ColorId.IncreasedMetaValue.Value() : ColorId.DecreasedMetaValue.Value(), - def != value); - if (ImGui.DragFloat("##rspValue", ref value, 0.001f, RspEntry.MinValue, RspEntry.MaxValue) - && value is >= RspEntry.MinValue and <= RspEntry.MaxValue) - editor.MetaEditor.Change(meta.Copy(new RspEntry(value))); - - ImGuiUtil.HoverTooltip($"Default Value: {def:0.###}"); - } - } - private static class GlobalEqpRow - { - private static GlobalEqpManipulation _new = new() - { - Type = GlobalEqpType.DoNotHideEarrings, - Condition = 1, - }; - - public static void DrawNew(MetaFileManager metaFileManager, ModEditor editor, Vector2 iconSize) - { - ImGui.TableNextColumn(); - CopyToClipboardButton("Copy all current global EQP manipulations to clipboard.", iconSize, - editor.MetaEditor.GlobalEqp.Select(m => (MetaManipulation)m)); - ImGui.TableNextColumn(); - var canAdd = editor.MetaEditor.CanAdd(_new); - var tt = canAdd ? "Stage this edit." : "This entry is already manipulated."; - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Plus.ToIconString(), iconSize, tt, !canAdd, true)) - editor.MetaEditor.Add(_new); - - // Identifier - ImGui.TableNextColumn(); - ImGui.SetNextItemWidth(250 * ImUtf8.GlobalScale); - using (var combo = ImUtf8.Combo("##geqpType"u8, _new.Type.ToName())) - { - if (combo) - foreach (var type in Enum.GetValues()) - { - if (ImUtf8.Selectable(type.ToName(), type == _new.Type)) - _new = new GlobalEqpManipulation - { - Type = type, - Condition = type.HasCondition() ? _new.Type.HasCondition() ? _new.Condition : 1 : 0, - }; - ImUtf8.HoverTooltip(type.ToDescription()); - } - } - - ImUtf8.HoverTooltip(_new.Type.ToDescription()); - - ImGui.TableNextColumn(); - if (!_new.Type.HasCondition()) - return; - - if (IdInput("##geqpCond", 100 * ImUtf8.GlobalScale, _new.Condition.Id, out var newId, 1, ushort.MaxValue, _new.Condition.Id <= 1)) - _new = _new with { Condition = newId }; - ImUtf8.HoverTooltip("The Model ID for the item that should not be hidden."u8); - } - - public static void Draw(MetaFileManager metaFileManager, GlobalEqpManipulation meta, ModEditor editor, Vector2 iconSize) - { - DrawMetaButtons(meta, editor, iconSize); - ImGui.TableNextColumn(); - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImUtf8.Text(meta.Type.ToName()); - ImUtf8.HoverTooltip(meta.Type.ToDescription()); - ImGui.TableNextColumn(); - if (meta.Type.HasCondition()) - { - ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X); - ImUtf8.Text($"{meta.Condition.Id}"); - } - } - } -#endif - - // A number input for ids with a optional max id of given width. - // Returns true if newId changed against currentId. - private static bool IdInput(string label, float width, ushort currentId, out ushort newId, int minId, int maxId, bool border) - { - int tmp = currentId; - ImGui.SetNextItemWidth(width); - using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameBorderSize, UiHelpers.Scale, border); - using var color = ImRaii.PushColor(ImGuiCol.Border, Colors.RegexWarningBorder, border); - if (ImGui.InputInt(label, ref tmp, 0)) - tmp = Math.Clamp(tmp, minId, maxId); - - newId = (ushort)tmp; - return newId != currentId; - } - - // A checkmark that compares against a default value and shows a tooltip. - // Returns true if newValue is changed against currentValue. - private static bool Checkmark(string label, string tooltip, bool currentValue, bool defaultValue, out bool newValue) - { - using var color = ImRaii.PushColor(ImGuiCol.FrameBg, - defaultValue ? ColorId.DecreasedMetaValue.Value() : ColorId.IncreasedMetaValue.Value(), - defaultValue != currentValue); - newValue = currentValue; - ImGui.Checkbox(label, ref newValue); - ImGuiUtil.HoverTooltip(tooltip, ImGuiHoveredFlags.AllowWhenDisabled); - return newValue != currentValue; - } - - // A dragging int input of given width that compares against a default value, shows a tooltip and clamps against min and max. - // Returns true if newValue changed against currentValue. - private static bool IntDragInput(string label, string tooltip, float width, int currentValue, int defaultValue, out int newValue, - int minValue, int maxValue, float speed) - { - newValue = currentValue; - using var color = ImRaii.PushColor(ImGuiCol.FrameBg, - defaultValue > currentValue ? ColorId.DecreasedMetaValue.Value() : ColorId.IncreasedMetaValue.Value(), - defaultValue != currentValue); - ImGui.SetNextItemWidth(width); - if (ImGui.DragInt(label, ref newValue, speed, minValue, maxValue)) - newValue = Math.Clamp(newValue, minValue, maxValue); - - ImGuiUtil.HoverTooltip(tooltip, ImGuiHoveredFlags.AllowWhenDisabled); - - return newValue != currentValue; - } - private static void CopyToClipboardButton(string tooltip, Vector2 iconSize, MetaDictionary manipulations) { if (!ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Clipboard.ToIconString(), iconSize, tooltip, false, true)) return; - var text = Functions.ToCompressedBase64(manipulations, MetaManipulation.CurrentVersion); + var text = Functions.ToCompressedBase64(manipulations, MetaApi.CurrentVersion); if (text.Length > 0) ImGui.SetClipboardText(text); } @@ -731,8 +119,11 @@ public partial class ModEditWindow var clipboard = ImGuiUtil.GetClipboardText(); var version = Functions.FromCompressedBase64(clipboard, out var manips); - if (version == MetaManipulation.CurrentVersion && manips != null) + if (version == MetaApi.CurrentVersion && manips != null) + { _editor.MetaEditor.UpdateTo(manips); + _editor.MetaEditor.Changes = true; + } } ImGuiUtil.HoverTooltip( @@ -745,194 +136,14 @@ public partial class ModEditWindow { var clipboard = ImGuiUtil.GetClipboardText(); var version = Functions.FromCompressedBase64(clipboard, out var manips); - if (version == MetaManipulation.CurrentVersion && manips != null) + if (version == MetaApi.CurrentVersion && manips != null) + { _editor.MetaEditor.SetTo(manips); + _editor.MetaEditor.Changes = true; + } } ImGuiUtil.HoverTooltip( "Try to set the current meta manipulations to the set currently stored in the clipboard.\nRemoves all other manipulations."); } - - private static void DrawMetaButtons(MetaManipulation meta, ModEditor editor, Vector2 iconSize) - { - //ImGui.TableNextColumn(); - //CopyToClipboardButton("Copy this manipulation to clipboard.", iconSize, Array.Empty().Append(meta)); - // - //ImGui.TableNextColumn(); - //if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Trash.ToIconString(), iconSize, "Delete this meta manipulation.", false, true)) - // editor.MetaEditor.Delete(meta); - } -} - - -public interface IMetaDrawer -{ - public void Draw(); } - - - - -public abstract class MetaDrawer(ModEditor editor, MetaFileManager metaFiles) : IMetaDrawer - where TIdentifier : unmanaged, IMetaIdentifier - where TEntry : unmanaged -{ - protected readonly ModEditor Editor = editor; - protected readonly MetaFileManager MetaFiles = metaFiles; - protected TIdentifier Identifier; - protected TEntry Entry; - private bool _initialized; - - public void Draw() - { - if (!_initialized) - { - Initialize(); - _initialized = true; - } - - DrawNew(); - foreach (var ((identifier, entry), idx) in Enumerate().WithIndex()) - { - using var id = ImUtf8.PushId(idx); - DrawEntry(identifier, entry); - } - } - - protected abstract void DrawNew(); - protected abstract void Initialize(); - protected abstract void DrawEntry(TIdentifier identifier, TEntry entry); - - protected abstract IEnumerable<(TIdentifier, TEntry)> Enumerate(); -} - - -#if false -public sealed class GmpMetaDrawer(ModEditor editor) : MetaDrawer, IService -{ - protected override void Initialize() - { - Identifier = new GmpIdentifier(1, EquipSlot.Body); - UpdateEntry(); - } - - private void UpdateEntry() - => Entry = ExpandedEqpFile.GetDefault(metaManager, Identifier.SetId); - - protected override void DrawNew() - { } - - protected override void DrawEntry(GmpIdentifier identifier, GmpEntry entry) - { } - - protected override IEnumerable<(GmpIdentifier, GmpEntry)> Enumerate() - => editor.MetaEditor.Eqp.Select(kvp => (kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))); -} - -public sealed class EstMetaDrawer(ModEditor editor) : MetaDrawer, IService -{ - protected override void Initialize() - { - Identifier = new EqpIdentifier(1, EquipSlot.Body); - UpdateEntry(); - } - - private void UpdateEntry() - => Entry = ExpandedEqpFile.GetDefault(metaManager, Identifier.SetId); - - protected override void DrawNew() - { } - - protected override void DrawEntry(EstIdentifier identifier, EstEntry entry) - { } - - protected override IEnumerable<(EstIdentifier, EstEntry)> Enumerate() - => editor.MetaEditor.Eqp.Select(kvp => (kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))); -} - -public sealed class EqdpMetaDrawer(ModEditor editor) : MetaDrawer, IService -{ - protected override void Initialize() - { - Identifier = new EqdpIdentifier(1, EquipSlot.Body); - UpdateEntry(); - } - - private void UpdateEntry() - => Entry = ExpandedEqpFile.GetDefault(metaManager, Identifier.SetId); - - protected override void DrawNew() - { } - - protected override void DrawEntry(EqdpIdentifier identifier, EqdpEntry entry) - { } - - protected override IEnumerable<(EqdpIdentifier, EqdpEntry)> Enumerate() - => editor.MetaEditor.Eqp.Select(kvp => (kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))); -} - -public sealed class EqpMetaDrawer(ModEditor editor, MetaFileManager metaManager) : MetaDrawer, IService -{ - protected override void Initialize() - { - Identifier = new EqpIdentifier(1, EquipSlot.Body); - UpdateEntry(); - } - - private void UpdateEntry() - => Entry = ExpandedEqpFile.GetDefault(metaManager, Identifier.SetId); - - protected override void DrawNew() - { } - - protected override void DrawEntry(EqpIdentifier identifier, EqpEntry entry) - { } - - protected override IEnumerable<(EqpIdentifier, EqpEntry)> Enumerate() - => editor.MetaEditor.Eqp.Select(kvp => (kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))); -} - -public sealed class RspMetaDrawer(ModEditor editor) : MetaDrawer, IService -{ - protected override void Initialize() - { - Identifier = new RspIdentifier(1, EquipSlot.Body); - UpdateEntry(); - } - - private void UpdateEntry() - => Entry = ExpandedEqpFile.GetDefault(metaManager, Identifier.SetId); - - protected override void DrawNew() - { } - - protected override void DrawEntry(RspIdentifier identifier, RspEntry entry) - { } - - protected override IEnumerable<(RspIdentifier, RspEntry)> Enumerate() - => editor.MetaEditor.Eqp.Select(kvp => (kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))); -} - - - -public sealed class GlobalEqpMetaDrawer(ModEditor editor) : MetaDrawer, IService -{ - protected override void Initialize() - { - Identifier = new EqpIdentifier(1, EquipSlot.Body); - UpdateEntry(); - } - - private void UpdateEntry() - => Entry = ExpandedEqpFile.GetDefault(metaManager, Identifier.SetId); - - protected override void DrawNew() - { } - - protected override void DrawEntry(GlobalEqpManipulation identifier, byte _) - { } - - protected override IEnumerable<(GlobalEqpManipulation, byte)> Enumerate() - => editor.MetaEditor.Eqp.Select(kvp => (kvp.Key, kvp.Value.ToEntry(kvp.Key.Slot))); -} -#endif diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs index 1f935eb6..72ab37b2 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs @@ -95,7 +95,7 @@ public partial class ModEditWindow task.ContinueWith(t => { GamePaths = FinalizeIo(t); }, TaskScheduler.Default); } - private EstManipulation[] GetCurrentEstManipulations() + private KeyValuePair[] GetCurrentEstManipulations() { var mod = _edit._editor.Mod; var option = _edit._editor.Option; @@ -106,9 +106,7 @@ public partial class ModEditWindow return mod.AllDataContainers .Where(subMod => subMod != option) .Prepend(option) - .SelectMany(subMod => subMod.Manipulations) - .Where(manipulation => manipulation.ManipulationType is MetaManipulation.Type.Est) - .Select(manipulation => manipulation.Est) + .SelectMany(subMod => subMod.Manipulations.Est) .ToArray(); } diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index af01047b..9410b793 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -25,6 +25,7 @@ using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.String; using Penumbra.String.Classes; +using Penumbra.UI.AdvancedWindow.Meta; using Penumbra.UI.Classes; using Penumbra.Util; using MdlMaterialEditor = Penumbra.Mods.Editor.MdlMaterialEditor; @@ -586,7 +587,7 @@ public partial class ModEditWindow : Window, IDisposable StainService stainService, ActiveCollections activeCollections, ModMergeTab modMergeTab, CommunicatorService communicator, TextureManager textures, ModelManager models, IDragDropManager dragDropManager, ResourceTreeViewerFactory resourceTreeViewerFactory, ObjectManager objects, IFramework framework, - CharacterBaseDestructor characterBaseDestructor) + CharacterBaseDestructor characterBaseDestructor, MetaDrawers metaDrawers) : base(WindowBaseLabel) { _performance = performance; @@ -606,6 +607,7 @@ public partial class ModEditWindow : Window, IDisposable _objects = objects; _framework = framework; _characterBaseDestructor = characterBaseDestructor; + _metaDrawers = metaDrawers; _materialTab = new FileEditor(this, _communicator, gameData, config, _editor.Compactor, _fileDialog, "Materials", ".mtrl", () => PopulateIsOnPlayer(_editor.Files.Mtrl, ResourceType.Mtrl), DrawMaterialPanel, () => Mod?.ModPath.FullName ?? string.Empty, (bytes, path, writable) => new MtrlTab(this, new MtrlFile(bytes), path, writable)); diff --git a/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs b/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs index 5e3aac48..962d156d 100644 --- a/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs @@ -120,9 +120,9 @@ public class ModPanelConflictsTab(CollectionManager collectionManager, ModFileSy { var _ = data switch { - Utf8GamePath p => ImGuiNative.igSelectable_Bool(p.Path.Path, 0, ImGuiSelectableFlags.None, Vector2.Zero) > 0, - MetaManipulation m => ImGui.Selectable(m.Manipulation?.ToString() ?? string.Empty), - _ => false, + Utf8GamePath p => ImGuiNative.igSelectable_Bool(p.Path.Path, 0, ImGuiSelectableFlags.None, Vector2.Zero) > 0, + IMetaIdentifier m => ImGui.Selectable(m.ToString()), + _ => false, }; } } diff --git a/Penumbra/UI/Tabs/EffectiveTab.cs b/Penumbra/UI/Tabs/EffectiveTab.cs index 37561000..7076b80f 100644 --- a/Penumbra/UI/Tabs/EffectiveTab.cs +++ b/Penumbra/UI/Tabs/EffectiveTab.cs @@ -98,7 +98,7 @@ public class EffectiveTab(CollectionManager collectionManager, CollectionSelectH // Filters mean we can not use the known counts. if (hasFilters) { - var it2 = m.Select(p => (p.Key.ToString(), p.Value.Name)); + var it2 = m.IdentifierSources.Select(p => (p.Item1.ToString(), p.Item2.Name)); if (stop >= 0) { ImGuiClip.DrawEndDummy(stop + it2.Count(CheckFilters), height); @@ -117,7 +117,7 @@ public class EffectiveTab(CollectionManager collectionManager, CollectionSelectH } else { - stop = ImGuiClip.ClippedDraw(m, skips, DrawLine, m.Count, ~stop); + stop = ImGuiClip.ClippedDraw(m.IdentifierSources, skips, DrawLine, m.Count, ~stop); ImGuiClip.DrawEndDummy(stop, height); } } @@ -152,11 +152,11 @@ public class EffectiveTab(CollectionManager collectionManager, CollectionSelectH ImGui.TableNextColumn(); ImGuiUtil.PrintIcon(FontAwesomeIcon.LongArrowAltLeft); ImGui.TableNextColumn(); - ImGuiUtil.CopyOnClickSelectable(name); + ImGuiUtil.CopyOnClickSelectable(name.Text); } /// Draw a line for a unfiltered/unconverted manipulation and mod-index pair. - private static void DrawLine(KeyValuePair pair) + private static void DrawLine((IMetaIdentifier, IMod) pair) { var (manipulation, mod) = pair; ImGui.TableNextColumn(); @@ -165,7 +165,7 @@ public class EffectiveTab(CollectionManager collectionManager, CollectionSelectH ImGui.TableNextColumn(); ImGuiUtil.PrintIcon(FontAwesomeIcon.LongArrowAltLeft); ImGui.TableNextColumn(); - ImGuiUtil.CopyOnClickSelectable(mod.Name); + ImGuiUtil.CopyOnClickSelectable(mod.Name.Text); } /// Check filters for file replacements. diff --git a/Penumbra/Util/IdentifierExtensions.cs b/Penumbra/Util/IdentifierExtensions.cs index 0647ea8e..cb43ac06 100644 --- a/Penumbra/Util/IdentifierExtensions.cs +++ b/Penumbra/Util/IdentifierExtensions.cs @@ -2,7 +2,6 @@ using OtterGui.Classes; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; -using Penumbra.Meta.Manipulations; using Penumbra.Mods.Editor; using Penumbra.Mods.SubMods; @@ -10,103 +9,14 @@ namespace Penumbra.Util; public static class IdentifierExtensions { - /// Compute the items changed by a given meta manipulation and put them into the changedItems dictionary. - public static void MetaChangedItems(this ObjectIdentification identifier, IDictionary changedItems, - MetaManipulation manip) - { - switch (manip.ManipulationType) - { - case MetaManipulation.Type.Imc: - switch (manip.Imc.ObjectType) - { - case ObjectType.Equipment: - case ObjectType.Accessory: - identifier.Identify(changedItems, - GamePaths.Equipment.Mtrl.Path(manip.Imc.PrimaryId, GenderRace.MidlanderMale, manip.Imc.EquipSlot, manip.Imc.Variant, - "a")); - break; - case ObjectType.Weapon: - identifier.Identify(changedItems, - GamePaths.Weapon.Mtrl.Path(manip.Imc.PrimaryId, manip.Imc.SecondaryId, manip.Imc.Variant, "a")); - break; - case ObjectType.DemiHuman: - identifier.Identify(changedItems, - GamePaths.DemiHuman.Mtrl.Path(manip.Imc.PrimaryId, manip.Imc.SecondaryId, manip.Imc.EquipSlot, manip.Imc.Variant, - "a")); - break; - case ObjectType.Monster: - identifier.Identify(changedItems, - GamePaths.Monster.Mtrl.Path(manip.Imc.PrimaryId, manip.Imc.SecondaryId, manip.Imc.Variant, "a")); - break; - } - - break; - case MetaManipulation.Type.Eqdp: - identifier.Identify(changedItems, - GamePaths.Equipment.Mdl.Path(manip.Eqdp.SetId, Names.CombinedRace(manip.Eqdp.Gender, manip.Eqdp.Race), manip.Eqdp.Slot)); - break; - case MetaManipulation.Type.Eqp: - identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(manip.Eqp.SetId, GenderRace.MidlanderMale, manip.Eqp.Slot)); - break; - case MetaManipulation.Type.Est: - switch (manip.Est.Slot) - { - case EstType.Hair: - changedItems.TryAdd($"Customization: {manip.Est.Race} {manip.Est.Gender} Hair (Hair) {manip.Est.SetId}", null); - break; - case EstType.Face: - changedItems.TryAdd($"Customization: {manip.Est.Race} {manip.Est.Gender} Face (Face) {manip.Est.SetId}", null); - break; - case EstType.Body: - identifier.Identify(changedItems, - GamePaths.Equipment.Mdl.Path(manip.Est.SetId, Names.CombinedRace(manip.Est.Gender, manip.Est.Race), - EquipSlot.Body)); - break; - case EstType.Head: - identifier.Identify(changedItems, - GamePaths.Equipment.Mdl.Path(manip.Est.SetId, Names.CombinedRace(manip.Est.Gender, manip.Est.Race), - EquipSlot.Head)); - break; - } - - break; - case MetaManipulation.Type.Gmp: - identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(manip.Gmp.SetId, GenderRace.MidlanderMale, EquipSlot.Head)); - break; - case MetaManipulation.Type.Rsp: - changedItems.TryAdd($"{manip.Rsp.SubRace.ToName()} {manip.Rsp.Attribute.ToFullString()}", null); - break; - case MetaManipulation.Type.GlobalEqp: - var path = manip.GlobalEqp.Type switch - { - GlobalEqpType.DoNotHideEarrings => GamePaths.Accessory.Mdl.Path(manip.GlobalEqp.Condition, GenderRace.MidlanderMale, - EquipSlot.Ears), - GlobalEqpType.DoNotHideNecklace => GamePaths.Accessory.Mdl.Path(manip.GlobalEqp.Condition, GenderRace.MidlanderMale, - EquipSlot.Neck), - GlobalEqpType.DoNotHideBracelets => GamePaths.Accessory.Mdl.Path(manip.GlobalEqp.Condition, GenderRace.MidlanderMale, - EquipSlot.Wrists), - GlobalEqpType.DoNotHideRingR => GamePaths.Accessory.Mdl.Path(manip.GlobalEqp.Condition, GenderRace.MidlanderMale, - EquipSlot.RFinger), - GlobalEqpType.DoNotHideRingL => GamePaths.Accessory.Mdl.Path(manip.GlobalEqp.Condition, GenderRace.MidlanderMale, - EquipSlot.LFinger), - GlobalEqpType.DoNotHideHrothgarHats => string.Empty, - GlobalEqpType.DoNotHideVieraHats => string.Empty, - _ => string.Empty, - }; - if (path.Length > 0) - identifier.Identify(changedItems, path); - break; - } - } - public static void AddChangedItems(this ObjectIdentification identifier, IModDataContainer container, IDictionary changedItems) { foreach (var gamePath in container.Files.Keys.Concat(container.FileSwaps.Keys)) identifier.Identify(changedItems, gamePath.ToString()); - foreach (var manip in container.Manipulations) - MetaChangedItems(identifier, changedItems, manip); + foreach (var manip in container.Manipulations.Identifiers) + manip.AddChangedItems(identifier, changedItems); } public static void RemoveMachinistOffhands(this SortedList changedItems) From 4ca49598f8ffff55728e98e108183c88d679c2e8 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 14 Jun 2024 17:40:47 +0200 Subject: [PATCH 241/433] Small improvement. --- Penumbra/UI/ModsTab/ModPanelConflictsTab.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs b/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs index 962d156d..c1a3c1eb 100644 --- a/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs @@ -3,6 +3,7 @@ using Dalamud.Interface.Utility; using ImGuiNET; using OtterGui; using OtterGui.Raii; +using OtterGui.Text; using OtterGui.Widgets; using Penumbra.Collections.Cache; using Penumbra.Collections.Manager; @@ -116,15 +117,12 @@ public class ModPanelConflictsTab(CollectionManager collectionManager, ModFileSy using var indent = ImRaii.PushIndent(30f); foreach (var data in conflict.Conflicts) { - unsafe + _ = data switch { - var _ = data switch - { - Utf8GamePath p => ImGuiNative.igSelectable_Bool(p.Path.Path, 0, ImGuiSelectableFlags.None, Vector2.Zero) > 0, - IMetaIdentifier m => ImGui.Selectable(m.ToString()), - _ => false, - }; - } + Utf8GamePath p => ImUtf8.Selectable(p.Path.Span, false), + IMetaIdentifier m => ImUtf8.Selectable(m.ToString(), false), + _ => false, + }; } return true; From e33512cf7ff3dcc887b0516188e79d6d9fe09aa1 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 14 Jun 2024 23:09:38 +0200 Subject: [PATCH 242/433] Fix issue, remove IMetaCache. --- Penumbra/Collections/Cache/IMetaCache.cs | 14 +++----------- Penumbra/Meta/Manipulations/MetaDictionary.cs | 2 +- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/Penumbra/Collections/Cache/IMetaCache.cs b/Penumbra/Collections/Cache/IMetaCache.cs index 218c1840..eacbdcc2 100644 --- a/Penumbra/Collections/Cache/IMetaCache.cs +++ b/Penumbra/Collections/Cache/IMetaCache.cs @@ -4,21 +4,12 @@ using Penumbra.Mods.Editor; namespace Penumbra.Collections.Cache; -public interface IMetaCache : IDisposable -{ - public void SetFiles(); - public void Reset(); - public void ResetFiles(); - - public int Count { get; } -} - public abstract class MetaCacheBase - : Dictionary, IMetaCache + : Dictionary where TIdentifier : unmanaged, IMetaIdentifier where TEntry : unmanaged { - public MetaCacheBase(MetaFileManager manager, ModCollection collection) + protected MetaCacheBase(MetaFileManager manager, ModCollection collection) { Manager = manager; Collection = collection; @@ -76,6 +67,7 @@ public abstract class MetaCacheBase { IncorporateChangesInternal(); } + if (Manager.ActiveCollections.Default == Collection && Manager.Config.EnableMods) SetFiles(); } diff --git a/Penumbra/Meta/Manipulations/MetaDictionary.cs b/Penumbra/Meta/Manipulations/MetaDictionary.cs index 5a51df83..1093c6c5 100644 --- a/Penumbra/Meta/Manipulations/MetaDictionary.cs +++ b/Penumbra/Meta/Manipulations/MetaDictionary.cs @@ -346,7 +346,7 @@ public class MetaDictionary } failedIdentifier = default; - return false; + return true; } public void SetTo(MetaDictionary other) From ad0c64d4ac9e3b2d8ba89b7920b2f96a72480709 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 15 Jun 2024 11:37:39 +0200 Subject: [PATCH 243/433] Change Eqp hook to not need eqp files anymore. --- Penumbra/Collections/Cache/EqpCache.cs | 12 ++++++++++++ Penumbra/Collections/Cache/MetaCache.cs | 8 -------- Penumbra/Collections/ModCollection.Cache.Access.cs | 7 ------- Penumbra/Interop/Hooks/Meta/EqpHook.cs | 7 +++---- Penumbra/Interop/PathResolving/MetaState.cs | 5 ++--- Penumbra/UI/ConfigWindow.cs | 3 ++- 6 files changed, 19 insertions(+), 23 deletions(-) diff --git a/Penumbra/Collections/Cache/EqpCache.cs b/Penumbra/Collections/Cache/EqpCache.cs index 599ae588..8dde9aba 100644 --- a/Penumbra/Collections/Cache/EqpCache.cs +++ b/Penumbra/Collections/Cache/EqpCache.cs @@ -1,3 +1,4 @@ +using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.Services; using Penumbra.Interop.Structs; @@ -28,6 +29,17 @@ public sealed class EqpCache(MetaFileManager manager, ModCollection collection) Penumbra.Log.Verbose($"{Collection.AnonymizedName}: Loaded {Count} delayed EQP manipulations."); } + public unsafe EqpEntry GetValues(CharacterArmor* armor) + => GetSingleValue(armor[0].Set, EquipSlot.Head) + | GetSingleValue(armor[1].Set, EquipSlot.Body) + | GetSingleValue(armor[2].Set, EquipSlot.Hands) + | GetSingleValue(armor[3].Set, EquipSlot.Legs) + | GetSingleValue(armor[4].Set, EquipSlot.Feet); + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + private EqpEntry GetSingleValue(PrimaryId id, EquipSlot slot) + => TryGetValue(new EqpIdentifier(id, slot), out var pair) ? pair.Entry : ExpandedEqpFile.GetDefault(manager, id) & Eqp.Mask(slot); + public MetaList.MetaReverter TemporarilySetFile() => Manager.TemporarilySetFile(_eqpFile, MetaIndex.Eqp); diff --git a/Penumbra/Collections/Cache/MetaCache.cs b/Penumbra/Collections/Cache/MetaCache.cs index bc6ef34d..45a85d0f 100644 --- a/Penumbra/Collections/Cache/MetaCache.cs +++ b/Penumbra/Collections/Cache/MetaCache.cs @@ -1,4 +1,3 @@ -using System.IO.Pipes; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.Services; @@ -146,9 +145,6 @@ public class MetaCache(MetaFileManager manager, ModCollection collection) public void SetImcFiles(bool fromFullCompute) => Imc.SetFiles(fromFullCompute); - public MetaList.MetaReverter TemporarilySetEqpFile() - => Eqp.TemporarilySetFile(); - public MetaList.MetaReverter? TemporarilySetEqdpFile(GenderRace genderRace, bool accessory) => Eqdp.TemporarilySetFile(genderRace, accessory); @@ -161,10 +157,6 @@ public class MetaCache(MetaFileManager manager, ModCollection collection) public MetaList.MetaReverter TemporarilySetEstFile(EstType type) => Est.TemporarilySetFiles(type); - public unsafe EqpEntry ApplyGlobalEqp(EqpEntry baseEntry, CharacterArmor* armor) - => GlobalEqp.Apply(baseEntry, armor); - - /// Try to obtain a manipulated IMC file. public bool GetImcFile(Utf8GamePath path, [NotNullWhen(true)] out Meta.Files.ImcFile? file) => Imc.GetFile(path, out file); diff --git a/Penumbra/Collections/ModCollection.Cache.Access.cs b/Penumbra/Collections/ModCollection.Cache.Access.cs index 484d4dd2..3e3386ea 100644 --- a/Penumbra/Collections/ModCollection.Cache.Access.cs +++ b/Penumbra/Collections/ModCollection.Cache.Access.cs @@ -100,10 +100,6 @@ public partial class ModCollection return idx >= 0 ? utility.TemporarilyResetResource(idx) : null; } - public MetaList.MetaReverter TemporarilySetEqpFile(CharacterUtility utility) - => _cache?.Meta.TemporarilySetEqpFile() - ?? utility.TemporarilyResetResource(MetaIndex.Eqp); - public MetaList.MetaReverter TemporarilySetGmpFile(CharacterUtility utility) => _cache?.Meta.TemporarilySetGmpFile() ?? utility.TemporarilyResetResource(MetaIndex.Gmp); @@ -115,7 +111,4 @@ public partial class ModCollection public MetaList.MetaReverter TemporarilySetEstFile(CharacterUtility utility, EstType type) => _cache?.Meta.TemporarilySetEstFile(type) ?? utility.TemporarilyResetResource((MetaIndex)type); - - public unsafe EqpEntry ApplyGlobalEqp(EqpEntry baseEntry, CharacterArmor* armor) - => _cache?.Meta.ApplyGlobalEqp(baseEntry, armor) ?? baseEntry; } diff --git a/Penumbra/Interop/Hooks/Meta/EqpHook.cs b/Penumbra/Interop/Hooks/Meta/EqpHook.cs index 6663c211..448605c1 100644 --- a/Penumbra/Interop/Hooks/Meta/EqpHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqpHook.cs @@ -19,11 +19,10 @@ public unsafe class EqpHook : FastHook private void Detour(CharacterUtility* utility, EqpEntry* flags, CharacterArmor* armor) { - if (_metaState.EqpCollection.Valid) + if (_metaState.EqpCollection is { Valid: true, ModCollection.MetaCache: { } cache }) { - using var eqp = _metaState.ResolveEqpData(_metaState.EqpCollection.ModCollection); - Task.Result.Original(utility, flags, armor); - *flags = _metaState.EqpCollection.ModCollection.ApplyGlobalEqp(*flags, armor); + *flags = cache.Eqp.GetValues(armor); + *flags = cache.GlobalEqp.Apply(*flags, armor); } else { diff --git a/Penumbra/Interop/PathResolving/MetaState.cs b/Penumbra/Interop/PathResolving/MetaState.cs index 6fa5c263..de7912e0 100644 --- a/Penumbra/Interop/PathResolving/MetaState.cs +++ b/Penumbra/Interop/PathResolving/MetaState.cs @@ -47,6 +47,8 @@ public sealed unsafe class MetaState : IDisposable public ResolveData CustomizeChangeCollection = ResolveData.Invalid; public ResolveData EqpCollection = ResolveData.Invalid; + public ResolveData GmpCollection = ResolveData.Invalid; + public PrimaryId UndividedGmpId = 0; private ResolveData _lastCreatedCollection = ResolveData.Invalid; private DisposableContainer _characterBaseCreateMetaChanges = DisposableContainer.Empty; @@ -93,9 +95,6 @@ public sealed unsafe class MetaState : IDisposable _ => DisposableContainer.Empty, }; - public MetaList.MetaReverter ResolveEqpData(ModCollection collection) - => collection.TemporarilySetEqpFile(_characterUtility); - public MetaList.MetaReverter ResolveGmpData(ModCollection collection) => collection.TemporarilySetGmpFile(_characterUtility); diff --git a/Penumbra/UI/ConfigWindow.cs b/Penumbra/UI/ConfigWindow.cs index 9ae11fc3..0ae16f6d 100644 --- a/Penumbra/UI/ConfigWindow.cs +++ b/Penumbra/UI/ConfigWindow.cs @@ -4,6 +4,7 @@ using ImGuiNET; using OtterGui; using OtterGui.Custom; using OtterGui.Raii; +using OtterGui.Text; using Penumbra.Api.Enums; using Penumbra.Services; using Penumbra.UI.Classes; @@ -144,7 +145,7 @@ public sealed class ConfigWindow : Window using var color = ImRaii.PushColor(ImGuiCol.Text, Colors.RegexWarningBorder); ImGui.NewLine(); ImGui.NewLine(); - ImGuiUtil.TextWrapped(text); + ImUtf8.TextWrapped(text); color.Pop(); ImGui.NewLine(); From 30b32fdcd21e82d9f9e1176793f6b01082a79528 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 15 Jun 2024 12:09:41 +0200 Subject: [PATCH 244/433] Fix EQDP bug. --- Penumbra/Collections/Cache/EqpCache.cs | 2 +- Penumbra/Meta/Files/EqpGmpFile.cs | 12 ++---------- Penumbra/UI/AdvancedWindow/Meta/EqdpMetaDrawer.cs | 2 +- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/Penumbra/Collections/Cache/EqpCache.cs b/Penumbra/Collections/Cache/EqpCache.cs index 8dde9aba..32c4c0ae 100644 --- a/Penumbra/Collections/Cache/EqpCache.cs +++ b/Penumbra/Collections/Cache/EqpCache.cs @@ -38,7 +38,7 @@ public sealed class EqpCache(MetaFileManager manager, ModCollection collection) [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private EqpEntry GetSingleValue(PrimaryId id, EquipSlot slot) - => TryGetValue(new EqpIdentifier(id, slot), out var pair) ? pair.Entry : ExpandedEqpFile.GetDefault(manager, id) & Eqp.Mask(slot); + => TryGetValue(new EqpIdentifier(id, slot), out var pair) ? pair.Entry : ExpandedEqpFile.GetDefault(Manager, id) & Eqp.Mask(slot); public MetaList.MetaReverter TemporarilySetFile() => Manager.TemporarilySetFile(_eqpFile, MetaIndex.Eqp); diff --git a/Penumbra/Meta/Files/EqpGmpFile.cs b/Penumbra/Meta/Files/EqpGmpFile.cs index 17541c4f..a7470b75 100644 --- a/Penumbra/Meta/Files/EqpGmpFile.cs +++ b/Penumbra/Meta/Files/EqpGmpFile.cs @@ -104,15 +104,11 @@ public unsafe class ExpandedEqpGmpBase : MetaBaseFile } } -public sealed class ExpandedEqpFile : ExpandedEqpGmpBase, IEnumerable +public sealed class ExpandedEqpFile(MetaFileManager manager) : ExpandedEqpGmpBase(manager, false), IEnumerable { public static readonly CharacterUtility.InternalIndex InternalIndex = CharacterUtility.ReverseIndices[(int)MetaIndex.Eqp]; - public ExpandedEqpFile(MetaFileManager manager) - : base(manager, false) - { } - public EqpEntry this[PrimaryId idx] { get => (EqpEntry)GetInternal(idx); @@ -147,15 +143,11 @@ public sealed class ExpandedEqpFile : ExpandedEqpGmpBase, IEnumerable => GetEnumerator(); } -public sealed class ExpandedGmpFile : ExpandedEqpGmpBase, IEnumerable +public sealed class ExpandedGmpFile(MetaFileManager manager) : ExpandedEqpGmpBase(manager, true), IEnumerable { public static readonly CharacterUtility.InternalIndex InternalIndex = CharacterUtility.ReverseIndices[(int)MetaIndex.Gmp]; - public ExpandedGmpFile(MetaFileManager manager) - : base(manager, true) - { } - public GmpEntry this[PrimaryId idx] { get => new() { Value = GetInternal(idx) }; diff --git a/Penumbra/UI/AdvancedWindow/Meta/EqdpMetaDrawer.cs b/Penumbra/UI/AdvancedWindow/Meta/EqdpMetaDrawer.cs index b1ac93f1..970b70cb 100644 --- a/Penumbra/UI/AdvancedWindow/Meta/EqdpMetaDrawer.cs +++ b/Penumbra/UI/AdvancedWindow/Meta/EqdpMetaDrawer.cs @@ -112,7 +112,7 @@ public sealed class EqdpMetaDrawer(ModMetaEditor editor, MetaFileManager metaFil ImGui.SameLine(); if (Checkmark("Model##eqdp"u8, "\0"u8, entry.Model, defaultEntry.Model, out var newModel)) { - entry = entry with { Material = newModel }; + entry = entry with { Model = newModel }; changes = true; } From a61a96f1ef5bcc9e0c96210595c07a52c0ac8115 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 15 Jun 2024 14:04:16 +0200 Subject: [PATCH 245/433] Make GmpEntry readonly. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 0a2e2650..cf1ff07e 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 0a2e2650d693d1bba267498f96112682cc09dbab +Subproject commit cf1ff07e900e2f93ab628a1fa535fc2b103794a5 From a7b90639c6deb2859adeac608521bd664ac2cbca Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 15 Jun 2024 14:46:42 +0200 Subject: [PATCH 246/433] Some fixes. --- Penumbra/Collections/Cache/EqdpCache.cs | 10 ++++++---- Penumbra/Collections/Cache/EstCache.cs | 7 ++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Penumbra/Collections/Cache/EqdpCache.cs b/Penumbra/Collections/Cache/EqdpCache.cs index f3475c7e..7139bb72 100644 --- a/Penumbra/Collections/Cache/EqdpCache.cs +++ b/Penumbra/Collections/Cache/EqdpCache.cs @@ -1,4 +1,3 @@ -using FFXIVClientStructs.FFXIV.Client.Graphics.Render; using OtterGui; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; @@ -28,7 +27,10 @@ public sealed class EqdpCache(MetaFileManager manager, ModCollection collection) } public override void ResetFiles() - => Manager.SetFile(null, MetaIndex.Eqp); + { + foreach (var t in CharacterUtilityData.EqdpIndices) + Manager.SetFile(null, t); + } protected override void IncorporateChangesInternal() { @@ -89,8 +91,8 @@ public sealed class EqdpCache(MetaFileManager manager, ModCollection collection) var mask = Eqdp.Mask(identifier.Slot); if ((origEntry & mask) == entry) return false; - - file[identifier.SetId] = (entry & ~mask) | origEntry; + + file[identifier.SetId] = (origEntry & ~mask) | entry; return true; } diff --git a/Penumbra/Collections/Cache/EstCache.cs b/Penumbra/Collections/Cache/EstCache.cs index 412dd322..8ee530cc 100644 --- a/Penumbra/Collections/Cache/EstCache.cs +++ b/Penumbra/Collections/Cache/EstCache.cs @@ -55,7 +55,12 @@ public sealed class EstCache(MetaFileManager manager, ModCollection collection) } public override void ResetFiles() - => Manager.SetFile(null, MetaIndex.Eqp); + { + Manager.SetFile(null, MetaIndex.FaceEst); + Manager.SetFile(null, MetaIndex.HairEst); + Manager.SetFile(null, MetaIndex.BodyEst); + Manager.SetFile(null, MetaIndex.HeadEst); + } protected override void IncorporateChangesInternal() { From c53f29c257003bb8131441cc5bcf7086d808ad73 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 15 Jun 2024 15:19:48 +0200 Subject: [PATCH 247/433] Fix unnecessary EST file creations. --- Penumbra/Collections/Cache/EstCache.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Penumbra/Collections/Cache/EstCache.cs b/Penumbra/Collections/Cache/EstCache.cs index 8ee530cc..845ff128 100644 --- a/Penumbra/Collections/Cache/EstCache.cs +++ b/Penumbra/Collections/Cache/EstCache.cs @@ -74,7 +74,7 @@ public sealed class EstCache(MetaFileManager manager, ModCollection collection) public EstEntry GetEstEntry(EstIdentifier identifier) { - var file = GetFile(identifier); + var file = GetCurrentFile(identifier); return file != null ? file[identifier.GenderRace, identifier.SetId] : EstFile.GetDefault(Manager, identifier); @@ -124,9 +124,19 @@ public sealed class EstCache(MetaFileManager manager, ModCollection collection) Clear(); } + private EstFile? GetCurrentFile(EstIdentifier identifier) + => identifier.Slot switch + { + EstType.Hair => _estHairFile, + EstType.Face => _estFaceFile, + EstType.Body => _estBodyFile, + EstType.Head => _estHeadFile, + _ => null, + }; + private EstFile? GetFile(EstIdentifier identifier) { - if (Manager.CharacterUtility.Ready) + if (!Manager.CharacterUtility.Ready) return null; return identifier.Slot switch From 943207cae8923720fc8dbe56d763ddc3d1034bd4 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 15 Jun 2024 15:55:51 +0200 Subject: [PATCH 248/433] Make GMP independent of file, cleanup unused functions. --- Penumbra/Collections/Cache/EqdpCache.cs | 4 +- Penumbra/Collections/Cache/EqpCache.cs | 59 ++--------------- Penumbra/Collections/Cache/EstCache.cs | 4 +- Penumbra/Collections/Cache/GmpCache.cs | 59 ++--------------- Penumbra/Collections/Cache/IMetaCache.cs | 2 - Penumbra/Collections/Cache/ImcCache.cs | 4 +- Penumbra/Collections/Cache/MetaCache.cs | 3 - Penumbra/Collections/Cache/RspCache.cs | 5 +- .../Collections/ModCollection.Cache.Access.cs | 4 -- Penumbra/Interop/Hooks/Meta/GmpHook.cs | 64 +++++++++++++++++++ Penumbra/Interop/Hooks/Meta/SetupVisor.cs | 24 +++---- Penumbra/Interop/PathResolving/MetaState.cs | 3 - Penumbra/Interop/Services/MetaList.cs | 23 ++----- Penumbra/Meta/Files/EqpGmpFile.cs | 8 +-- 14 files changed, 108 insertions(+), 158 deletions(-) create mode 100644 Penumbra/Interop/Hooks/Meta/GmpHook.cs diff --git a/Penumbra/Collections/Cache/EqdpCache.cs b/Penumbra/Collections/Cache/EqdpCache.cs index 7139bb72..c63403ae 100644 --- a/Penumbra/Collections/Cache/EqdpCache.cs +++ b/Penumbra/Collections/Cache/EqdpCache.cs @@ -26,7 +26,7 @@ public sealed class EqdpCache(MetaFileManager manager, ModCollection collection) Manager.SetFile(_eqdpFiles[i], index); } - public override void ResetFiles() + public void ResetFiles() { foreach (var t in CharacterUtilityData.EqdpIndices) Manager.SetFile(null, t); @@ -62,7 +62,7 @@ public sealed class EqdpCache(MetaFileManager manager, ModCollection collection) return Manager.TemporarilySetFile(_eqdpFiles[i], idx); } - public override void Reset() + public void Reset() { foreach (var file in _eqdpFiles.OfType()) { diff --git a/Penumbra/Collections/Cache/EqpCache.cs b/Penumbra/Collections/Cache/EqpCache.cs index 32c4c0ae..b1e03943 100644 --- a/Penumbra/Collections/Cache/EqpCache.cs +++ b/Penumbra/Collections/Cache/EqpCache.cs @@ -1,7 +1,5 @@ using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; -using Penumbra.Interop.Services; -using Penumbra.Interop.Structs; using Penumbra.Meta; using Penumbra.Meta.Files; using Penumbra.Meta.Manipulations; @@ -10,24 +8,11 @@ namespace Penumbra.Collections.Cache; public sealed class EqpCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - private ExpandedEqpFile? _eqpFile; - public override void SetFiles() - => Manager.SetFile(_eqpFile, MetaIndex.Eqp); - - public override void ResetFiles() - => Manager.SetFile(null, MetaIndex.Eqp); + { } protected override void IncorporateChangesInternal() - { - if (GetFile() is not { } file) - return; - - foreach (var (identifier, (_, entry)) in this) - Apply(file, identifier, entry); - - Penumbra.Log.Verbose($"{Collection.AnonymizedName}: Loaded {Count} delayed EQP manipulations."); - } + { } public unsafe EqpEntry GetValues(CharacterArmor* armor) => GetSingleValue(armor[0].Set, EquipSlot.Head) @@ -40,29 +25,14 @@ public sealed class EqpCache(MetaFileManager manager, ModCollection collection) private EqpEntry GetSingleValue(PrimaryId id, EquipSlot slot) => TryGetValue(new EqpIdentifier(id, slot), out var pair) ? pair.Entry : ExpandedEqpFile.GetDefault(Manager, id) & Eqp.Mask(slot); - public MetaList.MetaReverter TemporarilySetFile() - => Manager.TemporarilySetFile(_eqpFile, MetaIndex.Eqp); - - public override void Reset() - { - if (_eqpFile == null) - return; - - _eqpFile.Reset(Keys.Select(identifier => identifier.SetId)); - Clear(); - } + public void Reset() + => Clear(); protected override void ApplyModInternal(EqpIdentifier identifier, EqpEntry entry) - { - if (GetFile() is { } file) - Apply(file, identifier, entry); - } + { } protected override void RevertModInternal(EqpIdentifier identifier) - { - if (GetFile() is { } file) - Apply(file, identifier, ExpandedEqpFile.GetDefault(Manager, identifier.SetId)); - } + { } public static bool Apply(ExpandedEqpFile file, EqpIdentifier identifier, EqpEntry entry) { @@ -76,20 +46,5 @@ public sealed class EqpCache(MetaFileManager manager, ModCollection collection) } protected override void Dispose(bool _) - { - _eqpFile?.Dispose(); - _eqpFile = null; - Clear(); - } - - private ExpandedEqpFile? GetFile() - { - if (_eqpFile != null) - return _eqpFile; - - if (!Manager.CharacterUtility.Ready) - return null; - - return _eqpFile = new ExpandedEqpFile(Manager); - } + => Clear(); } diff --git a/Penumbra/Collections/Cache/EstCache.cs b/Penumbra/Collections/Cache/EstCache.cs index 845ff128..6a9fa909 100644 --- a/Penumbra/Collections/Cache/EstCache.cs +++ b/Penumbra/Collections/Cache/EstCache.cs @@ -54,7 +54,7 @@ public sealed class EstCache(MetaFileManager manager, ModCollection collection) return Manager.TemporarilySetFile(file, idx); } - public override void ResetFiles() + public void ResetFiles() { Manager.SetFile(null, MetaIndex.FaceEst); Manager.SetFile(null, MetaIndex.HairEst); @@ -80,7 +80,7 @@ public sealed class EstCache(MetaFileManager manager, ModCollection collection) : EstFile.GetDefault(Manager, identifier); } - public override void Reset() + public void Reset() { _estFaceFile?.Reset(); _estHairFile?.Reset(); diff --git a/Penumbra/Collections/Cache/GmpCache.cs b/Penumbra/Collections/Cache/GmpCache.cs index 1475ffd5..0beb51d8 100644 --- a/Penumbra/Collections/Cache/GmpCache.cs +++ b/Penumbra/Collections/Cache/GmpCache.cs @@ -1,6 +1,4 @@ using Penumbra.GameData.Structs; -using Penumbra.Interop.Services; -using Penumbra.Interop.Structs; using Penumbra.Meta; using Penumbra.Meta.Files; using Penumbra.Meta.Manipulations; @@ -9,48 +7,20 @@ namespace Penumbra.Collections.Cache; public sealed class GmpCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - private ExpandedGmpFile? _gmpFile; - public override void SetFiles() - => Manager.SetFile(_gmpFile, MetaIndex.Gmp); - - public override void ResetFiles() - => Manager.SetFile(null, MetaIndex.Gmp); + { } protected override void IncorporateChangesInternal() - { - if (GetFile() is not { } file) - return; + { } - foreach (var (identifier, (_, entry)) in this) - Apply(file, identifier, entry); - - Penumbra.Log.Verbose($"{Collection.AnonymizedName}: Loaded {Count} delayed GMP manipulations."); - } - - public MetaList.MetaReverter TemporarilySetFile() - => Manager.TemporarilySetFile(_gmpFile, MetaIndex.Gmp); - - public override void Reset() - { - if (_gmpFile == null) - return; - - _gmpFile.Reset(Keys.Select(identifier => identifier.SetId)); - Clear(); - } + public void Reset() + => Clear(); protected override void ApplyModInternal(GmpIdentifier identifier, GmpEntry entry) - { - if (GetFile() is { } file) - Apply(file, identifier, entry); - } + { } protected override void RevertModInternal(GmpIdentifier identifier) - { - if (GetFile() is { } file) - Apply(file, identifier, ExpandedGmpFile.GetDefault(Manager, identifier.SetId)); - } + { } public static bool Apply(ExpandedGmpFile file, GmpIdentifier identifier, GmpEntry entry) { @@ -63,20 +33,5 @@ public sealed class GmpCache(MetaFileManager manager, ModCollection collection) } protected override void Dispose(bool _) - { - _gmpFile?.Dispose(); - _gmpFile = null; - Clear(); - } - - private ExpandedGmpFile? GetFile() - { - if (_gmpFile != null) - return _gmpFile; - - if (!Manager.CharacterUtility.Ready) - return null; - - return _gmpFile = new ExpandedGmpFile(Manager); - } + => Clear(); } diff --git a/Penumbra/Collections/Cache/IMetaCache.cs b/Penumbra/Collections/Cache/IMetaCache.cs index eacbdcc2..dd218b48 100644 --- a/Penumbra/Collections/Cache/IMetaCache.cs +++ b/Penumbra/Collections/Cache/IMetaCache.cs @@ -27,8 +27,6 @@ public abstract class MetaCacheBase } public abstract void SetFiles(); - public abstract void Reset(); - public abstract void ResetFiles(); public bool ApplyMod(IMod source, TIdentifier identifier, TEntry entry) { diff --git a/Penumbra/Collections/Cache/ImcCache.cs b/Penumbra/Collections/Cache/ImcCache.cs index 3d05e793..a9daf795 100644 --- a/Penumbra/Collections/Cache/ImcCache.cs +++ b/Penumbra/Collections/Cache/ImcCache.cs @@ -38,7 +38,7 @@ public sealed class ImcCache(MetaFileManager manager, ModCollection collection) Collection._cache!.ForceFile(path, PathDataHandler.CreateImc(path.Path, Collection)); } - public override void ResetFiles() + public void ResetFiles() { foreach (var (path, _) in _imcFiles) Collection._cache!.ForceFile(path, FullPath.Empty); @@ -56,7 +56,7 @@ public sealed class ImcCache(MetaFileManager manager, ModCollection collection) } - public override void Reset() + public void Reset() { foreach (var (path, (file, set)) in _imcFiles) { diff --git a/Penumbra/Collections/Cache/MetaCache.cs b/Penumbra/Collections/Cache/MetaCache.cs index 45a85d0f..c8a116eb 100644 --- a/Penumbra/Collections/Cache/MetaCache.cs +++ b/Penumbra/Collections/Cache/MetaCache.cs @@ -148,9 +148,6 @@ public class MetaCache(MetaFileManager manager, ModCollection collection) public MetaList.MetaReverter? TemporarilySetEqdpFile(GenderRace genderRace, bool accessory) => Eqdp.TemporarilySetFile(genderRace, accessory); - public MetaList.MetaReverter TemporarilySetGmpFile() - => Gmp.TemporarilySetFile(); - public MetaList.MetaReverter TemporarilySetCmpFile() => Rsp.TemporarilySetFile(); diff --git a/Penumbra/Collections/Cache/RspCache.cs b/Penumbra/Collections/Cache/RspCache.cs index 3889d6f1..8a5fe97d 100644 --- a/Penumbra/Collections/Cache/RspCache.cs +++ b/Penumbra/Collections/Cache/RspCache.cs @@ -13,9 +13,6 @@ public sealed class RspCache(MetaFileManager manager, ModCollection collection) public override void SetFiles() => Manager.SetFile(_cmpFile, MetaIndex.HumanCmp); - public override void ResetFiles() - => Manager.SetFile(null, MetaIndex.HumanCmp); - protected override void IncorporateChangesInternal() { if (GetFile() is not { } file) @@ -30,7 +27,7 @@ public sealed class RspCache(MetaFileManager manager, ModCollection collection) public MetaList.MetaReverter TemporarilySetFile() => Manager.TemporarilySetFile(_cmpFile, MetaIndex.HumanCmp); - public override void Reset() + public void Reset() { if (_cmpFile == null) return; diff --git a/Penumbra/Collections/ModCollection.Cache.Access.cs b/Penumbra/Collections/ModCollection.Cache.Access.cs index 3e3386ea..8701e3bb 100644 --- a/Penumbra/Collections/ModCollection.Cache.Access.cs +++ b/Penumbra/Collections/ModCollection.Cache.Access.cs @@ -100,10 +100,6 @@ public partial class ModCollection return idx >= 0 ? utility.TemporarilyResetResource(idx) : null; } - public MetaList.MetaReverter TemporarilySetGmpFile(CharacterUtility utility) - => _cache?.Meta.TemporarilySetGmpFile() - ?? utility.TemporarilyResetResource(MetaIndex.Gmp); - public MetaList.MetaReverter TemporarilySetCmpFile(CharacterUtility utility) => _cache?.Meta.TemporarilySetCmpFile() ?? utility.TemporarilyResetResource(MetaIndex.HumanCmp); diff --git a/Penumbra/Interop/Hooks/Meta/GmpHook.cs b/Penumbra/Interop/Hooks/Meta/GmpHook.cs new file mode 100644 index 00000000..60966fb7 --- /dev/null +++ b/Penumbra/Interop/Hooks/Meta/GmpHook.cs @@ -0,0 +1,64 @@ +using OtterGui.Services; +using Penumbra.Interop.PathResolving; +using Penumbra.Meta.Files; +using Penumbra.Meta.Manipulations; + +namespace Penumbra.Interop.Hooks.Meta; + +public unsafe class GmpHook : FastHook +{ + public delegate nint Delegate(nint gmpResource, uint dividedHeadId); + + private readonly MetaState _metaState; + + private static readonly Finalizer StablePointer = new(); + + public GmpHook(HookManager hooks, MetaState metaState) + { + _metaState = metaState; + Task = hooks.CreateHook("GetGmpEntry", "E8 ?? ?? ?? ?? 48 85 C0 74 ?? 43 8D 0C", Detour, true); + } + + /// + /// This function returns a pointer to the correct block in the GMP file, if it exists - cf. . + /// To work around this, we just have a single stable ulong accessible and offset the pointer to this by the required distance, + /// which is defined by the modulo of the original ID and the block size, if we return our own custom gmp entry. + /// + private nint Detour(nint gmpResource, uint dividedHeadId) + { + nint ret; + if (_metaState.GmpCollection is { Valid: true, ModCollection.MetaCache: { } cache } + && cache.Gmp.TryGetValue(new GmpIdentifier(_metaState.UndividedGmpId), out var entry)) + { + if (entry.Entry.Enabled) + { + *StablePointer.Pointer = entry.Entry.Value; + // This function already gets the original ID divided by the block size, so we can compute the modulo with a single multiplication and addition. + // We then go backwards from our pointer because this gets added by the calling functions. + ret = (nint)(StablePointer.Pointer - (_metaState.UndividedGmpId.Id - dividedHeadId * ExpandedEqpGmpBase.BlockSize)); + } + else + { + ret = nint.Zero; + } + } + else + { + ret = Task.Result.Original(gmpResource, dividedHeadId); + } + + Penumbra.Log.Excessive($"[GetGmpFlags] Invoked on 0x{gmpResource:X} with {dividedHeadId}, returned {ret:X10}."); + return ret; + } + + /// Allocate and clean up our single stable ulong pointer. + private class Finalizer + { + public readonly ulong* Pointer = (ulong*)Marshal.AllocHGlobal(8); + + ~Finalizer() + { + Marshal.FreeHGlobal((nint)Pointer); + } + } +} diff --git a/Penumbra/Interop/Hooks/Meta/SetupVisor.cs b/Penumbra/Interop/Hooks/Meta/SetupVisor.cs index e451f118..a3e56d7f 100644 --- a/Penumbra/Interop/Hooks/Meta/SetupVisor.cs +++ b/Penumbra/Interop/Hooks/Meta/SetupVisor.cs @@ -1,10 +1,11 @@ -using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; -using OtterGui.Services; -using Penumbra.GameData; -using Penumbra.Interop.PathResolving; - -namespace Penumbra.Interop.Hooks.Meta; - +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using OtterGui.Services; +using Penumbra.Collections; +using Penumbra.GameData; +using Penumbra.Interop.PathResolving; + +namespace Penumbra.Interop.Hooks.Meta; + /// /// GMP. This gets called every time when changing visor state, and it accesses the gmp file itself, /// but it only applies a changed gmp file after a redraw for some reason. @@ -26,10 +27,11 @@ public sealed unsafe class SetupVisor : FastHook [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private byte Detour(DrawObject* drawObject, ushort modelId, byte visorState) { - var collection = _collectionResolver.IdentifyCollection(drawObject, true); - using var gmp = _metaState.ResolveGmpData(collection.ModCollection); - var ret = Task.Result.Original.Invoke(drawObject, modelId, visorState); - Penumbra.Log.Excessive($"[Setup Visor] Invoked on {(nint)drawObject:X} with {modelId}, {visorState} -> {ret}."); + _metaState.GmpCollection = _collectionResolver.IdentifyCollection(drawObject, true); + _metaState.UndividedGmpId = modelId; + var ret = Task.Result.Original.Invoke(drawObject, modelId, visorState); + Penumbra.Log.Information($"[Setup Visor] Invoked on {(nint)drawObject:X} with {modelId}, {visorState} -> {ret}."); + _metaState.GmpCollection = ResolveData.Invalid; return ret; } } diff --git a/Penumbra/Interop/PathResolving/MetaState.cs b/Penumbra/Interop/PathResolving/MetaState.cs index de7912e0..c8ebe18f 100644 --- a/Penumbra/Interop/PathResolving/MetaState.cs +++ b/Penumbra/Interop/PathResolving/MetaState.cs @@ -95,9 +95,6 @@ public sealed unsafe class MetaState : IDisposable _ => DisposableContainer.Empty, }; - public MetaList.MetaReverter ResolveGmpData(ModCollection collection) - => collection.TemporarilySetGmpFile(_characterUtility); - public MetaList.MetaReverter ResolveRspData(ModCollection collection) => collection.TemporarilySetCmpFile(_characterUtility); diff --git a/Penumbra/Interop/Services/MetaList.cs b/Penumbra/Interop/Services/MetaList.cs index e956040b..dc472b8e 100644 --- a/Penumbra/Interop/Services/MetaList.cs +++ b/Penumbra/Interop/Services/MetaList.cs @@ -102,30 +102,19 @@ public unsafe class MetaList : IDisposable ResetResourceInternal(); } - public sealed class MetaReverter : IDisposable + public sealed class MetaReverter(MetaList metaList, nint data, int length) : IDisposable { public static readonly MetaReverter Disabled = new(null!) { Disposed = true }; - public readonly MetaList MetaList; - public readonly nint Data; - public readonly int Length; + public readonly MetaList MetaList = metaList; + public readonly nint Data = data; + public readonly int Length = length; public readonly bool Resetter; public bool Disposed; - public MetaReverter(MetaList metaList, nint data, int length) - { - MetaList = metaList; - Data = data; - Length = length; - } - public MetaReverter(MetaList metaList) - { - MetaList = metaList; - Data = nint.Zero; - Length = 0; - Resetter = true; - } + : this(metaList, nint.Zero, 0) + => Resetter = true; public void Dispose() { diff --git a/Penumbra/Meta/Files/EqpGmpFile.cs b/Penumbra/Meta/Files/EqpGmpFile.cs index a7470b75..c47c84ef 100644 --- a/Penumbra/Meta/Files/EqpGmpFile.cs +++ b/Penumbra/Meta/Files/EqpGmpFile.cs @@ -15,10 +15,10 @@ namespace Penumbra.Meta.Files; /// public unsafe class ExpandedEqpGmpBase : MetaBaseFile { - protected const int BlockSize = 160; - protected const int NumBlocks = 64; - protected const int EntrySize = 8; - protected const int MaxSize = BlockSize * NumBlocks * EntrySize; + public const int BlockSize = 160; + public const int NumBlocks = 64; + public const int EntrySize = 8; + public const int MaxSize = BlockSize * NumBlocks * EntrySize; public const int Count = BlockSize * NumBlocks; From ebef4ff650c19aee26306135ab125a451210bab9 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 15 Jun 2024 16:17:44 +0200 Subject: [PATCH 249/433] No EST files anymore. --- Penumbra/Collections/Cache/EqpCache.cs | 11 -- Penumbra/Collections/Cache/EstCache.cs | 136 ++---------------- Penumbra/Collections/Cache/GmpCache.cs | 11 -- Penumbra/Collections/Cache/MetaCache.cs | 6 - .../Collections/ModCollection.Cache.Access.cs | 4 - Penumbra/Interop/Hooks/Meta/EstHook.cs | 49 +++++++ .../Hooks/Resources/ResolvePathHooksBase.cs | 29 ++-- Penumbra/Interop/PathResolving/MetaState.cs | 5 +- 8 files changed, 68 insertions(+), 183 deletions(-) create mode 100644 Penumbra/Interop/Hooks/Meta/EstHook.cs diff --git a/Penumbra/Collections/Cache/EqpCache.cs b/Penumbra/Collections/Cache/EqpCache.cs index b1e03943..7ba0c489 100644 --- a/Penumbra/Collections/Cache/EqpCache.cs +++ b/Penumbra/Collections/Cache/EqpCache.cs @@ -34,17 +34,6 @@ public sealed class EqpCache(MetaFileManager manager, ModCollection collection) protected override void RevertModInternal(EqpIdentifier identifier) { } - public static bool Apply(ExpandedEqpFile file, EqpIdentifier identifier, EqpEntry entry) - { - var origEntry = file[identifier.SetId]; - var mask = Eqp.Mask(identifier.Slot); - if ((origEntry & mask) == entry) - return false; - - file[identifier.SetId] = (origEntry & ~mask) | entry; - return true; - } - protected override void Dispose(bool _) => Clear(); } diff --git a/Penumbra/Collections/Cache/EstCache.cs b/Penumbra/Collections/Cache/EstCache.cs index 6a9fa909..ff94324e 100644 --- a/Penumbra/Collections/Cache/EstCache.cs +++ b/Penumbra/Collections/Cache/EstCache.cs @@ -1,5 +1,3 @@ -using Penumbra.Interop.Services; -using Penumbra.Interop.Structs; using Penumbra.Meta; using Penumbra.Meta.Files; using Penumbra.Meta.Manipulations; @@ -8,144 +6,26 @@ namespace Penumbra.Collections.Cache; public sealed class EstCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - private EstFile? _estFaceFile; - private EstFile? _estHairFile; - private EstFile? _estBodyFile; - private EstFile? _estHeadFile; - public override void SetFiles() - { - Manager.SetFile(_estFaceFile, MetaIndex.FaceEst); - Manager.SetFile(_estHairFile, MetaIndex.HairEst); - Manager.SetFile(_estBodyFile, MetaIndex.BodyEst); - Manager.SetFile(_estHeadFile, MetaIndex.HeadEst); - } - - public void SetFile(MetaIndex index) - { - switch (index) - { - case MetaIndex.FaceEst: - Manager.SetFile(_estFaceFile, MetaIndex.FaceEst); - break; - case MetaIndex.HairEst: - Manager.SetFile(_estHairFile, MetaIndex.HairEst); - break; - case MetaIndex.BodyEst: - Manager.SetFile(_estBodyFile, MetaIndex.BodyEst); - break; - case MetaIndex.HeadEst: - Manager.SetFile(_estHeadFile, MetaIndex.HeadEst); - break; - } - } - - public MetaList.MetaReverter TemporarilySetFiles(EstType type) - { - var (file, idx) = type switch - { - EstType.Face => (_estFaceFile, MetaIndex.FaceEst), - EstType.Hair => (_estHairFile, MetaIndex.HairEst), - EstType.Body => (_estBodyFile, MetaIndex.BodyEst), - EstType.Head => (_estHeadFile, MetaIndex.HeadEst), - _ => (null, 0), - }; - - return Manager.TemporarilySetFile(file, idx); - } - - public void ResetFiles() - { - Manager.SetFile(null, MetaIndex.FaceEst); - Manager.SetFile(null, MetaIndex.HairEst); - Manager.SetFile(null, MetaIndex.BodyEst); - Manager.SetFile(null, MetaIndex.HeadEst); - } + { } protected override void IncorporateChangesInternal() - { - if (!Manager.CharacterUtility.Ready) - return; - - foreach (var (identifier, (_, entry)) in this) - Apply(GetFile(identifier)!, identifier, entry); - Penumbra.Log.Verbose($"{Collection.AnonymizedName}: Loaded {Count} delayed EST manipulations."); - } + { } public EstEntry GetEstEntry(EstIdentifier identifier) - { - var file = GetCurrentFile(identifier); - return file != null - ? file[identifier.GenderRace, identifier.SetId] + => TryGetValue(identifier, out var entry) + ? entry.Entry : EstFile.GetDefault(Manager, identifier); - } public void Reset() - { - _estFaceFile?.Reset(); - _estHairFile?.Reset(); - _estBodyFile?.Reset(); - _estHeadFile?.Reset(); - Clear(); - } + => Clear(); protected override void ApplyModInternal(EstIdentifier identifier, EstEntry entry) - { - if (GetFile(identifier) is { } file) - Apply(file, identifier, entry); - } + { } protected override void RevertModInternal(EstIdentifier identifier) - { - if (GetFile(identifier) is { } file) - Apply(file, identifier, EstFile.GetDefault(Manager, identifier.Slot, identifier.GenderRace, identifier.SetId)); - } - - public static bool Apply(EstFile file, EstIdentifier identifier, EstEntry entry) - => file.SetEntry(identifier.GenderRace, identifier.SetId, entry) switch - { - EstFile.EstEntryChange.Unchanged => false, - EstFile.EstEntryChange.Changed => true, - EstFile.EstEntryChange.Added => true, - EstFile.EstEntryChange.Removed => true, - _ => false, - }; + { } protected override void Dispose(bool _) - { - _estFaceFile?.Dispose(); - _estHairFile?.Dispose(); - _estBodyFile?.Dispose(); - _estHeadFile?.Dispose(); - _estFaceFile = null; - _estHairFile = null; - _estBodyFile = null; - _estHeadFile = null; - Clear(); - } - - private EstFile? GetCurrentFile(EstIdentifier identifier) - => identifier.Slot switch - { - EstType.Hair => _estHairFile, - EstType.Face => _estFaceFile, - EstType.Body => _estBodyFile, - EstType.Head => _estHeadFile, - _ => null, - }; - - private EstFile? GetFile(EstIdentifier identifier) - { - if (!Manager.CharacterUtility.Ready) - return null; - - return identifier.Slot switch - { - EstType.Hair => _estHairFile ??= new EstFile(Manager, EstType.Hair), - EstType.Face => _estFaceFile ??= new EstFile(Manager, EstType.Face), - EstType.Body => _estBodyFile ??= new EstFile(Manager, EstType.Body), - EstType.Head => _estHeadFile ??= new EstFile(Manager, EstType.Head), - _ => null, - }; - } + => Clear(); } diff --git a/Penumbra/Collections/Cache/GmpCache.cs b/Penumbra/Collections/Cache/GmpCache.cs index 0beb51d8..541b424d 100644 --- a/Penumbra/Collections/Cache/GmpCache.cs +++ b/Penumbra/Collections/Cache/GmpCache.cs @@ -1,6 +1,5 @@ using Penumbra.GameData.Structs; using Penumbra.Meta; -using Penumbra.Meta.Files; using Penumbra.Meta.Manipulations; namespace Penumbra.Collections.Cache; @@ -22,16 +21,6 @@ public sealed class GmpCache(MetaFileManager manager, ModCollection collection) protected override void RevertModInternal(GmpIdentifier identifier) { } - public static bool Apply(ExpandedGmpFile file, GmpIdentifier identifier, GmpEntry entry) - { - var origEntry = file[identifier.SetId]; - if (entry == origEntry) - return false; - - file[identifier.SetId] = entry; - return true; - } - protected override void Dispose(bool _) => Clear(); } diff --git a/Penumbra/Collections/Cache/MetaCache.cs b/Penumbra/Collections/Cache/MetaCache.cs index c8a116eb..014c7552 100644 --- a/Penumbra/Collections/Cache/MetaCache.cs +++ b/Penumbra/Collections/Cache/MetaCache.cs @@ -121,10 +121,8 @@ public class MetaCache(MetaFileManager manager, ModCollection collection) switch (metaIndex) { case MetaIndex.Eqp: - Eqp.SetFiles(); break; case MetaIndex.Gmp: - Gmp.SetFiles(); break; case MetaIndex.HumanCmp: Rsp.SetFiles(); @@ -133,7 +131,6 @@ public class MetaCache(MetaFileManager manager, ModCollection collection) case MetaIndex.HairEst: case MetaIndex.HeadEst: case MetaIndex.BodyEst: - Est.SetFile(metaIndex); break; default: Eqdp.SetFile(metaIndex); @@ -151,9 +148,6 @@ public class MetaCache(MetaFileManager manager, ModCollection collection) public MetaList.MetaReverter TemporarilySetCmpFile() => Rsp.TemporarilySetFile(); - public MetaList.MetaReverter TemporarilySetEstFile(EstType type) - => Est.TemporarilySetFiles(type); - /// Try to obtain a manipulated IMC file. public bool GetImcFile(Utf8GamePath path, [NotNullWhen(true)] out Meta.Files.ImcFile? file) => Imc.GetFile(path, out file); diff --git a/Penumbra/Collections/ModCollection.Cache.Access.cs b/Penumbra/Collections/ModCollection.Cache.Access.cs index 8701e3bb..dba971c6 100644 --- a/Penumbra/Collections/ModCollection.Cache.Access.cs +++ b/Penumbra/Collections/ModCollection.Cache.Access.cs @@ -103,8 +103,4 @@ public partial class ModCollection public MetaList.MetaReverter TemporarilySetCmpFile(CharacterUtility utility) => _cache?.Meta.TemporarilySetCmpFile() ?? utility.TemporarilyResetResource(MetaIndex.HumanCmp); - - public MetaList.MetaReverter TemporarilySetEstFile(CharacterUtility utility, EstType type) - => _cache?.Meta.TemporarilySetEstFile(type) - ?? utility.TemporarilyResetResource((MetaIndex)type); } diff --git a/Penumbra/Interop/Hooks/Meta/EstHook.cs b/Penumbra/Interop/Hooks/Meta/EstHook.cs new file mode 100644 index 00000000..3fab1434 --- /dev/null +++ b/Penumbra/Interop/Hooks/Meta/EstHook.cs @@ -0,0 +1,49 @@ +using OtterGui.Services; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Interop.PathResolving; +using Penumbra.Meta.Manipulations; + +namespace Penumbra.Interop.Hooks.Meta; + +public class EstHook : FastHook +{ + public delegate EstEntry Delegate(uint id, int estType, uint genderRace); + + private readonly MetaState _metaState; + + public EstHook(HookManager hooks, MetaState metaState) + { + _metaState = metaState; + Task = hooks.CreateHook("GetEstEntry", "44 8B C9 83 EA ?? 74", Detour, true); + } + + private EstEntry Detour(uint genderRace, int estType, uint id) + { + EstEntry ret; + if (_metaState.EstCollection is { Valid: true, ModCollection.MetaCache: { } cache } + && cache.Est.TryGetValue(Convert(genderRace, estType, id), out var entry)) + ret = entry.Entry; + else + ret = Task.Result.Original(genderRace, estType, id); + + Penumbra.Log.Information($"[GetEstEntry] Invoked with {genderRace}, {estType}, {id}, returned {ret.Value}."); + return ret; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static EstIdentifier Convert(uint genderRace, int estType, uint id) + { + var i = new PrimaryId((ushort)id); + var gr = (GenderRace)genderRace; + var type = estType switch + { + 1 => EstType.Face, + 2 => EstType.Hair, + 3 => EstType.Head, + 4 => EstType.Body, + _ => (EstType)0, + }; + return new EstIdentifier(i, type, gr); + } +} diff --git a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs index 9a68160b..6c9c1b7d 100644 --- a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs +++ b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs @@ -158,26 +158,26 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable private nint ResolvePapHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint unkAnimationIndex, nint animationName) { - using var est = GetEstChanges(drawObject, out var data); - return ResolvePath(data, _resolvePapPathHook.Original(drawObject, pathBuffer, pathBufferSize, unkAnimationIndex, animationName)); + _parent.MetaState.EstCollection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); + return ResolvePath(_parent.MetaState.EstCollection, _resolvePapPathHook.Original(drawObject, pathBuffer, pathBufferSize, unkAnimationIndex, animationName)); } private nint ResolvePhybHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint partialSkeletonIndex) { - using var est = GetEstChanges(drawObject, out var data); - return ResolvePath(data, _resolvePhybPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); + _parent.MetaState.EstCollection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); + return ResolvePath(_parent.MetaState.EstCollection, _resolvePhybPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); } private nint ResolveSklbHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint partialSkeletonIndex) { - using var est = GetEstChanges(drawObject, out var data); - return ResolvePath(data, _resolveSklbPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); + _parent.MetaState.EstCollection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); + return ResolvePath(_parent.MetaState.EstCollection, _resolveSklbPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); } private nint ResolveSkpHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint partialSkeletonIndex) { - using var est = GetEstChanges(drawObject, out var data); - return ResolvePath(data, _resolveSkpPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); + _parent.MetaState.EstCollection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); + return ResolvePath(_parent.MetaState.EstCollection, _resolveSkpPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); } private nint ResolveVfxHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint slotIndex, nint unkOutParam) @@ -206,19 +206,6 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable return ResolvePath(drawObject, pathBuffer); } - private DisposableContainer GetEstChanges(nint drawObject, out ResolveData data) - { - data = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); - if (_parent.InInternalResolve) - return DisposableContainer.Empty; - - return new DisposableContainer(data.ModCollection.TemporarilySetEstFile(_parent.CharacterUtility, EstType.Face), - data.ModCollection.TemporarilySetEstFile(_parent.CharacterUtility, EstType.Body), - data.ModCollection.TemporarilySetEstFile(_parent.CharacterUtility, EstType.Hair), - data.ModCollection.TemporarilySetEstFile(_parent.CharacterUtility, EstType.Head)); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static Hook Create(string name, HookManager hooks, nint address, Type type, T other, T human) where T : Delegate { diff --git a/Penumbra/Interop/PathResolving/MetaState.cs b/Penumbra/Interop/PathResolving/MetaState.cs index c8ebe18f..8fa09232 100644 --- a/Penumbra/Interop/PathResolving/MetaState.cs +++ b/Penumbra/Interop/PathResolving/MetaState.cs @@ -18,7 +18,7 @@ namespace Penumbra.Interop.PathResolving; // GetSlotEqpData seems to be the only function using the EQP table. // It is only called by CheckSlotsForUnload (called by UpdateModels), // SetupModelAttributes (called by UpdateModels and OnModelLoadComplete) -// and a unnamed function called by UpdateRender. +// and an unnamed function called by UpdateRender. // It seems to be enough to change the EQP entries for UpdateModels. // GetEqdpDataFor[Adults|Children|Other] seem to be the only functions using the EQDP tables. @@ -35,7 +35,7 @@ namespace Penumbra.Interop.PathResolving; // they all are called by many functions, but the most relevant seem to be Human.SetupFromCharacterData, which is only called by CharacterBase.Create, // ChangeCustomize and RspSetupCharacter, which is hooked here, as well as Character.CalculateHeight. -// GMP Entries seem to be only used by "48 8B ?? 53 55 57 48 83 ?? ?? 48 8B", which has a DrawObject as its first parameter. +// GMP Entries seem to be only used by "48 8B ?? 53 55 57 48 83 ?? ?? 48 8B", which is SetupVisor. public sealed unsafe class MetaState : IDisposable { private readonly Configuration _config; @@ -48,6 +48,7 @@ public sealed unsafe class MetaState : IDisposable public ResolveData CustomizeChangeCollection = ResolveData.Invalid; public ResolveData EqpCollection = ResolveData.Invalid; public ResolveData GmpCollection = ResolveData.Invalid; + public ResolveData EstCollection = ResolveData.Invalid; public PrimaryId UndividedGmpId = 0; private ResolveData _lastCreatedCollection = ResolveData.Invalid; From 9ecc4ab46d1b04e1bea0a6816d3e8ab06b862812 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 15 Jun 2024 20:47:08 +0200 Subject: [PATCH 250/433] Remove CMP file. --- Penumbra/Collections/Cache/MetaCache.cs | 3 - Penumbra/Collections/Cache/RspCache.cs | 64 ++--------------- .../Collections/ModCollection.Cache.Access.cs | 4 -- .../Interop/Hooks/Meta/CalculateHeight.cs | 7 +- .../Interop/Hooks/Meta/ChangeCustomize.cs | 3 +- Penumbra/Interop/Hooks/Meta/EstHook.cs | 2 +- Penumbra/Interop/Hooks/Meta/RspBustHook.cs | 66 ++++++++++++++++++ Penumbra/Interop/Hooks/Meta/RspHeightHook.cs | 68 +++++++++++++++++++ .../Interop/Hooks/Meta/RspSetupCharacter.cs | 5 +- Penumbra/Interop/Hooks/Meta/RspTailHook.cs | 68 +++++++++++++++++++ Penumbra/Interop/Hooks/Meta/SetupVisor.cs | 2 +- .../Hooks/Resources/ResolvePathHooksBase.cs | 16 +++-- Penumbra/Interop/PathResolving/MetaState.cs | 9 ++- Penumbra/Meta/Files/CmpFile.cs | 8 +++ .../UI/AdvancedWindow/Meta/RspMetaDrawer.cs | 2 +- 15 files changed, 244 insertions(+), 83 deletions(-) create mode 100644 Penumbra/Interop/Hooks/Meta/RspBustHook.cs create mode 100644 Penumbra/Interop/Hooks/Meta/RspHeightHook.cs create mode 100644 Penumbra/Interop/Hooks/Meta/RspTailHook.cs diff --git a/Penumbra/Collections/Cache/MetaCache.cs b/Penumbra/Collections/Cache/MetaCache.cs index 014c7552..e6083351 100644 --- a/Penumbra/Collections/Cache/MetaCache.cs +++ b/Penumbra/Collections/Cache/MetaCache.cs @@ -145,9 +145,6 @@ public class MetaCache(MetaFileManager manager, ModCollection collection) public MetaList.MetaReverter? TemporarilySetEqdpFile(GenderRace genderRace, bool accessory) => Eqdp.TemporarilySetFile(genderRace, accessory); - public MetaList.MetaReverter TemporarilySetCmpFile() - => Rsp.TemporarilySetFile(); - /// Try to obtain a manipulated IMC file. public bool GetImcFile(Utf8GamePath path, [NotNullWhen(true)] out Meta.Files.ImcFile? file) => Imc.GetFile(path, out file); diff --git a/Penumbra/Collections/Cache/RspCache.cs b/Penumbra/Collections/Cache/RspCache.cs index 8a5fe97d..8a983c6c 100644 --- a/Penumbra/Collections/Cache/RspCache.cs +++ b/Penumbra/Collections/Cache/RspCache.cs @@ -1,78 +1,26 @@ -using Penumbra.Interop.Services; -using Penumbra.Interop.Structs; using Penumbra.Meta; -using Penumbra.Meta.Files; using Penumbra.Meta.Manipulations; namespace Penumbra.Collections.Cache; public sealed class RspCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - private CmpFile? _cmpFile; - public override void SetFiles() - => Manager.SetFile(_cmpFile, MetaIndex.HumanCmp); + { } protected override void IncorporateChangesInternal() - { - if (GetFile() is not { } file) - return; - - foreach (var (identifier, (_, entry)) in this) - Apply(file, identifier, entry); - - Penumbra.Log.Verbose($"{Collection.AnonymizedName}: Loaded {Count} delayed RSP manipulations."); - } - - public MetaList.MetaReverter TemporarilySetFile() - => Manager.TemporarilySetFile(_cmpFile, MetaIndex.HumanCmp); + { } public void Reset() - { - if (_cmpFile == null) - return; - - _cmpFile.Reset(Keys.Select(identifier => (identifier.SubRace, identifier.Attribute))); - Clear(); - } + => Clear(); protected override void ApplyModInternal(RspIdentifier identifier, RspEntry entry) - { - if (GetFile() is { } file) - Apply(file, identifier, entry); - } + { } protected override void RevertModInternal(RspIdentifier identifier) - { - if (GetFile() is { } file) - Apply(file, identifier, CmpFile.GetDefault(Manager, identifier.SubRace, identifier.Attribute)); - } + { } - public static bool Apply(CmpFile file, RspIdentifier identifier, RspEntry entry) - { - var value = file[identifier.SubRace, identifier.Attribute]; - if (value == entry) - return false; - - file[identifier.SubRace, identifier.Attribute] = entry; - return true; - } protected override void Dispose(bool _) - { - _cmpFile?.Dispose(); - _cmpFile = null; - Clear(); - } - - private CmpFile? GetFile() - { - if (_cmpFile != null) - return _cmpFile; - - if (!Manager.CharacterUtility.Ready) - return null; - - return _cmpFile = new CmpFile(Manager); - } + => Clear(); } diff --git a/Penumbra/Collections/ModCollection.Cache.Access.cs b/Penumbra/Collections/ModCollection.Cache.Access.cs index dba971c6..d93a0f53 100644 --- a/Penumbra/Collections/ModCollection.Cache.Access.cs +++ b/Penumbra/Collections/ModCollection.Cache.Access.cs @@ -99,8 +99,4 @@ public partial class ModCollection var idx = CharacterUtilityData.EqdpIdx(genderRace, accessory); return idx >= 0 ? utility.TemporarilyResetResource(idx) : null; } - - public MetaList.MetaReverter TemporarilySetCmpFile(CharacterUtility utility) - => _cache?.Meta.TemporarilySetCmpFile() - ?? utility.TemporarilyResetResource(MetaIndex.HumanCmp); } diff --git a/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs b/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs index 2fd87f6e..5a207491 100644 --- a/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs +++ b/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs @@ -1,5 +1,6 @@ using FFXIVClientStructs.FFXIV.Client.Game.Object; using OtterGui.Services; +using Penumbra.Collections; using Penumbra.Interop.PathResolving; using Character = FFXIVClientStructs.FFXIV.Client.Game.Character.Character; @@ -22,10 +23,10 @@ public sealed unsafe class CalculateHeight : FastHook [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private ulong Detour(Character* character) { - var collection = _collectionResolver.IdentifyCollection((GameObject*)character, true); - using var cmp = _metaState.ResolveRspData(collection.ModCollection); - var ret = Task.Result.Original.Invoke(character); + _metaState.RspCollection = _collectionResolver.IdentifyCollection((GameObject*)character, true); + var ret = Task.Result.Original.Invoke(character); Penumbra.Log.Excessive($"[Calculate Height] Invoked on {(nint)character:X} -> {ret}."); + _metaState.RspCollection = ResolveData.Invalid; return ret; } } diff --git a/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs b/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs index 2f717491..4e0a5744 100644 --- a/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs +++ b/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs @@ -25,12 +25,13 @@ public sealed unsafe class ChangeCustomize : FastHook private bool Detour(Human* human, CustomizeArray* data, byte skipEquipment) { _metaState.CustomizeChangeCollection = _collectionResolver.IdentifyCollection((DrawObject*)human, true); - using var cmp = _metaState.ResolveRspData(_metaState.CustomizeChangeCollection.ModCollection); + _metaState.RspCollection = _metaState.CustomizeChangeCollection; using var decal1 = _metaState.ResolveDecal(_metaState.CustomizeChangeCollection, true); using var decal2 = _metaState.ResolveDecal(_metaState.CustomizeChangeCollection, false); var ret = Task.Result.Original.Invoke(human, data, skipEquipment); Penumbra.Log.Excessive($"[Change Customize] Invoked on {(nint)human:X} with {(nint)data:X}, {skipEquipment} -> {ret}."); _metaState.CustomizeChangeCollection = ResolveData.Invalid; + _metaState.RspCollection = ResolveData.Invalid; return ret; } } diff --git a/Penumbra/Interop/Hooks/Meta/EstHook.cs b/Penumbra/Interop/Hooks/Meta/EstHook.cs index 3fab1434..34935edb 100644 --- a/Penumbra/Interop/Hooks/Meta/EstHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EstHook.cs @@ -27,7 +27,7 @@ public class EstHook : FastHook else ret = Task.Result.Original(genderRace, estType, id); - Penumbra.Log.Information($"[GetEstEntry] Invoked with {genderRace}, {estType}, {id}, returned {ret.Value}."); + Penumbra.Log.Excessive($"[GetEstEntry] Invoked with {genderRace}, {estType}, {id}, returned {ret.Value}."); return ret; } diff --git a/Penumbra/Interop/Hooks/Meta/RspBustHook.cs b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs new file mode 100644 index 00000000..fc1d743a --- /dev/null +++ b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs @@ -0,0 +1,66 @@ +using OtterGui.Services; +using Penumbra.GameData.Enums; +using Penumbra.Interop.PathResolving; +using Penumbra.Meta; +using Penumbra.Meta.Files; +using Penumbra.Meta.Manipulations; + +namespace Penumbra.Interop.Hooks.Meta; + +public unsafe class RspBustHook : FastHook +{ + public delegate float* Delegate(nint cmpResource, float* storage, Race race, byte gender, byte isSecondSubRace, byte bodyType, + byte bustSize); + + private readonly MetaState _metaState; + private readonly MetaFileManager _metaFileManager; + + public RspBustHook(HookManager hooks, MetaState metaState, MetaFileManager metaFileManager) + { + _metaState = metaState; + _metaFileManager = metaFileManager; + Task = hooks.CreateHook("GetRspBust", "E8 ?? ?? ?? ?? F2 0F 10 44 24 ?? 8B 44 24", Detour, true); + } + + private float* Detour(nint cmpResource, float* storage, Race race, byte gender, byte isSecondSubRace, byte bodyType, byte bustSize) + { + if (gender == 0) + { + storage[0] = 1f; + storage[1] = 1f; + storage[2] = 1f; + return storage; + } + + var ret = storage; + if (bodyType < 2 && _metaState.RspCollection is { Valid: true, ModCollection.MetaCache: { } cache }) + { + var bustScale = bustSize / 100f; + var clan = (SubRace)(((int)race - 1) * 2 + 1 + isSecondSubRace); + var ptr = CmpFile.GetDefaults(_metaFileManager, clan, RspAttribute.BustMinX); + storage[0] = GetValue(0, RspAttribute.BustMinX, RspAttribute.BustMaxX); + storage[1] = GetValue(1, RspAttribute.BustMinY, RspAttribute.BustMaxY); + storage[2] = GetValue(2, RspAttribute.BustMinZ, RspAttribute.BustMaxZ); + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + float GetValue(int dimension, RspAttribute min, RspAttribute max) + { + var minValue = cache.Rsp.TryGetValue(new RspIdentifier(clan, min), out var minEntry) + ? minEntry.Entry.Value + : (ptr + dimension)->Value; + var maxValue = cache.Rsp.TryGetValue(new RspIdentifier(clan, max), out var maxEntry) + ? maxEntry.Entry.Value + : (ptr + 3 + dimension)->Value; + return (maxValue - minValue) * bustScale + minValue; + } + } + else + { + ret = Task.Result.Original(cmpResource, storage, race, gender, isSecondSubRace, bodyType, bustSize); + } + + Penumbra.Log.Information( + $"[GetRspBust] Invoked on 0x{cmpResource:X} with {race}, {(Gender)(gender + 1)}, {isSecondSubRace == 1}, {bodyType}, {bustSize}, returned {storage[0]}, {storage[1]}, {storage[2]}."); + return ret; + } +} diff --git a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs new file mode 100644 index 00000000..883f5fc6 --- /dev/null +++ b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs @@ -0,0 +1,68 @@ +using OtterGui.Services; +using Penumbra.GameData.Enums; +using Penumbra.Interop.PathResolving; +using Penumbra.Meta; +using Penumbra.Meta.Files; +using Penumbra.Meta.Manipulations; + +namespace Penumbra.Interop.Hooks.Meta; + +public class RspHeightHook : FastHook +{ + public delegate float Delegate(nint cmpResource, Race clan, byte gender, byte isSecondSubRace, byte bodyType, byte height); + + private readonly MetaState _metaState; + private readonly MetaFileManager _metaFileManager; + + public RspHeightHook(HookManager hooks, MetaState metaState, MetaFileManager metaFileManager) + { + _metaState = metaState; + _metaFileManager = metaFileManager; + Task = hooks.CreateHook("GetRspHeight", "E8 ?? ?? ?? ?? 48 8B 8E ?? ?? ?? ?? 44 8B CF", Detour, true); + } + + private unsafe float Detour(nint cmpResource, Race race, byte gender, byte isSecondSubRace, byte bodyType, byte height) + { + float scale; + if (bodyType < 2 && _metaState.RspCollection is { Valid: true, ModCollection.MetaCache: { } cache }) + { + var clan = (SubRace)(((int)race - 1) * 2 + 1 + isSecondSubRace); + var (minIdent, maxIdent) = gender == 0 + ? (new RspIdentifier(clan, RspAttribute.MaleMinSize), new RspIdentifier(clan, RspAttribute.MaleMaxSize)) + : (new RspIdentifier(clan, RspAttribute.FemaleMinSize), new RspIdentifier(clan, RspAttribute.FemaleMaxSize)); + + float minEntry, maxEntry; + if (cache.Rsp.TryGetValue(minIdent, out var min)) + { + minEntry = min.Entry.Value; + maxEntry = cache.Rsp.TryGetValue(maxIdent, out var max) + ? max.Entry.Value + : CmpFile.GetDefault(_metaFileManager, minIdent.SubRace, maxIdent.Attribute).Value; + } + else + { + var ptr = CmpFile.GetDefaults(_metaFileManager, minIdent.SubRace, minIdent.Attribute); + if (cache.Rsp.TryGetValue(maxIdent, out var max)) + { + minEntry = ptr->Value; + maxEntry = max.Entry.Value; + } + else + { + minEntry = ptr[0].Value; + maxEntry = ptr[1].Value; + } + } + + scale = (maxEntry - minEntry) * height / 100f + minEntry; + } + else + { + scale = Task.Result.Original(cmpResource, race, gender, isSecondSubRace, bodyType, height); + } + + Penumbra.Log.Excessive( + $"[GetRspHeight] Invoked on 0x{cmpResource:X} with {race}, {(Gender)(gender + 1)}, {isSecondSubRace == 1}, {bodyType}, {height}, returned {scale}."); + return scale; + } +} diff --git a/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs b/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs index 8f8f1d78..831c99bb 100644 --- a/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs +++ b/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs @@ -1,5 +1,6 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; +using Penumbra.Collections; using Penumbra.GameData; using Penumbra.Interop.PathResolving; @@ -30,8 +31,8 @@ public sealed unsafe class RspSetupCharacter : FastHook +{ + public delegate float Delegate(nint cmpResource, Race clan, byte gender, byte isSecondSubRace, byte bodyType, byte height); + + private readonly MetaState _metaState; + private readonly MetaFileManager _metaFileManager; + + public RspTailHook(HookManager hooks, MetaState metaState, MetaFileManager metaFileManager) + { + _metaState = metaState; + _metaFileManager = metaFileManager; + Task = hooks.CreateHook("GetRspTail", "E8 ?? ?? ?? ?? 0F 28 F0 48 8B 05", Detour, true); + } + + private unsafe float Detour(nint cmpResource, Race race, byte gender, byte isSecondSubRace, byte bodyType, byte tailLength) + { + float scale; + if (bodyType < 2 && _metaState.RspCollection is { Valid: true, ModCollection.MetaCache: { } cache }) + { + var clan = (SubRace)(((int)race - 1) * 2 + 1 + isSecondSubRace); + var (minIdent, maxIdent) = gender == 0 + ? (new RspIdentifier(clan, RspAttribute.MaleMinTail), new RspIdentifier(clan, RspAttribute.MaleMaxTail)) + : (new RspIdentifier(clan, RspAttribute.FemaleMinTail), new RspIdentifier(clan, RspAttribute.FemaleMaxTail)); + + float minEntry, maxEntry; + if (cache.Rsp.TryGetValue(minIdent, out var min)) + { + minEntry = min.Entry.Value; + maxEntry = cache.Rsp.TryGetValue(maxIdent, out var max) + ? max.Entry.Value + : CmpFile.GetDefault(_metaFileManager, minIdent.SubRace, maxIdent.Attribute).Value; + } + else + { + var ptr = CmpFile.GetDefaults(_metaFileManager, minIdent.SubRace, minIdent.Attribute); + if (cache.Rsp.TryGetValue(maxIdent, out var max)) + { + minEntry = ptr->Value; + maxEntry = max.Entry.Value; + } + else + { + minEntry = ptr[0].Value; + maxEntry = ptr[1].Value; + } + } + + scale = (maxEntry - minEntry) * tailLength / 100f + minEntry; + } + else + { + scale = Task.Result.Original(cmpResource, race, gender, isSecondSubRace, bodyType, tailLength); + } + + Penumbra.Log.Excessive( + $"[GetRspTail] Invoked on 0x{cmpResource:X} with {race}, {(Gender)(gender + 1)}, {isSecondSubRace == 1}, {bodyType}, {tailLength}, returned {scale}."); + return scale; + } +} diff --git a/Penumbra/Interop/Hooks/Meta/SetupVisor.cs b/Penumbra/Interop/Hooks/Meta/SetupVisor.cs index a3e56d7f..8479968f 100644 --- a/Penumbra/Interop/Hooks/Meta/SetupVisor.cs +++ b/Penumbra/Interop/Hooks/Meta/SetupVisor.cs @@ -30,7 +30,7 @@ public sealed unsafe class SetupVisor : FastHook _metaState.GmpCollection = _collectionResolver.IdentifyCollection(drawObject, true); _metaState.UndividedGmpId = modelId; var ret = Task.Result.Original.Invoke(drawObject, modelId, visorState); - Penumbra.Log.Information($"[Setup Visor] Invoked on {(nint)drawObject:X} with {modelId}, {visorState} -> {ret}."); + Penumbra.Log.Excessive($"[Setup Visor] Invoked on {(nint)drawObject:X} with {modelId}, {visorState} -> {ret}."); _metaState.GmpCollection = ResolveData.Invalid; return ret; } diff --git a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs index 6c9c1b7d..17cfa3f6 100644 --- a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs +++ b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs @@ -159,25 +159,33 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable private nint ResolvePapHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint unkAnimationIndex, nint animationName) { _parent.MetaState.EstCollection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); - return ResolvePath(_parent.MetaState.EstCollection, _resolvePapPathHook.Original(drawObject, pathBuffer, pathBufferSize, unkAnimationIndex, animationName)); + var ret = ResolvePath(_parent.MetaState.EstCollection, _resolvePapPathHook.Original(drawObject, pathBuffer, pathBufferSize, unkAnimationIndex, animationName)); + _parent.MetaState.EstCollection = ResolveData.Invalid; + return ret; } private nint ResolvePhybHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint partialSkeletonIndex) { _parent.MetaState.EstCollection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); - return ResolvePath(_parent.MetaState.EstCollection, _resolvePhybPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); + var ret = ResolvePath(_parent.MetaState.EstCollection, _resolvePhybPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); + _parent.MetaState.EstCollection = ResolveData.Invalid; + return ret; } private nint ResolveSklbHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint partialSkeletonIndex) { _parent.MetaState.EstCollection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); - return ResolvePath(_parent.MetaState.EstCollection, _resolveSklbPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); + var ret = ResolvePath(_parent.MetaState.EstCollection, _resolveSklbPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); + _parent.MetaState.EstCollection = ResolveData.Invalid; + return ret; } private nint ResolveSkpHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint partialSkeletonIndex) { _parent.MetaState.EstCollection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); - return ResolvePath(_parent.MetaState.EstCollection, _resolveSkpPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); + var ret = ResolvePath(_parent.MetaState.EstCollection, _resolveSkpPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); + _parent.MetaState.EstCollection = ResolveData.Invalid; + return ret; } private nint ResolveVfxHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint slotIndex, nint unkOutParam) diff --git a/Penumbra/Interop/PathResolving/MetaState.cs b/Penumbra/Interop/PathResolving/MetaState.cs index 8fa09232..3da94ce3 100644 --- a/Penumbra/Interop/PathResolving/MetaState.cs +++ b/Penumbra/Interop/PathResolving/MetaState.cs @@ -49,6 +49,7 @@ public sealed unsafe class MetaState : IDisposable public ResolveData EqpCollection = ResolveData.Invalid; public ResolveData GmpCollection = ResolveData.Invalid; public ResolveData EstCollection = ResolveData.Invalid; + public ResolveData RspCollection = ResolveData.Invalid; public PrimaryId UndividedGmpId = 0; private ResolveData _lastCreatedCollection = ResolveData.Invalid; @@ -96,9 +97,6 @@ public sealed unsafe class MetaState : IDisposable _ => DisposableContainer.Empty, }; - public MetaList.MetaReverter ResolveRspData(ModCollection collection) - => collection.TemporarilySetCmpFile(_characterUtility); - public DecalReverter ResolveDecal(ResolveData resolve, bool which) => new(_config, _characterUtility, _resources, resolve, which); @@ -132,9 +130,9 @@ public sealed unsafe class MetaState : IDisposable var decal = new DecalReverter(_config, _characterUtility, _resources, _lastCreatedCollection, UsesDecal(*(uint*)modelCharaId, (nint)customize)); - var cmp = _lastCreatedCollection.ModCollection.TemporarilySetCmpFile(_characterUtility); + RspCollection = _lastCreatedCollection; _characterBaseCreateMetaChanges.Dispose(); // Should always be empty. - _characterBaseCreateMetaChanges = new DisposableContainer(decal, cmp); + _characterBaseCreateMetaChanges = new DisposableContainer(decal); } private void OnCharacterBaseCreated(ModelCharaId _1, CustomizeArray* _2, CharacterArmor* _3, CharacterBase* drawObject) @@ -144,6 +142,7 @@ public sealed unsafe class MetaState : IDisposable if (_lastCreatedCollection.Valid && _lastCreatedCollection.AssociatedGameObject != nint.Zero && drawObject != null) _communicator.CreatedCharacterBase.Invoke(_lastCreatedCollection.AssociatedGameObject, _lastCreatedCollection.ModCollection, (nint)drawObject); + RspCollection = ResolveData.Invalid; _lastCreatedCollection = ResolveData.Invalid; } diff --git a/Penumbra/Meta/Files/CmpFile.cs b/Penumbra/Meta/Files/CmpFile.cs index 96cda496..8ca7cb80 100644 --- a/Penumbra/Meta/Files/CmpFile.cs +++ b/Penumbra/Meta/Files/CmpFile.cs @@ -46,6 +46,14 @@ public sealed unsafe class CmpFile : MetaBaseFile return *(RspEntry*)(data + RacialScalingStart + ToRspIndex(subRace) * RspData.ByteSize + (int)attribute * 4); } + public static RspEntry* GetDefaults(MetaFileManager manager, SubRace subRace, RspAttribute attribute) + { + { + var data = (byte*)manager.CharacterUtility.DefaultResource(InternalIndex).Address; + return (RspEntry*)(data + RacialScalingStart + ToRspIndex(subRace) * RspData.ByteSize + (int)attribute * 4); + } + } + private static int ToRspIndex(SubRace subRace) => subRace switch { diff --git a/Penumbra/UI/AdvancedWindow/Meta/RspMetaDrawer.cs b/Penumbra/UI/AdvancedWindow/Meta/RspMetaDrawer.cs index 2b7904ce..be02e321 100644 --- a/Penumbra/UI/AdvancedWindow/Meta/RspMetaDrawer.cs +++ b/Penumbra/UI/AdvancedWindow/Meta/RspMetaDrawer.cs @@ -85,7 +85,7 @@ public sealed class RspMetaDrawer(ModMetaEditor editor, MetaFileManager metaFile { using var dis = ImRaii.Disabled(disabled); ImGui.TableNextColumn(); - var ret = DragInput("##rspValue"u8, [], ImUtf8.GlobalScale * 150, defaultEntry.Value, entry.Value, out var newValue, + var ret = DragInput("##rspValue"u8, [], ImUtf8.GlobalScale * 150, entry.Value, defaultEntry.Value, out var newValue, RspEntry.MinValue, RspEntry.MaxValue, 0.001f, !disabled); if (ret) entry = new RspEntry(newValue); From 600fd2ecd36faa72292a7a8e2e8ce8982f6c708e Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 16 Jun 2024 01:02:42 +0200 Subject: [PATCH 251/433] Get rid off EQDP files --- Penumbra/Collections/Cache/EqdpCache.cs | 115 +++++------------- Penumbra/Collections/Cache/MetaCache.cs | 38 +----- .../Collections/ModCollection.Cache.Access.cs | 36 ------ .../Interop/Hooks/Meta/CalculateHeight.cs | 5 +- .../Interop/Hooks/Meta/ChangeCustomize.cs | 23 ++-- .../Interop/Hooks/Meta/EqdpAccessoryHook.cs | 33 +++++ Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs | 32 +++++ Penumbra/Interop/Hooks/Meta/EqpHook.cs | 2 +- Penumbra/Interop/Hooks/Meta/EstHook.cs | 2 +- Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs | 5 +- .../Interop/Hooks/Meta/GetEqpIndirect2.cs | 23 ++-- Penumbra/Interop/Hooks/Meta/GmpHook.cs | 6 +- .../Interop/Hooks/Meta/ModelLoadComplete.cs | 9 +- Penumbra/Interop/Hooks/Meta/RspBustHook.cs | 2 +- Penumbra/Interop/Hooks/Meta/RspHeightHook.cs | 3 +- .../Interop/Hooks/Meta/RspSetupCharacter.cs | 5 +- Penumbra/Interop/Hooks/Meta/RspTailHook.cs | 2 +- Penumbra/Interop/Hooks/Meta/SetupVisor.cs | 6 +- Penumbra/Interop/Hooks/Meta/UpdateModel.cs | 9 +- .../Hooks/Resources/ResolvePathHooksBase.cs | 45 ++++--- Penumbra/Interop/PathResolving/MetaState.cs | 33 ++--- Penumbra/Interop/Services/MetaList.cs | 2 +- Penumbra/Penumbra.cs | 2 - 23 files changed, 192 insertions(+), 246 deletions(-) create mode 100644 Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs create mode 100644 Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs diff --git a/Penumbra/Collections/Cache/EqdpCache.cs b/Penumbra/Collections/Cache/EqdpCache.cs index c63403ae..5bfe2dbf 100644 --- a/Penumbra/Collections/Cache/EqdpCache.cs +++ b/Penumbra/Collections/Cache/EqdpCache.cs @@ -1,8 +1,5 @@ -using OtterGui; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; -using Penumbra.Interop.Services; -using Penumbra.Interop.Structs; using Penumbra.Meta; using Penumbra.Meta.Files; using Penumbra.Meta.Manipulations; @@ -11,108 +8,60 @@ namespace Penumbra.Collections.Cache; public sealed class EqdpCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - private readonly ExpandedEqdpFile?[] _eqdpFiles = new ExpandedEqdpFile[CharacterUtilityData.EqdpIndices.Length]; // TODO: female Hrothgar + private readonly Dictionary<(PrimaryId Id, GenderRace GenderRace, bool Accessory), EqdpEntry> _fullEntries = []; public override void SetFiles() - { - for (var i = 0; i < CharacterUtilityData.EqdpIndices.Length; ++i) - Manager.SetFile(_eqdpFiles[i], CharacterUtilityData.EqdpIndices[i]); - } + { } - public void SetFile(MetaIndex index) - { - var i = CharacterUtilityData.EqdpIndices.IndexOf(index); - if (i != -1) - Manager.SetFile(_eqdpFiles[i], index); - } - - public void ResetFiles() - { - foreach (var t in CharacterUtilityData.EqdpIndices) - Manager.SetFile(null, t); - } + public bool TryGetFullEntry(PrimaryId id, GenderRace genderRace, bool accessory, out EqdpEntry entry) + => _fullEntries.TryGetValue((id, genderRace, accessory), out entry); protected override void IncorporateChangesInternal() - { - foreach (var (identifier, (_, entry)) in this) - Apply(GetFile(identifier)!, identifier, entry); - - Penumbra.Log.Verbose($"{Collection.AnonymizedName}: Loaded {Count} delayed EQDP manipulations."); - } - - public ExpandedEqdpFile? EqdpFile(GenderRace race, bool accessory) - => _eqdpFiles[Array.IndexOf(CharacterUtilityData.EqdpIndices, CharacterUtilityData.EqdpIdx(race, accessory))]; // TODO: female Hrothgar - - public MetaList.MetaReverter? TemporarilySetFile(GenderRace genderRace, bool accessory) - { - var idx = CharacterUtilityData.EqdpIdx(genderRace, accessory); - if (idx < 0) - { - Penumbra.Log.Warning($"Invalid Gender, Race or Accessory for EQDP file {genderRace}, {accessory}."); - return null; - } - - var i = CharacterUtilityData.EqdpIndices.IndexOf(idx); - if (i < 0) - { - Penumbra.Log.Warning($"Invalid Gender, Race or Accessory for EQDP file {genderRace}, {accessory}."); - return null; - } - - return Manager.TemporarilySetFile(_eqdpFiles[i], idx); - } + { } public void Reset() { - foreach (var file in _eqdpFiles.OfType()) - { - var relevant = CharacterUtility.RelevantIndices[file.Index.Value]; - file.Reset(Keys.Where(m => m.FileIndex() == relevant).Select(m => m.SetId)); - } - Clear(); + _fullEntries.Clear(); } protected override void ApplyModInternal(EqdpIdentifier identifier, EqdpEntry entry) { - if (GetFile(identifier) is { } file) - Apply(file, identifier, entry); + var tuple = (identifier.SetId, identifier.GenderRace, identifier.Slot.IsAccessory()); + var mask = Eqdp.Mask(identifier.Slot); + if (!_fullEntries.TryGetValue(tuple, out var currentEntry)) + currentEntry = ExpandedEqdpFile.GetDefault(Manager, identifier); + + _fullEntries[tuple] = (currentEntry & ~mask) | (entry & mask); } protected override void RevertModInternal(EqdpIdentifier identifier) { - if (GetFile(identifier) is { } file) - Apply(file, identifier, ExpandedEqdpFile.GetDefault(Manager, identifier)); - } + var tuple = (identifier.SetId, identifier.GenderRace, identifier.Slot.IsAccessory()); + var mask = Eqdp.Mask(identifier.Slot); - public static bool Apply(ExpandedEqdpFile file, EqdpIdentifier identifier, EqdpEntry entry) - { - var origEntry = file[identifier.SetId]; - var mask = Eqdp.Mask(identifier.Slot); - if ((origEntry & mask) == entry) - return false; - - file[identifier.SetId] = (origEntry & ~mask) | entry; - return true; + if (_fullEntries.TryGetValue(tuple, out var currentEntry)) + { + var def = ExpandedEqdpFile.GetDefault(Manager, identifier); + var newEntry = (currentEntry & ~mask) | (def & mask); + if (currentEntry != newEntry) + { + _fullEntries[tuple] = newEntry; + } + else + { + var slots = tuple.Item3 ? EquipSlotExtensions.AccessorySlots : EquipSlotExtensions.EquipmentSlots; + if (slots.All(s => !ContainsKey(identifier with { Slot = s }))) + _fullEntries.Remove(tuple); + else + _fullEntries[tuple] = newEntry; + } + } } protected override void Dispose(bool _) { - for (var i = 0; i < _eqdpFiles.Length; ++i) - { - _eqdpFiles[i]?.Dispose(); - _eqdpFiles[i] = null; - } - Clear(); - } - - private ExpandedEqdpFile? GetFile(EqdpIdentifier identifier) - { - if (!Manager.CharacterUtility.Ready) - return null; - - var index = Array.IndexOf(CharacterUtilityData.EqdpIndices, identifier.FileIndex()); - return _eqdpFiles[index] ??= new ExpandedEqdpFile(Manager, identifier.GenderRace, identifier.Slot.IsAccessory()); + _fullEntries.Clear(); } } diff --git a/Penumbra/Collections/Cache/MetaCache.cs b/Penumbra/Collections/Cache/MetaCache.cs index e6083351..614a5a2c 100644 --- a/Penumbra/Collections/Cache/MetaCache.cs +++ b/Penumbra/Collections/Cache/MetaCache.cs @@ -1,7 +1,5 @@ using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; -using Penumbra.Interop.Services; -using Penumbra.Interop.Structs; using Penumbra.Meta; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Editor; @@ -115,48 +113,18 @@ public class MetaCache(MetaFileManager manager, ModCollection collection) ~MetaCache() => Dispose(); - /// Set a single file. - public void SetFile(MetaIndex metaIndex) - { - switch (metaIndex) - { - case MetaIndex.Eqp: - break; - case MetaIndex.Gmp: - break; - case MetaIndex.HumanCmp: - Rsp.SetFiles(); - break; - case MetaIndex.FaceEst: - case MetaIndex.HairEst: - case MetaIndex.HeadEst: - case MetaIndex.BodyEst: - break; - default: - Eqdp.SetFile(metaIndex); - break; - } - } - /// Set the currently relevant IMC files for the collection cache. public void SetImcFiles(bool fromFullCompute) => Imc.SetFiles(fromFullCompute); - public MetaList.MetaReverter? TemporarilySetEqdpFile(GenderRace genderRace, bool accessory) - => Eqdp.TemporarilySetFile(genderRace, accessory); - /// Try to obtain a manipulated IMC file. public bool GetImcFile(Utf8GamePath path, [NotNullWhen(true)] out Meta.Files.ImcFile? file) => Imc.GetFile(path, out file); internal EqdpEntry GetEqdpEntry(GenderRace race, bool accessory, PrimaryId primaryId) - { - var eqdpFile = Eqdp.EqdpFile(race, accessory); - if (eqdpFile != null) - return primaryId.Id < eqdpFile.Count ? eqdpFile[primaryId] : default; - - return Meta.Files.ExpandedEqdpFile.GetDefault(manager, race, accessory, primaryId); - } + => Eqdp.TryGetFullEntry(primaryId, race, accessory, out var entry) + ? entry + : Meta.Files.ExpandedEqdpFile.GetDefault(manager, race, accessory, primaryId); internal EstEntry GetEstEntry(EstType type, GenderRace genderRace, PrimaryId primaryId) => Est.GetEstEntry(new EstIdentifier(primaryId, type, genderRace)); diff --git a/Penumbra/Collections/ModCollection.Cache.Access.cs b/Penumbra/Collections/ModCollection.Cache.Access.cs index d93a0f53..81751128 100644 --- a/Penumbra/Collections/ModCollection.Cache.Access.cs +++ b/Penumbra/Collections/ModCollection.Cache.Access.cs @@ -1,14 +1,9 @@ using OtterGui.Classes; -using Penumbra.GameData.Enums; using Penumbra.Mods; -using Penumbra.Interop.Structs; using Penumbra.Meta.Files; -using Penumbra.Meta.Manipulations; using Penumbra.String.Classes; using Penumbra.Collections.Cache; -using Penumbra.Interop.Services; using Penumbra.Mods.Editor; -using Penumbra.GameData.Structs; namespace Penumbra.Collections; @@ -68,35 +63,4 @@ public partial class ModCollection internal SingleArray Conflicts(Mod mod) => _cache?.Conflicts(mod) ?? new SingleArray(); - - public void SetFiles(CharacterUtility utility) - { - if (_cache == null) - { - utility.ResetAll(); - } - else - { - _cache.Meta.SetFiles(); - Penumbra.Log.Debug($"Set CharacterUtility resources for collection {Identifier}."); - } - } - - public void SetMetaFile(CharacterUtility utility, MetaIndex idx) - { - if (_cache == null) - utility.ResetResource(idx); - else - _cache.Meta.SetFile(idx); - } - - // Used for short periods of changed files. - public MetaList.MetaReverter? TemporarilySetEqdpFile(CharacterUtility utility, GenderRace genderRace, bool accessory) - { - if (_cache != null) - return _cache?.Meta.TemporarilySetEqdpFile(genderRace, accessory); - - var idx = CharacterUtilityData.EqdpIdx(genderRace, accessory); - return idx >= 0 ? utility.TemporarilyResetResource(idx) : null; - } } diff --git a/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs b/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs index 5a207491..7936b831 100644 --- a/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs +++ b/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs @@ -23,10 +23,11 @@ public sealed unsafe class CalculateHeight : FastHook [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private ulong Detour(Character* character) { - _metaState.RspCollection = _collectionResolver.IdentifyCollection((GameObject*)character, true); + var collection = _collectionResolver.IdentifyCollection((GameObject*)character, true); + _metaState.RspCollection.Push(collection); var ret = Task.Result.Original.Invoke(character); Penumbra.Log.Excessive($"[Calculate Height] Invoked on {(nint)character:X} -> {ret}."); - _metaState.RspCollection = ResolveData.Invalid; + _metaState.RspCollection.Pop(); return ret; } } diff --git a/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs b/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs index 4e0a5744..f589cf4e 100644 --- a/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs +++ b/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs @@ -1,12 +1,12 @@ -using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; using Penumbra.Collections; -using Penumbra.GameData; -using Penumbra.GameData.Structs; -using Penumbra.Interop.PathResolving; - -namespace Penumbra.Interop.Hooks.Meta; - +using Penumbra.GameData; +using Penumbra.GameData.Structs; +using Penumbra.Interop.PathResolving; + +namespace Penumbra.Interop.Hooks.Meta; + public sealed unsafe class ChangeCustomize : FastHook { private readonly CollectionResolver _collectionResolver; @@ -24,14 +24,15 @@ public sealed unsafe class ChangeCustomize : FastHook [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private bool Detour(Human* human, CustomizeArray* data, byte skipEquipment) { - _metaState.CustomizeChangeCollection = _collectionResolver.IdentifyCollection((DrawObject*)human, true); - _metaState.RspCollection = _metaState.CustomizeChangeCollection; + var collection = _collectionResolver.IdentifyCollection((DrawObject*)human, true); + _metaState.CustomizeChangeCollection = collection; + _metaState.RspCollection.Push(collection); using var decal1 = _metaState.ResolveDecal(_metaState.CustomizeChangeCollection, true); using var decal2 = _metaState.ResolveDecal(_metaState.CustomizeChangeCollection, false); var ret = Task.Result.Original.Invoke(human, data, skipEquipment); Penumbra.Log.Excessive($"[Change Customize] Invoked on {(nint)human:X} with {(nint)data:X}, {skipEquipment} -> {ret}."); _metaState.CustomizeChangeCollection = ResolveData.Invalid; - _metaState.RspCollection = ResolveData.Invalid; + _metaState.RspCollection.Pop(); return ret; } -} +} diff --git a/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs b/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs new file mode 100644 index 00000000..475e1eb7 --- /dev/null +++ b/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs @@ -0,0 +1,33 @@ +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using OtterGui.Services; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Interop.PathResolving; +using Penumbra.Meta.Manipulations; + +namespace Penumbra.Interop.Hooks.Meta; + +public unsafe class EqdpAccessoryHook : FastHook +{ + public delegate void Delegate(CharacterUtility* utility, EqdpEntry* entry, uint id, uint raceCode); + + private readonly MetaState _metaState; + + public EqdpAccessoryHook(HookManager hooks, MetaState metaState) + { + _metaState = metaState; + Task = hooks.CreateHook("GetEqdpAccessoryEntry", "E8 ?? ?? ?? ?? 41 BF ?? ?? ?? ?? 83 FB", Detour, true); + } + + private void Detour(CharacterUtility* utility, EqdpEntry* entry, uint setId, uint raceCode) + { + if (_metaState.EqdpCollection.TryPeek(out var collection) + && collection is { Valid: true, ModCollection.MetaCache: { } cache } + && cache.Eqdp.TryGetFullEntry(new PrimaryId((ushort)setId), (GenderRace)raceCode, true, out var newEntry)) + *entry = newEntry; + else + Task.Result.Original(utility, entry, setId, raceCode); + Penumbra.Log.Information( + $"[GetEqdpAccessoryEntry] Invoked on 0x{(ulong)utility:X} with {setId}, {(GenderRace)raceCode}, returned {(ushort)*entry:B10}."); + } +} diff --git a/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs b/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs new file mode 100644 index 00000000..9b911710 --- /dev/null +++ b/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs @@ -0,0 +1,32 @@ +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using OtterGui.Services; +using Penumbra.GameData.Enums; +using Penumbra.GameData.Structs; +using Penumbra.Interop.PathResolving; + +namespace Penumbra.Interop.Hooks.Meta; + +public unsafe class EqdpEquipHook : FastHook +{ + public delegate void Delegate(CharacterUtility* utility, EqdpEntry* entry, uint id, uint raceCode); + + private readonly MetaState _metaState; + + public EqdpEquipHook(HookManager hooks, MetaState metaState) + { + _metaState = metaState; + Task = hooks.CreateHook("GetEqdpEquipEntry", "E8 ?? ?? ?? ?? 85 DB 75 ?? F6 45", Detour, true); + } + + private void Detour(CharacterUtility* utility, EqdpEntry* entry, uint setId, uint raceCode) + { + if (_metaState.EqdpCollection.TryPeek(out var collection) + && collection is { Valid: true, ModCollection.MetaCache: { } cache } + && cache.Eqdp.TryGetFullEntry(new PrimaryId((ushort)setId), (GenderRace)raceCode, false, out var newEntry)) + *entry = newEntry; + else + Task.Result.Original(utility, entry, setId, raceCode); + Penumbra.Log.Information( + $"[GetEqdpEquipEntry] Invoked on 0x{(ulong)utility:X} with {setId}, {(GenderRace)raceCode}, returned {(ushort)*entry:B10}."); + } +} diff --git a/Penumbra/Interop/Hooks/Meta/EqpHook.cs b/Penumbra/Interop/Hooks/Meta/EqpHook.cs index 448605c1..7107e26b 100644 --- a/Penumbra/Interop/Hooks/Meta/EqpHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqpHook.cs @@ -19,7 +19,7 @@ public unsafe class EqpHook : FastHook private void Detour(CharacterUtility* utility, EqpEntry* flags, CharacterArmor* armor) { - if (_metaState.EqpCollection is { Valid: true, ModCollection.MetaCache: { } cache }) + if (_metaState.EqpCollection.TryPeek(out var collection) && collection is { Valid: true, ModCollection.MetaCache: { } cache }) { *flags = cache.Eqp.GetValues(armor); *flags = cache.GlobalEqp.Apply(*flags, armor); diff --git a/Penumbra/Interop/Hooks/Meta/EstHook.cs b/Penumbra/Interop/Hooks/Meta/EstHook.cs index 34935edb..23931182 100644 --- a/Penumbra/Interop/Hooks/Meta/EstHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EstHook.cs @@ -21,7 +21,7 @@ public class EstHook : FastHook private EstEntry Detour(uint genderRace, int estType, uint id) { EstEntry ret; - if (_metaState.EstCollection is { Valid: true, ModCollection.MetaCache: { } cache } + if (_metaState.EstCollection.TryPeek(out var collection) && collection is { Valid: true, ModCollection.MetaCache: { } cache } && cache.Est.TryGetValue(Convert(genderRace, estType, id), out var entry)) ret = entry.Entry; else diff --git a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs index beae6acc..a10b511a 100644 --- a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs +++ b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs @@ -29,8 +29,9 @@ public sealed unsafe class GetEqpIndirect : FastHook return; Penumbra.Log.Excessive($"[Get EQP Indirect] Invoked on {(nint)drawObject:X}."); - _metaState.EqpCollection = _collectionResolver.IdentifyCollection(drawObject, true); + var collection = _collectionResolver.IdentifyCollection(drawObject, true); + _metaState.EqpCollection.Push(collection); Task.Result.Original(drawObject); - _metaState.EqpCollection = ResolveData.Invalid; + _metaState.EqpCollection.Pop(); } } diff --git a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs index 89aaa9b0..30ec2597 100644 --- a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs +++ b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs @@ -1,11 +1,11 @@ -using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; -using OtterGui.Services; -using Penumbra.Collections; -using Penumbra.GameData; -using Penumbra.Interop.PathResolving; - -namespace Penumbra.Interop.Hooks.Meta; - +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using OtterGui.Services; +using Penumbra.Collections; +using Penumbra.GameData; +using Penumbra.Interop.PathResolving; + +namespace Penumbra.Interop.Hooks.Meta; + public sealed unsafe class GetEqpIndirect2 : FastHook { private readonly CollectionResolver _collectionResolver; @@ -29,8 +29,9 @@ public sealed unsafe class GetEqpIndirect2 : FastHook return; Penumbra.Log.Excessive($"[Get EQP Indirect 2] Invoked on {(nint)drawObject:X}."); - _metaState.EqpCollection = _collectionResolver.IdentifyCollection(drawObject, true); + var collection = _collectionResolver.IdentifyCollection(drawObject, true); + _metaState.EqpCollection.Push(collection); Task.Result.Original(drawObject); - _metaState.EqpCollection = ResolveData.Invalid; + _metaState.EqpCollection.Pop(); } -} +} diff --git a/Penumbra/Interop/Hooks/Meta/GmpHook.cs b/Penumbra/Interop/Hooks/Meta/GmpHook.cs index 60966fb7..256d8702 100644 --- a/Penumbra/Interop/Hooks/Meta/GmpHook.cs +++ b/Penumbra/Interop/Hooks/Meta/GmpHook.cs @@ -27,15 +27,15 @@ public unsafe class GmpHook : FastHook private nint Detour(nint gmpResource, uint dividedHeadId) { nint ret; - if (_metaState.GmpCollection is { Valid: true, ModCollection.MetaCache: { } cache } - && cache.Gmp.TryGetValue(new GmpIdentifier(_metaState.UndividedGmpId), out var entry)) + if (_metaState.GmpCollection.TryPeek(out var collection) && collection.Collection is { Valid: true, ModCollection.MetaCache: { } cache } + && cache.Gmp.TryGetValue(new GmpIdentifier(collection.Id), out var entry)) { if (entry.Entry.Enabled) { *StablePointer.Pointer = entry.Entry.Value; // This function already gets the original ID divided by the block size, so we can compute the modulo with a single multiplication and addition. // We then go backwards from our pointer because this gets added by the calling functions. - ret = (nint)(StablePointer.Pointer - (_metaState.UndividedGmpId.Id - dividedHeadId * ExpandedEqpGmpBase.BlockSize)); + ret = (nint)(StablePointer.Pointer - (collection.Id.Id - dividedHeadId * ExpandedEqpGmpBase.BlockSize)); } else { diff --git a/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs b/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs index 10c12594..2c17362d 100644 --- a/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs +++ b/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs @@ -23,10 +23,11 @@ public sealed unsafe class ModelLoadComplete : FastHook } var ret = storage; - if (bodyType < 2 && _metaState.RspCollection is { Valid: true, ModCollection.MetaCache: { } cache }) + if (bodyType < 2 && _metaState.RspCollection.TryPeek(out var collection) && collection is { Valid: true, ModCollection.MetaCache: { } cache }) { var bustScale = bustSize / 100f; var clan = (SubRace)(((int)race - 1) * 2 + 1 + isSecondSubRace); diff --git a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs index 883f5fc6..cf88c34a 100644 --- a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs @@ -1,3 +1,4 @@ +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; using Penumbra.GameData.Enums; using Penumbra.Interop.PathResolving; @@ -24,7 +25,7 @@ public class RspHeightHook : FastHook private unsafe float Detour(nint cmpResource, Race race, byte gender, byte isSecondSubRace, byte bodyType, byte height) { float scale; - if (bodyType < 2 && _metaState.RspCollection is { Valid: true, ModCollection.MetaCache: { } cache }) + if (bodyType < 2 && _metaState.RspCollection.TryPeek(out var collection) && collection is { Valid: true, ModCollection.MetaCache: { } cache }) { var clan = (SubRace)(((int)race - 1) * 2 + 1 + isSecondSubRace); var (minIdent, maxIdent) = gender == 0 diff --git a/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs b/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs index 831c99bb..58856f52 100644 --- a/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs +++ b/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs @@ -31,8 +31,9 @@ public sealed unsafe class RspSetupCharacter : FastHook private unsafe float Detour(nint cmpResource, Race race, byte gender, byte isSecondSubRace, byte bodyType, byte tailLength) { float scale; - if (bodyType < 2 && _metaState.RspCollection is { Valid: true, ModCollection.MetaCache: { } cache }) + if (bodyType < 2 && _metaState.RspCollection.TryPeek(out var collection) && collection is { Valid: true, ModCollection.MetaCache: { } cache }) { var clan = (SubRace)(((int)race - 1) * 2 + 1 + isSecondSubRace); var (minIdent, maxIdent) = gender == 0 diff --git a/Penumbra/Interop/Hooks/Meta/SetupVisor.cs b/Penumbra/Interop/Hooks/Meta/SetupVisor.cs index 8479968f..82b24dc4 100644 --- a/Penumbra/Interop/Hooks/Meta/SetupVisor.cs +++ b/Penumbra/Interop/Hooks/Meta/SetupVisor.cs @@ -27,11 +27,11 @@ public sealed unsafe class SetupVisor : FastHook [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private byte Detour(DrawObject* drawObject, ushort modelId, byte visorState) { - _metaState.GmpCollection = _collectionResolver.IdentifyCollection(drawObject, true); - _metaState.UndividedGmpId = modelId; + var collection = _collectionResolver.IdentifyCollection(drawObject, true); + _metaState.GmpCollection.Push((collection, modelId)); var ret = Task.Result.Original.Invoke(drawObject, modelId, visorState); Penumbra.Log.Excessive($"[Setup Visor] Invoked on {(nint)drawObject:X} with {modelId}, {visorState} -> {ret}."); - _metaState.GmpCollection = ResolveData.Invalid; + _metaState.GmpCollection.Pop(); return ret; } } diff --git a/Penumbra/Interop/Hooks/Meta/UpdateModel.cs b/Penumbra/Interop/Hooks/Meta/UpdateModel.cs index b0298ac7..76854bca 100644 --- a/Penumbra/Interop/Hooks/Meta/UpdateModel.cs +++ b/Penumbra/Interop/Hooks/Meta/UpdateModel.cs @@ -29,10 +29,11 @@ public sealed unsafe class UpdateModel : FastHook return; Penumbra.Log.Excessive($"[Update Model] Invoked on {(nint)drawObject:X}."); - var collection = _collectionResolver.IdentifyCollection(drawObject, true); - using var eqdp = _metaState.ResolveEqdpData(collection.ModCollection, MetaState.GetDrawObjectGenderRace((nint)drawObject), true, true); - _metaState.EqpCollection = collection; + var collection = _collectionResolver.IdentifyCollection(drawObject, true); + _metaState.EqpCollection.Push(collection); + _metaState.EqdpCollection.Push(collection); Task.Result.Original(drawObject); - _metaState.EqpCollection = ResolveData.Invalid; + _metaState.EqpCollection.Pop(); + _metaState.EqdpCollection.Pop(); } } diff --git a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs index 17cfa3f6..5941773f 100644 --- a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs +++ b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs @@ -1,11 +1,9 @@ using System.Text.Unicode; using Dalamud.Hooking; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; -using OtterGui.Classes; using OtterGui.Services; using Penumbra.Collections; using Penumbra.Interop.PathResolving; -using Penumbra.Meta.Manipulations; namespace Penumbra.Interop.Hooks.Resources; @@ -149,42 +147,51 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable private nint ResolveMdlHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint slotIndex) { - var data = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); - using var eqdp = slotIndex > 9 || _parent.InInternalResolve - ? DisposableContainer.Empty - : _parent.MetaState.ResolveEqdpData(data.ModCollection, MetaState.GetHumanGenderRace(drawObject), slotIndex < 5, slotIndex > 4); - return ResolvePath(data, _resolveMdlPathHook.Original(drawObject, pathBuffer, pathBufferSize, slotIndex)); + var collection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); + if (slotIndex < 10) + _parent.MetaState.EqdpCollection.Push(collection); + + var ret = ResolvePath(collection, _resolveMdlPathHook.Original(drawObject, pathBuffer, pathBufferSize, slotIndex)); + if (slotIndex < 10) + _parent.MetaState.EqdpCollection.Pop(); + + return ret; } private nint ResolvePapHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint unkAnimationIndex, nint animationName) { - _parent.MetaState.EstCollection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); - var ret = ResolvePath(_parent.MetaState.EstCollection, _resolvePapPathHook.Original(drawObject, pathBuffer, pathBufferSize, unkAnimationIndex, animationName)); - _parent.MetaState.EstCollection = ResolveData.Invalid; + var collection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); + _parent.MetaState.EstCollection.Push(collection); + var ret = ResolvePath(collection, + _resolvePapPathHook.Original(drawObject, pathBuffer, pathBufferSize, unkAnimationIndex, animationName)); + _parent.MetaState.EstCollection.Pop(); return ret; } private nint ResolvePhybHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint partialSkeletonIndex) { - _parent.MetaState.EstCollection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); - var ret = ResolvePath(_parent.MetaState.EstCollection, _resolvePhybPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); - _parent.MetaState.EstCollection = ResolveData.Invalid; + var collection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); + _parent.MetaState.EstCollection.Push(collection); + var ret = ResolvePath(collection, _resolvePhybPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); + _parent.MetaState.EstCollection.Pop(); return ret; } private nint ResolveSklbHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint partialSkeletonIndex) { - _parent.MetaState.EstCollection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); - var ret = ResolvePath(_parent.MetaState.EstCollection, _resolveSklbPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); - _parent.MetaState.EstCollection = ResolveData.Invalid; + var collection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); + _parent.MetaState.EstCollection.Push(collection); + var ret = ResolvePath(collection, _resolveSklbPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); + _parent.MetaState.EstCollection.Pop(); return ret; } private nint ResolveSkpHuman(nint drawObject, nint pathBuffer, nint pathBufferSize, uint partialSkeletonIndex) { - _parent.MetaState.EstCollection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); - var ret = ResolvePath(_parent.MetaState.EstCollection, _resolveSkpPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); - _parent.MetaState.EstCollection = ResolveData.Invalid; + var collection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); + _parent.MetaState.EstCollection.Push(collection); + var ret = ResolvePath(collection, _resolveSkpPathHook.Original(drawObject, pathBuffer, pathBufferSize, partialSkeletonIndex)); + _parent.MetaState.EstCollection.Pop(); return ret; } diff --git a/Penumbra/Interop/PathResolving/MetaState.cs b/Penumbra/Interop/PathResolving/MetaState.cs index 3da94ce3..4bd23cf8 100644 --- a/Penumbra/Interop/PathResolving/MetaState.cs +++ b/Penumbra/Interop/PathResolving/MetaState.cs @@ -45,12 +45,14 @@ public sealed unsafe class MetaState : IDisposable private readonly CharacterUtility _characterUtility; private readonly CreateCharacterBase _createCharacterBase; - public ResolveData CustomizeChangeCollection = ResolveData.Invalid; - public ResolveData EqpCollection = ResolveData.Invalid; - public ResolveData GmpCollection = ResolveData.Invalid; - public ResolveData EstCollection = ResolveData.Invalid; - public ResolveData RspCollection = ResolveData.Invalid; - public PrimaryId UndividedGmpId = 0; + public ResolveData CustomizeChangeCollection = ResolveData.Invalid; + public readonly Stack EqpCollection = []; + public readonly Stack EqdpCollection = []; + public readonly Stack EstCollection = []; + public readonly Stack RspCollection = []; + + public readonly Stack<(ResolveData Collection, PrimaryId Id)> GmpCollection = []; + private ResolveData _lastCreatedCollection = ResolveData.Invalid; private DisposableContainer _characterBaseCreateMetaChanges = DisposableContainer.Empty; @@ -82,21 +84,6 @@ public sealed unsafe class MetaState : IDisposable return false; } - public DisposableContainer ResolveEqdpData(ModCollection collection, GenderRace race, bool equipment, bool accessory) - => (equipment, accessory) switch - { - (true, true) => new DisposableContainer(race.Dependencies().SelectMany(r => new[] - { - collection.TemporarilySetEqdpFile(_characterUtility, r, false), - collection.TemporarilySetEqdpFile(_characterUtility, r, true), - })), - (true, false) => new DisposableContainer(race.Dependencies() - .Select(r => collection.TemporarilySetEqdpFile(_characterUtility, r, false))), - (false, true) => new DisposableContainer(race.Dependencies() - .Select(r => collection.TemporarilySetEqdpFile(_characterUtility, r, true))), - _ => DisposableContainer.Empty, - }; - public DecalReverter ResolveDecal(ResolveData resolve, bool which) => new(_config, _characterUtility, _resources, resolve, which); @@ -130,7 +117,7 @@ public sealed unsafe class MetaState : IDisposable var decal = new DecalReverter(_config, _characterUtility, _resources, _lastCreatedCollection, UsesDecal(*(uint*)modelCharaId, (nint)customize)); - RspCollection = _lastCreatedCollection; + RspCollection.Push(_lastCreatedCollection); _characterBaseCreateMetaChanges.Dispose(); // Should always be empty. _characterBaseCreateMetaChanges = new DisposableContainer(decal); } @@ -142,7 +129,7 @@ public sealed unsafe class MetaState : IDisposable if (_lastCreatedCollection.Valid && _lastCreatedCollection.AssociatedGameObject != nint.Zero && drawObject != null) _communicator.CreatedCharacterBase.Invoke(_lastCreatedCollection.AssociatedGameObject, _lastCreatedCollection.ModCollection, (nint)drawObject); - RspCollection = ResolveData.Invalid; + RspCollection.Pop(); _lastCreatedCollection = ResolveData.Invalid; } diff --git a/Penumbra/Interop/Services/MetaList.cs b/Penumbra/Interop/Services/MetaList.cs index dc472b8e..24d3f088 100644 --- a/Penumbra/Interop/Services/MetaList.cs +++ b/Penumbra/Interop/Services/MetaList.cs @@ -87,7 +87,7 @@ public unsafe class MetaList : IDisposable => SetResourceInternal(_defaultResourceData, _defaultResourceSize); private void SetResourceToDefaultCollection() - => _utility.Active.Default.SetMetaFile(_utility, GlobalMetaIndex); + {} public void Dispose() { diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index 3bbfdf65..905b998d 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -144,7 +144,6 @@ public class Penumbra : IDalamudPlugin { if (_characterUtility.Ready) { - _collectionManager.Active.Default.SetFiles(_characterUtility); _residentResources.Reload(); _redrawService.RedrawAll(RedrawType.Redraw); } @@ -153,7 +152,6 @@ public class Penumbra : IDalamudPlugin { if (_characterUtility.Ready) { - _characterUtility.ResetAll(); _residentResources.Reload(); _redrawService.RedrawAll(RedrawType.Redraw); } From 91d9e465ede9850fd7d5d0a457870e068cfd0cc8 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 16 Jun 2024 12:30:47 +0200 Subject: [PATCH 252/433] Improve eqdp. --- Penumbra.GameData | 2 +- Penumbra/Collections/Cache/EqdpCache.cs | 49 ++++++++----------- Penumbra/Collections/Cache/MetaCache.cs | 4 +- .../Interop/Hooks/Meta/EqdpAccessoryHook.cs | 9 ++-- Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs | 8 ++- Penumbra/Interop/PathResolving/MetaState.cs | 17 ------- .../Services/ShaderReplacementFixer.cs | 4 +- 7 files changed, 31 insertions(+), 62 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index cf1ff07e..3fbc7045 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit cf1ff07e900e2f93ab628a1fa535fc2b103794a5 +Subproject commit 3fbc704515b7b5fa9be02fb2a44719fc333747c1 diff --git a/Penumbra/Collections/Cache/EqdpCache.cs b/Penumbra/Collections/Cache/EqdpCache.cs index 5bfe2dbf..6047736b 100644 --- a/Penumbra/Collections/Cache/EqdpCache.cs +++ b/Penumbra/Collections/Cache/EqdpCache.cs @@ -1,20 +1,22 @@ using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Meta; -using Penumbra.Meta.Files; using Penumbra.Meta.Manipulations; namespace Penumbra.Collections.Cache; public sealed class EqdpCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - private readonly Dictionary<(PrimaryId Id, GenderRace GenderRace, bool Accessory), EqdpEntry> _fullEntries = []; + private readonly Dictionary<(PrimaryId Id, GenderRace GenderRace, bool Accessory), (EqdpEntry Entry, EqdpEntry InverseMask)> _fullEntries = + []; public override void SetFiles() { } - public bool TryGetFullEntry(PrimaryId id, GenderRace genderRace, bool accessory, out EqdpEntry entry) - => _fullEntries.TryGetValue((id, genderRace, accessory), out entry); + public EqdpEntry ApplyFullEntry(PrimaryId id, GenderRace genderRace, bool accessory, EqdpEntry originalEntry) + => _fullEntries.TryGetValue((id, genderRace, accessory), out var pair) + ? (originalEntry & pair.InverseMask) | pair.Entry + : originalEntry; protected override void IncorporateChangesInternal() { } @@ -27,36 +29,27 @@ public sealed class EqdpCache(MetaFileManager manager, ModCollection collection) protected override void ApplyModInternal(EqdpIdentifier identifier, EqdpEntry entry) { - var tuple = (identifier.SetId, identifier.GenderRace, identifier.Slot.IsAccessory()); - var mask = Eqdp.Mask(identifier.Slot); - if (!_fullEntries.TryGetValue(tuple, out var currentEntry)) - currentEntry = ExpandedEqdpFile.GetDefault(Manager, identifier); - - _fullEntries[tuple] = (currentEntry & ~mask) | (entry & mask); + var tuple = (identifier.SetId, identifier.GenderRace, identifier.Slot.IsAccessory()); + var mask = Eqdp.Mask(identifier.Slot); + var inverseMask = ~mask; + if (_fullEntries.TryGetValue(tuple, out var pair)) + pair = ((pair.Entry & inverseMask) | (entry & mask), pair.InverseMask & inverseMask); + else + pair = (entry & mask, inverseMask); + _fullEntries[tuple] = pair; } protected override void RevertModInternal(EqdpIdentifier identifier) { var tuple = (identifier.SetId, identifier.GenderRace, identifier.Slot.IsAccessory()); - var mask = Eqdp.Mask(identifier.Slot); - if (_fullEntries.TryGetValue(tuple, out var currentEntry)) - { - var def = ExpandedEqdpFile.GetDefault(Manager, identifier); - var newEntry = (currentEntry & ~mask) | (def & mask); - if (currentEntry != newEntry) - { - _fullEntries[tuple] = newEntry; - } - else - { - var slots = tuple.Item3 ? EquipSlotExtensions.AccessorySlots : EquipSlotExtensions.EquipmentSlots; - if (slots.All(s => !ContainsKey(identifier with { Slot = s }))) - _fullEntries.Remove(tuple); - else - _fullEntries[tuple] = newEntry; - } - } + if (!_fullEntries.Remove(tuple, out var pair)) + return; + + var mask = Eqdp.Mask(identifier.Slot); + var newMask = pair.InverseMask | mask; + if (newMask is not EqdpEntry.FullMask) + _fullEntries[tuple] = (pair.Entry & ~mask, newMask); } protected override void Dispose(bool _) diff --git a/Penumbra/Collections/Cache/MetaCache.cs b/Penumbra/Collections/Cache/MetaCache.cs index 614a5a2c..92a445dd 100644 --- a/Penumbra/Collections/Cache/MetaCache.cs +++ b/Penumbra/Collections/Cache/MetaCache.cs @@ -122,9 +122,7 @@ public class MetaCache(MetaFileManager manager, ModCollection collection) => Imc.GetFile(path, out file); internal EqdpEntry GetEqdpEntry(GenderRace race, bool accessory, PrimaryId primaryId) - => Eqdp.TryGetFullEntry(primaryId, race, accessory, out var entry) - ? entry - : Meta.Files.ExpandedEqdpFile.GetDefault(manager, race, accessory, primaryId); + => Eqdp.ApplyFullEntry(primaryId, race, accessory, Meta.Files.ExpandedEqdpFile.GetDefault(manager, race, accessory, primaryId)); internal EstEntry GetEstEntry(EstType type, GenderRace genderRace, PrimaryId primaryId) => Est.GetEstEntry(new EstIdentifier(primaryId, type, genderRace)); diff --git a/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs b/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs index 475e1eb7..f7390ea3 100644 --- a/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs @@ -3,7 +3,6 @@ using OtterGui.Services; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.PathResolving; -using Penumbra.Meta.Manipulations; namespace Penumbra.Interop.Hooks.Meta; @@ -21,12 +20,10 @@ public unsafe class EqdpAccessoryHook : FastHook private void Detour(CharacterUtility* utility, EqdpEntry* entry, uint setId, uint raceCode) { + Task.Result.Original(utility, entry, setId, raceCode); if (_metaState.EqdpCollection.TryPeek(out var collection) - && collection is { Valid: true, ModCollection.MetaCache: { } cache } - && cache.Eqdp.TryGetFullEntry(new PrimaryId((ushort)setId), (GenderRace)raceCode, true, out var newEntry)) - *entry = newEntry; - else - Task.Result.Original(utility, entry, setId, raceCode); + && collection is { Valid: true, ModCollection.MetaCache: { } cache }) + *entry = cache.Eqdp.ApplyFullEntry(new PrimaryId((ushort)setId), (GenderRace)raceCode, true, *entry); Penumbra.Log.Information( $"[GetEqdpAccessoryEntry] Invoked on 0x{(ulong)utility:X} with {setId}, {(GenderRace)raceCode}, returned {(ushort)*entry:B10}."); } diff --git a/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs b/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs index 9b911710..9b635b1f 100644 --- a/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs @@ -20,12 +20,10 @@ public unsafe class EqdpEquipHook : FastHook private void Detour(CharacterUtility* utility, EqdpEntry* entry, uint setId, uint raceCode) { + Task.Result.Original(utility, entry, setId, raceCode); if (_metaState.EqdpCollection.TryPeek(out var collection) - && collection is { Valid: true, ModCollection.MetaCache: { } cache } - && cache.Eqdp.TryGetFullEntry(new PrimaryId((ushort)setId), (GenderRace)raceCode, false, out var newEntry)) - *entry = newEntry; - else - Task.Result.Original(utility, entry, setId, raceCode); + && collection is { Valid: true, ModCollection.MetaCache: { } cache }) + *entry = cache.Eqdp.ApplyFullEntry(new PrimaryId((ushort)setId), (GenderRace)raceCode, false, *entry); Penumbra.Log.Information( $"[GetEqdpEquipEntry] Invoked on 0x{(ulong)utility:X} with {setId}, {(GenderRace)raceCode}, returned {(ushort)*entry:B10}."); } diff --git a/Penumbra/Interop/PathResolving/MetaState.cs b/Penumbra/Interop/PathResolving/MetaState.cs index 4bd23cf8..7f820b4e 100644 --- a/Penumbra/Interop/PathResolving/MetaState.cs +++ b/Penumbra/Interop/PathResolving/MetaState.cs @@ -2,13 +2,11 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Classes; using Penumbra.Collections; using Penumbra.Api.Enums; -using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.ResourceLoading; using Penumbra.Interop.Services; using Penumbra.Services; using Penumbra.String.Classes; -using ObjectType = FFXIVClientStructs.FFXIV.Client.Graphics.Scene.ObjectType; using CharacterUtility = Penumbra.Interop.Services.CharacterUtility; using Penumbra.Interop.Hooks.Objects; @@ -87,21 +85,6 @@ public sealed unsafe class MetaState : IDisposable public DecalReverter ResolveDecal(ResolveData resolve, bool which) => new(_config, _characterUtility, _resources, resolve, which); - public static GenderRace GetHumanGenderRace(nint human) - => (GenderRace)((Human*)human)->RaceSexId; - - public static GenderRace GetDrawObjectGenderRace(nint drawObject) - { - var draw = (DrawObject*)drawObject; - if (draw->Object.GetObjectType() != ObjectType.CharacterBase) - return GenderRace.Unknown; - - var c = (CharacterBase*)drawObject; - return c->GetModelType() == CharacterBase.ModelType.Human - ? GetHumanGenderRace(drawObject) - : GenderRace.Unknown; - } - public void Dispose() { _createCharacterBase.Unsubscribe(OnCreatingCharacterBase); diff --git a/Penumbra/Interop/Services/ShaderReplacementFixer.cs b/Penumbra/Interop/Services/ShaderReplacementFixer.cs index 3809ecbd..95e70b45 100644 --- a/Penumbra/Interop/Services/ShaderReplacementFixer.cs +++ b/Penumbra/Interop/Services/ShaderReplacementFixer.cs @@ -139,9 +139,9 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic // Performance considerations: // - This function is called from several threads simultaneously, hence the need for synchronization in the swapping path ; - // - Function is called each frame for each material on screen, after culling, i. e. up to thousands of times a frame in crowded areas ; + // - Function is called each frame for each material on screen, after culling, i.e. up to thousands of times a frame in crowded areas ; // - Swapping path is taken up to hundreds of times a frame. - // At the time of writing, the lock doesn't seem to have a noticeable impact in either framerate or CPU usage, but the swapping path shall still be avoided as much as possible. + // At the time of writing, the lock doesn't seem to have a noticeable impact in either frame rate or CPU usage, but the swapping path shall still be avoided as much as possible. lock (_skinLock) { try From be729afd4b382e618c91558373e013f34959ba02 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 17 Jun 2024 16:39:10 +0200 Subject: [PATCH 253/433] Some cleanup --- Penumbra/Collections/Cache/ImcCache.cs | 2 -- Penumbra/Communication/CreatingCharacterBase.cs | 3 +-- Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs | 2 +- Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs | 2 +- Penumbra/Interop/Hooks/Meta/RspBustHook.cs | 2 +- Penumbra/Mods/Manager/ModManager.cs | 2 +- 6 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Penumbra/Collections/Cache/ImcCache.cs b/Penumbra/Collections/Cache/ImcCache.cs index a9daf795..c6bb0330 100644 --- a/Penumbra/Collections/Cache/ImcCache.cs +++ b/Penumbra/Collections/Cache/ImcCache.cs @@ -1,5 +1,3 @@ -using FFXIVClientStructs.FFXIV.Client.Graphics.Render; -using Penumbra.Collections.Manager; using Penumbra.GameData.Structs; using Penumbra.Interop.PathResolving; using Penumbra.Meta; diff --git a/Penumbra/Communication/CreatingCharacterBase.cs b/Penumbra/Communication/CreatingCharacterBase.cs index 8a906ca0..51d55868 100644 --- a/Penumbra/Communication/CreatingCharacterBase.cs +++ b/Penumbra/Communication/CreatingCharacterBase.cs @@ -1,5 +1,4 @@ using OtterGui.Classes; -using Penumbra.Api; using Penumbra.Api.Api; using Penumbra.Services; @@ -19,7 +18,7 @@ public sealed class CreatingCharacterBase() { public enum Priority { - /// + /// Api = 0, /// diff --git a/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs b/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs index f7390ea3..aaaaccd4 100644 --- a/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs @@ -24,7 +24,7 @@ public unsafe class EqdpAccessoryHook : FastHook if (_metaState.EqdpCollection.TryPeek(out var collection) && collection is { Valid: true, ModCollection.MetaCache: { } cache }) *entry = cache.Eqdp.ApplyFullEntry(new PrimaryId((ushort)setId), (GenderRace)raceCode, true, *entry); - Penumbra.Log.Information( + Penumbra.Log.Excessive( $"[GetEqdpAccessoryEntry] Invoked on 0x{(ulong)utility:X} with {setId}, {(GenderRace)raceCode}, returned {(ushort)*entry:B10}."); } } diff --git a/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs b/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs index 9b635b1f..2711f195 100644 --- a/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs @@ -24,7 +24,7 @@ public unsafe class EqdpEquipHook : FastHook if (_metaState.EqdpCollection.TryPeek(out var collection) && collection is { Valid: true, ModCollection.MetaCache: { } cache }) *entry = cache.Eqdp.ApplyFullEntry(new PrimaryId((ushort)setId), (GenderRace)raceCode, false, *entry); - Penumbra.Log.Information( + Penumbra.Log.Excessive( $"[GetEqdpEquipEntry] Invoked on 0x{(ulong)utility:X} with {setId}, {(GenderRace)raceCode}, returned {(ushort)*entry:B10}."); } } diff --git a/Penumbra/Interop/Hooks/Meta/RspBustHook.cs b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs index 13a5410d..86759460 100644 --- a/Penumbra/Interop/Hooks/Meta/RspBustHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs @@ -59,7 +59,7 @@ public unsafe class RspBustHook : FastHook ret = Task.Result.Original(cmpResource, storage, race, gender, isSecondSubRace, bodyType, bustSize); } - Penumbra.Log.Information( + Penumbra.Log.Excessive( $"[GetRspBust] Invoked on 0x{cmpResource:X} with {race}, {(Gender)(gender + 1)}, {isSecondSubRace == 1}, {bodyType}, {bustSize}, returned {storage[0]}, {storage[1]}, {storage[2]}."); return ret; } diff --git a/Penumbra/Mods/Manager/ModManager.cs b/Penumbra/Mods/Manager/ModManager.cs index 62b54865..42082383 100644 --- a/Penumbra/Mods/Manager/ModManager.cs +++ b/Penumbra/Mods/Manager/ModManager.cs @@ -261,7 +261,7 @@ public sealed class ModManager : ModStorage, IDisposable /// /// Set the mod base directory. - /// If its not the first time, check if it is the same directory as before. + /// If it's not the first time, check if it is the same directory as before. /// Also checks if the directory is available and tries to create it if it is not. /// private void SetBaseDirectory(string newPath, bool firstTime, out string resultNewDir) From d7a8c9415bb57f8fe13e3cfeed067e6d4a579470 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 17 Jun 2024 23:11:42 +0200 Subject: [PATCH 254/433] Use specific counter for Imc. --- Penumbra/Collections/Cache/ImcCache.cs | 2 ++ Penumbra/Collections/ModCollection.cs | 2 ++ Penumbra/Interop/PathResolving/PathDataHandler.cs | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Penumbra/Collections/Cache/ImcCache.cs b/Penumbra/Collections/Cache/ImcCache.cs index c6bb0330..786463bc 100644 --- a/Penumbra/Collections/Cache/ImcCache.cs +++ b/Penumbra/Collections/Cache/ImcCache.cs @@ -68,6 +68,7 @@ public sealed class ImcCache(MetaFileManager manager, ModCollection collection) protected override void ApplyModInternal(ImcIdentifier identifier, ImcEntry entry) { + ++Collection.ImcChangeCounter; if (Manager.CharacterUtility.Ready) ApplyFile(identifier, entry); } @@ -102,6 +103,7 @@ public sealed class ImcCache(MetaFileManager manager, ModCollection collection) protected override void RevertModInternal(ImcIdentifier identifier) { + ++Collection.ImcChangeCounter; var path = identifier.GamePath(); if (!_imcFiles.TryGetValue(path, out var pair)) return; diff --git a/Penumbra/Collections/ModCollection.cs b/Penumbra/Collections/ModCollection.cs index 9286d459..eb5ab46a 100644 --- a/Penumbra/Collections/ModCollection.cs +++ b/Penumbra/Collections/ModCollection.cs @@ -56,6 +56,8 @@ public partial class ModCollection /// public int ChangeCounter { get; private set; } + public uint ImcChangeCounter { get; set; } + /// Increment the number of changes in the effective file list. public int IncrementCounter() => ++ChangeCounter; diff --git a/Penumbra/Interop/PathResolving/PathDataHandler.cs b/Penumbra/Interop/PathResolving/PathDataHandler.cs index 5627e015..a8be97c8 100644 --- a/Penumbra/Interop/PathResolving/PathDataHandler.cs +++ b/Penumbra/Interop/PathResolving/PathDataHandler.cs @@ -32,7 +32,7 @@ public static class PathDataHandler /// Create the encoding path for an IMC file. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static FullPath CreateImc(ByteString path, ModCollection collection) - => CreateBase(path, collection); + => new($"|{collection.LocalId.Id}_{collection.ImcChangeCounter}_{DiscriminatorString}|{path}"); /// Create the encoding path for a TMB file. [MethodImpl(MethodImplOptions.AggressiveInlining)] From 03d3c38ad577756d3a4fb20c1bd0417344538727 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 18 Jun 2024 17:52:34 +0200 Subject: [PATCH 255/433] Improve Imc Handling. --- Penumbra/Collections/Cache/CollectionCache.cs | 16 +--- .../Cache/CollectionCacheManager.cs | 2 - Penumbra/Collections/Cache/ImcCache.cs | 58 ++++---------- Penumbra/Collections/Cache/MetaCache.cs | 8 +- .../Interop/PathResolving/PathResolver.cs | 50 +++--------- .../Interop/PathResolving/SubfileHelper.cs | 17 ++-- .../Interop/ResourceLoading/ResourceLoader.cs | 69 +++++++++++++++++ Penumbra/Interop/Services/CharacterUtility.cs | 35 +-------- Penumbra/Meta/Files/CmpFile.cs | 2 +- Penumbra/Meta/Files/EqdpFile.cs | 2 +- Penumbra/Meta/Files/EqpGmpFile.cs | 2 +- Penumbra/Meta/Files/EstFile.cs | 2 +- Penumbra/Meta/Files/EvpFile.cs | 6 +- Penumbra/Meta/Files/ImcFile.cs | 30 ++++---- Penumbra/Meta/Files/MetaBaseFile.cs | 77 +++++++++++++++---- Penumbra/Meta/MetaFileManager.cs | 54 +------------ 16 files changed, 197 insertions(+), 233 deletions(-) diff --git a/Penumbra/Collections/Cache/CollectionCache.cs b/Penumbra/Collections/Cache/CollectionCache.cs index fd801d3b..4755840e 100644 --- a/Penumbra/Collections/Cache/CollectionCache.cs +++ b/Penumbra/Collections/Cache/CollectionCache.cs @@ -36,7 +36,7 @@ public sealed class CollectionCache : IDisposable => ConflictDict.Values; public SingleArray Conflicts(IMod mod) - => ConflictDict.TryGetValue(mod, out SingleArray c) ? c : new SingleArray(); + => ConflictDict.TryGetValue(mod, out var c) ? c : new SingleArray(); private int _changedItemsSaveCounter = -1; @@ -125,12 +125,6 @@ public sealed class CollectionCache : IDisposable return ret; } - public void ForceFile(Utf8GamePath path, FullPath fullPath) - => _manager.AddChange(ChangeData.ForcedFile(this, path, fullPath)); - - public void RemovePath(Utf8GamePath path) - => _manager.AddChange(ChangeData.ForcedFile(this, path, FullPath.Empty)); - public void ReloadMod(IMod mod, bool addMetaChanges) => _manager.AddChange(ChangeData.ModReload(this, mod, addMetaChanges)); @@ -251,9 +245,6 @@ public sealed class CollectionCache : IDisposable if (addMetaChanges) { _collection.IncrementCounter(); - if (mod.TotalManipulations > 0) - AddMetaFiles(false); - _manager.MetaFileManager.ApplyDefaultFiles(_collection); } } @@ -408,11 +399,6 @@ public sealed class CollectionCache : IDisposable } - // Add all necessary meta file redirects. - public void AddMetaFiles(bool fromFullCompute) - => Meta.SetImcFiles(fromFullCompute); - - // Identify and record all manipulated objects for this entire collection. private void SetChangedItems() { diff --git a/Penumbra/Collections/Cache/CollectionCacheManager.cs b/Penumbra/Collections/Cache/CollectionCacheManager.cs index ae424b94..02c9c8a9 100644 --- a/Penumbra/Collections/Cache/CollectionCacheManager.cs +++ b/Penumbra/Collections/Cache/CollectionCacheManager.cs @@ -180,8 +180,6 @@ public class CollectionCacheManager : IDisposable foreach (var mod in _modStorage) cache.AddModSync(mod, false); - cache.AddMetaFiles(true); - collection.IncrementCounter(); MetaFileManager.ApplyDefaultFiles(collection); diff --git a/Penumbra/Collections/Cache/ImcCache.cs b/Penumbra/Collections/Cache/ImcCache.cs index 786463bc..e7eedc04 100644 --- a/Penumbra/Collections/Cache/ImcCache.cs +++ b/Penumbra/Collections/Cache/ImcCache.cs @@ -1,20 +1,22 @@ using Penumbra.GameData.Structs; -using Penumbra.Interop.PathResolving; using Penumbra.Meta; using Penumbra.Meta.Files; using Penumbra.Meta.Manipulations; -using Penumbra.String.Classes; +using Penumbra.String; namespace Penumbra.Collections.Cache; public sealed class ImcCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - private readonly Dictionary)> _imcFiles = []; + private readonly Dictionary)> _imcFiles = []; public override void SetFiles() - => SetFiles(false); + { } - public bool GetFile(Utf8GamePath path, [NotNullWhen(true)] out ImcFile? file) + public bool HasFile(ByteString path) + => _imcFiles.ContainsKey(path); + + public bool GetFile(ByteString path, [NotNullWhen(true)] out ImcFile? file) { if (!_imcFiles.TryGetValue(path, out var p)) { @@ -26,56 +28,31 @@ public sealed class ImcCache(MetaFileManager manager, ModCollection collection) return true; } - public void SetFiles(bool fromFullCompute) - { - if (fromFullCompute) - foreach (var (path, _) in _imcFiles) - Collection._cache!.ForceFileSync(path, PathDataHandler.CreateImc(path.Path, Collection)); - else - foreach (var (path, _) in _imcFiles) - Collection._cache!.ForceFile(path, PathDataHandler.CreateImc(path.Path, Collection)); - } - - public void ResetFiles() - { - foreach (var (path, _) in _imcFiles) - Collection._cache!.ForceFile(path, FullPath.Empty); - } - protected override void IncorporateChangesInternal() - { - if (!Manager.CharacterUtility.Ready) - return; - - foreach (var (identifier, (_, entry)) in this) - ApplyFile(identifier, entry); - - Penumbra.Log.Verbose($"{Collection.AnonymizedName}: Loaded {Count} delayed IMC manipulations."); - } + { } public void Reset() { - foreach (var (path, (file, set)) in _imcFiles) + foreach (var (_, (file, set)) in _imcFiles) { - Collection._cache!.RemovePath(path); file.Reset(); set.Clear(); } + _imcFiles.Clear(); Clear(); } protected override void ApplyModInternal(ImcIdentifier identifier, ImcEntry entry) { ++Collection.ImcChangeCounter; - if (Manager.CharacterUtility.Ready) - ApplyFile(identifier, entry); + ApplyFile(identifier, entry); } private void ApplyFile(ImcIdentifier identifier, ImcEntry entry) { - var path = identifier.GamePath(); + var path = identifier.GamePath().Path; try { if (!_imcFiles.TryGetValue(path, out var pair)) @@ -87,8 +64,6 @@ public sealed class ImcCache(MetaFileManager manager, ModCollection collection) pair.Item2.Add(identifier); _imcFiles[path] = pair; - var fullPath = PathDataHandler.CreateImc(pair.Item1.Path.Path, Collection); - Collection._cache!.ForceFile(path, fullPath); } catch (ImcException e) { @@ -104,7 +79,7 @@ public sealed class ImcCache(MetaFileManager manager, ModCollection collection) protected override void RevertModInternal(ImcIdentifier identifier) { ++Collection.ImcChangeCounter; - var path = identifier.GamePath(); + var path = identifier.GamePath().Path; if (!_imcFiles.TryGetValue(path, out var pair)) return; @@ -114,17 +89,12 @@ public sealed class ImcCache(MetaFileManager manager, ModCollection collection) if (pair.Item2.Count == 0) { _imcFiles.Remove(path); - Collection._cache!.ForceFile(pair.Item1.Path, FullPath.Empty); pair.Item1.Dispose(); return; } var def = ImcFile.GetDefault(Manager, pair.Item1.Path, identifier.EquipSlot, identifier.Variant, out _); - if (!Apply(pair.Item1, identifier, def)) - return; - - var fullPath = PathDataHandler.CreateImc(pair.Item1.Path.Path, Collection); - Collection._cache!.ForceFile(pair.Item1.Path, fullPath); + Apply(pair.Item1, identifier, def); } public static bool Apply(ImcFile file, ImcIdentifier identifier, ImcEntry entry) diff --git a/Penumbra/Collections/Cache/MetaCache.cs b/Penumbra/Collections/Cache/MetaCache.cs index 92a445dd..253f3c7f 100644 --- a/Penumbra/Collections/Cache/MetaCache.cs +++ b/Penumbra/Collections/Cache/MetaCache.cs @@ -36,7 +36,7 @@ public class MetaCache(MetaFileManager manager, ModCollection collection) Est.SetFiles(); Gmp.SetFiles(); Rsp.SetFiles(); - Imc.SetFiles(false); + Imc.SetFiles(); } public void Reset() @@ -113,13 +113,9 @@ public class MetaCache(MetaFileManager manager, ModCollection collection) ~MetaCache() => Dispose(); - /// Set the currently relevant IMC files for the collection cache. - public void SetImcFiles(bool fromFullCompute) - => Imc.SetFiles(fromFullCompute); - /// Try to obtain a manipulated IMC file. public bool GetImcFile(Utf8GamePath path, [NotNullWhen(true)] out Meta.Files.ImcFile? file) - => Imc.GetFile(path, out file); + => Imc.GetFile(path.Path, out file); internal EqdpEntry GetEqdpEntry(GenderRace race, bool accessory, PrimaryId primaryId) => Eqdp.ApplyFullEntry(primaryId, race, accessory, Meta.Files.ExpandedEqdpFile.GetDefault(manager, race, accessory, primaryId)); diff --git a/Penumbra/Interop/PathResolving/PathResolver.cs b/Penumbra/Interop/PathResolving/PathResolver.cs index e5c75327..e069e3ea 100644 --- a/Penumbra/Interop/PathResolving/PathResolver.cs +++ b/Penumbra/Interop/PathResolving/PathResolver.cs @@ -1,11 +1,8 @@ -using System.Runtime; using FFXIVClientStructs.FFXIV.Client.System.Resource; using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.Collections.Manager; using Penumbra.Interop.ResourceLoading; -using Penumbra.Interop.Structs; -using Penumbra.String; using Penumbra.String.Classes; using Penumbra.Util; @@ -27,24 +24,16 @@ public class PathResolver : IDisposable public unsafe PathResolver(PerformanceTracker performance, Configuration config, CollectionManager collectionManager, ResourceLoader loader, SubfileHelper subfileHelper, PathState pathState, MetaState metaState, CollectionResolver collectionResolver, GameState gameState) { - _performance = performance; - _config = config; - _collectionManager = collectionManager; - _subfileHelper = subfileHelper; - _pathState = pathState; - _metaState = metaState; - _gameState = gameState; - _collectionResolver = collectionResolver; - _loader = loader; - _loader.ResolvePath = ResolvePath; - _loader.FileLoaded += ImcLoadResource; - } - - /// Obtain a temporary or permanent collection by local ID. - public bool CollectionByLocalId(LocalCollectionId id, out ModCollection collection) - { - collection = _collectionManager.Storage.ByLocalId(id); - return collection != ModCollection.Empty; + _performance = performance; + _config = config; + _collectionManager = collectionManager; + _subfileHelper = subfileHelper; + _pathState = pathState; + _metaState = metaState; + _gameState = gameState; + _collectionResolver = collectionResolver; + _loader = loader; + _loader.ResolvePath = ResolvePath; } /// Try to resolve the given game path to the replaced path. @@ -120,7 +109,6 @@ public class PathResolver : IDisposable public unsafe void Dispose() { _loader.ResetResolvePath(); - _loader.FileLoaded -= ImcLoadResource; } /// Use the default method of path replacement. @@ -130,24 +118,6 @@ public class PathResolver : IDisposable return (resolved, _collectionManager.Active.Default.ToResolveData()); } - /// After loading an IMC file, replace its contents with the modded IMC file. - private unsafe void ImcLoadResource(ResourceHandle* resource, ByteString path, bool returnValue, bool custom, - ReadOnlySpan additionalData) - { - if (resource->FileType != ResourceType.Imc - || !PathDataHandler.Read(additionalData, out var data) - || data.Discriminator != PathDataHandler.Discriminator - || !Utf8GamePath.FromByteString(path, out var gamePath) - || !CollectionByLocalId(data.Collection, out var collection) - || !collection.HasCache - || !collection.GetImcFile(gamePath, out var file)) - return; - - file.Replace(resource); - Penumbra.Log.Verbose( - $"[ResourceLoader] Loaded {gamePath} from file and replaced with IMC from collection {collection.AnonymizedName}."); - } - /// Resolve a path from the interface collection. private (FullPath?, ResolveData) ResolveUi(Utf8GamePath path) => (_collectionManager.Active.Interface.ResolvePath(path), diff --git a/Penumbra/Interop/PathResolving/SubfileHelper.cs b/Penumbra/Interop/PathResolving/SubfileHelper.cs index 793ea20b..b9631cf2 100644 --- a/Penumbra/Interop/PathResolving/SubfileHelper.cs +++ b/Penumbra/Interop/PathResolving/SubfileHelper.cs @@ -70,14 +70,15 @@ public sealed unsafe class SubfileHelper : IDisposable, IReadOnlyCollection PathDataHandler.CreateMtrl(path, resolveData.ModCollection, originalPath), - ResourceType.Avfx => PathDataHandler.CreateAvfx(path, resolveData.ModCollection), - ResourceType.Tmb => PathDataHandler.CreateTmb(path, resolveData.ModCollection), - _ => resolved, - }; + resolved = type switch + { + ResourceType.Mtrl when nonDefault => PathDataHandler.CreateMtrl(path, resolveData.ModCollection, originalPath), + ResourceType.Avfx when nonDefault => PathDataHandler.CreateAvfx(path, resolveData.ModCollection), + ResourceType.Tmb when nonDefault => PathDataHandler.CreateTmb(path, resolveData.ModCollection), + ResourceType.Imc when resolveData.ModCollection.MetaCache?.Imc.HasFile(path) ?? false => PathDataHandler.CreateImc(path, + resolveData.ModCollection), + _ => resolved, + }; data = (resolved, resolveData); } diff --git a/Penumbra/Interop/ResourceLoading/ResourceLoader.cs b/Penumbra/Interop/ResourceLoading/ResourceLoader.cs index 7b49beab..fae38907 100644 --- a/Penumbra/Interop/ResourceLoading/ResourceLoader.cs +++ b/Penumbra/Interop/ResourceLoading/ResourceLoader.cs @@ -1,6 +1,9 @@ +using System.Collections.Frozen; using FFXIVClientStructs.FFXIV.Client.System.Resource; +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Collections; +using Penumbra.Collections.Manager; using Penumbra.Interop.PathResolving; using Penumbra.Interop.SafeHandles; using Penumbra.Interop.Structs; @@ -10,6 +13,72 @@ using FileMode = Penumbra.Interop.Structs.FileMode; namespace Penumbra.Interop.ResourceLoading; +public interface IFilePostProcessor : IService +{ + public ResourceType Type { get; } + public unsafe void PostProcess(ResourceHandle* resource, ByteString originalGamePath, ReadOnlySpan additionalData); +} + +public sealed class MaterialFilePostProcessor : IFilePostProcessor +{ + public ResourceType Type + => ResourceType.Mtrl; + + public unsafe void PostProcess(ResourceHandle* resource, ByteString originalGamePath, ReadOnlySpan additionalData) + { + if (!PathDataHandler.ReadMtrl(additionalData, out var data)) + return; + } +} + +public sealed class ImcFilePostProcessor(CollectionStorage collections) : IFilePostProcessor +{ + public ResourceType Type + => ResourceType.Imc; + + public unsafe void PostProcess(ResourceHandle* resource, ByteString originalGamePath, ReadOnlySpan additionalData) + { + if (!PathDataHandler.Read(additionalData, out var data) || data.Discriminator != PathDataHandler.Discriminator) + return; + + var collection = collections.ByLocalId(data.Collection); + if (collection.MetaCache is not { } cache) + return; + + if (!cache.Imc.GetFile(originalGamePath, out var file)) + return; + + file.Replace(resource); + Penumbra.Log.Information( + $"[ResourceLoader] Loaded {originalGamePath} from file and replaced with IMC from collection {collection.AnonymizedName}."); + } +} + +public unsafe class FilePostProcessService : IRequiredService, IDisposable +{ + private readonly ResourceLoader _resourceLoader; + private readonly FrozenDictionary _processors; + + public FilePostProcessService(ResourceLoader resourceLoader, ServiceManager services) + { + _resourceLoader = resourceLoader; + _processors = services.GetServicesImplementing().ToFrozenDictionary(s => s.Type, s => s); + _resourceLoader.FileLoaded += OnFileLoaded; + } + + public void Dispose() + { + _resourceLoader.FileLoaded -= OnFileLoaded; + } + + private void OnFileLoaded(ResourceHandle* resource, ByteString path, bool returnValue, bool custom, + ReadOnlySpan additionalData) + { + if (_processors.TryGetValue(resource->FileType, out var processor)) + processor.PostProcess(resource, path, additionalData); + } +} + public unsafe class ResourceLoader : IDisposable { private readonly ResourceService _resources; diff --git a/Penumbra/Interop/Services/CharacterUtility.cs b/Penumbra/Interop/Services/CharacterUtility.cs index da04bf90..9459df06 100644 --- a/Penumbra/Interop/Services/CharacterUtility.cs +++ b/Penumbra/Interop/Services/CharacterUtility.cs @@ -1,6 +1,5 @@ using Dalamud.Plugin.Services; using Dalamud.Utility.Signatures; -using Penumbra.Collections.Manager; using Penumbra.GameData; using Penumbra.Interop.Structs; @@ -53,17 +52,15 @@ public unsafe class CharacterUtility : IDisposable public (nint Address, int Size) DefaultResource(InternalIndex idx) => _lists[idx.Value].DefaultResource; - private readonly IFramework _framework; - public readonly ActiveCollectionData Active; + private readonly IFramework _framework; - public CharacterUtility(IFramework framework, IGameInteropProvider interop, ActiveCollectionData active) + public CharacterUtility(IFramework framework, IGameInteropProvider interop) { interop.InitializeFromAttributes(this); _lists = Enumerable.Range(0, RelevantIndices.Length) .Select(idx => new MetaList(this, new InternalIndex(idx))) .ToArray(); _framework = framework; - Active = active; LoadingFinished += () => Penumbra.Log.Debug("Loading of CharacterUtility finished."); LoadDefaultResources(null!); if (!Ready) @@ -121,34 +118,6 @@ public unsafe class CharacterUtility : IDisposable LoadingFinished.Invoke(); } - public void SetResource(MetaIndex resourceIdx, nint data, int length) - { - var idx = ReverseIndices[(int)resourceIdx]; - var list = _lists[idx.Value]; - list.SetResource(data, length); - } - - public void ResetResource(MetaIndex resourceIdx) - { - var idx = ReverseIndices[(int)resourceIdx]; - var list = _lists[idx.Value]; - list.ResetResource(); - } - - public MetaList.MetaReverter TemporarilySetResource(MetaIndex resourceIdx, nint data, int length) - { - var idx = ReverseIndices[(int)resourceIdx]; - var list = _lists[idx.Value]; - return list.TemporarilySetResource(data, length); - } - - public MetaList.MetaReverter TemporarilyResetResource(MetaIndex resourceIdx) - { - var idx = ReverseIndices[(int)resourceIdx]; - var list = _lists[idx.Value]; - return list.TemporarilyResetResource(); - } - /// Return all relevant resources to the default resource. public void ResetAll() { diff --git a/Penumbra/Meta/Files/CmpFile.cs b/Penumbra/Meta/Files/CmpFile.cs index 8ca7cb80..5028a3de 100644 --- a/Penumbra/Meta/Files/CmpFile.cs +++ b/Penumbra/Meta/Files/CmpFile.cs @@ -34,7 +34,7 @@ public sealed unsafe class CmpFile : MetaBaseFile } public CmpFile(MetaFileManager manager) - : base(manager, MetaIndex.HumanCmp) + : base(manager, manager.MarshalAllocator, MetaIndex.HumanCmp) { AllocateData(DefaultData.Length); Reset(); diff --git a/Penumbra/Meta/Files/EqdpFile.cs b/Penumbra/Meta/Files/EqdpFile.cs index e46e82e9..34b4f25b 100644 --- a/Penumbra/Meta/Files/EqdpFile.cs +++ b/Penumbra/Meta/Files/EqdpFile.cs @@ -87,7 +87,7 @@ public sealed unsafe class ExpandedEqdpFile : MetaBaseFile } public ExpandedEqdpFile(MetaFileManager manager, GenderRace raceCode, bool accessory) - : base(manager, CharacterUtilityData.EqdpIdx(raceCode, accessory)) + : base(manager, manager.MarshalAllocator, CharacterUtilityData.EqdpIdx(raceCode, accessory)) { var def = (byte*)DefaultData.Data; var blockSize = *(ushort*)(def + IdentifierSize); diff --git a/Penumbra/Meta/Files/EqpGmpFile.cs b/Penumbra/Meta/Files/EqpGmpFile.cs index c47c84ef..a7540f4b 100644 --- a/Penumbra/Meta/Files/EqpGmpFile.cs +++ b/Penumbra/Meta/Files/EqpGmpFile.cs @@ -76,7 +76,7 @@ public unsafe class ExpandedEqpGmpBase : MetaBaseFile } public ExpandedEqpGmpBase(MetaFileManager manager, bool gmp) - : base(manager, gmp ? MetaIndex.Gmp : MetaIndex.Eqp) + : base(manager, manager.MarshalAllocator, gmp ? MetaIndex.Gmp : MetaIndex.Eqp) { AllocateData(MaxSize); Reset(); diff --git a/Penumbra/Meta/Files/EstFile.cs b/Penumbra/Meta/Files/EstFile.cs index f3860416..ba38d6d9 100644 --- a/Penumbra/Meta/Files/EstFile.cs +++ b/Penumbra/Meta/Files/EstFile.cs @@ -157,7 +157,7 @@ public sealed unsafe class EstFile : MetaBaseFile } public EstFile(MetaFileManager manager, EstType estType) - : base(manager, (MetaIndex)estType) + : base(manager, manager.MarshalAllocator, (MetaIndex)estType) { var length = DefaultData.Length; AllocateData(length + IncreaseSize); diff --git a/Penumbra/Meta/Files/EvpFile.cs b/Penumbra/Meta/Files/EvpFile.cs index 3d0b4dbe..6ab1591c 100644 --- a/Penumbra/Meta/Files/EvpFile.cs +++ b/Penumbra/Meta/Files/EvpFile.cs @@ -12,7 +12,7 @@ namespace Penumbra.Meta.Files; /// Containing Flags in each byte, 0x01 set for Body, 0x02 set for Helmet. /// Each flag corresponds to a mount row from the Mounts table and determines whether the mount disables the effect. /// -public unsafe class EvpFile : MetaBaseFile +public unsafe class EvpFile(MetaFileManager manager) : MetaBaseFile(manager, manager.MarshalAllocator, (MetaIndex)1) { public const int FlagArraySize = 512; @@ -57,8 +57,4 @@ public unsafe class EvpFile : MetaBaseFile return EvpFlag.None; } - - public EvpFile(MetaFileManager manager) - : base(manager, (MetaIndex)1) // TODO: Name - { } } diff --git a/Penumbra/Meta/Files/ImcFile.cs b/Penumbra/Meta/Files/ImcFile.cs index 892f5b44..01ef3f16 100644 --- a/Penumbra/Meta/Files/ImcFile.cs +++ b/Penumbra/Meta/Files/ImcFile.cs @@ -7,16 +7,10 @@ using Penumbra.String.Functions; namespace Penumbra.Meta.Files; -public class ImcException : Exception +public class ImcException(ImcIdentifier identifier, Utf8GamePath path) : Exception { - public readonly ImcIdentifier Identifier; - public readonly string GamePath; - - public ImcException(ImcIdentifier identifier, Utf8GamePath path) - { - Identifier = identifier; - GamePath = path.ToString(); - } + public readonly ImcIdentifier Identifier = identifier; + public readonly string GamePath = path.ToString(); public override string Message => "Could not obtain default Imc File.\n" @@ -146,7 +140,11 @@ public unsafe class ImcFile : MetaBaseFile } public ImcFile(MetaFileManager manager, ImcIdentifier identifier) - : base(manager, 0) + : this(manager, manager.MarshalAllocator, identifier) + { } + + public ImcFile(MetaFileManager manager, IFileAllocator alloc, ImcIdentifier identifier) + : base(manager, alloc, 0) { var path = identifier.GamePathString(); Path = Utf8GamePath.FromString(path, out var p) ? p : Utf8GamePath.Empty; @@ -194,7 +192,13 @@ public unsafe class ImcFile : MetaBaseFile public void Replace(ResourceHandle* resource) { var (data, length) = resource->GetData(); - var newData = Manager.AllocateDefaultMemory(ActualLength, 8); + if (length == ActualLength) + { + MemoryUtility.MemCpyUnchecked((byte*)data, Data, ActualLength); + return; + } + + var newData = Manager.XivAllocator.Allocate(ActualLength, 8); if (newData == null) { Penumbra.Log.Error($"Could not replace loaded IMC data at 0x{(ulong)resource:X}, allocation failed."); @@ -203,7 +207,7 @@ public unsafe class ImcFile : MetaBaseFile MemoryUtility.MemCpyUnchecked(newData, Data, ActualLength); - Manager.Free(data, length); - resource->SetData((IntPtr)newData, ActualLength); + Manager.XivAllocator.Release((void*)data, length); + resource->SetData((nint)newData, ActualLength); } } diff --git a/Penumbra/Meta/Files/MetaBaseFile.cs b/Penumbra/Meta/Files/MetaBaseFile.cs index ab08efc2..86a55101 100644 --- a/Penumbra/Meta/Files/MetaBaseFile.cs +++ b/Penumbra/Meta/Files/MetaBaseFile.cs @@ -1,23 +1,75 @@ using Dalamud.Memory; +using Dalamud.Plugin.Services; +using Dalamud.Utility.Signatures; +using FFXIVClientStructs.FFXIV.Client.System.Memory; +using OtterGui.Services; +using Penumbra.GameData; using Penumbra.Interop.Structs; using Penumbra.String.Functions; using CharacterUtility = Penumbra.Interop.Services.CharacterUtility; namespace Penumbra.Meta.Files; -public unsafe class MetaBaseFile : IDisposable +public unsafe interface IFileAllocator { - protected readonly MetaFileManager Manager; + public T* Allocate(int length, int alignment = 1) where T : unmanaged; + public void Release(ref T* pointer, int length) where T : unmanaged; + + public void Release(void* pointer, int length) + { + var tmp = (byte*)pointer; + Release(ref tmp, length); + } + + public byte* Allocate(int length, int alignment = 1) + => Allocate(length, alignment); +} + +public sealed class MarshalAllocator : IFileAllocator +{ + public unsafe T* Allocate(int length, int alignment = 1) where T : unmanaged + => (T*)Marshal.AllocHGlobal(length * sizeof(T)); + + public unsafe void Release(ref T* pointer, int length) where T : unmanaged + { + Marshal.FreeHGlobal((nint)pointer); + pointer = null; + } +} + +public sealed unsafe class XivFileAllocator : IFileAllocator, IService +{ + /// + /// Allocate in the games space for file storage. + /// We only need this if using any meta file. + /// + [Signature(Sigs.GetFileSpace)] + private readonly nint _getFileSpaceAddress = nint.Zero; + + public XivFileAllocator(IGameInteropProvider provider) + => provider.InitializeFromAttributes(this); + + public IMemorySpace* GetFileSpace() + => ((delegate* unmanaged)_getFileSpaceAddress)(); + + public T* Allocate(int length, int alignment = 1) where T : unmanaged + => (T*)GetFileSpace()->Malloc((ulong)(length * sizeof(T)), (ulong)alignment); + + public void Release(ref T* pointer, int length) where T : unmanaged + { + IMemorySpace.Free(pointer, (ulong)(length * sizeof(T))); + pointer = null; + } +} + +public unsafe class MetaBaseFile(MetaFileManager manager, IFileAllocator alloc, MetaIndex idx) : IDisposable +{ + protected readonly MetaFileManager Manager = manager; + protected readonly IFileAllocator Allocator = alloc; public byte* Data { get; private set; } public int Length { get; private set; } - public CharacterUtility.InternalIndex Index { get; } - - public MetaBaseFile(MetaFileManager manager, MetaIndex idx) - { - Manager = manager; - Index = CharacterUtility.ReverseIndices[(int)idx]; - } + public CharacterUtility.InternalIndex Index { get; } = CharacterUtility.ReverseIndices[(int)idx]; protected (IntPtr Data, int Length) DefaultData => Manager.CharacterUtility.DefaultResource(Index); @@ -30,7 +82,7 @@ public unsafe class MetaBaseFile : IDisposable protected void AllocateData(int length) { Length = length; - Data = (byte*)Manager.AllocateFileMemory(length); + Data = Allocator.Allocate(length); if (length > 0) GC.AddMemoryPressure(length); } @@ -38,8 +90,7 @@ public unsafe class MetaBaseFile : IDisposable /// Free memory. protected void ReleaseUnmanagedResources() { - var ptr = (IntPtr)Data; - MemoryHelper.GameFree(ref ptr, (ulong)Length); + Allocator.Release(Data, Length); if (Length > 0) GC.RemoveMemoryPressure(Length); @@ -53,7 +104,7 @@ public unsafe class MetaBaseFile : IDisposable if (newLength == Length) return; - var data = (byte*)Manager.AllocateFileMemory((ulong)newLength); + var data = Allocator.Allocate(newLength); if (newLength > Length) { MemoryUtility.MemCpyUnchecked(data, Data, Length); diff --git a/Penumbra/Meta/MetaFileManager.cs b/Penumbra/Meta/MetaFileManager.cs index 40fceb07..81c0fa3e 100644 --- a/Penumbra/Meta/MetaFileManager.cs +++ b/Penumbra/Meta/MetaFileManager.cs @@ -1,14 +1,10 @@ using Dalamud.Plugin.Services; -using Dalamud.Utility.Signatures; -using FFXIVClientStructs.FFXIV.Client.System.Memory; using OtterGui.Compression; using Penumbra.Collections; using Penumbra.Collections.Manager; -using Penumbra.GameData; using Penumbra.GameData.Data; using Penumbra.Import; using Penumbra.Interop.Services; -using Penumbra.Interop.Structs; using Penumbra.Meta.Files; using Penumbra.Mods; using Penumbra.Mods.Groups; @@ -28,6 +24,9 @@ public unsafe class MetaFileManager internal readonly ObjectIdentification Identifier; internal readonly FileCompactor Compactor; internal readonly ImcChecker ImcChecker; + internal readonly IFileAllocator MarshalAllocator = new MarshalAllocator(); + internal readonly IFileAllocator XivAllocator; + public MetaFileManager(CharacterUtility characterUtility, ResidentResourceManager residentResources, IDataManager gameData, ActiveCollectionData activeCollections, Configuration config, ValidityChecker validityChecker, ObjectIdentification identifier, @@ -42,6 +41,7 @@ public unsafe class MetaFileManager Identifier = identifier; Compactor = compactor; ImcChecker = new ImcChecker(this); + XivAllocator = new XivFileAllocator(interop); interop.InitializeFromAttributes(this); } @@ -76,57 +76,11 @@ public unsafe class MetaFileManager } } - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public void SetFile(MetaBaseFile? file, MetaIndex metaIndex) - { - if (file == null || !Config.EnableMods) - CharacterUtility.ResetResource(metaIndex); - else - CharacterUtility.SetResource(metaIndex, (nint)file.Data, file.Length); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public MetaList.MetaReverter TemporarilySetFile(MetaBaseFile? file, MetaIndex metaIndex) - => Config.EnableMods - ? file == null - ? CharacterUtility.TemporarilyResetResource(metaIndex) - : CharacterUtility.TemporarilySetResource(metaIndex, (nint)file.Data, file.Length) - : MetaList.MetaReverter.Disabled; - public void ApplyDefaultFiles(ModCollection? collection) { if (ActiveCollections.Default != collection || !CharacterUtility.Ready || !Config.EnableMods) return; ResidentResources.Reload(); - if (collection._cache == null) - CharacterUtility.ResetAll(); - else - collection._cache.Meta.SetFiles(); } - - /// - /// Allocate in the games space for file storage. - /// We only need this if using any meta file. - /// - [Signature(Sigs.GetFileSpace)] - private readonly nint _getFileSpaceAddress = nint.Zero; - - public IMemorySpace* GetFileSpace() - => ((delegate* unmanaged)_getFileSpaceAddress)(); - - public void* AllocateFileMemory(ulong length, ulong alignment = 0) - => GetFileSpace()->Malloc(length, alignment); - - public void* AllocateFileMemory(int length, int alignment = 0) - => AllocateFileMemory((ulong)length, (ulong)alignment); - - public void* AllocateDefaultMemory(ulong length, ulong alignment = 0) - => GetFileSpace()->Malloc(length, alignment); - - public void* AllocateDefaultMemory(int length, int alignment = 0) - => IMemorySpace.GetDefaultSpace()->Malloc((ulong)length, (ulong)alignment); - - public void Free(nint ptr, int length) - => IMemorySpace.Free((void*)ptr, (ulong)length); } From f9c45a2f3f9bae991a67076622c6ef7d701c94ea Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 18 Jun 2024 17:57:12 +0200 Subject: [PATCH 256/433] Clean unused functions. --- Penumbra/Collections/Cache/EqdpCache.cs | 10 ++----- Penumbra/Collections/Cache/EqpCache.cs | 12 --------- Penumbra/Collections/Cache/EstCache.cs | 12 --------- Penumbra/Collections/Cache/GmpCache.cs | 12 --------- Penumbra/Collections/Cache/IMetaCache.cs | 33 +++++------------------- Penumbra/Collections/Cache/ImcCache.cs | 7 ----- Penumbra/Collections/Cache/MetaCache.cs | 10 ------- Penumbra/Collections/Cache/RspCache.cs | 13 ---------- 8 files changed, 9 insertions(+), 100 deletions(-) diff --git a/Penumbra/Collections/Cache/EqdpCache.cs b/Penumbra/Collections/Cache/EqdpCache.cs index 6047736b..5e0626cf 100644 --- a/Penumbra/Collections/Cache/EqdpCache.cs +++ b/Penumbra/Collections/Cache/EqdpCache.cs @@ -10,17 +10,11 @@ public sealed class EqdpCache(MetaFileManager manager, ModCollection collection) private readonly Dictionary<(PrimaryId Id, GenderRace GenderRace, bool Accessory), (EqdpEntry Entry, EqdpEntry InverseMask)> _fullEntries = []; - public override void SetFiles() - { } - public EqdpEntry ApplyFullEntry(PrimaryId id, GenderRace genderRace, bool accessory, EqdpEntry originalEntry) => _fullEntries.TryGetValue((id, genderRace, accessory), out var pair) ? (originalEntry & pair.InverseMask) | pair.Entry : originalEntry; - protected override void IncorporateChangesInternal() - { } - public void Reset() { Clear(); @@ -46,8 +40,8 @@ public sealed class EqdpCache(MetaFileManager manager, ModCollection collection) if (!_fullEntries.Remove(tuple, out var pair)) return; - var mask = Eqdp.Mask(identifier.Slot); - var newMask = pair.InverseMask | mask; + var mask = Eqdp.Mask(identifier.Slot); + var newMask = pair.InverseMask | mask; if (newMask is not EqdpEntry.FullMask) _fullEntries[tuple] = (pair.Entry & ~mask, newMask); } diff --git a/Penumbra/Collections/Cache/EqpCache.cs b/Penumbra/Collections/Cache/EqpCache.cs index 7ba0c489..60e38aef 100644 --- a/Penumbra/Collections/Cache/EqpCache.cs +++ b/Penumbra/Collections/Cache/EqpCache.cs @@ -8,12 +8,6 @@ namespace Penumbra.Collections.Cache; public sealed class EqpCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - public override void SetFiles() - { } - - protected override void IncorporateChangesInternal() - { } - public unsafe EqpEntry GetValues(CharacterArmor* armor) => GetSingleValue(armor[0].Set, EquipSlot.Head) | GetSingleValue(armor[1].Set, EquipSlot.Body) @@ -28,12 +22,6 @@ public sealed class EqpCache(MetaFileManager manager, ModCollection collection) public void Reset() => Clear(); - protected override void ApplyModInternal(EqpIdentifier identifier, EqpEntry entry) - { } - - protected override void RevertModInternal(EqpIdentifier identifier) - { } - protected override void Dispose(bool _) => Clear(); } diff --git a/Penumbra/Collections/Cache/EstCache.cs b/Penumbra/Collections/Cache/EstCache.cs index ff94324e..aff8beef 100644 --- a/Penumbra/Collections/Cache/EstCache.cs +++ b/Penumbra/Collections/Cache/EstCache.cs @@ -6,12 +6,6 @@ namespace Penumbra.Collections.Cache; public sealed class EstCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - public override void SetFiles() - { } - - protected override void IncorporateChangesInternal() - { } - public EstEntry GetEstEntry(EstIdentifier identifier) => TryGetValue(identifier, out var entry) ? entry.Entry @@ -20,12 +14,6 @@ public sealed class EstCache(MetaFileManager manager, ModCollection collection) public void Reset() => Clear(); - protected override void ApplyModInternal(EstIdentifier identifier, EstEntry entry) - { } - - protected override void RevertModInternal(EstIdentifier identifier) - { } - protected override void Dispose(bool _) => Clear(); } diff --git a/Penumbra/Collections/Cache/GmpCache.cs b/Penumbra/Collections/Cache/GmpCache.cs index 541b424d..9170b871 100644 --- a/Penumbra/Collections/Cache/GmpCache.cs +++ b/Penumbra/Collections/Cache/GmpCache.cs @@ -6,21 +6,9 @@ namespace Penumbra.Collections.Cache; public sealed class GmpCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - public override void SetFiles() - { } - - protected override void IncorporateChangesInternal() - { } - public void Reset() => Clear(); - protected override void ApplyModInternal(GmpIdentifier identifier, GmpEntry entry) - { } - - protected override void RevertModInternal(GmpIdentifier identifier) - { } - protected override void Dispose(bool _) => Clear(); } diff --git a/Penumbra/Collections/Cache/IMetaCache.cs b/Penumbra/Collections/Cache/IMetaCache.cs index dd218b48..fecc6f50 100644 --- a/Penumbra/Collections/Cache/IMetaCache.cs +++ b/Penumbra/Collections/Cache/IMetaCache.cs @@ -4,30 +4,19 @@ using Penumbra.Mods.Editor; namespace Penumbra.Collections.Cache; -public abstract class MetaCacheBase +public abstract class MetaCacheBase(MetaFileManager manager, ModCollection collection) : Dictionary where TIdentifier : unmanaged, IMetaIdentifier where TEntry : unmanaged { - protected MetaCacheBase(MetaFileManager manager, ModCollection collection) - { - Manager = manager; - Collection = collection; - if (!Manager.CharacterUtility.Ready) - Manager.CharacterUtility.LoadingFinished += IncorporateChanges; - } - - protected readonly MetaFileManager Manager; - protected readonly ModCollection Collection; + protected readonly MetaFileManager Manager = manager; + protected readonly ModCollection Collection = collection; public void Dispose() { - Manager.CharacterUtility.LoadingFinished -= IncorporateChanges; Dispose(true); } - public abstract void SetFiles(); - public bool ApplyMod(IMod source, TIdentifier identifier, TEntry entry) { lock (this) @@ -59,20 +48,12 @@ public abstract class MetaCacheBase return true; } - private void IncorporateChanges() - { - lock (this) - { - IncorporateChangesInternal(); - } - if (Manager.ActiveCollections.Default == Collection && Manager.Config.EnableMods) - SetFiles(); - } + protected virtual void ApplyModInternal(TIdentifier identifier, TEntry entry) + { } - protected abstract void ApplyModInternal(TIdentifier identifier, TEntry entry); - protected abstract void RevertModInternal(TIdentifier identifier); - protected abstract void IncorporateChangesInternal(); + protected virtual void RevertModInternal(TIdentifier identifier) + { } protected virtual void Dispose(bool _) { } diff --git a/Penumbra/Collections/Cache/ImcCache.cs b/Penumbra/Collections/Cache/ImcCache.cs index e7eedc04..40c3d2c7 100644 --- a/Penumbra/Collections/Cache/ImcCache.cs +++ b/Penumbra/Collections/Cache/ImcCache.cs @@ -10,9 +10,6 @@ public sealed class ImcCache(MetaFileManager manager, ModCollection collection) { private readonly Dictionary)> _imcFiles = []; - public override void SetFiles() - { } - public bool HasFile(ByteString path) => _imcFiles.ContainsKey(path); @@ -28,10 +25,6 @@ public sealed class ImcCache(MetaFileManager manager, ModCollection collection) return true; } - protected override void IncorporateChangesInternal() - { } - - public void Reset() { foreach (var (_, (file, set)) in _imcFiles) diff --git a/Penumbra/Collections/Cache/MetaCache.cs b/Penumbra/Collections/Cache/MetaCache.cs index 253f3c7f..02056fad 100644 --- a/Penumbra/Collections/Cache/MetaCache.cs +++ b/Penumbra/Collections/Cache/MetaCache.cs @@ -29,16 +29,6 @@ public class MetaCache(MetaFileManager manager, ModCollection collection) .Concat(Imc.Select(kvp => ((IMetaIdentifier)kvp.Key, kvp.Value.Source))) .Concat(GlobalEqp.Select(kvp => ((IMetaIdentifier)kvp.Key, kvp.Value))); - public void SetFiles() - { - Eqp.SetFiles(); - Eqdp.SetFiles(); - Est.SetFiles(); - Gmp.SetFiles(); - Rsp.SetFiles(); - Imc.SetFiles(); - } - public void Reset() { Eqp.Reset(); diff --git a/Penumbra/Collections/Cache/RspCache.cs b/Penumbra/Collections/Cache/RspCache.cs index 8a983c6c..064b1f44 100644 --- a/Penumbra/Collections/Cache/RspCache.cs +++ b/Penumbra/Collections/Cache/RspCache.cs @@ -5,22 +5,9 @@ namespace Penumbra.Collections.Cache; public sealed class RspCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - public override void SetFiles() - { } - - protected override void IncorporateChangesInternal() - { } - public void Reset() => Clear(); - protected override void ApplyModInternal(RspIdentifier identifier, RspEntry entry) - { } - - protected override void RevertModInternal(RspIdentifier identifier) - { } - - protected override void Dispose(bool _) => Clear(); } From cf1dcfcb7cb27e6928810b75c63f1f352da4ecd2 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 18 Jun 2024 18:33:37 +0200 Subject: [PATCH 257/433] Improve Path preprocessing. --- .../Interop/PathResolving/PathResolver.cs | 23 ++-- .../Interop/PathResolving/SubfileHelper.cs | 16 --- .../Processing/AvfxPathPreProcessor.cs | 16 +++ .../Processing/FilePostProcessService.cs | 39 ++++++ .../Processing/GamePathPreProcessService.cs | 37 +++++ .../Processing/ImcFilePostProcessor.cs | 30 ++++ .../Interop/Processing/ImcPathPreProcessor.cs | 18 +++ .../Processing/MaterialFilePostProcessor.cs | 18 +++ .../Processing/MtrlPathPreProcessor.cs | 16 +++ .../Interop/Processing/TmbPathPreProcessor.cs | 16 +++ .../Interop/ResourceLoading/ResourceLoader.cs | 69 ---------- Penumbra/Interop/Services/CharacterUtility.cs | 8 +- Penumbra/Interop/Services/MetaList.cs | 130 +----------------- Penumbra/UI/Tabs/Debug/DebugTab.cs | 19 --- 14 files changed, 209 insertions(+), 246 deletions(-) create mode 100644 Penumbra/Interop/Processing/AvfxPathPreProcessor.cs create mode 100644 Penumbra/Interop/Processing/FilePostProcessService.cs create mode 100644 Penumbra/Interop/Processing/GamePathPreProcessService.cs create mode 100644 Penumbra/Interop/Processing/ImcFilePostProcessor.cs create mode 100644 Penumbra/Interop/Processing/ImcPathPreProcessor.cs create mode 100644 Penumbra/Interop/Processing/MaterialFilePostProcessor.cs create mode 100644 Penumbra/Interop/Processing/MtrlPathPreProcessor.cs create mode 100644 Penumbra/Interop/Processing/TmbPathPreProcessor.cs diff --git a/Penumbra/Interop/PathResolving/PathResolver.cs b/Penumbra/Interop/PathResolving/PathResolver.cs index e069e3ea..f31b3323 100644 --- a/Penumbra/Interop/PathResolving/PathResolver.cs +++ b/Penumbra/Interop/PathResolving/PathResolver.cs @@ -2,6 +2,7 @@ using FFXIVClientStructs.FFXIV.Client.System.Resource; using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.Collections.Manager; +using Penumbra.Interop.Processing; using Penumbra.Interop.ResourceLoading; using Penumbra.String.Classes; using Penumbra.Util; @@ -15,14 +16,16 @@ public class PathResolver : IDisposable private readonly CollectionManager _collectionManager; private readonly ResourceLoader _loader; - private readonly SubfileHelper _subfileHelper; - private readonly PathState _pathState; - private readonly MetaState _metaState; - private readonly GameState _gameState; - private readonly CollectionResolver _collectionResolver; + private readonly SubfileHelper _subfileHelper; + private readonly PathState _pathState; + private readonly MetaState _metaState; + private readonly GameState _gameState; + private readonly CollectionResolver _collectionResolver; + private readonly GamePathPreProcessService _preprocessor; - public unsafe PathResolver(PerformanceTracker performance, Configuration config, CollectionManager collectionManager, ResourceLoader loader, - SubfileHelper subfileHelper, PathState pathState, MetaState metaState, CollectionResolver collectionResolver, GameState gameState) + public PathResolver(PerformanceTracker performance, Configuration config, CollectionManager collectionManager, ResourceLoader loader, + SubfileHelper subfileHelper, PathState pathState, MetaState metaState, CollectionResolver collectionResolver, GameState gameState, + GamePathPreProcessService preprocessor) { _performance = performance; _config = config; @@ -31,6 +34,7 @@ public class PathResolver : IDisposable _pathState = pathState; _metaState = metaState; _gameState = gameState; + _preprocessor = preprocessor; _collectionResolver = collectionResolver; _loader = loader; _loader.ResolvePath = ResolvePath; @@ -102,11 +106,10 @@ public class PathResolver : IDisposable // so that the functions loading tex and shpk can find that path and use its collection. // We also need to handle defaulted materials against a non-default collection. var path = resolved == null ? gamePath.Path : resolved.Value.InternalName; - SubfileHelper.HandleCollection(resolveData, path, nonDefault, type, resolved, gamePath, out var pair); - return pair; + return _preprocessor.PreProcess(resolveData, path, nonDefault, type, resolved, gamePath); } - public unsafe void Dispose() + public void Dispose() { _loader.ResetResolvePath(); } diff --git a/Penumbra/Interop/PathResolving/SubfileHelper.cs b/Penumbra/Interop/PathResolving/SubfileHelper.cs index b9631cf2..3cefd98d 100644 --- a/Penumbra/Interop/PathResolving/SubfileHelper.cs +++ b/Penumbra/Interop/PathResolving/SubfileHelper.cs @@ -66,22 +66,6 @@ public sealed unsafe class SubfileHelper : IDisposable, IReadOnlyCollection Materials, TMB, and AVFX need to be set per collection, so they can load their sub files independently of each other. - public static void HandleCollection(ResolveData resolveData, ByteString path, bool nonDefault, ResourceType type, FullPath? resolved, - Utf8GamePath originalPath, out (FullPath?, ResolveData) data) - { - resolved = type switch - { - ResourceType.Mtrl when nonDefault => PathDataHandler.CreateMtrl(path, resolveData.ModCollection, originalPath), - ResourceType.Avfx when nonDefault => PathDataHandler.CreateAvfx(path, resolveData.ModCollection), - ResourceType.Tmb when nonDefault => PathDataHandler.CreateTmb(path, resolveData.ModCollection), - ResourceType.Imc when resolveData.ModCollection.MetaCache?.Imc.HasFile(path) ?? false => PathDataHandler.CreateImc(path, - resolveData.ModCollection), - _ => resolved, - }; - data = (resolved, resolveData); - } - public void Dispose() { _loader.ResourceLoaded -= SubfileContainerRequested; diff --git a/Penumbra/Interop/Processing/AvfxPathPreProcessor.cs b/Penumbra/Interop/Processing/AvfxPathPreProcessor.cs new file mode 100644 index 00000000..56f693e6 --- /dev/null +++ b/Penumbra/Interop/Processing/AvfxPathPreProcessor.cs @@ -0,0 +1,16 @@ +using Penumbra.Api.Enums; +using Penumbra.Collections; +using Penumbra.Interop.PathResolving; +using Penumbra.String; +using Penumbra.String.Classes; + +namespace Penumbra.Interop.Processing; + +public sealed class AvfxPathPreProcessor : IPathPreProcessor +{ + public ResourceType Type + => ResourceType.Avfx; + + public FullPath? PreProcess(ResolveData resolveData, ByteString path, Utf8GamePath _, bool nonDefault, FullPath? resolved) + => nonDefault ? PathDataHandler.CreateAvfx(path, resolveData.ModCollection) : resolved; +} diff --git a/Penumbra/Interop/Processing/FilePostProcessService.cs b/Penumbra/Interop/Processing/FilePostProcessService.cs new file mode 100644 index 00000000..0dc62b3d --- /dev/null +++ b/Penumbra/Interop/Processing/FilePostProcessService.cs @@ -0,0 +1,39 @@ +using System.Collections.Frozen; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Interop.ResourceLoading; +using Penumbra.Interop.Structs; +using Penumbra.String; + +namespace Penumbra.Interop.Processing; + +public interface IFilePostProcessor : IService +{ + public ResourceType Type { get; } + public unsafe void PostProcess(ResourceHandle* resource, ByteString originalGamePath, ReadOnlySpan additionalData); +} + +public unsafe class FilePostProcessService : IRequiredService, IDisposable +{ + private readonly ResourceLoader _resourceLoader; + private readonly FrozenDictionary _processors; + + public FilePostProcessService(ResourceLoader resourceLoader, ServiceManager services) + { + _resourceLoader = resourceLoader; + _processors = services.GetServicesImplementing().ToFrozenDictionary(s => s.Type, s => s); + _resourceLoader.FileLoaded += OnFileLoaded; + } + + public void Dispose() + { + _resourceLoader.FileLoaded -= OnFileLoaded; + } + + private void OnFileLoaded(ResourceHandle* resource, ByteString path, bool returnValue, bool custom, + ReadOnlySpan additionalData) + { + if (_processors.TryGetValue(resource->FileType, out var processor)) + processor.PostProcess(resource, path, additionalData); + } +} diff --git a/Penumbra/Interop/Processing/GamePathPreProcessService.cs b/Penumbra/Interop/Processing/GamePathPreProcessService.cs new file mode 100644 index 00000000..004b7168 --- /dev/null +++ b/Penumbra/Interop/Processing/GamePathPreProcessService.cs @@ -0,0 +1,37 @@ +using System.Collections.Frozen; +using OtterGui.Services; +using Penumbra.Api.Enums; +using Penumbra.Collections; +using Penumbra.String; +using Penumbra.String.Classes; + +namespace Penumbra.Interop.Processing; + +public interface IPathPreProcessor : IService +{ + public ResourceType Type { get; } + + public FullPath? PreProcess(ResolveData resolveData, ByteString path, Utf8GamePath originalGamePath, bool nonDefault, FullPath? resolved); +} + +public class GamePathPreProcessService : IService +{ + private readonly FrozenDictionary _processors; + + public GamePathPreProcessService(ServiceManager services) + { + _processors = services.GetServicesImplementing().ToFrozenDictionary(s => s.Type, s => s); + } + + + public (FullPath? Path, ResolveData Data) PreProcess(ResolveData resolveData, ByteString path, bool nonDefault, ResourceType type, + FullPath? resolved, + Utf8GamePath originalPath) + { + if (!_processors.TryGetValue(type, out var processor)) + return (resolved, resolveData); + + resolved = processor.PreProcess(resolveData, path, originalPath, nonDefault, resolved); + return (resolved, resolveData); + } +} diff --git a/Penumbra/Interop/Processing/ImcFilePostProcessor.cs b/Penumbra/Interop/Processing/ImcFilePostProcessor.cs new file mode 100644 index 00000000..4a0ebe22 --- /dev/null +++ b/Penumbra/Interop/Processing/ImcFilePostProcessor.cs @@ -0,0 +1,30 @@ +using Penumbra.Api.Enums; +using Penumbra.Collections.Manager; +using Penumbra.Interop.PathResolving; +using Penumbra.Interop.Structs; +using Penumbra.String; + +namespace Penumbra.Interop.Processing; + +public sealed class ImcFilePostProcessor(CollectionStorage collections) : IFilePostProcessor +{ + public ResourceType Type + => ResourceType.Imc; + + public unsafe void PostProcess(ResourceHandle* resource, ByteString originalGamePath, ReadOnlySpan additionalData) + { + if (!PathDataHandler.Read(additionalData, out var data) || data.Discriminator != PathDataHandler.Discriminator) + return; + + var collection = collections.ByLocalId(data.Collection); + if (collection.MetaCache is not { } cache) + return; + + if (!cache.Imc.GetFile(originalGamePath, out var file)) + return; + + file.Replace(resource); + Penumbra.Log.Information( + $"[ResourceLoader] Loaded {originalGamePath} from file and replaced with IMC from collection {collection.AnonymizedName}."); + } +} diff --git a/Penumbra/Interop/Processing/ImcPathPreProcessor.cs b/Penumbra/Interop/Processing/ImcPathPreProcessor.cs new file mode 100644 index 00000000..907d7587 --- /dev/null +++ b/Penumbra/Interop/Processing/ImcPathPreProcessor.cs @@ -0,0 +1,18 @@ +using Penumbra.Api.Enums; +using Penumbra.Collections; +using Penumbra.Interop.PathResolving; +using Penumbra.String; +using Penumbra.String.Classes; + +namespace Penumbra.Interop.Processing; + +public sealed class ImcPathPreProcessor : IPathPreProcessor +{ + public ResourceType Type + => ResourceType.Imc; + + public FullPath? PreProcess(ResolveData resolveData, ByteString path, Utf8GamePath originalGamePath, bool _, FullPath? resolved) + => resolveData.ModCollection.MetaCache?.Imc.HasFile(originalGamePath.Path) ?? false + ? PathDataHandler.CreateImc(path, resolveData.ModCollection) + : resolved; +} diff --git a/Penumbra/Interop/Processing/MaterialFilePostProcessor.cs b/Penumbra/Interop/Processing/MaterialFilePostProcessor.cs new file mode 100644 index 00000000..02b5d46c --- /dev/null +++ b/Penumbra/Interop/Processing/MaterialFilePostProcessor.cs @@ -0,0 +1,18 @@ +using Penumbra.Api.Enums; +using Penumbra.Interop.PathResolving; +using Penumbra.Interop.Structs; +using Penumbra.String; + +namespace Penumbra.Interop.Processing; + +public sealed class MaterialFilePostProcessor //: IFilePostProcessor +{ + public ResourceType Type + => ResourceType.Mtrl; + + public unsafe void PostProcess(ResourceHandle* resource, ByteString originalGamePath, ReadOnlySpan additionalData) + { + if (!PathDataHandler.ReadMtrl(additionalData, out var data)) + return; + } +} diff --git a/Penumbra/Interop/Processing/MtrlPathPreProcessor.cs b/Penumbra/Interop/Processing/MtrlPathPreProcessor.cs new file mode 100644 index 00000000..8fb2400b --- /dev/null +++ b/Penumbra/Interop/Processing/MtrlPathPreProcessor.cs @@ -0,0 +1,16 @@ +using Penumbra.Api.Enums; +using Penumbra.Collections; +using Penumbra.Interop.PathResolving; +using Penumbra.String; +using Penumbra.String.Classes; + +namespace Penumbra.Interop.Processing; + +public sealed class MtrlPathPreProcessor : IPathPreProcessor +{ + public ResourceType Type + => ResourceType.Mtrl; + + public FullPath? PreProcess(ResolveData resolveData, ByteString path, Utf8GamePath originalGamePath, bool nonDefault, FullPath? resolved) + => nonDefault ? PathDataHandler.CreateMtrl(path, resolveData.ModCollection, originalGamePath) : resolved; +} diff --git a/Penumbra/Interop/Processing/TmbPathPreProcessor.cs b/Penumbra/Interop/Processing/TmbPathPreProcessor.cs new file mode 100644 index 00000000..dd887819 --- /dev/null +++ b/Penumbra/Interop/Processing/TmbPathPreProcessor.cs @@ -0,0 +1,16 @@ +using Penumbra.Api.Enums; +using Penumbra.Collections; +using Penumbra.Interop.PathResolving; +using Penumbra.String; +using Penumbra.String.Classes; + +namespace Penumbra.Interop.Processing; + +public sealed class TmbPathPreProcessor : IPathPreProcessor +{ + public ResourceType Type + => ResourceType.Tmb; + + public FullPath? PreProcess(ResolveData resolveData, ByteString path, Utf8GamePath _, bool nonDefault, FullPath? resolved) + => nonDefault ? PathDataHandler.CreateTmb(path, resolveData.ModCollection) : resolved; +} diff --git a/Penumbra/Interop/ResourceLoading/ResourceLoader.cs b/Penumbra/Interop/ResourceLoading/ResourceLoader.cs index fae38907..7b49beab 100644 --- a/Penumbra/Interop/ResourceLoading/ResourceLoader.cs +++ b/Penumbra/Interop/ResourceLoading/ResourceLoader.cs @@ -1,9 +1,6 @@ -using System.Collections.Frozen; using FFXIVClientStructs.FFXIV.Client.System.Resource; -using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Collections; -using Penumbra.Collections.Manager; using Penumbra.Interop.PathResolving; using Penumbra.Interop.SafeHandles; using Penumbra.Interop.Structs; @@ -13,72 +10,6 @@ using FileMode = Penumbra.Interop.Structs.FileMode; namespace Penumbra.Interop.ResourceLoading; -public interface IFilePostProcessor : IService -{ - public ResourceType Type { get; } - public unsafe void PostProcess(ResourceHandle* resource, ByteString originalGamePath, ReadOnlySpan additionalData); -} - -public sealed class MaterialFilePostProcessor : IFilePostProcessor -{ - public ResourceType Type - => ResourceType.Mtrl; - - public unsafe void PostProcess(ResourceHandle* resource, ByteString originalGamePath, ReadOnlySpan additionalData) - { - if (!PathDataHandler.ReadMtrl(additionalData, out var data)) - return; - } -} - -public sealed class ImcFilePostProcessor(CollectionStorage collections) : IFilePostProcessor -{ - public ResourceType Type - => ResourceType.Imc; - - public unsafe void PostProcess(ResourceHandle* resource, ByteString originalGamePath, ReadOnlySpan additionalData) - { - if (!PathDataHandler.Read(additionalData, out var data) || data.Discriminator != PathDataHandler.Discriminator) - return; - - var collection = collections.ByLocalId(data.Collection); - if (collection.MetaCache is not { } cache) - return; - - if (!cache.Imc.GetFile(originalGamePath, out var file)) - return; - - file.Replace(resource); - Penumbra.Log.Information( - $"[ResourceLoader] Loaded {originalGamePath} from file and replaced with IMC from collection {collection.AnonymizedName}."); - } -} - -public unsafe class FilePostProcessService : IRequiredService, IDisposable -{ - private readonly ResourceLoader _resourceLoader; - private readonly FrozenDictionary _processors; - - public FilePostProcessService(ResourceLoader resourceLoader, ServiceManager services) - { - _resourceLoader = resourceLoader; - _processors = services.GetServicesImplementing().ToFrozenDictionary(s => s.Type, s => s); - _resourceLoader.FileLoaded += OnFileLoaded; - } - - public void Dispose() - { - _resourceLoader.FileLoaded -= OnFileLoaded; - } - - private void OnFileLoaded(ResourceHandle* resource, ByteString path, bool returnValue, bool custom, - ReadOnlySpan additionalData) - { - if (_processors.TryGetValue(resource->FileType, out var processor)) - processor.PostProcess(resource, path, additionalData); - } -} - public unsafe class ResourceLoader : IDisposable { private readonly ResourceService _resources; diff --git a/Penumbra/Interop/Services/CharacterUtility.cs b/Penumbra/Interop/Services/CharacterUtility.cs index 9459df06..0877d221 100644 --- a/Penumbra/Interop/Services/CharacterUtility.cs +++ b/Penumbra/Interop/Services/CharacterUtility.cs @@ -46,9 +46,6 @@ public unsafe class CharacterUtility : IDisposable private readonly MetaList[] _lists; - public IReadOnlyList Lists - => _lists; - public (nint Address, int Size) DefaultResource(InternalIndex idx) => _lists[idx.Value].DefaultResource; @@ -58,7 +55,7 @@ public unsafe class CharacterUtility : IDisposable { interop.InitializeFromAttributes(this); _lists = Enumerable.Range(0, RelevantIndices.Length) - .Select(idx => new MetaList(this, new InternalIndex(idx))) + .Select(idx => new MetaList(new InternalIndex(idx))) .ToArray(); _framework = framework; LoadingFinished += () => Penumbra.Log.Debug("Loading of CharacterUtility finished."); @@ -124,9 +121,6 @@ public unsafe class CharacterUtility : IDisposable if (!Ready) return; - foreach (var list in _lists) - list.Dispose(); - Address->HumanPbdResource = (ResourceHandle*)DefaultHumanPbdResource; Address->TransparentTexResource = (TextureResourceHandle*)DefaultTransparentResource; Address->DecalTexResource = (TextureResourceHandle*)DefaultDecalResource; diff --git a/Penumbra/Interop/Services/MetaList.cs b/Penumbra/Interop/Services/MetaList.cs index 24d3f088..839c289e 100644 --- a/Penumbra/Interop/Services/MetaList.cs +++ b/Penumbra/Interop/Services/MetaList.cs @@ -2,26 +2,14 @@ using Penumbra.Interop.Structs; namespace Penumbra.Interop.Services; -public unsafe class MetaList : IDisposable +public class MetaList(CharacterUtility.InternalIndex index) { - private readonly CharacterUtility _utility; - private readonly LinkedList _entries = new(); - public readonly CharacterUtility.InternalIndex Index; - public readonly MetaIndex GlobalMetaIndex; - - public IReadOnlyCollection Entries - => _entries; + public readonly CharacterUtility.InternalIndex Index = index; + public readonly MetaIndex GlobalMetaIndex = CharacterUtility.RelevantIndices[index.Value]; private nint _defaultResourceData = nint.Zero; - private int _defaultResourceSize = 0; - public bool Ready { get; private set; } = false; - - public MetaList(CharacterUtility utility, CharacterUtility.InternalIndex index) - { - _utility = utility; - Index = index; - GlobalMetaIndex = CharacterUtility.RelevantIndices[index.Value]; - } + private int _defaultResourceSize; + public bool Ready { get; private set; } public void SetDefaultResource(nint data, int size) { @@ -31,116 +19,8 @@ public unsafe class MetaList : IDisposable _defaultResourceData = data; _defaultResourceSize = size; Ready = _defaultResourceData != nint.Zero && size != 0; - if (_entries.Count <= 0) - return; - - var first = _entries.First!.Value; - SetResource(first.Data, first.Length); } public (nint Address, int Size) DefaultResource => (_defaultResourceData, _defaultResourceSize); - - public MetaReverter TemporarilySetResource(nint data, int length) - { - Penumbra.Log.Excessive($"Temporarily set resource {GlobalMetaIndex} to 0x{(ulong)data:X} ({length} bytes)."); - var reverter = new MetaReverter(this, data, length); - _entries.AddFirst(reverter); - SetResourceInternal(data, length); - return reverter; - } - - public MetaReverter TemporarilyResetResource() - { - Penumbra.Log.Excessive( - $"Temporarily reset resource {GlobalMetaIndex} to default at 0x{_defaultResourceData:X} ({_defaultResourceSize} bytes)."); - var reverter = new MetaReverter(this); - _entries.AddFirst(reverter); - ResetResourceInternal(); - return reverter; - } - - public void SetResource(nint data, int length) - { - Penumbra.Log.Excessive($"Set resource {GlobalMetaIndex} to 0x{(ulong)data:X} ({length} bytes)."); - SetResourceInternal(data, length); - } - - public void ResetResource() - { - Penumbra.Log.Excessive($"Reset resource {GlobalMetaIndex} to default at 0x{_defaultResourceData:X} ({_defaultResourceSize} bytes)."); - ResetResourceInternal(); - } - - /// Set the currently stored data of this resource to new values. - private void SetResourceInternal(nint data, int length) - { - if (!Ready) - return; - - var resource = _utility.Address->Resource(GlobalMetaIndex); - resource->SetData(data, length); - } - - /// Reset the currently stored data of this resource to its default values. - private void ResetResourceInternal() - => SetResourceInternal(_defaultResourceData, _defaultResourceSize); - - private void SetResourceToDefaultCollection() - {} - - public void Dispose() - { - if (_entries.Count > 0) - { - foreach (var entry in _entries) - entry.Disposed = true; - - _entries.Clear(); - } - - ResetResourceInternal(); - } - - public sealed class MetaReverter(MetaList metaList, nint data, int length) : IDisposable - { - public static readonly MetaReverter Disabled = new(null!) { Disposed = true }; - - public readonly MetaList MetaList = metaList; - public readonly nint Data = data; - public readonly int Length = length; - public readonly bool Resetter; - public bool Disposed; - - public MetaReverter(MetaList metaList) - : this(metaList, nint.Zero, 0) - => Resetter = true; - - public void Dispose() - { - if (Disposed) - return; - - var list = MetaList._entries; - var wasCurrent = ReferenceEquals(this, list.First?.Value); - list.Remove(this); - if (!wasCurrent) - return; - - if (list.Count == 0) - { - MetaList.SetResourceToDefaultCollection(); - } - else - { - var next = list.First!.Value; - if (next.Resetter) - MetaList.ResetResourceInternal(); - else - MetaList.SetResourceInternal(next.Data, next.Length); - } - - Disposed = true; - } - } } diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index 396029d5..1519ebf0 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -179,8 +179,6 @@ public class DebugTab : Window, ITab ImGui.NewLine(); DrawData(); ImGui.NewLine(); - DrawDebugTabMetaLists(); - ImGui.NewLine(); DrawResourceProblems(); ImGui.NewLine(); DrawPlayerModelInfo(); @@ -788,23 +786,6 @@ public class DebugTab : Window, ITab } } - private void DrawDebugTabMetaLists() - { - if (!ImGui.CollapsingHeader("Metadata Changes")) - return; - - using var table = Table("##DebugMetaTable", 3, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; - - foreach (var list in _characterUtility.Lists) - { - ImGuiUtil.DrawTableColumn(list.GlobalMetaIndex.ToString()); - ImGuiUtil.DrawTableColumn(list.Entries.Count.ToString()); - ImGuiUtil.DrawTableColumn(string.Join(", ", list.Entries.Select(e => $"0x{e.Data:X}"))); - } - } - /// Draw information about the resident resource files. private unsafe void DrawDebugResidentResources() { From e05dbe988570d949f6af81f65b3073c875fb9fb9 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 18 Jun 2024 21:59:04 +0200 Subject: [PATCH 258/433] Make everything services. --- OtterGui | 2 +- Penumbra/Api/TempModManager.cs | 3 +- .../Cache/CollectionCacheManager.cs | 3 +- .../Collections/Manager/ActiveCollections.cs | 5 +- .../Collections/Manager/CollectionEditor.cs | 3 +- .../Collections/Manager/CollectionManager.cs | 3 +- .../Collections/Manager/CollectionStorage.cs | 5 +- .../Collections/Manager/InheritanceManager.cs | 14 +- .../Manager/TempCollectionManager.cs | 3 +- Penumbra/CommandHandler.cs | 4 +- Penumbra/Configuration.cs | 3 +- Penumbra/EphemeralConfig.cs | 3 +- Penumbra/Import/Models/ModelManager.cs | 4 +- Penumbra/Import/Textures/TextureManager.cs | 26 ++- .../Interop/PathResolving/CutsceneService.cs | 3 +- .../IdentifiedCollectionCache.cs | 4 +- Penumbra/Interop/PathResolving/MetaState.cs | 3 +- .../Interop/PathResolving/PathResolver.cs | 3 +- Penumbra/Interop/PathResolving/PathState.cs | 3 +- .../Interop/PathResolving/SubfileHelper.cs | 4 +- .../ResourceLoading/FileReadService.cs | 3 +- .../Interop/ResourceLoading/ResourceLoader.cs | 5 +- .../ResourceLoading/ResourceManagerService.cs | 3 +- .../ResourceLoading/ResourceService.cs | 3 +- .../Interop/ResourceLoading/TexMdlService.cs | 3 +- .../ResourceTree/ResourceTreeFactory.cs | 3 +- Penumbra/Interop/Services/CharacterUtility.cs | 3 +- Penumbra/Interop/Services/FontReloader.cs | 3 +- Penumbra/Interop/Services/ModelRenderer.cs | 9 +- Penumbra/Interop/Services/RedrawService.cs | 11 +- .../Services/ResidentResourceManager.cs | 5 +- Penumbra/Meta/MetaFileManager.cs | 3 +- Penumbra/Mods/Editor/DuplicateManager.cs | 3 +- Penumbra/Mods/Editor/MdlMaterialEditor.cs | 3 +- Penumbra/Mods/Editor/ModEditor.cs | 4 +- Penumbra/Mods/Editor/ModFileCollection.cs | 3 +- Penumbra/Mods/Editor/ModFileEditor.cs | 3 +- Penumbra/Mods/Editor/ModMerger.cs | 3 +- Penumbra/Mods/Editor/ModNormalizer.cs | 4 +- Penumbra/Mods/Editor/ModSwapEditor.cs | 4 +- Penumbra/Mods/Manager/ModCacheManager.cs | 3 +- Penumbra/Mods/Manager/ModDataEditor.cs | 5 +- Penumbra/Mods/Manager/ModExportManager.cs | 3 +- Penumbra/Mods/Manager/ModFileSystem.cs | 5 +- Penumbra/Mods/Manager/ModImportManager.cs | 6 +- Penumbra/Mods/Manager/ModManager.cs | 3 +- Penumbra/Mods/ModCreator.cs | 3 +- Penumbra/Services/StaticServiceManager.cs | 154 +----------------- Penumbra/UI/AdvancedWindow/ItemSwapTab.cs | 3 +- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 3 +- Penumbra/UI/AdvancedWindow/ModMergeTab.cs | 5 +- Penumbra/UI/ChangedItemDrawer.cs | 5 +- Penumbra/UI/Changelog.cs | 3 +- Penumbra/UI/Classes/CollectionSelectHeader.cs | 3 +- Penumbra/UI/ConfigWindow.cs | 3 +- Penumbra/UI/FileDialogService.cs | 3 +- Penumbra/UI/ImportPopup.cs | 3 +- Penumbra/UI/LaunchButton.cs | 3 +- Penumbra/UI/ModsTab/ModFileSystemSelector.cs | 9 +- Penumbra/UI/ModsTab/ModPanel.cs | 3 +- .../UI/ModsTab/ModPanelChangedItemsTab.cs | 28 +--- Penumbra/UI/ModsTab/ModPanelCollectionsTab.cs | 18 +- Penumbra/UI/ModsTab/ModPanelConflictsTab.cs | 3 +- Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs | 3 +- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 4 +- Penumbra/UI/ModsTab/ModPanelTabBar.cs | 11 +- Penumbra/UI/ModsTab/MultiModPanel.cs | 7 +- Penumbra/UI/PredefinedTagManager.cs | 4 +- .../UI/ResourceWatcher/ResourceWatcher.cs | 3 +- Penumbra/UI/Tabs/ChangedItemsTab.cs | 3 +- Penumbra/UI/Tabs/CollectionsTab.cs | 3 +- Penumbra/UI/Tabs/ConfigTabBar.cs | 3 +- Penumbra/UI/Tabs/Debug/DebugTab.cs | 6 +- Penumbra/UI/Tabs/EffectiveTab.cs | 3 +- Penumbra/UI/Tabs/MessagesTab.cs | 3 +- Penumbra/UI/Tabs/ModsTab.cs | 3 +- Penumbra/UI/Tabs/OnScreenTab.cs | 10 +- Penumbra/UI/Tabs/ResourceTab.cs | 3 +- Penumbra/UI/Tabs/SettingsTab.cs | 3 +- Penumbra/UI/TutorialService.cs | 3 +- Penumbra/UI/WindowSystem.cs | 3 +- 81 files changed, 220 insertions(+), 317 deletions(-) diff --git a/OtterGui b/OtterGui index e95c0f04..caa9e9b9 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit e95c0f04edc7e85aea67498fd8bf495a7fe6d3c8 +Subproject commit caa9e9b9a5dc3928eba10b315cf6a0f6f1d84b65 diff --git a/Penumbra/Api/TempModManager.cs b/Penumbra/Api/TempModManager.cs index cbb07436..0b52e64a 100644 --- a/Penumbra/Api/TempModManager.cs +++ b/Penumbra/Api/TempModManager.cs @@ -1,3 +1,4 @@ +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.Meta.Manipulations; @@ -18,7 +19,7 @@ public enum RedirectResult FilteredGamePath = 3, } -public class TempModManager : IDisposable +public class TempModManager : IDisposable, IService { private readonly CommunicatorService _communicator; diff --git a/Penumbra/Collections/Cache/CollectionCacheManager.cs b/Penumbra/Collections/Cache/CollectionCacheManager.cs index 02c9c8a9..44c12856 100644 --- a/Penumbra/Collections/Cache/CollectionCacheManager.cs +++ b/Penumbra/Collections/Cache/CollectionCacheManager.cs @@ -1,5 +1,6 @@ using Dalamud.Plugin.Services; using OtterGui.Classes; +using OtterGui.Services; using Penumbra.Api; using Penumbra.Api.Enums; using Penumbra.Collections.Manager; @@ -17,7 +18,7 @@ using Penumbra.String.Classes; namespace Penumbra.Collections.Cache; -public class CollectionCacheManager : IDisposable +public class CollectionCacheManager : IDisposable, IService { private readonly FrameworkManager _framework; private readonly CommunicatorService _communicator; diff --git a/Penumbra/Collections/Manager/ActiveCollections.cs b/Penumbra/Collections/Manager/ActiveCollections.cs index 4e8ebe36..6d48f74b 100644 --- a/Penumbra/Collections/Manager/ActiveCollections.cs +++ b/Penumbra/Collections/Manager/ActiveCollections.cs @@ -3,6 +3,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui; using OtterGui.Classes; +using OtterGui.Services; using Penumbra.Communication; using Penumbra.GameData.Actors; using Penumbra.GameData.Enums; @@ -11,7 +12,7 @@ using Penumbra.UI; namespace Penumbra.Collections.Manager; -public class ActiveCollectionData +public class ActiveCollectionData : IService { public ModCollection Current { get; internal set; } = ModCollection.Empty; public ModCollection Default { get; internal set; } = ModCollection.Empty; @@ -20,7 +21,7 @@ public class ActiveCollectionData public readonly ModCollection?[] SpecialCollections = new ModCollection?[Enum.GetValues().Length - 3]; } -public class ActiveCollections : ISavable, IDisposable +public class ActiveCollections : ISavable, IDisposable, IService { public const int Version = 2; diff --git a/Penumbra/Collections/Manager/CollectionEditor.cs b/Penumbra/Collections/Manager/CollectionEditor.cs index 0243de1e..caff2c86 100644 --- a/Penumbra/Collections/Manager/CollectionEditor.cs +++ b/Penumbra/Collections/Manager/CollectionEditor.cs @@ -1,4 +1,5 @@ using OtterGui; +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Mods; using Penumbra.Mods.Manager; @@ -7,7 +8,7 @@ using Penumbra.Services; namespace Penumbra.Collections.Manager; -public class CollectionEditor(SaveService saveService, CommunicatorService communicator, ModStorage modStorage) +public class CollectionEditor(SaveService saveService, CommunicatorService communicator, ModStorage modStorage) : IService { /// Enable or disable the mod inheritance of mod idx. public bool SetModInheritance(ModCollection collection, Mod mod, bool inherit) diff --git a/Penumbra/Collections/Manager/CollectionManager.cs b/Penumbra/Collections/Manager/CollectionManager.cs index e95617b1..85f5b957 100644 --- a/Penumbra/Collections/Manager/CollectionManager.cs +++ b/Penumbra/Collections/Manager/CollectionManager.cs @@ -1,3 +1,4 @@ +using OtterGui.Services; using Penumbra.Collections.Cache; namespace Penumbra.Collections.Manager; @@ -8,7 +9,7 @@ public class CollectionManager( InheritanceManager inheritances, CollectionCacheManager caches, TempCollectionManager temp, - CollectionEditor editor) + CollectionEditor editor) : IService { public readonly CollectionStorage Storage = storage; public readonly ActiveCollections Active = active; diff --git a/Penumbra/Collections/Manager/CollectionStorage.cs b/Penumbra/Collections/Manager/CollectionStorage.cs index 67de3a03..cd680d36 100644 --- a/Penumbra/Collections/Manager/CollectionStorage.cs +++ b/Penumbra/Collections/Manager/CollectionStorage.cs @@ -1,7 +1,7 @@ -using System; using Dalamud.Interface.Internal.Notifications; using OtterGui; using OtterGui.Classes; +using OtterGui.Services; using Penumbra.Communication; using Penumbra.Mods; using Penumbra.Mods.Editor; @@ -11,7 +11,6 @@ using Penumbra.Mods.Manager.OptionEditor; using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; -using Penumbra.UI.CollectionTab; namespace Penumbra.Collections.Manager; @@ -24,7 +23,7 @@ public readonly record struct LocalCollectionId(int Id) : IAdditionOperators new(left.Id + right); } -public class CollectionStorage : IReadOnlyList, IDisposable +public class CollectionStorage : IReadOnlyList, IDisposable, IService { private readonly CommunicatorService _communicator; private readonly SaveService _saveService; diff --git a/Penumbra/Collections/Manager/InheritanceManager.cs b/Penumbra/Collections/Manager/InheritanceManager.cs index 6003b5f9..f3482cdf 100644 --- a/Penumbra/Collections/Manager/InheritanceManager.cs +++ b/Penumbra/Collections/Manager/InheritanceManager.cs @@ -2,11 +2,10 @@ using Dalamud.Interface.Internal.Notifications; using OtterGui; using OtterGui.Classes; using OtterGui.Filesystem; +using OtterGui.Services; using Penumbra.Communication; using Penumbra.Mods.Manager; using Penumbra.Services; -using Penumbra.UI.CollectionTab; -using Penumbra.Util; namespace Penumbra.Collections.Manager; @@ -15,7 +14,7 @@ namespace Penumbra.Collections.Manager; /// This is transitive, so a collection A inheriting from B also inherits from everything B inherits. /// Circular dependencies are resolved by distinctness. /// -public class InheritanceManager : IDisposable +public class InheritanceManager : IDisposable, IService { public enum ValidInheritance { @@ -144,7 +143,8 @@ public class InheritanceManager : IDisposable continue; changes = true; - Penumbra.Messager.NotificationMessage($"{collection.Name} can not inherit from {subCollection.Name}, removed.", NotificationType.Warning); + Penumbra.Messager.NotificationMessage($"{collection.Name} can not inherit from {subCollection.Name}, removed.", + NotificationType.Warning); } else if (_storage.ByName(subCollectionName, out subCollection)) { @@ -153,12 +153,14 @@ public class InheritanceManager : IDisposable if (AddInheritance(collection, subCollection, false)) continue; - Penumbra.Messager.NotificationMessage($"{collection.Name} can not inherit from {subCollection.Name}, removed.", NotificationType.Warning); + Penumbra.Messager.NotificationMessage($"{collection.Name} can not inherit from {subCollection.Name}, removed.", + NotificationType.Warning); } else { Penumbra.Messager.NotificationMessage( - $"Inherited collection {subCollectionName} for {collection.AnonymizedName} does not exist, it was removed.", NotificationType.Warning); + $"Inherited collection {subCollectionName} for {collection.AnonymizedName} does not exist, it was removed.", + NotificationType.Warning); changes = true; } } diff --git a/Penumbra/Collections/Manager/TempCollectionManager.cs b/Penumbra/Collections/Manager/TempCollectionManager.cs index ce438a6b..5c893232 100644 --- a/Penumbra/Collections/Manager/TempCollectionManager.cs +++ b/Penumbra/Collections/Manager/TempCollectionManager.cs @@ -1,4 +1,5 @@ using OtterGui; +using OtterGui.Services; using Penumbra.Api; using Penumbra.Communication; using Penumbra.GameData.Actors; @@ -8,7 +9,7 @@ using Penumbra.String; namespace Penumbra.Collections.Manager; -public class TempCollectionManager : IDisposable +public class TempCollectionManager : IDisposable, IService { public int GlobalChangeCounter { get; private set; } public readonly IndividualCollections Collections; diff --git a/Penumbra/CommandHandler.cs b/Penumbra/CommandHandler.cs index 4e1d6453..484dd954 100644 --- a/Penumbra/CommandHandler.cs +++ b/Penumbra/CommandHandler.cs @@ -3,6 +3,7 @@ using Dalamud.Game.Text.SeStringHandling; using Dalamud.Plugin.Services; using ImGuiNET; using OtterGui.Classes; +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.Collections.Manager; @@ -10,12 +11,11 @@ using Penumbra.GameData.Actors; using Penumbra.Interop.Services; using Penumbra.Mods; using Penumbra.Mods.Manager; -using Penumbra.Services; using Penumbra.UI; namespace Penumbra; -public class CommandHandler : IDisposable +public class CommandHandler : IDisposable, IApiService { private const string CommandName = "/penumbra"; diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index 02286cc7..f6100b62 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -4,6 +4,7 @@ using Newtonsoft.Json; using OtterGui; using OtterGui.Classes; using OtterGui.Filesystem; +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Import.Structs; using Penumbra.Interop.Services; @@ -18,7 +19,7 @@ using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs; namespace Penumbra; [Serializable] -public class Configuration : IPluginConfiguration, ISavable +public class Configuration : IPluginConfiguration, ISavable, IService { [JsonIgnore] private readonly SaveService _saveService; diff --git a/Penumbra/EphemeralConfig.cs b/Penumbra/EphemeralConfig.cs index 0a542d04..52e276c7 100644 --- a/Penumbra/EphemeralConfig.cs +++ b/Penumbra/EphemeralConfig.cs @@ -1,6 +1,7 @@ using Dalamud.Interface.Internal.Notifications; using Newtonsoft.Json; using OtterGui.Classes; +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Communication; using Penumbra.Enums; @@ -14,7 +15,7 @@ using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs; namespace Penumbra; -public class EphemeralConfig : ISavable, IDisposable +public class EphemeralConfig : ISavable, IDisposable, IService { [JsonIgnore] private readonly SaveService _saveService; diff --git a/Penumbra/Import/Models/ModelManager.cs b/Penumbra/Import/Models/ModelManager.cs index 9fa77784..01396cfb 100644 --- a/Penumbra/Import/Models/ModelManager.cs +++ b/Penumbra/Import/Models/ModelManager.cs @@ -1,6 +1,7 @@ using Dalamud.Plugin.Services; using Lumina.Data.Parsing; using OtterGui; +using OtterGui.Services; using OtterGui.Tasks; using Penumbra.Collections.Manager; using Penumbra.GameData; @@ -21,7 +22,8 @@ namespace Penumbra.Import.Models; using Schema2 = SharpGLTF.Schema2; using LuminaMaterial = Lumina.Models.Materials.Material; -public sealed class ModelManager(IFramework framework, ActiveCollections collections, GamePathParser parser) : SingleTaskQueue, IDisposable +public sealed class ModelManager(IFramework framework, ActiveCollections collections, GamePathParser parser) + : SingleTaskQueue, IDisposable, IService { private readonly IFramework _framework = framework; diff --git a/Penumbra/Import/Textures/TextureManager.cs b/Penumbra/Import/Textures/TextureManager.cs index 976bc179..4aa64209 100644 --- a/Penumbra/Import/Textures/TextureManager.cs +++ b/Penumbra/Import/Textures/TextureManager.cs @@ -3,6 +3,7 @@ using Dalamud.Interface.Internal; using Dalamud.Plugin.Services; using Lumina.Data.Files; using OtterGui.Log; +using OtterGui.Services; using OtterGui.Tasks; using OtterTex; using SixLabors.ImageSharp; @@ -12,22 +13,14 @@ using Image = SixLabors.ImageSharp.Image; namespace Penumbra.Import.Textures; -public sealed class TextureManager : SingleTaskQueue, IDisposable +public sealed class TextureManager(UiBuilder uiBuilder, IDataManager gameData, Logger logger) + : SingleTaskQueue, IDisposable, IService { - private readonly Logger _logger; - private readonly UiBuilder _uiBuilder; - private readonly IDataManager _gameData; + private readonly Logger _logger = logger; - private readonly ConcurrentDictionary _tasks = new(); + private readonly ConcurrentDictionary _tasks = new(); private bool _disposed; - public TextureManager(UiBuilder uiBuilder, IDataManager gameData, Logger logger) - { - _uiBuilder = uiBuilder; - _gameData = gameData; - _logger = logger; - } - public IReadOnlyDictionary Tasks => _tasks; @@ -64,7 +57,8 @@ public sealed class TextureManager : SingleTaskQueue, IDisposable { var token = new CancellationTokenSource(); var task = Enqueue(a, token.Token); - task.ContinueWith(_ => _tasks.TryRemove(a, out var unused), CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.Default); + task.ContinueWith(_ => _tasks.TryRemove(a, out var unused), CancellationToken.None, TaskContinuationOptions.None, + TaskScheduler.Default); return (task, token); }).Item1; } @@ -217,7 +211,7 @@ public sealed class TextureManager : SingleTaskQueue, IDisposable /// Load a texture wrap for a given image. public IDalamudTextureWrap LoadTextureWrap(byte[] rgba, int width, int height) - => _uiBuilder.LoadImageRaw(rgba, width, height, 4); + => uiBuilder.LoadImageRaw(rgba, width, height, 4); /// Load any supported file from game data or drive depending on extension and if the path is rooted. public (BaseImage, TextureType) Load(string path) @@ -326,7 +320,7 @@ public sealed class TextureManager : SingleTaskQueue, IDisposable } public bool GameFileExists(string path) - => _gameData.FileExists(path); + => gameData.FileExists(path); /// Add up to 13 mip maps to the input if mip maps is true, otherwise return input. public static ScratchImage AddMipMaps(ScratchImage input, bool mipMaps) @@ -382,7 +376,7 @@ public sealed class TextureManager : SingleTaskQueue, IDisposable if (Path.IsPathRooted(path)) return File.OpenRead(path); - var file = _gameData.GetFile(path); + var file = gameData.GetFile(path); return file != null ? new MemoryStream(file.Data) : throw new Exception($"Unable to obtain \"{path}\" from game files."); } diff --git a/Penumbra/Interop/PathResolving/CutsceneService.cs b/Penumbra/Interop/PathResolving/CutsceneService.cs index 93fee11e..feb27341 100644 --- a/Penumbra/Interop/PathResolving/CutsceneService.cs +++ b/Penumbra/Interop/PathResolving/CutsceneService.cs @@ -1,6 +1,5 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Character; -using FFXIVClientStructs.FFXIV.Client.Game.Object; using OtterGui.Services; using Penumbra.GameData.Enums; using Penumbra.GameData.Interop; @@ -9,7 +8,7 @@ using Penumbra.String; namespace Penumbra.Interop.PathResolving; -public sealed class CutsceneService : IService, IDisposable +public sealed class CutsceneService : IRequiredService, IDisposable { public const int CutsceneStartIdx = (int)ScreenActor.CutsceneStart; public const int CutsceneEndIdx = (int)ScreenActor.CutsceneEnd; diff --git a/Penumbra/Interop/PathResolving/IdentifiedCollectionCache.cs b/Penumbra/Interop/PathResolving/IdentifiedCollectionCache.cs index 32090f7c..eeff7eee 100644 --- a/Penumbra/Interop/PathResolving/IdentifiedCollectionCache.cs +++ b/Penumbra/Interop/PathResolving/IdentifiedCollectionCache.cs @@ -1,6 +1,7 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Character; using FFXIVClientStructs.FFXIV.Client.Game.Object; +using OtterGui.Services; using Penumbra.Collections; using Penumbra.Collections.Manager; using Penumbra.Communication; @@ -10,7 +11,8 @@ using Penumbra.Services; namespace Penumbra.Interop.PathResolving; -public unsafe class IdentifiedCollectionCache : IDisposable, IEnumerable<(nint Address, ActorIdentifier Identifier, ModCollection Collection)> +public unsafe class IdentifiedCollectionCache : IDisposable, IEnumerable<(nint Address, ActorIdentifier Identifier, ModCollection Collection)>, + IService { private readonly CommunicatorService _communicator; private readonly CharacterDestructor _characterDestructor; diff --git a/Penumbra/Interop/PathResolving/MetaState.cs b/Penumbra/Interop/PathResolving/MetaState.cs index 7f820b4e..f7dcfc07 100644 --- a/Penumbra/Interop/PathResolving/MetaState.cs +++ b/Penumbra/Interop/PathResolving/MetaState.cs @@ -1,5 +1,6 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Classes; +using OtterGui.Services; using Penumbra.Collections; using Penumbra.Api.Enums; using Penumbra.GameData.Structs; @@ -34,7 +35,7 @@ namespace Penumbra.Interop.PathResolving; // ChangeCustomize and RspSetupCharacter, which is hooked here, as well as Character.CalculateHeight. // GMP Entries seem to be only used by "48 8B ?? 53 55 57 48 83 ?? ?? 48 8B", which is SetupVisor. -public sealed unsafe class MetaState : IDisposable +public sealed unsafe class MetaState : IDisposable, IService { private readonly Configuration _config; private readonly CommunicatorService _communicator; diff --git a/Penumbra/Interop/PathResolving/PathResolver.cs b/Penumbra/Interop/PathResolving/PathResolver.cs index f31b3323..cc3e0e9b 100644 --- a/Penumbra/Interop/PathResolving/PathResolver.cs +++ b/Penumbra/Interop/PathResolving/PathResolver.cs @@ -1,4 +1,5 @@ using FFXIVClientStructs.FFXIV.Client.System.Resource; +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.Collections.Manager; @@ -9,7 +10,7 @@ using Penumbra.Util; namespace Penumbra.Interop.PathResolving; -public class PathResolver : IDisposable +public class PathResolver : IDisposable, IService { private readonly PerformanceTracker _performance; private readonly Configuration _config; diff --git a/Penumbra/Interop/PathResolving/PathState.cs b/Penumbra/Interop/PathResolving/PathState.cs index f4218e9c..bf9d1e25 100644 --- a/Penumbra/Interop/PathResolving/PathState.cs +++ b/Penumbra/Interop/PathResolving/PathState.cs @@ -1,3 +1,4 @@ +using OtterGui.Services; using Penumbra.Collections; using Penumbra.Interop.Services; using Penumbra.String; @@ -5,7 +6,7 @@ using Penumbra.String; namespace Penumbra.Interop.PathResolving; public sealed class PathState(CollectionResolver collectionResolver, MetaState metaState, CharacterUtility characterUtility) - : IDisposable + : IDisposable, IService { public readonly CollectionResolver CollectionResolver = collectionResolver; public readonly MetaState MetaState = metaState; diff --git a/Penumbra/Interop/PathResolving/SubfileHelper.cs b/Penumbra/Interop/PathResolving/SubfileHelper.cs index 3cefd98d..44a152f0 100644 --- a/Penumbra/Interop/PathResolving/SubfileHelper.cs +++ b/Penumbra/Interop/PathResolving/SubfileHelper.cs @@ -1,9 +1,9 @@ +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.Interop.Hooks.Resources; using Penumbra.Interop.ResourceLoading; using Penumbra.Interop.Structs; -using Penumbra.String; using Penumbra.String.Classes; namespace Penumbra.Interop.PathResolving; @@ -13,7 +13,7 @@ namespace Penumbra.Interop.PathResolving; /// Those are loaded synchronously. /// Thus, we need to ensure the correct files are loaded when a material is loaded. /// -public sealed unsafe class SubfileHelper : IDisposable, IReadOnlyCollection> +public sealed unsafe class SubfileHelper : IDisposable, IReadOnlyCollection>, IService { private readonly GameState _gameState; private readonly ResourceLoader _loader; diff --git a/Penumbra/Interop/ResourceLoading/FileReadService.cs b/Penumbra/Interop/ResourceLoading/FileReadService.cs index 64442771..f1d7fe24 100644 --- a/Penumbra/Interop/ResourceLoading/FileReadService.cs +++ b/Penumbra/Interop/ResourceLoading/FileReadService.cs @@ -1,13 +1,14 @@ using Dalamud.Hooking; using Dalamud.Plugin.Services; using Dalamud.Utility.Signatures; +using OtterGui.Services; using Penumbra.GameData; using Penumbra.Interop.Structs; using Penumbra.Util; namespace Penumbra.Interop.ResourceLoading; -public unsafe class FileReadService : IDisposable +public unsafe class FileReadService : IDisposable, IRequiredService { public FileReadService(PerformanceTracker performance, ResourceManagerService resourceManager, IGameInteropProvider interop) { diff --git a/Penumbra/Interop/ResourceLoading/ResourceLoader.cs b/Penumbra/Interop/ResourceLoading/ResourceLoader.cs index 7b49beab..4a423993 100644 --- a/Penumbra/Interop/ResourceLoading/ResourceLoader.cs +++ b/Penumbra/Interop/ResourceLoading/ResourceLoader.cs @@ -1,4 +1,5 @@ using FFXIVClientStructs.FFXIV.Client.System.Resource; +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.Interop.PathResolving; @@ -10,7 +11,7 @@ using FileMode = Penumbra.Interop.Structs.FileMode; namespace Penumbra.Interop.ResourceLoading; -public unsafe class ResourceLoader : IDisposable +public unsafe class ResourceLoader : IDisposable, IService { private readonly ResourceService _resources; private readonly FileReadService _fileReadService; @@ -212,7 +213,7 @@ public unsafe class ResourceLoader : IDisposable /// /// Catch weird errors with invalid decrements of the reference count. /// - private void DecRefProtection(ResourceHandle* handle, ref byte? returnValue) + private static void DecRefProtection(ResourceHandle* handle, ref byte? returnValue) { if (handle->RefCount != 0) return; diff --git a/Penumbra/Interop/ResourceLoading/ResourceManagerService.cs b/Penumbra/Interop/ResourceLoading/ResourceManagerService.cs index a087a659..c885c317 100644 --- a/Penumbra/Interop/ResourceLoading/ResourceManagerService.cs +++ b/Penumbra/Interop/ResourceLoading/ResourceManagerService.cs @@ -4,12 +4,13 @@ using FFXIVClientStructs.FFXIV.Client.System.Resource; using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; using FFXIVClientStructs.Interop; using FFXIVClientStructs.STD; +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.GameData; namespace Penumbra.Interop.ResourceLoading; -public unsafe class ResourceManagerService +public unsafe class ResourceManagerService : IRequiredService { public ResourceManagerService(IGameInteropProvider interop) => interop.InitializeFromAttributes(this); diff --git a/Penumbra/Interop/ResourceLoading/ResourceService.cs b/Penumbra/Interop/ResourceLoading/ResourceService.cs index e3338e6c..54c86777 100644 --- a/Penumbra/Interop/ResourceLoading/ResourceService.cs +++ b/Penumbra/Interop/ResourceLoading/ResourceService.cs @@ -2,6 +2,7 @@ using Dalamud.Hooking; using Dalamud.Plugin.Services; using Dalamud.Utility.Signatures; using FFXIVClientStructs.FFXIV.Client.System.Resource; +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.GameData; using Penumbra.Interop.SafeHandles; @@ -13,7 +14,7 @@ using CSResourceHandle = FFXIVClientStructs.FFXIV.Client.System.Resource.Handle. namespace Penumbra.Interop.ResourceLoading; -public unsafe class ResourceService : IDisposable +public unsafe class ResourceService : IDisposable, IRequiredService { private readonly PerformanceTracker _performance; private readonly ResourceManagerService _resourceManager; diff --git a/Penumbra/Interop/ResourceLoading/TexMdlService.cs b/Penumbra/Interop/ResourceLoading/TexMdlService.cs index b9279f54..e617673e 100644 --- a/Penumbra/Interop/ResourceLoading/TexMdlService.cs +++ b/Penumbra/Interop/ResourceLoading/TexMdlService.cs @@ -2,13 +2,14 @@ using Dalamud.Hooking; using Dalamud.Plugin.Services; using Dalamud.Utility.Signatures; using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.GameData; using Penumbra.String.Classes; namespace Penumbra.Interop.ResourceLoading; -public unsafe class TexMdlService : IDisposable +public unsafe class TexMdlService : IDisposable, IRequiredService { /// Custom ulong flag to signal our files as opposed to SE files. public static readonly IntPtr CustomFileFlag = new(0xDEADBEEF); diff --git a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs index 5a190e52..e26c1436 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs @@ -1,5 +1,6 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Object; +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.GameData.Actors; using Penumbra.GameData.Data; @@ -17,7 +18,7 @@ public class ResourceTreeFactory( ObjectIdentification identifier, Configuration config, ActorManager actors, - PathState pathState) + PathState pathState) : IService { private TreeBuildCache CreateTreeBuildCache() => new(objects, gameData, actors); diff --git a/Penumbra/Interop/Services/CharacterUtility.cs b/Penumbra/Interop/Services/CharacterUtility.cs index 0877d221..532dc823 100644 --- a/Penumbra/Interop/Services/CharacterUtility.cs +++ b/Penumbra/Interop/Services/CharacterUtility.cs @@ -1,11 +1,12 @@ using Dalamud.Plugin.Services; using Dalamud.Utility.Signatures; +using OtterGui.Services; using Penumbra.GameData; using Penumbra.Interop.Structs; namespace Penumbra.Interop.Services; -public unsafe class CharacterUtility : IDisposable +public unsafe class CharacterUtility : IDisposable, IRequiredService { public record struct InternalIndex(int Value); diff --git a/Penumbra/Interop/Services/FontReloader.cs b/Penumbra/Interop/Services/FontReloader.cs index 2f4a3cfd..259fdd10 100644 --- a/Penumbra/Interop/Services/FontReloader.cs +++ b/Penumbra/Interop/Services/FontReloader.cs @@ -1,6 +1,7 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.System.Framework; using FFXIVClientStructs.FFXIV.Component.GUI; +using OtterGui.Services; using Penumbra.GameData; namespace Penumbra.Interop.Services; @@ -9,7 +10,7 @@ namespace Penumbra.Interop.Services; /// Handle font reloading via game functions. /// May cause a interface flicker while reloading. /// -public unsafe class FontReloader +public unsafe class FontReloader : IService { public bool Valid => _reloadFontsFunc != null; diff --git a/Penumbra/Interop/Services/ModelRenderer.cs b/Penumbra/Interop/Services/ModelRenderer.cs index 7df83cf7..b268b395 100644 --- a/Penumbra/Interop/Services/ModelRenderer.cs +++ b/Penumbra/Interop/Services/ModelRenderer.cs @@ -1,10 +1,11 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Graphics.Render; using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; +using OtterGui.Services; namespace Penumbra.Interop.Services; -public unsafe class ModelRenderer : IDisposable +public unsafe class ModelRenderer : IDisposable, IRequiredService { public bool Ready { get; private set; } @@ -37,14 +38,14 @@ public unsafe class ModelRenderer : IDisposable if (DefaultCharacterGlassShaderPackage == null) { - DefaultCharacterGlassShaderPackage = *CharacterGlassShaderPackage; - anyMissing |= DefaultCharacterGlassShaderPackage == null; + DefaultCharacterGlassShaderPackage = *CharacterGlassShaderPackage; + anyMissing |= DefaultCharacterGlassShaderPackage == null; } if (anyMissing) return; - Ready = true; + Ready = true; _framework.Update -= LoadDefaultResources; } diff --git a/Penumbra/Interop/Services/RedrawService.cs b/Penumbra/Interop/Services/RedrawService.cs index 21ecfd4f..61d7b90c 100644 --- a/Penumbra/Interop/Services/RedrawService.cs +++ b/Penumbra/Interop/Services/RedrawService.cs @@ -6,6 +6,7 @@ using Dalamud.Game.ClientState.Objects.Types; using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Housing; using FFXIVClientStructs.Interop; +using OtterGui.Services; using Penumbra.Api; using Penumbra.Api.Enums; using Penumbra.Communication; @@ -20,7 +21,7 @@ using Character = FFXIVClientStructs.FFXIV.Client.Game.Character.Character; namespace Penumbra.Interop.Services; -public unsafe partial class RedrawService +public unsafe partial class RedrawService : IService { public const int GPosePlayerIdx = 201; public const int GPoseSlots = 42; @@ -171,7 +172,8 @@ public sealed unsafe partial class RedrawService : IDisposable if (gPose) DisableDraw(actor!); - if (actor is PlayerCharacter && _objects.GetDalamudObject(tableIndex + 1) is { ObjectKind: ObjectKind.MountType or ObjectKind.Ornament } mountOrOrnament) + if (actor is PlayerCharacter + && _objects.GetDalamudObject(tableIndex + 1) is { ObjectKind: ObjectKind.MountType or ObjectKind.Ornament } mountOrOrnament) { *ActorDrawState(mountOrOrnament) |= DrawState.Invisibility; if (gPose) @@ -190,7 +192,8 @@ public sealed unsafe partial class RedrawService : IDisposable if (gPose) EnableDraw(actor!); - if (actor is PlayerCharacter && _objects.GetDalamudObject(tableIndex + 1) is { ObjectKind: ObjectKind.MountType or ObjectKind.Ornament } mountOrOrnament) + if (actor is PlayerCharacter + && _objects.GetDalamudObject(tableIndex + 1) is { ObjectKind: ObjectKind.MountType or ObjectKind.Ornament } mountOrOrnament) { *ActorDrawState(mountOrOrnament) &= ~DrawState.Invisibility; if (gPose) @@ -380,7 +383,7 @@ public sealed unsafe partial class RedrawService : IDisposable if (!ret && lowerName.Length > 1 && lowerName[0] == '#' && ushort.TryParse(lowerName[1..], out var objectIndex)) { ret = true; - actor = _objects.GetDalamudObject((int) objectIndex); + actor = _objects.GetDalamudObject((int)objectIndex); } return ret; diff --git a/Penumbra/Interop/Services/ResidentResourceManager.cs b/Penumbra/Interop/Services/ResidentResourceManager.cs index 72697185..4f430aa1 100644 --- a/Penumbra/Interop/Services/ResidentResourceManager.cs +++ b/Penumbra/Interop/Services/ResidentResourceManager.cs @@ -1,10 +1,11 @@ -using Dalamud.Plugin.Services; +using Dalamud.Plugin.Services; using Dalamud.Utility.Signatures; +using OtterGui.Services; using Penumbra.GameData; namespace Penumbra.Interop.Services; -public unsafe class ResidentResourceManager +public unsafe class ResidentResourceManager : IService { // A static pointer to the resident resource manager address. [Signature(Sigs.ResidentResourceManager, ScanType = ScanType.StaticAddress)] diff --git a/Penumbra/Meta/MetaFileManager.cs b/Penumbra/Meta/MetaFileManager.cs index 81c0fa3e..3755afa2 100644 --- a/Penumbra/Meta/MetaFileManager.cs +++ b/Penumbra/Meta/MetaFileManager.cs @@ -1,5 +1,6 @@ using Dalamud.Plugin.Services; using OtterGui.Compression; +using OtterGui.Services; using Penumbra.Collections; using Penumbra.Collections.Manager; using Penumbra.GameData.Data; @@ -13,7 +14,7 @@ using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManage namespace Penumbra.Meta; -public unsafe class MetaFileManager +public class MetaFileManager : IService { internal readonly Configuration Config; internal readonly CharacterUtility CharacterUtility; diff --git a/Penumbra/Mods/Editor/DuplicateManager.cs b/Penumbra/Mods/Editor/DuplicateManager.cs index 47aa18dc..bcecf264 100644 --- a/Penumbra/Mods/Editor/DuplicateManager.cs +++ b/Penumbra/Mods/Editor/DuplicateManager.cs @@ -1,4 +1,5 @@ using OtterGui.Classes; +using OtterGui.Services; using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; using Penumbra.Mods.SubMods; @@ -7,7 +8,7 @@ using Penumbra.String.Classes; namespace Penumbra.Mods.Editor; -public class DuplicateManager(ModManager modManager, SaveService saveService, Configuration config) +public class DuplicateManager(ModManager modManager, SaveService saveService, Configuration config) : IService { private readonly SHA256 _hasher = SHA256.Create(); private readonly List<(FullPath[] Paths, long Size, byte[] Hash)> _duplicates = []; diff --git a/Penumbra/Mods/Editor/MdlMaterialEditor.cs b/Penumbra/Mods/Editor/MdlMaterialEditor.cs index 738e606e..2a23ffad 100644 --- a/Penumbra/Mods/Editor/MdlMaterialEditor.cs +++ b/Penumbra/Mods/Editor/MdlMaterialEditor.cs @@ -1,11 +1,12 @@ using OtterGui; using OtterGui.Compression; +using OtterGui.Services; using Penumbra.GameData.Enums; using Penumbra.GameData.Files; namespace Penumbra.Mods.Editor; -public partial class MdlMaterialEditor(ModFileCollection files) +public partial class MdlMaterialEditor(ModFileCollection files) : IService { [GeneratedRegex(@"/mt_c(?'RaceCode'\d{4})b0001_(?'Suffix'.*?)\.mtrl", RegexOptions.ExplicitCapture | RegexOptions.NonBacktracking)] private static partial Regex MaterialRegex(); diff --git a/Penumbra/Mods/Editor/ModEditor.cs b/Penumbra/Mods/Editor/ModEditor.cs index 37524da1..cacb7f88 100644 --- a/Penumbra/Mods/Editor/ModEditor.cs +++ b/Penumbra/Mods/Editor/ModEditor.cs @@ -1,5 +1,5 @@ -using OtterGui; using OtterGui.Compression; +using OtterGui.Services; using Penumbra.Mods.Groups; using Penumbra.Mods.SubMods; @@ -14,7 +14,7 @@ public class ModEditor( ModSwapEditor swapEditor, MdlMaterialEditor mdlMaterialEditor, FileCompactor compactor) - : IDisposable + : IDisposable, IService { public readonly ModNormalizer ModNormalizer = modNormalizer; public readonly ModMetaEditor MetaEditor = metaEditor; diff --git a/Penumbra/Mods/Editor/ModFileCollection.cs b/Penumbra/Mods/Editor/ModFileCollection.cs index 551d04cf..241f5b3b 100644 --- a/Penumbra/Mods/Editor/ModFileCollection.cs +++ b/Penumbra/Mods/Editor/ModFileCollection.cs @@ -1,10 +1,11 @@ using OtterGui; +using OtterGui.Services; using Penumbra.Mods.SubMods; using Penumbra.String.Classes; namespace Penumbra.Mods.Editor; -public class ModFileCollection : IDisposable +public class ModFileCollection : IDisposable, IService { private readonly List _available = []; private readonly List _mtrl = []; diff --git a/Penumbra/Mods/Editor/ModFileEditor.cs b/Penumbra/Mods/Editor/ModFileEditor.cs index e2c0b726..55e0e94e 100644 --- a/Penumbra/Mods/Editor/ModFileEditor.cs +++ b/Penumbra/Mods/Editor/ModFileEditor.cs @@ -1,3 +1,4 @@ +using OtterGui.Services; using Penumbra.Mods.Manager; using Penumbra.Mods.SubMods; using Penumbra.Services; @@ -5,7 +6,7 @@ using Penumbra.String.Classes; namespace Penumbra.Mods.Editor; -public class ModFileEditor(ModFileCollection files, ModManager modManager, CommunicatorService communicator) +public class ModFileEditor(ModFileCollection files, ModManager modManager, CommunicatorService communicator) : IService { public bool Changes { get; private set; } diff --git a/Penumbra/Mods/Editor/ModMerger.cs b/Penumbra/Mods/Editor/ModMerger.cs index 2df76838..9d31664b 100644 --- a/Penumbra/Mods/Editor/ModMerger.cs +++ b/Penumbra/Mods/Editor/ModMerger.cs @@ -2,6 +2,7 @@ using Dalamud.Interface.Internal.Notifications; using Dalamud.Utility; using OtterGui; using OtterGui.Classes; +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Communication; using Penumbra.Mods.Manager; @@ -13,7 +14,7 @@ using Penumbra.UI.ModsTab; namespace Penumbra.Mods.Editor; -public class ModMerger : IDisposable +public class ModMerger : IDisposable, IService { private readonly Configuration _config; private readonly CommunicatorService _communicator; diff --git a/Penumbra/Mods/Editor/ModNormalizer.cs b/Penumbra/Mods/Editor/ModNormalizer.cs index 58e4fc08..c6bc4939 100644 --- a/Penumbra/Mods/Editor/ModNormalizer.cs +++ b/Penumbra/Mods/Editor/ModNormalizer.cs @@ -1,15 +1,15 @@ using Dalamud.Interface.Internal.Notifications; using OtterGui; using OtterGui.Classes; +using OtterGui.Services; using OtterGui.Tasks; -using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; using Penumbra.Mods.SubMods; using Penumbra.String.Classes; namespace Penumbra.Mods.Editor; -public class ModNormalizer(ModManager _modManager, Configuration _config) +public class ModNormalizer(ModManager _modManager, Configuration _config) : IService { private readonly List>> _redirections = []; diff --git a/Penumbra/Mods/Editor/ModSwapEditor.cs b/Penumbra/Mods/Editor/ModSwapEditor.cs index 0250efae..1a8ff2eb 100644 --- a/Penumbra/Mods/Editor/ModSwapEditor.cs +++ b/Penumbra/Mods/Editor/ModSwapEditor.cs @@ -1,10 +1,10 @@ -using Penumbra.Mods; +using OtterGui.Services; using Penumbra.Mods.Manager; using Penumbra.Mods.SubMods; using Penumbra.String.Classes; using Penumbra.Util; -public class ModSwapEditor(ModManager modManager) +public class ModSwapEditor(ModManager modManager) : IService { private readonly Dictionary _swaps = []; diff --git a/Penumbra/Mods/Manager/ModCacheManager.cs b/Penumbra/Mods/Manager/ModCacheManager.cs index 8ab8cf33..38d98d7c 100644 --- a/Penumbra/Mods/Manager/ModCacheManager.cs +++ b/Penumbra/Mods/Manager/ModCacheManager.cs @@ -1,3 +1,4 @@ +using OtterGui.Services; using Penumbra.Communication; using Penumbra.GameData.Data; using Penumbra.Mods.Groups; @@ -8,7 +9,7 @@ using Penumbra.Util; namespace Penumbra.Mods.Manager; -public class ModCacheManager : IDisposable +public class ModCacheManager : IDisposable, IService { private readonly Configuration _config; private readonly CommunicatorService _communicator; diff --git a/Penumbra/Mods/Manager/ModDataEditor.cs b/Penumbra/Mods/Manager/ModDataEditor.cs index c7c7c2cc..4ab9deb1 100644 --- a/Penumbra/Mods/Manager/ModDataEditor.cs +++ b/Penumbra/Mods/Manager/ModDataEditor.cs @@ -1,6 +1,7 @@ using Dalamud.Utility; using Newtonsoft.Json.Linq; using OtterGui.Classes; +using OtterGui.Services; using Penumbra.Services; namespace Penumbra.Mods.Manager; @@ -23,7 +24,7 @@ public enum ModDataChangeType : ushort Note = 0x0800, } -public class ModDataEditor(SaveService saveService, CommunicatorService communicatorService) +public class ModDataEditor(SaveService saveService, CommunicatorService communicatorService) : IService { /// Create the file containing the meta information about a mod from scratch. public void CreateMeta(DirectoryInfo directory, string? name, string? author, string? description, string? version, @@ -49,7 +50,6 @@ public class ModDataEditor(SaveService saveService, CommunicatorService communic var save = true; if (File.Exists(dataFile)) - { try { var text = File.ReadAllText(dataFile); @@ -65,7 +65,6 @@ public class ModDataEditor(SaveService saveService, CommunicatorService communic { Penumbra.Log.Error($"Could not load local mod data:\n{e}"); } - } if (importDate == 0) importDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); diff --git a/Penumbra/Mods/Manager/ModExportManager.cs b/Penumbra/Mods/Manager/ModExportManager.cs index 676018be..38b9c0fd 100644 --- a/Penumbra/Mods/Manager/ModExportManager.cs +++ b/Penumbra/Mods/Manager/ModExportManager.cs @@ -1,10 +1,11 @@ +using OtterGui.Services; using Penumbra.Communication; using Penumbra.Mods.Editor; using Penumbra.Services; namespace Penumbra.Mods.Manager; -public class ModExportManager : IDisposable +public class ModExportManager : IDisposable, IService { private readonly Configuration _config; private readonly CommunicatorService _communicator; diff --git a/Penumbra/Mods/Manager/ModFileSystem.cs b/Penumbra/Mods/Manager/ModFileSystem.cs index c8a0a5db..e32fec0c 100644 --- a/Penumbra/Mods/Manager/ModFileSystem.cs +++ b/Penumbra/Mods/Manager/ModFileSystem.cs @@ -1,12 +1,11 @@ -using Newtonsoft.Json.Linq; -using OtterGui.Classes; using OtterGui.Filesystem; +using OtterGui.Services; using Penumbra.Communication; using Penumbra.Services; namespace Penumbra.Mods.Manager; -public sealed class ModFileSystem : FileSystem, IDisposable, ISavable +public sealed class ModFileSystem : FileSystem, IDisposable, ISavable, IService { private readonly ModManager _modManager; private readonly CommunicatorService _communicator; diff --git a/Penumbra/Mods/Manager/ModImportManager.cs b/Penumbra/Mods/Manager/ModImportManager.cs index c99b7d0e..ff39b021 100644 --- a/Penumbra/Mods/Manager/ModImportManager.cs +++ b/Penumbra/Mods/Manager/ModImportManager.cs @@ -1,11 +1,12 @@ using Dalamud.Interface.Internal.Notifications; using OtterGui.Classes; +using OtterGui.Services; using Penumbra.Import; using Penumbra.Mods.Editor; namespace Penumbra.Mods.Manager; -public class ModImportManager(ModManager modManager, Configuration config, ModEditor modEditor) : IDisposable +public class ModImportManager(ModManager modManager, Configuration config, ModEditor modEditor) : IDisposable, IService { private readonly ConcurrentQueue _modsToUnpack = new(); @@ -32,7 +33,8 @@ public class ModImportManager(ModManager modManager, Configuration config, ModEd if (File.Exists(s)) return true; - Penumbra.Messager.NotificationMessage($"Failed to import queued mod at {s}, the file does not exist.", NotificationType.Warning, false); + Penumbra.Messager.NotificationMessage($"Failed to import queued mod at {s}, the file does not exist.", NotificationType.Warning, + false); return false; }).Select(s => new FileInfo(s)).ToArray(); diff --git a/Penumbra/Mods/Manager/ModManager.cs b/Penumbra/Mods/Manager/ModManager.cs index 42082383..4b19ea4c 100644 --- a/Penumbra/Mods/Manager/ModManager.cs +++ b/Penumbra/Mods/Manager/ModManager.cs @@ -1,3 +1,4 @@ +using OtterGui.Services; using Penumbra.Communication; using Penumbra.Mods.Editor; using Penumbra.Mods.Manager.OptionEditor; @@ -27,7 +28,7 @@ public enum ModPathChangeType StartingReload, } -public sealed class ModManager : ModStorage, IDisposable +public sealed class ModManager : ModStorage, IDisposable, IService { private readonly Configuration _config; private readonly CommunicatorService _communicator; diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index ed4245c4..0e66367a 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -4,6 +4,7 @@ using Newtonsoft.Json.Linq; using OtterGui; using OtterGui.Classes; using OtterGui.Filesystem; +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.GameData.Data; using Penumbra.Import; @@ -23,7 +24,7 @@ public partial class ModCreator( Configuration config, ModDataEditor _dataEditor, MetaFileManager _metaFileManager, - GamePathParser _gamePathParser) + GamePathParser _gamePathParser) : IService { public readonly Configuration Config = config; diff --git a/Penumbra/Services/StaticServiceManager.cs b/Penumbra/Services/StaticServiceManager.cs index 35e36349..3279da96 100644 --- a/Penumbra/Services/StaticServiceManager.cs +++ b/Penumbra/Services/StaticServiceManager.cs @@ -5,36 +5,15 @@ using Dalamud.Plugin; using Dalamud.Plugin.Services; using Microsoft.Extensions.DependencyInjection; using OtterGui; -using OtterGui.Classes; using OtterGui.Log; using OtterGui.Services; -using Penumbra.Api; using Penumbra.Api.Api; -using Penumbra.Collections.Cache; -using Penumbra.Collections.Manager; using Penumbra.GameData.Actors; -using Penumbra.Import.Models; using Penumbra.GameData.Structs; -using Penumbra.Import.Textures; using Penumbra.Interop.PathResolving; -using Penumbra.Interop.ResourceLoading; -using Penumbra.Interop.ResourceTree; -using Penumbra.Interop.Services; using Penumbra.Meta; -using Penumbra.Mods; -using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; -using Penumbra.UI; -using Penumbra.UI.AdvancedWindow; -using Penumbra.UI.Classes; -using Penumbra.UI.ModsTab; -using Penumbra.UI.ResourceWatcher; -using Penumbra.UI.Tabs; -using Penumbra.UI.Tabs.Debug; using IPenumbraApi = Penumbra.Api.Api.IPenumbraApi; -using MdlMaterialEditor = Penumbra.Mods.Editor.MdlMaterialEditor; -using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManager; -using Penumbra.Api.IpcTester; namespace Penumbra.Services; @@ -45,19 +24,18 @@ public static class StaticServiceManager var services = new ServiceManager(log) .AddDalamudServices(pi) .AddExistingService(log) - .AddExistingService(penumbra) - .AddInterop() - .AddConfiguration() - .AddCollections() - .AddMods() - .AddResources() - .AddResolvers() - .AddInterface() - .AddModEditor() - .AddApi(); + .AddExistingService(penumbra); services.AddIServices(typeof(EquipItem).Assembly); services.AddIServices(typeof(Penumbra).Assembly); services.AddIServices(typeof(ImGuiUtil).Assembly); + services.AddSingleton(p => + { + var cutsceneService = p.GetRequiredService(); + return new CutsceneResolver(cutsceneService.GetParentIndex); + }) + .AddSingleton(p => p.GetRequiredService().ImcChecker) + .AddSingleton(s => (ModStorage)s.GetRequiredService()) + .AddSingleton(x => x.GetRequiredService()); services.CreateProvider(); return services; } @@ -83,118 +61,4 @@ public static class StaticServiceManager .AddDalamudService(pi) .AddDalamudService(pi) .AddDalamudService(pi); - - private static ServiceManager AddInterop(this ServiceManager services) - => services.AddSingleton() - .AddSingleton() - .AddSingleton(p => - { - var cutsceneService = p.GetRequiredService(); - return new CutsceneResolver(cutsceneService.GetParentIndex); - }) - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton(p => p.GetRequiredService().ImcChecker); - - private static ServiceManager AddConfiguration(this ServiceManager services) - => services.AddSingleton() - .AddSingleton() - .AddSingleton(); - - private static ServiceManager AddCollections(this ServiceManager services) - => services.AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton(); - - private static ServiceManager AddMods(this ServiceManager services) - => services.AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton(s => (ModStorage)s.GetRequiredService()); - - private static ServiceManager AddResources(this ServiceManager services) - => services.AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton(); - - private static ServiceManager AddResolvers(this ServiceManager services) - => services.AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton(); - - private static ServiceManager AddInterface(this ServiceManager services) - => services.AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton(p => new Diagnostics(p)); - - private static ServiceManager AddModEditor(this ServiceManager services) - => services.AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton(); - - private static ServiceManager AddApi(this ServiceManager services) - => services.AddSingleton(x => x.GetRequiredService()); } diff --git a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs index 62115dd6..652f928d 100644 --- a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs +++ b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs @@ -3,6 +3,7 @@ using ImGuiNET; using OtterGui; using OtterGui.Classes; using OtterGui.Raii; +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Api.Enums; using Penumbra.Collections; @@ -24,7 +25,7 @@ using Penumbra.UI.Classes; namespace Penumbra.UI.AdvancedWindow; -public class ItemSwapTab : IDisposable, ITab +public class ItemSwapTab : IDisposable, ITab, IUiService { private readonly Configuration _config; private readonly CommunicatorService _communicator; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index 9410b793..90fdc48e 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -7,6 +7,7 @@ using Dalamud.Plugin.Services; using ImGuiNET; using OtterGui; using OtterGui.Raii; +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Collections.Manager; using Penumbra.Communication; @@ -32,7 +33,7 @@ using MdlMaterialEditor = Penumbra.Mods.Editor.MdlMaterialEditor; namespace Penumbra.UI.AdvancedWindow; -public partial class ModEditWindow : Window, IDisposable +public partial class ModEditWindow : Window, IDisposable, IUiService { private const string WindowBaseLabel = "###SubModEdit"; diff --git a/Penumbra/UI/AdvancedWindow/ModMergeTab.cs b/Penumbra/UI/AdvancedWindow/ModMergeTab.cs index 5dad66b4..b5f0255c 100644 --- a/Penumbra/UI/AdvancedWindow/ModMergeTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModMergeTab.cs @@ -2,6 +2,7 @@ using Dalamud.Interface.Utility; using ImGuiNET; using OtterGui; using OtterGui.Raii; +using OtterGui.Services; using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; using Penumbra.Mods.SubMods; @@ -9,7 +10,7 @@ using Penumbra.UI.Classes; namespace Penumbra.UI.AdvancedWindow; -public class ModMergeTab(ModMerger modMerger) +public class ModMergeTab(ModMerger modMerger) : IUiService { private readonly ModCombo _modCombo = new(() => modMerger.ModsWithoutCurrent.ToList()); private string _newModName = string.Empty; @@ -183,7 +184,7 @@ public class ModMergeTab(ModMerger modMerger) else { ImGuiUtil.DrawTableColumn(option.GetName()); - + ImGui.TableNextColumn(); ImGui.Selectable(group.Name, false); if (ImGui.BeginPopupContextItem("##groupContext")) diff --git a/Penumbra/UI/ChangedItemDrawer.cs b/Penumbra/UI/ChangedItemDrawer.cs index 29a1f291..0afeeeeb 100644 --- a/Penumbra/UI/ChangedItemDrawer.cs +++ b/Penumbra/UI/ChangedItemDrawer.cs @@ -10,6 +10,7 @@ using Lumina.Excel.GeneratedSheets; using OtterGui; using OtterGui.Classes; using OtterGui.Raii; +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; @@ -19,7 +20,7 @@ using ApiChangedItemIcon = Penumbra.Api.Enums.ChangedItemIcon; namespace Penumbra.UI; -public class ChangedItemDrawer : IDisposable +public class ChangedItemDrawer : IDisposable, IUiService { [Flags] public enum ChangedItemIcon : uint @@ -99,8 +100,10 @@ public class ChangedItemDrawer : IDisposable slot = 0; foreach (var (item, flag) in LowerNames.Zip(Order)) + { if (item.Contains(lowerInput, StringComparison.Ordinal)) slot |= flag; + } return slot != 0; } diff --git a/Penumbra/UI/Changelog.cs b/Penumbra/UI/Changelog.cs index 184633f2..f4cedf7d 100644 --- a/Penumbra/UI/Changelog.cs +++ b/Penumbra/UI/Changelog.cs @@ -1,8 +1,9 @@ +using OtterGui.Services; using OtterGui.Widgets; namespace Penumbra.UI; -public class PenumbraChangelog +public class PenumbraChangelog : IUiService { public const int LastChangelogVersion = 0; diff --git a/Penumbra/UI/Classes/CollectionSelectHeader.cs b/Penumbra/UI/Classes/CollectionSelectHeader.cs index bff0092a..6c8bbf64 100644 --- a/Penumbra/UI/Classes/CollectionSelectHeader.cs +++ b/Penumbra/UI/Classes/CollectionSelectHeader.cs @@ -1,6 +1,7 @@ using ImGuiNET; using OtterGui.Raii; using OtterGui; +using OtterGui.Services; using Penumbra.Collections; using Penumbra.Collections.Manager; using Penumbra.Interop.PathResolving; @@ -9,7 +10,7 @@ using Penumbra.UI.ModsTab; namespace Penumbra.UI.Classes; -public class CollectionSelectHeader +public class CollectionSelectHeader : IUiService { private readonly CollectionCombo _collectionCombo; private readonly ActiveCollections _activeCollections; diff --git a/Penumbra/UI/ConfigWindow.cs b/Penumbra/UI/ConfigWindow.cs index 0ae16f6d..67b0a50c 100644 --- a/Penumbra/UI/ConfigWindow.cs +++ b/Penumbra/UI/ConfigWindow.cs @@ -4,6 +4,7 @@ using ImGuiNET; using OtterGui; using OtterGui.Custom; using OtterGui.Raii; +using OtterGui.Services; using OtterGui.Text; using Penumbra.Api.Enums; using Penumbra.Services; @@ -13,7 +14,7 @@ using Penumbra.Util; namespace Penumbra.UI; -public sealed class ConfigWindow : Window +public sealed class ConfigWindow : Window, IUiService { private readonly DalamudPluginInterface _pluginInterface; private readonly Configuration _config; diff --git a/Penumbra/UI/FileDialogService.cs b/Penumbra/UI/FileDialogService.cs index 88c0b00f..cc2a7f6a 100644 --- a/Penumbra/UI/FileDialogService.cs +++ b/Penumbra/UI/FileDialogService.cs @@ -3,12 +3,13 @@ using Dalamud.Interface.ImGuiFileDialog; using Dalamud.Utility; using ImGuiNET; using OtterGui; +using OtterGui.Services; using Penumbra.Communication; using Penumbra.Services; namespace Penumbra.UI; -public class FileDialogService : IDisposable +public class FileDialogService : IDisposable, IUiService { private readonly CommunicatorService _communicator; private readonly FileDialogManager _manager; diff --git a/Penumbra/UI/ImportPopup.cs b/Penumbra/UI/ImportPopup.cs index 71164d1d..fb2028b5 100644 --- a/Penumbra/UI/ImportPopup.cs +++ b/Penumbra/UI/ImportPopup.cs @@ -1,13 +1,14 @@ using Dalamud.Interface.Windowing; using ImGuiNET; using OtterGui.Raii; +using OtterGui.Services; using Penumbra.Import.Structs; using Penumbra.Mods.Manager; namespace Penumbra.UI; /// Draw the progress information for import. -public sealed class ImportPopup : Window +public sealed class ImportPopup : Window, IUiService { public const string WindowLabel = "Penumbra Import Status"; diff --git a/Penumbra/UI/LaunchButton.cs b/Penumbra/UI/LaunchButton.cs index 9650ccf8..14e16432 100644 --- a/Penumbra/UI/LaunchButton.cs +++ b/Penumbra/UI/LaunchButton.cs @@ -2,6 +2,7 @@ using Dalamud.Interface; using Dalamud.Interface.Internal; using Dalamud.Plugin; using Dalamud.Plugin.Services; +using OtterGui.Services; namespace Penumbra.UI; @@ -9,7 +10,7 @@ namespace Penumbra.UI; /// A Launch Button used in the title screen of the game, /// using the Dalamud-provided collapsible submenu. /// -public class LaunchButton : IDisposable +public class LaunchButton : IDisposable, IUiService { private readonly ConfigWindow _configWindow; private readonly UiBuilder _uiBuilder; diff --git a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs index 58f0b615..0ca4d40c 100644 --- a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs +++ b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs @@ -8,6 +8,7 @@ using OtterGui.Classes; using OtterGui.Filesystem; using OtterGui.FileSystem.Selector; using OtterGui.Raii; +using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.Collections.Manager; @@ -21,7 +22,7 @@ using MessageService = Penumbra.Services.MessageService; namespace Penumbra.UI.ModsTab; -public sealed class ModFileSystemSelector : FileSystemSelector +public sealed class ModFileSystemSelector : FileSystemSelector, IUiService { private readonly CommunicatorService _communicator; private readonly MessageService _messager; @@ -33,9 +34,9 @@ public sealed class ModFileSystemSelector : FileSystemSelector().Aggregate((a, b) => a | b); - public ReadOnlySpan Label => "Changed Items"u8; - public ModPanelChangedItemsTab(ModFileSystemSelector selector, ChangedItemDrawer drawer) - { - _selector = selector; - _drawer = drawer; - } - public bool IsVisible - => _selector.Selected!.ChangedItems.Count > 0; + => selector.Selected!.ChangedItems.Count > 0; public void DrawContent() { - _drawer.DrawTypeFilter(); + drawer.DrawTypeFilter(); ImGui.Separator(); using var table = ImRaii.Table("##changedItems", 1, ImGuiTableFlags.RowBg | ImGuiTableFlags.ScrollY, new Vector2(ImGui.GetContentRegionAvail().X, -1)); if (!table) return; - var zipList = ZipList.FromSortedList((SortedList)_selector.Selected!.ChangedItems); + var zipList = ZipList.FromSortedList((SortedList)selector.Selected!.ChangedItems); var height = ImGui.GetFrameHeightWithSpacing(); ImGui.TableNextColumn(); var skips = ImGuiClip.GetNecessarySkips(height); @@ -43,14 +33,14 @@ public class ModPanelChangedItemsTab : ITab } private bool CheckFilter((string Name, object? Data) kvp) - => _drawer.FilterChangedItem(kvp.Name, kvp.Data, LowerString.Empty); + => drawer.FilterChangedItem(kvp.Name, kvp.Data, LowerString.Empty); private void DrawChangedItem((string Name, object? Data) kvp) { ImGui.TableNextColumn(); - _drawer.DrawCategoryIcon(kvp.Name, kvp.Data); + drawer.DrawCategoryIcon(kvp.Name, kvp.Data); ImGui.SameLine(); - _drawer.DrawChangedItem(kvp.Name, kvp.Data); - _drawer.DrawModelData(kvp.Data); + drawer.DrawChangedItem(kvp.Name, kvp.Data); + drawer.DrawModelData(kvp.Data); } } diff --git a/Penumbra/UI/ModsTab/ModPanelCollectionsTab.cs b/Penumbra/UI/ModsTab/ModPanelCollectionsTab.cs index aa598557..9f37f847 100644 --- a/Penumbra/UI/ModsTab/ModPanelCollectionsTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelCollectionsTab.cs @@ -2,6 +2,7 @@ using Dalamud.Interface.Utility; using ImGuiNET; using OtterGui; using OtterGui.Raii; +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Collections; using Penumbra.Collections.Manager; @@ -10,25 +11,16 @@ using Penumbra.UI.Classes; namespace Penumbra.UI.ModsTab; -public class ModPanelCollectionsTab : ITab +public class ModPanelCollectionsTab(CollectionStorage storage, ModFileSystemSelector selector) : ITab, IUiService { - private readonly ModFileSystemSelector _selector; - private readonly CollectionStorage _collections; - - private readonly List<(ModCollection, ModCollection, uint, string)> _cache = new(); - - public ModPanelCollectionsTab(CollectionStorage storage, ModFileSystemSelector selector) - { - _collections = storage; - _selector = selector; - } + private readonly List<(ModCollection, ModCollection, uint, string)> _cache = []; public ReadOnlySpan Label => "Collections"u8; public void DrawContent() { - var (direct, inherited) = CountUsage(_selector.Selected!); + var (direct, inherited) = CountUsage(selector.Selected!); ImGui.NewLine(); if (direct == 1) ImGui.TextUnformatted("This Mod is directly configured in 1 collection."); @@ -80,7 +72,7 @@ public class ModPanelCollectionsTab : ITab var disInherited = ColorId.InheritedDisabledMod.Value(); var directCount = 0; var inheritedCount = 0; - foreach (var collection in _collections) + foreach (var collection in storage) { var (settings, parent) = collection[mod.Index]; var (color, text) = settings == null diff --git a/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs b/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs index c1a3c1eb..bee48068 100644 --- a/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelConflictsTab.cs @@ -3,6 +3,7 @@ using Dalamud.Interface.Utility; using ImGuiNET; using OtterGui; using OtterGui.Raii; +using OtterGui.Services; using OtterGui.Text; using OtterGui.Widgets; using Penumbra.Collections.Cache; @@ -16,7 +17,7 @@ using Penumbra.UI.Classes; namespace Penumbra.UI.ModsTab; -public class ModPanelConflictsTab(CollectionManager collectionManager, ModFileSystemSelector selector) : ITab +public class ModPanelConflictsTab(CollectionManager collectionManager, ModFileSystemSelector selector) : ITab, IUiService { private int? _currentPriority; diff --git a/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs b/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs index ed6340ab..6fe3e4c6 100644 --- a/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelDescriptionTab.cs @@ -2,6 +2,7 @@ using Dalamud.Interface.Utility; using ImGuiNET; using OtterGui.Raii; using OtterGui; +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Mods.Manager; @@ -12,7 +13,7 @@ public class ModPanelDescriptionTab( TutorialService tutorial, ModManager modManager, PredefinedTagManager predefinedTagsConfig) - : ITab + : ITab, IUiService { private readonly TagButtons _localTags = new(); private readonly TagButtons _modTags = new(); diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index 468e97b9..1e371065 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -6,13 +6,13 @@ using OtterGui; using OtterGui.Raii; using OtterGui.Widgets; using OtterGui.Classes; +using OtterGui.Services; using Penumbra.Mods; using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; using Penumbra.Services; using Penumbra.UI.AdvancedWindow; using Penumbra.Mods.Settings; -using Penumbra.Mods.Manager.OptionEditor; using Penumbra.UI.ModsTab.Groups; namespace Penumbra.UI.ModsTab; @@ -31,7 +31,7 @@ public class ModPanelEditTab( ModGroupEditDrawer groupEditDrawer, DescriptionEditPopup descriptionPopup, AddGroupDrawer addGroupDrawer) - : ITab + : ITab, IUiService { private readonly TagButtons _modTags = new(); diff --git a/Penumbra/UI/ModsTab/ModPanelTabBar.cs b/Penumbra/UI/ModsTab/ModPanelTabBar.cs index 8b09d8b9..639118f5 100644 --- a/Penumbra/UI/ModsTab/ModPanelTabBar.cs +++ b/Penumbra/UI/ModsTab/ModPanelTabBar.cs @@ -2,6 +2,7 @@ using Dalamud.Interface; using ImGuiNET; using OtterGui; using OtterGui.Raii; +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Mods; using Penumbra.Mods.Manager; @@ -9,7 +10,7 @@ using Penumbra.UI.AdvancedWindow; namespace Penumbra.UI.ModsTab; -public class ModPanelTabBar +public class ModPanelTabBar : IUiService { private enum ModPanelTabType { @@ -33,7 +34,7 @@ public class ModPanelTabBar public readonly ITab[] Tabs; private ModPanelTabType _preferredTab = ModPanelTabType.Settings; - private Mod? _lastMod = null; + private Mod? _lastMod; public ModPanelTabBar(ModEditWindow modEditWindow, ModPanelSettingsTab settings, ModPanelDescriptionTab description, ModPanelConflictsTab conflicts, ModPanelChangedItemsTab changedItems, ModPanelEditTab edit, ModManager modManager, @@ -49,15 +50,15 @@ public class ModPanelTabBar _tutorial = tutorial; Collections = collections; - Tabs = new ITab[] - { + Tabs = + [ Settings, Description, Conflicts, ChangedItems, Collections, Edit, - }; + ]; } public void Draw(Mod mod) diff --git a/Penumbra/UI/ModsTab/MultiModPanel.cs b/Penumbra/UI/ModsTab/MultiModPanel.cs index 595240f4..4079748e 100644 --- a/Penumbra/UI/ModsTab/MultiModPanel.cs +++ b/Penumbra/UI/ModsTab/MultiModPanel.cs @@ -3,12 +3,13 @@ using Dalamud.Interface.Utility; using ImGuiNET; using OtterGui; using OtterGui.Raii; +using OtterGui.Services; using Penumbra.Mods; using Penumbra.Mods.Manager; namespace Penumbra.UI.ModsTab; -public class MultiModPanel(ModFileSystemSelector _selector, ModDataEditor _editor) +public class MultiModPanel(ModFileSystemSelector _selector, ModDataEditor _editor) : IUiService { public void Draw() { @@ -65,8 +66,8 @@ public class MultiModPanel(ModFileSystemSelector _selector, ModDataEditor _edito } private string _tag = string.Empty; - private readonly List _addMods = []; - private readonly List<(Mod, int)> _removeMods = []; + private readonly List _addMods = []; + private readonly List<(Mod, int)> _removeMods = []; private void DrawMultiTagger() { diff --git a/Penumbra/UI/PredefinedTagManager.cs b/Penumbra/UI/PredefinedTagManager.cs index 0e5377d6..d531b1a2 100644 --- a/Penumbra/UI/PredefinedTagManager.cs +++ b/Penumbra/UI/PredefinedTagManager.cs @@ -6,14 +6,14 @@ using Newtonsoft.Json.Linq; using OtterGui; using OtterGui.Classes; using OtterGui.Raii; +using OtterGui.Services; using Penumbra.Mods.Manager; using Penumbra.Services; using Penumbra.UI.Classes; -using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs; namespace Penumbra.UI; -public sealed class PredefinedTagManager : ISavable, IReadOnlyList +public sealed class PredefinedTagManager : ISavable, IReadOnlyList, IService { public const int Version = 1; diff --git a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs index 65a8fe76..a7d1a8c6 100644 --- a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs +++ b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs @@ -2,6 +2,7 @@ using FFXIVClientStructs.FFXIV.Client.Game.Object; using FFXIVClientStructs.FFXIV.Client.System.Resource; using ImGuiNET; using OtterGui.Raii; +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Api.Enums; using Penumbra.Collections; @@ -15,7 +16,7 @@ using Penumbra.UI.Classes; namespace Penumbra.UI.ResourceWatcher; -public sealed class ResourceWatcher : IDisposable, ITab +public sealed class ResourceWatcher : IDisposable, ITab, IUiService { public const int DefaultMaxEntries = 1024; public const RecordType AllRecords = RecordType.Request | RecordType.ResourceLoad | RecordType.FileLoad | RecordType.Destruction; diff --git a/Penumbra/UI/Tabs/ChangedItemsTab.cs b/Penumbra/UI/Tabs/ChangedItemsTab.cs index ab0badf4..2aeaaea0 100644 --- a/Penumbra/UI/Tabs/ChangedItemsTab.cs +++ b/Penumbra/UI/Tabs/ChangedItemsTab.cs @@ -2,6 +2,7 @@ using ImGuiNET; using OtterGui; using OtterGui.Classes; using OtterGui.Raii; +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Api.Enums; using Penumbra.Collections.Manager; @@ -17,7 +18,7 @@ public class ChangedItemsTab( CollectionSelectHeader collectionHeader, ChangedItemDrawer drawer, CommunicatorService communicator) - : ITab + : ITab, IUiService { public ReadOnlySpan Label => "Changed Items"u8; diff --git a/Penumbra/UI/Tabs/CollectionsTab.cs b/Penumbra/UI/Tabs/CollectionsTab.cs index fabf7561..34e2cbcf 100644 --- a/Penumbra/UI/Tabs/CollectionsTab.cs +++ b/Penumbra/UI/Tabs/CollectionsTab.cs @@ -2,6 +2,7 @@ using Dalamud.Game.ClientState.Objects; using Dalamud.Plugin; using ImGuiNET; using OtterGui.Raii; +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Collections.Manager; using Penumbra.GameData.Actors; @@ -11,7 +12,7 @@ using Penumbra.UI.CollectionTab; namespace Penumbra.UI.Tabs; -public sealed class CollectionsTab : IDisposable, ITab +public sealed class CollectionsTab : IDisposable, ITab, IUiService { private readonly EphemeralConfig _config; private readonly CollectionSelector _selector; diff --git a/Penumbra/UI/Tabs/ConfigTabBar.cs b/Penumbra/UI/Tabs/ConfigTabBar.cs index 9fd07f27..28827ad9 100644 --- a/Penumbra/UI/Tabs/ConfigTabBar.cs +++ b/Penumbra/UI/Tabs/ConfigTabBar.cs @@ -1,4 +1,5 @@ using ImGuiNET; +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Api.Enums; using Penumbra.Mods; @@ -8,7 +9,7 @@ using Watcher = Penumbra.UI.ResourceWatcher.ResourceWatcher; namespace Penumbra.UI.Tabs; -public class ConfigTabBar : IDisposable +public class ConfigTabBar : IDisposable, IUiService { private readonly CommunicatorService _communicator; diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index 1519ebf0..0122a6f5 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -44,7 +44,7 @@ using Penumbra.Api.IpcTester; namespace Penumbra.UI.Tabs.Debug; -public class Diagnostics(IServiceProvider provider) +public class Diagnostics(ServiceManager provider) : IUiService { public void DrawDiagnostics() { @@ -55,7 +55,7 @@ public class Diagnostics(IServiceProvider provider) foreach (var type in typeof(ActorManager).Assembly.GetTypes() .Where(t => t is { IsAbstract: false, IsInterface: false } && t.IsAssignableTo(typeof(IAsyncDataContainer)))) { - var container = (IAsyncDataContainer)provider.GetRequiredService(type); + var container = (IAsyncDataContainer)provider.Provider!.GetRequiredService(type); ImGuiUtil.DrawTableColumn(container.Name); ImGuiUtil.DrawTableColumn(container.Time.ToString()); ImGuiUtil.DrawTableColumn(Functions.HumanReadableSize(container.Memory)); @@ -64,7 +64,7 @@ public class Diagnostics(IServiceProvider provider) } } -public class DebugTab : Window, ITab +public class DebugTab : Window, ITab, IUiService { private readonly PerformanceTracker _performance; private readonly Configuration _config; diff --git a/Penumbra/UI/Tabs/EffectiveTab.cs b/Penumbra/UI/Tabs/EffectiveTab.cs index 7076b80f..1b9af75c 100644 --- a/Penumbra/UI/Tabs/EffectiveTab.cs +++ b/Penumbra/UI/Tabs/EffectiveTab.cs @@ -3,6 +3,7 @@ using ImGuiNET; using OtterGui; using OtterGui.Classes; using OtterGui.Raii; +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Collections; using Penumbra.Collections.Cache; @@ -15,7 +16,7 @@ using Penumbra.UI.Classes; namespace Penumbra.UI.Tabs; public class EffectiveTab(CollectionManager collectionManager, CollectionSelectHeader collectionHeader) - : ITab + : ITab, IUiService { public ReadOnlySpan Label => "Effective Changes"u8; diff --git a/Penumbra/UI/Tabs/MessagesTab.cs b/Penumbra/UI/Tabs/MessagesTab.cs index abaf2ba6..190f9407 100644 --- a/Penumbra/UI/Tabs/MessagesTab.cs +++ b/Penumbra/UI/Tabs/MessagesTab.cs @@ -1,9 +1,10 @@ +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Services; namespace Penumbra.UI.Tabs; -public class MessagesTab(MessageService messages) : ITab +public class MessagesTab(MessageService messages) : ITab, IUiService { public ReadOnlySpan Label => "Messages"u8; diff --git a/Penumbra/UI/Tabs/ModsTab.cs b/Penumbra/UI/Tabs/ModsTab.cs index e4d94bb5..7faa3da8 100644 --- a/Penumbra/UI/Tabs/ModsTab.cs +++ b/Penumbra/UI/Tabs/ModsTab.cs @@ -6,6 +6,7 @@ using Penumbra.UI.Classes; using Dalamud.Interface; using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Housing; +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Api.Enums; using Penumbra.Interop.Services; @@ -30,7 +31,7 @@ public class ModsTab( CollectionSelectHeader collectionHeader, ITargetManager targets, ObjectManager objects) - : ITab + : ITab, IUiService { private readonly ActiveCollections _activeCollections = collectionManager.Active; diff --git a/Penumbra/UI/Tabs/OnScreenTab.cs b/Penumbra/UI/Tabs/OnScreenTab.cs index 787e07a1..fa33f702 100644 --- a/Penumbra/UI/Tabs/OnScreenTab.cs +++ b/Penumbra/UI/Tabs/OnScreenTab.cs @@ -1,16 +1,12 @@ +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.UI.AdvancedWindow; namespace Penumbra.UI.Tabs; -public class OnScreenTab : ITab +public class OnScreenTab(ResourceTreeViewerFactory resourceTreeViewerFactory) : ITab, IUiService { - private readonly ResourceTreeViewer _viewer; - - public OnScreenTab(ResourceTreeViewerFactory resourceTreeViewerFactory) - { - _viewer = resourceTreeViewerFactory.Create(0, delegate { }, delegate { }); - } + private readonly ResourceTreeViewer _viewer = resourceTreeViewerFactory.Create(0, delegate { }, delegate { }); public ReadOnlySpan Label => "On-Screen"u8; diff --git a/Penumbra/UI/Tabs/ResourceTab.cs b/Penumbra/UI/Tabs/ResourceTab.cs index bbb0561b..0b54c5e2 100644 --- a/Penumbra/UI/Tabs/ResourceTab.cs +++ b/Penumbra/UI/Tabs/ResourceTab.cs @@ -6,6 +6,7 @@ using FFXIVClientStructs.STD; using ImGuiNET; using OtterGui; using OtterGui.Raii; +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Interop.ResourceLoading; using Penumbra.String.Classes; @@ -13,7 +14,7 @@ using Penumbra.String.Classes; namespace Penumbra.UI.Tabs; public class ResourceTab(Configuration config, ResourceManagerService resourceManager, ISigScanner sigScanner) - : ITab + : ITab, IUiService { public ReadOnlySpan Label => "Resource Manager"u8; diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index 0de4f790..17db21c9 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -9,6 +9,7 @@ using OtterGui; using OtterGui.Compression; using OtterGui.Custom; using OtterGui.Raii; +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Api; using Penumbra.Interop.Services; @@ -19,7 +20,7 @@ using Penumbra.UI.ModsTab; namespace Penumbra.UI.Tabs; -public class SettingsTab : ITab +public class SettingsTab : ITab, IUiService { public const int RootDirectoryMaxLength = 64; diff --git a/Penumbra/UI/TutorialService.cs b/Penumbra/UI/TutorialService.cs index d87df19e..7d2a0d2a 100644 --- a/Penumbra/UI/TutorialService.cs +++ b/Penumbra/UI/TutorialService.cs @@ -1,3 +1,4 @@ +using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Collections; using Penumbra.Collections.Manager; @@ -40,7 +41,7 @@ public enum BasicTutorialSteps } /// Service for the in-game tutorial. -public class TutorialService +public class TutorialService : IUiService { public const string SelectedCollection = "Selected Collection"; public const string DefaultCollection = "Base Collection"; diff --git a/Penumbra/UI/WindowSystem.cs b/Penumbra/UI/WindowSystem.cs index c5418eb3..99819fce 100644 --- a/Penumbra/UI/WindowSystem.cs +++ b/Penumbra/UI/WindowSystem.cs @@ -1,12 +1,13 @@ using Dalamud.Interface; using Dalamud.Interface.Windowing; using Dalamud.Plugin; +using OtterGui.Services; using Penumbra.UI.AdvancedWindow; using Penumbra.UI.Tabs.Debug; namespace Penumbra.UI; -public class PenumbraWindowSystem : IDisposable +public class PenumbraWindowSystem : IDisposable, IUiService { private readonly UiBuilder _uiBuilder; private readonly WindowSystem _windowSystem; From 90124e83df4dfc6c21c5a8107e0d749229df2309 Mon Sep 17 00:00:00 2001 From: Actions User Date: Wed, 19 Jun 2024 11:51:55 +0000 Subject: [PATCH 259/433] [CI] Updating repo.json for testing_1.1.1.3 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 318aafc2..fc7234bd 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.1", - "TestingAssemblyVersion": "1.1.1.1", + "TestingAssemblyVersion": "1.1.1.3", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -18,7 +18,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.1/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.1/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.1.1.3/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.1/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From a90e253c73b6bd6e68f37f911e7be3f2c5e0a69b Mon Sep 17 00:00:00 2001 From: Ottermandias <70807659+Ottermandias@users.noreply.github.com> Date: Wed, 19 Jun 2024 13:54:17 +0200 Subject: [PATCH 260/433] Update repo.json --- repo.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/repo.json b/repo.json index fc7234bd..c00e3c8f 100644 --- a/repo.json +++ b/repo.json @@ -5,7 +5,7 @@ "Punchline": "Runtime mod loader and manager.", "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", - "AssemblyVersion": "1.1.1.1", + "AssemblyVersion": "1.1.1.2", "TestingAssemblyVersion": "1.1.1.3", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", @@ -17,9 +17,9 @@ "LoadPriority": 69420, "LoadRequiredState": 2, "LoadSync": true, - "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.1/Penumbra.zip", + "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.1.1.3/Penumbra.zip", - "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.1/Penumbra.zip", + "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } ] From 29f8c91306daafc4bc5eab156015c895c8aa9a21 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 19 Jun 2024 22:34:59 +0200 Subject: [PATCH 261/433] Make meta hooks respect Enable Mod setting and fix EQP composition. --- OtterGui | 2 +- Penumbra/Collections/Cache/EqpCache.cs | 49 +++++++++++++++++-- Penumbra/Configuration.cs | 16 +++++- .../Interop/Hooks/Meta/EqdpAccessoryHook.cs | 8 ++- Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs | 8 ++- Penumbra/Interop/Hooks/Meta/EqpHook.cs | 8 ++- Penumbra/Interop/Hooks/Meta/EstHook.cs | 13 +++-- Penumbra/Interop/Hooks/Meta/GmpHook.cs | 11 +++-- Penumbra/Interop/Hooks/Meta/RspBustHook.cs | 8 ++- Penumbra/Interop/Hooks/Meta/RspHeightHook.cs | 15 ++++-- Penumbra/Interop/Hooks/Meta/RspTailHook.cs | 8 ++- Penumbra/Interop/PathResolving/MetaState.cs | 8 +-- 12 files changed, 121 insertions(+), 33 deletions(-) diff --git a/OtterGui b/OtterGui index caa9e9b9..6fafc03b 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit caa9e9b9a5dc3928eba10b315cf6a0f6f1d84b65 +Subproject commit 6fafc03b34971be7c0e74fd9a638d1ed642ea19a diff --git a/Penumbra/Collections/Cache/EqpCache.cs b/Penumbra/Collections/Cache/EqpCache.cs index 60e38aef..c681b230 100644 --- a/Penumbra/Collections/Cache/EqpCache.cs +++ b/Penumbra/Collections/Cache/EqpCache.cs @@ -9,11 +9,24 @@ namespace Penumbra.Collections.Cache; public sealed class EqpCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { public unsafe EqpEntry GetValues(CharacterArmor* armor) - => GetSingleValue(armor[0].Set, EquipSlot.Head) - | GetSingleValue(armor[1].Set, EquipSlot.Body) - | GetSingleValue(armor[2].Set, EquipSlot.Hands) - | GetSingleValue(armor[3].Set, EquipSlot.Legs) - | GetSingleValue(armor[4].Set, EquipSlot.Feet); + { + var bodyEntry = GetSingleValue(armor[1].Set, EquipSlot.Body); + var headEntry = bodyEntry.HasFlag(EqpEntry.BodyShowHead) + ? GetSingleValue(armor[0].Set, EquipSlot.Head) + : GetSingleValue(armor[1].Set, EquipSlot.Head); + var handEntry = bodyEntry.HasFlag(EqpEntry.BodyShowHand) + ? GetSingleValue(armor[2].Set, EquipSlot.Hands) + : GetSingleValue(armor[1].Set, EquipSlot.Hands); + var (legsEntry, legsId) = bodyEntry.HasFlag(EqpEntry.BodyShowLeg) + ? (GetSingleValue(armor[3].Set, EquipSlot.Legs), 3) + : (GetSingleValue(armor[1].Set, EquipSlot.Legs), 1); + var footEntry = legsEntry.HasFlag(EqpEntry.LegsShowFoot) + ? GetSingleValue(armor[4].Set, EquipSlot.Feet) + : GetSingleValue(armor[legsId].Set, EquipSlot.Feet); + + var combined = bodyEntry | headEntry | handEntry | legsEntry | footEntry; + return PostProcessFeet(PostProcessHands(combined)); + } [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private EqpEntry GetSingleValue(PrimaryId id, EquipSlot slot) @@ -24,4 +37,30 @@ public sealed class EqpCache(MetaFileManager manager, ModCollection collection) protected override void Dispose(bool _) => Clear(); + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + private static EqpEntry PostProcessHands(EqpEntry entry) + { + if (!entry.HasFlag(EqpEntry.HandsHideForearm)) + return entry; + + var testFlag = entry.HasFlag(EqpEntry.HandsHideElbow) + ? entry.HasFlag(EqpEntry.BodyHideGlovesL) + : entry.HasFlag(EqpEntry.BodyHideGlovesM); + return testFlag + ? (entry | EqpEntry._4) & ~EqpEntry.BodyHideGlovesS + : entry & ~(EqpEntry._4 | EqpEntry.BodyHideGlovesS); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + private static EqpEntry PostProcessFeet(EqpEntry entry) + { + if (!entry.HasFlag(EqpEntry.FeetHideCalf)) + return entry; + + if (entry.HasFlag(EqpEntry.FeetHideKnee) || !entry.HasFlag(EqpEntry._20)) + return entry & ~(EqpEntry.LegsHideBootsS | EqpEntry.LegsHideBootsM); + + return (entry | EqpEntry.LegsHideBootsM) & ~EqpEntry.LegsHideBootsS; + } } diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index f6100b62..7faed6a2 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -31,7 +31,21 @@ public class Configuration : IPluginConfiguration, ISavable, IService public ChangeLogDisplayType ChangeLogDisplayType { get; set; } = ChangeLogDisplayType.New; - public bool EnableMods { get; set; } = true; + public event Action? ModsEnabled; + + [JsonIgnore] + private bool _enableMods = true; + + public bool EnableMods + { + get => _enableMods; + set + { + _enableMods = value; + ModsEnabled?.Invoke(value); + } + } + public string ModDirectory { get; set; } = string.Empty; public string ExportDirectory { get; set; } = string.Empty; diff --git a/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs b/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs index aaaaccd4..583a2ac5 100644 --- a/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs @@ -6,7 +6,7 @@ using Penumbra.Interop.PathResolving; namespace Penumbra.Interop.Hooks.Meta; -public unsafe class EqdpAccessoryHook : FastHook +public unsafe class EqdpAccessoryHook : FastHook, IDisposable { public delegate void Delegate(CharacterUtility* utility, EqdpEntry* entry, uint id, uint raceCode); @@ -15,7 +15,8 @@ public unsafe class EqdpAccessoryHook : FastHook public EqdpAccessoryHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetEqdpAccessoryEntry", "E8 ?? ?? ?? ?? 41 BF ?? ?? ?? ?? 83 FB", Detour, true); + Task = hooks.CreateHook("GetEqdpAccessoryEntry", "E8 ?? ?? ?? ?? 41 BF ?? ?? ?? ?? 83 FB", Detour, metaState.Config.EnableMods); + _metaState.Config.ModsEnabled += Toggle; } private void Detour(CharacterUtility* utility, EqdpEntry* entry, uint setId, uint raceCode) @@ -27,4 +28,7 @@ public unsafe class EqdpAccessoryHook : FastHook Penumbra.Log.Excessive( $"[GetEqdpAccessoryEntry] Invoked on 0x{(ulong)utility:X} with {setId}, {(GenderRace)raceCode}, returned {(ushort)*entry:B10}."); } + + public void Dispose() + => _metaState.Config.ModsEnabled -= Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs b/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs index 2711f195..f5488f80 100644 --- a/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs @@ -6,7 +6,7 @@ using Penumbra.Interop.PathResolving; namespace Penumbra.Interop.Hooks.Meta; -public unsafe class EqdpEquipHook : FastHook +public unsafe class EqdpEquipHook : FastHook, IDisposable { public delegate void Delegate(CharacterUtility* utility, EqdpEntry* entry, uint id, uint raceCode); @@ -15,7 +15,8 @@ public unsafe class EqdpEquipHook : FastHook public EqdpEquipHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetEqdpEquipEntry", "E8 ?? ?? ?? ?? 85 DB 75 ?? F6 45", Detour, true); + Task = hooks.CreateHook("GetEqdpEquipEntry", "E8 ?? ?? ?? ?? 85 DB 75 ?? F6 45", Detour, metaState.Config.EnableMods); + _metaState.Config.ModsEnabled += Toggle; } private void Detour(CharacterUtility* utility, EqdpEntry* entry, uint setId, uint raceCode) @@ -27,4 +28,7 @@ public unsafe class EqdpEquipHook : FastHook Penumbra.Log.Excessive( $"[GetEqdpEquipEntry] Invoked on 0x{(ulong)utility:X} with {setId}, {(GenderRace)raceCode}, returned {(ushort)*entry:B10}."); } + + public void Dispose() + => _metaState.Config.ModsEnabled -= Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/EqpHook.cs b/Penumbra/Interop/Hooks/Meta/EqpHook.cs index 7107e26b..e96d8115 100644 --- a/Penumbra/Interop/Hooks/Meta/EqpHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqpHook.cs @@ -5,7 +5,7 @@ using Penumbra.Interop.PathResolving; namespace Penumbra.Interop.Hooks.Meta; -public unsafe class EqpHook : FastHook +public unsafe class EqpHook : FastHook, IDisposable { public delegate void Delegate(CharacterUtility* utility, EqpEntry* flags, CharacterArmor* armor); @@ -14,7 +14,8 @@ public unsafe class EqpHook : FastHook public EqpHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetEqpFlags", "E8 ?? ?? ?? ?? 0F B6 44 24 ?? C0 E8", Detour, true); + Task = hooks.CreateHook("GetEqpFlags", "E8 ?? ?? ?? ?? 0F B6 44 24 ?? C0 E8", Detour, metaState.Config.EnableMods); + _metaState.Config.ModsEnabled += Toggle; } private void Detour(CharacterUtility* utility, EqpEntry* flags, CharacterArmor* armor) @@ -31,4 +32,7 @@ public unsafe class EqpHook : FastHook Penumbra.Log.Excessive($"[GetEqpFlags] Invoked on 0x{(nint)utility:X} with 0x{(ulong)armor:X}, returned 0x{(ulong)*flags:X16}."); } + + public void Dispose() + => _metaState.Config.ModsEnabled -= Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/EstHook.cs b/Penumbra/Interop/Hooks/Meta/EstHook.cs index 23931182..fa0bb3c5 100644 --- a/Penumbra/Interop/Hooks/Meta/EstHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EstHook.cs @@ -6,7 +6,7 @@ using Penumbra.Meta.Manipulations; namespace Penumbra.Interop.Hooks.Meta; -public class EstHook : FastHook +public class EstHook : FastHook, IDisposable { public delegate EstEntry Delegate(uint id, int estType, uint genderRace); @@ -14,14 +14,16 @@ public class EstHook : FastHook public EstHook(HookManager hooks, MetaState metaState) { - _metaState = metaState; - Task = hooks.CreateHook("GetEstEntry", "44 8B C9 83 EA ?? 74", Detour, true); + _metaState = metaState; + Task = hooks.CreateHook("GetEstEntry", "44 8B C9 83 EA ?? 74", Detour, metaState.Config.EnableMods); + _metaState.Config.ModsEnabled += Toggle; } private EstEntry Detour(uint genderRace, int estType, uint id) { EstEntry ret; - if (_metaState.EstCollection.TryPeek(out var collection) && collection is { Valid: true, ModCollection.MetaCache: { } cache } + if (_metaState.EstCollection.TryPeek(out var collection) + && collection is { Valid: true, ModCollection.MetaCache: { } cache } && cache.Est.TryGetValue(Convert(genderRace, estType, id), out var entry)) ret = entry.Entry; else @@ -46,4 +48,7 @@ public class EstHook : FastHook }; return new EstIdentifier(i, type, gr); } + + public void Dispose() + => _metaState.Config.ModsEnabled -= Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/GmpHook.cs b/Penumbra/Interop/Hooks/Meta/GmpHook.cs index 256d8702..44d35f12 100644 --- a/Penumbra/Interop/Hooks/Meta/GmpHook.cs +++ b/Penumbra/Interop/Hooks/Meta/GmpHook.cs @@ -5,7 +5,7 @@ using Penumbra.Meta.Manipulations; namespace Penumbra.Interop.Hooks.Meta; -public unsafe class GmpHook : FastHook +public unsafe class GmpHook : FastHook, IDisposable { public delegate nint Delegate(nint gmpResource, uint dividedHeadId); @@ -16,7 +16,8 @@ public unsafe class GmpHook : FastHook public GmpHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetGmpEntry", "E8 ?? ?? ?? ?? 48 85 C0 74 ?? 43 8D 0C", Detour, true); + Task = hooks.CreateHook("GetGmpEntry", "E8 ?? ?? ?? ?? 48 85 C0 74 ?? 43 8D 0C", Detour, metaState.Config.EnableMods); + _metaState.Config.ModsEnabled += Toggle; } /// @@ -27,7 +28,8 @@ public unsafe class GmpHook : FastHook private nint Detour(nint gmpResource, uint dividedHeadId) { nint ret; - if (_metaState.GmpCollection.TryPeek(out var collection) && collection.Collection is { Valid: true, ModCollection.MetaCache: { } cache } + if (_metaState.GmpCollection.TryPeek(out var collection) + && collection.Collection is { Valid: true, ModCollection.MetaCache: { } cache } && cache.Gmp.TryGetValue(new GmpIdentifier(collection.Id), out var entry)) { if (entry.Entry.Enabled) @@ -61,4 +63,7 @@ public unsafe class GmpHook : FastHook Marshal.FreeHGlobal((nint)Pointer); } } + + public void Dispose() + => _metaState.Config.ModsEnabled -= Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/RspBustHook.cs b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs index 86759460..db22d90c 100644 --- a/Penumbra/Interop/Hooks/Meta/RspBustHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs @@ -7,7 +7,7 @@ using Penumbra.Meta.Manipulations; namespace Penumbra.Interop.Hooks.Meta; -public unsafe class RspBustHook : FastHook +public unsafe class RspBustHook : FastHook, IDisposable { public delegate float* Delegate(nint cmpResource, float* storage, Race race, byte gender, byte isSecondSubRace, byte bodyType, byte bustSize); @@ -19,7 +19,8 @@ public unsafe class RspBustHook : FastHook { _metaState = metaState; _metaFileManager = metaFileManager; - Task = hooks.CreateHook("GetRspBust", "E8 ?? ?? ?? ?? F2 0F 10 44 24 ?? 8B 44 24", Detour, true); + Task = hooks.CreateHook("GetRspBust", "E8 ?? ?? ?? ?? F2 0F 10 44 24 ?? 8B 44 24", Detour, metaState.Config.EnableMods); + _metaState.Config.ModsEnabled += Toggle; } private float* Detour(nint cmpResource, float* storage, Race race, byte gender, byte isSecondSubRace, byte bodyType, byte bustSize) @@ -63,4 +64,7 @@ public unsafe class RspBustHook : FastHook $"[GetRspBust] Invoked on 0x{cmpResource:X} with {race}, {(Gender)(gender + 1)}, {isSecondSubRace == 1}, {bodyType}, {bustSize}, returned {storage[0]}, {storage[1]}, {storage[2]}."); return ret; } + + public void Dispose() + => _metaState.Config.ModsEnabled -= Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs index cf88c34a..dcb3f19c 100644 --- a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs @@ -1,4 +1,3 @@ -using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; using Penumbra.GameData.Enums; using Penumbra.Interop.PathResolving; @@ -8,7 +7,7 @@ using Penumbra.Meta.Manipulations; namespace Penumbra.Interop.Hooks.Meta; -public class RspHeightHook : FastHook +public class RspHeightHook : FastHook, IDisposable { public delegate float Delegate(nint cmpResource, Race clan, byte gender, byte isSecondSubRace, byte bodyType, byte height); @@ -17,15 +16,18 @@ public class RspHeightHook : FastHook public RspHeightHook(HookManager hooks, MetaState metaState, MetaFileManager metaFileManager) { - _metaState = metaState; + _metaState = metaState; _metaFileManager = metaFileManager; - Task = hooks.CreateHook("GetRspHeight", "E8 ?? ?? ?? ?? 48 8B 8E ?? ?? ?? ?? 44 8B CF", Detour, true); + Task = hooks.CreateHook("GetRspHeight", "E8 ?? ?? ?? ?? 48 8B 8E ?? ?? ?? ?? 44 8B CF", Detour, metaState.Config.EnableMods); + _metaState.Config.ModsEnabled += Toggle; } private unsafe float Detour(nint cmpResource, Race race, byte gender, byte isSecondSubRace, byte bodyType, byte height) { float scale; - if (bodyType < 2 && _metaState.RspCollection.TryPeek(out var collection) && collection is { Valid: true, ModCollection.MetaCache: { } cache }) + if (bodyType < 2 + && _metaState.RspCollection.TryPeek(out var collection) + && collection is { Valid: true, ModCollection.MetaCache: { } cache }) { var clan = (SubRace)(((int)race - 1) * 2 + 1 + isSecondSubRace); var (minIdent, maxIdent) = gender == 0 @@ -66,4 +68,7 @@ public class RspHeightHook : FastHook $"[GetRspHeight] Invoked on 0x{cmpResource:X} with {race}, {(Gender)(gender + 1)}, {isSecondSubRace == 1}, {bodyType}, {height}, returned {scale}."); return scale; } + + public void Dispose() + => _metaState.Config.ModsEnabled -= Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/RspTailHook.cs b/Penumbra/Interop/Hooks/Meta/RspTailHook.cs index e40f0161..8d333575 100644 --- a/Penumbra/Interop/Hooks/Meta/RspTailHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspTailHook.cs @@ -7,7 +7,7 @@ using Penumbra.Meta.Manipulations; namespace Penumbra.Interop.Hooks.Meta; -public class RspTailHook : FastHook +public class RspTailHook : FastHook, IDisposable { public delegate float Delegate(nint cmpResource, Race clan, byte gender, byte isSecondSubRace, byte bodyType, byte height); @@ -18,7 +18,8 @@ public class RspTailHook : FastHook { _metaState = metaState; _metaFileManager = metaFileManager; - Task = hooks.CreateHook("GetRspTail", "E8 ?? ?? ?? ?? 0F 28 F0 48 8B 05", Detour, true); + Task = hooks.CreateHook("GetRspTail", "E8 ?? ?? ?? ?? 0F 28 F0 48 8B 05", Detour, metaState.Config.EnableMods); + _metaState.Config.ModsEnabled += Toggle; } private unsafe float Detour(nint cmpResource, Race race, byte gender, byte isSecondSubRace, byte bodyType, byte tailLength) @@ -65,4 +66,7 @@ public class RspTailHook : FastHook $"[GetRspTail] Invoked on 0x{cmpResource:X} with {race}, {(Gender)(gender + 1)}, {isSecondSubRace == 1}, {bodyType}, {tailLength}, returned {scale}."); return scale; } + + public void Dispose() + => _metaState.Config.ModsEnabled -= Toggle; } diff --git a/Penumbra/Interop/PathResolving/MetaState.cs b/Penumbra/Interop/PathResolving/MetaState.cs index f7dcfc07..5eacbfb0 100644 --- a/Penumbra/Interop/PathResolving/MetaState.cs +++ b/Penumbra/Interop/PathResolving/MetaState.cs @@ -37,7 +37,7 @@ namespace Penumbra.Interop.PathResolving; // GMP Entries seem to be only used by "48 8B ?? 53 55 57 48 83 ?? ?? 48 8B", which is SetupVisor. public sealed unsafe class MetaState : IDisposable, IService { - private readonly Configuration _config; + public readonly Configuration Config; private readonly CommunicatorService _communicator; private readonly CollectionResolver _collectionResolver; private readonly ResourceLoader _resources; @@ -64,7 +64,7 @@ public sealed unsafe class MetaState : IDisposable, IService _resources = resources; _createCharacterBase = createCharacterBase; _characterUtility = characterUtility; - _config = config; + Config = config; _createCharacterBase.Subscribe(OnCreatingCharacterBase, CreateCharacterBase.Priority.MetaState); _createCharacterBase.Subscribe(OnCharacterBaseCreated, CreateCharacterBase.PostEvent.Priority.MetaState); } @@ -84,7 +84,7 @@ public sealed unsafe class MetaState : IDisposable, IService } public DecalReverter ResolveDecal(ResolveData resolve, bool which) - => new(_config, _characterUtility, _resources, resolve, which); + => new(Config, _characterUtility, _resources, resolve, which); public void Dispose() { @@ -99,7 +99,7 @@ public sealed unsafe class MetaState : IDisposable, IService _communicator.CreatingCharacterBase.Invoke(_lastCreatedCollection.AssociatedGameObject, _lastCreatedCollection.ModCollection.Id, (nint)modelCharaId, (nint)customize, (nint)equipData); - var decal = new DecalReverter(_config, _characterUtility, _resources, _lastCreatedCollection, + var decal = new DecalReverter(Config, _characterUtility, _resources, _lastCreatedCollection, UsesDecal(*(uint*)modelCharaId, (nint)customize)); RspCollection.Push(_lastCreatedCollection); _characterBaseCreateMetaChanges.Dispose(); // Should always be empty. From f686a0ff09873a08eba20a332b27c0090834dbae Mon Sep 17 00:00:00 2001 From: Actions User Date: Wed, 19 Jun 2024 20:37:08 +0000 Subject: [PATCH 262/433] [CI] Updating repo.json for testing_1.1.1.4 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index c00e3c8f..03f13499 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.1.1.3", + "TestingAssemblyVersion": "1.1.1.4", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -18,7 +18,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.1.1.3/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.1.1.4/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 8cd8efa72347bda899a32baab1f878e55c3c7c9f Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 20 Jun 2024 14:24:35 +0200 Subject: [PATCH 263/433] Fix RSP scaling for NPC values. --- Penumbra/Interop/Hooks/Meta/RspHeightHook.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs index dcb3f19c..98e39061 100644 --- a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs @@ -29,6 +29,12 @@ public class RspHeightHook : FastHook, IDisposable && _metaState.RspCollection.TryPeek(out var collection) && collection is { Valid: true, ModCollection.MetaCache: { } cache }) { + // Special cases. + if (height == 0xFF) + return 1.0f; + if (height > 100) + height = 0; + var clan = (SubRace)(((int)race - 1) * 2 + 1 + isSecondSubRace); var (minIdent, maxIdent) = gender == 0 ? (new RspIdentifier(clan, RspAttribute.MaleMinSize), new RspIdentifier(clan, RspAttribute.MaleMaxSize)) From ab1e11aba1b42a67552d5557cc16402b41f0e4ad Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 20 Jun 2024 14:51:17 +0200 Subject: [PATCH 264/433] Improve support info a bit. --- Penumbra/Penumbra.cs | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index 905b998d..38d9c7b2 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -21,6 +21,7 @@ using OtterGui.Tasks; using Penumbra.GameData.Enums; using Penumbra.UI; using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManager; +using System.Xml.Linq; namespace Penumbra; @@ -175,6 +176,26 @@ public class Penumbra : IDalamudPlugin _disposed = true; } + private void GatherRelevantPlugins(StringBuilder sb) + { + ReadOnlySpan relevantPlugins = + [ + "Glamourer", "MareSynchronos", "CustomizePlus", "SimpleHeels", "VfxEditor", "heliosphere-plugin", "Ktisis", "Brio", "DynamicBridge", + ]; + var plugins = _services.GetService().InstalledPlugins + .GroupBy(p => p.InternalName) + .ToDictionary(g => g.Key, g => + { + var item = g.OrderByDescending(p => p.IsLoaded).ThenByDescending(p => p.Version).First(); + return (item.IsLoaded, item.Version, item.Name); + }); + foreach (var plugin in relevantPlugins) + { + if (plugins.TryGetValue(plugin, out var data)) + sb.Append($"> **`{data.Name + ':',-29}`** {data.Version}{(data.IsLoaded ? string.Empty : " (Disabled)")}\n"); + } + } + public string GatherSupportInformation() { var sb = new StringBuilder(10240); @@ -198,6 +219,7 @@ public class Penumbra : IDalamudPlugin sb.Append( $"> **`Logging: `** Log: {_config.Ephemeral.EnableResourceLogging}, Watcher: {_config.Ephemeral.EnableResourceWatcher} ({_config.MaxResourceWatcherRecords})\n"); sb.Append($"> **`Use Ownership: `** {_config.UseOwnerNameForCharacterCollection}\n"); + GatherRelevantPlugins(sb); sb.AppendLine("**Mods**"); sb.Append($"> **`Installed Mods: `** {_modManager.Count}\n"); sb.Append($"> **`Mods with Config: `** {_modManager.Count(m => m.HasOptions)}\n"); @@ -212,27 +234,25 @@ public class Penumbra : IDalamudPlugin $"> **`#Temp Mods: `** {_tempMods.Mods.Sum(kvp => kvp.Value.Count) + _tempMods.ModsForAllCollections.Count}\n"); void PrintCollection(ModCollection c, CollectionCache _) - => sb.Append($"**Collection {c.AnonymizedName}**\n" - + $"> **`Inheritances: `** {c.DirectlyInheritsFrom.Count}\n" - + $"> **`Enabled Mods: `** {c.ActualSettings.Count(s => s is { Enabled: true })}\n" - + $"> **`Conflicts (Solved/Total): `** {c.AllConflicts.SelectMany(x => x).Sum(x => x.HasPriority && x.Solved ? x.Conflicts.Count : 0)}/{c.AllConflicts.SelectMany(x => x).Sum(x => x.HasPriority ? x.Conflicts.Count : 0)}\n"); + => sb.Append( + $"> **`Collection {c.AnonymizedName + ':',-18}`** Inheritances: `{c.DirectlyInheritsFrom.Count,3}`, Enabled Mods: `{c.ActualSettings.Count(s => s is { Enabled: true }),4}`, Conflicts: `{c.AllConflicts.SelectMany(x => x).Sum(x => x is { HasPriority: true, Solved: true } ? x.Conflicts.Count : 0),5}/{c.AllConflicts.SelectMany(x => x).Sum(x => x.HasPriority ? x.Conflicts.Count : 0),5}`\n"); sb.AppendLine("**Collections**"); - sb.Append($"> **`#Collections: `** {_collectionManager.Storage.Count - 1}\n"); - sb.Append($"> **`#Temp Collections: `** {_tempCollections.Count}\n"); - sb.Append($"> **`Active Collections: `** {_collectionManager.Caches.Count}\n"); - sb.Append($"> **`Base Collection: `** {_collectionManager.Active.Default.AnonymizedName}\n"); - sb.Append($"> **`Interface Collection: `** {_collectionManager.Active.Interface.AnonymizedName}\n"); - sb.Append($"> **`Selected Collection: `** {_collectionManager.Active.Current.AnonymizedName}\n"); + sb.Append($"> **`#Collections: `** {_collectionManager.Storage.Count - 1}\n"); + sb.Append($"> **`#Temp Collections: `** {_tempCollections.Count}\n"); + sb.Append($"> **`Active Collections: `** {_collectionManager.Caches.Count}\n"); + sb.Append($"> **`Base Collection: `** {_collectionManager.Active.Default.AnonymizedName}\n"); + sb.Append($"> **`Interface Collection: `** {_collectionManager.Active.Interface.AnonymizedName}\n"); + sb.Append($"> **`Selected Collection: `** {_collectionManager.Active.Current.AnonymizedName}\n"); foreach (var (type, name, _) in CollectionTypeExtensions.Special) { var collection = _collectionManager.Active.ByType(type); if (collection != null) - sb.Append($"> **`{name,-30}`** {collection.AnonymizedName}\n"); + sb.Append($"> **`{name,-29}`** {collection.AnonymizedName}\n"); } foreach (var (name, id, collection) in _collectionManager.Active.Individuals.Assignments) - sb.Append($"> **`{id[0].Incognito(name) + ':',-30}`** {collection.AnonymizedName}\n"); + sb.Append($"> **`{id[0].Incognito(name) + ':',-29}`** {collection.AnonymizedName}\n"); foreach (var collection in _collectionManager.Caches.Active) PrintCollection(collection, collection._cache!); From 045abc787d46bb472366a08bf22c020906ec5690 Mon Sep 17 00:00:00 2001 From: Actions User Date: Thu, 20 Jun 2024 12:53:23 +0000 Subject: [PATCH 265/433] [CI] Updating repo.json for testing_1.1.1.5 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 03f13499..3142f8d4 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.1.1.4", + "TestingAssemblyVersion": "1.1.1.5", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -18,7 +18,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.1.1.4/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.1.1.5/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From b07af32deead6ee2c601ff26d1d8a5194ea3ae30 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 22 Jun 2024 23:04:09 +0200 Subject: [PATCH 266/433] Fix doubled hook. --- .../Resources/ResourceHandleDestructor.cs | 4 +++ .../ResourceLoading/ResourceService.cs | 28 +--------------- .../UI/ResourceWatcher/ResourceWatcher.cs | 32 +++++++++++-------- 3 files changed, 23 insertions(+), 41 deletions(-) diff --git a/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs b/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs index 5ddb7eaa..ac3f504a 100644 --- a/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs +++ b/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs @@ -3,6 +3,7 @@ using OtterGui.Classes; using OtterGui.Services; using Penumbra.GameData; using Penumbra.Interop.Structs; +using Penumbra.UI.ResourceWatcher; namespace Penumbra.Interop.Hooks.Resources; @@ -15,6 +16,9 @@ public sealed unsafe class ResourceHandleDestructor : EventWrapperPtr ShaderReplacementFixer, + + /// + ResourceWatcher, } public ResourceHandleDestructor(HookManager hooks) diff --git a/Penumbra/Interop/ResourceLoading/ResourceService.cs b/Penumbra/Interop/ResourceLoading/ResourceService.cs index 54c86777..0947d2ec 100644 --- a/Penumbra/Interop/ResourceLoading/ResourceService.cs +++ b/Penumbra/Interop/ResourceLoading/ResourceService.cs @@ -26,7 +26,6 @@ public unsafe class ResourceService : IDisposable, IRequiredService interop.InitializeFromAttributes(this); _getResourceSyncHook.Enable(); _getResourceAsyncHook.Enable(); - _resourceHandleDestructorHook.Enable(); _incRefHook = interop.HookFromAddress( (nint)CSResourceHandle.MemberFunctionPointers.IncRef, ResourceHandleIncRefDetour); @@ -51,7 +50,6 @@ public unsafe class ResourceService : IDisposable, IRequiredService { _getResourceSyncHook.Dispose(); _getResourceAsyncHook.Dispose(); - _resourceHandleDestructorHook.Dispose(); _incRefHook.Dispose(); _decRefHook.Dispose(); } @@ -67,8 +65,7 @@ public unsafe class ResourceService : IDisposable, IRequiredService /// Whether to request the resource synchronously or asynchronously. /// The returned resource handle. If this is not null, calling original will be skipped. public delegate void GetResourcePreDelegate(ref ResourceCategory category, ref ResourceType type, ref int hash, ref Utf8GamePath path, - Utf8GamePath original, - GetResourceParameters* parameters, ref bool sync, ref ResourceHandle* returnValue); + Utf8GamePath original, GetResourceParameters* parameters, ref bool sync, ref ResourceHandle* returnValue); /// /// Subscribers should be exception-safe. @@ -192,27 +189,4 @@ public unsafe class ResourceService : IDisposable, IRequiredService } #endregion - - #region Destructor - - /// Invoked before a resource handle is destructed. - /// The resource handle. - public delegate void ResourceHandleDtorDelegate(ResourceHandle* handle); - - /// - /// - /// Subscribers should be exception-safe. - /// - public event ResourceHandleDtorDelegate? ResourceHandleDestructor; - - [Signature(Sigs.ResourceHandleDestructor, DetourName = nameof(ResourceHandleDestructorDetour))] - private readonly Hook _resourceHandleDestructorHook = null!; - - private nint ResourceHandleDestructorDetour(ResourceHandle* handle) - { - ResourceHandleDestructor?.Invoke(handle); - return _resourceHandleDestructorHook.OriginalDisposeSafe(handle); - } - - #endregion } diff --git a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs index a7d1a8c6..3bf4cd88 100644 --- a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs +++ b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs @@ -8,8 +8,10 @@ using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.GameData.Actors; using Penumbra.GameData.Enums; +using Penumbra.Interop.Hooks.Resources; using Penumbra.Interop.ResourceLoading; using Penumbra.Interop.Structs; +using Penumbra.Services; using Penumbra.String; using Penumbra.String.Classes; using Penumbra.UI.Classes; @@ -21,28 +23,30 @@ public sealed class ResourceWatcher : IDisposable, ITab, IUiService public const int DefaultMaxEntries = 1024; public const RecordType AllRecords = RecordType.Request | RecordType.ResourceLoad | RecordType.FileLoad | RecordType.Destruction; - private readonly Configuration _config; - private readonly EphemeralConfig _ephemeral; - private readonly ResourceService _resources; - private readonly ResourceLoader _loader; - private readonly ActorManager _actors; - private readonly List _records = []; - private readonly ConcurrentQueue _newRecords = []; - private readonly ResourceWatcherTable _table; - private string _logFilter = string.Empty; - private Regex? _logRegex; - private int _newMaxEntries; + private readonly Configuration _config; + private readonly EphemeralConfig _ephemeral; + private readonly ResourceService _resources; + private readonly ResourceLoader _loader; + private readonly ResourceHandleDestructor _destructor; + private readonly ActorManager _actors; + private readonly List _records = []; + private readonly ConcurrentQueue _newRecords = []; + private readonly ResourceWatcherTable _table; + private string _logFilter = string.Empty; + private Regex? _logRegex; + private int _newMaxEntries; - public unsafe ResourceWatcher(ActorManager actors, Configuration config, ResourceService resources, ResourceLoader loader) + public unsafe ResourceWatcher(ActorManager actors, Configuration config, ResourceService resources, ResourceLoader loader, ResourceHandleDestructor destructor) { _actors = actors; _config = config; _ephemeral = config.Ephemeral; _resources = resources; + _destructor = destructor; _loader = loader; _table = new ResourceWatcherTable(config.Ephemeral, _records); _resources.ResourceRequested += OnResourceRequested; - _resources.ResourceHandleDestructor += OnResourceDestroyed; + _destructor.Subscribe(OnResourceDestroyed, ResourceHandleDestructor.Priority.ResourceWatcher); _loader.ResourceLoaded += OnResourceLoaded; _loader.FileLoaded += OnFileLoaded; UpdateFilter(_ephemeral.ResourceLoggingFilter, false); @@ -54,7 +58,7 @@ public sealed class ResourceWatcher : IDisposable, ITab, IUiService Clear(); _records.TrimExcess(); _resources.ResourceRequested -= OnResourceRequested; - _resources.ResourceHandleDestructor -= OnResourceDestroyed; + _destructor.Unsubscribe(OnResourceDestroyed); _loader.ResourceLoaded -= OnResourceLoaded; _loader.FileLoaded -= OnFileLoaded; } From c2e74ed382c494272fd7ee1a7474ba33fb504c9b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 25 Jun 2024 22:50:52 +0200 Subject: [PATCH 267/433] Improve signatures. --- Penumbra.GameData | 2 +- Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs | 3 ++- Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs | 3 ++- Penumbra/Interop/Hooks/Meta/EqpHook.cs | 3 ++- Penumbra/Interop/Hooks/Meta/EstHook.cs | 3 ++- Penumbra/Interop/Hooks/Meta/GmpHook.cs | 3 ++- Penumbra/Interop/Hooks/Meta/RspBustHook.cs | 3 ++- Penumbra/Interop/Hooks/Meta/RspHeightHook.cs | 3 ++- Penumbra/Interop/Hooks/Meta/RspTailHook.cs | 3 ++- 9 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 3fbc7045..4b55c05c 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 3fbc704515b7b5fa9be02fb2a44719fc333747c1 +Subproject commit 4b55c05c72eb194bec0d28c52cf076962010424b diff --git a/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs b/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs index 583a2ac5..bfbe6866 100644 --- a/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs @@ -1,5 +1,6 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; +using Penumbra.GameData; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.PathResolving; @@ -15,7 +16,7 @@ public unsafe class EqdpAccessoryHook : FastHook, ID public EqdpAccessoryHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetEqdpAccessoryEntry", "E8 ?? ?? ?? ?? 41 BF ?? ?? ?? ?? 83 FB", Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetEqdpAccessoryEntry", Sigs.GetEqdpAccessoryEntry, Detour, metaState.Config.EnableMods); _metaState.Config.ModsEnabled += Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs b/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs index f5488f80..6ea38ee2 100644 --- a/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs @@ -1,5 +1,6 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; +using Penumbra.GameData; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.PathResolving; @@ -15,7 +16,7 @@ public unsafe class EqdpEquipHook : FastHook, IDisposabl public EqdpEquipHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetEqdpEquipEntry", "E8 ?? ?? ?? ?? 85 DB 75 ?? F6 45", Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetEqdpEquipEntry", Sigs.GetEqdpEquipEntry, Detour, metaState.Config.EnableMods); _metaState.Config.ModsEnabled += Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/EqpHook.cs b/Penumbra/Interop/Hooks/Meta/EqpHook.cs index e96d8115..19b870b0 100644 --- a/Penumbra/Interop/Hooks/Meta/EqpHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqpHook.cs @@ -1,5 +1,6 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; +using Penumbra.GameData; using Penumbra.GameData.Structs; using Penumbra.Interop.PathResolving; @@ -14,7 +15,7 @@ public unsafe class EqpHook : FastHook, IDisposable public EqpHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetEqpFlags", "E8 ?? ?? ?? ?? 0F B6 44 24 ?? C0 E8", Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetEqpFlags", Sigs.GetEqpEntry, Detour, metaState.Config.EnableMods); _metaState.Config.ModsEnabled += Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/EstHook.cs b/Penumbra/Interop/Hooks/Meta/EstHook.cs index fa0bb3c5..3fc7080f 100644 --- a/Penumbra/Interop/Hooks/Meta/EstHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EstHook.cs @@ -1,4 +1,5 @@ using OtterGui.Services; +using Penumbra.GameData; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.PathResolving; @@ -15,7 +16,7 @@ public class EstHook : FastHook, IDisposable public EstHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetEstEntry", "44 8B C9 83 EA ?? 74", Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetEstEntry", Sigs.GetEstEntry, Detour, metaState.Config.EnableMods); _metaState.Config.ModsEnabled += Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/GmpHook.cs b/Penumbra/Interop/Hooks/Meta/GmpHook.cs index 44d35f12..72c8d075 100644 --- a/Penumbra/Interop/Hooks/Meta/GmpHook.cs +++ b/Penumbra/Interop/Hooks/Meta/GmpHook.cs @@ -1,4 +1,5 @@ using OtterGui.Services; +using Penumbra.GameData; using Penumbra.Interop.PathResolving; using Penumbra.Meta.Files; using Penumbra.Meta.Manipulations; @@ -16,7 +17,7 @@ public unsafe class GmpHook : FastHook, IDisposable public GmpHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetGmpEntry", "E8 ?? ?? ?? ?? 48 85 C0 74 ?? 43 8D 0C", Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetGmpEntry", Sigs.GetGmpEntry, Detour, metaState.Config.EnableMods); _metaState.Config.ModsEnabled += Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/RspBustHook.cs b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs index db22d90c..d1019d3e 100644 --- a/Penumbra/Interop/Hooks/Meta/RspBustHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs @@ -1,4 +1,5 @@ using OtterGui.Services; +using Penumbra.GameData; using Penumbra.GameData.Enums; using Penumbra.Interop.PathResolving; using Penumbra.Meta; @@ -19,7 +20,7 @@ public unsafe class RspBustHook : FastHook, IDisposable { _metaState = metaState; _metaFileManager = metaFileManager; - Task = hooks.CreateHook("GetRspBust", "E8 ?? ?? ?? ?? F2 0F 10 44 24 ?? 8B 44 24", Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetRspBust", Sigs.GetRspBust, Detour, metaState.Config.EnableMods); _metaState.Config.ModsEnabled += Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs index 98e39061..d54fe31e 100644 --- a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs @@ -1,4 +1,5 @@ using OtterGui.Services; +using Penumbra.GameData; using Penumbra.GameData.Enums; using Penumbra.Interop.PathResolving; using Penumbra.Meta; @@ -18,7 +19,7 @@ public class RspHeightHook : FastHook, IDisposable { _metaState = metaState; _metaFileManager = metaFileManager; - Task = hooks.CreateHook("GetRspHeight", "E8 ?? ?? ?? ?? 48 8B 8E ?? ?? ?? ?? 44 8B CF", Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetRspHeight", Sigs.GetRspHeight, Detour, metaState.Config.EnableMods); _metaState.Config.ModsEnabled += Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/RspTailHook.cs b/Penumbra/Interop/Hooks/Meta/RspTailHook.cs index 8d333575..8aa7ea9f 100644 --- a/Penumbra/Interop/Hooks/Meta/RspTailHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspTailHook.cs @@ -1,4 +1,5 @@ using OtterGui.Services; +using Penumbra.GameData; using Penumbra.GameData.Enums; using Penumbra.Interop.PathResolving; using Penumbra.Meta; @@ -18,7 +19,7 @@ public class RspTailHook : FastHook, IDisposable { _metaState = metaState; _metaFileManager = metaFileManager; - Task = hooks.CreateHook("GetRspTail", "E8 ?? ?? ?? ?? 0F 28 F0 48 8B 05", Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetRspTail", Sigs.GetRspTail, Detour, metaState.Config.EnableMods); _metaState.Config.ModsEnabled += Toggle; } From 221b18751d092a8138d5e8087cf6d9143e47f25f Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 2 Jul 2024 17:08:27 +0200 Subject: [PATCH 268/433] Some updates. --- OtterGui | 2 +- Penumbra.Api | 2 +- Penumbra.GameData | 2 +- Penumbra.String | 2 +- Penumbra/Api/Api/RedrawApi.cs | 2 +- Penumbra/Api/Api/ResourceTreeApi.cs | 6 +- Penumbra/Api/IpcProviders.cs | 2 +- .../Api/IpcTester/CollectionsIpcTester.cs | 2 +- Penumbra/Api/IpcTester/EditingIpcTester.cs | 2 +- Penumbra/Api/IpcTester/GameStateIpcTester.cs | 7 +- Penumbra/Api/IpcTester/MetaIpcTester.cs | 2 +- .../Api/IpcTester/ModSettingsIpcTester.cs | 4 +- Penumbra/Api/IpcTester/ModsIpcTester.cs | 4 +- .../Api/IpcTester/PluginStateIpcTester.cs | 4 +- Penumbra/Api/IpcTester/RedrawingIpcTester.cs | 4 +- Penumbra/Api/IpcTester/ResolveIpcTester.cs | 2 +- .../Api/IpcTester/ResourceTreeIpcTester.cs | 2 +- Penumbra/Api/IpcTester/TemporaryIpcTester.cs | 2 +- Penumbra/Api/IpcTester/UiIpcTester.cs | 4 +- .../Manager/ActiveCollectionMigration.cs | 2 +- .../Collections/Manager/ActiveCollections.cs | 2 +- .../Collections/Manager/CollectionStorage.cs | 2 +- .../Manager/IndividualCollections.Access.cs | 2 +- .../Manager/IndividualCollections.Files.cs | 2 +- .../Collections/Manager/InheritanceManager.cs | 2 +- Penumbra/Configuration.cs | 2 +- Penumbra/EphemeralConfig.cs | 2 +- Penumbra/Import/Models/HavokConverter.cs | 5 +- Penumbra/Import/Textures/BaseImage.cs | 2 +- Penumbra/Import/Textures/TexFileParser.cs | 16 ++-- Penumbra/Import/Textures/Texture.cs | 2 +- Penumbra/Import/Textures/TextureDrawer.cs | 2 +- Penumbra/Import/Textures/TextureManager.cs | 8 +- .../Animation/ApricotListenerSoundPlay.cs | 2 +- .../Animation/CharacterBaseLoadAnimation.cs | 2 +- Penumbra/Interop/Hooks/Animation/Dismount.cs | 2 +- .../Interop/Hooks/Animation/LoadAreaVfx.cs | 2 +- .../Hooks/Animation/LoadCharacterSound.cs | 9 +- .../Hooks/Animation/LoadCharacterVfx.cs | 2 +- .../Hooks/Animation/LoadTimelineResources.cs | 2 +- .../Interop/Hooks/Animation/PlayFootstep.cs | 2 +- .../Hooks/Animation/ScheduleClipUpdate.cs | 2 +- .../Interop/Hooks/Animation/SomeActionLoad.cs | 10 +- .../Hooks/Animation/SomeMountAnimation.cs | 2 +- .../Interop/Hooks/Animation/SomePapLoad.cs | 2 +- .../Hooks/Animation/SomeParasolAnimation.cs | 2 +- Penumbra/Interop/Hooks/HookSettings.cs | 8 ++ .../Interop/Hooks/Meta/CalculateHeight.cs | 3 +- .../Interop/Hooks/Meta/ChangeCustomize.cs | 2 +- .../Interop/Hooks/Meta/EqdpAccessoryHook.cs | 2 +- Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs | 2 +- Penumbra/Interop/Hooks/Meta/EqpHook.cs | 2 +- Penumbra/Interop/Hooks/Meta/EstHook.cs | 2 +- Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs | 2 +- .../Interop/Hooks/Meta/GetEqpIndirect2.cs | 2 +- Penumbra/Interop/Hooks/Meta/GmpHook.cs | 2 +- .../Interop/Hooks/Meta/ModelLoadComplete.cs | 3 +- Penumbra/Interop/Hooks/Meta/RspBustHook.cs | 2 +- Penumbra/Interop/Hooks/Meta/RspHeightHook.cs | 2 +- .../Interop/Hooks/Meta/RspSetupCharacter.cs | 2 +- Penumbra/Interop/Hooks/Meta/RspTailHook.cs | 2 +- Penumbra/Interop/Hooks/Meta/SetupVisor.cs | 2 +- Penumbra/Interop/Hooks/Meta/UpdateModel.cs | 2 +- .../Interop/Hooks/Objects/CopyCharacter.cs | 6 +- .../Interop/Hooks/Objects/WeaponReload.cs | 2 +- .../Hooks/Resources/ResolvePathHooksBase.cs | 26 ++--- .../PathResolving/CollectionResolver.cs | 4 +- .../Interop/PathResolving/CutsceneService.cs | 5 +- .../ResourceLoading/FileReadService.cs | 4 +- .../ResourceLoading/ResourceManagerService.cs | 33 ++----- .../ResourceLoading/ResourceService.cs | 2 +- .../Interop/ResourceLoading/TexMdlService.cs | 96 +++++++++++-------- .../Interop/ResourceTree/ResolveContext.cs | 2 +- Penumbra/Interop/ResourceTree/ResourceTree.cs | 2 +- .../ResourceTree/ResourceTreeApiHelper.cs | 6 +- .../ResourceTree/ResourceTreeFactory.cs | 21 ++-- .../Interop/ResourceTree/TreeBuildCache.cs | 20 ++-- Penumbra/Interop/Services/FontReloader.cs | 4 +- Penumbra/Interop/Services/RedrawService.cs | 59 ++++++------ Penumbra/Interop/Structs/ClipScheduler.cs | 4 +- Penumbra/Interop/Structs/ResourceHandle.cs | 8 +- Penumbra/Meta/Files/MetaBaseFile.cs | 2 +- Penumbra/Mods/Editor/ModMerger.cs | 2 +- Penumbra/Mods/Editor/ModNormalizer.cs | 2 +- Penumbra/Mods/Groups/ImcModGroup.cs | 2 +- Penumbra/Mods/Groups/MultiModGroup.cs | 3 +- Penumbra/Mods/Manager/ModImportManager.cs | 2 +- .../Manager/OptionEditor/ModGroupEditor.cs | 2 +- Penumbra/Mods/ModCreator.cs | 2 +- Penumbra/Penumbra.cs | 4 +- Penumbra/Penumbra.csproj | 1 + Penumbra/Penumbra.json | 2 +- Penumbra/Services/DalamudConfigService.cs | 6 +- Penumbra/Services/FilenameService.cs | 2 +- Penumbra/Services/MessageService.cs | 4 +- Penumbra/Services/StaticServiceManager.cs | 4 +- Penumbra/Services/ValidityChecker.cs | 12 +-- Penumbra/UI/AdvancedWindow/FileEditor.cs | 2 +- Penumbra/UI/AdvancedWindow/ItemSwapTab.cs | 2 +- .../ModEditWindow.Materials.MtrlTab.cs | 2 +- .../ModEditWindow.Materials.Shpk.cs | 2 +- .../ModEditWindow.ShaderPackages.cs | 2 +- Penumbra/UI/ChangedItemDrawer.cs | 29 +++--- Penumbra/UI/CollectionTab/CollectionPanel.cs | 8 +- Penumbra/UI/ConfigWindow.cs | 4 +- Penumbra/UI/LaunchButton.cs | 25 ++--- .../UI/ModsTab/Groups/ModGroupEditDrawer.cs | 2 +- Penumbra/UI/ModsTab/ModFileSystemSelector.cs | 2 +- Penumbra/UI/ModsTab/ModPanel.cs | 2 +- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 2 +- Penumbra/UI/ModsTab/ModPanelHeader.cs | 2 +- Penumbra/UI/PredefinedTagManager.cs | 2 +- .../UI/ResourceWatcher/ResourceWatcher.cs | 2 +- Penumbra/UI/Tabs/CollectionsTab.cs | 2 +- Penumbra/UI/Tabs/Debug/DebugTab.cs | 11 +-- Penumbra/UI/Tabs/ModsTab.cs | 2 +- Penumbra/UI/Tabs/ResourceTab.cs | 2 +- Penumbra/UI/Tabs/SettingsTab.cs | 4 +- Penumbra/UI/UiHelpers.cs | 2 +- Penumbra/UI/WindowSystem.cs | 4 +- repo.json | 2 +- 121 files changed, 338 insertions(+), 328 deletions(-) create mode 100644 Penumbra/Interop/Hooks/HookSettings.cs diff --git a/OtterGui b/OtterGui index 6fafc03b..437ef65c 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 6fafc03b34971be7c0e74fd9a638d1ed642ea19a +Subproject commit 437ef65c6464c54c8f40196dd2428da901d73aab diff --git a/Penumbra.Api b/Penumbra.Api index f1e4e520..43b0b47f 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit f1e4e520daaa8f23e5c8b71d55e5992b8f6768e2 +Subproject commit 43b0b47f2d019af0fe4681dfc578f9232e3ba90c diff --git a/Penumbra.GameData b/Penumbra.GameData index 4b55c05c..3a97e5ae 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 4b55c05c72eb194bec0d28c52cf076962010424b +Subproject commit 3a97e5aeee3b7375b333c1add5305d0ce80cbf83 diff --git a/Penumbra.String b/Penumbra.String index caa58c5c..f04abbab 160000 --- a/Penumbra.String +++ b/Penumbra.String @@ -1 +1 @@ -Subproject commit caa58c5c92710e69ce07b9d736ebe2d228cb4488 +Subproject commit f04abbabedf5e757c5cbb970f3e513fef23e53cf diff --git a/Penumbra/Api/Api/RedrawApi.cs b/Penumbra/Api/Api/RedrawApi.cs index 03b42493..82d14f7b 100644 --- a/Penumbra/Api/Api/RedrawApi.cs +++ b/Penumbra/Api/Api/RedrawApi.cs @@ -13,7 +13,7 @@ public class RedrawApi(RedrawService redrawService) : IPenumbraApiRedraw, IApiSe public void RedrawObject(string name, RedrawType setting) => redrawService.RedrawObject(name, setting); - public void RedrawObject(GameObject? gameObject, RedrawType setting) + public void RedrawObject(IGameObject? gameObject, RedrawType setting) => redrawService.RedrawObject(gameObject, setting); public void RedrawAll(RedrawType setting) diff --git a/Penumbra/Api/Api/ResourceTreeApi.cs b/Penumbra/Api/Api/ResourceTreeApi.cs index 6e9aaa48..dcec99bf 100644 --- a/Penumbra/Api/Api/ResourceTreeApi.cs +++ b/Penumbra/Api/Api/ResourceTreeApi.cs @@ -12,7 +12,7 @@ public class ResourceTreeApi(ResourceTreeFactory resourceTreeFactory, ObjectMana { public Dictionary>?[] GetGameObjectResourcePaths(params ushort[] gameObjects) { - var characters = gameObjects.Select(index => objects.GetDalamudObject((int)index)).OfType(); + var characters = gameObjects.Select(index => objects.GetDalamudObject((int)index)).OfType(); var resourceTrees = resourceTreeFactory.FromCharacters(characters, 0); var pathDictionaries = ResourceTreeApiHelper.GetResourcePathDictionaries(resourceTrees); @@ -28,7 +28,7 @@ public class ResourceTreeApi(ResourceTreeFactory resourceTreeFactory, ObjectMana public GameResourceDict?[] GetGameObjectResourcesOfType(ResourceType type, bool withUiData, params ushort[] gameObjects) { - var characters = gameObjects.Select(index => objects.GetDalamudObject((int)index)).OfType(); + var characters = gameObjects.Select(index => objects.GetDalamudObject((int)index)).OfType(); var resourceTrees = resourceTreeFactory.FromCharacters(characters, withUiData ? ResourceTreeFactory.Flags.WithUiData : 0); var resDictionaries = ResourceTreeApiHelper.GetResourcesOfType(resourceTrees, type); @@ -45,7 +45,7 @@ public class ResourceTreeApi(ResourceTreeFactory resourceTreeFactory, ObjectMana public JObject?[] GetGameObjectResourceTrees(bool withUiData, params ushort[] gameObjects) { - var characters = gameObjects.Select(index => objects.GetDalamudObject((int)index)).OfType(); + var characters = gameObjects.Select(index => objects.GetDalamudObject((int)index)).OfType(); var resourceTrees = resourceTreeFactory.FromCharacters(characters, withUiData ? ResourceTreeFactory.Flags.WithUiData : 0); var resDictionary = ResourceTreeApiHelper.EncapsulateResourceTrees(resourceTrees); diff --git a/Penumbra/Api/IpcProviders.cs b/Penumbra/Api/IpcProviders.cs index 6b146c39..861225fa 100644 --- a/Penumbra/Api/IpcProviders.cs +++ b/Penumbra/Api/IpcProviders.cs @@ -12,7 +12,7 @@ public sealed class IpcProviders : IDisposable, IApiService private readonly EventProvider _disposedProvider; private readonly EventProvider _initializedProvider; - public IpcProviders(DalamudPluginInterface pi, IPenumbraApi api) + public IpcProviders(IDalamudPluginInterface pi, IPenumbraApi api) { _disposedProvider = IpcSubscribers.Disposed.Provider(pi); _initializedProvider = IpcSubscribers.Initialized.Provider(pi); diff --git a/Penumbra/Api/IpcTester/CollectionsIpcTester.cs b/Penumbra/Api/IpcTester/CollectionsIpcTester.cs index 2679bc69..026fabbc 100644 --- a/Penumbra/Api/IpcTester/CollectionsIpcTester.cs +++ b/Penumbra/Api/IpcTester/CollectionsIpcTester.cs @@ -13,7 +13,7 @@ using ImGuiClip = OtterGui.ImGuiClip; namespace Penumbra.Api.IpcTester; -public class CollectionsIpcTester(DalamudPluginInterface pi) : IUiService +public class CollectionsIpcTester(IDalamudPluginInterface pi) : IUiService { private int _objectIdx; private string _collectionIdString = string.Empty; diff --git a/Penumbra/Api/IpcTester/EditingIpcTester.cs b/Penumbra/Api/IpcTester/EditingIpcTester.cs index 94b1e4e8..a1001630 100644 --- a/Penumbra/Api/IpcTester/EditingIpcTester.cs +++ b/Penumbra/Api/IpcTester/EditingIpcTester.cs @@ -8,7 +8,7 @@ using Penumbra.Api.IpcSubscribers; namespace Penumbra.Api.IpcTester; -public class EditingIpcTester(DalamudPluginInterface pi) : IUiService +public class EditingIpcTester(IDalamudPluginInterface pi) : IUiService { private string _inputPath = string.Empty; private string _inputPath2 = string.Empty; diff --git a/Penumbra/Api/IpcTester/GameStateIpcTester.cs b/Penumbra/Api/IpcTester/GameStateIpcTester.cs index 93806162..04541a57 100644 --- a/Penumbra/Api/IpcTester/GameStateIpcTester.cs +++ b/Penumbra/Api/IpcTester/GameStateIpcTester.cs @@ -12,7 +12,7 @@ namespace Penumbra.Api.IpcTester; public class GameStateIpcTester : IUiService, IDisposable { - private readonly DalamudPluginInterface _pi; + private readonly IDalamudPluginInterface _pi; public readonly EventSubscriber CharacterBaseCreating; public readonly EventSubscriber CharacterBaseCreated; public readonly EventSubscriber GameObjectResourcePathResolved; @@ -30,7 +30,7 @@ public class GameStateIpcTester : IUiService, IDisposable private int _currentCutsceneParent; private PenumbraApiEc _cutsceneError = PenumbraApiEc.Success; - public GameStateIpcTester(DalamudPluginInterface pi) + public GameStateIpcTester(IDalamudPluginInterface pi) { _pi = pi; CharacterBaseCreating = IpcSubscribers.CreatingCharacterBase.Subscriber(pi, UpdateLastCreated); @@ -134,7 +134,6 @@ public class GameStateIpcTester : IUiService, IDisposable private static unsafe string GetObjectName(nint gameObject) { var obj = (FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject*)gameObject; - var name = obj != null ? obj->Name : null; - return name != null && *name != 0 ? new ByteString(name).ToString() : "Unknown"; + return obj != null && obj->Name[0] != 0 ? new ByteString(obj->Name).ToString() : "Unknown"; } } diff --git a/Penumbra/Api/IpcTester/MetaIpcTester.cs b/Penumbra/Api/IpcTester/MetaIpcTester.cs index 3fa7de7f..8b393ade 100644 --- a/Penumbra/Api/IpcTester/MetaIpcTester.cs +++ b/Penumbra/Api/IpcTester/MetaIpcTester.cs @@ -6,7 +6,7 @@ using Penumbra.Api.IpcSubscribers; namespace Penumbra.Api.IpcTester; -public class MetaIpcTester(DalamudPluginInterface pi) : IUiService +public class MetaIpcTester(IDalamudPluginInterface pi) : IUiService { private int _gameObjectIndex; diff --git a/Penumbra/Api/IpcTester/ModSettingsIpcTester.cs b/Penumbra/Api/IpcTester/ModSettingsIpcTester.cs index b117d603..23078576 100644 --- a/Penumbra/Api/IpcTester/ModSettingsIpcTester.cs +++ b/Penumbra/Api/IpcTester/ModSettingsIpcTester.cs @@ -12,7 +12,7 @@ namespace Penumbra.Api.IpcTester; public class ModSettingsIpcTester : IUiService, IDisposable { - private readonly DalamudPluginInterface _pi; + private readonly IDalamudPluginInterface _pi; public readonly EventSubscriber SettingChanged; private PenumbraApiEc _lastSettingsError = PenumbraApiEc.Success; @@ -33,7 +33,7 @@ public class ModSettingsIpcTester : IUiService, IDisposable private IReadOnlyDictionary? _availableSettings; private Dictionary>? _currentSettings; - public ModSettingsIpcTester(DalamudPluginInterface pi) + public ModSettingsIpcTester(IDalamudPluginInterface pi) { _pi = pi; SettingChanged = ModSettingChanged.Subscriber(pi, UpdateLastModSetting); diff --git a/Penumbra/Api/IpcTester/ModsIpcTester.cs b/Penumbra/Api/IpcTester/ModsIpcTester.cs index 2be51a80..a24861a3 100644 --- a/Penumbra/Api/IpcTester/ModsIpcTester.cs +++ b/Penumbra/Api/IpcTester/ModsIpcTester.cs @@ -12,7 +12,7 @@ namespace Penumbra.Api.IpcTester; public class ModsIpcTester : IUiService, IDisposable { - private readonly DalamudPluginInterface _pi; + private readonly IDalamudPluginInterface _pi; private string _modDirectory = string.Empty; private string _modName = string.Empty; @@ -38,7 +38,7 @@ public class ModsIpcTester : IUiService, IDisposable private string _lastMovedModFrom = string.Empty; private string _lastMovedModTo = string.Empty; - public ModsIpcTester(DalamudPluginInterface pi) + public ModsIpcTester(IDalamudPluginInterface pi) { _pi = pi; DeleteSubscriber = ModDeleted.Subscriber(pi, s => diff --git a/Penumbra/Api/IpcTester/PluginStateIpcTester.cs b/Penumbra/Api/IpcTester/PluginStateIpcTester.cs index 984f17b1..df82033d 100644 --- a/Penumbra/Api/IpcTester/PluginStateIpcTester.cs +++ b/Penumbra/Api/IpcTester/PluginStateIpcTester.cs @@ -12,7 +12,7 @@ namespace Penumbra.Api.IpcTester; public class PluginStateIpcTester : IUiService, IDisposable { - private readonly DalamudPluginInterface _pi; + private readonly IDalamudPluginInterface _pi; public readonly EventSubscriber ModDirectoryChanged; public readonly EventSubscriber Initialized; public readonly EventSubscriber Disposed; @@ -29,7 +29,7 @@ public class PluginStateIpcTester : IUiService, IDisposable private DateTimeOffset _lastEnabledChange = DateTimeOffset.UnixEpoch; private bool? _lastEnabledValue; - public PluginStateIpcTester(DalamudPluginInterface pi) + public PluginStateIpcTester(IDalamudPluginInterface pi) { _pi = pi; ModDirectoryChanged = IpcSubscribers.ModDirectoryChanged.Subscriber(pi, UpdateModDirectoryChanged); diff --git a/Penumbra/Api/IpcTester/RedrawingIpcTester.cs b/Penumbra/Api/IpcTester/RedrawingIpcTester.cs index 801f0b97..b862dde5 100644 --- a/Penumbra/Api/IpcTester/RedrawingIpcTester.cs +++ b/Penumbra/Api/IpcTester/RedrawingIpcTester.cs @@ -13,14 +13,14 @@ namespace Penumbra.Api.IpcTester; public class RedrawingIpcTester : IUiService, IDisposable { - private readonly DalamudPluginInterface _pi; + private readonly IDalamudPluginInterface _pi; private readonly ObjectManager _objects; public readonly EventSubscriber Redrawn; private int _redrawIndex; private string _lastRedrawnString = "None"; - public RedrawingIpcTester(DalamudPluginInterface pi, ObjectManager objects) + public RedrawingIpcTester(IDalamudPluginInterface pi, ObjectManager objects) { _pi = pi; _objects = objects; diff --git a/Penumbra/Api/IpcTester/ResolveIpcTester.cs b/Penumbra/Api/IpcTester/ResolveIpcTester.cs index 978ed8d6..a79b099d 100644 --- a/Penumbra/Api/IpcTester/ResolveIpcTester.cs +++ b/Penumbra/Api/IpcTester/ResolveIpcTester.cs @@ -7,7 +7,7 @@ using Penumbra.String.Classes; namespace Penumbra.Api.IpcTester; -public class ResolveIpcTester(DalamudPluginInterface pi) : IUiService +public class ResolveIpcTester(IDalamudPluginInterface pi) : IUiService { private string _currentResolvePath = string.Empty; private string _currentReversePath = string.Empty; diff --git a/Penumbra/Api/IpcTester/ResourceTreeIpcTester.cs b/Penumbra/Api/IpcTester/ResourceTreeIpcTester.cs index 1f57fc9d..088a77bd 100644 --- a/Penumbra/Api/IpcTester/ResourceTreeIpcTester.cs +++ b/Penumbra/Api/IpcTester/ResourceTreeIpcTester.cs @@ -15,7 +15,7 @@ using Penumbra.GameData.Structs; namespace Penumbra.Api.IpcTester; -public class ResourceTreeIpcTester(DalamudPluginInterface pi, ObjectManager objects) : IUiService +public class ResourceTreeIpcTester(IDalamudPluginInterface pi, ObjectManager objects) : IUiService { private readonly Stopwatch _stopwatch = new(); diff --git a/Penumbra/Api/IpcTester/TemporaryIpcTester.cs b/Penumbra/Api/IpcTester/TemporaryIpcTester.cs index 0aa6821c..6d4f17b2 100644 --- a/Penumbra/Api/IpcTester/TemporaryIpcTester.cs +++ b/Penumbra/Api/IpcTester/TemporaryIpcTester.cs @@ -17,7 +17,7 @@ using Penumbra.Services; namespace Penumbra.Api.IpcTester; public class TemporaryIpcTester( - DalamudPluginInterface pi, + IDalamudPluginInterface pi, ModManager modManager, CollectionManager collections, TempModManager tempMods, diff --git a/Penumbra/Api/IpcTester/UiIpcTester.cs b/Penumbra/Api/IpcTester/UiIpcTester.cs index a2c36938..647a4dda 100644 --- a/Penumbra/Api/IpcTester/UiIpcTester.cs +++ b/Penumbra/Api/IpcTester/UiIpcTester.cs @@ -10,7 +10,7 @@ namespace Penumbra.Api.IpcTester; public class UiIpcTester : IUiService, IDisposable { - private readonly DalamudPluginInterface _pi; + private readonly IDalamudPluginInterface _pi; public readonly EventSubscriber PreSettingsTabBar; public readonly EventSubscriber PreSettingsPanel; public readonly EventSubscriber PostEnabled; @@ -28,7 +28,7 @@ public class UiIpcTester : IUiService, IDisposable private string _modName = string.Empty; private PenumbraApiEc _ec = PenumbraApiEc.Success; - public UiIpcTester(DalamudPluginInterface pi) + public UiIpcTester(IDalamudPluginInterface pi) { _pi = pi; PreSettingsTabBar = IpcSubscribers.PreSettingsTabBarDraw.Subscriber(pi, UpdateLastDrawnMod); diff --git a/Penumbra/Collections/Manager/ActiveCollectionMigration.cs b/Penumbra/Collections/Manager/ActiveCollectionMigration.cs index 2f9e9b15..19f781fc 100644 --- a/Penumbra/Collections/Manager/ActiveCollectionMigration.cs +++ b/Penumbra/Collections/Manager/ActiveCollectionMigration.cs @@ -1,4 +1,4 @@ -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui.Classes; diff --git a/Penumbra/Collections/Manager/ActiveCollections.cs b/Penumbra/Collections/Manager/ActiveCollections.cs index 6d48f74b..60f9a427 100644 --- a/Penumbra/Collections/Manager/ActiveCollections.cs +++ b/Penumbra/Collections/Manager/ActiveCollections.cs @@ -1,4 +1,4 @@ -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui; diff --git a/Penumbra/Collections/Manager/CollectionStorage.cs b/Penumbra/Collections/Manager/CollectionStorage.cs index cd680d36..a326fb92 100644 --- a/Penumbra/Collections/Manager/CollectionStorage.cs +++ b/Penumbra/Collections/Manager/CollectionStorage.cs @@ -1,4 +1,4 @@ -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using OtterGui; using OtterGui.Classes; using OtterGui.Services; diff --git a/Penumbra/Collections/Manager/IndividualCollections.Access.cs b/Penumbra/Collections/Manager/IndividualCollections.Access.cs index 785f0013..6b90a333 100644 --- a/Penumbra/Collections/Manager/IndividualCollections.Access.cs +++ b/Penumbra/Collections/Manager/IndividualCollections.Access.cs @@ -127,7 +127,7 @@ public sealed partial class IndividualCollections : IReadOnlyList<(string Displa } } - public bool TryGetCollection(GameObject? gameObject, out ModCollection? collection) + public bool TryGetCollection(IGameObject? gameObject, out ModCollection? collection) => TryGetCollection(_actors.FromObject(gameObject, true, false, false), out collection); public unsafe bool TryGetCollection(FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject* gameObject, out ModCollection? collection) diff --git a/Penumbra/Collections/Manager/IndividualCollections.Files.cs b/Penumbra/Collections/Manager/IndividualCollections.Files.cs index 8a717b35..f7a26384 100644 --- a/Penumbra/Collections/Manager/IndividualCollections.Files.cs +++ b/Penumbra/Collections/Manager/IndividualCollections.Files.cs @@ -1,5 +1,5 @@ using Dalamud.Game.ClientState.Objects.Enums; -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using Newtonsoft.Json.Linq; using OtterGui.Classes; using Penumbra.GameData.Actors; diff --git a/Penumbra/Collections/Manager/InheritanceManager.cs b/Penumbra/Collections/Manager/InheritanceManager.cs index f3482cdf..bc1a362c 100644 --- a/Penumbra/Collections/Manager/InheritanceManager.cs +++ b/Penumbra/Collections/Manager/InheritanceManager.cs @@ -1,4 +1,4 @@ -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using OtterGui; using OtterGui.Classes; using OtterGui.Filesystem; diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index 7faed6a2..49aecfdc 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -1,5 +1,5 @@ using Dalamud.Configuration; -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using Newtonsoft.Json; using OtterGui; using OtterGui.Classes; diff --git a/Penumbra/EphemeralConfig.cs b/Penumbra/EphemeralConfig.cs index 52e276c7..7457c910 100644 --- a/Penumbra/EphemeralConfig.cs +++ b/Penumbra/EphemeralConfig.cs @@ -1,4 +1,4 @@ -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using Newtonsoft.Json; using OtterGui.Classes; using OtterGui.Services; diff --git a/Penumbra/Import/Models/HavokConverter.cs b/Penumbra/Import/Models/HavokConverter.cs index dc9d3e6a..e3797083 100644 --- a/Penumbra/Import/Models/HavokConverter.cs +++ b/Penumbra/Import/Models/HavokConverter.cs @@ -1,4 +1,7 @@ -using FFXIVClientStructs.Havok; +using FFXIVClientStructs.Havok.Common.Base.System.IO.OStream; +using FFXIVClientStructs.Havok.Common.Base.Types; +using FFXIVClientStructs.Havok.Common.Serialize.Resource; +using FFXIVClientStructs.Havok.Common.Serialize.Util; namespace Penumbra.Import.Models; diff --git a/Penumbra/Import/Textures/BaseImage.cs b/Penumbra/Import/Textures/BaseImage.cs index a4a0e203..eba2d8ba 100644 --- a/Penumbra/Import/Textures/BaseImage.cs +++ b/Penumbra/Import/Textures/BaseImage.cs @@ -103,7 +103,7 @@ public readonly struct BaseImage : IDisposable { null => 0, ScratchImage s => s.Meta.MipLevels, - TexFile t => t.Header.MipLevelsCount, + TexFile t => t.Header.MipCount, _ => 1, }; } diff --git a/Penumbra/Import/Textures/TexFileParser.cs b/Penumbra/Import/Textures/TexFileParser.cs index 6f854022..09025b61 100644 --- a/Penumbra/Import/Textures/TexFileParser.cs +++ b/Penumbra/Import/Textures/TexFileParser.cs @@ -79,8 +79,8 @@ public static class TexFileParser w.Write(header.Width); w.Write(header.Height); w.Write(header.Depth); - w.Write(header.MipLevelsCount); - w.Write((byte)0); // TODO Lumina Update + w.Write(header.MipCount); + w.Write(header.MipUnknownFlag); // TODO Lumina Update unsafe { w.Write(header.LodOffset[0]); @@ -96,11 +96,11 @@ public static class TexFileParser var meta = scratch.Meta; var ret = new TexFile.TexHeader() { - Height = (ushort)meta.Height, - Width = (ushort)meta.Width, - Depth = (ushort)Math.Max(meta.Depth, 1), - MipLevelsCount = (byte)Math.Min(meta.MipLevels, 13), - Format = meta.Format.ToTexFormat(), + Height = (ushort)meta.Height, + Width = (ushort)meta.Width, + Depth = (ushort)Math.Max(meta.Depth, 1), + MipCount = (byte)Math.Min(meta.MipLevels, 13), + Format = meta.Format.ToTexFormat(), Type = meta.Dimension switch { _ when meta.IsCubeMap => TexFile.Attribute.TextureTypeCube, @@ -143,7 +143,7 @@ public static class TexFileParser Height = header.Height, Width = header.Width, Depth = Math.Max(header.Depth, (ushort)1), - MipLevels = header.MipLevelsCount, + MipLevels = header.MipCount, ArraySize = 1, Format = header.Format.ToDXGI(), Dimension = header.Type.ToDimension(), diff --git a/Penumbra/Import/Textures/Texture.cs b/Penumbra/Import/Textures/Texture.cs index c4d6dc56..c5207e94 100644 --- a/Penumbra/Import/Textures/Texture.cs +++ b/Penumbra/Import/Textures/Texture.cs @@ -1,4 +1,4 @@ -using Dalamud.Interface.Internal; +using Dalamud.Interface.Textures.TextureWraps; using OtterTex; namespace Penumbra.Import.Textures; diff --git a/Penumbra/Import/Textures/TextureDrawer.cs b/Penumbra/Import/Textures/TextureDrawer.cs index 427db92d..bd95d1ab 100644 --- a/Penumbra/Import/Textures/TextureDrawer.cs +++ b/Penumbra/Import/Textures/TextureDrawer.cs @@ -105,7 +105,7 @@ public static class TextureDrawer ImGuiUtil.DrawTableColumn("Format"); ImGuiUtil.DrawTableColumn(t.Header.Format.ToString()); ImGuiUtil.DrawTableColumn("Mip Levels"); - ImGuiUtil.DrawTableColumn(t.Header.MipLevelsCount.ToString()); + ImGuiUtil.DrawTableColumn(t.Header.MipCount.ToString()); ImGuiUtil.DrawTableColumn("Data Size"); ImGuiUtil.DrawTableColumn($"{Functions.HumanReadableSize(t.ImageData.Length)} ({t.ImageData.Length} Bytes)"); break; diff --git a/Penumbra/Import/Textures/TextureManager.cs b/Penumbra/Import/Textures/TextureManager.cs index 4aa64209..cc785d02 100644 --- a/Penumbra/Import/Textures/TextureManager.cs +++ b/Penumbra/Import/Textures/TextureManager.cs @@ -1,5 +1,5 @@ -using Dalamud.Interface; -using Dalamud.Interface.Internal; +using Dalamud.Interface.Textures; +using Dalamud.Interface.Textures.TextureWraps; using Dalamud.Plugin.Services; using Lumina.Data.Files; using OtterGui.Log; @@ -13,7 +13,7 @@ using Image = SixLabors.ImageSharp.Image; namespace Penumbra.Import.Textures; -public sealed class TextureManager(UiBuilder uiBuilder, IDataManager gameData, Logger logger) +public sealed class TextureManager(IDataManager gameData, Logger logger, ITextureProvider textureProvider) : SingleTaskQueue, IDisposable, IService { private readonly Logger _logger = logger; @@ -211,7 +211,7 @@ public sealed class TextureManager(UiBuilder uiBuilder, IDataManager gameData, L /// Load a texture wrap for a given image. public IDalamudTextureWrap LoadTextureWrap(byte[] rgba, int width, int height) - => uiBuilder.LoadImageRaw(rgba, width, height, 4); + => textureProvider.CreateFromRaw(RawImageSpecification.Rgba32(width, height), rgba, "Penumbra.Texture"); /// Load any supported file from game data or drive depending on extension and if the path is rooted. public (BaseImage, TextureType) Load(string path) diff --git a/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs b/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs index 77927593..e58c7268 100644 --- a/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs +++ b/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs @@ -21,7 +21,7 @@ public sealed unsafe class ApricotListenerSoundPlay : FastHook("Apricot Listener Sound Play", Sigs.ApricotListenerSoundPlay, Detour, true); + Task = hooks.CreateHook("Apricot Listener Sound Play", Sigs.ApricotListenerSoundPlay, Detour, HookSettings.VfxIdentificationHooks); } public delegate nint Delegate(nint a1, nint a2, nint a3, nint a4, nint a5, nint a6); diff --git a/Penumbra/Interop/Hooks/Animation/CharacterBaseLoadAnimation.cs b/Penumbra/Interop/Hooks/Animation/CharacterBaseLoadAnimation.cs index df25358e..f99d8ca4 100644 --- a/Penumbra/Interop/Hooks/Animation/CharacterBaseLoadAnimation.cs +++ b/Penumbra/Interop/Hooks/Animation/CharacterBaseLoadAnimation.cs @@ -26,7 +26,7 @@ public sealed unsafe class CharacterBaseLoadAnimation : FastHook("CharacterBase Load Animation", Sigs.CharacterBaseLoadAnimation, Detour, true); + Task = hooks.CreateHook("CharacterBase Load Animation", Sigs.CharacterBaseLoadAnimation, Detour, HookSettings.VfxIdentificationHooks); } public delegate void Delegate(DrawObject* drawBase); diff --git a/Penumbra/Interop/Hooks/Animation/Dismount.cs b/Penumbra/Interop/Hooks/Animation/Dismount.cs index 8085bcdb..523e750c 100644 --- a/Penumbra/Interop/Hooks/Animation/Dismount.cs +++ b/Penumbra/Interop/Hooks/Animation/Dismount.cs @@ -15,7 +15,7 @@ public sealed unsafe class Dismount : FastHook { _state = state; _collectionResolver = collectionResolver; - Task = hooks.CreateHook("Dismount", Sigs.Dismount, Detour, true); + Task = hooks.CreateHook("Dismount", Sigs.Dismount, Detour, HookSettings.VfxIdentificationHooks); } public delegate void Delegate(nint a1, nint a2); diff --git a/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs b/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs index 1a78d3b4..0f51157c 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs @@ -20,7 +20,7 @@ public sealed unsafe class LoadAreaVfx : FastHook _state = state; _collectionResolver = collectionResolver; _crashHandler = crashHandler; - Task = hooks.CreateHook("Load Area VFX", Sigs.LoadAreaVfx, Detour, true); + Task = hooks.CreateHook("Load Area VFX", Sigs.LoadAreaVfx, Detour, HookSettings.VfxIdentificationHooks); } public delegate nint Delegate(uint vfxId, float* pos, GameObject* caster, float unk1, float unk2, byte unk3); diff --git a/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs b/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs index 98454a77..ed04880e 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs @@ -1,3 +1,4 @@ +using FFXIVClientStructs.FFXIV.Client.Game.Character; using FFXIVClientStructs.FFXIV.Client.Game.Object; using OtterGui.Services; using Penumbra.CrashHandler.Buffers; @@ -15,12 +16,10 @@ public sealed unsafe class LoadCharacterSound : FastHook("Load Character Sound", - (nint)FFXIVClientStructs.FFXIV.Client.Game.Character.Character.VfxContainer.MemberFunctionPointers.LoadCharacterSound, Detour, - true); + _crashHandler = crashHandler; + Task = hooks.CreateHook("Load Character Sound", (nint)VfxContainer.MemberFunctionPointers.LoadCharacterSound, Detour, HookSettings.VfxIdentificationHooks); } public delegate nint Delegate(nint container, int unk1, int unk2, nint unk3, ulong unk4, int unk5, int unk6, ulong unk7); diff --git a/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs b/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs index 77aaa742..af801345 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs @@ -26,7 +26,7 @@ public sealed unsafe class LoadCharacterVfx : FastHook("Load Character VFX", Sigs.LoadCharacterVfx, Detour, true); + Task = hooks.CreateHook("Load Character VFX", Sigs.LoadCharacterVfx, Detour, HookSettings.VfxIdentificationHooks); } public delegate nint Delegate(byte* vfxPath, VfxParams* vfxParams, byte unk1, byte unk2, float unk3, int unk4); diff --git a/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs b/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs index 018892a0..4e9037bd 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs @@ -30,7 +30,7 @@ public sealed unsafe class LoadTimelineResources : FastHook("Load Timeline Resources", Sigs.LoadTimelineResources, Detour, true); + Task = hooks.CreateHook("Load Timeline Resources", Sigs.LoadTimelineResources, Detour, HookSettings.VfxIdentificationHooks); } public delegate ulong Delegate(nint timeline); diff --git a/Penumbra/Interop/Hooks/Animation/PlayFootstep.cs b/Penumbra/Interop/Hooks/Animation/PlayFootstep.cs index 491d7662..e4a8c83c 100644 --- a/Penumbra/Interop/Hooks/Animation/PlayFootstep.cs +++ b/Penumbra/Interop/Hooks/Animation/PlayFootstep.cs @@ -14,7 +14,7 @@ public sealed unsafe class PlayFootstep : FastHook { _state = state; _collectionResolver = collectionResolver; - Task = hooks.CreateHook("Play Footstep", Sigs.FootStepSound, Detour, true); + Task = hooks.CreateHook("Play Footstep", Sigs.FootStepSound, Detour, HookSettings.VfxIdentificationHooks); } public delegate void Delegate(GameObject* gameObject, int id, int unk); diff --git a/Penumbra/Interop/Hooks/Animation/ScheduleClipUpdate.cs b/Penumbra/Interop/Hooks/Animation/ScheduleClipUpdate.cs index 342ffc25..645b3565 100644 --- a/Penumbra/Interop/Hooks/Animation/ScheduleClipUpdate.cs +++ b/Penumbra/Interop/Hooks/Animation/ScheduleClipUpdate.cs @@ -23,7 +23,7 @@ public sealed unsafe class ScheduleClipUpdate : FastHook("Schedule Clip Update", Sigs.ScheduleClipUpdate, Detour, true); + Task = hooks.CreateHook("Schedule Clip Update", Sigs.ScheduleClipUpdate, Detour, HookSettings.VfxIdentificationHooks); } public delegate void Delegate(ClipScheduler* x); diff --git a/Penumbra/Interop/Hooks/Animation/SomeActionLoad.cs b/Penumbra/Interop/Hooks/Animation/SomeActionLoad.cs index 6de3aeb0..1f3c0e3b 100644 --- a/Penumbra/Interop/Hooks/Animation/SomeActionLoad.cs +++ b/Penumbra/Interop/Hooks/Animation/SomeActionLoad.cs @@ -1,4 +1,4 @@ -using FFXIVClientStructs.FFXIV.Client.Game; +using FFXIVClientStructs.FFXIV.Client.Game.Character; using FFXIVClientStructs.FFXIV.Client.Game.Object; using OtterGui.Services; using Penumbra.CrashHandler.Buffers; @@ -20,15 +20,15 @@ public sealed unsafe class SomeActionLoad : FastHook _state = state; _collectionResolver = collectionResolver; _crashHandler = crashHandler; - Task = hooks.CreateHook("Some Action Load", Sigs.LoadSomeAction, Detour, true); + Task = hooks.CreateHook("Some Action Load", Sigs.LoadSomeAction, Detour, HookSettings.VfxIdentificationHooks); } - public delegate void Delegate(ActionTimelineManager* timelineManager); + public delegate void Delegate(TimelineContainer* timelineManager); [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private void Detour(ActionTimelineManager* timelineManager) + private void Detour(TimelineContainer* timelineManager) { - var newData = _collectionResolver.IdentifyCollection((GameObject*)timelineManager->Parent, true); + var newData = _collectionResolver.IdentifyCollection((GameObject*)timelineManager->OwnerObject, true); var last = _state.SetAnimationData(newData); Penumbra.Log.Excessive($"[Some Action Load] Invoked on 0x{(nint)timelineManager:X}."); _crashHandler.LogAnimation(newData.AssociatedGameObject, newData.ModCollection, AnimationInvocationType.ActionLoad); diff --git a/Penumbra/Interop/Hooks/Animation/SomeMountAnimation.cs b/Penumbra/Interop/Hooks/Animation/SomeMountAnimation.cs index 5dd8227d..f2b48afe 100644 --- a/Penumbra/Interop/Hooks/Animation/SomeMountAnimation.cs +++ b/Penumbra/Interop/Hooks/Animation/SomeMountAnimation.cs @@ -15,7 +15,7 @@ public sealed unsafe class SomeMountAnimation : FastHook("Some Mount Animation", Sigs.UnkMountAnimation, Detour, true); + Task = hooks.CreateHook("Some Mount Animation", Sigs.UnkMountAnimation, Detour, HookSettings.VfxIdentificationHooks); } public delegate void Delegate(DrawObject* drawObject, uint unk1, byte unk2, uint unk3); diff --git a/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs b/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs index fad1f819..8f952df5 100644 --- a/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs +++ b/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs @@ -22,7 +22,7 @@ public sealed unsafe class SomePapLoad : FastHook _collectionResolver = collectionResolver; _objects = objects; _crashHandler = crashHandler; - Task = hooks.CreateHook("Some PAP Load", Sigs.LoadSomePap, Detour, true); + Task = hooks.CreateHook("Some PAP Load", Sigs.LoadSomePap, Detour, HookSettings.VfxIdentificationHooks); } public delegate void Delegate(nint a1, int a2, nint a3, int a4); diff --git a/Penumbra/Interop/Hooks/Animation/SomeParasolAnimation.cs b/Penumbra/Interop/Hooks/Animation/SomeParasolAnimation.cs index ab4a7201..165bd5eb 100644 --- a/Penumbra/Interop/Hooks/Animation/SomeParasolAnimation.cs +++ b/Penumbra/Interop/Hooks/Animation/SomeParasolAnimation.cs @@ -15,7 +15,7 @@ public sealed unsafe class SomeParasolAnimation : FastHook("Some Parasol Animation", Sigs.UnkParasolAnimation, Detour, true); + Task = hooks.CreateHook("Some Parasol Animation", Sigs.UnkParasolAnimation, Detour, HookSettings.VfxIdentificationHooks); } public delegate void Delegate(DrawObject* drawObject, int unk1); diff --git a/Penumbra/Interop/Hooks/HookSettings.cs b/Penumbra/Interop/Hooks/HookSettings.cs new file mode 100644 index 00000000..6d511a28 --- /dev/null +++ b/Penumbra/Interop/Hooks/HookSettings.cs @@ -0,0 +1,8 @@ +namespace Penumbra.Interop.Hooks; + +public static class HookSettings +{ + public const bool MetaEntryHooks = false; + public const bool MetaParentHooks = false; + public const bool VfxIdentificationHooks = false; +} diff --git a/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs b/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs index 7936b831..aab64871 100644 --- a/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs +++ b/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs @@ -1,6 +1,5 @@ using FFXIVClientStructs.FFXIV.Client.Game.Object; using OtterGui.Services; -using Penumbra.Collections; using Penumbra.Interop.PathResolving; using Character = FFXIVClientStructs.FFXIV.Client.Game.Character.Character; @@ -15,7 +14,7 @@ public sealed unsafe class CalculateHeight : FastHook { _collectionResolver = collectionResolver; _metaState = metaState; - Task = hooks.CreateHook("Calculate Height", (nint)Character.MemberFunctionPointers.CalculateHeight, Detour, true); + Task = hooks.CreateHook("Calculate Height", (nint)Character.MemberFunctionPointers.CalculateHeight, Detour, HookSettings.MetaParentHooks); } public delegate ulong Delegate(Character* character); diff --git a/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs b/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs index f589cf4e..f69e98e7 100644 --- a/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs +++ b/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs @@ -16,7 +16,7 @@ public sealed unsafe class ChangeCustomize : FastHook { _collectionResolver = collectionResolver; _metaState = metaState; - Task = hooks.CreateHook("Change Customize", Sigs.ChangeCustomize, Detour, true); + Task = hooks.CreateHook("Change Customize", Sigs.ChangeCustomize, Detour, HookSettings.MetaParentHooks); } public delegate bool Delegate(Human* human, CustomizeArray* data, byte skipEquipment); diff --git a/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs b/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs index bfbe6866..63cca53f 100644 --- a/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs @@ -16,7 +16,7 @@ public unsafe class EqdpAccessoryHook : FastHook, ID public EqdpAccessoryHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetEqdpAccessoryEntry", Sigs.GetEqdpAccessoryEntry, Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetEqdpAccessoryEntry", Sigs.GetEqdpAccessoryEntry, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); _metaState.Config.ModsEnabled += Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs b/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs index 6ea38ee2..5d5d2f84 100644 --- a/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs @@ -16,7 +16,7 @@ public unsafe class EqdpEquipHook : FastHook, IDisposabl public EqdpEquipHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetEqdpEquipEntry", Sigs.GetEqdpEquipEntry, Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetEqdpEquipEntry", Sigs.GetEqdpEquipEntry, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); _metaState.Config.ModsEnabled += Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/EqpHook.cs b/Penumbra/Interop/Hooks/Meta/EqpHook.cs index 19b870b0..f47db795 100644 --- a/Penumbra/Interop/Hooks/Meta/EqpHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqpHook.cs @@ -15,7 +15,7 @@ public unsafe class EqpHook : FastHook, IDisposable public EqpHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetEqpFlags", Sigs.GetEqpEntry, Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetEqpFlags", Sigs.GetEqpEntry, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); _metaState.Config.ModsEnabled += Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/EstHook.cs b/Penumbra/Interop/Hooks/Meta/EstHook.cs index 3fc7080f..5b272019 100644 --- a/Penumbra/Interop/Hooks/Meta/EstHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EstHook.cs @@ -16,7 +16,7 @@ public class EstHook : FastHook, IDisposable public EstHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetEstEntry", Sigs.GetEstEntry, Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetEstEntry", Sigs.GetEstEntry, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); _metaState.Config.ModsEnabled += Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs index a10b511a..8bd49500 100644 --- a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs +++ b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs @@ -15,7 +15,7 @@ public sealed unsafe class GetEqpIndirect : FastHook { _collectionResolver = collectionResolver; _metaState = metaState; - Task = hooks.CreateHook("Get EQP Indirect", Sigs.GetEqpIndirect, Detour, true); + Task = hooks.CreateHook("Get EQP Indirect", Sigs.GetEqpIndirect, Detour, HookSettings.MetaParentHooks); } public delegate void Delegate(DrawObject* drawObject); diff --git a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs index 30ec2597..3767c4a2 100644 --- a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs +++ b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs @@ -15,7 +15,7 @@ public sealed unsafe class GetEqpIndirect2 : FastHook { _collectionResolver = collectionResolver; _metaState = metaState; - Task = hooks.CreateHook("Get EQP Indirect 2", Sigs.GetEqpIndirect2, Detour, true); + Task = hooks.CreateHook("Get EQP Indirect 2", Sigs.GetEqpIndirect2, Detour, HookSettings.MetaParentHooks); } public delegate void Delegate(DrawObject* drawObject); diff --git a/Penumbra/Interop/Hooks/Meta/GmpHook.cs b/Penumbra/Interop/Hooks/Meta/GmpHook.cs index 72c8d075..329a8beb 100644 --- a/Penumbra/Interop/Hooks/Meta/GmpHook.cs +++ b/Penumbra/Interop/Hooks/Meta/GmpHook.cs @@ -17,7 +17,7 @@ public unsafe class GmpHook : FastHook, IDisposable public GmpHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetGmpEntry", Sigs.GetGmpEntry, Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetGmpEntry", Sigs.GetGmpEntry, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); _metaState.Config.ModsEnabled += Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs b/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs index 2c17362d..79e7f6a6 100644 --- a/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs +++ b/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs @@ -1,6 +1,5 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; -using Penumbra.Collections; using Penumbra.Interop.PathResolving; namespace Penumbra.Interop.Hooks.Meta; @@ -14,7 +13,7 @@ public sealed unsafe class ModelLoadComplete : FastHook("Model Load Complete", vtables.HumanVTable[58], Detour, true); + Task = hooks.CreateHook("Model Load Complete", vtables.HumanVTable[58], Detour, HookSettings.MetaParentHooks); } public delegate void Delegate(DrawObject* drawObject); diff --git a/Penumbra/Interop/Hooks/Meta/RspBustHook.cs b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs index d1019d3e..eb8a8a37 100644 --- a/Penumbra/Interop/Hooks/Meta/RspBustHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs @@ -20,7 +20,7 @@ public unsafe class RspBustHook : FastHook, IDisposable { _metaState = metaState; _metaFileManager = metaFileManager; - Task = hooks.CreateHook("GetRspBust", Sigs.GetRspBust, Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetRspBust", Sigs.GetRspBust, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); _metaState.Config.ModsEnabled += Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs index d54fe31e..f8f9e51e 100644 --- a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs @@ -19,7 +19,7 @@ public class RspHeightHook : FastHook, IDisposable { _metaState = metaState; _metaFileManager = metaFileManager; - Task = hooks.CreateHook("GetRspHeight", Sigs.GetRspHeight, Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetRspHeight", Sigs.GetRspHeight, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); _metaState.Config.ModsEnabled += Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs b/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs index 58856f52..8bcc7593 100644 --- a/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs +++ b/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs @@ -15,7 +15,7 @@ public sealed unsafe class RspSetupCharacter : FastHook("RSP Setup Character", Sigs.RspSetupCharacter, Detour, true); + Task = hooks.CreateHook("RSP Setup Character", Sigs.RspSetupCharacter, Detour, HookSettings.MetaParentHooks); } public delegate void Delegate(DrawObject* drawObject, nint unk2, float unk3, nint unk4, byte unk5); diff --git a/Penumbra/Interop/Hooks/Meta/RspTailHook.cs b/Penumbra/Interop/Hooks/Meta/RspTailHook.cs index 8aa7ea9f..86d21c6f 100644 --- a/Penumbra/Interop/Hooks/Meta/RspTailHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspTailHook.cs @@ -19,7 +19,7 @@ public class RspTailHook : FastHook, IDisposable { _metaState = metaState; _metaFileManager = metaFileManager; - Task = hooks.CreateHook("GetRspTail", Sigs.GetRspTail, Detour, metaState.Config.EnableMods); + Task = hooks.CreateHook("GetRspTail", Sigs.GetRspTail, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); _metaState.Config.ModsEnabled += Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/SetupVisor.cs b/Penumbra/Interop/Hooks/Meta/SetupVisor.cs index 82b24dc4..83c0e0c4 100644 --- a/Penumbra/Interop/Hooks/Meta/SetupVisor.cs +++ b/Penumbra/Interop/Hooks/Meta/SetupVisor.cs @@ -19,7 +19,7 @@ public sealed unsafe class SetupVisor : FastHook { _collectionResolver = collectionResolver; _metaState = metaState; - Task = hooks.CreateHook("Setup Visor", Sigs.SetupVisor, Detour, true); + Task = hooks.CreateHook("Setup Visor", Sigs.SetupVisor, Detour, HookSettings.MetaParentHooks); } public delegate byte Delegate(DrawObject* drawObject, ushort modelId, byte visorState); diff --git a/Penumbra/Interop/Hooks/Meta/UpdateModel.cs b/Penumbra/Interop/Hooks/Meta/UpdateModel.cs index 76854bca..a088a0f2 100644 --- a/Penumbra/Interop/Hooks/Meta/UpdateModel.cs +++ b/Penumbra/Interop/Hooks/Meta/UpdateModel.cs @@ -15,7 +15,7 @@ public sealed unsafe class UpdateModel : FastHook { _collectionResolver = collectionResolver; _metaState = metaState; - Task = hooks.CreateHook("Update Model", Sigs.UpdateModel, Detour, true); + Task = hooks.CreateHook("Update Model", Sigs.UpdateModel, Detour, HookSettings.MetaParentHooks); } public delegate void Delegate(DrawObject* drawObject); diff --git a/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs b/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs index 7b730f84..20c96f56 100644 --- a/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs +++ b/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs @@ -20,7 +20,7 @@ public sealed unsafe class CopyCharacter : EventWrapperPtr> _task; public nint Address - => (nint)CharacterSetup.MemberFunctionPointers.CopyFromCharacter; + => (nint)CharacterSetupContainer.MemberFunctionPointers.CopyFromCharacter; public void Enable() => _task.Result.Enable(); @@ -34,9 +34,9 @@ public sealed unsafe class CopyCharacter : EventWrapperPtr _task.IsCompletedSuccessfully; - private delegate ulong Delegate(CharacterSetup* target, Character* source, uint unk); + private delegate ulong Delegate(CharacterSetupContainer* target, Character* source, uint unk); - private ulong Detour(CharacterSetup* target, Character* source, uint unk) + private ulong Detour(CharacterSetupContainer* target, Character* source, uint unk) { // TODO: update when CS updated. var character = ((Character**)target)[1]; diff --git a/Penumbra/Interop/Hooks/Objects/WeaponReload.cs b/Penumbra/Interop/Hooks/Objects/WeaponReload.cs index 31c6b883..fec0a13f 100644 --- a/Penumbra/Interop/Hooks/Objects/WeaponReload.cs +++ b/Penumbra/Interop/Hooks/Objects/WeaponReload.cs @@ -39,7 +39,7 @@ public sealed unsafe class WeaponReload : EventWrapperPtrParent; + var gameObject = drawData->OwnerObject; Penumbra.Log.Verbose($"[{Name}] Triggered with drawData: 0x{(nint)drawData:X}, {slot}, {weapon}, {d}, {e}, {f}, {g}."); Invoke(drawData, gameObject, (CharacterWeapon*)(&weapon)); _task.Result.Original(drawData, slot, weapon, d, e, f, g); diff --git a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs index 5941773f..a7e82b72 100644 --- a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs +++ b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs @@ -44,18 +44,20 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable { _parent = parent; // @formatter:off - _resolveDecalPathHook = Create($"{name}.{nameof(ResolveDecal)}", hooks, vTable[83], ResolveDecal); - _resolveEidPathHook = Create( $"{name}.{nameof(ResolveEid)}", hooks, vTable[85], ResolveEid); - _resolveImcPathHook = Create($"{name}.{nameof(ResolveImc)}", hooks, vTable[81], ResolveImc); - _resolveMPapPathHook = Create( $"{name}.{nameof(ResolveMPap)}", hooks, vTable[79], ResolveMPap); - _resolveMdlPathHook = Create($"{name}.{nameof(ResolveMdl)}", hooks, vTable[73], type, ResolveMdl, ResolveMdlHuman); - _resolveMtrlPathHook = Create( $"{name}.{nameof(ResolveMtrl)}", hooks, vTable[82], ResolveMtrl); - _resolvePapPathHook = Create( $"{name}.{nameof(ResolvePap)}", hooks, vTable[76], type, ResolvePap, ResolvePapHuman); - _resolvePhybPathHook = Create($"{name}.{nameof(ResolvePhyb)}", hooks, vTable[75], type, ResolvePhyb, ResolvePhybHuman); - _resolveSklbPathHook = Create($"{name}.{nameof(ResolveSklb)}", hooks, vTable[72], type, ResolveSklb, ResolveSklbHuman); - _resolveSkpPathHook = Create($"{name}.{nameof(ResolveSkp)}", hooks, vTable[74], type, ResolveSkp, ResolveSkpHuman); - _resolveTmbPathHook = Create( $"{name}.{nameof(ResolveTmb)}", hooks, vTable[77], ResolveTmb); - _resolveVfxPathHook = Create( $"{name}.{nameof(ResolveVfx)}", hooks, vTable[84], type, ResolveVfx, ResolveVfxHuman); + _resolveSklbPathHook = Create($"{name}.{nameof(ResolveSklb)}", hooks, vTable[76], type, ResolveSklb, ResolveSklbHuman); + _resolveMdlPathHook = Create($"{name}.{nameof(ResolveMdl)}", hooks, vTable[77], type, ResolveMdl, ResolveMdlHuman); + _resolveSkpPathHook = Create($"{name}.{nameof(ResolveSkp)}", hooks, vTable[78], type, ResolveSkp, ResolveSkpHuman); + _resolvePhybPathHook = Create($"{name}.{nameof(ResolvePhyb)}", hooks, vTable[79], type, ResolvePhyb, ResolvePhybHuman); + + + _resolvePapPathHook = Create( $"{name}.{nameof(ResolvePap)}", hooks, vTable[84], type, ResolvePap, ResolvePapHuman); + _resolveTmbPathHook = Create( $"{name}.{nameof(ResolveTmb)}", hooks, vTable[85], ResolveTmb); + _resolveMPapPathHook = Create( $"{name}.{nameof(ResolveMPap)}", hooks, vTable[87], ResolveMPap); + _resolveImcPathHook = Create($"{name}.{nameof(ResolveImc)}", hooks, vTable[89], ResolveImc); + _resolveMtrlPathHook = Create( $"{name}.{nameof(ResolveMtrl)}", hooks, vTable[90], ResolveMtrl); + _resolveDecalPathHook = Create($"{name}.{nameof(ResolveDecal)}", hooks, vTable[92], ResolveDecal); + _resolveVfxPathHook = Create( $"{name}.{nameof(ResolveVfx)}", hooks, vTable[93], type, ResolveVfx, ResolveVfxHuman); + _resolveEidPathHook = Create( $"{name}.{nameof(ResolveEid)}", hooks, vTable[94], ResolveEid); // @formatter:on Enable(); } diff --git a/Penumbra/Interop/PathResolving/CollectionResolver.cs b/Penumbra/Interop/PathResolving/CollectionResolver.cs index bc474952..136da0f5 100644 --- a/Penumbra/Interop/PathResolving/CollectionResolver.cs +++ b/Penumbra/Interop/PathResolving/CollectionResolver.cs @@ -120,7 +120,7 @@ public sealed unsafe class CollectionResolver( var lobby = AgentLobby.Instance(); if (lobby != null) { - var span = lobby->LobbyData.CharaSelectEntries.Span; + var span = lobby->LobbyData.CharaSelectEntries.AsSpan(); // The lobby uses the first 8 cutscene actors. var idx = gameObject->ObjectIndex - ObjectIndex.CutsceneStart.Index; if (idx >= 0 && idx < span.Length && span[idx].Value != null) @@ -148,7 +148,7 @@ public sealed unsafe class CollectionResolver( /// Used if at the aesthetician. The relevant actor is yourself, so use player collection when possible. private bool Aesthetician(GameObject* gameObject, out ResolveData ret) { - if (gameGui.GetAddonByName("ScreenLog") != IntPtr.Zero) + if (gameGui.GetAddonByName("ScreenLog") != nint.Zero) { ret = ResolveData.Invalid; return false; diff --git a/Penumbra/Interop/PathResolving/CutsceneService.cs b/Penumbra/Interop/PathResolving/CutsceneService.cs index feb27341..8e32dd76 100644 --- a/Penumbra/Interop/PathResolving/CutsceneService.cs +++ b/Penumbra/Interop/PathResolving/CutsceneService.cs @@ -1,3 +1,4 @@ +using Dalamud.Game.ClientState.Objects.Types; using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Character; using OtterGui.Services; @@ -19,7 +20,7 @@ public sealed class CutsceneService : IRequiredService, IDisposable private readonly CharacterDestructor _characterDestructor; private readonly short[] _copiedCharacters = Enumerable.Repeat((short)-1, CutsceneSlots).ToArray(); - public IEnumerable> Actors + public IEnumerable> Actors => Enumerable.Range(CutsceneStartIdx, CutsceneSlots) .Where(i => _objects[i].Valid) .Select(i => KeyValuePair.Create(i, this[i] ?? _objects.GetDalamudObject(i)!)); @@ -42,7 +43,7 @@ public sealed class CutsceneService : IRequiredService, IDisposable /// Does not check for valid input index. /// Returns null if no connected actor is set or the actor does not exist anymore. /// - private Dalamud.Game.ClientState.Objects.Types.GameObject? this[int idx] + private IGameObject? this[int idx] { get { diff --git a/Penumbra/Interop/ResourceLoading/FileReadService.cs b/Penumbra/Interop/ResourceLoading/FileReadService.cs index f1d7fe24..5edba790 100644 --- a/Penumbra/Interop/ResourceLoading/FileReadService.cs +++ b/Penumbra/Interop/ResourceLoading/FileReadService.cs @@ -63,7 +63,7 @@ public unsafe class FileReadService : IDisposable, IRequiredService byte? ret = null; _lastFileThreadResourceManager.Value = resourceManager; ReadSqPack?.Invoke(fileDescriptor, ref priority, ref isSync, ref ret); - _lastFileThreadResourceManager.Value = IntPtr.Zero; + _lastFileThreadResourceManager.Value = nint.Zero; return ret ?? _readSqPackHook.Original(resourceManager, fileDescriptor, priority, isSync); } @@ -82,7 +82,7 @@ public unsafe class FileReadService : IDisposable, IRequiredService /// So we keep track of them per thread and use them. /// private nint GetResourceManager() - => !_lastFileThreadResourceManager.IsValueCreated || _lastFileThreadResourceManager.Value == IntPtr.Zero + => !_lastFileThreadResourceManager.IsValueCreated || _lastFileThreadResourceManager.Value == nint.Zero ? (nint)_resourceManager.ResourceManager : _lastFileThreadResourceManager.Value; } diff --git a/Penumbra/Interop/ResourceLoading/ResourceManagerService.cs b/Penumbra/Interop/ResourceLoading/ResourceManagerService.cs index c885c317..0479d2a6 100644 --- a/Penumbra/Interop/ResourceLoading/ResourceManagerService.cs +++ b/Penumbra/Interop/ResourceLoading/ResourceManagerService.cs @@ -25,8 +25,8 @@ public unsafe class ResourceManagerService : IRequiredService ref var manager = ref *ResourceManager; var catIdx = (uint)cat >> 0x18; cat = (ResourceCategory)(ushort)cat; - ref var category = ref manager.ResourceGraph->ContainerArraySpan[(int)cat]; - var extMap = FindInMap(category.CategoryMapsSpan[(int)catIdx].Value, (uint)ext); + ref var category = ref manager.ResourceGraph->Containers[(int)cat]; + var extMap = FindInMap(category.CategoryMaps[(int)catIdx].Value, (uint)ext); if (extMap == null) return null; @@ -44,10 +44,10 @@ public unsafe class ResourceManagerService : IRequiredService ref var manager = ref *ResourceManager; foreach (var resourceType in Enum.GetValues().SkipLast(1)) { - ref var graph = ref manager.ResourceGraph->ContainerArraySpan[(int)resourceType]; + ref var graph = ref manager.ResourceGraph->Containers[(int)resourceType]; for (var i = 0; i < 20; ++i) { - var map = graph.CategoryMapsSpan[i]; + var map = graph.CategoryMaps[i]; if (map.Value != null) action(resourceType, map, i); } @@ -79,25 +79,10 @@ public unsafe class ResourceManagerService : IRequiredService where TKey : unmanaged, IComparable where TValue : unmanaged { - if (map == null || map->Count == 0) + if (map == null) return null; - var node = map->Head->Parent; - while (!node->IsNil) - { - switch (key.CompareTo(node->KeyValuePair.Item1)) - { - case 0: return &node->KeyValuePair.Item2; - case < 0: - node = node->Left; - break; - default: - node = node->Right; - break; - } - } - - return null; + return map->TryGetValuePointer(key, out var val) ? val : null; } // Iterate in tree-order through a map, applying action to each KeyValuePair. @@ -105,10 +90,10 @@ public unsafe class ResourceManagerService : IRequiredService where TKey : unmanaged where TValue : unmanaged { - if (map == null || map->Count == 0) + if (map == null) return; - for (var node = map->SmallestValue; !node->IsNil; node = node->Next()) - action(node->KeyValuePair.Item1, node->KeyValuePair.Item2); + foreach (var (key, value) in *map) + action(key, value); } } diff --git a/Penumbra/Interop/ResourceLoading/ResourceService.cs b/Penumbra/Interop/ResourceLoading/ResourceService.cs index 0947d2ec..d623d72d 100644 --- a/Penumbra/Interop/ResourceLoading/ResourceService.cs +++ b/Penumbra/Interop/ResourceLoading/ResourceService.cs @@ -127,7 +127,7 @@ public unsafe class ResourceService : IDisposable, IRequiredService #endregion - private delegate IntPtr ResourceHandlePrototype(ResourceHandle* handle); + private delegate nint ResourceHandlePrototype(ResourceHandle* handle); #region IncRef diff --git a/Penumbra/Interop/ResourceLoading/TexMdlService.cs b/Penumbra/Interop/ResourceLoading/TexMdlService.cs index e617673e..a2b43c64 100644 --- a/Penumbra/Interop/ResourceLoading/TexMdlService.cs +++ b/Penumbra/Interop/ResourceLoading/TexMdlService.cs @@ -1,6 +1,7 @@ using Dalamud.Hooking; using Dalamud.Plugin.Services; using Dalamud.Utility.Signatures; +using FFXIVClientStructs.FFXIV.Client.LayoutEngine; using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; using OtterGui.Services; using Penumbra.Api.Enums; @@ -12,88 +13,99 @@ namespace Penumbra.Interop.ResourceLoading; public unsafe class TexMdlService : IDisposable, IRequiredService { /// Custom ulong flag to signal our files as opposed to SE files. - public static readonly IntPtr CustomFileFlag = new(0xDEADBEEF); - + public static readonly nint CustomFileFlag = new(0xDEADBEEF); + /// /// We need to keep a list of all CRC64 hash values of our replaced Mdl and Tex files, /// i.e. CRC32 of filename in the lower bytes, CRC32 of parent path in the upper bytes. /// public IReadOnlySet CustomFileCrc => _customFileCrc; - + public TexMdlService(IGameInteropProvider interop) { interop.InitializeFromAttributes(this); - _checkFileStateHook.Enable(); - _loadTexFileExternHook.Enable(); - _loadMdlFileExternHook.Enable(); + //_checkFileStateHook.Enable(); + //_loadTexFileExternHook.Enable(); + //_loadMdlFileExternHook.Enable(); } - + /// Add CRC64 if the given file is a model or texture file and has an associated path. public void AddCrc(ResourceType type, FullPath? path) { if (path.HasValue && type is ResourceType.Mdl or ResourceType.Tex) _customFileCrc.Add(path.Value.Crc64); } - + /// Add a fixed CRC64 value. public void AddCrc(ulong crc64) => _customFileCrc.Add(crc64); - + public void Dispose() { - _checkFileStateHook.Dispose(); - _loadTexFileExternHook.Dispose(); - _loadMdlFileExternHook.Dispose(); + //_checkFileStateHook.Dispose(); + //_loadTexFileExternHook.Dispose(); + //_loadMdlFileExternHook.Dispose(); } - + private readonly HashSet _customFileCrc = new(); - - private delegate IntPtr CheckFileStatePrototype(IntPtr unk1, ulong crc64); - + + private delegate nint CheckFileStatePrototype(nint unk1, ulong crc64); + [Signature(Sigs.CheckFileState, DetourName = nameof(CheckFileStateDetour))] private readonly Hook _checkFileStateHook = null!; - + /// /// The function that checks a files CRC64 to determine whether it is 'protected'. /// We use it to check against our stored CRC64s and if it corresponds, we return the custom flag. /// - private IntPtr CheckFileStateDetour(IntPtr ptr, ulong crc64) + private nint CheckFileStateDetour(nint ptr, ulong crc64) => _customFileCrc.Contains(crc64) ? CustomFileFlag : _checkFileStateHook.Original(ptr, crc64); - - - private delegate byte LoadTexFileLocalDelegate(ResourceHandle* handle, int unk1, IntPtr unk2, bool unk3); - + + + private delegate byte LoadTexFileLocalDelegate(ResourceHandle* handle, int unk1, nint unk2, bool unk3); + /// We use the local functions for our own files in the extern hook. [Signature(Sigs.LoadTexFileLocal)] private readonly LoadTexFileLocalDelegate _loadTexFileLocal = null!; - - private delegate byte LoadMdlFileLocalPrototype(ResourceHandle* handle, IntPtr unk1, bool unk2); - + + private delegate byte LoadMdlFileLocalPrototype(ResourceHandle* handle, nint unk1, bool unk2); + /// We use the local functions for our own files in the extern hook. [Signature(Sigs.LoadMdlFileLocal)] private readonly LoadMdlFileLocalPrototype _loadMdlFileLocal = null!; - - - private delegate byte LoadTexFileExternPrototype(ResourceHandle* handle, int unk1, IntPtr unk2, bool unk3, IntPtr unk4); - - [Signature(Sigs.LoadTexFileExtern, DetourName = nameof(LoadTexFileExternDetour))] - private readonly Hook _loadTexFileExternHook = null!; - + + + private delegate byte LoadTexFileExternPrototype(ResourceHandle* handle, int unk1, nint unk2, bool unk3, nint unk4); + + private delegate byte TexResourceHandleVf32Prototype(ResourceHandle* handle, nint unk1, byte unk2); + + //[Signature("40 53 55 41 54 41 55 41 56 41 57 48 81 EC ?? ?? ?? ?? 48 8B D9", DetourName = nameof(Vf32Detour))] + //private readonly Hook _vf32Hook = null!; + // + //private byte Vf32Detour(ResourceHandle* handle, nint unk1, byte unk2) + //{ + // var ret = _vf32Hook.Original(handle, unk1, unk2); + // return _loadTexFileLocal() + //} + + //[Signature(Sigs.LoadTexFileExtern, DetourName = nameof(LoadTexFileExternDetour))] + //private readonly Hook _loadTexFileExternHook = null!; + /// We hook the extern functions to just return the local one if given the custom flag as last argument. - private byte LoadTexFileExternDetour(ResourceHandle* resourceHandle, int unk1, IntPtr unk2, bool unk3, IntPtr ptr) - => ptr.Equals(CustomFileFlag) - ? _loadTexFileLocal.Invoke(resourceHandle, unk1, unk2, unk3) - : _loadTexFileExternHook.Original(resourceHandle, unk1, unk2, unk3, ptr); - - public delegate byte LoadMdlFileExternPrototype(ResourceHandle* handle, IntPtr unk1, bool unk2, IntPtr unk3); - - + //private byte LoadTexFileExternDetour(ResourceHandle* resourceHandle, int unk1, nint unk2, bool unk3, nint ptr) + // => ptr.Equals(CustomFileFlag) + // ? _loadTexFileLocal.Invoke(resourceHandle, unk1, unk2, unk3) + // : _loadTexFileExternHook.Original(resourceHandle, unk1, unk2, unk3, ptr); + + public delegate byte LoadMdlFileExternPrototype(ResourceHandle* handle, nint unk1, bool unk2, nint unk3); + + [Signature(Sigs.LoadMdlFileExtern, DetourName = nameof(LoadMdlFileExternDetour))] private readonly Hook _loadMdlFileExternHook = null!; - + /// We hook the extern functions to just return the local one if given the custom flag as last argument. - private byte LoadMdlFileExternDetour(ResourceHandle* resourceHandle, IntPtr unk1, bool unk2, IntPtr ptr) + private byte LoadMdlFileExternDetour(ResourceHandle* resourceHandle, nint unk1, bool unk2, nint ptr) => ptr.Equals(CustomFileFlag) ? _loadMdlFileLocal.Invoke(resourceHandle, unk1, unk2) : _loadMdlFileExternHook.Original(resourceHandle, unk1, unk2, ptr); diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.cs b/Penumbra/Interop/ResourceTree/ResolveContext.cs index e38bf4f6..ca8836b0 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.cs @@ -111,7 +111,7 @@ internal unsafe partial record ResolveContext( if (resourceHandle == null) throw new ArgumentNullException(nameof(resourceHandle)); - var fileName = resourceHandle->FileName.AsSpan(); + var fileName = (ReadOnlySpan) resourceHandle->FileName.AsSpan(); var additionalData = ByteString.Empty; if (PathDataHandler.Split(fileName, out fileName, out var data)) additionalData = ByteString.FromSpanUnsafe(data, false).Clone(); diff --git a/Penumbra/Interop/ResourceTree/ResourceTree.cs b/Penumbra/Interop/ResourceTree/ResourceTree.cs index fc8c805a..b8bad84a 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTree.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTree.cs @@ -62,7 +62,7 @@ public class ResourceTree var equipment = modelType switch { CharacterBase.ModelType.Human => new ReadOnlySpan(&human->Head, 10), - CharacterBase.ModelType.DemiHuman => new ReadOnlySpan(&character->DrawData.Head, 10), + CharacterBase.ModelType.DemiHuman => new ReadOnlySpan(Unsafe.AsPointer(ref character->DrawData.EquipmentModelIds[0]), 10), _ => ReadOnlySpan.Empty, }; ModelId = character->CharacterData.ModelCharaId; diff --git a/Penumbra/Interop/ResourceTree/ResourceTreeApiHelper.cs b/Penumbra/Interop/ResourceTree/ResourceTreeApiHelper.cs index 0d1e3abc..22025dd6 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTreeApiHelper.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTreeApiHelper.cs @@ -10,7 +10,7 @@ namespace Penumbra.Interop.ResourceTree; internal static class ResourceTreeApiHelper { public static Dictionary>> GetResourcePathDictionaries( - IEnumerable<(Character, ResourceTree)> resourceTrees) + IEnumerable<(ICharacter, ResourceTree)> resourceTrees) { var pathDictionaries = new Dictionary>>(4); @@ -47,7 +47,7 @@ internal static class ResourceTreeApiHelper } } - public static Dictionary GetResourcesOfType(IEnumerable<(Character, ResourceTree)> resourceTrees, + public static Dictionary GetResourcesOfType(IEnumerable<(ICharacter, ResourceTree)> resourceTrees, ResourceType type) { var resDictionaries = new Dictionary(4); @@ -74,7 +74,7 @@ internal static class ResourceTreeApiHelper return resDictionaries; } - public static Dictionary EncapsulateResourceTrees(IEnumerable<(Character, ResourceTree)> resourceTrees) + public static Dictionary EncapsulateResourceTrees(IEnumerable<(ICharacter, ResourceTree)> resourceTrees) { var resDictionary = new Dictionary(4); foreach (var (gameObject, resourceTree) in resourceTrees) diff --git a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs index e26c1436..1f6d1f6f 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs @@ -1,3 +1,4 @@ +using Dalamud.Game.ClientState.Objects.Types; using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Object; using OtterGui.Services; @@ -23,13 +24,13 @@ public class ResourceTreeFactory( private TreeBuildCache CreateTreeBuildCache() => new(objects, gameData, actors); - public IEnumerable GetLocalPlayerRelatedCharacters() + public IEnumerable GetLocalPlayerRelatedCharacters() { var cache = CreateTreeBuildCache(); return cache.GetLocalPlayerRelatedCharacters(); } - public IEnumerable<(Dalamud.Game.ClientState.Objects.Types.Character Character, ResourceTree ResourceTree)> FromObjectTable( + public IEnumerable<(ICharacter Character, ResourceTree ResourceTree)> FromObjectTable( Flags flags) { var cache = CreateTreeBuildCache(); @@ -43,8 +44,8 @@ public class ResourceTreeFactory( } } - public IEnumerable<(Dalamud.Game.ClientState.Objects.Types.Character Character, ResourceTree ResourceTree)> FromCharacters( - IEnumerable characters, Flags flags) + public IEnumerable<(ICharacter Character, ResourceTree ResourceTree)> FromCharacters( + IEnumerable characters, Flags flags) { var cache = CreateTreeBuildCache(); foreach (var character in characters) @@ -55,10 +56,10 @@ public class ResourceTreeFactory( } } - public ResourceTree? FromCharacter(Dalamud.Game.ClientState.Objects.Types.Character character, Flags flags) + public ResourceTree? FromCharacter(ICharacter character, Flags flags) => FromCharacter(character, CreateTreeBuildCache(), flags); - private unsafe ResourceTree? FromCharacter(Dalamud.Game.ClientState.Objects.Types.Character character, TreeBuildCache cache, Flags flags) + private unsafe ResourceTree? FromCharacter(ICharacter character, TreeBuildCache cache, Flags flags) { if (!character.IsValid()) return null; @@ -74,7 +75,7 @@ public class ResourceTreeFactory( var localPlayerRelated = cache.IsLocalPlayerRelated(character); var (name, anonymizedName, related) = GetCharacterName(character); - var networked = character.ObjectId != Dalamud.Game.ClientState.Objects.Types.GameObject.InvalidGameObjectId; + var networked = character.EntityId != 0xE0000000; var tree = new ResourceTree(name, anonymizedName, character.ObjectIndex, (nint)gameObjStruct, (nint)drawObjStruct, localPlayerRelated, related, networked, collectionResolveData.ModCollection.Name, collectionResolveData.ModCollection.AnonymizedName); var globalContext = new GlobalResolveContext(identifier, collectionResolveData.ModCollection, @@ -155,14 +156,14 @@ public class ResourceTreeFactory( } } - private unsafe (string Name, string AnonymizedName, bool PlayerRelated) GetCharacterName(Dalamud.Game.ClientState.Objects.Types.Character character) + private unsafe (string Name, string AnonymizedName, bool PlayerRelated) GetCharacterName(ICharacter character) { var identifier = actors.FromObject((GameObject*)character.Address, out var owner, true, false, false); var identifierStr = identifier.ToString(); return (identifierStr, identifier.Incognito(identifierStr), IsPlayerRelated(identifier, owner)); } - private unsafe bool IsPlayerRelated(Dalamud.Game.ClientState.Objects.Types.Character? character) + private unsafe bool IsPlayerRelated(ICharacter? character) { if (character == null) return false; @@ -175,7 +176,7 @@ public class ResourceTreeFactory( => identifier.Type switch { IdentifierType.Player => true, - IdentifierType.Owned => IsPlayerRelated(objects.Objects.CreateObjectReference(owner) as Dalamud.Game.ClientState.Objects.Types.Character), + IdentifierType.Owned => IsPlayerRelated(objects.Objects.CreateObjectReference(owner) as ICharacter), _ => false, }; diff --git a/Penumbra/Interop/ResourceTree/TreeBuildCache.cs b/Penumbra/Interop/ResourceTree/TreeBuildCache.cs index 2798002a..ca5ff736 100644 --- a/Penumbra/Interop/ResourceTree/TreeBuildCache.cs +++ b/Penumbra/Interop/ResourceTree/TreeBuildCache.cs @@ -13,7 +13,7 @@ internal readonly struct TreeBuildCache(ObjectManager objects, IDataManager data { private readonly Dictionary _shaderPackages = []; - public unsafe bool IsLocalPlayerRelated(Character character) + public unsafe bool IsLocalPlayerRelated(ICharacter character) { var player = objects.GetDalamudObject(0); if (player == null) @@ -25,36 +25,36 @@ internal readonly struct TreeBuildCache(ObjectManager objects, IDataManager data return actualIndex switch { < 2 => true, - < (int)ScreenActor.CutsceneStart => gameObject->OwnerID == player.ObjectId, + < (int)ScreenActor.CutsceneStart => gameObject->OwnerId == player.EntityId, _ => false, }; } - public IEnumerable GetCharacters() - => objects.Objects.OfType(); + public IEnumerable GetCharacters() + => objects.Objects.OfType(); - public IEnumerable GetLocalPlayerRelatedCharacters() + public IEnumerable GetLocalPlayerRelatedCharacters() { var player = objects.GetDalamudObject(0); if (player == null) yield break; - yield return (Character)player; + yield return (ICharacter)player; var minion = objects.GetDalamudObject(1); if (minion != null) - yield return (Character)minion; + yield return (ICharacter)minion; - var playerId = player.ObjectId; + var playerId = player.EntityId; for (var i = 2; i < ObjectIndex.CutsceneStart.Index; i += 2) { - if (objects.GetDalamudObject(i) is Character owned && owned.OwnerId == playerId) + if (objects.GetDalamudObject(i) is ICharacter owned && owned.OwnerId == playerId) yield return owned; } for (var i = ObjectIndex.CutsceneStart.Index; i < ObjectIndex.CharacterScreen.Index; ++i) { - var character = objects.GetDalamudObject((int) i) as Character; + var character = objects.GetDalamudObject((int) i) as ICharacter; if (character == null) continue; diff --git a/Penumbra/Interop/Services/FontReloader.cs b/Penumbra/Interop/Services/FontReloader.cs index 259fdd10..4f48f08f 100644 --- a/Penumbra/Interop/Services/FontReloader.cs +++ b/Penumbra/Interop/Services/FontReloader.cs @@ -34,7 +34,7 @@ public unsafe class FontReloader : IService if (framework == null) return; - var uiModule = framework->GetUiModule(); + var uiModule = framework->GetUIModule(); if (uiModule == null) return; @@ -43,7 +43,7 @@ public unsafe class FontReloader : IService return; _atkModule = &atkModule->AtkModule; - _reloadFontsFunc = ((delegate* unmanaged*)_atkModule->vtbl)[Offsets.ReloadFontsVfunc]; + _reloadFontsFunc = ((delegate* unmanaged*)_atkModule->VirtualTable)[Offsets.ReloadFontsVfunc]; }); } } diff --git a/Penumbra/Interop/Services/RedrawService.cs b/Penumbra/Interop/Services/RedrawService.cs index 61d7b90c..163b2c0e 100644 --- a/Penumbra/Interop/Services/RedrawService.cs +++ b/Penumbra/Interop/Services/RedrawService.cs @@ -4,8 +4,8 @@ using Dalamud.Game.ClientState.Objects.Enums; using Dalamud.Game.ClientState.Objects.SubKinds; using Dalamud.Game.ClientState.Objects.Types; using Dalamud.Plugin.Services; -using FFXIVClientStructs.FFXIV.Client.Game.Housing; -using FFXIVClientStructs.Interop; +using FFXIVClientStructs.FFXIV.Client.Game; +using FFXIVClientStructs.FFXIV.Client.Game.Character; using OtterGui.Services; using Penumbra.Api; using Penumbra.Api.Enums; @@ -37,11 +37,11 @@ public unsafe partial class RedrawService : IService => _clientState.IsGPosing; // VFuncs that disable and enable draw, used only for GPose actors. - private static void DisableDraw(GameObject actor) - => ((delegate* unmanaged< IntPtr, void >**)actor.Address)[0][Offsets.DisableDrawVfunc](actor.Address); + private static void DisableDraw(IGameObject actor) + => ((delegate* unmanaged**)actor.Address)[0][Offsets.DisableDrawVfunc](actor.Address); - private static void EnableDraw(GameObject actor) - => ((delegate* unmanaged< IntPtr, void >**)actor.Address)[0][Offsets.EnableDrawVfunc](actor.Address); + private static void EnableDraw(IGameObject actor) + => ((delegate* unmanaged**)actor.Address)[0][Offsets.EnableDrawVfunc](actor.Address); // Check whether we currently are in GPose. // Also clear the name list. @@ -57,7 +57,7 @@ public unsafe partial class RedrawService : IService // obj will be the object itself (or null) and false will be returned. // If we are in GPose and a game object with the same name as the original actor is found, // this will be in obj and true will be returned. - private bool FindCorrectActor(int idx, out GameObject? obj) + private bool FindCorrectActor(int idx, out IGameObject? obj) { obj = _objects.GetDalamudObject(idx); if (!InGPose || obj == null || IsGPoseActor(idx)) @@ -91,11 +91,11 @@ public unsafe partial class RedrawService : IService } } - return obj; + return false; } // Do not ever redraw any of the five UI Window actors. - private static bool BadRedrawIndices(GameObject? actor, out int tableIndex) + private static bool BadRedrawIndices(IGameObject? actor, out int tableIndex) { if (actor == null) { @@ -155,13 +155,13 @@ public sealed unsafe partial class RedrawService : IDisposable _communicator.ModFileChanged.Unsubscribe(OnModFileChanged); } - public static DrawState* ActorDrawState(GameObject actor) + public static DrawState* ActorDrawState(IGameObject actor) => (DrawState*)(&((FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject*)actor.Address)->RenderFlags); - private static int ObjectTableIndex(GameObject actor) + private static int ObjectTableIndex(IGameObject actor) => ((FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject*)actor.Address)->ObjectIndex; - private void WriteInvisible(GameObject? actor) + private void WriteInvisible(IGameObject? actor) { if (BadRedrawIndices(actor, out var tableIndex)) return; @@ -172,7 +172,7 @@ public sealed unsafe partial class RedrawService : IDisposable if (gPose) DisableDraw(actor!); - if (actor is PlayerCharacter + if (actor is IPlayerCharacter && _objects.GetDalamudObject(tableIndex + 1) is { ObjectKind: ObjectKind.MountType or ObjectKind.Ornament } mountOrOrnament) { *ActorDrawState(mountOrOrnament) |= DrawState.Invisibility; @@ -181,7 +181,7 @@ public sealed unsafe partial class RedrawService : IDisposable } } - private void WriteVisible(GameObject? actor) + private void WriteVisible(IGameObject? actor) { if (BadRedrawIndices(actor, out var tableIndex)) return; @@ -192,7 +192,7 @@ public sealed unsafe partial class RedrawService : IDisposable if (gPose) EnableDraw(actor!); - if (actor is PlayerCharacter + if (actor is IPlayerCharacter && _objects.GetDalamudObject(tableIndex + 1) is { ObjectKind: ObjectKind.MountType or ObjectKind.Ornament } mountOrOrnament) { *ActorDrawState(mountOrOrnament) &= ~DrawState.Invisibility; @@ -203,7 +203,7 @@ public sealed unsafe partial class RedrawService : IDisposable GameObjectRedrawn?.Invoke(actor!.Address, tableIndex); } - private void ReloadActor(GameObject? actor) + private void ReloadActor(IGameObject? actor) { if (BadRedrawIndices(actor, out var tableIndex)) return; @@ -214,7 +214,7 @@ public sealed unsafe partial class RedrawService : IDisposable _queue.Add(~tableIndex); } - private void ReloadActorAfterGPose(GameObject? actor) + private void ReloadActorAfterGPose(IGameObject? actor) { if (_objects[GPosePlayerIdx].Valid) { @@ -284,21 +284,21 @@ public sealed unsafe partial class RedrawService : IDisposable _queue.RemoveRange(numKept, _queue.Count - numKept); } - private static uint GetCurrentAnimationId(GameObject obj) + private static uint GetCurrentAnimationId(IGameObject obj) { var gameObj = (FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject*)obj.Address; if (gameObj == null || !gameObj->IsCharacter()) return 0; var chara = (Character*)gameObj; - var ptr = (byte*)&chara->ActionTimelineManager + 0xF0; + var ptr = (byte*)&chara->Timeline + 0xF0; return *(uint*)ptr; } - private static bool DelayRedraw(GameObject obj) + private static bool DelayRedraw(IGameObject obj) => ((Character*)obj.Address)->Mode switch { - (Character.CharacterModes)6 => // fishing + (CharacterModes)6 => // fishing GetCurrentAnimationId(obj) switch { 278 => true, // line out. @@ -345,7 +345,7 @@ public sealed unsafe partial class RedrawService : IDisposable HandleTarget(); } - public void RedrawObject(GameObject? actor, RedrawType settings) + public void RedrawObject(IGameObject? actor, RedrawType settings) { switch (settings) { @@ -359,13 +359,13 @@ public sealed unsafe partial class RedrawService : IDisposable } } - private GameObject? GetLocalPlayer() + private IGameObject? GetLocalPlayer() { var gPosePlayer = _objects.GetDalamudObject(GPosePlayerIdx); return gPosePlayer ?? _objects.GetDalamudObject(0); } - public bool GetName(string lowerName, out GameObject? actor) + public bool GetName(string lowerName, out IGameObject? actor) { (actor, var ret) = lowerName switch { @@ -419,15 +419,14 @@ public sealed unsafe partial class RedrawService : IDisposable if (housingManager == null) return; - var currentTerritory = housingManager->CurrentTerritory; - if (currentTerritory == null) - return; - if (!housingManager->IsInside()) + var currentTerritory = (OutdoorTerritory*) housingManager->CurrentTerritory; + if (currentTerritory == null || currentTerritory->GetTerritoryType() is not HousingTerritoryType.Outdoor) return; - foreach (var f in currentTerritory->FurnitureSpan.PointerEnumerator()) + + foreach (ref var f in currentTerritory->Furniture) { - var gameObject = f->Index >= 0 ? currentTerritory->HousingObjectManager.ObjectsSpan[f->Index].Value : null; + var gameObject = f.Index >= 0 ? currentTerritory->HousingObjectManager.Objects[f.Index].Value : null; if (gameObject == null) continue; diff --git a/Penumbra/Interop/Structs/ClipScheduler.cs b/Penumbra/Interop/Structs/ClipScheduler.cs index 3211c4f9..44a905b8 100644 --- a/Penumbra/Interop/Structs/ClipScheduler.cs +++ b/Penumbra/Interop/Structs/ClipScheduler.cs @@ -4,8 +4,8 @@ namespace Penumbra.Interop.Structs; public unsafe struct ClipScheduler { [FieldOffset(0)] - public IntPtr* VTable; + public nint* VTable; [FieldOffset(0x38)] - public IntPtr SchedulerTimeline; + public nint SchedulerTimeline; } diff --git a/Penumbra/Interop/Structs/ResourceHandle.cs b/Penumbra/Interop/Structs/ResourceHandle.cs index 382368b4..058b9004 100644 --- a/Penumbra/Interop/Structs/ResourceHandle.cs +++ b/Penumbra/Interop/Structs/ResourceHandle.cs @@ -83,12 +83,12 @@ public unsafe struct ResourceHandle [FieldOffset(0xB8)] public uint DataLength; - public (IntPtr Data, int Length) GetData() + public (nint Data, int Length) GetData() => Data != null - ? ((IntPtr)Data->DataPtr, (int)Data->DataLength) - : (IntPtr.Zero, 0); + ? ((nint)Data->DataPtr, (int)Data->DataLength) + : (nint.Zero, 0); - public bool SetData(IntPtr data, int length) + public bool SetData(nint data, int length) { if (Data == null) return false; diff --git a/Penumbra/Meta/Files/MetaBaseFile.cs b/Penumbra/Meta/Files/MetaBaseFile.cs index 86a55101..5bc36068 100644 --- a/Penumbra/Meta/Files/MetaBaseFile.cs +++ b/Penumbra/Meta/Files/MetaBaseFile.cs @@ -71,7 +71,7 @@ public unsafe class MetaBaseFile(MetaFileManager manager, IFileAllocator alloc, public int Length { get; private set; } public CharacterUtility.InternalIndex Index { get; } = CharacterUtility.ReverseIndices[(int)idx]; - protected (IntPtr Data, int Length) DefaultData + protected (nint Data, int Length) DefaultData => Manager.CharacterUtility.DefaultResource(Index); /// Reset to default values. diff --git a/Penumbra/Mods/Editor/ModMerger.cs b/Penumbra/Mods/Editor/ModMerger.cs index 9d31664b..b059813b 100644 --- a/Penumbra/Mods/Editor/ModMerger.cs +++ b/Penumbra/Mods/Editor/ModMerger.cs @@ -1,4 +1,4 @@ -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using Dalamud.Utility; using OtterGui; using OtterGui.Classes; diff --git a/Penumbra/Mods/Editor/ModNormalizer.cs b/Penumbra/Mods/Editor/ModNormalizer.cs index c6bc4939..c0876f5d 100644 --- a/Penumbra/Mods/Editor/ModNormalizer.cs +++ b/Penumbra/Mods/Editor/ModNormalizer.cs @@ -1,4 +1,4 @@ -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using OtterGui; using OtterGui.Classes; using OtterGui.Services; diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs index c52828c0..46204d6c 100644 --- a/Penumbra/Mods/Groups/ImcModGroup.cs +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -1,4 +1,4 @@ -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui; diff --git a/Penumbra/Mods/Groups/MultiModGroup.cs b/Penumbra/Mods/Groups/MultiModGroup.cs index 220d0a7c..95f49230 100644 --- a/Penumbra/Mods/Groups/MultiModGroup.cs +++ b/Penumbra/Mods/Groups/MultiModGroup.cs @@ -1,10 +1,9 @@ -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui; using OtterGui.Classes; using Penumbra.Api.Enums; -using Penumbra.GameData; using Penumbra.GameData.Data; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Settings; diff --git a/Penumbra/Mods/Manager/ModImportManager.cs b/Penumbra/Mods/Manager/ModImportManager.cs index ff39b021..39a53bb9 100644 --- a/Penumbra/Mods/Manager/ModImportManager.cs +++ b/Penumbra/Mods/Manager/ModImportManager.cs @@ -1,4 +1,4 @@ -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using OtterGui.Classes; using OtterGui.Services; using Penumbra.Import; diff --git a/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs index 594ec9d2..712630c6 100644 --- a/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs +++ b/Penumbra/Mods/Manager/OptionEditor/ModGroupEditor.cs @@ -1,4 +1,4 @@ -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using OtterGui.Classes; using OtterGui.Filesystem; using OtterGui.Services; diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index 0e66367a..546f5f5c 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -1,4 +1,4 @@ -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui; diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index 38d9c7b2..b1ad0b78 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -48,7 +48,7 @@ public class Penumbra : IDalamudPlugin private readonly ServiceManager _services; - public Penumbra(DalamudPluginInterface pluginInterface) + public Penumbra(IDalamudPluginInterface pluginInterface) { try { @@ -182,7 +182,7 @@ public class Penumbra : IDalamudPlugin [ "Glamourer", "MareSynchronos", "CustomizePlus", "SimpleHeels", "VfxEditor", "heliosphere-plugin", "Ktisis", "Brio", "DynamicBridge", ]; - var plugins = _services.GetService().InstalledPlugins + var plugins = _services.GetService().InstalledPlugins .GroupBy(p => p.InternalName) .ToDictionary(g => g.Key, g => { diff --git a/Penumbra/Penumbra.csproj b/Penumbra/Penumbra.csproj index 2e53bd22..ed5c5e30 100644 --- a/Penumbra/Penumbra.csproj +++ b/Penumbra/Penumbra.csproj @@ -37,6 +37,7 @@ $(AppData)\XIVLauncher\addon\Hooks\dev\ + H:\Projects\FFPlugins\Dalamud\bin\Release\ diff --git a/Penumbra/Penumbra.json b/Penumbra/Penumbra.json index 85e01c84..805f4d85 100644 --- a/Penumbra/Penumbra.json +++ b/Penumbra/Penumbra.json @@ -8,7 +8,7 @@ "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "Tags": [ "modding" ], - "DalamudApiLevel": 9, + "DalamudApiLevel": 10, "LoadPriority": 69420, "LoadState": 2, "LoadSync": true, diff --git a/Penumbra/Services/DalamudConfigService.cs b/Penumbra/Services/DalamudConfigService.cs index 8379a3e7..012a45f5 100644 --- a/Penumbra/Services/DalamudConfigService.cs +++ b/Penumbra/Services/DalamudConfigService.cs @@ -10,9 +10,9 @@ public class DalamudConfigService : IService try { var serviceType = - typeof(DalamudPluginInterface).Assembly.DefinedTypes.FirstOrDefault(t => t.Name == "Service`1" && t.IsGenericType); - var configType = typeof(DalamudPluginInterface).Assembly.DefinedTypes.FirstOrDefault(t => t.Name == "DalamudConfiguration"); - var interfaceType = typeof(DalamudPluginInterface).Assembly.DefinedTypes.FirstOrDefault(t => t.Name == "DalamudInterface"); + typeof(IDalamudPluginInterface).Assembly.DefinedTypes.FirstOrDefault(t => t.Name == "Service`1" && t.IsGenericType); + var configType = typeof(IDalamudPluginInterface).Assembly.DefinedTypes.FirstOrDefault(t => t.Name == "DalamudConfiguration"); + var interfaceType = typeof(IDalamudPluginInterface).Assembly.DefinedTypes.FirstOrDefault(t => t.Name == "DalamudInterface"); if (serviceType == null || configType == null || interfaceType == null) return; diff --git a/Penumbra/Services/FilenameService.cs b/Penumbra/Services/FilenameService.cs index e1c482f7..817af0d2 100644 --- a/Penumbra/Services/FilenameService.cs +++ b/Penumbra/Services/FilenameService.cs @@ -5,7 +5,7 @@ using Penumbra.Mods; namespace Penumbra.Services; -public class FilenameService(DalamudPluginInterface pi) : IService +public class FilenameService(IDalamudPluginInterface pi) : IService { public readonly string ConfigDirectory = pi.ConfigDirectory.FullName; public readonly string CollectionDirectory = Path.Combine(pi.ConfigDirectory.FullName, "collections"); diff --git a/Penumbra/Services/MessageService.cs b/Penumbra/Services/MessageService.cs index 0a85a569..08118483 100644 --- a/Penumbra/Services/MessageService.cs +++ b/Penumbra/Services/MessageService.cs @@ -9,8 +9,8 @@ using OtterGui.Services; namespace Penumbra.Services; -public class MessageService(Logger log, UiBuilder uiBuilder, IChatGui chat, INotificationManager notificationManager) - : OtterGui.Classes.MessageService(log, uiBuilder, chat, notificationManager), IService +public class MessageService(Logger log, IUiBuilder builder, IChatGui chat, INotificationManager notificationManager) + : OtterGui.Classes.MessageService(log, builder, chat, notificationManager), IService { public void LinkItem(Item item) { diff --git a/Penumbra/Services/StaticServiceManager.cs b/Penumbra/Services/StaticServiceManager.cs index 3279da96..c0dc9314 100644 --- a/Penumbra/Services/StaticServiceManager.cs +++ b/Penumbra/Services/StaticServiceManager.cs @@ -19,7 +19,7 @@ namespace Penumbra.Services; public static class StaticServiceManager { - public static ServiceManager CreateProvider(Penumbra penumbra, DalamudPluginInterface pi, Logger log) + public static ServiceManager CreateProvider(Penumbra penumbra, IDalamudPluginInterface pi, Logger log) { var services = new ServiceManager(log) .AddDalamudServices(pi) @@ -40,7 +40,7 @@ public static class StaticServiceManager return services; } - private static ServiceManager AddDalamudServices(this ServiceManager services, DalamudPluginInterface pi) + private static ServiceManager AddDalamudServices(this ServiceManager services, IDalamudPluginInterface pi) => services.AddExistingService(pi) .AddExistingService(pi.UiBuilder) .AddDalamudService(pi) diff --git a/Penumbra/Services/ValidityChecker.cs b/Penumbra/Services/ValidityChecker.cs index cc70306b..cefee139 100644 --- a/Penumbra/Services/ValidityChecker.cs +++ b/Penumbra/Services/ValidityChecker.cs @@ -1,4 +1,4 @@ -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using Dalamud.Plugin; using FFXIVClientStructs.FFXIV.Client.System.Framework; using OtterGui.Classes; @@ -27,11 +27,11 @@ public class ValidityChecker : IService get { var framework = Framework.Instance(); - return framework == null ? string.Empty : framework->GameVersion[0]; + return framework == null ? string.Empty : framework->GameVersionString; } } - public ValidityChecker(DalamudPluginInterface pi) + public ValidityChecker(IDalamudPluginInterface pi) { DevPenumbraExists = CheckDevPluginPenumbra(pi); IsNotInstalledPenumbra = CheckIsNotInstalled(pi); @@ -50,7 +50,7 @@ public class ValidityChecker : IService } // Because remnants of penumbra in devPlugins cause issues, we check for them to warn users to remove them. - private static bool CheckDevPluginPenumbra(DalamudPluginInterface pi) + private static bool CheckDevPluginPenumbra(IDalamudPluginInterface pi) { #if !DEBUG var path = Path.Combine(pi.DalamudAssetDirectory.Parent?.FullName ?? "INVALIDPATH", "devPlugins", "Penumbra"); @@ -71,7 +71,7 @@ public class ValidityChecker : IService } // Check if the loaded version of Penumbra itself is in devPlugins. - private static bool CheckIsNotInstalled(DalamudPluginInterface pi) + private static bool CheckIsNotInstalled(IDalamudPluginInterface pi) { #if !DEBUG var checkedDirectory = pi.AssemblyLocation.Directory?.Parent?.Parent?.Name; @@ -86,7 +86,7 @@ public class ValidityChecker : IService } // Check if the loaded version of Penumbra is installed from a valid source repo. - private static bool CheckSourceRepo(DalamudPluginInterface pi) + private static bool CheckSourceRepo(IDalamudPluginInterface pi) { #if !DEBUG return pi.SourceRepository?.Trim().ToLowerInvariant() switch diff --git a/Penumbra/UI/AdvancedWindow/FileEditor.cs b/Penumbra/UI/AdvancedWindow/FileEditor.cs index c95884c6..eeb94c71 100644 --- a/Penumbra/UI/AdvancedWindow/FileEditor.cs +++ b/Penumbra/UI/AdvancedWindow/FileEditor.cs @@ -1,5 +1,5 @@ using Dalamud.Interface; -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using Dalamud.Plugin.Services; using ImGuiNET; using OtterGui; diff --git a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs index 652f928d..6db4db5c 100644 --- a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs +++ b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs @@ -1,4 +1,4 @@ -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using ImGuiNET; using OtterGui; using OtterGui.Classes; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs index 56e9482b..91129129 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs @@ -1,5 +1,5 @@ using Dalamud.Interface; -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using ImGuiNET; using Newtonsoft.Json.Linq; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.Shpk.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.Shpk.cs index 3ce10224..b9525b29 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.Shpk.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.Shpk.cs @@ -287,7 +287,7 @@ public partial class ModEditWindow { fixed (ushort* v2 = &v) { - return ImGui.InputScalar(label, ImGuiDataType.U16, (nint)v2, IntPtr.Zero, IntPtr.Zero, "%04X", flags); + return ImGui.InputScalar(label, ImGuiDataType.U16, (nint)v2, nint.Zero, nint.Zero, "%04X", flags); } } diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs index 070895b5..a22c10ad 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs @@ -1,5 +1,5 @@ -using Dalamud.Interface.Internal.Notifications; using Dalamud.Interface; +using Dalamud.Interface.ImGuiNotification; using ImGuiNET; using Lumina.Misc; using OtterGui.Raii; diff --git a/Penumbra/UI/ChangedItemDrawer.cs b/Penumbra/UI/ChangedItemDrawer.cs index 0afeeeeb..72bfa266 100644 --- a/Penumbra/UI/ChangedItemDrawer.cs +++ b/Penumbra/UI/ChangedItemDrawer.cs @@ -1,5 +1,6 @@ using Dalamud.Interface; -using Dalamud.Interface.Internal; +using Dalamud.Interface.Textures; +using Dalamud.Interface.Textures.TextureWraps; using Dalamud.Plugin.Services; using Dalamud.Utility; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; @@ -121,7 +122,7 @@ public class ChangedItemDrawer : IDisposable, IUiService public static Vector2 TypeFilterIconSize => new(2 * ImGui.GetTextLineHeight()); - public ChangedItemDrawer(UiBuilder uiBuilder, IDataManager gameData, ITextureProvider textureProvider, CommunicatorService communicator, + public ChangedItemDrawer(IUiBuilder uiBuilder, IDataManager gameData, ITextureProvider textureProvider, CommunicatorService communicator, Configuration config) { _items = gameData.GetExcelSheet()!; @@ -417,7 +418,7 @@ public class ChangedItemDrawer : IDisposable, IUiService }; /// Initialize the icons. - private bool CreateEquipSlotIcons(UiBuilder uiBuilder, IDataManager gameData, ITextureProvider textureProvider) + private bool CreateEquipSlotIcons(IUiBuilder uiBuilder, IDataManager gameData, ITextureProvider textureProvider) { using var equipTypeIcons = uiBuilder.LoadUld("ui/uld/ArmouryBoard.uld"); @@ -441,20 +442,20 @@ public class ChangedItemDrawer : IDisposable, IUiService Add(ChangedItemIcon.Neck, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 9)); Add(ChangedItemIcon.Wrists, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 10)); Add(ChangedItemIcon.Finger, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 11)); - Add(ChangedItemIcon.Monster, textureProvider.GetTextureFromGame("ui/icon/062000/062042_hr1.tex", true)); - Add(ChangedItemIcon.Demihuman, textureProvider.GetTextureFromGame("ui/icon/062000/062041_hr1.tex", true)); - Add(ChangedItemIcon.Customization, textureProvider.GetTextureFromGame("ui/icon/062000/062043_hr1.tex", true)); - Add(ChangedItemIcon.Action, textureProvider.GetTextureFromGame("ui/icon/062000/062001_hr1.tex", true)); - Add(ChangedItemIcon.Emote, LoadEmoteTexture(gameData, uiBuilder)); - Add(ChangedItemIcon.Unknown, LoadUnknownTexture(gameData, uiBuilder)); - Add(AllFlags, textureProvider.GetTextureFromGame("ui/icon/114000/114052_hr1.tex", true)); + Add(ChangedItemIcon.Monster, textureProvider.CreateFromTexFile(gameData.GetFile("ui/icon/062000/062042_hr1.tex")!)); + Add(ChangedItemIcon.Demihuman, textureProvider.CreateFromTexFile(gameData.GetFile("ui/icon/062000/062041_hr1.tex")!)); + Add(ChangedItemIcon.Customization, textureProvider.CreateFromTexFile(gameData.GetFile("ui/icon/062000/062043_hr1.tex")!)); + Add(ChangedItemIcon.Action, textureProvider.CreateFromTexFile(gameData.GetFile("ui/icon/062000/062001_hr1.tex")!)); + Add(ChangedItemIcon.Emote, LoadEmoteTexture(gameData, textureProvider)); + Add(ChangedItemIcon.Unknown, LoadUnknownTexture(gameData, textureProvider)); + Add(AllFlags, textureProvider.CreateFromTexFile(gameData.GetFile("ui/icon/114000/114052_hr1.tex")!)); _smallestIconWidth = _icons.Values.Min(i => i.Width); return true; } - private static unsafe IDalamudTextureWrap? LoadUnknownTexture(IDataManager gameData, UiBuilder uiBuilder) + private static unsafe IDalamudTextureWrap? LoadUnknownTexture(IDataManager gameData, ITextureProvider textureProvider) { var unk = gameData.GetFile("ui/uld/levelup2_hr1.tex"); if (unk == null) @@ -466,10 +467,10 @@ public class ChangedItemDrawer : IDisposable, IUiService for (var y = 0; y < unk.Header.Height; ++y) image.AsSpan(4 * y * unk.Header.Width, 4 * unk.Header.Width).CopyTo(bytes.AsSpan(4 * y * unk.Header.Height + diff)); - return uiBuilder.LoadImageRaw(bytes, unk.Header.Height, unk.Header.Height, 4); + return textureProvider.CreateFromRaw(RawImageSpecification.Rgba32(unk.Header.Height, unk.Header.Height), bytes, "Penumbra.UnkItemIcon"); } - private static unsafe IDalamudTextureWrap? LoadEmoteTexture(IDataManager gameData, UiBuilder uiBuilder) + private static unsafe IDalamudTextureWrap? LoadEmoteTexture(IDataManager gameData, ITextureProvider textureProvider) { var emote = gameData.GetFile("ui/icon/000000/000019_hr1.tex"); if (emote == null) @@ -486,6 +487,6 @@ public class ChangedItemDrawer : IDisposable, IUiService } } - return uiBuilder.LoadImageRaw(image2, emote.Header.Width, emote.Header.Height, 4); + return textureProvider.CreateFromRaw(RawImageSpecification.Rgba32(emote.Header.Width, emote.Header.Height), image2, "Penumbra.EmoteItemIcon"); } } diff --git a/Penumbra/UI/CollectionTab/CollectionPanel.cs b/Penumbra/UI/CollectionTab/CollectionPanel.cs index 082b78b8..914f10d9 100644 --- a/Penumbra/UI/CollectionTab/CollectionPanel.cs +++ b/Penumbra/UI/CollectionTab/CollectionPanel.cs @@ -2,7 +2,7 @@ using Dalamud.Game.ClientState.Objects; using Dalamud.Interface; using Dalamud.Interface.Components; using Dalamud.Interface.GameFonts; -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using Dalamud.Interface.ManagedFontAtlas; using Dalamud.Interface.Utility; using Dalamud.Plugin; @@ -21,7 +21,7 @@ using Penumbra.UI.Classes; namespace Penumbra.UI.CollectionTab; public sealed class CollectionPanel( - DalamudPluginInterface pi, + IDalamudPluginInterface pi, CommunicatorService communicator, CollectionManager manager, CollectionSelector selector, @@ -318,7 +318,7 @@ public sealed class CollectionPanel( var button = ImGui.Button(text, width) || ImGui.IsItemClicked(ImGuiMouseButton.Right); var hovered = redundancy.Length > 0 && ImGui.IsItemHovered(); DrawIndividualDragSource(text, id); - DrawIndividualDragTarget(text, id); + DrawIndividualDragTarget(id); if (!invalid) { selector.DragTargetAssignment(type, id); @@ -349,7 +349,7 @@ public sealed class CollectionPanel( _draggedIndividualAssignment = _active.Individuals.Index(id); } - private void DrawIndividualDragTarget(string text, ActorIdentifier id) + private void DrawIndividualDragTarget(ActorIdentifier id) { if (!id.IsValid) return; diff --git a/Penumbra/UI/ConfigWindow.cs b/Penumbra/UI/ConfigWindow.cs index 67b0a50c..53fa0b33 100644 --- a/Penumbra/UI/ConfigWindow.cs +++ b/Penumbra/UI/ConfigWindow.cs @@ -16,7 +16,7 @@ namespace Penumbra.UI; public sealed class ConfigWindow : Window, IUiService { - private readonly DalamudPluginInterface _pluginInterface; + private readonly IDalamudPluginInterface _pluginInterface; private readonly Configuration _config; private readonly PerformanceTracker _tracker; private readonly ValidityChecker _validityChecker; @@ -24,7 +24,7 @@ public sealed class ConfigWindow : Window, IUiService private ConfigTabBar _configTabs = null!; private string? _lastException; - public ConfigWindow(PerformanceTracker tracker, DalamudPluginInterface pi, Configuration config, ValidityChecker checker, + public ConfigWindow(PerformanceTracker tracker, IDalamudPluginInterface pi, Configuration config, ValidityChecker checker, TutorialService tutorial) : base(GetLabel(checker)) { diff --git a/Penumbra/UI/LaunchButton.cs b/Penumbra/UI/LaunchButton.cs index 14e16432..cb533a00 100644 --- a/Penumbra/UI/LaunchButton.cs +++ b/Penumbra/UI/LaunchButton.cs @@ -1,5 +1,5 @@ using Dalamud.Interface; -using Dalamud.Interface.Internal; +using Dalamud.Interface.Textures.TextureWraps; using Dalamud.Plugin; using Dalamud.Plugin.Services; using OtterGui.Services; @@ -13,23 +13,25 @@ namespace Penumbra.UI; public class LaunchButton : IDisposable, IUiService { private readonly ConfigWindow _configWindow; - private readonly UiBuilder _uiBuilder; + private readonly IUiBuilder _uiBuilder; private readonly ITitleScreenMenu _title; private readonly string _fileName; + private readonly ITextureProvider _textureProvider; - private IDalamudTextureWrap? _icon; - private TitleScreenMenuEntry? _entry; + private IDalamudTextureWrap? _icon; + private IReadOnlyTitleScreenMenuEntry? _entry; /// /// Register the launch button to be created on the next draw event. /// - public LaunchButton(DalamudPluginInterface pi, ITitleScreenMenu title, ConfigWindow ui) + public LaunchButton(IDalamudPluginInterface pi, ITitleScreenMenu title, ConfigWindow ui, ITextureProvider textureProvider) { - _uiBuilder = pi.UiBuilder; - _configWindow = ui; - _title = title; - _icon = null; - _entry = null; + _uiBuilder = pi.UiBuilder; + _configWindow = ui; + _textureProvider = textureProvider; + _title = title; + _icon = null; + _entry = null; _fileName = Path.Combine(pi.AssemblyLocation.DirectoryName!, "tsmLogo.png"); _uiBuilder.Draw += CreateEntry; @@ -49,7 +51,8 @@ public class LaunchButton : IDisposable, IUiService { try { - _icon = _uiBuilder.LoadImage(_fileName); + // TODO: update when API updated. + _icon = _textureProvider.GetFromFile(_fileName).RentAsync().Result; if (_icon != null) _entry = _title.AddEntry("Manage Penumbra", _icon, OnTriggered); diff --git a/Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs index b8faadf7..ec5bb920 100644 --- a/Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/ModGroupEditDrawer.cs @@ -1,5 +1,5 @@ using Dalamud.Interface; -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using ImGuiNET; using OtterGui; using OtterGui.Classes; diff --git a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs index 0ca4d40c..88d6afa2 100644 --- a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs +++ b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs @@ -1,6 +1,6 @@ using Dalamud.Interface; using Dalamud.Interface.DragDrop; -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using Dalamud.Plugin.Services; using ImGuiNET; using OtterGui; diff --git a/Penumbra/UI/ModsTab/ModPanel.cs b/Penumbra/UI/ModsTab/ModPanel.cs index 28f00a97..ee6fab1f 100644 --- a/Penumbra/UI/ModsTab/ModPanel.cs +++ b/Penumbra/UI/ModsTab/ModPanel.cs @@ -17,7 +17,7 @@ public class ModPanel : IDisposable, IUiService private readonly ModPanelTabBar _tabs; private bool _resetCursor; - public ModPanel(DalamudPluginInterface pi, ModFileSystemSelector selector, ModEditWindow editWindow, ModPanelTabBar tabs, + public ModPanel(IDalamudPluginInterface pi, ModFileSystemSelector selector, ModEditWindow editWindow, ModPanelTabBar tabs, MultiModPanel multiModPanel, CommunicatorService communicator) { _selector = selector; diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index 1e371065..f81b2831 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -1,6 +1,6 @@ using Dalamud.Interface; using Dalamud.Interface.Components; -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using ImGuiNET; using OtterGui; using OtterGui.Raii; diff --git a/Penumbra/UI/ModsTab/ModPanelHeader.cs b/Penumbra/UI/ModsTab/ModPanelHeader.cs index a8b393b1..6c974f9c 100644 --- a/Penumbra/UI/ModsTab/ModPanelHeader.cs +++ b/Penumbra/UI/ModsTab/ModPanelHeader.cs @@ -20,7 +20,7 @@ public class ModPanelHeader : IDisposable private readonly CommunicatorService _communicator; private float _lastPreSettingsHeight = 0; - public ModPanelHeader(DalamudPluginInterface pi, CommunicatorService communicator) + public ModPanelHeader(IDalamudPluginInterface pi, CommunicatorService communicator) { _communicator = communicator; _nameFont = pi.UiBuilder.FontAtlas.NewGameFontHandle(new GameFontStyle(GameFontFamilyAndSize.Jupiter23)); diff --git a/Penumbra/UI/PredefinedTagManager.cs b/Penumbra/UI/PredefinedTagManager.cs index d531b1a2..8de613d4 100644 --- a/Penumbra/UI/PredefinedTagManager.cs +++ b/Penumbra/UI/PredefinedTagManager.cs @@ -1,5 +1,5 @@ using Dalamud.Interface; -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using ImGuiNET; using Newtonsoft.Json; using Newtonsoft.Json.Linq; diff --git a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs index 3bf4cd88..c53f1b8e 100644 --- a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs +++ b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs @@ -271,7 +271,7 @@ public sealed class ResourceWatcher : IDisposable, ITab, IUiService public unsafe string Name(ResolveData resolve, string none = "") { - if (resolve.AssociatedGameObject == IntPtr.Zero || !_actors.Awaiter.IsCompletedSuccessfully) + if (resolve.AssociatedGameObject == nint.Zero || !_actors.Awaiter.IsCompletedSuccessfully) return none; try diff --git a/Penumbra/UI/Tabs/CollectionsTab.cs b/Penumbra/UI/Tabs/CollectionsTab.cs index 34e2cbcf..05a1f33b 100644 --- a/Penumbra/UI/Tabs/CollectionsTab.cs +++ b/Penumbra/UI/Tabs/CollectionsTab.cs @@ -38,7 +38,7 @@ public sealed class CollectionsTab : IDisposable, ITab, IUiService } } - public CollectionsTab(DalamudPluginInterface pi, Configuration configuration, CommunicatorService communicator, IncognitoService incognito, + public CollectionsTab(IDalamudPluginInterface pi, Configuration configuration, CommunicatorService communicator, IncognitoService incognito, CollectionManager collectionManager, ModStorage modStorage, ActorManager actors, ITargetManager targets, TutorialService tutorial, SaveService saveService) { _config = configuration.Ephemeral; diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index 0122a6f5..41f28ab9 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -37,7 +37,6 @@ using Penumbra.Util; using static OtterGui.Raii.ImRaii; using CharacterBase = FFXIVClientStructs.FFXIV.Client.Graphics.Scene.CharacterBase; using CharacterUtility = Penumbra.Interop.Services.CharacterUtility; -using ObjectKind = Dalamud.Game.ClientState.Objects.Enums.ObjectKind; using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManager; using ImGuiClip = OtterGui.ImGuiClip; using Penumbra.Api.IpcTester; @@ -437,8 +436,8 @@ public class DebugTab : Window, ITab, IUiService : $"0x{(nint)((Character*)obj.Address)->GameObject.GetDrawObject():X}"); var identifier = _actors.FromObject(obj, out _, false, true, false); ImGuiUtil.DrawTableColumn(_actors.ToString(identifier)); - var id = obj.AsObject->ObjectKind == (byte)ObjectKind.BattleNpc - ? $"{identifier.DataId} | {obj.AsObject->DataID}" + var id = obj.AsObject->ObjectKind is ObjectKind.BattleNpc + ? $"{identifier.DataId} | {obj.AsObject->BaseId}" : identifier.DataId.ToString(); ImGuiUtil.DrawTableColumn(id); } @@ -587,11 +586,11 @@ public class DebugTab : Window, ITab, IUiService if (table) { ImGuiUtil.DrawTableColumn("Group Members"); - ImGuiUtil.DrawTableColumn(GroupManager.Instance()->MemberCount.ToString()); + ImGuiUtil.DrawTableColumn(GroupManager.Instance()->MainGroup.MemberCount.ToString()); for (var i = 0; i < 8; ++i) { ImGuiUtil.DrawTableColumn($"Member #{i}"); - var member = GroupManager.Instance()->GetPartyMemberByIndex(i); + var member = GroupManager.Instance()->MainGroup.GetPartyMemberByIndex(i); ImGuiUtil.DrawTableColumn(member == null ? "NULL" : new ByteString(member->Name).ToString()); } } @@ -612,7 +611,7 @@ public class DebugTab : Window, ITab, IUiService if (table) for (var i = 0; i < 8; ++i) { - ref var c = ref agent->Data->CharacterArraySpan[i]; + ref var c = ref agent->Data->Characters[i]; ImGuiUtil.DrawTableColumn($"Character {i}"); var name = c.Name1.ToString(); ImGuiUtil.DrawTableColumn(name.Length == 0 ? "NULL" : $"{name} ({c.WorldId})"); diff --git a/Penumbra/UI/Tabs/ModsTab.cs b/Penumbra/UI/Tabs/ModsTab.cs index 7faa3da8..50fdc1d3 100644 --- a/Penumbra/UI/Tabs/ModsTab.cs +++ b/Penumbra/UI/Tabs/ModsTab.cs @@ -5,7 +5,7 @@ using OtterGui.Raii; using Penumbra.UI.Classes; using Dalamud.Interface; using Dalamud.Plugin.Services; -using FFXIVClientStructs.FFXIV.Client.Game.Housing; +using FFXIVClientStructs.FFXIV.Client.Game; using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Api.Enums; diff --git a/Penumbra/UI/Tabs/ResourceTab.cs b/Penumbra/UI/Tabs/ResourceTab.cs index 0b54c5e2..14d4ed41 100644 --- a/Penumbra/UI/Tabs/ResourceTab.cs +++ b/Penumbra/UI/Tabs/ResourceTab.cs @@ -121,7 +121,7 @@ public class ResourceTab(Configuration config, ResourceManagerService resourceMa } /// Obtain a label for an extension node. - private static string GetNodeLabel(uint label, uint type, ulong count) + private static string GetNodeLabel(uint label, uint type, int count) { var (lowest, mid1, mid2, highest) = Functions.SplitBytes(type); return highest == 0 diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index 17db21c9..49e77a4d 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -41,7 +41,7 @@ public class SettingsTab : ITab, IUiService private readonly DalamudSubstitutionProvider _dalamudSubstitutionProvider; private readonly FileCompactor _compactor; private readonly DalamudConfigService _dalamudConfig; - private readonly DalamudPluginInterface _pluginInterface; + private readonly IDalamudPluginInterface _pluginInterface; private readonly IDataManager _gameData; private readonly PredefinedTagManager _predefinedTagManager; private readonly CrashHandlerService _crashService; @@ -51,7 +51,7 @@ public class SettingsTab : ITab, IUiService private readonly TagButtons _sharedTags = new(); - public SettingsTab(DalamudPluginInterface pluginInterface, Configuration config, FontReloader fontReloader, TutorialService tutorial, + public SettingsTab(IDalamudPluginInterface pluginInterface, Configuration config, FontReloader fontReloader, TutorialService tutorial, Penumbra penumbra, FileDialogService fileDialog, ModManager modManager, ModFileSystemSelector selector, CharacterUtility characterUtility, ResidentResourceManager residentResources, ModExportManager modExportManager, HttpApi httpApi, DalamudSubstitutionProvider dalamudSubstitutionProvider, FileCompactor compactor, DalamudConfigService dalamudConfig, diff --git a/Penumbra/UI/UiHelpers.cs b/Penumbra/UI/UiHelpers.cs index 8fbce6d0..deba7023 100644 --- a/Penumbra/UI/UiHelpers.cs +++ b/Penumbra/UI/UiHelpers.cs @@ -1,4 +1,4 @@ -using Dalamud.Interface.Internal.Notifications; +using Dalamud.Interface.ImGuiNotification; using Dalamud.Interface.Utility; using ImGuiNET; using OtterGui; diff --git a/Penumbra/UI/WindowSystem.cs b/Penumbra/UI/WindowSystem.cs index 99819fce..72ac0d01 100644 --- a/Penumbra/UI/WindowSystem.cs +++ b/Penumbra/UI/WindowSystem.cs @@ -9,13 +9,13 @@ namespace Penumbra.UI; public class PenumbraWindowSystem : IDisposable, IUiService { - private readonly UiBuilder _uiBuilder; + private readonly IUiBuilder _uiBuilder; private readonly WindowSystem _windowSystem; private readonly FileDialogService _fileDialog; public readonly ConfigWindow Window; public readonly PenumbraChangelog Changelog; - public PenumbraWindowSystem(DalamudPluginInterface pi, Configuration config, PenumbraChangelog changelog, ConfigWindow window, + public PenumbraWindowSystem(IDalamudPluginInterface pi, Configuration config, PenumbraChangelog changelog, ConfigWindow window, LaunchButton _, ModEditWindow editWindow, FileDialogService fileDialog, ImportPopup importPopup, DebugTab debugTab) { _uiBuilder = pi.UiBuilder; diff --git a/repo.json b/repo.json index 3142f8d4..6379595e 100644 --- a/repo.json +++ b/repo.json @@ -9,7 +9,7 @@ "TestingAssemblyVersion": "1.1.1.5", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", - "DalamudApiLevel": 9, + "DalamudApiLevel": 10, "IsHide": "False", "IsTestingExclusive": "False", "DownloadCount": 0, From 431933e9c16ac5e4e0e25504cda14eb1a670ea3c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 2 Jul 2024 18:27:53 +0200 Subject: [PATCH 269/433] Revert repo API version. --- repo.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repo.json b/repo.json index 6379595e..3142f8d4 100644 --- a/repo.json +++ b/repo.json @@ -9,7 +9,7 @@ "TestingAssemblyVersion": "1.1.1.5", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", - "DalamudApiLevel": 10, + "DalamudApiLevel": 9, "IsHide": "False", "IsTestingExclusive": "False", "DownloadCount": 0, From 9fb80907811f1fac339410ba60ce6384c40195a2 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 3 Jul 2024 17:29:49 +0200 Subject: [PATCH 270/433] Current state. --- OtterGui | 2 +- Penumbra.GameData | 2 +- Penumbra/Api/Api/GameStateApi.cs | 2 +- .../Cache/CollectionCacheManager.cs | 2 +- .../Collections/Cache/CustomResourceCache.cs | 2 +- Penumbra/Communication/MtrlShpkLoaded.cs | 2 +- Penumbra/Interop/Hooks/HookSettings.cs | 18 ++- .../Hooks/Objects/CharacterBaseDestructor.cs | 2 +- .../Hooks/Objects/CharacterDestructor.cs | 2 +- .../Interop/Hooks/Objects/CopyCharacter.cs | 2 +- .../Hooks/Objects/CreateCharacterBase.cs | 2 +- Penumbra/Interop/Hooks/Objects/EnableDraw.cs | 2 +- .../Interop/Hooks/Objects/WeaponReload.cs | 2 +- .../PreBoneDeformerReplacer.cs | 24 ++-- .../PostProcessing}/ShaderReplacementFixer.cs | 20 ++-- .../ResourceLoading/CreateFileWHook.cs | 5 +- .../ResourceLoading/FileReadService.cs | 11 +- .../ResourceLoading/ResourceLoader.cs | 26 ++-- .../ResourceLoading/ResourceManagerService.cs | 6 +- .../ResourceLoading/ResourceService.cs | 13 +- .../ResourceLoading/TexMdlService.cs | 113 +++++++++++------- .../Hooks/Resources/ApricotResourceLoad.cs | 2 +- .../Interop/Hooks/Resources/LoadMtrlShpk.cs | 2 +- .../Interop/Hooks/Resources/LoadMtrlTex.cs | 2 +- .../Hooks/Resources/ResolvePathHooksBase.cs | 3 +- .../Resources/ResourceHandleDestructor.cs | 2 +- Penumbra/Interop/PathResolving/MetaState.cs | 2 +- .../Interop/PathResolving/PathResolver.cs | 2 +- .../Interop/PathResolving/SubfileHelper.cs | 2 +- .../Processing/FilePostProcessService.cs | 2 +- .../Interop/ResourceTree/ResolveContext.cs | 2 +- Penumbra/Interop/ResourceTree/ResourceTree.cs | 30 +++-- Penumbra/Interop/Services/DecalReverter.cs | 2 +- Penumbra/Penumbra.cs | 2 +- Penumbra/Penumbra.csproj | 1 - Penumbra/Services/CrashHandlerService.cs | 2 +- .../UI/ResourceWatcher/ResourceWatcher.cs | 2 +- Penumbra/UI/Tabs/Debug/DebugTab.cs | 3 +- Penumbra/UI/Tabs/ResourceTab.cs | 2 +- 39 files changed, 186 insertions(+), 139 deletions(-) rename Penumbra/Interop/{Services => Hooks/PostProcessing}/PreBoneDeformerReplacer.cs (81%) rename Penumbra/Interop/{Services => Hooks/PostProcessing}/ShaderReplacementFixer.cs (91%) rename Penumbra/Interop/{ => Hooks}/ResourceLoading/CreateFileWHook.cs (97%) rename Penumbra/Interop/{ => Hooks}/ResourceLoading/FileReadService.cs (92%) rename Penumbra/Interop/{ => Hooks}/ResourceLoading/ResourceLoader.cs (93%) rename Penumbra/Interop/{ => Hooks}/ResourceLoading/ResourceManagerService.cs (93%) rename Penumbra/Interop/{ => Hooks}/ResourceLoading/ResourceService.cs (95%) rename Penumbra/Interop/{ => Hooks}/ResourceLoading/TexMdlService.cs (60%) diff --git a/OtterGui b/OtterGui index 437ef65c..c2738e1d 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 437ef65c6464c54c8f40196dd2428da901d73aab +Subproject commit c2738e1d42974cddbe5a31238c6ed236a831d17d diff --git a/Penumbra.GameData b/Penumbra.GameData index 3a97e5ae..066637ab 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 3a97e5aeee3b7375b333c1add5305d0ce80cbf83 +Subproject commit 066637abe05c659b79d84f52e6db33487498f433 diff --git a/Penumbra/Api/Api/GameStateApi.cs b/Penumbra/Api/Api/GameStateApi.cs index becb55ee..b035c886 100644 --- a/Penumbra/Api/Api/GameStateApi.cs +++ b/Penumbra/Api/Api/GameStateApi.cs @@ -2,8 +2,8 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Collections; +using Penumbra.Interop.Hooks.ResourceLoading; using Penumbra.Interop.PathResolving; -using Penumbra.Interop.ResourceLoading; using Penumbra.Interop.Structs; using Penumbra.Services; using Penumbra.String.Classes; diff --git a/Penumbra/Collections/Cache/CollectionCacheManager.cs b/Penumbra/Collections/Cache/CollectionCacheManager.cs index 44c12856..80d4cf1d 100644 --- a/Penumbra/Collections/Cache/CollectionCacheManager.cs +++ b/Penumbra/Collections/Cache/CollectionCacheManager.cs @@ -5,7 +5,7 @@ using Penumbra.Api; using Penumbra.Api.Enums; using Penumbra.Collections.Manager; using Penumbra.Communication; -using Penumbra.Interop.ResourceLoading; +using Penumbra.Interop.Hooks.ResourceLoading; using Penumbra.Meta; using Penumbra.Mods; using Penumbra.Mods.Groups; diff --git a/Penumbra/Collections/Cache/CustomResourceCache.cs b/Penumbra/Collections/Cache/CustomResourceCache.cs index 46c28393..e63f8637 100644 --- a/Penumbra/Collections/Cache/CustomResourceCache.cs +++ b/Penumbra/Collections/Cache/CustomResourceCache.cs @@ -1,6 +1,6 @@ using FFXIVClientStructs.FFXIV.Client.System.Resource; using Penumbra.Api.Enums; -using Penumbra.Interop.ResourceLoading; +using Penumbra.Interop.Hooks.ResourceLoading; using Penumbra.Interop.SafeHandles; using Penumbra.String.Classes; diff --git a/Penumbra/Communication/MtrlShpkLoaded.cs b/Penumbra/Communication/MtrlShpkLoaded.cs index 8aab0e0e..9d3597a8 100644 --- a/Penumbra/Communication/MtrlShpkLoaded.cs +++ b/Penumbra/Communication/MtrlShpkLoaded.cs @@ -10,7 +10,7 @@ public sealed class MtrlShpkLoaded() : EventWrapper + /// ShaderReplacementFixer = 0, } } diff --git a/Penumbra/Interop/Hooks/HookSettings.cs b/Penumbra/Interop/Hooks/HookSettings.cs index 6d511a28..ed4eb669 100644 --- a/Penumbra/Interop/Hooks/HookSettings.cs +++ b/Penumbra/Interop/Hooks/HookSettings.cs @@ -1,8 +1,14 @@ -namespace Penumbra.Interop.Hooks; - +namespace Penumbra.Interop.Hooks; + public static class HookSettings { - public const bool MetaEntryHooks = false; - public const bool MetaParentHooks = false; - public const bool VfxIdentificationHooks = false; -} + public const bool AllHooks = true; + + public const bool ObjectHooks = false && AllHooks; + public const bool ReplacementHooks = true && AllHooks; + public const bool ResourceHooks = false && AllHooks; + public const bool MetaEntryHooks = false && AllHooks; + public const bool MetaParentHooks = false && AllHooks; + public const bool VfxIdentificationHooks = false && AllHooks; + public const bool PostProcessingHooks = false && AllHooks; +} diff --git a/Penumbra/Interop/Hooks/Objects/CharacterBaseDestructor.cs b/Penumbra/Interop/Hooks/Objects/CharacterBaseDestructor.cs index e01a6550..c67bb9f3 100644 --- a/Penumbra/Interop/Hooks/Objects/CharacterBaseDestructor.cs +++ b/Penumbra/Interop/Hooks/Objects/CharacterBaseDestructor.cs @@ -19,7 +19,7 @@ public sealed unsafe class CharacterBaseDestructor : EventWrapperPtr _task = hooks.CreateHook(Name, Address, Detour, true); + => _task = hooks.CreateHook(Name, Address, Detour, HookSettings.ObjectHooks); private readonly Task> _task; diff --git a/Penumbra/Interop/Hooks/Objects/CharacterDestructor.cs b/Penumbra/Interop/Hooks/Objects/CharacterDestructor.cs index 6e10c5e3..618d0bd7 100644 --- a/Penumbra/Interop/Hooks/Objects/CharacterDestructor.cs +++ b/Penumbra/Interop/Hooks/Objects/CharacterDestructor.cs @@ -19,7 +19,7 @@ public sealed unsafe class CharacterDestructor : EventWrapperPtr _task = hooks.CreateHook(Name, Sigs.CharacterDestructor, Detour, true); + => _task = hooks.CreateHook(Name, Sigs.CharacterDestructor, Detour, HookSettings.ObjectHooks); private readonly Task> _task; diff --git a/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs b/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs index 20c96f56..663209ae 100644 --- a/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs +++ b/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs @@ -15,7 +15,7 @@ public sealed unsafe class CopyCharacter : EventWrapperPtr _task = hooks.CreateHook(Name, Address, Detour, true); + => _task = hooks.CreateHook(Name, Address, Detour, HookSettings.ObjectHooks); private readonly Task> _task; diff --git a/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs b/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs index 299f312a..56b3d853 100644 --- a/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs +++ b/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs @@ -16,7 +16,7 @@ public sealed unsafe class CreateCharacterBase : EventWrapperPtr _task = hooks.CreateHook(Name, Address, Detour, true); + => _task = hooks.CreateHook(Name, Address, Detour, HookSettings.ObjectHooks); private readonly Task> _task; diff --git a/Penumbra/Interop/Hooks/Objects/EnableDraw.cs b/Penumbra/Interop/Hooks/Objects/EnableDraw.cs index 267b4711..8b701fe5 100644 --- a/Penumbra/Interop/Hooks/Objects/EnableDraw.cs +++ b/Penumbra/Interop/Hooks/Objects/EnableDraw.cs @@ -17,7 +17,7 @@ public sealed unsafe class EnableDraw : IHookService public EnableDraw(HookManager hooks, GameState state) { _state = state; - _task = hooks.CreateHook("Enable Draw", Sigs.EnableDraw, Detour, true); + _task = hooks.CreateHook("Enable Draw", Sigs.EnableDraw, Detour, HookSettings.ObjectHooks); } private delegate void Delegate(GameObject* gameObject); diff --git a/Penumbra/Interop/Hooks/Objects/WeaponReload.cs b/Penumbra/Interop/Hooks/Objects/WeaponReload.cs index fec0a13f..da31840f 100644 --- a/Penumbra/Interop/Hooks/Objects/WeaponReload.cs +++ b/Penumbra/Interop/Hooks/Objects/WeaponReload.cs @@ -16,7 +16,7 @@ public sealed unsafe class WeaponReload : EventWrapperPtr _task = hooks.CreateHook(Name, Address, Detour, true); + => _task = hooks.CreateHook(Name, Address, Detour, HookSettings.ObjectHooks); private readonly Task> _task; diff --git a/Penumbra/Interop/Services/PreBoneDeformerReplacer.cs b/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs similarity index 81% rename from Penumbra/Interop/Services/PreBoneDeformerReplacer.cs rename to Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs index 9f553257..903484ea 100644 --- a/Penumbra/Interop/Services/PreBoneDeformerReplacer.cs +++ b/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs @@ -4,12 +4,13 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using FFXIVClientStructs.FFXIV.Client.System.Resource; using OtterGui.Services; using Penumbra.Api.Enums; +using Penumbra.Interop.Hooks.ResourceLoading; using Penumbra.Interop.PathResolving; -using Penumbra.Interop.ResourceLoading; using Penumbra.Interop.SafeHandles; using Penumbra.String.Classes; +using CharacterUtility = Penumbra.Interop.Services.CharacterUtility; -namespace Penumbra.Interop.Services; +namespace Penumbra.Interop.Hooks.PostProcessing; public sealed unsafe class PreBoneDeformerReplacer : IDisposable, IRequiredService { @@ -29,17 +30,16 @@ public sealed unsafe class PreBoneDeformerReplacer : IDisposable, IRequiredServi private readonly IFramework _framework; public PreBoneDeformerReplacer(CharacterUtility utility, CollectionResolver collectionResolver, ResourceLoader resourceLoader, - IGameInteropProvider interop, IFramework framework, CharacterBaseVTables vTables) + HookManager hooks, IFramework framework, CharacterBaseVTables vTables) { - interop.InitializeFromAttributes(this); - _utility = utility; - _collectionResolver = collectionResolver; - _resourceLoader = resourceLoader; - _framework = framework; - _humanSetupScalingHook = interop.HookFromAddress(vTables.HumanVTable[57], SetupScaling); - _humanCreateDeformerHook = interop.HookFromAddress(vTables.HumanVTable[91], CreateDeformer); - _humanSetupScalingHook.Enable(); - _humanCreateDeformerHook.Enable(); + _utility = utility; + _collectionResolver = collectionResolver; + _resourceLoader = resourceLoader; + _framework = framework; + _humanSetupScalingHook = hooks.CreateHook("HumanSetupScaling", vTables.HumanVTable[58], SetupScaling, + HookSettings.PostProcessingHooks).Result; + _humanCreateDeformerHook = hooks.CreateHook("HumanCreateDeformer", vTables.HumanVTable[101], + CreateDeformer, HookSettings.PostProcessingHooks).Result; } public void Dispose() diff --git a/Penumbra/Interop/Services/ShaderReplacementFixer.cs b/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs similarity index 91% rename from Penumbra/Interop/Services/ShaderReplacementFixer.cs rename to Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs index 95e70b45..b27ca4c5 100644 --- a/Penumbra/Interop/Services/ShaderReplacementFixer.cs +++ b/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs @@ -1,6 +1,4 @@ using Dalamud.Hooking; -using Dalamud.Plugin.Services; -using Dalamud.Utility.Signatures; using FFXIVClientStructs.FFXIV.Client.Graphics.Render; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; @@ -10,9 +8,11 @@ using Penumbra.Communication; using Penumbra.GameData; using Penumbra.Interop.Hooks.Resources; using Penumbra.Services; +using CharacterUtility = Penumbra.Interop.Services.CharacterUtility; using CSModelRenderer = FFXIVClientStructs.FFXIV.Client.Graphics.Render.ModelRenderer; +using ModelRenderer = Penumbra.Interop.Services.ModelRenderer; -namespace Penumbra.Interop.Services; +namespace Penumbra.Interop.Hooks.PostProcessing; public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredService { @@ -29,8 +29,7 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic private readonly Hook _humanOnRenderMaterialHook; - [Signature(Sigs.ModelRendererOnRenderMaterial, DetourName = nameof(ModelRendererOnRenderMaterialDetour))] - private readonly Hook _modelRendererOnRenderMaterialHook = null!; + private readonly Hook _modelRendererOnRenderMaterialHook; private readonly ResourceHandleDestructor _resourceHandleDestructor; private readonly CommunicatorService _communicator; @@ -59,19 +58,18 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic => _moddedCharacterGlassShpkCount; public ShaderReplacementFixer(ResourceHandleDestructor resourceHandleDestructor, CharacterUtility utility, ModelRenderer modelRenderer, - CommunicatorService communicator, IGameInteropProvider interop, CharacterBaseVTables vTables) + CommunicatorService communicator, HookManager hooks, CharacterBaseVTables vTables) { - interop.InitializeFromAttributes(this); _resourceHandleDestructor = resourceHandleDestructor; _utility = utility; _modelRenderer = modelRenderer; _communicator = communicator; - _humanOnRenderMaterialHook = - interop.HookFromAddress(vTables.HumanVTable[62], OnRenderHumanMaterial); + _humanOnRenderMaterialHook = hooks.CreateHook("Human.OnRenderMaterial", vTables.HumanVTable[62], + OnRenderHumanMaterial, HookSettings.PostProcessingHooks).Result; + _modelRendererOnRenderMaterialHook = hooks.CreateHook("ModelRenderer.OnRenderMaterial", + Sigs.ModelRendererOnRenderMaterial, ModelRendererOnRenderMaterialDetour, HookSettings.PostProcessingHooks).Result; _communicator.MtrlShpkLoaded.Subscribe(OnMtrlShpkLoaded, MtrlShpkLoaded.Priority.ShaderReplacementFixer); _resourceHandleDestructor.Subscribe(OnResourceHandleDestructor, ResourceHandleDestructor.Priority.ShaderReplacementFixer); - _humanOnRenderMaterialHook.Enable(); - _modelRendererOnRenderMaterialHook.Enable(); } public void Dispose() diff --git a/Penumbra/Interop/ResourceLoading/CreateFileWHook.cs b/Penumbra/Interop/Hooks/ResourceLoading/CreateFileWHook.cs similarity index 97% rename from Penumbra/Interop/ResourceLoading/CreateFileWHook.cs rename to Penumbra/Interop/Hooks/ResourceLoading/CreateFileWHook.cs index bde640d2..a8ac0608 100644 --- a/Penumbra/Interop/ResourceLoading/CreateFileWHook.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/CreateFileWHook.cs @@ -5,7 +5,7 @@ using Penumbra.String; using Penumbra.String.Classes; using Penumbra.String.Functions; -namespace Penumbra.Interop.ResourceLoading; +namespace Penumbra.Interop.Hooks.ResourceLoading; /// /// To allow XIV to load files of arbitrary path length, @@ -19,7 +19,8 @@ public unsafe class CreateFileWHook : IDisposable, IRequiredService public CreateFileWHook(IGameInteropProvider interop) { _createFileWHook = interop.HookFromImport(null, "KERNEL32.dll", "CreateFileW", 0, CreateFileWDetour); - _createFileWHook.Enable(); + if (HookSettings.ReplacementHooks) + _createFileWHook.Enable(); } /// diff --git a/Penumbra/Interop/ResourceLoading/FileReadService.cs b/Penumbra/Interop/Hooks/ResourceLoading/FileReadService.cs similarity index 92% rename from Penumbra/Interop/ResourceLoading/FileReadService.cs rename to Penumbra/Interop/Hooks/ResourceLoading/FileReadService.cs index 5edba790..199525fb 100644 --- a/Penumbra/Interop/ResourceLoading/FileReadService.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/FileReadService.cs @@ -6,16 +6,17 @@ using Penumbra.GameData; using Penumbra.Interop.Structs; using Penumbra.Util; -namespace Penumbra.Interop.ResourceLoading; +namespace Penumbra.Interop.Hooks.ResourceLoading; public unsafe class FileReadService : IDisposable, IRequiredService { public FileReadService(PerformanceTracker performance, ResourceManagerService resourceManager, IGameInteropProvider interop) { _resourceManager = resourceManager; - _performance = performance; + _performance = performance; interop.InitializeFromAttributes(this); - _readSqPackHook.Enable(); + if (HookSettings.ReplacementHooks) + _readSqPackHook.Enable(); } /// Invoked when a file is supposed to be read from SqPack. @@ -49,7 +50,7 @@ public unsafe class FileReadService : IDisposable, IRequiredService _readSqPackHook.Dispose(); } - private readonly PerformanceTracker _performance; + private readonly PerformanceTracker _performance; private readonly ResourceManagerService _resourceManager; private delegate byte ReadSqPackPrototype(nint resourceManager, SeFileDescriptor* pFileDesc, int priority, bool isSync); @@ -60,7 +61,7 @@ public unsafe class FileReadService : IDisposable, IRequiredService private byte ReadSqPackDetour(nint resourceManager, SeFileDescriptor* fileDescriptor, int priority, bool isSync) { using var performance = _performance.Measure(PerformanceType.ReadSqPack); - byte? ret = null; + byte? ret = null; _lastFileThreadResourceManager.Value = resourceManager; ReadSqPack?.Invoke(fileDescriptor, ref priority, ref isSync, ref ret); _lastFileThreadResourceManager.Value = nint.Zero; diff --git a/Penumbra/Interop/ResourceLoading/ResourceLoader.cs b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs similarity index 93% rename from Penumbra/Interop/ResourceLoading/ResourceLoader.cs rename to Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs index 4a423993..5cac2f32 100644 --- a/Penumbra/Interop/ResourceLoading/ResourceLoader.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs @@ -9,27 +9,27 @@ using Penumbra.String; using Penumbra.String.Classes; using FileMode = Penumbra.Interop.Structs.FileMode; -namespace Penumbra.Interop.ResourceLoading; +namespace Penumbra.Interop.Hooks.ResourceLoading; public unsafe class ResourceLoader : IDisposable, IService { private readonly ResourceService _resources; private readonly FileReadService _fileReadService; - private readonly TexMdlService _texMdlService; + private readonly TexMdlService _texMdlService; private ResolveData _resolvedData = ResolveData.Invalid; public ResourceLoader(ResourceService resources, FileReadService fileReadService, TexMdlService texMdlService) { - _resources = resources; + _resources = resources; _fileReadService = fileReadService; - _texMdlService = texMdlService; + _texMdlService = texMdlService; ResetResolvePath(); - _resources.ResourceRequested += ResourceHandler; + _resources.ResourceRequested += ResourceHandler; _resources.ResourceHandleIncRef += IncRefProtection; _resources.ResourceHandleDecRef += DecRefProtection; - _fileReadService.ReadSqPack += ReadSqPackDetour; + _fileReadService.ReadSqPack += ReadSqPackDetour; } /// Load a resource for a given path and a specific collection. @@ -80,10 +80,10 @@ public unsafe class ResourceLoader : IDisposable, IService public void Dispose() { - _resources.ResourceRequested -= ResourceHandler; + _resources.ResourceRequested -= ResourceHandler; _resources.ResourceHandleIncRef -= IncRefProtection; _resources.ResourceHandleDecRef -= DecRefProtection; - _fileReadService.ReadSqPack -= ReadSqPackDetour; + _fileReadService.ReadSqPack -= ReadSqPackDetour; } private void ResourceHandler(ref ResourceCategory category, ref ResourceType type, ref int hash, ref Utf8GamePath path, @@ -112,7 +112,7 @@ public unsafe class ResourceLoader : IDisposable, IService // Replace the hash and path with the correct one for the replacement. hash = ComputeHash(resolvedPath.Value.InternalName, parameters); var oldPath = path; - path = p; + path = p; returnValue = _resources.GetOriginalResource(sync, category, type, hash, path.Path, parameters); ResourceLoaded?.Invoke(returnValue, oldPath, resolvedPath.Value, data); } @@ -140,12 +140,12 @@ public unsafe class ResourceLoader : IDisposable, IService } var path = ByteString.FromSpanUnsafe(actualPath, gamePath.Path.IsNullTerminated, gamePath.Path.IsAsciiLowerCase, gamePath.Path.IsAscii); - fileDescriptor->ResourceHandle->FileNameData = path.Path; + fileDescriptor->ResourceHandle->FileNameData = path.Path; fileDescriptor->ResourceHandle->FileNameLength = path.Length; MtrlForceSync(fileDescriptor, ref isSync); returnValue = DefaultLoadResource(path, fileDescriptor, priority, isSync, data); // Return original resource handle path so that they can be loaded separately. - fileDescriptor->ResourceHandle->FileNameData = gamePath.Path.Path; + fileDescriptor->ResourceHandle->FileNameData = gamePath.Path.Path; fileDescriptor->ResourceHandle->FileNameLength = gamePath.Path.Length; } @@ -165,7 +165,7 @@ public unsafe class ResourceLoader : IDisposable, IService // Ensure that the file descriptor has its wchar_t array on aligned boundary even if it has to be odd. var fd = stackalloc char[0x11 + 0x0B + 14]; fileDescriptor->FileDescriptor = (byte*)fd + 1; - CreateFileWHook.WritePtr(fd + 0x11, gamePath.Path, gamePath.Length); + CreateFileWHook.WritePtr(fd + 0x11, gamePath.Path, gamePath.Length); CreateFileWHook.WritePtr(&fileDescriptor->Utf16FileName, gamePath.Path, gamePath.Length); // Use the SE ReadFile function. @@ -206,7 +206,7 @@ public unsafe class ResourceLoader : IDisposable, IService return; _incMode.Value = true; - returnValue = _resources.IncRef(handle); + returnValue = _resources.IncRef(handle); _incMode.Value = false; } diff --git a/Penumbra/Interop/ResourceLoading/ResourceManagerService.cs b/Penumbra/Interop/Hooks/ResourceLoading/ResourceManagerService.cs similarity index 93% rename from Penumbra/Interop/ResourceLoading/ResourceManagerService.cs rename to Penumbra/Interop/Hooks/ResourceLoading/ResourceManagerService.cs index 0479d2a6..1bff80ba 100644 --- a/Penumbra/Interop/ResourceLoading/ResourceManagerService.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/ResourceManagerService.cs @@ -8,7 +8,7 @@ using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.GameData; -namespace Penumbra.Interop.ResourceLoading; +namespace Penumbra.Interop.Hooks.ResourceLoading; public unsafe class ResourceManagerService : IRequiredService { @@ -23,10 +23,10 @@ public unsafe class ResourceManagerService : IRequiredService public ResourceHandle* FindResource(ResourceCategory cat, ResourceType ext, uint crc32) { ref var manager = ref *ResourceManager; - var catIdx = (uint)cat >> 0x18; + var catIdx = (uint)cat >> 0x18; cat = (ResourceCategory)(ushort)cat; ref var category = ref manager.ResourceGraph->Containers[(int)cat]; - var extMap = FindInMap(category.CategoryMaps[(int)catIdx].Value, (uint)ext); + var extMap = FindInMap(category.CategoryMaps[(int)catIdx].Value, (uint)ext); if (extMap == null) return null; diff --git a/Penumbra/Interop/ResourceLoading/ResourceService.cs b/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs similarity index 95% rename from Penumbra/Interop/ResourceLoading/ResourceService.cs rename to Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs index d623d72d..0b00452b 100644 --- a/Penumbra/Interop/ResourceLoading/ResourceService.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs @@ -12,7 +12,7 @@ using Penumbra.String.Classes; using Penumbra.Util; using CSResourceHandle = FFXIVClientStructs.FFXIV.Client.System.Resource.Handle.ResourceHandle; -namespace Penumbra.Interop.ResourceLoading; +namespace Penumbra.Interop.Hooks.ResourceLoading; public unsafe class ResourceService : IDisposable, IRequiredService { @@ -24,16 +24,19 @@ public unsafe class ResourceService : IDisposable, IRequiredService _performance = performance; _resourceManager = resourceManager; interop.InitializeFromAttributes(this); - _getResourceSyncHook.Enable(); - _getResourceAsyncHook.Enable(); _incRefHook = interop.HookFromAddress( (nint)CSResourceHandle.MemberFunctionPointers.IncRef, ResourceHandleIncRefDetour); - _incRefHook.Enable(); _decRefHook = interop.HookFromAddress( (nint)CSResourceHandle.MemberFunctionPointers.DecRef, ResourceHandleDecRefDetour); - _decRefHook.Enable(); + if (HookSettings.ReplacementHooks) + { + _getResourceSyncHook.Enable(); + _getResourceAsyncHook.Enable(); + _incRefHook.Enable(); + _decRefHook.Enable(); + } } public ResourceHandle* GetResource(ResourceCategory category, ResourceType type, ByteString path) diff --git a/Penumbra/Interop/ResourceLoading/TexMdlService.cs b/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs similarity index 60% rename from Penumbra/Interop/ResourceLoading/TexMdlService.cs rename to Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs index a2b43c64..28ad7aa4 100644 --- a/Penumbra/Interop/ResourceLoading/TexMdlService.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs @@ -1,109 +1,138 @@ using Dalamud.Hooking; using Dalamud.Plugin.Services; using Dalamud.Utility.Signatures; -using FFXIVClientStructs.FFXIV.Client.LayoutEngine; -using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; +using Lumina.Excel.GeneratedSheets2; using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.GameData; +using Penumbra.Interop.Structs; using Penumbra.String.Classes; +using FileMode = Penumbra.Interop.Structs.FileMode; +using ResourceHandle = FFXIVClientStructs.FFXIV.Client.System.Resource.Handle.ResourceHandle; -namespace Penumbra.Interop.ResourceLoading; +namespace Penumbra.Interop.Hooks.ResourceLoading; public unsafe class TexMdlService : IDisposable, IRequiredService { /// Custom ulong flag to signal our files as opposed to SE files. public static readonly nint CustomFileFlag = new(0xDEADBEEF); - + /// /// We need to keep a list of all CRC64 hash values of our replaced Mdl and Tex files, /// i.e. CRC32 of filename in the lower bytes, CRC32 of parent path in the upper bytes. /// public IReadOnlySet CustomFileCrc => _customFileCrc; - + public TexMdlService(IGameInteropProvider interop) { interop.InitializeFromAttributes(this); - //_checkFileStateHook.Enable(); - //_loadTexFileExternHook.Enable(); - //_loadMdlFileExternHook.Enable(); + if (HookSettings.ReplacementHooks) + { + _checkFileStateHook.Enable(); + _loadMdlFileExternHook.Enable(); + _textureSomethingHook.Enable(); + _vf32Hook.Enable(); + //_loadTexFileExternHook.Enable(); + } } - + /// Add CRC64 if the given file is a model or texture file and has an associated path. public void AddCrc(ResourceType type, FullPath? path) { - if (path.HasValue && type is ResourceType.Mdl or ResourceType.Tex) + if (path.HasValue && type is ResourceType.Mdl) _customFileCrc.Add(path.Value.Crc64); } - + /// Add a fixed CRC64 value. public void AddCrc(ulong crc64) => _customFileCrc.Add(crc64); - + public void Dispose() { - //_checkFileStateHook.Dispose(); + _checkFileStateHook.Dispose(); //_loadTexFileExternHook.Dispose(); - //_loadMdlFileExternHook.Dispose(); + _textureSomethingHook.Dispose(); + _loadMdlFileExternHook.Dispose(); + _vf32Hook.Dispose(); } - - private readonly HashSet _customFileCrc = new(); - + + private readonly HashSet _customFileCrc = []; + private delegate nint CheckFileStatePrototype(nint unk1, ulong crc64); - + + private delegate nint TextureSomethingDelegate(TextureResourceHandle* handle, int lod, SeFileDescriptor* descriptor); + [Signature(Sigs.CheckFileState, DetourName = nameof(CheckFileStateDetour))] private readonly Hook _checkFileStateHook = null!; - + + [Signature("E8 ?? ?? ?? ?? 0F B6 C8 EB ?? 4C 8B 83", DetourName = nameof(TextureSomethingDetour))] + private readonly Hook _textureSomethingHook = null!; + + private nint TextureSomethingDetour(TextureResourceHandle* handle, int lod, SeFileDescriptor* descriptor) + { + //Penumbra.Log.Information($"SomethingDetour {handle->Handle.FileName()}"); + //if (!handle->Handle.GamePath(out var path) || !path.IsRooted()) + return _textureSomethingHook.Original(handle, lod, descriptor); + + descriptor->FileMode = FileMode.LoadUnpackedResource; + return _loadTexFileLocal.Invoke((ResourceHandle*)handle, lod, (nint)descriptor, true); + } + /// /// The function that checks a files CRC64 to determine whether it is 'protected'. /// We use it to check against our stored CRC64s and if it corresponds, we return the custom flag. /// private nint CheckFileStateDetour(nint ptr, ulong crc64) => _customFileCrc.Contains(crc64) ? CustomFileFlag : _checkFileStateHook.Original(ptr, crc64); - - + + private delegate byte LoadTexFileLocalDelegate(ResourceHandle* handle, int unk1, nint unk2, bool unk3); - + /// We use the local functions for our own files in the extern hook. [Signature(Sigs.LoadTexFileLocal)] private readonly LoadTexFileLocalDelegate _loadTexFileLocal = null!; - + private delegate byte LoadMdlFileLocalPrototype(ResourceHandle* handle, nint unk1, bool unk2); - + /// We use the local functions for our own files in the extern hook. [Signature(Sigs.LoadMdlFileLocal)] private readonly LoadMdlFileLocalPrototype _loadMdlFileLocal = null!; - - + + private delegate byte LoadTexFileExternPrototype(ResourceHandle* handle, int unk1, nint unk2, bool unk3, nint unk4); + + private delegate byte TexResourceHandleVf32Prototype(TextureResourceHandle* handle, SeFileDescriptor* descriptor, byte unk2); + + [Signature("40 53 55 41 54 41 55 41 56 41 57 48 81 EC ?? ?? ?? ?? 48 8B D9", DetourName = nameof(Vf32Detour))] + private readonly Hook _vf32Hook = null!; - private delegate byte TexResourceHandleVf32Prototype(ResourceHandle* handle, nint unk1, byte unk2); - - //[Signature("40 53 55 41 54 41 55 41 56 41 57 48 81 EC ?? ?? ?? ?? 48 8B D9", DetourName = nameof(Vf32Detour))] - //private readonly Hook _vf32Hook = null!; - // - //private byte Vf32Detour(ResourceHandle* handle, nint unk1, byte unk2) - //{ - // var ret = _vf32Hook.Original(handle, unk1, unk2); - // return _loadTexFileLocal() - //} - + private byte Vf32Detour(TextureResourceHandle* handle, SeFileDescriptor* descriptor, byte unk2) + { + //if (handle->Handle.GamePath(out var path) && path.IsRooted()) + //{ + // Penumbra.Log.Information($"Replacing {descriptor->FileMode} with {FileMode.LoadSqPackResource} in VF32 for {path}."); + // descriptor->FileMode = FileMode.LoadSqPackResource; + //} + + var ret = _vf32Hook.Original(handle, descriptor, unk2); + return ret; + } + //[Signature(Sigs.LoadTexFileExtern, DetourName = nameof(LoadTexFileExternDetour))] //private readonly Hook _loadTexFileExternHook = null!; - + /// We hook the extern functions to just return the local one if given the custom flag as last argument. //private byte LoadTexFileExternDetour(ResourceHandle* resourceHandle, int unk1, nint unk2, bool unk3, nint ptr) // => ptr.Equals(CustomFileFlag) // ? _loadTexFileLocal.Invoke(resourceHandle, unk1, unk2, unk3) // : _loadTexFileExternHook.Original(resourceHandle, unk1, unk2, unk3, ptr); - public delegate byte LoadMdlFileExternPrototype(ResourceHandle* handle, nint unk1, bool unk2, nint unk3); - - + + [Signature(Sigs.LoadMdlFileExtern, DetourName = nameof(LoadMdlFileExternDetour))] private readonly Hook _loadMdlFileExternHook = null!; - + /// We hook the extern functions to just return the local one if given the custom flag as last argument. private byte LoadMdlFileExternDetour(ResourceHandle* resourceHandle, nint unk1, bool unk2, nint ptr) => ptr.Equals(CustomFileFlag) diff --git a/Penumbra/Interop/Hooks/Resources/ApricotResourceLoad.cs b/Penumbra/Interop/Hooks/Resources/ApricotResourceLoad.cs index 2e5698a3..511e842f 100644 --- a/Penumbra/Interop/Hooks/Resources/ApricotResourceLoad.cs +++ b/Penumbra/Interop/Hooks/Resources/ApricotResourceLoad.cs @@ -11,7 +11,7 @@ public sealed unsafe class ApricotResourceLoad : FastHook("Load Apricot Resource", Sigs.ApricotResourceLoad, Detour, true); + Task = hooks.CreateHook("Load Apricot Resource", Sigs.ApricotResourceLoad, Detour, HookSettings.ResourceHooks); } public delegate byte Delegate(ResourceHandle* handle, nint unk1, byte unk2); diff --git a/Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs b/Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs index 5ef3bf37..8447762b 100644 --- a/Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs +++ b/Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs @@ -14,7 +14,7 @@ public sealed unsafe class LoadMtrlShpk : FastHook { _gameState = gameState; _communicator = communicator; - Task = hooks.CreateHook("Load Material Shaders", Sigs.LoadMtrlShpk, Detour, true); + Task = hooks.CreateHook("Load Material Shaders", Sigs.LoadMtrlShpk, Detour, HookSettings.ResourceHooks); } public delegate byte Delegate(MaterialResourceHandle* mtrlResourceHandle); diff --git a/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs b/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs index 14a011ea..7bc3c7b0 100644 --- a/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs +++ b/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs @@ -11,7 +11,7 @@ public sealed unsafe class LoadMtrlTex : FastHook public LoadMtrlTex(HookManager hooks, GameState gameState) { _gameState = gameState; - Task = hooks.CreateHook("Load Material Textures", Sigs.LoadMtrlTex, Detour, true); + Task = hooks.CreateHook("Load Material Textures", Sigs.LoadMtrlTex, Detour, HookSettings.ResourceHooks); } public delegate byte Delegate(MaterialResourceHandle* mtrlResourceHandle); diff --git a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs index a7e82b72..8fa6d861 100644 --- a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs +++ b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs @@ -59,7 +59,8 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable _resolveVfxPathHook = Create( $"{name}.{nameof(ResolveVfx)}", hooks, vTable[93], type, ResolveVfx, ResolveVfxHuman); _resolveEidPathHook = Create( $"{name}.{nameof(ResolveEid)}", hooks, vTable[94], ResolveEid); // @formatter:on - Enable(); + if (HookSettings.ResourceHooks) + Enable(); } public void Enable() diff --git a/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs b/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs index ac3f504a..cd4a53c4 100644 --- a/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs +++ b/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs @@ -23,7 +23,7 @@ public sealed unsafe class ResourceHandleDestructor : EventWrapperPtr _task = hooks.CreateHook(Name, Sigs.ResourceHandleDestructor, Detour, true); + => _task = hooks.CreateHook(Name, Sigs.ResourceHandleDestructor, Detour, HookSettings.ResourceHooks); private readonly Task> _task; diff --git a/Penumbra/Interop/PathResolving/MetaState.cs b/Penumbra/Interop/PathResolving/MetaState.cs index 5eacbfb0..e709c210 100644 --- a/Penumbra/Interop/PathResolving/MetaState.cs +++ b/Penumbra/Interop/PathResolving/MetaState.cs @@ -4,12 +4,12 @@ using OtterGui.Services; using Penumbra.Collections; using Penumbra.Api.Enums; using Penumbra.GameData.Structs; -using Penumbra.Interop.ResourceLoading; using Penumbra.Interop.Services; using Penumbra.Services; using Penumbra.String.Classes; using CharacterUtility = Penumbra.Interop.Services.CharacterUtility; using Penumbra.Interop.Hooks.Objects; +using Penumbra.Interop.Hooks.ResourceLoading; namespace Penumbra.Interop.PathResolving; diff --git a/Penumbra/Interop/PathResolving/PathResolver.cs b/Penumbra/Interop/PathResolving/PathResolver.cs index cc3e0e9b..49035dc8 100644 --- a/Penumbra/Interop/PathResolving/PathResolver.cs +++ b/Penumbra/Interop/PathResolving/PathResolver.cs @@ -3,8 +3,8 @@ using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.Collections.Manager; +using Penumbra.Interop.Hooks.ResourceLoading; using Penumbra.Interop.Processing; -using Penumbra.Interop.ResourceLoading; using Penumbra.String.Classes; using Penumbra.Util; diff --git a/Penumbra/Interop/PathResolving/SubfileHelper.cs b/Penumbra/Interop/PathResolving/SubfileHelper.cs index 44a152f0..836cf731 100644 --- a/Penumbra/Interop/PathResolving/SubfileHelper.cs +++ b/Penumbra/Interop/PathResolving/SubfileHelper.cs @@ -1,8 +1,8 @@ using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Collections; +using Penumbra.Interop.Hooks.ResourceLoading; using Penumbra.Interop.Hooks.Resources; -using Penumbra.Interop.ResourceLoading; using Penumbra.Interop.Structs; using Penumbra.String.Classes; diff --git a/Penumbra/Interop/Processing/FilePostProcessService.cs b/Penumbra/Interop/Processing/FilePostProcessService.cs index 0dc62b3d..bba53c94 100644 --- a/Penumbra/Interop/Processing/FilePostProcessService.cs +++ b/Penumbra/Interop/Processing/FilePostProcessService.cs @@ -1,7 +1,7 @@ using System.Collections.Frozen; using OtterGui.Services; using Penumbra.Api.Enums; -using Penumbra.Interop.ResourceLoading; +using Penumbra.Interop.Hooks.ResourceLoading; using Penumbra.Interop.Structs; using Penumbra.String; diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.cs b/Penumbra/Interop/ResourceTree/ResolveContext.cs index ca8836b0..a852a4cc 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.cs @@ -9,8 +9,8 @@ using Penumbra.Collections; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; +using Penumbra.Interop.Hooks.PostProcessing; using Penumbra.Interop.PathResolving; -using Penumbra.Interop.Services; using Penumbra.String; using Penumbra.String.Classes; using Penumbra.UI; diff --git a/Penumbra/Interop/ResourceTree/ResourceTree.cs b/Penumbra/Interop/ResourceTree/ResourceTree.cs index b8bad84a..810c946d 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTree.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTree.cs @@ -5,7 +5,7 @@ using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; -using Penumbra.Interop.Services; +using Penumbra.Interop.Hooks.PostProcessing; using Penumbra.UI; using CustomizeData = FFXIVClientStructs.FFXIV.Client.Game.Character.CustomizeData; using CustomizeIndex = Dalamud.Game.ClientState.Objects.Enums.CustomizeIndex; @@ -31,7 +31,8 @@ public class ResourceTree public CustomizeData CustomizeData; public GenderRace RaceCode; - public ResourceTree(string name, string anonymizedName, int gameObjectIndex, nint gameObjectAddress, nint drawObjectAddress, bool localPlayerRelated, bool playerRelated, bool networked, string collectionName, string anonymizedCollectionName) + public ResourceTree(string name, string anonymizedName, int gameObjectIndex, nint gameObjectAddress, nint drawObjectAddress, + bool localPlayerRelated, bool playerRelated, bool networked, string collectionName, string anonymizedCollectionName) { Name = name; AnonymizedName = anonymizedName; @@ -61,9 +62,10 @@ public class ResourceTree var human = modelType == CharacterBase.ModelType.Human ? (Human*)model : null; var equipment = modelType switch { - CharacterBase.ModelType.Human => new ReadOnlySpan(&human->Head, 10), - CharacterBase.ModelType.DemiHuman => new ReadOnlySpan(Unsafe.AsPointer(ref character->DrawData.EquipmentModelIds[0]), 10), - _ => ReadOnlySpan.Empty, + CharacterBase.ModelType.Human => new ReadOnlySpan(&human->Head, 10), + CharacterBase.ModelType.DemiHuman => new ReadOnlySpan( + Unsafe.AsPointer(ref character->DrawData.EquipmentModelIds[0]), 10), + _ => ReadOnlySpan.Empty, }; ModelId = character->CharacterData.ModelCharaId; CustomizeData = character->DrawData.CustomizeData; @@ -112,15 +114,17 @@ public class ResourceTree { if (baseSubObject->GetObjectType() != FFXIVClientStructs.FFXIV.Client.Graphics.Scene.ObjectType.CharacterBase) continue; + var subObject = (CharacterBase*)baseSubObject; if (subObject->GetModelType() != CharacterBase.ModelType.Weapon) continue; - var weapon = (Weapon*)subObject; + + var weapon = (Weapon*)subObject; // This way to tell apart MainHand and OffHand is not always accurate, but seems good enough for what we're doing with it. var slot = weaponIndex > 0 ? EquipSlot.OffHand : EquipSlot.MainHand; - var equipment = new CharacterArmor(weapon->ModelSetId, (byte)weapon->Variant, (byte)weapon->ModelUnknown); + var equipment = new CharacterArmor(weapon->ModelSetId, (byte)weapon->Variant, new StainIds(weapon->Stain1, weapon->Stain2)); var weaponType = weapon->SecondaryId; var genericContext = globalContext.CreateContext(subObject, 0xFFFFFFFFu, slot, equipment, weaponType); @@ -152,6 +156,7 @@ public class ResourceTree ++weaponIndex; } + Nodes.InsertRange(0, weaponNodes); } @@ -167,10 +172,11 @@ public class ResourceTree { if (globalContext.WithUiData) { - pbdNode = pbdNode.Clone(); + pbdNode = pbdNode.Clone(); pbdNode.FallbackName = "Racial Deformer"; - pbdNode.Icon = ChangedItemDrawer.ChangedItemIcon.Customization; + pbdNode.Icon = ChangedItemDrawer.ChangedItemIcon.Customization; } + Nodes.Add(pbdNode); } } @@ -184,10 +190,11 @@ public class ResourceTree { if (globalContext.WithUiData) { - decalNode = decalNode.Clone(); + decalNode = decalNode.Clone(); decalNode.FallbackName = "Face Decal"; decalNode.Icon = ChangedItemDrawer.ChangedItemIcon.Customization; } + Nodes.Add(decalNode); } @@ -200,10 +207,11 @@ public class ResourceTree { if (globalContext.WithUiData) { - legacyDecalNode = legacyDecalNode.Clone(); + legacyDecalNode = legacyDecalNode.Clone(); legacyDecalNode.FallbackName = "Legacy Body Decal"; legacyDecalNode.Icon = ChangedItemDrawer.ChangedItemIcon.Customization; } + Nodes.Add(legacyDecalNode); } } diff --git a/Penumbra/Interop/Services/DecalReverter.cs b/Penumbra/Interop/Services/DecalReverter.cs index 17d8d2e0..21b51fd2 100644 --- a/Penumbra/Interop/Services/DecalReverter.cs +++ b/Penumbra/Interop/Services/DecalReverter.cs @@ -1,7 +1,7 @@ using FFXIVClientStructs.FFXIV.Client.System.Resource; using Penumbra.Api.Enums; using Penumbra.Collections; -using Penumbra.Interop.ResourceLoading; +using Penumbra.Interop.Hooks.ResourceLoading; using Penumbra.String.Classes; namespace Penumbra.Interop.Services; diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index b1ad0b78..9f2db2e6 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -8,7 +8,6 @@ using Penumbra.Api; using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.Collections.Cache; -using Penumbra.Interop.ResourceLoading; using Penumbra.Interop.PathResolving; using Penumbra.Services; using Penumbra.Interop.Services; @@ -22,6 +21,7 @@ using Penumbra.GameData.Enums; using Penumbra.UI; using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManager; using System.Xml.Linq; +using Penumbra.Interop.Hooks.ResourceLoading; namespace Penumbra; diff --git a/Penumbra/Penumbra.csproj b/Penumbra/Penumbra.csproj index ed5c5e30..2e53bd22 100644 --- a/Penumbra/Penumbra.csproj +++ b/Penumbra/Penumbra.csproj @@ -37,7 +37,6 @@ $(AppData)\XIVLauncher\addon\Hooks\dev\ - H:\Projects\FFPlugins\Dalamud\bin\Release\ diff --git a/Penumbra/Services/CrashHandlerService.cs b/Penumbra/Services/CrashHandlerService.cs index 25c6cf57..9103b29c 100644 --- a/Penumbra/Services/CrashHandlerService.cs +++ b/Penumbra/Services/CrashHandlerService.cs @@ -7,8 +7,8 @@ using Penumbra.Communication; using Penumbra.CrashHandler; using Penumbra.CrashHandler.Buffers; using Penumbra.GameData.Actors; +using Penumbra.Interop.Hooks.ResourceLoading; using Penumbra.Interop.PathResolving; -using Penumbra.Interop.ResourceLoading; using Penumbra.Interop.Structs; using Penumbra.String; using Penumbra.String.Classes; diff --git a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs index c53f1b8e..935f11e3 100644 --- a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs +++ b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs @@ -8,8 +8,8 @@ using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.GameData.Actors; using Penumbra.GameData.Enums; +using Penumbra.Interop.Hooks.ResourceLoading; using Penumbra.Interop.Hooks.Resources; -using Penumbra.Interop.ResourceLoading; using Penumbra.Interop.Structs; using Penumbra.Services; using Penumbra.String; diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index 41f28ab9..9a03f384 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -25,7 +25,6 @@ using Penumbra.GameData.Interop; using Penumbra.Import.Structs; using Penumbra.Import.Textures; using Penumbra.Interop.PathResolving; -using Penumbra.Interop.ResourceLoading; using Penumbra.Interop.Services; using Penumbra.Interop.Structs; using Penumbra.Mods; @@ -40,6 +39,8 @@ using CharacterUtility = Penumbra.Interop.Services.CharacterUtility; using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManager; using ImGuiClip = OtterGui.ImGuiClip; using Penumbra.Api.IpcTester; +using Penumbra.Interop.Hooks.PostProcessing; +using Penumbra.Interop.Hooks.ResourceLoading; namespace Penumbra.UI.Tabs.Debug; diff --git a/Penumbra/UI/Tabs/ResourceTab.cs b/Penumbra/UI/Tabs/ResourceTab.cs index 14d4ed41..a4dbba2f 100644 --- a/Penumbra/UI/Tabs/ResourceTab.cs +++ b/Penumbra/UI/Tabs/ResourceTab.cs @@ -8,7 +8,7 @@ using OtterGui; using OtterGui.Raii; using OtterGui.Services; using OtterGui.Widgets; -using Penumbra.Interop.ResourceLoading; +using Penumbra.Interop.Hooks.ResourceLoading; using Penumbra.String.Classes; namespace Penumbra.UI.Tabs; From 4026dd58672fb38a6c83dfe1309ded1b10a9aa67 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 5 Jul 2024 12:14:31 +0200 Subject: [PATCH 271/433] Change texture handling. --- Penumbra.GameData | 2 +- .../Hooks/ResourceLoading/TexMdlService.cs | 128 +++++++++--------- Penumbra/Interop/Structs/ResourceHandle.cs | 6 + 3 files changed, 73 insertions(+), 63 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 066637ab..19923f8d 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 066637abe05c659b79d84f52e6db33487498f433 +Subproject commit 19923f8d5649f11edcfae710c26d6273cf2e9d62 diff --git a/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs b/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs index 28ad7aa4..793d15af 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs @@ -1,93 +1,110 @@ using Dalamud.Hooking; using Dalamud.Plugin.Services; using Dalamud.Utility.Signatures; -using Lumina.Excel.GeneratedSheets2; using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.GameData; using Penumbra.Interop.Structs; using Penumbra.String.Classes; -using FileMode = Penumbra.Interop.Structs.FileMode; using ResourceHandle = FFXIVClientStructs.FFXIV.Client.System.Resource.Handle.ResourceHandle; namespace Penumbra.Interop.Hooks.ResourceLoading; public unsafe class TexMdlService : IDisposable, IRequiredService { + /// + /// We need to be able to obtain the requested LoD level. + /// This replicates the LoD behavior of a textures OnLoad function. + /// + private readonly struct LodService + { + public LodService(IGameInteropProvider interop) + => interop.InitializeFromAttributes(this); + + [Signature(Sigs.LodConfig)] + private readonly nint _lodConfig = nint.Zero; + + public byte GetLod(TextureResourceHandle* handle) + { + if (handle->ChangeLod) + { + var config = *(byte*)_lodConfig + 0xE; + if (config == byte.MaxValue) + return 2; + } + + return 0; + } + } + /// Custom ulong flag to signal our files as opposed to SE files. public static readonly nint CustomFileFlag = new(0xDEADBEEF); - /// - /// We need to keep a list of all CRC64 hash values of our replaced Mdl and Tex files, - /// i.e. CRC32 of filename in the lower bytes, CRC32 of parent path in the upper bytes. - /// - public IReadOnlySet CustomFileCrc - => _customFileCrc; + private readonly LodService _lodService; public TexMdlService(IGameInteropProvider interop) { interop.InitializeFromAttributes(this); + _lodService = new LodService(interop); if (HookSettings.ReplacementHooks) { _checkFileStateHook.Enable(); _loadMdlFileExternHook.Enable(); - _textureSomethingHook.Enable(); - _vf32Hook.Enable(); - //_loadTexFileExternHook.Enable(); + _textureOnLoadHook.Enable(); } } /// Add CRC64 if the given file is a model or texture file and has an associated path. public void AddCrc(ResourceType type, FullPath? path) { - if (path.HasValue && type is ResourceType.Mdl) - _customFileCrc.Add(path.Value.Crc64); + _ = type switch + { + ResourceType.Mdl when path.HasValue => _customMdlCrc.Add(path.Value.Crc64), + ResourceType.Tex when path.HasValue => _customTexCrc.Add(path.Value.Crc64), + _ => false, + }; } - /// Add a fixed CRC64 value. - public void AddCrc(ulong crc64) - => _customFileCrc.Add(crc64); - public void Dispose() { _checkFileStateHook.Dispose(); - //_loadTexFileExternHook.Dispose(); - _textureSomethingHook.Dispose(); _loadMdlFileExternHook.Dispose(); - _vf32Hook.Dispose(); + _textureOnLoadHook.Dispose(); } - private readonly HashSet _customFileCrc = []; + /// + /// We need to keep a list of all CRC64 hash values of our replaced Mdl and Tex files, + /// i.e. CRC32 of filename in the lower bytes, CRC32 of parent path in the upper bytes. + /// + private readonly HashSet _customMdlCrc = []; + + private readonly HashSet _customTexCrc = []; private delegate nint CheckFileStatePrototype(nint unk1, ulong crc64); - private delegate nint TextureSomethingDelegate(TextureResourceHandle* handle, int lod, SeFileDescriptor* descriptor); - [Signature(Sigs.CheckFileState, DetourName = nameof(CheckFileStateDetour))] private readonly Hook _checkFileStateHook = null!; - [Signature("E8 ?? ?? ?? ?? 0F B6 C8 EB ?? 4C 8B 83", DetourName = nameof(TextureSomethingDetour))] - private readonly Hook _textureSomethingHook = null!; - - private nint TextureSomethingDetour(TextureResourceHandle* handle, int lod, SeFileDescriptor* descriptor) - { - //Penumbra.Log.Information($"SomethingDetour {handle->Handle.FileName()}"); - //if (!handle->Handle.GamePath(out var path) || !path.IsRooted()) - return _textureSomethingHook.Original(handle, lod, descriptor); - - descriptor->FileMode = FileMode.LoadUnpackedResource; - return _loadTexFileLocal.Invoke((ResourceHandle*)handle, lod, (nint)descriptor, true); - } + private readonly ThreadLocal _texReturnData = new(() => default); /// /// The function that checks a files CRC64 to determine whether it is 'protected'. - /// We use it to check against our stored CRC64s and if it corresponds, we return the custom flag. + /// We use it to check against our stored CRC64s and if it corresponds, we return the custom flag for models. + /// Since Dawntrail inlined the RSF function for textures, we can not use the flag method here. + /// Instead, we signal the caller that this will fail and let it call the local function after intentionally failing. /// private nint CheckFileStateDetour(nint ptr, ulong crc64) - => _customFileCrc.Contains(crc64) ? CustomFileFlag : _checkFileStateHook.Original(ptr, crc64); + { + if (_customMdlCrc.Contains(crc64)) + return CustomFileFlag; + if (_customTexCrc.Contains(crc64)) + _texReturnData.Value = true; - private delegate byte LoadTexFileLocalDelegate(ResourceHandle* handle, int unk1, nint unk2, bool unk3); + return _checkFileStateHook.Original(ptr, crc64); + } + + private delegate byte LoadTexFileLocalDelegate(TextureResourceHandle* handle, int unk1, SeFileDescriptor* unk2, bool unk3); /// We use the local functions for our own files in the extern hook. [Signature(Sigs.LoadTexFileLocal)] @@ -99,36 +116,23 @@ public unsafe class TexMdlService : IDisposable, IRequiredService [Signature(Sigs.LoadMdlFileLocal)] private readonly LoadMdlFileLocalPrototype _loadMdlFileLocal = null!; + private delegate byte TexResourceHandleOnLoadPrototype(TextureResourceHandle* handle, SeFileDescriptor* descriptor, byte unk2); - private delegate byte LoadTexFileExternPrototype(ResourceHandle* handle, int unk1, nint unk2, bool unk3, nint unk4); + [Signature(Sigs.TexResourceHandleOnLoad, DetourName = nameof(OnLoadDetour))] + private readonly Hook _textureOnLoadHook = null!; - private delegate byte TexResourceHandleVf32Prototype(TextureResourceHandle* handle, SeFileDescriptor* descriptor, byte unk2); - - [Signature("40 53 55 41 54 41 55 41 56 41 57 48 81 EC ?? ?? ?? ?? 48 8B D9", DetourName = nameof(Vf32Detour))] - private readonly Hook _vf32Hook = null!; - - private byte Vf32Detour(TextureResourceHandle* handle, SeFileDescriptor* descriptor, byte unk2) + private byte OnLoadDetour(TextureResourceHandle* handle, SeFileDescriptor* descriptor, byte unk2) { - //if (handle->Handle.GamePath(out var path) && path.IsRooted()) - //{ - // Penumbra.Log.Information($"Replacing {descriptor->FileMode} with {FileMode.LoadSqPackResource} in VF32 for {path}."); - // descriptor->FileMode = FileMode.LoadSqPackResource; - //} + var ret = _textureOnLoadHook.Original(handle, descriptor, unk2); + if (!_texReturnData.Value) + return ret; - var ret = _vf32Hook.Original(handle, descriptor, unk2); - return ret; + // Function failed on a replaced texture, call local. + _texReturnData.Value = false; + return _loadTexFileLocal(handle, _lodService.GetLod(handle), descriptor, unk2 != 0); } - //[Signature(Sigs.LoadTexFileExtern, DetourName = nameof(LoadTexFileExternDetour))] - //private readonly Hook _loadTexFileExternHook = null!; - - /// We hook the extern functions to just return the local one if given the custom flag as last argument. - //private byte LoadTexFileExternDetour(ResourceHandle* resourceHandle, int unk1, nint unk2, bool unk3, nint ptr) - // => ptr.Equals(CustomFileFlag) - // ? _loadTexFileLocal.Invoke(resourceHandle, unk1, unk2, unk3) - // : _loadTexFileExternHook.Original(resourceHandle, unk1, unk2, unk3, ptr); - public delegate byte LoadMdlFileExternPrototype(ResourceHandle* handle, nint unk1, bool unk2, nint unk3); - + private delegate byte LoadMdlFileExternPrototype(ResourceHandle* handle, nint unk1, bool unk2, nint unk3); [Signature(Sigs.LoadMdlFileExtern, DetourName = nameof(LoadMdlFileExternDetour))] private readonly Hook _loadMdlFileExternHook = null!; diff --git a/Penumbra/Interop/Structs/ResourceHandle.cs b/Penumbra/Interop/Structs/ResourceHandle.cs index 058b9004..6e428f25 100644 --- a/Penumbra/Interop/Structs/ResourceHandle.cs +++ b/Penumbra/Interop/Structs/ResourceHandle.cs @@ -14,6 +14,12 @@ public unsafe struct TextureResourceHandle [FieldOffset(0x0)] public CsHandle.TextureResourceHandle CsHandle; + + [FieldOffset(0x104)] + public byte SomeLodFlag; + + public bool ChangeLod + => (SomeLodFlag & 1) != 0; } public enum LoadState : byte From 1284037554fdeebd4e21d7a3ed846629b1c43e6b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 5 Jul 2024 14:39:03 +0200 Subject: [PATCH 272/433] Fix some hooks. --- Penumbra.GameData | 2 +- Penumbra/Interop/Hooks/HookSettings.cs | 10 ++-- .../Interop/Hooks/Meta/GetEqpIndirect2.cs | 1 - Penumbra/Interop/Hooks/Meta/GmpHook.cs | 47 ++++--------------- .../Interop/Hooks/Meta/ModelLoadComplete.cs | 2 +- Penumbra/Interop/Hooks/Meta/RspBustHook.cs | 10 ++-- Penumbra/Interop/Hooks/Meta/RspHeightHook.cs | 9 ++-- .../Interop/Hooks/Objects/CopyCharacter.cs | 5 +- .../Hooks/Objects/CreateCharacterBase.cs | 6 +-- .../Interop/Structs/CharacterUtilityData.cs | 10 ++-- Penumbra/Interop/Structs/MetaIndex.cs | 13 +++-- 11 files changed, 40 insertions(+), 75 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 19923f8d..27d15145 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 19923f8d5649f11edcfae710c26d6273cf2e9d62 +Subproject commit 27d15145567c11e2bb1902857f8db25f02189390 diff --git a/Penumbra/Interop/Hooks/HookSettings.cs b/Penumbra/Interop/Hooks/HookSettings.cs index ed4eb669..9dd8d74f 100644 --- a/Penumbra/Interop/Hooks/HookSettings.cs +++ b/Penumbra/Interop/Hooks/HookSettings.cs @@ -4,11 +4,11 @@ public static class HookSettings { public const bool AllHooks = true; - public const bool ObjectHooks = false && AllHooks; + public const bool ObjectHooks = true && AllHooks; public const bool ReplacementHooks = true && AllHooks; - public const bool ResourceHooks = false && AllHooks; - public const bool MetaEntryHooks = false && AllHooks; - public const bool MetaParentHooks = false && AllHooks; + public const bool ResourceHooks = true && AllHooks; + public const bool MetaEntryHooks = true && AllHooks; + public const bool MetaParentHooks = true && AllHooks; public const bool VfxIdentificationHooks = false && AllHooks; - public const bool PostProcessingHooks = false && AllHooks; + public const bool PostProcessingHooks = true && AllHooks; } diff --git a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs index 3767c4a2..e90674a8 100644 --- a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs +++ b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs @@ -1,6 +1,5 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; -using Penumbra.Collections; using Penumbra.GameData; using Penumbra.Interop.PathResolving; diff --git a/Penumbra/Interop/Hooks/Meta/GmpHook.cs b/Penumbra/Interop/Hooks/Meta/GmpHook.cs index 329a8beb..12b221d9 100644 --- a/Penumbra/Interop/Hooks/Meta/GmpHook.cs +++ b/Penumbra/Interop/Hooks/Meta/GmpHook.cs @@ -1,3 +1,5 @@ +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using Lumina.Data.Parsing.Uld; using OtterGui.Services; using Penumbra.GameData; using Penumbra.Interop.PathResolving; @@ -8,12 +10,10 @@ namespace Penumbra.Interop.Hooks.Meta; public unsafe class GmpHook : FastHook, IDisposable { - public delegate nint Delegate(nint gmpResource, uint dividedHeadId); + public delegate ulong Delegate(CharacterUtility* characterUtility, ulong* outputEntry, ushort setId); private readonly MetaState _metaState; - private static readonly Finalizer StablePointer = new(); - public GmpHook(HookManager hooks, MetaState metaState) { _metaState = metaState; @@ -21,50 +21,21 @@ public unsafe class GmpHook : FastHook, IDisposable _metaState.Config.ModsEnabled += Toggle; } - /// - /// This function returns a pointer to the correct block in the GMP file, if it exists - cf. . - /// To work around this, we just have a single stable ulong accessible and offset the pointer to this by the required distance, - /// which is defined by the modulo of the original ID and the block size, if we return our own custom gmp entry. - /// - private nint Detour(nint gmpResource, uint dividedHeadId) + private ulong Detour(CharacterUtility* characterUtility, ulong* outputEntry, ushort setId) { - nint ret; + ulong ret; if (_metaState.GmpCollection.TryPeek(out var collection) && collection.Collection is { Valid: true, ModCollection.MetaCache: { } cache } && cache.Gmp.TryGetValue(new GmpIdentifier(collection.Id), out var entry)) - { - if (entry.Entry.Enabled) - { - *StablePointer.Pointer = entry.Entry.Value; - // This function already gets the original ID divided by the block size, so we can compute the modulo with a single multiplication and addition. - // We then go backwards from our pointer because this gets added by the calling functions. - ret = (nint)(StablePointer.Pointer - (collection.Id.Id - dividedHeadId * ExpandedEqpGmpBase.BlockSize)); - } - else - { - ret = nint.Zero; - } - } + ret = (*outputEntry) = entry.Entry.Enabled ? entry.Entry.Value : 0ul; else - { - ret = Task.Result.Original(gmpResource, dividedHeadId); - } + ret = Task.Result.Original(characterUtility, outputEntry, setId); - Penumbra.Log.Excessive($"[GetGmpFlags] Invoked on 0x{gmpResource:X} with {dividedHeadId}, returned {ret:X10}."); + Penumbra.Log.Excessive( + $"[GetGmpFlags] Invoked on 0x{(ulong)characterUtility:X} for {setId} with 0x{(ulong)outputEntry:X} (={*outputEntry:X}), returned {ret:X10}."); return ret; } - /// Allocate and clean up our single stable ulong pointer. - private class Finalizer - { - public readonly ulong* Pointer = (ulong*)Marshal.AllocHGlobal(8); - - ~Finalizer() - { - Marshal.FreeHGlobal((nint)Pointer); - } - } - public void Dispose() => _metaState.Config.ModsEnabled -= Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs b/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs index 79e7f6a6..c1803745 100644 --- a/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs +++ b/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs @@ -13,7 +13,7 @@ public sealed unsafe class ModelLoadComplete : FastHook("Model Load Complete", vtables.HumanVTable[58], Detour, HookSettings.MetaParentHooks); + Task = hooks.CreateHook("Model Load Complete", vtables.HumanVTable[59], Detour, HookSettings.MetaParentHooks); } public delegate void Delegate(DrawObject* drawObject); diff --git a/Penumbra/Interop/Hooks/Meta/RspBustHook.cs b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs index eb8a8a37..e08dc393 100644 --- a/Penumbra/Interop/Hooks/Meta/RspBustHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs @@ -10,8 +10,7 @@ namespace Penumbra.Interop.Hooks.Meta; public unsafe class RspBustHook : FastHook, IDisposable { - public delegate float* Delegate(nint cmpResource, float* storage, Race race, byte gender, byte isSecondSubRace, byte bodyType, - byte bustSize); + public delegate float* Delegate(nint cmpResource, float* storage, SubRace race, byte gender, byte bodyType, byte bustSize); private readonly MetaState _metaState; private readonly MetaFileManager _metaFileManager; @@ -24,7 +23,7 @@ public unsafe class RspBustHook : FastHook, IDisposable _metaState.Config.ModsEnabled += Toggle; } - private float* Detour(nint cmpResource, float* storage, Race race, byte gender, byte isSecondSubRace, byte bodyType, byte bustSize) + private float* Detour(nint cmpResource, float* storage, SubRace clan, byte gender, byte bodyType, byte bustSize) { if (gender == 0) { @@ -38,7 +37,6 @@ public unsafe class RspBustHook : FastHook, IDisposable if (bodyType < 2 && _metaState.RspCollection.TryPeek(out var collection) && collection is { Valid: true, ModCollection.MetaCache: { } cache }) { var bustScale = bustSize / 100f; - var clan = (SubRace)(((int)race - 1) * 2 + 1 + isSecondSubRace); var ptr = CmpFile.GetDefaults(_metaFileManager, clan, RspAttribute.BustMinX); storage[0] = GetValue(0, RspAttribute.BustMinX, RspAttribute.BustMaxX); storage[1] = GetValue(1, RspAttribute.BustMinY, RspAttribute.BustMaxY); @@ -58,11 +56,11 @@ public unsafe class RspBustHook : FastHook, IDisposable } else { - ret = Task.Result.Original(cmpResource, storage, race, gender, isSecondSubRace, bodyType, bustSize); + ret = Task.Result.Original(cmpResource, storage, clan, gender, bodyType, bustSize); } Penumbra.Log.Excessive( - $"[GetRspBust] Invoked on 0x{cmpResource:X} with {race}, {(Gender)(gender + 1)}, {isSecondSubRace == 1}, {bodyType}, {bustSize}, returned {storage[0]}, {storage[1]}, {storage[2]}."); + $"[GetRspBust] Invoked on 0x{cmpResource:X} with {clan}, {(Gender)(gender + 1)}, {bodyType}, {bustSize}, returned {storage[0]}, {storage[1]}, {storage[2]}."); return ret; } diff --git a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs index f8f9e51e..20e3c939 100644 --- a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs @@ -10,7 +10,7 @@ namespace Penumbra.Interop.Hooks.Meta; public class RspHeightHook : FastHook, IDisposable { - public delegate float Delegate(nint cmpResource, Race clan, byte gender, byte isSecondSubRace, byte bodyType, byte height); + public delegate float Delegate(nint cmpResource, SubRace clan, byte gender, byte bodyType, byte height); private readonly MetaState _metaState; private readonly MetaFileManager _metaFileManager; @@ -23,7 +23,7 @@ public class RspHeightHook : FastHook, IDisposable _metaState.Config.ModsEnabled += Toggle; } - private unsafe float Detour(nint cmpResource, Race race, byte gender, byte isSecondSubRace, byte bodyType, byte height) + private unsafe float Detour(nint cmpResource, SubRace clan, byte gender, byte bodyType, byte height) { float scale; if (bodyType < 2 @@ -36,7 +36,6 @@ public class RspHeightHook : FastHook, IDisposable if (height > 100) height = 0; - var clan = (SubRace)(((int)race - 1) * 2 + 1 + isSecondSubRace); var (minIdent, maxIdent) = gender == 0 ? (new RspIdentifier(clan, RspAttribute.MaleMinSize), new RspIdentifier(clan, RspAttribute.MaleMaxSize)) : (new RspIdentifier(clan, RspAttribute.FemaleMinSize), new RspIdentifier(clan, RspAttribute.FemaleMaxSize)); @@ -68,11 +67,11 @@ public class RspHeightHook : FastHook, IDisposable } else { - scale = Task.Result.Original(cmpResource, race, gender, isSecondSubRace, bodyType, height); + scale = Task.Result.Original(cmpResource, clan, gender, bodyType, height); } Penumbra.Log.Excessive( - $"[GetRspHeight] Invoked on 0x{cmpResource:X} with {race}, {(Gender)(gender + 1)}, {isSecondSubRace == 1}, {bodyType}, {height}, returned {scale}."); + $"[GetRspHeight] Invoked on 0x{cmpResource:X} with {clan}, {(Gender)(gender + 1)}, {bodyType}, {height}, returned {scale}."); return scale; } diff --git a/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs b/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs index 663209ae..d81043c8 100644 --- a/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs +++ b/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs @@ -9,7 +9,7 @@ public sealed unsafe class CopyCharacter : EventWrapperPtr + /// CutsceneService = 0, } @@ -38,8 +38,7 @@ public sealed unsafe class CopyCharacter : EventWrapperPtrOwnerObject; Penumbra.Log.Verbose($"[{Name}] Triggered with target: 0x{(nint)target:X}, source : 0x{(nint)source:X} unk: {unk}."); Invoke(character, source); return _task.Result.Original(target, source, unk); diff --git a/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs b/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs index 56b3d853..f00a9984 100644 --- a/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs +++ b/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs @@ -10,7 +10,7 @@ public sealed unsafe class CreateCharacterBase : EventWrapperPtr + /// MetaState = 0, } @@ -64,10 +64,10 @@ public sealed unsafe class CreateCharacterBase : EventWrapperPtr + /// DrawObjectState = 0, - /// + /// MetaState = 0, } } diff --git a/Penumbra/Interop/Structs/CharacterUtilityData.cs b/Penumbra/Interop/Structs/CharacterUtilityData.cs index 22150cc1..d33da477 100644 --- a/Penumbra/Interop/Structs/CharacterUtilityData.cs +++ b/Penumbra/Interop/Structs/CharacterUtilityData.cs @@ -6,16 +6,16 @@ namespace Penumbra.Interop.Structs; public unsafe struct CharacterUtilityData { public const int IndexHumanPbd = 63; - public const int IndexTransparentTex = 72; - public const int IndexDecalTex = 73; - public const int IndexSkinShpk = 76; + public const int IndexTransparentTex = 79; + public const int IndexDecalTex = 80; + public const int IndexSkinShpk = 83; public static readonly MetaIndex[] EqdpIndices = Enum.GetNames() .Zip(Enum.GetValues()) .Where(n => n.First.StartsWith("Eqdp")) .Select(n => n.Second).ToArray(); - public const int TotalNumResources = 87; + public const int TotalNumResources = 89; /// Obtain the index for the eqdp file corresponding to the given race code and accessory. public static MetaIndex EqdpIdx(GenderRace raceCode, bool accessory) @@ -36,7 +36,7 @@ public unsafe struct CharacterUtilityData 1301 => accessory ? MetaIndex.Eqdp1301Acc : MetaIndex.Eqdp1301, 1401 => accessory ? MetaIndex.Eqdp1401Acc : MetaIndex.Eqdp1401, 1501 => accessory ? MetaIndex.Eqdp1501Acc : MetaIndex.Eqdp1501, - //1601 => accessory ? MetaIndex.Eqdp1601Acc : MetaIndex.Eqdp1601, Female Hrothgar + 1601 => accessory ? MetaIndex.Eqdp1601Acc : MetaIndex.Eqdp1601, 1701 => accessory ? MetaIndex.Eqdp1701Acc : MetaIndex.Eqdp1701, 1801 => accessory ? MetaIndex.Eqdp1801Acc : MetaIndex.Eqdp1801, 0104 => accessory ? MetaIndex.Eqdp0104Acc : MetaIndex.Eqdp0104, diff --git a/Penumbra/Interop/Structs/MetaIndex.cs b/Penumbra/Interop/Structs/MetaIndex.cs index 65302264..2ec5fce4 100644 --- a/Penumbra/Interop/Structs/MetaIndex.cs +++ b/Penumbra/Interop/Structs/MetaIndex.cs @@ -4,6 +4,7 @@ namespace Penumbra.Interop.Structs; public enum MetaIndex : int { Eqp = 0, + Evp = 1, Gmp = 2, Eqdp0101 = 3, @@ -21,9 +22,8 @@ public enum MetaIndex : int Eqdp1301, Eqdp1401, Eqdp1501, - - //Eqdp1601, // TODO: female Hrothgar - Eqdp1701 = Eqdp1501 + 2, + Eqdp1601, + Eqdp1701, Eqdp1801, Eqdp0104, Eqdp0204, @@ -51,9 +51,8 @@ public enum MetaIndex : int Eqdp1301Acc, Eqdp1401Acc, Eqdp1501Acc, - - //Eqdp1601Acc, // TODO: female Hrothgar - Eqdp1701Acc = Eqdp1501Acc + 2, + Eqdp1601Acc, + Eqdp1701Acc, Eqdp1801Acc, Eqdp0104Acc, Eqdp0204Acc, @@ -66,7 +65,7 @@ public enum MetaIndex : int Eqdp9104Acc, Eqdp9204Acc, - HumanCmp = 64, + HumanCmp = 71, FaceEst, HairEst, HeadEst, From 41d271213efb97c34d173f3759d110780bd81d5b Mon Sep 17 00:00:00 2001 From: Exter-N Date: Fri, 5 Jul 2024 23:59:22 +0200 Subject: [PATCH 273/433] Update ShaderReplacementFixer for 7.0 --- .../PostProcessing/ShaderReplacementFixer.cs | 250 ++++++++++++++---- Penumbra/Interop/Services/ModelRenderer.cs | 82 +++++- Penumbra/UI/Tabs/Debug/DebugTab.cs | 97 +++++-- 3 files changed, 359 insertions(+), 70 deletions(-) diff --git a/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs b/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs index b27ca4c5..b87d33ef 100644 --- a/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs +++ b/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs @@ -19,9 +19,24 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic public static ReadOnlySpan SkinShpkName => "skin.shpk"u8; + public static ReadOnlySpan IrisShpkName + => "iris.shpk"u8; + public static ReadOnlySpan CharacterGlassShpkName => "characterglass.shpk"u8; + public static ReadOnlySpan CharacterTransparencyShpkName + => "charactertransparency.shpk"u8; + + public static ReadOnlySpan CharacterTattooShpkName + => "charactertattoo.shpk"u8; + + public static ReadOnlySpan CharacterOcclusionShpkName + => "characterocclusion.shpk"u8; + + public static ReadOnlySpan HairMaskShpkName + => "hairmask.shpk"u8; + private delegate nint CharacterBaseOnRenderMaterialDelegate(CharacterBase* drawObject, CSModelRenderer.OnRenderMaterialParams* param); private delegate nint ModelRendererOnRenderMaterialDelegate(CSModelRenderer* modelRenderer, ushort* outFlags, @@ -36,26 +51,36 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic private readonly CharacterUtility _utility; private readonly ModelRenderer _modelRenderer; - // MaterialResourceHandle set - private readonly ConcurrentSet _moddedSkinShpkMaterials = new(); - private readonly ConcurrentSet _moddedCharacterGlassShpkMaterials = new(); - - private readonly object _skinLock = new(); - private readonly object _characterGlassLock = new(); - - // ConcurrentDictionary.Count uses a lock in its current implementation. - private int _moddedSkinShpkCount; - private int _moddedCharacterGlassShpkCount; - private ulong _skinSlowPathCallDelta; - private ulong _characterGlassSlowPathCallDelta; + private readonly ModdedShaderPackageState _skinState; + private readonly ModdedShaderPackageState _irisState; + private readonly ModdedShaderPackageState _characterGlassState; + private readonly ModdedShaderPackageState _characterTransparencyState; + private readonly ModdedShaderPackageState _characterTattooState; + private readonly ModdedShaderPackageState _characterOcclusionState; + private readonly ModdedShaderPackageState _hairMaskState; public bool Enabled { get; internal set; } = true; - public int ModdedSkinShpkCount - => _moddedSkinShpkCount; + public uint ModdedSkinShpkCount + => _skinState.MaterialCount; - public int ModdedCharacterGlassShpkCount - => _moddedCharacterGlassShpkCount; + public uint ModdedIrisShpkCount + => _irisState.MaterialCount; + + public uint ModdedCharacterGlassShpkCount + => _characterGlassState.MaterialCount; + + public uint ModdedCharacterTransparencyShpkCount + => _characterTransparencyState.MaterialCount; + + public uint ModdedCharacterTattooShpkCount + => _characterTattooState.MaterialCount; + + public uint ModdedCharacterOcclusionShpkCount + => _characterOcclusionState.MaterialCount; + + public uint ModdedHairMaskShpkCount + => _hairMaskState.MaterialCount; public ShaderReplacementFixer(ResourceHandleDestructor resourceHandleDestructor, CharacterUtility utility, ModelRenderer modelRenderer, CommunicatorService communicator, HookManager hooks, CharacterBaseVTables vTables) @@ -64,7 +89,18 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic _utility = utility; _modelRenderer = modelRenderer; _communicator = communicator; - _humanOnRenderMaterialHook = hooks.CreateHook("Human.OnRenderMaterial", vTables.HumanVTable[62], + + _skinState = new( + () => (ShaderPackageResourceHandle**)&_utility.Address->SkinShpkResource, + () => (ShaderPackageResourceHandle*)_utility.DefaultSkinShpkResource); + _irisState = new(() => _modelRenderer.IrisShaderPackage, () => _modelRenderer.DefaultIrisShaderPackage); + _characterGlassState = new(() => _modelRenderer.CharacterGlassShaderPackage, () => _modelRenderer.DefaultCharacterGlassShaderPackage); + _characterTransparencyState = new(() => _modelRenderer.CharacterTransparencyShaderPackage, () => _modelRenderer.DefaultCharacterTransparencyShaderPackage); + _characterTattooState = new(() => _modelRenderer.CharacterTattooShaderPackage, () => _modelRenderer.DefaultCharacterTattooShaderPackage); + _characterOcclusionState = new(() => _modelRenderer.CharacterOcclusionShaderPackage, () => _modelRenderer.DefaultCharacterOcclusionShaderPackage); + _hairMaskState = new(() => _modelRenderer.HairMaskShaderPackage, () => _modelRenderer.DefaultHairMaskShaderPackage); + + _humanOnRenderMaterialHook = hooks.CreateHook("Human.OnRenderMaterial", vTables.HumanVTable[64], OnRenderHumanMaterial, HookSettings.PostProcessingHooks).Result; _modelRendererOnRenderMaterialHook = hooks.CreateHook("ModelRenderer.OnRenderMaterial", Sigs.ModelRendererOnRenderMaterial, ModelRendererOnRenderMaterialDetour, HookSettings.PostProcessingHooks).Result; @@ -78,14 +114,23 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic _humanOnRenderMaterialHook.Dispose(); _communicator.MtrlShpkLoaded.Unsubscribe(OnMtrlShpkLoaded); _resourceHandleDestructor.Unsubscribe(OnResourceHandleDestructor); - _moddedCharacterGlassShpkMaterials.Clear(); - _moddedSkinShpkMaterials.Clear(); - _moddedCharacterGlassShpkCount = 0; - _moddedSkinShpkCount = 0; + _hairMaskState.ClearMaterials(); + _characterOcclusionState.ClearMaterials(); + _characterTattooState.ClearMaterials(); + _characterTransparencyState.ClearMaterials(); + _characterGlassState.ClearMaterials(); + _irisState.ClearMaterials(); + _skinState.ClearMaterials(); } - public (ulong Skin, ulong CharacterGlass) GetAndResetSlowPathCallDeltas() - => (Interlocked.Exchange(ref _skinSlowPathCallDelta, 0), Interlocked.Exchange(ref _characterGlassSlowPathCallDelta, 0)); + public (ulong Skin, ulong Iris, ulong CharacterGlass, ulong CharacterTransparency, ulong CharacterTattoo, ulong CharacterOcclusion, ulong HairMask) GetAndResetSlowPathCallDeltas() + => (_skinState.GetAndResetSlowPathCallDelta(), + _irisState.GetAndResetSlowPathCallDelta(), + _characterGlassState.GetAndResetSlowPathCallDelta(), + _characterTransparencyState.GetAndResetSlowPathCallDelta(), + _characterTattooState.GetAndResetSlowPathCallDelta(), + _characterOcclusionState.GetAndResetSlowPathCallDelta(), + _hairMaskState.GetAndResetSlowPathCallDelta()); private static bool IsMaterialWithShpk(MaterialResourceHandle* mtrlResource, ReadOnlySpan shpkName) { @@ -102,54 +147,99 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic if (shpk == null) return; - var shpkName = mtrl->ShpkNameSpan; + var shpkName = mtrl->ShpkNameSpan; + var shpkState = GetStateForHuman(shpkName) ?? GetStateForModelRenderer(shpkName); - if (SkinShpkName.SequenceEqual(shpkName) && (nint)shpk != _utility.DefaultSkinShpkResource) - if (_moddedSkinShpkMaterials.TryAdd(mtrlResourceHandle)) - Interlocked.Increment(ref _moddedSkinShpkCount); - - if (CharacterGlassShpkName.SequenceEqual(shpkName) && shpk != _modelRenderer.DefaultCharacterGlassShaderPackage) - if (_moddedCharacterGlassShpkMaterials.TryAdd(mtrlResourceHandle)) - Interlocked.Increment(ref _moddedCharacterGlassShpkCount); + if (shpkState != null && shpk != shpkState.DefaultShaderPackage) + shpkState.TryAddMaterial(mtrlResourceHandle); } private void OnResourceHandleDestructor(Structs.ResourceHandle* handle) { - if (_moddedSkinShpkMaterials.TryRemove((nint)handle)) - Interlocked.Decrement(ref _moddedSkinShpkCount); - - if (_moddedCharacterGlassShpkMaterials.TryRemove((nint)handle)) - Interlocked.Decrement(ref _moddedCharacterGlassShpkCount); + _skinState.TryRemoveMaterial(handle); + _irisState.TryRemoveMaterial(handle); + _characterGlassState.TryRemoveMaterial(handle); + _characterTransparencyState.TryRemoveMaterial(handle); + _characterTattooState.TryRemoveMaterial(handle); + _characterOcclusionState.TryRemoveMaterial(handle); + _hairMaskState.TryRemoveMaterial(handle); } + private ModdedShaderPackageState? GetStateForHuman(MaterialResourceHandle* mtrlResource) + => mtrlResource == null ? null : GetStateForHuman(mtrlResource->ShpkNameSpan); + + private ModdedShaderPackageState? GetStateForHuman(ReadOnlySpan shpkName) + { + if (SkinShpkName.SequenceEqual(shpkName)) + return _skinState; + + return null; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + private uint GetTotalMaterialCountForHuman() + => _skinState.MaterialCount; + + private ModdedShaderPackageState? GetStateForModelRenderer(MaterialResourceHandle* mtrlResource) + => mtrlResource == null ? null : GetStateForModelRenderer(mtrlResource->ShpkNameSpan); + + private ModdedShaderPackageState? GetStateForModelRenderer(ReadOnlySpan shpkName) + { + if (IrisShpkName.SequenceEqual(shpkName)) + return _irisState; + + if (CharacterGlassShpkName.SequenceEqual(shpkName)) + return _characterGlassState; + + if (CharacterTransparencyShpkName.SequenceEqual(shpkName)) + return _characterTransparencyState; + + if (CharacterTattooShpkName.SequenceEqual(shpkName)) + return _characterTattooState; + + if (CharacterOcclusionShpkName.SequenceEqual(shpkName)) + return _characterOcclusionState; + + if (HairMaskShpkName.SequenceEqual(shpkName)) + return _hairMaskState; + + return null; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + private uint GetTotalMaterialCountForModelRenderer() + => _irisState.MaterialCount + _characterGlassState.MaterialCount + _characterTransparencyState.MaterialCount + _characterTattooState.MaterialCount + _characterOcclusionState.MaterialCount + _hairMaskState.MaterialCount; + private nint OnRenderHumanMaterial(CharacterBase* human, CSModelRenderer.OnRenderMaterialParams* param) { // If we don't have any on-screen instances of modded skin.shpk, we don't need the slow path at all. - if (!Enabled || _moddedSkinShpkCount == 0) + if (!Enabled || GetTotalMaterialCountForHuman() == 0) return _humanOnRenderMaterialHook.Original(human, param); var material = param->Model->Materials[param->MaterialIndex]; var mtrlResource = material->MaterialResourceHandle; - if (!IsMaterialWithShpk(mtrlResource, SkinShpkName)) + var shpkState = GetStateForHuman(mtrlResource); + if (shpkState == null) return _humanOnRenderMaterialHook.Original(human, param); - Interlocked.Increment(ref _skinSlowPathCallDelta); + shpkState.IncrementSlowPathCallDelta(); // Performance considerations: // - This function is called from several threads simultaneously, hence the need for synchronization in the swapping path ; // - Function is called each frame for each material on screen, after culling, i.e. up to thousands of times a frame in crowded areas ; // - Swapping path is taken up to hundreds of times a frame. // At the time of writing, the lock doesn't seem to have a noticeable impact in either frame rate or CPU usage, but the swapping path shall still be avoided as much as possible. - lock (_skinLock) + lock (shpkState) { + var shpkReference = shpkState.ShaderPackageReference; try { - _utility.Address->SkinShpkResource = (Structs.ResourceHandle*)mtrlResource->ShaderPackageResourceHandle; + *shpkReference = mtrlResource->ShaderPackageResourceHandle; return _humanOnRenderMaterialHook.Original(human, param); } finally { - _utility.Address->SkinShpkResource = (Structs.ResourceHandle*)_utility.DefaultSkinShpkResource; + *shpkReference = shpkState.DefaultShaderPackage; } } } @@ -158,27 +248,91 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic CSModelRenderer.OnRenderModelParams* param, Material* material, uint materialIndex) { // If we don't have any on-screen instances of modded characterglass.shpk, we don't need the slow path at all. - if (!Enabled || _moddedCharacterGlassShpkCount == 0) + if (!Enabled || GetTotalMaterialCountForModelRenderer() == 0) return _modelRendererOnRenderMaterialHook.Original(modelRenderer, outFlags, param, material, materialIndex); var mtrlResource = material->MaterialResourceHandle; - if (!IsMaterialWithShpk(mtrlResource, CharacterGlassShpkName)) + var shpkState = GetStateForModelRenderer(mtrlResource); + if (shpkState == null) return _modelRendererOnRenderMaterialHook.Original(modelRenderer, outFlags, param, material, materialIndex); - Interlocked.Increment(ref _characterGlassSlowPathCallDelta); + shpkState.IncrementSlowPathCallDelta(); // Same performance considerations as above. - lock (_characterGlassLock) + lock (shpkState) { + var shpkReference = shpkState.ShaderPackageReference; try { - *_modelRenderer.CharacterGlassShaderPackage = mtrlResource->ShaderPackageResourceHandle; + *shpkReference = mtrlResource->ShaderPackageResourceHandle; return _modelRendererOnRenderMaterialHook.Original(modelRenderer, outFlags, param, material, materialIndex); } finally { - *_modelRenderer.CharacterGlassShaderPackage = _modelRenderer.DefaultCharacterGlassShaderPackage; + *shpkReference = shpkState.DefaultShaderPackage; } } } + + private sealed class ModdedShaderPackageState(ShaderPackageReferenceGetter referenceGetter, DefaultShaderPackageGetter defaultGetter) + { + // MaterialResourceHandle set + private readonly ConcurrentSet _materials = new(); + + // ConcurrentDictionary.Count uses a lock in its current implementation. + private uint _materialCount = 0; + + private ulong _slowPathCallDelta = 0; + + public uint MaterialCount + { + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + get => _materialCount; + } + + public ShaderPackageResourceHandle** ShaderPackageReference + { + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + get => referenceGetter(); + } + + public ShaderPackageResourceHandle* DefaultShaderPackage + { + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + get => defaultGetter(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public void TryAddMaterial(nint mtrlResourceHandle) + { + if (_materials.TryAdd(mtrlResourceHandle)) + Interlocked.Increment(ref _materialCount); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public void TryRemoveMaterial(Structs.ResourceHandle* handle) + { + if (_materials.TryRemove((nint)handle)) + Interlocked.Decrement(ref _materialCount); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public void ClearMaterials() + { + _materials.Clear(); + _materialCount = 0; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public void IncrementSlowPathCallDelta() + => Interlocked.Increment(ref _slowPathCallDelta); + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public ulong GetAndResetSlowPathCallDelta() + => Interlocked.Exchange(ref _slowPathCallDelta, 0); + } + + private delegate ShaderPackageResourceHandle* DefaultShaderPackageGetter(); + + private delegate ShaderPackageResourceHandle** ShaderPackageReferenceGetter(); } diff --git a/Penumbra/Interop/Services/ModelRenderer.cs b/Penumbra/Interop/Services/ModelRenderer.cs index b268b395..10f3977f 100644 --- a/Penumbra/Interop/Services/ModelRenderer.cs +++ b/Penumbra/Interop/Services/ModelRenderer.cs @@ -9,6 +9,13 @@ public unsafe class ModelRenderer : IDisposable, IRequiredService { public bool Ready { get; private set; } + public ShaderPackageResourceHandle** IrisShaderPackage + => Manager.Instance() switch + { + null => null, + var renderManager => &renderManager->ModelRenderer.IrisShaderPackage, + }; + public ShaderPackageResourceHandle** CharacterGlassShaderPackage => Manager.Instance() switch { @@ -16,8 +23,46 @@ public unsafe class ModelRenderer : IDisposable, IRequiredService var renderManager => &renderManager->ModelRenderer.CharacterGlassShaderPackage, }; + public ShaderPackageResourceHandle** CharacterTransparencyShaderPackage + => Manager.Instance() switch + { + null => null, + var renderManager => &renderManager->ModelRenderer.CharacterTransparencyShaderPackage, + }; + + public ShaderPackageResourceHandle** CharacterTattooShaderPackage + => Manager.Instance() switch + { + null => null, + var renderManager => &renderManager->ModelRenderer.CharacterTattooShaderPackage, + }; + + public ShaderPackageResourceHandle** CharacterOcclusionShaderPackage + => Manager.Instance() switch + { + null => null, + var renderManager => &renderManager->ModelRenderer.CharacterOcclusionShaderPackage, + }; + + public ShaderPackageResourceHandle** HairMaskShaderPackage + => Manager.Instance() switch + { + null => null, + var renderManager => &renderManager->ModelRenderer.HairMaskShaderPackage, + }; + + public ShaderPackageResourceHandle* DefaultIrisShaderPackage { get; private set; } + public ShaderPackageResourceHandle* DefaultCharacterGlassShaderPackage { get; private set; } + public ShaderPackageResourceHandle* DefaultCharacterTransparencyShaderPackage { get; private set; } + + public ShaderPackageResourceHandle* DefaultCharacterTattooShaderPackage { get; private set; } + + public ShaderPackageResourceHandle* DefaultCharacterOcclusionShaderPackage { get; private set; } + + public ShaderPackageResourceHandle* DefaultHairMaskShaderPackage { get; private set; } + private readonly IFramework _framework; public ModelRenderer(IFramework framework) @@ -36,12 +81,42 @@ public unsafe class ModelRenderer : IDisposable, IRequiredService var anyMissing = false; + if (DefaultIrisShaderPackage == null) + { + DefaultIrisShaderPackage = *IrisShaderPackage; + anyMissing |= DefaultIrisShaderPackage == null; + } + if (DefaultCharacterGlassShaderPackage == null) { DefaultCharacterGlassShaderPackage = *CharacterGlassShaderPackage; anyMissing |= DefaultCharacterGlassShaderPackage == null; } + if (DefaultCharacterTransparencyShaderPackage == null) + { + DefaultCharacterTransparencyShaderPackage = *CharacterTransparencyShaderPackage; + anyMissing |= DefaultCharacterTransparencyShaderPackage == null; + } + + if (DefaultCharacterTattooShaderPackage == null) + { + DefaultCharacterTattooShaderPackage = *CharacterTattooShaderPackage; + anyMissing |= DefaultCharacterTattooShaderPackage == null; + } + + if (DefaultCharacterOcclusionShaderPackage == null) + { + DefaultCharacterOcclusionShaderPackage = *CharacterOcclusionShaderPackage; + anyMissing |= DefaultCharacterOcclusionShaderPackage == null; + } + + if (DefaultHairMaskShaderPackage == null) + { + DefaultHairMaskShaderPackage = *HairMaskShaderPackage; + anyMissing |= DefaultHairMaskShaderPackage == null; + } + if (anyMissing) return; @@ -55,7 +130,12 @@ public unsafe class ModelRenderer : IDisposable, IRequiredService if (!Ready) return; - *CharacterGlassShaderPackage = DefaultCharacterGlassShaderPackage; + *HairMaskShaderPackage = DefaultHairMaskShaderPackage; + *CharacterOcclusionShaderPackage = DefaultCharacterOcclusionShaderPackage; + *CharacterTattooShaderPackage = DefaultCharacterTattooShaderPackage; + *CharacterTransparencyShaderPackage = DefaultCharacterTransparencyShaderPackage; + *CharacterGlassShaderPackage = DefaultCharacterGlassShaderPackage; + *IrisShaderPackage = DefaultIrisShaderPackage; } public void Dispose() diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index 9a03f384..0c2581bf 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -177,6 +177,8 @@ public class DebugTab : Window, ITab, IUiService ImGui.NewLine(); DrawDebugCharacterUtility(); ImGui.NewLine(); + DrawShaderReplacementFixer(); + ImGui.NewLine(); DrawData(); ImGui.NewLine(); DrawResourceProblems(); @@ -711,27 +713,6 @@ public class DebugTab : Window, ITab, IUiService if (!ImGui.CollapsingHeader("Character Utility")) return; - var enableShaderReplacementFixer = _shaderReplacementFixer.Enabled; - if (ImGui.Checkbox("Enable Shader Replacement Fixer", ref enableShaderReplacementFixer)) - _shaderReplacementFixer.Enabled = enableShaderReplacementFixer; - - if (enableShaderReplacementFixer) - { - ImGui.SameLine(); - ImGui.Dummy(ImGuiHelpers.ScaledVector2(20, 0)); - var slowPathCallDeltas = _shaderReplacementFixer.GetAndResetSlowPathCallDeltas(); - ImGui.SameLine(); - ImGui.TextUnformatted($"\u0394 Slow-Path Calls for skin.shpk: {slowPathCallDeltas.Skin}"); - ImGui.SameLine(); - ImGui.TextUnformatted($"characterglass.shpk: {slowPathCallDeltas.CharacterGlass}"); - ImGui.SameLine(); - ImGui.Dummy(ImGuiHelpers.ScaledVector2(20, 0)); - ImGui.SameLine(); - ImGui.TextUnformatted($"Materials with Modded skin.shpk: {_shaderReplacementFixer.ModdedSkinShpkCount}"); - ImGui.SameLine(); - ImGui.TextUnformatted($"characterglass.shpk: {_shaderReplacementFixer.ModdedCharacterGlassShpkCount}"); - } - using var table = Table("##CharacterUtility", 7, ImGuiTableFlags.RowBg | ImGuiTableFlags.SizingFixedFit, -Vector2.UnitX); if (!table) @@ -786,6 +767,80 @@ public class DebugTab : Window, ITab, IUiService } } + private void DrawShaderReplacementFixer() + { + if (!ImGui.CollapsingHeader("Shader Replacement Fixer")) + return; + + var enableShaderReplacementFixer = _shaderReplacementFixer.Enabled; + if (ImGui.Checkbox("Enable Shader Replacement Fixer", ref enableShaderReplacementFixer)) + _shaderReplacementFixer.Enabled = enableShaderReplacementFixer; + + if (!enableShaderReplacementFixer) + return; + + using var table = Table("##ShaderReplacementFixer", 3, ImGuiTableFlags.RowBg | ImGuiTableFlags.SizingFixedFit, + -Vector2.UnitX); + if (!table) + return; + + var slowPathCallDeltas = _shaderReplacementFixer.GetAndResetSlowPathCallDeltas(); + + ImGui.TableSetupColumn("Shader Package Name", ImGuiTableColumnFlags.WidthStretch, 0.6f); + ImGui.TableSetupColumn("Materials with Modded ShPk", ImGuiTableColumnFlags.WidthStretch, 0.2f); + ImGui.TableSetupColumn("\u0394 Slow-Path Calls", ImGuiTableColumnFlags.WidthStretch, 0.2f); + ImGui.TableHeadersRow(); + + ImGui.TableNextColumn(); + ImGui.TextUnformatted("skin.shpk"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedSkinShpkCount}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{slowPathCallDeltas.Skin}"); + + ImGui.TableNextColumn(); + ImGui.TextUnformatted("iris.shpk"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedIrisShpkCount}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{slowPathCallDeltas.Iris}"); + + ImGui.TableNextColumn(); + ImGui.TextUnformatted("characterglass.shpk"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedCharacterGlassShpkCount}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{slowPathCallDeltas.CharacterGlass}"); + + ImGui.TableNextColumn(); + ImGui.TextUnformatted("charactertransparency.shpk"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedCharacterTransparencyShpkCount}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{slowPathCallDeltas.CharacterTransparency}"); + + ImGui.TableNextColumn(); + ImGui.TextUnformatted("charactertattoo.shpk"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedCharacterTattooShpkCount}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{slowPathCallDeltas.CharacterTattoo}"); + + ImGui.TableNextColumn(); + ImGui.TextUnformatted("characterocclusion.shpk"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedCharacterOcclusionShpkCount}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{slowPathCallDeltas.CharacterOcclusion}"); + + ImGui.TableNextColumn(); + ImGui.TextUnformatted("hairmask.shpk"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedHairMaskShpkCount}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{slowPathCallDeltas.HairMask}"); + } + /// Draw information about the resident resource files. private unsafe void DrawDebugResidentResources() { From 68135f3757eb4de0cd7ce83f37c7d7a6544656fd Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 5 Jul 2024 14:39:03 +0200 Subject: [PATCH 274/433] Update Gamedata --- Penumbra.GameData | 2 +- Penumbra/Interop/Hooks/HookSettings.cs | 10 ++-- .../Interop/Hooks/Meta/GetEqpIndirect2.cs | 1 - Penumbra/Interop/Hooks/Meta/GmpHook.cs | 47 ++++--------------- .../Interop/Hooks/Meta/ModelLoadComplete.cs | 2 +- Penumbra/Interop/Hooks/Meta/RspBustHook.cs | 10 ++-- Penumbra/Interop/Hooks/Meta/RspHeightHook.cs | 9 ++-- .../Interop/Hooks/Objects/CopyCharacter.cs | 5 +- .../Hooks/Objects/CreateCharacterBase.cs | 6 +-- .../Interop/Structs/CharacterUtilityData.cs | 10 ++-- Penumbra/Interop/Structs/MetaIndex.cs | 13 +++-- 11 files changed, 40 insertions(+), 75 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 19923f8d..62f6acfb 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 19923f8d5649f11edcfae710c26d6273cf2e9d62 +Subproject commit 62f6acfb0f9e31b2907c02a270d723bfff18b390 diff --git a/Penumbra/Interop/Hooks/HookSettings.cs b/Penumbra/Interop/Hooks/HookSettings.cs index ed4eb669..9dd8d74f 100644 --- a/Penumbra/Interop/Hooks/HookSettings.cs +++ b/Penumbra/Interop/Hooks/HookSettings.cs @@ -4,11 +4,11 @@ public static class HookSettings { public const bool AllHooks = true; - public const bool ObjectHooks = false && AllHooks; + public const bool ObjectHooks = true && AllHooks; public const bool ReplacementHooks = true && AllHooks; - public const bool ResourceHooks = false && AllHooks; - public const bool MetaEntryHooks = false && AllHooks; - public const bool MetaParentHooks = false && AllHooks; + public const bool ResourceHooks = true && AllHooks; + public const bool MetaEntryHooks = true && AllHooks; + public const bool MetaParentHooks = true && AllHooks; public const bool VfxIdentificationHooks = false && AllHooks; - public const bool PostProcessingHooks = false && AllHooks; + public const bool PostProcessingHooks = true && AllHooks; } diff --git a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs index 3767c4a2..e90674a8 100644 --- a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs +++ b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs @@ -1,6 +1,5 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; -using Penumbra.Collections; using Penumbra.GameData; using Penumbra.Interop.PathResolving; diff --git a/Penumbra/Interop/Hooks/Meta/GmpHook.cs b/Penumbra/Interop/Hooks/Meta/GmpHook.cs index 329a8beb..12b221d9 100644 --- a/Penumbra/Interop/Hooks/Meta/GmpHook.cs +++ b/Penumbra/Interop/Hooks/Meta/GmpHook.cs @@ -1,3 +1,5 @@ +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using Lumina.Data.Parsing.Uld; using OtterGui.Services; using Penumbra.GameData; using Penumbra.Interop.PathResolving; @@ -8,12 +10,10 @@ namespace Penumbra.Interop.Hooks.Meta; public unsafe class GmpHook : FastHook, IDisposable { - public delegate nint Delegate(nint gmpResource, uint dividedHeadId); + public delegate ulong Delegate(CharacterUtility* characterUtility, ulong* outputEntry, ushort setId); private readonly MetaState _metaState; - private static readonly Finalizer StablePointer = new(); - public GmpHook(HookManager hooks, MetaState metaState) { _metaState = metaState; @@ -21,50 +21,21 @@ public unsafe class GmpHook : FastHook, IDisposable _metaState.Config.ModsEnabled += Toggle; } - /// - /// This function returns a pointer to the correct block in the GMP file, if it exists - cf. . - /// To work around this, we just have a single stable ulong accessible and offset the pointer to this by the required distance, - /// which is defined by the modulo of the original ID and the block size, if we return our own custom gmp entry. - /// - private nint Detour(nint gmpResource, uint dividedHeadId) + private ulong Detour(CharacterUtility* characterUtility, ulong* outputEntry, ushort setId) { - nint ret; + ulong ret; if (_metaState.GmpCollection.TryPeek(out var collection) && collection.Collection is { Valid: true, ModCollection.MetaCache: { } cache } && cache.Gmp.TryGetValue(new GmpIdentifier(collection.Id), out var entry)) - { - if (entry.Entry.Enabled) - { - *StablePointer.Pointer = entry.Entry.Value; - // This function already gets the original ID divided by the block size, so we can compute the modulo with a single multiplication and addition. - // We then go backwards from our pointer because this gets added by the calling functions. - ret = (nint)(StablePointer.Pointer - (collection.Id.Id - dividedHeadId * ExpandedEqpGmpBase.BlockSize)); - } - else - { - ret = nint.Zero; - } - } + ret = (*outputEntry) = entry.Entry.Enabled ? entry.Entry.Value : 0ul; else - { - ret = Task.Result.Original(gmpResource, dividedHeadId); - } + ret = Task.Result.Original(characterUtility, outputEntry, setId); - Penumbra.Log.Excessive($"[GetGmpFlags] Invoked on 0x{gmpResource:X} with {dividedHeadId}, returned {ret:X10}."); + Penumbra.Log.Excessive( + $"[GetGmpFlags] Invoked on 0x{(ulong)characterUtility:X} for {setId} with 0x{(ulong)outputEntry:X} (={*outputEntry:X}), returned {ret:X10}."); return ret; } - /// Allocate and clean up our single stable ulong pointer. - private class Finalizer - { - public readonly ulong* Pointer = (ulong*)Marshal.AllocHGlobal(8); - - ~Finalizer() - { - Marshal.FreeHGlobal((nint)Pointer); - } - } - public void Dispose() => _metaState.Config.ModsEnabled -= Toggle; } diff --git a/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs b/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs index 79e7f6a6..c1803745 100644 --- a/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs +++ b/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs @@ -13,7 +13,7 @@ public sealed unsafe class ModelLoadComplete : FastHook("Model Load Complete", vtables.HumanVTable[58], Detour, HookSettings.MetaParentHooks); + Task = hooks.CreateHook("Model Load Complete", vtables.HumanVTable[59], Detour, HookSettings.MetaParentHooks); } public delegate void Delegate(DrawObject* drawObject); diff --git a/Penumbra/Interop/Hooks/Meta/RspBustHook.cs b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs index eb8a8a37..e08dc393 100644 --- a/Penumbra/Interop/Hooks/Meta/RspBustHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs @@ -10,8 +10,7 @@ namespace Penumbra.Interop.Hooks.Meta; public unsafe class RspBustHook : FastHook, IDisposable { - public delegate float* Delegate(nint cmpResource, float* storage, Race race, byte gender, byte isSecondSubRace, byte bodyType, - byte bustSize); + public delegate float* Delegate(nint cmpResource, float* storage, SubRace race, byte gender, byte bodyType, byte bustSize); private readonly MetaState _metaState; private readonly MetaFileManager _metaFileManager; @@ -24,7 +23,7 @@ public unsafe class RspBustHook : FastHook, IDisposable _metaState.Config.ModsEnabled += Toggle; } - private float* Detour(nint cmpResource, float* storage, Race race, byte gender, byte isSecondSubRace, byte bodyType, byte bustSize) + private float* Detour(nint cmpResource, float* storage, SubRace clan, byte gender, byte bodyType, byte bustSize) { if (gender == 0) { @@ -38,7 +37,6 @@ public unsafe class RspBustHook : FastHook, IDisposable if (bodyType < 2 && _metaState.RspCollection.TryPeek(out var collection) && collection is { Valid: true, ModCollection.MetaCache: { } cache }) { var bustScale = bustSize / 100f; - var clan = (SubRace)(((int)race - 1) * 2 + 1 + isSecondSubRace); var ptr = CmpFile.GetDefaults(_metaFileManager, clan, RspAttribute.BustMinX); storage[0] = GetValue(0, RspAttribute.BustMinX, RspAttribute.BustMaxX); storage[1] = GetValue(1, RspAttribute.BustMinY, RspAttribute.BustMaxY); @@ -58,11 +56,11 @@ public unsafe class RspBustHook : FastHook, IDisposable } else { - ret = Task.Result.Original(cmpResource, storage, race, gender, isSecondSubRace, bodyType, bustSize); + ret = Task.Result.Original(cmpResource, storage, clan, gender, bodyType, bustSize); } Penumbra.Log.Excessive( - $"[GetRspBust] Invoked on 0x{cmpResource:X} with {race}, {(Gender)(gender + 1)}, {isSecondSubRace == 1}, {bodyType}, {bustSize}, returned {storage[0]}, {storage[1]}, {storage[2]}."); + $"[GetRspBust] Invoked on 0x{cmpResource:X} with {clan}, {(Gender)(gender + 1)}, {bodyType}, {bustSize}, returned {storage[0]}, {storage[1]}, {storage[2]}."); return ret; } diff --git a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs index f8f9e51e..20e3c939 100644 --- a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs @@ -10,7 +10,7 @@ namespace Penumbra.Interop.Hooks.Meta; public class RspHeightHook : FastHook, IDisposable { - public delegate float Delegate(nint cmpResource, Race clan, byte gender, byte isSecondSubRace, byte bodyType, byte height); + public delegate float Delegate(nint cmpResource, SubRace clan, byte gender, byte bodyType, byte height); private readonly MetaState _metaState; private readonly MetaFileManager _metaFileManager; @@ -23,7 +23,7 @@ public class RspHeightHook : FastHook, IDisposable _metaState.Config.ModsEnabled += Toggle; } - private unsafe float Detour(nint cmpResource, Race race, byte gender, byte isSecondSubRace, byte bodyType, byte height) + private unsafe float Detour(nint cmpResource, SubRace clan, byte gender, byte bodyType, byte height) { float scale; if (bodyType < 2 @@ -36,7 +36,6 @@ public class RspHeightHook : FastHook, IDisposable if (height > 100) height = 0; - var clan = (SubRace)(((int)race - 1) * 2 + 1 + isSecondSubRace); var (minIdent, maxIdent) = gender == 0 ? (new RspIdentifier(clan, RspAttribute.MaleMinSize), new RspIdentifier(clan, RspAttribute.MaleMaxSize)) : (new RspIdentifier(clan, RspAttribute.FemaleMinSize), new RspIdentifier(clan, RspAttribute.FemaleMaxSize)); @@ -68,11 +67,11 @@ public class RspHeightHook : FastHook, IDisposable } else { - scale = Task.Result.Original(cmpResource, race, gender, isSecondSubRace, bodyType, height); + scale = Task.Result.Original(cmpResource, clan, gender, bodyType, height); } Penumbra.Log.Excessive( - $"[GetRspHeight] Invoked on 0x{cmpResource:X} with {race}, {(Gender)(gender + 1)}, {isSecondSubRace == 1}, {bodyType}, {height}, returned {scale}."); + $"[GetRspHeight] Invoked on 0x{cmpResource:X} with {clan}, {(Gender)(gender + 1)}, {bodyType}, {height}, returned {scale}."); return scale; } diff --git a/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs b/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs index 663209ae..d81043c8 100644 --- a/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs +++ b/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs @@ -9,7 +9,7 @@ public sealed unsafe class CopyCharacter : EventWrapperPtr + /// CutsceneService = 0, } @@ -38,8 +38,7 @@ public sealed unsafe class CopyCharacter : EventWrapperPtrOwnerObject; Penumbra.Log.Verbose($"[{Name}] Triggered with target: 0x{(nint)target:X}, source : 0x{(nint)source:X} unk: {unk}."); Invoke(character, source); return _task.Result.Original(target, source, unk); diff --git a/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs b/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs index 56b3d853..f00a9984 100644 --- a/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs +++ b/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs @@ -10,7 +10,7 @@ public sealed unsafe class CreateCharacterBase : EventWrapperPtr + /// MetaState = 0, } @@ -64,10 +64,10 @@ public sealed unsafe class CreateCharacterBase : EventWrapperPtr + /// DrawObjectState = 0, - /// + /// MetaState = 0, } } diff --git a/Penumbra/Interop/Structs/CharacterUtilityData.cs b/Penumbra/Interop/Structs/CharacterUtilityData.cs index 22150cc1..d33da477 100644 --- a/Penumbra/Interop/Structs/CharacterUtilityData.cs +++ b/Penumbra/Interop/Structs/CharacterUtilityData.cs @@ -6,16 +6,16 @@ namespace Penumbra.Interop.Structs; public unsafe struct CharacterUtilityData { public const int IndexHumanPbd = 63; - public const int IndexTransparentTex = 72; - public const int IndexDecalTex = 73; - public const int IndexSkinShpk = 76; + public const int IndexTransparentTex = 79; + public const int IndexDecalTex = 80; + public const int IndexSkinShpk = 83; public static readonly MetaIndex[] EqdpIndices = Enum.GetNames() .Zip(Enum.GetValues()) .Where(n => n.First.StartsWith("Eqdp")) .Select(n => n.Second).ToArray(); - public const int TotalNumResources = 87; + public const int TotalNumResources = 89; /// Obtain the index for the eqdp file corresponding to the given race code and accessory. public static MetaIndex EqdpIdx(GenderRace raceCode, bool accessory) @@ -36,7 +36,7 @@ public unsafe struct CharacterUtilityData 1301 => accessory ? MetaIndex.Eqdp1301Acc : MetaIndex.Eqdp1301, 1401 => accessory ? MetaIndex.Eqdp1401Acc : MetaIndex.Eqdp1401, 1501 => accessory ? MetaIndex.Eqdp1501Acc : MetaIndex.Eqdp1501, - //1601 => accessory ? MetaIndex.Eqdp1601Acc : MetaIndex.Eqdp1601, Female Hrothgar + 1601 => accessory ? MetaIndex.Eqdp1601Acc : MetaIndex.Eqdp1601, 1701 => accessory ? MetaIndex.Eqdp1701Acc : MetaIndex.Eqdp1701, 1801 => accessory ? MetaIndex.Eqdp1801Acc : MetaIndex.Eqdp1801, 0104 => accessory ? MetaIndex.Eqdp0104Acc : MetaIndex.Eqdp0104, diff --git a/Penumbra/Interop/Structs/MetaIndex.cs b/Penumbra/Interop/Structs/MetaIndex.cs index 65302264..2ec5fce4 100644 --- a/Penumbra/Interop/Structs/MetaIndex.cs +++ b/Penumbra/Interop/Structs/MetaIndex.cs @@ -4,6 +4,7 @@ namespace Penumbra.Interop.Structs; public enum MetaIndex : int { Eqp = 0, + Evp = 1, Gmp = 2, Eqdp0101 = 3, @@ -21,9 +22,8 @@ public enum MetaIndex : int Eqdp1301, Eqdp1401, Eqdp1501, - - //Eqdp1601, // TODO: female Hrothgar - Eqdp1701 = Eqdp1501 + 2, + Eqdp1601, + Eqdp1701, Eqdp1801, Eqdp0104, Eqdp0204, @@ -51,9 +51,8 @@ public enum MetaIndex : int Eqdp1301Acc, Eqdp1401Acc, Eqdp1501Acc, - - //Eqdp1601Acc, // TODO: female Hrothgar - Eqdp1701Acc = Eqdp1501Acc + 2, + Eqdp1601Acc, + Eqdp1701Acc, Eqdp1801Acc, Eqdp0104Acc, Eqdp0204Acc, @@ -66,7 +65,7 @@ public enum MetaIndex : int Eqdp9104Acc, Eqdp9204Acc, - HumanCmp = 64, + HumanCmp = 71, FaceEst, HairEst, HeadEst, From 4f0f3721a6296e0fad1c816c06e42d0ac3cf0de0 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 7 Jul 2024 16:16:58 +0200 Subject: [PATCH 275/433] Update animation hooks. --- Penumbra/Interop/Hooks/Animation/Dismount.cs | 13 ++++---- .../Interop/Hooks/Animation/LoadAreaVfx.cs | 8 ++--- .../Hooks/Animation/LoadCharacterSound.cs | 16 ++++++---- .../Hooks/Animation/LoadTimelineResources.cs | 32 +++++++++---------- Penumbra/Interop/Hooks/HookSettings.cs | 2 +- .../Hooks/ResourceLoading/ResourceLoader.cs | 30 +++++++++-------- .../Hooks/ResourceLoading/TexMdlService.cs | 18 +++++++++-- Penumbra/Interop/ResourceTree/ResourceTree.cs | 2 +- Penumbra/Interop/Structs/ClipScheduler.cs | 4 ++- Penumbra/Interop/Structs/StructExtensions.cs | 12 +++---- Penumbra/UI/Tabs/Debug/DebugTab.cs | 7 ++-- 11 files changed, 83 insertions(+), 61 deletions(-) diff --git a/Penumbra/Interop/Hooks/Animation/Dismount.cs b/Penumbra/Interop/Hooks/Animation/Dismount.cs index 523e750c..034011e7 100644 --- a/Penumbra/Interop/Hooks/Animation/Dismount.cs +++ b/Penumbra/Interop/Hooks/Animation/Dismount.cs @@ -1,3 +1,4 @@ +using FFXIVClientStructs.FFXIV.Client.Game.Character; using FFXIVClientStructs.FFXIV.Client.Game.Object; using OtterGui.Services; using Penumbra.GameData; @@ -18,26 +19,26 @@ public sealed unsafe class Dismount : FastHook Task = hooks.CreateHook("Dismount", Sigs.Dismount, Detour, HookSettings.VfxIdentificationHooks); } - public delegate void Delegate(nint a1, nint a2); + public delegate void Delegate(MountContainer* a1, nint a2); [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private void Detour(nint a1, nint a2) + private void Detour(MountContainer* a1, nint a2) { - Penumbra.Log.Excessive($"[Dismount] Invoked on {a1:X} with {a2:X}."); - if (a1 == nint.Zero) + Penumbra.Log.Excessive($"[Dismount] Invoked on 0x{(nint)a1:X} with {a2:X}."); + if (a1 == null) { Task.Result.Original(a1, a2); return; } - var gameObject = *(GameObject**)(a1 + 8); + var gameObject = a1->OwnerObject; if (gameObject == null) { Task.Result.Original(a1, a2); return; } - var last = _state.SetAnimationData(_collectionResolver.IdentifyCollection(gameObject, true)); + var last = _state.SetAnimationData(_collectionResolver.IdentifyCollection((GameObject*) gameObject, true)); Task.Result.Original(a1, a2); _state.RestoreAnimationData(last); } diff --git a/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs b/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs index 0f51157c..cfab29d3 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs @@ -29,13 +29,13 @@ public sealed unsafe class LoadAreaVfx : FastHook private nint Detour(uint vfxId, float* pos, GameObject* caster, float unk1, float unk2, byte unk3) { var newData = caster != null - ? _collectionResolver.IdentifyCollection(caster, true) - : ResolveData.Invalid; + ? _collectionResolver.IdentifyCollection(caster, true) + : ResolveData.Invalid; var last = _state.SetAnimationData(newData); _crashHandler.LogAnimation(newData.AssociatedGameObject, newData.ModCollection, AnimationInvocationType.LoadAreaVfx); - var ret = Task.Result.Original(vfxId, pos, caster, unk1, unk2, unk3); - Penumbra.Log.Excessive( + var ret = Task.Result.Original(vfxId, pos, caster, unk1, unk2, unk3); + Penumbra.Log.Information( $"[Load Area VFX] Invoked with {vfxId}, [{pos[0]} {pos[1]} {pos[2]}], 0x{(nint)caster:X}, {unk1}, {unk2}, {unk3} -> 0x{ret:X}."); _state.RestoreAnimationData(last); return ret; diff --git a/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs b/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs index ed04880e..8d1096d2 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs @@ -16,23 +16,25 @@ public sealed unsafe class LoadCharacterSound : FastHook("Load Character Sound", (nint)VfxContainer.MemberFunctionPointers.LoadCharacterSound, Detour, HookSettings.VfxIdentificationHooks); + _crashHandler = crashHandler; + Task = hooks.CreateHook("Load Character Sound", (nint)VfxContainer.MemberFunctionPointers.LoadCharacterSound, Detour, + HookSettings.VfxIdentificationHooks); } - public delegate nint Delegate(nint container, int unk1, int unk2, nint unk3, ulong unk4, int unk5, int unk6, ulong unk7); + public delegate nint Delegate(VfxContainer* container, int unk1, int unk2, nint unk3, ulong unk4, int unk5, int unk6, ulong unk7); [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private nint Detour(nint container, int unk1, int unk2, nint unk3, ulong unk4, int unk5, int unk6, ulong unk7) + private nint Detour(VfxContainer* container, int unk1, int unk2, nint unk3, ulong unk4, int unk5, int unk6, ulong unk7) { - var character = *(GameObject**)(container + 8); + var character = (GameObject*)container->OwnerObject; var newData = _collectionResolver.IdentifyCollection(character, true); var last = _state.SetSoundData(newData); _crashHandler.LogAnimation(newData.AssociatedGameObject, newData.ModCollection, AnimationInvocationType.LoadCharacterSound); var ret = Task.Result.Original(container, unk1, unk2, unk3, unk4, unk5, unk6, unk7); - Penumbra.Log.Excessive($"[Load Character Sound] Invoked with {container:X} {unk1} {unk2} {unk3} {unk4} {unk5} {unk6} {unk7} -> {ret}."); + Penumbra.Log.Excessive( + $"[Load Character Sound] Invoked with {(nint)container:X} {unk1} {unk2} {unk3} {unk4} {unk5} {unk6} {unk7} -> {ret}."); _state.RestoreSoundData(last); return ret; } diff --git a/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs b/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs index 4e9037bd..8bb14db6 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs @@ -1,6 +1,7 @@ using Dalamud.Game.ClientState.Conditions; using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Object; +using FFXIVClientStructs.FFXIV.Client.System.Scheduler.Base; using OtterGui.Services; using Penumbra.Collections; using Penumbra.GameData; @@ -25,45 +26,44 @@ public sealed unsafe class LoadTimelineResources : FastHook("Load Timeline Resources", Sigs.LoadTimelineResources, Detour, HookSettings.VfxIdentificationHooks); + _conditions = conditions; + _objects = objects; + _crashHandler = crashHandler; + Task = hooks.CreateHook("Load Timeline Resources", Sigs.LoadTimelineResources, Detour, HookSettings.VfxIdentificationHooks); } - public delegate ulong Delegate(nint timeline); + public delegate ulong Delegate(SchedulerTimeline* timeline); [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private ulong Detour(nint timeline) + private ulong Detour(SchedulerTimeline* timeline) { - Penumbra.Log.Excessive($"[Load Timeline Resources] Invoked on {timeline:X}."); + Penumbra.Log.Excessive($"[Load Timeline Resources] Invoked on {(nint)timeline:X}."); // Do not check timeline loading in cutscenes. if (_conditions[ConditionFlag.OccupiedInCutSceneEvent] || _conditions[ConditionFlag.WatchingCutscene78]) return Task.Result.Original(timeline); var newData = GetDataFromTimeline(_objects, _collectionResolver, timeline); - var last = _state.SetAnimationData(newData); - + var last = _state.SetAnimationData(newData); + #if false // This is called far too often and spams the log too much. _crashHandler.LogAnimation(newData.AssociatedGameObject, newData.ModCollection, AnimationInvocationType.LoadTimelineResources); #endif - var ret = Task.Result.Original(timeline); + var ret = Task.Result.Original(timeline); _state.RestoreAnimationData(last); return ret; } /// Use timelines vfuncs to obtain the associated game object. - public static ResolveData GetDataFromTimeline(ObjectManager objects, CollectionResolver resolver, nint timeline) + public static ResolveData GetDataFromTimeline(ObjectManager objects, CollectionResolver resolver, SchedulerTimeline* timeline) { try { - if (timeline != nint.Zero) + if (timeline != null) { - var getGameObjectIdx = ((delegate* unmanaged**)timeline)[0][Offsets.GetGameObjectIdxVfunc]; - var idx = getGameObjectIdx(timeline); + var idx = timeline->GetOwningGameObjectIndex(); if (idx >= 0 && idx < objects.TotalCount) { var obj = objects[idx]; @@ -73,7 +73,7 @@ public sealed unsafe class LoadTimelineResources : FastHook Load a resource for a given path and a specific collection. @@ -80,10 +80,10 @@ public unsafe class ResourceLoader : IDisposable, IService public void Dispose() { - _resources.ResourceRequested -= ResourceHandler; + _resources.ResourceRequested -= ResourceHandler; _resources.ResourceHandleIncRef -= IncRefProtection; _resources.ResourceHandleDecRef -= DecRefProtection; - _fileReadService.ReadSqPack -= ReadSqPackDetour; + _fileReadService.ReadSqPack -= ReadSqPackDetour; } private void ResourceHandler(ref ResourceCategory category, ref ResourceType type, ref int hash, ref Utf8GamePath path, @@ -94,6 +94,9 @@ public unsafe class ResourceLoader : IDisposable, IService CompareHash(ComputeHash(path.Path, parameters), hash, path); + if (path.ToString() == "vfx/common/eff/abi_cnj022g.avfx") + ; + // If no replacements are being made, we still want to be able to trigger the event. var (resolvedPath, data) = _incMode.Value ? (null, ResolveData.Invalid) @@ -112,7 +115,7 @@ public unsafe class ResourceLoader : IDisposable, IService // Replace the hash and path with the correct one for the replacement. hash = ComputeHash(resolvedPath.Value.InternalName, parameters); var oldPath = path; - path = p; + path = p; returnValue = _resources.GetOriginalResource(sync, category, type, hash, path.Path, parameters); ResourceLoaded?.Invoke(returnValue, oldPath, resolvedPath.Value, data); } @@ -121,7 +124,8 @@ public unsafe class ResourceLoader : IDisposable, IService { if (fileDescriptor->ResourceHandle == null) { - Penumbra.Log.Error("[ResourceLoader] Failure to load file from SqPack: invalid File Descriptor."); + Penumbra.Log.Verbose( + $"[ResourceLoader] Failure to load file from SqPack: invalid File Descriptor: {Marshal.PtrToStringUni((nint)(&fileDescriptor->Utf16FileName))}"); return; } @@ -140,12 +144,12 @@ public unsafe class ResourceLoader : IDisposable, IService } var path = ByteString.FromSpanUnsafe(actualPath, gamePath.Path.IsNullTerminated, gamePath.Path.IsAsciiLowerCase, gamePath.Path.IsAscii); - fileDescriptor->ResourceHandle->FileNameData = path.Path; + fileDescriptor->ResourceHandle->FileNameData = path.Path; fileDescriptor->ResourceHandle->FileNameLength = path.Length; MtrlForceSync(fileDescriptor, ref isSync); returnValue = DefaultLoadResource(path, fileDescriptor, priority, isSync, data); // Return original resource handle path so that they can be loaded separately. - fileDescriptor->ResourceHandle->FileNameData = gamePath.Path.Path; + fileDescriptor->ResourceHandle->FileNameData = gamePath.Path.Path; fileDescriptor->ResourceHandle->FileNameLength = gamePath.Path.Length; } @@ -165,7 +169,7 @@ public unsafe class ResourceLoader : IDisposable, IService // Ensure that the file descriptor has its wchar_t array on aligned boundary even if it has to be odd. var fd = stackalloc char[0x11 + 0x0B + 14]; fileDescriptor->FileDescriptor = (byte*)fd + 1; - CreateFileWHook.WritePtr(fd + 0x11, gamePath.Path, gamePath.Length); + CreateFileWHook.WritePtr(fd + 0x11, gamePath.Path, gamePath.Length); CreateFileWHook.WritePtr(&fileDescriptor->Utf16FileName, gamePath.Path, gamePath.Length); // Use the SE ReadFile function. @@ -206,7 +210,7 @@ public unsafe class ResourceLoader : IDisposable, IService return; _incMode.Value = true; - returnValue = _resources.IncRef(handle); + returnValue = _resources.IncRef(handle); _incMode.Value = false; } diff --git a/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs b/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs index 793d15af..80ba5cb9 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs @@ -87,6 +87,11 @@ public unsafe class TexMdlService : IDisposable, IRequiredService private readonly ThreadLocal _texReturnData = new(() => default); + private delegate void UpdateCategoryDelegate(TextureResourceHandle* resourceHandle); + + [Signature(Sigs.TexHandleUpdateCategory)] + private readonly UpdateCategoryDelegate _updateCategory = null!; + /// /// The function that checks a files CRC64 to determine whether it is 'protected'. /// We use it to check against our stored CRC64s and if it corresponds, we return the custom flag for models. @@ -99,9 +104,14 @@ public unsafe class TexMdlService : IDisposable, IRequiredService return CustomFileFlag; if (_customTexCrc.Contains(crc64)) + { _texReturnData.Value = true; + return nint.Zero; + } - return _checkFileStateHook.Original(ptr, crc64); + var ret = _checkFileStateHook.Original(ptr, crc64); + Penumbra.Log.Excessive($"[CheckFileState] Called on 0x{ptr:X} with CRC {crc64:X16}, returned 0x{ret:X}."); + return ret; } private delegate byte LoadTexFileLocalDelegate(TextureResourceHandle* handle, int unk1, SeFileDescriptor* unk2, bool unk3); @@ -118,7 +128,7 @@ public unsafe class TexMdlService : IDisposable, IRequiredService private delegate byte TexResourceHandleOnLoadPrototype(TextureResourceHandle* handle, SeFileDescriptor* descriptor, byte unk2); - [Signature(Sigs.TexResourceHandleOnLoad, DetourName = nameof(OnLoadDetour))] + [Signature(Sigs.TexHandleOnLoad, DetourName = nameof(OnLoadDetour))] private readonly Hook _textureOnLoadHook = null!; private byte OnLoadDetour(TextureResourceHandle* handle, SeFileDescriptor* descriptor, byte unk2) @@ -129,7 +139,9 @@ public unsafe class TexMdlService : IDisposable, IRequiredService // Function failed on a replaced texture, call local. _texReturnData.Value = false; - return _loadTexFileLocal(handle, _lodService.GetLod(handle), descriptor, unk2 != 0); + ret = _loadTexFileLocal(handle, _lodService.GetLod(handle), descriptor, unk2 != 0); + _updateCategory(handle); + return ret; } private delegate byte LoadMdlFileExternPrototype(ResourceHandle* handle, nint unk1, bool unk2, nint unk3); diff --git a/Penumbra/Interop/ResourceTree/ResourceTree.cs b/Penumbra/Interop/ResourceTree/ResourceTree.cs index 810c946d..96125df2 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTree.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTree.cs @@ -124,7 +124,7 @@ public class ResourceTree // This way to tell apart MainHand and OffHand is not always accurate, but seems good enough for what we're doing with it. var slot = weaponIndex > 0 ? EquipSlot.OffHand : EquipSlot.MainHand; - var equipment = new CharacterArmor(weapon->ModelSetId, (byte)weapon->Variant, new StainIds(weapon->Stain1, weapon->Stain2)); + var equipment = new CharacterArmor(weapon->ModelSetId, (byte)weapon->Variant, new StainIds(weapon->Stain0, weapon->Stain1)); var weaponType = weapon->SecondaryId; var genericContext = globalContext.CreateContext(subObject, 0xFFFFFFFFu, slot, equipment, weaponType); diff --git a/Penumbra/Interop/Structs/ClipScheduler.cs b/Penumbra/Interop/Structs/ClipScheduler.cs index 44a905b8..8270e0f2 100644 --- a/Penumbra/Interop/Structs/ClipScheduler.cs +++ b/Penumbra/Interop/Structs/ClipScheduler.cs @@ -1,3 +1,5 @@ +using FFXIVClientStructs.FFXIV.Client.System.Scheduler.Base; + namespace Penumbra.Interop.Structs; [StructLayout(LayoutKind.Explicit)] @@ -7,5 +9,5 @@ public unsafe struct ClipScheduler public nint* VTable; [FieldOffset(0x38)] - public nint SchedulerTimeline; + public SchedulerTimeline* SchedulerTimeline; } diff --git a/Penumbra/Interop/Structs/StructExtensions.cs b/Penumbra/Interop/Structs/StructExtensions.cs index 3cd87424..fc8b1c3d 100644 --- a/Penumbra/Interop/Structs/StructExtensions.cs +++ b/Penumbra/Interop/Structs/StructExtensions.cs @@ -9,37 +9,37 @@ internal static class StructExtensions public static unsafe ByteString AsByteString(in this StdString str) => ByteString.FromSpanUnsafe(str.AsSpan(), true); - public static ByteString ResolveEidPathAsByteString(in this CharacterBase character) + public static ByteString ResolveEidPathAsByteString(ref this CharacterBase character) { Span pathBuffer = stackalloc byte[CharacterBase.PathBufferSize]; return ToOwnedByteString(character.ResolveEidPath(pathBuffer)); } - public static ByteString ResolveImcPathAsByteString(in this CharacterBase character, uint slotIndex) + public static ByteString ResolveImcPathAsByteString(ref this CharacterBase character, uint slotIndex) { Span pathBuffer = stackalloc byte[CharacterBase.PathBufferSize]; return ToOwnedByteString(character.ResolveImcPath(pathBuffer, slotIndex)); } - public static ByteString ResolveMdlPathAsByteString(in this CharacterBase character, uint slotIndex) + public static ByteString ResolveMdlPathAsByteString(ref this CharacterBase character, uint slotIndex) { Span pathBuffer = stackalloc byte[CharacterBase.PathBufferSize]; return ToOwnedByteString(character.ResolveMdlPath(pathBuffer, slotIndex)); } - public static unsafe ByteString ResolveMtrlPathAsByteString(in this CharacterBase character, uint slotIndex, byte* mtrlFileName) + public static unsafe ByteString ResolveMtrlPathAsByteString(ref this CharacterBase character, uint slotIndex, byte* mtrlFileName) { var pathBuffer = stackalloc byte[CharacterBase.PathBufferSize]; return ToOwnedByteString(character.ResolveMtrlPath(pathBuffer, CharacterBase.PathBufferSize, slotIndex, mtrlFileName)); } - public static ByteString ResolveSklbPathAsByteString(in this CharacterBase character, uint partialSkeletonIndex) + public static ByteString ResolveSklbPathAsByteString(ref this CharacterBase character, uint partialSkeletonIndex) { Span pathBuffer = stackalloc byte[CharacterBase.PathBufferSize]; return ToOwnedByteString(character.ResolveSklbPath(pathBuffer, partialSkeletonIndex)); } - public static ByteString ResolveSkpPathAsByteString(in this CharacterBase character, uint partialSkeletonIndex) + public static ByteString ResolveSkpPathAsByteString(ref this CharacterBase character, uint partialSkeletonIndex) { Span pathBuffer = stackalloc byte[CharacterBase.PathBufferSize]; return ToOwnedByteString(character.ResolveSkpPath(pathBuffer, partialSkeletonIndex)); diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index 9a03f384..180049bc 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -430,7 +430,7 @@ public class DebugTab : Window, ITab, IUiService foreach (var obj in _objects) { - ImGuiUtil.DrawTableColumn(obj.Address == nint.Zero ? $"{((GameObject*)obj.Address)->ObjectIndex}" : "NULL"); + ImGuiUtil.DrawTableColumn(obj.Address != nint.Zero ? $"{((GameObject*)obj.Address)->ObjectIndex}" : "NULL"); ImGuiUtil.DrawTableColumn($"0x{obj.Address:X}"); ImGuiUtil.DrawTableColumn(obj.Address == nint.Zero ? string.Empty @@ -482,14 +482,15 @@ public class DebugTab : Window, ITab, IUiService { var gameObject = (GameObject*)gameObjectPtr; ImGui.TableNextColumn(); - ImGui.TextUnformatted($"0x{drawObject:X}"); + + ImGuiUtil.CopyOnClickSelectable($"0x{drawObject:X}"); ImGui.TableNextColumn(); ImGui.TextUnformatted(gameObject->ObjectIndex.ToString()); ImGui.TableNextColumn(); ImGui.TextUnformatted(child ? "Child" : "Main"); ImGui.TableNextColumn(); var (address, name) = ($"0x{gameObjectPtr:X}", new ByteString(gameObject->Name).ToString()); - ImGui.TextUnformatted(address); + ImGuiUtil.CopyOnClickSelectable(address); ImGui.TableNextColumn(); ImGui.TextUnformatted(name); ImGui.TableNextColumn(); From 0d939b12f4381a8dc4e6c96d977fdbd03348424f Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 7 Jul 2024 16:17:12 +0200 Subject: [PATCH 276/433] Add model update button. --- Penumbra/UI/AdvancedWindow/FileEditor.cs | 15 +++++++++------ .../AdvancedWindow/ModEditWindow.Models.MdlTab.cs | 1 - .../UI/AdvancedWindow/ModEditWindow.Models.cs | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Penumbra/UI/AdvancedWindow/FileEditor.cs b/Penumbra/UI/AdvancedWindow/FileEditor.cs index eeb94c71..2c6ac170 100644 --- a/Penumbra/UI/AdvancedWindow/FileEditor.cs +++ b/Penumbra/UI/AdvancedWindow/FileEditor.cs @@ -207,12 +207,15 @@ public class FileEditor( var canSave = _changed && _currentFile is { Valid: true }; if (ImGuiUtil.DrawDisabledButton("Save to File", Vector2.Zero, $"Save the selected {fileType} file with all changes applied. This is not revertible.", !canSave)) - { - compactor.WriteAllBytes(_currentPath!.File.FullName, _currentFile!.Write()); - if (owner.Mod != null) - communicator.ModFileChanged.Invoke(owner.Mod, _currentPath); - _changed = false; - } + SaveFile(); + } + + public void SaveFile() + { + compactor.WriteAllBytes(_currentPath!.File.FullName, _currentFile!.Write()); + if (owner.Mod != null) + communicator.ModFileChanged.Invoke(owner.Mod, _currentPath); + _changed = false; } private void ResetButton() diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs index 72ab37b2..b05bcac2 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs @@ -1,4 +1,3 @@ -using Lumina.Data.Parsing; using OtterGui; using Penumbra.GameData; using Penumbra.GameData.Files; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index bbed64b7..de088736 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -4,6 +4,7 @@ using Lumina.Data.Parsing; using OtterGui; using OtterGui.Custom; using OtterGui.Raii; +using OtterGui.Text; using OtterGui.Widgets; using Penumbra.GameData; using Penumbra.GameData.Files; @@ -67,6 +68,7 @@ public partial class ModEditWindow { var ret = tab.Dirty; var data = UpdateFile(tab.Mdl, ret, disabled); + DrawVersionUpdate(tab, disabled); DrawImportExport(tab, disabled); ret |= DrawModelMaterialDetails(tab, disabled); @@ -80,6 +82,19 @@ public partial class ModEditWindow return !disabled && ret; } + private void DrawVersionUpdate(MdlTab tab, bool disabled) + { + if (disabled || tab.Mdl.Version is not MdlFile.V5) + return; + + if (!ImUtf8.ButtonEx("Update MDL Version from V5 to V6"u8, "Try using this if the bone weights of a pre-Dawntrail model seem wrong.\n\nThis is not revertible."u8, + new Vector2(-0.1f, 0), false, 0, Colors.PressEnterWarningBg)) + return; + + tab.Mdl.ConvertV5ToV6(); + _modelTab.SaveFile(); + } + private void DrawImportExport(MdlTab tab, bool disabled) { if (!ImGui.CollapsingHeader("Import / Export")) From 56e284a99eedb73f2053d4e961d714edcbfc1937 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 8 Jul 2024 14:55:49 +0200 Subject: [PATCH 277/433] Add some migration things. --- OtterGui | 2 +- Penumbra.GameData | 2 +- Penumbra/Configuration.cs | 2 + Penumbra/Import/TexToolsImport.cs | 33 +- Penumbra/Import/TexToolsImporter.Archives.cs | 26 +- Penumbra/Import/TexToolsImporter.ModPack.cs | 23 +- .../Interop/Hooks/Animation/LoadAreaVfx.cs | 2 +- .../Hooks/ResourceLoading/ResourceLoader.cs | 3 - .../ResolveContext.PathResolution.cs | 50 +-- Penumbra/Interop/ResourceTree/ResourceTree.cs | 6 +- Penumbra/Mods/Manager/ModImportManager.cs | 5 +- Penumbra/Services/MigrationManager.cs | 287 ++++++++++++++++++ .../AdvancedWindow/ModEditWindow.Materials.cs | 16 + Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 5 +- Penumbra/UI/Classes/CollectionSelectHeader.cs | 4 +- Penumbra/UI/Classes/MigrationSectionDrawer.cs | 121 ++++++++ Penumbra/UI/Tabs/SettingsTab.cs | 8 +- 17 files changed, 515 insertions(+), 80 deletions(-) create mode 100644 Penumbra/Services/MigrationManager.cs create mode 100644 Penumbra/UI/Classes/MigrationSectionDrawer.cs diff --git a/OtterGui b/OtterGui index c2738e1d..89b3b951 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit c2738e1d42974cddbe5a31238c6ed236a831d17d +Subproject commit 89b3b9513f9b4989045517a452ef971e24377203 diff --git a/Penumbra.GameData b/Penumbra.GameData index 62f6acfb..b5eb074d 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 62f6acfb0f9e31b2907c02a270d723bfff18b390 +Subproject commit b5eb074d80a4aaa2703a3124d2973a7fa08046ad diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index 49aecfdc..8d0f7fd8 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -99,6 +99,8 @@ public class Configuration : IPluginConfiguration, ISavable, IService public bool UseFileSystemCompression { get; set; } = true; public bool EnableHttpApi { get; set; } = true; + public bool MigrateImportedModelsToV6 { get; set; } = false; + public string DefaultModImportPath { get; set; } = string.Empty; public bool AlwaysOpenDefaultImport { get; set; } = false; public bool KeepDefaultMetaChanges { get; set; } = false; diff --git a/Penumbra/Import/TexToolsImport.cs b/Penumbra/Import/TexToolsImport.cs index bb006d8d..ba089662 100644 --- a/Penumbra/Import/TexToolsImport.cs +++ b/Penumbra/Import/TexToolsImport.cs @@ -3,6 +3,7 @@ using OtterGui.Compression; using Penumbra.Import.Structs; using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; +using Penumbra.Services; using FileMode = System.IO.FileMode; using ZipArchive = SharpCompress.Archives.Zip.ZipArchive; using ZipArchiveEntry = SharpCompress.Archives.Zip.ZipArchiveEntry; @@ -27,24 +28,26 @@ public partial class TexToolsImporter : IDisposable public ImporterState State { get; private set; } public readonly List<(FileInfo File, DirectoryInfo? Mod, Exception? Error)> ExtractedMods; - private readonly Configuration _config; - private readonly ModEditor _editor; - private readonly ModManager _modManager; - private readonly FileCompactor _compactor; + private readonly Configuration _config; + private readonly ModEditor _editor; + private readonly ModManager _modManager; + private readonly FileCompactor _compactor; + private readonly MigrationManager _migrationManager; public TexToolsImporter(int count, IEnumerable modPackFiles, Action handler, - Configuration config, ModEditor editor, ModManager modManager, FileCompactor compactor) + Configuration config, ModEditor editor, ModManager modManager, FileCompactor compactor, MigrationManager migrationManager) { - _baseDirectory = modManager.BasePath; - _tmpFile = Path.Combine(_baseDirectory.FullName, TempFileName); - _modPackFiles = modPackFiles; - _config = config; - _editor = editor; - _modManager = modManager; - _compactor = compactor; - _modPackCount = count; - ExtractedMods = new List<(FileInfo, DirectoryInfo?, Exception?)>(count); - _token = _cancellation.Token; + _baseDirectory = modManager.BasePath; + _tmpFile = Path.Combine(_baseDirectory.FullName, TempFileName); + _modPackFiles = modPackFiles; + _config = config; + _editor = editor; + _modManager = modManager; + _compactor = compactor; + _migrationManager = migrationManager; + _modPackCount = count; + ExtractedMods = new List<(FileInfo, DirectoryInfo?, Exception?)>(count); + _token = _cancellation.Token; Task.Run(ImportFiles, _token) .ContinueWith(_ => CloseStreams(), TaskScheduler.Default) .ContinueWith(_ => diff --git a/Penumbra/Import/TexToolsImporter.Archives.cs b/Penumbra/Import/TexToolsImporter.Archives.cs index 57313ab1..a51dbc61 100644 --- a/Penumbra/Import/TexToolsImporter.Archives.cs +++ b/Penumbra/Import/TexToolsImporter.Archives.cs @@ -2,6 +2,7 @@ using Dalamud.Utility; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui.Filesystem; +using Penumbra.GameData.Files; using Penumbra.Import.Structs; using Penumbra.Mods; using SharpCompress.Archives; @@ -9,12 +10,19 @@ using SharpCompress.Archives.Rar; using SharpCompress.Archives.SevenZip; using SharpCompress.Common; using SharpCompress.Readers; +using static FFXIVClientStructs.FFXIV.Client.UI.Misc.ConfigModule; using ZipArchive = SharpCompress.Archives.Zip.ZipArchive; namespace Penumbra.Import; public partial class TexToolsImporter { + private static readonly ExtractionOptions _extractionOptions = new() + { + ExtractFullPath = true, + Overwrite = true, + }; + /// /// Extract regular compressed archives that are folders containing penumbra-formatted mods. /// The mod has to either contain a meta.json at top level, or one folder deep. @@ -45,11 +53,7 @@ public partial class TexToolsImporter Penumbra.Log.Information($" -> Importing {archive.Type} Archive."); _currentModDirectory = ModCreator.CreateModFolder(_baseDirectory, Path.GetRandomFileName(), _config.ReplaceNonAsciiOnImport, true); - var options = new ExtractionOptions() - { - ExtractFullPath = true, - Overwrite = true, - }; + State = ImporterState.ExtractingModFiles; _currentFileIdx = 0; @@ -86,7 +90,7 @@ public partial class TexToolsImporter } else { - reader.WriteEntryToDirectory(_currentModDirectory.FullName, options); + HandleFileMigrations(reader); } ++_currentFileIdx; @@ -114,6 +118,16 @@ public partial class TexToolsImporter } + private void HandleFileMigrations(IReader reader) + { + switch (Path.GetExtension(reader.Entry.Key)) + { + case ".mdl": + _migrationManager.MigrateMdlDuringExtraction(reader, _currentModDirectory!.FullName, _extractionOptions); + break; + } + } + // Search the archive for the meta.json file which needs to exist. private static string FindArchiveModMeta(IArchive archive, out bool leadDir) { diff --git a/Penumbra/Import/TexToolsImporter.ModPack.cs b/Penumbra/Import/TexToolsImporter.ModPack.cs index 2b45ecbe..ba294353 100644 --- a/Penumbra/Import/TexToolsImporter.ModPack.cs +++ b/Penumbra/Import/TexToolsImporter.ModPack.cs @@ -253,25 +253,12 @@ public partial class TexToolsImporter extractedFile.Directory?.Create(); - if (extractedFile.FullName.EndsWith(".mdl")) - ProcessMdl(data.Data); + data.Data = Path.GetExtension(extractedFile.FullName) switch + { + ".mdl" => _migrationManager.MigrateTtmpModel(extractedFile.FullName, data.Data), + _ => data.Data, + }; _compactor.WriteAllBytesAsync(extractedFile.FullName, data.Data, _token).Wait(_token); } - - private static void ProcessMdl(byte[] mdl) - { - const int modelHeaderLodOffset = 22; - - // Model file header LOD num - mdl[64] = 1; - - // Model header LOD num - var stackSize = BitConverter.ToUInt32(mdl, 4); - var runtimeBegin = stackSize + 0x44; - var stringsLengthOffset = runtimeBegin + 4; - var stringsLength = BitConverter.ToUInt32(mdl, (int)stringsLengthOffset); - var modelHeaderStart = stringsLengthOffset + stringsLength + 4; - mdl[modelHeaderStart + modelHeaderLodOffset] = 1; - } } diff --git a/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs b/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs index cfab29d3..48dc0078 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs @@ -35,7 +35,7 @@ public sealed unsafe class LoadAreaVfx : FastHook var last = _state.SetAnimationData(newData); _crashHandler.LogAnimation(newData.AssociatedGameObject, newData.ModCollection, AnimationInvocationType.LoadAreaVfx); var ret = Task.Result.Original(vfxId, pos, caster, unk1, unk2, unk3); - Penumbra.Log.Information( + Penumbra.Log.Excessive( $"[Load Area VFX] Invoked with {vfxId}, [{pos[0]} {pos[1]} {pos[2]}], 0x{(nint)caster:X}, {unk1}, {unk2}, {unk3} -> 0x{ret:X}."); _state.RestoreAnimationData(last); return ret; diff --git a/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs index af637768..195a8b9e 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs @@ -94,9 +94,6 @@ public unsafe class ResourceLoader : IDisposable, IService CompareHash(ComputeHash(path.Path, parameters), hash, path); - if (path.ToString() == "vfx/common/eff/abi_cnj022g.avfx") - ; - // If no replacements are being made, we still want to be able to trigger the event. var (resolvedPath, data) = _incMode.Value ? (null, ResolveData.Invalid) diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs index 4dfefd96..72cb1681 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs @@ -81,11 +81,12 @@ internal partial record ResolveContext // Resolving a material path through the game's code can dereference null pointers for materials that involve IMC metadata. return ModelType switch { - ModelType.Human when SlotIndex < 10 && mtrlFileName[8] != (byte)'b' => ResolveEquipmentMaterialPath(modelPath, imc, mtrlFileName), - ModelType.DemiHuman => ResolveEquipmentMaterialPath(modelPath, imc, mtrlFileName), - ModelType.Weapon => ResolveWeaponMaterialPath(modelPath, imc, mtrlFileName), - ModelType.Monster => ResolveMonsterMaterialPath(modelPath, imc, mtrlFileName), - _ => ResolveMaterialPathNative(mtrlFileName), + ModelType.Human when SlotIndex is < 10 or 16 && mtrlFileName[8] != (byte)'b' + => ResolveEquipmentMaterialPath(modelPath, imc, mtrlFileName), + ModelType.DemiHuman => ResolveEquipmentMaterialPath(modelPath, imc, mtrlFileName), + ModelType.Weapon => ResolveWeaponMaterialPath(modelPath, imc, mtrlFileName), + ModelType.Monster => ResolveMonsterMaterialPath(modelPath, imc, mtrlFileName), + _ => ResolveMaterialPathNative(mtrlFileName), }; } @@ -96,7 +97,7 @@ internal partial record ResolveContext var fileName = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(mtrlFileName); Span pathBuffer = stackalloc byte[260]; - pathBuffer = AssembleMaterialPath(pathBuffer, modelPath.Path.Span, variant, fileName); + pathBuffer = AssembleMaterialPath(pathBuffer, modelPath.Path.Span, variant, fileName); return Utf8GamePath.FromSpan(pathBuffer, out var path) ? path.Clone() : Utf8GamePath.Empty; } @@ -126,7 +127,7 @@ internal partial record ResolveContext WriteZeroPaddedNumber(mirroredFileName[4..8], mirroredSetId); Span pathBuffer = stackalloc byte[260]; - pathBuffer = AssembleMaterialPath(pathBuffer, modelPath.Path.Span, variant, mirroredFileName); + pathBuffer = AssembleMaterialPath(pathBuffer, modelPath.Path.Span, variant, mirroredFileName); var weaponPosition = pathBuffer.IndexOf("/weapon/w"u8); if (weaponPosition >= 0) @@ -145,7 +146,7 @@ internal partial record ResolveContext var fileName = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(mtrlFileName); Span pathBuffer = stackalloc byte[260]; - pathBuffer = AssembleMaterialPath(pathBuffer, modelPath.Path.Span, variant, fileName); + pathBuffer = AssembleMaterialPath(pathBuffer, modelPath.Path.Span, variant, fileName); return Utf8GamePath.FromSpan(pathBuffer, out var path) ? path.Clone() : Utf8GamePath.Empty; } @@ -166,7 +167,8 @@ internal partial record ResolveContext return entry.MaterialId; } - private static Span AssembleMaterialPath(Span materialPathBuffer, ReadOnlySpan modelPath, byte variant, ReadOnlySpan mtrlFileName) + private static Span AssembleMaterialPath(Span materialPathBuffer, ReadOnlySpan modelPath, byte variant, + ReadOnlySpan mtrlFileName) { var modelPosition = modelPath.IndexOf("/model/"u8); if (modelPosition < 0) @@ -187,8 +189,8 @@ internal partial record ResolveContext { for (var i = destination.Length; i-- > 0;) { - destination[i] = (byte)('0' + number % 10); - number /= 10; + destination[i] = (byte)('0' + number % 10); + number /= 10; } } @@ -197,13 +199,17 @@ internal partial record ResolveContext ByteString? path; try { + Penumbra.Log.Information($"{(nint)CharacterBase:X} {ModelType} {SlotIndex} 0x{(ulong)mtrlFileName:X}"); + Penumbra.Log.Information($"{new ByteString(mtrlFileName)}"); path = CharacterBase->ResolveMtrlPathAsByteString(SlotIndex, mtrlFileName); } catch (AccessViolationException) { - Penumbra.Log.Error($"Access violation during attempt to resolve material path\nDraw object: {(nint)CharacterBase:X} (of type {ModelType})\nSlot index: {SlotIndex}\nMaterial file name: {(nint)mtrlFileName:X} ({new string((sbyte*)mtrlFileName)})"); + Penumbra.Log.Error( + $"Access violation during attempt to resolve material path\nDraw object: {(nint)CharacterBase:X} (of type {ModelType})\nSlot index: {SlotIndex}\nMaterial file name: {(nint)mtrlFileName:X} ({new string((sbyte*)mtrlFileName)})"); return Utf8GamePath.Empty; } + return Utf8GamePath.FromByteString(path, out var gamePath) ? gamePath : Utf8GamePath.Empty; } @@ -235,30 +241,23 @@ internal partial record ResolveContext var characterRaceCode = (GenderRace)human->RaceSexId; switch (partialSkeletonIndex) { - case 0: - return (characterRaceCode, "base", 1); + case 0: return (characterRaceCode, "base", 1); case 1: var faceId = human->FaceId; var tribe = human->Customize[(int)Dalamud.Game.ClientState.Objects.Enums.CustomizeIndex.Tribe]; var modelType = human->Customize[(int)Dalamud.Game.ClientState.Objects.Enums.CustomizeIndex.ModelType]; if (faceId < 201) - { faceId -= tribe switch { 0xB when modelType == 4 => 100, 0xE | 0xF => 100, _ => 0, }; - } return ResolveHumanExtraSkeletonData(characterRaceCode, EstType.Face, faceId); - case 2: - return ResolveHumanExtraSkeletonData(characterRaceCode, EstType.Hair, human->HairId); - case 3: - return ResolveHumanEquipmentSkeletonData(EquipSlot.Head, EstType.Head); - case 4: - return ResolveHumanEquipmentSkeletonData(EquipSlot.Body, EstType.Body); - default: - return (0, string.Empty, 0); + case 2: return ResolveHumanExtraSkeletonData(characterRaceCode, EstType.Hair, human->HairId); + case 3: return ResolveHumanEquipmentSkeletonData(EquipSlot.Head, EstType.Head); + case 4: return ResolveHumanEquipmentSkeletonData(EquipSlot.Body, EstType.Body); + default: return (0, string.Empty, 0); } } @@ -269,7 +268,8 @@ internal partial record ResolveContext return ResolveHumanExtraSkeletonData(ResolveEqdpRaceCode(slot, equipment.Set), type, equipment.Set); } - private (GenderRace RaceCode, string Slot, PrimaryId Set) ResolveHumanExtraSkeletonData(GenderRace raceCode, EstType type, PrimaryId primary) + private (GenderRace RaceCode, string Slot, PrimaryId Set) ResolveHumanExtraSkeletonData(GenderRace raceCode, EstType type, + PrimaryId primary) { var metaCache = Global.Collection.MetaCache; var skeletonSet = metaCache?.GetEstEntry(type, raceCode, primary) ?? default; diff --git a/Penumbra/Interop/ResourceTree/ResourceTree.cs b/Penumbra/Interop/ResourceTree/ResourceTree.cs index 96125df2..6663fb40 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTree.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTree.cs @@ -73,11 +73,11 @@ public class ResourceTree var genericContext = globalContext.CreateContext(model); - for (var i = 0; i < model->SlotCount; ++i) + for (var i = 0u; i < model->SlotCount; ++i) { var slotContext = i < equipment.Length - ? globalContext.CreateContext(model, (uint)i, ((uint)i).ToEquipSlot(), equipment[i]) - : globalContext.CreateContext(model, (uint)i); + ? globalContext.CreateContext(model, i, i.ToEquipSlot(), equipment[(int)i]) + : globalContext.CreateContext(model, i); var imc = (ResourceHandle*)model->IMCArray[i]; var imcNode = slotContext.CreateNodeFromImc(imc); diff --git a/Penumbra/Mods/Manager/ModImportManager.cs b/Penumbra/Mods/Manager/ModImportManager.cs index 39a53bb9..d984d374 100644 --- a/Penumbra/Mods/Manager/ModImportManager.cs +++ b/Penumbra/Mods/Manager/ModImportManager.cs @@ -3,10 +3,11 @@ using OtterGui.Classes; using OtterGui.Services; using Penumbra.Import; using Penumbra.Mods.Editor; +using Penumbra.Services; namespace Penumbra.Mods.Manager; -public class ModImportManager(ModManager modManager, Configuration config, ModEditor modEditor) : IDisposable, IService +public class ModImportManager(ModManager modManager, Configuration config, ModEditor modEditor, MigrationManager migrationManager) : IDisposable, IService { private readonly ConcurrentQueue _modsToUnpack = new(); @@ -42,7 +43,7 @@ public class ModImportManager(ModManager modManager, Configuration config, ModEd if (files.Length == 0) return; - _import = new TexToolsImporter(files.Length, files, AddNewMod, config, modEditor, modManager, modEditor.Compactor); + _import = new TexToolsImporter(files.Length, files, AddNewMod, config, modEditor, modManager, modEditor.Compactor, migrationManager); } public bool Importing diff --git a/Penumbra/Services/MigrationManager.cs b/Penumbra/Services/MigrationManager.cs new file mode 100644 index 00000000..a4b80656 --- /dev/null +++ b/Penumbra/Services/MigrationManager.cs @@ -0,0 +1,287 @@ +using Dalamud.Interface.ImGuiNotification; +using OtterGui.Classes; +using OtterGui.Services; +using Penumbra.GameData.Files; +using SharpCompress.Common; +using SharpCompress.Readers; + +namespace Penumbra.Services; + +public class MigrationManager(Configuration config) : IService +{ + private Task? _currentTask; + private CancellationTokenSource? _source; + + public bool HasCleanUpTask { get; private set; } + public bool HasMigrationTask { get; private set; } + public bool HasRestoreTask { get; private set; } + + public bool IsMigrationTask { get; private set; } + public bool IsRestorationTask { get; private set; } + public bool IsCleanupTask { get; private set; } + + + public int Restored { get; private set; } + public int RestoreFails { get; private set; } + + public int CleanedUp { get; private set; } + + public int CleanupFails { get; private set; } + + public int Migrated { get; private set; } + + public int Unchanged { get; private set; } + + public int Failed { get; private set; } + + public bool IsRunning + => _currentTask is { IsCompleted: false }; + + /// Writes or migrates a .mdl file during extraction from a regular archive. + public void MigrateMdlDuringExtraction(IReader reader, string directory, ExtractionOptions options) + { + if (!config.MigrateImportedModelsToV6) + { + reader.WriteEntryToDirectory(directory, options); + return; + } + + var path = Path.Combine(directory, reader.Entry.Key); + using var s = new MemoryStream(); + using var e = reader.OpenEntryStream(); + e.CopyTo(s); + using var b = new BinaryReader(s); + var version = b.ReadUInt32(); + if (version == MdlFile.V5) + { + var data = s.ToArray(); + var mdl = new MdlFile(data); + MigrateModel(path, mdl, false); + Penumbra.Log.Debug($"Migrated model {reader.Entry.Key} from V5 to V6 during import."); + } + else + { + using var f = File.Open(path, FileMode.Create, FileAccess.Write); + s.Seek(0, SeekOrigin.Begin); + s.WriteTo(f); + } + } + + public void CleanBackups(string path) + { + if (IsRunning) + return; + + _source = new CancellationTokenSource(); + var token = _source.Token; + _currentTask = Task.Run(() => + { + HasCleanUpTask = true; + IsCleanupTask = true; + IsMigrationTask = false; + IsRestorationTask = false; + CleanedUp = 0; + CleanupFails = 0; + foreach (var file in Directory.EnumerateFiles(path, "*.mdl.bak", SearchOption.AllDirectories)) + { + if (token.IsCancellationRequested) + return; + + try + { + File.Delete(file); + ++CleanedUp; + Penumbra.Log.Debug($"Deleted model backup file {file}."); + } + catch (Exception ex) + { + Penumbra.Messager.NotificationMessage(ex, $"Failed to delete model backup file {file}", NotificationType.Warning); + ++CleanupFails; + } + } + }, token); + } + + public void RestoreBackups(string path) + { + if (IsRunning) + return; + + _source = new CancellationTokenSource(); + var token = _source.Token; + _currentTask = Task.Run(() => + { + HasRestoreTask = true; + IsCleanupTask = false; + IsMigrationTask = false; + IsRestorationTask = true; + CleanedUp = 0; + CleanupFails = 0; + foreach (var file in Directory.EnumerateFiles(path, "*.mdl.bak", SearchOption.AllDirectories)) + { + if (token.IsCancellationRequested) + return; + + var target = file[..^4]; + try + { + File.Copy(file, target, true); + ++Restored; + Penumbra.Log.Debug($"Restored model backup file {file} to {target}."); + } + catch (Exception ex) + { + Penumbra.Messager.NotificationMessage(ex, $"Failed to restore model backup file {file} to {target}", + NotificationType.Warning); + ++RestoreFails; + } + } + }, token); + } + + /// Update the data of a .mdl file during TTMP extraction. Returns either the existing array or a new one. + public byte[] MigrateTtmpModel(string path, byte[] data) + { + FixLodNum(data); + if (!config.MigrateImportedModelsToV6) + return data; + + var version = BitConverter.ToUInt32(data); + if (version != 5) + return data; + + var mdl = new MdlFile(data); + if (!mdl.ConvertV5ToV6()) + return data; + + data = mdl.Write(); + Penumbra.Log.Debug($"Migrated model {path} from V5 to V6 during import."); + return data; + } + + public void MigrateDirectory(string path, bool createBackups) + { + if (IsRunning) + return; + + _source = new CancellationTokenSource(); + var token = _source.Token; + _currentTask = Task.Run(() => + { + HasMigrationTask = true; + IsCleanupTask = false; + IsMigrationTask = true; + IsRestorationTask = false; + Unchanged = 0; + Migrated = 0; + Failed = 0; + foreach (var file in Directory.EnumerateFiles(path, "*.mdl", SearchOption.AllDirectories)) + { + if (token.IsCancellationRequested) + return; + + var timer = Stopwatch.StartNew(); + try + { + var data = File.ReadAllBytes(file); + var mdl = new MdlFile(data); + if (MigrateModel(file, mdl, createBackups)) + { + ++Migrated; + Penumbra.Log.Debug($"Migrated model file {file} from V5 to V6 in {timer.ElapsedMilliseconds} ms."); + } + else + { + ++Unchanged; + Penumbra.Log.Verbose($"Verified that model file {file} is already V6 in {timer.ElapsedMilliseconds} ms."); + } + } + catch (Exception ex) + { + Penumbra.Messager.NotificationMessage(ex, $"Failed to migrate model file {file} to V6 in {timer.ElapsedMilliseconds} ms", + NotificationType.Warning); + ++Failed; + } + } + }, token); + } + + public void Cancel() + { + _source?.Cancel(); + _source = null; + _currentTask = null; + } + + private static void FixLodNum(byte[] data) + { + const int modelHeaderLodOffset = 22; + + // Model file header LOD num + data[64] = 1; + + // Model header LOD num + var stackSize = BitConverter.ToUInt32(data, 4); + var runtimeBegin = stackSize + 0x44; + var stringsLengthOffset = runtimeBegin + 4; + var stringsLength = BitConverter.ToUInt32(data, (int)stringsLengthOffset); + var modelHeaderStart = stringsLengthOffset + stringsLength + 4; + data[modelHeaderStart + modelHeaderLodOffset] = 1; + } + + public static bool TryMigrateSingleModel(string path, bool createBackup) + { + try + { + var data = File.ReadAllBytes(path); + var mdl = new MdlFile(data); + return MigrateModel(path, mdl, createBackup); + } + catch (Exception ex) + { + Penumbra.Messager.NotificationMessage(ex, $"Failed to migrate the model {path} to V6", NotificationType.Warning); + return false; + } + } + + public static bool TryMigrateSingleMaterial(string path, bool createBackup) + { + try + { + var data = File.ReadAllBytes(path); + var mtrl = new MtrlFile(data); + return MigrateMaterial(path, mtrl, createBackup); + } + catch (Exception ex) + { + Penumbra.Messager.NotificationMessage(ex, $"Failed to migrate the material {path} to Dawntrail", NotificationType.Warning); + return false; + } + } + + private static bool MigrateModel(string path, MdlFile mdl, bool createBackup) + { + if (!mdl.ConvertV5ToV6()) + return false; + + var data = mdl.Write(); + if (createBackup) + File.Copy(path, Path.ChangeExtension(path, ".mdl.bak")); + File.WriteAllBytes(path, data); + return true; + } + + private static bool MigrateMaterial(string path, MtrlFile mtrl, bool createBackup) + { + if (!mtrl.MigrateToDawntrail()) + return false; + + var data = mtrl.Write(); + + mtrl.Write(); + if (createBackup) + File.Copy(path, Path.ChangeExtension(path, ".mtrl.bak")); + File.WriteAllBytes(path, data); + return true; + } +} diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs index 68b3717f..0223ca6b 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs @@ -3,6 +3,7 @@ using Dalamud.Interface.Utility; using ImGuiNET; using OtterGui; using OtterGui.Raii; +using OtterGui.Text; using OtterGui.Widgets; using Penumbra.GameData.Files; using Penumbra.String.Classes; @@ -16,6 +17,7 @@ public partial class ModEditWindow private bool DrawMaterialPanel(MtrlTab tab, bool disabled) { + DrawVersionUpdate(tab, disabled); DrawMaterialLivePreviewRebind(tab, disabled); ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); @@ -34,6 +36,20 @@ public partial class ModEditWindow return !disabled && ret; } + private void DrawVersionUpdate(MtrlTab tab, bool disabled) + { + if (disabled || tab.Mtrl.IsDawnTrail) + return; + + if (!ImUtf8.ButtonEx("Update MTRL Version to Dawntrail"u8, + "Try using this if the material can not be loaded or should use legacy shaders.\n\nThis is not revertible."u8, + new Vector2(-0.1f, 0), false, 0, Colors.PressEnterWarningBg)) + return; + + tab.Mtrl.MigrateToDawntrail(); + _materialTab.SaveFile(); + } + private static void DrawMaterialLivePreviewRebind(MtrlTab tab, bool disabled) { if (disabled) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index 90fdc48e..83a8958b 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -37,6 +37,8 @@ public partial class ModEditWindow : Window, IDisposable, IUiService { private const string WindowBaseLabel = "###SubModEdit"; + public readonly MigrationManager MigrationManager; + private readonly PerformanceTracker _performance; private readonly ModEditor _editor; private readonly Configuration _config; @@ -588,7 +590,7 @@ public partial class ModEditWindow : Window, IDisposable, IUiService StainService stainService, ActiveCollections activeCollections, ModMergeTab modMergeTab, CommunicatorService communicator, TextureManager textures, ModelManager models, IDragDropManager dragDropManager, ResourceTreeViewerFactory resourceTreeViewerFactory, ObjectManager objects, IFramework framework, - CharacterBaseDestructor characterBaseDestructor, MetaDrawers metaDrawers) + CharacterBaseDestructor characterBaseDestructor, MetaDrawers metaDrawers, MigrationManager migrationManager) : base(WindowBaseLabel) { _performance = performance; @@ -608,6 +610,7 @@ public partial class ModEditWindow : Window, IDisposable, IUiService _objects = objects; _framework = framework; _characterBaseDestructor = characterBaseDestructor; + MigrationManager = migrationManager; _metaDrawers = metaDrawers; _materialTab = new FileEditor(this, _communicator, gameData, config, _editor.Compactor, _fileDialog, "Materials", ".mtrl", () => PopulateIsOnPlayer(_editor.Files.Mtrl, ResourceType.Mtrl), DrawMaterialPanel, () => Mod?.ModPath.FullName ?? string.Empty, diff --git a/Penumbra/UI/Classes/CollectionSelectHeader.cs b/Penumbra/UI/Classes/CollectionSelectHeader.cs index 6c8bbf64..0f9b2518 100644 --- a/Penumbra/UI/Classes/CollectionSelectHeader.cs +++ b/Penumbra/UI/Classes/CollectionSelectHeader.cs @@ -37,9 +37,9 @@ public class CollectionSelectHeader : IUiService var buttonSize = new Vector2(comboWidth * 3f / 4f, 0f); using (var _ = ImRaii.Group()) { - DrawCollectionButton(buttonSize, GetDefaultCollectionInfo(), 1); + DrawCollectionButton(buttonSize, GetDefaultCollectionInfo(), 1); DrawCollectionButton(buttonSize, GetInterfaceCollectionInfo(), 2); - DrawCollectionButton(buttonSize, GetPlayerCollectionInfo(), 3); + DrawCollectionButton(buttonSize, GetPlayerCollectionInfo(), 3); DrawCollectionButton(buttonSize, GetInheritedCollectionInfo(), 4); _collectionCombo.Draw("##collectionSelector", comboWidth, ColorId.SelectedCollection.Value()); diff --git a/Penumbra/UI/Classes/MigrationSectionDrawer.cs b/Penumbra/UI/Classes/MigrationSectionDrawer.cs new file mode 100644 index 00000000..75d37368 --- /dev/null +++ b/Penumbra/UI/Classes/MigrationSectionDrawer.cs @@ -0,0 +1,121 @@ +using ImGuiNET; +using OtterGui.Services; +using OtterGui.Text; +using Penumbra.Services; + +namespace Penumbra.UI.Classes; + +public class MigrationSectionDrawer(MigrationManager migrationManager, Configuration config) : IUiService +{ + private bool _createBackups = true; + private Vector2 _buttonSize; + + public void Draw() + { + using var header = ImUtf8.CollapsingHeaderId("Migration"u8); + if (!header) + return; + + _buttonSize = UiHelpers.InputTextWidth; + DrawSettings(); + ImGui.Separator(); + DrawMigration(); + ImGui.Separator(); + DrawCleanup(); + ImGui.Separator(); + DrawRestore(); + } + + private void DrawSettings() + { + var value = config.MigrateImportedModelsToV6; + if (ImUtf8.Checkbox("Automatically Migrate V5 Models to V6 on Import"u8, ref value)) + { + config.MigrateImportedModelsToV6 = value; + config.Save(); + } + } + + private void DrawMigration() + { + ImUtf8.Checkbox("Create Backups During Manual Migration", ref _createBackups); + if (ImUtf8.ButtonEx("Migrate Model Files From V5 to V6"u8, "\0"u8, _buttonSize, migrationManager.IsRunning)) + migrationManager.MigrateDirectory(config.ModDirectory, _createBackups); + + ImUtf8.SameLineInner(); + DrawCancelButton(0, "Cancel the migration. This does not revert already finished migrations."u8); + DrawSpinner(migrationManager is { IsMigrationTask: true, IsRunning: true }); + + if (!migrationManager.HasMigrationTask) + { + ImUtf8.IconDummy(); + return; + } + + var total = migrationManager.Failed + migrationManager.Migrated + migrationManager.Unchanged; + if (total == 0) + ImUtf8.TextFrameAligned("No model files found."u8); + else + ImUtf8.TextFrameAligned($"{migrationManager.Migrated} files migrated, {migrationManager.Failed} files failed, {total} total files."); + } + + private void DrawCleanup() + { + if (ImUtf8.ButtonEx("Delete Existing Model Backup Files"u8, "\0"u8, _buttonSize, migrationManager.IsRunning)) + migrationManager.CleanBackups(config.ModDirectory); + + ImUtf8.SameLineInner(); + DrawCancelButton(1, "Cancel the cleanup. This is not revertible."u8); + DrawSpinner(migrationManager is { IsCleanupTask: true, IsRunning: true }); + if (!migrationManager.HasCleanUpTask) + { + ImUtf8.IconDummy(); + return; + } + + var total = migrationManager.CleanedUp + migrationManager.CleanupFails; + if (total == 0) + ImUtf8.TextFrameAligned("No model backup files found."u8); + else + ImUtf8.TextFrameAligned( + $"{migrationManager.CleanedUp} backups deleted, {migrationManager.CleanupFails} deletions failed, {total} total backups."); + } + + private void DrawSpinner(bool enabled) + { + if (!enabled) + return; + ImGui.SameLine(); + ImUtf8.Spinner("Spinner"u8, ImGui.GetTextLineHeight() / 2, 2, ImGui.GetColorU32(ImGuiCol.Text)); + } + + private void DrawRestore() + { + if (ImUtf8.ButtonEx("Restore Model Backups"u8, "\0"u8, _buttonSize, migrationManager.IsRunning)) + migrationManager.RestoreBackups(config.ModDirectory); + + ImUtf8.SameLineInner(); + DrawCancelButton(2, "Cancel the restoration. This does not revert already finished restoration."u8); + DrawSpinner(migrationManager is { IsRestorationTask: true, IsRunning: true }); + + if (!migrationManager.HasRestoreTask) + { + ImUtf8.IconDummy(); + return; + } + + var total = migrationManager.Restored + migrationManager.RestoreFails; + if (total == 0) + ImUtf8.TextFrameAligned("No model backup files found."u8); + else + ImUtf8.TextFrameAligned( + $"{migrationManager.Restored} backups restored, {migrationManager.RestoreFails} restorations failed, {total} total backups."); + } + + private void DrawCancelButton(int id, ReadOnlySpan tooltip) + { + using var _ = ImUtf8.PushId(id); + if (ImUtf8.ButtonEx("Cancel"u8, tooltip, disabled: !migrationManager.IsRunning)) + migrationManager.Cancel(); + } +} diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index 49e77a4d..8a4d6874 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -41,10 +41,11 @@ public class SettingsTab : ITab, IUiService private readonly DalamudSubstitutionProvider _dalamudSubstitutionProvider; private readonly FileCompactor _compactor; private readonly DalamudConfigService _dalamudConfig; - private readonly IDalamudPluginInterface _pluginInterface; + private readonly IDalamudPluginInterface _pluginInterface; private readonly IDataManager _gameData; private readonly PredefinedTagManager _predefinedTagManager; private readonly CrashHandlerService _crashService; + private readonly MigrationSectionDrawer _migrationDrawer; private int _minimumX = int.MaxValue; private int _minimumY = int.MaxValue; @@ -55,7 +56,8 @@ public class SettingsTab : ITab, IUiService Penumbra penumbra, FileDialogService fileDialog, ModManager modManager, ModFileSystemSelector selector, CharacterUtility characterUtility, ResidentResourceManager residentResources, ModExportManager modExportManager, HttpApi httpApi, DalamudSubstitutionProvider dalamudSubstitutionProvider, FileCompactor compactor, DalamudConfigService dalamudConfig, - IDataManager gameData, PredefinedTagManager predefinedTagConfig, CrashHandlerService crashService) + IDataManager gameData, PredefinedTagManager predefinedTagConfig, CrashHandlerService crashService, + MigrationSectionDrawer migrationDrawer) { _pluginInterface = pluginInterface; _config = config; @@ -77,6 +79,7 @@ public class SettingsTab : ITab, IUiService _compactor.Enabled = _config.UseFileSystemCompression; _predefinedTagManager = predefinedTagConfig; _crashService = crashService; + _migrationDrawer = migrationDrawer; } public void DrawHeader() @@ -102,6 +105,7 @@ public class SettingsTab : ITab, IUiService ImGui.NewLine(); DrawGeneralSettings(); + _migrationDrawer.Draw(); DrawColorSettings(); DrawPredefinedTagsSection(); DrawAdvancedSettings(); From f89eea721f524a4acc9b3e8041871bbbd1d8db66 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 8 Jul 2024 14:59:35 +0200 Subject: [PATCH 278/433] Update game data. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index b5eb074d..d7a56b70 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit b5eb074d80a4aaa2703a3124d2973a7fa08046ad +Subproject commit d7a56b708c73bc9917baeaa66842c1594ca3067b From 710f39768bec527db1de4172499dee2b80c7ac24 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 8 Jul 2024 15:00:37 +0200 Subject: [PATCH 279/433] Disable the required ShadersKnown for the time being. --- Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs index 91129129..cd7aca9d 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs @@ -691,8 +691,9 @@ public partial class ModEditWindow _edit._characterBaseDestructor.Unsubscribe(UnbindFromDrawObjectMaterialInstances); } + // TODO Readd ShadersKnown public bool Valid - => ShadersKnown && Mtrl.Valid; + => (true || ShadersKnown) && Mtrl.Valid; public byte[] Write() { From b677a14ceffe6d8a1c0e94d47e1af3bfd19e1616 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 9 Jul 2024 16:34:31 +0200 Subject: [PATCH 280/433] Update. --- Penumbra.Api | 2 +- Penumbra.GameData | 2 +- Penumbra/Configuration.cs | 3 +- Penumbra/Import/TexToolsImporter.Archives.cs | 11 +- Penumbra/Import/TexToolsImporter.ModPack.cs | 5 +- .../Animation/ApricotListenerSoundPlay.cs | 2 + Penumbra/Services/MigrationManager.cs | 340 +++++++++++------- Penumbra/UI/Classes/MigrationSectionDrawer.cs | 155 +++++--- Penumbra/UI/Tabs/Debug/DebugTab.cs | 10 +- 9 files changed, 338 insertions(+), 192 deletions(-) diff --git a/Penumbra.Api b/Penumbra.Api index 43b0b47f..f4c6144c 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit 43b0b47f2d019af0fe4681dfc578f9232e3ba90c +Subproject commit f4c6144ca2012b279e6d8aa52b2bef6cc2ba32d9 diff --git a/Penumbra.GameData b/Penumbra.GameData index d7a56b70..cf5be8af 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit d7a56b708c73bc9917baeaa66842c1594ca3067b +Subproject commit cf5be8af4c9ecbd9190bd3db746743fa5cd1560f diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index 8d0f7fd8..f16569b5 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -99,7 +99,8 @@ public class Configuration : IPluginConfiguration, ISavable, IService public bool UseFileSystemCompression { get; set; } = true; public bool EnableHttpApi { get; set; } = true; - public bool MigrateImportedModelsToV6 { get; set; } = false; + public bool MigrateImportedModelsToV6 { get; set; } = true; + public bool MigrateImportedMaterialsToLegacy { get; set; } = true; public string DefaultModImportPath { get; set; } = string.Empty; public bool AlwaysOpenDefaultImport { get; set; } = false; diff --git a/Penumbra/Import/TexToolsImporter.Archives.cs b/Penumbra/Import/TexToolsImporter.Archives.cs index a51dbc61..63c170cb 100644 --- a/Penumbra/Import/TexToolsImporter.Archives.cs +++ b/Penumbra/Import/TexToolsImporter.Archives.cs @@ -1,3 +1,4 @@ +using System.IO; using Dalamud.Utility; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -90,7 +91,7 @@ public partial class TexToolsImporter } else { - HandleFileMigrations(reader); + HandleFileMigrationsAndWrite(reader); } ++_currentFileIdx; @@ -118,13 +119,19 @@ public partial class TexToolsImporter } - private void HandleFileMigrations(IReader reader) + private void HandleFileMigrationsAndWrite(IReader reader) { switch (Path.GetExtension(reader.Entry.Key)) { case ".mdl": _migrationManager.MigrateMdlDuringExtraction(reader, _currentModDirectory!.FullName, _extractionOptions); break; + case ".mtrl": + _migrationManager.MigrateMtrlDuringExtraction(reader, _currentModDirectory!.FullName, _extractionOptions); + break; + default: + reader.WriteEntryToDirectory(_currentModDirectory!.FullName, _extractionOptions); + break; } } diff --git a/Penumbra/Import/TexToolsImporter.ModPack.cs b/Penumbra/Import/TexToolsImporter.ModPack.cs index ba294353..3ae1eda9 100644 --- a/Penumbra/Import/TexToolsImporter.ModPack.cs +++ b/Penumbra/Import/TexToolsImporter.ModPack.cs @@ -255,8 +255,9 @@ public partial class TexToolsImporter data.Data = Path.GetExtension(extractedFile.FullName) switch { - ".mdl" => _migrationManager.MigrateTtmpModel(extractedFile.FullName, data.Data), - _ => data.Data, + ".mdl" => _migrationManager.MigrateTtmpModel(extractedFile.FullName, data.Data), + ".mtrl" => _migrationManager.MigrateTtmpMaterial(extractedFile.FullName, data.Data), + _ => data.Data, }; _compactor.WriteAllBytesAsync(extractedFile.FullName, data.Data, _token).Wait(_token); diff --git a/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs b/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs index e58c7268..2e05c1b6 100644 --- a/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs +++ b/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs @@ -1,3 +1,4 @@ +using Dalamud.Hooking; using FFXIVClientStructs.FFXIV.Client.Game.Object; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; @@ -16,6 +17,7 @@ public sealed unsafe class ApricotListenerSoundPlay : FastHook Changed + Unchanged + Failed; + + public void Init() + { + Changed = 0; + Unchanged = 0; + Failed = 0; + HasData = true; + } + } + private Task? _currentTask; private CancellationTokenSource? _source; - public bool HasCleanUpTask { get; private set; } - public bool HasMigrationTask { get; private set; } - public bool HasRestoreTask { get; private set; } + public TaskType CurrentTask { get; private set; } - public bool IsMigrationTask { get; private set; } - public bool IsRestorationTask { get; private set; } - public bool IsCleanupTask { get; private set; } + public readonly MigrationData MdlMigration = new(true); + public readonly MigrationData MtrlMigration = new(true); + public readonly MigrationData MdlCleanup = new(false); + public readonly MigrationData MtrlCleanup = new(false); + public readonly MigrationData MdlRestoration = new(false); + public readonly MigrationData MtrlRestoration = new(false); - public int Restored { get; private set; } - public int RestoreFails { get; private set; } - - public int CleanedUp { get; private set; } - - public int CleanupFails { get; private set; } - - public int Migrated { get; private set; } - - public int Unchanged { get; private set; } - - public int Failed { get; private set; } - public bool IsRunning => _currentTask is { IsCompleted: false }; - /// Writes or migrates a .mdl file during extraction from a regular archive. - public void MigrateMdlDuringExtraction(IReader reader, string directory, ExtractionOptions options) - { - if (!config.MigrateImportedModelsToV6) - { - reader.WriteEntryToDirectory(directory, options); - return; - } + public void CleanMdlBackups(string path) + => CleanBackups(path, "*.mdl.bak", "model", MdlCleanup, TaskType.MdlCleanup); - var path = Path.Combine(directory, reader.Entry.Key); - using var s = new MemoryStream(); - using var e = reader.OpenEntryStream(); - e.CopyTo(s); - using var b = new BinaryReader(s); - var version = b.ReadUInt32(); - if (version == MdlFile.V5) - { - var data = s.ToArray(); - var mdl = new MdlFile(data); - MigrateModel(path, mdl, false); - Penumbra.Log.Debug($"Migrated model {reader.Entry.Key} from V5 to V6 during import."); - } - else - { - using var f = File.Open(path, FileMode.Create, FileAccess.Write); - s.Seek(0, SeekOrigin.Begin); - s.WriteTo(f); - } - } + public void CleanMtrlBackups(string path) + => CleanBackups(path, "*.mtrl.bak", "material", MtrlCleanup, TaskType.MtrlCleanup); - public void CleanBackups(string path) + private void CleanBackups(string path, string extension, string fileType, MigrationData data, TaskType type) { if (IsRunning) return; @@ -76,13 +72,9 @@ public class MigrationManager(Configuration config) : IService var token = _source.Token; _currentTask = Task.Run(() => { - HasCleanUpTask = true; - IsCleanupTask = true; - IsMigrationTask = false; - IsRestorationTask = false; - CleanedUp = 0; - CleanupFails = 0; - foreach (var file in Directory.EnumerateFiles(path, "*.mdl.bak", SearchOption.AllDirectories)) + CurrentTask = type; + data.Init(); + foreach (var file in Directory.EnumerateFiles(path, extension, SearchOption.AllDirectories)) { if (token.IsCancellationRequested) return; @@ -90,19 +82,25 @@ public class MigrationManager(Configuration config) : IService try { File.Delete(file); - ++CleanedUp; - Penumbra.Log.Debug($"Deleted model backup file {file}."); + ++data.Changed; + Penumbra.Log.Debug($"Deleted {fileType} backup file {file}."); } catch (Exception ex) { - Penumbra.Messager.NotificationMessage(ex, $"Failed to delete model backup file {file}", NotificationType.Warning); - ++CleanupFails; + Penumbra.Messager.NotificationMessage(ex, $"Failed to delete {fileType} backup file {file}", NotificationType.Warning); + ++data.Failed; } } }, token); } - public void RestoreBackups(string path) + public void RestoreMdlBackups(string path) + => RestoreBackups(path, "*.mdl.bak", "model", MdlRestoration, TaskType.MdlRestoration); + + public void RestoreMtrlBackups(string path) + => RestoreBackups(path, "*.mtrl.bak", "material", MtrlRestoration, TaskType.MtrlRestoration); + + private void RestoreBackups(string path, string extension, string fileType, MigrationData data, TaskType type) { if (IsRunning) return; @@ -111,13 +109,9 @@ public class MigrationManager(Configuration config) : IService var token = _source.Token; _currentTask = Task.Run(() => { - HasRestoreTask = true; - IsCleanupTask = false; - IsMigrationTask = false; - IsRestorationTask = true; - CleanedUp = 0; - CleanupFails = 0; - foreach (var file in Directory.EnumerateFiles(path, "*.mdl.bak", SearchOption.AllDirectories)) + CurrentTask = type; + data.Init(); + foreach (var file in Directory.EnumerateFiles(path, extension, SearchOption.AllDirectories)) { if (token.IsCancellationRequested) return; @@ -126,40 +120,38 @@ public class MigrationManager(Configuration config) : IService try { File.Copy(file, target, true); - ++Restored; - Penumbra.Log.Debug($"Restored model backup file {file} to {target}."); + ++data.Changed; + Penumbra.Log.Debug($"Restored {fileType} backup file {file} to {target}."); } catch (Exception ex) { - Penumbra.Messager.NotificationMessage(ex, $"Failed to restore model backup file {file} to {target}", + Penumbra.Messager.NotificationMessage(ex, $"Failed to restore {fileType} backup file {file} to {target}", NotificationType.Warning); - ++RestoreFails; + ++data.Failed; } } }, token); } - /// Update the data of a .mdl file during TTMP extraction. Returns either the existing array or a new one. - public byte[] MigrateTtmpModel(string path, byte[] data) - { - FixLodNum(data); - if (!config.MigrateImportedModelsToV6) - return data; + public void MigrateMdlDirectory(string path, bool createBackups) + => MigrateDirectory(path, createBackups, "*.mdl", "model", MdlMigration, TaskType.MdlMigration, "from V5 to V6", "V6", + (file, fileData, backups) => + { + var mdl = new MdlFile(fileData); + return MigrateModel(file, mdl, backups); + }); - var version = BitConverter.ToUInt32(data); - if (version != 5) - return data; + public void MigrateMtrlDirectory(string path, bool createBackups) + => MigrateDirectory(path, createBackups, "*.mtrl", "material", MtrlMigration, TaskType.MtrlMigration, "to Dawntrail", "Dawntrail", + (file, fileData, backups) => + { + var mtrl = new MtrlFile(fileData); + return MigrateMaterial(file, mtrl, backups); + } + ); - var mdl = new MdlFile(data); - if (!mdl.ConvertV5ToV6()) - return data; - - data = mdl.Write(); - Penumbra.Log.Debug($"Migrated model {path} from V5 to V6 during import."); - return data; - } - - public void MigrateDirectory(string path, bool createBackups) + private void MigrateDirectory(string path, bool createBackups, string extension, string fileType, MigrationData data, TaskType type, + string action, string state, Func func) { if (IsRunning) return; @@ -168,14 +160,9 @@ public class MigrationManager(Configuration config) : IService var token = _source.Token; _currentTask = Task.Run(() => { - HasMigrationTask = true; - IsCleanupTask = false; - IsMigrationTask = true; - IsRestorationTask = false; - Unchanged = 0; - Migrated = 0; - Failed = 0; - foreach (var file in Directory.EnumerateFiles(path, "*.mdl", SearchOption.AllDirectories)) + CurrentTask = type; + data.Init(); + foreach (var file in Directory.EnumerateFiles(path, extension, SearchOption.AllDirectories)) { if (token.IsCancellationRequested) return; @@ -183,24 +170,24 @@ public class MigrationManager(Configuration config) : IService var timer = Stopwatch.StartNew(); try { - var data = File.ReadAllBytes(file); - var mdl = new MdlFile(data); - if (MigrateModel(file, mdl, createBackups)) + var fileData = File.ReadAllBytes(file); + if (func(file, fileData, createBackups)) { - ++Migrated; - Penumbra.Log.Debug($"Migrated model file {file} from V5 to V6 in {timer.ElapsedMilliseconds} ms."); + ++data.Changed; + Penumbra.Log.Debug($"Migrated {fileType} file {file} {action} in {timer.ElapsedMilliseconds} ms."); } else { - ++Unchanged; - Penumbra.Log.Verbose($"Verified that model file {file} is already V6 in {timer.ElapsedMilliseconds} ms."); + ++data.Unchanged; + Penumbra.Log.Verbose($"Verified that {fileType} file {file} is already {state} in {timer.ElapsedMilliseconds} ms."); } } catch (Exception ex) { - Penumbra.Messager.NotificationMessage(ex, $"Failed to migrate model file {file} to V6 in {timer.ElapsedMilliseconds} ms", + ++data.Failed; + Penumbra.Messager.NotificationMessage(ex, + $"Failed to migrate {fileType} file {file} to {state} in {timer.ElapsedMilliseconds} ms", NotificationType.Warning); - ++Failed; } } }, token); @@ -213,22 +200,6 @@ public class MigrationManager(Configuration config) : IService _currentTask = null; } - private static void FixLodNum(byte[] data) - { - const int modelHeaderLodOffset = 22; - - // Model file header LOD num - data[64] = 1; - - // Model header LOD num - var stackSize = BitConverter.ToUInt32(data, 4); - var runtimeBegin = stackSize + 0x44; - var stringsLengthOffset = runtimeBegin + 4; - var stringsLength = BitConverter.ToUInt32(data, (int)stringsLengthOffset); - var modelHeaderStart = stringsLengthOffset + stringsLength + 4; - data[modelHeaderStart + modelHeaderLodOffset] = 1; - } - public static bool TryMigrateSingleModel(string path, bool createBackup) { try @@ -259,6 +230,113 @@ public class MigrationManager(Configuration config) : IService } } + /// Writes or migrates a .mdl file during extraction from a regular archive. + public void MigrateMdlDuringExtraction(IReader reader, string directory, ExtractionOptions options) + { + if (!config.MigrateImportedModelsToV6) + { + reader.WriteEntryToDirectory(directory, options); + return; + } + + var path = Path.Combine(directory, reader.Entry.Key); + using var s = new MemoryStream(); + using var e = reader.OpenEntryStream(); + e.CopyTo(s); + using var b = new BinaryReader(s); + var version = b.ReadUInt32(); + if (version == MdlFile.V5) + { + var data = s.ToArray(); + var mdl = new MdlFile(data); + MigrateModel(path, mdl, false); + Penumbra.Log.Debug($"Migrated model {reader.Entry.Key} from V5 to V6 during import."); + } + else + { + using var f = File.Open(path, FileMode.Create, FileAccess.Write); + s.Seek(0, SeekOrigin.Begin); + s.WriteTo(f); + } + } + + public void MigrateMtrlDuringExtraction(IReader reader, string directory, ExtractionOptions options) + { + if (!config.MigrateImportedMaterialsToLegacy) + { + reader.WriteEntryToDirectory(directory, options); + return; + } + + var path = Path.Combine(directory, reader.Entry.Key); + using var s = new MemoryStream(); + using var e = reader.OpenEntryStream(); + e.CopyTo(s); + var file = new MtrlFile(s.GetBuffer()); + if (!file.IsDawnTrail) + { + file.MigrateToDawntrail(); + Penumbra.Log.Debug($"Migrated material {reader.Entry.Key} to Dawntrail during import."); + } + + using var f = File.Open(path, FileMode.Create, FileAccess.Write); + s.Seek(0, SeekOrigin.Begin); + s.WriteTo(f); + } + + /// Update the data of a .mdl file during TTMP extraction. Returns either the existing array or a new one. + public byte[] MigrateTtmpModel(string path, byte[] data) + { + FixLodNum(data); + if (!config.MigrateImportedModelsToV6) + return data; + + var version = BitConverter.ToUInt32(data); + if (version != 5) + return data; + + try + { + var mdl = new MdlFile(data); + if (!mdl.ConvertV5ToV6()) + return data; + + data = mdl.Write(); + Penumbra.Log.Debug($"Migrated model {path} from V5 to V6 during import."); + return data; + } + catch (Exception ex) + { + Penumbra.Log.Warning($"Failed to migrate model {path} from V5 to V6 during import:\n{ex}"); + return data; + } + } + + /// Update the data of a .mtrl file during TTMP extraction. Returns either the existing array or a new one. + public byte[] MigrateTtmpMaterial(string path, byte[] data) + { + if (!config.MigrateImportedMaterialsToLegacy) + return data; + + try + { + var mtrl = new MtrlFile(data); + if (mtrl.IsDawnTrail) + return data; + + mtrl.MigrateToDawntrail(); + data = mtrl.Write(); + Penumbra.Log.Debug($"Migrated material {path} to Dawntrail during import."); + return data; + } + catch (Exception ex) + { + Penumbra.Log.Warning($"Failed to migrate material {path} to Dawntrail during import:\n{ex}"); + return data; + } + } + + private static bool MigrateModel(string path, MdlFile mdl, bool createBackup) { if (!mdl.ConvertV5ToV6()) @@ -284,4 +362,20 @@ public class MigrationManager(Configuration config) : IService File.WriteAllBytes(path, data); return true; } + + private static void FixLodNum(byte[] data) + { + const int modelHeaderLodOffset = 22; + + // Model file header LOD num + data[64] = 1; + + // Model header LOD num + var stackSize = BitConverter.ToUInt32(data, 4); + var runtimeBegin = stackSize + 0x44; + var stringsLengthOffset = runtimeBegin + 4; + var stringsLength = BitConverter.ToUInt32(data, (int)stringsLengthOffset); + var modelHeaderStart = stringsLengthOffset + stringsLength + 4; + data[modelHeaderStart + modelHeaderLodOffset] = 1; + } } diff --git a/Penumbra/UI/Classes/MigrationSectionDrawer.cs b/Penumbra/UI/Classes/MigrationSectionDrawer.cs index 75d37368..ec76ddae 100644 --- a/Penumbra/UI/Classes/MigrationSectionDrawer.cs +++ b/Penumbra/UI/Classes/MigrationSectionDrawer.cs @@ -19,11 +19,13 @@ public class MigrationSectionDrawer(MigrationManager migrationManager, Configura _buttonSize = UiHelpers.InputTextWidth; DrawSettings(); ImGui.Separator(); - DrawMigration(); + DrawMdlMigration(); + DrawMdlRestore(); + DrawMdlCleanup(); ImGui.Separator(); - DrawCleanup(); - ImGui.Separator(); - DrawRestore(); + DrawMtrlMigration(); + DrawMtrlRestore(); + DrawMtrlCleanup(); } private void DrawSettings() @@ -34,88 +36,125 @@ public class MigrationSectionDrawer(MigrationManager migrationManager, Configura config.MigrateImportedModelsToV6 = value; config.Save(); } - } - private void DrawMigration() - { - ImUtf8.Checkbox("Create Backups During Manual Migration", ref _createBackups); - if (ImUtf8.ButtonEx("Migrate Model Files From V5 to V6"u8, "\0"u8, _buttonSize, migrationManager.IsRunning)) - migrationManager.MigrateDirectory(config.ModDirectory, _createBackups); + ImUtf8.HoverTooltip("This increments the version marker and restructures the bone table to the new version."u8); - ImUtf8.SameLineInner(); - DrawCancelButton(0, "Cancel the migration. This does not revert already finished migrations."u8); - DrawSpinner(migrationManager is { IsMigrationTask: true, IsRunning: true }); - - if (!migrationManager.HasMigrationTask) + if (ImUtf8.Checkbox("Automatically Migrate Materials to Dawntrail on Import"u8, ref value)) { - ImUtf8.IconDummy(); - return; + config.MigrateImportedMaterialsToLegacy = value; + config.Save(); } - var total = migrationManager.Failed + migrationManager.Migrated + migrationManager.Unchanged; - if (total == 0) - ImUtf8.TextFrameAligned("No model files found."u8); - else - ImUtf8.TextFrameAligned($"{migrationManager.Migrated} files migrated, {migrationManager.Failed} files failed, {total} total files."); + ImUtf8.HoverTooltip( + "This currently only increases the color-table size and switches the shader from 'character.shpk' to 'characterlegacy.shpk', if the former is used."u8); + + ImUtf8.Checkbox("Create Backups During Manual Migration", ref _createBackups); } - private void DrawCleanup() + private static ReadOnlySpan MigrationTooltip + => "Cancel the migration. This does not revert already finished migrations."u8; + + private void DrawMdlMigration() + { + if (ImUtf8.ButtonEx("Migrate Model Files From V5 to V6"u8, "\0"u8, _buttonSize, migrationManager.IsRunning)) + migrationManager.MigrateMdlDirectory(config.ModDirectory, _createBackups); + + ImUtf8.SameLineInner(); + DrawCancelButton(MigrationManager.TaskType.MdlMigration, "Cancel the migration. This does not revert already finished migrations."u8); + DrawSpinner(migrationManager is { CurrentTask: MigrationManager.TaskType.MdlMigration, IsRunning: true }); + DrawData(migrationManager.MdlMigration, "No model files found."u8, "migrated"u8); + } + + private void DrawMtrlMigration() + { + if (ImUtf8.ButtonEx("Migrate Material Files to Dawntrail"u8, "\0"u8, _buttonSize, migrationManager.IsRunning)) + migrationManager.MigrateMtrlDirectory(config.ModDirectory, _createBackups); + + ImUtf8.SameLineInner(); + DrawCancelButton(MigrationManager.TaskType.MtrlMigration, MigrationTooltip); + DrawSpinner(migrationManager is { CurrentTask: MigrationManager.TaskType.MtrlMigration, IsRunning: true }); + DrawData(migrationManager.MtrlMigration, "No material files found."u8, "migrated"u8); + } + + + private static ReadOnlySpan CleanupTooltip + => "Cancel the cleanup. This is not revertible."u8; + + private void DrawMdlCleanup() { if (ImUtf8.ButtonEx("Delete Existing Model Backup Files"u8, "\0"u8, _buttonSize, migrationManager.IsRunning)) - migrationManager.CleanBackups(config.ModDirectory); + migrationManager.CleanMdlBackups(config.ModDirectory); ImUtf8.SameLineInner(); - DrawCancelButton(1, "Cancel the cleanup. This is not revertible."u8); - DrawSpinner(migrationManager is { IsCleanupTask: true, IsRunning: true }); - if (!migrationManager.HasCleanUpTask) - { - ImUtf8.IconDummy(); - return; - } - - var total = migrationManager.CleanedUp + migrationManager.CleanupFails; - if (total == 0) - ImUtf8.TextFrameAligned("No model backup files found."u8); - else - ImUtf8.TextFrameAligned( - $"{migrationManager.CleanedUp} backups deleted, {migrationManager.CleanupFails} deletions failed, {total} total backups."); + DrawCancelButton(MigrationManager.TaskType.MdlCleanup, CleanupTooltip); + DrawSpinner(migrationManager is { CurrentTask: MigrationManager.TaskType.MdlCleanup, IsRunning: true }); + DrawData(migrationManager.MdlCleanup, "No model backup files found."u8, "deleted"u8); } - private void DrawSpinner(bool enabled) + private void DrawMtrlCleanup() + { + if (ImUtf8.ButtonEx("Delete Existing Material Backup Files"u8, "\0"u8, _buttonSize, migrationManager.IsRunning)) + migrationManager.CleanMtrlBackups(config.ModDirectory); + + ImUtf8.SameLineInner(); + DrawCancelButton(MigrationManager.TaskType.MtrlCleanup, CleanupTooltip); + DrawSpinner(migrationManager is { CurrentTask: MigrationManager.TaskType.MtrlCleanup, IsRunning: true }); + DrawData(migrationManager.MtrlCleanup, "No material backup files found."u8, "deleted"u8); + } + + private static ReadOnlySpan RestorationTooltip + => "Cancel the restoration. This does not revert already finished restoration."u8; + + private void DrawMdlRestore() + { + if (ImUtf8.ButtonEx("Restore Model Backups"u8, "\0"u8, _buttonSize, migrationManager.IsRunning)) + migrationManager.RestoreMdlBackups(config.ModDirectory); + + ImUtf8.SameLineInner(); + DrawCancelButton(MigrationManager.TaskType.MdlRestoration, RestorationTooltip); + DrawSpinner(migrationManager is { CurrentTask: MigrationManager.TaskType.MdlRestoration, IsRunning: true }); + DrawData(migrationManager.MdlRestoration, "No model backup files found."u8, "restored"u8); + } + + private void DrawMtrlRestore() + { + if (ImUtf8.ButtonEx("Restore Material Backups"u8, "\0"u8, _buttonSize, migrationManager.IsRunning)) + migrationManager.RestoreMtrlBackups(config.ModDirectory); + + ImUtf8.SameLineInner(); + DrawCancelButton(MigrationManager.TaskType.MtrlRestoration, RestorationTooltip); + DrawSpinner(migrationManager is { CurrentTask: MigrationManager.TaskType.MtrlRestoration, IsRunning: true }); + DrawData(migrationManager.MtrlRestoration, "No material backup files found."u8, "restored"u8); + } + + private static void DrawSpinner(bool enabled) { if (!enabled) return; + ImGui.SameLine(); ImUtf8.Spinner("Spinner"u8, ImGui.GetTextLineHeight() / 2, 2, ImGui.GetColorU32(ImGuiCol.Text)); } - private void DrawRestore() + private void DrawCancelButton(MigrationManager.TaskType task, ReadOnlySpan tooltip) { - if (ImUtf8.ButtonEx("Restore Model Backups"u8, "\0"u8, _buttonSize, migrationManager.IsRunning)) - migrationManager.RestoreBackups(config.ModDirectory); + using var _ = ImUtf8.PushId((int)task); + if (ImUtf8.ButtonEx("Cancel"u8, tooltip, disabled: !migrationManager.IsRunning || task != migrationManager.CurrentTask)) + migrationManager.Cancel(); + } - ImUtf8.SameLineInner(); - DrawCancelButton(2, "Cancel the restoration. This does not revert already finished restoration."u8); - DrawSpinner(migrationManager is { IsRestorationTask: true, IsRunning: true }); - - if (!migrationManager.HasRestoreTask) + private static void DrawData(MigrationManager.MigrationData data, ReadOnlySpan empty, ReadOnlySpan action) + { + if (!data.HasData) { ImUtf8.IconDummy(); return; } - var total = migrationManager.Restored + migrationManager.RestoreFails; + var total = data.Total; if (total == 0) - ImUtf8.TextFrameAligned("No model backup files found."u8); + ImUtf8.TextFrameAligned(empty); else - ImUtf8.TextFrameAligned( - $"{migrationManager.Restored} backups restored, {migrationManager.RestoreFails} restorations failed, {total} total backups."); - } - - private void DrawCancelButton(int id, ReadOnlySpan tooltip) - { - using var _ = ImUtf8.PushId(id); - if (ImUtf8.ButtonEx("Cancel"u8, tooltip, disabled: !migrationManager.IsRunning)) - migrationManager.Cancel(); + ImUtf8.TextFrameAligned($"{data.Changed} files {action}, {data.Failed} files failed, {total} files found."); } } diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index ace3d6a3..be92b94e 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -15,6 +15,7 @@ using Microsoft.Extensions.DependencyInjection; using OtterGui; using OtterGui.Classes; using OtterGui.Services; +using OtterGui.Text; using OtterGui.Widgets; using Penumbra.Api; using Penumbra.Collections.Manager; @@ -433,10 +434,11 @@ public class DebugTab : Window, ITab, IUiService foreach (var obj in _objects) { ImGuiUtil.DrawTableColumn(obj.Address != nint.Zero ? $"{((GameObject*)obj.Address)->ObjectIndex}" : "NULL"); - ImGuiUtil.DrawTableColumn($"0x{obj.Address:X}"); - ImGuiUtil.DrawTableColumn(obj.Address == nint.Zero - ? string.Empty - : $"0x{(nint)((Character*)obj.Address)->GameObject.GetDrawObject():X}"); + ImGui.TableNextColumn(); + ImGuiUtil.CopyOnClickSelectable($"0x{obj.Address:X}"); + ImGui.TableNextColumn(); + if (obj.Address != nint.Zero) + ImGuiUtil.CopyOnClickSelectable($"0x{(nint)((Character*)obj.Address)->GameObject.GetDrawObject():X}"); var identifier = _actors.FromObject(obj, out _, false, true, false); ImGuiUtil.DrawTableColumn(_actors.ToString(identifier)); var id = obj.AsObject->ObjectKind is ObjectKind.BattleNpc From a0a3435918c8f644aa4cedefcd3d9efe8643ccf4 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 9 Jul 2024 16:50:48 +0200 Subject: [PATCH 281/433] Remove not-yet-existing CS requirement. --- Penumbra.GameData | 2 +- repo.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index cf5be8af..d8c784e4 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit cf5be8af4c9ecbd9190bd3db746743fa5cd1560f +Subproject commit d8c784e443112d17d93ba7e6ab5d54f00f7f1477 diff --git a/repo.json b/repo.json index 3142f8d4..6379595e 100644 --- a/repo.json +++ b/repo.json @@ -9,7 +9,7 @@ "TestingAssemblyVersion": "1.1.1.5", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", - "DalamudApiLevel": 9, + "DalamudApiLevel": 10, "IsHide": "False", "IsTestingExclusive": "False", "DownloadCount": 0, From 56502f19f9aa41003d532d68f225f7c956fa0f22 Mon Sep 17 00:00:00 2001 From: Actions User Date: Tue, 9 Jul 2024 14:55:15 +0000 Subject: [PATCH 282/433] [CI] Updating repo.json for testing_1.2.0.0 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 6379595e..c604a0dd 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.1.1.5", + "TestingAssemblyVersion": "1.2.0.0", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 10, @@ -18,7 +18,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.1.1.5/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.0/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From c2517499f27f1a7c83ad9510d3cbf9ad6ad255e1 Mon Sep 17 00:00:00 2001 From: Ottermandias <70807659+Ottermandias@users.noreply.github.com> Date: Tue, 9 Jul 2024 17:04:27 +0200 Subject: [PATCH 283/433] Update repo.json --- repo.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repo.json b/repo.json index c604a0dd..4f567253 100644 --- a/repo.json +++ b/repo.json @@ -11,7 +11,7 @@ "ApplicableVersion": "any", "DalamudApiLevel": 10, "IsHide": "False", - "IsTestingExclusive": "False", + "IsTestingExclusive": "True", "DownloadCount": 0, "LastUpdate": 0, "LoadPriority": 69420, From 3c417d7aeca0fd5668475e7644185c73f31e50b3 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 9 Jul 2024 17:48:34 +0200 Subject: [PATCH 284/433] Fix extraction of pmp failing --- Penumbra/Services/MigrationManager.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Penumbra/Services/MigrationManager.cs b/Penumbra/Services/MigrationManager.cs index 1e6cb6b6..e377b65e 100644 --- a/Penumbra/Services/MigrationManager.cs +++ b/Penumbra/Services/MigrationManager.cs @@ -249,11 +249,13 @@ public class MigrationManager(Configuration config) : IService { var data = s.ToArray(); var mdl = new MdlFile(data); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); MigrateModel(path, mdl, false); Penumbra.Log.Debug($"Migrated model {reader.Entry.Key} from V5 to V6 during import."); } else { + Directory.CreateDirectory(Path.GetDirectoryName(path)!); using var f = File.Open(path, FileMode.Create, FileAccess.Write); s.Seek(0, SeekOrigin.Begin); s.WriteTo(f); @@ -279,6 +281,7 @@ public class MigrationManager(Configuration config) : IService Penumbra.Log.Debug($"Migrated material {reader.Entry.Key} to Dawntrail during import."); } + Directory.CreateDirectory(Path.GetDirectoryName(path)!); using var f = File.Open(path, FileMode.Create, FileAccess.Write); s.Seek(0, SeekOrigin.Begin); s.WriteTo(f); From 1efd4938343bb0106d4a2e7c7166c7eebf8c8f12 Mon Sep 17 00:00:00 2001 From: Actions User Date: Tue, 9 Jul 2024 15:52:40 +0000 Subject: [PATCH 285/433] [CI] Updating repo.json for testing_1.2.0.1 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 4f567253..4231cf65 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.0", + "TestingAssemblyVersion": "1.2.0.1", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 10, @@ -18,7 +18,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.0/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.1/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 3b980c1a49e8292d5a602f8852c373345fe767e8 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 9 Jul 2024 18:09:37 +0200 Subject: [PATCH 286/433] Fix two import bugs. --- Penumbra/Services/MigrationManager.cs | 1 + Penumbra/UI/Classes/MigrationSectionDrawer.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/Penumbra/Services/MigrationManager.cs b/Penumbra/Services/MigrationManager.cs index e377b65e..5b353912 100644 --- a/Penumbra/Services/MigrationManager.cs +++ b/Penumbra/Services/MigrationManager.cs @@ -243,6 +243,7 @@ public class MigrationManager(Configuration config) : IService using var s = new MemoryStream(); using var e = reader.OpenEntryStream(); e.CopyTo(s); + s.Position = 0; using var b = new BinaryReader(s); var version = b.ReadUInt32(); if (version == MdlFile.V5) diff --git a/Penumbra/UI/Classes/MigrationSectionDrawer.cs b/Penumbra/UI/Classes/MigrationSectionDrawer.cs index ec76ddae..d588eaa0 100644 --- a/Penumbra/UI/Classes/MigrationSectionDrawer.cs +++ b/Penumbra/UI/Classes/MigrationSectionDrawer.cs @@ -39,6 +39,7 @@ public class MigrationSectionDrawer(MigrationManager migrationManager, Configura ImUtf8.HoverTooltip("This increments the version marker and restructures the bone table to the new version."u8); + value = config.MigrateImportedMaterialsToLegacy; if (ImUtf8.Checkbox("Automatically Migrate Materials to Dawntrail on Import"u8, ref value)) { config.MigrateImportedMaterialsToLegacy = value; From 806e001bad44e7bb6748c6ff20856ff9dbb41633 Mon Sep 17 00:00:00 2001 From: Actions User Date: Tue, 9 Jul 2024 16:11:41 +0000 Subject: [PATCH 287/433] [CI] Updating repo.json for testing_1.2.0.2 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 4231cf65..a5ec39b5 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.1", + "TestingAssemblyVersion": "1.2.0.2", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 10, @@ -18,7 +18,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.1/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.2/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From baa439d2463d3de0276005d70d9bc7ca954b9a26 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 9 Jul 2024 18:31:24 +0200 Subject: [PATCH 288/433] Fix enable/disable draw offsets. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index d8c784e4..49c5ba01 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit d8c784e443112d17d93ba7e6ab5d54f00f7f1477 +Subproject commit 49c5ba0115814f809aaf4f31e6dcf321efb52237 From 37ffe528699191c4a06d7fa2a37412b927d10f04 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 9 Jul 2024 18:36:45 +0200 Subject: [PATCH 289/433] Fix issue with file substitutions. --- Penumbra/Api/DalamudSubstitutionProvider.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Penumbra/Api/DalamudSubstitutionProvider.cs b/Penumbra/Api/DalamudSubstitutionProvider.cs index 1c2cebcc..6347447a 100644 --- a/Penumbra/Api/DalamudSubstitutionProvider.cs +++ b/Penumbra/Api/DalamudSubstitutionProvider.cs @@ -1,3 +1,4 @@ +using Dalamud.Interface; using Dalamud.Plugin.Services; using OtterGui.Services; using Penumbra.Collections; @@ -13,6 +14,7 @@ namespace Penumbra.Api; public class DalamudSubstitutionProvider : IDisposable, IApiService { private readonly ITextureSubstitutionProvider _substitution; + private readonly IUiBuilder _uiBuilder; private readonly ActiveCollectionData _activeCollectionData; private readonly Configuration _config; private readonly CommunicatorService _communicator; @@ -21,9 +23,10 @@ public class DalamudSubstitutionProvider : IDisposable, IApiService => _config.UseDalamudUiTextureRedirection; public DalamudSubstitutionProvider(ITextureSubstitutionProvider substitution, ActiveCollectionData activeCollectionData, - Configuration config, CommunicatorService communicator) + Configuration config, CommunicatorService communicator, IUiBuilder ui) { _substitution = substitution; + _uiBuilder = ui; _activeCollectionData = activeCollectionData; _config = config; _communicator = communicator; @@ -41,6 +44,9 @@ public class DalamudSubstitutionProvider : IDisposable, IApiService public void ResetSubstitutions(IEnumerable paths) { + if (!_uiBuilder.UiPrepared) + return; + var transformed = paths .Where(p => (p.Path.StartsWith("ui/"u8) || p.Path.StartsWith("common/font/"u8)) && p.Path.EndsWith(".tex"u8)) .Select(p => p.ToString()); @@ -91,10 +97,7 @@ public class DalamudSubstitutionProvider : IDisposable, IApiService case ResolvedFileChanged.Type.Added: case ResolvedFileChanged.Type.Removed: case ResolvedFileChanged.Type.Replaced: - ResetSubstitutions(new[] - { - key, - }); + ResetSubstitutions([key]); break; case ResolvedFileChanged.Type.FullRecomputeStart: case ResolvedFileChanged.Type.FullRecomputeFinished: From e2112202a05e50fb5ec1ea7c5f21b0b6f43a08d2 Mon Sep 17 00:00:00 2001 From: Actions User Date: Tue, 9 Jul 2024 16:38:39 +0000 Subject: [PATCH 290/433] [CI] Updating repo.json for testing_1.2.0.3 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index a5ec39b5..758a10d0 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.2", + "TestingAssemblyVersion": "1.2.0.3", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 10, @@ -18,7 +18,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.2/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.3/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 1be75444cd8344b8a5774193bdf097c9b9f47d17 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 10 Jul 2024 12:51:47 +0200 Subject: [PATCH 291/433] Update BNPC Names --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 49c5ba01..a64a30bf 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 49c5ba0115814f809aaf4f31e6dcf321efb52237 +Subproject commit a64a30bf29cf297285ecde0579830b4d7fbae2d9 From 380dd0cffb8bc675c04c282d8bf92f72c5a1c3bb Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 11 Jul 2024 01:40:45 +0200 Subject: [PATCH 292/433] Fix texture writing. --- Penumbra/Import/Textures/TexFileParser.cs | 4 +- Penumbra/UI/Tabs/Debug/DebugTab.cs | 7 +- Penumbra/UI/Tabs/Debug/TexHeaderDrawer.cs | 117 ++++++++++++++++++++++ Penumbra/UI/Tabs/EffectiveTab.cs | 2 +- 4 files changed, 125 insertions(+), 5 deletions(-) create mode 100644 Penumbra/UI/Tabs/Debug/TexHeaderDrawer.cs diff --git a/Penumbra/Import/Textures/TexFileParser.cs b/Penumbra/Import/Textures/TexFileParser.cs index 09025b61..ae4a39c0 100644 --- a/Penumbra/Import/Textures/TexFileParser.cs +++ b/Penumbra/Import/Textures/TexFileParser.cs @@ -79,8 +79,8 @@ public static class TexFileParser w.Write(header.Width); w.Write(header.Height); w.Write(header.Depth); - w.Write(header.MipCount); - w.Write(header.MipUnknownFlag); // TODO Lumina Update + w.Write((byte)(header.MipCount | (header.MipUnknownFlag ? 0x80 : 0))); + w.Write(header.ArraySize); unsafe { w.Write(header.LodOffset[0]); diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index be92b94e..4966dd64 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -15,7 +15,6 @@ using Microsoft.Extensions.DependencyInjection; using OtterGui; using OtterGui.Classes; using OtterGui.Services; -using OtterGui.Text; using OtterGui.Widgets; using Penumbra.Api; using Penumbra.Collections.Manager; @@ -96,6 +95,7 @@ public class DebugTab : Window, ITab, IUiService private readonly IClientState _clientState; private readonly IpcTester _ipcTester; private readonly CrashHandlerPanel _crashHandlerPanel; + private readonly TexHeaderDrawer _texHeaderDrawer; public DebugTab(PerformanceTracker performance, Configuration config, CollectionManager collectionManager, ObjectManager objects, IClientState clientState, @@ -105,7 +105,7 @@ public class DebugTab : Window, ITab, IUiService DrawObjectState drawObjectState, PathState pathState, SubfileHelper subfileHelper, IdentifiedCollectionCache identifiedCollectionCache, CutsceneService cutsceneService, ModImportManager modImporter, ImportPopup importPopup, FrameworkManager framework, TextureManager textureManager, ShaderReplacementFixer shaderReplacementFixer, RedrawService redraws, DictEmote emotes, - Diagnostics diagnostics, IpcTester ipcTester, CrashHandlerPanel crashHandlerPanel) + Diagnostics diagnostics, IpcTester ipcTester, CrashHandlerPanel crashHandlerPanel, TexHeaderDrawer texHeaderDrawer) : base("Penumbra Debug Window", ImGuiWindowFlags.NoCollapse) { IsOpen = true; @@ -141,6 +141,7 @@ public class DebugTab : Window, ITab, IUiService _diagnostics = diagnostics; _ipcTester = ipcTester; _crashHandlerPanel = crashHandlerPanel; + _texHeaderDrawer = texHeaderDrawer; _objects = objects; _clientState = clientState; } @@ -176,6 +177,8 @@ public class DebugTab : Window, ITab, IUiService ImGui.NewLine(); DrawCollectionCaches(); ImGui.NewLine(); + _texHeaderDrawer.Draw(); + ImGui.NewLine(); DrawDebugCharacterUtility(); ImGui.NewLine(); DrawShaderReplacementFixer(); diff --git a/Penumbra/UI/Tabs/Debug/TexHeaderDrawer.cs b/Penumbra/UI/Tabs/Debug/TexHeaderDrawer.cs new file mode 100644 index 00000000..08d51184 --- /dev/null +++ b/Penumbra/UI/Tabs/Debug/TexHeaderDrawer.cs @@ -0,0 +1,117 @@ +using Dalamud.Interface.DragDrop; +using Dalamud.Interface.Utility.Raii; +using ImGuiNET; +using Lumina.Data.Files; +using OtterGui.Services; +using OtterGui.Text; +using Penumbra.UI.Classes; + +namespace Penumbra.UI.Tabs.Debug; + +public class TexHeaderDrawer(IDragDropManager dragDrop) : IUiService +{ + private string? _path; + private TexFile.TexHeader _header; + private byte[]? _tex; + private Exception? _exception; + + public void Draw() + { + using var header = ImUtf8.CollapsingHeaderId("Tex Header"u8); + if (!header) + return; + + DrawDragDrop(); + DrawData(); + } + + private void DrawDragDrop() + { + dragDrop.CreateImGuiSource("TexFileDragDrop", m => m.Files.Count == 1 && m.Extensions.Contains(".tex"), m => + { + ImUtf8.Text($"Dragging {m.Files[0]}..."); + return true; + }); + + ImUtf8.Button("Drag .tex here..."); + if (dragDrop.CreateImGuiTarget("TexFileDragDrop", out var files, out _)) + ReadTex(files[0]); + } + + private void DrawData() + { + if (_path == null) + return; + + ImUtf8.TextFramed(_path, 0, borderColor: 0xFFFFFFFF); + + + if (_exception != null) + { + using var color = ImRaii.PushColor(ImGuiCol.Text, Colors.RegexWarningBorder); + ImUtf8.TextWrapped($"Failure to load file:\n{_exception}"); + } + else if (_tex != null) + { + using var table = ImRaii.Table("table", 2, ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg); + if (!table) + return; + + TableLine("Format"u8, _header.Format); + TableLine("Width"u8, _header.Width); + TableLine("Height"u8, _header.Height); + TableLine("Depth"u8, _header.Depth); + TableLine("Mip Levels"u8, _header.MipCount); + TableLine("Array Size"u8, _header.ArraySize); + TableLine("Type"u8, _header.Type); + TableLine("Mip Flag"u8, _header.MipUnknownFlag); + TableLine("Byte Size"u8, _tex.Length); + unsafe + { + TableLine("LoD Offset 0"u8, _header.LodOffset[0]); + TableLine("LoD Offset 1"u8, _header.LodOffset[1]); + TableLine("LoD Offset 2"u8, _header.LodOffset[2]); + TableLine("LoD Offset 0"u8, _header.OffsetToSurface[0]); + TableLine("LoD Offset 1"u8, _header.OffsetToSurface[1]); + TableLine("LoD Offset 2"u8, _header.OffsetToSurface[2]); + TableLine("LoD Offset 3"u8, _header.OffsetToSurface[3]); + TableLine("LoD Offset 4"u8, _header.OffsetToSurface[4]); + TableLine("LoD Offset 5"u8, _header.OffsetToSurface[5]); + TableLine("LoD Offset 6"u8, _header.OffsetToSurface[6]); + TableLine("LoD Offset 7"u8, _header.OffsetToSurface[7]); + TableLine("LoD Offset 8"u8, _header.OffsetToSurface[8]); + TableLine("LoD Offset 9"u8, _header.OffsetToSurface[9]); + TableLine("LoD Offset 10"u8, _header.OffsetToSurface[10]); + TableLine("LoD Offset 11"u8, _header.OffsetToSurface[11]); + TableLine("LoD Offset 12"u8, _header.OffsetToSurface[12]); + } + } + } + + private static void TableLine(ReadOnlySpan text, T value) + { + ImGui.TableNextColumn(); + ImUtf8.Text(text); + ImGui.TableNextColumn(); + ImUtf8.Text($"{value}"); + } + + private unsafe void ReadTex(string path) + { + try + { + _path = path; + _tex = File.ReadAllBytes(_path); + if (_tex.Length < sizeof(TexFile.TexHeader)) + throw new Exception($"Size {_tex.Length} does not include a header."); + + _header = MemoryMarshal.Read(_tex); + _exception = null; + } + catch (Exception ex) + { + _tex = null; + _exception = ex; + } + } +} diff --git a/Penumbra/UI/Tabs/EffectiveTab.cs b/Penumbra/UI/Tabs/EffectiveTab.cs index 1b9af75c..e0cab43f 100644 --- a/Penumbra/UI/Tabs/EffectiveTab.cs +++ b/Penumbra/UI/Tabs/EffectiveTab.cs @@ -26,7 +26,7 @@ public class EffectiveTab(CollectionManager collectionManager, CollectionSelectH SetupEffectiveSizes(); collectionHeader.Draw(true); DrawFilters(); - using var child = ImRaii.Child("##EffectiveChangesTab", -Vector2.One, false); + using var child = ImRaii.Child("##EffectiveChangesTab", ImGui.GetContentRegionAvail(), false); if (!child) return; From 34cbf37c32a24c9af7f1807653220d9ed0c47974 Mon Sep 17 00:00:00 2001 From: Actions User Date: Wed, 10 Jul 2024 23:43:04 +0000 Subject: [PATCH 293/433] [CI] Updating repo.json for testing_1.2.0.4 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 758a10d0..7e90f7a9 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.3", + "TestingAssemblyVersion": "1.2.0.4", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 10, @@ -18,7 +18,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.3/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.4/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 24597d7dc0cfa14bf25928f47b8e1f50665cbeee Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 12 Jul 2024 16:20:17 +0200 Subject: [PATCH 294/433] Fix mod normalization skipping the default submod. --- Penumbra/Mods/Editor/ModNormalizer.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Penumbra/Mods/Editor/ModNormalizer.cs b/Penumbra/Mods/Editor/ModNormalizer.cs index c0876f5d..30bf3d3f 100644 --- a/Penumbra/Mods/Editor/ModNormalizer.cs +++ b/Penumbra/Mods/Editor/ModNormalizer.cs @@ -277,6 +277,7 @@ public class ModNormalizer(ModManager _modManager, Configuration _config) : ISer private void ApplyRedirections() { + _modManager.OptionEditor.SetFiles(Mod.Default, _redirections[0][0]); foreach (var (group, groupIdx) in Mod.Groups.WithIndex()) foreach (var (container, containerIdx) in group.DataContainers.WithIndex()) _modManager.OptionEditor.SetFiles(container, _redirections[groupIdx + 1][containerIdx]); From 40be298d67d7a823ea67f62848028843d1b72244 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 12 Jul 2024 17:37:19 +0200 Subject: [PATCH 295/433] Add automatic reduplication for ui files in pmps, test. --- Penumbra/Configuration.cs | 1 + Penumbra/Import/TexToolsImporter.Archives.cs | 4 +- Penumbra/Import/TexToolsImporter.Gui.cs | 66 +++++------ Penumbra/Mods/Editor/ModNormalizer.cs | 109 ++++++++++++++++++- Penumbra/Penumbra.cs | 1 + Penumbra/UI/Tabs/SettingsTab.cs | 3 + 6 files changed, 145 insertions(+), 39 deletions(-) diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index f16569b5..63325433 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -96,6 +96,7 @@ public class Configuration : IPluginConfiguration, ISavable, IService public DoubleModifier DeleteModModifier { get; set; } = new(ModifierHotkey.Control, ModifierHotkey.Shift); public bool PrintSuccessfulCommandsToChat { get; set; } = true; public bool AutoDeduplicateOnImport { get; set; } = true; + public bool AutoReduplicateUiOnImport { get; set; } = true; public bool UseFileSystemCompression { get; set; } = true; public bool EnableHttpApi { get; set; } = true; diff --git a/Penumbra/Import/TexToolsImporter.Archives.cs b/Penumbra/Import/TexToolsImporter.Archives.cs index 63c170cb..dea343c6 100644 --- a/Penumbra/Import/TexToolsImporter.Archives.cs +++ b/Penumbra/Import/TexToolsImporter.Archives.cs @@ -1,9 +1,7 @@ -using System.IO; using Dalamud.Utility; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OtterGui.Filesystem; -using Penumbra.GameData.Files; using Penumbra.Import.Structs; using Penumbra.Mods; using SharpCompress.Archives; @@ -11,7 +9,6 @@ using SharpCompress.Archives.Rar; using SharpCompress.Archives.SevenZip; using SharpCompress.Common; using SharpCompress.Readers; -using static FFXIVClientStructs.FFXIV.Client.UI.Misc.ConfigModule; using ZipArchive = SharpCompress.Archives.Zip.ZipArchive; namespace Penumbra.Import; @@ -114,6 +111,7 @@ public partial class TexToolsImporter _currentModDirectory.Refresh(); _modManager.Creator.SplitMultiGroups(_currentModDirectory); + _editor.ModNormalizer.NormalizeUi(_currentModDirectory); return _currentModDirectory; } diff --git a/Penumbra/Import/TexToolsImporter.Gui.cs b/Penumbra/Import/TexToolsImporter.Gui.cs index 78665f30..309f107a 100644 --- a/Penumbra/Import/TexToolsImporter.Gui.cs +++ b/Penumbra/Import/TexToolsImporter.Gui.cs @@ -20,59 +20,61 @@ public partial class TexToolsImporter private string _currentOptionName = string.Empty; private string _currentFileName = string.Empty; - public void DrawProgressInfo(Vector2 size) + public bool DrawProgressInfo(Vector2 size) { if (_modPackCount == 0) { ImGuiUtil.Center("Nothing to extract."); + return false; } - else if (_modPackCount == _currentModPackIdx) - { - DrawEndState(); - } + + if (_modPackCount == _currentModPackIdx) + return DrawEndState(); + + ImGui.NewLine(); + var percentage = (float)_currentModPackIdx / _modPackCount; + ImGui.ProgressBar(percentage, size, $"Mod {_currentModPackIdx + 1} / {_modPackCount}"); + ImGui.NewLine(); + if (State == ImporterState.DeduplicatingFiles) + ImGui.TextUnformatted($"Deduplicating {_currentModName}..."); else + ImGui.TextUnformatted($"Extracting {_currentModName}..."); + + if (_currentNumOptions > 1) { ImGui.NewLine(); - var percentage = (float)_currentModPackIdx / _modPackCount; - ImGui.ProgressBar(percentage, size, $"Mod {_currentModPackIdx + 1} / {_modPackCount}"); ImGui.NewLine(); - if (State == ImporterState.DeduplicatingFiles) - ImGui.TextUnformatted($"Deduplicating {_currentModName}..."); - else - ImGui.TextUnformatted($"Extracting {_currentModName}..."); - - if (_currentNumOptions > 1) - { - ImGui.NewLine(); - ImGui.NewLine(); - percentage = _currentNumOptions == 0 ? 1f : _currentOptionIdx / (float)_currentNumOptions; - ImGui.ProgressBar(percentage, size, $"Option {_currentOptionIdx + 1} / {_currentNumOptions}"); - ImGui.NewLine(); - if (State != ImporterState.DeduplicatingFiles) - ImGui.TextUnformatted( - $"Extracting option {(_currentGroupName.Length == 0 ? string.Empty : $"{_currentGroupName} - ")}{_currentOptionName}..."); - } - - ImGui.NewLine(); - ImGui.NewLine(); - percentage = _currentNumFiles == 0 ? 1f : _currentFileIdx / (float)_currentNumFiles; - ImGui.ProgressBar(percentage, size, $"File {_currentFileIdx + 1} / {_currentNumFiles}"); + percentage = _currentNumOptions == 0 ? 1f : _currentOptionIdx / (float)_currentNumOptions; + ImGui.ProgressBar(percentage, size, $"Option {_currentOptionIdx + 1} / {_currentNumOptions}"); ImGui.NewLine(); if (State != ImporterState.DeduplicatingFiles) - ImGui.TextUnformatted($"Extracting file {_currentFileName}..."); + ImGui.TextUnformatted( + $"Extracting option {(_currentGroupName.Length == 0 ? string.Empty : $"{_currentGroupName} - ")}{_currentOptionName}..."); } + + ImGui.NewLine(); + ImGui.NewLine(); + percentage = _currentNumFiles == 0 ? 1f : _currentFileIdx / (float)_currentNumFiles; + ImGui.ProgressBar(percentage, size, $"File {_currentFileIdx + 1} / {_currentNumFiles}"); + ImGui.NewLine(); + if (State != ImporterState.DeduplicatingFiles) + ImGui.TextUnformatted($"Extracting file {_currentFileName}..."); + return false; } - private void DrawEndState() + private bool DrawEndState() { var success = ExtractedMods.Count(t => t.Error == null); + if (ImGui.IsKeyPressed(ImGuiKey.Escape)) + return true; + ImGui.TextUnformatted($"Successfully extracted {success} / {ExtractedMods.Count} files."); ImGui.NewLine(); using var table = ImRaii.Table("##files", 2); if (!table) - return; + return false; foreach (var (file, dir, ex) in ExtractedMods) { @@ -91,6 +93,8 @@ public partial class TexToolsImporter ImGuiUtil.HoverTooltip(ex.ToString()); } } + + return false; } public bool DrawCancelButton(Vector2 size) diff --git a/Penumbra/Mods/Editor/ModNormalizer.cs b/Penumbra/Mods/Editor/ModNormalizer.cs index 30bf3d3f..43cfc1ee 100644 --- a/Penumbra/Mods/Editor/ModNormalizer.cs +++ b/Penumbra/Mods/Editor/ModNormalizer.cs @@ -3,13 +3,15 @@ using OtterGui; using OtterGui.Classes; using OtterGui.Services; using OtterGui.Tasks; +using Penumbra.Mods.Groups; using Penumbra.Mods.Manager; using Penumbra.Mods.SubMods; +using Penumbra.Services; using Penumbra.String.Classes; namespace Penumbra.Mods.Editor; -public class ModNormalizer(ModManager _modManager, Configuration _config) : IService +public class ModNormalizer(ModManager modManager, Configuration config, SaveService saveService) : IService { private readonly List>> _redirections = []; @@ -39,6 +41,103 @@ public class ModNormalizer(ModManager _modManager, Configuration _config) : ISer Worker = TrackedTask.Run(NormalizeSync); } + public void NormalizeUi(DirectoryInfo modDirectory) + { + if (!config.AutoReduplicateUiOnImport) + return; + + if (modManager.Creator.LoadMod(modDirectory, false) is not { } mod) + return; + + Dictionary> paths = []; + Dictionary containers = []; + foreach (var container in mod.AllDataContainers) + { + foreach (var (gamePath, path) in container.Files) + { + if (!gamePath.Path.StartsWith("ui/"u8)) + continue; + + if (!paths.TryGetValue(path, out var list)) + { + list = []; + paths.Add(path, list); + } + + list.Add((container, gamePath)); + containers.TryAdd(container, string.Empty); + } + } + + foreach (var container in containers.Keys.ToList()) + { + if (container.Group == null) + containers[container] = mod.ModPath.FullName; + else + { + var groupDir = ModCreator.NewOptionDirectory(mod.ModPath, container.Group.Name, config.ReplaceNonAsciiOnImport); + var optionDir = ModCreator.NewOptionDirectory(groupDir, container.GetName(), config.ReplaceNonAsciiOnImport); + containers[container] = optionDir.FullName; + } + } + + var anyChanges = 0; + var modRootLength = mod.ModPath.FullName.Length + 1; + foreach (var (file, gamePaths) in paths) + { + if (gamePaths.Count < 2) + continue; + + var keptPath = false; + foreach (var (container, gamePath) in gamePaths) + { + var directory = containers[container]; + var relPath = new Utf8RelPath(gamePath).ToString(); + var newFilePath = Path.Combine(directory, relPath); + if (newFilePath == file.FullName) + { + Penumbra.Log.Verbose($"[UIReduplication] Kept {file.FullName[modRootLength..]} because new path was identical."); + keptPath = true; + continue; + } + + try + { + Directory.CreateDirectory(Path.GetDirectoryName(newFilePath)!); + File.Copy(file.FullName, newFilePath, false); + Penumbra.Log.Verbose($"[UIReduplication] Copied {file.FullName[modRootLength..]} to {newFilePath[modRootLength..]}."); + container.Files[gamePath] = new FullPath(newFilePath); + ++anyChanges; + } + catch (Exception ex) + { + Penumbra.Log.Error( + $"[UIReduplication] Failed to copy {file.FullName[modRootLength..]} to {newFilePath[modRootLength..]}:\n{ex}"); + } + } + + if (keptPath) + continue; + + try + { + File.Delete(file.FullName); + Penumbra.Log.Verbose($"[UIReduplication] Deleted {file.FullName[modRootLength..]} because no new path matched."); + } + catch (Exception ex) + { + Penumbra.Log.Error($"[UIReduplication] Failed to delete {file.FullName[modRootLength..]}:\n{ex}"); + } + } + + if (anyChanges == 0) + return; + + saveService.Save(SaveType.ImmediateSync, new ModSaveGroup(mod.Default, config.ReplaceNonAsciiOnImport)); + saveService.SaveAllOptionGroups(mod, false, config.ReplaceNonAsciiOnImport); + Penumbra.Log.Information($"[UIReduplication] Saved groups after {anyChanges} changes."); + } + private void NormalizeSync() { try @@ -168,7 +267,7 @@ public class ModNormalizer(ModManager _modManager, Configuration _config) : ISer // Normalize all other options. foreach (var (group, groupIdx) in Mod.Groups.WithIndex()) { - var groupDir = ModCreator.CreateModFolder(directory, group.Name, _config.ReplaceNonAsciiOnImport, true); + var groupDir = ModCreator.CreateModFolder(directory, group.Name, config.ReplaceNonAsciiOnImport, true); _redirections[groupIdx + 1].EnsureCapacity(group.DataContainers.Count); for (var i = _redirections[groupIdx + 1].Count; i < group.DataContainers.Count; ++i) _redirections[groupIdx + 1].Add([]); @@ -188,7 +287,7 @@ public class ModNormalizer(ModManager _modManager, Configuration _config) : ISer void HandleSubMod(DirectoryInfo groupDir, IModDataContainer option, Dictionary newDict) { var name = option.GetName(); - var optionDir = ModCreator.CreateModFolder(groupDir, name, _config.ReplaceNonAsciiOnImport, true); + var optionDir = ModCreator.CreateModFolder(groupDir, name, config.ReplaceNonAsciiOnImport, true); newDict.Clear(); newDict.EnsureCapacity(option.Files.Count); @@ -277,10 +376,10 @@ public class ModNormalizer(ModManager _modManager, Configuration _config) : ISer private void ApplyRedirections() { - _modManager.OptionEditor.SetFiles(Mod.Default, _redirections[0][0]); + modManager.OptionEditor.SetFiles(Mod.Default, _redirections[0][0]); foreach (var (group, groupIdx) in Mod.Groups.WithIndex()) foreach (var (container, containerIdx) in group.DataContainers.WithIndex()) - _modManager.OptionEditor.SetFiles(container, _redirections[groupIdx + 1][containerIdx]); + modManager.OptionEditor.SetFiles(container, _redirections[groupIdx + 1][containerIdx]); ++Step; } diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index 9f2db2e6..5f8d6805 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -213,6 +213,7 @@ public class Penumbra : IDalamudPlugin sb.Append( $"> **`Free Drive Space: `** {(drive != null ? Functions.HumanReadableSize(drive.AvailableFreeSpace) : "Unknown")}\n"); sb.Append($"> **`Auto-Deduplication: `** {_config.AutoDeduplicateOnImport}\n"); + sb.Append($"> **`Auto-UI-Reduplication: `** {_config.AutoReduplicateUiOnImport}\n"); sb.Append($"> **`Debug Mode: `** {_config.DebugMode}\n"); sb.Append( $"> **`Synchronous Load (Dalamud): `** {(_services.GetService().GetDalamudConfig(DalamudConfigService.WaitingForPluginsOption, out bool v) ? v.ToString() : "Unknown")}\n"); diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index 8a4d6874..ab47ce7c 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -757,6 +757,9 @@ public class SettingsTab : ITab, IUiService Checkbox("Auto Deduplicate on Import", "Automatically deduplicate mod files on import. This will make mod file sizes smaller, but deletes (binary identical) files.", _config.AutoDeduplicateOnImport, v => _config.AutoDeduplicateOnImport = v); + Checkbox("Auto Reduplicate UI Files on PMP Import", + "Automatically reduplicate and normalize UI-specific files on import from PMP files. This is STRONGLY recommended because deduplicated UI files crash the game.", + _config.AutoReduplicateUiOnImport, v => _config.AutoReduplicateUiOnImport = v); DrawCompressionBox(); Checkbox("Keep Default Metadata Changes on Import", "Normally, metadata changes that equal their default values, which are sometimes exported by TexTools, are discarded. " From 22af545e8db41e6ec712eb3294dfd54dec6b42e1 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 12 Jul 2024 17:37:55 +0200 Subject: [PATCH 296/433] Add image strings to groups and mods to keep them in the json on saves. --- Penumbra/Mods/Groups/IModGroup.cs | 1 + Penumbra/Mods/Groups/ImcModGroup.cs | 2 ++ Penumbra/Mods/Groups/ModSaveGroup.cs | 2 ++ Penumbra/Mods/Groups/MultiModGroup.cs | 2 ++ Penumbra/Mods/Groups/SingleModGroup.cs | 2 ++ Penumbra/Mods/Manager/ModDataEditor.cs | 8 ++++++++ Penumbra/Mods/Mod.cs | 1 + Penumbra/Mods/ModMeta.cs | 1 + 8 files changed, 19 insertions(+) diff --git a/Penumbra/Mods/Groups/IModGroup.cs b/Penumbra/Mods/Groups/IModGroup.cs index 00f47e25..9327ced9 100644 --- a/Penumbra/Mods/Groups/IModGroup.cs +++ b/Penumbra/Mods/Groups/IModGroup.cs @@ -27,6 +27,7 @@ public interface IModGroup public Mod Mod { get; } public string Name { get; set; } public string Description { get; set; } + public string Image { get; set; } public GroupType Type { get; } public GroupDrawBehaviour Behaviour { get; } public ModPriority Priority { get; set; } diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs index 46204d6c..03896134 100644 --- a/Penumbra/Mods/Groups/ImcModGroup.cs +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -20,6 +20,7 @@ public class ImcModGroup(Mod mod) : IModGroup public Mod Mod { get; } = mod; public string Name { get; set; } = "Option"; public string Description { get; set; } = string.Empty; + public string Image { get; set; } = string.Empty; public GroupType Type => GroupType.Imc; @@ -170,6 +171,7 @@ public class ImcModGroup(Mod mod) : IModGroup { Name = json[nameof(Name)]?.ToObject() ?? string.Empty, Description = json[nameof(Description)]?.ToObject() ?? string.Empty, + Image = json[nameof(Image)]?.ToObject() ?? string.Empty, Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, DefaultEntry = json[nameof(DefaultEntry)]?.ToObject() ?? new ImcEntry(), AllVariants = json[nameof(AllVariants)]?.ToObject() ?? false, diff --git a/Penumbra/Mods/Groups/ModSaveGroup.cs b/Penumbra/Mods/Groups/ModSaveGroup.cs index c82c67c7..c465822b 100644 --- a/Penumbra/Mods/Groups/ModSaveGroup.cs +++ b/Penumbra/Mods/Groups/ModSaveGroup.cs @@ -88,6 +88,8 @@ public readonly struct ModSaveGroup : ISavable jWriter.WriteValue(group.Name); jWriter.WritePropertyName(nameof(group.Description)); jWriter.WriteValue(group.Description); + jWriter.WritePropertyName(nameof(group.Image)); + jWriter.WriteValue(group.Image); jWriter.WritePropertyName(nameof(group.Priority)); jWriter.WriteValue(group.Priority.Value); jWriter.WritePropertyName(nameof(group.Type)); diff --git a/Penumbra/Mods/Groups/MultiModGroup.cs b/Penumbra/Mods/Groups/MultiModGroup.cs index 95f49230..ee27d534 100644 --- a/Penumbra/Mods/Groups/MultiModGroup.cs +++ b/Penumbra/Mods/Groups/MultiModGroup.cs @@ -26,6 +26,7 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup public Mod Mod { get; } = mod; public string Name { get; set; } = "Group"; public string Description { get; set; } = string.Empty; + public string Image { get; set; } = string.Empty; public ModPriority Priority { get; set; } public Setting DefaultSettings { get; set; } public readonly List OptionData = []; @@ -69,6 +70,7 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup { Name = json[nameof(Name)]?.ToObject() ?? string.Empty, Description = json[nameof(Description)]?.ToObject() ?? string.Empty, + Image = json[nameof(Image)]?.ToObject() ?? string.Empty, Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, DefaultSettings = json[nameof(DefaultSettings)]?.ToObject() ?? Setting.Zero, }; diff --git a/Penumbra/Mods/Groups/SingleModGroup.cs b/Penumbra/Mods/Groups/SingleModGroup.cs index a559d609..cc606f42 100644 --- a/Penumbra/Mods/Groups/SingleModGroup.cs +++ b/Penumbra/Mods/Groups/SingleModGroup.cs @@ -24,6 +24,7 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup public Mod Mod { get; } = mod; public string Name { get; set; } = "Option"; public string Description { get; set; } = string.Empty; + public string Image { get; set; } = string.Empty; public ModPriority Priority { get; set; } public Setting DefaultSettings { get; set; } @@ -65,6 +66,7 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup { Name = json[nameof(Name)]?.ToObject() ?? string.Empty, Description = json[nameof(Description)]?.ToObject() ?? string.Empty, + Image = json[nameof(Image)]?.ToObject() ?? string.Empty, Priority = json[nameof(Priority)]?.ToObject() ?? ModPriority.Default, DefaultSettings = json[nameof(DefaultSettings)]?.ToObject() ?? Setting.Zero, }; diff --git a/Penumbra/Mods/Manager/ModDataEditor.cs b/Penumbra/Mods/Manager/ModDataEditor.cs index 4ab9deb1..91ae4a4c 100644 --- a/Penumbra/Mods/Manager/ModDataEditor.cs +++ b/Penumbra/Mods/Manager/ModDataEditor.cs @@ -22,6 +22,7 @@ public enum ModDataChangeType : ushort Favorite = 0x0200, LocalTags = 0x0400, Note = 0x0800, + Image = 0x1000, } public class ModDataEditor(SaveService saveService, CommunicatorService communicatorService) : IService @@ -113,6 +114,7 @@ public class ModDataEditor(SaveService saveService, CommunicatorService communic var newName = json[nameof(Mod.Name)]?.Value() ?? string.Empty; var newAuthor = json[nameof(Mod.Author)]?.Value() ?? string.Empty; var newDescription = json[nameof(Mod.Description)]?.Value() ?? string.Empty; + var newImage = json[nameof(Mod.Image)]?.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(ModMeta.FileVersion)]?.Value() ?? 0; @@ -138,6 +140,12 @@ public class ModDataEditor(SaveService saveService, CommunicatorService communic mod.Description = newDescription; } + if (mod.Image != newImage) + { + changes |= ModDataChangeType.Image; + mod.Image = newImage; + } + if (mod.Version != newVersion) { changes |= ModDataChangeType.Version; diff --git a/Penumbra/Mods/Mod.cs b/Penumbra/Mods/Mod.cs index a7f87dcd..fcea7133 100644 --- a/Penumbra/Mods/Mod.cs +++ b/Penumbra/Mods/Mod.cs @@ -51,6 +51,7 @@ public sealed class Mod : IMod public string Description { get; internal set; } = string.Empty; public string Version { get; internal set; } = string.Empty; public string Website { get; internal set; } = string.Empty; + public string Image { get; internal set; } = string.Empty; public IReadOnlyList ModTags { get; internal set; } = []; diff --git a/Penumbra/Mods/ModMeta.cs b/Penumbra/Mods/ModMeta.cs index 870d6d4f..39dd20e4 100644 --- a/Penumbra/Mods/ModMeta.cs +++ b/Penumbra/Mods/ModMeta.cs @@ -19,6 +19,7 @@ public readonly struct ModMeta(Mod mod) : ISavable { nameof(Mod.Name), JToken.FromObject(mod.Name) }, { nameof(Mod.Author), JToken.FromObject(mod.Author) }, { nameof(Mod.Description), JToken.FromObject(mod.Description) }, + { nameof(Mod.Image), JToken.FromObject(mod.Image) }, { nameof(Mod.Version), JToken.FromObject(mod.Version) }, { nameof(Mod.Website), JToken.FromObject(mod.Website) }, { nameof(Mod.ModTags), JToken.FromObject(mod.ModTags) }, From 94a05afbe0ce00f2c1195ca8ac206bc6f29c4584 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 12 Jul 2024 17:54:47 +0200 Subject: [PATCH 297/433] Make the import popup closeable by clicking outside if it is finished. --- Penumbra/Import/TexToolsImporter.Gui.cs | 23 ++++++++++------------- Penumbra/UI/ImportPopup.cs | 11 ++++++++--- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Penumbra/Import/TexToolsImporter.Gui.cs b/Penumbra/Import/TexToolsImporter.Gui.cs index 309f107a..a069204c 100644 --- a/Penumbra/Import/TexToolsImporter.Gui.cs +++ b/Penumbra/Import/TexToolsImporter.Gui.cs @@ -25,20 +25,22 @@ public partial class TexToolsImporter if (_modPackCount == 0) { ImGuiUtil.Center("Nothing to extract."); - return false; + return true; } if (_modPackCount == _currentModPackIdx) - return DrawEndState(); + { + DrawEndState(); + return true; + } ImGui.NewLine(); var percentage = (float)_currentModPackIdx / _modPackCount; ImGui.ProgressBar(percentage, size, $"Mod {_currentModPackIdx + 1} / {_modPackCount}"); ImGui.NewLine(); - if (State == ImporterState.DeduplicatingFiles) - ImGui.TextUnformatted($"Deduplicating {_currentModName}..."); - else - ImGui.TextUnformatted($"Extracting {_currentModName}..."); + ImGui.TextUnformatted(State == ImporterState.DeduplicatingFiles + ? $"Deduplicating {_currentModName}..." + : $"Extracting {_currentModName}..."); if (_currentNumOptions > 1) { @@ -63,18 +65,15 @@ public partial class TexToolsImporter } - private bool DrawEndState() + private void DrawEndState() { var success = ExtractedMods.Count(t => t.Error == null); - if (ImGui.IsKeyPressed(ImGuiKey.Escape)) - return true; - ImGui.TextUnformatted($"Successfully extracted {success} / {ExtractedMods.Count} files."); ImGui.NewLine(); using var table = ImRaii.Table("##files", 2); if (!table) - return false; + return; foreach (var (file, dir, ex) in ExtractedMods) { @@ -93,8 +92,6 @@ public partial class TexToolsImporter ImGuiUtil.HoverTooltip(ex.ToString()); } } - - return false; } public bool DrawCancelButton(Vector2 size) diff --git a/Penumbra/UI/ImportPopup.cs b/Penumbra/UI/ImportPopup.cs index fb2028b5..28767edc 100644 --- a/Penumbra/UI/ImportPopup.cs +++ b/Penumbra/UI/ImportPopup.cs @@ -1,4 +1,6 @@ +using Dalamud.Game.ClientState.Keys; using Dalamud.Interface.Windowing; +using Dalamud.Plugin.Services; using ImGuiNET; using OtterGui.Raii; using OtterGui.Services; @@ -68,13 +70,16 @@ public sealed class ImportPopup : Window, IUiService ImGui.SetNextWindowSize(size); using var popup = ImRaii.Popup(importPopup, ImGuiWindowFlags.Modal); PopupWasDrawn = true; + var terminate = false; using (var child = ImRaii.Child("##import", new Vector2(-1, size.Y - ImGui.GetFrameHeight() * 2))) { - if (child) - import.DrawProgressInfo(new Vector2(-1, ImGui.GetFrameHeight())); + if (child.Success && import.DrawProgressInfo(new Vector2(-1, ImGui.GetFrameHeight()))) + if (!ImGui.IsMouseHoveringRect(ImGui.GetWindowPos(), ImGui.GetWindowPos() + ImGui.GetWindowSize()) + && ImGui.IsMouseClicked(ImGuiMouseButton.Left)) + terminate = true; } - var terminate = import.State == ImporterState.Done + terminate |= import.State == ImporterState.Done ? ImGui.Button("Close", -Vector2.UnitX) : import.DrawCancelButton(-Vector2.UnitX); if (terminate) From d815266ed7b92ce2c474cd8e4da9c36a2d7fdcf4 Mon Sep 17 00:00:00 2001 From: Actions User Date: Fri, 12 Jul 2024 15:56:58 +0000 Subject: [PATCH 298/433] [CI] Updating repo.json for testing_1.2.0.5 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 7e90f7a9..51e458a8 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.4", + "TestingAssemblyVersion": "1.2.0.5", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 10, @@ -18,7 +18,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.4/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.5/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From d6f61f06cbf16e23900b4f317e5ecc8f63c7fc1b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 12 Jul 2024 22:15:00 +0200 Subject: [PATCH 299/433] Add TestingDalamudApiLevel --- repo.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 51e458a8..59daa590 100644 --- a/repo.json +++ b/repo.json @@ -9,9 +9,10 @@ "TestingAssemblyVersion": "1.2.0.5", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", - "DalamudApiLevel": 10, + "DalamudApiLevel": 9, + "TestingDalamudApiLevel": 10, "IsHide": "False", - "IsTestingExclusive": "True", + "IsTestingExclusive": "False", "DownloadCount": 0, "LastUpdate": 0, "LoadPriority": 69420, From e46fcc4af1bbf083ab5ce07edea67ad5a554589d Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 14 Jul 2024 20:38:54 +0200 Subject: [PATCH 300/433] Gracefully deal with invalid offhand IMCs. --- Penumbra.GameData | 2 +- Penumbra/Meta/Manipulations/Imc.cs | 3 ++- Penumbra/Services/ValidityChecker.cs | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index a64a30bf..2c067b4f 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit a64a30bf29cf297285ecde0579830b4d7fbae2d9 +Subproject commit 2c067b4f3c1d84888c2b961a93fe2de01fffe5f1 diff --git a/Penumbra/Meta/Manipulations/Imc.cs b/Penumbra/Meta/Manipulations/Imc.cs index 44c60942..d4887fe2 100644 --- a/Penumbra/Meta/Manipulations/Imc.cs +++ b/Penumbra/Meta/Manipulations/Imc.cs @@ -100,7 +100,8 @@ public readonly record struct ImcIdentifier( return false; if (!Enum.IsDefined(ObjectType)) return false; - + if (ItemData.AdaptOffhandImc(PrimaryId, out _)) + return false; break; } diff --git a/Penumbra/Services/ValidityChecker.cs b/Penumbra/Services/ValidityChecker.cs index cefee139..5feeab02 100644 --- a/Penumbra/Services/ValidityChecker.cs +++ b/Penumbra/Services/ValidityChecker.cs @@ -45,7 +45,7 @@ public class ValidityChecker : IService public void LogExceptions() { if (ImcExceptions.Count > 0) - Penumbra.Messager.NotificationMessage($"{ImcExceptions} IMC Exceptions thrown during Penumbra load. Please repair your game files.", + Penumbra.Messager.NotificationMessage($"{ImcExceptions.Count} IMC Exceptions thrown during Penumbra load. Please repair your game files.", NotificationType.Warning); } From 07c3be641ddc7950a3a818a44b5d155e828a3b7d Mon Sep 17 00:00:00 2001 From: Actions User Date: Sun, 14 Jul 2024 18:41:39 +0000 Subject: [PATCH 301/433] [CI] Updating repo.json for testing_1.2.0.6 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 59daa590..4e1b17ba 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.5", + "TestingAssemblyVersion": "1.2.0.6", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.5/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.6/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 9c781f8563fb0bc85247fb83aab79980943cb2d5 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 16 Jul 2024 22:04:36 +0200 Subject: [PATCH 302/433] Disable material migration for now --- Penumbra.GameData | 2 +- Penumbra/Services/MigrationManager.cs | 22 ++++++++++----- Penumbra/UI/Classes/MigrationSectionDrawer.cs | 28 ++++++++++--------- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 2c067b4f..c2a4c4ee 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 2c067b4f3c1d84888c2b961a93fe2de01fffe5f1 +Subproject commit c2a4c4ee7470c5afbd3dd7731697ab49c055d1e3 diff --git a/Penumbra/Services/MigrationManager.cs b/Penumbra/Services/MigrationManager.cs index 5b353912..7115fe4d 100644 --- a/Penumbra/Services/MigrationManager.cs +++ b/Penumbra/Services/MigrationManager.cs @@ -233,6 +233,8 @@ public class MigrationManager(Configuration config) : IService /// Writes or migrates a .mdl file during extraction from a regular archive. public void MigrateMdlDuringExtraction(IReader reader, string directory, ExtractionOptions options) { + // TODO reactivate when this works. + return; if (!config.MigrateImportedModelsToV6) { reader.WriteEntryToDirectory(directory, options); @@ -265,6 +267,8 @@ public class MigrationManager(Configuration config) : IService public void MigrateMtrlDuringExtraction(IReader reader, string directory, ExtractionOptions options) { + // TODO reactivate when this works. + return; if (!config.MigrateImportedMaterialsToLegacy) { reader.WriteEntryToDirectory(directory, options); @@ -276,16 +280,20 @@ public class MigrationManager(Configuration config) : IService using var e = reader.OpenEntryStream(); e.CopyTo(s); var file = new MtrlFile(s.GetBuffer()); - if (!file.IsDawnTrail) - { - file.MigrateToDawntrail(); - Penumbra.Log.Debug($"Migrated material {reader.Entry.Key} to Dawntrail during import."); - } Directory.CreateDirectory(Path.GetDirectoryName(path)!); using var f = File.Open(path, FileMode.Create, FileAccess.Write); - s.Seek(0, SeekOrigin.Begin); - s.WriteTo(f); + if (file.IsDawnTrail) + { + file.MigrateToDawntrail(); + Penumbra.Log.Debug($"Migrated material {reader.Entry.Key} to Dawntrail during import."); + f.Write(file.Write()); + } + else + { + s.Seek(0, SeekOrigin.Begin); + s.WriteTo(f); + } } /// Update the data of a .mdl file during TTMP extraction. Returns either the existing array or a new one. diff --git a/Penumbra/UI/Classes/MigrationSectionDrawer.cs b/Penumbra/UI/Classes/MigrationSectionDrawer.cs index d588eaa0..a4a2010f 100644 --- a/Penumbra/UI/Classes/MigrationSectionDrawer.cs +++ b/Penumbra/UI/Classes/MigrationSectionDrawer.cs @@ -22,10 +22,11 @@ public class MigrationSectionDrawer(MigrationManager migrationManager, Configura DrawMdlMigration(); DrawMdlRestore(); DrawMdlCleanup(); - ImGui.Separator(); - DrawMtrlMigration(); - DrawMtrlRestore(); - DrawMtrlCleanup(); + // TODO enable when this works + //ImGui.Separator(); + //DrawMtrlMigration(); + //DrawMtrlRestore(); + //DrawMtrlCleanup(); } private void DrawSettings() @@ -39,15 +40,16 @@ public class MigrationSectionDrawer(MigrationManager migrationManager, Configura ImUtf8.HoverTooltip("This increments the version marker and restructures the bone table to the new version."u8); - value = config.MigrateImportedMaterialsToLegacy; - if (ImUtf8.Checkbox("Automatically Migrate Materials to Dawntrail on Import"u8, ref value)) - { - config.MigrateImportedMaterialsToLegacy = value; - config.Save(); - } - - ImUtf8.HoverTooltip( - "This currently only increases the color-table size and switches the shader from 'character.shpk' to 'characterlegacy.shpk', if the former is used."u8); + // TODO enable when this works + //value = config.MigrateImportedMaterialsToLegacy; + //if (ImUtf8.Checkbox("Automatically Migrate Materials to Dawntrail on Import"u8, ref value)) + //{ + // config.MigrateImportedMaterialsToLegacy = value; + // config.Save(); + //} + // + //ImUtf8.HoverTooltip( + // "This currently only increases the color-table size and switches the shader from 'character.shpk' to 'characterlegacy.shpk', if the former is used."u8); ImUtf8.Checkbox("Create Backups During Manual Migration", ref _createBackups); } From 12dfaaef99192edfd00302d2c249f31f0c7a5509 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 16 Jul 2024 22:05:10 +0200 Subject: [PATCH 303/433] Fix broken mods being deleted instead of removed. Fix tags crashing when null instead of empty. --- Penumbra/Mods/Manager/ModDataEditor.cs | 4 ++-- Penumbra/Mods/Manager/ModManager.cs | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Penumbra/Mods/Manager/ModDataEditor.cs b/Penumbra/Mods/Manager/ModDataEditor.cs index 91ae4a4c..7a0467d0 100644 --- a/Penumbra/Mods/Manager/ModDataEditor.cs +++ b/Penumbra/Mods/Manager/ModDataEditor.cs @@ -59,7 +59,7 @@ public class ModDataEditor(SaveService saveService, CommunicatorService communic importDate = json[nameof(Mod.ImportDate)]?.Value() ?? importDate; favorite = json[nameof(Mod.Favorite)]?.Value() ?? favorite; note = json[nameof(Mod.Note)]?.Value() ?? note; - localTags = json[nameof(Mod.LocalTags)]?.Values().OfType() ?? localTags; + localTags = (json[nameof(Mod.LocalTags)] as JArray)?.Values().OfType() ?? localTags; save = false; } catch (Exception e) @@ -119,7 +119,7 @@ public class ModDataEditor(SaveService saveService, CommunicatorService communic var newWebsite = json[nameof(Mod.Website)]?.Value() ?? string.Empty; var newFileVersion = json[nameof(ModMeta.FileVersion)]?.Value() ?? 0; var importDate = json[nameof(Mod.ImportDate)]?.Value(); - var modTags = json[nameof(Mod.ModTags)]?.Values().OfType(); + var modTags = (json[nameof(Mod.ModTags)] as JArray)?.Values().OfType(); ModDataChangeType changes = 0; if (mod.Name != newName) diff --git a/Penumbra/Mods/Manager/ModManager.cs b/Penumbra/Mods/Manager/ModManager.cs index 4b19ea4c..f170a31b 100644 --- a/Penumbra/Mods/Manager/ModManager.cs +++ b/Penumbra/Mods/Manager/ModManager.cs @@ -115,12 +115,21 @@ public sealed class ModManager : ModStorage, IDisposable, IService Penumbra.Log.Error($"Could not delete the mod {mod.ModPath.Name}:\n{e}"); } + RemoveMod(mod); + } + + /// + /// Remove a loaded mod. The event is invoked before the mod is removed from the list. + /// Does not delete the mod from the filesystem. + /// Updates indices of later mods. + /// + public void RemoveMod(Mod mod) + { _communicator.ModPathChanged.Invoke(ModPathChangeType.Deleted, mod, mod.ModPath, null); foreach (var remainingMod in Mods.Skip(mod.Index + 1)) --remainingMod.Index; Mods.RemoveAt(mod.Index); - - Penumbra.Log.Debug($"Deleted mod {mod.Name}."); + Penumbra.Log.Debug($"Removed loaded mod {mod.Name} from list."); } /// @@ -135,10 +144,9 @@ public sealed class ModManager : ModStorage, IDisposable, IService if (!Creator.ReloadMod(mod, true, out var metaChange)) { Penumbra.Log.Warning(mod.Name.Length == 0 - ? $"Reloading mod {oldName} has failed, new name is empty. Deleting instead." - : $"Reloading mod {oldName} failed, {mod.ModPath.FullName} does not exist anymore or it ha. Deleting instead."); - - DeleteMod(mod); + ? $"Reloading mod {oldName} has failed, new name is empty. Removing from loaded mods instead." + : $"Reloading mod {oldName} failed, {mod.ModPath.FullName} does not exist anymore or it has invalid data. Removing from loaded mods instead."); + RemoveMod(mod); return; } From d952d83adf1b61ccb664420b29e7eb81593d3f7b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 16 Jul 2024 22:06:02 +0200 Subject: [PATCH 304/433] Fix redrawing while fishing while sitting. --- Penumbra/Interop/Services/RedrawService.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Penumbra/Interop/Services/RedrawService.cs b/Penumbra/Interop/Services/RedrawService.cs index 163b2c0e..f288a35e 100644 --- a/Penumbra/Interop/Services/RedrawService.cs +++ b/Penumbra/Interop/Services/RedrawService.cs @@ -301,9 +301,14 @@ public sealed unsafe partial class RedrawService : IDisposable (CharacterModes)6 => // fishing GetCurrentAnimationId(obj) switch { - 278 => true, // line out. - 283 => true, // reeling in - _ => false, + 278 => true, // line out. + 283 => true, // reeling in + 284 => true, // reeling in + 287 => true, // reeling in 2 + 3149 => true, // line out sitting, + 3155 => true, // reeling in sitting, + 3159 => true, // reeling in sitting 2, + _ => false, }, _ => false, }; @@ -419,7 +424,7 @@ public sealed unsafe partial class RedrawService : IDisposable if (housingManager == null) return; - var currentTerritory = (OutdoorTerritory*) housingManager->CurrentTerritory; + var currentTerritory = (OutdoorTerritory*)housingManager->CurrentTerritory; if (currentTerritory == null || currentTerritory->GetTerritoryType() is not HousingTerritoryType.Outdoor) return; From c98bee67a5bb28adf2f59efbb23968f862d7653a Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 16 Jul 2024 22:25:28 +0200 Subject: [PATCH 305/433] Update GameData. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index c2a4c4ee..67109fa9 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit c2a4c4ee7470c5afbd3dd7731697ab49c055d1e3 +Subproject commit 67109fa9e89d5ff5c9f93705208db92e836e9ef4 From 78af40d5078dc322174935c76606256fcc08fdc1 Mon Sep 17 00:00:00 2001 From: Actions User Date: Tue, 16 Jul 2024 20:27:30 +0000 Subject: [PATCH 306/433] [CI] Updating repo.json for testing_1.2.0.7 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 4e1b17ba..9f11c118 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.6", + "TestingAssemblyVersion": "1.2.0.7", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.6/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.7/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 519b3d4891c67650d3a333fe9d52f77240f989f9 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 16 Jul 2024 22:36:55 +0200 Subject: [PATCH 307/433] Update GameData. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 67109fa9..c9e0d890 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 67109fa9e89d5ff5c9f93705208db92e836e9ef4 +Subproject commit c9e0d8905137fef6ed7c247ee1d2824d3f89f3b2 From 89cbb3f60dde8e72f3b4b8e2887eae5701268d39 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 16 Jul 2024 23:02:17 +0200 Subject: [PATCH 308/433] Update GameData. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index c9e0d890..45f2c901 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit c9e0d8905137fef6ed7c247ee1d2824d3f89f3b2 +Subproject commit 45f2c901b3a0131eaee18b3520184baeb0d1049d From eb784dddf04e4380db02df6f0a58b95bd2635db8 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 17 Jul 2024 00:40:49 +0200 Subject: [PATCH 309/433] Fix missing file display. --- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index 83a8958b..e915a879 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -132,7 +132,7 @@ public partial class ModEditWindow : Window, IDisposable, IUiService sb.Append($" | {unused} Unused Files"); if (_editor.Files.Missing.Count > 0) - sb.Append($" | {_editor.Files.Available.Count} Missing Files"); + sb.Append($" | {_editor.Files.Missing.Count} Missing Files"); if (redirections > 0) sb.Append($" | {redirections} Redirections"); From 67a35b9abbb22576dc8d5c5d16e7201d3fbb4fa9 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 17 Jul 2024 00:49:40 +0200 Subject: [PATCH 310/433] stupid --- Penumbra/Services/MigrationManager.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Penumbra/Services/MigrationManager.cs b/Penumbra/Services/MigrationManager.cs index 7115fe4d..84318da6 100644 --- a/Penumbra/Services/MigrationManager.cs +++ b/Penumbra/Services/MigrationManager.cs @@ -233,8 +233,6 @@ public class MigrationManager(Configuration config) : IService /// Writes or migrates a .mdl file during extraction from a regular archive. public void MigrateMdlDuringExtraction(IReader reader, string directory, ExtractionOptions options) { - // TODO reactivate when this works. - return; if (!config.MigrateImportedModelsToV6) { reader.WriteEntryToDirectory(directory, options); @@ -267,9 +265,7 @@ public class MigrationManager(Configuration config) : IService public void MigrateMtrlDuringExtraction(IReader reader, string directory, ExtractionOptions options) { - // TODO reactivate when this works. - return; - if (!config.MigrateImportedMaterialsToLegacy) + if (!config.MigrateImportedMaterialsToLegacy || true) // TODO change when this is working { reader.WriteEntryToDirectory(directory, options); return; @@ -327,7 +323,7 @@ public class MigrationManager(Configuration config) : IService /// Update the data of a .mtrl file during TTMP extraction. Returns either the existing array or a new one. public byte[] MigrateTtmpMaterial(string path, byte[] data) { - if (!config.MigrateImportedMaterialsToLegacy) + if (!config.MigrateImportedMaterialsToLegacy || true) // TODO fix when this is working return data; try From ad877e68e610fcdec3f7fee993154a74b8929d3b Mon Sep 17 00:00:00 2001 From: Actions User Date: Tue, 16 Jul 2024 22:51:33 +0000 Subject: [PATCH 311/433] [CI] Updating repo.json for testing_1.2.0.8 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 9f11c118..850acf1b 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.7", + "TestingAssemblyVersion": "1.2.0.8", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.7/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.8/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 4824a96ab0f9854ab75248c38a3f7549b1aad97a Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 17 Jul 2024 01:35:56 +0200 Subject: [PATCH 312/433] Enable Mtrl Restore and Cleanup again. --- Penumbra/UI/Classes/MigrationSectionDrawer.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Penumbra/UI/Classes/MigrationSectionDrawer.cs b/Penumbra/UI/Classes/MigrationSectionDrawer.cs index a4a2010f..a3dcd23a 100644 --- a/Penumbra/UI/Classes/MigrationSectionDrawer.cs +++ b/Penumbra/UI/Classes/MigrationSectionDrawer.cs @@ -23,10 +23,10 @@ public class MigrationSectionDrawer(MigrationManager migrationManager, Configura DrawMdlRestore(); DrawMdlCleanup(); // TODO enable when this works - //ImGui.Separator(); + ImGui.Separator(); //DrawMtrlMigration(); - //DrawMtrlRestore(); - //DrawMtrlCleanup(); + DrawMtrlRestore(); + DrawMtrlCleanup(); } private void DrawSettings() From c9379b6d60d2a1f7f133c9fa7da3d7a6952a3651 Mon Sep 17 00:00:00 2001 From: Actions User Date: Tue, 16 Jul 2024 23:38:11 +0000 Subject: [PATCH 313/433] [CI] Updating repo.json for testing_1.2.0.9 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 850acf1b..b4becf0f 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.8", + "TestingAssemblyVersion": "1.2.0.9", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.8/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.9/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 1922353ba30e05a2b862f88b74a5e311fe5eb6d0 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 17 Jul 2024 02:01:52 +0200 Subject: [PATCH 314/433] Update GameData. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 45f2c901..c25ea7b1 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 45f2c901b3a0131eaee18b3520184baeb0d1049d +Subproject commit c25ea7b19a6db37dd36e12b9a7a71f72a192ab57 From e7c786b239a53b3e145cc1e1101324170eb883b2 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 17 Jul 2024 18:02:48 +0200 Subject: [PATCH 315/433] Add and rework hooks around EST entries. --- Penumbra.GameData | 2 +- Penumbra/Interop/Hooks/Meta/EstHook.cs | 47 +++++++++++-------- .../Hooks/Resources/ResolvePathHooksBase.cs | 38 ++++++++++++++- 3 files changed, 65 insertions(+), 22 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index c25ea7b1..94df458d 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit c25ea7b19a6db37dd36e12b9a7a71f72a192ab57 +Subproject commit 94df458dfb2a704a611fa77d955808284aeb23ac diff --git a/Penumbra/Interop/Hooks/Meta/EstHook.cs b/Penumbra/Interop/Hooks/Meta/EstHook.cs index 5b272019..ce002664 100644 --- a/Penumbra/Interop/Hooks/Meta/EstHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EstHook.cs @@ -3,51 +3,58 @@ using Penumbra.GameData; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.PathResolving; +using Penumbra.Interop.Structs; using Penumbra.Meta.Manipulations; +using CharacterUtility = Penumbra.Interop.Services.CharacterUtility; namespace Penumbra.Interop.Hooks.Meta; -public class EstHook : FastHook, IDisposable +public unsafe class EstHook : FastHook, IDisposable { - public delegate EstEntry Delegate(uint id, int estType, uint genderRace); + public delegate EstEntry Delegate(ResourceHandle* estResource, uint id, uint genderRace); - private readonly MetaState _metaState; + private readonly CharacterUtility _characterUtility; + private readonly MetaState _metaState; - public EstHook(HookManager hooks, MetaState metaState) + public EstHook(HookManager hooks, MetaState metaState, CharacterUtility characterUtility) { - _metaState = metaState; - Task = hooks.CreateHook("GetEstEntry", Sigs.GetEstEntry, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); + _metaState = metaState; + _characterUtility = characterUtility; + Task = hooks.CreateHook("FindEstEntry", Sigs.FindEstEntry, Detour, + metaState.Config.EnableMods && HookSettings.MetaEntryHooks); _metaState.Config.ModsEnabled += Toggle; } - private EstEntry Detour(uint genderRace, int estType, uint id) + private EstEntry Detour(ResourceHandle* estResource, uint genderRace, uint id) { EstEntry ret; if (_metaState.EstCollection.TryPeek(out var collection) && collection is { Valid: true, ModCollection.MetaCache: { } cache } - && cache.Est.TryGetValue(Convert(genderRace, estType, id), out var entry)) + && cache.Est.TryGetValue(Convert(estResource, genderRace, id), out var entry)) ret = entry.Entry; else - ret = Task.Result.Original(genderRace, estType, id); + ret = Task.Result.Original(estResource, genderRace, id); - Penumbra.Log.Excessive($"[GetEstEntry] Invoked with {genderRace}, {estType}, {id}, returned {ret.Value}."); + Penumbra.Log.Information($"[FindEstEntry] Invoked with 0x{(nint)estResource:X}, {genderRace}, {id}, returned {ret.Value}."); return ret; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static EstIdentifier Convert(uint genderRace, int estType, uint id) + private EstIdentifier Convert(ResourceHandle* estResource, uint genderRace, uint id) { var i = new PrimaryId((ushort)id); var gr = (GenderRace)genderRace; - var type = estType switch - { - 1 => EstType.Face, - 2 => EstType.Hair, - 3 => EstType.Head, - 4 => EstType.Body, - _ => (EstType)0, - }; - return new EstIdentifier(i, type, gr); + + if (estResource == _characterUtility.Address->BodyEstResource) + return new EstIdentifier(i, EstType.Body, gr); + if (estResource == _characterUtility.Address->HairEstResource) + return new EstIdentifier(i, EstType.Hair, gr); + if (estResource == _characterUtility.Address->FaceEstResource) + return new EstIdentifier(i, EstType.Face, gr); + if (estResource == _characterUtility.Address->HeadEstResource) + return new EstIdentifier(i, EstType.Head, gr); + + return new EstIdentifier(i, 0, gr); } public void Dispose() diff --git a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs index 8fa6d861..e1b6e46e 100644 --- a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs +++ b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs @@ -19,6 +19,7 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable private delegate nint NamedResolveDelegate(nint drawObject, nint pathBuffer, nint pathBufferSize, uint slotIndex, nint name); private delegate nint PerSlotResolveDelegate(nint drawObject, nint pathBuffer, nint pathBufferSize, uint slotIndex); private delegate nint SingleResolveDelegate(nint drawObject, nint pathBuffer, nint pathBufferSize); + private delegate nint SkeletonVFuncDelegate(nint drawObject, int estType, nint unk); private delegate nint TmbResolveDelegate(nint drawObject, nint pathBuffer, nint pathBufferSize, nint timelineName); @@ -37,6 +38,8 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable private readonly Hook _resolveSkpPathHook; private readonly Hook _resolveTmbPathHook; private readonly Hook _resolveVfxPathHook; + private readonly Hook? _vFunc81Hook; + private readonly Hook? _vFunc83Hook; private readonly PathState _parent; @@ -49,6 +52,9 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable _resolveSkpPathHook = Create($"{name}.{nameof(ResolveSkp)}", hooks, vTable[78], type, ResolveSkp, ResolveSkpHuman); _resolvePhybPathHook = Create($"{name}.{nameof(ResolvePhyb)}", hooks, vTable[79], type, ResolvePhyb, ResolvePhybHuman); + _vFunc81Hook = Create( $"{name}.{nameof(VFunc81)}", hooks, vTable[81], type, null, VFunc81); + + _vFunc83Hook = Create( $"{name}.{nameof(VFunc83)}", hooks, vTable[83], type, null, VFunc83); _resolvePapPathHook = Create( $"{name}.{nameof(ResolvePap)}", hooks, vTable[84], type, ResolvePap, ResolvePapHuman); _resolveTmbPathHook = Create( $"{name}.{nameof(ResolveTmb)}", hooks, vTable[85], ResolveTmb); @@ -58,6 +64,8 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable _resolveDecalPathHook = Create($"{name}.{nameof(ResolveDecal)}", hooks, vTable[92], ResolveDecal); _resolveVfxPathHook = Create( $"{name}.{nameof(ResolveVfx)}", hooks, vTable[93], type, ResolveVfx, ResolveVfxHuman); _resolveEidPathHook = Create( $"{name}.{nameof(ResolveEid)}", hooks, vTable[94], ResolveEid); + + // @formatter:on if (HookSettings.ResourceHooks) Enable(); @@ -77,6 +85,8 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable _resolveSkpPathHook.Enable(); _resolveTmbPathHook.Enable(); _resolveVfxPathHook.Enable(); + _vFunc81Hook?.Enable(); + _vFunc83Hook?.Enable(); } public void Disable() @@ -93,6 +103,8 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable _resolveSkpPathHook.Disable(); _resolveTmbPathHook.Disable(); _resolveVfxPathHook.Disable(); + _vFunc81Hook?.Disable(); + _vFunc83Hook?.Disable(); } public void Dispose() @@ -109,6 +121,8 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable _resolveSkpPathHook.Dispose(); _resolveTmbPathHook.Dispose(); _resolveVfxPathHook.Dispose(); + _vFunc81Hook?.Dispose(); + _vFunc83Hook?.Dispose(); } private nint ResolveDecal(nint drawObject, nint pathBuffer, nint pathBufferSize, uint slotIndex) @@ -224,14 +238,36 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable return ResolvePath(drawObject, pathBuffer); } + private nint VFunc81(nint drawObject, int estType, nint unk) + { + var collection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); + _parent.MetaState.EstCollection.Push(collection); + var ret = _vFunc81Hook!.Original(drawObject, estType, unk); + _parent.MetaState.EstCollection.Pop(); + return ret; + } + + private nint VFunc83(nint drawObject, int estType, nint unk) + { + var collection = _parent.CollectionResolver.IdentifyCollection((DrawObject*)drawObject, true); + _parent.MetaState.EstCollection.Push(collection); + var ret = _vFunc83Hook!.Original(drawObject, estType, unk); + _parent.MetaState.EstCollection.Pop(); + return ret; + } + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private static Hook Create(string name, HookManager hooks, nint address, Type type, T other, T human) where T : Delegate + [return: NotNullIfNotNull(nameof(other))] + private static Hook? Create(string name, HookManager hooks, nint address, Type type, T? other, T human) where T : Delegate { var del = type switch { Type.Human => human, _ => other, }; + if (del == null) + return null; + return hooks.CreateHook(name, address, del).Result; } From 6d0562180acbae396cbcbb8c9ed21f0d1b1eec2c Mon Sep 17 00:00:00 2001 From: Actions User Date: Wed, 17 Jul 2024 16:05:28 +0000 Subject: [PATCH 316/433] [CI] Updating repo.json for testing_1.2.0.10 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index b4becf0f..986c1707 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.9", + "TestingAssemblyVersion": "1.2.0.10", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.9/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.10/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 9bba1e2b31a26e890e34d72693e7d2521e43fea3 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 17 Jul 2024 18:05:57 +0200 Subject: [PATCH 317/433] Remove log spamming. --- Penumbra/Interop/Hooks/Meta/EstHook.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Interop/Hooks/Meta/EstHook.cs b/Penumbra/Interop/Hooks/Meta/EstHook.cs index ce002664..825b1244 100644 --- a/Penumbra/Interop/Hooks/Meta/EstHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EstHook.cs @@ -35,7 +35,7 @@ public unsafe class EstHook : FastHook, IDisposable else ret = Task.Result.Original(estResource, genderRace, id); - Penumbra.Log.Information($"[FindEstEntry] Invoked with 0x{(nint)estResource:X}, {genderRace}, {id}, returned {ret.Value}."); + Penumbra.Log.Excessive($"[FindEstEntry] Invoked with 0x{(nint)estResource:X}, {genderRace}, {id}, returned {ret.Value}."); return ret; } From 5abbd8b1101090afba2729cfbbf4adeeb673e615 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 17 Jul 2024 18:34:03 +0200 Subject: [PATCH 318/433] Hook UpdateRender despite per-frame calls. --- Penumbra.GameData | 2 +- Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs | 37 ------------------- .../{GetEqpIndirect2.cs => UpdateRender.cs} | 15 +++----- 3 files changed, 6 insertions(+), 48 deletions(-) delete mode 100644 Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs rename Penumbra/Interop/Hooks/Meta/{GetEqpIndirect2.cs => UpdateRender.cs} (55%) diff --git a/Penumbra.GameData b/Penumbra.GameData index 94df458d..c5ad1f3a 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 94df458dfb2a704a611fa77d955808284aeb23ac +Subproject commit c5ad1f3ae9818baa446327bdcf49fac65088c703 diff --git a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs b/Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs deleted file mode 100644 index 8bd49500..00000000 --- a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect.cs +++ /dev/null @@ -1,37 +0,0 @@ -using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; -using OtterGui.Services; -using Penumbra.Collections; -using Penumbra.GameData; -using Penumbra.Interop.PathResolving; - -namespace Penumbra.Interop.Hooks.Meta; - -public sealed unsafe class GetEqpIndirect : FastHook -{ - private readonly CollectionResolver _collectionResolver; - private readonly MetaState _metaState; - - public GetEqpIndirect(HookManager hooks, CollectionResolver collectionResolver, MetaState metaState) - { - _collectionResolver = collectionResolver; - _metaState = metaState; - Task = hooks.CreateHook("Get EQP Indirect", Sigs.GetEqpIndirect, Detour, HookSettings.MetaParentHooks); - } - - public delegate void Delegate(DrawObject* drawObject); - - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private void Detour(DrawObject* drawObject) - { - // Shortcut because this is also called all the time. - // Same thing is checked at the beginning of the original function. - if ((*(byte*)((nint)drawObject + Offsets.GetEqpIndirectSkip1) & 1) == 0 || *(ulong*)((nint)drawObject + Offsets.GetEqpIndirectSkip2) == 0) - return; - - Penumbra.Log.Excessive($"[Get EQP Indirect] Invoked on {(nint)drawObject:X}."); - var collection = _collectionResolver.IdentifyCollection(drawObject, true); - _metaState.EqpCollection.Push(collection); - Task.Result.Original(drawObject); - _metaState.EqpCollection.Pop(); - } -} diff --git a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs b/Penumbra/Interop/Hooks/Meta/UpdateRender.cs similarity index 55% rename from Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs rename to Penumbra/Interop/Hooks/Meta/UpdateRender.cs index e90674a8..95cc0e15 100644 --- a/Penumbra/Interop/Hooks/Meta/GetEqpIndirect2.cs +++ b/Penumbra/Interop/Hooks/Meta/UpdateRender.cs @@ -1,20 +1,20 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; -using Penumbra.GameData; using Penumbra.Interop.PathResolving; namespace Penumbra.Interop.Hooks.Meta; -public sealed unsafe class GetEqpIndirect2 : FastHook +/// The actual function is inlined, so we need to hook its only callsite: Human.UpdateRender instead. +public sealed unsafe class UpdateRender : FastHook { private readonly CollectionResolver _collectionResolver; private readonly MetaState _metaState; - public GetEqpIndirect2(HookManager hooks, CollectionResolver collectionResolver, MetaState metaState) + public UpdateRender(HookManager hooks, CollectionResolver collectionResolver, MetaState metaState, CharacterBaseVTables vTables) { _collectionResolver = collectionResolver; _metaState = metaState; - Task = hooks.CreateHook("Get EQP Indirect 2", Sigs.GetEqpIndirect2, Detour, HookSettings.MetaParentHooks); + Task = hooks.CreateHook("Human.UpdateRender", vTables.HumanVTable[4], Detour, HookSettings.MetaParentHooks); } public delegate void Delegate(DrawObject* drawObject); @@ -22,12 +22,7 @@ public sealed unsafe class GetEqpIndirect2 : FastHook [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private void Detour(DrawObject* drawObject) { - // Shortcut because this is also called all the time. - // Same thing is checked at the beginning of the original function. - if (((*(uint*)((nint)drawObject + Offsets.GetEqpIndirect2Skip) >> 0x12) & 1) == 0) - return; - - Penumbra.Log.Excessive($"[Get EQP Indirect 2] Invoked on {(nint)drawObject:X}."); + Penumbra.Log.Excessive($"[Human.UpdateRender] Invoked on {(nint)drawObject:X}."); var collection = _collectionResolver.IdentifyCollection(drawObject, true); _metaState.EqpCollection.Push(collection); Task.Result.Original(drawObject); From defba19b2d7714162d0eca036d212aef48aad667 Mon Sep 17 00:00:00 2001 From: Actions User Date: Wed, 17 Jul 2024 16:36:04 +0000 Subject: [PATCH 319/433] [CI] Updating repo.json for testing_1.2.0.11 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 986c1707..d715bca2 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.10", + "TestingAssemblyVersion": "1.2.0.11", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.10/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.11/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From f978b35b764fac7037f72f9ffa6f60dc2cd7bf13 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 19 Jul 2024 14:24:29 +0200 Subject: [PATCH 320/433] Make ResourceTrees work with UseNoMods. --- Penumbra.GameData | 2 +- Penumbra/Collections/Cache/MetaCache.cs | 4 ---- .../Collections/ModCollection.Cache.Access.cs | 9 --------- Penumbra/Import/Models/ModelManager.cs | 6 ++++-- .../ResourceTree/ResolveContext.PathResolution.cs | 15 ++++++++------- Penumbra/Interop/ResourceTree/ResolveContext.cs | 10 ++++++++-- .../Interop/ResourceTree/ResourceTreeFactory.cs | 4 +++- 7 files changed, 24 insertions(+), 26 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index c5ad1f3a..a1e637f8 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit c5ad1f3ae9818baa446327bdcf49fac65088c703 +Subproject commit a1e637f835c1a42732825e8e0690aeef0024b101 diff --git a/Penumbra/Collections/Cache/MetaCache.cs b/Penumbra/Collections/Cache/MetaCache.cs index 02056fad..1a6924a9 100644 --- a/Penumbra/Collections/Cache/MetaCache.cs +++ b/Penumbra/Collections/Cache/MetaCache.cs @@ -103,10 +103,6 @@ public class MetaCache(MetaFileManager manager, ModCollection collection) ~MetaCache() => Dispose(); - /// Try to obtain a manipulated IMC file. - public bool GetImcFile(Utf8GamePath path, [NotNullWhen(true)] out Meta.Files.ImcFile? file) - => Imc.GetFile(path.Path, out file); - internal EqdpEntry GetEqdpEntry(GenderRace race, bool accessory, PrimaryId primaryId) => Eqdp.ApplyFullEntry(primaryId, race, accessory, Meta.Files.ExpandedEqdpFile.GetDefault(manager, race, accessory, primaryId)); diff --git a/Penumbra/Collections/ModCollection.Cache.Access.cs b/Penumbra/Collections/ModCollection.Cache.Access.cs index 81751128..983509a4 100644 --- a/Penumbra/Collections/ModCollection.Cache.Access.cs +++ b/Penumbra/Collections/ModCollection.Cache.Access.cs @@ -43,15 +43,6 @@ public partial class ModCollection internal MetaCache? MetaCache => _cache?.Meta; - public bool GetImcFile(Utf8GamePath path, [NotNullWhen(true)] out ImcFile? file) - { - if (_cache != null) - return _cache.Meta.GetImcFile(path, out file); - - file = null; - return false; - } - internal IReadOnlyDictionary ResolvedFiles => _cache?.ResolvedFiles ?? new ConcurrentDictionary(); diff --git a/Penumbra/Import/Models/ModelManager.cs b/Penumbra/Import/Models/ModelManager.cs index 01396cfb..0c19bc0a 100644 --- a/Penumbra/Import/Models/ModelManager.cs +++ b/Penumbra/Import/Models/ModelManager.cs @@ -12,6 +12,8 @@ using Penumbra.GameData.Structs; using Penumbra.Import.Models.Export; using Penumbra.Import.Models.Import; using Penumbra.Import.Textures; +using Penumbra.Meta; +using Penumbra.Meta.Files; using Penumbra.Meta.Manipulations; using SharpGLTF.Scenes; using SixLabors.ImageSharp; @@ -22,7 +24,7 @@ namespace Penumbra.Import.Models; using Schema2 = SharpGLTF.Schema2; using LuminaMaterial = Lumina.Models.Materials.Material; -public sealed class ModelManager(IFramework framework, ActiveCollections collections, GamePathParser parser) +public sealed class ModelManager(IFramework framework, MetaFileManager metaFileManager, ActiveCollections collections, GamePathParser parser) : SingleTaskQueue, IDisposable, IService { private readonly IFramework _framework = framework; @@ -97,7 +99,7 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect // Try to use an entry from provided manipulations, falling back to the current collection. var targetId = modEst?.Value ?? collections.Current.MetaCache?.GetEstEntry(type, info.GenderRace, info.PrimaryId) - ?? EstEntry.Zero; + ?? EstFile.GetDefault(metaFileManager, type, info.GenderRace, info.PrimaryId); // If there's no entries, we can assume that there's no additional skeleton. if (targetId == EstEntry.Zero) diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs index 72cb1681..07f305ac 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs @@ -3,6 +3,7 @@ using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; +using Penumbra.Meta; using Penumbra.Meta.Files; using Penumbra.Meta.Manipulations; using Penumbra.String; @@ -51,10 +52,8 @@ internal partial record ResolveContext return GenderRace.MidlanderMale; var metaCache = Global.Collection.MetaCache; - if (metaCache == null) - return GenderRace.MidlanderMale; - - var entry = metaCache.GetEqdpEntry(characterRaceCode, accessory, primaryId); + var entry = metaCache?.GetEqdpEntry(characterRaceCode, accessory, primaryId) + ?? ExpandedEqdpFile.GetDefault(Global.MetaFileManager, characterRaceCode, accessory, primaryId); if (entry.ToBits(slot).Item2) return characterRaceCode; @@ -62,7 +61,8 @@ internal partial record ResolveContext if (fallbackRaceCode == GenderRace.MidlanderMale) return GenderRace.MidlanderMale; - entry = metaCache.GetEqdpEntry(fallbackRaceCode, accessory, primaryId); + entry = metaCache?.GetEqdpEntry(fallbackRaceCode, accessory, primaryId) + ?? ExpandedEqdpFile.GetDefault(Global.MetaFileManager, fallbackRaceCode, accessory, primaryId); if (entry.ToBits(slot).Item2) return fallbackRaceCode; @@ -271,8 +271,9 @@ internal partial record ResolveContext private (GenderRace RaceCode, string Slot, PrimaryId Set) ResolveHumanExtraSkeletonData(GenderRace raceCode, EstType type, PrimaryId primary) { - var metaCache = Global.Collection.MetaCache; - var skeletonSet = metaCache?.GetEstEntry(type, raceCode, primary) ?? default; + var metaCache = Global.Collection.MetaCache; + var skeletonSet = metaCache?.GetEstEntry(type, raceCode, primary) + ?? EstFile.GetDefault(Global.MetaFileManager, type, raceCode, primary); return (raceCode, type.ToName(), skeletonSet.AsId); } diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.cs b/Penumbra/Interop/ResourceTree/ResolveContext.cs index a852a4cc..acb320d4 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.cs @@ -11,6 +11,7 @@ using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; using Penumbra.Interop.Hooks.PostProcessing; using Penumbra.Interop.PathResolving; +using Penumbra.Meta; using Penumbra.String; using Penumbra.String.Classes; using Penumbra.UI; @@ -19,7 +20,12 @@ using ModelType = FFXIVClientStructs.FFXIV.Client.Graphics.Scene.CharacterBase.M namespace Penumbra.Interop.ResourceTree; -internal record GlobalResolveContext(ObjectIdentification Identifier, ModCollection Collection, TreeBuildCache TreeBuildCache, bool WithUiData) +internal record GlobalResolveContext( + MetaFileManager MetaFileManager, + ObjectIdentification Identifier, + ModCollection Collection, + TreeBuildCache TreeBuildCache, + bool WithUiData) { public readonly Dictionary<(Utf8GamePath, nint), ResourceNode> Nodes = new(128); @@ -111,7 +117,7 @@ internal unsafe partial record ResolveContext( if (resourceHandle == null) throw new ArgumentNullException(nameof(resourceHandle)); - var fileName = (ReadOnlySpan) resourceHandle->FileName.AsSpan(); + var fileName = (ReadOnlySpan)resourceHandle->FileName.AsSpan(); var additionalData = ByteString.Empty; if (PathDataHandler.Split(fileName, out fileName, out var data)) additionalData = ByteString.FromSpanUnsafe(data, false).Clone(); diff --git a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs index 1f6d1f6f..46c7ce35 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs @@ -8,6 +8,7 @@ using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.GameData.Interop; using Penumbra.Interop.PathResolving; +using Penumbra.Meta; using Penumbra.String.Classes; namespace Penumbra.Interop.ResourceTree; @@ -15,6 +16,7 @@ namespace Penumbra.Interop.ResourceTree; public class ResourceTreeFactory( IDataManager gameData, ObjectManager objects, + MetaFileManager metaFileManager, CollectionResolver resolver, ObjectIdentification identifier, Configuration config, @@ -78,7 +80,7 @@ public class ResourceTreeFactory( var networked = character.EntityId != 0xE0000000; var tree = new ResourceTree(name, anonymizedName, character.ObjectIndex, (nint)gameObjStruct, (nint)drawObjStruct, localPlayerRelated, related, networked, collectionResolveData.ModCollection.Name, collectionResolveData.ModCollection.AnonymizedName); - var globalContext = new GlobalResolveContext(identifier, collectionResolveData.ModCollection, + var globalContext = new GlobalResolveContext(metaFileManager, identifier, collectionResolveData.ModCollection, cache, (flags & Flags.WithUiData) != 0); using (var _ = pathState.EnterInternalResolve()) { From f533ae66671c49682451982656f268d13192d7a0 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 19 Jul 2024 17:33:54 +0200 Subject: [PATCH 321/433] Some cleanup. --- .../ResolveContext.PathResolution.cs | 2 -- .../ResourceTree/ResourceTreeFactory.cs | 18 ++++++++++-------- .../Interop/Structs/CharacterUtilityData.cs | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs index 07f305ac..678dd8a9 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs @@ -199,8 +199,6 @@ internal partial record ResolveContext ByteString? path; try { - Penumbra.Log.Information($"{(nint)CharacterBase:X} {ModelType} {SlotIndex} 0x{(ulong)mtrlFileName:X}"); - Penumbra.Log.Information($"{new ByteString(mtrlFileName)}"); path = CharacterBase->ResolveMtrlPathAsByteString(SlotIndex, mtrlFileName); } catch (AccessViolationException) diff --git a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs index 46c7ce35..65fac68f 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs @@ -18,7 +18,7 @@ public class ResourceTreeFactory( ObjectManager objects, MetaFileManager metaFileManager, CollectionResolver resolver, - ObjectIdentification identifier, + ObjectIdentification objectIdentifier, Configuration config, ActorManager actors, PathState pathState) : IService @@ -80,7 +80,7 @@ public class ResourceTreeFactory( var networked = character.EntityId != 0xE0000000; var tree = new ResourceTree(name, anonymizedName, character.ObjectIndex, (nint)gameObjStruct, (nint)drawObjStruct, localPlayerRelated, related, networked, collectionResolveData.ModCollection.Name, collectionResolveData.ModCollection.AnonymizedName); - var globalContext = new GlobalResolveContext(metaFileManager, identifier, collectionResolveData.ModCollection, + var globalContext = new GlobalResolveContext(metaFileManager, objectIdentifier, collectionResolveData.ModCollection, cache, (flags & Flags.WithUiData) != 0); using (var _ = pathState.EnterInternalResolve()) { @@ -125,6 +125,14 @@ public class ResourceTreeFactory( private static void FilterFullPaths(ResourceTree tree, string? onlyWithinPath) { + foreach (var node in tree.FlatNodes) + { + if (!ShallKeepPath(node.FullPath, onlyWithinPath)) + node.FullPath = FullPath.Empty; + } + + return; + static bool ShallKeepPath(FullPath fullPath, string? onlyWithinPath) { if (!fullPath.IsRooted) @@ -139,12 +147,6 @@ public class ResourceTreeFactory( return fullPath.Exists; } - - foreach (var node in tree.FlatNodes) - { - if (!ShallKeepPath(node.FullPath, onlyWithinPath)) - node.FullPath = FullPath.Empty; - } } private static void Cleanup(ResourceTree tree) diff --git a/Penumbra/Interop/Structs/CharacterUtilityData.cs b/Penumbra/Interop/Structs/CharacterUtilityData.cs index d33da477..197de0bb 100644 --- a/Penumbra/Interop/Structs/CharacterUtilityData.cs +++ b/Penumbra/Interop/Structs/CharacterUtilityData.cs @@ -15,7 +15,7 @@ public unsafe struct CharacterUtilityData .Where(n => n.First.StartsWith("Eqdp")) .Select(n => n.Second).ToArray(); - public const int TotalNumResources = 89; + public const int TotalNumResources = 114; /// Obtain the index for the eqdp file corresponding to the given race code and accessory. public static MetaIndex EqdpIdx(GenderRace raceCode, bool accessory) From a4548bbf0426fb181e49396147677ebecfe87147 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 21 Jul 2024 00:11:13 +0200 Subject: [PATCH 322/433] Apply unprioritized mod groups in reverse order. --- Penumbra/Mods/Mod.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Mods/Mod.cs b/Penumbra/Mods/Mod.cs index fcea7133..16f06de2 100644 --- a/Penumbra/Mods/Mod.cs +++ b/Penumbra/Mods/Mod.cs @@ -73,7 +73,7 @@ public sealed class Mod : IMod var dictRedirections = new Dictionary(TotalFileCount); var setManips = new MetaDictionary(); - foreach (var (group, groupIndex) in Groups.WithIndex().OrderByDescending(g => g.Value.Priority)) + foreach (var (group, groupIndex) in Groups.WithIndex().Reverse().OrderByDescending(g => g.Value.Priority)) { var config = settings.Settings[groupIndex]; group.AddData(config, dictRedirections, setManips); From 258f7e9732229f754f086fa79126e4246a7eb0b1 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 21 Jul 2024 00:13:43 +0200 Subject: [PATCH 323/433] Reinstate the inlined ApricotSoundPlay hook one layer hup. --- Penumbra.GameData | 2 +- .../Animation/ApricotListenerSoundPlay.cs | 42 ++++++++++++------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index a1e637f8..9f1816f1 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit a1e637f835c1a42732825e8e0690aeef0024b101 +Subproject commit 9f1816f1b75003d01c5576769831c10f3d8948a7 diff --git a/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs b/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs index 2e05c1b6..361fcd4e 100644 --- a/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs +++ b/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs @@ -1,4 +1,3 @@ -using Dalamud.Hooking; using FFXIVClientStructs.FFXIV.Client.Game.Object; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; @@ -11,33 +10,48 @@ using Penumbra.Services; namespace Penumbra.Interop.Hooks.Animation; /// Called for some sound effects caused by animations or VFX. -public sealed unsafe class ApricotListenerSoundPlay : FastHook +/// Actual function got inlined. +public sealed unsafe class ApricotListenerSoundPlayCaller : FastHook { private readonly GameState _state; private readonly CollectionResolver _collectionResolver; private readonly CrashHandlerService _crashHandler; - // TODO because of inlining. - public ApricotListenerSoundPlay(HookManager hooks, GameState state, CollectionResolver collectionResolver, CrashHandlerService crashHandler) + public ApricotListenerSoundPlayCaller(HookManager hooks, GameState state, CollectionResolver collectionResolver, + CrashHandlerService crashHandler) { _state = state; _collectionResolver = collectionResolver; _crashHandler = crashHandler; - Task = hooks.CreateHook("Apricot Listener Sound Play", Sigs.ApricotListenerSoundPlay, Detour, HookSettings.VfxIdentificationHooks); + Task = hooks.CreateHook("Apricot Listener Sound Play Caller", Sigs.ApricotListenerSoundPlayCaller, Detour, + true); //HookSettings.VfxIdentificationHooks); } - public delegate nint Delegate(nint a1, nint a2, nint a3, nint a4, nint a5, nint a6); + public delegate nint Delegate(nint a1, nint a2, float a3); [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private nint Detour(nint a1, nint a2, nint a3, nint a4, nint a5, nint a6) + private nint Detour(nint a1, nint unused, float timeOffset) { - Penumbra.Log.Excessive($"[Apricot Listener Sound Play] Invoked on 0x{a1:X} with {a2}, {a3}, {a4}, {a5}, {a6}."); - if (a6 == nint.Zero) - return Task.Result.Original(a1, a2, a3, a4, a5, a6); + // Short-circuiting and sanity checks done by game. + var playTime = a1 == nint.Zero ? -1 : *(float*)(a1 + 0x250); + if (playTime < 0) + return Task.Result.Original(a1, unused, timeOffset); - // a6 is some instance of Apricot.IInstanceListenner, in some cases we can obtain the associated caster via vfunc 1. - var gameObject = (*(delegate* unmanaged**)a6)[1](a6); + var someIntermediate = *(nint*)(a1 + 0x1F8); + var flags = someIntermediate == nint.Zero ? (ushort)0 : *(ushort*)(someIntermediate + 0x49C); + if (((flags >> 13) & 1) == 0) + return Task.Result.Original(a1, unused, timeOffset); + + Penumbra.Log.Information( + $"[Apricot Listener Sound Play Caller] Invoked on 0x{a1:X} with {unused}, {timeOffset}."); + // Fetch the IInstanceListenner (sixth argument to inlined call of SoundPlay) + var apricotIInstanceListenner = *(nint*)(someIntermediate + 0x270); + if (apricotIInstanceListenner == nint.Zero) + return Task.Result.Original(a1, unused, timeOffset); + + // In some cases we can obtain the associated caster via vfunc 1. var newData = ResolveData.Invalid; + var gameObject = (*(delegate* unmanaged**)apricotIInstanceListenner)[1](apricotIInstanceListenner); if (gameObject != null) { newData = _collectionResolver.IdentifyCollection(gameObject, true); @@ -47,14 +61,14 @@ public sealed unsafe class ApricotListenerSoundPlay : FastHook Date: Sun, 21 Jul 2024 00:21:27 +0200 Subject: [PATCH 324/433] Fix field. --- .../UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs index 15bd7cc9..25c0e448 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs @@ -75,7 +75,7 @@ public partial class ModEditWindow ImGui.TableHeader("Dye Preview"); } - for (var i = 0; i < ColorTable.NumUsedRows; ++i) + for (var i = 0; i < ColorTable.NumRows; ++i) { ret |= DrawColorTableRow(tab, i, disabled); ImGui.TableNextRow(); From 5b1c0cf0e3206fff550060f97375aeb8efae7aee Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 21 Jul 2024 00:21:41 +0200 Subject: [PATCH 325/433] Fix direction of furniture redrawing. --- Penumbra/Interop/Services/RedrawService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Penumbra/Interop/Services/RedrawService.cs b/Penumbra/Interop/Services/RedrawService.cs index f288a35e..2cdc1137 100644 --- a/Penumbra/Interop/Services/RedrawService.cs +++ b/Penumbra/Interop/Services/RedrawService.cs @@ -424,8 +424,8 @@ public sealed unsafe partial class RedrawService : IDisposable if (housingManager == null) return; - var currentTerritory = (OutdoorTerritory*)housingManager->CurrentTerritory; - if (currentTerritory == null || currentTerritory->GetTerritoryType() is not HousingTerritoryType.Outdoor) + var currentTerritory = (IndoorTerritory*)housingManager->CurrentTerritory; + if (currentTerritory == null || currentTerritory->GetTerritoryType() is not HousingTerritoryType.Indoor) return; From c3b7ddad2810e4687aec33b4da7f721de3b4625f Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 21 Jul 2024 00:48:48 +0200 Subject: [PATCH 326/433] Create newly added mods in import folder instead of moving them. --- OtterGui | 2 +- Penumbra/Mods/Manager/ModFileSystem.cs | 21 +++++++++++-- Penumbra/UI/ModsTab/ModFileSystemSelector.cs | 31 -------------------- 3 files changed, 20 insertions(+), 34 deletions(-) diff --git a/OtterGui b/OtterGui index 89b3b951..dc17161b 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 89b3b9513f9b4989045517a452ef971e24377203 +Subproject commit dc17161b1d9c47ffd6bcc17e91f4832cf7762993 diff --git a/Penumbra/Mods/Manager/ModFileSystem.cs b/Penumbra/Mods/Manager/ModFileSystem.cs index e32fec0c..693db944 100644 --- a/Penumbra/Mods/Manager/ModFileSystem.cs +++ b/Penumbra/Mods/Manager/ModFileSystem.cs @@ -1,3 +1,5 @@ +using Dalamud.Interface.ImGuiNotification; +using OtterGui.Classes; using OtterGui.Filesystem; using OtterGui.Services; using Penumbra.Communication; @@ -10,13 +12,15 @@ public sealed class ModFileSystem : FileSystem, IDisposable, ISavable, ISer private readonly ModManager _modManager; private readonly CommunicatorService _communicator; private readonly SaveService _saveService; + private readonly Configuration _config; // Create a new ModFileSystem from the currently loaded mods and the current sort order file. - public ModFileSystem(ModManager modManager, CommunicatorService communicator, SaveService saveService) + public ModFileSystem(ModManager modManager, CommunicatorService communicator, SaveService saveService, Configuration config) { _modManager = modManager; _communicator = communicator; _saveService = saveService; + _config = config; Reload(); Changed += OnChange; _communicator.ModDiscoveryFinished.Subscribe(Reload, ModDiscoveryFinished.Priority.ModFileSystem); @@ -91,7 +95,20 @@ public sealed class ModFileSystem : FileSystem, IDisposable, ISavable, ISer switch (type) { case ModPathChangeType.Added: - CreateDuplicateLeaf(Root, mod.Name.Text, mod); + var parent = Root; + if (_config.DefaultImportFolder.Length != 0) + try + { + parent = FindOrCreateAllFolders(_config.DefaultImportFolder); + } + catch (Exception e) + { + Penumbra.Messager.NotificationMessage(e, + $"Could not move newly imported mod {mod.Name} to default import folder {_config.DefaultImportFolder}.", + NotificationType.Warning); + } + + CreateDuplicateLeaf(parent, mod.Name.Text, mod); break; case ModPathChangeType.Deleted: if (FindLeaf(mod, out var leaf)) diff --git a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs index 88d6afa2..55405313 100644 --- a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs +++ b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs @@ -196,10 +196,7 @@ public sealed class ModFileSystemSelector : FileSystemSelector.Leaf leaf, in ModState state, bool selected) @@ -379,34 +376,6 @@ public sealed class ModFileSystemSelector : FileSystemSelector - /// If a default import folder is setup, try to move the given mod in there. - /// If the folder does not exist, create it if possible. - /// - /// - private void MoveModToDefaultDirectory(Mod mod) - { - if (_config.DefaultImportFolder.Length == 0) - return; - - try - { - var leaf = FileSystem.Root.GetChildren(ISortMode.Lexicographical) - .FirstOrDefault(f => f is FileSystem.Leaf l && l.Value == mod); - if (leaf == null) - throw new Exception("Mod was not found at root."); - - var folder = FileSystem.FindOrCreateAllFolders(_config.DefaultImportFolder); - FileSystem.Move(leaf, folder); - } - catch (Exception e) - { - _messager.NotificationMessage(e, - $"Could not move newly imported mod {mod.Name} to default import folder {_config.DefaultImportFolder}.", - NotificationType.Warning); - } - } - private void DrawHelpPopup() { ImGuiUtil.HelpPopup("ExtendedHelp", new Vector2(1000 * UiHelpers.Scale, 38.5f * ImGui.GetTextLineHeightWithSpacing()), () => From 48ab98bee69117f5c74c425ab19d9c65fd545041 Mon Sep 17 00:00:00 2001 From: Actions User Date: Sat, 20 Jul 2024 22:53:37 +0000 Subject: [PATCH 327/433] [CI] Updating repo.json for testing_1.2.0.12 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index d715bca2..ca13bb5b 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.11", + "TestingAssemblyVersion": "1.2.0.12", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.11/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.12/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 8c34c18643b34586346a902f6b0ffdcca6cc2907 Mon Sep 17 00:00:00 2001 From: pmgr <26606291+pmgr@users.noreply.github.com> Date: Sun, 21 Jul 2024 16:34:27 +0100 Subject: [PATCH 328/433] Add scuffed pap handling --- .../Hooks/ResourceLoading/MappedCodeReader.cs | 13 ++ .../Hooks/ResourceLoading/PapHandler.cs | 23 +++ .../Hooks/ResourceLoading/PapRewriter.cs | 181 ++++++++++++++++ .../Hooks/ResourceLoading/PeSigScanner.cs | 194 ++++++++++++++++++ .../Hooks/ResourceLoading/ResourceLoader.cs | 26 +++ Penumbra/Penumbra.csproj | 13 +- 6 files changed, 446 insertions(+), 4 deletions(-) create mode 100644 Penumbra/Interop/Hooks/ResourceLoading/MappedCodeReader.cs create mode 100644 Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs create mode 100644 Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs create mode 100644 Penumbra/Interop/Hooks/ResourceLoading/PeSigScanner.cs diff --git a/Penumbra/Interop/Hooks/ResourceLoading/MappedCodeReader.cs b/Penumbra/Interop/Hooks/ResourceLoading/MappedCodeReader.cs new file mode 100644 index 00000000..81712cca --- /dev/null +++ b/Penumbra/Interop/Hooks/ResourceLoading/MappedCodeReader.cs @@ -0,0 +1,13 @@ +using Iced.Intel; + +namespace Penumbra.Interop.Hooks.ResourceLoading; + +public class MappedCodeReader(UnmanagedMemoryAccessor data, long offset) : CodeReader +{ + public override int ReadByte() { + if (offset >= data.Capacity) + return -1; + + return data.ReadByte(offset++); + } +} diff --git a/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs b/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs new file mode 100644 index 00000000..29d77d83 --- /dev/null +++ b/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs @@ -0,0 +1,23 @@ +using Penumbra.GameData; + +namespace Penumbra.Interop.Hooks.ResourceLoading; + +public sealed class PapHandler(PapRewriter.PapResourceHandlerPrototype papResourceHandler) : IDisposable +{ + private readonly PapRewriter _papRewriter = new(papResourceHandler); + + public void Enable() + { + _papRewriter.Rewrite(Sigs.LoadAlwaysResidentMotionPacks); + _papRewriter.Rewrite(Sigs.LoadWeaponDependentResidentMotionPacks); + _papRewriter.Rewrite(Sigs.LoadInitialResidentMotionPacks); + _papRewriter.Rewrite(Sigs.LoadMotionPacks); + _papRewriter.Rewrite(Sigs.LoadMotionPacks2); + _papRewriter.Rewrite(Sigs.LoadMigratoryMotionPack); + } + + public void Dispose() + { + _papRewriter.Dispose(); + } +} diff --git a/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs b/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs new file mode 100644 index 00000000..cb437d9e --- /dev/null +++ b/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs @@ -0,0 +1,181 @@ +using Dalamud.Hooking; +using Iced.Intel; +using Penumbra.String.Classes; + +namespace Penumbra.Interop.Hooks.ResourceLoading; + +public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResourceHandler) : IDisposable +{ + public unsafe delegate int PapResourceHandlerPrototype(void* self, byte* path, int length); + + private PeSigScanner Scanner { get; } = new(); + private Dictionary Hooks { get; }= []; + private List NativeAllocList { get; } = []; + private PapResourceHandlerPrototype PapResourceHandler { get; } = papResourceHandler; + + public void Rewrite(string sig) + { + if (!Scanner.TryScanText(sig, out var addr)) + { + throw new Exception($"Sig is fucked: {sig}"); + } + + var funcInstructions = Scanner.GetFunctionInstructions(addr).ToList(); + + var hookPoints = ScanPapHookPoints(funcInstructions).ToList(); + + foreach (var hookPoint in hookPoints) + { + var stackAccesses = ScanStackAccesses(funcInstructions, hookPoint).ToList(); + + var stringLoc = NativeAlloc(Utf8GamePath.MaxGamePathLength); + + { + // We'll need to grab our true hook point; the location where we can change the path at our leisure. + // This is going to be the first call instruction after our 'hookPoint', so, we'll find that. + // Pretty scuffed, this might need a refactoring at some point. + // We're doing it by skipping to our hookPoint's address in the list of instructions inside the function; then getting next CALL + var detourPoint = funcInstructions.Skip( + funcInstructions.FindIndex(instr => instr.IP == hookPoint.IP) + 1 + ).First(instr => instr.Mnemonic == Mnemonic.Call); + + // We'll also remove all the 'hookPoints' from 'stackAccesses'. + // We're handling the char *path redirection here, so we don't want this to hit the later code + foreach (var hp in hookPoints) + { + stackAccesses.RemoveAll(instr => instr.IP == hp.IP); + } + + var pDetour = Marshal.GetFunctionPointerForDelegate(PapResourceHandler); + var targetRegister = hookPoint.Op0Register.ToString().ToLower(); + var hookAddr = new IntPtr((long)detourPoint.IP); + + var caveLoc = NativeAlloc(16); + var hook = new AsmHook( + hookAddr, + [ + "use64", + $"mov {targetRegister}, 0x{stringLoc:x8}", // Move our char *path into the relevant register (rdx) + + // After this asm stub, we have a call to Crc32(); since r9 is a volatile, unused register, we can use it ourselves + // We're essentially storing the original 2 arguments ('this', 'path'), in case they get mangled in our call + // We technically don't need to save rdx ('path'), since it'll be stringLoc, but eh + $"mov r9, 0x{caveLoc:x8}", + "mov [r9], rcx", + "mov [r9+0x8], rdx", + + // We can use 'rax' here too since it's also volatile, and it'll be overwritten by Crc32()'s return anyway + $"mov rax, 0x{pDetour:x8}", // Get a pointer to our detour in place + "call rax", // Call detour + + // Do the reverse process and retrieve the stored stuff + $"mov r9, 0x{caveLoc:x8}", + "mov rcx, [r9]", + "mov rdx, [r9+0x8]", + + // Plop 'rax' (our return value, the path size) into r8, so it's the third argument for the subsequent Crc32() call + "mov r8, rax", + ], "Pap Redirection" + ); + + Hooks.Add(hookAddr, hook); + hook.Enable(); + } + + // Now we're adjusting every single reference to the stack allocated 'path' to our substantially bigger 'stringLoc' + foreach (var stackAccess in stackAccesses) + { + var hookAddr = new IntPtr((long)stackAccess.IP + stackAccess.Length); + + if (Hooks.ContainsKey(hookAddr)) + { + // Hook already exists, means there's reuse of the same stack address across 2 GetResourceAsync; just skip + continue; + } + + var targetRegister = stackAccess.Op0Register.ToString().ToLower(); + var hook = new AsmHook( + hookAddr, + [ + "use64", + $"mov {targetRegister}, 0x{stringLoc:x8}", + ], "Pap Stack Accesses" + ); + + Hooks.Add(hookAddr, hook); + hook.Enable(); + } + } + + + + } + + private static IEnumerable ScanStackAccesses(IEnumerable instructions, Instruction hookPoint) + { + return instructions.Where(instr => + instr.Code == hookPoint.Code + && instr.Op0Kind == hookPoint.Op0Kind + && instr.Op1Kind == hookPoint.Op1Kind + && instr.MemoryBase == hookPoint.MemoryBase + && instr.MemoryDisplacement64 == hookPoint.MemoryDisplacement64) + .GroupBy(instr => instr.IP) + .Select(grp => grp.First()); + } + + // This is utterly fucked and hardcoded, but, again, it works + // Might be a neat idea for a more versatile kind of signature though + private static IEnumerable ScanPapHookPoints(List funcInstructions) + { + for (var i = 0; i < funcInstructions.Count - 8; i++) + { + if (funcInstructions[i .. (i + 8)] is + [ + {Code : Code.Lea_r64_m}, + {Code : Code.Lea_r64_m}, + {Mnemonic: Mnemonic.Call}, + {Code : Code.Lea_r64_m}, + {Mnemonic: Mnemonic.Call}, + {Code : Code.Lea_r64_m}, + .., + ] + ) + { + yield return funcInstructions[i]; + } + } + } + + private unsafe IntPtr NativeAlloc(nuint size) + { + var caveLoc = new IntPtr(NativeMemory.Alloc(size)); + NativeAllocList.Add(caveLoc); + + return caveLoc; + } + + private static unsafe void NativeFree(IntPtr mem) + { + NativeMemory.Free(mem.ToPointer()); + } + + public void Dispose() + { + Scanner.Dispose(); + + foreach (var hook in Hooks.Values) + { + hook.Disable(); + hook.Dispose(); + } + + Hooks.Clear(); + + foreach (var mem in NativeAllocList) + { + NativeFree(mem); + } + + NativeAllocList.Clear(); + } +} diff --git a/Penumbra/Interop/Hooks/ResourceLoading/PeSigScanner.cs b/Penumbra/Interop/Hooks/ResourceLoading/PeSigScanner.cs new file mode 100644 index 00000000..231e04f3 --- /dev/null +++ b/Penumbra/Interop/Hooks/ResourceLoading/PeSigScanner.cs @@ -0,0 +1,194 @@ +using System.IO.MemoryMappedFiles; +using Iced.Intel; +using PeNet; +using Decoder = Iced.Intel.Decoder; + +namespace Penumbra.Interop.Hooks.ResourceLoading; + +// A good chunk of this was blatantly stolen from Dalamud's SigScanner 'cause I could not be faffed, maybe I'll rewrite it later +public class PeSigScanner : IDisposable +{ + private MemoryMappedFile File { get; } + + private uint TextSectionStart { get; } + private uint TextSectionSize { get; } + + private IntPtr ModuleBaseAddress { get; } + private uint TextSectionVirtualAddress { get; } + + private MemoryMappedViewAccessor TextSection { get; } + + + public PeSigScanner() + { + var mainModule = Process.GetCurrentProcess().MainModule!; + var fileName = mainModule.FileName; + ModuleBaseAddress = mainModule.BaseAddress; + + if (fileName == null) + { + throw new Exception("Can't get main module path, the fuck is going on?"); + } + + File = MemoryMappedFile.CreateFromFile(fileName, FileMode.Open, null, 0, MemoryMappedFileAccess.Read); + + using var fileStream = File.CreateViewStream(0, 0, MemoryMappedFileAccess.Read); + var pe = new PeFile(fileStream); + + var textSection = pe.ImageSectionHeaders!.First(header => header.Name == ".text"); + + TextSectionStart = textSection.PointerToRawData; + TextSectionSize = textSection.SizeOfRawData; + TextSectionVirtualAddress = textSection.VirtualAddress; + + TextSection = File.CreateViewAccessor(TextSectionStart, TextSectionSize, MemoryMappedFileAccess.Read); + } + + + private IntPtr ScanText(string signature) + { + var scanRet = Scan(TextSection, signature); + + var instrByte = Marshal.ReadByte(scanRet); + + if (instrByte is 0xE8 or 0xE9) + scanRet = ReadJmpCallSig(scanRet); + + return scanRet; + } + + private static IntPtr ReadJmpCallSig(IntPtr sigLocation) + { + var jumpOffset = Marshal.ReadInt32(sigLocation, 1); + return IntPtr.Add(sigLocation, 5 + jumpOffset); + } + + public bool TryScanText(string signature, out IntPtr result) + { + try + { + result = ScanText(signature); + return true; + } + catch (KeyNotFoundException) + { + result = IntPtr.Zero; + return false; + } + } + + private IntPtr Scan(MemoryMappedViewAccessor section, string signature) + { + var (needle, mask) = ParseSignature(signature); + + var index = IndexOf(section, needle, mask); + if (index < 0) + throw new KeyNotFoundException($"Can't find a signature of {signature}"); + return new IntPtr(ModuleBaseAddress + index - section.PointerOffset + TextSectionVirtualAddress); + } + + private static (byte[] Needle, bool[] Mask) ParseSignature(string signature) + { + signature = signature.Replace(" ", string.Empty); + if (signature.Length % 2 != 0) + throw new ArgumentException("Signature without whitespaces must be divisible by two.", nameof(signature)); + + var needleLength = signature.Length / 2; + var needle = new byte[needleLength]; + var mask = new bool[needleLength]; + for (var i = 0; i < needleLength; i++) + { + var hexString = signature.Substring(i * 2, 2); + if (hexString == "??" || hexString == "**") + { + needle[i] = 0; + mask[i] = true; + continue; + } + + needle[i] = byte.Parse(hexString, NumberStyles.AllowHexSpecifier); + mask[i] = false; + } + + return (needle, mask); + } + + private static unsafe int IndexOf(MemoryMappedViewAccessor section, byte[] needle, bool[] mask) + { + if (needle.Length > section.Capacity) return -1; + var badShift = BuildBadCharTable(needle, mask); + var last = needle.Length - 1; + var offset = 0; + var maxOffset = section.Capacity - needle.Length; + + byte* buffer = null; + section.SafeMemoryMappedViewHandle.AcquirePointer(ref buffer); + try + { + while (offset <= maxOffset) + { + int position; + for (position = last; needle[position] == *(buffer + position + offset) || mask[position]; position--) + { + if (position == 0) + return offset; + } + + offset += badShift[*(buffer + offset + last)]; + } + } + finally + { + section.SafeMemoryMappedViewHandle.ReleasePointer(); + } + + return -1; + } + + + private static int[] BuildBadCharTable(byte[] needle, bool[] mask) + { + int idx; + var last = needle.Length - 1; + var badShift = new int[256]; + for (idx = last; idx > 0 && !mask[idx]; --idx) + { + } + + var diff = last - idx; + if (diff == 0) diff = 1; + + for (idx = 0; idx <= 255; ++idx) + badShift[idx] = diff; + for (idx = last - diff; idx < last; ++idx) + badShift[needle[idx]] = last - idx; + return badShift; + } + + // Detects function termination; this is done in a really stupid way that will possibly break if looked at wrong, but it'll work for now + // If this shits itself, go bother Winter to implement proper CFG + basic block detection + public IEnumerable GetFunctionInstructions(IntPtr addr) + { + var fileOffset = addr - TextSectionVirtualAddress - ModuleBaseAddress; + + var codeReader = new MappedCodeReader(TextSection, fileOffset); + var decoder = Decoder.Create(64, codeReader, (ulong)addr.ToInt64()); + + do + { + decoder.Decode(out var instr); + + // Yes, this is catastrophically bad, but it works for some cases okay + if (instr.Mnemonic == Mnemonic.Int3) + break; + + yield return instr; + } while (true); + } + + public void Dispose() + { + TextSection.Dispose(); + File.Dispose(); + } +} diff --git a/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs index 195a8b9e..bc28c200 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs @@ -16,6 +16,8 @@ public unsafe class ResourceLoader : IDisposable, IService private readonly ResourceService _resources; private readonly FileReadService _fileReadService; private readonly TexMdlService _texMdlService; + + private readonly PapHandler _papHandler; private ResolveData _resolvedData = ResolveData.Invalid; @@ -30,6 +32,29 @@ public unsafe class ResourceLoader : IDisposable, IService _resources.ResourceHandleIncRef += IncRefProtection; _resources.ResourceHandleDecRef += DecRefProtection; _fileReadService.ReadSqPack += ReadSqPackDetour; + + _papHandler = new PapHandler(PapResourceHandler); + _papHandler.Enable(); + } + + private int PapResourceHandler(void* self, byte* path, int length) + { + Utf8GamePath.FromPointer(path, out var gamePath); + + var (resolvedPath, _) = _incMode.Value + ? (null, ResolveData.Invalid) + : _resolvedData.Valid + ? (_resolvedData.ModCollection.ResolvePath(gamePath), _resolvedData) + : ResolvePath(gamePath, ResourceCategory.Chara, ResourceType.Pap); + + if (!resolvedPath.HasValue || !Utf8GamePath.FromString(resolvedPath.Value.FullName, out var utf8ResolvedPath)) + { + return length; + } + + NativeMemory.Copy(utf8ResolvedPath.Path.Path, path, (nuint)utf8ResolvedPath.Length); + path[utf8ResolvedPath.Length] = 0; + return utf8ResolvedPath.Length; } /// Load a resource for a given path and a specific collection. @@ -84,6 +109,7 @@ public unsafe class ResourceLoader : IDisposable, IService _resources.ResourceHandleIncRef -= IncRefProtection; _resources.ResourceHandleDecRef -= DecRefProtection; _fileReadService.ReadSqPack -= ReadSqPackDetour; + _papHandler.Dispose(); } private void ResourceHandler(ref ResourceCategory category, ref ResourceType type, ref int hash, ref Utf8GamePath path, diff --git a/Penumbra/Penumbra.csproj b/Penumbra/Penumbra.csproj index 2e53bd22..70208737 100644 --- a/Penumbra/Penumbra.csproj +++ b/Penumbra/Penumbra.csproj @@ -23,10 +23,10 @@ PROFILING; - - - - + + + + @@ -68,6 +68,10 @@ $(DalamudLibPath)Newtonsoft.Json.dll False + + $(DalamudLibPath)Iced.dll + False + lib\OtterTex.dll @@ -79,6 +83,7 @@ + From 8351b74b21c5dfaca5691bc7e2956715573e7fa5 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 21 Jul 2024 22:23:31 +0200 Subject: [PATCH 329/433] Update GameData and packages. --- Penumbra.GameData | 2 +- Penumbra/packages.lock.json | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 9f1816f1..f13818fd 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 9f1816f1b75003d01c5576769831c10f3d8948a7 +Subproject commit f13818fd85b436d0a0f66293fe7c6b60d4bffe3c diff --git a/Penumbra/packages.lock.json b/Penumbra/packages.lock.json index b431e595..42539e78 100644 --- a/Penumbra/packages.lock.json +++ b/Penumbra/packages.lock.json @@ -11,6 +11,16 @@ "Unosquare.Swan.Lite": "3.0.0" } }, + "PeNet": { + "type": "Direct", + "requested": "[4.0.5, )", + "resolved": "4.0.5", + "contentHash": "/OUfRnMG8STVuK8kTpdfm+WQGTDoUiJnI845kFw4QrDmv2gYourmSnH84pqVjHT1YHBSuRfCzfioIpHGjFJrGA==", + "dependencies": { + "PeNet.Asn1": "2.0.1", + "System.Security.Cryptography.Pkcs": "8.0.0" + } + }, "SharpCompress": { "type": "Direct", "requested": "[0.33.0, )", @@ -56,6 +66,11 @@ "resolved": "8.0.0", "contentHash": "cjWrLkJXK0rs4zofsK4bSdg+jhDLTaxrkXu4gS6Y7MAlCvRyNNgwY/lJi5RDlQOnSZweHqoyvgvbdvQsRIW+hg==" }, + "PeNet.Asn1": { + "type": "Transitive", + "resolved": "2.0.1", + "contentHash": "YR2O2YokSAYB+7CXkCDN3bd6/p0K3/AicCPkOJHKUz500v1D/hulCuVlggguqNc3M0LgSfOZKGvVYg2ud1GA9A==" + }, "SharpGLTF.Runtime": { "type": "Transitive", "resolved": "1.0.0-alpha0030", @@ -64,6 +79,19 @@ "SharpGLTF.Core": "1.0.0-alpha0030" } }, + "System.Formats.Asn1": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "AJukBuLoe3QeAF+mfaRKQb2dgyrvt340iMBHYv+VdBzCUM06IxGlvl0o/uPOS7lHnXPN6u8fFRHSHudx5aTi8w==" + }, + "System.Security.Cryptography.Pkcs": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "ULmp3xoOwNYjOYp4JZ2NK/6NdTgiN1GQXzVVN1njQ7LOZ0d0B9vyMnhyqbIi9Qw4JXj1JgCsitkTShboHRx7Eg==", + "dependencies": { + "System.Formats.Asn1": "8.0.0" + } + }, "System.ValueTuple": { "type": "Transitive", "resolved": "4.5.0", @@ -94,7 +122,7 @@ "type": "Project", "dependencies": { "OtterGui": "[1.0.0, )", - "Penumbra.Api": "[5.0.0, )", + "Penumbra.Api": "[5.2.0, )", "Penumbra.String": "[1.0.4, )" } }, From 0db70c89b105ec8b76a0c3a6764ab4c566e016f7 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 21 Jul 2024 22:58:03 +0200 Subject: [PATCH 330/433] Some cleanup of PeSigScanner. --- .../Hooks/ResourceLoading/PapHandler.cs | 4 +- .../Hooks/ResourceLoading/PeSigScanner.cs | 118 +++++++++--------- 2 files changed, 57 insertions(+), 65 deletions(-) diff --git a/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs b/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs index 29d77d83..f0fd8b0e 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs @@ -17,7 +17,5 @@ public sealed class PapHandler(PapRewriter.PapResourceHandlerPrototype papResour } public void Dispose() - { - _papRewriter.Dispose(); - } + => _papRewriter.Dispose(); } diff --git a/Penumbra/Interop/Hooks/ResourceLoading/PeSigScanner.cs b/Penumbra/Interop/Hooks/ResourceLoading/PeSigScanner.cs index 231e04f3..f5dd2d45 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/PeSigScanner.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/PeSigScanner.cs @@ -5,65 +5,56 @@ using Decoder = Iced.Intel.Decoder; namespace Penumbra.Interop.Hooks.ResourceLoading; -// A good chunk of this was blatantly stolen from Dalamud's SigScanner 'cause I could not be faffed, maybe I'll rewrite it later -public class PeSigScanner : IDisposable +// A good chunk of this was blatantly stolen from Dalamud's SigScanner 'cause Winter could not be faffed, Winter will definitely not rewrite it later +public unsafe class PeSigScanner : IDisposable { - private MemoryMappedFile File { get; } + private readonly MemoryMappedFile _file; + private readonly MemoryMappedViewAccessor _textSection; + + private readonly nint _moduleBaseAddress; + private readonly uint _textSectionVirtualAddress; + - private uint TextSectionStart { get; } - private uint TextSectionSize { get; } - - private IntPtr ModuleBaseAddress { get; } - private uint TextSectionVirtualAddress { get; } - - private MemoryMappedViewAccessor TextSection { get; } - - public PeSigScanner() { var mainModule = Process.GetCurrentProcess().MainModule!; - var fileName = mainModule.FileName; - ModuleBaseAddress = mainModule.BaseAddress; + var fileName = mainModule.FileName; + _moduleBaseAddress = mainModule.BaseAddress; if (fileName == null) - { - throw new Exception("Can't get main module path, the fuck is going on?"); - } - - File = MemoryMappedFile.CreateFromFile(fileName, FileMode.Open, null, 0, MemoryMappedFileAccess.Read); + throw new Exception("Unable to obtain main module path. This should not happen."); - using var fileStream = File.CreateViewStream(0, 0, MemoryMappedFileAccess.Read); + _file = MemoryMappedFile.CreateFromFile(fileName, FileMode.Open, null, 0, MemoryMappedFileAccess.Read); + + using var fileStream = _file.CreateViewStream(0, 0, MemoryMappedFileAccess.Read); var pe = new PeFile(fileStream); var textSection = pe.ImageSectionHeaders!.First(header => header.Name == ".text"); - TextSectionStart = textSection.PointerToRawData; - TextSectionSize = textSection.SizeOfRawData; - TextSectionVirtualAddress = textSection.VirtualAddress; + var textSectionStart = textSection.PointerToRawData; + var textSectionSize = textSection.SizeOfRawData; + _textSectionVirtualAddress = textSection.VirtualAddress; - TextSection = File.CreateViewAccessor(TextSectionStart, TextSectionSize, MemoryMappedFileAccess.Read); + _textSection = _file.CreateViewAccessor(textSectionStart, textSectionSize, MemoryMappedFileAccess.Read); } - private IntPtr ScanText(string signature) + private nint ScanText(string signature) { - var scanRet = Scan(TextSection, signature); - - var instrByte = Marshal.ReadByte(scanRet); - - if (instrByte is 0xE8 or 0xE9) + var scanRet = Scan(_textSection, signature); + if (*(byte*)scanRet is 0xE8 or 0xE9) scanRet = ReadJmpCallSig(scanRet); return scanRet; } - - private static IntPtr ReadJmpCallSig(IntPtr sigLocation) + + private static nint ReadJmpCallSig(nint sigLocation) { - var jumpOffset = Marshal.ReadInt32(sigLocation, 1); - return IntPtr.Add(sigLocation, 5 + jumpOffset); + var jumpOffset = *(int*)(sigLocation + 1); + return sigLocation + 5 + jumpOffset; } - - public bool TryScanText(string signature, out IntPtr result) + + public bool TryScanText(string signature, out nint result) { try { @@ -72,21 +63,22 @@ public class PeSigScanner : IDisposable } catch (KeyNotFoundException) { - result = IntPtr.Zero; + result = nint.Zero; return false; } } - - private IntPtr Scan(MemoryMappedViewAccessor section, string signature) + + private nint Scan(MemoryMappedViewAccessor section, string signature) { var (needle, mask) = ParseSignature(signature); - - var index = IndexOf(section, needle, mask); + + var index = IndexOf(section, needle, mask); if (index < 0) throw new KeyNotFoundException($"Can't find a signature of {signature}"); - return new IntPtr(ModuleBaseAddress + index - section.PointerOffset + TextSectionVirtualAddress); + + return (nint)(_moduleBaseAddress + index - section.PointerOffset + _textSectionVirtualAddress); } - + private static (byte[] Needle, bool[] Mask) ParseSignature(string signature) { signature = signature.Replace(" ", string.Empty); @@ -99,7 +91,7 @@ public class PeSigScanner : IDisposable for (var i = 0; i < needleLength; i++) { var hexString = signature.Substring(i * 2, 2); - if (hexString == "??" || hexString == "**") + if (hexString is "??" or "**") { needle[i] = 0; mask[i] = true; @@ -112,10 +104,12 @@ public class PeSigScanner : IDisposable return (needle, mask); } - - private static unsafe int IndexOf(MemoryMappedViewAccessor section, byte[] needle, bool[] mask) + + private static int IndexOf(MemoryMappedViewAccessor section, byte[] needle, bool[] mask) { - if (needle.Length > section.Capacity) return -1; + if (needle.Length > section.Capacity) + return -1; + var badShift = BuildBadCharTable(needle, mask); var last = needle.Length - 1; var offset = 0; @@ -144,19 +138,19 @@ public class PeSigScanner : IDisposable return -1; } - - + + private static int[] BuildBadCharTable(byte[] needle, bool[] mask) { int idx; var last = needle.Length - 1; var badShift = new int[256]; for (idx = last; idx > 0 && !mask[idx]; --idx) - { - } + { } - var diff = last - idx; - if (diff == 0) diff = 1; + var diff = last - idx; + if (diff == 0) + diff = 1; for (idx = 0; idx <= 255; ++idx) badShift[idx] = diff; @@ -164,16 +158,16 @@ public class PeSigScanner : IDisposable badShift[needle[idx]] = last - idx; return badShift; } - + // Detects function termination; this is done in a really stupid way that will possibly break if looked at wrong, but it'll work for now // If this shits itself, go bother Winter to implement proper CFG + basic block detection - public IEnumerable GetFunctionInstructions(IntPtr addr) + public IEnumerable GetFunctionInstructions(nint address) { - var fileOffset = addr - TextSectionVirtualAddress - ModuleBaseAddress; - - var codeReader = new MappedCodeReader(TextSection, fileOffset); - var decoder = Decoder.Create(64, codeReader, (ulong)addr.ToInt64()); - + var fileOffset = address - _textSectionVirtualAddress - _moduleBaseAddress; + + var codeReader = new MappedCodeReader(_textSection, fileOffset); + var decoder = Decoder.Create(64, codeReader, (ulong)address.ToInt64()); + do { decoder.Decode(out var instr); @@ -188,7 +182,7 @@ public class PeSigScanner : IDisposable public void Dispose() { - TextSection.Dispose(); - File.Dispose(); + _textSection.Dispose(); + _file.Dispose(); } } From ee5a21f7a20dc459b1ec0d903c008f616fbcde3c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 21 Jul 2024 22:58:24 +0200 Subject: [PATCH 331/433] Add pap requested event, some cleanup. --- .../Hooks/ResourceLoading/ResourceLoader.cs | 22 +++++---- .../UI/ResourceWatcher/ResourceWatcher.cs | 47 ++++++++++++------- 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs index bc28c200..cf87aa2b 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs @@ -16,10 +16,10 @@ public unsafe class ResourceLoader : IDisposable, IService private readonly ResourceService _resources; private readonly FileReadService _fileReadService; private readonly TexMdlService _texMdlService; - - private readonly PapHandler _papHandler; + private readonly PapHandler _papHandler; - private ResolveData _resolvedData = ResolveData.Invalid; + private ResolveData _resolvedData = ResolveData.Invalid; + public event Action? PapRequested; public ResourceLoader(ResourceService resources, FileReadService fileReadService, TexMdlService texMdlService) { @@ -36,24 +36,28 @@ public unsafe class ResourceLoader : IDisposable, IService _papHandler = new PapHandler(PapResourceHandler); _papHandler.Enable(); } - + private int PapResourceHandler(void* self, byte* path, int length) { - Utf8GamePath.FromPointer(path, out var gamePath); - + if (!Utf8GamePath.FromPointer(path, out var gamePath)) + return length; + var (resolvedPath, _) = _incMode.Value ? (null, ResolveData.Invalid) : _resolvedData.Valid ? (_resolvedData.ModCollection.ResolvePath(gamePath), _resolvedData) : ResolvePath(gamePath, ResourceCategory.Chara, ResourceType.Pap); - - if (!resolvedPath.HasValue || !Utf8GamePath.FromString(resolvedPath.Value.FullName, out var utf8ResolvedPath)) + + + if (!resolvedPath.HasValue || !Utf8GamePath.FromByteString(resolvedPath.Value.InternalName, out var utf8ResolvedPath)) { + PapRequested?.Invoke(gamePath, gamePath, _resolvedData); return length; } - + NativeMemory.Copy(utf8ResolvedPath.Path.Path, path, (nuint)utf8ResolvedPath.Length); path[utf8ResolvedPath.Length] = 0; + PapRequested?.Invoke(gamePath, utf8ResolvedPath, _resolvedData); return utf8ResolvedPath.Length; } diff --git a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs index 935f11e3..a00b33c7 100644 --- a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs +++ b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs @@ -36,31 +36,47 @@ public sealed class ResourceWatcher : IDisposable, ITab, IUiService private Regex? _logRegex; private int _newMaxEntries; - public unsafe ResourceWatcher(ActorManager actors, Configuration config, ResourceService resources, ResourceLoader loader, ResourceHandleDestructor destructor) + public unsafe ResourceWatcher(ActorManager actors, Configuration config, ResourceService resources, ResourceLoader loader, + ResourceHandleDestructor destructor) { - _actors = actors; - _config = config; - _ephemeral = config.Ephemeral; - _resources = resources; - _destructor = destructor; - _loader = loader; - _table = new ResourceWatcherTable(config.Ephemeral, _records); - _resources.ResourceRequested += OnResourceRequested; + _actors = actors; + _config = config; + _ephemeral = config.Ephemeral; + _resources = resources; + _destructor = destructor; + _loader = loader; + _table = new ResourceWatcherTable(config.Ephemeral, _records); + _resources.ResourceRequested += OnResourceRequested; _destructor.Subscribe(OnResourceDestroyed, ResourceHandleDestructor.Priority.ResourceWatcher); - _loader.ResourceLoaded += OnResourceLoaded; - _loader.FileLoaded += OnFileLoaded; + _loader.ResourceLoaded += OnResourceLoaded; + _loader.FileLoaded += OnFileLoaded; + _loader.PapRequested += OnPapRequested; UpdateFilter(_ephemeral.ResourceLoggingFilter, false); _newMaxEntries = _config.MaxResourceWatcherRecords; } + private void OnPapRequested(Utf8GamePath original) + { + if (_ephemeral.EnableResourceLogging && FilterMatch(original.Path, out var match)) + Penumbra.Log.Information($"[ResourceLoader] [REQ] {match} was requested asynchronously."); + + if (!_ephemeral.EnableResourceWatcher) + return; + + var record = Record.CreateRequest(original.Path, false); + if (!_ephemeral.OnlyAddMatchingResources || _table.WouldBeVisible(record)) + _newRecords.Enqueue(record); + } + public unsafe void Dispose() { Clear(); _records.TrimExcess(); - _resources.ResourceRequested -= OnResourceRequested; + _resources.ResourceRequested -= OnResourceRequested; _destructor.Unsubscribe(OnResourceDestroyed); - _loader.ResourceLoaded -= OnResourceLoaded; - _loader.FileLoaded -= OnFileLoaded; + _loader.ResourceLoaded -= OnResourceLoaded; + _loader.FileLoaded -= OnFileLoaded; + _loader.PapRequested -= OnPapRequested; } private void Clear() @@ -200,8 +216,7 @@ public sealed class ResourceWatcher : IDisposable, ITab, IUiService private unsafe void OnResourceRequested(ref ResourceCategory category, ref ResourceType type, ref int hash, ref Utf8GamePath path, - Utf8GamePath original, - GetResourceParameters* parameters, ref bool sync, ref ResourceHandle* returnValue) + Utf8GamePath original, GetResourceParameters* parameters, ref bool sync, ref ResourceHandle* returnValue) { if (_ephemeral.EnableResourceLogging && FilterMatch(original.Path, out var match)) Penumbra.Log.Information($"[ResourceLoader] [REQ] {match} was requested {(sync ? "synchronously." : "asynchronously.")}"); From ceaa9ca29a05125fd08cd76e664145cac37c7343 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 21 Jul 2024 23:26:09 +0200 Subject: [PATCH 332/433] Some further cleanup. --- .../Hooks/ResourceLoading/PapHandler.cs | 25 +- .../Hooks/ResourceLoading/PapRewriter.cs | 230 ++++++++---------- .../Hooks/ResourceLoading/ResourceLoader.cs | 4 +- 3 files changed, 127 insertions(+), 132 deletions(-) diff --git a/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs b/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs index f0fd8b0e..65add13c 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs @@ -5,17 +5,26 @@ namespace Penumbra.Interop.Hooks.ResourceLoading; public sealed class PapHandler(PapRewriter.PapResourceHandlerPrototype papResourceHandler) : IDisposable { private readonly PapRewriter _papRewriter = new(papResourceHandler); - + public void Enable() { - _papRewriter.Rewrite(Sigs.LoadAlwaysResidentMotionPacks); - _papRewriter.Rewrite(Sigs.LoadWeaponDependentResidentMotionPacks); - _papRewriter.Rewrite(Sigs.LoadInitialResidentMotionPacks); - _papRewriter.Rewrite(Sigs.LoadMotionPacks); - _papRewriter.Rewrite(Sigs.LoadMotionPacks2); - _papRewriter.Rewrite(Sigs.LoadMigratoryMotionPack); + ReadOnlySpan signatures = + [ + Sigs.LoadAlwaysResidentMotionPacks, + Sigs.LoadWeaponDependentResidentMotionPacks, + Sigs.LoadInitialResidentMotionPacks, + Sigs.LoadMotionPacks, + Sigs.LoadMotionPacks2, + Sigs.LoadMigratoryMotionPack, + ]; + + var stopwatch = Stopwatch.StartNew(); + foreach (var sig in signatures) + _papRewriter.Rewrite(sig); + Penumbra.Log.Debug( + $"[PapHandler] Rewrote {signatures.Length} .pap functions for inlined GetResourceAsync in {stopwatch.ElapsedMilliseconds} ms."); } - + public void Dispose() => _papRewriter.Dispose(); } diff --git a/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs b/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs index cb437d9e..af16d706 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs @@ -1,5 +1,6 @@ using Dalamud.Hooking; using Iced.Intel; +using OtterGui; using Penumbra.String.Classes; namespace Penumbra.Interop.Hooks.ResourceLoading; @@ -7,175 +8,160 @@ namespace Penumbra.Interop.Hooks.ResourceLoading; public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResourceHandler) : IDisposable { public unsafe delegate int PapResourceHandlerPrototype(void* self, byte* path, int length); - - private PeSigScanner Scanner { get; } = new(); - private Dictionary Hooks { get; }= []; - private List NativeAllocList { get; } = []; - private PapResourceHandlerPrototype PapResourceHandler { get; } = papResourceHandler; + + private readonly PeSigScanner _scanner = new(); + private readonly Dictionary _hooks = []; + private readonly List _nativeAllocList = []; public void Rewrite(string sig) { - if (!Scanner.TryScanText(sig, out var addr)) - { - throw new Exception($"Sig is fucked: {sig}"); - } + if (!_scanner.TryScanText(sig, out var address)) + throw new Exception($"Signature [{sig}] could not be found."); - var funcInstructions = Scanner.GetFunctionInstructions(addr).ToList(); - - var hookPoints = ScanPapHookPoints(funcInstructions).ToList(); + var funcInstructions = _scanner.GetFunctionInstructions(address).ToArray(); + var hookPoints = ScanPapHookPoints(funcInstructions).ToList(); foreach (var hookPoint in hookPoints) { - var stackAccesses = ScanStackAccesses(funcInstructions, hookPoint).ToList(); + var stackAccesses = ScanStackAccesses(funcInstructions, hookPoint).ToList(); + var stringAllocation = NativeAlloc(Utf8GamePath.MaxGamePathLength); - var stringLoc = NativeAlloc(Utf8GamePath.MaxGamePathLength); + // We'll need to grab our true hook point; the location where we can change the path at our leisure. + // This is going to be the first call instruction after our 'hookPoint', so, we'll find that. + // Pretty scuffed, this might need a refactoring at some point. + // We're doing it by skipping to our hookPoint's address in the list of instructions inside the function; then getting next CALL + var skipIndex = funcInstructions.IndexOf(instr => instr.IP == hookPoint.IP) + 1; + var detourPoint = funcInstructions.Skip(skipIndex) + .First(instr => instr.Mnemonic == Mnemonic.Call); - { - // We'll need to grab our true hook point; the location where we can change the path at our leisure. - // This is going to be the first call instruction after our 'hookPoint', so, we'll find that. - // Pretty scuffed, this might need a refactoring at some point. - // We're doing it by skipping to our hookPoint's address in the list of instructions inside the function; then getting next CALL - var detourPoint = funcInstructions.Skip( - funcInstructions.FindIndex(instr => instr.IP == hookPoint.IP) + 1 - ).First(instr => instr.Mnemonic == Mnemonic.Call); + // We'll also remove all the 'hookPoints' from 'stackAccesses'. + // We're handling the char *path redirection here, so we don't want this to hit the later code + foreach (var hp in hookPoints) + stackAccesses.RemoveAll(instr => instr.IP == hp.IP); - // We'll also remove all the 'hookPoints' from 'stackAccesses'. - // We're handling the char *path redirection here, so we don't want this to hit the later code - foreach (var hp in hookPoints) - { - stackAccesses.RemoveAll(instr => instr.IP == hp.IP); - } + var detourPointer = Marshal.GetFunctionPointerForDelegate(papResourceHandler); + var targetRegister = hookPoint.Op0Register.ToString().ToLower(); + var hookAddress = new IntPtr((long)detourPoint.IP); - var pDetour = Marshal.GetFunctionPointerForDelegate(PapResourceHandler); - var targetRegister = hookPoint.Op0Register.ToString().ToLower(); - var hookAddr = new IntPtr((long)detourPoint.IP); + var caveAllocation = NativeAlloc(16); + var hook = new AsmHook( + hookAddress, + [ + "use64", + $"mov {targetRegister}, 0x{stringAllocation:x8}", // Move our char *path into the relevant register (rdx) - var caveLoc = NativeAlloc(16); - var hook = new AsmHook( - hookAddr, - [ - "use64", - $"mov {targetRegister}, 0x{stringLoc:x8}", // Move our char *path into the relevant register (rdx) - - // After this asm stub, we have a call to Crc32(); since r9 is a volatile, unused register, we can use it ourselves - // We're essentially storing the original 2 arguments ('this', 'path'), in case they get mangled in our call - // We technically don't need to save rdx ('path'), since it'll be stringLoc, but eh - $"mov r9, 0x{caveLoc:x8}", - "mov [r9], rcx", - "mov [r9+0x8], rdx", - - // We can use 'rax' here too since it's also volatile, and it'll be overwritten by Crc32()'s return anyway - $"mov rax, 0x{pDetour:x8}", // Get a pointer to our detour in place - "call rax", // Call detour - - // Do the reverse process and retrieve the stored stuff - $"mov r9, 0x{caveLoc:x8}", - "mov rcx, [r9]", - "mov rdx, [r9+0x8]", - - // Plop 'rax' (our return value, the path size) into r8, so it's the third argument for the subsequent Crc32() call - "mov r8, rax", - ], "Pap Redirection" - ); + // After this asm stub, we have a call to Crc32(); since r9 is a volatile, unused register, we can use it ourselves + // We're essentially storing the original 2 arguments ('this', 'path'), in case they get mangled in our call + // We technically don't need to save rdx ('path'), since it'll be stringLoc, but eh + $"mov r9, 0x{caveAllocation:x8}", + "mov [r9], rcx", + "mov [r9+0x8], rdx", - Hooks.Add(hookAddr, hook); - hook.Enable(); - } + // We can use 'rax' here too since it's also volatile, and it'll be overwritten by Crc32()'s return anyway + $"mov rax, 0x{detourPointer:x8}", // Get a pointer to our detour in place + "call rax", // Call detour + + // Do the reverse process and retrieve the stored stuff + $"mov r9, 0x{caveAllocation:x8}", + "mov rcx, [r9]", + "mov rdx, [r9+0x8]", + + // Plop 'rax' (our return value, the path size) into r8, so it's the third argument for the subsequent Crc32() call + "mov r8, rax", + ], "Pap Redirection" + ); + + _hooks.Add(hookAddress, hook); + hook.Enable(); // Now we're adjusting every single reference to the stack allocated 'path' to our substantially bigger 'stringLoc' - foreach (var stackAccess in stackAccesses) - { - var hookAddr = new IntPtr((long)stackAccess.IP + stackAccess.Length); - - if (Hooks.ContainsKey(hookAddr)) - { - // Hook already exists, means there's reuse of the same stack address across 2 GetResourceAsync; just skip - continue; - } - - var targetRegister = stackAccess.Op0Register.ToString().ToLower(); - var hook = new AsmHook( - hookAddr, - [ - "use64", - $"mov {targetRegister}, 0x{stringLoc:x8}", - ], "Pap Stack Accesses" - ); - - Hooks.Add(hookAddr, hook); - hook.Enable(); - } + UpdatePathAddresses(stackAccesses, stringAllocation); + } + } + + private void UpdatePathAddresses(IEnumerable stackAccesses, nint stringAllocation) + { + foreach (var stackAccess in stackAccesses) + { + var hookAddress = new IntPtr((long)stackAccess.IP + stackAccess.Length); + + // Hook already exists, means there's reuse of the same stack address across 2 GetResourceAsync; just skip + if (_hooks.ContainsKey(hookAddress)) + continue; + + var targetRegister = stackAccess.Op0Register.ToString().ToLower(); + var hook = new AsmHook( + hookAddress, + [ + "use64", + $"mov {targetRegister}, 0x{stringAllocation:x8}", + ], "Pap Stack Accesses" + ); + + _hooks.Add(hookAddress, hook); + hook.Enable(); } - - - } private static IEnumerable ScanStackAccesses(IEnumerable instructions, Instruction hookPoint) { return instructions.Where(instr => - instr.Code == hookPoint.Code - && instr.Op0Kind == hookPoint.Op0Kind - && instr.Op1Kind == hookPoint.Op1Kind - && instr.MemoryBase == hookPoint.MemoryBase - && instr.MemoryDisplacement64 == hookPoint.MemoryDisplacement64) - .GroupBy(instr => instr.IP) - .Select(grp => grp.First()); + instr.Code == hookPoint.Code + && instr.Op0Kind == hookPoint.Op0Kind + && instr.Op1Kind == hookPoint.Op1Kind + && instr.MemoryBase == hookPoint.MemoryBase + && instr.MemoryDisplacement64 == hookPoint.MemoryDisplacement64) + .GroupBy(instr => instr.IP) + .Select(grp => grp.First()); } // This is utterly fucked and hardcoded, but, again, it works // Might be a neat idea for a more versatile kind of signature though - private static IEnumerable ScanPapHookPoints(List funcInstructions) + private static IEnumerable ScanPapHookPoints(Instruction[] funcInstructions) { - for (var i = 0; i < funcInstructions.Count - 8; i++) + for (var i = 0; i < funcInstructions.Length - 8; i++) { - if (funcInstructions[i .. (i + 8)] is + if (funcInstructions.AsSpan(i, 8) is [ - {Code : Code.Lea_r64_m}, - {Code : Code.Lea_r64_m}, - {Mnemonic: Mnemonic.Call}, - {Code : Code.Lea_r64_m}, - {Mnemonic: Mnemonic.Call}, - {Code : Code.Lea_r64_m}, + { Code : Code.Lea_r64_m }, + { Code : Code.Lea_r64_m }, + { Mnemonic: Mnemonic.Call }, + { Code : Code.Lea_r64_m }, + { Mnemonic: Mnemonic.Call }, + { Code : Code.Lea_r64_m }, .., ] ) - { yield return funcInstructions[i]; - } } } - private unsafe IntPtr NativeAlloc(nuint size) + private unsafe nint NativeAlloc(nuint size) { - var caveLoc = new IntPtr(NativeMemory.Alloc(size)); - NativeAllocList.Add(caveLoc); + var caveLoc = (nint)NativeMemory.Alloc(size); + _nativeAllocList.Add(caveLoc); return caveLoc; } - private static unsafe void NativeFree(IntPtr mem) - { - NativeMemory.Free(mem.ToPointer()); - } + private static unsafe void NativeFree(nint mem) + => NativeMemory.Free((void*)mem); public void Dispose() { - Scanner.Dispose(); - - foreach (var hook in Hooks.Values) + _scanner.Dispose(); + + foreach (var hook in _hooks.Values) { hook.Disable(); hook.Dispose(); } - - Hooks.Clear(); - - foreach (var mem in NativeAllocList) - { - NativeFree(mem); - } - NativeAllocList.Clear(); + _hooks.Clear(); + + foreach (var mem in _nativeAllocList) + NativeFree(mem); + + _nativeAllocList.Clear(); } } diff --git a/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs index cf87aa2b..002846fa 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs @@ -51,13 +51,13 @@ public unsafe class ResourceLoader : IDisposable, IService if (!resolvedPath.HasValue || !Utf8GamePath.FromByteString(resolvedPath.Value.InternalName, out var utf8ResolvedPath)) { - PapRequested?.Invoke(gamePath, gamePath, _resolvedData); + PapRequested?.Invoke(gamePath); return length; } NativeMemory.Copy(utf8ResolvedPath.Path.Path, path, (nuint)utf8ResolvedPath.Length); path[utf8ResolvedPath.Length] = 0; - PapRequested?.Invoke(gamePath, utf8ResolvedPath, _resolvedData); + PapRequested?.Invoke(gamePath); return utf8ResolvedPath.Length; } From cec28a1823fcbb228136705ff98733d70ca71c9e Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 21 Jul 2024 23:49:27 +0200 Subject: [PATCH 333/433] Provide actual hook names. --- .../Hooks/ResourceLoading/PapHandler.cs | 18 +++++++++--------- .../Hooks/ResourceLoading/PapRewriter.cs | 14 +++++++------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs b/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs index 65add13c..ea12a480 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs @@ -8,19 +8,19 @@ public sealed class PapHandler(PapRewriter.PapResourceHandlerPrototype papResour public void Enable() { - ReadOnlySpan signatures = + ReadOnlySpan<(string Sig, string Name)> signatures = [ - Sigs.LoadAlwaysResidentMotionPacks, - Sigs.LoadWeaponDependentResidentMotionPacks, - Sigs.LoadInitialResidentMotionPacks, - Sigs.LoadMotionPacks, - Sigs.LoadMotionPacks2, - Sigs.LoadMigratoryMotionPack, + (Sigs.LoadAlwaysResidentMotionPacks, nameof(Sigs.LoadAlwaysResidentMotionPacks)), + (Sigs.LoadWeaponDependentResidentMotionPacks, nameof(Sigs.LoadWeaponDependentResidentMotionPacks)), + (Sigs.LoadInitialResidentMotionPacks, nameof(Sigs.LoadInitialResidentMotionPacks)), + (Sigs.LoadMotionPacks, nameof(Sigs.LoadMotionPacks)), + (Sigs.LoadMotionPacks2, nameof(Sigs.LoadMotionPacks2)), + (Sigs.LoadMigratoryMotionPack, nameof(Sigs.LoadMigratoryMotionPack)), ]; var stopwatch = Stopwatch.StartNew(); - foreach (var sig in signatures) - _papRewriter.Rewrite(sig); + foreach (var (sig, name) in signatures) + _papRewriter.Rewrite(sig, name); Penumbra.Log.Debug( $"[PapHandler] Rewrote {signatures.Length} .pap functions for inlined GetResourceAsync in {stopwatch.ElapsedMilliseconds} ms."); } diff --git a/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs b/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs index af16d706..5a2b09bf 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs @@ -13,10 +13,10 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou private readonly Dictionary _hooks = []; private readonly List _nativeAllocList = []; - public void Rewrite(string sig) + public void Rewrite(string sig, string name) { if (!_scanner.TryScanText(sig, out var address)) - throw new Exception($"Signature [{sig}] could not be found."); + throw new Exception($"Signature for {name} [{sig}] could not be found."); var funcInstructions = _scanner.GetFunctionInstructions(address).ToArray(); var hookPoints = ScanPapHookPoints(funcInstructions).ToList(); @@ -68,20 +68,20 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou // Plop 'rax' (our return value, the path size) into r8, so it's the third argument for the subsequent Crc32() call "mov r8, rax", - ], "Pap Redirection" + ], $"{name}.PapRedirection" ); _hooks.Add(hookAddress, hook); hook.Enable(); // Now we're adjusting every single reference to the stack allocated 'path' to our substantially bigger 'stringLoc' - UpdatePathAddresses(stackAccesses, stringAllocation); + UpdatePathAddresses(stackAccesses, stringAllocation, name); } } - private void UpdatePathAddresses(IEnumerable stackAccesses, nint stringAllocation) + private void UpdatePathAddresses(IEnumerable stackAccesses, nint stringAllocation, string name) { - foreach (var stackAccess in stackAccesses) + foreach (var (stackAccess, index) in stackAccesses.WithIndex()) { var hookAddress = new IntPtr((long)stackAccess.IP + stackAccess.Length); @@ -95,7 +95,7 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou [ "use64", $"mov {targetRegister}, 0x{stringAllocation:x8}", - ], "Pap Stack Accesses" + ], $"{name}.PapStackAccess[{index}]" ); _hooks.Add(hookAddress, hook); From 1501bd4fbf5b05be2b770b0e1b8b856d71f1dce3 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 22 Jul 2024 12:41:20 +0200 Subject: [PATCH 334/433] Fix negative matching on folders with no matches. --- Penumbra/UI/ModsTab/ModSearchStringSplitter.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Penumbra/UI/ModsTab/ModSearchStringSplitter.cs b/Penumbra/UI/ModsTab/ModSearchStringSplitter.cs index 1ea70731..e7550eea 100644 --- a/Penumbra/UI/ModsTab/ModSearchStringSplitter.cs +++ b/Penumbra/UI/ModsTab/ModSearchStringSplitter.cs @@ -95,9 +95,9 @@ public sealed class ModSearchStringSplitter : SearchStringSplitter MatchesName(i, folder.Name, fullName)) - && !Negated.Any(i => MatchesName(i, folder.Name, fullName)) - && (General.Count == 0 || General.Any(i => MatchesName(i, folder.Name, fullName))); + return Forced.All(i => MatchesName(i, folder.Name, fullName, false)) + && !Negated.Any(i => MatchesName(i, folder.Name, fullName, true)) + && (General.Count == 0 || General.Any(i => MatchesName(i, folder.Name, fullName, false))); } protected override bool Matches(Entry entry, ModFileSystem.Leaf leaf) @@ -128,11 +128,11 @@ public sealed class ModSearchStringSplitter : SearchStringSplitter true, }; - private static bool MatchesName(Entry entry, ReadOnlySpan name, ReadOnlySpan fullName) + private static bool MatchesName(Entry entry, ReadOnlySpan name, ReadOnlySpan fullName, bool defaultValue) => entry.Type switch { ModSearchType.Default => fullName.Contains(entry.Needle, StringComparison.OrdinalIgnoreCase), ModSearchType.Name => name.Contains(entry.Needle, StringComparison.OrdinalIgnoreCase), - _ => false, + _ => defaultValue, }; } From 29dce8f3ab3656253a6c843edde527d2442f9199 Mon Sep 17 00:00:00 2001 From: Actions User Date: Mon, 22 Jul 2024 13:16:22 +0000 Subject: [PATCH 335/433] [CI] Updating repo.json for testing_1.2.0.13 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index ca13bb5b..b6d37a92 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.12", + "TestingAssemblyVersion": "1.2.0.13", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.12/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.13/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From ca648f98a173fa75fc538e0a1c1bc0630b64ac55 Mon Sep 17 00:00:00 2001 From: pmgr <26606291+pmgr@users.noreply.github.com> Date: Mon, 22 Jul 2024 19:37:57 +0100 Subject: [PATCH 336/433] Fix for pap weirdness, hopefully --- .../Hooks/ResourceLoading/PapRewriter.cs | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs b/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs index 5a2b09bf..84cd0c11 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs @@ -2,6 +2,7 @@ using Dalamud.Hooking; using Iced.Intel; using OtterGui; using Penumbra.String.Classes; +using Swan; namespace Penumbra.Interop.Hooks.ResourceLoading; @@ -9,9 +10,10 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou { public unsafe delegate int PapResourceHandlerPrototype(void* self, byte* path, int length); - private readonly PeSigScanner _scanner = new(); - private readonly Dictionary _hooks = []; - private readonly List _nativeAllocList = []; + private readonly PeSigScanner _scanner = new(); + private readonly Dictionary _hooks = []; + private readonly Dictionary<(nint, Register, ulong), nint> _nativeAllocPaths = []; + private readonly List _nativeAllocCaves = []; public void Rewrite(string sig, string name) { @@ -24,7 +26,10 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou foreach (var hookPoint in hookPoints) { var stackAccesses = ScanStackAccesses(funcInstructions, hookPoint).ToList(); - var stringAllocation = NativeAlloc(Utf8GamePath.MaxGamePathLength); + var stringAllocation = NativeAllocPath( + address, hookPoint.MemoryBase, hookPoint.MemoryDisplacement64, + Utf8GamePath.MaxGamePathLength + ); // We'll need to grab our true hook point; the location where we can change the path at our leisure. // This is going to be the first call instruction after our 'hookPoint', so, we'll find that. @@ -43,7 +48,7 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou var targetRegister = hookPoint.Op0Register.ToString().ToLower(); var hookAddress = new IntPtr((long)detourPoint.IP); - var caveAllocation = NativeAlloc(16); + var caveAllocation = NativeAllocCave(16); var hook = new AsmHook( hookAddress, [ @@ -136,13 +141,24 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou } } - private unsafe nint NativeAlloc(nuint size) + private unsafe nint NativeAllocCave(nuint size) { var caveLoc = (nint)NativeMemory.Alloc(size); - _nativeAllocList.Add(caveLoc); + _nativeAllocCaves.Add(caveLoc); return caveLoc; } + + // This is a bit conked but, if we identify a path by: + // 1) The function it belongs to (starting address, 'funcAddress') + // 2) The stack register (not strictly necessary - should always be rbp - but abundance of caution, so I don't hit myself in the future) + // 3) The displacement on the stack + // Then we ensure we have a unique identifier for the specific variable location of that specific function + // This is useful because sometimes the stack address is reused within the same function for different GetResourceAsync calls + private unsafe nint NativeAllocPath(nint funcAddress, Register stackRegister, ulong stackDisplacement, nuint size) + { + return _nativeAllocPaths.GetOrAdd((funcAddress, stackRegister, stackDisplacement), _ => (nint)NativeMemory.Alloc(size)); + } private static unsafe void NativeFree(nint mem) => NativeMemory.Free((void*)mem); @@ -159,9 +175,14 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou _hooks.Clear(); - foreach (var mem in _nativeAllocList) + foreach (var mem in _nativeAllocCaves) NativeFree(mem); - _nativeAllocList.Clear(); + _nativeAllocCaves.Clear(); + + foreach (var mem in _nativeAllocPaths.Values) + NativeFree(mem); + + _nativeAllocPaths.Clear(); } } From a4cd5695fb3e4908e7e51c19e2d42551acc27d19 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 22 Jul 2024 20:41:57 +0200 Subject: [PATCH 337/433] Fix some stuff. --- Penumbra/Api/Api/GameStateApi.cs | 23 +++++++++++++++---- .../Animation/ApricotListenerSoundPlay.cs | 2 +- .../Hooks/ResourceLoading/PapRewriter.cs | 10 +++++++- .../Hooks/ResourceLoading/ResourceLoader.cs | 18 +++++++-------- .../UI/ResourceWatcher/ResourceWatcher.cs | 3 +-- 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/Penumbra/Api/Api/GameStateApi.cs b/Penumbra/Api/Api/GameStateApi.cs index b035c886..c2cae32b 100644 --- a/Penumbra/Api/Api/GameStateApi.cs +++ b/Penumbra/Api/Api/GameStateApi.cs @@ -25,12 +25,14 @@ public class GameStateApi : IPenumbraApiGameState, IApiService, IDisposable _cutsceneService = cutsceneService; _resourceLoader = resourceLoader; _resourceLoader.ResourceLoaded += OnResourceLoaded; + _resourceLoader.PapRequested += OnPapRequested; _communicator.CreatedCharacterBase.Subscribe(OnCreatedCharacterBase, Communication.CreatedCharacterBase.Priority.Api); } public unsafe void Dispose() { _resourceLoader.ResourceLoaded -= OnResourceLoaded; + _resourceLoader.PapRequested -= OnPapRequested; _communicator.CreatedCharacterBase.Unsubscribe(OnCreatedCharacterBase); } @@ -67,14 +69,27 @@ public class GameStateApi : IPenumbraApiGameState, IApiService, IDisposable public PenumbraApiEc SetCutsceneParentIndex(int copyIdx, int newParentIdx) => _cutsceneService.SetParentIndex(copyIdx, newParentIdx) - ? PenumbraApiEc.Success + ? PenumbraApiEc.Success : PenumbraApiEc.InvalidArgument; private unsafe void OnResourceLoaded(ResourceHandle* handle, Utf8GamePath originalPath, FullPath? manipulatedPath, ResolveData resolveData) { - if (resolveData.AssociatedGameObject != nint.Zero) - GameObjectResourceResolved?.Invoke(resolveData.AssociatedGameObject, originalPath.ToString(), - manipulatedPath?.ToString() ?? originalPath.ToString()); + if (resolveData.AssociatedGameObject != nint.Zero && GameObjectResourceResolved != null) + { + var original = originalPath.ToString(); + GameObjectResourceResolved.Invoke(resolveData.AssociatedGameObject, original, + manipulatedPath?.ToString() ?? original); + } + } + + private void OnPapRequested(Utf8GamePath originalPath, FullPath? manipulatedPath, ResolveData resolveData) + { + if (resolveData.AssociatedGameObject != nint.Zero && GameObjectResourceResolved != null) + { + var original = originalPath.ToString(); + GameObjectResourceResolved.Invoke(resolveData.AssociatedGameObject, original, + manipulatedPath?.ToString() ?? original); + } } private void OnCreatedCharacterBase(nint gameObject, ModCollection collection, nint drawObject) diff --git a/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs b/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs index 361fcd4e..96a51027 100644 --- a/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs +++ b/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs @@ -42,7 +42,7 @@ public sealed unsafe class ApricotListenerSoundPlayCaller : FastHook> 13) & 1) == 0) return Task.Result.Original(a1, unused, timeOffset); - Penumbra.Log.Information( + Penumbra.Log.Excessive( $"[Apricot Listener Sound Play Caller] Invoked on 0x{a1:X} with {unused}, {timeOffset}."); // Fetch the IInstanceListenner (sixth argument to inlined call of SoundPlay) var apricotIInstanceListenner = *(nint*)(someIntermediate + 0x270); diff --git a/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs b/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs index 5a2b09bf..33b124c8 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs @@ -1,3 +1,4 @@ +using System.Text.Unicode; using Dalamud.Hooking; using Iced.Intel; using OtterGui; @@ -25,7 +26,7 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou { var stackAccesses = ScanStackAccesses(funcInstructions, hookPoint).ToList(); var stringAllocation = NativeAlloc(Utf8GamePath.MaxGamePathLength); - + WriteToAlloc(stringAllocation, Utf8GamePath.MaxGamePathLength, name); // We'll need to grab our true hook point; the location where we can change the path at our leisure. // This is going to be the first call instruction after our 'hookPoint', so, we'll find that. // Pretty scuffed, this might need a refactoring at some point. @@ -164,4 +165,11 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou _nativeAllocList.Clear(); } + + [Conditional("DEBUG")] + private static unsafe void WriteToAlloc(nint alloc, int size, string name) + { + var span = new Span((void*)alloc, size); + Utf8.TryWrite(span, $"Penumbra.{name}\0", out _); + } } diff --git a/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs index 002846fa..10821287 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs @@ -18,8 +18,8 @@ public unsafe class ResourceLoader : IDisposable, IService private readonly TexMdlService _texMdlService; private readonly PapHandler _papHandler; - private ResolveData _resolvedData = ResolveData.Invalid; - public event Action? PapRequested; + private ResolveData _resolvedData = ResolveData.Invalid; + public event Action? PapRequested; public ResourceLoader(ResourceService resources, FileReadService fileReadService, TexMdlService texMdlService) { @@ -42,23 +42,23 @@ public unsafe class ResourceLoader : IDisposable, IService if (!Utf8GamePath.FromPointer(path, out var gamePath)) return length; - var (resolvedPath, _) = _incMode.Value + var (resolvedPath, data) = _incMode.Value ? (null, ResolveData.Invalid) : _resolvedData.Valid ? (_resolvedData.ModCollection.ResolvePath(gamePath), _resolvedData) : ResolvePath(gamePath, ResourceCategory.Chara, ResourceType.Pap); - if (!resolvedPath.HasValue || !Utf8GamePath.FromByteString(resolvedPath.Value.InternalName, out var utf8ResolvedPath)) + if (!resolvedPath.HasValue) { - PapRequested?.Invoke(gamePath); + PapRequested?.Invoke(gamePath, null, data); return length; } - NativeMemory.Copy(utf8ResolvedPath.Path.Path, path, (nuint)utf8ResolvedPath.Length); - path[utf8ResolvedPath.Length] = 0; - PapRequested?.Invoke(gamePath); - return utf8ResolvedPath.Length; + PapRequested?.Invoke(gamePath, resolvedPath.Value, data); + NativeMemory.Copy(resolvedPath.Value.InternalName.Path, path, (nuint)resolvedPath.Value.InternalName.Length); + path[resolvedPath.Value.InternalName.Length] = 0; + return resolvedPath.Value.InternalName.Length; } /// Load a resource for a given path and a specific collection. diff --git a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs index a00b33c7..14d69489 100644 --- a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs +++ b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs @@ -11,7 +11,6 @@ using Penumbra.GameData.Enums; using Penumbra.Interop.Hooks.ResourceLoading; using Penumbra.Interop.Hooks.Resources; using Penumbra.Interop.Structs; -using Penumbra.Services; using Penumbra.String; using Penumbra.String.Classes; using Penumbra.UI.Classes; @@ -55,7 +54,7 @@ public sealed class ResourceWatcher : IDisposable, ITab, IUiService _newMaxEntries = _config.MaxResourceWatcherRecords; } - private void OnPapRequested(Utf8GamePath original) + private void OnPapRequested(Utf8GamePath original, FullPath? _1, ResolveData _2) { if (_ephemeral.EnableResourceLogging && FilterMatch(original.Path, out var match)) Penumbra.Log.Information($"[ResourceLoader] [REQ] {match} was requested asynchronously."); From df335574778cfee6fa44a5d63a2ef869c5706c11 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 22 Jul 2024 20:54:24 +0200 Subject: [PATCH 338/433] Cleanup. --- Penumbra.Api | 2 +- .../Hooks/ResourceLoading/PapRewriter.cs | 22 +++++++++---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/Penumbra.Api b/Penumbra.Api index f4c6144c..86249598 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit f4c6144ca2012b279e6d8aa52b2bef6cc2ba32d9 +Subproject commit 86249598afb71601b247f9629d9c29dbecfe6eb1 diff --git a/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs b/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs index afe00b8d..2fb1623d 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/PapRewriter.cs @@ -11,10 +11,10 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou { public unsafe delegate int PapResourceHandlerPrototype(void* self, byte* path, int length); - private readonly PeSigScanner _scanner = new(); - private readonly Dictionary _hooks = []; - private readonly Dictionary<(nint, Register, ulong), nint> _nativeAllocPaths = []; - private readonly List _nativeAllocCaves = []; + private readonly PeSigScanner _scanner = new(); + private readonly Dictionary _hooks = []; + private readonly Dictionary<(nint, Register, ulong), nint> _nativeAllocPaths = []; + private readonly List _nativeAllocCaves = []; public void Rewrite(string sig, string name) { @@ -26,13 +26,13 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou foreach (var hookPoint in hookPoints) { - var stackAccesses = ScanStackAccesses(funcInstructions, hookPoint).ToList(); + var stackAccesses = ScanStackAccesses(funcInstructions, hookPoint).ToList(); var stringAllocation = NativeAllocPath( address, hookPoint.MemoryBase, hookPoint.MemoryDisplacement64, Utf8GamePath.MaxGamePathLength - ); + ); WriteToAlloc(stringAllocation, Utf8GamePath.MaxGamePathLength, name); - + // We'll need to grab our true hook point; the location where we can change the path at our leisure. // This is going to be the first call instruction after our 'hookPoint', so, we'll find that. // Pretty scuffed, this might need a refactoring at some point. @@ -150,7 +150,7 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou return caveLoc; } - + // This is a bit conked but, if we identify a path by: // 1) The function it belongs to (starting address, 'funcAddress') // 2) The stack register (not strictly necessary - should always be rbp - but abundance of caution, so I don't hit myself in the future) @@ -158,9 +158,7 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou // Then we ensure we have a unique identifier for the specific variable location of that specific function // This is useful because sometimes the stack address is reused within the same function for different GetResourceAsync calls private unsafe nint NativeAllocPath(nint funcAddress, Register stackRegister, ulong stackDisplacement, nuint size) - { - return _nativeAllocPaths.GetOrAdd((funcAddress, stackRegister, stackDisplacement), _ => (nint)NativeMemory.Alloc(size)); - } + => _nativeAllocPaths.GetOrAdd((funcAddress, stackRegister, stackDisplacement), _ => (nint)NativeMemory.Alloc(size)); private static unsafe void NativeFree(nint mem) => NativeMemory.Free((void*)mem); @@ -181,7 +179,7 @@ public sealed class PapRewriter(PapRewriter.PapResourceHandlerPrototype papResou NativeFree(mem); _nativeAllocCaves.Clear(); - + foreach (var mem in _nativeAllocPaths.Values) NativeFree(mem); From 30f92338627b2d29558a96aa66eb6e1184443138 Mon Sep 17 00:00:00 2001 From: Actions User Date: Mon, 22 Jul 2024 18:56:35 +0000 Subject: [PATCH 339/433] [CI] Updating repo.json for testing_1.2.0.14 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index b6d37a92..f1108cfe 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.13", + "TestingAssemblyVersion": "1.2.0.14", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.13/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.14/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From c501d0b36524d163a45758901390cb840f7f1107 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 24 Jul 2024 15:11:35 +0200 Subject: [PATCH 340/433] Fix card actor identification. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index f13818fd..f5a74c70 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit f13818fd85b436d0a0f66293fe7c6b60d4bffe3c +Subproject commit f5a74c70ad3861c5c66e1df6ae9a29fc7a0d736a From 72f2834dfd13352f70f10e77b1d40a2910198f13 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 24 Jul 2024 15:11:52 +0200 Subject: [PATCH 341/433] Add some resource flags. --- Penumbra/Enums/ResourceTypeFlag.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Penumbra/Enums/ResourceTypeFlag.cs b/Penumbra/Enums/ResourceTypeFlag.cs index 0cfc5469..461e7ac1 100644 --- a/Penumbra/Enums/ResourceTypeFlag.cs +++ b/Penumbra/Enums/ResourceTypeFlag.cs @@ -60,6 +60,13 @@ public enum ResourceTypeFlag : ulong Uld = 0x0002_0000_0000_0000, Waoe = 0x0004_0000_0000_0000, Wtd = 0x0008_0000_0000_0000, + Bklb = 0x0010_0000_0000_0000, + Cutb = 0x0020_0000_0000_0000, + Eanb = 0x0040_0000_0000_0000, + Eslb = 0x0080_0000_0000_0000, + Fpeb = 0x0100_0000_0000_0000, + Kdb = 0x0200_0000_0000_0000, + Kdlb = 0x0400_0000_0000_0000, } [Flags] @@ -141,6 +148,13 @@ public static class ResourceExtensions ResourceType.Uld => ResourceTypeFlag.Uld, ResourceType.Waoe => ResourceTypeFlag.Waoe, ResourceType.Wtd => ResourceTypeFlag.Wtd, + ResourceType.Bklb => ResourceTypeFlag.Bklb, + ResourceType.Cutb => ResourceTypeFlag.Cutb, + ResourceType.Eanb => ResourceTypeFlag.Eanb, + ResourceType.Eslb => ResourceTypeFlag.Eslb, + ResourceType.Fpeb => ResourceTypeFlag.Fpeb, + ResourceType.Kdb => ResourceTypeFlag.Kdb , + ResourceType.Kdlb => ResourceTypeFlag.Kdlb, _ => 0, }; @@ -148,7 +162,7 @@ public static class ResourceExtensions => (type.ToFlag() & flags) != 0; public static ResourceCategoryFlag ToFlag(this ResourceCategory type) - => type switch + => (ResourceCategory)((uint) type & 0x00FFFFFF) switch { ResourceCategory.Common => ResourceCategoryFlag.Common, ResourceCategory.BgCommon => ResourceCategoryFlag.BgCommon, From 246f4f65f5bf60a9c1ee8ecc7bc65075346c6229 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 26 Jul 2024 18:42:48 +0200 Subject: [PATCH 342/433] Make items changed in a mod sort before other items for item swap, also color them. --- OtterGui | 2 +- Penumbra/Mods/ItemSwap/ItemSwap.cs | 10 ++-- Penumbra/UI/AdvancedWindow/ItemSwapTab.cs | 65 ++++++++++++++--------- 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/OtterGui b/OtterGui index dc17161b..87a53262 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit dc17161b1d9c47ffd6bcc17e91f4832cf7762993 +Subproject commit 87a532620d622a00e60059b5dd42a04f0319b5b5 diff --git a/Penumbra/Mods/ItemSwap/ItemSwap.cs b/Penumbra/Mods/ItemSwap/ItemSwap.cs index 1f4c5e7a..03abfc45 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwap.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwap.cs @@ -15,13 +15,9 @@ public static class ItemSwap public class InvalidItemTypeException : Exception { } - public class MissingFileException : Exception + public class MissingFileException(ResourceType type, object path) : Exception($"Could not load {type} File Data for \"{path}\".") { - public readonly ResourceType Type; - - public MissingFileException(ResourceType type, object path) - : base($"Could not load {type} File Data for \"{path}\".") - => Type = type; + public readonly ResourceType Type = type; } private static bool LoadFile(MetaFileManager manager, FullPath path, out byte[] data) @@ -47,7 +43,7 @@ public static class ItemSwap Penumbra.Log.Debug($"Could not load file {path}:\n{e}"); } - data = Array.Empty(); + data = []; return false; } diff --git a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs index 6db4db5c..da2daeb7 100644 --- a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs +++ b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs @@ -22,6 +22,7 @@ using Penumbra.Mods.Settings; using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.UI.Classes; +using Penumbra.UI.ModsTab; namespace Penumbra.UI.AdvancedWindow; @@ -34,7 +35,8 @@ public class ItemSwapTab : IDisposable, ITab, IUiService private readonly MetaFileManager _metaFileManager; public ItemSwapTab(CommunicatorService communicator, ItemData itemService, CollectionManager collectionManager, - ModManager modManager, ObjectIdentification identifier, MetaFileManager metaFileManager, Configuration config) + ModManager modManager, ModFileSystemSelector selector, ObjectIdentification identifier, MetaFileManager metaFileManager, + Configuration config) { _communicator = communicator; _collectionManager = collectionManager; @@ -46,15 +48,15 @@ public class ItemSwapTab : IDisposable, ITab, IUiService _selectors = new Dictionary { // @formatter:off - [SwapType.Hat] = (new ItemSelector(itemService, FullEquipType.Head), new ItemSelector(itemService, FullEquipType.Head), "Take this Hat", "and put it on this one" ), - [SwapType.Top] = (new ItemSelector(itemService, FullEquipType.Body), new ItemSelector(itemService, FullEquipType.Body), "Take this Top", "and put it on this one" ), - [SwapType.Gloves] = (new ItemSelector(itemService, FullEquipType.Hands), new ItemSelector(itemService, FullEquipType.Hands), "Take these Gloves", "and put them on these" ), - [SwapType.Pants] = (new ItemSelector(itemService, FullEquipType.Legs), new ItemSelector(itemService, FullEquipType.Legs), "Take these Pants", "and put them on these" ), - [SwapType.Shoes] = (new ItemSelector(itemService, FullEquipType.Feet), new ItemSelector(itemService, FullEquipType.Feet), "Take these Shoes", "and put them on these" ), - [SwapType.Earrings] = (new ItemSelector(itemService, FullEquipType.Ears), new ItemSelector(itemService, FullEquipType.Ears), "Take these Earrings", "and put them on these" ), - [SwapType.Necklace] = (new ItemSelector(itemService, FullEquipType.Neck), new ItemSelector(itemService, FullEquipType.Neck), "Take this Necklace", "and put it on this one" ), - [SwapType.Bracelet] = (new ItemSelector(itemService, FullEquipType.Wrists), new ItemSelector(itemService, FullEquipType.Wrists), "Take these Bracelets", "and put them on these" ), - [SwapType.Ring] = (new ItemSelector(itemService, FullEquipType.Finger), new ItemSelector(itemService, FullEquipType.Finger), "Take this Ring", "and put it on this one" ), + [SwapType.Hat] = (new ItemSelector(itemService, selector, FullEquipType.Head), new ItemSelector(itemService, null, FullEquipType.Head), "Take this Hat", "and put it on this one" ), + [SwapType.Top] = (new ItemSelector(itemService, selector, FullEquipType.Body), new ItemSelector(itemService, null, FullEquipType.Body), "Take this Top", "and put it on this one" ), + [SwapType.Gloves] = (new ItemSelector(itemService, selector, FullEquipType.Hands), new ItemSelector(itemService, null, FullEquipType.Hands), "Take these Gloves", "and put them on these" ), + [SwapType.Pants] = (new ItemSelector(itemService, selector, FullEquipType.Legs), new ItemSelector(itemService, null, FullEquipType.Legs), "Take these Pants", "and put them on these" ), + [SwapType.Shoes] = (new ItemSelector(itemService, selector, FullEquipType.Feet), new ItemSelector(itemService, null, FullEquipType.Feet), "Take these Shoes", "and put them on these" ), + [SwapType.Earrings] = (new ItemSelector(itemService, selector, FullEquipType.Ears), new ItemSelector(itemService, null, FullEquipType.Ears), "Take these Earrings", "and put them on these" ), + [SwapType.Necklace] = (new ItemSelector(itemService, selector, FullEquipType.Neck), new ItemSelector(itemService, null, FullEquipType.Neck), "Take this Necklace", "and put it on this one" ), + [SwapType.Bracelet] = (new ItemSelector(itemService, selector, FullEquipType.Wrists), new ItemSelector(itemService, null, FullEquipType.Wrists), "Take these Bracelets", "and put them on these" ), + [SwapType.Ring] = (new ItemSelector(itemService, selector, FullEquipType.Finger), new ItemSelector(itemService, null, FullEquipType.Finger), "Take this Ring", "and put it on this one" ), // @formatter:on }; @@ -129,11 +131,24 @@ public class ItemSwapTab : IDisposable, ITab, IUiService Weapon, } - private class ItemSelector(ItemData data, FullEquipType type) - : FilterComboCache(() => data.ByType[type], MouseWheelType.None, Penumbra.Log) + private class ItemSelector(ItemData data, ModFileSystemSelector? selector, FullEquipType type) + : FilterComboCache<(EquipItem Item, bool InMod)>(() => + { + var list = data.ByType[type]; + if (selector?.Selected is { } mod && mod.ChangedItems.Values.Any(o => o is EquipItem i && i.Type == type)) + return list.Select(i => (i, mod.ChangedItems.ContainsKey(i.Name))).OrderByDescending(p => p.Item2).ToList(); + + return list.Select(i => (i, false)).ToList(); + }, MouseWheelType.None, Penumbra.Log) { - protected override string ToString(EquipItem obj) - => obj.Name; + protected override bool DrawSelectable(int globalIdx, bool selected) + { + using var color = ImRaii.PushColor(ImGuiCol.Text, ColorId.ResTreeLocalPlayer.Value(), Items[globalIdx].InMod); + return base.DrawSelectable(globalIdx, selected); + } + + protected override string ToString((EquipItem Item, bool InMod) obj) + => obj.Item.Name; } private readonly Dictionary _selectors; @@ -186,17 +201,17 @@ public class ItemSwapTab : IDisposable, ITab, IUiService case SwapType.Bracelet: case SwapType.Ring: var values = _selectors[_lastTab]; - if (values.Source.CurrentSelection.Type != FullEquipType.Unknown - && values.Target.CurrentSelection.Type != FullEquipType.Unknown) - _affectedItems = _swapData.LoadEquipment(values.Target.CurrentSelection, values.Source.CurrentSelection, + if (values.Source.CurrentSelection.Item.Type != FullEquipType.Unknown + && values.Target.CurrentSelection.Item.Type != FullEquipType.Unknown) + _affectedItems = _swapData.LoadEquipment(values.Target.CurrentSelection.Item, values.Source.CurrentSelection.Item, _useCurrentCollection ? _collectionManager.Active.Current : null, _useRightRing, _useLeftRing); break; case SwapType.BetweenSlots: var (_, _, selectorFrom) = GetAccessorySelector(_slotFrom, true); var (_, _, selectorTo) = GetAccessorySelector(_slotTo, false); - if (selectorFrom.CurrentSelection.Valid && selectorTo.CurrentSelection.Valid) - _affectedItems = _swapData.LoadTypeSwap(_slotTo, selectorTo.CurrentSelection, _slotFrom, selectorFrom.CurrentSelection, + if (selectorFrom.CurrentSelection.Item.Valid && selectorTo.CurrentSelection.Item.Valid) + _affectedItems = _swapData.LoadTypeSwap(_slotTo, selectorTo.CurrentSelection.Item, _slotFrom, selectorFrom.CurrentSelection.Item, _useCurrentCollection ? _collectionManager.Active.Current : null); break; case SwapType.Hair when _targetId > 0 && _sourceId > 0: @@ -455,7 +470,7 @@ public class ItemSwapTab : IDisposable, ITab, IUiService } ImGui.TableNextColumn(); - _dirty |= selector.Draw("##itemSource", selector.CurrentSelection.Name ?? string.Empty, string.Empty, InputWidth * 2 * UiHelpers.Scale, + _dirty |= selector.Draw("##itemSource", selector.CurrentSelection.Item.Name ?? string.Empty, string.Empty, InputWidth * 2 * UiHelpers.Scale, ImGui.GetTextLineHeightWithSpacing()); (article1, _, selector) = GetAccessorySelector(_slotTo, false); @@ -480,7 +495,7 @@ public class ItemSwapTab : IDisposable, ITab, IUiService ImGui.TableNextColumn(); - _dirty |= selector.Draw("##itemTarget", selector.CurrentSelection.Name, string.Empty, InputWidth * 2 * UiHelpers.Scale, + _dirty |= selector.Draw("##itemTarget", selector.CurrentSelection.Item.Name, string.Empty, InputWidth * 2 * UiHelpers.Scale, ImGui.GetTextLineHeightWithSpacing()); if (_affectedItems is not { Length: > 1 }) return; @@ -489,7 +504,7 @@ public class ItemSwapTab : IDisposable, ITab, IUiService ImGuiUtil.DrawTextButton($"which will also affect {_affectedItems.Length - 1} other Items.", Vector2.Zero, Colors.PressEnterWarningBg); if (ImGui.IsItemHovered()) - ImGui.SetTooltip(string.Join('\n', _affectedItems.Where(i => !ReferenceEquals(i.Name, selector.CurrentSelection.Name)) + ImGui.SetTooltip(string.Join('\n', _affectedItems.Where(i => !ReferenceEquals(i.Name, selector.CurrentSelection.Item.Name)) .Select(i => i.Name))); } @@ -521,7 +536,7 @@ public class ItemSwapTab : IDisposable, ITab, IUiService ImGui.AlignTextToFramePadding(); ImGui.TextUnformatted(text1); ImGui.TableNextColumn(); - _dirty |= sourceSelector.Draw("##itemSource", sourceSelector.CurrentSelection.Name, string.Empty, InputWidth * 2 * UiHelpers.Scale, + _dirty |= sourceSelector.Draw("##itemSource", sourceSelector.CurrentSelection.Item.Name, string.Empty, InputWidth * 2 * UiHelpers.Scale, ImGui.GetTextLineHeightWithSpacing()); if (type == SwapType.Ring) @@ -534,7 +549,7 @@ public class ItemSwapTab : IDisposable, ITab, IUiService ImGui.AlignTextToFramePadding(); ImGui.TextUnformatted(text2); ImGui.TableNextColumn(); - _dirty |= targetSelector.Draw("##itemTarget", targetSelector.CurrentSelection.Name, string.Empty, InputWidth * 2 * UiHelpers.Scale, + _dirty |= targetSelector.Draw("##itemTarget", targetSelector.CurrentSelection.Item.Name, string.Empty, InputWidth * 2 * UiHelpers.Scale, ImGui.GetTextLineHeightWithSpacing()); if (type == SwapType.Ring) { @@ -549,7 +564,7 @@ public class ItemSwapTab : IDisposable, ITab, IUiService ImGuiUtil.DrawTextButton($"which will also affect {_affectedItems.Length - 1} other Items.", Vector2.Zero, Colors.PressEnterWarningBg); if (ImGui.IsItemHovered()) - ImGui.SetTooltip(string.Join('\n', _affectedItems.Where(i => !ReferenceEquals(i.Name, targetSelector.CurrentSelection.Name)) + ImGui.SetTooltip(string.Join('\n', _affectedItems.Where(i => !ReferenceEquals(i.Name, targetSelector.CurrentSelection.Item.Name)) .Select(i => i.Name))); } From 3ffe6151ffd05b732ac9df38cac17ba70b3a68ce Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 28 Jul 2024 01:08:41 +0200 Subject: [PATCH 343/433] Add ToString for InternalEqpEntry. --- Penumbra/Meta/Manipulations/Eqp.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Penumbra/Meta/Manipulations/Eqp.cs b/Penumbra/Meta/Manipulations/Eqp.cs index ec4dd6e7..5d37aac8 100644 --- a/Penumbra/Meta/Manipulations/Eqp.cs +++ b/Penumbra/Meta/Manipulations/Eqp.cs @@ -72,4 +72,7 @@ public readonly record struct EqpEntryInternal(uint Value) var (offset, mask) = Eqp.OffsetAndMask(slot); return (uint)((ulong)(entry & mask) >> offset); } + + public override string ToString() + => Value.ToString("X8"); } From f143601aa00d8807fdf6fb016262afdc57cb00ea Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 28 Jul 2024 01:08:58 +0200 Subject: [PATCH 344/433] Do not replace paths when mods are not enabled. --- Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs index 10821287..3f055f64 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs @@ -17,15 +17,17 @@ public unsafe class ResourceLoader : IDisposable, IService private readonly FileReadService _fileReadService; private readonly TexMdlService _texMdlService; private readonly PapHandler _papHandler; + private readonly Configuration _config; private ResolveData _resolvedData = ResolveData.Invalid; public event Action? PapRequested; - public ResourceLoader(ResourceService resources, FileReadService fileReadService, TexMdlService texMdlService) + public ResourceLoader(ResourceService resources, FileReadService fileReadService, TexMdlService texMdlService, Configuration config) { _resources = resources; _fileReadService = fileReadService; _texMdlService = texMdlService; + _config = config; ResetResolvePath(); _resources.ResourceRequested += ResourceHandler; @@ -39,7 +41,7 @@ public unsafe class ResourceLoader : IDisposable, IService private int PapResourceHandler(void* self, byte* path, int length) { - if (!Utf8GamePath.FromPointer(path, out var gamePath)) + if (!_config.EnableMods || !Utf8GamePath.FromPointer(path, out var gamePath)) return length; var (resolvedPath, data) = _incMode.Value @@ -119,7 +121,7 @@ public unsafe class ResourceLoader : IDisposable, IService private void ResourceHandler(ref ResourceCategory category, ref ResourceType type, ref int hash, ref Utf8GamePath path, Utf8GamePath original, GetResourceParameters* parameters, ref bool sync, ref ResourceHandle* returnValue) { - if (returnValue != null) + if (!_config.EnableMods || returnValue != null) return; CompareHash(ComputeHash(path.Path, parameters), hash, path); From 6f3d9eb272b7ad35fade8c951df8254285bb7fa5 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 28 Jul 2024 01:09:11 +0200 Subject: [PATCH 345/433] Fix bug in EquipmentSwap --- Penumbra/Mods/ItemSwap/EquipmentSwap.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Mods/ItemSwap/EquipmentSwap.cs b/Penumbra/Mods/ItemSwap/EquipmentSwap.cs index b7827c47..2c292a14 100644 --- a/Penumbra/Mods/ItemSwap/EquipmentSwap.cs +++ b/Penumbra/Mods/ItemSwap/EquipmentSwap.cs @@ -186,7 +186,7 @@ public static class EquipmentSwap PrimaryId idTo, byte mtrlTo) { var eqdpFromIdentifier = new EqdpIdentifier(idFrom, slotFrom, gr); - var eqdpToIdentifier = new EqdpIdentifier(idFrom, slotFrom, gr); + var eqdpToIdentifier = new EqdpIdentifier(idTo, slotTo, gr); var eqdpFromDefault = new EqdpEntryInternal(ExpandedEqdpFile.GetDefault(manager, eqdpFromIdentifier), slotFrom); var eqdpToDefault = new EqdpEntryInternal(ExpandedEqdpFile.GetDefault(manager, eqdpToIdentifier), slotTo); var meta = new MetaSwap(i => manips.TryGetValue(i, out var e) ? e : null, eqdpFromIdentifier, From cbf5baf65cef7450bbcb2be667d6fe06f69cb7f7 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 28 Jul 2024 01:09:48 +0200 Subject: [PATCH 346/433] Remove Material Reassignment Tab from advanced editing due to being obsolete. --- .../AdvancedWindow/ModEditWindow.Materials.cs | 72 ++----------------- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 1 - 2 files changed, 7 insertions(+), 66 deletions(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs index 0223ca6b..c3483c35 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs @@ -1,5 +1,4 @@ using Dalamud.Interface; -using Dalamud.Interface.Utility; using ImGuiNET; using OtterGui; using OtterGui.Raii; @@ -112,13 +111,13 @@ public partial class ModEditWindow } ImGui.TableNextColumn(); - using (var font = ImRaii.PushFont(UiBuilder.MonoFont, monoFont)) - { - ImGui.AlignTextToFramePadding(); - if (description.Length > 0) - ImGuiUtil.LabeledHelpMarker(label, description); - else - ImGui.TextUnformatted(label); + using (ImRaii.PushFont(UiBuilder.MonoFont, monoFont)) + { + ImGui.AlignTextToFramePadding(); + if (description.Length > 0) + ImGuiUtil.LabeledHelpMarker(label, description); + else + ImGui.TextUnformatted(label); } if (unfolded) @@ -189,61 +188,4 @@ public partial class ModEditWindow if (t) Widget.DrawHexViewer(file.AdditionalData); } - - private void DrawMaterialReassignmentTab() - { - if (_editor.Files.Mdl.Count == 0) - return; - - using var tab = ImRaii.TabItem("Material Reassignment"); - if (!tab) - return; - - ImGui.NewLine(); - MaterialSuffix.Draw(_editor, ImGuiHelpers.ScaledVector2(175, 0)); - - ImGui.NewLine(); - using var child = ImRaii.Child("##mdlFiles", -Vector2.One, true); - if (!child) - return; - - using var table = ImRaii.Table("##files", 4, ImGuiTableFlags.RowBg | ImGuiTableFlags.SizingFixedFit, -Vector2.One); - if (!table) - return; - - var iconSize = ImGui.GetFrameHeight() * Vector2.One; - foreach (var (info, idx) in _editor.MdlMaterialEditor.ModelFiles.WithIndex()) - { - using var id = ImRaii.PushId(idx); - ImGui.TableNextColumn(); - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Save.ToIconString(), iconSize, - "Save the changed mdl file.\nUse at own risk!", !info.Changed, true)) - info.Save(_editor.Compactor); - - ImGui.TableNextColumn(); - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Recycle.ToIconString(), iconSize, - "Restore current changes to default.", !info.Changed, true)) - info.Restore(); - - ImGui.TableNextColumn(); - ImGui.TextUnformatted(info.Path.FullName[(Mod!.ModPath.FullName.Length + 1)..]); - ImGui.TableNextColumn(); - ImGui.SetNextItemWidth(400 * UiHelpers.Scale); - var tmp = info.CurrentMaterials[0]; - if (ImGui.InputText("##0", ref tmp, 64)) - info.SetMaterial(tmp, 0); - - for (var i = 1; i < info.Count; ++i) - { - ImGui.TableNextColumn(); - ImGui.TableNextColumn(); - ImGui.TableNextColumn(); - ImGui.TableNextColumn(); - ImGui.SetNextItemWidth(400 * UiHelpers.Scale); - tmp = info.CurrentMaterials[i]; - if (ImGui.InputText($"##{i}", ref tmp, 64)) - info.SetMaterial(tmp, i); - } - } - } } diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index e915a879..79a8ae34 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -179,7 +179,6 @@ public partial class ModEditWindow : Window, IDisposable, IUiService DrawSwapTab(); _modMergeTab.Draw(); DrawDuplicatesTab(); - DrawMaterialReassignmentTab(); DrawQuickImportTab(); _modelTab.Draw(); _materialTab.Draw(); From a1a7487897aeb8980e82db90b457873c95ae1f49 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 28 Jul 2024 01:11:20 +0200 Subject: [PATCH 347/433] Remove Update Bibo Button. --- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index f81b2831..f8874f89 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -111,27 +111,6 @@ public class ModPanelEditTab( MoveDirectory.Draw(modManager, _mod, buttonSize); UiHelpers.DefaultLineSpace(); - DrawUpdateBibo(buttonSize); - - UiHelpers.DefaultLineSpace(); - } - - private void DrawUpdateBibo(Vector2 buttonSize) - { - if (ImGui.Button("Update Bibo Material", buttonSize)) - { - editor.LoadMod(_mod); - editor.MdlMaterialEditor.ReplaceAllMaterials("bibo", "b"); - editor.MdlMaterialEditor.ReplaceAllMaterials("bibopube", "c"); - editor.MdlMaterialEditor.SaveAllModels(editor.Compactor); - editWindow.UpdateModels(); - } - - ImGuiUtil.HoverTooltip( - "For every model in this mod, change all material names that end in a _b or _c suffix to a _bibo or _bibopube suffix respectively.\n" - + "Does nothing if the mod does not contain any such models or no model contains such materials.\n" - + "Use this for outdated mods made for old Bibo bodies.\n" - + "Go to Advanced Editing for more fine-tuned control over material assignment."); } private void BackupButtons(Vector2 buttonSize) From 19166d8cf4951dafe62766832f7bba00ea25971c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 28 Jul 2024 01:11:52 +0200 Subject: [PATCH 348/433] Add rudimentary start for knowledge window. --- Penumbra/CommandHandler.cs | 20 ++++++--- Penumbra/UI/Knowledge/IKnowledgeTab.cs | 8 ++++ Penumbra/UI/Knowledge/KnowledgeWindow.cs | 55 ++++++++++++++++++++++++ Penumbra/UI/Knowledge/RaceCodeTab.cs | 42 ++++++++++++++++++ Penumbra/UI/WindowSystem.cs | 17 +++++--- 5 files changed, 131 insertions(+), 11 deletions(-) create mode 100644 Penumbra/UI/Knowledge/IKnowledgeTab.cs create mode 100644 Penumbra/UI/Knowledge/KnowledgeWindow.cs create mode 100644 Penumbra/UI/Knowledge/RaceCodeTab.cs diff --git a/Penumbra/CommandHandler.cs b/Penumbra/CommandHandler.cs index 484dd954..db8d9aca 100644 --- a/Penumbra/CommandHandler.cs +++ b/Penumbra/CommandHandler.cs @@ -12,6 +12,7 @@ using Penumbra.Interop.Services; using Penumbra.Mods; using Penumbra.Mods.Manager; using Penumbra.UI; +using Penumbra.UI.Knowledge; namespace Penumbra; @@ -29,11 +30,12 @@ public class CommandHandler : IDisposable, IApiService private readonly CollectionManager _collectionManager; private readonly Penumbra _penumbra; private readonly CollectionEditor _collectionEditor; + private readonly KnowledgeWindow _knowledgeWindow; public CommandHandler(IFramework framework, ICommandManager commandManager, IChatGui chat, RedrawService redrawService, - Configuration config, - ConfigWindow configWindow, ModManager modManager, CollectionManager collectionManager, ActorManager actors, Penumbra penumbra, - CollectionEditor collectionEditor) + Configuration config, ConfigWindow configWindow, ModManager modManager, CollectionManager collectionManager, ActorManager actors, + Penumbra penumbra, + CollectionEditor collectionEditor, KnowledgeWindow knowledgeWindow) { _commandManager = commandManager; _redrawService = redrawService; @@ -45,6 +47,7 @@ public class CommandHandler : IDisposable, IApiService _chat = chat; _penumbra = penumbra; _collectionEditor = collectionEditor; + _knowledgeWindow = knowledgeWindow; framework.RunOnFrameworkThread(() => { if (_commandManager.Commands.ContainsKey(CommandName)) @@ -69,7 +72,7 @@ public class CommandHandler : IDisposable, IApiService var argumentList = arguments.Split(' ', 2); arguments = argumentList.Length == 2 ? argumentList[1] : string.Empty; - var _ = argumentList[0].ToLowerInvariant() switch + _ = argumentList[0].ToLowerInvariant() switch { "window" => ToggleWindow(arguments), "enable" => SetPenumbraState(arguments, true), @@ -83,6 +86,7 @@ public class CommandHandler : IDisposable, IApiService "collection" => SetCollection(arguments), "mod" => SetMod(arguments), "bulktag" => SetTag(arguments), + "knowledge" => HandleKnowledge(arguments), _ => PrintHelp(argumentList[0]), }; } @@ -304,7 +308,7 @@ public class CommandHandler : IDisposable, IApiService identifiers = _actors.FromUserString(split[2], false); } } - catch (ActorManager.IdentifierParseError e) + catch (ActorIdentifierFactory.IdentifierParseError e) { _chat.Print(new SeStringBuilder().AddText("The argument ").AddRed(split[2], true) .AddText($" could not be converted to an identifier. {e.Message}") @@ -619,4 +623,10 @@ public class CommandHandler : IDisposable, IApiService if (_config.PrintSuccessfulCommandsToChat) _chat.Print(text()); } + + private bool HandleKnowledge(string arguments) + { + _knowledgeWindow.Toggle(); + return true; + } } diff --git a/Penumbra/UI/Knowledge/IKnowledgeTab.cs b/Penumbra/UI/Knowledge/IKnowledgeTab.cs new file mode 100644 index 00000000..568d5fda --- /dev/null +++ b/Penumbra/UI/Knowledge/IKnowledgeTab.cs @@ -0,0 +1,8 @@ +namespace Penumbra.UI.Knowledge; + +public interface IKnowledgeTab +{ + public ReadOnlySpan Name { get; } + public ReadOnlySpan SearchTags { get; } + public void Draw(); +} diff --git a/Penumbra/UI/Knowledge/KnowledgeWindow.cs b/Penumbra/UI/Knowledge/KnowledgeWindow.cs new file mode 100644 index 00000000..de1b36b8 --- /dev/null +++ b/Penumbra/UI/Knowledge/KnowledgeWindow.cs @@ -0,0 +1,55 @@ +using System.Text.Unicode; +using Dalamud.Interface.Utility.Raii; +using Dalamud.Interface.Windowing; +using Dalamud.Memory; +using ImGuiNET; +using OtterGui.Services; +using OtterGui.Text; + +namespace Penumbra.UI.Knowledge; + +/// Draw the progress information for import. +public sealed class KnowledgeWindow() : Window("Penumbra Knowledge Window"), IUiService +{ + private readonly IReadOnlyList _tabs = + [ + new RaceCodeTab(), + ]; + + private IKnowledgeTab? _selected; + private readonly byte[] _filterStore = new byte[256]; + + private TerminatedByteString _filter = TerminatedByteString.Empty; + + public override void Draw() + { + DrawSelector(); + ImUtf8.SameLineInner(); + DrawMain(); + } + + private void DrawSelector() + { + using var child = ImUtf8.Child("KnowledgeSelector"u8, new Vector2(250 * ImUtf8.GlobalScale, ImGui.GetContentRegionAvail().Y), true); + if (!child) + return; + + ImGui.SetNextItemWidth(ImGui.GetContentRegionAvail().X); + ImUtf8.InputText("##Filter"u8, _filterStore, out _filter, "Filter..."u8); + + foreach (var tab in _tabs) + { + if (ImUtf8.Selectable(tab.Name, _selected == tab)) + _selected = tab; + } + } + + private void DrawMain() + { + using var child = ImUtf8.Child("KnowledgeMain"u8, ImGui.GetContentRegionAvail(), true); + if (!child || _selected == null) + return; + + _selected.Draw(); + } +} diff --git a/Penumbra/UI/Knowledge/RaceCodeTab.cs b/Penumbra/UI/Knowledge/RaceCodeTab.cs new file mode 100644 index 00000000..988506dd --- /dev/null +++ b/Penumbra/UI/Knowledge/RaceCodeTab.cs @@ -0,0 +1,42 @@ +using ImGuiNET; +using OtterGui.Text; +using Penumbra.GameData.Enums; + +namespace Penumbra.UI.Knowledge; + +public sealed class RaceCodeTab : IKnowledgeTab +{ + public ReadOnlySpan Name + => "Race Codes"u8; + + public ReadOnlySpan SearchTags + => "deformersracecodesmodel"u8; + + public void Draw() + { + using var table = ImUtf8.Table("table"u8, 4, ImGuiTableFlags.SizingFixedFit); + if (!table) + return; + + ImUtf8.TableHeader("Race Code"u8); + ImUtf8.TableHeader("Race"u8); + ImUtf8.TableHeader("Gender"u8); + ImUtf8.TableHeader("NPC"u8); + + foreach (var genderRace in Enum.GetValues()) + { + ImGui.TableNextColumn(); + ImUtf8.Text(genderRace.ToRaceCode()); + + var (gender, race) = genderRace.Split(); + ImGui.TableNextColumn(); + ImUtf8.Text($"{race}"); + + ImGui.TableNextColumn(); + ImUtf8.Text($"{gender}"); + + ImGui.TableNextColumn(); + ImUtf8.Text(((ushort)genderRace & 0xF) != 1 ? "NPC"u8 : "Normal"u8); + } + } +} diff --git a/Penumbra/UI/WindowSystem.cs b/Penumbra/UI/WindowSystem.cs index 72ac0d01..6d382ad4 100644 --- a/Penumbra/UI/WindowSystem.cs +++ b/Penumbra/UI/WindowSystem.cs @@ -3,6 +3,7 @@ using Dalamud.Interface.Windowing; using Dalamud.Plugin; using OtterGui.Services; using Penumbra.UI.AdvancedWindow; +using Penumbra.UI.Knowledge; using Penumbra.UI.Tabs.Debug; namespace Penumbra.UI; @@ -14,20 +15,24 @@ public class PenumbraWindowSystem : IDisposable, IUiService private readonly FileDialogService _fileDialog; public readonly ConfigWindow Window; public readonly PenumbraChangelog Changelog; + public readonly KnowledgeWindow KnowledgeWindow; public PenumbraWindowSystem(IDalamudPluginInterface pi, Configuration config, PenumbraChangelog changelog, ConfigWindow window, - LaunchButton _, ModEditWindow editWindow, FileDialogService fileDialog, ImportPopup importPopup, DebugTab debugTab) + LaunchButton _, ModEditWindow editWindow, FileDialogService fileDialog, ImportPopup importPopup, DebugTab debugTab, + KnowledgeWindow knowledgeWindow) { - _uiBuilder = pi.UiBuilder; - _fileDialog = fileDialog; - Changelog = changelog; - Window = window; - _windowSystem = new WindowSystem("Penumbra"); + _uiBuilder = pi.UiBuilder; + _fileDialog = fileDialog; + KnowledgeWindow = knowledgeWindow; + Changelog = changelog; + Window = window; + _windowSystem = new WindowSystem("Penumbra"); _windowSystem.AddWindow(changelog.Changelog); _windowSystem.AddWindow(window); _windowSystem.AddWindow(editWindow); _windowSystem.AddWindow(importPopup); _windowSystem.AddWindow(debugTab); + _windowSystem.AddWindow(KnowledgeWindow); _uiBuilder.OpenMainUi += Window.Toggle; _uiBuilder.OpenConfigUi += Window.OpenSettings; _uiBuilder.Draw += _windowSystem.Draw; From d0c4d6984cbd8e40db28e47398a309b71382f6ab Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 28 Jul 2024 12:57:59 +0200 Subject: [PATCH 349/433] Dispose collection caches on plugin disposal. --- Penumbra/Collections/Cache/CollectionCacheManager.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Penumbra/Collections/Cache/CollectionCacheManager.cs b/Penumbra/Collections/Cache/CollectionCacheManager.cs index 80d4cf1d..a3b6bb83 100644 --- a/Penumbra/Collections/Cache/CollectionCacheManager.cs +++ b/Penumbra/Collections/Cache/CollectionCacheManager.cs @@ -84,6 +84,12 @@ public class CollectionCacheManager : IDisposable, IService _communicator.ModSettingChanged.Unsubscribe(OnModSettingChange); _communicator.CollectionInheritanceChanged.Unsubscribe(OnCollectionInheritanceChange); MetaFileManager.CharacterUtility.LoadingFinished -= IncrementCounters; + + foreach (var collection in _storage) + { + collection._cache?.Dispose(); + collection._cache = null; + } } public void AddChange(CollectionCache.ChangeData data) From bb4665c367a876d7bff704794651bd673d9c2978 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 28 Jul 2024 12:58:17 +0200 Subject: [PATCH 350/433] Remove unused params. --- Penumbra/UI/ModsTab/ModPanelEditTab.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Penumbra/UI/ModsTab/ModPanelEditTab.cs b/Penumbra/UI/ModsTab/ModPanelEditTab.cs index f8874f89..90d8fb74 100644 --- a/Penumbra/UI/ModsTab/ModPanelEditTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelEditTab.cs @@ -11,7 +11,6 @@ using Penumbra.Mods; using Penumbra.Mods.Editor; using Penumbra.Mods.Manager; using Penumbra.Services; -using Penumbra.UI.AdvancedWindow; using Penumbra.Mods.Settings; using Penumbra.UI.ModsTab.Groups; @@ -22,8 +21,6 @@ public class ModPanelEditTab( ModFileSystemSelector selector, ModFileSystem fileSystem, Services.MessageService messager, - ModEditWindow editWindow, - ModEditor editor, FilenameService filenames, ModExportManager modExportManager, Configuration config, From 4806f8dc3eeea497ce95f52b30ce7b12bf12dc82 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 28 Jul 2024 13:43:01 +0200 Subject: [PATCH 351/433] Do not force loaded game paths to lowercase. --- Penumbra/Mods/SubMods/SubMod.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Penumbra/Mods/SubMods/SubMod.cs b/Penumbra/Mods/SubMods/SubMod.cs index a8c37369..f6b1be96 100644 --- a/Penumbra/Mods/SubMods/SubMod.cs +++ b/Penumbra/Mods/SubMods/SubMod.cs @@ -52,7 +52,7 @@ public static class SubMod if (files != null) foreach (var property in files.Properties()) { - if (Utf8GamePath.FromString(property.Name, out var p, true)) + if (Utf8GamePath.FromString(property.Name, out var p)) data.Files.TryAdd(p, new FullPath(basePath, property.Value.ToObject())); } @@ -60,7 +60,7 @@ public static class SubMod if (swaps != null) foreach (var property in swaps.Properties()) { - if (Utf8GamePath.FromString(property.Name, out var p, true)) + if (Utf8GamePath.FromString(property.Name, out var p)) data.FileSwaps.TryAdd(p, new FullPath(property.Value.ToObject()!)); } From af0bbeb8bfd0c093716f110e77442a60b69543f9 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 28 Jul 2024 13:48:11 +0200 Subject: [PATCH 352/433] Force saving to be synchronous. --- Penumbra/Mods/TemporaryMod.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Mods/TemporaryMod.cs b/Penumbra/Mods/TemporaryMod.cs index e1cf9f2b..e4049482 100644 --- a/Penumbra/Mods/TemporaryMod.cs +++ b/Penumbra/Mods/TemporaryMod.cs @@ -96,7 +96,7 @@ public class TemporaryMod : IMod var manips = new MetaDictionary(collection.MetaCache); defaultMod.Manipulations.UnionWith(manips); - saveService.ImmediateSave(new ModSaveGroup(dir, defaultMod, config.ReplaceNonAsciiOnImport)); + saveService.ImmediateSaveSync(new ModSaveGroup(dir, defaultMod, config.ReplaceNonAsciiOnImport)); modManager.AddMod(dir); Penumbra.Log.Information( $"Successfully generated mod {mod.Name} at {mod.ModPath.FullName} for collection {collection.Identifier}."); From 5d50523c72ca2ccdb41392b285711315e90ea998 Mon Sep 17 00:00:00 2001 From: Actions User Date: Sun, 28 Jul 2024 12:12:56 +0000 Subject: [PATCH 353/433] [CI] Updating repo.json for testing_1.2.0.15 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index f1108cfe..7bcc18b6 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.14", + "TestingAssemblyVersion": "1.2.0.15", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.14/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.15/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From d30d418afe571b8bde490f1566ee421955757c10 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 28 Jul 2024 23:56:11 +0200 Subject: [PATCH 354/433] Remove a ToLower when resolving paths. --- Penumbra/Interop/PathResolving/PathResolver.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Penumbra/Interop/PathResolving/PathResolver.cs b/Penumbra/Interop/PathResolving/PathResolver.cs index 49035dc8..67ec4fc3 100644 --- a/Penumbra/Interop/PathResolving/PathResolver.cs +++ b/Penumbra/Interop/PathResolving/PathResolver.cs @@ -52,7 +52,6 @@ public class PathResolver : IDisposable, IService if (resourceType is ResourceType.Lvb or ResourceType.Lgb or ResourceType.Sgb) return (null, ResolveData.Invalid); - path = path.ToLower(); return category switch { // Only Interface collection. From e52b027545e01786dfce4754fe345fabcdae91b0 Mon Sep 17 00:00:00 2001 From: Actions User Date: Sun, 28 Jul 2024 22:03:19 +0000 Subject: [PATCH 355/433] [CI] Updating repo.json for testing_1.2.0.16 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 7bcc18b6..9c9e41db 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.15", + "TestingAssemblyVersion": "1.2.0.16", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.15/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.16/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 3754f5713224024aa778456d3ecc7d39b5b483a2 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 29 Jul 2024 09:27:00 +0200 Subject: [PATCH 356/433] Reinstate spacebar heating --- .../AdvancedWindow/ModEditWindow.Materials.cs | 58 +++++++++++++++++++ Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 1 + 2 files changed, 59 insertions(+) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs index c3483c35..5a8fb13a 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs @@ -1,4 +1,5 @@ using Dalamud.Interface; +using Dalamud.Interface.Utility; using ImGuiNET; using OtterGui; using OtterGui.Raii; @@ -188,4 +189,61 @@ public partial class ModEditWindow if (t) Widget.DrawHexViewer(file.AdditionalData); } + + private void DrawMaterialReassignmentTab() + { + if (_editor.Files.Mdl.Count == 0) + return; + + using var tab = ImRaii.TabItem("Material Reassignment"); + if (!tab) + return; + + ImGui.NewLine(); + MaterialSuffix.Draw(_editor, ImGuiHelpers.ScaledVector2(175, 0)); + + ImGui.NewLine(); + using var child = ImRaii.Child("##mdlFiles", -Vector2.One, true); + if (!child) + return; + + using var table = ImRaii.Table("##files", 4, ImGuiTableFlags.RowBg | ImGuiTableFlags.SizingFixedFit, -Vector2.One); + if (!table) + return; + + var iconSize = ImGui.GetFrameHeight() * Vector2.One; + foreach (var (info, idx) in _editor.MdlMaterialEditor.ModelFiles.WithIndex()) + { + using var id = ImRaii.PushId(idx); + ImGui.TableNextColumn(); + if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Save.ToIconString(), iconSize, + "Save the changed mdl file.\nUse at own risk!", !info.Changed, true)) + info.Save(_editor.Compactor); + + ImGui.TableNextColumn(); + if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Recycle.ToIconString(), iconSize, + "Restore current changes to default.", !info.Changed, true)) + info.Restore(); + + ImGui.TableNextColumn(); + ImGui.TextUnformatted(info.Path.FullName[(Mod!.ModPath.FullName.Length + 1)..]); + ImGui.TableNextColumn(); + ImGui.SetNextItemWidth(400 * UiHelpers.Scale); + var tmp = info.CurrentMaterials[0]; + if (ImGui.InputText("##0", ref tmp, 64)) + info.SetMaterial(tmp, 0); + + for (var i = 1; i < info.Count; ++i) + { + ImGui.TableNextColumn(); + ImGui.TableNextColumn(); + ImGui.TableNextColumn(); + ImGui.TableNextColumn(); + ImGui.SetNextItemWidth(400 * UiHelpers.Scale); + tmp = info.CurrentMaterials[i]; + if (ImGui.InputText($"##{i}", ref tmp, 64)) + info.SetMaterial(tmp, i); + } + } + } } diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index 79a8ae34..e915a879 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -179,6 +179,7 @@ public partial class ModEditWindow : Window, IDisposable, IUiService DrawSwapTab(); _modMergeTab.Draw(); DrawDuplicatesTab(); + DrawMaterialReassignmentTab(); DrawQuickImportTab(); _modelTab.Draw(); _materialTab.Draw(); From 5512e0cad2b273caca8f1b10b82a72be2274be80 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 29 Jul 2024 09:43:07 +0200 Subject: [PATCH 357/433] Revert no-lowercasing for the moment. --- Penumbra/Interop/PathResolving/PathResolver.cs | 1 + Penumbra/Mods/SubMods/SubMod.cs | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Penumbra/Interop/PathResolving/PathResolver.cs b/Penumbra/Interop/PathResolving/PathResolver.cs index 67ec4fc3..49035dc8 100644 --- a/Penumbra/Interop/PathResolving/PathResolver.cs +++ b/Penumbra/Interop/PathResolving/PathResolver.cs @@ -52,6 +52,7 @@ public class PathResolver : IDisposable, IService if (resourceType is ResourceType.Lvb or ResourceType.Lgb or ResourceType.Sgb) return (null, ResolveData.Invalid); + path = path.ToLower(); return category switch { // Only Interface collection. diff --git a/Penumbra/Mods/SubMods/SubMod.cs b/Penumbra/Mods/SubMods/SubMod.cs index f6b1be96..a8c37369 100644 --- a/Penumbra/Mods/SubMods/SubMod.cs +++ b/Penumbra/Mods/SubMods/SubMod.cs @@ -52,7 +52,7 @@ public static class SubMod if (files != null) foreach (var property in files.Properties()) { - if (Utf8GamePath.FromString(property.Name, out var p)) + if (Utf8GamePath.FromString(property.Name, out var p, true)) data.Files.TryAdd(p, new FullPath(basePath, property.Value.ToObject())); } @@ -60,7 +60,7 @@ public static class SubMod if (swaps != null) foreach (var property in swaps.Properties()) { - if (Utf8GamePath.FromString(property.Name, out var p)) + if (Utf8GamePath.FromString(property.Name, out var p, true)) data.FileSwaps.TryAdd(p, new FullPath(property.Value.ToObject()!)); } From ee801b637eb6a7b1c2092d0b5e54c532b20a462a Mon Sep 17 00:00:00 2001 From: Actions User Date: Mon, 29 Jul 2024 07:46:20 +0000 Subject: [PATCH 358/433] [CI] Updating repo.json for testing_1.2.0.17 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 9c9e41db..adf152b0 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.16", + "TestingAssemblyVersion": "1.2.0.17", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.16/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.17/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 7ceaeb826f88fc08f35f8e85cba7488a2644453a Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 29 Jul 2024 18:25:30 +0200 Subject: [PATCH 359/433] Move Material Reassignment to the back (and shoot it) --- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index e915a879..b0e9af7f 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -179,7 +179,6 @@ public partial class ModEditWindow : Window, IDisposable, IUiService DrawSwapTab(); _modMergeTab.Draw(); DrawDuplicatesTab(); - DrawMaterialReassignmentTab(); DrawQuickImportTab(); _modelTab.Draw(); _materialTab.Draw(); @@ -192,6 +191,7 @@ public partial class ModEditWindow : Window, IDisposable, IUiService } DrawMissingFilesTab(); + DrawMaterialReassignmentTab(); } /// A row of three buttonSizes and a help marker that can be used for material suffix changing. From 8518240bf919a7499953dad8b399114bfa10e1ea Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 29 Jul 2024 18:25:41 +0200 Subject: [PATCH 360/433] Improve knowledge window somewhat. --- Penumbra/UI/Knowledge/KnowledgeWindow.cs | 37 +++++++++++-- Penumbra/UI/Knowledge/RaceCodeTab.cs | 70 +++++++++++++++++++----- 2 files changed, 86 insertions(+), 21 deletions(-) diff --git a/Penumbra/UI/Knowledge/KnowledgeWindow.cs b/Penumbra/UI/Knowledge/KnowledgeWindow.cs index de1b36b8..b14949de 100644 --- a/Penumbra/UI/Knowledge/KnowledgeWindow.cs +++ b/Penumbra/UI/Knowledge/KnowledgeWindow.cs @@ -5,11 +5,12 @@ using Dalamud.Memory; using ImGuiNET; using OtterGui.Services; using OtterGui.Text; +using Penumbra.String; namespace Penumbra.UI.Knowledge; /// Draw the progress information for import. -public sealed class KnowledgeWindow() : Window("Penumbra Knowledge Window"), IUiService +public sealed class KnowledgeWindow : Window, IUiService { private readonly IReadOnlyList _tabs = [ @@ -19,7 +20,16 @@ public sealed class KnowledgeWindow() : Window("Penumbra Knowledge Window"), IUi private IKnowledgeTab? _selected; private readonly byte[] _filterStore = new byte[256]; - private TerminatedByteString _filter = TerminatedByteString.Empty; + private ByteString _lower = ByteString.Empty; + + /// Draw the progress information for import. + public KnowledgeWindow() + : base("Penumbra Knowledge Window") + => SizeConstraints = new WindowSizeConstraints + { + MaximumSize = new Vector2(10000, 10000), + MinimumSize = new Vector2(400, 200), + }; public override void Draw() { @@ -30,15 +40,23 @@ public sealed class KnowledgeWindow() : Window("Penumbra Knowledge Window"), IUi private void DrawSelector() { - using var child = ImUtf8.Child("KnowledgeSelector"u8, new Vector2(250 * ImUtf8.GlobalScale, ImGui.GetContentRegionAvail().Y), true); + using var group = ImUtf8.Group(); + using (ImRaii.PushStyle(ImGuiStyleVar.FrameRounding, 0).Push(ImGuiStyleVar.ItemSpacing, Vector2.Zero)) + { + ImGui.SetNextItemWidth(200 * ImUtf8.GlobalScale); + if (ImUtf8.InputText("##Filter"u8, _filterStore, out TerminatedByteString filter, "Filter..."u8)) + _lower = ByteString.FromSpanUnsafe(filter, true, null, null).AsciiToLowerClone(); + } + + using var child = ImUtf8.Child("KnowledgeSelector"u8, new Vector2(200 * ImUtf8.GlobalScale, ImGui.GetContentRegionAvail().Y), true); if (!child) return; - ImGui.SetNextItemWidth(ImGui.GetContentRegionAvail().X); - ImUtf8.InputText("##Filter"u8, _filterStore, out _filter, "Filter..."u8); - foreach (var tab in _tabs) { + if (!_lower.IsEmpty && tab.SearchTags.IndexOf(_lower.Span) < 0) + continue; + if (ImUtf8.Selectable(tab.Name, _selected == tab)) _selected = tab; } @@ -46,6 +64,13 @@ public sealed class KnowledgeWindow() : Window("Penumbra Knowledge Window"), IUi private void DrawMain() { + using var group = ImUtf8.Group(); + using (ImRaii.PushStyle(ImGuiStyleVar.FrameRounding, 0).Push(ImGuiStyleVar.ItemSpacing, Vector2.Zero)) + { + ImUtf8.TextFramed(_selected == null ? "No Selection"u8 : _selected.Name, ImGui.GetColorU32(ImGuiCol.FrameBg), + new Vector2(ImGui.GetContentRegionAvail().X, 0)); + } + using var child = ImUtf8.Child("KnowledgeMain"u8, ImGui.GetContentRegionAvail(), true); if (!child || _selected == null) return; diff --git a/Penumbra/UI/Knowledge/RaceCodeTab.cs b/Penumbra/UI/Knowledge/RaceCodeTab.cs index 988506dd..36b048aa 100644 --- a/Penumbra/UI/Knowledge/RaceCodeTab.cs +++ b/Penumbra/UI/Knowledge/RaceCodeTab.cs @@ -4,7 +4,7 @@ using Penumbra.GameData.Enums; namespace Penumbra.UI.Knowledge; -public sealed class RaceCodeTab : IKnowledgeTab +public sealed class RaceCodeTab() : IKnowledgeTab { public ReadOnlySpan Name => "Race Codes"u8; @@ -14,29 +14,69 @@ public sealed class RaceCodeTab : IKnowledgeTab public void Draw() { - using var table = ImUtf8.Table("table"u8, 4, ImGuiTableFlags.SizingFixedFit); - if (!table) - return; + var size = new Vector2((ImGui.GetContentRegionAvail().X - ImUtf8.ItemSpacing.X) / 2, 0); + using (var table = ImUtf8.Table("adults"u8, 4, ImGuiTableFlags.BordersOuter, size)) + { + if (!table) + return; - ImUtf8.TableHeader("Race Code"u8); - ImUtf8.TableHeader("Race"u8); - ImUtf8.TableHeader("Gender"u8); - ImUtf8.TableHeader("NPC"u8); + DrawHeaders(); + foreach (var gr in Enum.GetValues()) + { + var (gender, race) = gr.Split(); + if (gender is not Gender.Male and not Gender.Female || race is ModelRace.Unknown) + continue; - foreach (var genderRace in Enum.GetValues()) + DrawRow(gender, race, false); + } + } + + ImGui.SameLine(); + + using (var table = ImUtf8.Table("children"u8, 4, ImGuiTableFlags.BordersOuter, size)) + { + if (!table) + return; + + DrawHeaders(); + foreach (var race in (ReadOnlySpan) + [ModelRace.Midlander, ModelRace.Elezen, ModelRace.Miqote, ModelRace.AuRa, ModelRace.Unknown]) + { + foreach (var gender in (ReadOnlySpan) [Gender.Male, Gender.Female]) + DrawRow(gender, race, true); + } + } + + return; + + static void DrawHeaders() { ImGui.TableNextColumn(); - ImUtf8.Text(genderRace.ToRaceCode()); - - var (gender, race) = genderRace.Split(); + ImUtf8.TableHeader("Race"u8); ImGui.TableNextColumn(); - ImUtf8.Text($"{race}"); + ImUtf8.TableHeader("Gender"u8); + ImGui.TableNextColumn(); + ImUtf8.TableHeader("Age"u8); + ImGui.TableNextColumn(); + ImUtf8.TableHeader("Race Code"u8); + } + + static void DrawRow(Gender gender, ModelRace race, bool child) + { + var gr = child + ? Names.CombinedRace(gender is Gender.Male ? Gender.MaleNpc : Gender.FemaleNpc, race) + : Names.CombinedRace(gender, race); + ImGui.TableNextColumn(); + ImUtf8.Text(race.ToName()); ImGui.TableNextColumn(); - ImUtf8.Text($"{gender}"); + ImUtf8.Text(gender.ToName()); ImGui.TableNextColumn(); - ImUtf8.Text(((ushort)genderRace & 0xF) != 1 ? "NPC"u8 : "Normal"u8); + ImUtf8.Text(child ? "Child"u8 : "Adult"u8); + + ImGui.TableNextColumn(); + ImUtf8.CopyOnClickSelectable(gr.ToRaceCode()); } } } From 5270ad4d0d8637c9c6ff5d3ed715e486236c9a1b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 29 Jul 2024 23:00:03 +0200 Subject: [PATCH 361/433] Update ImageSharp --- Penumbra/Penumbra.csproj | 2 +- Penumbra/packages.lock.json | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Penumbra/Penumbra.csproj b/Penumbra/Penumbra.csproj index 70208737..24ffe469 100644 --- a/Penumbra/Penumbra.csproj +++ b/Penumbra/Penumbra.csproj @@ -79,7 +79,7 @@ - + diff --git a/Penumbra/packages.lock.json b/Penumbra/packages.lock.json index 42539e78..8e7106dd 100644 --- a/Penumbra/packages.lock.json +++ b/Penumbra/packages.lock.json @@ -44,9 +44,9 @@ }, "SixLabors.ImageSharp": { "type": "Direct", - "requested": "[3.1.4, )", - "resolved": "3.1.4", - "contentHash": "lFIdxgGDA5iYkUMRFOze7BGLcdpoLFbR+a20kc1W7NepvzU7ejtxtWOg9RvgG7kb9tBoJ3ONYOK6kLil/dgF1w==" + "requested": "[3.1.5, )", + "resolved": "3.1.5", + "contentHash": "lNtlq7dSI/QEbYey+A0xn48z5w4XHSffF8222cC4F4YwTXfEImuiBavQcWjr49LThT/pRmtWJRcqA/PlL+eJ6g==" }, "JetBrains.Annotations": { "type": "Transitive", From 70281c576e7b16ac5b7551e53472e625b7f44a91 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 30 Jul 2024 18:34:26 +0200 Subject: [PATCH 362/433] Update Submodules. --- OtterGui | 2 +- Penumbra.GameData | 2 +- Penumbra.String | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/OtterGui b/OtterGui index 87a53262..33ffd7cb 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 87a532620d622a00e60059b5dd42a04f0319b5b5 +Subproject commit 33ffd7cba3e487e98e55adca1677354078089943 diff --git a/Penumbra.GameData b/Penumbra.GameData index f5a74c70..d8ebd63c 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit f5a74c70ad3861c5c66e1df6ae9a29fc7a0d736a +Subproject commit d8ebd63cec1ac12ea547fd37b6c32bdf9b3f57d1 diff --git a/Penumbra.String b/Penumbra.String index f04abbab..91f0f211 160000 --- a/Penumbra.String +++ b/Penumbra.String @@ -1 +1 @@ -Subproject commit f04abbabedf5e757c5cbb970f3e513fef23e53cf +Subproject commit 91f0f21137c61bd39281debf88a8ecc494043330 From 9d128a4d831849c791dbce8efa9dbcda4c75f75f Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 30 Jul 2024 18:38:25 +0200 Subject: [PATCH 363/433] Fix potential threading issue on launch. --- Penumbra/Interop/PathResolving/DrawObjectState.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Penumbra/Interop/PathResolving/DrawObjectState.cs b/Penumbra/Interop/PathResolving/DrawObjectState.cs index 2a9ec7a9..5e413fe2 100644 --- a/Penumbra/Interop/PathResolving/DrawObjectState.cs +++ b/Penumbra/Interop/PathResolving/DrawObjectState.cs @@ -1,3 +1,4 @@ +using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Game.Character; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using OtterGui.Services; @@ -22,18 +23,19 @@ public sealed class DrawObjectState : IDisposable, IReadOnlyDictionary _gameState.LastGameObject; public unsafe DrawObjectState(ObjectManager objects, CreateCharacterBase createCharacterBase, WeaponReload weaponReload, - CharacterBaseDestructor characterBaseDestructor, GameState gameState) + CharacterBaseDestructor characterBaseDestructor, GameState gameState, IFramework framework) { _objects = objects; _createCharacterBase = createCharacterBase; _weaponReload = weaponReload; _characterBaseDestructor = characterBaseDestructor; _gameState = gameState; + framework.RunOnFrameworkThread(InitializeDrawObjects); + _weaponReload.Subscribe(OnWeaponReloading, WeaponReload.Priority.DrawObjectState); _weaponReload.Subscribe(OnWeaponReloaded, WeaponReload.PostEvent.Priority.DrawObjectState); _createCharacterBase.Subscribe(OnCharacterBaseCreated, CreateCharacterBase.PostEvent.Priority.DrawObjectState); _characterBaseDestructor.Subscribe(OnCharacterBaseDestructor, CharacterBaseDestructor.Priority.DrawObjectState); - InitializeDrawObjects(); } public bool ContainsKey(nint key) @@ -94,8 +96,8 @@ public sealed class DrawObjectState : IDisposable, IReadOnlyDictionary private unsafe void InitializeDrawObjects() { - foreach(var actor in _objects) - { + foreach (var actor in _objects) + { if (actor is { IsCharacter: true, Model.Valid: true }) IterateDrawObjectTree((Object*)actor.Model.Address, actor, false, false); } From d247f83e1db8bf59ea647fdae3e9a22fb4996015 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 30 Jul 2024 18:53:55 +0200 Subject: [PATCH 364/433] Use CiByteString for anything path-related. --- Penumbra/Api/Api/ResolveApi.cs | 2 +- Penumbra/Api/Api/TemporaryApi.cs | 2 +- Penumbra/Api/DalamudSubstitutionProvider.cs | 3 +- Penumbra/Collections/Cache/ImcCache.cs | 6 ++-- Penumbra/Enums/ResourceTypeFlag.cs | 6 ++-- .../PostProcessing/PreBoneDeformerReplacer.cs | 3 +- .../Hooks/ResourceLoading/CreateFileWHook.cs | 2 +- .../Hooks/ResourceLoading/ResourceLoader.cs | 21 +++++++------- .../Hooks/ResourceLoading/ResourceService.cs | 8 +++--- .../Interop/MaterialPreview/MaterialInfo.cs | 7 +++-- .../Interop/PathResolving/PathDataHandler.cs | 10 +++---- .../Interop/PathResolving/PathResolver.cs | 1 - Penumbra/Interop/PathResolving/PathState.cs | 2 +- .../Processing/AvfxPathPreProcessor.cs | 2 +- .../Processing/FilePostProcessService.cs | 4 +-- .../Processing/GamePathPreProcessService.cs | 4 +-- .../Processing/ImcFilePostProcessor.cs | 2 +- .../Interop/Processing/ImcPathPreProcessor.cs | 2 +- .../Processing/MaterialFilePostProcessor.cs | 2 +- .../Processing/MtrlPathPreProcessor.cs | 2 +- .../Interop/Processing/TmbPathPreProcessor.cs | 2 +- .../ResolveContext.PathResolution.cs | 9 +++--- .../Interop/ResourceTree/ResolveContext.cs | 20 ++++++------- Penumbra/Interop/ResourceTree/ResourceNode.cs | 12 ++++---- Penumbra/Interop/Services/DecalReverter.cs | 5 ++-- Penumbra/Interop/Structs/ResourceHandle.cs | 4 +-- Penumbra/Interop/Structs/StructExtensions.cs | 24 ++++++++-------- Penumbra/Mods/ModCreator.cs | 4 +-- Penumbra/Mods/SubMods/SubMod.cs | 4 +-- Penumbra/UI/AdvancedWindow/FileEditor.cs | 5 ++-- .../UI/AdvancedWindow/ModEditWindow.Files.cs | 8 +++--- .../ModEditWindow.Materials.MtrlTab.cs | 4 +-- .../ModEditWindow.Models.MdlTab.cs | 2 +- .../ModEditWindow.ShaderPackages.cs | 2 +- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 2 +- .../UI/AdvancedWindow/ResourceTreeViewer.cs | 28 ++++++++++++------- Penumbra/UI/Knowledge/KnowledgeWindow.cs | 2 -- Penumbra/UI/ResourceWatcher/Record.cs | 18 ++++++------ .../UI/ResourceWatcher/ResourceWatcher.cs | 4 +-- .../ResourceWatcher/ResourceWatcherTable.cs | 4 +-- Penumbra/UI/Tabs/Debug/DebugTab.cs | 28 +++++++++++++++++++ Penumbra/UI/Tabs/EffectiveTab.cs | 5 ++-- 42 files changed, 163 insertions(+), 124 deletions(-) diff --git a/Penumbra/Api/Api/ResolveApi.cs b/Penumbra/Api/Api/ResolveApi.cs index ec57eba7..481ea7ad 100644 --- a/Penumbra/Api/Api/ResolveApi.cs +++ b/Penumbra/Api/Api/ResolveApi.cs @@ -94,7 +94,7 @@ public class ResolveApi( if (!config.EnableMods) return path; - var gamePath = Utf8GamePath.FromString(path, out var p, true) ? p : Utf8GamePath.Empty; + var gamePath = Utf8GamePath.FromString(path, out var p) ? p : Utf8GamePath.Empty; var ret = collection.ResolvePath(gamePath); return ret?.ToString() ?? path; } diff --git a/Penumbra/Api/Api/TemporaryApi.cs b/Penumbra/Api/Api/TemporaryApi.cs index 0894a8e5..f02b0d94 100644 --- a/Penumbra/Api/Api/TemporaryApi.cs +++ b/Penumbra/Api/Api/TemporaryApi.cs @@ -137,7 +137,7 @@ public class TemporaryApi( paths = new Dictionary(redirections.Count); foreach (var (gString, fString) in redirections) { - if (!Utf8GamePath.FromString(gString, out var path, false)) + if (!Utf8GamePath.FromString(gString, out var path)) { paths = null; return false; diff --git a/Penumbra/Api/DalamudSubstitutionProvider.cs b/Penumbra/Api/DalamudSubstitutionProvider.cs index 6347447a..e10dc461 100644 --- a/Penumbra/Api/DalamudSubstitutionProvider.cs +++ b/Penumbra/Api/DalamudSubstitutionProvider.cs @@ -4,7 +4,6 @@ using OtterGui.Services; using Penumbra.Collections; using Penumbra.Collections.Manager; using Penumbra.Communication; -using Penumbra.Mods; using Penumbra.Mods.Editor; using Penumbra.Services; using Penumbra.String.Classes; @@ -130,7 +129,7 @@ public class DalamudSubstitutionProvider : IDisposable, IApiService try { - if (!Utf8GamePath.FromString(path, out var utf8Path, true)) + if (!Utf8GamePath.FromString(path, out var utf8Path)) return; var resolved = _activeCollectionData.Interface.ResolvePath(utf8Path); diff --git a/Penumbra/Collections/Cache/ImcCache.cs b/Penumbra/Collections/Cache/ImcCache.cs index 40c3d2c7..cac52f99 100644 --- a/Penumbra/Collections/Cache/ImcCache.cs +++ b/Penumbra/Collections/Cache/ImcCache.cs @@ -8,12 +8,12 @@ namespace Penumbra.Collections.Cache; public sealed class ImcCache(MetaFileManager manager, ModCollection collection) : MetaCacheBase(manager, collection) { - private readonly Dictionary)> _imcFiles = []; + private readonly Dictionary)> _imcFiles = []; - public bool HasFile(ByteString path) + public bool HasFile(CiByteString path) => _imcFiles.ContainsKey(path); - public bool GetFile(ByteString path, [NotNullWhen(true)] out ImcFile? file) + public bool GetFile(CiByteString path, [NotNullWhen(true)] out ImcFile? file) { if (!_imcFiles.TryGetValue(path, out var p)) { diff --git a/Penumbra/Enums/ResourceTypeFlag.cs b/Penumbra/Enums/ResourceTypeFlag.cs index 461e7ac1..920e9780 100644 --- a/Penumbra/Enums/ResourceTypeFlag.cs +++ b/Penumbra/Enums/ResourceTypeFlag.cs @@ -216,10 +216,10 @@ public static class ResourceExtensions }; } - public static ResourceType Type(ByteString path) + public static ResourceType Type(CiByteString path) { var extIdx = path.LastIndexOf((byte)'.'); - var ext = extIdx == -1 ? path : extIdx == path.Length - 1 ? ByteString.Empty : path.Substring(extIdx + 1); + var ext = extIdx == -1 ? path : extIdx == path.Length - 1 ? CiByteString.Empty : path.Substring(extIdx + 1); return ext.Length switch { @@ -231,7 +231,7 @@ public static class ResourceExtensions }; } - public static ResourceCategory Category(ByteString path) + public static ResourceCategory Category(CiByteString path) { if (path.Length < 3) return ResourceCategory.Debug; diff --git a/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs b/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs index 903484ea..834a7d28 100644 --- a/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs +++ b/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs @@ -7,6 +7,7 @@ using Penumbra.Api.Enums; using Penumbra.Interop.Hooks.ResourceLoading; using Penumbra.Interop.PathResolving; using Penumbra.Interop.SafeHandles; +using Penumbra.String; using Penumbra.String.Classes; using CharacterUtility = Penumbra.Interop.Services.CharacterUtility; @@ -15,7 +16,7 @@ namespace Penumbra.Interop.Hooks.PostProcessing; public sealed unsafe class PreBoneDeformerReplacer : IDisposable, IRequiredService { public static readonly Utf8GamePath PreBoneDeformerPath = - Utf8GamePath.FromSpan("chara/xls/boneDeformer/human.pbd"u8, out var p) ? p : Utf8GamePath.Empty; + Utf8GamePath.FromSpan("chara/xls/boneDeformer/human.pbd"u8, MetaDataComputation.All, out var p) ? p : Utf8GamePath.Empty; // Approximate name guesses. private delegate void CharacterBaseSetupScalingDelegate(CharacterBase* drawObject, uint slotIndex); diff --git a/Penumbra/Interop/Hooks/ResourceLoading/CreateFileWHook.cs b/Penumbra/Interop/Hooks/ResourceLoading/CreateFileWHook.cs index a8ac0608..8d0ac8cb 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/CreateFileWHook.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/CreateFileWHook.cs @@ -102,7 +102,7 @@ public unsafe class CreateFileWHook : IDisposable, IRequiredService { // Use static storage. var ptr = WriteFileName(name); - Penumbra.Log.Excessive($"[ResourceHooks] Calling CreateFileWDetour with {ByteString.FromSpanUnsafe(name, false)}."); + Penumbra.Log.Excessive($"[ResourceHooks] Calling CreateFileWDetour with {CiByteString.FromSpanUnsafe(name, false)}."); return _createFileWHook.OriginalDisposeSafe(ptr, access, shareMode, security, creation, flags, template); } diff --git a/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs index 3f055f64..bcd09b37 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/ResourceLoader.cs @@ -41,7 +41,7 @@ public unsafe class ResourceLoader : IDisposable, IService private int PapResourceHandler(void* self, byte* path, int length) { - if (!_config.EnableMods || !Utf8GamePath.FromPointer(path, out var gamePath)) + if (!_config.EnableMods || !Utf8GamePath.FromPointer(path, MetaDataComputation.CiCrc32, out var gamePath)) return length; var (resolvedPath, data) = _incMode.Value @@ -64,7 +64,7 @@ public unsafe class ResourceLoader : IDisposable, IService } /// Load a resource for a given path and a specific collection. - public ResourceHandle* LoadResolvedResource(ResourceCategory category, ResourceType type, ByteString path, ResolveData resolveData) + public ResourceHandle* LoadResolvedResource(ResourceCategory category, ResourceType type, CiByteString path, ResolveData resolveData) { _resolvedData = resolveData; var ret = _resources.GetResource(category, type, path); @@ -73,7 +73,7 @@ public unsafe class ResourceLoader : IDisposable, IService } /// Load a resource for a given path and a specific collection. - public SafeResourceHandle LoadResolvedSafeResource(ResourceCategory category, ResourceType type, ByteString path, ResolveData resolveData) + public SafeResourceHandle LoadResolvedSafeResource(ResourceCategory category, ResourceType type, CiByteString path, ResolveData resolveData) { _resolvedData = resolveData; var ret = _resources.GetSafeResource(category, type, path); @@ -98,7 +98,7 @@ public unsafe class ResourceLoader : IDisposable, IService /// public event ResourceLoadedDelegate? ResourceLoaded; - public delegate void FileLoadedDelegate(ResourceHandle* resource, ByteString path, bool returnValue, bool custom, + public delegate void FileLoadedDelegate(ResourceHandle* resource, CiByteString path, bool returnValue, bool custom, ReadOnlySpan additionalData); /// @@ -172,7 +172,8 @@ public unsafe class ResourceLoader : IDisposable, IService return; } - var path = ByteString.FromSpanUnsafe(actualPath, gamePath.Path.IsNullTerminated, gamePath.Path.IsAsciiLowerCase, gamePath.Path.IsAscii); + var path = CiByteString.FromSpanUnsafe(actualPath, gamePath.Path.IsNullTerminated, gamePath.Path.IsAsciiLowerCase, + gamePath.Path.IsAscii); fileDescriptor->ResourceHandle->FileNameData = path.Path; fileDescriptor->ResourceHandle->FileNameLength = path.Length; MtrlForceSync(fileDescriptor, ref isSync); @@ -184,7 +185,7 @@ public unsafe class ResourceLoader : IDisposable, IService /// Load a resource by its path. If it is rooted, it will be loaded from the drive, otherwise from the SqPack. - private byte DefaultLoadResource(ByteString gamePath, SeFileDescriptor* fileDescriptor, int priority, + private byte DefaultLoadResource(CiByteString gamePath, SeFileDescriptor* fileDescriptor, int priority, bool isSync, ReadOnlySpan additionalData) { if (Utf8GamePath.IsRooted(gamePath)) @@ -265,7 +266,7 @@ public unsafe class ResourceLoader : IDisposable, IService } /// Compute the CRC32 hash for a given path together with potential resource parameters. - private static int ComputeHash(ByteString path, GetResourceParameters* pGetResParams) + private static int ComputeHash(CiByteString path, GetResourceParameters* pGetResParams) { if (pGetResParams == null || !pGetResParams->IsPartialRead) return path.Crc32; @@ -273,11 +274,11 @@ public unsafe class ResourceLoader : IDisposable, IService // When the game requests file only partially, crc32 includes that information, in format of: // path/to/file.ext.hex_offset.hex_size // ex) music/ex4/BGM_EX4_System_Title.scd.381adc.30000 - return ByteString.Join( + return CiByteString.Join( (byte)'.', path, - ByteString.FromStringUnsafe(pGetResParams->SegmentOffset.ToString("x"), true), - ByteString.FromStringUnsafe(pGetResParams->SegmentLength.ToString("x"), true) + CiByteString.FromString(pGetResParams->SegmentOffset.ToString("x"), out var s1, MetaDataComputation.None) ? s1 : CiByteString.Empty, + CiByteString.FromString(pGetResParams->SegmentLength.ToString("x"), out var s2, MetaDataComputation.None) ? s2 : CiByteString.Empty ).Crc32; } diff --git a/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs b/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs index 0b00452b..8b99dc37 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs @@ -39,14 +39,14 @@ public unsafe class ResourceService : IDisposable, IRequiredService } } - public ResourceHandle* GetResource(ResourceCategory category, ResourceType type, ByteString path) + public ResourceHandle* GetResource(ResourceCategory category, ResourceType type, CiByteString path) { var hash = path.Crc32; return GetResourceHandler(true, (ResourceManager*)_resourceManager.ResourceManagerAddress, &category, &type, &hash, path.Path, null, false); } - public SafeResourceHandle GetSafeResource(ResourceCategory category, ResourceType type, ByteString path) + public SafeResourceHandle GetSafeResource(ResourceCategory category, ResourceType type, CiByteString path) => new((CSResourceHandle*)GetResource(category, type, path), false); public void Dispose() @@ -102,7 +102,7 @@ public unsafe class ResourceService : IDisposable, IRequiredService ResourceType* resourceType, int* resourceHash, byte* path, GetResourceParameters* pGetResParams, bool isUnk) { using var performance = _performance.Measure(PerformanceType.GetResourceHandler); - if (!Utf8GamePath.FromPointer(path, out var gamePath)) + if (!Utf8GamePath.FromPointer(path, MetaDataComputation.CiCrc32, out var gamePath)) { Penumbra.Log.Error("[ResourceService] Could not create GamePath from resource path."); return isSync @@ -120,7 +120,7 @@ public unsafe class ResourceService : IDisposable, IRequiredService } /// Call the original GetResource function. - public ResourceHandle* GetOriginalResource(bool sync, ResourceCategory categoryId, ResourceType type, int hash, ByteString path, + public ResourceHandle* GetOriginalResource(bool sync, ResourceCategory categoryId, ResourceType type, int hash, CiByteString path, GetResourceParameters* resourceParameters = null, bool unk = false) => sync ? _getResourceSyncHook.OriginalDisposeSafe(_resourceManager.ResourceManager, &categoryId, &type, &hash, path.Path, diff --git a/Penumbra/Interop/MaterialPreview/MaterialInfo.cs b/Penumbra/Interop/MaterialPreview/MaterialInfo.cs index f7e6caf0..f2ea2d6c 100644 --- a/Penumbra/Interop/MaterialPreview/MaterialInfo.cs +++ b/Penumbra/Interop/MaterialPreview/MaterialInfo.cs @@ -49,7 +49,10 @@ public readonly record struct MaterialInfo(ObjectIndex ObjectIndex, DrawObjectTy public static unsafe List FindMaterials(IEnumerable gameObjects, string materialPath) { - var needle = ByteString.FromString(materialPath.Replace('\\', '/'), out var m, true) ? m : ByteString.Empty; + var needle = CiByteString.FromString(materialPath.Replace('\\', '/'), out var m, + MetaDataComputation.CiCrc32 | MetaDataComputation.Crc32) + ? m + : CiByteString.Empty; var result = new List(Enum.GetValues().Length); foreach (var objectPtr in gameObjects) @@ -83,7 +86,7 @@ public readonly record struct MaterialInfo(ObjectIndex ObjectIndex, DrawObjectTy continue; PathDataHandler.Split(mtrlHandle->ResourceHandle.FileName.AsSpan(), out var path, out _); - var fileName = ByteString.FromSpanUnsafe(path, true); + var fileName = CiByteString.FromSpanUnsafe(path, true); if (fileName == needle) result.Add(new MaterialInfo(index, type, i, j)); } diff --git a/Penumbra/Interop/PathResolving/PathDataHandler.cs b/Penumbra/Interop/PathResolving/PathDataHandler.cs index a8be97c8..9410ff98 100644 --- a/Penumbra/Interop/PathResolving/PathDataHandler.cs +++ b/Penumbra/Interop/PathResolving/PathDataHandler.cs @@ -31,27 +31,27 @@ public static class PathDataHandler /// Create the encoding path for an IMC file. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static FullPath CreateImc(ByteString path, ModCollection collection) + public static FullPath CreateImc(CiByteString path, ModCollection collection) => new($"|{collection.LocalId.Id}_{collection.ImcChangeCounter}_{DiscriminatorString}|{path}"); /// Create the encoding path for a TMB file. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static FullPath CreateTmb(ByteString path, ModCollection collection) + public static FullPath CreateTmb(CiByteString path, ModCollection collection) => CreateBase(path, collection); /// Create the encoding path for an AVFX file. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static FullPath CreateAvfx(ByteString path, ModCollection collection) + public static FullPath CreateAvfx(CiByteString path, ModCollection collection) => CreateBase(path, collection); /// Create the encoding path for a MTRL file. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static FullPath CreateMtrl(ByteString path, ModCollection collection, Utf8GamePath originalPath) + public static FullPath CreateMtrl(CiByteString path, ModCollection collection, Utf8GamePath originalPath) => new($"|{collection.LocalId.Id}_{collection.ChangeCounter}_{originalPath.Path.Crc32:X8}_{DiscriminatorString}|{path}"); /// The base function shared by most file types. [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static FullPath CreateBase(ByteString path, ModCollection collection) + private static FullPath CreateBase(CiByteString path, ModCollection collection) => new($"|{collection.LocalId.Id}_{collection.ChangeCounter}_{DiscriminatorString}|{path}"); /// Read an additional data blurb and parse it into usable data for all file types but Materials. diff --git a/Penumbra/Interop/PathResolving/PathResolver.cs b/Penumbra/Interop/PathResolving/PathResolver.cs index 49035dc8..67ec4fc3 100644 --- a/Penumbra/Interop/PathResolving/PathResolver.cs +++ b/Penumbra/Interop/PathResolving/PathResolver.cs @@ -52,7 +52,6 @@ public class PathResolver : IDisposable, IService if (resourceType is ResourceType.Lvb or ResourceType.Lgb or ResourceType.Sgb) return (null, ResolveData.Invalid); - path = path.ToLower(); return category switch { // Only Interface collection. diff --git a/Penumbra/Interop/PathResolving/PathState.cs b/Penumbra/Interop/PathResolving/PathState.cs index bf9d1e25..60a61408 100644 --- a/Penumbra/Interop/PathResolving/PathState.cs +++ b/Penumbra/Interop/PathResolving/PathState.cs @@ -28,7 +28,7 @@ public sealed class PathState(CollectionResolver collectionResolver, MetaState m _internalResolve.Dispose(); } - public bool Consume(ByteString _, out ResolveData collection) + public bool Consume(CiByteString _, out ResolveData collection) { if (_resolveData.IsValueCreated) { diff --git a/Penumbra/Interop/Processing/AvfxPathPreProcessor.cs b/Penumbra/Interop/Processing/AvfxPathPreProcessor.cs index 56f693e6..2194354a 100644 --- a/Penumbra/Interop/Processing/AvfxPathPreProcessor.cs +++ b/Penumbra/Interop/Processing/AvfxPathPreProcessor.cs @@ -11,6 +11,6 @@ public sealed class AvfxPathPreProcessor : IPathPreProcessor public ResourceType Type => ResourceType.Avfx; - public FullPath? PreProcess(ResolveData resolveData, ByteString path, Utf8GamePath _, bool nonDefault, FullPath? resolved) + public FullPath? PreProcess(ResolveData resolveData, CiByteString path, Utf8GamePath _, bool nonDefault, FullPath? resolved) => nonDefault ? PathDataHandler.CreateAvfx(path, resolveData.ModCollection) : resolved; } diff --git a/Penumbra/Interop/Processing/FilePostProcessService.cs b/Penumbra/Interop/Processing/FilePostProcessService.cs index bba53c94..ecf78c69 100644 --- a/Penumbra/Interop/Processing/FilePostProcessService.cs +++ b/Penumbra/Interop/Processing/FilePostProcessService.cs @@ -10,7 +10,7 @@ namespace Penumbra.Interop.Processing; public interface IFilePostProcessor : IService { public ResourceType Type { get; } - public unsafe void PostProcess(ResourceHandle* resource, ByteString originalGamePath, ReadOnlySpan additionalData); + public unsafe void PostProcess(ResourceHandle* resource, CiByteString originalGamePath, ReadOnlySpan additionalData); } public unsafe class FilePostProcessService : IRequiredService, IDisposable @@ -30,7 +30,7 @@ public unsafe class FilePostProcessService : IRequiredService, IDisposable _resourceLoader.FileLoaded -= OnFileLoaded; } - private void OnFileLoaded(ResourceHandle* resource, ByteString path, bool returnValue, bool custom, + private void OnFileLoaded(ResourceHandle* resource, CiByteString path, bool returnValue, bool custom, ReadOnlySpan additionalData) { if (_processors.TryGetValue(resource->FileType, out var processor)) diff --git a/Penumbra/Interop/Processing/GamePathPreProcessService.cs b/Penumbra/Interop/Processing/GamePathPreProcessService.cs index 004b7168..65608ba0 100644 --- a/Penumbra/Interop/Processing/GamePathPreProcessService.cs +++ b/Penumbra/Interop/Processing/GamePathPreProcessService.cs @@ -11,7 +11,7 @@ public interface IPathPreProcessor : IService { public ResourceType Type { get; } - public FullPath? PreProcess(ResolveData resolveData, ByteString path, Utf8GamePath originalGamePath, bool nonDefault, FullPath? resolved); + public FullPath? PreProcess(ResolveData resolveData, CiByteString path, Utf8GamePath originalGamePath, bool nonDefault, FullPath? resolved); } public class GamePathPreProcessService : IService @@ -24,7 +24,7 @@ public class GamePathPreProcessService : IService } - public (FullPath? Path, ResolveData Data) PreProcess(ResolveData resolveData, ByteString path, bool nonDefault, ResourceType type, + public (FullPath? Path, ResolveData Data) PreProcess(ResolveData resolveData, CiByteString path, bool nonDefault, ResourceType type, FullPath? resolved, Utf8GamePath originalPath) { diff --git a/Penumbra/Interop/Processing/ImcFilePostProcessor.cs b/Penumbra/Interop/Processing/ImcFilePostProcessor.cs index 4a0ebe22..33a3941a 100644 --- a/Penumbra/Interop/Processing/ImcFilePostProcessor.cs +++ b/Penumbra/Interop/Processing/ImcFilePostProcessor.cs @@ -11,7 +11,7 @@ public sealed class ImcFilePostProcessor(CollectionStorage collections) : IFileP public ResourceType Type => ResourceType.Imc; - public unsafe void PostProcess(ResourceHandle* resource, ByteString originalGamePath, ReadOnlySpan additionalData) + public unsafe void PostProcess(ResourceHandle* resource, CiByteString originalGamePath, ReadOnlySpan additionalData) { if (!PathDataHandler.Read(additionalData, out var data) || data.Discriminator != PathDataHandler.Discriminator) return; diff --git a/Penumbra/Interop/Processing/ImcPathPreProcessor.cs b/Penumbra/Interop/Processing/ImcPathPreProcessor.cs index 907d7587..7030dd8d 100644 --- a/Penumbra/Interop/Processing/ImcPathPreProcessor.cs +++ b/Penumbra/Interop/Processing/ImcPathPreProcessor.cs @@ -11,7 +11,7 @@ public sealed class ImcPathPreProcessor : IPathPreProcessor public ResourceType Type => ResourceType.Imc; - public FullPath? PreProcess(ResolveData resolveData, ByteString path, Utf8GamePath originalGamePath, bool _, FullPath? resolved) + public FullPath? PreProcess(ResolveData resolveData, CiByteString path, Utf8GamePath originalGamePath, bool _, FullPath? resolved) => resolveData.ModCollection.MetaCache?.Imc.HasFile(originalGamePath.Path) ?? false ? PathDataHandler.CreateImc(path, resolveData.ModCollection) : resolved; diff --git a/Penumbra/Interop/Processing/MaterialFilePostProcessor.cs b/Penumbra/Interop/Processing/MaterialFilePostProcessor.cs index 02b5d46c..26956845 100644 --- a/Penumbra/Interop/Processing/MaterialFilePostProcessor.cs +++ b/Penumbra/Interop/Processing/MaterialFilePostProcessor.cs @@ -10,7 +10,7 @@ public sealed class MaterialFilePostProcessor //: IFilePostProcessor public ResourceType Type => ResourceType.Mtrl; - public unsafe void PostProcess(ResourceHandle* resource, ByteString originalGamePath, ReadOnlySpan additionalData) + public unsafe void PostProcess(ResourceHandle* resource, CiByteString originalGamePath, ReadOnlySpan additionalData) { if (!PathDataHandler.ReadMtrl(additionalData, out var data)) return; diff --git a/Penumbra/Interop/Processing/MtrlPathPreProcessor.cs b/Penumbra/Interop/Processing/MtrlPathPreProcessor.cs index 8fb2400b..603781ed 100644 --- a/Penumbra/Interop/Processing/MtrlPathPreProcessor.cs +++ b/Penumbra/Interop/Processing/MtrlPathPreProcessor.cs @@ -11,6 +11,6 @@ public sealed class MtrlPathPreProcessor : IPathPreProcessor public ResourceType Type => ResourceType.Mtrl; - public FullPath? PreProcess(ResolveData resolveData, ByteString path, Utf8GamePath originalGamePath, bool nonDefault, FullPath? resolved) + public FullPath? PreProcess(ResolveData resolveData, CiByteString path, Utf8GamePath originalGamePath, bool nonDefault, FullPath? resolved) => nonDefault ? PathDataHandler.CreateMtrl(path, resolveData.ModCollection, originalGamePath) : resolved; } diff --git a/Penumbra/Interop/Processing/TmbPathPreProcessor.cs b/Penumbra/Interop/Processing/TmbPathPreProcessor.cs index dd887819..0a7aa16f 100644 --- a/Penumbra/Interop/Processing/TmbPathPreProcessor.cs +++ b/Penumbra/Interop/Processing/TmbPathPreProcessor.cs @@ -11,6 +11,6 @@ public sealed class TmbPathPreProcessor : IPathPreProcessor public ResourceType Type => ResourceType.Tmb; - public FullPath? PreProcess(ResolveData resolveData, ByteString path, Utf8GamePath _, bool nonDefault, FullPath? resolved) + public FullPath? PreProcess(ResolveData resolveData, CiByteString path, Utf8GamePath _, bool nonDefault, FullPath? resolved) => nonDefault ? PathDataHandler.CreateTmb(path, resolveData.ModCollection) : resolved; } diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs index 678dd8a9..85b3284a 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs @@ -3,7 +3,6 @@ using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; -using Penumbra.Meta; using Penumbra.Meta.Files; using Penumbra.Meta.Manipulations; using Penumbra.String; @@ -99,7 +98,7 @@ internal partial record ResolveContext Span pathBuffer = stackalloc byte[260]; pathBuffer = AssembleMaterialPath(pathBuffer, modelPath.Path.Span, variant, fileName); - return Utf8GamePath.FromSpan(pathBuffer, out var path) ? path.Clone() : Utf8GamePath.Empty; + return Utf8GamePath.FromSpan(pathBuffer, MetaDataComputation.None, out var path) ? path.Clone() : Utf8GamePath.Empty; } [SkipLocalsInit] @@ -133,7 +132,7 @@ internal partial record ResolveContext if (weaponPosition >= 0) WriteZeroPaddedNumber(pathBuffer[(weaponPosition + 9)..(weaponPosition + 13)], mirroredSetId); - return Utf8GamePath.FromSpan(pathBuffer, out var path) ? path.Clone() : Utf8GamePath.Empty; + return Utf8GamePath.FromSpan(pathBuffer, MetaDataComputation.None, out var path) ? path.Clone() : Utf8GamePath.Empty; } } @@ -148,7 +147,7 @@ internal partial record ResolveContext Span pathBuffer = stackalloc byte[260]; pathBuffer = AssembleMaterialPath(pathBuffer, modelPath.Path.Span, variant, fileName); - return Utf8GamePath.FromSpan(pathBuffer, out var path) ? path.Clone() : Utf8GamePath.Empty; + return Utf8GamePath.FromSpan(pathBuffer, MetaDataComputation.None, out var path) ? path.Clone() : Utf8GamePath.Empty; } private unsafe byte ResolveMaterialVariant(ResourceHandle* imc, Variant variant) @@ -196,7 +195,7 @@ internal partial record ResolveContext private unsafe Utf8GamePath ResolveMaterialPathNative(byte* mtrlFileName) { - ByteString? path; + CiByteString? path; try { path = CharacterBase->ResolveMtrlPathAsByteString(SlotIndex, mtrlFileName); diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.cs b/Penumbra/Interop/ResourceTree/ResolveContext.cs index acb320d4..3fc1ae3c 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.cs @@ -45,25 +45,25 @@ internal unsafe partial record ResolveContext( public CharacterBase* CharacterBase => CharacterBasePointer.Value; - private static readonly ByteString ShpkPrefix = ByteString.FromSpanUnsafe("shader/sm5/shpk"u8, true, true, true); + private static readonly CiByteString ShpkPrefix = CiByteString.FromSpanUnsafe("shader/sm5/shpk"u8, true, true, true); private ModelType ModelType => CharacterBase->GetModelType(); - private ResourceNode? CreateNodeFromShpk(ShaderPackageResourceHandle* resourceHandle, ByteString gamePath) + private ResourceNode? CreateNodeFromShpk(ShaderPackageResourceHandle* resourceHandle, CiByteString gamePath) { if (resourceHandle == null) return null; if (gamePath.IsEmpty) return null; - if (!Utf8GamePath.FromByteString(ByteString.Join((byte)'/', ShpkPrefix, gamePath), out var path, false)) + if (!Utf8GamePath.FromByteString(CiByteString.Join((byte)'/', ShpkPrefix, gamePath), out var path)) return null; return GetOrCreateNode(ResourceType.Shpk, (nint)resourceHandle->ShaderPackage, &resourceHandle->ResourceHandle, path); } [SkipLocalsInit] - private ResourceNode? CreateNodeFromTex(TextureResourceHandle* resourceHandle, ByteString gamePath, bool dx11) + private ResourceNode? CreateNodeFromTex(TextureResourceHandle* resourceHandle, CiByteString gamePath, bool dx11) { if (resourceHandle == null) return null; @@ -81,7 +81,7 @@ internal unsafe partial record ResolveContext( prefixed[lastDirectorySeparator + 2] = (byte)'-'; gamePath.Span[(lastDirectorySeparator + 1)..].CopyTo(prefixed[(lastDirectorySeparator + 3)..]); - if (!Utf8GamePath.FromSpan(prefixed[..(gamePath.Length + 2)], out var tmp)) + if (!Utf8GamePath.FromSpan(prefixed[..(gamePath.Length + 2)], MetaDataComputation.None, out var tmp)) return null; path = tmp.Clone(); @@ -118,11 +118,11 @@ internal unsafe partial record ResolveContext( throw new ArgumentNullException(nameof(resourceHandle)); var fileName = (ReadOnlySpan)resourceHandle->FileName.AsSpan(); - var additionalData = ByteString.Empty; + var additionalData = CiByteString.Empty; if (PathDataHandler.Split(fileName, out fileName, out var data)) - additionalData = ByteString.FromSpanUnsafe(data, false).Clone(); + additionalData = CiByteString.FromSpanUnsafe(data, false).Clone(); - var fullPath = Utf8GamePath.FromSpan(fileName, out var p) ? new FullPath(p.Clone()) : FullPath.Empty; + var fullPath = Utf8GamePath.FromSpan(fileName, MetaDataComputation.None, out var p) ? new FullPath(p.Clone()) : FullPath.Empty; var node = new ResourceNode(type, objectAddress, (nint)resourceHandle, GetResourceHandleLength(resourceHandle), this) { @@ -222,7 +222,7 @@ internal unsafe partial record ResolveContext( return cached; var node = CreateNode(ResourceType.Mtrl, (nint)mtrl, &resource->ResourceHandle, path, false); - var shpkNode = CreateNodeFromShpk(resource->ShaderPackageResourceHandle, new ByteString(resource->ShpkName)); + var shpkNode = CreateNodeFromShpk(resource->ShaderPackageResourceHandle, new CiByteString(resource->ShpkName)); if (shpkNode != null) { if (Global.WithUiData) @@ -236,7 +236,7 @@ internal unsafe partial record ResolveContext( var alreadyProcessedSamplerIds = new HashSet(); for (var i = 0; i < resource->TextureCount; i++) { - var texNode = CreateNodeFromTex(resource->Textures[i].TextureResourceHandle, new ByteString(resource->TexturePath(i)), + var texNode = CreateNodeFromTex(resource->Textures[i].TextureResourceHandle, new CiByteString(resource->TexturePath(i)), resource->Textures[i].IsDX11); if (texNode == null) continue; diff --git a/Penumbra/Interop/ResourceTree/ResourceNode.cs b/Penumbra/Interop/ResourceTree/ResourceNode.cs index 9c911791..de43a874 100644 --- a/Penumbra/Interop/ResourceTree/ResourceNode.cs +++ b/Penumbra/Interop/ResourceTree/ResourceNode.cs @@ -15,7 +15,7 @@ public class ResourceNode : ICloneable public readonly nint ResourceHandle; public Utf8GamePath[] PossibleGamePaths; public FullPath FullPath; - public ByteString AdditionalData; + public CiByteString AdditionalData; public readonly ulong Length; public readonly List Children; internal ResolveContext? ResolveContext; @@ -26,9 +26,9 @@ public class ResourceNode : ICloneable set { if (value.IsEmpty) - PossibleGamePaths = Array.Empty(); + PossibleGamePaths = []; else - PossibleGamePaths = new[] { value }; + PossibleGamePaths = [value]; } } @@ -40,8 +40,8 @@ public class ResourceNode : ICloneable Type = type; ObjectAddress = objectAddress; ResourceHandle = resourceHandle; - PossibleGamePaths = Array.Empty(); - AdditionalData = ByteString.Empty; + PossibleGamePaths = []; + AdditionalData = CiByteString.Empty; Length = length; Children = new List(); ResolveContext = resolveContext; @@ -90,7 +90,7 @@ public class ResourceNode : ICloneable public readonly record struct UiData(string? Name, ChangedItemIcon Icon) { - public readonly UiData PrependName(string prefix) + public UiData PrependName(string prefix) => Name == null ? this : new UiData(prefix + Name, Icon); } } diff --git a/Penumbra/Interop/Services/DecalReverter.cs b/Penumbra/Interop/Services/DecalReverter.cs index 21b51fd2..3d5d7845 100644 --- a/Penumbra/Interop/Services/DecalReverter.cs +++ b/Penumbra/Interop/Services/DecalReverter.cs @@ -2,6 +2,7 @@ using FFXIVClientStructs.FFXIV.Client.System.Resource; using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.Interop.Hooks.ResourceLoading; +using Penumbra.String; using Penumbra.String.Classes; namespace Penumbra.Interop.Services; @@ -9,10 +10,10 @@ namespace Penumbra.Interop.Services; public sealed unsafe class DecalReverter : IDisposable { public static readonly Utf8GamePath DecalPath = - Utf8GamePath.FromSpan("chara/common/texture/decal_equip/_stigma.tex"u8, out var p) ? p : Utf8GamePath.Empty; + Utf8GamePath.FromSpan("chara/common/texture/decal_equip/_stigma.tex"u8, MetaDataComputation.All, out var p) ? p : Utf8GamePath.Empty; public static readonly Utf8GamePath TransparentPath = - Utf8GamePath.FromSpan("chara/common/texture/transparent.tex"u8, out var p) ? p : Utf8GamePath.Empty; + Utf8GamePath.FromSpan("chara/common/texture/transparent.tex"u8, MetaDataComputation.All, out var p) ? p : Utf8GamePath.Empty; private readonly CharacterUtility _utility; private readonly Structs.TextureResourceHandle* _decal; diff --git a/Penumbra/Interop/Structs/ResourceHandle.cs b/Penumbra/Interop/Structs/ResourceHandle.cs index 6e428f25..65550563 100644 --- a/Penumbra/Interop/Structs/ResourceHandle.cs +++ b/Penumbra/Interop/Structs/ResourceHandle.cs @@ -47,11 +47,11 @@ public unsafe struct ResourceHandle public ulong DataLength; } - public readonly ByteString FileName() + public readonly CiByteString FileName() => CsHandle.FileName.AsByteString(); public readonly bool GamePath(out Utf8GamePath path) - => Utf8GamePath.FromSpan(CsHandle.FileName.AsSpan(), out path); + => Utf8GamePath.FromSpan(CsHandle.FileName.AsSpan(), MetaDataComputation.All, out path); [FieldOffset(0x00)] public CsHandle.ResourceHandle CsHandle; diff --git a/Penumbra/Interop/Structs/StructExtensions.cs b/Penumbra/Interop/Structs/StructExtensions.cs index fc8b1c3d..9dd9a96d 100644 --- a/Penumbra/Interop/Structs/StructExtensions.cs +++ b/Penumbra/Interop/Structs/StructExtensions.cs @@ -6,48 +6,48 @@ namespace Penumbra.Interop.Structs; internal static class StructExtensions { - public static unsafe ByteString AsByteString(in this StdString str) - => ByteString.FromSpanUnsafe(str.AsSpan(), true); + public static CiByteString AsByteString(in this StdString str) + => CiByteString.FromSpanUnsafe(str.AsSpan(), true); - public static ByteString ResolveEidPathAsByteString(ref this CharacterBase character) + public static CiByteString ResolveEidPathAsByteString(ref this CharacterBase character) { Span pathBuffer = stackalloc byte[CharacterBase.PathBufferSize]; return ToOwnedByteString(character.ResolveEidPath(pathBuffer)); } - public static ByteString ResolveImcPathAsByteString(ref this CharacterBase character, uint slotIndex) + public static CiByteString ResolveImcPathAsByteString(ref this CharacterBase character, uint slotIndex) { Span pathBuffer = stackalloc byte[CharacterBase.PathBufferSize]; return ToOwnedByteString(character.ResolveImcPath(pathBuffer, slotIndex)); } - public static ByteString ResolveMdlPathAsByteString(ref this CharacterBase character, uint slotIndex) + public static CiByteString ResolveMdlPathAsByteString(ref this CharacterBase character, uint slotIndex) { Span pathBuffer = stackalloc byte[CharacterBase.PathBufferSize]; return ToOwnedByteString(character.ResolveMdlPath(pathBuffer, slotIndex)); } - public static unsafe ByteString ResolveMtrlPathAsByteString(ref this CharacterBase character, uint slotIndex, byte* mtrlFileName) + public static unsafe CiByteString ResolveMtrlPathAsByteString(ref this CharacterBase character, uint slotIndex, byte* mtrlFileName) { var pathBuffer = stackalloc byte[CharacterBase.PathBufferSize]; return ToOwnedByteString(character.ResolveMtrlPath(pathBuffer, CharacterBase.PathBufferSize, slotIndex, mtrlFileName)); } - public static ByteString ResolveSklbPathAsByteString(ref this CharacterBase character, uint partialSkeletonIndex) + public static CiByteString ResolveSklbPathAsByteString(ref this CharacterBase character, uint partialSkeletonIndex) { Span pathBuffer = stackalloc byte[CharacterBase.PathBufferSize]; return ToOwnedByteString(character.ResolveSklbPath(pathBuffer, partialSkeletonIndex)); } - public static ByteString ResolveSkpPathAsByteString(ref this CharacterBase character, uint partialSkeletonIndex) + public static CiByteString ResolveSkpPathAsByteString(ref this CharacterBase character, uint partialSkeletonIndex) { Span pathBuffer = stackalloc byte[CharacterBase.PathBufferSize]; return ToOwnedByteString(character.ResolveSkpPath(pathBuffer, partialSkeletonIndex)); } - private static unsafe ByteString ToOwnedByteString(byte* str) - => str == null ? ByteString.Empty : new ByteString(str).Clone(); + private static unsafe CiByteString ToOwnedByteString(byte* str) + => str == null ? CiByteString.Empty : new CiByteString(str).Clone(); - private static ByteString ToOwnedByteString(ReadOnlySpan str) - => str.Length == 0 ? ByteString.Empty : ByteString.FromSpanUnsafe(str, true).Clone(); + private static CiByteString ToOwnedByteString(ReadOnlySpan str) + => str.Length == 0 ? CiByteString.Empty : CiByteString.FromSpanUnsafe(str, true).Clone(); } diff --git a/Penumbra/Mods/ModCreator.cs b/Penumbra/Mods/ModCreator.cs index 546f5f5c..0f4972e3 100644 --- a/Penumbra/Mods/ModCreator.cs +++ b/Penumbra/Mods/ModCreator.cs @@ -270,7 +270,7 @@ public partial class ModCreator( public MultiSubMod CreateSubMod(DirectoryInfo baseFolder, DirectoryInfo optionFolder, OptionList option, ModPriority priority) { var list = optionFolder.EnumerateNonHiddenFiles() - .Select(f => (Utf8GamePath.FromFile(f, optionFolder, out var gamePath, true), gamePath, new FullPath(f))) + .Select(f => (Utf8GamePath.FromFile(f, optionFolder, out var gamePath), gamePath, new FullPath(f))) .Where(t => t.Item1); var mod = MultiSubMod.WithoutGroup(option.Name, option.Description, priority); @@ -291,7 +291,7 @@ public partial class ModCreator( ReloadMod(mod, false, out _); foreach (var file in mod.FindUnusedFiles()) { - if (Utf8GamePath.FromFile(new FileInfo(file.FullName), directory, out var gamePath, true)) + if (Utf8GamePath.FromFile(new FileInfo(file.FullName), directory, out var gamePath)) mod.Default.Files.TryAdd(gamePath, file); } diff --git a/Penumbra/Mods/SubMods/SubMod.cs b/Penumbra/Mods/SubMods/SubMod.cs index a8c37369..f6b1be96 100644 --- a/Penumbra/Mods/SubMods/SubMod.cs +++ b/Penumbra/Mods/SubMods/SubMod.cs @@ -52,7 +52,7 @@ public static class SubMod if (files != null) foreach (var property in files.Properties()) { - if (Utf8GamePath.FromString(property.Name, out var p, true)) + if (Utf8GamePath.FromString(property.Name, out var p)) data.Files.TryAdd(p, new FullPath(basePath, property.Value.ToObject())); } @@ -60,7 +60,7 @@ public static class SubMod if (swaps != null) foreach (var property in swaps.Properties()) { - if (Utf8GamePath.FromString(property.Name, out var p, true)) + if (Utf8GamePath.FromString(property.Name, out var p)) data.FileSwaps.TryAdd(p, new FullPath(property.Value.ToObject()!)); } diff --git a/Penumbra/UI/AdvancedWindow/FileEditor.cs b/Penumbra/UI/AdvancedWindow/FileEditor.cs index 2c6ac170..c783e17f 100644 --- a/Penumbra/UI/AdvancedWindow/FileEditor.cs +++ b/Penumbra/UI/AdvancedWindow/FileEditor.cs @@ -6,6 +6,7 @@ using OtterGui; using OtterGui.Classes; using OtterGui.Compression; using OtterGui.Raii; +using OtterGui.Text; using OtterGui.Widgets; using Penumbra.GameData.Files; using Penumbra.Mods.Editor; @@ -98,7 +99,7 @@ public class FileEditor( _inInput = ImGui.IsItemActive(); if (ImGui.IsItemDeactivatedAfterEdit() && _defaultPath.Length > 0) { - _isDefaultPathUtf8Valid = Utf8GamePath.FromString(_defaultPath, out _defaultPathUtf8, true); + _isDefaultPathUtf8Valid = Utf8GamePath.FromString(_defaultPath, out _defaultPathUtf8); _quickImport = null; fileDialog.Reset(); try @@ -306,7 +307,7 @@ public class FileEditor( foreach (var (option, gamePath) in file.SubModUsage) { ImGui.TableNextColumn(); - UiHelpers.Text(gamePath.Path); + ImUtf8.Text(gamePath.Path.Span); ImGui.TableNextColumn(); using var color = ImRaii.PushColor(ImGuiCol.Text, ColorId.ItemId.Value()); ImGui.TextUnformatted(option.GetFullName()); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs index 107c56e6..ffa7473d 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Files.cs @@ -209,7 +209,7 @@ public partial class ModEditWindow if (ImGui.IsItemDeactivatedAfterEdit()) { - if (Utf8GamePath.FromString(_gamePathEdit, out var path, false)) + if (Utf8GamePath.FromString(_gamePathEdit, out var path)) _editor.FileEditor.SetGamePath(_editor.Option!, _fileIdx, _pathIdx, path); _fileIdx = -1; @@ -217,7 +217,7 @@ public partial class ModEditWindow } else if (_fileIdx == i && _pathIdx == j - && (!Utf8GamePath.FromString(_gamePathEdit, out var path, false) + && (!Utf8GamePath.FromString(_gamePathEdit, out var path) || !path.IsEmpty && !path.Equals(gamePath) && !_editor.FileEditor.CanAddGamePath(path))) { ImGui.SameLine(); @@ -241,7 +241,7 @@ public partial class ModEditWindow if (ImGui.IsItemDeactivatedAfterEdit()) { - if (Utf8GamePath.FromString(_gamePathEdit, out var path, false) && !path.IsEmpty) + if (Utf8GamePath.FromString(_gamePathEdit, out var path) && !path.IsEmpty) _editor.FileEditor.SetGamePath(_editor.Option!, _fileIdx, _pathIdx, path); _fileIdx = -1; @@ -249,7 +249,7 @@ public partial class ModEditWindow } else if (_fileIdx == i && _pathIdx == -1 - && (!Utf8GamePath.FromString(_gamePathEdit, out var path, false) + && (!Utf8GamePath.FromString(_gamePathEdit, out var path) || !path.IsEmpty && !_editor.FileEditor.CanAddGamePath(path))) { ImGui.SameLine(); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs index cd7aca9d..a50599a1 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs @@ -25,7 +25,7 @@ public partial class ModEditWindow { private const int ShpkPrefixLength = 16; - private static readonly ByteString ShpkPrefix = ByteString.FromSpanUnsafe("shader/sm5/shpk/"u8, true, true, true); + private static readonly CiByteString ShpkPrefix = CiByteString.FromSpanUnsafe("shader/sm5/shpk/"u8, true, true, true); private readonly ModEditWindow _edit; public readonly MtrlFile Mtrl; @@ -77,7 +77,7 @@ public partial class ModEditWindow public FullPath FindAssociatedShpk(out string defaultPath, out Utf8GamePath defaultGamePath) { defaultPath = GamePaths.Shader.ShpkPath(Mtrl.ShaderPackage.Name); - if (!Utf8GamePath.FromString(defaultPath, out defaultGamePath, true)) + if (!Utf8GamePath.FromString(defaultPath, out defaultGamePath)) return FullPath.Empty; return _edit.FindBestMatch(defaultGamePath); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs index b05bcac2..b436448f 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs @@ -271,7 +271,7 @@ public partial class ModEditWindow private byte[]? ReadFile(string path) { // TODO: if cross-collection lookups are turned off, this conversion can be skipped - if (!Utf8GamePath.FromString(path, out var utf8Path, true)) + if (!Utf8GamePath.FromString(path, out var utf8Path)) throw new Exception($"Resolved path {path} could not be converted to a game path."); var resolvedPath = _edit._activeCollections.Current.ResolvePath(utf8Path) ?? new FullPath(utf8Path); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs index a22c10ad..017478a7 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs @@ -16,7 +16,7 @@ namespace Penumbra.UI.AdvancedWindow; public partial class ModEditWindow { - private static readonly ByteString DisassemblyLabel = ByteString.FromSpanUnsafe("##disassembly"u8, true, true, true); + private static readonly CiByteString DisassemblyLabel = CiByteString.FromSpanUnsafe("##disassembly"u8, true, true, true); private readonly FileEditor _shaderPackageTab; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index b0e9af7f..0d3dce8c 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -562,7 +562,7 @@ public partial class ModEditWindow : Window, IDisposable, IUiService return new FullPath(path); } - private HashSet FindPathsStartingWith(ByteString prefix) + private HashSet FindPathsStartingWith(CiByteString prefix) { var ret = new HashSet(); diff --git a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs index 7315f136..c47414b9 100644 --- a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs +++ b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs @@ -84,7 +84,8 @@ public class ResourceTreeViewer using (var c = ImRaii.PushColor(ImGuiCol.Text, CategoryColor(category).Value())) { - var isOpen = ImGui.CollapsingHeader($"{(_incognito.IncognitoMode ? tree.AnonymizedName : tree.Name)}###{index}", index == 0 ? ImGuiTreeNodeFlags.DefaultOpen : 0); + var isOpen = ImGui.CollapsingHeader($"{(_incognito.IncognitoMode ? tree.AnonymizedName : tree.Name)}###{index}", + index == 0 ? ImGuiTreeNodeFlags.DefaultOpen : 0); if (debugMode) { using var _ = ImRaii.PushFont(UiBuilder.MonoFont); @@ -149,7 +150,9 @@ public class ResourceTreeViewer var filterChanged = false; ImGui.SetCursorPosY(ImGui.GetCursorPosY() - yOffset); using (ImRaii.Child("##typeFilter", new Vector2(ImGui.GetContentRegionAvail().X, ChangedItemDrawer.TypeFilterIconSize.Y))) + { filterChanged |= _changedItemDrawer.DrawTypeFilter(ref _typeFilter); + } var fieldWidth = (ImGui.GetContentRegionAvail().X - checkSpacing * 2.0f - ImGui.GetFrameHeightWithSpacing()) / 2.0f; ImGui.SetNextItemWidth(fieldWidth); @@ -181,7 +184,8 @@ public class ResourceTreeViewer } }); - private void DrawNodes(IEnumerable resourceNodes, int level, nint pathHash, ChangedItemDrawer.ChangedItemIcon parentFilterIcon) + private void DrawNodes(IEnumerable resourceNodes, int level, nint pathHash, + ChangedItemDrawer.ChangedItemIcon parentFilterIcon) { var debugMode = _config.DebugMode; var frameHeight = ImGui.GetFrameHeight(); @@ -196,9 +200,9 @@ public class ResourceTreeViewer return true; return node.Name != null && node.Name.Contains(_nodeFilter, StringComparison.OrdinalIgnoreCase) - || node.FullPath.FullName.Contains(_nodeFilter, StringComparison.OrdinalIgnoreCase) - || node.FullPath.InternalName.ToString().Contains(_nodeFilter, StringComparison.OrdinalIgnoreCase) - || Array.Exists(node.PossibleGamePaths, path => path.Path.ToString().Contains(_nodeFilter, StringComparison.OrdinalIgnoreCase)); + || node.FullPath.FullName.Contains(_nodeFilter, StringComparison.OrdinalIgnoreCase) + || node.FullPath.InternalName.ToString().Contains(_nodeFilter, StringComparison.OrdinalIgnoreCase) + || Array.Exists(node.PossibleGamePaths, path => path.Path.ToString().Contains(_nodeFilter, StringComparison.OrdinalIgnoreCase)); } NodeVisibility CalculateNodeVisibility(nint nodePathHash, ResourceNode node, ChangedItemDrawer.ChangedItemIcon parentFilterIcon) @@ -226,10 +230,11 @@ public class ResourceTreeViewer visibility = CalculateNodeVisibility(nodePathHash, node, parentFilterIcon); _filterCache.Add(nodePathHash, visibility); } + return visibility; } - string GetAdditionalDataSuffix(ByteString data) + string GetAdditionalDataSuffix(CiByteString data) => !debugMode || data.IsEmpty ? string.Empty : $"\n\nAdditional Data: {data}"; foreach (var (resourceNode, index) in resourceNodes.WithIndex()) @@ -252,8 +257,9 @@ public class ResourceTreeViewer var unfolded = _unfolded.Contains(nodePathHash); using (var indent = ImRaii.PushIndent(level)) { - var hasVisibleChildren = resourceNode.Children.Any(child => GetNodeVisibility(unchecked(nodePathHash * 31 + child.ResourceHandle), child, filterIcon) != NodeVisibility.Hidden); - var unfoldable = hasVisibleChildren && visibility != NodeVisibility.DescendentsOnly; + var hasVisibleChildren = resourceNode.Children.Any(child + => GetNodeVisibility(unchecked(nodePathHash * 31 + child.ResourceHandle), child, filterIcon) != NodeVisibility.Hidden); + var unfoldable = hasVisibleChildren && visibility != NodeVisibility.DescendentsOnly; if (unfoldable) { using var font = ImRaii.PushFont(UiBuilder.IconFont); @@ -317,13 +323,15 @@ public class ResourceTreeViewer ImGui.Selectable(resourceNode.FullPath.ToPath(), false, 0, new Vector2(ImGui.GetContentRegionAvail().X, cellHeight)); if (ImGui.IsItemClicked()) ImGui.SetClipboardText(resourceNode.FullPath.ToPath()); - ImGuiUtil.HoverTooltip($"{resourceNode.FullPath.ToPath()}\n\nClick to copy to clipboard.{GetAdditionalDataSuffix(resourceNode.AdditionalData)}"); + ImGuiUtil.HoverTooltip( + $"{resourceNode.FullPath.ToPath()}\n\nClick to copy to clipboard.{GetAdditionalDataSuffix(resourceNode.AdditionalData)}"); } else { ImGui.Selectable("(unavailable)", false, ImGuiSelectableFlags.Disabled, new Vector2(ImGui.GetContentRegionAvail().X, cellHeight)); - ImGuiUtil.HoverTooltip($"The actual path to this file is unavailable.\nIt may be managed by another plug-in.{GetAdditionalDataSuffix(resourceNode.AdditionalData)}"); + ImGuiUtil.HoverTooltip( + $"The actual path to this file is unavailable.\nIt may be managed by another plug-in.{GetAdditionalDataSuffix(resourceNode.AdditionalData)}"); } mutedColor.Dispose(); diff --git a/Penumbra/UI/Knowledge/KnowledgeWindow.cs b/Penumbra/UI/Knowledge/KnowledgeWindow.cs index b14949de..f831975b 100644 --- a/Penumbra/UI/Knowledge/KnowledgeWindow.cs +++ b/Penumbra/UI/Knowledge/KnowledgeWindow.cs @@ -1,7 +1,5 @@ -using System.Text.Unicode; using Dalamud.Interface.Utility.Raii; using Dalamud.Interface.Windowing; -using Dalamud.Memory; using ImGuiNET; using OtterGui.Services; using OtterGui.Text; diff --git a/Penumbra/UI/ResourceWatcher/Record.cs b/Penumbra/UI/ResourceWatcher/Record.cs index 0fc51f26..b69d9944 100644 --- a/Penumbra/UI/ResourceWatcher/Record.cs +++ b/Penumbra/UI/ResourceWatcher/Record.cs @@ -18,8 +18,8 @@ public enum RecordType : byte internal unsafe struct Record { public DateTime Time; - public ByteString Path; - public ByteString OriginalPath; + public CiByteString Path; + public CiByteString OriginalPath; public string AssociatedGameObject; public ModCollection? Collection; public ResourceHandle* Handle; @@ -32,12 +32,12 @@ internal unsafe struct Record public OptionalBool CustomLoad; public LoadState LoadState; - public static Record CreateRequest(ByteString path, bool sync) + public static Record CreateRequest(CiByteString path, bool sync) => new() { Time = DateTime.UtcNow, Path = path.IsOwned ? path : path.Clone(), - OriginalPath = ByteString.Empty, + OriginalPath = CiByteString.Empty, Collection = null, Handle = null, ResourceType = ResourceExtensions.Type(path).ToFlag(), @@ -51,7 +51,7 @@ internal unsafe struct Record LoadState = LoadState.None, }; - public static Record CreateDefaultLoad(ByteString path, ResourceHandle* handle, ModCollection collection, string associatedGameObject) + public static Record CreateDefaultLoad(CiByteString path, ResourceHandle* handle, ModCollection collection, string associatedGameObject) { path = path.IsOwned ? path : path.Clone(); return new Record @@ -73,7 +73,7 @@ internal unsafe struct Record }; } - public static Record CreateLoad(ByteString path, ByteString originalPath, ResourceHandle* handle, ModCollection collection, + public static Record CreateLoad(CiByteString path, CiByteString originalPath, ResourceHandle* handle, ModCollection collection, string associatedGameObject) => new() { @@ -100,7 +100,7 @@ internal unsafe struct Record { Time = DateTime.UtcNow, Path = path, - OriginalPath = ByteString.Empty, + OriginalPath = CiByteString.Empty, Collection = null, Handle = handle, ResourceType = handle->FileType.ToFlag(), @@ -115,12 +115,12 @@ internal unsafe struct Record }; } - public static Record CreateFileLoad(ByteString path, ResourceHandle* handle, bool ret, bool custom) + public static Record CreateFileLoad(CiByteString path, ResourceHandle* handle, bool ret, bool custom) => new() { Time = DateTime.UtcNow, Path = path.IsOwned ? path : path.Clone(), - OriginalPath = ByteString.Empty, + OriginalPath = CiByteString.Empty, Collection = null, Handle = handle, ResourceType = handle->FileType.ToFlag(), diff --git a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs index 14d69489..6f1ce9cf 100644 --- a/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs +++ b/Penumbra/UI/ResourceWatcher/ResourceWatcher.cs @@ -163,7 +163,7 @@ public sealed class ResourceWatcher : IDisposable, ITab, IUiService } } - private bool FilterMatch(ByteString path, out string match) + private bool FilterMatch(CiByteString path, out string match) { match = path.ToString(); return _logFilter.Length == 0 || (_logRegex?.IsMatch(match) ?? false) || match.Contains(_logFilter, StringComparison.OrdinalIgnoreCase); @@ -255,7 +255,7 @@ public sealed class ResourceWatcher : IDisposable, ITab, IUiService _newRecords.Enqueue(record); } - private unsafe void OnFileLoaded(ResourceHandle* resource, ByteString path, bool success, bool custom, ReadOnlySpan _) + private unsafe void OnFileLoaded(ResourceHandle* resource, CiByteString path, bool success, bool custom, ReadOnlySpan _) { if (_ephemeral.EnableResourceLogging && FilterMatch(path, out var match)) Penumbra.Log.Information( diff --git a/Penumbra/UI/ResourceWatcher/ResourceWatcherTable.cs b/Penumbra/UI/ResourceWatcher/ResourceWatcherTable.cs index b47574d0..33e301ae 100644 --- a/Penumbra/UI/ResourceWatcher/ResourceWatcherTable.cs +++ b/Penumbra/UI/ResourceWatcher/ResourceWatcherTable.cs @@ -50,7 +50,7 @@ internal sealed class ResourceWatcherTable : Table => DrawByteString(item.Path, 280 * UiHelpers.Scale); } - private static unsafe void DrawByteString(ByteString path, float length) + private static unsafe void DrawByteString(CiByteString path, float length) { Vector2 vec; ImGuiNative.igCalcTextSize(&vec, path.Path, path.Path + path.Length, 0, 0); @@ -61,7 +61,7 @@ internal sealed class ResourceWatcherTable : Table else { var fileName = path.LastIndexOf((byte)'/'); - ByteString shortPath; + CiByteString shortPath; if (fileName != -1) { using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, new Vector2(2 * UiHelpers.Scale)); diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index 4966dd64..a1e9da03 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -15,6 +15,7 @@ using Microsoft.Extensions.DependencyInjection; using OtterGui; using OtterGui.Classes; using OtterGui.Services; +using OtterGui.Text; using OtterGui.Widgets; using Penumbra.Api; using Penumbra.Collections.Manager; @@ -402,6 +403,33 @@ public class DebugTab : Window, ITab, IUiService } } } + + using (var tree = ImUtf8.TreeNode("String Memory"u8)) + { + if (tree) + { + using (ImUtf8.Group()) + { + ImUtf8.Text("Currently Allocated Strings"u8); + ImUtf8.Text("Total Allocated Strings"u8); + ImUtf8.Text("Free'd Allocated Strings"u8); + ImUtf8.Text("Currently Allocated Bytes"u8); + ImUtf8.Text("Total Allocated Bytes"u8); + ImUtf8.Text("Free'd Allocated Bytes"u8); + } + + ImGui.SameLine(); + using (ImUtf8.Group()) + { + ImUtf8.Text($"{PenumbraStringMemory.CurrentStrings}"); + ImUtf8.Text($"{PenumbraStringMemory.AllocatedStrings}"); + ImUtf8.Text($"{PenumbraStringMemory.FreedStrings}"); + ImUtf8.Text($"{PenumbraStringMemory.CurrentBytes}"); + ImUtf8.Text($"{PenumbraStringMemory.AllocatedBytes}"); + ImUtf8.Text($"{PenumbraStringMemory.FreedBytes}"); + } + } + } } private void DrawPerformanceTab() diff --git a/Penumbra/UI/Tabs/EffectiveTab.cs b/Penumbra/UI/Tabs/EffectiveTab.cs index e0cab43f..ecf9a886 100644 --- a/Penumbra/UI/Tabs/EffectiveTab.cs +++ b/Penumbra/UI/Tabs/EffectiveTab.cs @@ -4,6 +4,7 @@ using OtterGui; using OtterGui.Classes; using OtterGui.Raii; using OtterGui.Services; +using OtterGui.Text; using OtterGui.Widgets; using Penumbra.Collections; using Penumbra.Collections.Cache; @@ -134,12 +135,12 @@ public class EffectiveTab(CollectionManager collectionManager, CollectionSelectH { var (path, name) = pair; ImGui.TableNextColumn(); - UiHelpers.CopyOnClickSelectable(path.Path); + ImUtf8.CopyOnClickSelectable(path.Path.Span); ImGui.TableNextColumn(); ImGuiUtil.PrintIcon(FontAwesomeIcon.LongArrowAltLeft); ImGui.TableNextColumn(); - UiHelpers.CopyOnClickSelectable(name.Path.InternalName); + ImUtf8.CopyOnClickSelectable(name.Path.InternalName.Span); ImGuiUtil.HoverTooltip($"\nChanged by {name.Mod.Name}."); } From 4b9870f09089d58e9171fa39511f93e7c8c9cbc0 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 31 Jul 2024 23:09:37 +0200 Subject: [PATCH 365/433] Fix some OtterGui changes. --- OtterGui | 2 +- Penumbra.GameData | 2 +- .../ModEditWindow.Materials.MtrlTab.cs | 2 +- .../UI/ModsTab/Groups/ImcModGroupEditDrawer.cs | 2 +- Penumbra/UI/ModsTab/ImcManipulationDrawer.cs | 14 -------------- Penumbra/UI/Tabs/ResourceTab.cs | 4 ++-- 6 files changed, 6 insertions(+), 20 deletions(-) delete mode 100644 Penumbra/UI/ModsTab/ImcManipulationDrawer.cs diff --git a/OtterGui b/OtterGui index 33ffd7cb..b0464b7f 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 33ffd7cba3e487e98e55adca1677354078089943 +Subproject commit b0464b7f215a0db1393e600968c6666307a3ae05 diff --git a/Penumbra.GameData b/Penumbra.GameData index d8ebd63c..75582ece 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit d8ebd63cec1ac12ea547fd37b6c32bdf9b3f57d1 +Subproject commit 75582ece58e6ee311074ff4ecaa68b804677878c diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs index a50599a1..29fd7531 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs @@ -405,7 +405,7 @@ public partial class ModEditWindow } var fcGroup = FindOrAddGroup(Constants, "Further Constants"); - foreach (var (start, end) in handledElements.Ranges(true)) + foreach (var (start, end) in handledElements.Ranges(complement:true)) { if ((shpkConstant.ByteOffset & 0x3) == 0) { diff --git a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs index 5d10febd..bbb5e54e 100644 --- a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs @@ -140,7 +140,7 @@ public readonly struct ImcModGroupEditDrawer(ModGroupEditDrawer editor, ImcModGr var value = (mask & (1 << i)) != 0; using (ImRaii.Disabled(!cache.CanChange(i))) { - if (ImUtf8.Checkbox(TerminatedByteString.Empty, ref value)) + if (ImUtf8.Checkbox(""u8, ref value)) { if (data is ImcModGroup g) editor.ChangeDefaultAttribute(g, cache, i, value); diff --git a/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs b/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs deleted file mode 100644 index 1291f568..00000000 --- a/Penumbra/UI/ModsTab/ImcManipulationDrawer.cs +++ /dev/null @@ -1,14 +0,0 @@ -using ImGuiNET; -using OtterGui.Raii; -using OtterGui.Text; -using Penumbra.GameData.Enums; -using Penumbra.GameData.Structs; -using Penumbra.Meta.Manipulations; -using Penumbra.UI.Classes; - -namespace Penumbra.UI.ModsTab; - -public static class ImcManipulationDrawer -{ - -} diff --git a/Penumbra/UI/Tabs/ResourceTab.cs b/Penumbra/UI/Tabs/ResourceTab.cs index a4dbba2f..c54e3433 100644 --- a/Penumbra/UI/Tabs/ResourceTab.cs +++ b/Penumbra/UI/Tabs/ResourceTab.cs @@ -81,7 +81,7 @@ public class ResourceTab(Configuration config, ResourceManagerService resourceMa return; var address = $"0x{(ulong)r:X}"; - ImGuiUtil.TextNextColumn($"0x{hash:X8}"); + ImGuiUtil.DrawTableColumn($"0x{hash:X8}"); ImGui.TableNextColumn(); ImGuiUtil.CopyOnClickSelectable(address); @@ -101,7 +101,7 @@ public class ResourceTab(Configuration config, ResourceManagerService resourceMa ImGuiUtil.HoverTooltip("Click to copy byte-wise file data to clipboard, if any."); - ImGuiUtil.TextNextColumn(r->RefCount.ToString()); + ImGuiUtil.DrawTableColumn(r->RefCount.ToString()); }); } From 67a220f821afac69aeff24bc8e49ec7cc3dcb1b1 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 31 Jul 2024 23:24:59 +0200 Subject: [PATCH 366/433] Add context menu to change mod state from Collections tab. --- Penumbra/UI/ModsTab/ModPanelCollectionsTab.cs | 86 ++++++++++++++----- 1 file changed, 66 insertions(+), 20 deletions(-) diff --git a/Penumbra/UI/ModsTab/ModPanelCollectionsTab.cs b/Penumbra/UI/ModsTab/ModPanelCollectionsTab.cs index 9f37f847..b7648428 100644 --- a/Penumbra/UI/ModsTab/ModPanelCollectionsTab.cs +++ b/Penumbra/UI/ModsTab/ModPanelCollectionsTab.cs @@ -3,6 +3,7 @@ using ImGuiNET; using OtterGui; using OtterGui.Raii; using OtterGui.Services; +using OtterGui.Text; using OtterGui.Widgets; using Penumbra.Collections; using Penumbra.Collections.Manager; @@ -11,9 +12,16 @@ using Penumbra.UI.Classes; namespace Penumbra.UI.ModsTab; -public class ModPanelCollectionsTab(CollectionStorage storage, ModFileSystemSelector selector) : ITab, IUiService +public class ModPanelCollectionsTab(CollectionManager manager, ModFileSystemSelector selector) : ITab, IUiService { - private readonly List<(ModCollection, ModCollection, uint, string)> _cache = []; + private enum ModState + { + Enabled, + Disabled, + Unconfigured, + } + + private readonly List<(ModCollection, ModCollection, uint, ModState)> _cache = []; public ReadOnlySpan Label => "Collections"u8; @@ -23,45 +31,83 @@ public class ModPanelCollectionsTab(CollectionStorage storage, ModFileSystemSele var (direct, inherited) = CountUsage(selector.Selected!); ImGui.NewLine(); if (direct == 1) - ImGui.TextUnformatted("This Mod is directly configured in 1 collection."); + ImUtf8.Text("This Mod is directly configured in 1 collection."u8); else if (direct == 0) - ImGuiUtil.TextColored(Colors.RegexWarningBorder, "This mod is entirely unused."); + ImUtf8.Text("This mod is entirely unused."u8, Colors.RegexWarningBorder); else - ImGui.TextUnformatted($"This Mod is directly configured in {direct} collections."); + ImUtf8.Text($"This Mod is directly configured in {direct} collections."); if (inherited > 0) - ImGui.TextUnformatted( - $"It is also implicitly used in {inherited} {(inherited == 1 ? "collection" : "collections")} through inheritance."); + ImUtf8.Text($"It is also implicitly used in {inherited} {(inherited == 1 ? "collection" : "collections")} through inheritance."); ImGui.NewLine(); ImGui.Separator(); ImGui.NewLine(); - using var table = ImRaii.Table("##modCollections", 3, ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg); + using var table = ImUtf8.Table("##modCollections"u8, 3, ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg); if (!table) return; - var size = ImGui.CalcTextSize("Unconfigured").X + 20 * ImGuiHelpers.GlobalScale; + var size = ImUtf8.CalcTextSize(ToText(ModState.Unconfigured)).X + 20 * ImGuiHelpers.GlobalScale; var collectionSize = 200 * ImGuiHelpers.GlobalScale; ImGui.TableSetupColumn("Collection", ImGuiTableColumnFlags.WidthFixed, collectionSize); ImGui.TableSetupColumn("State", ImGuiTableColumnFlags.WidthFixed, size); ImGui.TableSetupColumn("Inherited From", ImGuiTableColumnFlags.WidthFixed, collectionSize); ImGui.TableHeadersRow(); - foreach (var (collection, parent, color, text) in _cache) + foreach (var ((collection, parent, color, state), idx) in _cache.WithIndex()) { - ImGui.TableNextColumn(); - ImGui.TextUnformatted(collection.Name); + using var id = ImUtf8.PushId(idx); + ImUtf8.DrawTableColumn(collection.Name); ImGui.TableNextColumn(); - using (var c = ImRaii.PushColor(ImGuiCol.Text, color)) + ImUtf8.Text(ToText(state), color); + + using (var context = ImUtf8.PopupContextItem("Context"u8, ImGuiPopupFlags.MouseButtonRight)) { - ImGui.TextUnformatted(text); + if (context) + { + ImUtf8.Text(collection.Name); + ImGui.Separator(); + using (ImRaii.Disabled(state is ModState.Enabled && parent == collection)) + { + if (ImUtf8.MenuItem("Enable"u8)) + { + if (parent != collection) + manager.Editor.SetModInheritance(collection, selector.Selected!, false); + manager.Editor.SetModState(collection, selector.Selected!, true); + } + } + + using (ImRaii.Disabled(state is ModState.Disabled && parent == collection)) + { + if (ImUtf8.MenuItem("Disable"u8)) + { + if (parent != collection) + manager.Editor.SetModInheritance(collection, selector.Selected!, false); + manager.Editor.SetModState(collection, selector.Selected!, false); + } + } + + using (ImRaii.Disabled(parent != collection)) + { + if (ImUtf8.MenuItem("Inherit"u8)) + manager.Editor.SetModInheritance(collection, selector.Selected!, true); + } + } } - ImGui.TableNextColumn(); - ImGui.TextUnformatted(parent == collection ? string.Empty : parent.Name); + ImUtf8.DrawTableColumn(parent == collection ? string.Empty : parent.Name); } } + private static ReadOnlySpan ToText(ModState state) + => state switch + { + ModState.Unconfigured => "Unconfigured"u8, + ModState.Enabled => "Enabled"u8, + ModState.Disabled => "Disabled"u8, + _ => "Unknown"u8, + }; + private (int Direct, int Inherited) CountUsage(Mod mod) { _cache.Clear(); @@ -72,14 +118,14 @@ public class ModPanelCollectionsTab(CollectionStorage storage, ModFileSystemSele var disInherited = ColorId.InheritedDisabledMod.Value(); var directCount = 0; var inheritedCount = 0; - foreach (var collection in storage) + foreach (var collection in manager.Storage) { var (settings, parent) = collection[mod.Index]; var (color, text) = settings == null - ? (undefined, "Unconfigured") + ? (undefined, ModState.Unconfigured) : settings.Enabled - ? (parent == collection ? enabled : inherited, "Enabled") - : (parent == collection ? disabled : disInherited, "Disabled"); + ? (parent == collection ? enabled : inherited, ModState.Enabled) + : (parent == collection ? disabled : disInherited, ModState.Disabled); _cache.Add((collection, parent, color, text)); if (color == enabled) From 9e15865a99fd78240943f6d11bd784c2c122ca1a Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 1 Aug 2024 16:37:31 +0200 Subject: [PATCH 367/433] Fix some further issues with empty byte strings. --- OtterGui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OtterGui b/OtterGui index b0464b7f..2b79faac 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit b0464b7f215a0db1393e600968c6666307a3ae05 +Subproject commit 2b79faacff30a31e9ad4b0a3c5d57ffd6e34cfa4 From a308fb9f779acf939191cf3c10c8468a440d7ba9 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 1 Aug 2024 16:40:37 +0200 Subject: [PATCH 368/433] Allow hook overrides. --- .../Animation/ApricotListenerSoundPlay.cs | 2 +- .../Animation/CharacterBaseLoadAnimation.cs | 3 +- Penumbra/Interop/Hooks/Animation/Dismount.cs | 2 +- .../Interop/Hooks/Animation/LoadAreaVfx.cs | 6 +- .../Hooks/Animation/LoadCharacterSound.cs | 2 +- .../Hooks/Animation/LoadCharacterVfx.cs | 2 +- .../Hooks/Animation/LoadTimelineResources.cs | 2 +- .../Interop/Hooks/Animation/PlayFootstep.cs | 2 +- .../Hooks/Animation/ScheduleClipUpdate.cs | 2 +- .../Interop/Hooks/Animation/SomeActionLoad.cs | 2 +- .../Hooks/Animation/SomeMountAnimation.cs | 2 +- .../Interop/Hooks/Animation/SomePapLoad.cs | 2 +- .../Hooks/Animation/SomeParasolAnimation.cs | 2 +- Penumbra/Interop/Hooks/HookSettings.cs | 144 ++++++++++++++++-- .../Interop/Hooks/Meta/CalculateHeight.cs | 2 +- .../Interop/Hooks/Meta/ChangeCustomize.cs | 2 +- .../Interop/Hooks/Meta/EqdpAccessoryHook.cs | 6 +- Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs | 5 +- Penumbra/Interop/Hooks/Meta/EqpHook.cs | 6 +- Penumbra/Interop/Hooks/Meta/EstHook.cs | 5 +- Penumbra/Interop/Hooks/Meta/GmpHook.cs | 6 +- .../Interop/Hooks/Meta/ModelLoadComplete.cs | 2 +- Penumbra/Interop/Hooks/Meta/RspBustHook.cs | 10 +- Penumbra/Interop/Hooks/Meta/RspHeightHook.cs | 9 +- .../Interop/Hooks/Meta/RspSetupCharacter.cs | 2 +- Penumbra/Interop/Hooks/Meta/RspTailHook.cs | 10 +- Penumbra/Interop/Hooks/Meta/SetupVisor.cs | 2 +- Penumbra/Interop/Hooks/Meta/UpdateModel.cs | 2 +- Penumbra/Interop/Hooks/Meta/UpdateRender.cs | 4 +- .../Hooks/Objects/CharacterBaseDestructor.cs | 2 +- .../Hooks/Objects/CharacterDestructor.cs | 2 +- .../Interop/Hooks/Objects/CopyCharacter.cs | 2 +- .../Hooks/Objects/CreateCharacterBase.cs | 2 +- Penumbra/Interop/Hooks/Objects/EnableDraw.cs | 2 +- .../Interop/Hooks/Objects/WeaponReload.cs | 2 +- .../PostProcessing/PreBoneDeformerReplacer.cs | 4 +- .../PostProcessing/ShaderReplacementFixer.cs | 34 +++-- .../Hooks/ResourceLoading/CreateFileWHook.cs | 2 +- .../Hooks/ResourceLoading/FileReadService.cs | 2 +- .../Hooks/ResourceLoading/MappedCodeReader.cs | 11 +- .../Hooks/ResourceLoading/PapHandler.cs | 3 + .../Hooks/ResourceLoading/PeSigScanner.cs | 1 - .../Hooks/ResourceLoading/ResourceService.cs | 7 +- .../Hooks/ResourceLoading/TexMdlService.cs | 6 +- .../Hooks/Resources/ApricotResourceLoad.cs | 2 +- .../Interop/Hooks/Resources/LoadMtrlShpk.cs | 2 +- .../Interop/Hooks/Resources/LoadMtrlTex.cs | 2 +- .../Hooks/Resources/ResolvePathHooksBase.cs | 2 +- .../Resources/ResourceHandleDestructor.cs | 3 +- Penumbra/Penumbra.cs | 9 +- Penumbra/UI/Tabs/Debug/DebugTab.cs | 21 +-- Penumbra/UI/Tabs/Debug/HookOverrideDrawer.cs | 63 ++++++++ 52 files changed, 326 insertions(+), 108 deletions(-) create mode 100644 Penumbra/UI/Tabs/Debug/HookOverrideDrawer.cs diff --git a/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs b/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs index 96a51027..44eb7ebb 100644 --- a/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs +++ b/Penumbra/Interop/Hooks/Animation/ApricotListenerSoundPlay.cs @@ -24,7 +24,7 @@ public sealed unsafe class ApricotListenerSoundPlayCaller : FastHook("Apricot Listener Sound Play Caller", Sigs.ApricotListenerSoundPlayCaller, Detour, - true); //HookSettings.VfxIdentificationHooks); + !HookOverrides.Instance.Animation.ApricotListenerSoundPlayCaller); } public delegate nint Delegate(nint a1, nint a2, float a3); diff --git a/Penumbra/Interop/Hooks/Animation/CharacterBaseLoadAnimation.cs b/Penumbra/Interop/Hooks/Animation/CharacterBaseLoadAnimation.cs index f99d8ca4..22609afc 100644 --- a/Penumbra/Interop/Hooks/Animation/CharacterBaseLoadAnimation.cs +++ b/Penumbra/Interop/Hooks/Animation/CharacterBaseLoadAnimation.cs @@ -26,7 +26,8 @@ public sealed unsafe class CharacterBaseLoadAnimation : FastHook("CharacterBase Load Animation", Sigs.CharacterBaseLoadAnimation, Detour, HookSettings.VfxIdentificationHooks); + Task = hooks.CreateHook("CharacterBase Load Animation", Sigs.CharacterBaseLoadAnimation, Detour, + !HookOverrides.Instance.Animation.CharacterBaseLoadAnimation); } public delegate void Delegate(DrawObject* drawBase); diff --git a/Penumbra/Interop/Hooks/Animation/Dismount.cs b/Penumbra/Interop/Hooks/Animation/Dismount.cs index 034011e7..17151083 100644 --- a/Penumbra/Interop/Hooks/Animation/Dismount.cs +++ b/Penumbra/Interop/Hooks/Animation/Dismount.cs @@ -16,7 +16,7 @@ public sealed unsafe class Dismount : FastHook { _state = state; _collectionResolver = collectionResolver; - Task = hooks.CreateHook("Dismount", Sigs.Dismount, Detour, HookSettings.VfxIdentificationHooks); + Task = hooks.CreateHook("Dismount", Sigs.Dismount, Detour, !HookOverrides.Instance.Animation.Dismount); } public delegate void Delegate(MountContainer* a1, nint a2); diff --git a/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs b/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs index 48dc0078..29afd4ea 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadAreaVfx.cs @@ -17,10 +17,10 @@ public sealed unsafe class LoadAreaVfx : FastHook public LoadAreaVfx(HookManager hooks, GameState state, CollectionResolver collectionResolver, CrashHandlerService crashHandler) { - _state = state; + _state = state; _collectionResolver = collectionResolver; - _crashHandler = crashHandler; - Task = hooks.CreateHook("Load Area VFX", Sigs.LoadAreaVfx, Detour, HookSettings.VfxIdentificationHooks); + _crashHandler = crashHandler; + Task = hooks.CreateHook("Load Area VFX", Sigs.LoadAreaVfx, Detour, !HookOverrides.Instance.Animation.LoadAreaVfx); } public delegate nint Delegate(uint vfxId, float* pos, GameObject* caster, float unk1, float unk2, byte unk3); diff --git a/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs b/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs index 8d1096d2..91b70ede 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadCharacterSound.cs @@ -20,7 +20,7 @@ public sealed unsafe class LoadCharacterSound : FastHook("Load Character Sound", (nint)VfxContainer.MemberFunctionPointers.LoadCharacterSound, Detour, - HookSettings.VfxIdentificationHooks); + !HookOverrides.Instance.Animation.LoadCharacterSound); } public delegate nint Delegate(VfxContainer* container, int unk1, int unk2, nint unk3, ulong unk4, int unk5, int unk6, ulong unk7); diff --git a/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs b/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs index af801345..9a57ca12 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadCharacterVfx.cs @@ -26,7 +26,7 @@ public sealed unsafe class LoadCharacterVfx : FastHook("Load Character VFX", Sigs.LoadCharacterVfx, Detour, HookSettings.VfxIdentificationHooks); + Task = hooks.CreateHook("Load Character VFX", Sigs.LoadCharacterVfx, Detour, !HookOverrides.Instance.Animation.LoadCharacterVfx); } public delegate nint Delegate(byte* vfxPath, VfxParams* vfxParams, byte unk1, byte unk2, float unk3, int unk4); diff --git a/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs b/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs index 8bb14db6..cdd82b95 100644 --- a/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs +++ b/Penumbra/Interop/Hooks/Animation/LoadTimelineResources.cs @@ -31,7 +31,7 @@ public sealed unsafe class LoadTimelineResources : FastHook("Load Timeline Resources", Sigs.LoadTimelineResources, Detour, HookSettings.VfxIdentificationHooks); + Task = hooks.CreateHook("Load Timeline Resources", Sigs.LoadTimelineResources, Detour, !HookOverrides.Instance.Animation.LoadTimelineResources); } public delegate ulong Delegate(SchedulerTimeline* timeline); diff --git a/Penumbra/Interop/Hooks/Animation/PlayFootstep.cs b/Penumbra/Interop/Hooks/Animation/PlayFootstep.cs index e4a8c83c..858357c8 100644 --- a/Penumbra/Interop/Hooks/Animation/PlayFootstep.cs +++ b/Penumbra/Interop/Hooks/Animation/PlayFootstep.cs @@ -14,7 +14,7 @@ public sealed unsafe class PlayFootstep : FastHook { _state = state; _collectionResolver = collectionResolver; - Task = hooks.CreateHook("Play Footstep", Sigs.FootStepSound, Detour, HookSettings.VfxIdentificationHooks); + Task = hooks.CreateHook("Play Footstep", Sigs.FootStepSound, Detour, !HookOverrides.Instance.Animation.PlayFootstep); } public delegate void Delegate(GameObject* gameObject, int id, int unk); diff --git a/Penumbra/Interop/Hooks/Animation/ScheduleClipUpdate.cs b/Penumbra/Interop/Hooks/Animation/ScheduleClipUpdate.cs index 645b3565..dfbc615a 100644 --- a/Penumbra/Interop/Hooks/Animation/ScheduleClipUpdate.cs +++ b/Penumbra/Interop/Hooks/Animation/ScheduleClipUpdate.cs @@ -23,7 +23,7 @@ public sealed unsafe class ScheduleClipUpdate : FastHook("Schedule Clip Update", Sigs.ScheduleClipUpdate, Detour, HookSettings.VfxIdentificationHooks); + Task = hooks.CreateHook("Schedule Clip Update", Sigs.ScheduleClipUpdate, Detour, !HookOverrides.Instance.Animation.ScheduleClipUpdate); } public delegate void Delegate(ClipScheduler* x); diff --git a/Penumbra/Interop/Hooks/Animation/SomeActionLoad.cs b/Penumbra/Interop/Hooks/Animation/SomeActionLoad.cs index 1f3c0e3b..e1751261 100644 --- a/Penumbra/Interop/Hooks/Animation/SomeActionLoad.cs +++ b/Penumbra/Interop/Hooks/Animation/SomeActionLoad.cs @@ -20,7 +20,7 @@ public sealed unsafe class SomeActionLoad : FastHook _state = state; _collectionResolver = collectionResolver; _crashHandler = crashHandler; - Task = hooks.CreateHook("Some Action Load", Sigs.LoadSomeAction, Detour, HookSettings.VfxIdentificationHooks); + Task = hooks.CreateHook("Some Action Load", Sigs.LoadSomeAction, Detour, !HookOverrides.Instance.Animation.SomeActionLoad); } public delegate void Delegate(TimelineContainer* timelineManager); diff --git a/Penumbra/Interop/Hooks/Animation/SomeMountAnimation.cs b/Penumbra/Interop/Hooks/Animation/SomeMountAnimation.cs index f2b48afe..75f1240a 100644 --- a/Penumbra/Interop/Hooks/Animation/SomeMountAnimation.cs +++ b/Penumbra/Interop/Hooks/Animation/SomeMountAnimation.cs @@ -15,7 +15,7 @@ public sealed unsafe class SomeMountAnimation : FastHook("Some Mount Animation", Sigs.UnkMountAnimation, Detour, HookSettings.VfxIdentificationHooks); + Task = hooks.CreateHook("Some Mount Animation", Sigs.UnkMountAnimation, Detour, !HookOverrides.Instance.Animation.SomeMountAnimation); } public delegate void Delegate(DrawObject* drawObject, uint unk1, byte unk2, uint unk3); diff --git a/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs b/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs index 8f952df5..7339c397 100644 --- a/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs +++ b/Penumbra/Interop/Hooks/Animation/SomePapLoad.cs @@ -22,7 +22,7 @@ public sealed unsafe class SomePapLoad : FastHook _collectionResolver = collectionResolver; _objects = objects; _crashHandler = crashHandler; - Task = hooks.CreateHook("Some PAP Load", Sigs.LoadSomePap, Detour, HookSettings.VfxIdentificationHooks); + Task = hooks.CreateHook("Some PAP Load", Sigs.LoadSomePap, Detour, !HookOverrides.Instance.Animation.SomePapLoad); } public delegate void Delegate(nint a1, int a2, nint a3, int a4); diff --git a/Penumbra/Interop/Hooks/Animation/SomeParasolAnimation.cs b/Penumbra/Interop/Hooks/Animation/SomeParasolAnimation.cs index 165bd5eb..9df8d4eb 100644 --- a/Penumbra/Interop/Hooks/Animation/SomeParasolAnimation.cs +++ b/Penumbra/Interop/Hooks/Animation/SomeParasolAnimation.cs @@ -15,7 +15,7 @@ public sealed unsafe class SomeParasolAnimation : FastHook("Some Parasol Animation", Sigs.UnkParasolAnimation, Detour, HookSettings.VfxIdentificationHooks); + Task = hooks.CreateHook("Some Parasol Animation", Sigs.UnkParasolAnimation, Detour, !HookOverrides.Instance.Animation.SomeParasolAnimation); } public delegate void Delegate(DrawObject* drawObject, int unk1); diff --git a/Penumbra/Interop/Hooks/HookSettings.cs b/Penumbra/Interop/Hooks/HookSettings.cs index 9c60096f..a4f4201f 100644 --- a/Penumbra/Interop/Hooks/HookSettings.cs +++ b/Penumbra/Interop/Hooks/HookSettings.cs @@ -1,14 +1,140 @@ +using Dalamud.Plugin; +using Newtonsoft.Json; + namespace Penumbra.Interop.Hooks; -public static class HookSettings +public class HookOverrides { - public const bool AllHooks = true; + public static HookOverrides Instance = new(); - public const bool ObjectHooks = true && AllHooks; - public const bool ReplacementHooks = true && AllHooks; - public const bool ResourceHooks = true && AllHooks; - public const bool MetaEntryHooks = true && AllHooks; - public const bool MetaParentHooks = true && AllHooks; - public const bool VfxIdentificationHooks = true && AllHooks; - public const bool PostProcessingHooks = true && AllHooks; + public AnimationHooks Animation; + public MetaHooks Meta; + public ObjectHooks Objects; + public PostProcessingHooks PostProcessing; + public ResourceLoadingHooks ResourceLoading; + public ResourceHooks Resources; + + public HookOverrides Clone() + => new() + { + Animation = Animation, + Meta = Meta, + Objects = Objects, + PostProcessing = PostProcessing, + ResourceLoading = ResourceLoading, + Resources = Resources, + }; + + public struct AnimationHooks + { + public bool ApricotListenerSoundPlayCaller; + public bool CharacterBaseLoadAnimation; + public bool Dismount; + public bool LoadAreaVfx; + public bool LoadCharacterSound; + public bool LoadCharacterVfx; + public bool LoadTimelineResources; + public bool PlayFootstep; + public bool ScheduleClipUpdate; + public bool SomeActionLoad; + public bool SomeMountAnimation; + public bool SomePapLoad; + public bool SomeParasolAnimation; + } + + public struct MetaHooks + { + public bool CalculateHeight; + public bool ChangeCustomize; + public bool EqdpAccessoryHook; + public bool EqdpEquipHook; + public bool EqpHook; + public bool EstHook; + public bool GmpHook; + public bool ModelLoadComplete; + public bool RspBustHook; + public bool RspHeightHook; + public bool RspSetupCharacter; + public bool RspTailHook; + public bool SetupVisor; + public bool UpdateModel; + public bool UpdateRender; + } + + public struct ObjectHooks + { + public bool CharacterBaseDestructor; + public bool CharacterDestructor; + public bool CopyCharacter; + public bool CreateCharacterBase; + public bool EnableDraw; + public bool WeaponReload; + } + + public struct PostProcessingHooks + { + public bool HumanSetupScaling; + public bool HumanCreateDeformer; + public bool HumanOnRenderMaterial; + public bool ModelRendererOnRenderMaterial; + } + + public struct ResourceLoadingHooks + { + public bool CreateFileWHook; + public bool PapHooks; + public bool ReadSqPack; + public bool IncRef; + public bool DecRef; + public bool GetResourceSync; + public bool GetResourceAsync; + public bool CheckFileState; + public bool TexResourceHandleOnLoad; + public bool LoadMdlFileExtern; + } + + public struct ResourceHooks + { + public bool ApricotResourceLoad; + public bool LoadMtrlShpk; + public bool LoadMtrlTex; + public bool ResolvePathHooks; + public bool ResourceHandleDestructor; + } + + public const string FileName = "HookOverrides.json"; + + public static HookOverrides LoadFile(IDalamudPluginInterface pi) + { + var path = Path.Combine(pi.GetPluginConfigDirectory(), FileName); + if (!File.Exists(path)) + return new HookOverrides(); + + try + { + var text = File.ReadAllText(path); + var ret = JsonConvert.DeserializeObject(text); + Penumbra.Log.Warning("A hook override file was loaded, some hooks may be disabled and Penumbra might not be working as expected."); + return ret; + } + catch (Exception ex) + { + Penumbra.Log.Error($"A hook override file was found at {path}, but could not be loaded:\n{ex}"); + return new HookOverrides(); + } + } + + public void Write(IDalamudPluginInterface pi) + { + var path = Path.Combine(pi.GetPluginConfigDirectory(), FileName); + try + { + var text = JsonConvert.SerializeObject(this, Formatting.Indented); + File.WriteAllText(path, text); + } + catch (Exception ex) + { + Penumbra.Log.Error($"Could not write hook override file to {path}:\n{ex}"); + } + } } diff --git a/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs b/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs index aab64871..e71d07dd 100644 --- a/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs +++ b/Penumbra/Interop/Hooks/Meta/CalculateHeight.cs @@ -14,7 +14,7 @@ public sealed unsafe class CalculateHeight : FastHook { _collectionResolver = collectionResolver; _metaState = metaState; - Task = hooks.CreateHook("Calculate Height", (nint)Character.MemberFunctionPointers.CalculateHeight, Detour, HookSettings.MetaParentHooks); + Task = hooks.CreateHook("Calculate Height", (nint)Character.MemberFunctionPointers.CalculateHeight, Detour, !HookOverrides.Instance.Meta.CalculateHeight); } public delegate ulong Delegate(Character* character); diff --git a/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs b/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs index f69e98e7..368845b4 100644 --- a/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs +++ b/Penumbra/Interop/Hooks/Meta/ChangeCustomize.cs @@ -16,7 +16,7 @@ public sealed unsafe class ChangeCustomize : FastHook { _collectionResolver = collectionResolver; _metaState = metaState; - Task = hooks.CreateHook("Change Customize", Sigs.ChangeCustomize, Detour, HookSettings.MetaParentHooks); + Task = hooks.CreateHook("Change Customize", Sigs.ChangeCustomize, Detour, !HookOverrides.Instance.Meta.ChangeCustomize); } public delegate bool Delegate(Human* human, CustomizeArray* data, byte skipEquipment); diff --git a/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs b/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs index 63cca53f..43328600 100644 --- a/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqdpAccessoryHook.cs @@ -16,8 +16,10 @@ public unsafe class EqdpAccessoryHook : FastHook, ID public EqdpAccessoryHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetEqdpAccessoryEntry", Sigs.GetEqdpAccessoryEntry, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); - _metaState.Config.ModsEnabled += Toggle; + Task = hooks.CreateHook("GetEqdpAccessoryEntry", Sigs.GetEqdpAccessoryEntry, Detour, + metaState.Config.EnableMods && !HookOverrides.Instance.Meta.EqdpAccessoryHook); + if (!HookOverrides.Instance.Meta.EqdpAccessoryHook) + _metaState.Config.ModsEnabled += Toggle; } private void Detour(CharacterUtility* utility, EqdpEntry* entry, uint setId, uint raceCode) diff --git a/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs b/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs index 5d5d2f84..fa0d5a29 100644 --- a/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqdpEquipHook.cs @@ -16,8 +16,9 @@ public unsafe class EqdpEquipHook : FastHook, IDisposabl public EqdpEquipHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetEqdpEquipEntry", Sigs.GetEqdpEquipEntry, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); - _metaState.Config.ModsEnabled += Toggle; + Task = hooks.CreateHook("GetEqdpEquipEntry", Sigs.GetEqdpEquipEntry, Detour, metaState.Config.EnableMods && !HookOverrides.Instance.Meta.EqdpEquipHook); + if (!HookOverrides.Instance.Meta.EqdpEquipHook) + _metaState.Config.ModsEnabled += Toggle; } private void Detour(CharacterUtility* utility, EqdpEntry* entry, uint setId, uint raceCode) diff --git a/Penumbra/Interop/Hooks/Meta/EqpHook.cs b/Penumbra/Interop/Hooks/Meta/EqpHook.cs index f47db795..f35b922b 100644 --- a/Penumbra/Interop/Hooks/Meta/EqpHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EqpHook.cs @@ -15,8 +15,10 @@ public unsafe class EqpHook : FastHook, IDisposable public EqpHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetEqpFlags", Sigs.GetEqpEntry, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); - _metaState.Config.ModsEnabled += Toggle; + Task = hooks.CreateHook("GetEqpFlags", Sigs.GetEqpEntry, Detour, + metaState.Config.EnableMods && !HookOverrides.Instance.Meta.EqpHook); + if (!HookOverrides.Instance.Meta.EqpHook) + _metaState.Config.ModsEnabled += Toggle; } private void Detour(CharacterUtility* utility, EqpEntry* flags, CharacterArmor* armor) diff --git a/Penumbra/Interop/Hooks/Meta/EstHook.cs b/Penumbra/Interop/Hooks/Meta/EstHook.cs index 825b1244..8284eb69 100644 --- a/Penumbra/Interop/Hooks/Meta/EstHook.cs +++ b/Penumbra/Interop/Hooks/Meta/EstHook.cs @@ -21,8 +21,9 @@ public unsafe class EstHook : FastHook, IDisposable _metaState = metaState; _characterUtility = characterUtility; Task = hooks.CreateHook("FindEstEntry", Sigs.FindEstEntry, Detour, - metaState.Config.EnableMods && HookSettings.MetaEntryHooks); - _metaState.Config.ModsEnabled += Toggle; + metaState.Config.EnableMods && !HookOverrides.Instance.Meta.EstHook); + if (!HookOverrides.Instance.Meta.EstHook) + _metaState.Config.ModsEnabled += Toggle; } private EstEntry Detour(ResourceHandle* estResource, uint genderRace, uint id) diff --git a/Penumbra/Interop/Hooks/Meta/GmpHook.cs b/Penumbra/Interop/Hooks/Meta/GmpHook.cs index 12b221d9..d656ebdb 100644 --- a/Penumbra/Interop/Hooks/Meta/GmpHook.cs +++ b/Penumbra/Interop/Hooks/Meta/GmpHook.cs @@ -17,8 +17,10 @@ public unsafe class GmpHook : FastHook, IDisposable public GmpHook(HookManager hooks, MetaState metaState) { _metaState = metaState; - Task = hooks.CreateHook("GetGmpEntry", Sigs.GetGmpEntry, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); - _metaState.Config.ModsEnabled += Toggle; + Task = hooks.CreateHook("GetGmpEntry", Sigs.GetGmpEntry, Detour, + metaState.Config.EnableMods && !HookOverrides.Instance.Meta.GmpHook); + if (!HookOverrides.Instance.Meta.GmpHook) + _metaState.Config.ModsEnabled += Toggle; } private ulong Detour(CharacterUtility* characterUtility, ulong* outputEntry, ushort setId) diff --git a/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs b/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs index c1803745..4b9b05b1 100644 --- a/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs +++ b/Penumbra/Interop/Hooks/Meta/ModelLoadComplete.cs @@ -13,7 +13,7 @@ public sealed unsafe class ModelLoadComplete : FastHook("Model Load Complete", vtables.HumanVTable[59], Detour, HookSettings.MetaParentHooks); + Task = hooks.CreateHook("Model Load Complete", vtables.HumanVTable[59], Detour, !HookOverrides.Instance.Meta.ModelLoadComplete); } public delegate void Delegate(DrawObject* drawObject); diff --git a/Penumbra/Interop/Hooks/Meta/RspBustHook.cs b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs index e08dc393..c49556bf 100644 --- a/Penumbra/Interop/Hooks/Meta/RspBustHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspBustHook.cs @@ -19,8 +19,10 @@ public unsafe class RspBustHook : FastHook, IDisposable { _metaState = metaState; _metaFileManager = metaFileManager; - Task = hooks.CreateHook("GetRspBust", Sigs.GetRspBust, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); - _metaState.Config.ModsEnabled += Toggle; + Task = hooks.CreateHook("GetRspBust", Sigs.GetRspBust, Detour, + metaState.Config.EnableMods && !HookOverrides.Instance.Meta.RspBustHook); + if (!HookOverrides.Instance.Meta.RspBustHook) + _metaState.Config.ModsEnabled += Toggle; } private float* Detour(nint cmpResource, float* storage, SubRace clan, byte gender, byte bodyType, byte bustSize) @@ -34,7 +36,9 @@ public unsafe class RspBustHook : FastHook, IDisposable } var ret = storage; - if (bodyType < 2 && _metaState.RspCollection.TryPeek(out var collection) && collection is { Valid: true, ModCollection.MetaCache: { } cache }) + if (bodyType < 2 + && _metaState.RspCollection.TryPeek(out var collection) + && collection is { Valid: true, ModCollection.MetaCache: { } cache }) { var bustScale = bustSize / 100f; var ptr = CmpFile.GetDefaults(_metaFileManager, clan, RspAttribute.BustMinX); diff --git a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs index 20e3c939..49180d6e 100644 --- a/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspHeightHook.cs @@ -17,10 +17,12 @@ public class RspHeightHook : FastHook, IDisposable public RspHeightHook(HookManager hooks, MetaState metaState, MetaFileManager metaFileManager) { - _metaState = metaState; + _metaState = metaState; _metaFileManager = metaFileManager; - Task = hooks.CreateHook("GetRspHeight", Sigs.GetRspHeight, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); - _metaState.Config.ModsEnabled += Toggle; + Task = hooks.CreateHook("GetRspHeight", Sigs.GetRspHeight, Detour, + metaState.Config.EnableMods && !HookOverrides.Instance.Meta.RspHeightHook); + if (!HookOverrides.Instance.Meta.RspHeightHook) + _metaState.Config.ModsEnabled += Toggle; } private unsafe float Detour(nint cmpResource, SubRace clan, byte gender, byte bodyType, byte height) @@ -33,6 +35,7 @@ public class RspHeightHook : FastHook, IDisposable // Special cases. if (height == 0xFF) return 1.0f; + if (height > 100) height = 0; diff --git a/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs b/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs index 8bcc7593..952a2e29 100644 --- a/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs +++ b/Penumbra/Interop/Hooks/Meta/RspSetupCharacter.cs @@ -15,7 +15,7 @@ public sealed unsafe class RspSetupCharacter : FastHook("RSP Setup Character", Sigs.RspSetupCharacter, Detour, HookSettings.MetaParentHooks); + Task = hooks.CreateHook("RSP Setup Character", Sigs.RspSetupCharacter, Detour, !HookOverrides.Instance.Meta.RspSetupCharacter); } public delegate void Delegate(DrawObject* drawObject, nint unk2, float unk3, nint unk4, byte unk5); diff --git a/Penumbra/Interop/Hooks/Meta/RspTailHook.cs b/Penumbra/Interop/Hooks/Meta/RspTailHook.cs index 86d21c6f..b434efa6 100644 --- a/Penumbra/Interop/Hooks/Meta/RspTailHook.cs +++ b/Penumbra/Interop/Hooks/Meta/RspTailHook.cs @@ -19,14 +19,18 @@ public class RspTailHook : FastHook, IDisposable { _metaState = metaState; _metaFileManager = metaFileManager; - Task = hooks.CreateHook("GetRspTail", Sigs.GetRspTail, Detour, metaState.Config.EnableMods && HookSettings.MetaEntryHooks); - _metaState.Config.ModsEnabled += Toggle; + Task = hooks.CreateHook("GetRspTail", Sigs.GetRspTail, Detour, + metaState.Config.EnableMods && !HookOverrides.Instance.Meta.RspTailHook); + if (!HookOverrides.Instance.Meta.RspTailHook) + _metaState.Config.ModsEnabled += Toggle; } private unsafe float Detour(nint cmpResource, Race race, byte gender, byte isSecondSubRace, byte bodyType, byte tailLength) { float scale; - if (bodyType < 2 && _metaState.RspCollection.TryPeek(out var collection) && collection is { Valid: true, ModCollection.MetaCache: { } cache }) + if (bodyType < 2 + && _metaState.RspCollection.TryPeek(out var collection) + && collection is { Valid: true, ModCollection.MetaCache: { } cache }) { var clan = (SubRace)(((int)race - 1) * 2 + 1 + isSecondSubRace); var (minIdent, maxIdent) = gender == 0 diff --git a/Penumbra/Interop/Hooks/Meta/SetupVisor.cs b/Penumbra/Interop/Hooks/Meta/SetupVisor.cs index 83c0e0c4..063a9462 100644 --- a/Penumbra/Interop/Hooks/Meta/SetupVisor.cs +++ b/Penumbra/Interop/Hooks/Meta/SetupVisor.cs @@ -19,7 +19,7 @@ public sealed unsafe class SetupVisor : FastHook { _collectionResolver = collectionResolver; _metaState = metaState; - Task = hooks.CreateHook("Setup Visor", Sigs.SetupVisor, Detour, HookSettings.MetaParentHooks); + Task = hooks.CreateHook("Setup Visor", Sigs.SetupVisor, Detour, !HookOverrides.Instance.Meta.SetupVisor); } public delegate byte Delegate(DrawObject* drawObject, ushort modelId, byte visorState); diff --git a/Penumbra/Interop/Hooks/Meta/UpdateModel.cs b/Penumbra/Interop/Hooks/Meta/UpdateModel.cs index a088a0f2..72beea0e 100644 --- a/Penumbra/Interop/Hooks/Meta/UpdateModel.cs +++ b/Penumbra/Interop/Hooks/Meta/UpdateModel.cs @@ -15,7 +15,7 @@ public sealed unsafe class UpdateModel : FastHook { _collectionResolver = collectionResolver; _metaState = metaState; - Task = hooks.CreateHook("Update Model", Sigs.UpdateModel, Detour, HookSettings.MetaParentHooks); + Task = hooks.CreateHook("Update Model", Sigs.UpdateModel, Detour, !HookOverrides.Instance.Meta.UpdateModel); } public delegate void Delegate(DrawObject* drawObject); diff --git a/Penumbra/Interop/Hooks/Meta/UpdateRender.cs b/Penumbra/Interop/Hooks/Meta/UpdateRender.cs index 95cc0e15..ef0068b6 100644 --- a/Penumbra/Interop/Hooks/Meta/UpdateRender.cs +++ b/Penumbra/Interop/Hooks/Meta/UpdateRender.cs @@ -13,8 +13,8 @@ public sealed unsafe class UpdateRender : FastHook public UpdateRender(HookManager hooks, CollectionResolver collectionResolver, MetaState metaState, CharacterBaseVTables vTables) { _collectionResolver = collectionResolver; - _metaState = metaState; - Task = hooks.CreateHook("Human.UpdateRender", vTables.HumanVTable[4], Detour, HookSettings.MetaParentHooks); + _metaState = metaState; + Task = hooks.CreateHook("Human.UpdateRender", vTables.HumanVTable[4], Detour, !HookOverrides.Instance.Meta.UpdateRender); } public delegate void Delegate(DrawObject* drawObject); diff --git a/Penumbra/Interop/Hooks/Objects/CharacterBaseDestructor.cs b/Penumbra/Interop/Hooks/Objects/CharacterBaseDestructor.cs index c67bb9f3..7636718e 100644 --- a/Penumbra/Interop/Hooks/Objects/CharacterBaseDestructor.cs +++ b/Penumbra/Interop/Hooks/Objects/CharacterBaseDestructor.cs @@ -19,7 +19,7 @@ public sealed unsafe class CharacterBaseDestructor : EventWrapperPtr _task = hooks.CreateHook(Name, Address, Detour, HookSettings.ObjectHooks); + => _task = hooks.CreateHook(Name, Address, Detour, !HookOverrides.Instance.Objects.CharacterBaseDestructor); private readonly Task> _task; diff --git a/Penumbra/Interop/Hooks/Objects/CharacterDestructor.cs b/Penumbra/Interop/Hooks/Objects/CharacterDestructor.cs index 618d0bd7..ffe2f72d 100644 --- a/Penumbra/Interop/Hooks/Objects/CharacterDestructor.cs +++ b/Penumbra/Interop/Hooks/Objects/CharacterDestructor.cs @@ -19,7 +19,7 @@ public sealed unsafe class CharacterDestructor : EventWrapperPtr _task = hooks.CreateHook(Name, Sigs.CharacterDestructor, Detour, HookSettings.ObjectHooks); + => _task = hooks.CreateHook(Name, Sigs.CharacterDestructor, Detour, !HookOverrides.Instance.Objects.CharacterDestructor); private readonly Task> _task; diff --git a/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs b/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs index d81043c8..bc18a7ad 100644 --- a/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs +++ b/Penumbra/Interop/Hooks/Objects/CopyCharacter.cs @@ -15,7 +15,7 @@ public sealed unsafe class CopyCharacter : EventWrapperPtr _task = hooks.CreateHook(Name, Address, Detour, HookSettings.ObjectHooks); + => _task = hooks.CreateHook(Name, Address, Detour, !HookOverrides.Instance.Objects.CopyCharacter); private readonly Task> _task; diff --git a/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs b/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs index f00a9984..e29876ac 100644 --- a/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs +++ b/Penumbra/Interop/Hooks/Objects/CreateCharacterBase.cs @@ -16,7 +16,7 @@ public sealed unsafe class CreateCharacterBase : EventWrapperPtr _task = hooks.CreateHook(Name, Address, Detour, HookSettings.ObjectHooks); + => _task = hooks.CreateHook(Name, Address, Detour, !HookOverrides.Instance.Objects.CreateCharacterBase); private readonly Task> _task; diff --git a/Penumbra/Interop/Hooks/Objects/EnableDraw.cs b/Penumbra/Interop/Hooks/Objects/EnableDraw.cs index 8b701fe5..68bb28af 100644 --- a/Penumbra/Interop/Hooks/Objects/EnableDraw.cs +++ b/Penumbra/Interop/Hooks/Objects/EnableDraw.cs @@ -17,7 +17,7 @@ public sealed unsafe class EnableDraw : IHookService public EnableDraw(HookManager hooks, GameState state) { _state = state; - _task = hooks.CreateHook("Enable Draw", Sigs.EnableDraw, Detour, HookSettings.ObjectHooks); + _task = hooks.CreateHook("Enable Draw", Sigs.EnableDraw, Detour, !HookOverrides.Instance.Objects.EnableDraw); } private delegate void Delegate(GameObject* gameObject); diff --git a/Penumbra/Interop/Hooks/Objects/WeaponReload.cs b/Penumbra/Interop/Hooks/Objects/WeaponReload.cs index da31840f..b09103f6 100644 --- a/Penumbra/Interop/Hooks/Objects/WeaponReload.cs +++ b/Penumbra/Interop/Hooks/Objects/WeaponReload.cs @@ -16,7 +16,7 @@ public sealed unsafe class WeaponReload : EventWrapperPtr _task = hooks.CreateHook(Name, Address, Detour, HookSettings.ObjectHooks); + => _task = hooks.CreateHook(Name, Address, Detour, !HookOverrides.Instance.Objects.WeaponReload); private readonly Task> _task; diff --git a/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs b/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs index 834a7d28..1aa09d7f 100644 --- a/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs +++ b/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs @@ -38,9 +38,9 @@ public sealed unsafe class PreBoneDeformerReplacer : IDisposable, IRequiredServi _resourceLoader = resourceLoader; _framework = framework; _humanSetupScalingHook = hooks.CreateHook("HumanSetupScaling", vTables.HumanVTable[58], SetupScaling, - HookSettings.PostProcessingHooks).Result; + !HookOverrides.Instance.PostProcessing.HumanSetupScaling).Result; _humanCreateDeformerHook = hooks.CreateHook("HumanCreateDeformer", vTables.HumanVTable[101], - CreateDeformer, HookSettings.PostProcessingHooks).Result; + CreateDeformer, !HookOverrides.Instance.PostProcessing.HumanCreateDeformer).Result; } public void Dispose() diff --git a/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs b/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs index b87d33ef..53b69741 100644 --- a/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs +++ b/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs @@ -90,20 +90,26 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic _modelRenderer = modelRenderer; _communicator = communicator; - _skinState = new( + _skinState = new ModdedShaderPackageState( () => (ShaderPackageResourceHandle**)&_utility.Address->SkinShpkResource, () => (ShaderPackageResourceHandle*)_utility.DefaultSkinShpkResource); - _irisState = new(() => _modelRenderer.IrisShaderPackage, () => _modelRenderer.DefaultIrisShaderPackage); - _characterGlassState = new(() => _modelRenderer.CharacterGlassShaderPackage, () => _modelRenderer.DefaultCharacterGlassShaderPackage); - _characterTransparencyState = new(() => _modelRenderer.CharacterTransparencyShaderPackage, () => _modelRenderer.DefaultCharacterTransparencyShaderPackage); - _characterTattooState = new(() => _modelRenderer.CharacterTattooShaderPackage, () => _modelRenderer.DefaultCharacterTattooShaderPackage); - _characterOcclusionState = new(() => _modelRenderer.CharacterOcclusionShaderPackage, () => _modelRenderer.DefaultCharacterOcclusionShaderPackage); - _hairMaskState = new(() => _modelRenderer.HairMaskShaderPackage, () => _modelRenderer.DefaultHairMaskShaderPackage); + _irisState = new ModdedShaderPackageState(() => _modelRenderer.IrisShaderPackage, () => _modelRenderer.DefaultIrisShaderPackage); + _characterGlassState = new ModdedShaderPackageState(() => _modelRenderer.CharacterGlassShaderPackage, + () => _modelRenderer.DefaultCharacterGlassShaderPackage); + _characterTransparencyState = new ModdedShaderPackageState(() => _modelRenderer.CharacterTransparencyShaderPackage, + () => _modelRenderer.DefaultCharacterTransparencyShaderPackage); + _characterTattooState = new ModdedShaderPackageState(() => _modelRenderer.CharacterTattooShaderPackage, + () => _modelRenderer.DefaultCharacterTattooShaderPackage); + _characterOcclusionState = new ModdedShaderPackageState(() => _modelRenderer.CharacterOcclusionShaderPackage, + () => _modelRenderer.DefaultCharacterOcclusionShaderPackage); + _hairMaskState = + new ModdedShaderPackageState(() => _modelRenderer.HairMaskShaderPackage, () => _modelRenderer.DefaultHairMaskShaderPackage); _humanOnRenderMaterialHook = hooks.CreateHook("Human.OnRenderMaterial", vTables.HumanVTable[64], - OnRenderHumanMaterial, HookSettings.PostProcessingHooks).Result; + OnRenderHumanMaterial, !HookOverrides.Instance.PostProcessing.HumanOnRenderMaterial).Result; _modelRendererOnRenderMaterialHook = hooks.CreateHook("ModelRenderer.OnRenderMaterial", - Sigs.ModelRendererOnRenderMaterial, ModelRendererOnRenderMaterialDetour, HookSettings.PostProcessingHooks).Result; + Sigs.ModelRendererOnRenderMaterial, ModelRendererOnRenderMaterialDetour, + !HookOverrides.Instance.PostProcessing.ModelRendererOnRenderMaterial).Result; _communicator.MtrlShpkLoaded.Subscribe(OnMtrlShpkLoaded, MtrlShpkLoaded.Priority.ShaderReplacementFixer); _resourceHandleDestructor.Subscribe(OnResourceHandleDestructor, ResourceHandleDestructor.Priority.ShaderReplacementFixer); } @@ -123,7 +129,8 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic _skinState.ClearMaterials(); } - public (ulong Skin, ulong Iris, ulong CharacterGlass, ulong CharacterTransparency, ulong CharacterTattoo, ulong CharacterOcclusion, ulong HairMask) GetAndResetSlowPathCallDeltas() + public (ulong Skin, ulong Iris, ulong CharacterGlass, ulong CharacterTransparency, ulong CharacterTattoo, ulong CharacterOcclusion, ulong + HairMask) GetAndResetSlowPathCallDeltas() => (_skinState.GetAndResetSlowPathCallDelta(), _irisState.GetAndResetSlowPathCallDelta(), _characterGlassState.GetAndResetSlowPathCallDelta(), @@ -208,7 +215,12 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private uint GetTotalMaterialCountForModelRenderer() - => _irisState.MaterialCount + _characterGlassState.MaterialCount + _characterTransparencyState.MaterialCount + _characterTattooState.MaterialCount + _characterOcclusionState.MaterialCount + _hairMaskState.MaterialCount; + => _irisState.MaterialCount + + _characterGlassState.MaterialCount + + _characterTransparencyState.MaterialCount + + _characterTattooState.MaterialCount + + _characterOcclusionState.MaterialCount + + _hairMaskState.MaterialCount; private nint OnRenderHumanMaterial(CharacterBase* human, CSModelRenderer.OnRenderMaterialParams* param) { diff --git a/Penumbra/Interop/Hooks/ResourceLoading/CreateFileWHook.cs b/Penumbra/Interop/Hooks/ResourceLoading/CreateFileWHook.cs index 8d0ac8cb..a9a5f41d 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/CreateFileWHook.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/CreateFileWHook.cs @@ -19,7 +19,7 @@ public unsafe class CreateFileWHook : IDisposable, IRequiredService public CreateFileWHook(IGameInteropProvider interop) { _createFileWHook = interop.HookFromImport(null, "KERNEL32.dll", "CreateFileW", 0, CreateFileWDetour); - if (HookSettings.ReplacementHooks) + if (!HookOverrides.Instance.ResourceLoading.CreateFileWHook) _createFileWHook.Enable(); } diff --git a/Penumbra/Interop/Hooks/ResourceLoading/FileReadService.cs b/Penumbra/Interop/Hooks/ResourceLoading/FileReadService.cs index 199525fb..d8801b81 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/FileReadService.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/FileReadService.cs @@ -15,7 +15,7 @@ public unsafe class FileReadService : IDisposable, IRequiredService _resourceManager = resourceManager; _performance = performance; interop.InitializeFromAttributes(this); - if (HookSettings.ReplacementHooks) + if (!HookOverrides.Instance.ResourceLoading.ReadSqPack) _readSqPackHook.Enable(); } diff --git a/Penumbra/Interop/Hooks/ResourceLoading/MappedCodeReader.cs b/Penumbra/Interop/Hooks/ResourceLoading/MappedCodeReader.cs index 81712cca..de0014d2 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/MappedCodeReader.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/MappedCodeReader.cs @@ -4,10 +4,11 @@ namespace Penumbra.Interop.Hooks.ResourceLoading; public class MappedCodeReader(UnmanagedMemoryAccessor data, long offset) : CodeReader { - public override int ReadByte() { - if (offset >= data.Capacity) - return -1; + public override int ReadByte() + { + if (offset >= data.Capacity) + return -1; - return data.ReadByte(offset++); - } + return data.ReadByte(offset++); + } } diff --git a/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs b/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs index ea12a480..5ba8c975 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/PapHandler.cs @@ -8,6 +8,9 @@ public sealed class PapHandler(PapRewriter.PapResourceHandlerPrototype papResour public void Enable() { + if (HookOverrides.Instance.ResourceLoading.PapHooks) + return; + ReadOnlySpan<(string Sig, string Name)> signatures = [ (Sigs.LoadAlwaysResidentMotionPacks, nameof(Sigs.LoadAlwaysResidentMotionPacks)), diff --git a/Penumbra/Interop/Hooks/ResourceLoading/PeSigScanner.cs b/Penumbra/Interop/Hooks/ResourceLoading/PeSigScanner.cs index f5dd2d45..4be0da00 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/PeSigScanner.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/PeSigScanner.cs @@ -14,7 +14,6 @@ public unsafe class PeSigScanner : IDisposable private readonly nint _moduleBaseAddress; private readonly uint _textSectionVirtualAddress; - public PeSigScanner() { var mainModule = Process.GetCurrentProcess().MainModule!; diff --git a/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs b/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs index 8b99dc37..f75b0623 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs @@ -30,13 +30,14 @@ public unsafe class ResourceService : IDisposable, IRequiredService _decRefHook = interop.HookFromAddress( (nint)CSResourceHandle.MemberFunctionPointers.DecRef, ResourceHandleDecRefDetour); - if (HookSettings.ReplacementHooks) - { + if (HookOverrides.Instance.ResourceLoading.GetResourceSync) _getResourceSyncHook.Enable(); + if (HookOverrides.Instance.ResourceLoading.GetResourceAsync) _getResourceAsyncHook.Enable(); + if (HookOverrides.Instance.ResourceLoading.IncRef) _incRefHook.Enable(); + if (HookOverrides.Instance.ResourceLoading.DecRef) _decRefHook.Enable(); - } } public ResourceHandle* GetResource(ResourceCategory category, ResourceType type, CiByteString path) diff --git a/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs b/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs index 80ba5cb9..fc3289bd 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs @@ -46,12 +46,12 @@ public unsafe class TexMdlService : IDisposable, IRequiredService { interop.InitializeFromAttributes(this); _lodService = new LodService(interop); - if (HookSettings.ReplacementHooks) - { + if (HookOverrides.Instance.ResourceLoading.CheckFileState) _checkFileStateHook.Enable(); + if (HookOverrides.Instance.ResourceLoading.LoadMdlFileExtern) _loadMdlFileExternHook.Enable(); + if (HookOverrides.Instance.ResourceLoading.TexResourceHandleOnLoad) _textureOnLoadHook.Enable(); - } } /// Add CRC64 if the given file is a model or texture file and has an associated path. diff --git a/Penumbra/Interop/Hooks/Resources/ApricotResourceLoad.cs b/Penumbra/Interop/Hooks/Resources/ApricotResourceLoad.cs index 511e842f..f6cccc19 100644 --- a/Penumbra/Interop/Hooks/Resources/ApricotResourceLoad.cs +++ b/Penumbra/Interop/Hooks/Resources/ApricotResourceLoad.cs @@ -11,7 +11,7 @@ public sealed unsafe class ApricotResourceLoad : FastHook("Load Apricot Resource", Sigs.ApricotResourceLoad, Detour, HookSettings.ResourceHooks); + Task = hooks.CreateHook("Load Apricot Resource", Sigs.ApricotResourceLoad, Detour, HookOverrides.Instance.Resources.ApricotResourceLoad); } public delegate byte Delegate(ResourceHandle* handle, nint unk1, byte unk2); diff --git a/Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs b/Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs index 8447762b..7aaa62d5 100644 --- a/Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs +++ b/Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs @@ -14,7 +14,7 @@ public sealed unsafe class LoadMtrlShpk : FastHook { _gameState = gameState; _communicator = communicator; - Task = hooks.CreateHook("Load Material Shaders", Sigs.LoadMtrlShpk, Detour, HookSettings.ResourceHooks); + Task = hooks.CreateHook("Load Material Shaders", Sigs.LoadMtrlShpk, Detour, HookOverrides.Instance.Resources.LoadMtrlShpk); } public delegate byte Delegate(MaterialResourceHandle* mtrlResourceHandle); diff --git a/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs b/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs index 7bc3c7b0..ed0e067b 100644 --- a/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs +++ b/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs @@ -11,7 +11,7 @@ public sealed unsafe class LoadMtrlTex : FastHook public LoadMtrlTex(HookManager hooks, GameState gameState) { _gameState = gameState; - Task = hooks.CreateHook("Load Material Textures", Sigs.LoadMtrlTex, Detour, HookSettings.ResourceHooks); + Task = hooks.CreateHook("Load Material Textures", Sigs.LoadMtrlTex, Detour, HookOverrides.Instance.Resources.LoadMtrlTex); } public delegate byte Delegate(MaterialResourceHandle* mtrlResourceHandle); diff --git a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs index e1b6e46e..66945009 100644 --- a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs +++ b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs @@ -67,7 +67,7 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable // @formatter:on - if (HookSettings.ResourceHooks) + if (HookOverrides.Instance.Resources.ResolvePathHooks) Enable(); } diff --git a/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs b/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs index cd4a53c4..5c4b5c90 100644 --- a/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs +++ b/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs @@ -23,7 +23,8 @@ public sealed unsafe class ResourceHandleDestructor : EventWrapperPtr _task = hooks.CreateHook(Name, Sigs.ResourceHandleDestructor, Detour, HookSettings.ResourceHooks); + => _task = hooks.CreateHook(Name, Sigs.ResourceHandleDestructor, Detour, + HookOverrides.Instance.Resources.ResourceHandleDestructor); private readonly Task> _task; diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index 5f8d6805..8ea74987 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -21,6 +21,7 @@ using Penumbra.GameData.Enums; using Penumbra.UI; using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManager; using System.Xml.Linq; +using Penumbra.Interop.Hooks; using Penumbra.Interop.Hooks.ResourceLoading; namespace Penumbra; @@ -52,9 +53,10 @@ public class Penumbra : IDalamudPlugin { try { - _services = StaticServiceManager.CreateProvider(this, pluginInterface, Log); - Messager = _services.GetService(); - _validityChecker = _services.GetService(); + HookOverrides.Instance = HookOverrides.LoadFile(pluginInterface); + _services = StaticServiceManager.CreateProvider(this, pluginInterface, Log); + Messager = _services.GetService(); + _validityChecker = _services.GetService(); _services.EnsureRequiredServices(); var startup = _services.GetService() @@ -215,6 +217,7 @@ public class Penumbra : IDalamudPlugin sb.Append($"> **`Auto-Deduplication: `** {_config.AutoDeduplicateOnImport}\n"); sb.Append($"> **`Auto-UI-Reduplication: `** {_config.AutoReduplicateUiOnImport}\n"); sb.Append($"> **`Debug Mode: `** {_config.DebugMode}\n"); + sb.Append($"> **`Hook Overrides: `** {HookOverrides.Instance.IsCustomLoaded}\n"); sb.Append( $"> **`Synchronous Load (Dalamud): `** {(_services.GetService().GetDalamudConfig(DalamudConfigService.WaitingForPluginsOption, out bool v) ? v.ToString() : "Unknown")}\n"); sb.Append( diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index a1e9da03..3a64e556 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -97,6 +97,7 @@ public class DebugTab : Window, ITab, IUiService private readonly IpcTester _ipcTester; private readonly CrashHandlerPanel _crashHandlerPanel; private readonly TexHeaderDrawer _texHeaderDrawer; + private readonly HookOverrideDrawer _hookOverrides; public DebugTab(PerformanceTracker performance, Configuration config, CollectionManager collectionManager, ObjectManager objects, IClientState clientState, @@ -106,7 +107,8 @@ public class DebugTab : Window, ITab, IUiService DrawObjectState drawObjectState, PathState pathState, SubfileHelper subfileHelper, IdentifiedCollectionCache identifiedCollectionCache, CutsceneService cutsceneService, ModImportManager modImporter, ImportPopup importPopup, FrameworkManager framework, TextureManager textureManager, ShaderReplacementFixer shaderReplacementFixer, RedrawService redraws, DictEmote emotes, - Diagnostics diagnostics, IpcTester ipcTester, CrashHandlerPanel crashHandlerPanel, TexHeaderDrawer texHeaderDrawer) + Diagnostics diagnostics, IpcTester ipcTester, CrashHandlerPanel crashHandlerPanel, TexHeaderDrawer texHeaderDrawer, + HookOverrideDrawer hookOverrides) : base("Penumbra Debug Window", ImGuiWindowFlags.NoCollapse) { IsOpen = true; @@ -143,6 +145,7 @@ public class DebugTab : Window, ITab, IUiService _ipcTester = ipcTester; _crashHandlerPanel = crashHandlerPanel; _texHeaderDrawer = texHeaderDrawer; + _hookOverrides = hookOverrides; _objects = objects; _clientState = clientState; } @@ -166,34 +169,21 @@ public class DebugTab : Window, ITab, IUiService return; DrawDebugTabGeneral(); - ImGui.NewLine(); _crashHandlerPanel.Draw(); - ImGui.NewLine(); _diagnostics.DrawDiagnostics(); DrawPerformanceTab(); - ImGui.NewLine(); DrawPathResolverDebug(); - ImGui.NewLine(); DrawActorsDebug(); - ImGui.NewLine(); DrawCollectionCaches(); - ImGui.NewLine(); _texHeaderDrawer.Draw(); - ImGui.NewLine(); DrawDebugCharacterUtility(); - ImGui.NewLine(); DrawShaderReplacementFixer(); - ImGui.NewLine(); DrawData(); - ImGui.NewLine(); DrawResourceProblems(); - ImGui.NewLine(); + _hookOverrides.Draw(); DrawPlayerModelInfo(); - ImGui.NewLine(); DrawGlobalVariableInfo(); - ImGui.NewLine(); DrawDebugTabIpc(); - ImGui.NewLine(); } @@ -434,7 +424,6 @@ public class DebugTab : Window, ITab, IUiService private void DrawPerformanceTab() { - ImGui.NewLine(); if (!ImGui.CollapsingHeader("Performance")) return; diff --git a/Penumbra/UI/Tabs/Debug/HookOverrideDrawer.cs b/Penumbra/UI/Tabs/Debug/HookOverrideDrawer.cs new file mode 100644 index 00000000..7af1f884 --- /dev/null +++ b/Penumbra/UI/Tabs/Debug/HookOverrideDrawer.cs @@ -0,0 +1,63 @@ +using Dalamud.Plugin; +using ImGuiNET; +using OtterGui.Services; +using OtterGui.Text; +using Penumbra.Interop.Hooks; + +namespace Penumbra.UI.Tabs.Debug; + +public class HookOverrideDrawer(IDalamudPluginInterface pluginInterface) : IUiService +{ + private HookOverrides? _overrides; + + public void Draw() + { + using var header = ImUtf8.CollapsingHeaderId("Generate Hook Override"u8); + if (!header) + return; + + _overrides ??= HookOverrides.Instance.Clone(); + + if (ImUtf8.Button("Save"u8)) + _overrides.Write(pluginInterface); + + ImGui.SameLine(); + var path = Path.Combine(pluginInterface.GetPluginConfigDirectory(), HookOverrides.FileName); + var exists = File.Exists(path); + if (ImUtf8.ButtonEx("Delete"u8, disabled: !exists, tooltip: exists ? ""u8 : "File does not exist."u8)) + try + { + File.Delete(path); + } + catch (Exception ex) + { + Penumbra.Log.Error($"Could not delete hook override file at {path}:\n{ex}"); + } + + bool? all = null; + ImGui.SameLine(); + if (ImUtf8.Button("Disable All Hooks"u8)) + all = true; + ImGui.SameLine(); + if (ImUtf8.Button("Enable All Hooks"u8)) + all = false; + + foreach (var propertyField in typeof(HookOverrides).GetFields().Where(f => f is { IsStatic: false, FieldType.IsValueType: true })) + { + using var tree = ImUtf8.TreeNode(propertyField.Name); + if (!tree) + continue; + + var property = propertyField.GetValue(_overrides); + foreach (var valueField in propertyField.FieldType.GetFields()) + { + var value = valueField.GetValue(property) as bool? ?? false; + if (ImUtf8.Checkbox($"Disable {valueField.Name}", ref value) || all.HasValue) + { + valueField.SetValue(property, all ?? value); + propertyField.SetValue(_overrides, property); + } + } + } + } +} From 73b9d1fca0ede105413561ba0d61f17d7b176636 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 1 Aug 2024 16:49:22 +0200 Subject: [PATCH 369/433] Meh. --- Penumbra/Interop/Hooks/HookSettings.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Penumbra/Interop/Hooks/HookSettings.cs b/Penumbra/Interop/Hooks/HookSettings.cs index a4f4201f..0c0a4020 100644 --- a/Penumbra/Interop/Hooks/HookSettings.cs +++ b/Penumbra/Interop/Hooks/HookSettings.cs @@ -5,6 +5,9 @@ namespace Penumbra.Interop.Hooks; public class HookOverrides { + [JsonIgnore] + public bool IsCustomLoaded { get; private set; } + public static HookOverrides Instance = new(); public AnimationHooks Animation; @@ -113,7 +116,8 @@ public class HookOverrides try { var text = File.ReadAllText(path); - var ret = JsonConvert.DeserializeObject(text); + var ret = JsonConvert.DeserializeObject(text)!; + ret.IsCustomLoaded = true; Penumbra.Log.Warning("A hook override file was loaded, some hooks may be disabled and Penumbra might not be working as expected."); return ret; } From 7579eaacbe0ffd3fcfcd03e33c6b526d5f2d2310 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 1 Aug 2024 17:14:28 +0200 Subject: [PATCH 370/433] Meh. --- Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs | 8 ++++---- Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs | 6 +++--- Penumbra/Interop/Hooks/Resources/ApricotResourceLoad.cs | 3 ++- Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs | 4 ++-- Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs | 2 +- Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs | 2 +- .../Interop/Hooks/Resources/ResourceHandleDestructor.cs | 2 +- 7 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs b/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs index f75b0623..e55c9bb0 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs @@ -30,13 +30,13 @@ public unsafe class ResourceService : IDisposable, IRequiredService _decRefHook = interop.HookFromAddress( (nint)CSResourceHandle.MemberFunctionPointers.DecRef, ResourceHandleDecRefDetour); - if (HookOverrides.Instance.ResourceLoading.GetResourceSync) + if (!HookOverrides.Instance.ResourceLoading.GetResourceSync) _getResourceSyncHook.Enable(); - if (HookOverrides.Instance.ResourceLoading.GetResourceAsync) + if (!HookOverrides.Instance.ResourceLoading.GetResourceAsync) _getResourceAsyncHook.Enable(); - if (HookOverrides.Instance.ResourceLoading.IncRef) + if (!HookOverrides.Instance.ResourceLoading.IncRef) _incRefHook.Enable(); - if (HookOverrides.Instance.ResourceLoading.DecRef) + if (!HookOverrides.Instance.ResourceLoading.DecRef) _decRefHook.Enable(); } diff --git a/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs b/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs index fc3289bd..d4a2dfba 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/TexMdlService.cs @@ -46,11 +46,11 @@ public unsafe class TexMdlService : IDisposable, IRequiredService { interop.InitializeFromAttributes(this); _lodService = new LodService(interop); - if (HookOverrides.Instance.ResourceLoading.CheckFileState) + if (!HookOverrides.Instance.ResourceLoading.CheckFileState) _checkFileStateHook.Enable(); - if (HookOverrides.Instance.ResourceLoading.LoadMdlFileExtern) + if (!HookOverrides.Instance.ResourceLoading.LoadMdlFileExtern) _loadMdlFileExternHook.Enable(); - if (HookOverrides.Instance.ResourceLoading.TexResourceHandleOnLoad) + if (!HookOverrides.Instance.ResourceLoading.TexResourceHandleOnLoad) _textureOnLoadHook.Enable(); } diff --git a/Penumbra/Interop/Hooks/Resources/ApricotResourceLoad.cs b/Penumbra/Interop/Hooks/Resources/ApricotResourceLoad.cs index f6cccc19..40860b0b 100644 --- a/Penumbra/Interop/Hooks/Resources/ApricotResourceLoad.cs +++ b/Penumbra/Interop/Hooks/Resources/ApricotResourceLoad.cs @@ -11,7 +11,8 @@ public sealed unsafe class ApricotResourceLoad : FastHook("Load Apricot Resource", Sigs.ApricotResourceLoad, Detour, HookOverrides.Instance.Resources.ApricotResourceLoad); + Task = hooks.CreateHook("Load Apricot Resource", Sigs.ApricotResourceLoad, Detour, + !HookOverrides.Instance.Resources.ApricotResourceLoad); } public delegate byte Delegate(ResourceHandle* handle, nint unk1, byte unk2); diff --git a/Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs b/Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs index 7aaa62d5..8c410ad8 100644 --- a/Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs +++ b/Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs @@ -12,9 +12,9 @@ public sealed unsafe class LoadMtrlShpk : FastHook public LoadMtrlShpk(HookManager hooks, GameState gameState, CommunicatorService communicator) { - _gameState = gameState; + _gameState = gameState; _communicator = communicator; - Task = hooks.CreateHook("Load Material Shaders", Sigs.LoadMtrlShpk, Detour, HookOverrides.Instance.Resources.LoadMtrlShpk); + Task = hooks.CreateHook("Load Material Shaders", Sigs.LoadMtrlShpk, Detour, !HookOverrides.Instance.Resources.LoadMtrlShpk); } public delegate byte Delegate(MaterialResourceHandle* mtrlResourceHandle); diff --git a/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs b/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs index ed0e067b..0759d9b1 100644 --- a/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs +++ b/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs @@ -11,7 +11,7 @@ public sealed unsafe class LoadMtrlTex : FastHook public LoadMtrlTex(HookManager hooks, GameState gameState) { _gameState = gameState; - Task = hooks.CreateHook("Load Material Textures", Sigs.LoadMtrlTex, Detour, HookOverrides.Instance.Resources.LoadMtrlTex); + Task = hooks.CreateHook("Load Material Textures", Sigs.LoadMtrlTex, Detour, !HookOverrides.Instance.Resources.LoadMtrlTex); } public delegate byte Delegate(MaterialResourceHandle* mtrlResourceHandle); diff --git a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs index 66945009..b1b23f27 100644 --- a/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs +++ b/Penumbra/Interop/Hooks/Resources/ResolvePathHooksBase.cs @@ -67,7 +67,7 @@ public sealed unsafe class ResolvePathHooksBase : IDisposable // @formatter:on - if (HookOverrides.Instance.Resources.ResolvePathHooks) + if (!HookOverrides.Instance.Resources.ResolvePathHooks) Enable(); } diff --git a/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs b/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs index 5c4b5c90..bdb11752 100644 --- a/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs +++ b/Penumbra/Interop/Hooks/Resources/ResourceHandleDestructor.cs @@ -24,7 +24,7 @@ public sealed unsafe class ResourceHandleDestructor : EventWrapperPtr _task = hooks.CreateHook(Name, Sigs.ResourceHandleDestructor, Detour, - HookOverrides.Instance.Resources.ResourceHandleDestructor); + !HookOverrides.Instance.Resources.ResourceHandleDestructor); private readonly Task> _task; From 1e1637f0e72dff18e6d1beee6c686d5ed509b9f8 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 1 Aug 2024 17:14:46 +0200 Subject: [PATCH 371/433] Test IMC group toggling off. --- Penumbra/Mods/Groups/ImcModGroup.cs | 2 +- .../Manager/OptionEditor/ImcAttributeCache.cs | 34 +++++------------- .../ModsTab/Groups/ImcModGroupEditDrawer.cs | 36 +++++++++++++++---- 3 files changed, 39 insertions(+), 33 deletions(-) diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs index 03896134..5f99673e 100644 --- a/Penumbra/Mods/Groups/ImcModGroup.cs +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -242,7 +242,7 @@ public class ImcModGroup(Mod mod) : IModGroup continue; var option = OptionData[i]; - mask |= option.AttributeMask; + mask ^= option.AttributeMask; } return mask; diff --git a/Penumbra/Mods/Manager/OptionEditor/ImcAttributeCache.cs b/Penumbra/Mods/Manager/OptionEditor/ImcAttributeCache.cs index e1235c5b..a7b73ac9 100644 --- a/Penumbra/Mods/Manager/OptionEditor/ImcAttributeCache.cs +++ b/Penumbra/Mods/Manager/OptionEditor/ImcAttributeCache.cs @@ -21,23 +21,14 @@ public unsafe ref struct ImcAttributeCache _option[i] = byte.MaxValue; var flag = (ushort)(1 << i); - var set = (group.DefaultEntry.AttributeMask & flag) != 0; - if (set) - { - _canChange[i] = true; - _option[i] = byte.MaxValue - 1; - continue; - } - foreach (var (option, idx) in group.OptionData.WithIndex()) { - set = (option.AttributeMask & flag) != 0; - if (set) - { - _canChange[i] = option.AttributeMask != flag; - _option[i] = (byte)idx; - break; - } + if ((option.AttributeMask & flag) == 0) + continue; + + _canChange[i] = option.AttributeMask != flag; + _option[i] = (byte)idx; + break; } if (_option[i] == byte.MaxValue && LowestUnsetMask is 0) @@ -65,25 +56,16 @@ public unsafe ref struct ImcAttributeCache return true; } - if (!_canChange[idx]) - return false; - var mask = (ushort)(oldMask | flag); if (oldMask == mask) return false; group.DefaultEntry = group.DefaultEntry with { AttributeMask = mask }; - if (_option[idx] <= ImcEntry.NumAttributes) - { - var option = group.OptionData[_option[idx]]; - option.AttributeMask = (ushort)(option.AttributeMask & ~flag); - } - return true; } /// Set an attribute flag to a value if possible, remove it from its prior option or the default entry if necessary, and return if anything changed. - public readonly bool Set(ImcSubMod option, int idx, bool value) + public readonly bool Set(ImcSubMod option, int idx, bool value, bool turnOffDefault = false) { if (!_canChange[idx]) return false; @@ -110,7 +92,7 @@ public unsafe ref struct ImcAttributeCache var oldOption = option.Group.OptionData[_option[idx]]; oldOption.AttributeMask = (ushort)(oldOption.AttributeMask & ~flag); } - else if (_option[idx] is byte.MaxValue - 1) + else if (turnOffDefault && _option[idx] is byte.MaxValue - 1) { option.Group.DefaultEntry = option.Group.DefaultEntry with { diff --git a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs index bbb5e54e..9d1ab78a 100644 --- a/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs +++ b/Penumbra/UI/ModsTab/Groups/ImcModGroupEditDrawer.cs @@ -3,6 +3,9 @@ using ImGuiNET; using OtterGui; using OtterGui.Raii; using OtterGui.Text; +using OtterGui.Text.Widget; +using OtterGui.Widgets; +using OtterGuiInternal.Utility; using Penumbra.GameData.Structs; using Penumbra.Mods.Groups; using Penumbra.Mods.Manager.OptionEditor; @@ -86,7 +89,8 @@ public readonly struct ImcModGroupEditDrawer(ModGroupEditDrawer editor, ImcModGr foreach (var (option, idx) in group.OptionData.WithIndex().Where(o => !o.Value.IsDisableSubMod)) { using var id = ImUtf8.PushId(idx); - DrawAttributes(editor.ModManager.OptionEditor.ImcEditor, attributeCache, option.AttributeMask, option); + DrawAttributes(editor.ModManager.OptionEditor.ImcEditor, attributeCache, option.AttributeMask, option, + group.DefaultEntry.AttributeMask); } } } @@ -132,15 +136,18 @@ public readonly struct ImcModGroupEditDrawer(ModGroupEditDrawer editor, ImcModGr } } - private static void DrawAttributes(ImcModGroupEditor editor, in ImcAttributeCache cache, ushort mask, object data) + private static void DrawAttributes(ImcModGroupEditor editor, in ImcAttributeCache cache, ushort mask, object data, + ushort? defaultMask = null) { for (var i = 0; i < ImcEntry.NumAttributes; ++i) { - using var id = ImRaii.PushId(i); - var value = (mask & (1 << i)) != 0; - using (ImRaii.Disabled(!cache.CanChange(i))) + using var id = ImRaii.PushId(i); + var flag = 1 << i; + var value = (mask & flag) != 0; + var inDefault = defaultMask.HasValue && (defaultMask & flag) != 0; + using (ImRaii.Disabled(defaultMask != null && !cache.CanChange(i))) { - if (ImUtf8.Checkbox(""u8, ref value)) + if (inDefault ? NegativeCheckbox.Instance.Draw(""u8, ref value) : ImUtf8.Checkbox(""u8, ref value)) { if (data is ImcModGroup g) editor.ChangeDefaultAttribute(g, cache, i, value); @@ -154,4 +161,21 @@ public readonly struct ImcModGroupEditDrawer(ModGroupEditDrawer editor, ImcModGr ImUtf8.SameLineInner(); } } + + private sealed class NegativeCheckbox : MultiStateCheckbox + { + public static readonly NegativeCheckbox Instance = new(); + + protected override void RenderSymbol(bool value, Vector2 position, float size) + { + if (value) + SymbolHelpers.RenderCross(ImGui.GetWindowDrawList(), position, ImGui.GetColorU32(ImGuiCol.CheckMark), size); + } + + protected override bool NextValue(bool value) + => !value; + + protected override bool PreviousValue(bool value) + => !value; + } } From 5e9c7f7eac0c6334b3063918bd221a1525e80844 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 1 Aug 2024 22:17:56 +0200 Subject: [PATCH 372/433] Fix IMC import sanity check. --- OtterGui | 2 +- Penumbra/Mods/Groups/ImcModGroup.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/OtterGui b/OtterGui index 2b79faac..c53955cb 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 2b79faacff30a31e9ad4b0a3c5d57ffd6e34cfa4 +Subproject commit c53955cb6199dd418c5a9538d3251ac5942e7067 diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs index 5f99673e..7b0eb094 100644 --- a/Penumbra/Mods/Groups/ImcModGroup.cs +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -186,7 +186,7 @@ public class ImcModGroup(Mod mod) : IModGroup return null; } - var rollingMask = ret.DefaultEntry.AttributeMask; + var rollingMask = 0ul; if (options != null) foreach (var child in options.Children()) { From d903f1b8c3a61fdadc9387cad59cf7a3f337ab64 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 1 Aug 2024 22:18:26 +0200 Subject: [PATCH 373/433] Update GetResourceSync and GetResourceAsync function signatures for testing, including unused stack parameters. --- .../Hooks/ResourceLoading/ResourceService.cs | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs b/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs index e55c9bb0..126505d1 100644 --- a/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs +++ b/Penumbra/Interop/Hooks/ResourceLoading/ResourceService.cs @@ -44,7 +44,7 @@ public unsafe class ResourceService : IDisposable, IRequiredService { var hash = path.Crc32; return GetResourceHandler(true, (ResourceManager*)_resourceManager.ResourceManagerAddress, - &category, &type, &hash, path.Path, null, false); + &category, &type, &hash, path.Path, null, 0, 0, 0); } public SafeResourceHandle GetSafeResource(ResourceCategory category, ResourceType type, CiByteString path) @@ -76,10 +76,10 @@ public unsafe class ResourceService : IDisposable, IRequiredService public event GetResourcePreDelegate? ResourceRequested; private delegate ResourceHandle* GetResourceSyncPrototype(ResourceManager* resourceManager, ResourceCategory* pCategoryId, - ResourceType* pResourceType, int* pResourceHash, byte* pPath, GetResourceParameters* pGetResParams); + ResourceType* pResourceType, int* pResourceHash, byte* pPath, GetResourceParameters* pGetResParams, nint unk7, uint unk8); private delegate ResourceHandle* GetResourceAsyncPrototype(ResourceManager* resourceManager, ResourceCategory* pCategoryId, - ResourceType* pResourceType, int* pResourceHash, byte* pPath, GetResourceParameters* pGetResParams, bool isUnknown); + ResourceType* pResourceType, int* pResourceHash, byte* pPath, GetResourceParameters* pGetResParams, byte isUnknown, nint unk8, uint unk9); [Signature(Sigs.GetResourceSync, DetourName = nameof(GetResourceSyncDetour))] private readonly Hook _getResourceSyncHook = null!; @@ -88,27 +88,28 @@ public unsafe class ResourceService : IDisposable, IRequiredService private readonly Hook _getResourceAsyncHook = null!; private ResourceHandle* GetResourceSyncDetour(ResourceManager* resourceManager, ResourceCategory* categoryId, ResourceType* resourceType, - int* resourceHash, byte* path, GetResourceParameters* pGetResParams) - => GetResourceHandler(true, resourceManager, categoryId, resourceType, resourceHash, path, pGetResParams, false); + int* resourceHash, byte* path, GetResourceParameters* pGetResParams, nint unk8, uint unk9) + => GetResourceHandler(true, resourceManager, categoryId, resourceType, resourceHash, path, pGetResParams, 0, unk8, unk9); private ResourceHandle* GetResourceAsyncDetour(ResourceManager* resourceManager, ResourceCategory* categoryId, ResourceType* resourceType, - int* resourceHash, byte* path, GetResourceParameters* pGetResParams, bool isUnk) - => GetResourceHandler(false, resourceManager, categoryId, resourceType, resourceHash, path, pGetResParams, isUnk); + int* resourceHash, byte* path, GetResourceParameters* pGetResParams, byte isUnk, nint unk8, uint unk9) + => GetResourceHandler(false, resourceManager, categoryId, resourceType, resourceHash, path, pGetResParams, isUnk, unk8, unk9); /// /// Resources can be obtained synchronously and asynchronously. We need to change behaviour in both cases. /// Both work basically the same, so we can reduce the main work to one function used by both hooks. /// private ResourceHandle* GetResourceHandler(bool isSync, ResourceManager* resourceManager, ResourceCategory* categoryId, - ResourceType* resourceType, int* resourceHash, byte* path, GetResourceParameters* pGetResParams, bool isUnk) + ResourceType* resourceType, int* resourceHash, byte* path, GetResourceParameters* pGetResParams, byte isUnk, nint unk8, uint unk9) { using var performance = _performance.Measure(PerformanceType.GetResourceHandler); if (!Utf8GamePath.FromPointer(path, MetaDataComputation.CiCrc32, out var gamePath)) { Penumbra.Log.Error("[ResourceService] Could not create GamePath from resource path."); return isSync - ? _getResourceSyncHook.Original(resourceManager, categoryId, resourceType, resourceHash, path, pGetResParams) - : _getResourceAsyncHook.Original(resourceManager, categoryId, resourceType, resourceHash, path, pGetResParams, isUnk); + ? _getResourceSyncHook.Original(resourceManager, categoryId, resourceType, resourceHash, path, pGetResParams, unk8, unk9) + : _getResourceAsyncHook.Original(resourceManager, categoryId, resourceType, resourceHash, path, pGetResParams, isUnk, unk8, + unk9); } ResourceHandle* returnValue = null; @@ -117,17 +118,17 @@ public unsafe class ResourceService : IDisposable, IRequiredService if (returnValue != null) return returnValue; - return GetOriginalResource(isSync, *categoryId, *resourceType, *resourceHash, gamePath.Path, pGetResParams, isUnk); + return GetOriginalResource(isSync, *categoryId, *resourceType, *resourceHash, gamePath.Path, pGetResParams, isUnk, unk8, unk9); } /// Call the original GetResource function. public ResourceHandle* GetOriginalResource(bool sync, ResourceCategory categoryId, ResourceType type, int hash, CiByteString path, - GetResourceParameters* resourceParameters = null, bool unk = false) + GetResourceParameters* resourceParameters = null, byte unk = 0, nint unk8 = 0, uint unk9 = 0) => sync ? _getResourceSyncHook.OriginalDisposeSafe(_resourceManager.ResourceManager, &categoryId, &type, &hash, path.Path, - resourceParameters) + resourceParameters, unk8, unk9) : _getResourceAsyncHook.OriginalDisposeSafe(_resourceManager.ResourceManager, &categoryId, &type, &hash, path.Path, - resourceParameters, unk); + resourceParameters, unk, unk8, unk9); #endregion From f3e72711578f865e6c94fa68f69d50aa75248d90 Mon Sep 17 00:00:00 2001 From: Actions User Date: Fri, 2 Aug 2024 12:48:57 +0000 Subject: [PATCH 374/433] [CI] Updating repo.json for testing_1.2.0.18 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index adf152b0..e1653454 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.17", + "TestingAssemblyVersion": "1.2.0.18", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.17/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.18/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 6241187431f313b6ac9d1959943677d04752546c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 3 Aug 2024 15:15:19 +0200 Subject: [PATCH 375/433] Fix span constructors going over boundaries for ByteStrings. --- Penumbra.String | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.String b/Penumbra.String index 91f0f211..bd52d080 160000 --- a/Penumbra.String +++ b/Penumbra.String @@ -1 +1 @@ -Subproject commit 91f0f21137c61bd39281debf88a8ecc494043330 +Subproject commit bd52d080b72d67263dc47068e461f17c93bdc779 From 4454ac48daadb2375806677f58fe9a7bb5710b8a Mon Sep 17 00:00:00 2001 From: Actions User Date: Sat, 3 Aug 2024 13:18:14 +0000 Subject: [PATCH 376/433] [CI] Updating repo.json for testing_1.2.0.19 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index e1653454..cbe2b121 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.18", + "TestingAssemblyVersion": "1.2.0.19", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.18/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.19/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 069b28272bcec59d03fc84e89850e7acf279e180 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Sat, 3 Aug 2024 17:09:20 +0200 Subject: [PATCH 377/433] Add TextureArraySlicer --- .../Interop/Services/TextureArraySlicer.cs | 119 ++++++++++++++++++ Penumbra/Penumbra.csproj | 8 ++ Penumbra/UI/WindowSystem.cs | 31 +++-- 3 files changed, 145 insertions(+), 13 deletions(-) create mode 100644 Penumbra/Interop/Services/TextureArraySlicer.cs diff --git a/Penumbra/Interop/Services/TextureArraySlicer.cs b/Penumbra/Interop/Services/TextureArraySlicer.cs new file mode 100644 index 00000000..c934ac2b --- /dev/null +++ b/Penumbra/Interop/Services/TextureArraySlicer.cs @@ -0,0 +1,119 @@ +using FFXIVClientStructs.FFXIV.Client.Graphics.Kernel; +using OtterGui.Services; +using SharpDX.Direct3D; +using SharpDX.Direct3D11; + +namespace Penumbra.Interop.Services; + +/// +/// Creates ImGui handles over slices of array textures, and manages their lifetime. +/// +public sealed unsafe class TextureArraySlicer : IUiService, IDisposable +{ + private const uint InitialTimeToLive = 2; + + private readonly Dictionary<(nint XivTexture, byte SliceIndex), SliceState> _activeSlices = []; + private readonly HashSet<(nint XivTexture, byte SliceIndex)> _expiredKeys = []; + + /// Caching this across frames will cause a crash to desktop. + public nint GetImGuiHandle(Texture* texture, byte sliceIndex) + { + if (texture == null) + throw new ArgumentNullException(nameof(texture)); + if (sliceIndex >= texture->ArraySize) + throw new ArgumentOutOfRangeException(nameof(sliceIndex), $"Slice index ({sliceIndex}) is greater than or equal to the texture array size ({texture->ArraySize})"); + if (_activeSlices.TryGetValue(((nint)texture, sliceIndex), out var state)) + { + state.Refresh(); + return (nint)state.ShaderResourceView; + } + var srv = (ShaderResourceView)(nint)texture->D3D11ShaderResourceView; + var description = srv.Description; + switch (description.Dimension) + { + case ShaderResourceViewDimension.Texture1D: + case ShaderResourceViewDimension.Texture2D: + case ShaderResourceViewDimension.Texture2DMultisampled: + case ShaderResourceViewDimension.Texture3D: + case ShaderResourceViewDimension.TextureCube: + // This function treats these as single-slice arrays. + // As per the range check above, the only valid slice (i. e. 0) has been requested, therefore there is nothing to do. + break; + case ShaderResourceViewDimension.Texture1DArray: + description.Texture1DArray.FirstArraySlice = sliceIndex; + description.Texture2DArray.ArraySize = 1; + break; + case ShaderResourceViewDimension.Texture2DArray: + description.Texture2DArray.FirstArraySlice = sliceIndex; + description.Texture2DArray.ArraySize = 1; + break; + case ShaderResourceViewDimension.Texture2DMultisampledArray: + description.Texture2DMSArray.FirstArraySlice = sliceIndex; + description.Texture2DMSArray.ArraySize = 1; + break; + case ShaderResourceViewDimension.TextureCubeArray: + description.TextureCubeArray.First2DArrayFace = sliceIndex * 6; + description.TextureCubeArray.CubeCount = 1; + break; + default: + throw new NotSupportedException($"{nameof(TextureArraySlicer)} does not support dimension {description.Dimension}"); + } + state = new SliceState(new ShaderResourceView(srv.Device, srv.Resource, description)); + _activeSlices.Add(((nint)texture, sliceIndex), state); + return (nint)state.ShaderResourceView; + } + + public void Tick() + { + try + { + foreach (var (key, slice) in _activeSlices) + { + if (!slice.Tick()) + _expiredKeys.Add(key); + } + foreach (var key in _expiredKeys) + { + _activeSlices.Remove(key); + } + } + finally + { + _expiredKeys.Clear(); + } + } + + public void Dispose() + { + foreach (var slice in _activeSlices.Values) + { + slice.Dispose(); + } + } + + private sealed class SliceState(ShaderResourceView shaderResourceView) : IDisposable + { + public readonly ShaderResourceView ShaderResourceView = shaderResourceView; + + private uint _timeToLive = InitialTimeToLive; + + public void Refresh() + { + _timeToLive = InitialTimeToLive; + } + + public bool Tick() + { + if (unchecked(_timeToLive--) > 0) + return true; + + ShaderResourceView.Dispose(); + return false; + } + + public void Dispose() + { + ShaderResourceView.Dispose(); + } + } +} diff --git a/Penumbra/Penumbra.csproj b/Penumbra/Penumbra.csproj index 24ffe469..8e143e3c 100644 --- a/Penumbra/Penumbra.csproj +++ b/Penumbra/Penumbra.csproj @@ -72,6 +72,14 @@ $(DalamudLibPath)Iced.dll False + + $(DalamudLibPath)SharpDX.dll + False + + + $(DalamudLibPath)SharpDX.Direct3D11.dll + False + lib\OtterTex.dll diff --git a/Penumbra/UI/WindowSystem.cs b/Penumbra/UI/WindowSystem.cs index 6d382ad4..575a381f 100644 --- a/Penumbra/UI/WindowSystem.cs +++ b/Penumbra/UI/WindowSystem.cs @@ -2,6 +2,7 @@ using Dalamud.Interface; using Dalamud.Interface.Windowing; using Dalamud.Plugin; using OtterGui.Services; +using Penumbra.Interop.Services; using Penumbra.UI.AdvancedWindow; using Penumbra.UI.Knowledge; using Penumbra.UI.Tabs.Debug; @@ -10,23 +11,25 @@ namespace Penumbra.UI; public class PenumbraWindowSystem : IDisposable, IUiService { - private readonly IUiBuilder _uiBuilder; - private readonly WindowSystem _windowSystem; - private readonly FileDialogService _fileDialog; - public readonly ConfigWindow Window; - public readonly PenumbraChangelog Changelog; - public readonly KnowledgeWindow KnowledgeWindow; + private readonly IUiBuilder _uiBuilder; + private readonly WindowSystem _windowSystem; + private readonly FileDialogService _fileDialog; + private readonly TextureArraySlicer _textureArraySlicer; + public readonly ConfigWindow Window; + public readonly PenumbraChangelog Changelog; + public readonly KnowledgeWindow KnowledgeWindow; public PenumbraWindowSystem(IDalamudPluginInterface pi, Configuration config, PenumbraChangelog changelog, ConfigWindow window, LaunchButton _, ModEditWindow editWindow, FileDialogService fileDialog, ImportPopup importPopup, DebugTab debugTab, - KnowledgeWindow knowledgeWindow) + KnowledgeWindow knowledgeWindow, TextureArraySlicer textureArraySlicer) { - _uiBuilder = pi.UiBuilder; - _fileDialog = fileDialog; - KnowledgeWindow = knowledgeWindow; - Changelog = changelog; - Window = window; - _windowSystem = new WindowSystem("Penumbra"); + _uiBuilder = pi.UiBuilder; + _fileDialog = fileDialog; + _textureArraySlicer = textureArraySlicer; + KnowledgeWindow = knowledgeWindow; + Changelog = changelog; + Window = window; + _windowSystem = new WindowSystem("Penumbra"); _windowSystem.AddWindow(changelog.Changelog); _windowSystem.AddWindow(window); _windowSystem.AddWindow(editWindow); @@ -37,6 +40,7 @@ public class PenumbraWindowSystem : IDisposable, IUiService _uiBuilder.OpenConfigUi += Window.OpenSettings; _uiBuilder.Draw += _windowSystem.Draw; _uiBuilder.Draw += _fileDialog.Draw; + _uiBuilder.Draw += _textureArraySlicer.Tick; _uiBuilder.DisableGposeUiHide = !config.HideUiInGPose; _uiBuilder.DisableCutsceneUiHide = !config.HideUiInCutscenes; _uiBuilder.DisableUserUiHide = !config.HideUiWhenUiHidden; @@ -51,5 +55,6 @@ public class PenumbraWindowSystem : IDisposable, IUiService _uiBuilder.OpenConfigUi -= Window.OpenSettings; _uiBuilder.Draw -= _windowSystem.Draw; _uiBuilder.Draw -= _fileDialog.Draw; + _uiBuilder.Draw -= _textureArraySlicer.Tick; } } From 60986c78f8449b2e6aed128b5b2b42ce6aafe35a Mon Sep 17 00:00:00 2001 From: Exter-N Date: Sat, 3 Aug 2024 17:17:48 +0200 Subject: [PATCH 378/433] Update GameData --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 75582ece..ee6c6faa 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 75582ece58e6ee311074ff4ecaa68b804677878c +Subproject commit ee6c6faa1e4a3e96279cb6c89df96e351f112c6a From 59b3859f117e083cb105e84838ad3d5bd8c186fc Mon Sep 17 00:00:00 2001 From: Exter-N Date: Sat, 3 Aug 2024 17:31:12 +0200 Subject: [PATCH 379/433] Minor upgrades to follow dependencies --- Penumbra/Import/Models/Export/MaterialExporter.cs | 11 ++++++----- Penumbra/Import/Textures/TextureDrawer.cs | 4 +--- Penumbra/Services/MigrationManager.cs | 4 ++-- Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs | 6 +----- 4 files changed, 10 insertions(+), 15 deletions(-) diff --git a/Penumbra/Import/Models/Export/MaterialExporter.cs b/Penumbra/Import/Models/Export/MaterialExporter.cs index 5df9e1c1..62892473 100644 --- a/Penumbra/Import/Models/Export/MaterialExporter.cs +++ b/Penumbra/Import/Models/Export/MaterialExporter.cs @@ -49,7 +49,7 @@ public class MaterialExporter private static MaterialBuilder BuildCharacter(Material material, string name) { // Build the textures from the color table. - var table = new LegacyColorTable(material.Mtrl.Table); + var table = new LegacyColorTable(material.Mtrl.Table!); var normal = material.Textures[TextureUsage.SamplerNormal]; @@ -103,6 +103,7 @@ public class MaterialExporter // TODO: It feels a little silly to request the entire normal here when extracting the normal only needs some of the components. // As a future refactor, it would be neat to accept a single-channel field here, and then do composition of other stuff later. + // TODO(Dawntrail): Use the dedicated index (_id) map, that is not embedded in the normal map's alpha channel anymore. private readonly struct ProcessCharacterNormalOperation(Image normal, LegacyColorTable table) : IRowOperation { public Image Normal { get; } = normal.Clone(); @@ -139,17 +140,17 @@ public class MaterialExporter var nextRow = table[tableRow.Next]; // Base colour (table, .b) - var lerpedDiffuse = Vector3.Lerp(prevRow.Diffuse, nextRow.Diffuse, tableRow.Weight); + var lerpedDiffuse = Vector3.Lerp((Vector3)prevRow.DiffuseColor, (Vector3)nextRow.DiffuseColor, tableRow.Weight); baseColorSpan[x].FromVector4(new Vector4(lerpedDiffuse, 1)); baseColorSpan[x].A = normalPixel.B; // Specular (table) - var lerpedSpecularColor = Vector3.Lerp(prevRow.Specular, nextRow.Specular, tableRow.Weight); - var lerpedSpecularFactor = float.Lerp(prevRow.SpecularStrength, nextRow.SpecularStrength, tableRow.Weight); + var lerpedSpecularColor = Vector3.Lerp((Vector3)prevRow.SpecularColor, (Vector3)nextRow.SpecularColor, tableRow.Weight); + var lerpedSpecularFactor = float.Lerp((float)prevRow.SpecularMask, (float)nextRow.SpecularMask, tableRow.Weight); specularSpan[x].FromVector4(new Vector4(lerpedSpecularColor, lerpedSpecularFactor)); // Emissive (table) - var lerpedEmissive = Vector3.Lerp(prevRow.Emissive, nextRow.Emissive, tableRow.Weight); + var lerpedEmissive = Vector3.Lerp((Vector3)prevRow.EmissiveColor, (Vector3)nextRow.EmissiveColor, tableRow.Weight); emissiveSpan[x].FromVector4(new Vector4(lerpedEmissive, 1)); // Normal (.rg) diff --git a/Penumbra/Import/Textures/TextureDrawer.cs b/Penumbra/Import/Textures/TextureDrawer.cs index bd95d1ab..c83604e4 100644 --- a/Penumbra/Import/Textures/TextureDrawer.cs +++ b/Penumbra/Import/Textures/TextureDrawer.cs @@ -18,9 +18,7 @@ public static class TextureDrawer { if (texture.TextureWrap != null) { - size = size.X < texture.TextureWrap.Width - ? size with { Y = texture.TextureWrap.Height * size.X / texture.TextureWrap.Width } - : new Vector2(texture.TextureWrap.Width, texture.TextureWrap.Height); + size = texture.TextureWrap.Size.Contain(size); ImGui.Image(texture.TextureWrap.ImGuiHandle, size); DrawData(texture); diff --git a/Penumbra/Services/MigrationManager.cs b/Penumbra/Services/MigrationManager.cs index 84318da6..7726f6fd 100644 --- a/Penumbra/Services/MigrationManager.cs +++ b/Penumbra/Services/MigrationManager.cs @@ -279,7 +279,7 @@ public class MigrationManager(Configuration config) : IService Directory.CreateDirectory(Path.GetDirectoryName(path)!); using var f = File.Open(path, FileMode.Create, FileAccess.Write); - if (file.IsDawnTrail) + if (file.IsDawntrail) { file.MigrateToDawntrail(); Penumbra.Log.Debug($"Migrated material {reader.Entry.Key} to Dawntrail during import."); @@ -329,7 +329,7 @@ public class MigrationManager(Configuration config) : IService try { var mtrl = new MtrlFile(data); - if (mtrl.IsDawnTrail) + if (mtrl.IsDawntrail) return data; mtrl.MigrateToDawntrail(); diff --git a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs index c47414b9..e2776b2f 100644 --- a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs +++ b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs @@ -6,7 +6,6 @@ using OtterGui; using Penumbra.Interop.ResourceTree; using Penumbra.UI.Classes; using Penumbra.String; -using Penumbra.UI.Tabs; namespace Penumbra.UI.AdvancedWindow; @@ -245,10 +244,7 @@ public class ResourceTreeViewer if (visibility == NodeVisibility.Hidden) continue; - var textColor = ImGui.GetColorU32(ImGuiCol.Text); - var textColorInternal = (textColor & 0x00FFFFFFu) | ((textColor & 0xFE000000u) >> 1); // Half opacity - - using var mutedColor = ImRaii.PushColor(ImGuiCol.Text, textColorInternal, resourceNode.Internal); + using var mutedColor = ImRaii.PushColor(ImGuiCol.Text, ImGuiUtil.HalfTransparentText(), resourceNode.Internal); var filterIcon = resourceNode.Icon != 0 ? resourceNode.Icon : parentFilterIcon; From e8182f285e2fd83ee8c6ed532fa14c858f9557c1 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Sat, 3 Aug 2024 17:47:22 +0200 Subject: [PATCH 380/433] Update StainService for DT --- .../Interop/Structs/CharacterUtilityData.cs | 28 ++++++- Penumbra/Services/StainService.cs | 84 ++++++++++++++----- Penumbra/UI/Tabs/Debug/DebugTab.cs | 51 +++++++---- 3 files changed, 124 insertions(+), 39 deletions(-) diff --git a/Penumbra/Interop/Structs/CharacterUtilityData.cs b/Penumbra/Interop/Structs/CharacterUtilityData.cs index 197de0bb..7595353f 100644 --- a/Penumbra/Interop/Structs/CharacterUtilityData.cs +++ b/Penumbra/Interop/Structs/CharacterUtilityData.cs @@ -5,10 +5,15 @@ namespace Penumbra.Interop.Structs; [StructLayout(LayoutKind.Explicit)] public unsafe struct CharacterUtilityData { - public const int IndexHumanPbd = 63; - public const int IndexTransparentTex = 79; - public const int IndexDecalTex = 80; - public const int IndexSkinShpk = 83; + public const int IndexHumanPbd = 63; + public const int IndexTransparentTex = 79; + public const int IndexDecalTex = 80; + public const int IndexTileOrbArrayTex = 81; + public const int IndexTileNormArrayTex = 82; + public const int IndexSkinShpk = 83; + public const int IndexGudStm = 94; + public const int IndexLegacyStm = 95; + public const int IndexSphereDArrayTex = 96; public static readonly MetaIndex[] EqdpIndices = Enum.GetNames() .Zip(Enum.GetValues()) @@ -97,8 +102,23 @@ public unsafe struct CharacterUtilityData [FieldOffset(8 + IndexDecalTex * 8)] public TextureResourceHandle* DecalTexResource; + [FieldOffset(8 + IndexTileOrbArrayTex * 8)] + public TextureResourceHandle* TileOrbArrayTexResource; + + [FieldOffset(8 + IndexTileNormArrayTex * 8)] + public TextureResourceHandle* TileNormArrayTexResource; + [FieldOffset(8 + IndexSkinShpk * 8)] public ResourceHandle* SkinShpkResource; + [FieldOffset(8 + IndexGudStm * 8)] + public ResourceHandle* GudStmResource; + + [FieldOffset(8 + IndexLegacyStm * 8)] + public ResourceHandle* LegacyStmResource; + + [FieldOffset(8 + IndexSphereDArrayTex * 8)] + public TextureResourceHandle* SphereDArrayTexResource; + // not included resources have no known use case. } diff --git a/Penumbra/Services/StainService.cs b/Penumbra/Services/StainService.cs index 26b39229..50713968 100644 --- a/Penumbra/Services/StainService.cs +++ b/Penumbra/Services/StainService.cs @@ -6,19 +6,25 @@ using OtterGui.Services; using OtterGui.Widgets; using Penumbra.GameData.DataContainers; using Penumbra.GameData.Files; -using Penumbra.UI.AdvancedWindow; +using Penumbra.GameData.Files.StainMapStructs; +using Penumbra.Interop.Services; +using Penumbra.Interop.Structs; +using Penumbra.UI.AdvancedWindow.Materials; namespace Penumbra.Services; public class StainService : IService { - public sealed class StainTemplateCombo(FilterComboColors stainCombo, StmFile stmFile) - : FilterComboCache(stmFile.Entries.Keys.Prepend((ushort)0), MouseWheelType.None, Penumbra.Log) + public sealed class StainTemplateCombo(FilterComboColors[] stainCombos, StmFile stmFile) + : FilterComboCache(stmFile.Entries.Keys.Prepend((ushort)0), MouseWheelType.None, Penumbra.Log) where TDyePack : unmanaged, IDyePack { + // FIXME There might be a better way to handle that. + public int CurrentDyeChannel = 0; + protected override float GetFilterWidth() { var baseSize = ImGui.CalcTextSize("0000").X + ImGui.GetStyle().ScrollbarSize + ImGui.GetStyle().ItemInnerSpacing.X; - if (stainCombo.CurrentSelection.Key == 0) + if (stainCombos[CurrentDyeChannel].CurrentSelection.Key == 0) return baseSize; return baseSize + ImGui.GetTextLineHeight() * 3 + ImGui.GetStyle().ItemInnerSpacing.X * 3; @@ -47,33 +53,73 @@ public class StainService : IService protected override bool DrawSelectable(int globalIdx, bool selected) { var ret = base.DrawSelectable(globalIdx, selected); - var selection = stainCombo.CurrentSelection.Key; + var selection = stainCombos[CurrentDyeChannel].CurrentSelection.Key; if (selection == 0 || !stmFile.TryGetValue(Items[globalIdx], selection, out var colors)) return ret; ImGui.SameLine(); var frame = new Vector2(ImGui.GetTextLineHeight()); - ImGui.ColorButton("D", new Vector4(ModEditWindow.PseudoSqrtRgb(colors.Diffuse), 1), 0, frame); + ImGui.ColorButton("D", new Vector4(MtrlTab.PseudoSqrtRgb((Vector3)colors.DiffuseColor), 1), 0, frame); ImGui.SameLine(); - ImGui.ColorButton("S", new Vector4(ModEditWindow.PseudoSqrtRgb(colors.Specular), 1), 0, frame); + ImGui.ColorButton("S", new Vector4(MtrlTab.PseudoSqrtRgb((Vector3)colors.SpecularColor), 1), 0, frame); ImGui.SameLine(); - ImGui.ColorButton("E", new Vector4(ModEditWindow.PseudoSqrtRgb(colors.Emissive), 1), 0, frame); + ImGui.ColorButton("E", new Vector4(MtrlTab.PseudoSqrtRgb((Vector3)colors.EmissiveColor), 1), 0, frame); return ret; } } - public readonly DictStain StainData; - public readonly FilterComboColors StainCombo; - public readonly StmFile StmFile; - public readonly StainTemplateCombo TemplateCombo; + public const int ChannelCount = 2; - public StainService(IDataManager dataManager, DictStain stainData) + public readonly DictStain StainData; + public readonly FilterComboColors StainCombo1; + public readonly FilterComboColors StainCombo2; // FIXME is there a better way to handle this? + public readonly StmFile LegacyStmFile; + public readonly StmFile GudStmFile; + public readonly StainTemplateCombo LegacyTemplateCombo; + public readonly StainTemplateCombo GudTemplateCombo; + + public unsafe StainService(IDataManager dataManager, CharacterUtility characterUtility, DictStain stainData) { - StainData = stainData; - StainCombo = new FilterComboColors(140, MouseWheelType.None, - () => StainData.Value.Prepend(new KeyValuePair(0, ("None", 0, false))).ToList(), - Penumbra.Log); - StmFile = new StmFile(dataManager); - TemplateCombo = new StainTemplateCombo(StainCombo, StmFile); + StainData = stainData; + StainCombo1 = CreateStainCombo(); + StainCombo2 = CreateStainCombo(); + LegacyStmFile = LoadStmFile(characterUtility.Address->LegacyStmResource, dataManager); + GudStmFile = LoadStmFile(characterUtility.Address->GudStmResource, dataManager); + + FilterComboColors[] stainCombos = [StainCombo1, StainCombo2]; + + LegacyTemplateCombo = new StainTemplateCombo(stainCombos, LegacyStmFile); + GudTemplateCombo = new StainTemplateCombo(stainCombos, GudStmFile); } + + /// Retrieves the instance for the given channel. Indexing is zero-based. + public FilterComboColors GetStainCombo(int channel) + => channel switch + { + 0 => StainCombo1, + 1 => StainCombo2, + _ => throw new ArgumentOutOfRangeException(nameof(channel), channel, $"Unsupported dye channel {channel} (supported values are 0 and 1)") + }; + + /// Loads a STM file. Opportunistically attempts to re-use the file already read by the game, with Lumina fallback. + private static unsafe StmFile LoadStmFile(ResourceHandle* stmResourceHandle, IDataManager dataManager) where TDyePack : unmanaged, IDyePack + { + if (stmResourceHandle != null) + { + var stmData = stmResourceHandle->CsHandle.GetDataSpan(); + if (stmData.Length > 0) + { + Penumbra.Log.Debug($"[StainService] Loading StmFile<{typeof(TDyePack)}> from ResourceHandle 0x{(nint)stmResourceHandle:X}"); + return new StmFile(stmData); + } + } + + Penumbra.Log.Debug($"[StainService] Loading StmFile<{typeof(TDyePack)}> from Lumina"); + return new StmFile(dataManager); + } + + private FilterComboColors CreateStainCombo() + => new(140, MouseWheelType.None, + () => StainData.Value.Prepend(new KeyValuePair(0, ("None", 0, false))).ToList(), + Penumbra.Log); } diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index 3a64e556..ead02874 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -42,6 +42,9 @@ using ImGuiClip = OtterGui.ImGuiClip; using Penumbra.Api.IpcTester; using Penumbra.Interop.Hooks.PostProcessing; using Penumbra.Interop.Hooks.ResourceLoading; +using Penumbra.GameData.Files.StainMapStructs; +using Penumbra.UI.AdvancedWindow; +using Penumbra.UI.AdvancedWindow.Materials; namespace Penumbra.UI.Tabs.Debug; @@ -697,32 +700,48 @@ public class DebugTab : Window, ITab, IUiService if (!mainTree) return; - foreach (var (key, data) in _stains.StmFile.Entries) + using (var legacyTree = TreeNode("stainingtemplate.stm")) + { + if (legacyTree) + DrawStainTemplatesFile(_stains.LegacyStmFile); + } + + using (var gudTree = TreeNode("stainingtemplate_gud.stm")) + { + if (gudTree) + DrawStainTemplatesFile(_stains.GudStmFile); + } + } + + private static void DrawStainTemplatesFile(StmFile stmFile) where TDyePack : unmanaged, IDyePack + { + foreach (var (key, data) in stmFile.Entries) { using var tree = TreeNode($"Template {key}"); if (!tree) continue; - using var table = Table("##table", 5, ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg); + using var table = Table("##table", data.Colors.Length + data.Scalars.Length, ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg); if (!table) continue; - for (var i = 0; i < StmFile.StainingTemplateEntry.NumElements; ++i) + for (var i = 0; i < StmFile.StainingTemplateEntry.NumElements; ++i) { - var (r, g, b) = data.DiffuseEntries[i]; - ImGuiUtil.DrawTableColumn($"{r:F6} | {g:F6} | {b:F6}"); + foreach (var list in data.Colors) + { + var color = list[i]; + ImGui.TableNextColumn(); + var frame = new Vector2(ImGui.GetTextLineHeight()); + ImGui.ColorButton("###color", new Vector4(MtrlTab.PseudoSqrtRgb((Vector3)color), 1), 0, frame); + ImGui.SameLine(); + ImGui.TextUnformatted($"{color.Red:F6} | {color.Green:F6} | {color.Blue:F6}"); + } - (r, g, b) = data.SpecularEntries[i]; - ImGuiUtil.DrawTableColumn($"{r:F6} | {g:F6} | {b:F6}"); - - (r, g, b) = data.EmissiveEntries[i]; - ImGuiUtil.DrawTableColumn($"{r:F6} | {g:F6} | {b:F6}"); - - var a = data.SpecularPowerEntries[i]; - ImGuiUtil.DrawTableColumn($"{a:F6}"); - - a = data.GlossEntries[i]; - ImGuiUtil.DrawTableColumn($"{a:F6}"); + foreach (var list in data.Scalars) + { + var scalar = list[i]; + ImGuiUtil.DrawTableColumn($"{scalar:F6}"); + } } } } From 450751e43fe4d9627da938715b3930e9d25ebe10 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Sat, 3 Aug 2024 17:47:36 +0200 Subject: [PATCH 381/433] DT material editor, supporting components --- .../Materials/ConstantEditors.cs | 71 +++++++ .../Materials/MaterialTemplatePickers.cs | 177 ++++++++++++++++++ 2 files changed, 248 insertions(+) create mode 100644 Penumbra/UI/AdvancedWindow/Materials/ConstantEditors.cs create mode 100644 Penumbra/UI/AdvancedWindow/Materials/MaterialTemplatePickers.cs diff --git a/Penumbra/UI/AdvancedWindow/Materials/ConstantEditors.cs b/Penumbra/UI/AdvancedWindow/Materials/ConstantEditors.cs new file mode 100644 index 00000000..690580df --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Materials/ConstantEditors.cs @@ -0,0 +1,71 @@ +using System.Collections.Frozen; +using OtterGui.Text.Widget.Editors; +using Penumbra.GameData.Files.ShaderStructs; + +namespace Penumbra.UI.AdvancedWindow.Materials; + +public static class ConstantEditors +{ + public static readonly IEditor DefaultFloat = Editors.DefaultFloat.AsByteEditor(); + public static readonly IEditor DefaultInt = Editors.DefaultInt.AsByteEditor(); + public static readonly IEditor DefaultIntAsFloat = Editors.DefaultInt.IntAsFloatEditor().AsByteEditor(); + public static readonly IEditor DefaultColor = ColorEditor.HighDynamicRange.Reinterpreting(); + + /// + /// Material constants known to be encoded as native s. + /// + /// A editor is nonfunctional for them, as typical values for these constants would fall into the IEEE 754 denormalized number range. + /// + private static readonly FrozenSet KnownIntConstants; + + static ConstantEditors() + { + IReadOnlyList knownIntConstants = [ + "g_ToonIndex", + "g_ToonSpecIndex", + ]; + + KnownIntConstants = knownIntConstants.ToFrozenSet(); + } + + public static IEditor DefaultFor(Name name, MaterialTemplatePickers? materialTemplatePickers = null) + { + if (materialTemplatePickers != null) + { + if (name == Names.SphereMapIndexConstantName) + return materialTemplatePickers.SphereMapIndexPicker; + else if (name == Names.TileIndexConstantName) + return materialTemplatePickers.TileIndexPicker; + } + + if (name.Value != null && name.Value.EndsWith("Color")) + return DefaultColor; + + if (KnownIntConstants.Contains(name)) + return DefaultInt; + + return DefaultFloat; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static IEditor AsByteEditor(this IEditor inner) where T : unmanaged + => inner.Reinterpreting(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static IEditor IntAsFloatEditor(this IEditor inner) + => inner.Converting(value => int.CreateSaturating(MathF.Round(value)), value => value); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static IEditor WithExponent(this IEditor inner, T exponent) + where T : unmanaged, IPowerFunctions, IComparisonOperators + => exponent == T.MultiplicativeIdentity + ? inner + : inner.Converting(value => value < T.Zero ? -T.Pow(-value, T.MultiplicativeIdentity / exponent) : T.Pow(value, T.MultiplicativeIdentity / exponent), value => value < T.Zero ? -T.Pow(-value, exponent) : T.Pow(value, exponent)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static IEditor WithFactorAndBias(this IEditor inner, T factor, T bias) + where T : unmanaged, IMultiplicativeIdentity, IAdditiveIdentity, IMultiplyOperators, IAdditionOperators, ISubtractionOperators, IDivisionOperators, IEqualityOperators + => factor == T.MultiplicativeIdentity && bias == T.AdditiveIdentity + ? inner + : inner.Converting(value => (value - bias) / factor, value => value * factor + bias); +} diff --git a/Penumbra/UI/AdvancedWindow/Materials/MaterialTemplatePickers.cs b/Penumbra/UI/AdvancedWindow/Materials/MaterialTemplatePickers.cs new file mode 100644 index 00000000..6ffd1f88 --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Materials/MaterialTemplatePickers.cs @@ -0,0 +1,177 @@ +using Dalamud.Interface; +using FFXIVClientStructs.Interop; +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Services; +using OtterGui.Text; +using OtterGui.Text.Widget.Editors; +using Penumbra.Interop.Services; +using Penumbra.Interop.Structs; + +namespace Penumbra.UI.AdvancedWindow.Materials; + +public sealed unsafe class MaterialTemplatePickers : IUiService +{ + private const float MaximumTextureSize = 64.0f; + + private readonly TextureArraySlicer _textureArraySlicer; + private readonly CharacterUtility _characterUtility; + + public readonly IEditor TileIndexPicker; + public readonly IEditor SphereMapIndexPicker; + + public MaterialTemplatePickers(TextureArraySlicer textureArraySlicer, CharacterUtility characterUtility) + { + _textureArraySlicer = textureArraySlicer; + _characterUtility = characterUtility; + + TileIndexPicker = new Editor(DrawTileIndexPicker).AsByteEditor(); + SphereMapIndexPicker = new Editor(DrawSphereMapIndexPicker).AsByteEditor(); + } + + public bool DrawTileIndexPicker(ReadOnlySpan label, ReadOnlySpan description, ref ushort value, bool compact) + => _characterUtility.Address != null + && DrawTextureArrayIndexPicker(label, description, ref value, compact, [ + _characterUtility.Address->TileOrbArrayTexResource, + _characterUtility.Address->TileNormArrayTexResource, + ]); + + public bool DrawSphereMapIndexPicker(ReadOnlySpan label, ReadOnlySpan description, ref ushort value, bool compact) + => _characterUtility.Address != null + && DrawTextureArrayIndexPicker(label, description, ref value, compact, [ + _characterUtility.Address->SphereDArrayTexResource, + ]); + + public bool DrawTextureArrayIndexPicker(ReadOnlySpan label, ReadOnlySpan description, ref ushort value, bool compact, ReadOnlySpan> textureRHs) + { + TextureResourceHandle* firstNonNullTextureRH = null; + foreach (var texture in textureRHs) + { + if (texture.Value != null && texture.Value->CsHandle.Texture != null) + { + firstNonNullTextureRH = texture; + break; + } + } + var firstNonNullTexture = firstNonNullTextureRH != null ? firstNonNullTextureRH->CsHandle.Texture : null; + + var textureSize = firstNonNullTexture != null ? new Vector2(firstNonNullTexture->Width, firstNonNullTexture->Height).Contain(new Vector2(MaximumTextureSize)) : Vector2.Zero; + var count = firstNonNullTexture != null ? firstNonNullTexture->ArraySize : 0; + + var ret = false; + + var framePadding = ImGui.GetStyle().FramePadding; + var itemSpacing = ImGui.GetStyle().ItemSpacing; + using (var font = ImRaii.PushFont(UiBuilder.MonoFont)) + { + var spaceSize = ImUtf8.CalcTextSize(" "u8).X; + var spaces = (int)((ImGui.CalcItemWidth() - framePadding.X * 2.0f - (compact ? 0.0f : (textureSize.X + itemSpacing.X) * textureRHs.Length)) / spaceSize); + using var padding = ImRaii.PushStyle(ImGuiStyleVar.FramePadding, framePadding + new Vector2(0.0f, Math.Max(textureSize.Y - ImGui.GetFrameHeight() + itemSpacing.Y, 0.0f) * 0.5f), !compact); + using var combo = ImUtf8.Combo(label, (value == ushort.MaxValue ? "-" : value.ToString()).PadLeft(spaces), ImGuiComboFlags.NoArrowButton | ImGuiComboFlags.HeightLarge); + if (combo.Success && firstNonNullTextureRH != null) + { + var lineHeight = Math.Max(ImGui.GetTextLineHeightWithSpacing(), framePadding.Y * 2.0f + textureSize.Y); + var itemWidth = Math.Max(ImGui.GetContentRegionAvail().X, ImUtf8.CalcTextSize("MMM"u8).X + (itemSpacing.X + textureSize.X) * textureRHs.Length + framePadding.X * 2.0f); + using var center = ImRaii.PushStyle(ImGuiStyleVar.SelectableTextAlign, new Vector2(0, 0.5f)); + using var clipper = ImUtf8.ListClipper(count, lineHeight); + while (clipper.Step()) + { + for (var i = clipper.DisplayStart; i < clipper.DisplayEnd && i < count; i++) + { + if (ImUtf8.Selectable($"{i,3}", i == value, size: new(itemWidth, lineHeight))) + { + ret = value != i; + value = (ushort)i; + } + var rectMin = ImGui.GetItemRectMin(); + var rectMax = ImGui.GetItemRectMax(); + var textureRegionStart = new Vector2( + rectMax.X - framePadding.X - textureSize.X * textureRHs.Length - itemSpacing.X * (textureRHs.Length - 1), + rectMin.Y + framePadding.Y); + var maxSize = new Vector2(textureSize.X, rectMax.Y - framePadding.Y - textureRegionStart.Y); + DrawTextureSlices(textureRegionStart, maxSize, itemSpacing.X, textureRHs, (byte)i); + } + } + } + } + if (!compact && value != ushort.MaxValue) + { + var cbRectMin = ImGui.GetItemRectMin(); + var cbRectMax = ImGui.GetItemRectMax(); + var cbTextureRegionStart = new Vector2(cbRectMax.X - framePadding.X - textureSize.X * textureRHs.Length - itemSpacing.X * (textureRHs.Length - 1), cbRectMin.Y + framePadding.Y); + var cbMaxSize = new Vector2(textureSize.X, cbRectMax.Y - framePadding.Y - cbTextureRegionStart.Y); + DrawTextureSlices(cbTextureRegionStart, cbMaxSize, itemSpacing.X, textureRHs, (byte)value); + } + if (ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled) && (description.Length > 0 || compact && value != ushort.MaxValue)) + { + using var disabled = ImRaii.Enabled(); + using var tt = ImUtf8.Tooltip(); + if (description.Length > 0) + ImUtf8.Text(description); + if (compact && value != ushort.MaxValue) + { + ImGui.Dummy(new Vector2(textureSize.X * textureRHs.Length + itemSpacing.X * (textureRHs.Length - 1), textureSize.Y)); + var rectMin = ImGui.GetItemRectMin(); + var rectMax = ImGui.GetItemRectMax(); + DrawTextureSlices(rectMin, textureSize, itemSpacing.X, textureRHs, (byte)value); + } + } + + return ret; + } + + public void DrawTextureSlices(Vector2 regionStart, Vector2 itemSize, float itemSpacing, ReadOnlySpan> textureRHs, byte sliceIndex) + { + for (var j = 0; j < textureRHs.Length; ++j) + { + if (textureRHs[j].Value == null) + continue; + var texture = textureRHs[j].Value->CsHandle.Texture; + if (texture == null) + continue; + var handle = _textureArraySlicer.GetImGuiHandle(texture, sliceIndex); + if (handle == 0) + continue; + + var position = regionStart with { X = regionStart.X + (itemSize.X + itemSpacing) * j }; + var size = new Vector2(texture->Width, texture->Height).Contain(itemSize); + position += (itemSize - size) * 0.5f; + ImGui.GetWindowDrawList().AddImage(handle, position, position + size, Vector2.Zero, + new Vector2(texture->Width / (float)texture->Width2, texture->Height / (float)texture->Height2)); + } + } + + private delegate bool DrawEditor(ReadOnlySpan label, ReadOnlySpan description, ref ushort value, bool compact); + + private sealed class Editor(DrawEditor draw) : IEditor + { + public bool Draw(Span values, bool disabled) + { + var helper = Editors.PrepareMultiComponent(values.Length); + var ret = false; + + for (var valueIdx = 0; valueIdx < values.Length; ++valueIdx) + { + helper.SetupComponent(valueIdx); + + var value = ushort.CreateSaturating(MathF.Round(values[valueIdx])); + if (disabled) + { + using var _ = ImRaii.Disabled(); + draw(helper.Id, default, ref value, true); + } + else + { + if (draw(helper.Id, default, ref value, true)) + { + values[valueIdx] = value; + ret = true; + } + } + } + + return ret; + } + } +} From 36ab9573aec81e56655be92f62c7e3e473d737f5 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Sat, 3 Aug 2024 17:41:38 +0200 Subject: [PATCH 382/433] DT material editor, main part --- Penumbra/Configuration.cs | 1 + .../LiveColorTablePreviewer.cs | 24 +- .../MaterialPreview/LiveMaterialPreviewer.cs | 20 +- .../Materials/MtrlTab.CommonColorTable.cs | 509 ++++++++++++ .../Materials/MtrlTab.Constants.cs | 277 +++++++ .../Materials/MtrlTab.Devkit.cs | 240 ++++++ .../Materials/MtrlTab.LegacyColorTable.cs | 368 ++++++++ .../Materials/MtrlTab.LivePreview.cs | 272 ++++++ .../Materials/MtrlTab.ShaderPackage.cs | 505 +++++++++++ .../Materials/MtrlTab.Textures.cs | 276 ++++++ .../UI/AdvancedWindow/Materials/MtrlTab.cs | 199 +++++ .../Materials/MtrlTabFactory.cs | 18 + .../ModEditWindow.Materials.ColorTable.cs | 538 ------------ .../ModEditWindow.Materials.ConstantEditor.cs | 247 ------ .../ModEditWindow.Materials.MtrlTab.cs | 783 ------------------ .../ModEditWindow.Materials.Shpk.cs | 481 ----------- .../AdvancedWindow/ModEditWindow.Materials.cs | 179 +--- .../ModEditWindow.QuickImport.cs | 1 + Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 76 +- Penumbra/UI/Classes/Colors.cs | 4 +- Penumbra/UI/Tabs/SettingsTab.cs | 12 + 21 files changed, 2744 insertions(+), 2286 deletions(-) create mode 100644 Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs create mode 100644 Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Constants.cs create mode 100644 Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Devkit.cs create mode 100644 Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs create mode 100644 Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs create mode 100644 Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ShaderPackage.cs create mode 100644 Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Textures.cs create mode 100644 Penumbra/UI/AdvancedWindow/Materials/MtrlTab.cs create mode 100644 Penumbra/UI/AdvancedWindow/Materials/MtrlTabFactory.cs delete mode 100644 Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs delete mode 100644 Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ConstantEditor.cs delete mode 100644 Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs delete mode 100644 Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.Shpk.cs diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index 63325433..50426b38 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -107,6 +107,7 @@ public class Configuration : IPluginConfiguration, ISavable, IService public bool AlwaysOpenDefaultImport { get; set; } = false; public bool KeepDefaultMetaChanges { get; set; } = false; public string DefaultModAuthor { get; set; } = DefaultTexToolsData.Author; + public bool EditRawTileTransforms { get; set; } = false; public Dictionary Colors { get; set; } = Enum.GetValues().ToDictionary(c => c, c => c.Data().DefaultColor); diff --git a/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs b/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs index 8e75a895..bbd3b16c 100644 --- a/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs +++ b/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs @@ -1,5 +1,6 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Graphics.Kernel; +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using Penumbra.GameData.Interop; using Penumbra.Interop.SafeHandles; @@ -7,10 +8,6 @@ namespace Penumbra.Interop.MaterialPreview; public sealed unsafe class LiveColorTablePreviewer : LiveMaterialPreviewerBase { - public const int TextureWidth = 4; - public const int TextureHeight = GameData.Files.MaterialStructs.LegacyColorTable.NumUsedRows; - public const int TextureLength = TextureWidth * TextureHeight * 4; - private readonly IFramework _framework; private readonly Texture** _colorTableTexture; @@ -18,6 +15,9 @@ public sealed unsafe class LiveColorTablePreviewer : LiveMaterialPreviewerBase private bool _updatePending; + public int Width { get; } + public int Height { get; } + public Half[] ColorTable { get; } public LiveColorTablePreviewer(ObjectManager objects, IFramework framework, MaterialInfo materialInfo) @@ -33,18 +33,24 @@ public sealed unsafe class LiveColorTablePreviewer : LiveMaterialPreviewerBase if (colorSetTextures == null) throw new InvalidOperationException("Draw object doesn't have color table textures"); - _colorTableTexture = colorSetTextures + (MaterialInfo.ModelSlot * 4 + MaterialInfo.MaterialSlot); + _colorTableTexture = colorSetTextures + (MaterialInfo.ModelSlot * CharacterBase.MaterialsPerSlot + MaterialInfo.MaterialSlot); + _originalColorTableTexture = new SafeTextureHandle(*_colorTableTexture, true); if (_originalColorTableTexture == null) throw new InvalidOperationException("Material doesn't have a color table"); - ColorTable = new Half[TextureLength]; + Width = (int)_originalColorTableTexture.Texture->Width; + Height = (int)_originalColorTableTexture.Texture->Height; + ColorTable = new Half[Width * Height * 4]; _updatePending = true; framework.Update += OnFrameworkUpdate; } + public Span GetColorRow(int i) + => ColorTable.AsSpan().Slice(Width * 4 * i, Width * 4); + protected override void Clear(bool disposing, bool reset) { _framework.Update -= OnFrameworkUpdate; @@ -74,8 +80,8 @@ public sealed unsafe class LiveColorTablePreviewer : LiveMaterialPreviewerBase return; var textureSize = stackalloc int[2]; - textureSize[0] = TextureWidth; - textureSize[1] = TextureHeight; + textureSize[0] = Width; + textureSize[1] = Height; using var texture = new SafeTextureHandle(Device.Instance()->CreateTexture2D(textureSize, 1, 0x2460, 0x80000804, 7), false); @@ -104,6 +110,6 @@ public sealed unsafe class LiveColorTablePreviewer : LiveMaterialPreviewerBase if (colorSetTextures == null) return false; - return _colorTableTexture == colorSetTextures + (MaterialInfo.ModelSlot * 4 + MaterialInfo.MaterialSlot); + return _colorTableTexture == colorSetTextures + (MaterialInfo.ModelSlot * CharacterBase.MaterialsPerSlot + MaterialInfo.MaterialSlot); } } diff --git a/Penumbra/Interop/MaterialPreview/LiveMaterialPreviewer.cs b/Penumbra/Interop/MaterialPreview/LiveMaterialPreviewer.cs index 0556fdc4..60762ac7 100644 --- a/Penumbra/Interop/MaterialPreview/LiveMaterialPreviewer.cs +++ b/Penumbra/Interop/MaterialPreview/LiveMaterialPreviewer.cs @@ -7,9 +7,9 @@ public sealed unsafe class LiveMaterialPreviewer : LiveMaterialPreviewerBase { private readonly ShaderPackage* _shaderPackage; - private readonly uint _originalShPkFlags; - private readonly float[] _originalMaterialParameter; - private readonly uint[] _originalSamplerFlags; + private readonly uint _originalShPkFlags; + private readonly byte[] _originalMaterialParameter; + private readonly uint[] _originalSamplerFlags; public LiveMaterialPreviewer(ObjectManager objects, MaterialInfo materialInfo) : base(objects, materialInfo) @@ -28,7 +28,7 @@ public sealed unsafe class LiveMaterialPreviewer : LiveMaterialPreviewerBase _originalShPkFlags = Material->ShaderFlags; - _originalMaterialParameter = Material->MaterialParameterCBuffer->TryGetBuffer().ToArray(); + _originalMaterialParameter = Material->MaterialParameterCBuffer->TryGetBuffer().ToArray(); _originalSamplerFlags = new uint[Material->TextureCount]; for (var i = 0; i < _originalSamplerFlags.Length; ++i) @@ -43,7 +43,7 @@ public sealed unsafe class LiveMaterialPreviewer : LiveMaterialPreviewerBase return; Material->ShaderFlags = _originalShPkFlags; - var materialParameter = Material->MaterialParameterCBuffer->TryGetBuffer(); + var materialParameter = Material->MaterialParameterCBuffer->TryGetBuffer(); if (!materialParameter.IsEmpty) _originalMaterialParameter.AsSpan().CopyTo(materialParameter); @@ -59,7 +59,7 @@ public sealed unsafe class LiveMaterialPreviewer : LiveMaterialPreviewerBase Material->ShaderFlags = shPkFlags; } - public void SetMaterialParameter(uint parameterCrc, Index offset, Span value) + public void SetMaterialParameter(uint parameterCrc, Index offset, ReadOnlySpan value) { if (!CheckValidity()) return; @@ -68,7 +68,7 @@ public sealed unsafe class LiveMaterialPreviewer : LiveMaterialPreviewerBase if (constantBuffer == null) return; - var buffer = constantBuffer->TryGetBuffer(); + var buffer = constantBuffer->TryGetBuffer(); if (buffer.IsEmpty) return; @@ -78,12 +78,10 @@ public sealed unsafe class LiveMaterialPreviewer : LiveMaterialPreviewerBase if (parameter.CRC != parameterCrc) continue; - if ((parameter.Offset & 0x3) != 0 - || (parameter.Size & 0x3) != 0 - || (parameter.Offset + parameter.Size) >> 2 > buffer.Length) + if (parameter.Offset + parameter.Size > buffer.Length) return; - value.TryCopyTo(buffer.Slice(parameter.Offset >> 2, parameter.Size >> 2)[offset..]); + value.TryCopyTo(buffer.Slice(parameter.Offset, parameter.Size)[offset..]); return; } } diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs new file mode 100644 index 00000000..937614de --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs @@ -0,0 +1,509 @@ +using Dalamud.Interface; +using Dalamud.Interface.Utility; +using ImGuiNET; +using Penumbra.GameData.Files.MaterialStructs; +using Penumbra.GameData.Files; +using OtterGui.Text; +using Penumbra.GameData.Structs; +using OtterGui.Raii; +using OtterGui.Text.Widget; + +namespace Penumbra.UI.AdvancedWindow.Materials; + +public partial class MtrlTab +{ + private static readonly float HalfMinValue = (float)Half.MinValue; + private static readonly float HalfMaxValue = (float)Half.MaxValue; + private static readonly float HalfEpsilon = (float)Half.Epsilon; + + private static readonly FontAwesomeCheckbox ApplyStainCheckbox = new(FontAwesomeIcon.FillDrip); + + private static (Vector2 Scale, float Rotation, float Shear)? _pinnedTileTransform; + + private bool DrawColorTableSection(bool disabled) + { + if ((!ShpkLoading && !SamplerIds.Contains(ShpkFile.TableSamplerId)) || Mtrl.Table == null) + return false; + + ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); + if (!ImGui.CollapsingHeader("Color Table", ImGuiTreeNodeFlags.DefaultOpen)) + return false; + + ColorTableCopyAllClipboardButton(); + ImGui.SameLine(); + var ret = ColorTablePasteAllClipboardButton(disabled); + if (!disabled) + { + ImGui.SameLine(); + ImGui.Dummy(ImGuiHelpers.ScaledVector2(20, 0)); + ImGui.SameLine(); + ret |= ColorTableDyeableCheckbox(); + } + + if (Mtrl.DyeTable != null) + { + ImGui.SameLine(); + ImGui.Dummy(ImGuiHelpers.ScaledVector2(20, 0)); + ImGui.SameLine(); + ret |= DrawPreviewDye(disabled); + } + + ret |= Mtrl.Table switch + { + LegacyColorTable legacyTable => DrawLegacyColorTable(legacyTable, Mtrl.DyeTable as LegacyColorDyeTable, disabled), + ColorTable table when Mtrl.ShaderPackage.Name is "characterlegacy.shpk" => DrawLegacyColorTable(table, Mtrl.DyeTable as ColorDyeTable, disabled), + _ => false, + }; + + return ret; + } + + private void ColorTableCopyAllClipboardButton() + { + if (Mtrl.Table == null) + return; + + if (!ImGui.Button("Export All Rows to Clipboard", ImGuiHelpers.ScaledVector2(200, 0))) + return; + + try + { + var data1 = Mtrl.Table.AsBytes(); + var data2 = Mtrl.DyeTable != null ? Mtrl.DyeTable.AsBytes() : []; + + var array = new byte[data1.Length + data2.Length]; + data1.TryCopyTo(array); + data2.TryCopyTo(array.AsSpan(data1.Length)); + + var text = Convert.ToBase64String(array); + ImGui.SetClipboardText(text); + } + catch + { + // ignored + } + } + + private bool DrawPreviewDye(bool disabled) + { + var (dyeId1, (name1, dyeColor1, gloss1)) = _stainService.StainCombo1.CurrentSelection; + var (dyeId2, (name2, dyeColor2, gloss2)) = _stainService.StainCombo2.CurrentSelection; + var tt = dyeId1 == 0 && dyeId2 == 0 + ? "Select a preview dye first."u8 + : "Apply all preview values corresponding to the dye template and chosen dye where dyeing is enabled."u8; + if (ImUtf8.ButtonEx("Apply Preview Dye"u8, tt, disabled: disabled || dyeId1 == 0 && dyeId2 == 0)) + { + var ret = false; + if (Mtrl.DyeTable != null) + { + ret |= Mtrl.ApplyDye(_stainService.LegacyStmFile, [dyeId1, dyeId2]); + ret |= Mtrl.ApplyDye(_stainService.GudStmFile, [dyeId1, dyeId2]); + } + + UpdateColorTablePreview(); + + return ret; + } + + ImGui.SameLine(); + var label = dyeId1 == 0 ? "Preview Dye 1###previewDye1" : $"{name1} (Preview 1)###previewDye1"; + if (_stainService.StainCombo1.Draw(label, dyeColor1, string.Empty, true, gloss1)) + UpdateColorTablePreview(); + ImGui.SameLine(); + label = dyeId2 == 0 ? "Preview Dye 2###previewDye2" : $"{name2} (Preview 2)###previewDye2"; + if (_stainService.StainCombo2.Draw(label, dyeColor2, string.Empty, true, gloss2)) + UpdateColorTablePreview(); + return false; + } + + private bool ColorTablePasteAllClipboardButton(bool disabled) + { + if (Mtrl.Table == null) + return false; + + if (!ImUtf8.ButtonEx("Import All Rows from Clipboard"u8, ImGuiHelpers.ScaledVector2(200, 0), disabled)) + return false; + + try + { + var text = ImGui.GetClipboardText(); + var data = Convert.FromBase64String(text); + var table = Mtrl.Table.AsBytes(); + var dyeTable = Mtrl.DyeTable != null ? Mtrl.DyeTable.AsBytes() : []; + if (data.Length != table.Length && data.Length != table.Length + dyeTable.Length) + return false; + + data.AsSpan(0, table.Length).TryCopyTo(table); + data.AsSpan(table.Length).TryCopyTo(dyeTable); + + UpdateColorTablePreview(); + + return true; + } + catch + { + return false; + } + } + + [SkipLocalsInit] + private void ColorTableCopyClipboardButton(int rowIdx) + { + if (Mtrl.Table == null) + return; + + if (!ImUtf8.IconButton(FontAwesomeIcon.Clipboard, "Export this row to your clipboard."u8, + ImGui.GetFrameHeight() * Vector2.One)) + return; + + try + { + var data1 = Mtrl.Table.RowAsBytes(rowIdx); + var data2 = Mtrl.DyeTable != null ? Mtrl.DyeTable.RowAsBytes(rowIdx) : []; + + var array = new byte[data1.Length + data2.Length]; + data1.TryCopyTo(array); + data2.TryCopyTo(array.AsSpan(data1.Length)); + + var text = Convert.ToBase64String(array); + ImGui.SetClipboardText(text); + } + catch + { + // ignored + } + } + + private bool ColorTableDyeableCheckbox() + { + var dyeable = Mtrl.DyeTable != null; + var ret = ImGui.Checkbox("Dyeable", ref dyeable); + + if (ret) + { + Mtrl.DyeTable = dyeable ? Mtrl.Table switch + { + ColorTable => new ColorDyeTable(), + LegacyColorTable => new LegacyColorDyeTable(), + _ => null, + } : null; + UpdateColorTablePreview(); + } + + return ret; + } + + private bool ColorTablePasteFromClipboardButton(int rowIdx, bool disabled) + { + if (Mtrl.Table == null) + return false; + + if (!ImUtf8.IconButton(FontAwesomeIcon.Paste, "Import an exported row from your clipboard onto this row."u8, + ImGui.GetFrameHeight() * Vector2.One, disabled)) + return false; + + try + { + var text = ImGui.GetClipboardText(); + var data = Convert.FromBase64String(text); + var row = Mtrl.Table.RowAsBytes(rowIdx); + var dyeRow = Mtrl.DyeTable != null ? Mtrl.DyeTable.RowAsBytes(rowIdx) : []; + if (data.Length != row.Length && data.Length != row.Length + dyeRow.Length) + return false; + + data.AsSpan(0, row.Length).TryCopyTo(row); + data.AsSpan(row.Length).TryCopyTo(dyeRow); + + UpdateColorTableRowPreview(rowIdx); + + return true; + } + catch + { + return false; + } + } + + private void ColorTableHighlightButton(int pairIdx, bool disabled) + { + ImUtf8.IconButton(FontAwesomeIcon.Crosshairs, "Highlight this pair of rows on your character, if possible.\n\nHighlight colors can be configured in Penumbra's settings."u8, + ImGui.GetFrameHeight() * Vector2.One, disabled || ColorTablePreviewers.Count == 0); + + if (ImGui.IsItemHovered()) + HighlightColorTablePair(pairIdx); + else if (HighlightedColorTablePair == pairIdx) + CancelColorTableHighlight(); + } + + private static void CtBlendRect(Vector2 rcMin, Vector2 rcMax, uint topColor, uint bottomColor) + { + var style = ImGui.GetStyle(); + var frameRounding = style.FrameRounding; + var frameThickness = style.FrameBorderSize; + var borderColor = ImGui.GetColorU32(ImGuiCol.Border); + var drawList = ImGui.GetWindowDrawList(); + if (topColor == bottomColor) + drawList.AddRectFilled(rcMin, rcMax, topColor, frameRounding, ImDrawFlags.RoundCornersDefault); + else + { + drawList.AddRectFilled( + rcMin, rcMax with { Y = float.Lerp(rcMin.Y, rcMax.Y, 1.0f / 3) }, + topColor, frameRounding, ImDrawFlags.RoundCornersTopLeft | ImDrawFlags.RoundCornersTopRight); + drawList.AddRectFilledMultiColor( + rcMin with { Y = float.Lerp(rcMin.Y, rcMax.Y, 1.0f / 3) }, + rcMax with { Y = float.Lerp(rcMin.Y, rcMax.Y, 2.0f / 3) }, + topColor, topColor, bottomColor, bottomColor); + drawList.AddRectFilled( + rcMin with { Y = float.Lerp(rcMin.Y, rcMax.Y, 2.0f / 3) }, rcMax, + bottomColor, frameRounding, ImDrawFlags.RoundCornersBottomLeft | ImDrawFlags.RoundCornersBottomRight); + } + drawList.AddRect(rcMin, rcMax, borderColor, frameRounding, ImDrawFlags.RoundCornersDefault, frameThickness); + } + + private static bool CtColorPicker(ReadOnlySpan label, ReadOnlySpan description, HalfColor current, Action setter, ReadOnlySpan letter = default) + { + var ret = false; + var inputSqrt = PseudoSqrtRgb((Vector3)current); + var tmp = inputSqrt; + if (ImUtf8.ColorEdit(label, ref tmp, + ImGuiColorEditFlags.NoInputs + | ImGuiColorEditFlags.DisplayRGB + | ImGuiColorEditFlags.InputRGB + | ImGuiColorEditFlags.NoTooltip + | ImGuiColorEditFlags.HDR) + && tmp != inputSqrt) + { + setter((HalfColor)PseudoSquareRgb(tmp)); + ret = true; + } + + if (letter.Length > 0 && ImGui.IsItemVisible()) + { + var textSize = ImUtf8.CalcTextSize(letter); + var center = ImGui.GetItemRectMin() + (ImGui.GetItemRectSize() - textSize) / 2; + var textColor = inputSqrt.LengthSquared() < 0.25f ? 0x80FFFFFFu : 0x80000000u; + ImGui.GetWindowDrawList().AddText(letter, center, textColor); + } + + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, description); + + return ret; + } + + private static void CtColorPicker(ReadOnlySpan label, ReadOnlySpan description, HalfColor? current, ReadOnlySpan letter = default) + { + if (current.HasValue) + CtColorPicker(label, description, current.Value, Nop, letter); + else + { + var tmp = Vector4.Zero; + ImUtf8.ColorEdit(label, ref tmp, + ImGuiColorEditFlags.NoInputs + | ImGuiColorEditFlags.DisplayRGB + | ImGuiColorEditFlags.InputRGB + | ImGuiColorEditFlags.NoTooltip + | ImGuiColorEditFlags.HDR + | ImGuiColorEditFlags.AlphaPreview); + + if (letter.Length > 0 && ImGui.IsItemVisible()) + { + var textSize = ImUtf8.CalcTextSize(letter); + var center = ImGui.GetItemRectMin() + (ImGui.GetItemRectSize() - textSize) / 2; + ImGui.GetWindowDrawList().AddText(letter, center, 0x80000000u); + } + + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, description); + } + } + + private static bool CtApplyStainCheckbox(ReadOnlySpan label, ReadOnlySpan description, bool current, Action setter) + { + var tmp = current; + var result = ApplyStainCheckbox.Draw(label, ref tmp); + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, description); + if (!result || tmp == current) + return false; + + setter(tmp); + return true; + } + + private static bool CtDragHalf(ReadOnlySpan label, ReadOnlySpan description, Half value, ReadOnlySpan format, float min, float max, float speed, Action setter) + { + var tmp = (float)value; + var result = ImUtf8.DragScalar(label, ref tmp, format, min, max, speed); + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, description); + if (!result) + return false; + var newValue = (Half)tmp; + if (newValue == value) + return false; + setter(newValue); + return true; + } + + private static bool CtDragHalf(ReadOnlySpan label, ReadOnlySpan description, ref Half value, ReadOnlySpan format, float min, float max, float speed) + { + var tmp = (float)value; + var result = ImUtf8.DragScalar(label, ref tmp, format, min, max, speed); + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, description); + if (!result) + return false; + var newValue = (Half)tmp; + if (newValue == value) + return false; + value = newValue; + return true; + } + + private static void CtDragHalf(ReadOnlySpan label, ReadOnlySpan description, Half? value, ReadOnlySpan format) + { + using var _ = ImRaii.Disabled(); + var valueOrDefault = value ?? Half.Zero; + var floatValue = (float)valueOrDefault; + CtDragHalf(label, description, valueOrDefault, value.HasValue ? format : "-"u8, floatValue, floatValue, 0.0f, Nop); + } + + private static bool CtDragScalar(ReadOnlySpan label, ReadOnlySpan description, T value, ReadOnlySpan format, T min, T max, float speed, Action setter) where T : unmanaged, INumber + { + var tmp = value; + var result = ImUtf8.DragScalar(label, ref tmp, format, min, max, speed); + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, description); + if (!result || tmp == value) + return false; + setter(tmp); + return true; + } + + private static bool CtDragScalar(ReadOnlySpan label, ReadOnlySpan description, ref T value, ReadOnlySpan format, T min, T max, float speed) where T : unmanaged, INumber + { + var tmp = value; + var result = ImUtf8.DragScalar(label, ref tmp, format, min, max, speed); + ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, description); + if (!result || tmp == value) + return false; + value = tmp; + return true; + } + + private static void CtDragScalar(ReadOnlySpan label, ReadOnlySpan description, T? value, ReadOnlySpan format) where T : unmanaged, INumber + { + using var _ = ImRaii.Disabled(); + var valueOrDefault = value ?? T.Zero; + CtDragScalar(label, description, valueOrDefault, value.HasValue ? format : "-"u8, valueOrDefault, valueOrDefault, 0.0f, Nop); + } + + private bool CtTileIndexPicker(ReadOnlySpan label, ReadOnlySpan description, ushort value, bool compact, Action setter) + { + if (!_materialTemplatePickers.DrawTileIndexPicker(label, description, ref value, compact)) + return false; + setter(value); + return true; + } + + private bool CtSphereMapIndexPicker(ReadOnlySpan label, ReadOnlySpan description, ushort value, bool compact, Action setter) + { + if (!_materialTemplatePickers.DrawSphereMapIndexPicker(label, description, ref value, compact)) + return false; + setter(value); + return true; + } + + private bool CtTileTransformMatrix(HalfMatrix2x2 value, float floatSize, bool twoRowLayout, Action setter) + { + var ret = false; + if (_config.EditRawTileTransforms) + { + var tmp = value; + ImGui.SetNextItemWidth(floatSize); + ret |= CtDragHalf("##TileTransformUU"u8, "Tile Repeat U"u8, ref tmp.UU, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f); + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(floatSize); + ret |= CtDragHalf("##TileTransformVV"u8, "Tile Repeat V"u8, ref tmp.VV, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f); + if (!twoRowLayout) + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(floatSize); + ret |= CtDragHalf("##TileTransformUV"u8, "Tile Skew U"u8, ref tmp.UV, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f); + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(floatSize); + ret |= CtDragHalf("##TileTransformVU"u8, "Tile Skew V"u8, ref tmp.VU, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f); + if (!ret || tmp == value) + return false; + setter(tmp); + } + else + { + value.Decompose(out var scale, out var rotation, out var shear); + rotation *= 180.0f / MathF.PI; + shear *= 180.0f / MathF.PI; + ImGui.SetNextItemWidth(floatSize); + var scaleXChanged = CtDragScalar("##TileScaleU"u8, "Tile Scale U"u8, ref scale.X, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f); + var activated = ImGui.IsItemActivated(); + var deactivated = ImGui.IsItemDeactivated(); + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(floatSize); + var scaleYChanged = CtDragScalar("##TileScaleV"u8, "Tile Scale V"u8, ref scale.Y, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f); + activated |= ImGui.IsItemActivated(); + deactivated |= ImGui.IsItemDeactivated(); + if (!twoRowLayout) + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(floatSize); + var rotationChanged = CtDragScalar("##TileRotation"u8, "Tile Rotation"u8, ref rotation, "%.0f°"u8, -180.0f, 180.0f, 1.0f); + activated |= ImGui.IsItemActivated(); + deactivated |= ImGui.IsItemDeactivated(); + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(floatSize); + var shearChanged = CtDragScalar("##TileShear"u8, "Tile Shear"u8, ref shear, "%.0f°"u8, -90.0f, 90.0f, 1.0f); + activated |= ImGui.IsItemActivated(); + deactivated |= ImGui.IsItemDeactivated(); + if (deactivated) + _pinnedTileTransform = null; + else if (activated) + _pinnedTileTransform = (scale, rotation, shear); + ret = scaleXChanged | scaleYChanged | rotationChanged | shearChanged; + if (!ret) + return false; + if (_pinnedTileTransform.HasValue) + { + var (pinScale, pinRotation, pinShear) = _pinnedTileTransform.Value; + if (!scaleXChanged) + scale.X = pinScale.X; + if (!scaleYChanged) + scale.Y = pinScale.Y; + if (!rotationChanged) + rotation = pinRotation; + if (!shearChanged) + shear = pinShear; + } + var newValue = HalfMatrix2x2.Compose(scale, rotation * MathF.PI / 180.0f, shear * MathF.PI / 180.0f); + if (newValue == value) + return false; + setter(newValue); + } + return true; + } + + /// For use as setter of read-only fields. + private static void Nop(T _) + { } + + // Functions to deal with squared RGB values without making negatives useless. + + internal static float PseudoSquareRgb(float x) + => x < 0.0f ? -(x * x) : x * x; + + internal static Vector3 PseudoSquareRgb(Vector3 vec) + => new(PseudoSquareRgb(vec.X), PseudoSquareRgb(vec.Y), PseudoSquareRgb(vec.Z)); + + internal static Vector4 PseudoSquareRgb(Vector4 vec) + => new(PseudoSquareRgb(vec.X), PseudoSquareRgb(vec.Y), PseudoSquareRgb(vec.Z), vec.W); + + internal static float PseudoSqrtRgb(float x) + => x < 0.0f ? -MathF.Sqrt(-x) : MathF.Sqrt(x); + + internal static Vector3 PseudoSqrtRgb(Vector3 vec) + => new(PseudoSqrtRgb(vec.X), PseudoSqrtRgb(vec.Y), PseudoSqrtRgb(vec.Z)); + + internal static Vector4 PseudoSqrtRgb(Vector4 vec) + => new(PseudoSqrtRgb(vec.X), PseudoSqrtRgb(vec.Y), PseudoSqrtRgb(vec.Z), vec.W); +} diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Constants.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Constants.cs new file mode 100644 index 00000000..56496005 --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Constants.cs @@ -0,0 +1,277 @@ +using Dalamud.Interface; +using ImGuiNET; +using OtterGui; +using OtterGui.Classes; +using OtterGui.Raii; +using OtterGui.Text; +using OtterGui.Text.Widget.Editors; +using Penumbra.GameData.Files.ShaderStructs; +using static Penumbra.GameData.Files.ShpkFile; + +namespace Penumbra.UI.AdvancedWindow.Materials; + +public partial class MtrlTab +{ + private const float MaterialConstantSize = 250.0f; + + public readonly + List<(string Header, List<(string Label, int ConstantIndex, Range Slice, string Description, bool MonoFont, IEditor Editor)> + Constants)> Constants = new(16); + + private void UpdateConstants() + { + static List FindOrAddGroup(List<(string, List)> groups, string name) + { + foreach (var (groupName, group) in groups) + { + if (string.Equals(name, groupName, StringComparison.Ordinal)) + return group; + } + + var newGroup = new List(16); + groups.Add((name, newGroup)); + return newGroup; + } + + Constants.Clear(); + string mpPrefix; + if (AssociatedShpk == null) + { + mpPrefix = MaterialParamsConstantName.Value!; + var fcGroup = FindOrAddGroup(Constants, "Further Constants"); + foreach (var (constant, index) in Mtrl.ShaderPackage.Constants.WithIndex()) + { + var values = Mtrl.GetConstantValue(constant); + for (var i = 0; i < values.Length; i += 4) + { + fcGroup.Add(($"0x{constant.Id:X8}", index, i..Math.Min(i + 4, values.Length), string.Empty, true, + ConstantEditors.DefaultFloat)); + } + } + } + else + { + mpPrefix = AssociatedShpk.GetConstantById(MaterialParamsConstantId)?.Name ?? MaterialParamsConstantName.Value!; + var autoNameMaxLength = Math.Max(Names.LongestKnownNameLength, mpPrefix.Length + 8); + foreach (var shpkConstant in AssociatedShpk.MaterialParams) + { + var name = Names.KnownNames.TryResolve(shpkConstant.Id); + var constant = Mtrl.GetOrAddConstant(shpkConstant.Id, AssociatedShpk, out var constantIndex); + var values = Mtrl.GetConstantValue(constant); + var handledElements = new IndexSet(values.Length, false); + + var dkData = TryGetShpkDevkitData("Constants", shpkConstant.Id, true); + if (dkData != null) + foreach (var dkConstant in dkData) + { + var offset = (int)dkConstant.EffectiveByteOffset; + var length = values.Length - offset; + var constantSize = dkConstant.EffectiveByteSize; + if (constantSize.HasValue) + length = Math.Min(length, (int)constantSize.Value); + if (length <= 0) + continue; + + var editor = dkConstant.CreateEditor(_materialTemplatePickers); + if (editor != null) + FindOrAddGroup(Constants, dkConstant.Group.Length > 0 ? dkConstant.Group : "Further Constants") + .Add((dkConstant.Label, constantIndex, offset..(offset + length), dkConstant.Description, false, editor)); + handledElements.AddRange(offset, length); + } + + if (handledElements.IsFull) + continue; + + var fcGroup = FindOrAddGroup(Constants, "Further Constants"); + foreach (var (start, end) in handledElements.Ranges(complement: true)) + { + if (start == 0 && end == values.Length && end - start <= 16) + { + if (name.Value != null) + { + fcGroup.Add(( + $"{name.Value.PadRight(autoNameMaxLength)} (0x{shpkConstant.Id:X8})", + constantIndex, 0..values.Length, string.Empty, true, DefaultConstantEditorFor(name))); + continue; + } + } + + if ((shpkConstant.ByteOffset & 0x3) == 0 && (shpkConstant.ByteSize & 0x3) == 0) + { + var offset = shpkConstant.ByteOffset; + for (int i = (start & ~0xF) - (offset & 0xF), j = offset >> 4; i < end; i += 16, ++j) + { + var rangeStart = Math.Max(i, start); + var rangeEnd = Math.Min(i + 16, end); + if (rangeEnd > rangeStart) + { + var autoName = $"{mpPrefix}[{j,2:D}]{VectorSwizzle(((offset + rangeStart) & 0xF) >> 2, ((offset + rangeEnd - 1) & 0xF) >> 2)}"; + fcGroup.Add(( + $"{autoName.PadRight(autoNameMaxLength)} (0x{shpkConstant.Id:X8})", + constantIndex, rangeStart..rangeEnd, string.Empty, true, DefaultConstantEditorFor(name))); + } + } + } + else + { + for (var i = start; i < end; i += 16) + { + fcGroup.Add(($"{"???".PadRight(autoNameMaxLength)} (0x{shpkConstant.Id:X8})", constantIndex, i..Math.Min(i + 16, end), string.Empty, true, + DefaultConstantEditorFor(name))); + } + } + } + } + } + + Constants.RemoveAll(group => group.Constants.Count == 0); + Constants.Sort((x, y) => + { + if (string.Equals(x.Header, "Further Constants", StringComparison.Ordinal)) + return 1; + if (string.Equals(y.Header, "Further Constants", StringComparison.Ordinal)) + return -1; + + return string.Compare(x.Header, y.Header, StringComparison.Ordinal); + }); + // HACK the Replace makes w appear after xyz, for the cbuffer-location-based naming scheme, and cbuffer-location names appear after known variable names + foreach (var (_, group) in Constants) + { + group.Sort((x, y) => string.CompareOrdinal( + x.MonoFont ? x.Label.Replace("].w", "].{").Replace(mpPrefix, "}_MaterialParameter") : x.Label, + y.MonoFont ? y.Label.Replace("].w", "].{").Replace(mpPrefix, "}_MaterialParameter") : y.Label)); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private IEditor DefaultConstantEditorFor(Name name) + => ConstantEditors.DefaultFor(name, _materialTemplatePickers); + + private bool DrawConstantsSection(bool disabled) + { + if (Constants.Count == 0) + return false; + + ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); + if (!ImGui.CollapsingHeader("Material Constants")) + return false; + + using var _ = ImRaii.PushId("MaterialConstants"); + + var ret = false; + foreach (var (header, group) in Constants) + { + using var t = ImRaii.TreeNode(header, ImGuiTreeNodeFlags.DefaultOpen); + if (!t) + continue; + + foreach (var (label, constantIndex, slice, description, monoFont, editor) in group) + { + var constant = Mtrl.ShaderPackage.Constants[constantIndex]; + var buffer = Mtrl.GetConstantValue(constant); + if (buffer.Length > 0) + { + using var id = ImRaii.PushId($"##{constant.Id:X8}:{slice.Start}"); + ImGui.SetNextItemWidth(MaterialConstantSize * UiHelpers.Scale); + if (editor.Draw(buffer[slice], disabled)) + { + ret = true; + SetMaterialParameter(constant.Id, slice.Start, buffer[slice]); + } + var shpkConstant = AssociatedShpk?.GetMaterialParamById(constant.Id); + var defaultConstantValue = shpkConstant.HasValue ? AssociatedShpk!.GetMaterialParamDefault(shpkConstant.Value) : []; + var defaultValue = IsValid(slice, defaultConstantValue.Length) ? defaultConstantValue[slice] : []; + var canReset = AssociatedShpk?.MaterialParamsDefaults != null + ? defaultValue.Length > 0 && !defaultValue.SequenceEqual(buffer[slice]) + : buffer[slice].ContainsAnyExcept((byte)0); + ImUtf8.SameLineInner(); + if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Backspace.ToIconString(), ImGui.GetFrameHeight() * Vector2.One, + "Reset this constant to its default value.\n\nHold Ctrl to unlock.", !ImGui.GetIO().KeyCtrl || !canReset, true)) + { + ret = true; + if (defaultValue.Length > 0) + defaultValue.CopyTo(buffer[slice]); + else + buffer[slice].Clear(); + + SetMaterialParameter(constant.Id, slice.Start, buffer[slice]); + } + + ImGui.SameLine(); + using var font = ImRaii.PushFont(UiBuilder.MonoFont, monoFont); + if (description.Length > 0) + ImGuiUtil.LabeledHelpMarker(label, description); + else + ImGui.TextUnformatted(label); + } + } + } + + return ret; + } + + private static bool IsValid(Range range, int length) + { + var start = range.Start.GetOffset(length); + var end = range.End.GetOffset(length); + return start >= 0 && start <= length && end >= start && end <= length; + } + + internal static string? MaterialParamName(bool componentOnly, int offset) + { + if (offset < 0) + return null; + + return (componentOnly, offset & 0x3) switch + { + (true, 0) => "x", + (true, 1) => "y", + (true, 2) => "z", + (true, 3) => "w", + (false, 0) => $"[{offset >> 2:D2}].x", + (false, 1) => $"[{offset >> 2:D2}].y", + (false, 2) => $"[{offset >> 2:D2}].z", + (false, 3) => $"[{offset >> 2:D2}].w", + _ => null, + }; + } + + /// Returned string is 4 chars long. + private static string VectorSwizzle(int firstComponent, int lastComponent) + => (firstComponent, lastComponent) switch + { + (0, 4) => " ", + (0, 0) => ".x ", + (0, 1) => ".xy ", + (0, 2) => ".xyz", + (0, 3) => " ", + (1, 1) => ".y ", + (1, 2) => ".yz ", + (1, 3) => ".yzw", + (2, 2) => ".z ", + (2, 3) => ".zw ", + (3, 3) => ".w ", + _ => string.Empty, + }; + + internal static (string? Name, bool ComponentOnly) MaterialParamRangeName(string prefix, int valueOffset, int valueLength) + { + if (valueLength == 0 || valueOffset < 0) + return (null, false); + + var firstVector = valueOffset >> 2; + var lastVector = (valueOffset + valueLength - 1) >> 2; + var firstComponent = valueOffset & 0x3; + var lastComponent = (valueOffset + valueLength - 1) & 0x3; + if (firstVector == lastVector) + return ($"{prefix}[{firstVector}]{VectorSwizzle(firstComponent, lastComponent)}", true); + + var sb = new StringBuilder(128); + sb.Append($"{prefix}[{firstVector}]{VectorSwizzle(firstComponent, 3).TrimEnd()}"); + for (var i = firstVector + 1; i < lastVector; ++i) + sb.Append($", [{i}]"); + + sb.Append($", [{lastVector}]{VectorSwizzle(0, lastComponent)}"); + return (sb.ToString(), false); + } +} diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Devkit.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Devkit.cs new file mode 100644 index 00000000..cd62d58f --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Devkit.cs @@ -0,0 +1,240 @@ +using Newtonsoft.Json.Linq; +using OtterGui.Text.Widget.Editors; +using Penumbra.String.Classes; +using static Penumbra.GameData.Files.ShpkFile; + +namespace Penumbra.UI.AdvancedWindow.Materials; + +public partial class MtrlTab +{ + private JObject? TryLoadShpkDevkit(string shpkBaseName, out string devkitPathName) + { + try + { + if (!Utf8GamePath.FromString("penumbra/shpk_devkit/" + shpkBaseName + ".json", out var devkitPath)) + throw new Exception("Could not assemble ShPk dev-kit path."); + + var devkitFullPath = _edit.FindBestMatch(devkitPath); + if (!devkitFullPath.IsRooted) + throw new Exception("Could not resolve ShPk dev-kit path."); + + devkitPathName = devkitFullPath.FullName; + return JObject.Parse(File.ReadAllText(devkitFullPath.FullName)); + } + catch + { + devkitPathName = string.Empty; + return null; + } + } + + private T? TryGetShpkDevkitData(string category, uint? id, bool mayVary) where T : class + => TryGetShpkDevkitData(AssociatedShpkDevkit, LoadedShpkDevkitPathName, category, id, mayVary) + ?? TryGetShpkDevkitData(AssociatedBaseDevkit, LoadedBaseDevkitPathName, category, id, mayVary); + + private T? TryGetShpkDevkitData(JObject? devkit, string devkitPathName, string category, uint? id, bool mayVary) where T : class + { + if (devkit == null) + return null; + + try + { + var data = devkit[category]; + if (id.HasValue) + data = data?[id.Value.ToString()]; + + if (mayVary && (data as JObject)?["Vary"] != null) + { + var selector = BuildSelector(data!["Vary"]! + .Select(key => (uint)key) + .Select(key => Mtrl.GetShaderKey(key)?.Value ?? AssociatedShpk!.GetMaterialKeyById(key)!.Value.DefaultValue)); + var index = (int)data["Selectors"]![selector.ToString()]!; + data = data["Items"]![index]; + } + + return data?.ToObject(typeof(T)) as T; + } + catch (Exception e) + { + // Some element in the JSON was undefined or invalid (wrong type, key that doesn't exist in the ShPk, index out of range, …) + Penumbra.Log.Error($"Error while traversing the ShPk dev-kit file at {devkitPathName}: {e}"); + return null; + } + } + + private sealed class DevkitShaderKeyValue + { + public string Label = string.Empty; + public string Description = string.Empty; + } + + private sealed class DevkitShaderKey + { + public string Label = string.Empty; + public string Description = string.Empty; + public Dictionary Values = []; + } + + private sealed class DevkitSampler + { + public string Label = string.Empty; + public string Description = string.Empty; + public string DefaultTexture = string.Empty; + } + + private enum DevkitConstantType + { + Hidden = -1, + Float = 0, + /// Integer encoded as a float. + Integer = 1, + Color = 2, + Enum = 3, + /// Native integer. + Int32 = 4, + Int32Enum = 5, + Int8 = 6, + Int8Enum = 7, + Int16 = 8, + Int16Enum = 9, + Int64 = 10, + Int64Enum = 11, + Half = 12, + Double = 13, + TileIndex = 14, + SphereMapIndex = 15, + } + + private sealed class DevkitConstantValue + { + public string Label = string.Empty; + public string Description = string.Empty; + public double Value = 0; + } + + private sealed class DevkitConstant + { + public uint Offset = 0; + public uint? Length = null; + public uint? ByteOffset = null; + public uint? ByteSize = null; + public string Group = string.Empty; + public string Label = string.Empty; + public string Description = string.Empty; + public DevkitConstantType Type = DevkitConstantType.Float; + + public float? Minimum = null; + public float? Maximum = null; + public float Step = 0.0f; + public float StepFast = 0.0f; + public float? Speed = null; + public float RelativeSpeed = 0.0f; + public float Exponent = 1.0f; + public float Factor = 1.0f; + public float Bias = 0.0f; + public byte Precision = 3; + public bool Hex = false; + public bool Slider = true; + public bool Drag = true; + public string Unit = string.Empty; + + public bool SquaredRgb = false; + public bool Clamped = false; + + public DevkitConstantValue[] Values = []; + + public uint EffectiveByteOffset + => ByteOffset ?? Offset * ValueSize; + + public uint? EffectiveByteSize + => ByteSize ?? (Length * ValueSize); + + public unsafe uint ValueSize + => Type switch + { + DevkitConstantType.Hidden => sizeof(byte), + DevkitConstantType.Float => sizeof(float), + DevkitConstantType.Integer => sizeof(float), + DevkitConstantType.Color => sizeof(float), + DevkitConstantType.Enum => sizeof(float), + DevkitConstantType.Int32 => sizeof(int), + DevkitConstantType.Int32Enum => sizeof(int), + DevkitConstantType.Int8 => sizeof(byte), + DevkitConstantType.Int8Enum => sizeof(byte), + DevkitConstantType.Int16 => sizeof(short), + DevkitConstantType.Int16Enum => sizeof(short), + DevkitConstantType.Int64 => sizeof(long), + DevkitConstantType.Int64Enum => sizeof(long), + DevkitConstantType.Half => (uint)sizeof(Half), + DevkitConstantType.Double => sizeof(double), + DevkitConstantType.TileIndex => sizeof(float), + DevkitConstantType.SphereMapIndex => sizeof(float), + _ => sizeof(float), + }; + + public IEditor? CreateEditor(MaterialTemplatePickers? materialTemplatePickers) + => Type switch + { + DevkitConstantType.Hidden => null, + DevkitConstantType.Float => CreateFloatEditor().AsByteEditor(), + DevkitConstantType.Integer => CreateIntegerEditor().IntAsFloatEditor().AsByteEditor(), + DevkitConstantType.Color => ColorEditor.Get(!Clamped).WithExponent(SquaredRgb ? 2.0f : 1.0f).AsByteEditor(), + DevkitConstantType.Enum => CreateEnumEditor(float.CreateSaturating).AsByteEditor(), + DevkitConstantType.Int32 => CreateIntegerEditor().AsByteEditor(), + DevkitConstantType.Int32Enum => CreateEnumEditor(ToInteger).AsByteEditor(), + DevkitConstantType.Int8 => CreateIntegerEditor(), + DevkitConstantType.Int8Enum => CreateEnumEditor(ToInteger), + DevkitConstantType.Int16 => CreateIntegerEditor().AsByteEditor(), + DevkitConstantType.Int16Enum => CreateEnumEditor(ToInteger).AsByteEditor(), + DevkitConstantType.Int64 => CreateIntegerEditor().AsByteEditor(), + DevkitConstantType.Int64Enum => CreateEnumEditor(ToInteger).AsByteEditor(), + DevkitConstantType.Half => CreateFloatEditor().AsByteEditor(), + DevkitConstantType.Double => CreateFloatEditor().AsByteEditor(), + DevkitConstantType.TileIndex => materialTemplatePickers?.TileIndexPicker ?? ConstantEditors.DefaultIntAsFloat, + DevkitConstantType.SphereMapIndex => materialTemplatePickers?.SphereMapIndexPicker ?? ConstantEditors.DefaultIntAsFloat, + _ => ConstantEditors.DefaultFloat, + }; + + private IEditor CreateIntegerEditor() + where T : unmanaged, INumber + => ((Drag || Slider) && !Hex + ? (Drag + ? (IEditor)DragEditor.CreateInteger(ToInteger(Minimum), ToInteger(Maximum), Speed ?? 0.25f, RelativeSpeed, Unit, 0) + : SliderEditor.CreateInteger(ToInteger(Minimum) ?? default, ToInteger(Maximum) ?? default, Unit, 0)) + : InputEditor.CreateInteger(ToInteger(Minimum), ToInteger(Maximum), ToInteger(Step), ToInteger(StepFast), Hex, Unit, 0)) + .WithFactorAndBias(ToInteger(Factor), ToInteger(Bias)); + + private IEditor CreateFloatEditor() + where T : unmanaged, INumber, IPowerFunctions + => ((Drag || Slider) + ? (Drag + ? (IEditor)DragEditor.CreateFloat(ToFloat(Minimum), ToFloat(Maximum), Speed ?? 0.1f, RelativeSpeed, Precision, Unit, 0) + : SliderEditor.CreateFloat(ToFloat(Minimum) ?? default, ToFloat(Maximum) ?? default, Precision, Unit, 0)) + : InputEditor.CreateFloat(ToFloat(Minimum), ToFloat(Maximum), T.CreateSaturating(Step), T.CreateSaturating(StepFast), Precision, Unit, 0)) + .WithExponent(T.CreateSaturating(Exponent)) + .WithFactorAndBias(T.CreateSaturating(Factor), T.CreateSaturating(Bias)); + + private EnumEditor CreateEnumEditor(Func convertValue) + where T : unmanaged, IUtf8SpanFormattable, IEqualityOperators + => new(Array.ConvertAll(Values, value => (ToUtf8(value.Label), convertValue(value.Value), ToUtf8(value.Description)))); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static T ToInteger(float value) where T : struct, INumberBase + => T.CreateSaturating(MathF.Round(value)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static T ToInteger(double value) where T : struct, INumberBase + => T.CreateSaturating(Math.Round(value)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static T? ToInteger(float? value) where T : struct, INumberBase + => value.HasValue ? ToInteger(value.Value) : null; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static T? ToFloat(float? value) where T : struct, INumberBase + => value.HasValue ? T.CreateSaturating(value.Value) : null; + + private static ReadOnlyMemory ToUtf8(string value) + => Encoding.UTF8.GetBytes(value); + } +} diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs new file mode 100644 index 00000000..f3ec5307 --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs @@ -0,0 +1,368 @@ +using Dalamud.Interface; +using Dalamud.Interface.Utility.Raii; +using ImGuiNET; +using OtterGui; +using OtterGui.Text; +using Penumbra.GameData.Files.MaterialStructs; +using Penumbra.GameData.Files.StainMapStructs; +using Penumbra.Services; + +namespace Penumbra.UI.AdvancedWindow.Materials; + +public partial class MtrlTab +{ + private const float LegacyColorTableFloatSize = 65.0f; + private const float LegacyColorTablePercentageSize = 50.0f; + private const float LegacyColorTableIntegerSize = 40.0f; + private const float LegacyColorTableByteSize = 25.0f; + + private bool DrawLegacyColorTable(LegacyColorTable table, LegacyColorDyeTable? dyeTable, bool disabled) + { + using var imTable = ImUtf8.Table("##ColorTable"u8, dyeTable != null ? 10 : 8, + ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg | ImGuiTableFlags.BordersInnerV); + if (!imTable) + return false; + + DrawLegacyColorTableHeader(dyeTable != null); + + var ret = false; + for (var i = 0; i < LegacyColorTable.NumRows; ++i) + { + if (DrawLegacyColorTableRow(table, dyeTable, i, disabled)) + { + UpdateColorTableRowPreview(i); + ret = true; + } + ImGui.TableNextRow(); + } + + return ret; + } + + private bool DrawLegacyColorTable(ColorTable table, ColorDyeTable? dyeTable, bool disabled) + { + using var imTable = ImUtf8.Table("##ColorTable"u8, dyeTable != null ? 10 : 8, + ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg | ImGuiTableFlags.BordersInnerV); + if (!imTable) + return false; + + DrawLegacyColorTableHeader(dyeTable != null); + + var ret = false; + for (var i = 0; i < ColorTable.NumRows; ++i) + { + if (DrawLegacyColorTableRow(table, dyeTable, i, disabled)) + { + UpdateColorTableRowPreview(i); + ret = true; + } + ImGui.TableNextRow(); + } + + return ret; + } + + private static void DrawLegacyColorTableHeader(bool hasDyeTable) + { + ImGui.TableNextColumn(); + ImUtf8.TableHeader(default(ReadOnlySpan)); + ImGui.TableNextColumn(); + ImUtf8.TableHeader("Row"u8); + ImGui.TableNextColumn(); + ImUtf8.TableHeader("Diffuse"u8); + ImGui.TableNextColumn(); + ImUtf8.TableHeader("Specular"u8); + ImGui.TableNextColumn(); + ImUtf8.TableHeader("Emissive"u8); + ImGui.TableNextColumn(); + ImUtf8.TableHeader("Gloss"u8); + ImGui.TableNextColumn(); + ImUtf8.TableHeader("Tile"u8); + ImGui.TableNextColumn(); + ImUtf8.TableHeader("Repeat / Skew"u8); + if (hasDyeTable) + { + ImGui.TableNextColumn(); + ImUtf8.TableHeader("Dye"u8); + ImGui.TableNextColumn(); + ImUtf8.TableHeader("Dye Preview"u8); + } + } + + private bool DrawLegacyColorTableRow(LegacyColorTable table, LegacyColorDyeTable? dyeTable, int rowIdx, bool disabled) + { + using var id = ImRaii.PushId(rowIdx); + ref var row = ref table[rowIdx]; + var dye = dyeTable != null ? dyeTable[rowIdx] : default; + var floatSize = LegacyColorTableFloatSize * UiHelpers.Scale; + var pctSize = LegacyColorTablePercentageSize * UiHelpers.Scale; + var intSize = LegacyColorTableIntegerSize * UiHelpers.Scale; + ImGui.TableNextColumn(); + ColorTableCopyClipboardButton(rowIdx); + ImUtf8.SameLineInner(); + var ret = ColorTablePasteFromClipboardButton(rowIdx, disabled); + if ((rowIdx & 1) == 0) + { + ImUtf8.SameLineInner(); + ColorTableHighlightButton(rowIdx >> 1, disabled); + } + + ImGui.TableNextColumn(); + using (var font = ImRaii.PushFont(UiBuilder.MonoFont)) + ImUtf8.Text($"{(rowIdx >> 1) + 1,2:D}{"AB"[rowIdx & 1]}"); + + ImGui.TableNextColumn(); + using var dis = ImRaii.Disabled(disabled); + ret |= CtColorPicker("##Diffuse"u8, "Diffuse Color"u8, row.DiffuseColor, + c => table[rowIdx].DiffuseColor = c); + if (dyeTable != null) + { + ImUtf8.SameLineInner(); + ret |= CtApplyStainCheckbox("##dyeDiffuse"u8, "Apply Diffuse Color on Dye"u8, dye.DiffuseColor, + b => dyeTable[rowIdx].DiffuseColor = b); + } + + ImGui.TableNextColumn(); + ret |= CtColorPicker("##Specular"u8, "Specular Color"u8, row.SpecularColor, + c => table[rowIdx].SpecularColor = c); + if (dyeTable != null) + { + ImUtf8.SameLineInner(); + ret |= CtApplyStainCheckbox("##dyeSpecular"u8, "Apply Specular Color on Dye"u8, dye.SpecularColor, + b => dyeTable[rowIdx].SpecularColor = b); + } + ImGui.SameLine(); + ImGui.SetNextItemWidth(pctSize); + ret |= CtDragScalar("##SpecularMask"u8, "Specular Strength"u8, (float)row.SpecularMask * 100.0f, "%.0f%%"u8, 0f, HalfMaxValue * 100.0f, 1.0f, + v => table[rowIdx].SpecularMask = (Half)(v * 0.01f)); + if (dyeTable != null) + { + ImUtf8.SameLineInner(); + ret |= CtApplyStainCheckbox("##dyeSpecularMask"u8, "Apply Specular Strength on Dye"u8, dye.SpecularMask, + b => dyeTable[rowIdx].SpecularMask = b); + } + + ImGui.TableNextColumn(); + ret |= CtColorPicker("##Emissive"u8, "Emissive Color"u8, row.EmissiveColor, + c => table[rowIdx].EmissiveColor = c); + if (dyeTable != null) + { + ImUtf8.SameLineInner(); + ret |= CtApplyStainCheckbox("##dyeEmissive"u8, "Apply Emissive Color on Dye"u8, dye.EmissiveColor, + b => dyeTable[rowIdx].EmissiveColor = b); + } + + ImGui.TableNextColumn(); + ImGui.SetNextItemWidth(floatSize); + var glossStrengthMin = ImGui.GetIO().KeyCtrl ? 0.0f : HalfEpsilon; + ret |= CtDragHalf("##Shininess"u8, "Gloss Strength"u8, row.Shininess, "%.1f"u8, glossStrengthMin, HalfMaxValue, Math.Max(0.1f, (float)row.Shininess * 0.025f), + v => table[rowIdx].Shininess = v); + + if (dyeTable != null) + { + ImUtf8.SameLineInner(); + ret |= CtApplyStainCheckbox("##dyeShininess"u8, "Apply Gloss Strength on Dye"u8, dye.Shininess, + b => dyeTable[rowIdx].Shininess = b); + } + + ImGui.TableNextColumn(); + ImGui.SetNextItemWidth(intSize); + ret |= CtTileIndexPicker("##TileIndex"u8, "Tile Index"u8, row.TileIndex, true, + value => table[rowIdx].TileIndex = value); + + ImGui.TableNextColumn(); + ret |= CtTileTransformMatrix(row.TileTransform, floatSize, false, + m => table[rowIdx].TileTransform = m); + + if (dyeTable != null) + { + ImGui.TableNextColumn(); + if (_stainService.LegacyTemplateCombo.Draw("##dyeTemplate", dye.Template.ToString(), string.Empty, intSize + + ImGui.GetStyle().ScrollbarSize / 2, ImGui.GetTextLineHeightWithSpacing(), ImGuiComboFlags.NoArrowButton)) + { + dyeTable[rowIdx].Template = _stainService.LegacyTemplateCombo.CurrentSelection; + ret = true; + } + + ImGuiUtil.HoverTooltip("Dye Template", ImGuiHoveredFlags.AllowWhenDisabled); + + ImGui.TableNextColumn(); + ret |= DrawLegacyDyePreview(rowIdx, disabled, dye, floatSize); + } + + return ret; + } + + private bool DrawLegacyColorTableRow(ColorTable table, ColorDyeTable? dyeTable, int rowIdx, bool disabled) + { + using var id = ImRaii.PushId(rowIdx); + ref var row = ref table[rowIdx]; + var dye = dyeTable != null ? dyeTable[rowIdx] : default; + var floatSize = LegacyColorTableFloatSize * UiHelpers.Scale; + var pctSize = LegacyColorTablePercentageSize * UiHelpers.Scale; + var intSize = LegacyColorTableIntegerSize * UiHelpers.Scale; + var byteSize = LegacyColorTableByteSize * UiHelpers.Scale; + ImGui.TableNextColumn(); + ColorTableCopyClipboardButton(rowIdx); + ImUtf8.SameLineInner(); + var ret = ColorTablePasteFromClipboardButton(rowIdx, disabled); + if ((rowIdx & 1) == 0) + { + ImUtf8.SameLineInner(); + ColorTableHighlightButton(rowIdx >> 1, disabled); + } + + ImGui.TableNextColumn(); + using (var font = ImRaii.PushFont(UiBuilder.MonoFont)) + ImUtf8.Text($"{(rowIdx >> 1) + 1,2:D}{"AB"[rowIdx & 1]}"); + + ImGui.TableNextColumn(); + using var dis = ImRaii.Disabled(disabled); + ret |= CtColorPicker("##Diffuse"u8, "Diffuse Color"u8, row.DiffuseColor, + c => table[rowIdx].DiffuseColor = c); + if (dyeTable != null) + { + ImUtf8.SameLineInner(); + ret |= CtApplyStainCheckbox("##dyeDiffuse"u8, "Apply Diffuse Color on Dye"u8, dye.DiffuseColor, + b => dyeTable[rowIdx].DiffuseColor = b); + } + + ImGui.TableNextColumn(); + ret |= CtColorPicker("##Specular"u8, "Specular Color"u8, row.SpecularColor, + c => table[rowIdx].SpecularColor = c); + if (dyeTable != null) + { + ImUtf8.SameLineInner(); + ret |= CtApplyStainCheckbox("##dyeSpecular"u8, "Apply Specular Color on Dye"u8, dye.SpecularColor, + b => dyeTable[rowIdx].SpecularColor = b); + } + ImGui.SameLine(); + ImGui.SetNextItemWidth(pctSize); + ret |= CtDragScalar("##SpecularMask"u8, "Specular Strength"u8, (float)row.Scalar7 * 100.0f, "%.0f%%"u8, 0f, HalfMaxValue * 100.0f, 1.0f, + v => table[rowIdx].Scalar7 = (Half)(v * 0.01f)); + if (dyeTable != null) + { + ImUtf8.SameLineInner(); + ret |= CtApplyStainCheckbox("##dyeSpecularMask"u8, "Apply Specular Strength on Dye"u8, dye.Metalness, + b => dyeTable[rowIdx].Metalness = b); + } + + ImGui.TableNextColumn(); + ret |= CtColorPicker("##Emissive"u8, "Emissive Color"u8, row.EmissiveColor, + c => table[rowIdx].EmissiveColor = c); + if (dyeTable != null) + { + ImUtf8.SameLineInner(); + ret |= CtApplyStainCheckbox("##dyeEmissive"u8, "Apply Emissive Color on Dye"u8, dye.EmissiveColor, + b => dyeTable[rowIdx].EmissiveColor = b); + } + + ImGui.TableNextColumn(); + ImGui.SetNextItemWidth(floatSize); + var glossStrengthMin = ImGui.GetIO().KeyCtrl ? 0.0f : HalfEpsilon; + ret |= CtDragHalf("##Shininess"u8, "Gloss Strength"u8, row.Scalar3, "%.1f"u8, glossStrengthMin, HalfMaxValue, Math.Max(0.1f, (float)row.Scalar3 * 0.025f), + v => table[rowIdx].Scalar3 = v); + + if (dyeTable != null) + { + ImUtf8.SameLineInner(); + ret |= CtApplyStainCheckbox("##dyeShininess"u8, "Apply Gloss Strength on Dye"u8, dye.Scalar3, + b => dyeTable[rowIdx].Scalar3 = b); + } + + ImGui.TableNextColumn(); + ImGui.SetNextItemWidth(intSize); + ret |= CtTileIndexPicker("##TileIndex"u8, "Tile Index"u8, row.TileIndex, true, + value => table[rowIdx].TileIndex = value); + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(pctSize); + ret |= CtDragScalar("##TileAlpha"u8, "Tile Opacity"u8, (float)row.TileAlpha * 100.0f, "%.0f%%"u8, 0f, HalfMaxValue * 100.0f, 1.0f, + v => table[rowIdx].TileAlpha = (Half)(v * 0.01f)); + + ImGui.TableNextColumn(); + ret |= CtTileTransformMatrix(row.TileTransform, floatSize, false, + m => table[rowIdx].TileTransform = m); + + if (dyeTable != null) + { + ImGui.TableNextColumn(); + ImGui.SetNextItemWidth(byteSize); + ret |= CtDragScalar("##DyeChannel"u8, "Dye Channel"u8, dye.Channel + 1, "%hhd"u8, 1, StainService.ChannelCount, 0.25f, + value => dyeTable[rowIdx].Channel = (byte)(Math.Clamp(value, 1, StainService.ChannelCount) - 1)); + ImUtf8.SameLineInner(); + _stainService.LegacyTemplateCombo.CurrentDyeChannel = dye.Channel; + if (_stainService.LegacyTemplateCombo.Draw("##dyeTemplate", dye.Template.ToString(), string.Empty, intSize + + ImGui.GetStyle().ScrollbarSize / 2, ImGui.GetTextLineHeightWithSpacing(), ImGuiComboFlags.NoArrowButton)) + { + dyeTable[rowIdx].Template = _stainService.LegacyTemplateCombo.CurrentSelection; + ret = true; + } + + ImGuiUtil.HoverTooltip("Dye Template", ImGuiHoveredFlags.AllowWhenDisabled); + + ImGui.TableNextColumn(); + ret |= DrawLegacyDyePreview(rowIdx, disabled, dye, floatSize); + } + + return ret; + } + + private bool DrawLegacyDyePreview(int rowIdx, bool disabled, LegacyColorDyeTable.Row dye, float floatSize) + { + var stain = _stainService.StainCombo1.CurrentSelection.Key; + if (stain == 0 || !_stainService.LegacyStmFile.TryGetValue(dye.Template, stain, out var values)) + return false; + + using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, ImGui.GetStyle().ItemSpacing / 2); + + var ret = ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.PaintBrush.ToIconString(), new Vector2(ImGui.GetFrameHeight()), + "Apply the selected dye to this row.", disabled, true); + + ret = ret && Mtrl.ApplyDyeToRow(_stainService.LegacyStmFile, [stain], rowIdx); + + ImGui.SameLine(); + DrawLegacyDyePreview(values, floatSize); + + return ret; + } + + private bool DrawLegacyDyePreview(int rowIdx, bool disabled, ColorDyeTable.Row dye, float floatSize) + { + var stain = _stainService.GetStainCombo(dye.Channel).CurrentSelection.Key; + if (stain == 0 || !_stainService.LegacyStmFile.TryGetValue(dye.Template, stain, out var values)) + return false; + + using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, ImGui.GetStyle().ItemSpacing / 2); + + var ret = ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.PaintBrush.ToIconString(), new Vector2(ImGui.GetFrameHeight()), + "Apply the selected dye to this row.", disabled, true); + + ret = ret && Mtrl.ApplyDyeToRow(_stainService.LegacyStmFile, [ + _stainService.StainCombo1.CurrentSelection.Key, + _stainService.StainCombo2.CurrentSelection.Key, + ], rowIdx); + + ImGui.SameLine(); + DrawLegacyDyePreview(values, floatSize); + + return ret; + } + + private static void DrawLegacyDyePreview(LegacyDyePack values, float floatSize) + { + CtColorPicker("##diffusePreview"u8, default, values.DiffuseColor, "D"u8); + ImUtf8.SameLineInner(); + CtColorPicker("##specularPreview"u8, default, values.SpecularColor, "S"u8); + ImUtf8.SameLineInner(); + CtColorPicker("##emissivePreview"u8, default, values.EmissiveColor, "E"u8); + ImUtf8.SameLineInner(); + using var dis = ImRaii.Disabled(); + ImGui.SetNextItemWidth(floatSize); + var shininess = (float)values.Shininess; + ImGui.DragFloat("##shininessPreview", ref shininess, 0, shininess, shininess, "%.1f G"); + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(floatSize); + var specularMask = (float)values.SpecularMask * 100.0f; + ImGui.DragFloat("##specularMaskPreview", ref specularMask, 0, specularMask, specularMask, "%.0f%% S"); + } +} diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs new file mode 100644 index 00000000..bb346534 --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs @@ -0,0 +1,272 @@ +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using ImGuiNET; +using OtterGui.Raii; +using OtterGui.Text; +using Penumbra.GameData.Files.MaterialStructs; +using Penumbra.GameData.Structs; +using Penumbra.Interop.MaterialPreview; +using Penumbra.Services; +using Penumbra.UI.Classes; + +namespace Penumbra.UI.AdvancedWindow.Materials; + +public partial class MtrlTab +{ + public readonly List MaterialPreviewers = new(4); + public readonly List ColorTablePreviewers = new(4); + public int HighlightedColorTablePair = -1; + public readonly Stopwatch HighlightTime = new(); + + private void DrawMaterialLivePreviewRebind(bool disabled) + { + if (disabled) + return; + + if (ImGui.Button("Reload live preview")) + BindToMaterialInstances(); + + if (MaterialPreviewers.Count != 0 || ColorTablePreviewers.Count != 0) + return; + + ImGui.SameLine(); + using var c = ImRaii.PushColor(ImGuiCol.Text, Colors.RegexWarningBorder); + ImUtf8.Text( + "The current material has not been found on your character. Please check the Import from Screen tab for more information."u8); + } + + public unsafe void BindToMaterialInstances() + { + UnbindFromMaterialInstances(); + + var instances = MaterialInfo.FindMaterials(_resourceTreeFactory.GetLocalPlayerRelatedCharacters().Select(ch => ch.Address), + FilePath); + + var foundMaterials = new HashSet(); + foreach (var materialInfo in instances) + { + var material = materialInfo.GetDrawObjectMaterial(_objects); + if (foundMaterials.Contains((nint)material)) + continue; + + try + { + MaterialPreviewers.Add(new LiveMaterialPreviewer(_objects, materialInfo)); + foundMaterials.Add((nint)material); + } + catch (InvalidOperationException) + { + // Carry on without that previewer. + } + } + + UpdateMaterialPreview(); + + if (Mtrl.Table == null) + return; + + foreach (var materialInfo in instances) + { + try + { + ColorTablePreviewers.Add(new LiveColorTablePreviewer(_objects, _framework, materialInfo)); + } + catch (InvalidOperationException) + { + // Carry on without that previewer. + } + } + + UpdateColorTablePreview(); + } + + private void UnbindFromMaterialInstances() + { + foreach (var previewer in MaterialPreviewers) + previewer.Dispose(); + MaterialPreviewers.Clear(); + + foreach (var previewer in ColorTablePreviewers) + previewer.Dispose(); + ColorTablePreviewers.Clear(); + } + + private unsafe void UnbindFromDrawObjectMaterialInstances(CharacterBase* characterBase) + { + for (var i = MaterialPreviewers.Count; i-- > 0;) + { + var previewer = MaterialPreviewers[i]; + if (previewer.DrawObject != characterBase) + continue; + + previewer.Dispose(); + MaterialPreviewers.RemoveAt(i); + } + + for (var i = ColorTablePreviewers.Count; i-- > 0;) + { + var previewer = ColorTablePreviewers[i]; + if (previewer.DrawObject != characterBase) + continue; + + previewer.Dispose(); + ColorTablePreviewers.RemoveAt(i); + } + } + + public void SetShaderPackageFlags(uint shPkFlags) + { + foreach (var previewer in MaterialPreviewers) + previewer.SetShaderPackageFlags(shPkFlags); + } + + public void SetMaterialParameter(uint parameterCrc, Index offset, Span value) + { + foreach (var previewer in MaterialPreviewers) + previewer.SetMaterialParameter(parameterCrc, offset, value); + } + + public void SetSamplerFlags(uint samplerCrc, uint samplerFlags) + { + foreach (var previewer in MaterialPreviewers) + previewer.SetSamplerFlags(samplerCrc, samplerFlags); + } + + private void UpdateMaterialPreview() + { + SetShaderPackageFlags(Mtrl.ShaderPackage.Flags); + foreach (var constant in Mtrl.ShaderPackage.Constants) + { + var values = Mtrl.GetConstantValue(constant); + if (values != null) + SetMaterialParameter(constant.Id, 0, values); + } + + foreach (var sampler in Mtrl.ShaderPackage.Samplers) + SetSamplerFlags(sampler.SamplerId, sampler.Flags); + } + + public void HighlightColorTablePair(int pairIdx) + { + var oldPairIdx = HighlightedColorTablePair; + + if (HighlightedColorTablePair != pairIdx) + { + HighlightedColorTablePair = pairIdx; + HighlightTime.Restart(); + } + + if (oldPairIdx >= 0) + { + UpdateColorTableRowPreview(oldPairIdx << 1); + UpdateColorTableRowPreview((oldPairIdx << 1) | 1); + } + if (pairIdx >= 0) + { + UpdateColorTableRowPreview(pairIdx << 1); + UpdateColorTableRowPreview((pairIdx << 1) | 1); + } + } + + public void CancelColorTableHighlight() + { + var pairIdx = HighlightedColorTablePair; + + HighlightedColorTablePair = -1; + HighlightTime.Reset(); + + if (pairIdx >= 0) + { + UpdateColorTableRowPreview(pairIdx << 1); + UpdateColorTableRowPreview((pairIdx << 1) | 1); + } + } + + public void UpdateColorTableRowPreview(int rowIdx) + { + if (ColorTablePreviewers.Count == 0) + return; + + if (Mtrl.Table == null) + return; + + var row = Mtrl.Table switch + { + LegacyColorTable legacyTable => new ColorTable.Row(legacyTable[rowIdx]), + ColorTable table => table[rowIdx], + _ => throw new InvalidOperationException($"Unsupported color table type {Mtrl.Table.GetType()}"), + }; + if (Mtrl.DyeTable != null) + { + var dyeRow = Mtrl.DyeTable switch + { + LegacyColorDyeTable legacyDyeTable => new ColorDyeTable.Row(legacyDyeTable[rowIdx]), + ColorDyeTable dyeTable => dyeTable[rowIdx], + _ => throw new InvalidOperationException($"Unsupported color dye table type {Mtrl.DyeTable.GetType()}"), + }; + if (dyeRow.Channel < StainService.ChannelCount) + { + StainId stainId = _stainService.GetStainCombo(dyeRow.Channel).CurrentSelection.Key; + if (_stainService.LegacyStmFile.TryGetValue(dyeRow.Template, stainId, out var legacyDyes)) + row.ApplyDye(dyeRow, legacyDyes); + if (_stainService.GudStmFile.TryGetValue(dyeRow.Template, stainId, out var gudDyes)) + row.ApplyDye(dyeRow, gudDyes); + } + } + + if (HighlightedColorTablePair << 1 == rowIdx) + ApplyHighlight(ref row, ColorId.InGameHighlight, (float)HighlightTime.Elapsed.TotalSeconds); + else if (((HighlightedColorTablePair << 1) | 1) == rowIdx) + ApplyHighlight(ref row, ColorId.InGameHighlight2, (float)HighlightTime.Elapsed.TotalSeconds); + + foreach (var previewer in ColorTablePreviewers) + { + row[..].CopyTo(previewer.GetColorRow(rowIdx)); + previewer.ScheduleUpdate(); + } + } + + public void UpdateColorTablePreview() + { + if (ColorTablePreviewers.Count == 0) + return; + + if (Mtrl.Table == null) + return; + + var rows = new ColorTable(Mtrl.Table); + var dyeRows = Mtrl.DyeTable != null ? ColorDyeTable.CastOrConvert(Mtrl.DyeTable) : null; + if (dyeRows != null) + { + ReadOnlySpan stainIds = [ + _stainService.StainCombo1.CurrentSelection.Key, + _stainService.StainCombo2.CurrentSelection.Key, + ]; + rows.ApplyDye(_stainService.LegacyStmFile, stainIds, dyeRows); + rows.ApplyDye(_stainService.GudStmFile, stainIds, dyeRows); + } + + if (HighlightedColorTablePair >= 0) + { + ApplyHighlight(ref rows[HighlightedColorTablePair << 1], ColorId.InGameHighlight, (float)HighlightTime.Elapsed.TotalSeconds); + ApplyHighlight(ref rows[(HighlightedColorTablePair << 1) | 1], ColorId.InGameHighlight2, (float)HighlightTime.Elapsed.TotalSeconds); + } + + foreach (var previewer in ColorTablePreviewers) + { + rows.AsHalves().CopyTo(previewer.ColorTable); + previewer.ScheduleUpdate(); + } + } + + private static void ApplyHighlight(ref ColorTable.Row row, ColorId colorId, float time) + { + var level = (MathF.Sin(time * 2.0f * MathF.PI) + 2.0f) / 3.0f / 255.0f; + var baseColor = colorId.Value(); + var color = level * new Vector3(baseColor & 0xFF, (baseColor >> 8) & 0xFF, (baseColor >> 16) & 0xFF); + var halfColor = (HalfColor)(color * color); + + row.DiffuseColor = halfColor; + row.SpecularColor = halfColor; + row.EmissiveColor = halfColor; + } +} diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ShaderPackage.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ShaderPackage.cs new file mode 100644 index 00000000..21557939 --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ShaderPackage.cs @@ -0,0 +1,505 @@ +using Dalamud.Interface; +using Dalamud.Interface.ImGuiNotification; +using ImGuiNET; +using Newtonsoft.Json.Linq; +using OtterGui; +using OtterGui.Classes; +using OtterGui.Raii; +using OtterGui.Text; +using Penumbra.GameData; +using Penumbra.GameData.Data; +using Penumbra.GameData.Files; +using Penumbra.GameData.Files.ShaderStructs; +using Penumbra.String.Classes; +using static Penumbra.GameData.Files.ShpkFile; + +namespace Penumbra.UI.AdvancedWindow.Materials; + +public partial class MtrlTab +{ + // strings path/to/the.exe | grep --fixed-strings '.shpk' | sort -u | sed -e 's#^shader/sm5/shpk/##' + // Apricot shader packages are unlisted because + // 1. they cause severe performance/memory issues when calculating the effective shader set + // 2. they probably aren't intended for use with materials anyway + internal static readonly IReadOnlyList StandardShaderPackages = new[] + { + "3dui.shpk", + // "apricot_decal_dummy.shpk", + // "apricot_decal_ring.shpk", + // "apricot_decal.shpk", + // "apricot_fogModel.shpk", + // "apricot_gbuffer_decal_dummy.shpk", + // "apricot_gbuffer_decal_ring.shpk", + // "apricot_gbuffer_decal.shpk", + // "apricot_lightmodel.shpk", + // "apricot_model_dummy.shpk", + // "apricot_model_morph.shpk", + // "apricot_model.shpk", + // "apricot_powder_dummy.shpk", + // "apricot_powder.shpk", + // "apricot_shape_dummy.shpk", + // "apricot_shape.shpk", + "bgcolorchange.shpk", + "bg_composite.shpk", + "bgcrestchange.shpk", + "bgdecal.shpk", + "bgprop.shpk", + "bg.shpk", + "bguvscroll.shpk", + "characterglass.shpk", + "characterinc.shpk", + "characterlegacy.shpk", + "characterocclusion.shpk", + "characterreflection.shpk", + "characterscroll.shpk", + "charactershadowoffset.shpk", + "character.shpk", + "characterstockings.shpk", + "charactertattoo.shpk", + "charactertransparency.shpk", + "cloud.shpk", + "createviewposition.shpk", + "crystal.shpk", + "directionallighting.shpk", + "directionalshadow.shpk", + "furblur.shpk", + "grassdynamicwave.shpk", + "grass.shpk", + "hairmask.shpk", + "hair.shpk", + "iris.shpk", + "lightshaft.shpk", + "linelighting.shpk", + "planelighting.shpk", + "pointlighting.shpk", + "river.shpk", + "shadowmask.shpk", + "skin.shpk", + "spotlighting.shpk", + "subsurfaceblur.shpk", + "verticalfog.shpk", + "water.shpk", + "weather.shpk", + }; + + private static readonly byte[] UnknownShadersString = Encoding.UTF8.GetBytes("Vertex Shaders: ???\nPixel Shaders: ???"); + + private string[]? _shpkNames; + + public string ShaderHeader = "Shader###Shader"; + public FullPath LoadedShpkPath = FullPath.Empty; + public string LoadedShpkPathName = string.Empty; + public string LoadedShpkDevkitPathName = string.Empty; + public string ShaderComment = string.Empty; + public ShpkFile? AssociatedShpk; + public bool ShpkLoading; + public JObject? AssociatedShpkDevkit; + + public readonly string LoadedBaseDevkitPathName; + public readonly JObject? AssociatedBaseDevkit; + + // Shader Key State + public readonly + List<(string Label, int Index, string Description, bool MonoFont, IReadOnlyList<(string Label, uint Value, string Description)> + Values)> ShaderKeys = new(16); + + public readonly HashSet VertexShaders = new(16); + public readonly HashSet PixelShaders = new(16); + public bool ShadersKnown; + public ReadOnlyMemory ShadersString = UnknownShadersString; + + public string[] GetShpkNames() + { + if (null != _shpkNames) + return _shpkNames; + + var names = new HashSet(StandardShaderPackages); + names.UnionWith(_edit.FindPathsStartingWith(ShpkPrefix).Select(path => path.ToString()[ShpkPrefixLength..])); + + _shpkNames = names.ToArray(); + Array.Sort(_shpkNames); + + return _shpkNames; + } + + public FullPath FindAssociatedShpk(out string defaultPath, out Utf8GamePath defaultGamePath) + { + defaultPath = GamePaths.Shader.ShpkPath(Mtrl.ShaderPackage.Name); + if (!Utf8GamePath.FromString(defaultPath, out defaultGamePath)) + return FullPath.Empty; + + return _edit.FindBestMatch(defaultGamePath); + } + + public void LoadShpk(FullPath path) + => Task.Run(() => DoLoadShpk(path)); + + private async Task DoLoadShpk(FullPath path) + { + ShadersKnown = false; + ShaderHeader = $"Shader ({Mtrl.ShaderPackage.Name})###Shader"; + ShpkLoading = true; + + try + { + var data = path.IsRooted + ? await File.ReadAllBytesAsync(path.FullName) + : _gameData.GetFile(path.InternalName.ToString())?.Data; + LoadedShpkPath = path; + AssociatedShpk = data?.Length > 0 ? new ShpkFile(data) : throw new Exception("Failure to load file data."); + LoadedShpkPathName = path.ToPath(); + } + catch (Exception e) + { + LoadedShpkPath = FullPath.Empty; + LoadedShpkPathName = string.Empty; + AssociatedShpk = null; + Penumbra.Messager.NotificationMessage(e, $"Could not load {LoadedShpkPath.ToPath()}.", NotificationType.Error, false); + } + finally + { + ShpkLoading = false; + } + + if (LoadedShpkPath.InternalName.IsEmpty) + { + AssociatedShpkDevkit = null; + LoadedShpkDevkitPathName = string.Empty; + } + else + { + AssociatedShpkDevkit = + TryLoadShpkDevkit(Path.GetFileNameWithoutExtension(Mtrl.ShaderPackage.Name), out LoadedShpkDevkitPathName); + } + + UpdateShaderKeys(); + _updateOnNextFrame = true; + } + + private void UpdateShaderKeys() + { + ShaderKeys.Clear(); + if (AssociatedShpk != null) + foreach (var key in AssociatedShpk.MaterialKeys) + { + var keyName = Names.KnownNames.TryResolve(key.Id); + var dkData = TryGetShpkDevkitData("ShaderKeys", key.Id, false); + var hasDkLabel = !string.IsNullOrEmpty(dkData?.Label); + + var valueSet = new HashSet(key.Values); + if (dkData != null) + valueSet.UnionWith(dkData.Values.Keys); + + var valueKnownNames = keyName.WithKnownSuffixes(); + + var mtrlKeyIndex = Mtrl.FindOrAddShaderKey(key.Id, key.DefaultValue); + var values = valueSet.Select(value => + { + var valueName = valueKnownNames.TryResolve(Names.KnownNames, value); + if (dkData != null && dkData.Values.TryGetValue(value, out var dkValue)) + return (dkValue.Label.Length > 0 ? dkValue.Label : valueName.ToString(), value, dkValue.Description); + + return (valueName.ToString(), value, string.Empty); + }).ToArray(); + Array.Sort(values, (x, y) => + { + if (x.Value == key.DefaultValue) + return -1; + if (y.Value == key.DefaultValue) + return 1; + + return string.Compare(x.Label, y.Label, StringComparison.Ordinal); + }); + ShaderKeys.Add((hasDkLabel ? dkData!.Label : keyName.ToString(), mtrlKeyIndex, dkData?.Description ?? string.Empty, + !hasDkLabel, values)); + } + else + foreach (var (key, index) in Mtrl.ShaderPackage.ShaderKeys.WithIndex()) + { + var keyName = Names.KnownNames.TryResolve(key.Category); + var valueName = keyName.WithKnownSuffixes().TryResolve(Names.KnownNames, key.Value); + ShaderKeys.Add((keyName.ToString(), index, string.Empty, true, [(valueName.ToString(), key.Value, string.Empty)])); + } + } + + private void UpdateShaders() + { + static void AddShader(HashSet globalSet, Dictionary> byPassSets, uint passId, int shaderIndex) + { + globalSet.Add(shaderIndex); + if (!byPassSets.TryGetValue(passId, out var passSet)) + { + passSet = []; + byPassSets.Add(passId, passSet); + } + passSet.Add(shaderIndex); + } + + VertexShaders.Clear(); + PixelShaders.Clear(); + + var vertexShadersByPass = new Dictionary>(); + var pixelShadersByPass = new Dictionary>(); + + if (AssociatedShpk == null || !AssociatedShpk.IsExhaustiveNodeAnalysisFeasible()) + { + ShadersKnown = false; + } + else + { + ShadersKnown = true; + var systemKeySelectors = AllSelectors(AssociatedShpk.SystemKeys).ToArray(); + var sceneKeySelectors = AllSelectors(AssociatedShpk.SceneKeys).ToArray(); + var subViewKeySelectors = AllSelectors(AssociatedShpk.SubViewKeys).ToArray(); + var materialKeySelector = + BuildSelector(AssociatedShpk.MaterialKeys.Select(key => Mtrl.GetOrAddShaderKey(key.Id, key.DefaultValue).Value)); + + foreach (var systemKeySelector in systemKeySelectors) + { + foreach (var sceneKeySelector in sceneKeySelectors) + { + foreach (var subViewKeySelector in subViewKeySelectors) + { + var selector = BuildSelector(systemKeySelector, sceneKeySelector, materialKeySelector, subViewKeySelector); + var node = AssociatedShpk.GetNodeBySelector(selector); + if (node.HasValue) + foreach (var pass in node.Value.Passes) + { + AddShader(VertexShaders, vertexShadersByPass, pass.Id, (int)pass.VertexShader); + AddShader(PixelShaders, pixelShadersByPass, pass.Id, (int)pass.PixelShader); + } + else + ShadersKnown = false; + } + } + } + } + + if (ShadersKnown) + { + var builder = new StringBuilder(); + foreach (var (passId, passVS) in vertexShadersByPass) + { + if (builder.Length > 0) + builder.Append("\n\n"); + + var passName = Names.KnownNames.TryResolve(passId); + var shaders = passVS.OrderBy(i => i).Select(i => $"#{i}"); + builder.Append($"Vertex Shaders ({passName}): {string.Join(", ", shaders)}"); + if (pixelShadersByPass.TryGetValue(passId, out var passPS)) + { + shaders = passPS.OrderBy(i => i).Select(i => $"#{i}"); + builder.Append($"\nPixel Shaders ({passName}): {string.Join(", ", shaders)}"); + } + } + foreach (var (passId, passPS) in pixelShadersByPass) + { + if (vertexShadersByPass.ContainsKey(passId)) + continue; + + if (builder.Length > 0) + builder.Append("\n\n"); + + var passName = Names.KnownNames.TryResolve(passId); + var shaders = passPS.OrderBy(i => i).Select(i => $"#{i}"); + builder.Append($"Pixel Shaders ({passName}): {string.Join(", ", shaders)}"); + } + + ShadersString = Encoding.UTF8.GetBytes(builder.ToString()); + } + else + ShadersString = UnknownShadersString; + + ShaderComment = TryGetShpkDevkitData("Comment", null, true) ?? string.Empty; + } + + private bool DrawShaderSection(bool disabled) + { + var ret = false; + if (ImGui.CollapsingHeader(ShaderHeader)) + { + ret |= DrawPackageNameInput(disabled); + ret |= DrawShaderFlagsInput(disabled); + DrawCustomAssociations(); + ret |= DrawMaterialShaderKeys(disabled); + DrawMaterialShaders(); + } + + if (!ShpkLoading && (AssociatedShpk == null || AssociatedShpkDevkit == null)) + { + ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); + + if (AssociatedShpk == null) + { + ImUtf8.Text("Unable to find a suitable shader (.shpk) file for cross-references. Some functionality will be missing."u8, + ImGuiUtil.HalfBlendText(0x80u)); // Half red + } + else + { + ImUtf8.Text("No dev-kit file found for this material's shaders. Please install one for optimal editing experience, such as actual constant names instead of hexadecimal identifiers."u8, + ImGuiUtil.HalfBlendText(0x8080u)); // Half yellow + } + } + + return ret; + } + + private bool DrawPackageNameInput(bool disabled) + { + if (disabled) + { + ImGui.TextUnformatted("Shader Package: " + Mtrl.ShaderPackage.Name); + return false; + } + + var ret = false; + ImGui.SetNextItemWidth(UiHelpers.Scale * 250.0f); + using var c = ImRaii.Combo("Shader Package", Mtrl.ShaderPackage.Name); + if (c) + foreach (var value in GetShpkNames()) + { + if (ImGui.Selectable(value, value == Mtrl.ShaderPackage.Name)) + { + Mtrl.ShaderPackage.Name = value; + ret = true; + AssociatedShpk = null; + LoadedShpkPath = FullPath.Empty; + LoadShpk(FindAssociatedShpk(out _, out _)); + } + } + + return ret; + } + + private bool DrawShaderFlagsInput(bool disabled) + { + var shpkFlags = (int)Mtrl.ShaderPackage.Flags; + ImGui.SetNextItemWidth(UiHelpers.Scale * 250.0f); + if (!ImGui.InputInt("Shader Flags", ref shpkFlags, 0, 0, + ImGuiInputTextFlags.CharsHexadecimal | (disabled ? ImGuiInputTextFlags.ReadOnly : ImGuiInputTextFlags.None))) + return false; + + Mtrl.ShaderPackage.Flags = (uint)shpkFlags; + SetShaderPackageFlags((uint)shpkFlags); + return true; + } + + /// + /// Show the currently associated shpk file, if any, and the buttons to associate + /// a specific shpk from your drive, the modded shpk by path or the default shpk. + /// + private void DrawCustomAssociations() + { + const string tooltip = "Click to copy file path to clipboard."; + var text = AssociatedShpk == null + ? "Associated .shpk file: None" + : $"Associated .shpk file: {LoadedShpkPathName}"; + var devkitText = AssociatedShpkDevkit == null + ? "Associated dev-kit file: None" + : $"Associated dev-kit file: {LoadedShpkDevkitPathName}"; + var baseDevkitText = AssociatedBaseDevkit == null + ? "Base dev-kit file: None" + : $"Base dev-kit file: {LoadedBaseDevkitPathName}"; + + ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); + + ImGuiUtil.CopyOnClickSelectable(text, LoadedShpkPathName, tooltip); + ImGuiUtil.CopyOnClickSelectable(devkitText, LoadedShpkDevkitPathName, tooltip); + ImGuiUtil.CopyOnClickSelectable(baseDevkitText, LoadedBaseDevkitPathName, tooltip); + + if (ImGui.Button("Associate Custom .shpk File")) + _fileDialog.OpenFilePicker("Associate Custom .shpk File...", ".shpk", (success, name) => + { + if (success) + LoadShpk(new FullPath(name[0])); + }, 1, _edit.Mod!.ModPath.FullName, false); + + var moddedPath = FindAssociatedShpk(out var defaultPath, out var gamePath); + ImGui.SameLine(); + if (ImGuiUtil.DrawDisabledButton("Associate Default .shpk File", Vector2.Zero, moddedPath.ToPath(), + moddedPath.Equals(LoadedShpkPath))) + LoadShpk(moddedPath); + + if (!gamePath.Path.Equals(moddedPath.InternalName)) + { + ImGui.SameLine(); + if (ImGuiUtil.DrawDisabledButton("Associate Unmodded .shpk File", Vector2.Zero, defaultPath, + gamePath.Path.Equals(LoadedShpkPath.InternalName))) + LoadShpk(new FullPath(gamePath)); + } + + ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); + } + + private bool DrawMaterialShaderKeys(bool disabled) + { + if (ShaderKeys.Count == 0) + return false; + + var ret = false; + foreach (var (label, index, description, monoFont, values) in ShaderKeys) + { + using var font = ImRaii.PushFont(UiBuilder.MonoFont, monoFont); + ref var key = ref Mtrl.ShaderPackage.ShaderKeys[index]; + var shpkKey = AssociatedShpk?.GetMaterialKeyById(key.Category); + var currentValue = key.Value; + var (currentLabel, _, currentDescription) = + values.FirstOrNull(v => v.Value == currentValue) ?? ($"0x{currentValue:X8}", currentValue, string.Empty); + if (!disabled && shpkKey.HasValue) + { + ImGui.SetNextItemWidth(UiHelpers.Scale * 250.0f); + using (var c = ImRaii.Combo($"##{key.Category:X8}", currentLabel)) + { + if (c) + foreach (var (valueLabel, value, valueDescription) in values) + { + if (ImGui.Selectable(valueLabel, value == currentValue)) + { + key.Value = value; + ret = true; + Update(); + } + + if (valueDescription.Length > 0) + ImGuiUtil.SelectableHelpMarker(valueDescription); + } + } + + ImGui.SameLine(); + if (description.Length > 0) + ImGuiUtil.LabeledHelpMarker(label, description); + else + ImGui.TextUnformatted(label); + } + else if (description.Length > 0 || currentDescription.Length > 0) + { + ImGuiUtil.LabeledHelpMarker($"{label}: {currentLabel}", + description + (description.Length > 0 && currentDescription.Length > 0 ? "\n\n" : string.Empty) + currentDescription); + } + else + { + ImGui.TextUnformatted($"{label}: {currentLabel}"); + } + } + + return ret; + } + + private void DrawMaterialShaders() + { + if (AssociatedShpk == null) + return; + + using (var node = ImUtf8.TreeNode("Candidate Shaders"u8)) + { + if (node) + ImUtf8.Text(ShadersString.Span); + } + + if (ShaderComment.Length > 0) + { + ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); + ImGui.TextUnformatted(ShaderComment); + } + } +} diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Textures.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Textures.cs new file mode 100644 index 00000000..3181dafe --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Textures.cs @@ -0,0 +1,276 @@ +using Dalamud.Interface; +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Text; +using Penumbra.GameData; +using Penumbra.GameData.Files.MaterialStructs; +using Penumbra.String.Classes; +using static Penumbra.GameData.Files.MaterialStructs.SamplerFlags; +using static Penumbra.GameData.Files.ShpkFile; + +namespace Penumbra.UI.AdvancedWindow.Materials; + +public partial class MtrlTab +{ + public readonly List<(string Label, int TextureIndex, int SamplerIndex, string Description, bool MonoFont)> Textures = new(4); + + public readonly HashSet UnfoldedTextures = new(4); + public readonly HashSet SamplerIds = new(16); + public float TextureLabelWidth; + + private void UpdateTextures() + { + Textures.Clear(); + SamplerIds.Clear(); + if (AssociatedShpk == null) + { + SamplerIds.UnionWith(Mtrl.ShaderPackage.Samplers.Select(sampler => sampler.SamplerId)); + if (Mtrl.Table != null) + SamplerIds.Add(TableSamplerId); + + foreach (var (sampler, index) in Mtrl.ShaderPackage.Samplers.WithIndex()) + Textures.Add(($"0x{sampler.SamplerId:X8}", sampler.TextureIndex, index, string.Empty, true)); + } + else + { + foreach (var index in VertexShaders) + SamplerIds.UnionWith(AssociatedShpk.VertexShaders[index].Samplers.Select(sampler => sampler.Id)); + foreach (var index in PixelShaders) + SamplerIds.UnionWith(AssociatedShpk.PixelShaders[index].Samplers.Select(sampler => sampler.Id)); + if (!ShadersKnown) + { + SamplerIds.UnionWith(Mtrl.ShaderPackage.Samplers.Select(sampler => sampler.SamplerId)); + if (Mtrl.Table != null) + SamplerIds.Add(TableSamplerId); + } + + foreach (var samplerId in SamplerIds) + { + var shpkSampler = AssociatedShpk.GetSamplerById(samplerId); + if (shpkSampler is not { Slot: 2 }) + continue; + + var dkData = TryGetShpkDevkitData("Samplers", samplerId, true); + var hasDkLabel = !string.IsNullOrEmpty(dkData?.Label); + + var sampler = Mtrl.GetOrAddSampler(samplerId, dkData?.DefaultTexture ?? string.Empty, out var samplerIndex); + Textures.Add((hasDkLabel ? dkData!.Label : shpkSampler.Value.Name, sampler.TextureIndex, samplerIndex, + dkData?.Description ?? string.Empty, !hasDkLabel)); + } + + if (SamplerIds.Contains(TableSamplerId)) + Mtrl.Table ??= new ColorTable(); + } + + Textures.Sort((x, y) => string.CompareOrdinal(x.Label, y.Label)); + + TextureLabelWidth = 50f * UiHelpers.Scale; + + float helpWidth; + using (var _ = ImRaii.PushFont(UiBuilder.IconFont)) + { + helpWidth = ImGui.GetStyle().ItemSpacing.X + ImGui.CalcTextSize(FontAwesomeIcon.InfoCircle.ToIconString()).X; + } + + foreach (var (label, _, _, description, monoFont) in Textures) + { + if (!monoFont) + TextureLabelWidth = Math.Max(TextureLabelWidth, ImGui.CalcTextSize(label).X + (description.Length > 0 ? helpWidth : 0.0f)); + } + + using (var _ = ImRaii.PushFont(UiBuilder.MonoFont)) + { + foreach (var (label, _, _, description, monoFont) in Textures) + { + if (monoFont) + TextureLabelWidth = Math.Max(TextureLabelWidth, + ImGui.CalcTextSize(label).X + (description.Length > 0 ? helpWidth : 0.0f)); + } + } + + TextureLabelWidth = TextureLabelWidth / UiHelpers.Scale + 4; + } + + private static ReadOnlySpan TextureAddressModeTooltip(TextureAddressMode addressMode) + => addressMode switch + { + TextureAddressMode.Wrap => "Tile the texture at every UV integer junction.\n\nFor example, for U values between 0 and 3, the texture is repeated three times."u8, + TextureAddressMode.Mirror => "Flip the texture at every UV integer junction.\n\nFor U values between 0 and 1, for example, the texture is addressed normally; between 1 and 2, the texture is mirrored; between 2 and 3, the texture is normal again; and so on."u8, + TextureAddressMode.Clamp => "Texture coordinates outside the range [0.0, 1.0] are set to the texture color at 0.0 or 1.0, respectively."u8, + TextureAddressMode.Border => "Texture coordinates outside the range [0.0, 1.0] are set to the border color (generally black)."u8, + _ => ""u8, + }; + + private bool DrawTextureSection(bool disabled) + { + if (Textures.Count == 0) + return false; + + ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); + if (!ImGui.CollapsingHeader("Textures and Samplers", ImGuiTreeNodeFlags.DefaultOpen)) + return false; + + var frameHeight = ImGui.GetFrameHeight(); + var ret = false; + using var table = ImRaii.Table("##Textures", 3); + + ImGui.TableSetupColumn(string.Empty, ImGuiTableColumnFlags.WidthFixed, frameHeight); + ImGui.TableSetupColumn("Path", ImGuiTableColumnFlags.WidthStretch); + ImGui.TableSetupColumn("Name", ImGuiTableColumnFlags.WidthFixed, TextureLabelWidth * UiHelpers.Scale); + foreach (var (label, textureI, samplerI, description, monoFont) in Textures) + { + using var _ = ImRaii.PushId(samplerI); + var tmp = Mtrl.Textures[textureI].Path; + var unfolded = UnfoldedTextures.Contains(samplerI); + ImGui.TableNextColumn(); + if (ImGuiUtil.DrawDisabledButton((unfolded ? FontAwesomeIcon.CaretDown : FontAwesomeIcon.CaretRight).ToIconString(), + new Vector2(frameHeight), + "Settings for this texture and the associated sampler", false, true)) + { + unfolded = !unfolded; + if (unfolded) + UnfoldedTextures.Add(samplerI); + else + UnfoldedTextures.Remove(samplerI); + } + + ImGui.TableNextColumn(); + ImGui.SetNextItemWidth(ImGui.GetContentRegionAvail().X); + if (ImGui.InputText(string.Empty, ref tmp, Utf8GamePath.MaxGamePathLength, + disabled ? ImGuiInputTextFlags.ReadOnly : ImGuiInputTextFlags.None) + && tmp.Length > 0 + && tmp != Mtrl.Textures[textureI].Path) + { + ret = true; + Mtrl.Textures[textureI].Path = tmp; + } + + ImGui.TableNextColumn(); + using (var font = ImRaii.PushFont(UiBuilder.MonoFont, monoFont)) + { + ImGui.AlignTextToFramePadding(); + if (description.Length > 0) + ImGuiUtil.LabeledHelpMarker(label, description); + else + ImGui.TextUnformatted(label); + } + + if (unfolded) + { + ImGui.TableNextColumn(); + ImGui.TableNextColumn(); + ret |= DrawMaterialSampler(disabled, textureI, samplerI); + ImGui.TableNextColumn(); + } + } + + return ret; + } + + private static bool ComboTextureAddressMode(ReadOnlySpan label, ref TextureAddressMode value) + { + using var c = ImUtf8.Combo(label, value.ToString()); + if (!c) + return false; + + var ret = false; + foreach (var mode in Enum.GetValues()) + { + if (ImGui.Selectable(mode.ToString(), mode == value)) + { + value = mode; + ret = true; + } + + ImUtf8.SelectableHelpMarker(TextureAddressModeTooltip(mode)); + } + + return ret; + } + + private bool DrawMaterialSampler(bool disabled, int textureIdx, int samplerIdx) + { + var ret = false; + ref var texture = ref Mtrl.Textures[textureIdx]; + ref var sampler = ref Mtrl.ShaderPackage.Samplers[samplerIdx]; + + var dx11 = texture.DX11; + if (ImUtf8.Checkbox("Prepend -- to the file name on DirectX 11"u8, ref dx11)) + { + texture.DX11 = dx11; + ret = true; + } + + ref var samplerFlags = ref SamplerFlags.Wrap(ref sampler.Flags); + + ImGui.SetNextItemWidth(UiHelpers.Scale * 100.0f); + var addressMode = samplerFlags.UAddressMode; + if (ComboTextureAddressMode("##UAddressMode"u8, ref addressMode)) + { + samplerFlags.UAddressMode = addressMode; + ret = true; + SetSamplerFlags(sampler.SamplerId, sampler.Flags); + } + + ImGui.SameLine(); + ImUtf8.LabeledHelpMarker("U Address Mode"u8, "Method to use for resolving a U texture coordinate that is outside the 0 to 1 range."); + + ImGui.SetNextItemWidth(UiHelpers.Scale * 100.0f); + addressMode = samplerFlags.VAddressMode; + if (ComboTextureAddressMode("##VAddressMode"u8, ref addressMode)) + { + samplerFlags.VAddressMode = addressMode; + ret = true; + SetSamplerFlags(sampler.SamplerId, sampler.Flags); + } + + ImGui.SameLine(); + ImUtf8.LabeledHelpMarker("V Address Mode"u8, "Method to use for resolving a V texture coordinate that is outside the 0 to 1 range."); + + var lodBias = samplerFlags.LodBias; + ImGui.SetNextItemWidth(UiHelpers.Scale * 100.0f); + if (ImUtf8.DragScalar("##LoDBias"u8, ref lodBias, -8.0f, 7.984375f, 0.1f)) + { + samplerFlags.LodBias = lodBias; + ret = true; + SetSamplerFlags(sampler.SamplerId, sampler.Flags); + } + + ImGui.SameLine(); + ImUtf8.LabeledHelpMarker("Level of Detail Bias"u8, + "Offset from the calculated mipmap level.\n\nHigher means that the texture will start to lose detail nearer.\nLower means that the texture will keep its detail until farther."); + + var minLod = samplerFlags.MinLod; + ImGui.SetNextItemWidth(UiHelpers.Scale * 100.0f); + if (ImUtf8.DragScalar("##MinLoD"u8, ref minLod, 0, 15, 0.1f)) + { + samplerFlags.MinLod = minLod; + ret = true; + SetSamplerFlags(sampler.SamplerId, sampler.Flags); + } + + ImGui.SameLine(); + ImUtf8.LabeledHelpMarker("Minimum Level of Detail"u8, + "Most detailed mipmap level to use.\n\n0 is the full-sized texture, 1 is the half-sized texture, 2 is the quarter-sized texture, and so on.\n15 will forcibly reduce the texture to its smallest mipmap."); + + using var t = ImUtf8.TreeNode("Advanced Settings"u8); + if (!t) + return ret; + + ImGui.SetNextItemWidth(UiHelpers.Scale * 100.0f); + if (ImUtf8.InputScalar("Texture Flags"u8, ref texture.Flags, "%04X"u8, + flags: disabled ? ImGuiInputTextFlags.ReadOnly : ImGuiInputTextFlags.None)) + ret = true; + + ImGui.SetNextItemWidth(UiHelpers.Scale * 100.0f); + if (ImUtf8.InputScalar("Sampler Flags"u8, ref sampler.Flags, "%08X"u8, + flags: ImGuiInputTextFlags.CharsHexadecimal | (disabled ? ImGuiInputTextFlags.ReadOnly : ImGuiInputTextFlags.None))) + { + ret = true; + SetSamplerFlags(sampler.SamplerId, sampler.Flags); + } + + return ret; + } +} diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.cs new file mode 100644 index 00000000..2d4e93f1 --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.cs @@ -0,0 +1,199 @@ +using Dalamud.Plugin.Services; +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Text; +using OtterGui.Widgets; +using Penumbra.GameData; +using Penumbra.GameData.Files; +using Penumbra.GameData.Files.MaterialStructs; +using Penumbra.GameData.Interop; +using Penumbra.Interop.Hooks.Objects; +using Penumbra.Interop.ResourceTree; +using Penumbra.Services; +using Penumbra.String; +using Penumbra.UI.Classes; + +namespace Penumbra.UI.AdvancedWindow.Materials; + +public sealed partial class MtrlTab : IWritable, IDisposable +{ + private const int ShpkPrefixLength = 16; + + private static readonly CiByteString ShpkPrefix = CiByteString.FromSpanUnsafe("shader/sm5/shpk/"u8, true, true, true); + + private readonly IDataManager _gameData; + private readonly IFramework _framework; + private readonly ObjectManager _objects; + private readonly CharacterBaseDestructor _characterBaseDestructor; + private readonly StainService _stainService; + private readonly ResourceTreeFactory _resourceTreeFactory; + private readonly FileDialogService _fileDialog; + private readonly MaterialTemplatePickers _materialTemplatePickers; + private readonly Configuration _config; + + private readonly ModEditWindow _edit; + public readonly MtrlFile Mtrl; + public readonly string FilePath; + public readonly bool Writable; + + private bool _updateOnNextFrame; + + public unsafe MtrlTab(IDataManager gameData, IFramework framework, ObjectManager objects, CharacterBaseDestructor characterBaseDestructor, + StainService stainService, ResourceTreeFactory resourceTreeFactory, FileDialogService fileDialog, MaterialTemplatePickers materialTemplatePickers, + Configuration config, ModEditWindow edit, MtrlFile file, string filePath, bool writable) + { + _gameData = gameData; + _framework = framework; + _objects = objects; + _characterBaseDestructor = characterBaseDestructor; + _stainService = stainService; + _resourceTreeFactory = resourceTreeFactory; + _fileDialog = fileDialog; + _materialTemplatePickers = materialTemplatePickers; + _config = config; + + _edit = edit; + Mtrl = file; + FilePath = filePath; + Writable = writable; + AssociatedBaseDevkit = TryLoadShpkDevkit("_base", out LoadedBaseDevkitPathName); + Update(); + LoadShpk(FindAssociatedShpk(out _, out _)); + if (writable) + { + _characterBaseDestructor.Subscribe(UnbindFromDrawObjectMaterialInstances, CharacterBaseDestructor.Priority.MtrlTab); + BindToMaterialInstances(); + } + } + + public bool DrawVersionUpdate(bool disabled) + { + if (disabled || Mtrl.IsDawntrail) + return false; + + if (!ImUtf8.ButtonEx("Update MTRL Version to Dawntrail"u8, + "Try using this if the material can not be loaded or should use legacy shaders.\n\nThis is not revertible."u8, + new Vector2(-0.1f, 0), false, 0, Colors.PressEnterWarningBg)) + return false; + + Mtrl.MigrateToDawntrail(); + Update(); + LoadShpk(FindAssociatedShpk(out _, out _)); + return true; + } + + public bool DrawPanel(bool disabled) + { + if (_updateOnNextFrame) + { + _updateOnNextFrame = false; + Update(); + } + + DrawMaterialLivePreviewRebind(disabled); + + ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); + var ret = DrawBackFaceAndTransparency(disabled); + + ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); + ret |= DrawShaderSection(disabled); + + ret |= DrawTextureSection(disabled); + ret |= DrawColorTableSection(disabled); + ret |= DrawConstantsSection(disabled); + + ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); + DrawOtherMaterialDetails(disabled); + + return !disabled && ret; + } + + private bool DrawBackFaceAndTransparency(bool disabled) + { + ref var shaderFlags = ref ShaderFlags.Wrap(ref Mtrl.ShaderPackage.Flags); + + var ret = false; + + using var dis = ImRaii.Disabled(disabled); + + var tmp = shaderFlags.EnableTransparency; + if (ImGui.Checkbox("Enable Transparency", ref tmp)) + { + shaderFlags.EnableTransparency = tmp; + ret = true; + SetShaderPackageFlags(Mtrl.ShaderPackage.Flags); + } + + ImGui.SameLine(200 * UiHelpers.Scale + ImGui.GetStyle().ItemSpacing.X + ImGui.GetStyle().WindowPadding.X); + tmp = shaderFlags.HideBackfaces; + if (ImGui.Checkbox("Hide Backfaces", ref tmp)) + { + shaderFlags.HideBackfaces = tmp; + ret = true; + SetShaderPackageFlags(Mtrl.ShaderPackage.Flags); + } + + if (ShpkLoading) + { + ImGui.SameLine(400 * UiHelpers.Scale + 2 * ImGui.GetStyle().ItemSpacing.X + ImGui.GetStyle().WindowPadding.X); + + ImUtf8.Text("Loading shader (.shpk) file. Some functionality will only be available after this is done."u8, + ImGuiUtil.HalfBlendText(0x808000u)); // Half cyan + } + + return ret; + } + + private void DrawOtherMaterialDetails(bool _) + { + if (!ImGui.CollapsingHeader("Further Content")) + return; + + using (var sets = ImRaii.TreeNode("UV Sets", ImGuiTreeNodeFlags.DefaultOpen)) + { + if (sets) + foreach (var set in Mtrl.UvSets) + ImRaii.TreeNode($"#{set.Index:D2} - {set.Name}", ImGuiTreeNodeFlags.Leaf).Dispose(); + } + + using (var sets = ImRaii.TreeNode("Color Sets", ImGuiTreeNodeFlags.DefaultOpen)) + { + if (sets) + foreach (var set in Mtrl.ColorSets) + ImRaii.TreeNode($"#{set.Index:D2} - {set.Name}", ImGuiTreeNodeFlags.Leaf).Dispose(); + } + + if (Mtrl.AdditionalData.Length <= 0) + return; + + using var t = ImRaii.TreeNode($"Additional Data (Size: {Mtrl.AdditionalData.Length})###AdditionalData"); + if (t) + Widget.DrawHexViewer(Mtrl.AdditionalData); + } + + public void Update() + { + UpdateShaders(); + UpdateTextures(); + UpdateConstants(); + } + + public unsafe void Dispose() + { + UnbindFromMaterialInstances(); + if (Writable) + _characterBaseDestructor.Unsubscribe(UnbindFromDrawObjectMaterialInstances); + } + + public bool Valid + => ShadersKnown && Mtrl.Valid; + + public byte[] Write() + { + var output = Mtrl.Clone(); + output.GarbageCollect(AssociatedShpk, SamplerIds); + + return output.Write(); + } +} diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTabFactory.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTabFactory.cs new file mode 100644 index 00000000..af8b7db2 --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTabFactory.cs @@ -0,0 +1,18 @@ +using Dalamud.Plugin.Services; +using OtterGui.Services; +using Penumbra.GameData.Files; +using Penumbra.GameData.Interop; +using Penumbra.Interop.Hooks.Objects; +using Penumbra.Interop.ResourceTree; +using Penumbra.Services; + +namespace Penumbra.UI.AdvancedWindow.Materials; + +public sealed class MtrlTabFactory(IDataManager gameData, IFramework framework, ObjectManager objects, + CharacterBaseDestructor characterBaseDestructor, StainService stainService, ResourceTreeFactory resourceTreeFactory, + FileDialogService fileDialog, MaterialTemplatePickers materialTemplatePickers, Configuration config) : IUiService +{ + public MtrlTab Create(ModEditWindow edit, MtrlFile file, string filePath, bool writable) + => new(gameData, framework, objects, characterBaseDestructor, stainService, resourceTreeFactory, fileDialog, + materialTemplatePickers, config, edit, file, filePath, writable); +} diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs deleted file mode 100644 index 25c0e448..00000000 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs +++ /dev/null @@ -1,538 +0,0 @@ -using Dalamud.Interface; -using Dalamud.Interface.Utility; -using ImGuiNET; -using OtterGui; -using OtterGui.Raii; -using Penumbra.GameData.Files; -using Penumbra.GameData.Files.MaterialStructs; -using Penumbra.String.Functions; - -namespace Penumbra.UI.AdvancedWindow; - -public partial class ModEditWindow -{ - private static readonly float HalfMinValue = (float)Half.MinValue; - private static readonly float HalfMaxValue = (float)Half.MaxValue; - private static readonly float HalfEpsilon = (float)Half.Epsilon; - - private bool DrawMaterialColorTableChange(MtrlTab tab, bool disabled) - { - if (!tab.SamplerIds.Contains(ShpkFile.TableSamplerId) || !tab.Mtrl.HasTable) - return false; - - ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - if (!ImGui.CollapsingHeader("Color Table", ImGuiTreeNodeFlags.DefaultOpen)) - return false; - - ColorTableCopyAllClipboardButton(tab.Mtrl); - ImGui.SameLine(); - var ret = ColorTablePasteAllClipboardButton(tab, disabled); - if (!disabled) - { - ImGui.SameLine(); - ImGui.Dummy(ImGuiHelpers.ScaledVector2(20, 0)); - ImGui.SameLine(); - ret |= ColorTableDyeableCheckbox(tab); - } - - var hasDyeTable = tab.Mtrl.HasDyeTable; - if (hasDyeTable) - { - ImGui.SameLine(); - ImGui.Dummy(ImGuiHelpers.ScaledVector2(20, 0)); - ImGui.SameLine(); - ret |= DrawPreviewDye(tab, disabled); - } - - using var table = ImRaii.Table("##ColorTable", hasDyeTable ? 11 : 9, - ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg | ImGuiTableFlags.BordersInnerV); - if (!table) - return false; - - ImGui.TableNextColumn(); - ImGui.TableHeader(string.Empty); - ImGui.TableNextColumn(); - ImGui.TableHeader("Row"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Diffuse"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Specular"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Emissive"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Gloss"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Tile"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Repeat"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Skew"); - if (hasDyeTable) - { - ImGui.TableNextColumn(); - ImGui.TableHeader("Dye"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Dye Preview"); - } - - for (var i = 0; i < ColorTable.NumRows; ++i) - { - ret |= DrawColorTableRow(tab, i, disabled); - ImGui.TableNextRow(); - } - - return ret; - } - - - private static void ColorTableCopyAllClipboardButton(MtrlFile file) - { - if (!ImGui.Button("Export All Rows to Clipboard", ImGuiHelpers.ScaledVector2(200, 0))) - return; - - try - { - var data1 = file.Table.AsBytes(); - var data2 = file.HasDyeTable ? file.DyeTable.AsBytes() : ReadOnlySpan.Empty; - var array = new byte[data1.Length + data2.Length]; - data1.TryCopyTo(array); - data2.TryCopyTo(array.AsSpan(data1.Length)); - var text = Convert.ToBase64String(array); - ImGui.SetClipboardText(text); - } - catch - { - // ignored - } - } - - private bool DrawPreviewDye(MtrlTab tab, bool disabled) - { - var (dyeId, (name, dyeColor, gloss)) = _stainService.StainCombo.CurrentSelection; - var tt = dyeId == 0 - ? "Select a preview dye first." - : "Apply all preview values corresponding to the dye template and chosen dye where dyeing is enabled."; - if (ImGuiUtil.DrawDisabledButton("Apply Preview Dye", Vector2.Zero, tt, disabled || dyeId == 0)) - { - var ret = false; - if (tab.Mtrl.HasDyeTable) - for (var i = 0; i < LegacyColorTable.NumUsedRows; ++i) - ret |= tab.Mtrl.ApplyDyeTemplate(_stainService.StmFile, i, dyeId, 0); - - tab.UpdateColorTablePreview(); - - return ret; - } - - ImGui.SameLine(); - var label = dyeId == 0 ? "Preview Dye###previewDye" : $"{name} (Preview)###previewDye"; - if (_stainService.StainCombo.Draw(label, dyeColor, string.Empty, true, gloss)) - tab.UpdateColorTablePreview(); - return false; - } - - private static unsafe bool ColorTablePasteAllClipboardButton(MtrlTab tab, bool disabled) - { - if (!ImGuiUtil.DrawDisabledButton("Import All Rows from Clipboard", ImGuiHelpers.ScaledVector2(200, 0), string.Empty, disabled) - || !tab.Mtrl.HasTable) - return false; - - try - { - var text = ImGui.GetClipboardText(); - var data = Convert.FromBase64String(text); - if (data.Length < Marshal.SizeOf()) - return false; - - ref var rows = ref tab.Mtrl.Table; - fixed (void* ptr = data, output = &rows) - { - MemoryUtility.MemCpyUnchecked(output, ptr, Marshal.SizeOf()); - if (data.Length >= Marshal.SizeOf() + Marshal.SizeOf() - && tab.Mtrl.HasDyeTable) - { - ref var dyeRows = ref tab.Mtrl.DyeTable; - fixed (void* output2 = &dyeRows) - { - MemoryUtility.MemCpyUnchecked(output2, (byte*)ptr + Marshal.SizeOf(), - Marshal.SizeOf()); - } - } - } - - tab.UpdateColorTablePreview(); - - return true; - } - catch - { - return false; - } - } - - [SkipLocalsInit] - private static unsafe void ColorTableCopyClipboardButton(ColorTable.Row row, ColorDyeTable.Row dye) - { - if (!ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Clipboard.ToIconString(), ImGui.GetFrameHeight() * Vector2.One, - "Export this row to your clipboard.", false, true)) - return; - - try - { - Span data = stackalloc byte[ColorTable.Row.Size + ColorDyeTable.Row.Size]; - fixed (byte* ptr = data) - { - MemoryUtility.MemCpyUnchecked(ptr, &row, ColorTable.Row.Size); - MemoryUtility.MemCpyUnchecked(ptr + ColorTable.Row.Size, &dye, ColorDyeTable.Row.Size); - } - - var text = Convert.ToBase64String(data); - ImGui.SetClipboardText(text); - } - catch - { - // ignored - } - } - - private static bool ColorTableDyeableCheckbox(MtrlTab tab) - { - var dyeable = tab.Mtrl.HasDyeTable; - var ret = ImGui.Checkbox("Dyeable", ref dyeable); - - if (ret) - { - tab.Mtrl.HasDyeTable = dyeable; - tab.UpdateColorTablePreview(); - } - - return ret; - } - - private static unsafe bool ColorTablePasteFromClipboardButton(MtrlTab tab, int rowIdx, bool disabled) - { - if (!ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Paste.ToIconString(), ImGui.GetFrameHeight() * Vector2.One, - "Import an exported row from your clipboard onto this row.", disabled, true)) - return false; - - try - { - var text = ImGui.GetClipboardText(); - var data = Convert.FromBase64String(text); - if (data.Length != ColorTable.Row.Size + ColorDyeTable.Row.Size - || !tab.Mtrl.HasTable) - return false; - - fixed (byte* ptr = data) - { - tab.Mtrl.Table[rowIdx] = *(ColorTable.Row*)ptr; - if (tab.Mtrl.HasDyeTable) - tab.Mtrl.DyeTable[rowIdx] = *(ColorDyeTable.Row*)(ptr + ColorTable.Row.Size); - } - - tab.UpdateColorTableRowPreview(rowIdx); - - return true; - } - catch - { - return false; - } - } - - private static void ColorTableHighlightButton(MtrlTab tab, int rowIdx, bool disabled) - { - ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Crosshairs.ToIconString(), ImGui.GetFrameHeight() * Vector2.One, - "Highlight this row on your character, if possible.", disabled || tab.ColorTablePreviewers.Count == 0, true); - - if (ImGui.IsItemHovered()) - tab.HighlightColorTableRow(rowIdx); - else if (tab.HighlightedColorTableRow == rowIdx) - tab.CancelColorTableHighlight(); - } - - private bool DrawColorTableRow(MtrlTab tab, int rowIdx, bool disabled) - { - static bool FixFloat(ref float val, float current) - { - val = (float)(Half)val; - return val != current; - } - - using var id = ImRaii.PushId(rowIdx); - ref var row = ref tab.Mtrl.Table[rowIdx]; - var hasDye = tab.Mtrl.HasDyeTable; - ref var dye = ref tab.Mtrl.DyeTable[rowIdx]; - var floatSize = 70 * UiHelpers.Scale; - var intSize = 45 * UiHelpers.Scale; - ImGui.TableNextColumn(); - ColorTableCopyClipboardButton(row, dye); - ImGui.SameLine(); - var ret = ColorTablePasteFromClipboardButton(tab, rowIdx, disabled); - ImGui.SameLine(); - ColorTableHighlightButton(tab, rowIdx, disabled); - - ImGui.TableNextColumn(); - ImGui.TextUnformatted($"#{rowIdx + 1:D2}"); - - ImGui.TableNextColumn(); - using var dis = ImRaii.Disabled(disabled); - ret |= ColorPicker("##Diffuse", "Diffuse Color", row.Diffuse, c => - { - tab.Mtrl.Table[rowIdx].Diffuse = c; - tab.UpdateColorTableRowPreview(rowIdx); - }); - if (hasDye) - { - ImGui.SameLine(); - ret |= ImGuiUtil.Checkbox("##dyeDiffuse", "Apply Diffuse Color on Dye", dye.Diffuse, - b => - { - tab.Mtrl.DyeTable[rowIdx].Diffuse = b; - tab.UpdateColorTableRowPreview(rowIdx); - }, ImGuiHoveredFlags.AllowWhenDisabled); - } - - ImGui.TableNextColumn(); - ret |= ColorPicker("##Specular", "Specular Color", row.Specular, c => - { - tab.Mtrl.Table[rowIdx].Specular = c; - tab.UpdateColorTableRowPreview(rowIdx); - }); - ImGui.SameLine(); - var tmpFloat = row.SpecularStrength; - ImGui.SetNextItemWidth(floatSize); - if (ImGui.DragFloat("##SpecularStrength", ref tmpFloat, 0.01f, 0f, HalfMaxValue, "%.2f") - && FixFloat(ref tmpFloat, row.SpecularStrength)) - { - row.SpecularStrength = tmpFloat; - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Specular Strength", ImGuiHoveredFlags.AllowWhenDisabled); - - if (hasDye) - { - ImGui.SameLine(); - ret |= ImGuiUtil.Checkbox("##dyeSpecular", "Apply Specular Color on Dye", dye.Specular, - b => - { - tab.Mtrl.DyeTable[rowIdx].Specular = b; - tab.UpdateColorTableRowPreview(rowIdx); - }, ImGuiHoveredFlags.AllowWhenDisabled); - ImGui.SameLine(); - ret |= ImGuiUtil.Checkbox("##dyeSpecularStrength", "Apply Specular Strength on Dye", dye.SpecularStrength, - b => - { - tab.Mtrl.DyeTable[rowIdx].SpecularStrength = b; - tab.UpdateColorTableRowPreview(rowIdx); - }, ImGuiHoveredFlags.AllowWhenDisabled); - } - - ImGui.TableNextColumn(); - ret |= ColorPicker("##Emissive", "Emissive Color", row.Emissive, c => - { - tab.Mtrl.Table[rowIdx].Emissive = c; - tab.UpdateColorTableRowPreview(rowIdx); - }); - if (hasDye) - { - ImGui.SameLine(); - ret |= ImGuiUtil.Checkbox("##dyeEmissive", "Apply Emissive Color on Dye", dye.Emissive, - b => - { - tab.Mtrl.DyeTable[rowIdx].Emissive = b; - tab.UpdateColorTableRowPreview(rowIdx); - }, ImGuiHoveredFlags.AllowWhenDisabled); - } - - ImGui.TableNextColumn(); - tmpFloat = row.GlossStrength; - ImGui.SetNextItemWidth(floatSize); - var glossStrengthMin = ImGui.GetIO().KeyCtrl ? 0.0f : HalfEpsilon; - if (ImGui.DragFloat("##GlossStrength", ref tmpFloat, Math.Max(0.1f, tmpFloat * 0.025f), glossStrengthMin, HalfMaxValue, "%.1f") - && FixFloat(ref tmpFloat, row.GlossStrength)) - { - row.GlossStrength = Math.Max(tmpFloat, glossStrengthMin); - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Gloss Strength", ImGuiHoveredFlags.AllowWhenDisabled); - if (hasDye) - { - ImGui.SameLine(); - ret |= ImGuiUtil.Checkbox("##dyeGloss", "Apply Gloss Strength on Dye", dye.Gloss, - b => - { - tab.Mtrl.DyeTable[rowIdx].Gloss = b; - tab.UpdateColorTableRowPreview(rowIdx); - }, ImGuiHoveredFlags.AllowWhenDisabled); - } - - ImGui.TableNextColumn(); - int tmpInt = row.TileSet; - ImGui.SetNextItemWidth(intSize); - if (ImGui.DragInt("##TileSet", ref tmpInt, 0.25f, 0, 63) && tmpInt != row.TileSet && tmpInt is >= 0 and <= ushort.MaxValue) - { - row.TileSet = (ushort)Math.Clamp(tmpInt, 0, 63); - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Tile Set", ImGuiHoveredFlags.AllowWhenDisabled); - - ImGui.TableNextColumn(); - tmpFloat = row.MaterialRepeat.X; - ImGui.SetNextItemWidth(floatSize); - if (ImGui.DragFloat("##RepeatX", ref tmpFloat, 0.1f, HalfMinValue, HalfMaxValue, "%.2f") - && FixFloat(ref tmpFloat, row.MaterialRepeat.X)) - { - row.MaterialRepeat = row.MaterialRepeat with { X = tmpFloat }; - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Repeat X", ImGuiHoveredFlags.AllowWhenDisabled); - ImGui.SameLine(); - tmpFloat = row.MaterialRepeat.Y; - ImGui.SetNextItemWidth(floatSize); - if (ImGui.DragFloat("##RepeatY", ref tmpFloat, 0.1f, HalfMinValue, HalfMaxValue, "%.2f") - && FixFloat(ref tmpFloat, row.MaterialRepeat.Y)) - { - row.MaterialRepeat = row.MaterialRepeat with { Y = tmpFloat }; - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Repeat Y", ImGuiHoveredFlags.AllowWhenDisabled); - - ImGui.TableNextColumn(); - tmpFloat = row.MaterialSkew.X; - ImGui.SetNextItemWidth(floatSize); - if (ImGui.DragFloat("##SkewX", ref tmpFloat, 0.1f, HalfMinValue, HalfMaxValue, "%.2f") && FixFloat(ref tmpFloat, row.MaterialSkew.X)) - { - row.MaterialSkew = row.MaterialSkew with { X = tmpFloat }; - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Skew X", ImGuiHoveredFlags.AllowWhenDisabled); - - ImGui.SameLine(); - tmpFloat = row.MaterialSkew.Y; - ImGui.SetNextItemWidth(floatSize); - if (ImGui.DragFloat("##SkewY", ref tmpFloat, 0.1f, HalfMinValue, HalfMaxValue, "%.2f") && FixFloat(ref tmpFloat, row.MaterialSkew.Y)) - { - row.MaterialSkew = row.MaterialSkew with { Y = tmpFloat }; - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Skew Y", ImGuiHoveredFlags.AllowWhenDisabled); - - if (hasDye) - { - ImGui.TableNextColumn(); - if (_stainService.TemplateCombo.Draw("##dyeTemplate", dye.Template.ToString(), string.Empty, intSize - + ImGui.GetStyle().ScrollbarSize / 2, ImGui.GetTextLineHeightWithSpacing(), ImGuiComboFlags.NoArrowButton)) - { - dye.Template = _stainService.TemplateCombo.CurrentSelection; - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Dye Template", ImGuiHoveredFlags.AllowWhenDisabled); - - ImGui.TableNextColumn(); - ret |= DrawDyePreview(tab, rowIdx, disabled, dye, floatSize); - } - - - return ret; - } - - private bool DrawDyePreview(MtrlTab tab, int rowIdx, bool disabled, ColorDyeTable.Row dye, float floatSize) - { - var stain = _stainService.StainCombo.CurrentSelection.Key; - if (stain == 0 || !_stainService.StmFile.Entries.TryGetValue(dye.Template, out var entry)) - return false; - - var values = entry[(int)stain]; - using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, ImGui.GetStyle().ItemSpacing / 2); - - var ret = ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.PaintBrush.ToIconString(), new Vector2(ImGui.GetFrameHeight()), - "Apply the selected dye to this row.", disabled, true); - - ret = ret && tab.Mtrl.ApplyDyeTemplate(_stainService.StmFile, rowIdx, stain, 0); - if (ret) - tab.UpdateColorTableRowPreview(rowIdx); - - ImGui.SameLine(); - ColorPicker("##diffusePreview", string.Empty, values.Diffuse, _ => { }, "D"); - ImGui.SameLine(); - ColorPicker("##specularPreview", string.Empty, values.Specular, _ => { }, "S"); - ImGui.SameLine(); - ColorPicker("##emissivePreview", string.Empty, values.Emissive, _ => { }, "E"); - ImGui.SameLine(); - using var dis = ImRaii.Disabled(); - ImGui.SetNextItemWidth(floatSize); - ImGui.DragFloat("##gloss", ref values.Gloss, 0, values.Gloss, values.Gloss, "%.1f G"); - ImGui.SameLine(); - ImGui.SetNextItemWidth(floatSize); - ImGui.DragFloat("##specularStrength", ref values.SpecularPower, 0, values.SpecularPower, values.SpecularPower, "%.2f S"); - - return ret; - } - - private static bool ColorPicker(string label, string tooltip, Vector3 input, Action setter, string letter = "") - { - var ret = false; - var inputSqrt = PseudoSqrtRgb(input); - var tmp = inputSqrt; - if (ImGui.ColorEdit3(label, ref tmp, - ImGuiColorEditFlags.NoInputs - | ImGuiColorEditFlags.DisplayRGB - | ImGuiColorEditFlags.InputRGB - | ImGuiColorEditFlags.NoTooltip - | ImGuiColorEditFlags.HDR) - && tmp != inputSqrt) - { - setter(PseudoSquareRgb(tmp)); - ret = true; - } - - if (letter.Length > 0 && ImGui.IsItemVisible()) - { - var textSize = ImGui.CalcTextSize(letter); - var center = ImGui.GetItemRectMin() + (ImGui.GetItemRectSize() - textSize) / 2; - var textColor = input.LengthSquared() < 0.25f ? 0x80FFFFFFu : 0x80000000u; - ImGui.GetWindowDrawList().AddText(center, textColor, letter); - } - - ImGuiUtil.HoverTooltip(tooltip, ImGuiHoveredFlags.AllowWhenDisabled); - - return ret; - } - - // Functions to deal with squared RGB values without making negatives useless. - - private static float PseudoSquareRgb(float x) - => x < 0.0f ? -(x * x) : x * x; - - private static Vector3 PseudoSquareRgb(Vector3 vec) - => new(PseudoSquareRgb(vec.X), PseudoSquareRgb(vec.Y), PseudoSquareRgb(vec.Z)); - - private static Vector4 PseudoSquareRgb(Vector4 vec) - => new(PseudoSquareRgb(vec.X), PseudoSquareRgb(vec.Y), PseudoSquareRgb(vec.Z), vec.W); - - private static float PseudoSqrtRgb(float x) - => x < 0.0f ? -MathF.Sqrt(-x) : MathF.Sqrt(x); - - internal static Vector3 PseudoSqrtRgb(Vector3 vec) - => new(PseudoSqrtRgb(vec.X), PseudoSqrtRgb(vec.Y), PseudoSqrtRgb(vec.Z)); - - private static Vector4 PseudoSqrtRgb(Vector4 vec) - => new(PseudoSqrtRgb(vec.X), PseudoSqrtRgb(vec.Y), PseudoSqrtRgb(vec.Z), vec.W); -} diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ConstantEditor.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ConstantEditor.cs deleted file mode 100644 index 1f5db38e..00000000 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ConstantEditor.cs +++ /dev/null @@ -1,247 +0,0 @@ -using ImGuiNET; -using OtterGui.Raii; -using OtterGui; -using Penumbra.GameData; - -namespace Penumbra.UI.AdvancedWindow; - -public partial class ModEditWindow -{ - private interface IConstantEditor - { - bool Draw(Span values, bool disabled); - } - - private sealed class FloatConstantEditor : IConstantEditor - { - public static readonly FloatConstantEditor Default = new(null, null, 0.1f, 0.0f, 1.0f, 0.0f, 3, string.Empty); - - private readonly float? _minimum; - private readonly float? _maximum; - private readonly float _speed; - private readonly float _relativeSpeed; - private readonly float _factor; - private readonly float _bias; - private readonly string _format; - - public FloatConstantEditor(float? minimum, float? maximum, float speed, float relativeSpeed, float factor, float bias, byte precision, - string unit) - { - _minimum = minimum; - _maximum = maximum; - _speed = speed; - _relativeSpeed = relativeSpeed; - _factor = factor; - _bias = bias; - _format = $"%.{Math.Min(precision, (byte)9)}f"; - if (unit.Length > 0) - _format = $"{_format} {unit.Replace("%", "%%")}"; - } - - public bool Draw(Span values, bool disabled) - { - var spacing = ImGui.GetStyle().ItemInnerSpacing.X; - var fieldWidth = (ImGui.CalcItemWidth() - (values.Length - 1) * spacing) / values.Length; - - var ret = false; - - // Not using DragScalarN because of _relativeSpeed and other points of lost flexibility. - for (var valueIdx = 0; valueIdx < values.Length; ++valueIdx) - { - if (valueIdx > 0) - ImGui.SameLine(0.0f, spacing); - - ImGui.SetNextItemWidth(MathF.Round(fieldWidth * (valueIdx + 1)) - MathF.Round(fieldWidth * valueIdx)); - - var value = (values[valueIdx] - _bias) / _factor; - if (disabled) - { - ImGui.DragFloat($"##{valueIdx}", ref value, Math.Max(_speed, value * _relativeSpeed), value, value, _format); - } - else - { - if (ImGui.DragFloat($"##{valueIdx}", ref value, Math.Max(_speed, value * _relativeSpeed), _minimum ?? 0.0f, - _maximum ?? 0.0f, _format)) - { - values[valueIdx] = Clamp(value) * _factor + _bias; - ret = true; - } - } - } - - return ret; - } - - private float Clamp(float value) - => Math.Clamp(value, _minimum ?? float.NegativeInfinity, _maximum ?? float.PositiveInfinity); - } - - private sealed class IntConstantEditor : IConstantEditor - { - private readonly int? _minimum; - private readonly int? _maximum; - private readonly float _speed; - private readonly float _relativeSpeed; - private readonly float _factor; - private readonly float _bias; - private readonly string _format; - - public IntConstantEditor(int? minimum, int? maximum, float speed, float relativeSpeed, float factor, float bias, string unit) - { - _minimum = minimum; - _maximum = maximum; - _speed = speed; - _relativeSpeed = relativeSpeed; - _factor = factor; - _bias = bias; - _format = "%d"; - if (unit.Length > 0) - _format = $"{_format} {unit.Replace("%", "%%")}"; - } - - public bool Draw(Span values, bool disabled) - { - var spacing = ImGui.GetStyle().ItemInnerSpacing.X; - var fieldWidth = (ImGui.CalcItemWidth() - (values.Length - 1) * spacing) / values.Length; - - var ret = false; - - // Not using DragScalarN because of _relativeSpeed and other points of lost flexibility. - for (var valueIdx = 0; valueIdx < values.Length; ++valueIdx) - { - if (valueIdx > 0) - ImGui.SameLine(0.0f, spacing); - - ImGui.SetNextItemWidth(MathF.Round(fieldWidth * (valueIdx + 1)) - MathF.Round(fieldWidth * valueIdx)); - - var value = (int)Math.Clamp(MathF.Round((values[valueIdx] - _bias) / _factor), int.MinValue, int.MaxValue); - if (disabled) - { - ImGui.DragInt($"##{valueIdx}", ref value, Math.Max(_speed, value * _relativeSpeed), value, value, _format); - } - else - { - if (ImGui.DragInt($"##{valueIdx}", ref value, Math.Max(_speed, value * _relativeSpeed), _minimum ?? 0, _maximum ?? 0, - _format)) - { - values[valueIdx] = Clamp(value) * _factor + _bias; - ret = true; - } - } - } - - return ret; - } - - private int Clamp(int value) - => Math.Clamp(value, _minimum ?? int.MinValue, _maximum ?? int.MaxValue); - } - - private sealed class ColorConstantEditor : IConstantEditor - { - private readonly bool _squaredRgb; - private readonly bool _clamped; - - public ColorConstantEditor(bool squaredRgb, bool clamped) - { - _squaredRgb = squaredRgb; - _clamped = clamped; - } - - public bool Draw(Span values, bool disabled) - { - switch (values.Length) - { - case 3: - { - var value = new Vector3(values); - if (_squaredRgb) - value = PseudoSqrtRgb(value); - if (!ImGui.ColorEdit3("##0", ref value, ImGuiColorEditFlags.Float | (_clamped ? 0 : ImGuiColorEditFlags.HDR)) || disabled) - return false; - - if (_squaredRgb) - value = PseudoSquareRgb(value); - if (_clamped) - value = Vector3.Clamp(value, Vector3.Zero, Vector3.One); - value.CopyTo(values); - return true; - } - case 4: - { - var value = new Vector4(values); - if (_squaredRgb) - value = PseudoSqrtRgb(value); - if (!ImGui.ColorEdit4("##0", ref value, - ImGuiColorEditFlags.Float | ImGuiColorEditFlags.AlphaPreviewHalf | (_clamped ? 0 : ImGuiColorEditFlags.HDR)) - || disabled) - return false; - - if (_squaredRgb) - value = PseudoSquareRgb(value); - if (_clamped) - value = Vector4.Clamp(value, Vector4.Zero, Vector4.One); - value.CopyTo(values); - return true; - } - default: return FloatConstantEditor.Default.Draw(values, disabled); - } - } - } - - private sealed class EnumConstantEditor : IConstantEditor - { - private readonly IReadOnlyList<(string Label, float Value, string Description)> _values; - - public EnumConstantEditor(IReadOnlyList<(string Label, float Value, string Description)> values) - => _values = values; - - public bool Draw(Span values, bool disabled) - { - var spacing = ImGui.GetStyle().ItemInnerSpacing.X; - var fieldWidth = (ImGui.CalcItemWidth() - (values.Length - 1) * spacing) / values.Length; - - var ret = false; - - for (var valueIdx = 0; valueIdx < values.Length; ++valueIdx) - { - using var id = ImRaii.PushId(valueIdx); - if (valueIdx > 0) - ImGui.SameLine(0.0f, spacing); - - ImGui.SetNextItemWidth(MathF.Round(fieldWidth * (valueIdx + 1)) - MathF.Round(fieldWidth * valueIdx)); - - var currentValue = values[valueIdx]; - var currentLabel = _values.FirstOrNull(v => v.Value == currentValue)?.Label - ?? currentValue.ToString(CultureInfo.CurrentCulture); - ret = disabled - ? ImGui.InputText(string.Empty, ref currentLabel, (uint)currentLabel.Length, ImGuiInputTextFlags.ReadOnly) - : DrawCombo(currentLabel, ref values[valueIdx]); - } - - return ret; - } - - private bool DrawCombo(string label, ref float currentValue) - { - using var c = ImRaii.Combo(string.Empty, label); - if (!c) - return false; - - var ret = false; - foreach (var (valueLabel, value, valueDescription) in _values) - { - if (ImGui.Selectable(valueLabel, value == currentValue)) - { - currentValue = value; - ret = true; - } - - if (valueDescription.Length > 0) - ImGuiUtil.SelectableHelpMarker(valueDescription); - } - - return ret; - } - } -} diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs deleted file mode 100644 index 29fd7531..00000000 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs +++ /dev/null @@ -1,783 +0,0 @@ -using Dalamud.Interface; -using Dalamud.Interface.ImGuiNotification; -using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; -using ImGuiNET; -using Newtonsoft.Json.Linq; -using OtterGui; -using OtterGui.Classes; -using OtterGui.Raii; -using Penumbra.GameData.Data; -using Penumbra.GameData.Files; -using Penumbra.GameData.Files.MaterialStructs; -using Penumbra.GameData.Structs; -using Penumbra.Interop.Hooks.Objects; -using Penumbra.Interop.MaterialPreview; -using Penumbra.String; -using Penumbra.String.Classes; -using Penumbra.UI.Classes; -using static Penumbra.GameData.Files.ShpkFile; - -namespace Penumbra.UI.AdvancedWindow; - -public partial class ModEditWindow -{ - private sealed class MtrlTab : IWritable, IDisposable - { - private const int ShpkPrefixLength = 16; - - private static readonly CiByteString ShpkPrefix = CiByteString.FromSpanUnsafe("shader/sm5/shpk/"u8, true, true, true); - - private readonly ModEditWindow _edit; - public readonly MtrlFile Mtrl; - public readonly string FilePath; - public readonly bool Writable; - - private string[]? _shpkNames; - - public string ShaderHeader = "Shader###Shader"; - public FullPath LoadedShpkPath = FullPath.Empty; - public string LoadedShpkPathName = string.Empty; - public string LoadedShpkDevkitPathName = string.Empty; - public string ShaderComment = string.Empty; - public ShpkFile? AssociatedShpk; - public JObject? AssociatedShpkDevkit; - - public readonly string LoadedBaseDevkitPathName; - public readonly JObject? AssociatedBaseDevkit; - - // Shader Key State - public readonly - List<(string Label, int Index, string Description, bool MonoFont, IReadOnlyList<(string Label, uint Value, string Description)> - Values)> ShaderKeys = new(16); - - public readonly HashSet VertexShaders = new(16); - public readonly HashSet PixelShaders = new(16); - public bool ShadersKnown; - public string VertexShadersString = "Vertex Shaders: ???"; - public string PixelShadersString = "Pixel Shaders: ???"; - - // Textures & Samplers - public readonly List<(string Label, int TextureIndex, int SamplerIndex, string Description, bool MonoFont)> Textures = new(4); - - public readonly HashSet UnfoldedTextures = new(4); - public readonly HashSet SamplerIds = new(16); - public float TextureLabelWidth; - - // Material Constants - public readonly - List<(string Header, List<(string Label, int ConstantIndex, Range Slice, string Description, bool MonoFont, IConstantEditor Editor)> - Constants)> Constants = new(16); - - // Live-Previewers - public readonly List MaterialPreviewers = new(4); - public readonly List ColorTablePreviewers = new(4); - public int HighlightedColorTableRow = -1; - public readonly Stopwatch HighlightTime = new(); - - public FullPath FindAssociatedShpk(out string defaultPath, out Utf8GamePath defaultGamePath) - { - defaultPath = GamePaths.Shader.ShpkPath(Mtrl.ShaderPackage.Name); - if (!Utf8GamePath.FromString(defaultPath, out defaultGamePath)) - return FullPath.Empty; - - return _edit.FindBestMatch(defaultGamePath); - } - - public string[] GetShpkNames() - { - if (null != _shpkNames) - return _shpkNames; - - var names = new HashSet(StandardShaderPackages); - names.UnionWith(_edit.FindPathsStartingWith(ShpkPrefix).Select(path => path.ToString()[ShpkPrefixLength..])); - - _shpkNames = names.ToArray(); - Array.Sort(_shpkNames); - - return _shpkNames; - } - - public void LoadShpk(FullPath path) - { - ShaderHeader = $"Shader ({Mtrl.ShaderPackage.Name})###Shader"; - - try - { - LoadedShpkPath = path; - var data = LoadedShpkPath.IsRooted - ? File.ReadAllBytes(LoadedShpkPath.FullName) - : _edit._gameData.GetFile(LoadedShpkPath.InternalName.ToString())?.Data; - AssociatedShpk = data?.Length > 0 ? new ShpkFile(data) : throw new Exception("Failure to load file data."); - LoadedShpkPathName = path.ToPath(); - } - catch (Exception e) - { - LoadedShpkPath = FullPath.Empty; - LoadedShpkPathName = string.Empty; - AssociatedShpk = null; - Penumbra.Messager.NotificationMessage(e, $"Could not load {LoadedShpkPath.ToPath()}.", NotificationType.Error, false); - } - - if (LoadedShpkPath.InternalName.IsEmpty) - { - AssociatedShpkDevkit = null; - LoadedShpkDevkitPathName = string.Empty; - } - else - { - AssociatedShpkDevkit = - TryLoadShpkDevkit(Path.GetFileNameWithoutExtension(Mtrl.ShaderPackage.Name), out LoadedShpkDevkitPathName); - } - - UpdateShaderKeys(); - Update(); - } - - private JObject? TryLoadShpkDevkit(string shpkBaseName, out string devkitPathName) - { - try - { - if (!Utf8GamePath.FromString("penumbra/shpk_devkit/" + shpkBaseName + ".json", out var devkitPath)) - throw new Exception("Could not assemble ShPk dev-kit path."); - - var devkitFullPath = _edit.FindBestMatch(devkitPath); - if (!devkitFullPath.IsRooted) - throw new Exception("Could not resolve ShPk dev-kit path."); - - devkitPathName = devkitFullPath.FullName; - return JObject.Parse(File.ReadAllText(devkitFullPath.FullName)); - } - catch - { - devkitPathName = string.Empty; - return null; - } - } - - private T? TryGetShpkDevkitData(string category, uint? id, bool mayVary) where T : class - => TryGetShpkDevkitData(AssociatedShpkDevkit, LoadedShpkDevkitPathName, category, id, mayVary) - ?? TryGetShpkDevkitData(AssociatedBaseDevkit, LoadedBaseDevkitPathName, category, id, mayVary); - - private T? TryGetShpkDevkitData(JObject? devkit, string devkitPathName, string category, uint? id, bool mayVary) where T : class - { - if (devkit == null) - return null; - - try - { - var data = devkit[category]; - if (id.HasValue) - data = data?[id.Value.ToString()]; - - if (mayVary && (data as JObject)?["Vary"] != null) - { - var selector = BuildSelector(data!["Vary"]! - .Select(key => (uint)key) - .Select(key => Mtrl.GetShaderKey(key)?.Value ?? AssociatedShpk!.GetMaterialKeyById(key)!.Value.DefaultValue)); - var index = (int)data["Selectors"]![selector.ToString()]!; - data = data["Items"]![index]; - } - - return data?.ToObject(typeof(T)) as T; - } - catch (Exception e) - { - // Some element in the JSON was undefined or invalid (wrong type, key that doesn't exist in the ShPk, index out of range, …) - Penumbra.Log.Error($"Error while traversing the ShPk dev-kit file at {devkitPathName}: {e}"); - return null; - } - } - - private void UpdateShaderKeys() - { - ShaderKeys.Clear(); - if (AssociatedShpk != null) - foreach (var key in AssociatedShpk.MaterialKeys) - { - var dkData = TryGetShpkDevkitData("ShaderKeys", key.Id, false); - var hasDkLabel = !string.IsNullOrEmpty(dkData?.Label); - - var valueSet = new HashSet(key.Values); - if (dkData != null) - valueSet.UnionWith(dkData.Values.Keys); - - var mtrlKeyIndex = Mtrl.FindOrAddShaderKey(key.Id, key.DefaultValue); - var values = valueSet.Select(value => - { - if (dkData != null && dkData.Values.TryGetValue(value, out var dkValue)) - return (dkValue.Label.Length > 0 ? dkValue.Label : $"0x{value:X8}", value, dkValue.Description); - - return ($"0x{value:X8}", value, string.Empty); - }).ToArray(); - Array.Sort(values, (x, y) => - { - if (x.Value == key.DefaultValue) - return -1; - if (y.Value == key.DefaultValue) - return 1; - - return string.Compare(x.Label, y.Label, StringComparison.Ordinal); - }); - ShaderKeys.Add((hasDkLabel ? dkData!.Label : $"0x{key.Id:X8}", mtrlKeyIndex, dkData?.Description ?? string.Empty, - !hasDkLabel, values)); - } - else - foreach (var (key, index) in Mtrl.ShaderPackage.ShaderKeys.WithIndex()) - ShaderKeys.Add(($"0x{key.Category:X8}", index, string.Empty, true, Array.Empty<(string, uint, string)>())); - } - - private void UpdateShaders() - { - VertexShaders.Clear(); - PixelShaders.Clear(); - if (AssociatedShpk == null) - { - ShadersKnown = false; - } - else - { - ShadersKnown = true; - var systemKeySelectors = AllSelectors(AssociatedShpk.SystemKeys).ToArray(); - var sceneKeySelectors = AllSelectors(AssociatedShpk.SceneKeys).ToArray(); - var subViewKeySelectors = AllSelectors(AssociatedShpk.SubViewKeys).ToArray(); - var materialKeySelector = - BuildSelector(AssociatedShpk.MaterialKeys.Select(key => Mtrl.GetOrAddShaderKey(key.Id, key.DefaultValue).Value)); - foreach (var systemKeySelector in systemKeySelectors) - { - foreach (var sceneKeySelector in sceneKeySelectors) - { - foreach (var subViewKeySelector in subViewKeySelectors) - { - var selector = BuildSelector(systemKeySelector, sceneKeySelector, materialKeySelector, subViewKeySelector); - var node = AssociatedShpk.GetNodeBySelector(selector); - if (node.HasValue) - foreach (var pass in node.Value.Passes) - { - VertexShaders.Add((int)pass.VertexShader); - PixelShaders.Add((int)pass.PixelShader); - } - else - ShadersKnown = false; - } - } - } - } - - var vertexShaders = VertexShaders.OrderBy(i => i).Select(i => $"#{i}"); - var pixelShaders = PixelShaders.OrderBy(i => i).Select(i => $"#{i}"); - - VertexShadersString = $"Vertex Shaders: {string.Join(", ", ShadersKnown ? vertexShaders : vertexShaders.Append("???"))}"; - PixelShadersString = $"Pixel Shaders: {string.Join(", ", ShadersKnown ? pixelShaders : pixelShaders.Append("???"))}"; - - ShaderComment = TryGetShpkDevkitData("Comment", null, true) ?? string.Empty; - } - - private void UpdateTextures() - { - Textures.Clear(); - SamplerIds.Clear(); - if (AssociatedShpk == null) - { - SamplerIds.UnionWith(Mtrl.ShaderPackage.Samplers.Select(sampler => sampler.SamplerId)); - if (Mtrl.HasTable) - SamplerIds.Add(TableSamplerId); - - foreach (var (sampler, index) in Mtrl.ShaderPackage.Samplers.WithIndex()) - Textures.Add(($"0x{sampler.SamplerId:X8}", sampler.TextureIndex, index, string.Empty, true)); - } - else - { - foreach (var index in VertexShaders) - SamplerIds.UnionWith(AssociatedShpk.VertexShaders[index].Samplers.Select(sampler => sampler.Id)); - foreach (var index in PixelShaders) - SamplerIds.UnionWith(AssociatedShpk.PixelShaders[index].Samplers.Select(sampler => sampler.Id)); - if (!ShadersKnown) - { - SamplerIds.UnionWith(Mtrl.ShaderPackage.Samplers.Select(sampler => sampler.SamplerId)); - if (Mtrl.HasTable) - SamplerIds.Add(TableSamplerId); - } - - foreach (var samplerId in SamplerIds) - { - var shpkSampler = AssociatedShpk.GetSamplerById(samplerId); - if (shpkSampler is not { Slot: 2 }) - continue; - - var dkData = TryGetShpkDevkitData("Samplers", samplerId, true); - var hasDkLabel = !string.IsNullOrEmpty(dkData?.Label); - - var sampler = Mtrl.GetOrAddSampler(samplerId, dkData?.DefaultTexture ?? string.Empty, out var samplerIndex); - Textures.Add((hasDkLabel ? dkData!.Label : shpkSampler.Value.Name, sampler.TextureIndex, samplerIndex, - dkData?.Description ?? string.Empty, !hasDkLabel)); - } - - if (SamplerIds.Contains(TableSamplerId)) - Mtrl.HasTable = true; - } - - Textures.Sort((x, y) => string.CompareOrdinal(x.Label, y.Label)); - - TextureLabelWidth = 50f * UiHelpers.Scale; - - float helpWidth; - using (var _ = ImRaii.PushFont(UiBuilder.IconFont)) - { - helpWidth = ImGui.GetStyle().ItemSpacing.X + ImGui.CalcTextSize(FontAwesomeIcon.InfoCircle.ToIconString()).X; - } - - foreach (var (label, _, _, description, monoFont) in Textures) - { - if (!monoFont) - TextureLabelWidth = Math.Max(TextureLabelWidth, ImGui.CalcTextSize(label).X + (description.Length > 0 ? helpWidth : 0.0f)); - } - - using (var _ = ImRaii.PushFont(UiBuilder.MonoFont)) - { - foreach (var (label, _, _, description, monoFont) in Textures) - { - if (monoFont) - TextureLabelWidth = Math.Max(TextureLabelWidth, - ImGui.CalcTextSize(label).X + (description.Length > 0 ? helpWidth : 0.0f)); - } - } - - TextureLabelWidth = TextureLabelWidth / UiHelpers.Scale + 4; - } - - private void UpdateConstants() - { - static List FindOrAddGroup(List<(string, List)> groups, string name) - { - foreach (var (groupName, group) in groups) - { - if (string.Equals(name, groupName, StringComparison.Ordinal)) - return group; - } - - var newGroup = new List(16); - groups.Add((name, newGroup)); - return newGroup; - } - - Constants.Clear(); - if (AssociatedShpk == null) - { - var fcGroup = FindOrAddGroup(Constants, "Further Constants"); - foreach (var (constant, index) in Mtrl.ShaderPackage.Constants.WithIndex()) - { - var values = Mtrl.GetConstantValues(constant); - for (var i = 0; i < values.Length; i += 4) - { - fcGroup.Add(($"0x{constant.Id:X8}", index, i..Math.Min(i + 4, values.Length), string.Empty, true, - FloatConstantEditor.Default)); - } - } - } - else - { - var prefix = AssociatedShpk.GetConstantById(MaterialParamsConstantId)?.Name ?? string.Empty; - foreach (var shpkConstant in AssociatedShpk.MaterialParams) - { - if ((shpkConstant.ByteSize & 0x3) != 0) - continue; - - var constant = Mtrl.GetOrAddConstant(shpkConstant.Id, shpkConstant.ByteSize >> 2, out var constantIndex); - var values = Mtrl.GetConstantValues(constant); - var handledElements = new IndexSet(values.Length, false); - - var dkData = TryGetShpkDevkitData("Constants", shpkConstant.Id, true); - if (dkData != null) - foreach (var dkConstant in dkData) - { - var offset = (int)dkConstant.Offset; - var length = values.Length - offset; - if (dkConstant.Length.HasValue) - length = Math.Min(length, (int)dkConstant.Length.Value); - if (length <= 0) - continue; - - var editor = dkConstant.CreateEditor(); - if (editor != null) - FindOrAddGroup(Constants, dkConstant.Group.Length > 0 ? dkConstant.Group : "Further Constants") - .Add((dkConstant.Label, constantIndex, offset..(offset + length), dkConstant.Description, false, editor)); - handledElements.AddRange(offset, length); - } - - var fcGroup = FindOrAddGroup(Constants, "Further Constants"); - foreach (var (start, end) in handledElements.Ranges(complement:true)) - { - if ((shpkConstant.ByteOffset & 0x3) == 0) - { - var offset = shpkConstant.ByteOffset >> 2; - for (int i = (start & ~0x3) - (offset & 0x3), j = offset >> 2; i < end; i += 4, ++j) - { - var rangeStart = Math.Max(i, start); - var rangeEnd = Math.Min(i + 4, end); - if (rangeEnd > rangeStart) - fcGroup.Add(( - $"{prefix}[{j:D2}]{VectorSwizzle((offset + rangeStart) & 0x3, (offset + rangeEnd - 1) & 0x3)} (0x{shpkConstant.Id:X8})", - constantIndex, rangeStart..rangeEnd, string.Empty, true, FloatConstantEditor.Default)); - } - } - else - { - for (var i = start; i < end; i += 4) - { - fcGroup.Add(($"0x{shpkConstant.Id:X8}", constantIndex, i..Math.Min(i + 4, end), string.Empty, true, - FloatConstantEditor.Default)); - } - } - } - } - } - - Constants.RemoveAll(group => group.Constants.Count == 0); - Constants.Sort((x, y) => - { - if (string.Equals(x.Header, "Further Constants", StringComparison.Ordinal)) - return 1; - if (string.Equals(y.Header, "Further Constants", StringComparison.Ordinal)) - return -1; - - return string.Compare(x.Header, y.Header, StringComparison.Ordinal); - }); - // HACK the Replace makes w appear after xyz, for the cbuffer-location-based naming scheme - foreach (var (_, group) in Constants) - { - group.Sort((x, y) => string.CompareOrdinal( - x.MonoFont ? x.Label.Replace("].w", "].{") : x.Label, - y.MonoFont ? y.Label.Replace("].w", "].{") : y.Label)); - } - } - - public unsafe void BindToMaterialInstances() - { - UnbindFromMaterialInstances(); - - var instances = MaterialInfo.FindMaterials(_edit._resourceTreeFactory.GetLocalPlayerRelatedCharacters().Select(ch => ch.Address), - FilePath); - - var foundMaterials = new HashSet(); - foreach (var materialInfo in instances) - { - var material = materialInfo.GetDrawObjectMaterial(_edit._objects); - if (foundMaterials.Contains((nint)material)) - continue; - - try - { - MaterialPreviewers.Add(new LiveMaterialPreviewer(_edit._objects, materialInfo)); - foundMaterials.Add((nint)material); - } - catch (InvalidOperationException) - { - // Carry on without that previewer. - } - } - - UpdateMaterialPreview(); - - if (!Mtrl.HasTable) - return; - - foreach (var materialInfo in instances) - { - try - { - ColorTablePreviewers.Add(new LiveColorTablePreviewer(_edit._objects, _edit._framework, materialInfo)); - } - catch (InvalidOperationException) - { - // Carry on without that previewer. - } - } - - UpdateColorTablePreview(); - } - - private void UnbindFromMaterialInstances() - { - foreach (var previewer in MaterialPreviewers) - previewer.Dispose(); - MaterialPreviewers.Clear(); - - foreach (var previewer in ColorTablePreviewers) - previewer.Dispose(); - ColorTablePreviewers.Clear(); - } - - private unsafe void UnbindFromDrawObjectMaterialInstances(CharacterBase* characterBase) - { - for (var i = MaterialPreviewers.Count; i-- > 0;) - { - var previewer = MaterialPreviewers[i]; - if (previewer.DrawObject != characterBase) - continue; - - previewer.Dispose(); - MaterialPreviewers.RemoveAt(i); - } - - for (var i = ColorTablePreviewers.Count; i-- > 0;) - { - var previewer = ColorTablePreviewers[i]; - if (previewer.DrawObject != characterBase) - continue; - - previewer.Dispose(); - ColorTablePreviewers.RemoveAt(i); - } - } - - public void SetShaderPackageFlags(uint shPkFlags) - { - foreach (var previewer in MaterialPreviewers) - previewer.SetShaderPackageFlags(shPkFlags); - } - - public void SetMaterialParameter(uint parameterCrc, Index offset, Span value) - { - foreach (var previewer in MaterialPreviewers) - previewer.SetMaterialParameter(parameterCrc, offset, value); - } - - public void SetSamplerFlags(uint samplerCrc, uint samplerFlags) - { - foreach (var previewer in MaterialPreviewers) - previewer.SetSamplerFlags(samplerCrc, samplerFlags); - } - - private void UpdateMaterialPreview() - { - SetShaderPackageFlags(Mtrl.ShaderPackage.Flags); - foreach (var constant in Mtrl.ShaderPackage.Constants) - { - var values = Mtrl.GetConstantValues(constant); - if (values != null) - SetMaterialParameter(constant.Id, 0, values); - } - - foreach (var sampler in Mtrl.ShaderPackage.Samplers) - SetSamplerFlags(sampler.SamplerId, sampler.Flags); - } - - public void HighlightColorTableRow(int rowIdx) - { - var oldRowIdx = HighlightedColorTableRow; - - if (HighlightedColorTableRow != rowIdx) - { - HighlightedColorTableRow = rowIdx; - HighlightTime.Restart(); - } - - if (oldRowIdx >= 0) - UpdateColorTableRowPreview(oldRowIdx); - if (rowIdx >= 0) - UpdateColorTableRowPreview(rowIdx); - } - - public void CancelColorTableHighlight() - { - var rowIdx = HighlightedColorTableRow; - - HighlightedColorTableRow = -1; - HighlightTime.Reset(); - - if (rowIdx >= 0) - UpdateColorTableRowPreview(rowIdx); - } - - public void UpdateColorTableRowPreview(int rowIdx) - { - if (ColorTablePreviewers.Count == 0) - return; - - if (!Mtrl.HasTable) - return; - - var row = new LegacyColorTable.Row(Mtrl.Table[rowIdx]); - if (Mtrl.HasDyeTable) - { - var stm = _edit._stainService.StmFile; - var dye = new LegacyColorDyeTable.Row(Mtrl.DyeTable[rowIdx]); - if (stm.TryGetValue(dye.Template, _edit._stainService.StainCombo.CurrentSelection.Key, out var dyes)) - row.ApplyDyeTemplate(dye, dyes); - } - - if (HighlightedColorTableRow == rowIdx) - ApplyHighlight(ref row, (float)HighlightTime.Elapsed.TotalSeconds); - - foreach (var previewer in ColorTablePreviewers) - { - row.AsHalves().CopyTo(previewer.ColorTable.AsSpan() - .Slice(LiveColorTablePreviewer.TextureWidth * 4 * rowIdx, LiveColorTablePreviewer.TextureWidth * 4)); - previewer.ScheduleUpdate(); - } - } - - public void UpdateColorTablePreview() - { - if (ColorTablePreviewers.Count == 0) - return; - - if (!Mtrl.HasTable) - return; - - var rows = new LegacyColorTable(Mtrl.Table); - var dyeRows = new LegacyColorDyeTable(Mtrl.DyeTable); - if (Mtrl.HasDyeTable) - { - var stm = _edit._stainService.StmFile; - var stainId = (StainId)_edit._stainService.StainCombo.CurrentSelection.Key; - for (var i = 0; i < LegacyColorTable.NumUsedRows; ++i) - { - ref var row = ref rows[i]; - var dye = dyeRows[i]; - if (stm.TryGetValue(dye.Template, stainId, out var dyes)) - row.ApplyDyeTemplate(dye, dyes); - } - } - - if (HighlightedColorTableRow >= 0) - ApplyHighlight(ref rows[HighlightedColorTableRow], (float)HighlightTime.Elapsed.TotalSeconds); - - foreach (var previewer in ColorTablePreviewers) - { - // TODO: Dawntrail - rows.AsHalves().CopyTo(previewer.ColorTable); - previewer.ScheduleUpdate(); - } - } - - private static void ApplyHighlight(ref LegacyColorTable.Row row, float time) - { - var level = (MathF.Sin(time * 2.0f * MathF.PI) + 2.0f) / 3.0f / 255.0f; - var baseColor = ColorId.InGameHighlight.Value(); - var color = level * new Vector3(baseColor & 0xFF, (baseColor >> 8) & 0xFF, (baseColor >> 16) & 0xFF); - - row.Diffuse = Vector3.Zero; - row.Specular = Vector3.Zero; - row.Emissive = color * color; - } - - public void Update() - { - UpdateShaders(); - UpdateTextures(); - UpdateConstants(); - } - - public unsafe MtrlTab(ModEditWindow edit, MtrlFile file, string filePath, bool writable) - { - _edit = edit; - Mtrl = file; - FilePath = filePath; - Writable = writable; - AssociatedBaseDevkit = TryLoadShpkDevkit("_base", out LoadedBaseDevkitPathName); - LoadShpk(FindAssociatedShpk(out _, out _)); - if (writable) - { - _edit._characterBaseDestructor.Subscribe(UnbindFromDrawObjectMaterialInstances, CharacterBaseDestructor.Priority.MtrlTab); - BindToMaterialInstances(); - } - } - - public unsafe void Dispose() - { - UnbindFromMaterialInstances(); - if (Writable) - _edit._characterBaseDestructor.Unsubscribe(UnbindFromDrawObjectMaterialInstances); - } - - // TODO Readd ShadersKnown - public bool Valid - => (true || ShadersKnown) && Mtrl.Valid; - - public byte[] Write() - { - var output = Mtrl.Clone(); - output.GarbageCollect(AssociatedShpk, SamplerIds); - - return output.Write(); - } - - private sealed class DevkitShaderKeyValue - { - public string Label = string.Empty; - public string Description = string.Empty; - } - - private sealed class DevkitShaderKey - { - public string Label = string.Empty; - public string Description = string.Empty; - public Dictionary Values = new(); - } - - private sealed class DevkitSampler - { - public string Label = string.Empty; - public string Description = string.Empty; - public string DefaultTexture = string.Empty; - } - - private enum DevkitConstantType - { - Hidden = -1, - Float = 0, - Integer = 1, - Color = 2, - Enum = 3, - } - - private sealed class DevkitConstantValue - { - public string Label = string.Empty; - public string Description = string.Empty; - public float Value = 0; - } - - private sealed class DevkitConstant - { - public uint Offset = 0; - public uint? Length = null; - public string Group = string.Empty; - public string Label = string.Empty; - public string Description = string.Empty; - public DevkitConstantType Type = DevkitConstantType.Float; - - public float? Minimum = null; - public float? Maximum = null; - public float? Speed = null; - public float RelativeSpeed = 0.0f; - public float Factor = 1.0f; - public float Bias = 0.0f; - public byte Precision = 3; - public string Unit = string.Empty; - - public bool SquaredRgb = false; - public bool Clamped = false; - - public DevkitConstantValue[] Values = Array.Empty(); - - public IConstantEditor? CreateEditor() - => Type switch - { - DevkitConstantType.Hidden => null, - DevkitConstantType.Float => new FloatConstantEditor(Minimum, Maximum, Speed ?? 0.1f, RelativeSpeed, Factor, Bias, Precision, - Unit), - DevkitConstantType.Integer => new IntConstantEditor(ToInteger(Minimum), ToInteger(Maximum), Speed ?? 0.25f, RelativeSpeed, - Factor, Bias, Unit), - DevkitConstantType.Color => new ColorConstantEditor(SquaredRgb, Clamped), - DevkitConstantType.Enum => new EnumConstantEditor(Array.ConvertAll(Values, - value => (value.Label, value.Value, value.Description))), - _ => FloatConstantEditor.Default, - }; - - private static int? ToInteger(float? value) - => value.HasValue ? (int)Math.Clamp(MathF.Round(value.Value), int.MinValue, int.MaxValue) : null; - } - } -} diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.Shpk.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.Shpk.cs deleted file mode 100644 index b9525b29..00000000 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.Shpk.cs +++ /dev/null @@ -1,481 +0,0 @@ -using Dalamud.Interface; -using ImGuiNET; -using OtterGui; -using OtterGui.Raii; -using Penumbra.GameData; -using Penumbra.String.Classes; - -namespace Penumbra.UI.AdvancedWindow; - -public partial class ModEditWindow -{ - private readonly FileDialogService _fileDialog; - - // strings path/to/the.exe | grep --fixed-strings '.shpk' | sort -u | sed -e 's#^shader/sm5/shpk/##' - // Apricot shader packages are unlisted because - // 1. they cause performance/memory issues when calculating the effective shader set - // 2. they probably aren't intended for use with materials anyway - private static readonly IReadOnlyList StandardShaderPackages = new[] - { - "3dui.shpk", - // "apricot_decal_dummy.shpk", - // "apricot_decal_ring.shpk", - // "apricot_decal.shpk", - // "apricot_lightmodel.shpk", - // "apricot_model_dummy.shpk", - // "apricot_model_morph.shpk", - // "apricot_model.shpk", - // "apricot_powder_dummy.shpk", - // "apricot_powder.shpk", - // "apricot_shape_dummy.shpk", - // "apricot_shape.shpk", - "bgcolorchange.shpk", - "bgcrestchange.shpk", - "bgdecal.shpk", - "bg.shpk", - "bguvscroll.shpk", - "channeling.shpk", - "characterglass.shpk", - "charactershadowoffset.shpk", - "character.shpk", - "cloud.shpk", - "createviewposition.shpk", - "crystal.shpk", - "directionallighting.shpk", - "directionalshadow.shpk", - "grass.shpk", - "hair.shpk", - "iris.shpk", - "lightshaft.shpk", - "linelighting.shpk", - "planelighting.shpk", - "pointlighting.shpk", - "river.shpk", - "shadowmask.shpk", - "skin.shpk", - "spotlighting.shpk", - "verticalfog.shpk", - "water.shpk", - "weather.shpk", - }; - - private enum TextureAddressMode : uint - { - Wrap = 0, - Mirror = 1, - Clamp = 2, - Border = 3, - } - - private static readonly IReadOnlyList TextureAddressModeTooltips = new[] - { - "Tile the texture at every UV integer junction.\n\nFor example, for U values between 0 and 3, the texture is repeated three times.", - "Flip the texture at every UV integer junction.\n\nFor U values between 0 and 1, for example, the texture is addressed normally; between 1 and 2, the texture is mirrored; between 2 and 3, the texture is normal again; and so on.", - "Texture coordinates outside the range [0.0, 1.0] are set to the texture color at 0.0 or 1.0, respectively.", - "Texture coordinates outside the range [0.0, 1.0] are set to the border color (generally black).", - }; - - private static bool DrawPackageNameInput(MtrlTab tab, bool disabled) - { - if (disabled) - { - ImGui.TextUnformatted("Shader Package: " + tab.Mtrl.ShaderPackage.Name); - return false; - } - - var ret = false; - ImGui.SetNextItemWidth(UiHelpers.Scale * 250.0f); - using var c = ImRaii.Combo("Shader Package", tab.Mtrl.ShaderPackage.Name); - if (c) - foreach (var value in tab.GetShpkNames()) - { - if (ImGui.Selectable(value, value == tab.Mtrl.ShaderPackage.Name)) - { - tab.Mtrl.ShaderPackage.Name = value; - ret = true; - tab.AssociatedShpk = null; - tab.LoadedShpkPath = FullPath.Empty; - tab.LoadShpk(tab.FindAssociatedShpk(out _, out _)); - } - } - - return ret; - } - - private static bool DrawShaderFlagsInput(MtrlTab tab, bool disabled) - { - var shpkFlags = (int)tab.Mtrl.ShaderPackage.Flags; - ImGui.SetNextItemWidth(UiHelpers.Scale * 250.0f); - if (!ImGui.InputInt("Shader Flags", ref shpkFlags, 0, 0, - ImGuiInputTextFlags.CharsHexadecimal | (disabled ? ImGuiInputTextFlags.ReadOnly : ImGuiInputTextFlags.None))) - return false; - - tab.Mtrl.ShaderPackage.Flags = (uint)shpkFlags; - tab.SetShaderPackageFlags((uint)shpkFlags); - return true; - } - - /// - /// Show the currently associated shpk file, if any, and the buttons to associate - /// a specific shpk from your drive, the modded shpk by path or the default shpk. - /// - private void DrawCustomAssociations(MtrlTab tab) - { - const string tooltip = "Click to copy file path to clipboard."; - var text = tab.AssociatedShpk == null - ? "Associated .shpk file: None" - : $"Associated .shpk file: {tab.LoadedShpkPathName}"; - var devkitText = tab.AssociatedShpkDevkit == null - ? "Associated dev-kit file: None" - : $"Associated dev-kit file: {tab.LoadedShpkDevkitPathName}"; - var baseDevkitText = tab.AssociatedBaseDevkit == null - ? "Base dev-kit file: None" - : $"Base dev-kit file: {tab.LoadedBaseDevkitPathName}"; - - ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - - ImGuiUtil.CopyOnClickSelectable(text, tab.LoadedShpkPathName, tooltip); - ImGuiUtil.CopyOnClickSelectable(devkitText, tab.LoadedShpkDevkitPathName, tooltip); - ImGuiUtil.CopyOnClickSelectable(baseDevkitText, tab.LoadedBaseDevkitPathName, tooltip); - - if (ImGui.Button("Associate Custom .shpk File")) - _fileDialog.OpenFilePicker("Associate Custom .shpk File...", ".shpk", (success, name) => - { - if (success) - tab.LoadShpk(new FullPath(name[0])); - }, 1, Mod!.ModPath.FullName, false); - - var moddedPath = tab.FindAssociatedShpk(out var defaultPath, out var gamePath); - ImGui.SameLine(); - if (ImGuiUtil.DrawDisabledButton("Associate Default .shpk File", Vector2.Zero, moddedPath.ToPath(), - moddedPath.Equals(tab.LoadedShpkPath))) - tab.LoadShpk(moddedPath); - - if (!gamePath.Path.Equals(moddedPath.InternalName)) - { - ImGui.SameLine(); - if (ImGuiUtil.DrawDisabledButton("Associate Unmodded .shpk File", Vector2.Zero, defaultPath, - gamePath.Path.Equals(tab.LoadedShpkPath.InternalName))) - tab.LoadShpk(new FullPath(gamePath)); - } - - ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - } - - private static bool DrawMaterialShaderKeys(MtrlTab tab, bool disabled) - { - if (tab.ShaderKeys.Count == 0) - return false; - - var ret = false; - foreach (var (label, index, description, monoFont, values) in tab.ShaderKeys) - { - using var font = ImRaii.PushFont(UiBuilder.MonoFont, monoFont); - ref var key = ref tab.Mtrl.ShaderPackage.ShaderKeys[index]; - var shpkKey = tab.AssociatedShpk?.GetMaterialKeyById(key.Category); - var currentValue = key.Value; - var (currentLabel, _, currentDescription) = - values.FirstOrNull(v => v.Value == currentValue) ?? ($"0x{currentValue:X8}", currentValue, string.Empty); - if (!disabled && shpkKey.HasValue) - { - ImGui.SetNextItemWidth(UiHelpers.Scale * 250.0f); - using (var c = ImRaii.Combo($"##{key.Category:X8}", currentLabel)) - { - if (c) - foreach (var (valueLabel, value, valueDescription) in values) - { - if (ImGui.Selectable(valueLabel, value == currentValue)) - { - key.Value = value; - ret = true; - tab.Update(); - } - - if (valueDescription.Length > 0) - ImGuiUtil.SelectableHelpMarker(valueDescription); - } - } - - ImGui.SameLine(); - if (description.Length > 0) - ImGuiUtil.LabeledHelpMarker(label, description); - else - ImGui.TextUnformatted(label); - } - else if (description.Length > 0 || currentDescription.Length > 0) - { - ImGuiUtil.LabeledHelpMarker($"{label}: {currentLabel}", - description + (description.Length > 0 && currentDescription.Length > 0 ? "\n\n" : string.Empty) + currentDescription); - } - else - { - ImGui.TextUnformatted($"{label}: {currentLabel}"); - } - } - - return ret; - } - - private static void DrawMaterialShaders(MtrlTab tab) - { - if (tab.AssociatedShpk == null) - return; - - ImRaii.TreeNode(tab.VertexShadersString, ImGuiTreeNodeFlags.Leaf).Dispose(); - ImRaii.TreeNode(tab.PixelShadersString, ImGuiTreeNodeFlags.Leaf).Dispose(); - - if (tab.ShaderComment.Length > 0) - { - ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - ImGui.TextUnformatted(tab.ShaderComment); - } - } - - private static bool DrawMaterialConstants(MtrlTab tab, bool disabled) - { - if (tab.Constants.Count == 0) - return false; - - ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - if (!ImGui.CollapsingHeader("Material Constants")) - return false; - - using var _ = ImRaii.PushId("MaterialConstants"); - - var ret = false; - foreach (var (header, group) in tab.Constants) - { - using var t = ImRaii.TreeNode(header, ImGuiTreeNodeFlags.DefaultOpen); - if (!t) - continue; - - foreach (var (label, constantIndex, slice, description, monoFont, editor) in group) - { - var constant = tab.Mtrl.ShaderPackage.Constants[constantIndex]; - var buffer = tab.Mtrl.GetConstantValues(constant); - if (buffer.Length > 0) - { - using var id = ImRaii.PushId($"##{constant.Id:X8}:{slice.Start}"); - ImGui.SetNextItemWidth(250.0f); - if (editor.Draw(buffer[slice], disabled)) - { - ret = true; - tab.SetMaterialParameter(constant.Id, slice.Start, buffer[slice]); - } - - ImGui.SameLine(); - using var font = ImRaii.PushFont(UiBuilder.MonoFont, monoFont); - if (description.Length > 0) - ImGuiUtil.LabeledHelpMarker(label, description); - else - ImGui.TextUnformatted(label); - } - } - } - - return ret; - } - - private static bool DrawMaterialSampler(MtrlTab tab, bool disabled, int textureIdx, int samplerIdx) - { - var ret = false; - ref var texture = ref tab.Mtrl.Textures[textureIdx]; - ref var sampler = ref tab.Mtrl.ShaderPackage.Samplers[samplerIdx]; - - // FIXME this probably doesn't belong here - static unsafe bool InputHexUInt16(string label, ref ushort v, ImGuiInputTextFlags flags) - { - fixed (ushort* v2 = &v) - { - return ImGui.InputScalar(label, ImGuiDataType.U16, (nint)v2, nint.Zero, nint.Zero, "%04X", flags); - } - } - - static bool ComboTextureAddressMode(string label, ref uint samplerFlags, int bitOffset) - { - var current = (TextureAddressMode)((samplerFlags >> bitOffset) & 0x3u); - using var c = ImRaii.Combo(label, current.ToString()); - if (!c) - return false; - - var ret = false; - foreach (var value in Enum.GetValues()) - { - if (ImGui.Selectable(value.ToString(), value == current)) - { - samplerFlags = (samplerFlags & ~(0x3u << bitOffset)) | ((uint)value << bitOffset); - ret = true; - } - - ImGuiUtil.SelectableHelpMarker(TextureAddressModeTooltips[(int)value]); - } - - return ret; - } - - var dx11 = texture.DX11; - if (ImGui.Checkbox("Prepend -- to the file name on DirectX 11", ref dx11)) - { - texture.DX11 = dx11; - ret = true; - } - - ImGui.SetNextItemWidth(UiHelpers.Scale * 100.0f); - if (ComboTextureAddressMode("##UAddressMode", ref sampler.Flags, 2)) - { - ret = true; - tab.SetSamplerFlags(sampler.SamplerId, sampler.Flags); - } - - ImGui.SameLine(); - ImGuiUtil.LabeledHelpMarker("U Address Mode", "Method to use for resolving a U texture coordinate that is outside the 0 to 1 range."); - - ImGui.SetNextItemWidth(UiHelpers.Scale * 100.0f); - if (ComboTextureAddressMode("##VAddressMode", ref sampler.Flags, 0)) - { - ret = true; - tab.SetSamplerFlags(sampler.SamplerId, sampler.Flags); - } - - ImGui.SameLine(); - ImGuiUtil.LabeledHelpMarker("V Address Mode", "Method to use for resolving a V texture coordinate that is outside the 0 to 1 range."); - - var lodBias = ((int)(sampler.Flags << 12) >> 22) / 64.0f; - ImGui.SetNextItemWidth(UiHelpers.Scale * 100.0f); - if (ImGui.DragFloat("##LoDBias", ref lodBias, 0.1f, -8.0f, 7.984375f)) - { - sampler.Flags = (uint)((sampler.Flags & ~0x000FFC00) - | ((uint)((int)Math.Round(Math.Clamp(lodBias, -8.0f, 7.984375f) * 64.0f) & 0x3FF) << 10)); - ret = true; - tab.SetSamplerFlags(sampler.SamplerId, sampler.Flags); - } - - ImGui.SameLine(); - ImGuiUtil.LabeledHelpMarker("Level of Detail Bias", - "Offset from the calculated mipmap level.\n\nHigher means that the texture will start to lose detail nearer.\nLower means that the texture will keep its detail until farther."); - - var minLod = (int)((sampler.Flags >> 20) & 0xF); - ImGui.SetNextItemWidth(UiHelpers.Scale * 100.0f); - if (ImGui.DragInt("##MinLoD", ref minLod, 0.1f, 0, 15)) - { - sampler.Flags = (uint)((sampler.Flags & ~0x00F00000) | ((uint)Math.Clamp(minLod, 0, 15) << 20)); - ret = true; - tab.SetSamplerFlags(sampler.SamplerId, sampler.Flags); - } - - ImGui.SameLine(); - ImGuiUtil.LabeledHelpMarker("Minimum Level of Detail", - "Most detailed mipmap level to use.\n\n0 is the full-sized texture, 1 is the half-sized texture, 2 is the quarter-sized texture, and so on.\n15 will forcibly reduce the texture to its smallest mipmap."); - - using var t = ImRaii.TreeNode("Advanced Settings"); - if (!t) - return ret; - - ImGui.SetNextItemWidth(UiHelpers.Scale * 100.0f); - if (InputHexUInt16("Texture Flags", ref texture.Flags, - disabled ? ImGuiInputTextFlags.ReadOnly : ImGuiInputTextFlags.None)) - ret = true; - - var samplerFlags = (int)sampler.Flags; - ImGui.SetNextItemWidth(UiHelpers.Scale * 100.0f); - if (ImGui.InputInt("Sampler Flags", ref samplerFlags, 0, 0, - ImGuiInputTextFlags.CharsHexadecimal | (disabled ? ImGuiInputTextFlags.ReadOnly : ImGuiInputTextFlags.None))) - { - sampler.Flags = (uint)samplerFlags; - ret = true; - tab.SetSamplerFlags(sampler.SamplerId, (uint)samplerFlags); - } - - return ret; - } - - private bool DrawMaterialShader(MtrlTab tab, bool disabled) - { - var ret = false; - if (ImGui.CollapsingHeader(tab.ShaderHeader)) - { - ret |= DrawPackageNameInput(tab, disabled); - ret |= DrawShaderFlagsInput(tab, disabled); - DrawCustomAssociations(tab); - ret |= DrawMaterialShaderKeys(tab, disabled); - DrawMaterialShaders(tab); - } - - if (tab.AssociatedShpkDevkit == null) - { - ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - GC.KeepAlive(tab); - - var textColor = ImGui.GetColorU32(ImGuiCol.Text); - var textColorWarning = - (textColor & 0xFF000000u) - | ((textColor & 0x00FEFEFE) >> 1) - | (tab.AssociatedShpk == null ? 0x80u : 0x8080u); // Half red or yellow - - using var c = ImRaii.PushColor(ImGuiCol.Text, textColorWarning); - - ImGui.TextUnformatted(tab.AssociatedShpk == null - ? "Unable to find a suitable .shpk file for cross-references. Some functionality will be missing." - : "No dev-kit file found for this material's shaders. Please install one for optimal editing experience, such as actual constant names instead of hexadecimal identifiers."); - } - - return ret; - } - - private static string? MaterialParamName(bool componentOnly, int offset) - { - if (offset < 0) - return null; - - return (componentOnly, offset & 0x3) switch - { - (true, 0) => "x", - (true, 1) => "y", - (true, 2) => "z", - (true, 3) => "w", - (false, 0) => $"[{offset >> 2:D2}].x", - (false, 1) => $"[{offset >> 2:D2}].y", - (false, 2) => $"[{offset >> 2:D2}].z", - (false, 3) => $"[{offset >> 2:D2}].w", - _ => null, - }; - } - - private static string VectorSwizzle(int firstComponent, int lastComponent) - => (firstComponent, lastComponent) switch - { - (0, 4) => " ", - (0, 0) => ".x ", - (0, 1) => ".xy ", - (0, 2) => ".xyz ", - (0, 3) => " ", - (1, 1) => ".y ", - (1, 2) => ".yz ", - (1, 3) => ".yzw ", - (2, 2) => ".z ", - (2, 3) => ".zw ", - (3, 3) => ".w ", - _ => string.Empty, - }; - - private static (string? Name, bool ComponentOnly) MaterialParamRangeName(string prefix, int valueOffset, int valueLength) - { - if (valueLength == 0 || valueOffset < 0) - return (null, false); - - var firstVector = valueOffset >> 2; - var lastVector = (valueOffset + valueLength - 1) >> 2; - var firstComponent = valueOffset & 0x3; - var lastComponent = (valueOffset + valueLength - 1) & 0x3; - if (firstVector == lastVector) - return ($"{prefix}[{firstVector}]{VectorSwizzle(firstComponent, lastComponent)}", true); - - var sb = new StringBuilder(128); - sb.Append($"{prefix}[{firstVector}]{VectorSwizzle(firstComponent, 3).TrimEnd()}"); - for (var i = firstVector + 1; i < lastVector; ++i) - sb.Append($", [{i}]"); - - sb.Append($", [{lastVector}]{VectorSwizzle(0, lastComponent)}"); - return (sb.ToString(), false); - } -} diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs index 5a8fb13a..ee883daf 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs @@ -3,11 +3,7 @@ using Dalamud.Interface.Utility; using ImGuiNET; using OtterGui; using OtterGui.Raii; -using OtterGui.Text; -using OtterGui.Widgets; -using Penumbra.GameData.Files; -using Penumbra.String.Classes; -using Penumbra.UI.Classes; +using Penumbra.UI.AdvancedWindow.Materials; namespace Penumbra.UI.AdvancedWindow; @@ -17,177 +13,10 @@ public partial class ModEditWindow private bool DrawMaterialPanel(MtrlTab tab, bool disabled) { - DrawVersionUpdate(tab, disabled); - DrawMaterialLivePreviewRebind(tab, disabled); + if (tab.DrawVersionUpdate(disabled)) + _materialTab.SaveFile(); - ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - var ret = DrawBackFaceAndTransparency(tab, disabled); - - ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - ret |= DrawMaterialShader(tab, disabled); - - ret |= DrawMaterialTextureChange(tab, disabled); - ret |= DrawMaterialColorTableChange(tab, disabled); - ret |= DrawMaterialConstants(tab, disabled); - - ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - DrawOtherMaterialDetails(tab.Mtrl, disabled); - - return !disabled && ret; - } - - private void DrawVersionUpdate(MtrlTab tab, bool disabled) - { - if (disabled || tab.Mtrl.IsDawnTrail) - return; - - if (!ImUtf8.ButtonEx("Update MTRL Version to Dawntrail"u8, - "Try using this if the material can not be loaded or should use legacy shaders.\n\nThis is not revertible."u8, - new Vector2(-0.1f, 0), false, 0, Colors.PressEnterWarningBg)) - return; - - tab.Mtrl.MigrateToDawntrail(); - _materialTab.SaveFile(); - } - - private static void DrawMaterialLivePreviewRebind(MtrlTab tab, bool disabled) - { - if (disabled) - return; - - if (ImGui.Button("Reload live preview")) - tab.BindToMaterialInstances(); - - if (tab.MaterialPreviewers.Count != 0 || tab.ColorTablePreviewers.Count != 0) - return; - - ImGui.SameLine(); - using var c = ImRaii.PushColor(ImGuiCol.Text, Colors.RegexWarningBorder); - ImGui.TextUnformatted( - "The current material has not been found on your character. Please check the Import from Screen tab for more information."); - } - - private static bool DrawMaterialTextureChange(MtrlTab tab, bool disabled) - { - if (tab.Textures.Count == 0) - return false; - - ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - if (!ImGui.CollapsingHeader("Textures and Samplers", ImGuiTreeNodeFlags.DefaultOpen)) - return false; - - var frameHeight = ImGui.GetFrameHeight(); - var ret = false; - using var table = ImRaii.Table("##Textures", 3); - - ImGui.TableSetupColumn(string.Empty, ImGuiTableColumnFlags.WidthFixed, frameHeight); - ImGui.TableSetupColumn("Path", ImGuiTableColumnFlags.WidthStretch); - ImGui.TableSetupColumn("Name", ImGuiTableColumnFlags.WidthFixed, tab.TextureLabelWidth * UiHelpers.Scale); - foreach (var (label, textureI, samplerI, description, monoFont) in tab.Textures) - { - using var _ = ImRaii.PushId(samplerI); - var tmp = tab.Mtrl.Textures[textureI].Path; - var unfolded = tab.UnfoldedTextures.Contains(samplerI); - ImGui.TableNextColumn(); - if (ImGuiUtil.DrawDisabledButton((unfolded ? FontAwesomeIcon.CaretDown : FontAwesomeIcon.CaretRight).ToIconString(), - new Vector2(frameHeight), - "Settings for this texture and the associated sampler", false, true)) - { - unfolded = !unfolded; - if (unfolded) - tab.UnfoldedTextures.Add(samplerI); - else - tab.UnfoldedTextures.Remove(samplerI); - } - - ImGui.TableNextColumn(); - ImGui.SetNextItemWidth(ImGui.GetContentRegionAvail().X); - if (ImGui.InputText(string.Empty, ref tmp, Utf8GamePath.MaxGamePathLength, - disabled ? ImGuiInputTextFlags.ReadOnly : ImGuiInputTextFlags.None) - && tmp.Length > 0 - && tmp != tab.Mtrl.Textures[textureI].Path) - { - ret = true; - tab.Mtrl.Textures[textureI].Path = tmp; - } - - ImGui.TableNextColumn(); - using (ImRaii.PushFont(UiBuilder.MonoFont, monoFont)) - { - ImGui.AlignTextToFramePadding(); - if (description.Length > 0) - ImGuiUtil.LabeledHelpMarker(label, description); - else - ImGui.TextUnformatted(label); - } - - if (unfolded) - { - ImGui.TableNextColumn(); - ImGui.TableNextColumn(); - ret |= DrawMaterialSampler(tab, disabled, textureI, samplerI); - ImGui.TableNextColumn(); - } - } - - return ret; - } - - private static bool DrawBackFaceAndTransparency(MtrlTab tab, bool disabled) - { - const uint transparencyBit = 0x10; - const uint backfaceBit = 0x01; - - var ret = false; - - using var dis = ImRaii.Disabled(disabled); - - var tmp = (tab.Mtrl.ShaderPackage.Flags & transparencyBit) != 0; - if (ImGui.Checkbox("Enable Transparency", ref tmp)) - { - tab.Mtrl.ShaderPackage.Flags = - tmp ? tab.Mtrl.ShaderPackage.Flags | transparencyBit : tab.Mtrl.ShaderPackage.Flags & ~transparencyBit; - ret = true; - tab.SetShaderPackageFlags(tab.Mtrl.ShaderPackage.Flags); - } - - ImGui.SameLine(200 * UiHelpers.Scale + ImGui.GetStyle().ItemSpacing.X + ImGui.GetStyle().WindowPadding.X); - tmp = (tab.Mtrl.ShaderPackage.Flags & backfaceBit) != 0; - if (ImGui.Checkbox("Hide Backfaces", ref tmp)) - { - tab.Mtrl.ShaderPackage.Flags = tmp ? tab.Mtrl.ShaderPackage.Flags | backfaceBit : tab.Mtrl.ShaderPackage.Flags & ~backfaceBit; - ret = true; - tab.SetShaderPackageFlags(tab.Mtrl.ShaderPackage.Flags); - } - - return ret; - } - - private static void DrawOtherMaterialDetails(MtrlFile file, bool _) - { - if (!ImGui.CollapsingHeader("Further Content")) - return; - - using (var sets = ImRaii.TreeNode("UV Sets", ImGuiTreeNodeFlags.DefaultOpen)) - { - if (sets) - foreach (var set in file.UvSets) - ImRaii.TreeNode($"#{set.Index:D2} - {set.Name}", ImGuiTreeNodeFlags.Leaf).Dispose(); - } - - using (var sets = ImRaii.TreeNode("Color Sets", ImGuiTreeNodeFlags.DefaultOpen)) - { - if (sets) - foreach (var set in file.ColorSets) - ImRaii.TreeNode($"#{set.Index:D2} - {set.Name}", ImGuiTreeNodeFlags.Leaf).Dispose(); - } - - if (file.AdditionalData.Length <= 0) - return; - - using var t = ImRaii.TreeNode($"Additional Data (Size: {file.AdditionalData.Length})###AdditionalData"); - if (t) - Widget.DrawHexViewer(file.AdditionalData); + return tab.DrawPanel(disabled); } private void DrawMaterialReassignmentTab() diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs index 55b7e748..6fb223df 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.QuickImport.cs @@ -15,6 +15,7 @@ namespace Penumbra.UI.AdvancedWindow; public partial class ModEditWindow { + private readonly FileDialogService _fileDialog; private readonly ResourceTreeFactory _resourceTreeFactory; private readonly ResourceTreeViewer _quickImportViewer; private readonly Dictionary _quickImportWritables = new(); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index 0d3dce8c..f28cb632 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -13,10 +13,8 @@ using Penumbra.Collections.Manager; using Penumbra.Communication; using Penumbra.GameData.Enums; using Penumbra.GameData.Files; -using Penumbra.GameData.Interop; using Penumbra.Import.Models; using Penumbra.Import.Textures; -using Penumbra.Interop.Hooks.Objects; using Penumbra.Interop.ResourceTree; using Penumbra.Meta; using Penumbra.Mods; @@ -26,6 +24,7 @@ using Penumbra.Mods.SubMods; using Penumbra.Services; using Penumbra.String; using Penumbra.String.Classes; +using Penumbra.UI.AdvancedWindow.Materials; using Penumbra.UI.AdvancedWindow.Meta; using Penumbra.UI.Classes; using Penumbra.Util; @@ -39,20 +38,17 @@ public partial class ModEditWindow : Window, IDisposable, IUiService public readonly MigrationManager MigrationManager; - private readonly PerformanceTracker _performance; - private readonly ModEditor _editor; - private readonly Configuration _config; - private readonly ItemSwapTab _itemSwapTab; - private readonly MetaFileManager _metaFileManager; - private readonly ActiveCollections _activeCollections; - private readonly StainService _stainService; - private readonly ModMergeTab _modMergeTab; - private readonly CommunicatorService _communicator; - private readonly IDragDropManager _dragDropManager; - private readonly IDataManager _gameData; - private readonly IFramework _framework; - private readonly ObjectManager _objects; - private readonly CharacterBaseDestructor _characterBaseDestructor; + private readonly PerformanceTracker _performance; + private readonly ModEditor _editor; + private readonly Configuration _config; + private readonly ItemSwapTab _itemSwapTab; + private readonly MetaFileManager _metaFileManager; + private readonly ActiveCollections _activeCollections; + private readonly ModMergeTab _modMergeTab; + private readonly CommunicatorService _communicator; + private readonly IDragDropManager _dragDropManager; + private readonly IDataManager _gameData; + private readonly IFramework _framework; private Vector2 _iconSize = Vector2.Zero; private bool _allowReduplicate; @@ -541,7 +537,7 @@ public partial class ModEditWindow : Window, IDisposable, IUiService /// If none exists, goes through all options in the currently selected mod (if any) in order of priority and resolves in them. /// If no redirection is found in either of those options, returns the original path. /// - private FullPath FindBestMatch(Utf8GamePath path) + internal FullPath FindBestMatch(Utf8GamePath path) { var currentFile = _activeCollections.Current.ResolvePath(path); if (currentFile != null) @@ -562,7 +558,7 @@ public partial class ModEditWindow : Window, IDisposable, IUiService return new FullPath(path); } - private HashSet FindPathsStartingWith(CiByteString prefix) + internal HashSet FindPathsStartingWith(CiByteString prefix) { var ret = new HashSet(); @@ -587,34 +583,32 @@ public partial class ModEditWindow : Window, IDisposable, IUiService public ModEditWindow(PerformanceTracker performance, FileDialogService fileDialog, ItemSwapTab itemSwapTab, IDataManager gameData, Configuration config, ModEditor editor, ResourceTreeFactory resourceTreeFactory, MetaFileManager metaFileManager, - StainService stainService, ActiveCollections activeCollections, ModMergeTab modMergeTab, + ActiveCollections activeCollections, ModMergeTab modMergeTab, CommunicatorService communicator, TextureManager textures, ModelManager models, IDragDropManager dragDropManager, - ResourceTreeViewerFactory resourceTreeViewerFactory, ObjectManager objects, IFramework framework, - CharacterBaseDestructor characterBaseDestructor, MetaDrawers metaDrawers, MigrationManager migrationManager) + ResourceTreeViewerFactory resourceTreeViewerFactory, IFramework framework, + MetaDrawers metaDrawers, MigrationManager migrationManager, + MtrlTabFactory mtrlTabFactory) : base(WindowBaseLabel) { - _performance = performance; - _itemSwapTab = itemSwapTab; - _gameData = gameData; - _config = config; - _editor = editor; - _metaFileManager = metaFileManager; - _stainService = stainService; - _activeCollections = activeCollections; - _modMergeTab = modMergeTab; - _communicator = communicator; - _dragDropManager = dragDropManager; - _textures = textures; - _models = models; - _fileDialog = fileDialog; - _objects = objects; - _framework = framework; - _characterBaseDestructor = characterBaseDestructor; - MigrationManager = migrationManager; - _metaDrawers = metaDrawers; + _performance = performance; + _itemSwapTab = itemSwapTab; + _gameData = gameData; + _config = config; + _editor = editor; + _metaFileManager = metaFileManager; + _activeCollections = activeCollections; + _modMergeTab = modMergeTab; + _communicator = communicator; + _dragDropManager = dragDropManager; + _textures = textures; + _models = models; + _fileDialog = fileDialog; + _framework = framework; + MigrationManager = migrationManager; + _metaDrawers = metaDrawers; _materialTab = new FileEditor(this, _communicator, gameData, config, _editor.Compactor, _fileDialog, "Materials", ".mtrl", () => PopulateIsOnPlayer(_editor.Files.Mtrl, ResourceType.Mtrl), DrawMaterialPanel, () => Mod?.ModPath.FullName ?? string.Empty, - (bytes, path, writable) => new MtrlTab(this, new MtrlFile(bytes), path, writable)); + (bytes, path, writable) => mtrlTabFactory.Create(this, new MtrlFile(bytes), path, writable)); _modelTab = new FileEditor(this, _communicator, gameData, config, _editor.Compactor, _fileDialog, "Models", ".mdl", () => PopulateIsOnPlayer(_editor.Files.Mdl, ResourceType.Mdl), DrawModelPanel, () => Mod?.ModPath.FullName ?? string.Empty, (bytes, path, _) => new MdlTab(this, bytes, path)); diff --git a/Penumbra/UI/Classes/Colors.cs b/Penumbra/UI/Classes/Colors.cs index 4d0c62af..d135e10c 100644 --- a/Penumbra/UI/Classes/Colors.cs +++ b/Penumbra/UI/Classes/Colors.cs @@ -24,6 +24,7 @@ public enum ColorId NoAssignment, SelectorPriority, InGameHighlight, + InGameHighlight2, ResTreeLocalPlayer, ResTreePlayer, ResTreeNetworked, @@ -70,7 +71,8 @@ public static class Colors ColorId.NoModsAssignment => ( 0x50000080, "'Use No Mods' Collection Assignment", "A collection assignment set to not use any mods at all."), ColorId.NoAssignment => ( 0x00000000, "Unassigned Collection Assignment", "A collection assignment that is not configured to any collection and thus just has no specific treatment."), ColorId.SelectorPriority => ( 0xFF808080, "Mod Selector Priority", "The priority displayed for non-zero priority mods in the mod selector."), - ColorId.InGameHighlight => ( 0xFFEBCF89, "In-Game Highlight", "An in-game element that has been highlighted for ease of editing."), + ColorId.InGameHighlight => ( 0xFFEBCF89, "In-Game Highlight (Primary)", "An in-game element that has been highlighted for ease of editing."), + ColorId.InGameHighlight2 => ( 0xFF446CC0, "In-Game Highlight (Secondary)", "Another in-game element that has been highlighted for ease of editing."), ColorId.ResTreeLocalPlayer => ( 0xFFFFE0A0, "On-Screen: You", "You and what you own (mount, minion, accessory, pets and so on), in the On-Screen tab." ), ColorId.ResTreePlayer => ( 0xFFC0FFC0, "On-Screen: Other Players", "Other players and what they own, in the On-Screen tab." ), ColorId.ResTreeNetworked => ( 0xFFFFFFFF, "On-Screen: Non-Players (Networked)", "Non-player entities handled by the game server, in the On-Screen tab." ), diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index ab47ce7c..41ca8d6e 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -327,6 +327,9 @@ public class SettingsTab : ITab, IUiService UiHelpers.DefaultLineSpace(); DrawModHandlingSettings(); + UiHelpers.DefaultLineSpace(); + + DrawModEditorSettings(); ImGui.NewLine(); } @@ -723,6 +726,15 @@ public class SettingsTab : ITab, IUiService "Set the default Penumbra mod folder to place newly imported mods into.\nLeave blank to import into Root."); } + + /// Draw all settings pertaining to advanced editing of mods. + private void DrawModEditorSettings() + { + Checkbox("Advanced Editing: Edit Raw Tile UV Transforms", + "Edit the raw matrix components of tile UV transforms, instead of having them decomposed into scale, rotation and shear.", + _config.EditRawTileTransforms, v => _config.EditRawTileTransforms = v); + } + #endregion /// Draw the entire Color subsection. From f4fe3605f003456a2ba7d1310c907b2d54d9547b Mon Sep 17 00:00:00 2001 From: Exter-N Date: Sat, 3 Aug 2024 17:42:32 +0200 Subject: [PATCH 383/433] DT material editor, new color tables --- .../Materials/MtrlTab.ColorTable.cs | 562 ++++++++++++++++++ .../Materials/MtrlTab.CommonColorTable.cs | 1 + 2 files changed, 563 insertions(+) create mode 100644 Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs new file mode 100644 index 00000000..dc87ec41 --- /dev/null +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs @@ -0,0 +1,562 @@ +using Dalamud.Interface; +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; +using OtterGui.Text; +using Penumbra.GameData.Files.MaterialStructs; +using Penumbra.GameData.Files.StainMapStructs; +using Penumbra.Services; + +namespace Penumbra.UI.AdvancedWindow.Materials; + +public partial class MtrlTab +{ + private const float ColorTableScalarSize = 65.0f; + + private int _colorTableSelectedPair = 0; + + private bool DrawColorTable(ColorTable table, ColorDyeTable? dyeTable, bool disabled) + { + DrawColorTablePairSelector(table, disabled); + return DrawColorTablePairEditor(table, dyeTable, disabled); + } + + private void DrawColorTablePairSelector(ColorTable table, bool disabled) + { + using var font = ImRaii.PushFont(UiBuilder.MonoFont); + using var alignment = ImRaii.PushStyle(ImGuiStyleVar.ButtonTextAlign, new Vector2(0, 0.5f)); + var style = ImGui.GetStyle(); + var itemSpacing = style.ItemSpacing.X; + var itemInnerSpacing = style.ItemInnerSpacing.X; + var framePadding = style.FramePadding; + var buttonWidth = (ImGui.GetContentRegionAvail().X - itemSpacing * 7.0f) * 0.125f; + var frameHeight = ImGui.GetFrameHeight(); + var highlighterSize = ImUtf8.CalcIconSize(FontAwesomeIcon.Crosshairs) + framePadding * 2.0f; + var spaceWidth = ImUtf8.CalcTextSize(" "u8).X; + var spacePadding = (int)MathF.Ceiling((highlighterSize.X + framePadding.X + itemInnerSpacing) / spaceWidth); + for (var i = 0; i < ColorTable.NumRows >> 1; i += 8) + { + for (var j = 0; j < 8; ++j) + { + var pairIndex = i + j; + using (var color = ImRaii.PushColor(ImGuiCol.Button, ImGui.GetColorU32(ImGuiCol.ButtonActive), pairIndex == _colorTableSelectedPair)) + { + if (ImUtf8.Button($"#{pairIndex + 1}".PadLeft(3 + spacePadding), new Vector2(buttonWidth, ImGui.GetFrameHeightWithSpacing() + frameHeight))) + _colorTableSelectedPair = pairIndex; + } + + var rcMin = ImGui.GetItemRectMin() + framePadding; + var rcMax = ImGui.GetItemRectMax() - framePadding; + CtBlendRect( + rcMin with { X = rcMax.X - frameHeight * 3 - itemInnerSpacing * 2 }, + rcMax with { X = rcMax.X - (frameHeight + itemInnerSpacing) * 2 }, + ImGuiUtil.ColorConvertFloat3ToU32(PseudoSqrtRgb((Vector3)table[pairIndex << 1].DiffuseColor)), + ImGuiUtil.ColorConvertFloat3ToU32(PseudoSqrtRgb((Vector3)table[(pairIndex << 1) | 1].DiffuseColor)) + ); + CtBlendRect( + rcMin with { X = rcMax.X - frameHeight * 2 - itemInnerSpacing }, + rcMax with { X = rcMax.X - frameHeight - itemInnerSpacing }, + ImGuiUtil.ColorConvertFloat3ToU32(PseudoSqrtRgb((Vector3)table[pairIndex << 1].SpecularColor)), + ImGuiUtil.ColorConvertFloat3ToU32(PseudoSqrtRgb((Vector3)table[(pairIndex << 1) | 1].SpecularColor)) + ); + CtBlendRect( + rcMin with { X = rcMax.X - frameHeight }, rcMax, + ImGuiUtil.ColorConvertFloat3ToU32(PseudoSqrtRgb((Vector3)table[pairIndex << 1].EmissiveColor)), + ImGuiUtil.ColorConvertFloat3ToU32(PseudoSqrtRgb((Vector3)table[(pairIndex << 1) | 1].EmissiveColor)) + ); + if (j < 7) + ImGui.SameLine(); + + var cursor = ImGui.GetCursorScreenPos(); + ImGui.SetCursorScreenPos(rcMin with { Y = float.Lerp(rcMin.Y, rcMax.Y, 0.5f) - highlighterSize.Y * 0.5f }); + font.Pop(); + ColorTableHighlightButton(pairIndex, disabled); + font.Push(UiBuilder.MonoFont); + ImGui.SetCursorScreenPos(cursor); + } + } + } + + private bool DrawColorTablePairEditor(ColorTable table, ColorDyeTable? dyeTable, bool disabled) + { + var retA = false; + var retB = false; + ref var rowA = ref table[_colorTableSelectedPair << 1]; + ref var rowB = ref table[(_colorTableSelectedPair << 1) | 1]; + var dyeA = dyeTable != null ? dyeTable[_colorTableSelectedPair << 1] : default; + var dyeB = dyeTable != null ? dyeTable[(_colorTableSelectedPair << 1) | 1] : default; + var previewDyeA = _stainService.GetStainCombo(dyeA.Channel).CurrentSelection.Key; + var previewDyeB = _stainService.GetStainCombo(dyeB.Channel).CurrentSelection.Key; + var dyePackA = _stainService.GudStmFile.GetValueOrNull(dyeA.Template, previewDyeA); + var dyePackB = _stainService.GudStmFile.GetValueOrNull(dyeB.Template, previewDyeB); + using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) + { + ColorTableCopyClipboardButton(_colorTableSelectedPair << 1); + ImUtf8.SameLineInner(); + retA |= ColorTablePasteFromClipboardButton(_colorTableSelectedPair << 1, disabled); + ImGui.SameLine(); + CenteredTextInRest($"Row {_colorTableSelectedPair + 1}A"); + columns.Next(); + ColorTableCopyClipboardButton((_colorTableSelectedPair << 1) | 1); + ImUtf8.SameLineInner(); + retB |= ColorTablePasteFromClipboardButton((_colorTableSelectedPair << 1) | 1, disabled); + ImGui.SameLine(); + CenteredTextInRest($"Row {_colorTableSelectedPair + 1}B"); + } + + DrawHeader(" Colors"u8); + using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) + { + using var dis = ImRaii.Disabled(disabled); + using (var id = ImUtf8.PushId("ColorsA"u8)) + retA |= DrawColors(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + columns.Next(); + using (var id = ImUtf8.PushId("ColorsB"u8)) + retB |= DrawColors(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + } + + DrawHeader(" Physical Parameters"u8); + using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) + { + using var dis = ImRaii.Disabled(disabled); + using (var id = ImUtf8.PushId("PbrA"u8)) + retA |= DrawPbr(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + columns.Next(); + using (var id = ImUtf8.PushId("PbrB"u8)) + retB |= DrawPbr(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + } + + DrawHeader(" Sheen Layer Parameters"u8); + using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) + { + using var dis = ImRaii.Disabled(disabled); + using (var id = ImUtf8.PushId("SheenA"u8)) + retA |= DrawSheen(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + columns.Next(); + using (var id = ImUtf8.PushId("SheenB"u8)) + retB |= DrawSheen(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + } + + DrawHeader(" Pair Blending"u8); + using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) + { + using var dis = ImRaii.Disabled(disabled); + using (var id = ImUtf8.PushId("BlendingA"u8)) + retA |= DrawBlending(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + columns.Next(); + using (var id = ImUtf8.PushId("BlendingB"u8)) + retB |= DrawBlending(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + } + + DrawHeader(" Material Template"u8); + using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) + { + using var dis = ImRaii.Disabled(disabled); + using (var id = ImUtf8.PushId("TemplateA"u8)) + retA |= DrawTemplate(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + columns.Next(); + using (var id = ImUtf8.PushId("TemplateB"u8)) + retB |= DrawTemplate(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + } + + if (dyeTable != null) + { + DrawHeader(" Dye Properties"u8); + using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) + { + using var dis = ImRaii.Disabled(disabled); + using (var id = ImUtf8.PushId("DyeA"u8)) + retA |= DrawDye(dyeTable, dyePackA, _colorTableSelectedPair << 1); + columns.Next(); + using (var id = ImUtf8.PushId("DyeB"u8)) + retB |= DrawDye(dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + } + } + + DrawHeader(" Further Content"u8); + using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) + { + using var dis = ImRaii.Disabled(disabled); + using (var id = ImUtf8.PushId("FurtherA"u8)) + retA |= DrawFurther(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + columns.Next(); + using (var id = ImUtf8.PushId("FurtherB"u8)) + retB |= DrawFurther(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + } + + if (retA) + UpdateColorTableRowPreview(_colorTableSelectedPair << 1); + if (retB) + UpdateColorTableRowPreview((_colorTableSelectedPair << 1) | 1); + + return retA | retB; + } + + /// Padding styles do not seem to apply to this component. It is recommended to prepend two spaces. + private static void DrawHeader(ReadOnlySpan label) + { + var headerColor = ImGui.GetColorU32(ImGuiCol.Header); + using var _ = ImRaii.PushColor(ImGuiCol.HeaderHovered, headerColor).Push(ImGuiCol.HeaderActive, headerColor); + ImUtf8.CollapsingHeader(label, ImGuiTreeNodeFlags.Leaf); + } + + private static bool DrawColors(ColorTable table, ColorDyeTable? dyeTable, DyePack? dyePack, int rowIdx) + { + var dyeOffset = ImGui.GetContentRegionAvail().X + ImGui.GetStyle().ItemSpacing.X - ImGui.GetStyle().ItemInnerSpacing.X - ImGui.GetFrameHeight() * 2.0f; + + var ret = false; + ref var row = ref table[rowIdx]; + var dye = dyeTable != null ? dyeTable[rowIdx] : default; + + ret |= CtColorPicker("Diffuse Color"u8, default, row.DiffuseColor, + c => table[rowIdx].DiffuseColor = c); + if (dyeTable != null) + { + ImGui.SameLine(dyeOffset); + ret |= CtApplyStainCheckbox("##dyeDiffuseColor"u8, "Apply Diffuse Color on Dye"u8, dye.DiffuseColor, + b => dyeTable[rowIdx].DiffuseColor = b); + ImUtf8.SameLineInner(); + CtColorPicker("##dyePreviewDiffuseColor"u8, "Dye Preview for Diffuse Color"u8, dyePack?.DiffuseColor); + } + + ret |= CtColorPicker("Specular Color"u8, default, row.SpecularColor, + c => table[rowIdx].SpecularColor = c); + if (dyeTable != null) + { + ImGui.SameLine(dyeOffset); + ret |= CtApplyStainCheckbox("##dyeSpecularColor"u8, "Apply Specular Color on Dye"u8, dye.SpecularColor, + b => dyeTable[rowIdx].SpecularColor = b); + ImUtf8.SameLineInner(); + CtColorPicker("##dyePreviewSpecularColor"u8, "Dye Preview for Specular Color"u8, dyePack?.SpecularColor); + } + + ret |= CtColorPicker("Emissive Color"u8, default, row.EmissiveColor, + c => table[rowIdx].EmissiveColor = c); + if (dyeTable != null) + { + ImGui.SameLine(dyeOffset); + ret |= CtApplyStainCheckbox("##dyeEmissiveColor"u8, "Apply Emissive Color on Dye"u8, dye.EmissiveColor, + b => dyeTable[rowIdx].EmissiveColor = b); + ImUtf8.SameLineInner(); + CtColorPicker("##dyePreviewEmissiveColor"u8, "Dye Preview for Emissive Color"u8, dyePack?.EmissiveColor); + } + + return ret; + } + + private static bool DrawBlending(ColorTable table, ColorDyeTable? dyeTable, DyePack? dyePack, int rowIdx) + { + var scalarSize = ColorTableScalarSize * UiHelpers.Scale; + var dyeOffset = ImGui.GetContentRegionAvail().X + ImGui.GetStyle().ItemSpacing.X - ImGui.GetStyle().ItemInnerSpacing.X - ImGui.GetFrameHeight() - scalarSize; + + var isRowB = (rowIdx & 1) != 0; + + var ret = false; + ref var row = ref table[rowIdx]; + var dye = dyeTable != null ? dyeTable[rowIdx] : default; + + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragHalf(isRowB ? "Field #19"u8 : "Anisotropy Degree"u8, default, row.Anisotropy, "%.2f"u8, 0.0f, HalfMaxValue, 0.1f, + v => table[rowIdx].Anisotropy = v); + if (dyeTable != null) + { + ImGui.SameLine(dyeOffset); + ret |= CtApplyStainCheckbox("##dyeAnisotropy"u8, isRowB ? "Apply Field #19 on Dye"u8 : "Apply Anisotropy Degree on Dye"u8, dye.Anisotropy, + b => dyeTable[rowIdx].Anisotropy = b); + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(scalarSize); + CtDragHalf("##dyePreviewAnisotropy"u8, isRowB ? "Dye Preview for Field #19"u8 : "Dye Preview for Anisotropy Degree"u8, dyePack?.Anisotropy, "%.2f"u8); + } + + return ret; + } + + private bool DrawTemplate(ColorTable table, ColorDyeTable? dyeTable, DyePack? dyePack, int rowIdx) + { + var scalarSize = ColorTableScalarSize * UiHelpers.Scale; + var itemSpacing = ImGui.GetStyle().ItemSpacing.X; + var dyeOffset = ImGui.GetContentRegionAvail().X - ImGui.GetStyle().ItemInnerSpacing.X - ImGui.GetFrameHeight() - scalarSize - 64.0f; + var subcolWidth = CalculateSubcolumnWidth(2); + + var ret = false; + ref var row = ref table[rowIdx]; + var dye = dyeTable != null ? dyeTable[rowIdx] : default; + + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragScalar("Shader ID"u8, default, row.ShaderId, "%d"u8, (ushort)0, (ushort)255, 0.25f, + v => table[rowIdx].ShaderId = v); + + ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); + + ImGui.SetNextItemWidth(scalarSize + itemSpacing + 64.0f); + ret |= CtSphereMapIndexPicker("###SphereMapIndex"u8, default, row.SphereMapIndex, false, + v => table[rowIdx].SphereMapIndex = v); + ImUtf8.SameLineInner(); + ImUtf8.Text("Sphere Map"u8); + if (dyeTable != null) + { + var textRectMin = ImGui.GetItemRectMin(); + var textRectMax = ImGui.GetItemRectMax(); + ImGui.SameLine(dyeOffset); + var cursor = ImGui.GetCursorScreenPos(); + ImGui.SetCursorScreenPos(cursor with { Y = float.Lerp(textRectMin.Y, textRectMax.Y, 0.5f) - ImGui.GetFrameHeight() * 0.5f }); + ret |= CtApplyStainCheckbox("##dyeSphereMapIndex"u8, "Apply Sphere Map on Dye"u8, dye.SphereMapIndex, + b => dyeTable[rowIdx].SphereMapIndex = b); + ImUtf8.SameLineInner(); + ImGui.SetCursorScreenPos(ImGui.GetCursorScreenPos() with { Y = cursor.Y }); + ImGui.SetNextItemWidth(scalarSize + itemSpacing + 64.0f); + using var dis = ImRaii.Disabled(); + CtSphereMapIndexPicker("###SphereMapIndexDye"u8, "Dye Preview for Sphere Map"u8, dyePack?.SphereMapIndex ?? ushort.MaxValue, false, Nop); + } + + ImGui.Dummy(new Vector2(64.0f, 0.0f)); + ImGui.SameLine(); + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragScalar("Sphere Map Intensity"u8, default, (float)row.SphereMapMask * 100.0f, "%.0f%%"u8, HalfMinValue * 100.0f, HalfMaxValue * 100.0f, 1.0f, + v => table[rowIdx].SphereMapMask = (Half)(v * 0.01f)); + if (dyeTable != null) + { + ImGui.SameLine(dyeOffset); + ret |= CtApplyStainCheckbox("##dyeSphereMapMask"u8, "Apply Sphere Map Intensity on Dye"u8, dye.SphereMapMask, + b => dyeTable[rowIdx].SphereMapMask = b); + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(scalarSize); + CtDragScalar("##dyeSphereMapMask"u8, "Dye Preview for Sphere Map Intensity"u8, (float?)dyePack?.SphereMapMask * 100.0f, "%.0f%%"u8); + } + + ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); + + var leftLineHeight = 64.0f + ImGui.GetStyle().FramePadding.Y * 2.0f; + var rightLineHeight = 3.0f * ImGui.GetFrameHeight() + 2.0f * ImGui.GetStyle().ItemSpacing.Y; + var lineHeight = Math.Max(leftLineHeight, rightLineHeight); + var cursorPos = ImGui.GetCursorScreenPos(); + ImGui.SetCursorScreenPos(cursorPos + new Vector2(0.0f, (lineHeight - leftLineHeight) * 0.5f)); + ImGui.SetNextItemWidth(scalarSize + (itemSpacing + 64.0f) * 2.0f); + ret |= CtTileIndexPicker("###TileIndex"u8, default, row.TileIndex, false, + v => table[rowIdx].TileIndex = v); + ImUtf8.SameLineInner(); + ImUtf8.Text("Tile"u8); + + ImGui.SameLine(subcolWidth); + ImGui.SetCursorScreenPos(ImGui.GetCursorScreenPos() with { Y = cursorPos.Y + (lineHeight - rightLineHeight) * 0.5f, }); + using (var cld = ImUtf8.Child("###TileProperties"u8, new(ImGui.GetContentRegionAvail().X, float.Lerp(rightLineHeight, lineHeight, 0.5f)), false)) + { + ImGui.Dummy(new Vector2(scalarSize, 0.0f)); + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragScalar("Tile Opacity"u8, default, (float)row.TileAlpha * 100.0f, "%.0f%%"u8, 0.0f, HalfMaxValue * 100.0f, 1.0f, + v => table[rowIdx].TileAlpha = (Half)(v * 0.01f)); + + ret |= CtTileTransformMatrix(row.TileTransform, scalarSize, true, + m => table[rowIdx].TileTransform = m); + ImUtf8.SameLineInner(); + ImGui.SetCursorScreenPos(ImGui.GetCursorScreenPos() - new Vector2(0.0f, (ImGui.GetFrameHeight() + ImGui.GetStyle().ItemSpacing.Y) * 0.5f)); + ImUtf8.Text("Tile Transform"u8); + } + + return ret; + } + + private static bool DrawPbr(ColorTable table, ColorDyeTable? dyeTable, DyePack? dyePack, int rowIdx) + { + var scalarSize = ColorTableScalarSize * UiHelpers.Scale; + var subcolWidth = CalculateSubcolumnWidth(2) + ImGui.GetStyle().ItemSpacing.X; + var dyeOffset = subcolWidth - ImGui.GetStyle().ItemSpacing.X * 2.0f - ImGui.GetStyle().ItemInnerSpacing.X - ImGui.GetFrameHeight() - scalarSize; + + var ret = false; + ref var row = ref table[rowIdx]; + var dye = dyeTable != null ? dyeTable[rowIdx] : default; + + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragScalar("Roughness"u8, default, (float)row.Roughness * 100.0f, "%.0f%%"u8, HalfMinValue * 100.0f, HalfMaxValue * 100.0f, 1.0f, + v => table[rowIdx].Roughness = (Half)(v * 0.01f)); + if (dyeTable != null) + { + ImGui.SameLine(dyeOffset); + ret |= CtApplyStainCheckbox("##dyeRoughness"u8, "Apply Roughness on Dye"u8, dye.Roughness, + b => dyeTable[rowIdx].Roughness = b); + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(scalarSize); + CtDragScalar("##dyePreviewRoughness"u8, "Dye Preview for Roughness"u8, (float?)dyePack?.Roughness * 100.0f, "%.0f%%"u8); + } + + ImGui.SameLine(subcolWidth); + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragScalar("Metalness"u8, default, (float)row.Metalness * 100.0f, "%.0f%%"u8, HalfMinValue * 100.0f, HalfMaxValue * 100.0f, 1.0f, + v => table[rowIdx].Metalness = (Half)(v * 0.01f)); + if (dyeTable != null) + { + ImGui.SameLine(subcolWidth + dyeOffset); + ret |= CtApplyStainCheckbox("##dyeMetalness"u8, "Apply Metalness on Dye"u8, dye.Metalness, + b => dyeTable[rowIdx].Metalness = b); + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(scalarSize); + CtDragScalar("##dyePreviewMetalness"u8, "Dye Preview for Metalness"u8, (float?)dyePack?.Metalness * 100.0f, "%.0f%%"u8); + } + + return ret; + } + + private static bool DrawSheen(ColorTable table, ColorDyeTable? dyeTable, DyePack? dyePack, int rowIdx) + { + var scalarSize = ColorTableScalarSize * UiHelpers.Scale; + var subcolWidth = CalculateSubcolumnWidth(2) + ImGui.GetStyle().ItemSpacing.X; + var dyeOffset = subcolWidth - ImGui.GetStyle().ItemSpacing.X * 2.0f - ImGui.GetStyle().ItemInnerSpacing.X - ImGui.GetFrameHeight() - scalarSize; + + var ret = false; + ref var row = ref table[rowIdx]; + var dye = dyeTable != null ? dyeTable[rowIdx] : default; + + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragScalar("Sheen"u8, default, (float)row.SheenRate * 100.0f, "%.0f%%"u8, HalfMinValue * 100.0f, HalfMaxValue * 100.0f, 1.0f, + v => table[rowIdx].SheenRate = (Half)(v * 0.01f)); + if (dyeTable != null) + { + ImGui.SameLine(dyeOffset); + ret |= CtApplyStainCheckbox("##dyeSheenRate"u8, "Apply Sheen on Dye"u8, dye.SheenRate, + b => dyeTable[rowIdx].SheenRate = b); + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(scalarSize); + CtDragScalar("##dyePreviewSheenRate"u8, "Dye Preview for Sheen"u8, (float?)dyePack?.SheenRate * 100.0f, "%.0f%%"u8); + } + + ImGui.SameLine(subcolWidth); + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragScalar("Sheen Tint"u8, default, (float)row.SheenTintRate * 100.0f, "%.0f%%"u8, HalfMinValue * 100.0f, HalfMaxValue * 100.0f, 1.0f, + v => table[rowIdx].SheenTintRate = (Half)(v * 0.01f)); + if (dyeTable != null) + { + ImGui.SameLine(subcolWidth + dyeOffset); + ret |= CtApplyStainCheckbox("##dyeSheenTintRate"u8, "Apply Sheen Tint on Dye"u8, dye.SheenTintRate, + b => dyeTable[rowIdx].SheenTintRate = b); + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(scalarSize); + CtDragScalar("##dyePreviewSheenTintRate"u8, "Dye Preview for Sheen Tint"u8, (float?)dyePack?.SheenTintRate * 100.0f, "%.0f%%"u8); + } + + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragScalar("Sheen Roughness"u8, default, 100.0f / (float)row.SheenAperture, "%.0f%%"u8, 100.0f / HalfMaxValue, 100.0f / HalfEpsilon, 1.0f, + v => table[rowIdx].SheenAperture = (Half)(100.0f / v)); + if (dyeTable != null) + { + ImGui.SameLine(dyeOffset); + ret |= CtApplyStainCheckbox("##dyeSheenRoughness"u8, "Apply Sheen Roughness on Dye"u8, dye.SheenAperture, + b => dyeTable[rowIdx].SheenAperture = b); + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(scalarSize); + CtDragScalar("##dyePreviewSheenRoughness"u8, "Dye Preview for Sheen Roughness"u8, 100.0f / (float?)dyePack?.SheenAperture, "%.0f%%"u8); + } + + return ret; + } + + private static bool DrawFurther(ColorTable table, ColorDyeTable? dyeTable, DyePack? dyePack, int rowIdx) + { + var scalarSize = ColorTableScalarSize * UiHelpers.Scale; + var subcolWidth = CalculateSubcolumnWidth(2) + ImGui.GetStyle().ItemSpacing.X; + var dyeOffset = subcolWidth - ImGui.GetStyle().ItemSpacing.X * 2.0f - ImGui.GetStyle().ItemInnerSpacing.X - ImGui.GetFrameHeight() - scalarSize; + + var ret = false; + ref var row = ref table[rowIdx]; + var dye = dyeTable != null ? dyeTable[rowIdx] : default; + + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragHalf("Field #11"u8, default, row.Scalar11, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f, + v => table[rowIdx].Scalar11 = v); + if (dyeTable != null) + { + ImGui.SameLine(dyeOffset); + ret |= CtApplyStainCheckbox("##dyeScalar11"u8, "Apply Field #11 on Dye"u8, dye.Scalar3, + b => dyeTable[rowIdx].Scalar3 = b); + ImUtf8.SameLineInner(); + ImGui.SetNextItemWidth(scalarSize); + CtDragHalf("##dyePreviewScalar11"u8, "Dye Preview for Field #11"u8, dyePack?.Scalar3, "%.2f"u8); + } + + ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); + + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragHalf("Field #3"u8, default, row.Scalar3, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f, + v => table[rowIdx].Scalar3 = v); + + ImGui.SameLine(subcolWidth); + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragHalf("Field #7"u8, default, row.Scalar7, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f, + v => table[rowIdx].Scalar7 = v); + + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragHalf("Field #15"u8, default, row.Scalar15, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f, + v => table[rowIdx].Scalar15 = v); + + ImGui.SameLine(subcolWidth); + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragHalf("Field #17"u8, default, row.Scalar17, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f, + v => table[rowIdx].Scalar17 = v); + + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragHalf("Field #20"u8, default, row.Scalar20, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f, + v => table[rowIdx].Scalar20 = v); + + ImGui.SameLine(subcolWidth); + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragHalf("Field #22"u8, default, row.Scalar22, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f, + v => table[rowIdx].Scalar22 = v); + + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragHalf("Field #23"u8, default, row.Scalar23, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f, + v => table[rowIdx].Scalar23 = v); + + return ret; + } + + private bool DrawDye(ColorDyeTable dyeTable, DyePack? dyePack, int rowIdx) + { + var scalarSize = ColorTableScalarSize * UiHelpers.Scale; + var applyButtonWidth = ImUtf8.CalcTextSize("Apply Preview Dye"u8).X + ImGui.GetStyle().FramePadding.X * 2.0f; + var subcolWidth = CalculateSubcolumnWidth(2, applyButtonWidth); + + var ret = false; + ref var dye = ref dyeTable[rowIdx]; + + ImGui.SetNextItemWidth(scalarSize); + ret |= CtDragScalar("Dye Channel"u8, default, dye.Channel + 1, "%d"u8, 1, StainService.ChannelCount, 0.1f, + value => dyeTable[rowIdx].Channel = (byte)(Math.Clamp(value, 1, StainService.ChannelCount) - 1)); + ImGui.SameLine(subcolWidth); + ImGui.SetNextItemWidth(scalarSize); + if (_stainService.GudTemplateCombo.Draw("##dyeTemplate", dye.Template.ToString(), string.Empty, + scalarSize + ImGui.GetStyle().ScrollbarSize / 2, ImGui.GetTextLineHeightWithSpacing(), ImGuiComboFlags.NoArrowButton)) + { + dye.Template = _stainService.LegacyTemplateCombo.CurrentSelection; + ret = true; + } + ImUtf8.SameLineInner(); + ImUtf8.Text("Dye Template"u8); + ImGui.SameLine(ImGui.GetContentRegionAvail().X - applyButtonWidth + ImGui.GetStyle().ItemSpacing.X); + using var dis = ImRaii.Disabled(!dyePack.HasValue); + if (ImUtf8.Button("Apply Preview Dye"u8)) + { + ret |= Mtrl.ApplyDyeToRow(_stainService.GudStmFile, [ + _stainService.StainCombo1.CurrentSelection.Key, + _stainService.StainCombo2.CurrentSelection.Key, + ], rowIdx); + } + + return ret; + } + + private static void CenteredTextInRest(string text) + => AlignedTextInRest(text, 0.5f); + + private static void AlignedTextInRest(string text, float alignment) + { + var width = ImGui.CalcTextSize(text).X; + ImGui.SetCursorScreenPos(ImGui.GetCursorScreenPos() + new Vector2((ImGui.GetContentRegionAvail().X - width) * alignment, 0.0f)); + ImGui.TextUnformatted(text); + } + + private static float CalculateSubcolumnWidth(int numSubcolumns, float reservedSpace = 0.0f) + { + var itemSpacing = ImGui.GetStyle().ItemSpacing.X; + return (ImGui.GetContentRegionAvail().X - reservedSpace - itemSpacing * (numSubcolumns - 1)) / numSubcolumns + itemSpacing; + } +} diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs index 937614de..2b093e23 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs @@ -52,6 +52,7 @@ public partial class MtrlTab { LegacyColorTable legacyTable => DrawLegacyColorTable(legacyTable, Mtrl.DyeTable as LegacyColorDyeTable, disabled), ColorTable table when Mtrl.ShaderPackage.Name is "characterlegacy.shpk" => DrawLegacyColorTable(table, Mtrl.DyeTable as ColorDyeTable, disabled), + ColorTable table => DrawColorTable(table, Mtrl.DyeTable as ColorDyeTable, disabled), _ => false, }; From 5323add662874d3b0286f8ed35930b620fd99630 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Sat, 3 Aug 2024 17:45:52 +0200 Subject: [PATCH 384/433] Improve ShPk tab --- .../ModEditWindow.ShaderPackages.cs | 287 ++++++++++++++---- .../AdvancedWindow/ModEditWindow.ShpkTab.cs | 287 ++++++++++++++++-- Penumbra/UI/AdvancedWindow/ModEditWindow.cs | 2 +- 3 files changed, 494 insertions(+), 82 deletions(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs index 017478a7..8a1c729c 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs @@ -1,7 +1,6 @@ using Dalamud.Interface; using Dalamud.Interface.ImGuiNotification; using ImGuiNET; -using Lumina.Misc; using OtterGui.Raii; using OtterGui; using OtterGui.Classes; @@ -11,6 +10,9 @@ using Penumbra.GameData.Interop; using Penumbra.String; using static Penumbra.GameData.Files.ShpkFile; using OtterGui.Widgets; +using Penumbra.GameData.Files.ShaderStructs; +using OtterGui.Text; +using Penumbra.GameData.Structs; namespace Penumbra.UI.AdvancedWindow; @@ -24,6 +26,9 @@ public partial class ModEditWindow { DrawShaderPackageSummary(file); + ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); + DrawShaderPackageFilterSection(file); + var ret = false; ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); ret |= DrawShaderPackageShaderArray(file, "Vertex Shader", file.Shpk.VertexShaders, disabled); @@ -50,15 +55,16 @@ public partial class ModEditWindow private static void DrawShaderPackageSummary(ShpkTab tab) { + if (tab.Shpk.IsLegacy) + { + ImUtf8.Text("This legacy shader package will not work in the current version of the game. Do not attempt to load it.", + ImGuiUtil.HalfBlendText(0x80u)); // Half red + } ImGui.TextUnformatted(tab.Header); if (!tab.Shpk.Disassembled) { - var textColor = ImGui.GetColorU32(ImGuiCol.Text); - var textColorWarning = (textColor & 0xFF000000u) | ((textColor & 0x00FEFEFE) >> 1) | 0x80u; // Half red - - using var c = ImRaii.PushColor(ImGuiCol.Text, textColorWarning); - - ImGui.TextUnformatted("Your system doesn't support disassembling shaders. Some functionality will be missing."); + ImUtf8.Text("Your system doesn't support disassembling shaders. Some functionality will be missing.", + ImGuiUtil.HalfBlendText(0x80u)); // Half red } } @@ -123,6 +129,7 @@ public partial class ModEditWindow { shaders[idx].UpdateResources(tab.Shpk); tab.Shpk.UpdateResources(); + tab.UpdateFilteredUsed(); } catch (Exception e) { @@ -149,6 +156,97 @@ public partial class ModEditWindow ImGuiInputTextFlags.ReadOnly, null, null); } + private static void DrawShaderUsage(ShpkTab tab, Shader shader) + { + using (var node = ImUtf8.TreeNode("Used with Shader Keys"u8)) + { + if (node) + { + foreach (var (key, keyIdx) in shader.SystemValues!.WithIndex()) + { + ImRaii.TreeNode($"Used with System Key {tab.TryResolveName(tab.Shpk.SystemKeys[keyIdx].Id)} \u2208 {{ {tab.NameSetToString(key)} }}", + ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + } + + foreach (var (key, keyIdx) in shader.SceneValues!.WithIndex()) + { + ImRaii.TreeNode($"Used with Scene Key {tab.TryResolveName(tab.Shpk.SceneKeys[keyIdx].Id)} \u2208 {{ {tab.NameSetToString(key)} }}", + ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + } + + foreach (var (key, keyIdx) in shader.MaterialValues!.WithIndex()) + { + ImRaii.TreeNode($"Used with Material Key {tab.TryResolveName(tab.Shpk.MaterialKeys[keyIdx].Id)} \u2208 {{ {tab.NameSetToString(key)} }}", + ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + } + + foreach (var (key, keyIdx) in shader.SubViewValues!.WithIndex()) + { + ImRaii.TreeNode($"Used with Sub-View Key #{keyIdx} \u2208 {{ {tab.NameSetToString(key)} }}", + ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + } + } + } + + ImRaii.TreeNode($"Used in Passes: {tab.NameSetToString(shader.Passes)}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + } + + private static void DrawShaderPackageFilterSection(ShpkTab tab) + { + if (!ImUtf8.CollapsingHeader(tab.FilterPopCount == tab.FilterMaximumPopCount ? "Filters###Filters"u8 : "Filters (ACTIVE)###Filters"u8)) + return; + + foreach (var (key, keyIdx) in tab.Shpk.SystemKeys.WithIndex()) + DrawShaderPackageFilterSet(tab, $"System Key {tab.TryResolveName(key.Id)}", ref tab.FilterSystemValues[keyIdx]); + + foreach (var (key, keyIdx) in tab.Shpk.SceneKeys.WithIndex()) + DrawShaderPackageFilterSet(tab, $"Scene Key {tab.TryResolveName(key.Id)}", ref tab.FilterSceneValues[keyIdx]); + + foreach (var (key, keyIdx) in tab.Shpk.MaterialKeys.WithIndex()) + DrawShaderPackageFilterSet(tab, $"Material Key {tab.TryResolveName(key.Id)}", ref tab.FilterMaterialValues[keyIdx]); + + foreach (var (_, keyIdx) in tab.Shpk.SubViewKeys.WithIndex()) + DrawShaderPackageFilterSet(tab, $"Sub-View Key #{keyIdx}", ref tab.FilterSubViewValues[keyIdx]); + + DrawShaderPackageFilterSet(tab, "Passes", ref tab.FilterPasses); + } + + private static void DrawShaderPackageFilterSet(ShpkTab tab, string label, ref SharedSet values) + { + if (values.PossibleValues == null) + { + ImRaii.TreeNode(label, ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + return; + } + + using var node = ImRaii.TreeNode(label); + if (!node) + return; + + foreach (var value in values.PossibleValues) + { + var contains = values.Contains(value); + if (!ImGui.Checkbox($"{tab.TryResolveName(value)}", ref contains)) + continue; + if (contains) + { + if (values.AddExisting(value)) + { + ++tab.FilterPopCount; + tab.UpdateFilteredUsed(); + } + } + else + { + if (values.Remove(value)) + { + --tab.FilterPopCount; + tab.UpdateFilteredUsed(); + } + } + } + } + private static bool DrawShaderPackageShaderArray(ShpkTab tab, string objectName, Shader[] shaders, bool disabled) { if (shaders.Length == 0 || !ImGui.CollapsingHeader($"{objectName}s")) @@ -157,8 +255,11 @@ public partial class ModEditWindow var ret = false; for (var idx = 0; idx < shaders.Length; ++idx) { - var shader = shaders[idx]; - using var t = ImRaii.TreeNode($"{objectName} #{idx}"); + var shader = shaders[idx]; + if (!tab.IsFilterMatch(shader)) + continue; + + using var t = ImRaii.TreeNode($"{objectName} #{idx}"); if (!t) continue; @@ -169,9 +270,11 @@ public partial class ModEditWindow DrawShaderImportButton(tab, objectName, shaders, idx); } - ret |= DrawShaderPackageResourceArray("Constant Buffers", "slot", true, shader.Constants, true); - ret |= DrawShaderPackageResourceArray("Samplers", "slot", false, shader.Samplers, true); - ret |= DrawShaderPackageResourceArray("Unordered Access Views", "slot", true, shader.Uavs, true); + ret |= DrawShaderPackageResourceArray("Constant Buffers", "slot", true, shader.Constants, false, true); + ret |= DrawShaderPackageResourceArray("Samplers", "slot", false, shader.Samplers, false, true); + if (!tab.Shpk.IsLegacy) + ret |= DrawShaderPackageResourceArray("Textures", "slot", false, shader.Textures, false, true); + ret |= DrawShaderPackageResourceArray("Unordered Access Views", "slot", true, shader.Uavs, false, true); if (shader.DeclaredInputs != 0) ImRaii.TreeNode($"Declared Inputs: {shader.DeclaredInputs}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); @@ -187,12 +290,14 @@ public partial class ModEditWindow if (tab.Shpk.Disassembled) DrawRawDisassembly(shader); + + DrawShaderUsage(tab, shader); } return ret; } - private static bool DrawShaderPackageResource(string slotLabel, bool withSize, ref Resource resource, bool disabled) + private static bool DrawShaderPackageResource(string slotLabel, bool withSize, ref Resource resource, bool hasFilter, bool disabled) { var ret = false; if (!disabled) @@ -205,16 +310,26 @@ public partial class ModEditWindow if (resource.Used == null) return ret; - var usedString = UsedComponentString(withSize, resource); + var usedString = UsedComponentString(withSize, false, resource); if (usedString.Length > 0) - ImRaii.TreeNode($"Used: {usedString}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + { + ImRaii.TreeNode(hasFilter ? $"Globally Used: {usedString}" : $"Used: {usedString}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + if (hasFilter) + { + var filteredUsedString = UsedComponentString(withSize, true, resource); + if (filteredUsedString.Length > 0) + ImRaii.TreeNode($"Used within Filters: {filteredUsedString}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + else + ImRaii.TreeNode("Unused within Filters", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + } + } else - ImRaii.TreeNode("Unused", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + ImRaii.TreeNode(hasFilter ? "Globally Unused" : "Unused", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); return ret; } - private static bool DrawShaderPackageResourceArray(string arrayName, string slotLabel, bool withSize, Resource[] resources, bool disabled) + private static bool DrawShaderPackageResourceArray(string arrayName, string slotLabel, bool withSize, Resource[] resources, bool hasFilter, bool disabled) { if (resources.Length == 0) return false; @@ -233,7 +348,7 @@ public partial class ModEditWindow using var t2 = ImRaii.TreeNode(name, !disabled || buf.Used != null ? 0 : ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet); font.Dispose(); if (t2) - ret |= DrawShaderPackageResource(slotLabel, withSize, ref buf, disabled); + ret |= DrawShaderPackageResource(slotLabel, withSize, ref buf, hasFilter, disabled); } return ret; @@ -268,7 +383,7 @@ public partial class ModEditWindow private static bool DrawShaderPackageMaterialMatrix(ShpkTab tab, bool disabled) { ImGui.TextUnformatted(tab.Shpk.Disassembled - ? "Parameter positions (continuations are grayed out, unused values are red):" + ? "Parameter positions (continuations are grayed out, globally unused values are red, unused values within filters are yellow):" : "Parameter positions (continuations are grayed out):"); using var table = ImRaii.Table("##MaterialParamLayout", 5, @@ -276,17 +391,17 @@ public partial class ModEditWindow if (!table) return false; - ImGui.TableSetupColumn(string.Empty, ImGuiTableColumnFlags.WidthFixed, 25 * UiHelpers.Scale); - ImGui.TableSetupColumn("x", ImGuiTableColumnFlags.WidthFixed, 100 * UiHelpers.Scale); - ImGui.TableSetupColumn("y", ImGuiTableColumnFlags.WidthFixed, 100 * UiHelpers.Scale); - ImGui.TableSetupColumn("z", ImGuiTableColumnFlags.WidthFixed, 100 * UiHelpers.Scale); - ImGui.TableSetupColumn("w", ImGuiTableColumnFlags.WidthFixed, 100 * UiHelpers.Scale); + ImGui.TableSetupColumn(string.Empty, ImGuiTableColumnFlags.WidthFixed, 40 * UiHelpers.Scale); + ImGui.TableSetupColumn("x", ImGuiTableColumnFlags.WidthFixed, 250 * UiHelpers.Scale); + ImGui.TableSetupColumn("y", ImGuiTableColumnFlags.WidthFixed, 250 * UiHelpers.Scale); + ImGui.TableSetupColumn("z", ImGuiTableColumnFlags.WidthFixed, 250 * UiHelpers.Scale); + ImGui.TableSetupColumn("w", ImGuiTableColumnFlags.WidthFixed, 250 * UiHelpers.Scale); ImGui.TableHeadersRow(); var textColorStart = ImGui.GetColorU32(ImGuiCol.Text); - var textColorCont = (textColorStart & 0x00FFFFFFu) | ((textColorStart & 0xFE000000u) >> 1); // Half opacity - var textColorUnusedStart = (textColorStart & 0xFF000000u) | ((textColorStart & 0x00FEFEFE) >> 1) | 0x80u; // Half red - var textColorUnusedCont = (textColorUnusedStart & 0x00FFFFFFu) | ((textColorUnusedStart & 0xFE000000u) >> 1); + var textColorCont = ImGuiUtil.HalfTransparent(textColorStart); // Half opacity + var textColorUnusedStart = ImGuiUtil.HalfBlend(textColorStart, 0x80u); // Half red + var textColorUnusedCont = ImGuiUtil.HalfTransparent(textColorUnusedStart); var ret = false; for (var i = 0; i < tab.Matrix.GetLength(0); ++i) @@ -296,14 +411,13 @@ public partial class ModEditWindow for (var j = 0; j < 4; ++j) { var (name, tooltip, idx, colorType) = tab.Matrix[i, j]; - var color = colorType switch - { - ShpkTab.ColorType.Unused => textColorUnusedStart, - ShpkTab.ColorType.Used => textColorStart, - ShpkTab.ColorType.Continuation => textColorUnusedCont, - ShpkTab.ColorType.Continuation | ShpkTab.ColorType.Used => textColorCont, - _ => textColorStart, - }; + var color = textColorStart; + if (!colorType.HasFlag(ShpkTab.ColorType.Used)) + color = ImGuiUtil.HalfBlend(color, 0x80u); // Half red + else if (!colorType.HasFlag(ShpkTab.ColorType.FilteredUsed)) + color = ImGuiUtil.HalfBlend(color, 0x8080u); // Half yellow + if (colorType.HasFlag(ShpkTab.ColorType.Continuation)) + color = ImGuiUtil.HalfTransparent(color); // Half opacity using var _ = ImRaii.PushId(i * 4 + j); var deletable = !disabled && idx >= 0; using (var font = ImRaii.PushFont(UiBuilder.MonoFont, tooltip.Length > 0)) @@ -331,6 +445,35 @@ public partial class ModEditWindow return ret; } + private static void DrawShaderPackageMaterialDevkitExport(ShpkTab tab) + { + if (!ImUtf8.Button("Export globally unused parameters as material dev-kit file"u8)) + return; + + tab.FileDialog.OpenSavePicker("Export material dev-kit file", ".json", $"{Path.GetFileNameWithoutExtension(tab.FilePath)}.json", ".json", DoSave, null, false); + + void DoSave(bool success, string path) + { + if (!success) + return; + + try + { + File.WriteAllText(path, tab.ExportDevkit().ToString()); + } + catch (Exception e) + { + Penumbra.Messager.NotificationMessage(e, $"Could not export dev-kit for {Path.GetFileName(tab.FilePath)} to {path}.", + NotificationType.Error, false); + return; + } + + Penumbra.Messager.NotificationMessage( + $"Material dev-kit file for {Path.GetFileName(tab.FilePath)} exported successfully to {Path.GetFileName(path)}.", + NotificationType.Success, false); + } + } + private static void DrawShaderPackageMisalignedParameters(ShpkTab tab) { using var t = ImRaii.TreeNode("Misaligned / Overflowing Parameters"); @@ -396,23 +539,25 @@ public partial class ModEditWindow DrawShaderPackageEndCombo(tab); ImGui.SetNextItemWidth(UiHelpers.Scale * 400); - if (ImGui.InputText("Name", ref tab.NewMaterialParamName, 63)) - tab.NewMaterialParamId = Crc32.Get(tab.NewMaterialParamName, 0xFFFFFFFFu); + var newName = tab.NewMaterialParamName.Value!; + if (ImGui.InputText("Name", ref newName, 63)) + tab.NewMaterialParamName = newName; - var tooltip = tab.UsedIds.Contains(tab.NewMaterialParamId) + var tooltip = tab.UsedIds.Contains(tab.NewMaterialParamName.Crc32) ? "The ID is already in use. Please choose a different name." : string.Empty; - if (!ImGuiUtil.DrawDisabledButton($"Add ID 0x{tab.NewMaterialParamId:X8}", new Vector2(400 * UiHelpers.Scale, ImGui.GetFrameHeight()), + if (!ImGuiUtil.DrawDisabledButton($"Add {tab.NewMaterialParamName} (0x{tab.NewMaterialParamName.Crc32:X8})", new Vector2(400 * UiHelpers.Scale, ImGui.GetFrameHeight()), tooltip, tooltip.Length > 0)) return false; tab.Shpk.MaterialParams = tab.Shpk.MaterialParams.AddItem(new MaterialParam { - Id = tab.NewMaterialParamId, + Id = tab.NewMaterialParamName.Crc32, ByteOffset = (ushort)(tab.Orphans[tab.NewMaterialParamStart].Index << 2), ByteSize = (ushort)((tab.NewMaterialParamEnd - tab.NewMaterialParamStart + 1) << 2), }); + tab.AddNameToCache(tab.NewMaterialParamName); tab.Update(); return true; } @@ -434,6 +579,9 @@ public partial class ModEditWindow else if (!disabled && sizeWellDefined) ret |= DrawShaderPackageNewParameter(tab); + if (tab.Shpk.Disassembled) + DrawShaderPackageMaterialDevkitExport(tab); + return ret; } @@ -444,14 +592,17 @@ public partial class ModEditWindow if (!ImGui.CollapsingHeader("Shader Resources")) return false; - ret |= DrawShaderPackageResourceArray("Constant Buffers", "type", true, tab.Shpk.Constants, disabled); - ret |= DrawShaderPackageResourceArray("Samplers", "type", false, tab.Shpk.Samplers, disabled); - ret |= DrawShaderPackageResourceArray("Unordered Access Views", "type", false, tab.Shpk.Uavs, disabled); + var hasFilters = tab.FilterPopCount != tab.FilterMaximumPopCount; + ret |= DrawShaderPackageResourceArray("Constant Buffers", "type", true, tab.Shpk.Constants, hasFilters, disabled); + ret |= DrawShaderPackageResourceArray("Samplers", "type", false, tab.Shpk.Samplers, hasFilters, disabled); + if (!tab.Shpk.IsLegacy) + ret |= DrawShaderPackageResourceArray("Textures", "type", false, tab.Shpk.Textures, hasFilters, disabled); + ret |= DrawShaderPackageResourceArray("Unordered Access Views", "type", false, tab.Shpk.Uavs, hasFilters, disabled); return ret; } - private static void DrawKeyArray(string arrayName, bool withId, IReadOnlyCollection keys) + private static void DrawKeyArray(ShpkTab tab, string arrayName, bool withId, IReadOnlyCollection keys) { if (keys.Count == 0) return; @@ -463,12 +614,11 @@ public partial class ModEditWindow using var font = ImRaii.PushFont(UiBuilder.MonoFont); foreach (var (key, idx) in keys.WithIndex()) { - using var t2 = ImRaii.TreeNode(withId ? $"#{idx}: ID: 0x{key.Id:X8}" : $"#{idx}"); + using var t2 = ImRaii.TreeNode(withId ? $"#{idx}: {tab.TryResolveName(key.Id)} (0x{key.Id:X8})" : $"#{idx}"); if (t2) { - ImRaii.TreeNode($"Default Value: 0x{key.DefaultValue:X8}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); - ImRaii.TreeNode($"Known Values: {string.Join(", ", Array.ConvertAll(key.Values, value => $"0x{value:X8}"))}", - ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + ImRaii.TreeNode($"Default Value: {tab.TryResolveName(key.DefaultValue)} (0x{key.DefaultValue:X8})", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + ImRaii.TreeNode($"Known Values: {tab.NameSetToString(key.Values, true)}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); } } } @@ -482,39 +632,46 @@ public partial class ModEditWindow if (!t) return; + using var font = ImRaii.PushFont(UiBuilder.MonoFont); + foreach (var (node, idx) in tab.Shpk.Nodes.WithIndex()) { - using var font = ImRaii.PushFont(UiBuilder.MonoFont); - using var t2 = ImRaii.TreeNode($"#{idx:D4}: Selector: 0x{node.Selector:X8}"); + if (!tab.IsFilterMatch(node)) + continue; + + using var t2 = ImRaii.TreeNode($"#{idx:D4}: Selector: 0x{node.Selector:X8}"); if (!t2) continue; foreach (var (key, keyIdx) in node.SystemKeys.WithIndex()) { - ImRaii.TreeNode($"System Key 0x{tab.Shpk.SystemKeys[keyIdx].Id:X8} = 0x{key:X8}", + ImRaii.TreeNode($"System Key {tab.TryResolveName(tab.Shpk.SystemKeys[keyIdx].Id)} = {tab.TryResolveName(key)} / \u2208 {{ {tab.NameSetToString(node.SystemValues![keyIdx])} }}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); } foreach (var (key, keyIdx) in node.SceneKeys.WithIndex()) { - ImRaii.TreeNode($"Scene Key 0x{tab.Shpk.SceneKeys[keyIdx].Id:X8} = 0x{key:X8}", + ImRaii.TreeNode($"Scene Key {tab.TryResolveName(tab.Shpk.SceneKeys[keyIdx].Id)} = {tab.TryResolveName(key)} / \u2208 {{ {tab.NameSetToString(node.SceneValues![keyIdx])} }}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); } foreach (var (key, keyIdx) in node.MaterialKeys.WithIndex()) { - ImRaii.TreeNode($"Material Key 0x{tab.Shpk.MaterialKeys[keyIdx].Id:X8} = 0x{key:X8}", + ImRaii.TreeNode($"Material Key {tab.TryResolveName(tab.Shpk.MaterialKeys[keyIdx].Id)} = {tab.TryResolveName(key)} / \u2208 {{ {tab.NameSetToString(node.MaterialValues![keyIdx])} }}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); } foreach (var (key, keyIdx) in node.SubViewKeys.WithIndex()) - ImRaii.TreeNode($"Sub-View Key #{keyIdx} = 0x{key:X8}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + { + ImRaii.TreeNode($"Sub-View Key #{keyIdx} = {tab.TryResolveName(key)} / \u2208 {{ {tab.NameSetToString(node.SubViewValues![keyIdx])} }}", + ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + } ImRaii.TreeNode($"Pass Indices: {string.Join(' ', node.PassIndices.Select(c => $"{c:X2}"))}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); foreach (var (pass, passIdx) in node.Passes.WithIndex()) { - ImRaii.TreeNode($"Pass #{passIdx}: ID: 0x{pass.Id:X8}, Vertex Shader #{pass.VertexShader}, Pixel Shader #{pass.PixelShader}", + ImRaii.TreeNode($"Pass #{passIdx}: ID: {tab.TryResolveName(pass.Id)}, Vertex Shader #{pass.VertexShader}, Pixel Shader #{pass.PixelShader}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet) .Dispose(); } @@ -526,10 +683,10 @@ public partial class ModEditWindow if (!ImGui.CollapsingHeader("Shader Selection")) return; - DrawKeyArray("System Keys", true, tab.Shpk.SystemKeys); - DrawKeyArray("Scene Keys", true, tab.Shpk.SceneKeys); - DrawKeyArray("Material Keys", true, tab.Shpk.MaterialKeys); - DrawKeyArray("Sub-View Keys", false, tab.Shpk.SubViewKeys); + DrawKeyArray(tab, "System Keys", true, tab.Shpk.SystemKeys); + DrawKeyArray(tab, "Scene Keys", true, tab.Shpk.SceneKeys); + DrawKeyArray(tab, "Material Keys", true, tab.Shpk.MaterialKeys); + DrawKeyArray(tab, "Sub-View Keys", false, tab.Shpk.SubViewKeys); DrawShaderPackageNodes(tab); using var t = ImRaii.TreeNode($"Node Selectors ({tab.Shpk.NodeSelectors.Count})###NodeSelectors"); @@ -559,12 +716,14 @@ public partial class ModEditWindow } } - private static string UsedComponentString(bool withSize, in Resource resource) + private static string UsedComponentString(bool withSize, bool filtered, in Resource resource) { + var used = filtered ? resource.FilteredUsed : resource.Used; + var usedDynamically = filtered ? resource.FilteredUsedDynamically : resource.UsedDynamically; var sb = new StringBuilder(256); if (withSize) { - foreach (var (components, i) in (resource.Used ?? Array.Empty()).WithIndex()) + foreach (var (components, i) in (used ?? Array.Empty()).WithIndex()) { switch (components) { @@ -582,7 +741,7 @@ public partial class ModEditWindow } } - switch (resource.UsedDynamically ?? 0) + switch (usedDynamically ?? 0) { case 0: break; case DisassembledShader.VectorComponents.All: @@ -590,7 +749,7 @@ public partial class ModEditWindow break; default: sb.Append("[*]."); - foreach (var c in resource.UsedDynamically!.Value.ToString().Where(char.IsUpper)) + foreach (var c in usedDynamically!.Value.ToString().Where(char.IsUpper)) sb.Append(char.ToLower(c)); sb.Append(", "); @@ -599,7 +758,7 @@ public partial class ModEditWindow } else { - var components = (resource.Used is { Length: > 0 } ? resource.Used[0] : 0) | (resource.UsedDynamically ?? 0); + var components = (used is { Length: > 0 } ? used[0] : 0) | (usedDynamically ?? 0); if ((components & DisassembledShader.VectorComponents.X) != 0) sb.Append("Red, "); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.ShpkTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.ShpkTab.cs index 12b8d761..de20aa9f 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.ShpkTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.ShpkTab.cs @@ -1,9 +1,12 @@ using Dalamud.Utility; -using Lumina.Misc; +using Newtonsoft.Json.Linq; using OtterGui; -using Penumbra.GameData.Data; +using OtterGui.Classes; using Penumbra.GameData.Files; +using Penumbra.GameData.Files.ShaderStructs; using Penumbra.GameData.Interop; +using Penumbra.GameData.Structs; +using Penumbra.UI.AdvancedWindow.Materials; namespace Penumbra.UI.AdvancedWindow; @@ -12,18 +15,27 @@ public partial class ModEditWindow private class ShpkTab : IWritable { public readonly ShpkFile Shpk; + public readonly string FilePath; - public string NewMaterialParamName = string.Empty; - public uint NewMaterialParamId = Crc32.Get(string.Empty, 0xFFFFFFFFu); - public short NewMaterialParamStart; - public short NewMaterialParamEnd; + public Name NewMaterialParamName = string.Empty; + public short NewMaterialParamStart; + public short NewMaterialParamEnd; + + public SharedSet[] FilterSystemValues; + public SharedSet[] FilterSceneValues; + public SharedSet[] FilterMaterialValues; + public SharedSet[] FilterSubViewValues; + public SharedSet FilterPasses; + + public readonly int FilterMaximumPopCount; + public int FilterPopCount; public readonly FileDialogService FileDialog; public readonly string Header; public readonly string Extension; - public ShpkTab(FileDialogService fileDialog, byte[] bytes) + public ShpkTab(FileDialogService fileDialog, byte[] bytes, string filePath) { FileDialog = fileDialog; try @@ -34,6 +46,7 @@ public partial class ModEditWindow { Shpk = new ShpkFile(bytes, false); } + FilePath = filePath; Header = $"Shader Package for DirectX {(int)Shpk.DirectXVersion}"; Extension = Shpk.DirectXVersion switch @@ -42,15 +55,36 @@ public partial class ModEditWindow ShpkFile.DxVersion.DirectX11 => ".dxbc", _ => throw new NotImplementedException(), }; + + FilterSystemValues = Array.ConvertAll(Shpk.SystemKeys, key => key.Values.FullSet()); + FilterSceneValues = Array.ConvertAll(Shpk.SceneKeys, key => key.Values.FullSet()); + FilterMaterialValues = Array.ConvertAll(Shpk.MaterialKeys, key => key.Values.FullSet()); + FilterSubViewValues = Array.ConvertAll(Shpk.SubViewKeys, key => key.Values.FullSet()); + FilterPasses = Shpk.Passes.FullSet(); + + FilterMaximumPopCount = FilterPasses.Count; + foreach (var key in Shpk.SystemKeys) + FilterMaximumPopCount += key.Values.Count; + foreach (var key in Shpk.SceneKeys) + FilterMaximumPopCount += key.Values.Count; + foreach (var key in Shpk.MaterialKeys) + FilterMaximumPopCount += key.Values.Count; + foreach (var key in Shpk.SubViewKeys) + FilterMaximumPopCount += key.Values.Count; + + FilterPopCount = FilterMaximumPopCount; + + UpdateNameCache(); + Shpk.UpdateFilteredUsed(IsFilterMatch); Update(); } [Flags] public enum ColorType : byte { - Unused = 0, Used = 1, - Continuation = 2, + FilteredUsed = 2, + Continuation = 4, } public (string Name, string Tooltip, short Index, ColorType Color)[,] Matrix = null!; @@ -58,10 +92,87 @@ public partial class ModEditWindow public readonly HashSet UsedIds = new(16); public readonly List<(string Name, short Index)> Orphans = new(16); + private readonly Dictionary _nameCache = []; + private readonly Dictionary, string> _nameSetCache = []; + private readonly Dictionary, string> _nameSetWithIdsCache = []; + + public void AddNameToCache(Name name) + { + if (name.Value != null) + _nameCache.TryAdd(name.Crc32, name); + + _nameSetCache.Clear(); + _nameSetWithIdsCache.Clear(); + } + + public void UpdateNameCache() + { + static void CollectResourceNames(Dictionary nameCache, ShpkFile.Resource[] resources) + { + foreach (var resource in resources) + nameCache.TryAdd(resource.Id, resource.Name); + } + + static void CollectKeyNames(Dictionary nameCache, ShpkFile.Key[] keys) + { + foreach (var key in keys) + { + var keyName = nameCache.TryResolve(Names.KnownNames, key.Id); + var valueNames = keyName.WithKnownSuffixes(); + foreach (var value in key.Values) + { + var valueName = valueNames.TryResolve(value); + if (valueName.Value != null) + nameCache.TryAdd(value, valueName); + } + } + } + + CollectResourceNames(_nameCache, Shpk.Constants); + CollectResourceNames(_nameCache, Shpk.Samplers); + CollectResourceNames(_nameCache, Shpk.Textures); + CollectResourceNames(_nameCache, Shpk.Uavs); + + CollectKeyNames(_nameCache, Shpk.SystemKeys); + CollectKeyNames(_nameCache, Shpk.SceneKeys); + CollectKeyNames(_nameCache, Shpk.MaterialKeys); + CollectKeyNames(_nameCache, Shpk.SubViewKeys); + + _nameSetCache.Clear(); + _nameSetWithIdsCache.Clear(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Name TryResolveName(uint crc32) + => _nameCache.TryResolve(Names.KnownNames, crc32); + + public string NameSetToString(SharedSet nameSet, bool withIds = false) + { + var cache = withIds ? _nameSetWithIdsCache : _nameSetCache; + if (cache.TryGetValue(nameSet, out var nameSetStr)) + return nameSetStr; + if (withIds) + nameSetStr = string.Join(", ", nameSet.Select(id => $"{TryResolveName(id)} (0x{id:X8})")); + else + nameSetStr = string.Join(", ", nameSet.Select(TryResolveName)); + cache.Add(nameSet, nameSetStr); + return nameSetStr; + } + + public void UpdateFilteredUsed() + { + Shpk.UpdateFilteredUsed(IsFilterMatch); + + var materialParams = Shpk.GetConstantById(ShpkFile.MaterialParamsConstantId); + UpdateColors(materialParams); + } + public void Update() { var materialParams = Shpk.GetConstantById(ShpkFile.MaterialParamsConstantId); var numParameters = ((Shpk.MaterialParamsSize + 0xFu) & ~0xFu) >> 4; + var defaults = Shpk.MaterialParamsDefaults != null ? (ReadOnlySpan)Shpk.MaterialParamsDefaults : []; + var defaultFloats = MemoryMarshal.Cast(defaults); Matrix = new (string Name, string Tooltip, short Index, ColorType Color)[numParameters, 4]; MalformedParameters.Clear(); @@ -75,14 +186,14 @@ public partial class ModEditWindow var jEnd = ((param.ByteOffset + param.ByteSize - 1) >> 2) & 3; if ((param.ByteOffset & 0x3) != 0 || (param.ByteSize & 0x3) != 0) { - MalformedParameters.Add($"ID: 0x{param.Id:X8}, offset: 0x{param.ByteOffset:X4}, size: 0x{param.ByteSize:X4}"); + MalformedParameters.Add($"ID: {TryResolveName(param.Id)} (0x{param.Id:X8}), offset: 0x{param.ByteOffset:X4}, size: 0x{param.ByteSize:X4}"); continue; } if (iEnd >= numParameters) { MalformedParameters.Add( - $"{MaterialParamRangeName(materialParams?.Name ?? string.Empty, param.ByteOffset >> 2, param.ByteSize >> 2)} (ID: 0x{param.Id:X8})"); + $"{MtrlTab.MaterialParamRangeName(materialParams?.Name ?? string.Empty, param.ByteOffset >> 2, param.ByteSize >> 2)} ({TryResolveName(param.Id)}, 0x{param.Id:X8})"); continue; } @@ -91,9 +202,12 @@ public partial class ModEditWindow var end = i == iEnd ? jEnd : 3; for (var j = i == iStart ? jStart : 0; j <= end; ++j) { + var component = (i << 2) | j; var tt = - $"{MaterialParamRangeName(materialParams?.Name ?? string.Empty, param.ByteOffset >> 2, param.ByteSize >> 2).Item1} (ID: 0x{param.Id:X8})"; - Matrix[i, j] = ($"0x{param.Id:X8}", tt, (short)idx, 0); + $"{MtrlTab.MaterialParamRangeName(materialParams?.Name ?? string.Empty, param.ByteOffset >> 2, param.ByteSize >> 2).Item1} ({TryResolveName(param.Id)}, 0x{param.Id:X8})"; + if (component < defaultFloats.Length) + tt += $"\n\nDefault value: {defaultFloats[component]} ({defaults[component << 2]:X2} {defaults[(component << 2) | 1]:X2} {defaults[(component << 2) | 2]:X2} {defaults[(component << 2) | 3]:X2})"; + Matrix[i, j] = (TryResolveName(param.Id).ToString(), tt, (short)idx, 0); } } } @@ -151,7 +265,7 @@ public partial class ModEditWindow if (oldStart == linear) newMaterialParamStart = (short)Orphans.Count; - Orphans.Add(($"{materialParams?.Name ?? string.Empty}{MaterialParamName(false, linear)}", linear)); + Orphans.Add(($"{materialParams?.Name ?? ShpkFile.MaterialParamsConstantName}{MtrlTab.MaterialParamName(false, linear)}", linear)); } } @@ -168,11 +282,15 @@ public partial class ModEditWindow { var usedComponents = (materialParams?.Used?[i] ?? DisassembledShader.VectorComponents.All) | (materialParams?.UsedDynamically ?? 0); + var filteredUsedComponents = (materialParams?.FilteredUsed?[i] ?? DisassembledShader.VectorComponents.All) + | (materialParams?.FilteredUsedDynamically ?? 0); for (var j = 0; j < 4; ++j) { - var color = ((byte)usedComponents & (1 << j)) != 0 - ? ColorType.Used - : 0; + ColorType color = 0; + if (((byte)usedComponents & (1 << j)) != 0) + color |= ColorType.Used; + if (((byte)filteredUsedComponents & (1 << j)) != 0) + color |= ColorType.FilteredUsed; if (Matrix[i, j].Index == lastIndex || Matrix[i, j].Index < 0) color |= ColorType.Continuation; @@ -182,6 +300,141 @@ public partial class ModEditWindow } } + public bool IsFilterMatch(ShpkFile.Shader shader) + { + if (!FilterPasses.Overlaps(shader.Passes)) + return false; + + for (var i = 0; i < shader.SystemValues!.Length; ++i) + { + if (!FilterSystemValues[i].Overlaps(shader.SystemValues[i])) + return false; + } + + for (var i = 0; i < shader.SceneValues!.Length; ++i) + { + if (!FilterSceneValues[i].Overlaps(shader.SceneValues[i])) + return false; + } + + for (var i = 0; i < shader.MaterialValues!.Length; ++i) + { + if (!FilterMaterialValues[i].Overlaps(shader.MaterialValues[i])) + return false; + } + + for (var i = 0; i < shader.SubViewValues!.Length; ++i) + { + if (!FilterSubViewValues[i].Overlaps(shader.SubViewValues[i])) + return false; + } + + return true; + } + + public bool IsFilterMatch(ShpkFile.Node node) + { + if (!node.Passes.Any(pass => FilterPasses.Contains(pass.Id))) + return false; + + for (var i = 0; i < node.SystemValues!.Length; ++i) + { + if (!FilterSystemValues[i].Overlaps(node.SystemValues[i])) + return false; + } + + for (var i = 0; i < node.SceneValues!.Length; ++i) + { + if (!FilterSceneValues[i].Overlaps(node.SceneValues[i])) + return false; + } + + for (var i = 0; i < node.MaterialValues!.Length; ++i) + { + if (!FilterMaterialValues[i].Overlaps(node.MaterialValues[i])) + return false; + } + + for (var i = 0; i < node.SubViewValues!.Length; ++i) + { + if (!FilterSubViewValues[i].Overlaps(node.SubViewValues[i])) + return false; + } + + return true; + } + + /// + /// Generates a minimal material dev-kit file for the given shader package. + /// + /// This file currently only hides globally unused material constants. + /// + public JObject ExportDevkit() + { + var devkit = new JObject(); + + var maybeMaterialParameter = Shpk.GetConstantById(ShpkFile.MaterialParamsConstantId); + if (maybeMaterialParameter.HasValue) + { + var materialParameter = maybeMaterialParameter.Value; + var materialParameterUsage = new IndexSet(materialParameter.Size << 2, true); + + var used = materialParameter.Used ?? []; + var usedDynamically = materialParameter.UsedDynamically ?? 0; + for (var i = 0; i < used.Length; ++i) + { + for (var j = 0; j < 4; ++j) + { + if (!(used[i] | usedDynamically).HasFlag((DisassembledShader.VectorComponents)(1 << j))) + materialParameterUsage[(i << 2) | j] = false; + } + } + + var dkConstants = new JObject(); + foreach (var param in Shpk.MaterialParams) + { + // Don't handle misaligned parameters. + if ((param.ByteOffset & 0x3) != 0 || (param.ByteSize & 0x3) != 0) + continue; + + var start = param.ByteOffset >> 2; + var length = param.ByteSize >> 2; + + // If the parameter is fully used, don't include it. + if (!materialParameterUsage.Indices(start, length, true).Any()) + continue; + + var unusedSlices = new JArray(); + + if (materialParameterUsage.Indices(start, length).Any()) + { + foreach (var (rgStart, rgEnd) in materialParameterUsage.Ranges(start, length, true)) + { + unusedSlices.Add(new JObject + { + ["Type"] = "Hidden", + ["Offset"] = rgStart, + ["Length"] = rgEnd - rgStart, + }); + } + } + else + { + unusedSlices.Add(new JObject + { + ["Type"] = "Hidden", + }); + } + + dkConstants[param.Id.ToString()] = unusedSlices; + } + + devkit["Constants"] = dkConstants; + } + + return devkit; + } + public bool Valid => Shpk.Valid; diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs index f28cb632..13458252 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.cs @@ -615,7 +615,7 @@ public partial class ModEditWindow : Window, IDisposable, IUiService _shaderPackageTab = new FileEditor(this, _communicator, gameData, config, _editor.Compactor, _fileDialog, "Shaders", ".shpk", () => PopulateIsOnPlayer(_editor.Files.Shpk, ResourceType.Shpk), DrawShaderPackagePanel, () => Mod?.ModPath.FullName ?? string.Empty, - (bytes, _, _) => new ShpkTab(_fileDialog, bytes)); + (bytes, path, _) => new ShpkTab(_fileDialog, bytes, path)); _center = new CombinedTexture(_left, _right); _textureSelectCombo = new TextureDrawer.PathSelectCombo(textures, editor, () => GetPlayerResourcesOfType(ResourceType.Tex)); _resourceTreeFactory = resourceTreeFactory; From c01aa000fb94afcbfc13fcfe87c4ab6bd5b5f86d Mon Sep 17 00:00:00 2001 From: Exter-N Date: Sat, 3 Aug 2024 17:46:29 +0200 Subject: [PATCH 385/433] Optimize I/O of ShPk for ResourceTree generation --- Penumbra/Interop/ResourceTree/ResolveContext.cs | 11 ++++++++--- Penumbra/Interop/ResourceTree/TreeBuildCache.cs | 14 +++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.cs b/Penumbra/Interop/ResourceTree/ResolveContext.cs index 3fc1ae3c..41485d75 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.cs @@ -230,8 +230,8 @@ internal unsafe partial record ResolveContext( node.Children.Add(shpkNode); } - var shpkFile = Global.WithUiData && shpkNode != null ? Global.TreeBuildCache.ReadShaderPackage(shpkNode.FullPath) : null; - var shpk = Global.WithUiData && shpkNode != null ? (ShaderPackage*)shpkNode.ObjectAddress : null; + var shpkNames = Global.WithUiData && shpkNode != null ? Global.TreeBuildCache.ReadShaderPackageNames(shpkNode.FullPath) : null; + var shpk = Global.WithUiData && shpkNode != null ? (ShaderPackage*)shpkNode.ObjectAddress : null; var alreadyProcessedSamplerIds = new HashSet(); for (var i = 0; i < resource->TextureCount; i++) @@ -255,7 +255,12 @@ internal unsafe partial record ResolveContext( alreadyProcessedSamplerIds.Add(samplerId.Value); var samplerCrc = GetSamplerCrcById(shpk, samplerId.Value); if (samplerCrc.HasValue) - name = shpkFile?.GetSamplerById(samplerCrc.Value)?.Name ?? $"Texture 0x{samplerCrc.Value:X8}"; + { + if (shpkNames != null && shpkNames.TryGetValue(samplerCrc.Value, out var samplerName)) + name = samplerName.Value; + else + name = $"Texture 0x{samplerCrc.Value:X8}"; + } } } diff --git a/Penumbra/Interop/ResourceTree/TreeBuildCache.cs b/Penumbra/Interop/ResourceTree/TreeBuildCache.cs index ca5ff736..49e00547 100644 --- a/Penumbra/Interop/ResourceTree/TreeBuildCache.cs +++ b/Penumbra/Interop/ResourceTree/TreeBuildCache.cs @@ -1,8 +1,11 @@ +using System.IO.MemoryMappedFiles; using Dalamud.Game.ClientState.Objects.Types; using Dalamud.Plugin.Services; using Penumbra.GameData.Actors; using Penumbra.GameData.Enums; using Penumbra.GameData.Files; +using Penumbra.GameData.Files.ShaderStructs; +using Penumbra.GameData.Files.Utility; using Penumbra.GameData.Interop; using Penumbra.GameData.Structs; using Penumbra.String.Classes; @@ -11,7 +14,7 @@ namespace Penumbra.Interop.ResourceTree; internal readonly struct TreeBuildCache(ObjectManager objects, IDataManager dataManager, ActorManager actors) { - private readonly Dictionary _shaderPackages = []; + private readonly Dictionary?> _shaderPackageNames = []; public unsafe bool IsLocalPlayerRelated(ICharacter character) { @@ -68,10 +71,10 @@ internal readonly struct TreeBuildCache(ObjectManager objects, IDataManager data } /// Try to read a shpk file from the given path and cache it on success. - public ShpkFile? ReadShaderPackage(FullPath path) - => ReadFile(dataManager, path, _shaderPackages, bytes => new ShpkFile(bytes)); + public IReadOnlyDictionary? ReadShaderPackageNames(FullPath path) + => ReadFile(dataManager, path, _shaderPackageNames, bytes => ShpkFile.FastExtractNames(bytes.Span)); - private static T? ReadFile(IDataManager dataManager, FullPath path, Dictionary cache, Func parseFile) + private static T? ReadFile(IDataManager dataManager, FullPath path, Dictionary cache, Func, T> parseFile) where T : class { if (path.FullName.Length == 0) @@ -86,7 +89,8 @@ internal readonly struct TreeBuildCache(ObjectManager objects, IDataManager data { if (path.IsRooted) { - parsed = parseFile(File.ReadAllBytes(pathStr)); + using var mmFile = MmioMemoryManager.CreateFromFile(pathStr, access: MemoryMappedFileAccess.Read); + parsed = parseFile(mmFile.Memory); } else { From c849e310343b465d88619f05e9b30384d1caa709 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Sat, 3 Aug 2024 19:48:42 +0200 Subject: [PATCH 386/433] RT: Use SpanTextWriter to assemble paths --- .../ResolveContext.PathResolution.cs | 28 +++++++++++++------ .../Interop/ResourceTree/ResolveContext.cs | 25 +++++++++-------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs index 85b3284a..b99468f8 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs @@ -1,5 +1,6 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; +using OtterGui.Text.HelperObjects; using Penumbra.GameData.Data; using Penumbra.GameData.Enums; using Penumbra.GameData.Structs; @@ -8,6 +9,7 @@ using Penumbra.Meta.Manipulations; using Penumbra.String; using Penumbra.String.Classes; using static Penumbra.Interop.Structs.StructExtensions; +using CharaBase = FFXIVClientStructs.FFXIV.Client.Graphics.Scene.CharacterBase; using ModelType = FFXIVClientStructs.FFXIV.Client.Graphics.Scene.CharacterBase.ModelType; namespace Penumbra.Interop.ResourceTree; @@ -95,7 +97,7 @@ internal partial record ResolveContext var variant = ResolveMaterialVariant(imc, Equipment.Variant); var fileName = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(mtrlFileName); - Span pathBuffer = stackalloc byte[260]; + Span pathBuffer = stackalloc byte[CharaBase.PathBufferSize]; pathBuffer = AssembleMaterialPath(pathBuffer, modelPath.Path.Span, variant, fileName); return Utf8GamePath.FromSpan(pathBuffer, MetaDataComputation.None, out var path) ? path.Clone() : Utf8GamePath.Empty; @@ -125,7 +127,7 @@ internal partial record ResolveContext fileName.CopyTo(mirroredFileName); WriteZeroPaddedNumber(mirroredFileName[4..8], mirroredSetId); - Span pathBuffer = stackalloc byte[260]; + Span pathBuffer = stackalloc byte[CharaBase.PathBufferSize]; pathBuffer = AssembleMaterialPath(pathBuffer, modelPath.Path.Span, variant, mirroredFileName); var weaponPosition = pathBuffer.IndexOf("/weapon/w"u8); @@ -144,7 +146,7 @@ internal partial record ResolveContext var variant = ResolveMaterialVariant(imc, (byte)((Monster*)CharacterBase)->Variant); var fileName = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(mtrlFileName); - Span pathBuffer = stackalloc byte[260]; + Span pathBuffer = stackalloc byte[CharaBase.PathBufferSize]; pathBuffer = AssembleMaterialPath(pathBuffer, modelPath.Path.Span, variant, fileName); return Utf8GamePath.FromSpan(pathBuffer, MetaDataComputation.None, out var path) ? path.Clone() : Utf8GamePath.Empty; @@ -175,13 +177,21 @@ internal partial record ResolveContext var baseDirectory = modelPath[..modelPosition]; - baseDirectory.CopyTo(materialPathBuffer); - "/material/v"u8.CopyTo(materialPathBuffer[baseDirectory.Length..]); - WriteZeroPaddedNumber(materialPathBuffer.Slice(baseDirectory.Length + 11, 4), variant); - materialPathBuffer[baseDirectory.Length + 15] = (byte)'/'; - mtrlFileName.CopyTo(materialPathBuffer[(baseDirectory.Length + 16)..]); + var writer = new SpanTextWriter(materialPathBuffer); + writer.Append(baseDirectory); + writer.Append("/material/v"u8); + WriteZeroPaddedNumber(ref writer, 4, variant); + writer.Append((byte)'/'); + writer.Append(mtrlFileName); + writer.EnsureNullTerminated(); - return materialPathBuffer[..(baseDirectory.Length + 16 + mtrlFileName.Length)]; + return materialPathBuffer[..writer.Position]; + } + + private static void WriteZeroPaddedNumber(ref SpanTextWriter writer, int width, ushort number) + { + WriteZeroPaddedNumber(writer.GetRemainingSpan()[..width], number); + writer.Advance(width); } private static void WriteZeroPaddedNumber(Span destination, ushort number) diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.cs b/Penumbra/Interop/ResourceTree/ResolveContext.cs index 3fc1ae3c..29e15055 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.cs @@ -1,9 +1,9 @@ using FFXIVClientStructs.FFXIV.Client.Graphics.Kernel; using FFXIVClientStructs.FFXIV.Client.Graphics.Render; -using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; using FFXIVClientStructs.Interop; using OtterGui; +using OtterGui.Text.HelperObjects; using Penumbra.Api.Enums; using Penumbra.Collections; using Penumbra.GameData.Data; @@ -16,7 +16,7 @@ using Penumbra.String; using Penumbra.String.Classes; using Penumbra.UI; using static Penumbra.Interop.Structs.StructExtensions; -using ModelType = FFXIVClientStructs.FFXIV.Client.Graphics.Scene.CharacterBase.ModelType; +using CharaBase = FFXIVClientStructs.FFXIV.Client.Graphics.Scene.CharacterBase; namespace Penumbra.Interop.ResourceTree; @@ -29,25 +29,25 @@ internal record GlobalResolveContext( { public readonly Dictionary<(Utf8GamePath, nint), ResourceNode> Nodes = new(128); - public unsafe ResolveContext CreateContext(CharacterBase* characterBase, uint slotIndex = 0xFFFFFFFFu, + public unsafe ResolveContext CreateContext(CharaBase* characterBase, uint slotIndex = 0xFFFFFFFFu, EquipSlot slot = EquipSlot.Unknown, CharacterArmor equipment = default, SecondaryId secondaryId = default) => new(this, characterBase, slotIndex, slot, equipment, secondaryId); } internal unsafe partial record ResolveContext( GlobalResolveContext Global, - Pointer CharacterBasePointer, + Pointer CharacterBasePointer, uint SlotIndex, EquipSlot Slot, CharacterArmor Equipment, SecondaryId SecondaryId) { - public CharacterBase* CharacterBase + public CharaBase* CharacterBase => CharacterBasePointer.Value; private static readonly CiByteString ShpkPrefix = CiByteString.FromSpanUnsafe("shader/sm5/shpk"u8, true, true, true); - private ModelType ModelType + private CharaBase.ModelType ModelType => CharacterBase->GetModelType(); private ResourceNode? CreateNodeFromShpk(ShaderPackageResourceHandle* resourceHandle, CiByteString gamePath) @@ -75,11 +75,14 @@ internal unsafe partial record ResolveContext( if (lastDirectorySeparator == -1 || lastDirectorySeparator > gamePath.Length - 3) return null; - Span prefixed = stackalloc byte[260]; - gamePath.Span[..(lastDirectorySeparator + 1)].CopyTo(prefixed); - prefixed[lastDirectorySeparator + 1] = (byte)'-'; - prefixed[lastDirectorySeparator + 2] = (byte)'-'; - gamePath.Span[(lastDirectorySeparator + 1)..].CopyTo(prefixed[(lastDirectorySeparator + 3)..]); + Span prefixed = stackalloc byte[CharaBase.PathBufferSize]; + + var writer = new SpanTextWriter(prefixed); + writer.Append(gamePath.Span[..(lastDirectorySeparator + 1)]); + writer.Append((byte)'-'); + writer.Append((byte)'-'); + writer.Append(gamePath.Span[(lastDirectorySeparator + 1)..]); + writer.EnsureNullTerminated(); if (!Utf8GamePath.FromSpan(prefixed[..(gamePath.Length + 2)], MetaDataComputation.None, out var tmp)) return null; From 243593e30f74c43a14b7d0ccdd9a264830158a59 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Sat, 3 Aug 2024 19:52:39 +0200 Subject: [PATCH 387/433] RT: Fix VPR offhand material paths --- .../ResolveContext.PathResolution.cs | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs index b99468f8..c3894b05 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs @@ -111,31 +111,25 @@ internal partial record ResolveContext if (setIdHigh is 20 && mtrlFileName[14] == (byte)'c') return Utf8GamePath.FromString(GamePaths.Weapon.Mtrl.Path(2001, 1, 1, "c"), out var path) ? path : Utf8GamePath.Empty; - // MNK (03??, 16??), NIN (18??) and DNC (26??) offhands share materials with the corresponding mainhand - if (setIdHigh is 3 or 16 or 18 or 26) + // Some offhands share materials with the corresponding mainhand + if (ItemData.AdaptOffhandImc(Equipment.Set.Id, out var mirroredSetId)) { - var setIdLow = Equipment.Set.Id % 100; - if (setIdLow > 50) - { - var variant = ResolveMaterialVariant(imc, Equipment.Variant); - var fileName = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(mtrlFileName); + var variant = ResolveMaterialVariant(imc, Equipment.Variant); + var fileName = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(mtrlFileName); - var mirroredSetId = (ushort)(Equipment.Set.Id - 50); + Span mirroredFileName = stackalloc byte[32]; + mirroredFileName = mirroredFileName[..fileName.Length]; + fileName.CopyTo(mirroredFileName); + WriteZeroPaddedNumber(mirroredFileName[4..8], mirroredSetId.Id); - Span mirroredFileName = stackalloc byte[32]; - mirroredFileName = mirroredFileName[..fileName.Length]; - fileName.CopyTo(mirroredFileName); - WriteZeroPaddedNumber(mirroredFileName[4..8], mirroredSetId); + Span pathBuffer = stackalloc byte[CharaBase.PathBufferSize]; + pathBuffer = AssembleMaterialPath(pathBuffer, modelPath.Path.Span, variant, mirroredFileName); - Span pathBuffer = stackalloc byte[CharaBase.PathBufferSize]; - pathBuffer = AssembleMaterialPath(pathBuffer, modelPath.Path.Span, variant, mirroredFileName); + var weaponPosition = pathBuffer.IndexOf("/weapon/w"u8); + if (weaponPosition >= 0) + WriteZeroPaddedNumber(pathBuffer[(weaponPosition + 9)..(weaponPosition + 13)], mirroredSetId.Id); - var weaponPosition = pathBuffer.IndexOf("/weapon/w"u8); - if (weaponPosition >= 0) - WriteZeroPaddedNumber(pathBuffer[(weaponPosition + 9)..(weaponPosition + 13)], mirroredSetId); - - return Utf8GamePath.FromSpan(pathBuffer, MetaDataComputation.None, out var path) ? path.Clone() : Utf8GamePath.Empty; - } + return Utf8GamePath.FromSpan(pathBuffer, MetaDataComputation.None, out var path) ? path.Clone() : Utf8GamePath.Empty; } return ResolveEquipmentMaterialPath(modelPath, imc, mtrlFileName); From 75e3ef72f3dbaff37db0ba18d2770a5e7885f3ae Mon Sep 17 00:00:00 2001 From: Exter-N Date: Sat, 3 Aug 2024 20:27:16 +0200 Subject: [PATCH 388/433] RT: Fix Facewear --- .../ResolveContext.PathResolution.cs | 16 ++++++---- Penumbra/Interop/ResourceTree/ResourceTree.cs | 30 ++++++++++++------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs index c3894b05..43324516 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.PathResolution.cs @@ -16,20 +16,26 @@ namespace Penumbra.Interop.ResourceTree; internal partial record ResolveContext { + private static bool IsEquipmentOrAccessorySlot(uint slotIndex) + => slotIndex is < 10 or 16 or 17; + + private static bool IsEquipmentSlot(uint slotIndex) + => slotIndex is < 5 or 16 or 17; + private Utf8GamePath ResolveModelPath() { // Correctness: // Resolving a model path through the game's code can use EQDP metadata for human equipment models. return ModelType switch { - ModelType.Human when SlotIndex < 10 => ResolveEquipmentModelPath(), - _ => ResolveModelPathNative(), + ModelType.Human when IsEquipmentOrAccessorySlot(SlotIndex) => ResolveEquipmentModelPath(), + _ => ResolveModelPathNative(), }; } private Utf8GamePath ResolveEquipmentModelPath() { - var path = SlotIndex < 5 + var path = IsEquipmentSlot(SlotIndex) ? GamePaths.Equipment.Mdl.Path(Equipment.Set, ResolveModelRaceCode(), Slot) : GamePaths.Accessory.Mdl.Path(Equipment.Set, ResolveModelRaceCode(), Slot); return Utf8GamePath.FromString(path, out var gamePath) ? gamePath : Utf8GamePath.Empty; @@ -41,7 +47,7 @@ internal partial record ResolveContext private unsafe GenderRace ResolveEqdpRaceCode(EquipSlot slot, PrimaryId primaryId) { var slotIndex = slot.ToIndex(); - if (slotIndex >= 10 || ModelType != ModelType.Human) + if (!IsEquipmentOrAccessorySlot(slotIndex) || ModelType != ModelType.Human) return GenderRace.MidlanderMale; var characterRaceCode = (GenderRace)((Human*)CharacterBase)->RaceSexId; @@ -82,7 +88,7 @@ internal partial record ResolveContext // Resolving a material path through the game's code can dereference null pointers for materials that involve IMC metadata. return ModelType switch { - ModelType.Human when SlotIndex is < 10 or 16 && mtrlFileName[8] != (byte)'b' + ModelType.Human when IsEquipmentOrAccessorySlot(SlotIndex) && mtrlFileName[8] != (byte)'b' => ResolveEquipmentMaterialPath(modelPath, imc, mtrlFileName), ModelType.DemiHuman => ResolveEquipmentMaterialPath(modelPath, imc, mtrlFileName), ModelType.Weapon => ResolveWeaponMaterialPath(modelPath, imc, mtrlFileName), diff --git a/Penumbra/Interop/ResourceTree/ResourceTree.cs b/Penumbra/Interop/ResourceTree/ResourceTree.cs index 6663fb40..f1507294 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTree.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTree.cs @@ -9,6 +9,7 @@ using Penumbra.Interop.Hooks.PostProcessing; using Penumbra.UI; using CustomizeData = FFXIVClientStructs.FFXIV.Client.Game.Character.CustomizeData; using CustomizeIndex = Dalamud.Game.ClientState.Objects.Enums.CustomizeIndex; +using ModelType = FFXIVClientStructs.FFXIV.Client.Graphics.Scene.CharacterBase.ModelType; namespace Penumbra.Interop.ResourceTree; @@ -44,8 +45,8 @@ public class ResourceTree PlayerRelated = playerRelated; CollectionName = collectionName; AnonymizedCollectionName = anonymizedCollectionName; - Nodes = new List(); - FlatNodes = new HashSet(); + Nodes = []; + FlatNodes = []; } public void ProcessPostfix(Action action) @@ -59,13 +60,13 @@ public class ResourceTree var character = (Character*)GameObjectAddress; var model = (CharacterBase*)DrawObjectAddress; var modelType = model->GetModelType(); - var human = modelType == CharacterBase.ModelType.Human ? (Human*)model : null; + var human = modelType == ModelType.Human ? (Human*)model : null; var equipment = modelType switch { - CharacterBase.ModelType.Human => new ReadOnlySpan(&human->Head, 10), - CharacterBase.ModelType.DemiHuman => new ReadOnlySpan( + ModelType.Human => new ReadOnlySpan(&human->Head, 12), + ModelType.DemiHuman => new ReadOnlySpan( Unsafe.AsPointer(ref character->DrawData.EquipmentModelIds[0]), 10), - _ => ReadOnlySpan.Empty, + _ => [], }; ModelId = character->CharacterData.ModelCharaId; CustomizeData = character->DrawData.CustomizeData; @@ -75,9 +76,18 @@ public class ResourceTree for (var i = 0u; i < model->SlotCount; ++i) { - var slotContext = i < equipment.Length - ? globalContext.CreateContext(model, i, i.ToEquipSlot(), equipment[(int)i]) - : globalContext.CreateContext(model, i); + var slotContext = modelType switch + { + ModelType.Human => i switch + { + < 10 => globalContext.CreateContext(model, i, i.ToEquipSlot(), equipment[(int)i]), + 16 or 17 => globalContext.CreateContext(model, i, EquipSlot.Head, equipment[(int)(i - 6)]), + _ => globalContext.CreateContext(model, i), + }, + _ => i < equipment.Length + ? globalContext.CreateContext(model, i, i.ToEquipSlot(), equipment[(int)i]) + : globalContext.CreateContext(model, i), + }; var imc = (ResourceHandle*)model->IMCArray[i]; var imcNode = slotContext.CreateNodeFromImc(imc); @@ -117,7 +127,7 @@ public class ResourceTree var subObject = (CharacterBase*)baseSubObject; - if (subObject->GetModelType() != CharacterBase.ModelType.Weapon) + if (subObject->GetModelType() != ModelType.Weapon) continue; var weapon = (Weapon*)subObject; From da3f3b8df39c24a84b92d34ee730e7ffc74abe84 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 3 Aug 2024 22:45:38 +0200 Subject: [PATCH 389/433] Start rework of identified objects. --- Penumbra.Api | 2 +- Penumbra.GameData | 2 +- Penumbra/Api/Api/CollectionApi.cs | 2 +- Penumbra/Api/Api/ModsApi.cs | 2 +- Penumbra/Api/Api/UiApi.cs | 10 +- .../Api/IpcTester/CollectionsIpcTester.cs | 16 +- Penumbra/Collections/Cache/CollectionCache.cs | 25 +- .../Collections/ModCollection.Cache.Access.cs | 6 +- Penumbra/Communication/ChangedItemClick.cs | 3 +- Penumbra/Communication/ChangedItemHover.cs | 3 +- Penumbra/EphemeralConfig.cs | 36 +- .../Interop/ResourceTree/ResolveContext.cs | 12 +- Penumbra/Interop/ResourceTree/ResourceNode.cs | 12 +- Penumbra/Interop/ResourceTree/ResourceTree.cs | 6 +- .../ResourceTree/ResourceTreeApiHelper.cs | 4 +- Penumbra/Meta/Manipulations/Eqdp.cs | 2 +- Penumbra/Meta/Manipulations/Eqp.cs | 2 +- Penumbra/Meta/Manipulations/Est.cs | 2 +- .../Manipulations/GlobalEqpManipulation.cs | 2 +- Penumbra/Meta/Manipulations/Gmp.cs | 2 +- .../Meta/Manipulations/IMetaIdentifier.cs | 2 +- Penumbra/Meta/Manipulations/Imc.cs | 4 +- Penumbra/Meta/Manipulations/Rsp.cs | 3 +- Penumbra/Mods/Groups/IModGroup.cs | 2 +- Penumbra/Mods/Groups/ImcModGroup.cs | 2 +- Penumbra/Mods/Groups/MultiModGroup.cs | 2 +- Penumbra/Mods/Groups/SingleModGroup.cs | 2 +- Penumbra/Mods/ItemSwap/EquipmentSwap.cs | 5 +- Penumbra/Mods/Mod.cs | 3 +- Penumbra/Penumbra.cs | 9 +- Penumbra/Services/ConfigMigrationService.cs | 2 +- Penumbra/UI/AdvancedWindow/ItemSwapTab.cs | 2 +- .../UI/AdvancedWindow/ResourceTreeViewer.cs | 20 +- Penumbra/UI/ChangedItemDrawer.cs | 354 +++++------------- Penumbra/UI/ChangedItemIconFlag.cs | 122 ++++++ Penumbra/UI/ModsTab/ModFileSystemSelector.cs | 2 +- .../UI/ModsTab/ModPanelChangedItemsTab.cs | 11 +- .../UI/ModsTab/ModSearchStringSplitter.cs | 12 +- Penumbra/UI/Tabs/ChangedItemsTab.cs | 9 +- Penumbra/UI/Tabs/SettingsTab.cs | 2 +- Penumbra/Util/IdentifierExtensions.cs | 10 +- 41 files changed, 342 insertions(+), 389 deletions(-) create mode 100644 Penumbra/UI/ChangedItemIconFlag.cs diff --git a/Penumbra.Api b/Penumbra.Api index 86249598..759a8e9d 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit 86249598afb71601b247f9629d9c29dbecfe6eb1 +Subproject commit 759a8e9dc50b3453cdb7c3cba76de7174c94aba0 diff --git a/Penumbra.GameData b/Penumbra.GameData index 75582ece..44427ad0 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 75582ece58e6ee311074ff4ecaa68b804677878c +Subproject commit 44427ad0149059ab5ccb4e4a2f42a1a43423e4c5 diff --git a/Penumbra/Api/Api/CollectionApi.cs b/Penumbra/Api/Api/CollectionApi.cs index ff393aaf..04299187 100644 --- a/Penumbra/Api/Api/CollectionApi.cs +++ b/Penumbra/Api/Api/CollectionApi.cs @@ -36,7 +36,7 @@ public class CollectionApi(CollectionManager collections, ApiHelpers helpers) : collection = ModCollection.Empty; if (collection.HasCache) - return collection.ChangedItems.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Item2); + return collection.ChangedItems.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Item2?.ToInternalObject()); Penumbra.Log.Warning($"Collection {collectionId} does not exist or is not loaded."); return []; diff --git a/Penumbra/Api/Api/ModsApi.cs b/Penumbra/Api/Api/ModsApi.cs index 60b00d37..790121d5 100644 --- a/Penumbra/Api/Api/ModsApi.cs +++ b/Penumbra/Api/Api/ModsApi.cs @@ -134,6 +134,6 @@ public class ModsApi : IPenumbraApiMods, IApiService, IDisposable public Dictionary GetChangedItems(string modDirectory, string modName) => _modManager.TryGetMod(modDirectory, modName, out var mod) - ? mod.ChangedItems.ToDictionary(kvp => kvp.Key, kvp => kvp.Value) + ? mod.ChangedItems.ToDictionary(kvp => kvp.Key, kvp => kvp.Value?.ToInternalObject()) : []; } diff --git a/Penumbra/Api/Api/UiApi.cs b/Penumbra/Api/Api/UiApi.cs index cf3cd8f2..515874c0 100644 --- a/Penumbra/Api/Api/UiApi.cs +++ b/Penumbra/Api/Api/UiApi.cs @@ -1,7 +1,7 @@ using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Communication; -using Penumbra.GameData.Enums; +using Penumbra.GameData.Data; using Penumbra.Mods.Manager; using Penumbra.Services; using Penumbra.UI; @@ -81,21 +81,21 @@ public class UiApi : IPenumbraApiUi, IApiService, IDisposable public void CloseMainWindow() => _configWindow.IsOpen = false; - private void OnChangedItemClick(MouseButton button, object? data) + private void OnChangedItemClick(MouseButton button, IIdentifiedObjectData? data) { if (ChangedItemClicked == null) return; - var (type, id) = ChangedItemExtensions.ChangedItemToTypeAndId(data); + var (type, id) = data?.ToApiObject() ?? (ChangedItemType.None, 0); ChangedItemClicked.Invoke(button, type, id); } - private void OnChangedItemHover(object? data) + private void OnChangedItemHover(IIdentifiedObjectData? data) { if (ChangedItemTooltip == null) return; - var (type, id) = ChangedItemExtensions.ChangedItemToTypeAndId(data); + var (type, id) = data?.ToApiObject() ?? (ChangedItemType.None, 0); ChangedItemTooltip.Invoke(type, id); } } diff --git a/Penumbra/Api/IpcTester/CollectionsIpcTester.cs b/Penumbra/Api/IpcTester/CollectionsIpcTester.cs index 026fabbc..1d516eba 100644 --- a/Penumbra/Api/IpcTester/CollectionsIpcTester.cs +++ b/Penumbra/Api/IpcTester/CollectionsIpcTester.cs @@ -8,7 +8,7 @@ using OtterGui.Services; using Penumbra.Api.Enums; using Penumbra.Api.IpcSubscribers; using Penumbra.Collections.Manager; -using Penumbra.GameData.Enums; +using Penumbra.GameData.Data; using ImGuiClip = OtterGui.ImGuiClip; namespace Penumbra.Api.IpcTester; @@ -17,10 +17,10 @@ public class CollectionsIpcTester(IDalamudPluginInterface pi) : IUiService { private int _objectIdx; private string _collectionIdString = string.Empty; - private Guid? _collectionId = null; - private bool _allowCreation = true; - private bool _allowDeletion = true; - private ApiCollectionType _type = ApiCollectionType.Yourself; + private Guid? _collectionId; + private bool _allowCreation = true; + private bool _allowDeletion = true; + private ApiCollectionType _type = ApiCollectionType.Yourself; private Dictionary _collections = []; private (string, ChangedItemType, uint)[] _changedItems = []; @@ -116,7 +116,7 @@ public class CollectionsIpcTester(IDalamudPluginInterface pi) : IUiService var items = new GetChangedItemsForCollection(pi).Invoke(_collectionId.GetValueOrDefault(Guid.Empty)); _changedItems = items.Select(kvp => { - var (type, id) = ChangedItemExtensions.ChangedItemToTypeAndId(kvp.Value); + var (type, id) = kvp.Value.ToApiObject(); return (kvp.Key, type, id); }).ToArray(); ImGui.OpenPopup("Changed Item List"); @@ -130,9 +130,9 @@ public class CollectionsIpcTester(IDalamudPluginInterface pi) : IUiService if (!p) return; - using (var t = ImRaii.Table("##ChangedItems", 3, ImGuiTableFlags.SizingFixedFit)) + using (var table = ImRaii.Table("##ChangedItems", 3, ImGuiTableFlags.SizingFixedFit)) { - if (t) + if (table) ImGuiClip.ClippedDraw(_changedItems, t => { ImGuiUtil.DrawTableColumn(t.Item1); diff --git a/Penumbra/Collections/Cache/CollectionCache.cs b/Penumbra/Collections/Cache/CollectionCache.cs index 4755840e..abc0dff8 100644 --- a/Penumbra/Collections/Cache/CollectionCache.cs +++ b/Penumbra/Collections/Cache/CollectionCache.cs @@ -6,6 +6,7 @@ using Penumbra.Communication; using Penumbra.Mods.Editor; using Penumbra.String.Classes; using Penumbra.Util; +using Penumbra.GameData.Data; namespace Penumbra.Collections.Cache; @@ -18,14 +19,14 @@ public record ModConflicts(IMod Mod2, List Conflicts, bool HasPriority, /// public sealed class CollectionCache : IDisposable { - private readonly CollectionCacheManager _manager; - private readonly ModCollection _collection; - public readonly CollectionModData ModData = new(); - private readonly SortedList, object?)> _changedItems = []; - public readonly ConcurrentDictionary ResolvedFiles = new(); - public readonly CustomResourceCache CustomResources; - public readonly MetaCache Meta; - public readonly Dictionary> ConflictDict = []; + private readonly CollectionCacheManager _manager; + private readonly ModCollection _collection; + public readonly CollectionModData ModData = new(); + private readonly SortedList, IIdentifiedObjectData?)> _changedItems = []; + public readonly ConcurrentDictionary ResolvedFiles = new(); + public readonly CustomResourceCache CustomResources; + public readonly MetaCache Meta; + public readonly Dictionary> ConflictDict = []; public int Calculating = -1; @@ -41,7 +42,7 @@ public sealed class CollectionCache : IDisposable private int _changedItemsSaveCounter = -1; // Obtain currently changed items. Computes them if they haven't been computed before. - public IReadOnlyDictionary, object?)> ChangedItems + public IReadOnlyDictionary, IIdentifiedObjectData?)> ChangedItems { get { @@ -412,7 +413,7 @@ public sealed class CollectionCache : IDisposable // Skip IMCs because they would result in far too many false-positive items, // since they are per set instead of per item-slot/item/variant. var identifier = _manager.MetaFileManager.Identifier; - var items = new SortedList(512); + var items = new SortedList(512); void AddItems(IMod mod) { @@ -421,8 +422,8 @@ public sealed class CollectionCache : IDisposable if (!_changedItems.TryGetValue(name, out var data)) _changedItems.Add(name, (new SingleArray(mod), obj)); else if (!data.Item1.Contains(mod)) - _changedItems[name] = (data.Item1.Append(mod), obj is int x && data.Item2 is int y ? x + y : obj); - else if (obj is int x && data.Item2 is int y) + _changedItems[name] = (data.Item1.Append(mod), obj is IdentifiedCounter x && data.Item2 is IdentifiedCounter y ? x + y : obj); + else if (obj is IdentifiedCounter x && data.Item2 is IdentifiedCounter y) _changedItems[name] = (data.Item1, x + y); } diff --git a/Penumbra/Collections/ModCollection.Cache.Access.cs b/Penumbra/Collections/ModCollection.Cache.Access.cs index 983509a4..0b38dde8 100644 --- a/Penumbra/Collections/ModCollection.Cache.Access.cs +++ b/Penumbra/Collections/ModCollection.Cache.Access.cs @@ -1,8 +1,8 @@ using OtterGui.Classes; using Penumbra.Mods; -using Penumbra.Meta.Files; using Penumbra.String.Classes; using Penumbra.Collections.Cache; +using Penumbra.GameData.Data; using Penumbra.Mods.Editor; namespace Penumbra.Collections; @@ -46,8 +46,8 @@ public partial class ModCollection internal IReadOnlyDictionary ResolvedFiles => _cache?.ResolvedFiles ?? new ConcurrentDictionary(); - internal IReadOnlyDictionary, object?)> ChangedItems - => _cache?.ChangedItems ?? new Dictionary, object?)>(); + internal IReadOnlyDictionary, IIdentifiedObjectData?)> ChangedItems + => _cache?.ChangedItems ?? new Dictionary, IIdentifiedObjectData?)>(); internal IEnumerable> AllConflicts => _cache?.AllConflicts ?? Array.Empty>(); diff --git a/Penumbra/Communication/ChangedItemClick.cs b/Penumbra/Communication/ChangedItemClick.cs index 554e2221..1aac4454 100644 --- a/Penumbra/Communication/ChangedItemClick.cs +++ b/Penumbra/Communication/ChangedItemClick.cs @@ -1,6 +1,7 @@ using OtterGui.Classes; using Penumbra.Api.Api; using Penumbra.Api.Enums; +using Penumbra.GameData.Data; namespace Penumbra.Communication; @@ -11,7 +12,7 @@ namespace Penumbra.Communication; /// Parameter is the clicked object data if any. /// /// -public sealed class ChangedItemClick() : EventWrapper(nameof(ChangedItemClick)) +public sealed class ChangedItemClick() : EventWrapper(nameof(ChangedItemClick)) { public enum Priority { diff --git a/Penumbra/Communication/ChangedItemHover.cs b/Penumbra/Communication/ChangedItemHover.cs index 2dcced35..4e72b558 100644 --- a/Penumbra/Communication/ChangedItemHover.cs +++ b/Penumbra/Communication/ChangedItemHover.cs @@ -1,5 +1,6 @@ using OtterGui.Classes; using Penumbra.Api.Api; +using Penumbra.GameData.Data; namespace Penumbra.Communication; @@ -9,7 +10,7 @@ namespace Penumbra.Communication; /// Parameter is the hovered object data if any. /// /// -public sealed class ChangedItemHover() : EventWrapper(nameof(ChangedItemHover)) +public sealed class ChangedItemHover() : EventWrapper(nameof(ChangedItemHover)) { public enum Priority { diff --git a/Penumbra/EphemeralConfig.cs b/Penumbra/EphemeralConfig.cs index 7457c910..24ab466b 100644 --- a/Penumbra/EphemeralConfig.cs +++ b/Penumbra/EphemeralConfig.cs @@ -23,24 +23,24 @@ public class EphemeralConfig : ISavable, IDisposable, IService [JsonIgnore] private readonly ModPathChanged _modPathChanged; - public int Version { get; set; } = Configuration.Constants.CurrentVersion; - public int LastSeenVersion { get; set; } = PenumbraChangelog.LastChangelogVersion; - public bool DebugSeparateWindow { get; set; } = false; - public int TutorialStep { get; set; } = 0; - public bool EnableResourceLogging { get; set; } = false; - public string ResourceLoggingFilter { get; set; } = string.Empty; - public bool EnableResourceWatcher { get; set; } = false; - public bool OnlyAddMatchingResources { get; set; } = true; - public ResourceTypeFlag ResourceWatcherResourceTypes { get; set; } = ResourceExtensions.AllResourceTypes; - public ResourceCategoryFlag ResourceWatcherResourceCategories { get; set; } = ResourceExtensions.AllResourceCategories; - public RecordType ResourceWatcherRecordTypes { get; set; } = ResourceWatcher.AllRecords; - public CollectionsTab.PanelMode CollectionPanel { get; set; } = CollectionsTab.PanelMode.SimpleAssignment; - public TabType SelectedTab { get; set; } = TabType.Settings; - public ChangedItemDrawer.ChangedItemIcon ChangedItemFilter { get; set; } = ChangedItemDrawer.DefaultFlags; - public bool FixMainWindow { get; set; } = false; - public string LastModPath { get; set; } = string.Empty; - public bool AdvancedEditingOpen { get; set; } = false; - public bool ForceRedrawOnFileChange { get; set; } = false; + public int Version { get; set; } = Configuration.Constants.CurrentVersion; + public int LastSeenVersion { get; set; } = PenumbraChangelog.LastChangelogVersion; + public bool DebugSeparateWindow { get; set; } = false; + public int TutorialStep { get; set; } = 0; + public bool EnableResourceLogging { get; set; } = false; + public string ResourceLoggingFilter { get; set; } = string.Empty; + public bool EnableResourceWatcher { get; set; } = false; + public bool OnlyAddMatchingResources { get; set; } = true; + public ResourceTypeFlag ResourceWatcherResourceTypes { get; set; } = ResourceExtensions.AllResourceTypes; + public ResourceCategoryFlag ResourceWatcherResourceCategories { get; set; } = ResourceExtensions.AllResourceCategories; + public RecordType ResourceWatcherRecordTypes { get; set; } = ResourceWatcher.AllRecords; + public CollectionsTab.PanelMode CollectionPanel { get; set; } = CollectionsTab.PanelMode.SimpleAssignment; + public TabType SelectedTab { get; set; } = TabType.Settings; + public ChangedItemIconFlag ChangedItemFilter { get; set; } = ChangedItemFlagExtensions.DefaultFlags; + public bool FixMainWindow { get; set; } = false; + public string LastModPath { get; set; } = string.Empty; + public bool AdvancedEditingOpen { get; set; } = false; + public bool ForceRedrawOnFileChange { get; set; } = false; /// /// Load the current configuration. diff --git a/Penumbra/Interop/ResourceTree/ResolveContext.cs b/Penumbra/Interop/ResourceTree/ResolveContext.cs index 3fc1ae3c..9d0f1e46 100644 --- a/Penumbra/Interop/ResourceTree/ResolveContext.cs +++ b/Penumbra/Interop/ResourceTree/ResolveContext.cs @@ -345,7 +345,7 @@ internal unsafe partial record ResolveContext( _ => string.Empty, } + item.Name; - return new ResourceNode.UiData(name, ChangedItemDrawer.GetCategoryIcon(item.Name, item)); + return new ResourceNode.UiData(name, item.Type.GetCategoryIcon().ToFlag()); } var dataFromPath = GuessUiDataFromPath(gamePath); @@ -353,8 +353,8 @@ internal unsafe partial record ResolveContext( return dataFromPath; return isEquipment - ? new ResourceNode.UiData(Slot.ToName(), ChangedItemDrawer.GetCategoryIcon(Slot.ToSlot())) - : new ResourceNode.UiData(null, ChangedItemDrawer.ChangedItemIcon.Unknown); + ? new ResourceNode.UiData(Slot.ToName(), Slot.ToEquipType().GetCategoryIcon().ToFlag()) + : new ResourceNode.UiData(null, ChangedItemIconFlag.Unknown); } internal ResourceNode.UiData GuessUiDataFromPath(Utf8GamePath gamePath) @@ -362,13 +362,13 @@ internal unsafe partial record ResolveContext( foreach (var obj in Global.Identifier.Identify(gamePath.ToString())) { var name = obj.Key; - if (name.StartsWith("Customization:")) + if (obj.Value is IdentifiedCustomization) name = name[14..].Trim(); if (name != "Unknown") - return new ResourceNode.UiData(name, ChangedItemDrawer.GetCategoryIcon(obj.Key, obj.Value)); + return new ResourceNode.UiData(name, obj.Value.GetIcon().ToFlag()); } - return new ResourceNode.UiData(null, ChangedItemDrawer.ChangedItemIcon.Unknown); + return new ResourceNode.UiData(null, ChangedItemIconFlag.Unknown); } private static string? SafeGet(ReadOnlySpan array, Index index) diff --git a/Penumbra/Interop/ResourceTree/ResourceNode.cs b/Penumbra/Interop/ResourceTree/ResourceNode.cs index de43a874..6ab48325 100644 --- a/Penumbra/Interop/ResourceTree/ResourceNode.cs +++ b/Penumbra/Interop/ResourceTree/ResourceNode.cs @@ -1,7 +1,7 @@ using Penumbra.Api.Enums; using Penumbra.String; using Penumbra.String.Classes; -using ChangedItemIcon = Penumbra.UI.ChangedItemDrawer.ChangedItemIcon; +using Penumbra.UI; namespace Penumbra.Interop.ResourceTree; @@ -9,7 +9,7 @@ public class ResourceNode : ICloneable { public string? Name; public string? FallbackName; - public ChangedItemIcon Icon; + public ChangedItemIconFlag IconFlag; public readonly ResourceType Type; public readonly nint ObjectAddress; public readonly nint ResourceHandle; @@ -51,7 +51,7 @@ public class ResourceNode : ICloneable { Name = other.Name; FallbackName = other.FallbackName; - Icon = other.Icon; + IconFlag = other.IconFlag; Type = other.Type; ObjectAddress = other.ObjectAddress; ResourceHandle = other.ResourceHandle; @@ -79,7 +79,7 @@ public class ResourceNode : ICloneable public void SetUiData(UiData uiData) { Name = uiData.Name; - Icon = uiData.Icon; + IconFlag = uiData.IconFlag; } public void PrependName(string prefix) @@ -88,9 +88,9 @@ public class ResourceNode : ICloneable Name = prefix + Name; } - public readonly record struct UiData(string? Name, ChangedItemIcon Icon) + public readonly record struct UiData(string? Name, ChangedItemIconFlag IconFlag) { public UiData PrependName(string prefix) - => Name == null ? this : new UiData(prefix + Name, Icon); + => Name == null ? this : new UiData(prefix + Name, IconFlag); } } diff --git a/Penumbra/Interop/ResourceTree/ResourceTree.cs b/Penumbra/Interop/ResourceTree/ResourceTree.cs index 6663fb40..dc83fa65 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTree.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTree.cs @@ -174,7 +174,7 @@ public class ResourceTree { pbdNode = pbdNode.Clone(); pbdNode.FallbackName = "Racial Deformer"; - pbdNode.Icon = ChangedItemDrawer.ChangedItemIcon.Customization; + pbdNode.IconFlag = ChangedItemIconFlag.Customization; } Nodes.Add(pbdNode); @@ -192,7 +192,7 @@ public class ResourceTree { decalNode = decalNode.Clone(); decalNode.FallbackName = "Face Decal"; - decalNode.Icon = ChangedItemDrawer.ChangedItemIcon.Customization; + decalNode.IconFlag = ChangedItemIconFlag.Customization; } Nodes.Add(decalNode); @@ -209,7 +209,7 @@ public class ResourceTree { legacyDecalNode = legacyDecalNode.Clone(); legacyDecalNode.FallbackName = "Legacy Body Decal"; - legacyDecalNode.Icon = ChangedItemDrawer.ChangedItemIcon.Customization; + legacyDecalNode.IconFlag = ChangedItemIconFlag.Customization; } Nodes.Add(legacyDecalNode); diff --git a/Penumbra/Interop/ResourceTree/ResourceTreeApiHelper.cs b/Penumbra/Interop/ResourceTree/ResourceTreeApiHelper.cs index 22025dd6..48690e98 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTreeApiHelper.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTreeApiHelper.cs @@ -67,7 +67,7 @@ internal static class ResourceTreeApiHelper continue; var fullPath = node.FullPath.ToPath(); - resDictionary.Add(node.ResourceHandle, (fullPath, node.Name ?? string.Empty, (uint)ChangedItemDrawer.ToApiIcon(node.Icon))); + resDictionary.Add(node.ResourceHandle, (fullPath, node.Name ?? string.Empty, (uint)node.IconFlag.ToApiIcon())); } } @@ -106,7 +106,7 @@ internal static class ResourceTreeApiHelper var ret = new JObject { [nameof(ResourceNodeDto.Type)] = new JValue(node.Type), - [nameof(ResourceNodeDto.Icon)] = new JValue(ChangedItemDrawer.ToApiIcon(node.Icon)), + [nameof(ResourceNodeDto.Icon)] = new JValue(node.IconFlag.ToApiIcon()), [nameof(ResourceNodeDto.Name)] = node.Name, [nameof(ResourceNodeDto.GamePath)] = node.GamePath.Equals(Utf8GamePath.Empty) ? null : node.GamePath.ToString(), [nameof(ResourceNodeDto.ActualPath)] = node.FullPath.ToString(), diff --git a/Penumbra/Meta/Manipulations/Eqdp.cs b/Penumbra/Meta/Manipulations/Eqdp.cs index 3f856bd2..3a804d0c 100644 --- a/Penumbra/Meta/Manipulations/Eqdp.cs +++ b/Penumbra/Meta/Manipulations/Eqdp.cs @@ -15,7 +15,7 @@ public readonly record struct EqdpIdentifier(PrimaryId SetId, EquipSlot Slot, Ge public Gender Gender => GenderRace.Split().Item1; - public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) => identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(SetId, GenderRace, Slot)); public MetaIndex FileIndex() diff --git a/Penumbra/Meta/Manipulations/Eqp.cs b/Penumbra/Meta/Manipulations/Eqp.cs index 5d37aac8..f758126c 100644 --- a/Penumbra/Meta/Manipulations/Eqp.cs +++ b/Penumbra/Meta/Manipulations/Eqp.cs @@ -8,7 +8,7 @@ namespace Penumbra.Meta.Manipulations; public readonly record struct EqpIdentifier(PrimaryId SetId, EquipSlot Slot) : IMetaIdentifier, IComparable { - public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) => identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(SetId, GenderRace.MidlanderMale, Slot)); public MetaIndex FileIndex() diff --git a/Penumbra/Meta/Manipulations/Est.cs b/Penumbra/Meta/Manipulations/Est.cs index 2955dba4..cfe9b7d4 100644 --- a/Penumbra/Meta/Manipulations/Est.cs +++ b/Penumbra/Meta/Manipulations/Est.cs @@ -24,7 +24,7 @@ public readonly record struct EstIdentifier(PrimaryId SetId, EstType Slot, Gende public Gender Gender => GenderRace.Split().Item1; - public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) { switch (Slot) { diff --git a/Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs b/Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs index 2b88d962..ec59762b 100644 --- a/Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs +++ b/Penumbra/Meta/Manipulations/GlobalEqpManipulation.cs @@ -70,7 +70,7 @@ public readonly struct GlobalEqpManipulation : IMetaIdentifier public override string ToString() => $"Global EQP - {Type}{(Condition != 0 ? $" - {Condition.Id}" : string.Empty)}"; - public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) { var path = Type switch { diff --git a/Penumbra/Meta/Manipulations/Gmp.cs b/Penumbra/Meta/Manipulations/Gmp.cs index a6fcf58b..1f41adfb 100644 --- a/Penumbra/Meta/Manipulations/Gmp.cs +++ b/Penumbra/Meta/Manipulations/Gmp.cs @@ -8,7 +8,7 @@ namespace Penumbra.Meta.Manipulations; public readonly record struct GmpIdentifier(PrimaryId SetId) : IMetaIdentifier, IComparable { - public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) => identifier.Identify(changedItems, GamePaths.Equipment.Mdl.Path(SetId, GenderRace.MidlanderMale, EquipSlot.Head)); public MetaIndex FileIndex() diff --git a/Penumbra/Meta/Manipulations/IMetaIdentifier.cs b/Penumbra/Meta/Manipulations/IMetaIdentifier.cs index 5707ffca..d1668a4d 100644 --- a/Penumbra/Meta/Manipulations/IMetaIdentifier.cs +++ b/Penumbra/Meta/Manipulations/IMetaIdentifier.cs @@ -18,7 +18,7 @@ public enum MetaManipulationType : byte public interface IMetaIdentifier { - public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems); + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems); public MetaIndex FileIndex(); diff --git a/Penumbra/Meta/Manipulations/Imc.cs b/Penumbra/Meta/Manipulations/Imc.cs index d4887fe2..1b2492ee 100644 --- a/Penumbra/Meta/Manipulations/Imc.cs +++ b/Penumbra/Meta/Manipulations/Imc.cs @@ -27,10 +27,10 @@ public readonly record struct ImcIdentifier( : this(primaryId, variant, slot.IsAccessory() ? ObjectType.Accessory : ObjectType.Equipment, 0, slot, BodySlot.Unknown) { } - public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) => AddChangedItems(identifier, changedItems, false); - public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems, bool allVariants) + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems, bool allVariants) { var path = ObjectType switch { diff --git a/Penumbra/Meta/Manipulations/Rsp.cs b/Penumbra/Meta/Manipulations/Rsp.cs index 73d1d7e5..2d73ec7f 100644 --- a/Penumbra/Meta/Manipulations/Rsp.cs +++ b/Penumbra/Meta/Manipulations/Rsp.cs @@ -1,4 +1,3 @@ -using Lumina.Excel.GeneratedSheets; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Penumbra.GameData.Data; @@ -9,7 +8,7 @@ namespace Penumbra.Meta.Manipulations; public readonly record struct RspIdentifier(SubRace SubRace, RspAttribute Attribute) : IMetaIdentifier { - public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) => changedItems.TryAdd($"{SubRace.ToName()} {Attribute.ToFullString()}", null); public MetaIndex FileIndex() diff --git a/Penumbra/Mods/Groups/IModGroup.cs b/Penumbra/Mods/Groups/IModGroup.cs index 9327ced9..c5654019 100644 --- a/Penumbra/Mods/Groups/IModGroup.cs +++ b/Penumbra/Mods/Groups/IModGroup.cs @@ -45,7 +45,7 @@ public interface IModGroup public IModGroupEditDrawer EditDrawer(ModGroupEditDrawer editDrawer); public void AddData(Setting setting, Dictionary redirections, MetaDictionary manipulations); - public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems); + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems); /// Ensure that a value is valid for a group. public Setting FixSetting(Setting setting); diff --git a/Penumbra/Mods/Groups/ImcModGroup.cs b/Penumbra/Mods/Groups/ImcModGroup.cs index 7b0eb094..d42804ba 100644 --- a/Penumbra/Mods/Groups/ImcModGroup.cs +++ b/Penumbra/Mods/Groups/ImcModGroup.cs @@ -121,7 +121,7 @@ public class ImcModGroup(Mod mod) : IModGroup } } - public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) => Identifier.AddChangedItems(identifier, changedItems, AllVariants); public Setting FixSetting(Setting setting) diff --git a/Penumbra/Mods/Groups/MultiModGroup.cs b/Penumbra/Mods/Groups/MultiModGroup.cs index ee27d534..9cf7e6a3 100644 --- a/Penumbra/Mods/Groups/MultiModGroup.cs +++ b/Penumbra/Mods/Groups/MultiModGroup.cs @@ -126,7 +126,7 @@ public sealed class MultiModGroup(Mod mod) : IModGroup, ITexToolsGroup } } - public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) { foreach (var container in DataContainers) identifier.AddChangedItems(container, changedItems); diff --git a/Penumbra/Mods/Groups/SingleModGroup.cs b/Penumbra/Mods/Groups/SingleModGroup.cs index cc606f42..723cd5b1 100644 --- a/Penumbra/Mods/Groups/SingleModGroup.cs +++ b/Penumbra/Mods/Groups/SingleModGroup.cs @@ -111,7 +111,7 @@ public sealed class SingleModGroup(Mod mod) : IModGroup, ITexToolsGroup OptionData[setting.AsIndex].AddDataTo(redirections, manipulations); } - public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) + public void AddChangedItems(ObjectIdentification identifier, IDictionary changedItems) { foreach (var container in DataContainers) identifier.AddChangedItems(container, changedItems); diff --git a/Penumbra/Mods/ItemSwap/EquipmentSwap.cs b/Penumbra/Mods/ItemSwap/EquipmentSwap.cs index 2c292a14..1a2f2798 100644 --- a/Penumbra/Mods/ItemSwap/EquipmentSwap.cs +++ b/Penumbra/Mods/ItemSwap/EquipmentSwap.cs @@ -186,7 +186,7 @@ public static class EquipmentSwap PrimaryId idTo, byte mtrlTo) { var eqdpFromIdentifier = new EqdpIdentifier(idFrom, slotFrom, gr); - var eqdpToIdentifier = new EqdpIdentifier(idTo, slotTo, gr); + var eqdpToIdentifier = new EqdpIdentifier(idTo, slotTo, gr); var eqdpFromDefault = new EqdpEntryInternal(ExpandedEqdpFile.GetDefault(manager, eqdpFromIdentifier), slotFrom); var eqdpToDefault = new EqdpEntryInternal(ExpandedEqdpFile.GetDefault(manager, eqdpToIdentifier), slotTo); var meta = new MetaSwap(i => manips.TryGetValue(i, out var e) ? e : null, eqdpFromIdentifier, @@ -255,7 +255,8 @@ public static class EquipmentSwap { items = identifier.Identify(slotFrom.IsEquipment() ? GamePaths.Equipment.Mdl.Path(idFrom, GenderRace.MidlanderMale, slotFrom) - : GamePaths.Accessory.Mdl.Path(idFrom, GenderRace.MidlanderMale, slotFrom)).Select(kvp => kvp.Value).OfType() + : GamePaths.Accessory.Mdl.Path(idFrom, GenderRace.MidlanderMale, slotFrom)) + .Select(kvp => kvp.Value).OfType().Select(i => i.Item) .ToArray(); variants = Enumerable.Range(0, imc.Count + 1).Select(i => (Variant)i).ToArray(); } diff --git a/Penumbra/Mods/Mod.cs b/Penumbra/Mods/Mod.cs index 16f06de2..488e3dc1 100644 --- a/Penumbra/Mods/Mod.cs +++ b/Penumbra/Mods/Mod.cs @@ -1,5 +1,6 @@ using OtterGui; using OtterGui.Classes; +using Penumbra.GameData.Data; using Penumbra.Meta.Manipulations; using Penumbra.Mods.Editor; using Penumbra.Mods.Groups; @@ -100,7 +101,7 @@ public sealed class Mod : IMod } // Cache - public readonly SortedList ChangedItems = new(); + public readonly SortedList ChangedItems = new(); public string LowerChangedItemsString { get; internal set; } = string.Empty; public string AllTagsLower { get; internal set; } = string.Empty; diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index 8ea74987..dbe06803 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -21,6 +21,8 @@ using Penumbra.GameData.Enums; using Penumbra.UI; using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManager; using System.Xml.Linq; +using Dalamud.Plugin.Services; +using Penumbra.GameData.Data; using Penumbra.Interop.Hooks; using Penumbra.Interop.Hooks.ResourceLoading; @@ -109,16 +111,17 @@ public class Penumbra : IDalamudPlugin private void SetupApi() { _services.GetService(); + var itemSheet = _services.GetService().GetExcelSheet()!; _communicatorService.ChangedItemHover.Subscribe(it => { - if (it is (Item, FullEquipType)) + if (it is IdentifiedItem) ImGui.TextUnformatted("Left Click to create an item link in chat."); }, ChangedItemHover.Priority.Link); _communicatorService.ChangedItemClick.Subscribe((button, it) => { - if (button == MouseButton.Left && it is (Item item, FullEquipType type)) - Messager.LinkItem(item); + if (button == MouseButton.Left && it is IdentifiedItem item && itemSheet.GetRow(item.Item.ItemId.Id) is { } i) + Messager.LinkItem(i); }, ChangedItemClick.Priority.Link); } diff --git a/Penumbra/Services/ConfigMigrationService.cs b/Penumbra/Services/ConfigMigrationService.cs index 70b05a73..5ba57cf4 100644 --- a/Penumbra/Services/ConfigMigrationService.cs +++ b/Penumbra/Services/ConfigMigrationService.cs @@ -115,7 +115,7 @@ public class ConfigMigrationService(SaveService saveService, BackupService backu _data["ResourceWatcherRecordTypes"]?.ToObject() ?? _config.Ephemeral.ResourceWatcherRecordTypes; _config.Ephemeral.CollectionPanel = _data["CollectionPanel"]?.ToObject() ?? _config.Ephemeral.CollectionPanel; _config.Ephemeral.SelectedTab = _data["SelectedTab"]?.ToObject() ?? _config.Ephemeral.SelectedTab; - _config.Ephemeral.ChangedItemFilter = _data["ChangedItemFilter"]?.ToObject() + _config.Ephemeral.ChangedItemFilter = _data["ChangedItemFilter"]?.ToObject() ?? _config.Ephemeral.ChangedItemFilter; _config.Ephemeral.FixMainWindow = _data["FixMainWindow"]?.ToObject() ?? _config.Ephemeral.FixMainWindow; _config.Ephemeral.Save(); diff --git a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs index da2daeb7..b75c5aef 100644 --- a/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs +++ b/Penumbra/UI/AdvancedWindow/ItemSwapTab.cs @@ -135,7 +135,7 @@ public class ItemSwapTab : IDisposable, ITab, IUiService : FilterComboCache<(EquipItem Item, bool InMod)>(() => { var list = data.ByType[type]; - if (selector?.Selected is { } mod && mod.ChangedItems.Values.Any(o => o is EquipItem i && i.Type == type)) + if (selector?.Selected is { } mod && mod.ChangedItems.Values.Any(o => o is IdentifiedItem i && i.Item.Type == type)) return list.Select(i => (i, mod.ChangedItems.ContainsKey(i.Name))).OrderByDescending(p => p.Item2).ToList(); return list.Select(i => (i, false)).ToList(); diff --git a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs index c47414b9..9834d9f0 100644 --- a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs +++ b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs @@ -27,7 +27,7 @@ public class ResourceTreeViewer private readonly Dictionary _filterCache; private TreeCategory _categoryFilter; - private ChangedItemDrawer.ChangedItemIcon _typeFilter; + private ChangedItemIconFlag _typeFilter; private string _nameFilter; private string _nodeFilter; @@ -48,7 +48,7 @@ public class ResourceTreeViewer _filterCache = []; _categoryFilter = AllCategories; - _typeFilter = ChangedItemDrawer.AllFlags; + _typeFilter = ChangedItemFlagExtensions.AllFlags; _nameFilter = string.Empty; _nodeFilter = string.Empty; } @@ -185,13 +185,13 @@ public class ResourceTreeViewer }); private void DrawNodes(IEnumerable resourceNodes, int level, nint pathHash, - ChangedItemDrawer.ChangedItemIcon parentFilterIcon) + ChangedItemIconFlag parentFilterIconFlag) { var debugMode = _config.DebugMode; var frameHeight = ImGui.GetFrameHeight(); var cellHeight = _actionCapacity > 0 ? frameHeight : 0.0f; - bool MatchesFilter(ResourceNode node, ChangedItemDrawer.ChangedItemIcon filterIcon) + bool MatchesFilter(ResourceNode node, ChangedItemIconFlag filterIcon) { if (!_typeFilter.HasFlag(filterIcon)) return false; @@ -205,12 +205,12 @@ public class ResourceTreeViewer || Array.Exists(node.PossibleGamePaths, path => path.Path.ToString().Contains(_nodeFilter, StringComparison.OrdinalIgnoreCase)); } - NodeVisibility CalculateNodeVisibility(nint nodePathHash, ResourceNode node, ChangedItemDrawer.ChangedItemIcon parentFilterIcon) + NodeVisibility CalculateNodeVisibility(nint nodePathHash, ResourceNode node, ChangedItemIconFlag parentFilterIcon) { if (node.Internal && !debugMode) return NodeVisibility.Hidden; - var filterIcon = node.Icon != 0 ? node.Icon : parentFilterIcon; + var filterIcon = node.IconFlag != 0 ? node.IconFlag : parentFilterIcon; if (MatchesFilter(node, filterIcon)) return NodeVisibility.Visible; @@ -223,7 +223,7 @@ public class ResourceTreeViewer return NodeVisibility.Hidden; } - NodeVisibility GetNodeVisibility(nint nodePathHash, ResourceNode node, ChangedItemDrawer.ChangedItemIcon parentFilterIcon) + NodeVisibility GetNodeVisibility(nint nodePathHash, ResourceNode node, ChangedItemIconFlag parentFilterIcon) { if (!_filterCache.TryGetValue(nodePathHash, out var visibility)) { @@ -241,7 +241,7 @@ public class ResourceTreeViewer { var nodePathHash = unchecked(pathHash + resourceNode.ResourceHandle); - var visibility = GetNodeVisibility(nodePathHash, resourceNode, parentFilterIcon); + var visibility = GetNodeVisibility(nodePathHash, resourceNode, parentFilterIconFlag); if (visibility == NodeVisibility.Hidden) continue; @@ -250,7 +250,7 @@ public class ResourceTreeViewer using var mutedColor = ImRaii.PushColor(ImGuiCol.Text, textColorInternal, resourceNode.Internal); - var filterIcon = resourceNode.Icon != 0 ? resourceNode.Icon : parentFilterIcon; + var filterIcon = resourceNode.IconFlag != 0 ? resourceNode.IconFlag : parentFilterIconFlag; using var id = ImRaii.PushId(index); ImGui.TableNextColumn(); @@ -281,7 +281,7 @@ public class ResourceTreeViewer ImGui.SameLine(0f, ImGui.GetStyle().ItemInnerSpacing.X); } - _changedItemDrawer.DrawCategoryIcon(resourceNode.Icon); + _changedItemDrawer.DrawCategoryIcon(resourceNode.IconFlag); ImGui.SameLine(0f, ImGui.GetStyle().ItemInnerSpacing.X); ImGui.TableHeader(resourceNode.Name); if (ImGui.IsItemClicked() && unfoldable) diff --git a/Penumbra/UI/ChangedItemDrawer.cs b/Penumbra/UI/ChangedItemDrawer.cs index 72bfa266..af9782d5 100644 --- a/Penumbra/UI/ChangedItemDrawer.cs +++ b/Penumbra/UI/ChangedItemDrawer.cs @@ -3,72 +3,24 @@ using Dalamud.Interface.Textures; using Dalamud.Interface.Textures.TextureWraps; using Dalamud.Plugin.Services; using Dalamud.Utility; -using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using ImGuiNET; using Lumina.Data.Files; -using Lumina.Excel; -using Lumina.Excel.GeneratedSheets; using OtterGui; using OtterGui.Classes; using OtterGui.Raii; using OtterGui.Services; using Penumbra.Api.Enums; -using Penumbra.GameData.Enums; -using Penumbra.GameData.Structs; +using Penumbra.GameData.Data; using Penumbra.Services; using Penumbra.UI.Classes; -using ApiChangedItemIcon = Penumbra.Api.Enums.ChangedItemIcon; namespace Penumbra.UI; public class ChangedItemDrawer : IDisposable, IUiService { - [Flags] - public enum ChangedItemIcon : uint - { - Head = 0x00_00_01, - Body = 0x00_00_02, - Hands = 0x00_00_04, - Legs = 0x00_00_08, - Feet = 0x00_00_10, - Ears = 0x00_00_20, - Neck = 0x00_00_40, - Wrists = 0x00_00_80, - Finger = 0x00_01_00, - Monster = 0x00_02_00, - Demihuman = 0x00_04_00, - Customization = 0x00_08_00, - Action = 0x00_10_00, - Mainhand = 0x00_20_00, - Offhand = 0x00_40_00, - Unknown = 0x00_80_00, - Emote = 0x01_00_00, - } + private static readonly string[] LowerNames = ChangedItemFlagExtensions.Order.Select(f => f.ToDescription().ToLowerInvariant()).ToArray(); - private static readonly ChangedItemIcon[] Order = - [ - ChangedItemIcon.Head, - ChangedItemIcon.Body, - ChangedItemIcon.Hands, - ChangedItemIcon.Legs, - ChangedItemIcon.Feet, - ChangedItemIcon.Ears, - ChangedItemIcon.Neck, - ChangedItemIcon.Wrists, - ChangedItemIcon.Finger, - ChangedItemIcon.Mainhand, - ChangedItemIcon.Offhand, - ChangedItemIcon.Customization, - ChangedItemIcon.Action, - ChangedItemIcon.Emote, - ChangedItemIcon.Monster, - ChangedItemIcon.Demihuman, - ChangedItemIcon.Unknown, - ]; - - private static readonly string[] LowerNames = Order.Select(f => ToDescription(f).ToLowerInvariant()).ToArray(); - - public static bool TryParseIndex(ReadOnlySpan input, out ChangedItemIcon slot) + public static bool TryParseIndex(ReadOnlySpan input, out ChangedItemIconFlag slot) { // Handle numeric cases before TryParse because numbers // are not logical otherwise. @@ -77,15 +29,15 @@ public class ChangedItemDrawer : IDisposable, IUiService // We assume users will use 1-based index, but if they enter 0, just use the first. if (idx == 0) { - slot = Order[0]; + slot = ChangedItemFlagExtensions.Order[0]; return true; } // Use 1-based index. --idx; - if (idx >= 0 && idx < Order.Length) + if (idx >= 0 && idx < ChangedItemFlagExtensions.Order.Count) { - slot = Order[idx]; + slot = ChangedItemFlagExtensions.Order[idx]; return true; } } @@ -94,13 +46,13 @@ public class ChangedItemDrawer : IDisposable, IUiService return false; } - public static bool TryParsePartial(string lowerInput, out ChangedItemIcon slot) + public static bool TryParsePartial(string lowerInput, out ChangedItemIconFlag slot) { if (TryParseIndex(lowerInput, out slot)) return true; slot = 0; - foreach (var (item, flag) in LowerNames.Zip(Order)) + foreach (var (item, flag) in LowerNames.Zip(ChangedItemFlagExtensions.Order)) { if (item.Contains(lowerInput, StringComparison.Ordinal)) slot |= flag; @@ -109,15 +61,11 @@ public class ChangedItemDrawer : IDisposable, IUiService return slot != 0; } - public const ChangedItemIcon AllFlags = (ChangedItemIcon)0x01FFFF; - public static readonly int NumCategories = Order.Length; - public const ChangedItemIcon DefaultFlags = AllFlags & ~ChangedItemIcon.Offhand; - private readonly Configuration _config; - private readonly ExcelSheet _items; - private readonly CommunicatorService _communicator; - private readonly Dictionary _icons = new(16); - private float _smallestIconWidth; + private readonly Configuration _config; + private readonly CommunicatorService _communicator; + private readonly Dictionary _icons = new(16); + private float _smallestIconWidth; public static Vector2 TypeFilterIconSize => new(2 * ImGui.GetTextLineHeight()); @@ -125,7 +73,6 @@ public class ChangedItemDrawer : IDisposable, IUiService public ChangedItemDrawer(IUiBuilder uiBuilder, IDataManager gameData, ITextureProvider textureProvider, CommunicatorService communicator, Configuration config) { - _items = gameData.GetExcelSheet()!; uiBuilder.RunWhenUiPrepared(() => CreateEquipSlotIcons(uiBuilder, gameData, textureProvider), true); _communicator = communicator; _config = config; @@ -139,18 +86,19 @@ public class ChangedItemDrawer : IDisposable, IUiService } /// Check if a changed item should be drawn based on its category. - public bool FilterChangedItem(string name, object? data, LowerString filter) - => (_config.Ephemeral.ChangedItemFilter == AllFlags || _config.Ephemeral.ChangedItemFilter.HasFlag(GetCategoryIcon(name, data))) - && (filter.IsEmpty || filter.IsContained(ChangedItemFilterName(name, data))); + public bool FilterChangedItem(string name, IIdentifiedObjectData? data, LowerString filter) + => (_config.Ephemeral.ChangedItemFilter == ChangedItemFlagExtensions.AllFlags + || _config.Ephemeral.ChangedItemFilter.HasFlag(data.GetIcon().ToFlag())) + && (filter.IsEmpty || !data.IsFilteredOut(name, filter)); /// Draw the icon corresponding to the category of a changed item. - public void DrawCategoryIcon(string name, object? data) - => DrawCategoryIcon(GetCategoryIcon(name, data)); + public void DrawCategoryIcon(IIdentifiedObjectData? data) + => DrawCategoryIcon(data.GetIcon().ToFlag()); - public void DrawCategoryIcon(ChangedItemIcon iconType) + public void DrawCategoryIcon(ChangedItemIconFlag iconFlagType) { var height = ImGui.GetFrameHeight(); - if (!_icons.TryGetValue(iconType, out var icon)) + if (!_icons.TryGetValue(iconFlagType, out var icon)) { ImGui.Dummy(new Vector2(height)); return; @@ -162,18 +110,18 @@ public class ChangedItemDrawer : IDisposable, IUiService using var tt = ImRaii.Tooltip(); ImGui.Image(icon.ImGuiHandle, new Vector2(_smallestIconWidth)); ImGui.SameLine(); - ImGuiUtil.DrawTextButton(ToDescription(iconType), new Vector2(0, _smallestIconWidth), 0); + ImGuiUtil.DrawTextButton(iconFlagType.ToDescription(), new Vector2(0, _smallestIconWidth), 0); } } /// /// Draw a changed item, invoking the Api-Events for clicks and tooltips. - /// Also draw the item Id in grey if requested. + /// Also draw the item ID in grey if requested. /// - public void DrawChangedItem(string name, object? data) + public void DrawChangedItem(string name, IIdentifiedObjectData? data) { - name = ChangedItemName(name, data); - using (var style = ImRaii.PushStyle(ImGuiStyleVar.SelectableTextAlign, new Vector2(0, 0.5f)) + name = data?.ToName(name) ?? name; + using (ImRaii.PushStyle(ImGuiStyleVar.SelectableTextAlign, new Vector2(0, 0.5f)) .Push(ImGuiStyleVar.ItemSpacing, new Vector2(ImGui.GetStyle().ItemSpacing.X, ImGui.GetStyle().CellPadding.Y * 2))) { var ret = ImGui.Selectable(name, false, ImGuiSelectableFlags.None, new Vector2(0, ImGui.GetFrameHeight())) @@ -182,31 +130,34 @@ public class ChangedItemDrawer : IDisposable, IUiService ret = ImGui.IsItemClicked(ImGuiMouseButton.Right) ? MouseButton.Right : ret; ret = ImGui.IsItemClicked(ImGuiMouseButton.Middle) ? MouseButton.Middle : ret; if (ret != MouseButton.None) - _communicator.ChangedItemClick.Invoke(ret, Convert(data)); + _communicator.ChangedItemClick.Invoke(ret, data); } if (_communicator.ChangedItemHover.HasTooltip && ImGui.IsItemHovered()) { // We can not be sure that any subscriber actually prints something in any case. // Circumvent ugly blank tooltip with less-ugly useless tooltip. - using var tt = ImRaii.Tooltip(); - using var group = ImRaii.Group(); - _communicator.ChangedItemHover.Invoke(Convert(data)); - group.Dispose(); + using var tt = ImRaii.Tooltip(); + using (ImRaii.Group()) + { + _communicator.ChangedItemHover.Invoke(data); + } + if (ImGui.GetItemRectSize() == Vector2.Zero) ImGui.TextUnformatted("No actions available."); } } /// Draw the model information, right-justified. - public void DrawModelData(object? data) + public static void DrawModelData(IIdentifiedObjectData? data) { - if (!GetChangedItemObject(data, out var text)) + var additionalData = data?.AdditionalData ?? string.Empty; + if (additionalData.Length == 0) return; ImGui.SameLine(ImGui.GetContentRegionAvail().X); ImGui.AlignTextToFramePadding(); - ImGuiUtil.RightJustify(text, ColorId.ItemId.Value()); + ImGuiUtil.RightJustify(additionalData, ColorId.ItemId.Value()); } /// Draw a header line with the different icon types to filter them. @@ -224,7 +175,7 @@ public class ChangedItemDrawer : IDisposable, IUiService } /// Draw a header line with the different icon types to filter them. - public bool DrawTypeFilter(ref ChangedItemIcon typeFilter) + public bool DrawTypeFilter(ref ChangedItemIconFlag typeFilter) { var ret = false; using var _ = ImRaii.PushId("ChangedItemIconFilter"); @@ -232,16 +183,38 @@ public class ChangedItemDrawer : IDisposable, IUiService using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, Vector2.Zero); - bool DrawIcon(ChangedItemIcon type, ref ChangedItemIcon typeFilter) + foreach (var iconType in ChangedItemFlagExtensions.Order) { - var ret = false; - var icon = _icons[type]; - var flag = typeFilter.HasFlag(type); + ret |= DrawIcon(iconType, ref typeFilter); + ImGui.SameLine(); + } + + ImGui.SetCursorPosX(ImGui.GetContentRegionMax().X - size.X); + ImGui.Image(_icons[ChangedItemFlagExtensions.AllFlags].ImGuiHandle, size, Vector2.Zero, Vector2.One, + typeFilter switch + { + 0 => new Vector4(0.6f, 0.3f, 0.3f, 1f), + ChangedItemFlagExtensions.AllFlags => new Vector4(0.75f, 0.75f, 0.75f, 1f), + _ => new Vector4(0.5f, 0.5f, 1f, 1f), + }); + if (ImGui.IsItemClicked()) + { + typeFilter = typeFilter == ChangedItemFlagExtensions.AllFlags ? 0 : ChangedItemFlagExtensions.AllFlags; + ret = true; + } + + return ret; + + bool DrawIcon(ChangedItemIconFlag type, ref ChangedItemIconFlag typeFilter) + { + var localRet = false; + var icon = _icons[type]; + var flag = typeFilter.HasFlag(type); ImGui.Image(icon.ImGuiHandle, size, Vector2.Zero, Vector2.One, flag ? Vector4.One : new Vector4(0.6f, 0.3f, 0.3f, 1f)); if (ImGui.IsItemClicked(ImGuiMouseButton.Left)) { typeFilter = flag ? typeFilter & ~type : typeFilter | type; - ret = true; + localRet = true; } using var popup = ImRaii.ContextPopupItem(type.ToString()); @@ -249,7 +222,7 @@ public class ChangedItemDrawer : IDisposable, IUiService if (ImGui.MenuItem("Enable Only This")) { typeFilter = type; - ret = true; + localRet = true; ImGui.CloseCurrentPopup(); } @@ -258,165 +231,13 @@ public class ChangedItemDrawer : IDisposable, IUiService using var tt = ImRaii.Tooltip(); ImGui.Image(icon.ImGuiHandle, new Vector2(_smallestIconWidth)); ImGui.SameLine(); - ImGuiUtil.DrawTextButton(ToDescription(type), new Vector2(0, _smallestIconWidth), 0); + ImGuiUtil.DrawTextButton(type.ToDescription(), new Vector2(0, _smallestIconWidth), 0); } - return ret; - } - - foreach (var iconType in Order) - { - ret |= DrawIcon(iconType, ref typeFilter); - ImGui.SameLine(); - } - - ImGui.SetCursorPosX(ImGui.GetContentRegionMax().X - size.X); - ImGui.Image(_icons[AllFlags].ImGuiHandle, size, Vector2.Zero, Vector2.One, - typeFilter == 0 ? new Vector4(0.6f, 0.3f, 0.3f, 1f) : - typeFilter == AllFlags ? new Vector4(0.75f, 0.75f, 0.75f, 1f) : new Vector4(0.5f, 0.5f, 1f, 1f)); - if (ImGui.IsItemClicked()) - { - typeFilter = typeFilter == AllFlags ? 0 : AllFlags; - ret = true; - } - - return ret; - } - - /// Obtain the icon category corresponding to a changed item. - internal static ChangedItemIcon GetCategoryIcon(string name, object? obj) - { - var iconType = ChangedItemIcon.Unknown; - switch (obj) - { - case EquipItem it: - iconType = GetCategoryIcon(it.Type.ToSlot()); - break; - case ModelChara m: - iconType = (CharacterBase.ModelType)m.Type switch - { - CharacterBase.ModelType.DemiHuman => ChangedItemIcon.Demihuman, - CharacterBase.ModelType.Monster => ChangedItemIcon.Monster, - _ => ChangedItemIcon.Unknown, - }; - break; - default: - { - if (name.StartsWith("Action: ")) - iconType = ChangedItemIcon.Action; - else if (name.StartsWith("Emote: ")) - iconType = ChangedItemIcon.Emote; - else if (name.StartsWith("Customization: ")) - iconType = ChangedItemIcon.Customization; - break; - } - } - - return iconType; - } - - internal static ChangedItemIcon GetCategoryIcon(EquipSlot slot) - => slot switch - { - EquipSlot.MainHand => ChangedItemIcon.Mainhand, - EquipSlot.OffHand => ChangedItemIcon.Offhand, - EquipSlot.Head => ChangedItemIcon.Head, - EquipSlot.Body => ChangedItemIcon.Body, - EquipSlot.Hands => ChangedItemIcon.Hands, - EquipSlot.Legs => ChangedItemIcon.Legs, - EquipSlot.Feet => ChangedItemIcon.Feet, - EquipSlot.Ears => ChangedItemIcon.Ears, - EquipSlot.Neck => ChangedItemIcon.Neck, - EquipSlot.Wrists => ChangedItemIcon.Wrists, - EquipSlot.RFinger => ChangedItemIcon.Finger, - _ => ChangedItemIcon.Unknown, - }; - - /// Return more detailed object information in text, if it exists. - private static bool GetChangedItemObject(object? obj, out string text) - { - switch (obj) - { - case EquipItem it: - text = it.ModelString; - return true; - case ModelChara m: - text = $"({((CharacterBase.ModelType)m.Type).ToName()} {m.Model}-{m.Base}-{m.Variant})"; - return true; - default: - text = string.Empty; - return false; + return localRet; } } - /// We need to transform the internal EquipItem type to the Lumina Item type for API-events. - private object? Convert(object? data) - { - if (data is EquipItem it) - return (_items.GetRow(it.ItemId.Id), it.Type); - - return data; - } - - private static string ToDescription(ChangedItemIcon icon) - => icon switch - { - ChangedItemIcon.Head => EquipSlot.Head.ToName(), - ChangedItemIcon.Body => EquipSlot.Body.ToName(), - ChangedItemIcon.Hands => EquipSlot.Hands.ToName(), - ChangedItemIcon.Legs => EquipSlot.Legs.ToName(), - ChangedItemIcon.Feet => EquipSlot.Feet.ToName(), - ChangedItemIcon.Ears => EquipSlot.Ears.ToName(), - ChangedItemIcon.Neck => EquipSlot.Neck.ToName(), - ChangedItemIcon.Wrists => EquipSlot.Wrists.ToName(), - ChangedItemIcon.Finger => "Ring", - ChangedItemIcon.Monster => "Monster", - ChangedItemIcon.Demihuman => "Demi-Human", - ChangedItemIcon.Customization => "Customization", - ChangedItemIcon.Action => "Action", - ChangedItemIcon.Emote => "Emote", - ChangedItemIcon.Mainhand => "Weapon (Mainhand)", - ChangedItemIcon.Offhand => "Weapon (Offhand)", - _ => "Other", - }; - - internal static ApiChangedItemIcon ToApiIcon(ChangedItemIcon icon) - => icon switch - { - ChangedItemIcon.Head => ApiChangedItemIcon.Head, - ChangedItemIcon.Body => ApiChangedItemIcon.Body, - ChangedItemIcon.Hands => ApiChangedItemIcon.Hands, - ChangedItemIcon.Legs => ApiChangedItemIcon.Legs, - ChangedItemIcon.Feet => ApiChangedItemIcon.Feet, - ChangedItemIcon.Ears => ApiChangedItemIcon.Ears, - ChangedItemIcon.Neck => ApiChangedItemIcon.Neck, - ChangedItemIcon.Wrists => ApiChangedItemIcon.Wrists, - ChangedItemIcon.Finger => ApiChangedItemIcon.Finger, - ChangedItemIcon.Monster => ApiChangedItemIcon.Monster, - ChangedItemIcon.Demihuman => ApiChangedItemIcon.Demihuman, - ChangedItemIcon.Customization => ApiChangedItemIcon.Customization, - ChangedItemIcon.Action => ApiChangedItemIcon.Action, - ChangedItemIcon.Emote => ApiChangedItemIcon.Emote, - ChangedItemIcon.Mainhand => ApiChangedItemIcon.Mainhand, - ChangedItemIcon.Offhand => ApiChangedItemIcon.Offhand, - ChangedItemIcon.Unknown => ApiChangedItemIcon.Unknown, - _ => ApiChangedItemIcon.None, - }; - - /// Apply Changed Item Counters to the Name if necessary. - private static string ChangedItemName(string name, object? data) - => data is int counter ? $"{counter} Files Manipulating {name}s" : name; - - /// Add filterable information to the string. - private static string ChangedItemFilterName(string name, object? data) - => data switch - { - int counter => $"{counter} Files Manipulating {name}s", - EquipItem it => $"{name}\0{(GetChangedItemObject(it, out var t) ? t : string.Empty)}", - ModelChara m => $"{name}\0{(GetChangedItemObject(m, out var t) ? t : string.Empty)}", - _ => name, - }; - /// Initialize the icons. private bool CreateEquipSlotIcons(IUiBuilder uiBuilder, IDataManager gameData, ITextureProvider textureProvider) { @@ -425,30 +246,30 @@ public class ChangedItemDrawer : IDisposable, IUiService if (!equipTypeIcons.Valid) return false; - void Add(ChangedItemIcon icon, IDalamudTextureWrap? tex) + void Add(ChangedItemIconFlag icon, IDalamudTextureWrap? tex) { if (tex != null) _icons.Add(icon, tex); } - Add(ChangedItemIcon.Mainhand, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 0)); - Add(ChangedItemIcon.Head, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 1)); - Add(ChangedItemIcon.Body, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 2)); - Add(ChangedItemIcon.Hands, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 3)); - Add(ChangedItemIcon.Legs, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 5)); - Add(ChangedItemIcon.Feet, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 6)); - Add(ChangedItemIcon.Offhand, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 7)); - Add(ChangedItemIcon.Ears, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 8)); - Add(ChangedItemIcon.Neck, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 9)); - Add(ChangedItemIcon.Wrists, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 10)); - Add(ChangedItemIcon.Finger, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 11)); - Add(ChangedItemIcon.Monster, textureProvider.CreateFromTexFile(gameData.GetFile("ui/icon/062000/062042_hr1.tex")!)); - Add(ChangedItemIcon.Demihuman, textureProvider.CreateFromTexFile(gameData.GetFile("ui/icon/062000/062041_hr1.tex")!)); - Add(ChangedItemIcon.Customization, textureProvider.CreateFromTexFile(gameData.GetFile("ui/icon/062000/062043_hr1.tex")!)); - Add(ChangedItemIcon.Action, textureProvider.CreateFromTexFile(gameData.GetFile("ui/icon/062000/062001_hr1.tex")!)); - Add(ChangedItemIcon.Emote, LoadEmoteTexture(gameData, textureProvider)); - Add(ChangedItemIcon.Unknown, LoadUnknownTexture(gameData, textureProvider)); - Add(AllFlags, textureProvider.CreateFromTexFile(gameData.GetFile("ui/icon/114000/114052_hr1.tex")!)); + Add(ChangedItemIconFlag.Mainhand, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 0)); + Add(ChangedItemIconFlag.Head, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 1)); + Add(ChangedItemIconFlag.Body, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 2)); + Add(ChangedItemIconFlag.Hands, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 3)); + Add(ChangedItemIconFlag.Legs, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 5)); + Add(ChangedItemIconFlag.Feet, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 6)); + Add(ChangedItemIconFlag.Offhand, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 7)); + Add(ChangedItemIconFlag.Ears, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 8)); + Add(ChangedItemIconFlag.Neck, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 9)); + Add(ChangedItemIconFlag.Wrists, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 10)); + Add(ChangedItemIconFlag.Finger, equipTypeIcons.LoadTexturePart("ui/uld/ArmouryBoard_hr1.tex", 11)); + Add(ChangedItemIconFlag.Monster, textureProvider.CreateFromTexFile(gameData.GetFile("ui/icon/062000/062044_hr1.tex")!)); + Add(ChangedItemIconFlag.Demihuman, textureProvider.CreateFromTexFile(gameData.GetFile("ui/icon/062000/062043_hr1.tex")!)); + Add(ChangedItemIconFlag.Customization, textureProvider.CreateFromTexFile(gameData.GetFile("ui/icon/062000/062045_hr1.tex")!)); + Add(ChangedItemIconFlag.Action, textureProvider.CreateFromTexFile(gameData.GetFile("ui/icon/062000/062001_hr1.tex")!)); + Add(ChangedItemIconFlag.Emote, LoadEmoteTexture(gameData, textureProvider)); + Add(ChangedItemIconFlag.Unknown, LoadUnknownTexture(gameData, textureProvider)); + Add(ChangedItemFlagExtensions.AllFlags, textureProvider.CreateFromTexFile(gameData.GetFile("ui/icon/114000/114052_hr1.tex")!)); _smallestIconWidth = _icons.Values.Min(i => i.Width); @@ -487,6 +308,7 @@ public class ChangedItemDrawer : IDisposable, IUiService } } - return textureProvider.CreateFromRaw(RawImageSpecification.Rgba32(emote.Header.Width, emote.Header.Height), image2, "Penumbra.EmoteItemIcon"); + return textureProvider.CreateFromRaw(RawImageSpecification.Rgba32(emote.Header.Width, emote.Header.Height), image2, + "Penumbra.EmoteItemIcon"); } } diff --git a/Penumbra/UI/ChangedItemIconFlag.cs b/Penumbra/UI/ChangedItemIconFlag.cs new file mode 100644 index 00000000..fc7073f2 --- /dev/null +++ b/Penumbra/UI/ChangedItemIconFlag.cs @@ -0,0 +1,122 @@ +using Penumbra.Api.Enums; +using Penumbra.GameData.Enums; + +namespace Penumbra.UI; + +[Flags] +public enum ChangedItemIconFlag : uint +{ + Head = 0x00_00_01, + Body = 0x00_00_02, + Hands = 0x00_00_04, + Legs = 0x00_00_08, + Feet = 0x00_00_10, + Ears = 0x00_00_20, + Neck = 0x00_00_40, + Wrists = 0x00_00_80, + Finger = 0x00_01_00, + Monster = 0x00_02_00, + Demihuman = 0x00_04_00, + Customization = 0x00_08_00, + Action = 0x00_10_00, + Mainhand = 0x00_20_00, + Offhand = 0x00_40_00, + Unknown = 0x00_80_00, + Emote = 0x01_00_00, +} + +public static class ChangedItemFlagExtensions +{ + public static readonly IReadOnlyList Order = + [ + ChangedItemIconFlag.Head, + ChangedItemIconFlag.Body, + ChangedItemIconFlag.Hands, + ChangedItemIconFlag.Legs, + ChangedItemIconFlag.Feet, + ChangedItemIconFlag.Ears, + ChangedItemIconFlag.Neck, + ChangedItemIconFlag.Wrists, + ChangedItemIconFlag.Finger, + ChangedItemIconFlag.Mainhand, + ChangedItemIconFlag.Offhand, + ChangedItemIconFlag.Customization, + ChangedItemIconFlag.Action, + ChangedItemIconFlag.Emote, + ChangedItemIconFlag.Monster, + ChangedItemIconFlag.Demihuman, + ChangedItemIconFlag.Unknown, + ]; + + public const ChangedItemIconFlag AllFlags = (ChangedItemIconFlag)0x01FFFF; + public static readonly int NumCategories = Order.Count; + public const ChangedItemIconFlag DefaultFlags = AllFlags & ~ChangedItemIconFlag.Offhand; + + public static string ToDescription(this ChangedItemIconFlag iconFlag) + => iconFlag switch + { + ChangedItemIconFlag.Head => EquipSlot.Head.ToName(), + ChangedItemIconFlag.Body => EquipSlot.Body.ToName(), + ChangedItemIconFlag.Hands => EquipSlot.Hands.ToName(), + ChangedItemIconFlag.Legs => EquipSlot.Legs.ToName(), + ChangedItemIconFlag.Feet => EquipSlot.Feet.ToName(), + ChangedItemIconFlag.Ears => EquipSlot.Ears.ToName(), + ChangedItemIconFlag.Neck => EquipSlot.Neck.ToName(), + ChangedItemIconFlag.Wrists => EquipSlot.Wrists.ToName(), + ChangedItemIconFlag.Finger => "Ring", + ChangedItemIconFlag.Monster => "Monster", + ChangedItemIconFlag.Demihuman => "Demi-Human", + ChangedItemIconFlag.Customization => "Customization", + ChangedItemIconFlag.Action => "Action", + ChangedItemIconFlag.Emote => "Emote", + ChangedItemIconFlag.Mainhand => "Weapon (Mainhand)", + ChangedItemIconFlag.Offhand => "Weapon (Offhand)", + _ => "Other", + }; + + public static ChangedItemIcon ToApiIcon(this ChangedItemIconFlag iconFlag) + => iconFlag switch + { + ChangedItemIconFlag.Head => ChangedItemIcon.Head, + ChangedItemIconFlag.Body => ChangedItemIcon.Body, + ChangedItemIconFlag.Hands => ChangedItemIcon.Hands, + ChangedItemIconFlag.Legs => ChangedItemIcon.Legs, + ChangedItemIconFlag.Feet => ChangedItemIcon.Feet, + ChangedItemIconFlag.Ears => ChangedItemIcon.Ears, + ChangedItemIconFlag.Neck => ChangedItemIcon.Neck, + ChangedItemIconFlag.Wrists => ChangedItemIcon.Wrists, + ChangedItemIconFlag.Finger => ChangedItemIcon.Finger, + ChangedItemIconFlag.Monster => ChangedItemIcon.Monster, + ChangedItemIconFlag.Demihuman => ChangedItemIcon.Demihuman, + ChangedItemIconFlag.Customization => ChangedItemIcon.Customization, + ChangedItemIconFlag.Action => ChangedItemIcon.Action, + ChangedItemIconFlag.Emote => ChangedItemIcon.Emote, + ChangedItemIconFlag.Mainhand => ChangedItemIcon.Mainhand, + ChangedItemIconFlag.Offhand => ChangedItemIcon.Offhand, + ChangedItemIconFlag.Unknown => ChangedItemIcon.Unknown, + _ => ChangedItemIcon.None, + }; + + public static ChangedItemIconFlag ToFlag(this ChangedItemIcon icon) + => icon switch + { + ChangedItemIcon.Unknown => ChangedItemIconFlag.Unknown, + ChangedItemIcon.Head => ChangedItemIconFlag.Head, + ChangedItemIcon.Body => ChangedItemIconFlag.Body, + ChangedItemIcon.Hands => ChangedItemIconFlag.Hands, + ChangedItemIcon.Legs => ChangedItemIconFlag.Legs, + ChangedItemIcon.Feet => ChangedItemIconFlag.Feet, + ChangedItemIcon.Ears => ChangedItemIconFlag.Ears, + ChangedItemIcon.Neck => ChangedItemIconFlag.Neck, + ChangedItemIcon.Wrists => ChangedItemIconFlag.Wrists, + ChangedItemIcon.Finger => ChangedItemIconFlag.Finger, + ChangedItemIcon.Mainhand => ChangedItemIconFlag.Mainhand, + ChangedItemIcon.Offhand => ChangedItemIconFlag.Offhand, + ChangedItemIcon.Customization => ChangedItemIconFlag.Customization, + ChangedItemIcon.Monster => ChangedItemIconFlag.Monster, + ChangedItemIcon.Demihuman => ChangedItemIconFlag.Demihuman, + ChangedItemIcon.Action => ChangedItemIconFlag.Action, + ChangedItemIcon.Emote => ChangedItemIconFlag.Emote, + _ => ChangedItemIconFlag.Unknown, + }; +} diff --git a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs index 55405313..42689efb 100644 --- a/Penumbra/UI/ModsTab/ModFileSystemSelector.cs +++ b/Penumbra/UI/ModsTab/ModFileSystemSelector.cs @@ -550,7 +550,7 @@ public sealed class ModFileSystemSelector : FileSystemSelector)selector.Selected!.ChangedItems); + var zipList = ZipList.FromSortedList(selector.Selected!.ChangedItems); var height = ImGui.GetFrameHeightWithSpacing(); ImGui.TableNextColumn(); var skips = ImGuiClip.GetNecessarySkips(height); @@ -32,15 +33,15 @@ public class ModPanelChangedItemsTab(ModFileSystemSelector selector, ChangedItem ImGuiClip.DrawEndDummy(remainder, height); } - private bool CheckFilter((string Name, object? Data) kvp) + private bool CheckFilter((string Name, IIdentifiedObjectData? Data) kvp) => drawer.FilterChangedItem(kvp.Name, kvp.Data, LowerString.Empty); - private void DrawChangedItem((string Name, object? Data) kvp) + private void DrawChangedItem((string Name, IIdentifiedObjectData? Data) kvp) { ImGui.TableNextColumn(); - drawer.DrawCategoryIcon(kvp.Name, kvp.Data); + drawer.DrawCategoryIcon(kvp.Data); ImGui.SameLine(); drawer.DrawChangedItem(kvp.Name, kvp.Data); - drawer.DrawModelData(kvp.Data); + ChangedItemDrawer.DrawModelData(kvp.Data); } } diff --git a/Penumbra/UI/ModsTab/ModSearchStringSplitter.cs b/Penumbra/UI/ModsTab/ModSearchStringSplitter.cs index e7550eea..1eff1919 100644 --- a/Penumbra/UI/ModsTab/ModSearchStringSplitter.cs +++ b/Penumbra/UI/ModsTab/ModSearchStringSplitter.cs @@ -19,16 +19,16 @@ public sealed class ModSearchStringSplitter : SearchStringSplitter { - public string Needle { get; init; } - public ModSearchType Type { get; init; } - public ChangedItemDrawer.ChangedItemIcon IconFilter { get; init; } + public string Needle { get; init; } + public ModSearchType Type { get; init; } + public ChangedItemIconFlag IconFlagFilter { get; init; } public bool Contains(Entry other) { if (Type != other.Type) return false; if (Type is ModSearchType.Category) - return IconFilter == other.IconFilter; + return IconFlagFilter == other.IconFlagFilter; return Needle.Contains(other.Needle); } @@ -77,7 +77,7 @@ public sealed class ModSearchStringSplitter : SearchStringSplitter leaf.Value.Name.Lower.AsSpan().Contains(entry.Needle, StringComparison.Ordinal), ModSearchType.Author => leaf.Value.Author.Lower.AsSpan().Contains(entry.Needle, StringComparison.Ordinal), ModSearchType.Category => leaf.Value.ChangedItems.Any(p - => (ChangedItemDrawer.GetCategoryIcon(p.Key, p.Value) & entry.IconFilter) != 0), + => ((p.Value?.Icon.ToFlag() ?? ChangedItemIconFlag.Unknown) & entry.IconFlagFilter) != 0), _ => true, }; diff --git a/Penumbra/UI/Tabs/ChangedItemsTab.cs b/Penumbra/UI/Tabs/ChangedItemsTab.cs index 2aeaaea0..256b0d79 100644 --- a/Penumbra/UI/Tabs/ChangedItemsTab.cs +++ b/Penumbra/UI/Tabs/ChangedItemsTab.cs @@ -6,6 +6,7 @@ using OtterGui.Services; using OtterGui.Widgets; using Penumbra.Api.Enums; using Penumbra.Collections.Manager; +using Penumbra.GameData.Data; using Penumbra.Mods; using Penumbra.Mods.Editor; using Penumbra.Services; @@ -66,22 +67,22 @@ public class ChangedItemsTab( } /// Apply the current filters. - private bool FilterChangedItem(KeyValuePair, object?)> item) + private bool FilterChangedItem(KeyValuePair, IIdentifiedObjectData?)> item) => drawer.FilterChangedItem(item.Key, item.Value.Item2, _changedItemFilter) && (_changedItemModFilter.IsEmpty || item.Value.Item1.Any(m => m.Name.Contains(_changedItemModFilter))); /// Draw a full column for a changed item. - private void DrawChangedItemColumn(KeyValuePair, object?)> item) + private void DrawChangedItemColumn(KeyValuePair, IIdentifiedObjectData?)> item) { ImGui.TableNextColumn(); - drawer.DrawCategoryIcon(item.Key, item.Value.Item2); + drawer.DrawCategoryIcon(item.Value.Item2); ImGui.SameLine(); drawer.DrawChangedItem(item.Key, item.Value.Item2); ImGui.TableNextColumn(); DrawModColumn(item.Value.Item1); ImGui.TableNextColumn(); - drawer.DrawModelData(item.Value.Item2); + ChangedItemDrawer.DrawModelData(item.Value.Item2); } private void DrawModColumn(SingleArray mods) diff --git a/Penumbra/UI/Tabs/SettingsTab.cs b/Penumbra/UI/Tabs/SettingsTab.cs index ab47ce7c..6c36e49a 100644 --- a/Penumbra/UI/Tabs/SettingsTab.cs +++ b/Penumbra/UI/Tabs/SettingsTab.cs @@ -429,7 +429,7 @@ public class SettingsTab : ITab, IUiService _config.HideChangedItemFilters = v; if (v) { - _config.Ephemeral.ChangedItemFilter = ChangedItemDrawer.AllFlags; + _config.Ephemeral.ChangedItemFilter = ChangedItemFlagExtensions.AllFlags; _config.Ephemeral.Save(); } }); diff --git a/Penumbra/Util/IdentifierExtensions.cs b/Penumbra/Util/IdentifierExtensions.cs index cb43ac06..5bd3f77c 100644 --- a/Penumbra/Util/IdentifierExtensions.cs +++ b/Penumbra/Util/IdentifierExtensions.cs @@ -10,7 +10,7 @@ namespace Penumbra.Util; public static class IdentifierExtensions { public static void AddChangedItems(this ObjectIdentification identifier, IModDataContainer container, - IDictionary changedItems) + IDictionary changedItems) { foreach (var gamePath in container.Files.Keys.Concat(container.FileSwaps.Keys)) identifier.Identify(changedItems, gamePath.ToString()); @@ -19,25 +19,25 @@ public static class IdentifierExtensions manip.AddChangedItems(identifier, changedItems); } - public static void RemoveMachinistOffhands(this SortedList changedItems) + public static void RemoveMachinistOffhands(this SortedList changedItems) { for (var i = 0; i < changedItems.Count; i++) { { var value = changedItems.Values[i]; - if (value is EquipItem { Type: FullEquipType.GunOff }) + if (value is IdentifiedItem { Item.Type: FullEquipType.GunOff }) changedItems.RemoveAt(i--); } } } - public static void RemoveMachinistOffhands(this SortedList, object?)> changedItems) + public static void RemoveMachinistOffhands(this SortedList, IIdentifiedObjectData?)> changedItems) { for (var i = 0; i < changedItems.Count; i++) { { var value = changedItems.Values[i].Item2; - if (value is EquipItem { Type: FullEquipType.GunOff }) + if (value is IdentifiedItem { Item.Type: FullEquipType.GunOff }) changedItems.RemoveAt(i--); } } From ee086e3e7698a846cefa20c23dc09408b76caad8 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Sun, 4 Aug 2024 00:57:39 +0200 Subject: [PATCH 390/433] Update GameData --- Penumbra.GameData | 2 +- Penumbra/Services/StainService.cs | 4 ++-- Penumbra/UI/Tabs/Debug/DebugTab.cs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index ee6c6faa..1ec903d5 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit ee6c6faa1e4a3e96279cb6c89df96e351f112c6a +Subproject commit 1ec903d53747fc16f62139e2ed3541f224ee3403 diff --git a/Penumbra/Services/StainService.cs b/Penumbra/Services/StainService.cs index 50713968..ba5c3e63 100644 --- a/Penumbra/Services/StainService.cs +++ b/Penumbra/Services/StainService.cs @@ -16,7 +16,7 @@ namespace Penumbra.Services; public class StainService : IService { public sealed class StainTemplateCombo(FilterComboColors[] stainCombos, StmFile stmFile) - : FilterComboCache(stmFile.Entries.Keys.Prepend((ushort)0), MouseWheelType.None, Penumbra.Log) where TDyePack : unmanaged, IDyePack + : FilterComboCache(stmFile.Entries.Keys.Prepend((ushort)0), MouseWheelType.None, Penumbra.Log) where TDyePack : unmanaged, IDyePack { // FIXME There might be a better way to handle that. public int CurrentDyeChannel = 0; @@ -102,7 +102,7 @@ public class StainService : IService }; /// Loads a STM file. Opportunistically attempts to re-use the file already read by the game, with Lumina fallback. - private static unsafe StmFile LoadStmFile(ResourceHandle* stmResourceHandle, IDataManager dataManager) where TDyePack : unmanaged, IDyePack + private static unsafe StmFile LoadStmFile(ResourceHandle* stmResourceHandle, IDataManager dataManager) where TDyePack : unmanaged, IDyePack { if (stmResourceHandle != null) { diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index ead02874..7dae19c8 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -713,7 +713,7 @@ public class DebugTab : Window, ITab, IUiService } } - private static void DrawStainTemplatesFile(StmFile stmFile) where TDyePack : unmanaged, IDyePack + private static void DrawStainTemplatesFile(StmFile stmFile) where TDyePack : unmanaged, IDyePack { foreach (var (key, data) in stmFile.Entries) { From d90c3dd1af6e256932140ce84ddba78d4f233616 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 4 Aug 2024 15:35:27 +0200 Subject: [PATCH 391/433] Update row type names. --- Penumbra.GameData | 2 +- .../ModEditWindow.Materials.ColorTable.cs | 16 ++++++++-------- .../ModEditWindow.Materials.MtrlTab.cs | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 44427ad0..f2734d54 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 44427ad0149059ab5ccb4e4a2f42a1a43423e4c5 +Subproject commit f2734d543d9b2debecb8feb6d6fa928801eb2bcb diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs index 25c0e448..cb04dc0a 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs @@ -171,7 +171,7 @@ public partial class ModEditWindow } [SkipLocalsInit] - private static unsafe void ColorTableCopyClipboardButton(ColorTable.Row row, ColorDyeTable.Row dye) + private static unsafe void ColorTableCopyClipboardButton(ColorTableRow row, ColorDyeTableRow dye) { if (!ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Clipboard.ToIconString(), ImGui.GetFrameHeight() * Vector2.One, "Export this row to your clipboard.", false, true)) @@ -179,11 +179,11 @@ public partial class ModEditWindow try { - Span data = stackalloc byte[ColorTable.Row.Size + ColorDyeTable.Row.Size]; + Span data = stackalloc byte[ColorTableRow.Size + ColorDyeTableRow.Size]; fixed (byte* ptr = data) { - MemoryUtility.MemCpyUnchecked(ptr, &row, ColorTable.Row.Size); - MemoryUtility.MemCpyUnchecked(ptr + ColorTable.Row.Size, &dye, ColorDyeTable.Row.Size); + MemoryUtility.MemCpyUnchecked(ptr, &row, ColorTableRow.Size); + MemoryUtility.MemCpyUnchecked(ptr + ColorTableRow.Size, &dye, ColorDyeTableRow.Size); } var text = Convert.ToBase64String(data); @@ -219,15 +219,15 @@ public partial class ModEditWindow { var text = ImGui.GetClipboardText(); var data = Convert.FromBase64String(text); - if (data.Length != ColorTable.Row.Size + ColorDyeTable.Row.Size + if (data.Length != ColorTableRow.Size + ColorDyeTableRow.Size || !tab.Mtrl.HasTable) return false; fixed (byte* ptr = data) { - tab.Mtrl.Table[rowIdx] = *(ColorTable.Row*)ptr; + tab.Mtrl.Table[rowIdx] = *(ColorTableRow*)ptr; if (tab.Mtrl.HasDyeTable) - tab.Mtrl.DyeTable[rowIdx] = *(ColorDyeTable.Row*)(ptr + ColorTable.Row.Size); + tab.Mtrl.DyeTable[rowIdx] = *(ColorDyeTableRow*)(ptr + ColorTableRow.Size); } tab.UpdateColorTableRowPreview(rowIdx); @@ -453,7 +453,7 @@ public partial class ModEditWindow return ret; } - private bool DrawDyePreview(MtrlTab tab, int rowIdx, bool disabled, ColorDyeTable.Row dye, float floatSize) + private bool DrawDyePreview(MtrlTab tab, int rowIdx, bool disabled, ColorDyeTableRow dye, float floatSize) { var stain = _stainService.StainCombo.CurrentSelection.Key; if (stain == 0 || !_stainService.StmFile.Entries.TryGetValue(dye.Template, out var entry)) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs index 29fd7531..b95eca9d 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs @@ -597,11 +597,11 @@ public partial class ModEditWindow if (!Mtrl.HasTable) return; - var row = new LegacyColorTable.Row(Mtrl.Table[rowIdx]); + var row = new LegacyColorTableRow(Mtrl.Table[rowIdx]); if (Mtrl.HasDyeTable) { var stm = _edit._stainService.StmFile; - var dye = new LegacyColorDyeTable.Row(Mtrl.DyeTable[rowIdx]); + var dye = new LegacyColorDyeTableRow(Mtrl.DyeTable[rowIdx]); if (stm.TryGetValue(dye.Template, _edit._stainService.StainCombo.CurrentSelection.Key, out var dyes)) row.ApplyDyeTemplate(dye, dyes); } @@ -651,7 +651,7 @@ public partial class ModEditWindow } } - private static void ApplyHighlight(ref LegacyColorTable.Row row, float time) + private static void ApplyHighlight(ref LegacyColorTableRow row, float time) { var level = (MathF.Sin(time * 2.0f * MathF.PI) + 2.0f) / 3.0f / 255.0f; var baseColor = ColorId.InGameHighlight.Value(); From c8e859ae05ebb9d9b7f0fbce17d3223c19c88be3 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 4 Aug 2024 15:41:40 +0200 Subject: [PATCH 392/433] Fixups. --- .../Materials/MtrlTab.LegacyColorTable.cs | 38 +- .../Materials/MtrlTab.LivePreview.cs | 6 +- .../ModEditWindow.Materials.ColorTable.cs | 538 ------------ .../ModEditWindow.Materials.MtrlTab.cs | 783 ------------------ 4 files changed, 28 insertions(+), 1337 deletions(-) delete mode 100644 Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs delete mode 100644 Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs index f3ec5307..a2165760 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs @@ -33,6 +33,7 @@ public partial class MtrlTab UpdateColorTableRowPreview(i); ret = true; } + ImGui.TableNextRow(); } @@ -56,6 +57,7 @@ public partial class MtrlTab UpdateColorTableRowPreview(i); ret = true; } + ImGui.TableNextRow(); } @@ -108,8 +110,10 @@ public partial class MtrlTab } ImGui.TableNextColumn(); - using (var font = ImRaii.PushFont(UiBuilder.MonoFont)) - ImUtf8.Text($"{(rowIdx >> 1) + 1,2:D}{"AB"[rowIdx & 1]}"); + using (ImRaii.PushFont(UiBuilder.MonoFont)) + { + ImUtf8.Text($"{(rowIdx >> 1) + 1,2:D}{"AB"[rowIdx & 1]}"); + } ImGui.TableNextColumn(); using var dis = ImRaii.Disabled(disabled); @@ -131,9 +135,11 @@ public partial class MtrlTab ret |= CtApplyStainCheckbox("##dyeSpecular"u8, "Apply Specular Color on Dye"u8, dye.SpecularColor, b => dyeTable[rowIdx].SpecularColor = b); } + ImGui.SameLine(); ImGui.SetNextItemWidth(pctSize); - ret |= CtDragScalar("##SpecularMask"u8, "Specular Strength"u8, (float)row.SpecularMask * 100.0f, "%.0f%%"u8, 0f, HalfMaxValue * 100.0f, 1.0f, + ret |= CtDragScalar("##SpecularMask"u8, "Specular Strength"u8, (float)row.SpecularMask * 100.0f, "%.0f%%"u8, 0f, HalfMaxValue * 100.0f, + 1.0f, v => table[rowIdx].SpecularMask = (Half)(v * 0.01f)); if (dyeTable != null) { @@ -155,7 +161,8 @@ public partial class MtrlTab ImGui.TableNextColumn(); ImGui.SetNextItemWidth(floatSize); var glossStrengthMin = ImGui.GetIO().KeyCtrl ? 0.0f : HalfEpsilon; - ret |= CtDragHalf("##Shininess"u8, "Gloss Strength"u8, row.Shininess, "%.1f"u8, glossStrengthMin, HalfMaxValue, Math.Max(0.1f, (float)row.Shininess * 0.025f), + ret |= CtDragHalf("##Shininess"u8, "Gloss Strength"u8, row.Shininess, "%.1f"u8, glossStrengthMin, HalfMaxValue, + Math.Max(0.1f, (float)row.Shininess * 0.025f), v => table[rowIdx].Shininess = v); if (dyeTable != null) @@ -197,7 +204,7 @@ public partial class MtrlTab { using var id = ImRaii.PushId(rowIdx); ref var row = ref table[rowIdx]; - var dye = dyeTable != null ? dyeTable[rowIdx] : default; + var dye = dyeTable?[rowIdx] ?? default; var floatSize = LegacyColorTableFloatSize * UiHelpers.Scale; var pctSize = LegacyColorTablePercentageSize * UiHelpers.Scale; var intSize = LegacyColorTableIntegerSize * UiHelpers.Scale; @@ -213,8 +220,10 @@ public partial class MtrlTab } ImGui.TableNextColumn(); - using (var font = ImRaii.PushFont(UiBuilder.MonoFont)) + using (ImRaii.PushFont(UiBuilder.MonoFont)) + { ImUtf8.Text($"{(rowIdx >> 1) + 1,2:D}{"AB"[rowIdx & 1]}"); + } ImGui.TableNextColumn(); using var dis = ImRaii.Disabled(disabled); @@ -236,6 +245,7 @@ public partial class MtrlTab ret |= CtApplyStainCheckbox("##dyeSpecular"u8, "Apply Specular Color on Dye"u8, dye.SpecularColor, b => dyeTable[rowIdx].SpecularColor = b); } + ImGui.SameLine(); ImGui.SetNextItemWidth(pctSize); ret |= CtDragScalar("##SpecularMask"u8, "Specular Strength"u8, (float)row.Scalar7 * 100.0f, "%.0f%%"u8, 0f, HalfMaxValue * 100.0f, 1.0f, @@ -260,7 +270,8 @@ public partial class MtrlTab ImGui.TableNextColumn(); ImGui.SetNextItemWidth(floatSize); var glossStrengthMin = ImGui.GetIO().KeyCtrl ? 0.0f : HalfEpsilon; - ret |= CtDragHalf("##Shininess"u8, "Gloss Strength"u8, row.Scalar3, "%.1f"u8, glossStrengthMin, HalfMaxValue, Math.Max(0.1f, (float)row.Scalar3 * 0.025f), + ret |= CtDragHalf("##Shininess"u8, "Gloss Strength"u8, row.Scalar3, "%.1f"u8, glossStrengthMin, HalfMaxValue, + Math.Max(0.1f, (float)row.Scalar3 * 0.025f), v => table[rowIdx].Scalar3 = v); if (dyeTable != null) @@ -307,7 +318,7 @@ public partial class MtrlTab return ret; } - private bool DrawLegacyDyePreview(int rowIdx, bool disabled, LegacyColorDyeTable.Row dye, float floatSize) + private bool DrawLegacyDyePreview(int rowIdx, bool disabled, LegacyColorDyeTableRow dye, float floatSize) { var stain = _stainService.StainCombo1.CurrentSelection.Key; if (stain == 0 || !_stainService.LegacyStmFile.TryGetValue(dye.Template, stain, out var values)) @@ -326,7 +337,7 @@ public partial class MtrlTab return ret; } - private bool DrawLegacyDyePreview(int rowIdx, bool disabled, ColorDyeTable.Row dye, float floatSize) + private bool DrawLegacyDyePreview(int rowIdx, bool disabled, ColorDyeTableRow dye, float floatSize) { var stain = _stainService.GetStainCombo(dye.Channel).CurrentSelection.Key; if (stain == 0 || !_stainService.LegacyStmFile.TryGetValue(dye.Template, stain, out var values)) @@ -337,10 +348,11 @@ public partial class MtrlTab var ret = ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.PaintBrush.ToIconString(), new Vector2(ImGui.GetFrameHeight()), "Apply the selected dye to this row.", disabled, true); - ret = ret && Mtrl.ApplyDyeToRow(_stainService.LegacyStmFile, [ - _stainService.StainCombo1.CurrentSelection.Key, - _stainService.StainCombo2.CurrentSelection.Key, - ], rowIdx); + ret = ret + && Mtrl.ApplyDyeToRow(_stainService.LegacyStmFile, [ + _stainService.StainCombo1.CurrentSelection.Key, + _stainService.StainCombo2.CurrentSelection.Key, + ], rowIdx); ImGui.SameLine(); DrawLegacyDyePreview(values, floatSize); diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs index bb346534..3482e581 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs @@ -191,7 +191,7 @@ public partial class MtrlTab var row = Mtrl.Table switch { - LegacyColorTable legacyTable => new ColorTable.Row(legacyTable[rowIdx]), + LegacyColorTable legacyTable => new ColorTableRow(legacyTable[rowIdx]), ColorTable table => table[rowIdx], _ => throw new InvalidOperationException($"Unsupported color table type {Mtrl.Table.GetType()}"), }; @@ -199,7 +199,7 @@ public partial class MtrlTab { var dyeRow = Mtrl.DyeTable switch { - LegacyColorDyeTable legacyDyeTable => new ColorDyeTable.Row(legacyDyeTable[rowIdx]), + LegacyColorDyeTable legacyDyeTable => new ColorDyeTableRow(legacyDyeTable[rowIdx]), ColorDyeTable dyeTable => dyeTable[rowIdx], _ => throw new InvalidOperationException($"Unsupported color dye table type {Mtrl.DyeTable.GetType()}"), }; @@ -258,7 +258,7 @@ public partial class MtrlTab } } - private static void ApplyHighlight(ref ColorTable.Row row, ColorId colorId, float time) + private static void ApplyHighlight(ref ColorTableRow row, ColorId colorId, float time) { var level = (MathF.Sin(time * 2.0f * MathF.PI) + 2.0f) / 3.0f / 255.0f; var baseColor = colorId.Value(); diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs deleted file mode 100644 index cb04dc0a..00000000 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorTable.cs +++ /dev/null @@ -1,538 +0,0 @@ -using Dalamud.Interface; -using Dalamud.Interface.Utility; -using ImGuiNET; -using OtterGui; -using OtterGui.Raii; -using Penumbra.GameData.Files; -using Penumbra.GameData.Files.MaterialStructs; -using Penumbra.String.Functions; - -namespace Penumbra.UI.AdvancedWindow; - -public partial class ModEditWindow -{ - private static readonly float HalfMinValue = (float)Half.MinValue; - private static readonly float HalfMaxValue = (float)Half.MaxValue; - private static readonly float HalfEpsilon = (float)Half.Epsilon; - - private bool DrawMaterialColorTableChange(MtrlTab tab, bool disabled) - { - if (!tab.SamplerIds.Contains(ShpkFile.TableSamplerId) || !tab.Mtrl.HasTable) - return false; - - ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - if (!ImGui.CollapsingHeader("Color Table", ImGuiTreeNodeFlags.DefaultOpen)) - return false; - - ColorTableCopyAllClipboardButton(tab.Mtrl); - ImGui.SameLine(); - var ret = ColorTablePasteAllClipboardButton(tab, disabled); - if (!disabled) - { - ImGui.SameLine(); - ImGui.Dummy(ImGuiHelpers.ScaledVector2(20, 0)); - ImGui.SameLine(); - ret |= ColorTableDyeableCheckbox(tab); - } - - var hasDyeTable = tab.Mtrl.HasDyeTable; - if (hasDyeTable) - { - ImGui.SameLine(); - ImGui.Dummy(ImGuiHelpers.ScaledVector2(20, 0)); - ImGui.SameLine(); - ret |= DrawPreviewDye(tab, disabled); - } - - using var table = ImRaii.Table("##ColorTable", hasDyeTable ? 11 : 9, - ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg | ImGuiTableFlags.BordersInnerV); - if (!table) - return false; - - ImGui.TableNextColumn(); - ImGui.TableHeader(string.Empty); - ImGui.TableNextColumn(); - ImGui.TableHeader("Row"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Diffuse"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Specular"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Emissive"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Gloss"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Tile"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Repeat"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Skew"); - if (hasDyeTable) - { - ImGui.TableNextColumn(); - ImGui.TableHeader("Dye"); - ImGui.TableNextColumn(); - ImGui.TableHeader("Dye Preview"); - } - - for (var i = 0; i < ColorTable.NumRows; ++i) - { - ret |= DrawColorTableRow(tab, i, disabled); - ImGui.TableNextRow(); - } - - return ret; - } - - - private static void ColorTableCopyAllClipboardButton(MtrlFile file) - { - if (!ImGui.Button("Export All Rows to Clipboard", ImGuiHelpers.ScaledVector2(200, 0))) - return; - - try - { - var data1 = file.Table.AsBytes(); - var data2 = file.HasDyeTable ? file.DyeTable.AsBytes() : ReadOnlySpan.Empty; - var array = new byte[data1.Length + data2.Length]; - data1.TryCopyTo(array); - data2.TryCopyTo(array.AsSpan(data1.Length)); - var text = Convert.ToBase64String(array); - ImGui.SetClipboardText(text); - } - catch - { - // ignored - } - } - - private bool DrawPreviewDye(MtrlTab tab, bool disabled) - { - var (dyeId, (name, dyeColor, gloss)) = _stainService.StainCombo.CurrentSelection; - var tt = dyeId == 0 - ? "Select a preview dye first." - : "Apply all preview values corresponding to the dye template and chosen dye where dyeing is enabled."; - if (ImGuiUtil.DrawDisabledButton("Apply Preview Dye", Vector2.Zero, tt, disabled || dyeId == 0)) - { - var ret = false; - if (tab.Mtrl.HasDyeTable) - for (var i = 0; i < LegacyColorTable.NumUsedRows; ++i) - ret |= tab.Mtrl.ApplyDyeTemplate(_stainService.StmFile, i, dyeId, 0); - - tab.UpdateColorTablePreview(); - - return ret; - } - - ImGui.SameLine(); - var label = dyeId == 0 ? "Preview Dye###previewDye" : $"{name} (Preview)###previewDye"; - if (_stainService.StainCombo.Draw(label, dyeColor, string.Empty, true, gloss)) - tab.UpdateColorTablePreview(); - return false; - } - - private static unsafe bool ColorTablePasteAllClipboardButton(MtrlTab tab, bool disabled) - { - if (!ImGuiUtil.DrawDisabledButton("Import All Rows from Clipboard", ImGuiHelpers.ScaledVector2(200, 0), string.Empty, disabled) - || !tab.Mtrl.HasTable) - return false; - - try - { - var text = ImGui.GetClipboardText(); - var data = Convert.FromBase64String(text); - if (data.Length < Marshal.SizeOf()) - return false; - - ref var rows = ref tab.Mtrl.Table; - fixed (void* ptr = data, output = &rows) - { - MemoryUtility.MemCpyUnchecked(output, ptr, Marshal.SizeOf()); - if (data.Length >= Marshal.SizeOf() + Marshal.SizeOf() - && tab.Mtrl.HasDyeTable) - { - ref var dyeRows = ref tab.Mtrl.DyeTable; - fixed (void* output2 = &dyeRows) - { - MemoryUtility.MemCpyUnchecked(output2, (byte*)ptr + Marshal.SizeOf(), - Marshal.SizeOf()); - } - } - } - - tab.UpdateColorTablePreview(); - - return true; - } - catch - { - return false; - } - } - - [SkipLocalsInit] - private static unsafe void ColorTableCopyClipboardButton(ColorTableRow row, ColorDyeTableRow dye) - { - if (!ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Clipboard.ToIconString(), ImGui.GetFrameHeight() * Vector2.One, - "Export this row to your clipboard.", false, true)) - return; - - try - { - Span data = stackalloc byte[ColorTableRow.Size + ColorDyeTableRow.Size]; - fixed (byte* ptr = data) - { - MemoryUtility.MemCpyUnchecked(ptr, &row, ColorTableRow.Size); - MemoryUtility.MemCpyUnchecked(ptr + ColorTableRow.Size, &dye, ColorDyeTableRow.Size); - } - - var text = Convert.ToBase64String(data); - ImGui.SetClipboardText(text); - } - catch - { - // ignored - } - } - - private static bool ColorTableDyeableCheckbox(MtrlTab tab) - { - var dyeable = tab.Mtrl.HasDyeTable; - var ret = ImGui.Checkbox("Dyeable", ref dyeable); - - if (ret) - { - tab.Mtrl.HasDyeTable = dyeable; - tab.UpdateColorTablePreview(); - } - - return ret; - } - - private static unsafe bool ColorTablePasteFromClipboardButton(MtrlTab tab, int rowIdx, bool disabled) - { - if (!ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Paste.ToIconString(), ImGui.GetFrameHeight() * Vector2.One, - "Import an exported row from your clipboard onto this row.", disabled, true)) - return false; - - try - { - var text = ImGui.GetClipboardText(); - var data = Convert.FromBase64String(text); - if (data.Length != ColorTableRow.Size + ColorDyeTableRow.Size - || !tab.Mtrl.HasTable) - return false; - - fixed (byte* ptr = data) - { - tab.Mtrl.Table[rowIdx] = *(ColorTableRow*)ptr; - if (tab.Mtrl.HasDyeTable) - tab.Mtrl.DyeTable[rowIdx] = *(ColorDyeTableRow*)(ptr + ColorTableRow.Size); - } - - tab.UpdateColorTableRowPreview(rowIdx); - - return true; - } - catch - { - return false; - } - } - - private static void ColorTableHighlightButton(MtrlTab tab, int rowIdx, bool disabled) - { - ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Crosshairs.ToIconString(), ImGui.GetFrameHeight() * Vector2.One, - "Highlight this row on your character, if possible.", disabled || tab.ColorTablePreviewers.Count == 0, true); - - if (ImGui.IsItemHovered()) - tab.HighlightColorTableRow(rowIdx); - else if (tab.HighlightedColorTableRow == rowIdx) - tab.CancelColorTableHighlight(); - } - - private bool DrawColorTableRow(MtrlTab tab, int rowIdx, bool disabled) - { - static bool FixFloat(ref float val, float current) - { - val = (float)(Half)val; - return val != current; - } - - using var id = ImRaii.PushId(rowIdx); - ref var row = ref tab.Mtrl.Table[rowIdx]; - var hasDye = tab.Mtrl.HasDyeTable; - ref var dye = ref tab.Mtrl.DyeTable[rowIdx]; - var floatSize = 70 * UiHelpers.Scale; - var intSize = 45 * UiHelpers.Scale; - ImGui.TableNextColumn(); - ColorTableCopyClipboardButton(row, dye); - ImGui.SameLine(); - var ret = ColorTablePasteFromClipboardButton(tab, rowIdx, disabled); - ImGui.SameLine(); - ColorTableHighlightButton(tab, rowIdx, disabled); - - ImGui.TableNextColumn(); - ImGui.TextUnformatted($"#{rowIdx + 1:D2}"); - - ImGui.TableNextColumn(); - using var dis = ImRaii.Disabled(disabled); - ret |= ColorPicker("##Diffuse", "Diffuse Color", row.Diffuse, c => - { - tab.Mtrl.Table[rowIdx].Diffuse = c; - tab.UpdateColorTableRowPreview(rowIdx); - }); - if (hasDye) - { - ImGui.SameLine(); - ret |= ImGuiUtil.Checkbox("##dyeDiffuse", "Apply Diffuse Color on Dye", dye.Diffuse, - b => - { - tab.Mtrl.DyeTable[rowIdx].Diffuse = b; - tab.UpdateColorTableRowPreview(rowIdx); - }, ImGuiHoveredFlags.AllowWhenDisabled); - } - - ImGui.TableNextColumn(); - ret |= ColorPicker("##Specular", "Specular Color", row.Specular, c => - { - tab.Mtrl.Table[rowIdx].Specular = c; - tab.UpdateColorTableRowPreview(rowIdx); - }); - ImGui.SameLine(); - var tmpFloat = row.SpecularStrength; - ImGui.SetNextItemWidth(floatSize); - if (ImGui.DragFloat("##SpecularStrength", ref tmpFloat, 0.01f, 0f, HalfMaxValue, "%.2f") - && FixFloat(ref tmpFloat, row.SpecularStrength)) - { - row.SpecularStrength = tmpFloat; - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Specular Strength", ImGuiHoveredFlags.AllowWhenDisabled); - - if (hasDye) - { - ImGui.SameLine(); - ret |= ImGuiUtil.Checkbox("##dyeSpecular", "Apply Specular Color on Dye", dye.Specular, - b => - { - tab.Mtrl.DyeTable[rowIdx].Specular = b; - tab.UpdateColorTableRowPreview(rowIdx); - }, ImGuiHoveredFlags.AllowWhenDisabled); - ImGui.SameLine(); - ret |= ImGuiUtil.Checkbox("##dyeSpecularStrength", "Apply Specular Strength on Dye", dye.SpecularStrength, - b => - { - tab.Mtrl.DyeTable[rowIdx].SpecularStrength = b; - tab.UpdateColorTableRowPreview(rowIdx); - }, ImGuiHoveredFlags.AllowWhenDisabled); - } - - ImGui.TableNextColumn(); - ret |= ColorPicker("##Emissive", "Emissive Color", row.Emissive, c => - { - tab.Mtrl.Table[rowIdx].Emissive = c; - tab.UpdateColorTableRowPreview(rowIdx); - }); - if (hasDye) - { - ImGui.SameLine(); - ret |= ImGuiUtil.Checkbox("##dyeEmissive", "Apply Emissive Color on Dye", dye.Emissive, - b => - { - tab.Mtrl.DyeTable[rowIdx].Emissive = b; - tab.UpdateColorTableRowPreview(rowIdx); - }, ImGuiHoveredFlags.AllowWhenDisabled); - } - - ImGui.TableNextColumn(); - tmpFloat = row.GlossStrength; - ImGui.SetNextItemWidth(floatSize); - var glossStrengthMin = ImGui.GetIO().KeyCtrl ? 0.0f : HalfEpsilon; - if (ImGui.DragFloat("##GlossStrength", ref tmpFloat, Math.Max(0.1f, tmpFloat * 0.025f), glossStrengthMin, HalfMaxValue, "%.1f") - && FixFloat(ref tmpFloat, row.GlossStrength)) - { - row.GlossStrength = Math.Max(tmpFloat, glossStrengthMin); - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Gloss Strength", ImGuiHoveredFlags.AllowWhenDisabled); - if (hasDye) - { - ImGui.SameLine(); - ret |= ImGuiUtil.Checkbox("##dyeGloss", "Apply Gloss Strength on Dye", dye.Gloss, - b => - { - tab.Mtrl.DyeTable[rowIdx].Gloss = b; - tab.UpdateColorTableRowPreview(rowIdx); - }, ImGuiHoveredFlags.AllowWhenDisabled); - } - - ImGui.TableNextColumn(); - int tmpInt = row.TileSet; - ImGui.SetNextItemWidth(intSize); - if (ImGui.DragInt("##TileSet", ref tmpInt, 0.25f, 0, 63) && tmpInt != row.TileSet && tmpInt is >= 0 and <= ushort.MaxValue) - { - row.TileSet = (ushort)Math.Clamp(tmpInt, 0, 63); - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Tile Set", ImGuiHoveredFlags.AllowWhenDisabled); - - ImGui.TableNextColumn(); - tmpFloat = row.MaterialRepeat.X; - ImGui.SetNextItemWidth(floatSize); - if (ImGui.DragFloat("##RepeatX", ref tmpFloat, 0.1f, HalfMinValue, HalfMaxValue, "%.2f") - && FixFloat(ref tmpFloat, row.MaterialRepeat.X)) - { - row.MaterialRepeat = row.MaterialRepeat with { X = tmpFloat }; - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Repeat X", ImGuiHoveredFlags.AllowWhenDisabled); - ImGui.SameLine(); - tmpFloat = row.MaterialRepeat.Y; - ImGui.SetNextItemWidth(floatSize); - if (ImGui.DragFloat("##RepeatY", ref tmpFloat, 0.1f, HalfMinValue, HalfMaxValue, "%.2f") - && FixFloat(ref tmpFloat, row.MaterialRepeat.Y)) - { - row.MaterialRepeat = row.MaterialRepeat with { Y = tmpFloat }; - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Repeat Y", ImGuiHoveredFlags.AllowWhenDisabled); - - ImGui.TableNextColumn(); - tmpFloat = row.MaterialSkew.X; - ImGui.SetNextItemWidth(floatSize); - if (ImGui.DragFloat("##SkewX", ref tmpFloat, 0.1f, HalfMinValue, HalfMaxValue, "%.2f") && FixFloat(ref tmpFloat, row.MaterialSkew.X)) - { - row.MaterialSkew = row.MaterialSkew with { X = tmpFloat }; - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Skew X", ImGuiHoveredFlags.AllowWhenDisabled); - - ImGui.SameLine(); - tmpFloat = row.MaterialSkew.Y; - ImGui.SetNextItemWidth(floatSize); - if (ImGui.DragFloat("##SkewY", ref tmpFloat, 0.1f, HalfMinValue, HalfMaxValue, "%.2f") && FixFloat(ref tmpFloat, row.MaterialSkew.Y)) - { - row.MaterialSkew = row.MaterialSkew with { Y = tmpFloat }; - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Skew Y", ImGuiHoveredFlags.AllowWhenDisabled); - - if (hasDye) - { - ImGui.TableNextColumn(); - if (_stainService.TemplateCombo.Draw("##dyeTemplate", dye.Template.ToString(), string.Empty, intSize - + ImGui.GetStyle().ScrollbarSize / 2, ImGui.GetTextLineHeightWithSpacing(), ImGuiComboFlags.NoArrowButton)) - { - dye.Template = _stainService.TemplateCombo.CurrentSelection; - ret = true; - tab.UpdateColorTableRowPreview(rowIdx); - } - - ImGuiUtil.HoverTooltip("Dye Template", ImGuiHoveredFlags.AllowWhenDisabled); - - ImGui.TableNextColumn(); - ret |= DrawDyePreview(tab, rowIdx, disabled, dye, floatSize); - } - - - return ret; - } - - private bool DrawDyePreview(MtrlTab tab, int rowIdx, bool disabled, ColorDyeTableRow dye, float floatSize) - { - var stain = _stainService.StainCombo.CurrentSelection.Key; - if (stain == 0 || !_stainService.StmFile.Entries.TryGetValue(dye.Template, out var entry)) - return false; - - var values = entry[(int)stain]; - using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, ImGui.GetStyle().ItemSpacing / 2); - - var ret = ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.PaintBrush.ToIconString(), new Vector2(ImGui.GetFrameHeight()), - "Apply the selected dye to this row.", disabled, true); - - ret = ret && tab.Mtrl.ApplyDyeTemplate(_stainService.StmFile, rowIdx, stain, 0); - if (ret) - tab.UpdateColorTableRowPreview(rowIdx); - - ImGui.SameLine(); - ColorPicker("##diffusePreview", string.Empty, values.Diffuse, _ => { }, "D"); - ImGui.SameLine(); - ColorPicker("##specularPreview", string.Empty, values.Specular, _ => { }, "S"); - ImGui.SameLine(); - ColorPicker("##emissivePreview", string.Empty, values.Emissive, _ => { }, "E"); - ImGui.SameLine(); - using var dis = ImRaii.Disabled(); - ImGui.SetNextItemWidth(floatSize); - ImGui.DragFloat("##gloss", ref values.Gloss, 0, values.Gloss, values.Gloss, "%.1f G"); - ImGui.SameLine(); - ImGui.SetNextItemWidth(floatSize); - ImGui.DragFloat("##specularStrength", ref values.SpecularPower, 0, values.SpecularPower, values.SpecularPower, "%.2f S"); - - return ret; - } - - private static bool ColorPicker(string label, string tooltip, Vector3 input, Action setter, string letter = "") - { - var ret = false; - var inputSqrt = PseudoSqrtRgb(input); - var tmp = inputSqrt; - if (ImGui.ColorEdit3(label, ref tmp, - ImGuiColorEditFlags.NoInputs - | ImGuiColorEditFlags.DisplayRGB - | ImGuiColorEditFlags.InputRGB - | ImGuiColorEditFlags.NoTooltip - | ImGuiColorEditFlags.HDR) - && tmp != inputSqrt) - { - setter(PseudoSquareRgb(tmp)); - ret = true; - } - - if (letter.Length > 0 && ImGui.IsItemVisible()) - { - var textSize = ImGui.CalcTextSize(letter); - var center = ImGui.GetItemRectMin() + (ImGui.GetItemRectSize() - textSize) / 2; - var textColor = input.LengthSquared() < 0.25f ? 0x80FFFFFFu : 0x80000000u; - ImGui.GetWindowDrawList().AddText(center, textColor, letter); - } - - ImGuiUtil.HoverTooltip(tooltip, ImGuiHoveredFlags.AllowWhenDisabled); - - return ret; - } - - // Functions to deal with squared RGB values without making negatives useless. - - private static float PseudoSquareRgb(float x) - => x < 0.0f ? -(x * x) : x * x; - - private static Vector3 PseudoSquareRgb(Vector3 vec) - => new(PseudoSquareRgb(vec.X), PseudoSquareRgb(vec.Y), PseudoSquareRgb(vec.Z)); - - private static Vector4 PseudoSquareRgb(Vector4 vec) - => new(PseudoSquareRgb(vec.X), PseudoSquareRgb(vec.Y), PseudoSquareRgb(vec.Z), vec.W); - - private static float PseudoSqrtRgb(float x) - => x < 0.0f ? -MathF.Sqrt(-x) : MathF.Sqrt(x); - - internal static Vector3 PseudoSqrtRgb(Vector3 vec) - => new(PseudoSqrtRgb(vec.X), PseudoSqrtRgb(vec.Y), PseudoSqrtRgb(vec.Z)); - - private static Vector4 PseudoSqrtRgb(Vector4 vec) - => new(PseudoSqrtRgb(vec.X), PseudoSqrtRgb(vec.Y), PseudoSqrtRgb(vec.Z), vec.W); -} diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs deleted file mode 100644 index b95eca9d..00000000 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.MtrlTab.cs +++ /dev/null @@ -1,783 +0,0 @@ -using Dalamud.Interface; -using Dalamud.Interface.ImGuiNotification; -using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; -using ImGuiNET; -using Newtonsoft.Json.Linq; -using OtterGui; -using OtterGui.Classes; -using OtterGui.Raii; -using Penumbra.GameData.Data; -using Penumbra.GameData.Files; -using Penumbra.GameData.Files.MaterialStructs; -using Penumbra.GameData.Structs; -using Penumbra.Interop.Hooks.Objects; -using Penumbra.Interop.MaterialPreview; -using Penumbra.String; -using Penumbra.String.Classes; -using Penumbra.UI.Classes; -using static Penumbra.GameData.Files.ShpkFile; - -namespace Penumbra.UI.AdvancedWindow; - -public partial class ModEditWindow -{ - private sealed class MtrlTab : IWritable, IDisposable - { - private const int ShpkPrefixLength = 16; - - private static readonly CiByteString ShpkPrefix = CiByteString.FromSpanUnsafe("shader/sm5/shpk/"u8, true, true, true); - - private readonly ModEditWindow _edit; - public readonly MtrlFile Mtrl; - public readonly string FilePath; - public readonly bool Writable; - - private string[]? _shpkNames; - - public string ShaderHeader = "Shader###Shader"; - public FullPath LoadedShpkPath = FullPath.Empty; - public string LoadedShpkPathName = string.Empty; - public string LoadedShpkDevkitPathName = string.Empty; - public string ShaderComment = string.Empty; - public ShpkFile? AssociatedShpk; - public JObject? AssociatedShpkDevkit; - - public readonly string LoadedBaseDevkitPathName; - public readonly JObject? AssociatedBaseDevkit; - - // Shader Key State - public readonly - List<(string Label, int Index, string Description, bool MonoFont, IReadOnlyList<(string Label, uint Value, string Description)> - Values)> ShaderKeys = new(16); - - public readonly HashSet VertexShaders = new(16); - public readonly HashSet PixelShaders = new(16); - public bool ShadersKnown; - public string VertexShadersString = "Vertex Shaders: ???"; - public string PixelShadersString = "Pixel Shaders: ???"; - - // Textures & Samplers - public readonly List<(string Label, int TextureIndex, int SamplerIndex, string Description, bool MonoFont)> Textures = new(4); - - public readonly HashSet UnfoldedTextures = new(4); - public readonly HashSet SamplerIds = new(16); - public float TextureLabelWidth; - - // Material Constants - public readonly - List<(string Header, List<(string Label, int ConstantIndex, Range Slice, string Description, bool MonoFont, IConstantEditor Editor)> - Constants)> Constants = new(16); - - // Live-Previewers - public readonly List MaterialPreviewers = new(4); - public readonly List ColorTablePreviewers = new(4); - public int HighlightedColorTableRow = -1; - public readonly Stopwatch HighlightTime = new(); - - public FullPath FindAssociatedShpk(out string defaultPath, out Utf8GamePath defaultGamePath) - { - defaultPath = GamePaths.Shader.ShpkPath(Mtrl.ShaderPackage.Name); - if (!Utf8GamePath.FromString(defaultPath, out defaultGamePath)) - return FullPath.Empty; - - return _edit.FindBestMatch(defaultGamePath); - } - - public string[] GetShpkNames() - { - if (null != _shpkNames) - return _shpkNames; - - var names = new HashSet(StandardShaderPackages); - names.UnionWith(_edit.FindPathsStartingWith(ShpkPrefix).Select(path => path.ToString()[ShpkPrefixLength..])); - - _shpkNames = names.ToArray(); - Array.Sort(_shpkNames); - - return _shpkNames; - } - - public void LoadShpk(FullPath path) - { - ShaderHeader = $"Shader ({Mtrl.ShaderPackage.Name})###Shader"; - - try - { - LoadedShpkPath = path; - var data = LoadedShpkPath.IsRooted - ? File.ReadAllBytes(LoadedShpkPath.FullName) - : _edit._gameData.GetFile(LoadedShpkPath.InternalName.ToString())?.Data; - AssociatedShpk = data?.Length > 0 ? new ShpkFile(data) : throw new Exception("Failure to load file data."); - LoadedShpkPathName = path.ToPath(); - } - catch (Exception e) - { - LoadedShpkPath = FullPath.Empty; - LoadedShpkPathName = string.Empty; - AssociatedShpk = null; - Penumbra.Messager.NotificationMessage(e, $"Could not load {LoadedShpkPath.ToPath()}.", NotificationType.Error, false); - } - - if (LoadedShpkPath.InternalName.IsEmpty) - { - AssociatedShpkDevkit = null; - LoadedShpkDevkitPathName = string.Empty; - } - else - { - AssociatedShpkDevkit = - TryLoadShpkDevkit(Path.GetFileNameWithoutExtension(Mtrl.ShaderPackage.Name), out LoadedShpkDevkitPathName); - } - - UpdateShaderKeys(); - Update(); - } - - private JObject? TryLoadShpkDevkit(string shpkBaseName, out string devkitPathName) - { - try - { - if (!Utf8GamePath.FromString("penumbra/shpk_devkit/" + shpkBaseName + ".json", out var devkitPath)) - throw new Exception("Could not assemble ShPk dev-kit path."); - - var devkitFullPath = _edit.FindBestMatch(devkitPath); - if (!devkitFullPath.IsRooted) - throw new Exception("Could not resolve ShPk dev-kit path."); - - devkitPathName = devkitFullPath.FullName; - return JObject.Parse(File.ReadAllText(devkitFullPath.FullName)); - } - catch - { - devkitPathName = string.Empty; - return null; - } - } - - private T? TryGetShpkDevkitData(string category, uint? id, bool mayVary) where T : class - => TryGetShpkDevkitData(AssociatedShpkDevkit, LoadedShpkDevkitPathName, category, id, mayVary) - ?? TryGetShpkDevkitData(AssociatedBaseDevkit, LoadedBaseDevkitPathName, category, id, mayVary); - - private T? TryGetShpkDevkitData(JObject? devkit, string devkitPathName, string category, uint? id, bool mayVary) where T : class - { - if (devkit == null) - return null; - - try - { - var data = devkit[category]; - if (id.HasValue) - data = data?[id.Value.ToString()]; - - if (mayVary && (data as JObject)?["Vary"] != null) - { - var selector = BuildSelector(data!["Vary"]! - .Select(key => (uint)key) - .Select(key => Mtrl.GetShaderKey(key)?.Value ?? AssociatedShpk!.GetMaterialKeyById(key)!.Value.DefaultValue)); - var index = (int)data["Selectors"]![selector.ToString()]!; - data = data["Items"]![index]; - } - - return data?.ToObject(typeof(T)) as T; - } - catch (Exception e) - { - // Some element in the JSON was undefined or invalid (wrong type, key that doesn't exist in the ShPk, index out of range, …) - Penumbra.Log.Error($"Error while traversing the ShPk dev-kit file at {devkitPathName}: {e}"); - return null; - } - } - - private void UpdateShaderKeys() - { - ShaderKeys.Clear(); - if (AssociatedShpk != null) - foreach (var key in AssociatedShpk.MaterialKeys) - { - var dkData = TryGetShpkDevkitData("ShaderKeys", key.Id, false); - var hasDkLabel = !string.IsNullOrEmpty(dkData?.Label); - - var valueSet = new HashSet(key.Values); - if (dkData != null) - valueSet.UnionWith(dkData.Values.Keys); - - var mtrlKeyIndex = Mtrl.FindOrAddShaderKey(key.Id, key.DefaultValue); - var values = valueSet.Select(value => - { - if (dkData != null && dkData.Values.TryGetValue(value, out var dkValue)) - return (dkValue.Label.Length > 0 ? dkValue.Label : $"0x{value:X8}", value, dkValue.Description); - - return ($"0x{value:X8}", value, string.Empty); - }).ToArray(); - Array.Sort(values, (x, y) => - { - if (x.Value == key.DefaultValue) - return -1; - if (y.Value == key.DefaultValue) - return 1; - - return string.Compare(x.Label, y.Label, StringComparison.Ordinal); - }); - ShaderKeys.Add((hasDkLabel ? dkData!.Label : $"0x{key.Id:X8}", mtrlKeyIndex, dkData?.Description ?? string.Empty, - !hasDkLabel, values)); - } - else - foreach (var (key, index) in Mtrl.ShaderPackage.ShaderKeys.WithIndex()) - ShaderKeys.Add(($"0x{key.Category:X8}", index, string.Empty, true, Array.Empty<(string, uint, string)>())); - } - - private void UpdateShaders() - { - VertexShaders.Clear(); - PixelShaders.Clear(); - if (AssociatedShpk == null) - { - ShadersKnown = false; - } - else - { - ShadersKnown = true; - var systemKeySelectors = AllSelectors(AssociatedShpk.SystemKeys).ToArray(); - var sceneKeySelectors = AllSelectors(AssociatedShpk.SceneKeys).ToArray(); - var subViewKeySelectors = AllSelectors(AssociatedShpk.SubViewKeys).ToArray(); - var materialKeySelector = - BuildSelector(AssociatedShpk.MaterialKeys.Select(key => Mtrl.GetOrAddShaderKey(key.Id, key.DefaultValue).Value)); - foreach (var systemKeySelector in systemKeySelectors) - { - foreach (var sceneKeySelector in sceneKeySelectors) - { - foreach (var subViewKeySelector in subViewKeySelectors) - { - var selector = BuildSelector(systemKeySelector, sceneKeySelector, materialKeySelector, subViewKeySelector); - var node = AssociatedShpk.GetNodeBySelector(selector); - if (node.HasValue) - foreach (var pass in node.Value.Passes) - { - VertexShaders.Add((int)pass.VertexShader); - PixelShaders.Add((int)pass.PixelShader); - } - else - ShadersKnown = false; - } - } - } - } - - var vertexShaders = VertexShaders.OrderBy(i => i).Select(i => $"#{i}"); - var pixelShaders = PixelShaders.OrderBy(i => i).Select(i => $"#{i}"); - - VertexShadersString = $"Vertex Shaders: {string.Join(", ", ShadersKnown ? vertexShaders : vertexShaders.Append("???"))}"; - PixelShadersString = $"Pixel Shaders: {string.Join(", ", ShadersKnown ? pixelShaders : pixelShaders.Append("???"))}"; - - ShaderComment = TryGetShpkDevkitData("Comment", null, true) ?? string.Empty; - } - - private void UpdateTextures() - { - Textures.Clear(); - SamplerIds.Clear(); - if (AssociatedShpk == null) - { - SamplerIds.UnionWith(Mtrl.ShaderPackage.Samplers.Select(sampler => sampler.SamplerId)); - if (Mtrl.HasTable) - SamplerIds.Add(TableSamplerId); - - foreach (var (sampler, index) in Mtrl.ShaderPackage.Samplers.WithIndex()) - Textures.Add(($"0x{sampler.SamplerId:X8}", sampler.TextureIndex, index, string.Empty, true)); - } - else - { - foreach (var index in VertexShaders) - SamplerIds.UnionWith(AssociatedShpk.VertexShaders[index].Samplers.Select(sampler => sampler.Id)); - foreach (var index in PixelShaders) - SamplerIds.UnionWith(AssociatedShpk.PixelShaders[index].Samplers.Select(sampler => sampler.Id)); - if (!ShadersKnown) - { - SamplerIds.UnionWith(Mtrl.ShaderPackage.Samplers.Select(sampler => sampler.SamplerId)); - if (Mtrl.HasTable) - SamplerIds.Add(TableSamplerId); - } - - foreach (var samplerId in SamplerIds) - { - var shpkSampler = AssociatedShpk.GetSamplerById(samplerId); - if (shpkSampler is not { Slot: 2 }) - continue; - - var dkData = TryGetShpkDevkitData("Samplers", samplerId, true); - var hasDkLabel = !string.IsNullOrEmpty(dkData?.Label); - - var sampler = Mtrl.GetOrAddSampler(samplerId, dkData?.DefaultTexture ?? string.Empty, out var samplerIndex); - Textures.Add((hasDkLabel ? dkData!.Label : shpkSampler.Value.Name, sampler.TextureIndex, samplerIndex, - dkData?.Description ?? string.Empty, !hasDkLabel)); - } - - if (SamplerIds.Contains(TableSamplerId)) - Mtrl.HasTable = true; - } - - Textures.Sort((x, y) => string.CompareOrdinal(x.Label, y.Label)); - - TextureLabelWidth = 50f * UiHelpers.Scale; - - float helpWidth; - using (var _ = ImRaii.PushFont(UiBuilder.IconFont)) - { - helpWidth = ImGui.GetStyle().ItemSpacing.X + ImGui.CalcTextSize(FontAwesomeIcon.InfoCircle.ToIconString()).X; - } - - foreach (var (label, _, _, description, monoFont) in Textures) - { - if (!monoFont) - TextureLabelWidth = Math.Max(TextureLabelWidth, ImGui.CalcTextSize(label).X + (description.Length > 0 ? helpWidth : 0.0f)); - } - - using (var _ = ImRaii.PushFont(UiBuilder.MonoFont)) - { - foreach (var (label, _, _, description, monoFont) in Textures) - { - if (monoFont) - TextureLabelWidth = Math.Max(TextureLabelWidth, - ImGui.CalcTextSize(label).X + (description.Length > 0 ? helpWidth : 0.0f)); - } - } - - TextureLabelWidth = TextureLabelWidth / UiHelpers.Scale + 4; - } - - private void UpdateConstants() - { - static List FindOrAddGroup(List<(string, List)> groups, string name) - { - foreach (var (groupName, group) in groups) - { - if (string.Equals(name, groupName, StringComparison.Ordinal)) - return group; - } - - var newGroup = new List(16); - groups.Add((name, newGroup)); - return newGroup; - } - - Constants.Clear(); - if (AssociatedShpk == null) - { - var fcGroup = FindOrAddGroup(Constants, "Further Constants"); - foreach (var (constant, index) in Mtrl.ShaderPackage.Constants.WithIndex()) - { - var values = Mtrl.GetConstantValues(constant); - for (var i = 0; i < values.Length; i += 4) - { - fcGroup.Add(($"0x{constant.Id:X8}", index, i..Math.Min(i + 4, values.Length), string.Empty, true, - FloatConstantEditor.Default)); - } - } - } - else - { - var prefix = AssociatedShpk.GetConstantById(MaterialParamsConstantId)?.Name ?? string.Empty; - foreach (var shpkConstant in AssociatedShpk.MaterialParams) - { - if ((shpkConstant.ByteSize & 0x3) != 0) - continue; - - var constant = Mtrl.GetOrAddConstant(shpkConstant.Id, shpkConstant.ByteSize >> 2, out var constantIndex); - var values = Mtrl.GetConstantValues(constant); - var handledElements = new IndexSet(values.Length, false); - - var dkData = TryGetShpkDevkitData("Constants", shpkConstant.Id, true); - if (dkData != null) - foreach (var dkConstant in dkData) - { - var offset = (int)dkConstant.Offset; - var length = values.Length - offset; - if (dkConstant.Length.HasValue) - length = Math.Min(length, (int)dkConstant.Length.Value); - if (length <= 0) - continue; - - var editor = dkConstant.CreateEditor(); - if (editor != null) - FindOrAddGroup(Constants, dkConstant.Group.Length > 0 ? dkConstant.Group : "Further Constants") - .Add((dkConstant.Label, constantIndex, offset..(offset + length), dkConstant.Description, false, editor)); - handledElements.AddRange(offset, length); - } - - var fcGroup = FindOrAddGroup(Constants, "Further Constants"); - foreach (var (start, end) in handledElements.Ranges(complement:true)) - { - if ((shpkConstant.ByteOffset & 0x3) == 0) - { - var offset = shpkConstant.ByteOffset >> 2; - for (int i = (start & ~0x3) - (offset & 0x3), j = offset >> 2; i < end; i += 4, ++j) - { - var rangeStart = Math.Max(i, start); - var rangeEnd = Math.Min(i + 4, end); - if (rangeEnd > rangeStart) - fcGroup.Add(( - $"{prefix}[{j:D2}]{VectorSwizzle((offset + rangeStart) & 0x3, (offset + rangeEnd - 1) & 0x3)} (0x{shpkConstant.Id:X8})", - constantIndex, rangeStart..rangeEnd, string.Empty, true, FloatConstantEditor.Default)); - } - } - else - { - for (var i = start; i < end; i += 4) - { - fcGroup.Add(($"0x{shpkConstant.Id:X8}", constantIndex, i..Math.Min(i + 4, end), string.Empty, true, - FloatConstantEditor.Default)); - } - } - } - } - } - - Constants.RemoveAll(group => group.Constants.Count == 0); - Constants.Sort((x, y) => - { - if (string.Equals(x.Header, "Further Constants", StringComparison.Ordinal)) - return 1; - if (string.Equals(y.Header, "Further Constants", StringComparison.Ordinal)) - return -1; - - return string.Compare(x.Header, y.Header, StringComparison.Ordinal); - }); - // HACK the Replace makes w appear after xyz, for the cbuffer-location-based naming scheme - foreach (var (_, group) in Constants) - { - group.Sort((x, y) => string.CompareOrdinal( - x.MonoFont ? x.Label.Replace("].w", "].{") : x.Label, - y.MonoFont ? y.Label.Replace("].w", "].{") : y.Label)); - } - } - - public unsafe void BindToMaterialInstances() - { - UnbindFromMaterialInstances(); - - var instances = MaterialInfo.FindMaterials(_edit._resourceTreeFactory.GetLocalPlayerRelatedCharacters().Select(ch => ch.Address), - FilePath); - - var foundMaterials = new HashSet(); - foreach (var materialInfo in instances) - { - var material = materialInfo.GetDrawObjectMaterial(_edit._objects); - if (foundMaterials.Contains((nint)material)) - continue; - - try - { - MaterialPreviewers.Add(new LiveMaterialPreviewer(_edit._objects, materialInfo)); - foundMaterials.Add((nint)material); - } - catch (InvalidOperationException) - { - // Carry on without that previewer. - } - } - - UpdateMaterialPreview(); - - if (!Mtrl.HasTable) - return; - - foreach (var materialInfo in instances) - { - try - { - ColorTablePreviewers.Add(new LiveColorTablePreviewer(_edit._objects, _edit._framework, materialInfo)); - } - catch (InvalidOperationException) - { - // Carry on without that previewer. - } - } - - UpdateColorTablePreview(); - } - - private void UnbindFromMaterialInstances() - { - foreach (var previewer in MaterialPreviewers) - previewer.Dispose(); - MaterialPreviewers.Clear(); - - foreach (var previewer in ColorTablePreviewers) - previewer.Dispose(); - ColorTablePreviewers.Clear(); - } - - private unsafe void UnbindFromDrawObjectMaterialInstances(CharacterBase* characterBase) - { - for (var i = MaterialPreviewers.Count; i-- > 0;) - { - var previewer = MaterialPreviewers[i]; - if (previewer.DrawObject != characterBase) - continue; - - previewer.Dispose(); - MaterialPreviewers.RemoveAt(i); - } - - for (var i = ColorTablePreviewers.Count; i-- > 0;) - { - var previewer = ColorTablePreviewers[i]; - if (previewer.DrawObject != characterBase) - continue; - - previewer.Dispose(); - ColorTablePreviewers.RemoveAt(i); - } - } - - public void SetShaderPackageFlags(uint shPkFlags) - { - foreach (var previewer in MaterialPreviewers) - previewer.SetShaderPackageFlags(shPkFlags); - } - - public void SetMaterialParameter(uint parameterCrc, Index offset, Span value) - { - foreach (var previewer in MaterialPreviewers) - previewer.SetMaterialParameter(parameterCrc, offset, value); - } - - public void SetSamplerFlags(uint samplerCrc, uint samplerFlags) - { - foreach (var previewer in MaterialPreviewers) - previewer.SetSamplerFlags(samplerCrc, samplerFlags); - } - - private void UpdateMaterialPreview() - { - SetShaderPackageFlags(Mtrl.ShaderPackage.Flags); - foreach (var constant in Mtrl.ShaderPackage.Constants) - { - var values = Mtrl.GetConstantValues(constant); - if (values != null) - SetMaterialParameter(constant.Id, 0, values); - } - - foreach (var sampler in Mtrl.ShaderPackage.Samplers) - SetSamplerFlags(sampler.SamplerId, sampler.Flags); - } - - public void HighlightColorTableRow(int rowIdx) - { - var oldRowIdx = HighlightedColorTableRow; - - if (HighlightedColorTableRow != rowIdx) - { - HighlightedColorTableRow = rowIdx; - HighlightTime.Restart(); - } - - if (oldRowIdx >= 0) - UpdateColorTableRowPreview(oldRowIdx); - if (rowIdx >= 0) - UpdateColorTableRowPreview(rowIdx); - } - - public void CancelColorTableHighlight() - { - var rowIdx = HighlightedColorTableRow; - - HighlightedColorTableRow = -1; - HighlightTime.Reset(); - - if (rowIdx >= 0) - UpdateColorTableRowPreview(rowIdx); - } - - public void UpdateColorTableRowPreview(int rowIdx) - { - if (ColorTablePreviewers.Count == 0) - return; - - if (!Mtrl.HasTable) - return; - - var row = new LegacyColorTableRow(Mtrl.Table[rowIdx]); - if (Mtrl.HasDyeTable) - { - var stm = _edit._stainService.StmFile; - var dye = new LegacyColorDyeTableRow(Mtrl.DyeTable[rowIdx]); - if (stm.TryGetValue(dye.Template, _edit._stainService.StainCombo.CurrentSelection.Key, out var dyes)) - row.ApplyDyeTemplate(dye, dyes); - } - - if (HighlightedColorTableRow == rowIdx) - ApplyHighlight(ref row, (float)HighlightTime.Elapsed.TotalSeconds); - - foreach (var previewer in ColorTablePreviewers) - { - row.AsHalves().CopyTo(previewer.ColorTable.AsSpan() - .Slice(LiveColorTablePreviewer.TextureWidth * 4 * rowIdx, LiveColorTablePreviewer.TextureWidth * 4)); - previewer.ScheduleUpdate(); - } - } - - public void UpdateColorTablePreview() - { - if (ColorTablePreviewers.Count == 0) - return; - - if (!Mtrl.HasTable) - return; - - var rows = new LegacyColorTable(Mtrl.Table); - var dyeRows = new LegacyColorDyeTable(Mtrl.DyeTable); - if (Mtrl.HasDyeTable) - { - var stm = _edit._stainService.StmFile; - var stainId = (StainId)_edit._stainService.StainCombo.CurrentSelection.Key; - for (var i = 0; i < LegacyColorTable.NumUsedRows; ++i) - { - ref var row = ref rows[i]; - var dye = dyeRows[i]; - if (stm.TryGetValue(dye.Template, stainId, out var dyes)) - row.ApplyDyeTemplate(dye, dyes); - } - } - - if (HighlightedColorTableRow >= 0) - ApplyHighlight(ref rows[HighlightedColorTableRow], (float)HighlightTime.Elapsed.TotalSeconds); - - foreach (var previewer in ColorTablePreviewers) - { - // TODO: Dawntrail - rows.AsHalves().CopyTo(previewer.ColorTable); - previewer.ScheduleUpdate(); - } - } - - private static void ApplyHighlight(ref LegacyColorTableRow row, float time) - { - var level = (MathF.Sin(time * 2.0f * MathF.PI) + 2.0f) / 3.0f / 255.0f; - var baseColor = ColorId.InGameHighlight.Value(); - var color = level * new Vector3(baseColor & 0xFF, (baseColor >> 8) & 0xFF, (baseColor >> 16) & 0xFF); - - row.Diffuse = Vector3.Zero; - row.Specular = Vector3.Zero; - row.Emissive = color * color; - } - - public void Update() - { - UpdateShaders(); - UpdateTextures(); - UpdateConstants(); - } - - public unsafe MtrlTab(ModEditWindow edit, MtrlFile file, string filePath, bool writable) - { - _edit = edit; - Mtrl = file; - FilePath = filePath; - Writable = writable; - AssociatedBaseDevkit = TryLoadShpkDevkit("_base", out LoadedBaseDevkitPathName); - LoadShpk(FindAssociatedShpk(out _, out _)); - if (writable) - { - _edit._characterBaseDestructor.Subscribe(UnbindFromDrawObjectMaterialInstances, CharacterBaseDestructor.Priority.MtrlTab); - BindToMaterialInstances(); - } - } - - public unsafe void Dispose() - { - UnbindFromMaterialInstances(); - if (Writable) - _edit._characterBaseDestructor.Unsubscribe(UnbindFromDrawObjectMaterialInstances); - } - - // TODO Readd ShadersKnown - public bool Valid - => (true || ShadersKnown) && Mtrl.Valid; - - public byte[] Write() - { - var output = Mtrl.Clone(); - output.GarbageCollect(AssociatedShpk, SamplerIds); - - return output.Write(); - } - - private sealed class DevkitShaderKeyValue - { - public string Label = string.Empty; - public string Description = string.Empty; - } - - private sealed class DevkitShaderKey - { - public string Label = string.Empty; - public string Description = string.Empty; - public Dictionary Values = new(); - } - - private sealed class DevkitSampler - { - public string Label = string.Empty; - public string Description = string.Empty; - public string DefaultTexture = string.Empty; - } - - private enum DevkitConstantType - { - Hidden = -1, - Float = 0, - Integer = 1, - Color = 2, - Enum = 3, - } - - private sealed class DevkitConstantValue - { - public string Label = string.Empty; - public string Description = string.Empty; - public float Value = 0; - } - - private sealed class DevkitConstant - { - public uint Offset = 0; - public uint? Length = null; - public string Group = string.Empty; - public string Label = string.Empty; - public string Description = string.Empty; - public DevkitConstantType Type = DevkitConstantType.Float; - - public float? Minimum = null; - public float? Maximum = null; - public float? Speed = null; - public float RelativeSpeed = 0.0f; - public float Factor = 1.0f; - public float Bias = 0.0f; - public byte Precision = 3; - public string Unit = string.Empty; - - public bool SquaredRgb = false; - public bool Clamped = false; - - public DevkitConstantValue[] Values = Array.Empty(); - - public IConstantEditor? CreateEditor() - => Type switch - { - DevkitConstantType.Hidden => null, - DevkitConstantType.Float => new FloatConstantEditor(Minimum, Maximum, Speed ?? 0.1f, RelativeSpeed, Factor, Bias, Precision, - Unit), - DevkitConstantType.Integer => new IntConstantEditor(ToInteger(Minimum), ToInteger(Maximum), Speed ?? 0.25f, RelativeSpeed, - Factor, Bias, Unit), - DevkitConstantType.Color => new ColorConstantEditor(SquaredRgb, Clamped), - DevkitConstantType.Enum => new EnumConstantEditor(Array.ConvertAll(Values, - value => (value.Label, value.Value, value.Description))), - _ => FloatConstantEditor.Default, - }; - - private static int? ToInteger(float? value) - => value.HasValue ? (int)Math.Clamp(MathF.Round(value.Value), int.MinValue, int.MaxValue) : null; - } - } -} From f3ab1ddbb48f8e1bab94c59d7628b83ed9d29ba8 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Sun, 4 Aug 2024 22:27:34 +0200 Subject: [PATCH 393/433] Add game data file status to support info --- Penumbra/Penumbra.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index dbe06803..557e011c 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -46,6 +46,7 @@ public class Penumbra : IDalamudPlugin private readonly CharacterUtility _characterUtility; private readonly RedrawService _redrawService; private readonly CommunicatorService _communicatorService; + private readonly IDataManager _gameData; private PenumbraWindowSystem? _windowSystem; private bool _disposed; @@ -78,6 +79,7 @@ public class Penumbra : IDalamudPlugin _tempCollections = _services.GetService(); _redrawService = _services.GetService(); _communicatorService = _services.GetService(); + _gameData = _services.GetService(); _services.GetService(); // Initialize because not required anywhere else. _services.GetService(); // Initialize because not required anywhere else. _collectionManager.Caches.CreateNecessaryCaches(); @@ -217,6 +219,7 @@ public class Penumbra : IDalamudPlugin sb.Append($"> **`Root Directory: `** `{_config.ModDirectory}`, {(exists ? "Exists" : "Not Existing")}\n"); sb.Append( $"> **`Free Drive Space: `** {(drive != null ? Functions.HumanReadableSize(drive.AvailableFreeSpace) : "Unknown")}\n"); + sb.Append($"> **`Game Data Files: `** {(_gameData.HasModifiedGameDataFiles ? "Modified" : "Pristine")}\n"); sb.Append($"> **`Auto-Deduplication: `** {_config.AutoDeduplicateOnImport}\n"); sb.Append($"> **`Auto-UI-Reduplication: `** {_config.AutoReduplicateUiOnImport}\n"); sb.Append($"> **`Debug Mode: `** {_config.DebugMode}\n"); From f8b034c42d14c038ed79183a37bee8262b9c8a7b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 4 Aug 2024 22:48:15 +0200 Subject: [PATCH 394/433] Auto-formatting, generous application of ImUtf8, minor cleanups. --- .../Materials/MtrlTab.ColorTable.cs | 246 +++++++++++------- .../Materials/MtrlTab.CommonColorTable.cs | 142 ++++++---- .../Materials/MtrlTab.Constants.cs | 29 ++- .../Materials/MtrlTab.Devkit.cs | 50 ++-- .../Materials/MtrlTab.LegacyColorTable.cs | 6 +- .../Materials/MtrlTab.LivePreview.cs | 129 ++++----- .../Materials/MtrlTab.ShaderPackage.cs | 218 ++++++++-------- .../Materials/MtrlTab.Textures.cs | 29 ++- .../UI/AdvancedWindow/Materials/MtrlTab.cs | 40 +-- .../Materials/MtrlTabFactory.cs | 13 +- .../AdvancedWindow/ModEditWindow.Materials.cs | 21 +- .../ModEditWindow.ShaderPackages.cs | 179 +++++++------ .../AdvancedWindow/ModEditWindow.ShpkTab.cs | 62 ++--- 13 files changed, 647 insertions(+), 517 deletions(-) diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs index dc87ec41..352681bb 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs @@ -13,7 +13,7 @@ public partial class MtrlTab { private const float ColorTableScalarSize = 65.0f; - private int _colorTableSelectedPair = 0; + private int _colorTableSelectedPair; private bool DrawColorTable(ColorTable table, ColorDyeTable? dyeTable, bool disabled) { @@ -23,25 +23,27 @@ public partial class MtrlTab private void DrawColorTablePairSelector(ColorTable table, bool disabled) { + var style = ImGui.GetStyle(); + var itemSpacing = style.ItemSpacing.X; + var itemInnerSpacing = style.ItemInnerSpacing.X; + var framePadding = style.FramePadding; + var buttonWidth = (ImGui.GetContentRegionAvail().X - itemSpacing * 7.0f) * 0.125f; + var frameHeight = ImGui.GetFrameHeight(); + var highlighterSize = ImUtf8.CalcIconSize(FontAwesomeIcon.Crosshairs) + framePadding * 2.0f; + var spaceWidth = ImUtf8.CalcTextSize(" "u8).X; + var spacePadding = (int)MathF.Ceiling((highlighterSize.X + framePadding.X + itemInnerSpacing) / spaceWidth); + using var font = ImRaii.PushFont(UiBuilder.MonoFont); using var alignment = ImRaii.PushStyle(ImGuiStyleVar.ButtonTextAlign, new Vector2(0, 0.5f)); - var style = ImGui.GetStyle(); - var itemSpacing = style.ItemSpacing.X; - var itemInnerSpacing = style.ItemInnerSpacing.X; - var framePadding = style.FramePadding; - var buttonWidth = (ImGui.GetContentRegionAvail().X - itemSpacing * 7.0f) * 0.125f; - var frameHeight = ImGui.GetFrameHeight(); - var highlighterSize = ImUtf8.CalcIconSize(FontAwesomeIcon.Crosshairs) + framePadding * 2.0f; - var spaceWidth = ImUtf8.CalcTextSize(" "u8).X; - var spacePadding = (int)MathF.Ceiling((highlighterSize.X + framePadding.X + itemInnerSpacing) / spaceWidth); for (var i = 0; i < ColorTable.NumRows >> 1; i += 8) { for (var j = 0; j < 8; ++j) { var pairIndex = i + j; - using (var color = ImRaii.PushColor(ImGuiCol.Button, ImGui.GetColorU32(ImGuiCol.ButtonActive), pairIndex == _colorTableSelectedPair)) + using (ImRaii.PushColor(ImGuiCol.Button, ImGui.GetColorU32(ImGuiCol.ButtonActive), pairIndex == _colorTableSelectedPair)) { - if (ImUtf8.Button($"#{pairIndex + 1}".PadLeft(3 + spacePadding), new Vector2(buttonWidth, ImGui.GetFrameHeightWithSpacing() + frameHeight))) + if (ImUtf8.Button($"#{pairIndex + 1}".PadLeft(3 + spacePadding), + new Vector2(buttonWidth, ImGui.GetFrameHeightWithSpacing() + frameHeight))) _colorTableSelectedPair = pairIndex; } @@ -79,12 +81,10 @@ public partial class MtrlTab private bool DrawColorTablePairEditor(ColorTable table, ColorDyeTable? dyeTable, bool disabled) { - var retA = false; - var retB = false; - ref var rowA = ref table[_colorTableSelectedPair << 1]; - ref var rowB = ref table[(_colorTableSelectedPair << 1) | 1]; - var dyeA = dyeTable != null ? dyeTable[_colorTableSelectedPair << 1] : default; - var dyeB = dyeTable != null ? dyeTable[(_colorTableSelectedPair << 1) | 1] : default; + var retA = false; + var retB = false; + var dyeA = dyeTable?[_colorTableSelectedPair << 1] ?? default; + var dyeB = dyeTable?[(_colorTableSelectedPair << 1) | 1] ?? default; var previewDyeA = _stainService.GetStainCombo(dyeA.Channel).CurrentSelection.Key; var previewDyeB = _stainService.GetStainCombo(dyeB.Channel).CurrentSelection.Key; var dyePackA = _stainService.GudStmFile.GetValueOrNull(dyeA.Template, previewDyeA); @@ -108,68 +108,96 @@ public partial class MtrlTab using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) { using var dis = ImRaii.Disabled(disabled); - using (var id = ImUtf8.PushId("ColorsA"u8)) + using (ImUtf8.PushId("ColorsA"u8)) + { retA |= DrawColors(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + } + columns.Next(); - using (var id = ImUtf8.PushId("ColorsB"u8)) + using (ImUtf8.PushId("ColorsB"u8)) + { retB |= DrawColors(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + } } DrawHeader(" Physical Parameters"u8); using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) { using var dis = ImRaii.Disabled(disabled); - using (var id = ImUtf8.PushId("PbrA"u8)) + using (ImUtf8.PushId("PbrA"u8)) + { retA |= DrawPbr(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + } + columns.Next(); - using (var id = ImUtf8.PushId("PbrB"u8)) + using (ImUtf8.PushId("PbrB"u8)) + { retB |= DrawPbr(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + } } DrawHeader(" Sheen Layer Parameters"u8); using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) { using var dis = ImRaii.Disabled(disabled); - using (var id = ImUtf8.PushId("SheenA"u8)) + using (ImUtf8.PushId("SheenA"u8)) + { retA |= DrawSheen(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + } + columns.Next(); - using (var id = ImUtf8.PushId("SheenB"u8)) + using (ImUtf8.PushId("SheenB"u8)) + { retB |= DrawSheen(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + } } DrawHeader(" Pair Blending"u8); using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) { using var dis = ImRaii.Disabled(disabled); - using (var id = ImUtf8.PushId("BlendingA"u8)) + using (ImUtf8.PushId("BlendingA"u8)) + { retA |= DrawBlending(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + } + columns.Next(); - using (var id = ImUtf8.PushId("BlendingB"u8)) + using (ImUtf8.PushId("BlendingB"u8)) + { retB |= DrawBlending(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + } } DrawHeader(" Material Template"u8); using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) { using var dis = ImRaii.Disabled(disabled); - using (var id = ImUtf8.PushId("TemplateA"u8)) + using (ImUtf8.PushId("TemplateA"u8)) + { retA |= DrawTemplate(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + } + columns.Next(); - using (var id = ImUtf8.PushId("TemplateB"u8)) + using (ImUtf8.PushId("TemplateB"u8)) + { retB |= DrawTemplate(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + } } if (dyeTable != null) { DrawHeader(" Dye Properties"u8); - using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) + using var columns = ImUtf8.Columns(2, "ColorTable"u8); + using var dis = ImRaii.Disabled(disabled); + using (ImUtf8.PushId("DyeA"u8)) { - using var dis = ImRaii.Disabled(disabled); - using (var id = ImUtf8.PushId("DyeA"u8)) - retA |= DrawDye(dyeTable, dyePackA, _colorTableSelectedPair << 1); - columns.Next(); - using (var id = ImUtf8.PushId("DyeB"u8)) - retB |= DrawDye(dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + retA |= DrawDye(dyeTable, dyePackA, _colorTableSelectedPair << 1); + } + + columns.Next(); + using (ImUtf8.PushId("DyeB"u8)) + { + retB |= DrawDye(dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); } } @@ -177,11 +205,16 @@ public partial class MtrlTab using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) { using var dis = ImRaii.Disabled(disabled); - using (var id = ImUtf8.PushId("FurtherA"u8)) + using (ImUtf8.PushId("FurtherA"u8)) + { retA |= DrawFurther(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + } + columns.Next(); - using (var id = ImUtf8.PushId("FurtherB"u8)) + using (ImUtf8.PushId("FurtherB"u8)) + { retB |= DrawFurther(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + } } if (retA) @@ -195,18 +228,21 @@ public partial class MtrlTab /// Padding styles do not seem to apply to this component. It is recommended to prepend two spaces. private static void DrawHeader(ReadOnlySpan label) { - var headerColor = ImGui.GetColorU32(ImGuiCol.Header); - using var _ = ImRaii.PushColor(ImGuiCol.HeaderHovered, headerColor).Push(ImGuiCol.HeaderActive, headerColor); + var headerColor = ImGui.GetColorU32(ImGuiCol.Header); + using var _ = ImRaii.PushColor(ImGuiCol.HeaderHovered, headerColor).Push(ImGuiCol.HeaderActive, headerColor); ImUtf8.CollapsingHeader(label, ImGuiTreeNodeFlags.Leaf); } private static bool DrawColors(ColorTable table, ColorDyeTable? dyeTable, DyePack? dyePack, int rowIdx) { - var dyeOffset = ImGui.GetContentRegionAvail().X + ImGui.GetStyle().ItemSpacing.X - ImGui.GetStyle().ItemInnerSpacing.X - ImGui.GetFrameHeight() * 2.0f; + var dyeOffset = ImGui.GetContentRegionAvail().X + + ImGui.GetStyle().ItemSpacing.X + - ImGui.GetStyle().ItemInnerSpacing.X + - ImGui.GetFrameHeight() * 2.0f; - var ret = false; + var ret = false; ref var row = ref table[rowIdx]; - var dye = dyeTable != null ? dyeTable[rowIdx] : default; + var dye = dyeTable?[rowIdx] ?? default; ret |= CtColorPicker("Diffuse Color"u8, default, row.DiffuseColor, c => table[rowIdx].DiffuseColor = c); @@ -247,13 +283,17 @@ public partial class MtrlTab private static bool DrawBlending(ColorTable table, ColorDyeTable? dyeTable, DyePack? dyePack, int rowIdx) { var scalarSize = ColorTableScalarSize * UiHelpers.Scale; - var dyeOffset = ImGui.GetContentRegionAvail().X + ImGui.GetStyle().ItemSpacing.X - ImGui.GetStyle().ItemInnerSpacing.X - ImGui.GetFrameHeight() - scalarSize; + var dyeOffset = ImGui.GetContentRegionAvail().X + + ImGui.GetStyle().ItemSpacing.X + - ImGui.GetStyle().ItemInnerSpacing.X + - ImGui.GetFrameHeight() + - scalarSize; var isRowB = (rowIdx & 1) != 0; - var ret = false; + var ret = false; ref var row = ref table[rowIdx]; - var dye = dyeTable != null ? dyeTable[rowIdx] : default; + var dye = dyeTable?[rowIdx] ?? default; ImGui.SetNextItemWidth(scalarSize); ret |= CtDragHalf(isRowB ? "Field #19"u8 : "Anisotropy Degree"u8, default, row.Anisotropy, "%.2f"u8, 0.0f, HalfMaxValue, 0.1f, @@ -261,11 +301,13 @@ public partial class MtrlTab if (dyeTable != null) { ImGui.SameLine(dyeOffset); - ret |= CtApplyStainCheckbox("##dyeAnisotropy"u8, isRowB ? "Apply Field #19 on Dye"u8 : "Apply Anisotropy Degree on Dye"u8, dye.Anisotropy, + ret |= CtApplyStainCheckbox("##dyeAnisotropy"u8, isRowB ? "Apply Field #19 on Dye"u8 : "Apply Anisotropy Degree on Dye"u8, + dye.Anisotropy, b => dyeTable[rowIdx].Anisotropy = b); ImUtf8.SameLineInner(); ImGui.SetNextItemWidth(scalarSize); - CtDragHalf("##dyePreviewAnisotropy"u8, isRowB ? "Dye Preview for Field #19"u8 : "Dye Preview for Anisotropy Degree"u8, dyePack?.Anisotropy, "%.2f"u8); + CtDragHalf("##dyePreviewAnisotropy"u8, isRowB ? "Dye Preview for Field #19"u8 : "Dye Preview for Anisotropy Degree"u8, + dyePack?.Anisotropy, "%.2f"u8); } return ret; @@ -276,11 +318,11 @@ public partial class MtrlTab var scalarSize = ColorTableScalarSize * UiHelpers.Scale; var itemSpacing = ImGui.GetStyle().ItemSpacing.X; var dyeOffset = ImGui.GetContentRegionAvail().X - ImGui.GetStyle().ItemInnerSpacing.X - ImGui.GetFrameHeight() - scalarSize - 64.0f; - var subcolWidth = CalculateSubcolumnWidth(2); + var subColWidth = CalculateSubColumnWidth(2); - var ret = false; + var ret = false; ref var row = ref table[rowIdx]; - var dye = dyeTable != null ? dyeTable[rowIdx] : default; + var dye = dyeTable?[rowIdx] ?? default; ImGui.SetNextItemWidth(scalarSize); ret |= CtDragScalar("Shader ID"u8, default, row.ShaderId, "%d"u8, (ushort)0, (ushort)255, 0.25f, @@ -306,13 +348,15 @@ public partial class MtrlTab ImGui.SetCursorScreenPos(ImGui.GetCursorScreenPos() with { Y = cursor.Y }); ImGui.SetNextItemWidth(scalarSize + itemSpacing + 64.0f); using var dis = ImRaii.Disabled(); - CtSphereMapIndexPicker("###SphereMapIndexDye"u8, "Dye Preview for Sphere Map"u8, dyePack?.SphereMapIndex ?? ushort.MaxValue, false, Nop); + CtSphereMapIndexPicker("###SphereMapIndexDye"u8, "Dye Preview for Sphere Map"u8, dyePack?.SphereMapIndex ?? ushort.MaxValue, false, + Nop); } ImGui.Dummy(new Vector2(64.0f, 0.0f)); ImGui.SameLine(); ImGui.SetNextItemWidth(scalarSize); - ret |= CtDragScalar("Sphere Map Intensity"u8, default, (float)row.SphereMapMask * 100.0f, "%.0f%%"u8, HalfMinValue * 100.0f, HalfMaxValue * 100.0f, 1.0f, + ret |= CtDragScalar("Sphere Map Intensity"u8, default, (float)row.SphereMapMask * 100.0f, "%.0f%%"u8, HalfMinValue * 100.0f, + HalfMaxValue * 100.0f, 1.0f, v => table[rowIdx].SphereMapMask = (Half)(v * 0.01f)); if (dyeTable != null) { @@ -326,10 +370,10 @@ public partial class MtrlTab ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - var leftLineHeight = 64.0f + ImGui.GetStyle().FramePadding.Y * 2.0f; + var leftLineHeight = 64.0f + ImGui.GetStyle().FramePadding.Y * 2.0f; var rightLineHeight = 3.0f * ImGui.GetFrameHeight() + 2.0f * ImGui.GetStyle().ItemSpacing.Y; - var lineHeight = Math.Max(leftLineHeight, rightLineHeight); - var cursorPos = ImGui.GetCursorScreenPos(); + var lineHeight = Math.Max(leftLineHeight, rightLineHeight); + var cursorPos = ImGui.GetCursorScreenPos(); ImGui.SetCursorScreenPos(cursorPos + new Vector2(0.0f, (lineHeight - leftLineHeight) * 0.5f)); ImGui.SetNextItemWidth(scalarSize + (itemSpacing + 64.0f) * 2.0f); ret |= CtTileIndexPicker("###TileIndex"u8, default, row.TileIndex, false, @@ -337,9 +381,10 @@ public partial class MtrlTab ImUtf8.SameLineInner(); ImUtf8.Text("Tile"u8); - ImGui.SameLine(subcolWidth); - ImGui.SetCursorScreenPos(ImGui.GetCursorScreenPos() with { Y = cursorPos.Y + (lineHeight - rightLineHeight) * 0.5f, }); - using (var cld = ImUtf8.Child("###TileProperties"u8, new(ImGui.GetContentRegionAvail().X, float.Lerp(rightLineHeight, lineHeight, 0.5f)), false)) + ImGui.SameLine(subColWidth); + ImGui.SetCursorScreenPos(ImGui.GetCursorScreenPos() with { Y = cursorPos.Y + (lineHeight - rightLineHeight) * 0.5f }); + using (ImUtf8.Child("###TileProperties"u8, + new Vector2(ImGui.GetContentRegionAvail().X, float.Lerp(rightLineHeight, lineHeight, 0.5f)))) { ImGui.Dummy(new Vector2(scalarSize, 0.0f)); ImUtf8.SameLineInner(); @@ -350,7 +395,8 @@ public partial class MtrlTab ret |= CtTileTransformMatrix(row.TileTransform, scalarSize, true, m => table[rowIdx].TileTransform = m); ImUtf8.SameLineInner(); - ImGui.SetCursorScreenPos(ImGui.GetCursorScreenPos() - new Vector2(0.0f, (ImGui.GetFrameHeight() + ImGui.GetStyle().ItemSpacing.Y) * 0.5f)); + ImGui.SetCursorScreenPos(ImGui.GetCursorScreenPos() + - new Vector2(0.0f, (ImGui.GetFrameHeight() + ImGui.GetStyle().ItemSpacing.Y) * 0.5f)); ImUtf8.Text("Tile Transform"u8); } @@ -360,15 +406,20 @@ public partial class MtrlTab private static bool DrawPbr(ColorTable table, ColorDyeTable? dyeTable, DyePack? dyePack, int rowIdx) { var scalarSize = ColorTableScalarSize * UiHelpers.Scale; - var subcolWidth = CalculateSubcolumnWidth(2) + ImGui.GetStyle().ItemSpacing.X; - var dyeOffset = subcolWidth - ImGui.GetStyle().ItemSpacing.X * 2.0f - ImGui.GetStyle().ItemInnerSpacing.X - ImGui.GetFrameHeight() - scalarSize; + var subColWidth = CalculateSubColumnWidth(2) + ImGui.GetStyle().ItemSpacing.X; + var dyeOffset = subColWidth + - ImGui.GetStyle().ItemSpacing.X * 2.0f + - ImGui.GetStyle().ItemInnerSpacing.X + - ImGui.GetFrameHeight() + - scalarSize; - var ret = false; + var ret = false; ref var row = ref table[rowIdx]; - var dye = dyeTable != null ? dyeTable[rowIdx] : default; + var dye = dyeTable?[rowIdx] ?? default; ImGui.SetNextItemWidth(scalarSize); - ret |= CtDragScalar("Roughness"u8, default, (float)row.Roughness * 100.0f, "%.0f%%"u8, HalfMinValue * 100.0f, HalfMaxValue * 100.0f, 1.0f, + ret |= CtDragScalar("Roughness"u8, default, (float)row.Roughness * 100.0f, "%.0f%%"u8, HalfMinValue * 100.0f, HalfMaxValue * 100.0f, + 1.0f, v => table[rowIdx].Roughness = (Half)(v * 0.01f)); if (dyeTable != null) { @@ -380,13 +431,14 @@ public partial class MtrlTab CtDragScalar("##dyePreviewRoughness"u8, "Dye Preview for Roughness"u8, (float?)dyePack?.Roughness * 100.0f, "%.0f%%"u8); } - ImGui.SameLine(subcolWidth); + ImGui.SameLine(subColWidth); ImGui.SetNextItemWidth(scalarSize); - ret |= CtDragScalar("Metalness"u8, default, (float)row.Metalness * 100.0f, "%.0f%%"u8, HalfMinValue * 100.0f, HalfMaxValue * 100.0f, 1.0f, + ret |= CtDragScalar("Metalness"u8, default, (float)row.Metalness * 100.0f, "%.0f%%"u8, HalfMinValue * 100.0f, HalfMaxValue * 100.0f, + 1.0f, v => table[rowIdx].Metalness = (Half)(v * 0.01f)); if (dyeTable != null) { - ImGui.SameLine(subcolWidth + dyeOffset); + ImGui.SameLine(subColWidth + dyeOffset); ret |= CtApplyStainCheckbox("##dyeMetalness"u8, "Apply Metalness on Dye"u8, dye.Metalness, b => dyeTable[rowIdx].Metalness = b); ImUtf8.SameLineInner(); @@ -400,12 +452,16 @@ public partial class MtrlTab private static bool DrawSheen(ColorTable table, ColorDyeTable? dyeTable, DyePack? dyePack, int rowIdx) { var scalarSize = ColorTableScalarSize * UiHelpers.Scale; - var subcolWidth = CalculateSubcolumnWidth(2) + ImGui.GetStyle().ItemSpacing.X; - var dyeOffset = subcolWidth - ImGui.GetStyle().ItemSpacing.X * 2.0f - ImGui.GetStyle().ItemInnerSpacing.X - ImGui.GetFrameHeight() - scalarSize; + var subColWidth = CalculateSubColumnWidth(2) + ImGui.GetStyle().ItemSpacing.X; + var dyeOffset = subColWidth + - ImGui.GetStyle().ItemSpacing.X * 2.0f + - ImGui.GetStyle().ItemInnerSpacing.X + - ImGui.GetFrameHeight() + - scalarSize; - var ret = false; + var ret = false; ref var row = ref table[rowIdx]; - var dye = dyeTable != null ? dyeTable[rowIdx] : default; + var dye = dyeTable?[rowIdx] ?? default; ImGui.SetNextItemWidth(scalarSize); ret |= CtDragScalar("Sheen"u8, default, (float)row.SheenRate * 100.0f, "%.0f%%"u8, HalfMinValue * 100.0f, HalfMaxValue * 100.0f, 1.0f, @@ -420,13 +476,14 @@ public partial class MtrlTab CtDragScalar("##dyePreviewSheenRate"u8, "Dye Preview for Sheen"u8, (float?)dyePack?.SheenRate * 100.0f, "%.0f%%"u8); } - ImGui.SameLine(subcolWidth); + ImGui.SameLine(subColWidth); ImGui.SetNextItemWidth(scalarSize); - ret |= CtDragScalar("Sheen Tint"u8, default, (float)row.SheenTintRate * 100.0f, "%.0f%%"u8, HalfMinValue * 100.0f, HalfMaxValue * 100.0f, 1.0f, + ret |= CtDragScalar("Sheen Tint"u8, default, (float)row.SheenTintRate * 100.0f, "%.0f%%"u8, HalfMinValue * 100.0f, + HalfMaxValue * 100.0f, 1.0f, v => table[rowIdx].SheenTintRate = (Half)(v * 0.01f)); if (dyeTable != null) { - ImGui.SameLine(subcolWidth + dyeOffset); + ImGui.SameLine(subColWidth + dyeOffset); ret |= CtApplyStainCheckbox("##dyeSheenTintRate"u8, "Apply Sheen Tint on Dye"u8, dye.SheenTintRate, b => dyeTable[rowIdx].SheenTintRate = b); ImUtf8.SameLineInner(); @@ -435,7 +492,8 @@ public partial class MtrlTab } ImGui.SetNextItemWidth(scalarSize); - ret |= CtDragScalar("Sheen Roughness"u8, default, 100.0f / (float)row.SheenAperture, "%.0f%%"u8, 100.0f / HalfMaxValue, 100.0f / HalfEpsilon, 1.0f, + ret |= CtDragScalar("Sheen Roughness"u8, default, 100.0f / (float)row.SheenAperture, "%.0f%%"u8, 100.0f / HalfMaxValue, + 100.0f / HalfEpsilon, 1.0f, v => table[rowIdx].SheenAperture = (Half)(100.0f / v)); if (dyeTable != null) { @@ -444,7 +502,8 @@ public partial class MtrlTab b => dyeTable[rowIdx].SheenAperture = b); ImUtf8.SameLineInner(); ImGui.SetNextItemWidth(scalarSize); - CtDragScalar("##dyePreviewSheenRoughness"u8, "Dye Preview for Sheen Roughness"u8, 100.0f / (float?)dyePack?.SheenAperture, "%.0f%%"u8); + CtDragScalar("##dyePreviewSheenRoughness"u8, "Dye Preview for Sheen Roughness"u8, 100.0f / (float?)dyePack?.SheenAperture, + "%.0f%%"u8); } return ret; @@ -453,12 +512,16 @@ public partial class MtrlTab private static bool DrawFurther(ColorTable table, ColorDyeTable? dyeTable, DyePack? dyePack, int rowIdx) { var scalarSize = ColorTableScalarSize * UiHelpers.Scale; - var subcolWidth = CalculateSubcolumnWidth(2) + ImGui.GetStyle().ItemSpacing.X; - var dyeOffset = subcolWidth - ImGui.GetStyle().ItemSpacing.X * 2.0f - ImGui.GetStyle().ItemInnerSpacing.X - ImGui.GetFrameHeight() - scalarSize; + var subColWidth = CalculateSubColumnWidth(2) + ImGui.GetStyle().ItemSpacing.X; + var dyeOffset = subColWidth + - ImGui.GetStyle().ItemSpacing.X * 2.0f + - ImGui.GetStyle().ItemInnerSpacing.X + - ImGui.GetFrameHeight() + - scalarSize; - var ret = false; + var ret = false; ref var row = ref table[rowIdx]; - var dye = dyeTable != null ? dyeTable[rowIdx] : default; + var dye = dyeTable?[rowIdx] ?? default; ImGui.SetNextItemWidth(scalarSize); ret |= CtDragHalf("Field #11"u8, default, row.Scalar11, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f, @@ -479,7 +542,7 @@ public partial class MtrlTab ret |= CtDragHalf("Field #3"u8, default, row.Scalar3, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f, v => table[rowIdx].Scalar3 = v); - ImGui.SameLine(subcolWidth); + ImGui.SameLine(subColWidth); ImGui.SetNextItemWidth(scalarSize); ret |= CtDragHalf("Field #7"u8, default, row.Scalar7, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f, v => table[rowIdx].Scalar7 = v); @@ -488,7 +551,7 @@ public partial class MtrlTab ret |= CtDragHalf("Field #15"u8, default, row.Scalar15, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f, v => table[rowIdx].Scalar15 = v); - ImGui.SameLine(subcolWidth); + ImGui.SameLine(subColWidth); ImGui.SetNextItemWidth(scalarSize); ret |= CtDragHalf("Field #17"u8, default, row.Scalar17, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f, v => table[rowIdx].Scalar17 = v); @@ -497,7 +560,7 @@ public partial class MtrlTab ret |= CtDragHalf("Field #20"u8, default, row.Scalar20, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f, v => table[rowIdx].Scalar20 = v); - ImGui.SameLine(subcolWidth); + ImGui.SameLine(subColWidth); ImGui.SetNextItemWidth(scalarSize); ret |= CtDragHalf("Field #22"u8, default, row.Scalar22, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f, v => table[rowIdx].Scalar22 = v); @@ -513,33 +576,32 @@ public partial class MtrlTab { var scalarSize = ColorTableScalarSize * UiHelpers.Scale; var applyButtonWidth = ImUtf8.CalcTextSize("Apply Preview Dye"u8).X + ImGui.GetStyle().FramePadding.X * 2.0f; - var subcolWidth = CalculateSubcolumnWidth(2, applyButtonWidth); - - var ret = false; + var subColWidth = CalculateSubColumnWidth(2, applyButtonWidth); + + var ret = false; ref var dye = ref dyeTable[rowIdx]; ImGui.SetNextItemWidth(scalarSize); ret |= CtDragScalar("Dye Channel"u8, default, dye.Channel + 1, "%d"u8, 1, StainService.ChannelCount, 0.1f, value => dyeTable[rowIdx].Channel = (byte)(Math.Clamp(value, 1, StainService.ChannelCount) - 1)); - ImGui.SameLine(subcolWidth); + ImGui.SameLine(subColWidth); ImGui.SetNextItemWidth(scalarSize); if (_stainService.GudTemplateCombo.Draw("##dyeTemplate", dye.Template.ToString(), string.Empty, - scalarSize + ImGui.GetStyle().ScrollbarSize / 2, ImGui.GetTextLineHeightWithSpacing(), ImGuiComboFlags.NoArrowButton)) + scalarSize + ImGui.GetStyle().ScrollbarSize / 2, ImGui.GetTextLineHeightWithSpacing(), ImGuiComboFlags.NoArrowButton)) { dye.Template = _stainService.LegacyTemplateCombo.CurrentSelection; ret = true; } + ImUtf8.SameLineInner(); ImUtf8.Text("Dye Template"u8); ImGui.SameLine(ImGui.GetContentRegionAvail().X - applyButtonWidth + ImGui.GetStyle().ItemSpacing.X); using var dis = ImRaii.Disabled(!dyePack.HasValue); if (ImUtf8.Button("Apply Preview Dye"u8)) - { ret |= Mtrl.ApplyDyeToRow(_stainService.GudStmFile, [ _stainService.StainCombo1.CurrentSelection.Key, _stainService.StainCombo2.CurrentSelection.Key, ], rowIdx); - } return ret; } @@ -554,9 +616,9 @@ public partial class MtrlTab ImGui.TextUnformatted(text); } - private static float CalculateSubcolumnWidth(int numSubcolumns, float reservedSpace = 0.0f) + private static float CalculateSubColumnWidth(int numSubColumns, float reservedSpace = 0.0f) { var itemSpacing = ImGui.GetStyle().ItemSpacing.X; - return (ImGui.GetContentRegionAvail().X - reservedSpace - itemSpacing * (numSubcolumns - 1)) / numSubcolumns + itemSpacing; + return (ImGui.GetContentRegionAvail().X - reservedSpace - itemSpacing * (numSubColumns - 1)) / numSubColumns + itemSpacing; } } diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs index 2b093e23..09c8ea61 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs @@ -12,9 +12,9 @@ namespace Penumbra.UI.AdvancedWindow.Materials; public partial class MtrlTab { - private static readonly float HalfMinValue = (float)Half.MinValue; - private static readonly float HalfMaxValue = (float)Half.MaxValue; - private static readonly float HalfEpsilon = (float)Half.Epsilon; + private static readonly float HalfMinValue = (float)Half.MinValue; + private static readonly float HalfMaxValue = (float)Half.MaxValue; + private static readonly float HalfEpsilon = (float)Half.Epsilon; private static readonly FontAwesomeCheckbox ApplyStainCheckbox = new(FontAwesomeIcon.FillDrip); @@ -22,11 +22,11 @@ public partial class MtrlTab private bool DrawColorTableSection(bool disabled) { - if ((!ShpkLoading && !SamplerIds.Contains(ShpkFile.TableSamplerId)) || Mtrl.Table == null) + if (!_shpkLoading && !SamplerIds.Contains(ShpkFile.TableSamplerId) || Mtrl.Table == null) return false; ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - if (!ImGui.CollapsingHeader("Color Table", ImGuiTreeNodeFlags.DefaultOpen)) + if (!ImUtf8.CollapsingHeader("Color Table"u8, ImGuiTreeNodeFlags.DefaultOpen)) return false; ColorTableCopyAllClipboardButton(); @@ -35,7 +35,7 @@ public partial class MtrlTab if (!disabled) { ImGui.SameLine(); - ImGui.Dummy(ImGuiHelpers.ScaledVector2(20, 0)); + ImUtf8.IconDummy(); ImGui.SameLine(); ret |= ColorTableDyeableCheckbox(); } @@ -43,17 +43,18 @@ public partial class MtrlTab if (Mtrl.DyeTable != null) { ImGui.SameLine(); - ImGui.Dummy(ImGuiHelpers.ScaledVector2(20, 0)); + ImUtf8.IconDummy(); ImGui.SameLine(); ret |= DrawPreviewDye(disabled); } ret |= Mtrl.Table switch { - LegacyColorTable legacyTable => DrawLegacyColorTable(legacyTable, Mtrl.DyeTable as LegacyColorDyeTable, disabled), - ColorTable table when Mtrl.ShaderPackage.Name is "characterlegacy.shpk" => DrawLegacyColorTable(table, Mtrl.DyeTable as ColorDyeTable, disabled), - ColorTable table => DrawColorTable(table, Mtrl.DyeTable as ColorDyeTable, disabled), - _ => false, + LegacyColorTable legacyTable => DrawLegacyColorTable(legacyTable, Mtrl.DyeTable as LegacyColorDyeTable, disabled), + ColorTable table when Mtrl.ShaderPackage.Name is "characterlegacy.shpk" => DrawLegacyColorTable(table, + Mtrl.DyeTable as ColorDyeTable, disabled), + ColorTable table => DrawColorTable(table, Mtrl.DyeTable as ColorDyeTable, disabled), + _ => false, }; return ret; @@ -64,7 +65,7 @@ public partial class MtrlTab if (Mtrl.Table == null) return; - if (!ImGui.Button("Export All Rows to Clipboard", ImGuiHelpers.ScaledVector2(200, 0))) + if (!ImUtf8.Button("Export All Rows to Clipboard"u8, ImGuiHelpers.ScaledVector2(200, 0))) return; try @@ -178,16 +179,18 @@ public partial class MtrlTab private bool ColorTableDyeableCheckbox() { var dyeable = Mtrl.DyeTable != null; - var ret = ImGui.Checkbox("Dyeable", ref dyeable); + var ret = ImUtf8.Checkbox("Dyeable"u8, ref dyeable); if (ret) { - Mtrl.DyeTable = dyeable ? Mtrl.Table switch - { - ColorTable => new ColorDyeTable(), - LegacyColorTable => new LegacyColorDyeTable(), - _ => null, - } : null; + Mtrl.DyeTable = dyeable + ? Mtrl.Table switch + { + ColorTable => new ColorDyeTable(), + LegacyColorTable => new LegacyColorDyeTable(), + _ => null, + } + : null; UpdateColorTablePreview(); } @@ -227,24 +230,27 @@ public partial class MtrlTab private void ColorTableHighlightButton(int pairIdx, bool disabled) { - ImUtf8.IconButton(FontAwesomeIcon.Crosshairs, "Highlight this pair of rows on your character, if possible.\n\nHighlight colors can be configured in Penumbra's settings."u8, - ImGui.GetFrameHeight() * Vector2.One, disabled || ColorTablePreviewers.Count == 0); + ImUtf8.IconButton(FontAwesomeIcon.Crosshairs, + "Highlight this pair of rows on your character, if possible.\n\nHighlight colors can be configured in Penumbra's settings."u8, + ImGui.GetFrameHeight() * Vector2.One, disabled || _colorTablePreviewers.Count == 0); if (ImGui.IsItemHovered()) HighlightColorTablePair(pairIdx); - else if (HighlightedColorTablePair == pairIdx) + else if (_highlightedColorTablePair == pairIdx) CancelColorTableHighlight(); } private static void CtBlendRect(Vector2 rcMin, Vector2 rcMax, uint topColor, uint bottomColor) { - var style = ImGui.GetStyle(); - var frameRounding = style.FrameRounding; + var style = ImGui.GetStyle(); + var frameRounding = style.FrameRounding; var frameThickness = style.FrameBorderSize; - var borderColor = ImGui.GetColorU32(ImGuiCol.Border); - var drawList = ImGui.GetWindowDrawList(); + var borderColor = ImGui.GetColorU32(ImGuiCol.Border); + var drawList = ImGui.GetWindowDrawList(); if (topColor == bottomColor) + { drawList.AddRectFilled(rcMin, rcMax, topColor, frameRounding, ImDrawFlags.RoundCornersDefault); + } else { drawList.AddRectFilled( @@ -258,10 +264,12 @@ public partial class MtrlTab rcMin with { Y = float.Lerp(rcMin.Y, rcMax.Y, 2.0f / 3) }, rcMax, bottomColor, frameRounding, ImDrawFlags.RoundCornersBottomLeft | ImDrawFlags.RoundCornersBottomRight); } + drawList.AddRect(rcMin, rcMax, borderColor, frameRounding, ImDrawFlags.RoundCornersDefault, frameThickness); } - private static bool CtColorPicker(ReadOnlySpan label, ReadOnlySpan description, HalfColor current, Action setter, ReadOnlySpan letter = default) + private static bool CtColorPicker(ReadOnlySpan label, ReadOnlySpan description, HalfColor current, Action setter, + ReadOnlySpan letter = default) { var ret = false; var inputSqrt = PseudoSqrtRgb((Vector3)current); @@ -291,10 +299,13 @@ public partial class MtrlTab return ret; } - private static void CtColorPicker(ReadOnlySpan label, ReadOnlySpan description, HalfColor? current, ReadOnlySpan letter = default) + private static void CtColorPicker(ReadOnlySpan label, ReadOnlySpan description, HalfColor? current, + ReadOnlySpan letter = default) { if (current.HasValue) + { CtColorPicker(label, description, current.Value, Nop, letter); + } else { var tmp = Vector4.Zero; @@ -308,8 +319,8 @@ public partial class MtrlTab if (letter.Length > 0 && ImGui.IsItemVisible()) { - var textSize = ImUtf8.CalcTextSize(letter); - var center = ImGui.GetItemRectMin() + (ImGui.GetItemRectSize() - textSize) / 2; + var textSize = ImUtf8.CalcTextSize(letter); + var center = ImGui.GetItemRectMin() + (ImGui.GetItemRectSize() - textSize) / 2; ImGui.GetWindowDrawList().AddText(letter, center, 0x80000000u); } @@ -319,7 +330,7 @@ public partial class MtrlTab private static bool CtApplyStainCheckbox(ReadOnlySpan label, ReadOnlySpan description, bool current, Action setter) { - var tmp = current; + var tmp = current; var result = ApplyStainCheckbox.Draw(label, ref tmp); ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, description); if (!result || tmp == current) @@ -329,68 +340,79 @@ public partial class MtrlTab return true; } - private static bool CtDragHalf(ReadOnlySpan label, ReadOnlySpan description, Half value, ReadOnlySpan format, float min, float max, float speed, Action setter) + private static bool CtDragHalf(ReadOnlySpan label, ReadOnlySpan description, Half value, ReadOnlySpan format, float min, + float max, float speed, Action setter) { - var tmp = (float)value; + var tmp = (float)value; var result = ImUtf8.DragScalar(label, ref tmp, format, min, max, speed); ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, description); if (!result) return false; + var newValue = (Half)tmp; if (newValue == value) return false; + setter(newValue); return true; } - private static bool CtDragHalf(ReadOnlySpan label, ReadOnlySpan description, ref Half value, ReadOnlySpan format, float min, float max, float speed) + private static bool CtDragHalf(ReadOnlySpan label, ReadOnlySpan description, ref Half value, ReadOnlySpan format, + float min, float max, float speed) { - var tmp = (float)value; + var tmp = (float)value; var result = ImUtf8.DragScalar(label, ref tmp, format, min, max, speed); ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, description); if (!result) return false; + var newValue = (Half)tmp; if (newValue == value) return false; + value = newValue; return true; } private static void CtDragHalf(ReadOnlySpan label, ReadOnlySpan description, Half? value, ReadOnlySpan format) { - using var _ = ImRaii.Disabled(); - var valueOrDefault = value ?? Half.Zero; - var floatValue = (float)valueOrDefault; + using var _ = ImRaii.Disabled(); + var valueOrDefault = value ?? Half.Zero; + var floatValue = (float)valueOrDefault; CtDragHalf(label, description, valueOrDefault, value.HasValue ? format : "-"u8, floatValue, floatValue, 0.0f, Nop); } - private static bool CtDragScalar(ReadOnlySpan label, ReadOnlySpan description, T value, ReadOnlySpan format, T min, T max, float speed, Action setter) where T : unmanaged, INumber + private static bool CtDragScalar(ReadOnlySpan label, ReadOnlySpan description, T value, ReadOnlySpan format, T min, + T max, float speed, Action setter) where T : unmanaged, INumber { - var tmp = value; + var tmp = value; var result = ImUtf8.DragScalar(label, ref tmp, format, min, max, speed); ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, description); if (!result || tmp == value) return false; + setter(tmp); return true; } - private static bool CtDragScalar(ReadOnlySpan label, ReadOnlySpan description, ref T value, ReadOnlySpan format, T min, T max, float speed) where T : unmanaged, INumber + private static bool CtDragScalar(ReadOnlySpan label, ReadOnlySpan description, ref T value, ReadOnlySpan format, T min, + T max, float speed) where T : unmanaged, INumber { - var tmp = value; + var tmp = value; var result = ImUtf8.DragScalar(label, ref tmp, format, min, max, speed); ImUtf8.HoverTooltip(ImGuiHoveredFlags.AllowWhenDisabled, description); if (!result || tmp == value) return false; + value = tmp; return true; } - private static void CtDragScalar(ReadOnlySpan label, ReadOnlySpan description, T? value, ReadOnlySpan format) where T : unmanaged, INumber + private static void CtDragScalar(ReadOnlySpan label, ReadOnlySpan description, T? value, ReadOnlySpan format) + where T : unmanaged, INumber { - using var _ = ImRaii.Disabled(); - var valueOrDefault = value ?? T.Zero; + using var _ = ImRaii.Disabled(); + var valueOrDefault = value ?? T.Zero; CtDragScalar(label, description, valueOrDefault, value.HasValue ? format : "-"u8, valueOrDefault, valueOrDefault, 0.0f, Nop); } @@ -398,14 +420,17 @@ public partial class MtrlTab { if (!_materialTemplatePickers.DrawTileIndexPicker(label, description, ref value, compact)) return false; + setter(value); return true; } - private bool CtSphereMapIndexPicker(ReadOnlySpan label, ReadOnlySpan description, ushort value, bool compact, Action setter) + private bool CtSphereMapIndexPicker(ReadOnlySpan label, ReadOnlySpan description, ushort value, bool compact, + Action setter) { if (!_materialTemplatePickers.DrawSphereMapIndexPicker(label, description, ref value, compact)) return false; + setter(value); return true; } @@ -430,33 +455,34 @@ public partial class MtrlTab ret |= CtDragHalf("##TileTransformVU"u8, "Tile Skew V"u8, ref tmp.VU, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f); if (!ret || tmp == value) return false; + setter(tmp); } else { value.Decompose(out var scale, out var rotation, out var shear); rotation *= 180.0f / MathF.PI; - shear *= 180.0f / MathF.PI; + shear *= 180.0f / MathF.PI; ImGui.SetNextItemWidth(floatSize); var scaleXChanged = CtDragScalar("##TileScaleU"u8, "Tile Scale U"u8, ref scale.X, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f); var activated = ImGui.IsItemActivated(); var deactivated = ImGui.IsItemDeactivated(); ImUtf8.SameLineInner(); ImGui.SetNextItemWidth(floatSize); - var scaleYChanged = CtDragScalar("##TileScaleV"u8, "Tile Scale V"u8, ref scale.Y, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f); - activated |= ImGui.IsItemActivated(); - deactivated |= ImGui.IsItemDeactivated(); + var scaleYChanged = CtDragScalar("##TileScaleV"u8, "Tile Scale V"u8, ref scale.Y, "%.2f"u8, HalfMinValue, HalfMaxValue, 0.1f); + activated |= ImGui.IsItemActivated(); + deactivated |= ImGui.IsItemDeactivated(); if (!twoRowLayout) ImUtf8.SameLineInner(); ImGui.SetNextItemWidth(floatSize); - var rotationChanged = CtDragScalar("##TileRotation"u8, "Tile Rotation"u8, ref rotation, "%.0f°"u8, -180.0f, 180.0f, 1.0f); - activated |= ImGui.IsItemActivated(); - deactivated |= ImGui.IsItemDeactivated(); + var rotationChanged = CtDragScalar("##TileRotation"u8, "Tile Rotation"u8, ref rotation, "%.0f°"u8, -180.0f, 180.0f, 1.0f); + activated |= ImGui.IsItemActivated(); + deactivated |= ImGui.IsItemDeactivated(); ImUtf8.SameLineInner(); ImGui.SetNextItemWidth(floatSize); - var shearChanged = CtDragScalar("##TileShear"u8, "Tile Shear"u8, ref shear, "%.0f°"u8, -90.0f, 90.0f, 1.0f); - activated |= ImGui.IsItemActivated(); - deactivated |= ImGui.IsItemDeactivated(); + var shearChanged = CtDragScalar("##TileShear"u8, "Tile Shear"u8, ref shear, "%.0f°"u8, -90.0f, 90.0f, 1.0f); + activated |= ImGui.IsItemActivated(); + deactivated |= ImGui.IsItemDeactivated(); if (deactivated) _pinnedTileTransform = null; else if (activated) @@ -464,6 +490,7 @@ public partial class MtrlTab ret = scaleXChanged | scaleYChanged | rotationChanged | shearChanged; if (!ret) return false; + if (_pinnedTileTransform.HasValue) { var (pinScale, pinRotation, pinShear) = _pinnedTileTransform.Value; @@ -476,11 +503,14 @@ public partial class MtrlTab if (!shearChanged) shear = pinShear; } + var newValue = HalfMatrix2x2.Compose(scale, rotation * MathF.PI / 180.0f, shear * MathF.PI / 180.0f); if (newValue == value) return false; + setter(newValue); } + return true; } diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Constants.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Constants.cs index 56496005..176ec3f4 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Constants.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Constants.cs @@ -35,7 +35,7 @@ public partial class MtrlTab Constants.Clear(); string mpPrefix; - if (AssociatedShpk == null) + if (_associatedShpk == null) { mpPrefix = MaterialParamsConstantName.Value!; var fcGroup = FindOrAddGroup(Constants, "Further Constants"); @@ -51,12 +51,12 @@ public partial class MtrlTab } else { - mpPrefix = AssociatedShpk.GetConstantById(MaterialParamsConstantId)?.Name ?? MaterialParamsConstantName.Value!; + mpPrefix = _associatedShpk.GetConstantById(MaterialParamsConstantId)?.Name ?? MaterialParamsConstantName.Value!; var autoNameMaxLength = Math.Max(Names.LongestKnownNameLength, mpPrefix.Length + 8); - foreach (var shpkConstant in AssociatedShpk.MaterialParams) + foreach (var shpkConstant in _associatedShpk.MaterialParams) { var name = Names.KnownNames.TryResolve(shpkConstant.Id); - var constant = Mtrl.GetOrAddConstant(shpkConstant.Id, AssociatedShpk, out var constantIndex); + var constant = Mtrl.GetOrAddConstant(shpkConstant.Id, _associatedShpk, out var constantIndex); var values = Mtrl.GetConstantValue(constant); var handledElements = new IndexSet(values.Length, false); @@ -64,8 +64,8 @@ public partial class MtrlTab if (dkData != null) foreach (var dkConstant in dkData) { - var offset = (int)dkConstant.EffectiveByteOffset; - var length = values.Length - offset; + var offset = (int)dkConstant.EffectiveByteOffset; + var length = values.Length - offset; var constantSize = dkConstant.EffectiveByteSize; if (constantSize.HasValue) length = Math.Min(length, (int)constantSize.Value); @@ -86,7 +86,6 @@ public partial class MtrlTab foreach (var (start, end) in handledElements.Ranges(complement: true)) { if (start == 0 && end == values.Length && end - start <= 16) - { if (name.Value != null) { fcGroup.Add(( @@ -94,7 +93,6 @@ public partial class MtrlTab constantIndex, 0..values.Length, string.Empty, true, DefaultConstantEditorFor(name))); continue; } - } if ((shpkConstant.ByteOffset & 0x3) == 0 && (shpkConstant.ByteSize & 0x3) == 0) { @@ -105,7 +103,8 @@ public partial class MtrlTab var rangeEnd = Math.Min(i + 16, end); if (rangeEnd > rangeStart) { - var autoName = $"{mpPrefix}[{j,2:D}]{VectorSwizzle(((offset + rangeStart) & 0xF) >> 2, ((offset + rangeEnd - 1) & 0xF) >> 2)}"; + var autoName = + $"{mpPrefix}[{j,2:D}]{VectorSwizzle(((offset + rangeStart) & 0xF) >> 2, ((offset + rangeEnd - 1) & 0xF) >> 2)}"; fcGroup.Add(( $"{autoName.PadRight(autoNameMaxLength)} (0x{shpkConstant.Id:X8})", constantIndex, rangeStart..rangeEnd, string.Empty, true, DefaultConstantEditorFor(name))); @@ -116,7 +115,8 @@ public partial class MtrlTab { for (var i = start; i < end; i += 16) { - fcGroup.Add(($"{"???".PadRight(autoNameMaxLength)} (0x{shpkConstant.Id:X8})", constantIndex, i..Math.Min(i + 16, end), string.Empty, true, + fcGroup.Add(($"{"???".PadRight(autoNameMaxLength)} (0x{shpkConstant.Id:X8})", constantIndex, + i..Math.Min(i + 16, end), string.Empty, true, DefaultConstantEditorFor(name))); } } @@ -178,15 +178,16 @@ public partial class MtrlTab ret = true; SetMaterialParameter(constant.Id, slice.Start, buffer[slice]); } - var shpkConstant = AssociatedShpk?.GetMaterialParamById(constant.Id); - var defaultConstantValue = shpkConstant.HasValue ? AssociatedShpk!.GetMaterialParamDefault(shpkConstant.Value) : []; + + var shpkConstant = _associatedShpk?.GetMaterialParamById(constant.Id); + var defaultConstantValue = shpkConstant.HasValue ? _associatedShpk!.GetMaterialParamDefault(shpkConstant.Value) : []; var defaultValue = IsValid(slice, defaultConstantValue.Length) ? defaultConstantValue[slice] : []; - var canReset = AssociatedShpk?.MaterialParamsDefaults != null + var canReset = _associatedShpk?.MaterialParamsDefaults != null ? defaultValue.Length > 0 && !defaultValue.SequenceEqual(buffer[slice]) : buffer[slice].ContainsAnyExcept((byte)0); ImUtf8.SameLineInner(); if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Backspace.ToIconString(), ImGui.GetFrameHeight() * Vector2.One, - "Reset this constant to its default value.\n\nHold Ctrl to unlock.", !ImGui.GetIO().KeyCtrl || !canReset, true)) + "Reset this constant to its default value.\n\nHold Ctrl to unlock.", !ImGui.GetIO().KeyCtrl || !canReset, true)) { ret = true; if (defaultValue.Length > 0) diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Devkit.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Devkit.cs index cd62d58f..26fe3dcb 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Devkit.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Devkit.cs @@ -1,3 +1,4 @@ +using JetBrains.Annotations; using Newtonsoft.Json.Linq; using OtterGui.Text.Widget.Editors; using Penumbra.String.Classes; @@ -29,8 +30,8 @@ public partial class MtrlTab } private T? TryGetShpkDevkitData(string category, uint? id, bool mayVary) where T : class - => TryGetShpkDevkitData(AssociatedShpkDevkit, LoadedShpkDevkitPathName, category, id, mayVary) - ?? TryGetShpkDevkitData(AssociatedBaseDevkit, LoadedBaseDevkitPathName, category, id, mayVary); + => TryGetShpkDevkitData(_associatedShpkDevkit, _loadedShpkDevkitPathName, category, id, mayVary) + ?? TryGetShpkDevkitData(_associatedBaseDevkit, _loadedBaseDevkitPathName, category, id, mayVary); private T? TryGetShpkDevkitData(JObject? devkit, string devkitPathName, string category, uint? id, bool mayVary) where T : class { @@ -47,7 +48,7 @@ public partial class MtrlTab { var selector = BuildSelector(data!["Vary"]! .Select(key => (uint)key) - .Select(key => Mtrl.GetShaderKey(key)?.Value ?? AssociatedShpk!.GetMaterialKeyById(key)!.Value.DefaultValue)); + .Select(key => Mtrl.GetShaderKey(key)?.Value ?? _associatedShpk!.GetMaterialKeyById(key)!.Value.DefaultValue)); var index = (int)data["Selectors"]![selector.ToString()]!; data = data["Items"]![index]; } @@ -62,12 +63,14 @@ public partial class MtrlTab } } + [UsedImplicitly] private sealed class DevkitShaderKeyValue { public string Label = string.Empty; public string Description = string.Empty; } + [UsedImplicitly] private sealed class DevkitShaderKey { public string Label = string.Empty; @@ -75,6 +78,7 @@ public partial class MtrlTab public Dictionary Values = []; } + [UsedImplicitly] private sealed class DevkitSampler { public string Label = string.Empty; @@ -84,14 +88,16 @@ public partial class MtrlTab private enum DevkitConstantType { - Hidden = -1, - Float = 0, + Hidden = -1, + Float = 0, + /// Integer encoded as a float. - Integer = 1, - Color = 2, - Enum = 3, + Integer = 1, + Color = 2, + Enum = 3, + /// Native integer. - Int32 = 4, + Int32 = 4, Int32Enum = 5, Int8 = 6, Int8Enum = 7, @@ -105,6 +111,7 @@ public partial class MtrlTab SphereMapIndex = 15, } + [UsedImplicitly] private sealed class DevkitConstantValue { public string Label = string.Empty; @@ -112,6 +119,7 @@ public partial class MtrlTab public double Value = 0; } + [UsedImplicitly] private sealed class DevkitConstant { public uint Offset = 0; @@ -147,7 +155,7 @@ public partial class MtrlTab => ByteOffset ?? Offset * ValueSize; public uint? EffectiveByteSize - => ByteSize ?? (Length * ValueSize); + => ByteSize ?? Length * ValueSize; public unsafe uint ValueSize => Type switch @@ -198,19 +206,23 @@ public partial class MtrlTab private IEditor CreateIntegerEditor() where T : unmanaged, INumber => ((Drag || Slider) && !Hex - ? (Drag - ? (IEditor)DragEditor.CreateInteger(ToInteger(Minimum), ToInteger(Maximum), Speed ?? 0.25f, RelativeSpeed, Unit, 0) - : SliderEditor.CreateInteger(ToInteger(Minimum) ?? default, ToInteger(Maximum) ?? default, Unit, 0)) - : InputEditor.CreateInteger(ToInteger(Minimum), ToInteger(Maximum), ToInteger(Step), ToInteger(StepFast), Hex, Unit, 0)) + ? Drag + ? (IEditor)DragEditor.CreateInteger(ToInteger(Minimum), ToInteger(Maximum), Speed ?? 0.25f, RelativeSpeed, + Unit, 0) + : SliderEditor.CreateInteger(ToInteger(Minimum) ?? default, ToInteger(Maximum) ?? default, Unit, 0) + : InputEditor.CreateInteger(ToInteger(Minimum), ToInteger(Maximum), ToInteger(Step), ToInteger(StepFast), + Hex, Unit, 0)) .WithFactorAndBias(ToInteger(Factor), ToInteger(Bias)); private IEditor CreateFloatEditor() where T : unmanaged, INumber, IPowerFunctions - => ((Drag || Slider) - ? (Drag - ? (IEditor)DragEditor.CreateFloat(ToFloat(Minimum), ToFloat(Maximum), Speed ?? 0.1f, RelativeSpeed, Precision, Unit, 0) - : SliderEditor.CreateFloat(ToFloat(Minimum) ?? default, ToFloat(Maximum) ?? default, Precision, Unit, 0)) - : InputEditor.CreateFloat(ToFloat(Minimum), ToFloat(Maximum), T.CreateSaturating(Step), T.CreateSaturating(StepFast), Precision, Unit, 0)) + => (Drag || Slider + ? Drag + ? (IEditor)DragEditor.CreateFloat(ToFloat(Minimum), ToFloat(Maximum), Speed ?? 0.1f, RelativeSpeed, + Precision, Unit, 0) + : SliderEditor.CreateFloat(ToFloat(Minimum) ?? default, ToFloat(Maximum) ?? default, Precision, Unit, 0) + : InputEditor.CreateFloat(ToFloat(Minimum), ToFloat(Maximum), T.CreateSaturating(Step), + T.CreateSaturating(StepFast), Precision, Unit, 0)) .WithExponent(T.CreateSaturating(Exponent)) .WithFactorAndBias(T.CreateSaturating(Factor), T.CreateSaturating(Bias)); diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs index a2165760..0ff2b01f 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs @@ -110,9 +110,9 @@ public partial class MtrlTab } ImGui.TableNextColumn(); - using (ImRaii.PushFont(UiBuilder.MonoFont)) - { - ImUtf8.Text($"{(rowIdx >> 1) + 1,2:D}{"AB"[rowIdx & 1]}"); + using (ImRaii.PushFont(UiBuilder.MonoFont)) + { + ImUtf8.Text($"{(rowIdx >> 1) + 1,2:D}{"AB"[rowIdx & 1]}"); } ImGui.TableNextColumn(); diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs index 3482e581..6089f2d5 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs @@ -12,20 +12,20 @@ namespace Penumbra.UI.AdvancedWindow.Materials; public partial class MtrlTab { - public readonly List MaterialPreviewers = new(4); - public readonly List ColorTablePreviewers = new(4); - public int HighlightedColorTablePair = -1; - public readonly Stopwatch HighlightTime = new(); + private readonly List _materialPreviewers = new(4); + private readonly List _colorTablePreviewers = new(4); + private int _highlightedColorTablePair = -1; + private readonly Stopwatch _highlightTime = new(); private void DrawMaterialLivePreviewRebind(bool disabled) { if (disabled) return; - if (ImGui.Button("Reload live preview")) + if (ImUtf8.Button("Reload live preview"u8)) BindToMaterialInstances(); - if (MaterialPreviewers.Count != 0 || ColorTablePreviewers.Count != 0) + if (_materialPreviewers.Count != 0 || _colorTablePreviewers.Count != 0) return; ImGui.SameLine(); @@ -34,7 +34,7 @@ public partial class MtrlTab "The current material has not been found on your character. Please check the Import from Screen tab for more information."u8); } - public unsafe void BindToMaterialInstances() + private unsafe void BindToMaterialInstances() { UnbindFromMaterialInstances(); @@ -50,7 +50,7 @@ public partial class MtrlTab try { - MaterialPreviewers.Add(new LiveMaterialPreviewer(_objects, materialInfo)); + _materialPreviewers.Add(new LiveMaterialPreviewer(_objects, materialInfo)); foundMaterials.Add((nint)material); } catch (InvalidOperationException) @@ -68,7 +68,7 @@ public partial class MtrlTab { try { - ColorTablePreviewers.Add(new LiveColorTablePreviewer(_objects, _framework, materialInfo)); + _colorTablePreviewers.Add(new LiveColorTablePreviewer(_objects, _framework, materialInfo)); } catch (InvalidOperationException) { @@ -81,53 +81,53 @@ public partial class MtrlTab private void UnbindFromMaterialInstances() { - foreach (var previewer in MaterialPreviewers) + foreach (var previewer in _materialPreviewers) previewer.Dispose(); - MaterialPreviewers.Clear(); + _materialPreviewers.Clear(); - foreach (var previewer in ColorTablePreviewers) + foreach (var previewer in _colorTablePreviewers) previewer.Dispose(); - ColorTablePreviewers.Clear(); + _colorTablePreviewers.Clear(); } private unsafe void UnbindFromDrawObjectMaterialInstances(CharacterBase* characterBase) { - for (var i = MaterialPreviewers.Count; i-- > 0;) + for (var i = _materialPreviewers.Count; i-- > 0;) { - var previewer = MaterialPreviewers[i]; + var previewer = _materialPreviewers[i]; if (previewer.DrawObject != characterBase) continue; previewer.Dispose(); - MaterialPreviewers.RemoveAt(i); + _materialPreviewers.RemoveAt(i); } - for (var i = ColorTablePreviewers.Count; i-- > 0;) + for (var i = _colorTablePreviewers.Count; i-- > 0;) { - var previewer = ColorTablePreviewers[i]; + var previewer = _colorTablePreviewers[i]; if (previewer.DrawObject != characterBase) continue; previewer.Dispose(); - ColorTablePreviewers.RemoveAt(i); + _colorTablePreviewers.RemoveAt(i); } } - public void SetShaderPackageFlags(uint shPkFlags) + private void SetShaderPackageFlags(uint shPkFlags) { - foreach (var previewer in MaterialPreviewers) + foreach (var previewer in _materialPreviewers) previewer.SetShaderPackageFlags(shPkFlags); } - public void SetMaterialParameter(uint parameterCrc, Index offset, Span value) + private void SetMaterialParameter(uint parameterCrc, Index offset, Span value) { - foreach (var previewer in MaterialPreviewers) + foreach (var previewer in _materialPreviewers) previewer.SetMaterialParameter(parameterCrc, offset, value); } - public void SetSamplerFlags(uint samplerCrc, uint samplerFlags) + private void SetSamplerFlags(uint samplerCrc, uint samplerFlags) { - foreach (var previewer in MaterialPreviewers) + foreach (var previewer in _materialPreviewers) previewer.SetSamplerFlags(samplerCrc, samplerFlags); } @@ -145,14 +145,14 @@ public partial class MtrlTab SetSamplerFlags(sampler.SamplerId, sampler.Flags); } - public void HighlightColorTablePair(int pairIdx) + private void HighlightColorTablePair(int pairIdx) { - var oldPairIdx = HighlightedColorTablePair; + var oldPairIdx = _highlightedColorTablePair; - if (HighlightedColorTablePair != pairIdx) + if (_highlightedColorTablePair != pairIdx) { - HighlightedColorTablePair = pairIdx; - HighlightTime.Restart(); + _highlightedColorTablePair = pairIdx; + _highlightTime.Restart(); } if (oldPairIdx >= 0) @@ -160,19 +160,6 @@ public partial class MtrlTab UpdateColorTableRowPreview(oldPairIdx << 1); UpdateColorTableRowPreview((oldPairIdx << 1) | 1); } - if (pairIdx >= 0) - { - UpdateColorTableRowPreview(pairIdx << 1); - UpdateColorTableRowPreview((pairIdx << 1) | 1); - } - } - - public void CancelColorTableHighlight() - { - var pairIdx = HighlightedColorTablePair; - - HighlightedColorTablePair = -1; - HighlightTime.Reset(); if (pairIdx >= 0) { @@ -181,9 +168,23 @@ public partial class MtrlTab } } - public void UpdateColorTableRowPreview(int rowIdx) + private void CancelColorTableHighlight() { - if (ColorTablePreviewers.Count == 0) + var pairIdx = _highlightedColorTablePair; + + _highlightedColorTablePair = -1; + _highlightTime.Reset(); + + if (pairIdx >= 0) + { + UpdateColorTableRowPreview(pairIdx << 1); + UpdateColorTableRowPreview((pairIdx << 1) | 1); + } + } + + private void UpdateColorTableRowPreview(int rowIdx) + { + if (_colorTablePreviewers.Count == 0) return; if (Mtrl.Table == null) @@ -192,7 +193,7 @@ public partial class MtrlTab var row = Mtrl.Table switch { LegacyColorTable legacyTable => new ColorTableRow(legacyTable[rowIdx]), - ColorTable table => table[rowIdx], + ColorTable table => table[rowIdx], _ => throw new InvalidOperationException($"Unsupported color table type {Mtrl.Table.GetType()}"), }; if (Mtrl.DyeTable != null) @@ -200,8 +201,8 @@ public partial class MtrlTab var dyeRow = Mtrl.DyeTable switch { LegacyColorDyeTable legacyDyeTable => new ColorDyeTableRow(legacyDyeTable[rowIdx]), - ColorDyeTable dyeTable => dyeTable[rowIdx], - _ => throw new InvalidOperationException($"Unsupported color dye table type {Mtrl.DyeTable.GetType()}"), + ColorDyeTable dyeTable => dyeTable[rowIdx], + _ => throw new InvalidOperationException($"Unsupported color dye table type {Mtrl.DyeTable.GetType()}"), }; if (dyeRow.Channel < StainService.ChannelCount) { @@ -213,21 +214,21 @@ public partial class MtrlTab } } - if (HighlightedColorTablePair << 1 == rowIdx) - ApplyHighlight(ref row, ColorId.InGameHighlight, (float)HighlightTime.Elapsed.TotalSeconds); - else if (((HighlightedColorTablePair << 1) | 1) == rowIdx) - ApplyHighlight(ref row, ColorId.InGameHighlight2, (float)HighlightTime.Elapsed.TotalSeconds); + if (_highlightedColorTablePair << 1 == rowIdx) + ApplyHighlight(ref row, ColorId.InGameHighlight, (float)_highlightTime.Elapsed.TotalSeconds); + else if (((_highlightedColorTablePair << 1) | 1) == rowIdx) + ApplyHighlight(ref row, ColorId.InGameHighlight2, (float)_highlightTime.Elapsed.TotalSeconds); - foreach (var previewer in ColorTablePreviewers) + foreach (var previewer in _colorTablePreviewers) { row[..].CopyTo(previewer.GetColorRow(rowIdx)); previewer.ScheduleUpdate(); } } - public void UpdateColorTablePreview() + private void UpdateColorTablePreview() { - if (ColorTablePreviewers.Count == 0) + if (_colorTablePreviewers.Count == 0) return; if (Mtrl.Table == null) @@ -237,7 +238,8 @@ public partial class MtrlTab var dyeRows = Mtrl.DyeTable != null ? ColorDyeTable.CastOrConvert(Mtrl.DyeTable) : null; if (dyeRows != null) { - ReadOnlySpan stainIds = [ + ReadOnlySpan stainIds = + [ _stainService.StainCombo1.CurrentSelection.Key, _stainService.StainCombo2.CurrentSelection.Key, ]; @@ -245,13 +247,14 @@ public partial class MtrlTab rows.ApplyDye(_stainService.GudStmFile, stainIds, dyeRows); } - if (HighlightedColorTablePair >= 0) + if (_highlightedColorTablePair >= 0) { - ApplyHighlight(ref rows[HighlightedColorTablePair << 1], ColorId.InGameHighlight, (float)HighlightTime.Elapsed.TotalSeconds); - ApplyHighlight(ref rows[(HighlightedColorTablePair << 1) | 1], ColorId.InGameHighlight2, (float)HighlightTime.Elapsed.TotalSeconds); + ApplyHighlight(ref rows[_highlightedColorTablePair << 1], ColorId.InGameHighlight, (float)_highlightTime.Elapsed.TotalSeconds); + ApplyHighlight(ref rows[(_highlightedColorTablePair << 1) | 1], ColorId.InGameHighlight2, + (float)_highlightTime.Elapsed.TotalSeconds); } - foreach (var previewer in ColorTablePreviewers) + foreach (var previewer in _colorTablePreviewers) { rows.AsHalves().CopyTo(previewer.ColorTable); previewer.ScheduleUpdate(); @@ -260,11 +263,11 @@ public partial class MtrlTab private static void ApplyHighlight(ref ColorTableRow row, ColorId colorId, float time) { - var level = (MathF.Sin(time * 2.0f * MathF.PI) + 2.0f) / 3.0f / 255.0f; - var baseColor = colorId.Value(); + var level = (MathF.Sin(time * 2.0f * MathF.PI) + 2.0f) / 3.0f / 255.0f; + var baseColor = colorId.Value(); var color = level * new Vector3(baseColor & 0xFF, (baseColor >> 8) & 0xFF, (baseColor >> 16) & 0xFF); var halfColor = (HalfColor)(color * color); - + row.DiffuseColor = halfColor; row.SpecularColor = halfColor; row.EmissiveColor = halfColor; diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ShaderPackage.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ShaderPackage.cs index 21557939..ae57a122 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ShaderPackage.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ShaderPackage.cs @@ -21,8 +21,8 @@ public partial class MtrlTab // Apricot shader packages are unlisted because // 1. they cause severe performance/memory issues when calculating the effective shader set // 2. they probably aren't intended for use with materials anyway - internal static readonly IReadOnlyList StandardShaderPackages = new[] - { + private static readonly IReadOnlyList StandardShaderPackages = + [ "3dui.shpk", // "apricot_decal_dummy.shpk", // "apricot_decal_ring.shpk", @@ -80,35 +80,35 @@ public partial class MtrlTab "verticalfog.shpk", "water.shpk", "weather.shpk", - }; + ]; - private static readonly byte[] UnknownShadersString = Encoding.UTF8.GetBytes("Vertex Shaders: ???\nPixel Shaders: ???"); + private static readonly byte[] UnknownShadersString = "Vertex Shaders: ???\nPixel Shaders: ???"u8.ToArray(); private string[]? _shpkNames; - public string ShaderHeader = "Shader###Shader"; - public FullPath LoadedShpkPath = FullPath.Empty; - public string LoadedShpkPathName = string.Empty; - public string LoadedShpkDevkitPathName = string.Empty; - public string ShaderComment = string.Empty; - public ShpkFile? AssociatedShpk; - public bool ShpkLoading; - public JObject? AssociatedShpkDevkit; + private string _shaderHeader = "Shader###Shader"; + private FullPath _loadedShpkPath = FullPath.Empty; + private string _loadedShpkPathName = string.Empty; + private string _loadedShpkDevkitPathName = string.Empty; + private string _shaderComment = string.Empty; + private ShpkFile? _associatedShpk; + private bool _shpkLoading; + private JObject? _associatedShpkDevkit; - public readonly string LoadedBaseDevkitPathName; - public readonly JObject? AssociatedBaseDevkit; + private readonly string _loadedBaseDevkitPathName; + private readonly JObject? _associatedBaseDevkit; // Shader Key State - public readonly + private readonly List<(string Label, int Index, string Description, bool MonoFont, IReadOnlyList<(string Label, uint Value, string Description)> - Values)> ShaderKeys = new(16); + Values)> _shaderKeys = new(16); - public readonly HashSet VertexShaders = new(16); - public readonly HashSet PixelShaders = new(16); - public bool ShadersKnown; - public ReadOnlyMemory ShadersString = UnknownShadersString; + private readonly HashSet _vertexShaders = new(16); + private readonly HashSet _pixelShaders = new(16); + private bool _shadersKnown; + private ReadOnlyMemory _shadersString = UnknownShadersString; - public string[] GetShpkNames() + private string[] GetShpkNames() { if (null != _shpkNames) return _shpkNames; @@ -122,7 +122,7 @@ public partial class MtrlTab return _shpkNames; } - public FullPath FindAssociatedShpk(out string defaultPath, out Utf8GamePath defaultGamePath) + private FullPath FindAssociatedShpk(out string defaultPath, out Utf8GamePath defaultGamePath) { defaultPath = GamePaths.Shader.ShpkPath(Mtrl.ShaderPackage.Name); if (!Utf8GamePath.FromString(defaultPath, out defaultGamePath)) @@ -131,45 +131,45 @@ public partial class MtrlTab return _edit.FindBestMatch(defaultGamePath); } - public void LoadShpk(FullPath path) + private void LoadShpk(FullPath path) => Task.Run(() => DoLoadShpk(path)); private async Task DoLoadShpk(FullPath path) { - ShadersKnown = false; - ShaderHeader = $"Shader ({Mtrl.ShaderPackage.Name})###Shader"; - ShpkLoading = true; + _shadersKnown = false; + _shaderHeader = $"Shader ({Mtrl.ShaderPackage.Name})###Shader"; + _shpkLoading = true; try { var data = path.IsRooted ? await File.ReadAllBytesAsync(path.FullName) : _gameData.GetFile(path.InternalName.ToString())?.Data; - LoadedShpkPath = path; - AssociatedShpk = data?.Length > 0 ? new ShpkFile(data) : throw new Exception("Failure to load file data."); - LoadedShpkPathName = path.ToPath(); + _loadedShpkPath = path; + _associatedShpk = data?.Length > 0 ? new ShpkFile(data) : throw new Exception("Failure to load file data."); + _loadedShpkPathName = path.ToPath(); } catch (Exception e) { - LoadedShpkPath = FullPath.Empty; - LoadedShpkPathName = string.Empty; - AssociatedShpk = null; - Penumbra.Messager.NotificationMessage(e, $"Could not load {LoadedShpkPath.ToPath()}.", NotificationType.Error, false); + _loadedShpkPath = FullPath.Empty; + _loadedShpkPathName = string.Empty; + _associatedShpk = null; + Penumbra.Messager.NotificationMessage(e, $"Could not load {_loadedShpkPath.ToPath()}.", NotificationType.Error, false); } finally { - ShpkLoading = false; + _shpkLoading = false; } - if (LoadedShpkPath.InternalName.IsEmpty) + if (_loadedShpkPath.InternalName.IsEmpty) { - AssociatedShpkDevkit = null; - LoadedShpkDevkitPathName = string.Empty; + _associatedShpkDevkit = null; + _loadedShpkDevkitPathName = string.Empty; } else { - AssociatedShpkDevkit = - TryLoadShpkDevkit(Path.GetFileNameWithoutExtension(Mtrl.ShaderPackage.Name), out LoadedShpkDevkitPathName); + _associatedShpkDevkit = + TryLoadShpkDevkit(Path.GetFileNameWithoutExtension(Mtrl.ShaderPackage.Name), out _loadedShpkDevkitPathName); } UpdateShaderKeys(); @@ -178,9 +178,9 @@ public partial class MtrlTab private void UpdateShaderKeys() { - ShaderKeys.Clear(); - if (AssociatedShpk != null) - foreach (var key in AssociatedShpk.MaterialKeys) + _shaderKeys.Clear(); + if (_associatedShpk != null) + foreach (var key in _associatedShpk.MaterialKeys) { var keyName = Names.KnownNames.TryResolve(key.Id); var dkData = TryGetShpkDevkitData("ShaderKeys", key.Id, false); @@ -210,7 +210,7 @@ public partial class MtrlTab return string.Compare(x.Label, y.Label, StringComparison.Ordinal); }); - ShaderKeys.Add((hasDkLabel ? dkData!.Label : keyName.ToString(), mtrlKeyIndex, dkData?.Description ?? string.Empty, + _shaderKeys.Add((hasDkLabel ? dkData!.Label : keyName.ToString(), mtrlKeyIndex, dkData?.Description ?? string.Empty, !hasDkLabel, values)); } else @@ -218,7 +218,7 @@ public partial class MtrlTab { var keyName = Names.KnownNames.TryResolve(key.Category); var valueName = keyName.WithKnownSuffixes().TryResolve(Names.KnownNames, key.Value); - ShaderKeys.Add((keyName.ToString(), index, string.Empty, true, [(valueName.ToString(), key.Value, string.Empty)])); + _shaderKeys.Add((keyName.ToString(), index, string.Empty, true, [(valueName.ToString(), key.Value, string.Empty)])); } } @@ -232,27 +232,28 @@ public partial class MtrlTab passSet = []; byPassSets.Add(passId, passSet); } + passSet.Add(shaderIndex); } - VertexShaders.Clear(); - PixelShaders.Clear(); + _vertexShaders.Clear(); + _pixelShaders.Clear(); var vertexShadersByPass = new Dictionary>(); var pixelShadersByPass = new Dictionary>(); - if (AssociatedShpk == null || !AssociatedShpk.IsExhaustiveNodeAnalysisFeasible()) + if (_associatedShpk == null || !_associatedShpk.IsExhaustiveNodeAnalysisFeasible()) { - ShadersKnown = false; + _shadersKnown = false; } else { - ShadersKnown = true; - var systemKeySelectors = AllSelectors(AssociatedShpk.SystemKeys).ToArray(); - var sceneKeySelectors = AllSelectors(AssociatedShpk.SceneKeys).ToArray(); - var subViewKeySelectors = AllSelectors(AssociatedShpk.SubViewKeys).ToArray(); + _shadersKnown = true; + var systemKeySelectors = AllSelectors(_associatedShpk.SystemKeys).ToArray(); + var sceneKeySelectors = AllSelectors(_associatedShpk.SceneKeys).ToArray(); + var subViewKeySelectors = AllSelectors(_associatedShpk.SubViewKeys).ToArray(); var materialKeySelector = - BuildSelector(AssociatedShpk.MaterialKeys.Select(key => Mtrl.GetOrAddShaderKey(key.Id, key.DefaultValue).Value)); + BuildSelector(_associatedShpk.MaterialKeys.Select(key => Mtrl.GetOrAddShaderKey(key.Id, key.DefaultValue).Value)); foreach (var systemKeySelector in systemKeySelectors) { @@ -261,38 +262,39 @@ public partial class MtrlTab foreach (var subViewKeySelector in subViewKeySelectors) { var selector = BuildSelector(systemKeySelector, sceneKeySelector, materialKeySelector, subViewKeySelector); - var node = AssociatedShpk.GetNodeBySelector(selector); + var node = _associatedShpk.GetNodeBySelector(selector); if (node.HasValue) foreach (var pass in node.Value.Passes) { - AddShader(VertexShaders, vertexShadersByPass, pass.Id, (int)pass.VertexShader); - AddShader(PixelShaders, pixelShadersByPass, pass.Id, (int)pass.PixelShader); + AddShader(_vertexShaders, vertexShadersByPass, pass.Id, (int)pass.VertexShader); + AddShader(_pixelShaders, pixelShadersByPass, pass.Id, (int)pass.PixelShader); } else - ShadersKnown = false; + _shadersKnown = false; } } } } - if (ShadersKnown) + if (_shadersKnown) { var builder = new StringBuilder(); - foreach (var (passId, passVS) in vertexShadersByPass) + foreach (var (passId, passVertexShader) in vertexShadersByPass) { if (builder.Length > 0) builder.Append("\n\n"); var passName = Names.KnownNames.TryResolve(passId); - var shaders = passVS.OrderBy(i => i).Select(i => $"#{i}"); + var shaders = passVertexShader.OrderBy(i => i).Select(i => $"#{i}"); builder.Append($"Vertex Shaders ({passName}): {string.Join(", ", shaders)}"); - if (pixelShadersByPass.TryGetValue(passId, out var passPS)) + if (pixelShadersByPass.TryGetValue(passId, out var passPixelShader)) { - shaders = passPS.OrderBy(i => i).Select(i => $"#{i}"); + shaders = passPixelShader.OrderBy(i => i).Select(i => $"#{i}"); builder.Append($"\nPixel Shaders ({passName}): {string.Join(", ", shaders)}"); } } - foreach (var (passId, passPS) in pixelShadersByPass) + + foreach (var (passId, passPixelShader) in pixelShadersByPass) { if (vertexShadersByPass.ContainsKey(passId)) continue; @@ -301,22 +303,24 @@ public partial class MtrlTab builder.Append("\n\n"); var passName = Names.KnownNames.TryResolve(passId); - var shaders = passPS.OrderBy(i => i).Select(i => $"#{i}"); + var shaders = passPixelShader.OrderBy(i => i).Select(i => $"#{i}"); builder.Append($"Pixel Shaders ({passName}): {string.Join(", ", shaders)}"); } - ShadersString = Encoding.UTF8.GetBytes(builder.ToString()); + _shadersString = Encoding.UTF8.GetBytes(builder.ToString()); } else - ShadersString = UnknownShadersString; + { + _shadersString = UnknownShadersString; + } - ShaderComment = TryGetShpkDevkitData("Comment", null, true) ?? string.Empty; + _shaderComment = TryGetShpkDevkitData("Comment", null, true) ?? string.Empty; } private bool DrawShaderSection(bool disabled) { var ret = false; - if (ImGui.CollapsingHeader(ShaderHeader)) + if (ImGui.CollapsingHeader(_shaderHeader)) { ret |= DrawPackageNameInput(disabled); ret |= DrawShaderFlagsInput(disabled); @@ -325,20 +329,17 @@ public partial class MtrlTab DrawMaterialShaders(); } - if (!ShpkLoading && (AssociatedShpk == null || AssociatedShpkDevkit == null)) + if (!_shpkLoading && (_associatedShpk == null || _associatedShpkDevkit == null)) { ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - if (AssociatedShpk == null) - { + if (_associatedShpk == null) ImUtf8.Text("Unable to find a suitable shader (.shpk) file for cross-references. Some functionality will be missing."u8, ImGuiUtil.HalfBlendText(0x80u)); // Half red - } else - { - ImUtf8.Text("No dev-kit file found for this material's shaders. Please install one for optimal editing experience, such as actual constant names instead of hexadecimal identifiers."u8, + ImUtf8.Text( + "No dev-kit file found for this material's shaders. Please install one for optimal editing experience, such as actual constant names instead of hexadecimal identifiers."u8, ImGuiUtil.HalfBlendText(0x8080u)); // Half yellow - } } return ret; @@ -358,14 +359,14 @@ public partial class MtrlTab if (c) foreach (var value in GetShpkNames()) { - if (ImGui.Selectable(value, value == Mtrl.ShaderPackage.Name)) - { - Mtrl.ShaderPackage.Name = value; - ret = true; - AssociatedShpk = null; - LoadedShpkPath = FullPath.Empty; - LoadShpk(FindAssociatedShpk(out _, out _)); - } + if (!ImGui.Selectable(value, value == Mtrl.ShaderPackage.Name)) + continue; + + Mtrl.ShaderPackage.Name = value; + ret = true; + _associatedShpk = null; + _loadedShpkPath = FullPath.Empty; + LoadShpk(FindAssociatedShpk(out _, out _)); } return ret; @@ -391,23 +392,23 @@ public partial class MtrlTab private void DrawCustomAssociations() { const string tooltip = "Click to copy file path to clipboard."; - var text = AssociatedShpk == null + var text = _associatedShpk == null ? "Associated .shpk file: None" - : $"Associated .shpk file: {LoadedShpkPathName}"; - var devkitText = AssociatedShpkDevkit == null + : $"Associated .shpk file: {_loadedShpkPathName}"; + var devkitText = _associatedShpkDevkit == null ? "Associated dev-kit file: None" - : $"Associated dev-kit file: {LoadedShpkDevkitPathName}"; - var baseDevkitText = AssociatedBaseDevkit == null + : $"Associated dev-kit file: {_loadedShpkDevkitPathName}"; + var baseDevkitText = _associatedBaseDevkit == null ? "Base dev-kit file: None" - : $"Base dev-kit file: {LoadedBaseDevkitPathName}"; + : $"Base dev-kit file: {_loadedBaseDevkitPathName}"; ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - ImGuiUtil.CopyOnClickSelectable(text, LoadedShpkPathName, tooltip); - ImGuiUtil.CopyOnClickSelectable(devkitText, LoadedShpkDevkitPathName, tooltip); - ImGuiUtil.CopyOnClickSelectable(baseDevkitText, LoadedBaseDevkitPathName, tooltip); + ImUtf8.CopyOnClickSelectable(text, _loadedShpkPathName, tooltip); + ImUtf8.CopyOnClickSelectable(devkitText, _loadedShpkDevkitPathName, tooltip); + ImUtf8.CopyOnClickSelectable(baseDevkitText, _loadedBaseDevkitPathName, tooltip); - if (ImGui.Button("Associate Custom .shpk File")) + if (ImUtf8.Button("Associate Custom .shpk File"u8)) _fileDialog.OpenFilePicker("Associate Custom .shpk File...", ".shpk", (success, name) => { if (success) @@ -416,15 +417,15 @@ public partial class MtrlTab var moddedPath = FindAssociatedShpk(out var defaultPath, out var gamePath); ImGui.SameLine(); - if (ImGuiUtil.DrawDisabledButton("Associate Default .shpk File", Vector2.Zero, moddedPath.ToPath(), - moddedPath.Equals(LoadedShpkPath))) + if (ImUtf8.ButtonEx("Associate Default .shpk File"u8, moddedPath.ToPath(), Vector2.Zero, + moddedPath.Equals(_loadedShpkPath))) LoadShpk(moddedPath); if (!gamePath.Path.Equals(moddedPath.InternalName)) { ImGui.SameLine(); - if (ImGuiUtil.DrawDisabledButton("Associate Unmodded .shpk File", Vector2.Zero, defaultPath, - gamePath.Path.Equals(LoadedShpkPath.InternalName))) + if (ImUtf8.ButtonEx("Associate Unmodded .shpk File", defaultPath, Vector2.Zero, + gamePath.Path.Equals(_loadedShpkPath.InternalName))) LoadShpk(new FullPath(gamePath)); } @@ -433,22 +434,23 @@ public partial class MtrlTab private bool DrawMaterialShaderKeys(bool disabled) { - if (ShaderKeys.Count == 0) + if (_shaderKeys.Count == 0) return false; var ret = false; - foreach (var (label, index, description, monoFont, values) in ShaderKeys) + foreach (var (label, index, description, monoFont, values) in _shaderKeys) { using var font = ImRaii.PushFont(UiBuilder.MonoFont, monoFont); ref var key = ref Mtrl.ShaderPackage.ShaderKeys[index]; - var shpkKey = AssociatedShpk?.GetMaterialKeyById(key.Category); + using var id = ImUtf8.PushId((int)key.Category); + var shpkKey = _associatedShpk?.GetMaterialKeyById(key.Category); var currentValue = key.Value; var (currentLabel, _, currentDescription) = values.FirstOrNull(v => v.Value == currentValue) ?? ($"0x{currentValue:X8}", currentValue, string.Empty); if (!disabled && shpkKey.HasValue) { ImGui.SetNextItemWidth(UiHelpers.Scale * 250.0f); - using (var c = ImRaii.Combo($"##{key.Category:X8}", currentLabel)) + using (var c = ImUtf8.Combo(""u8, currentLabel)) { if (c) foreach (var (valueLabel, value, valueDescription) in values) @@ -469,16 +471,16 @@ public partial class MtrlTab if (description.Length > 0) ImGuiUtil.LabeledHelpMarker(label, description); else - ImGui.TextUnformatted(label); + ImUtf8.Text(label); } else if (description.Length > 0 || currentDescription.Length > 0) { - ImGuiUtil.LabeledHelpMarker($"{label}: {currentLabel}", + ImUtf8.LabeledHelpMarker($"{label}: {currentLabel}", description + (description.Length > 0 && currentDescription.Length > 0 ? "\n\n" : string.Empty) + currentDescription); } else { - ImGui.TextUnformatted($"{label}: {currentLabel}"); + ImUtf8.Text($"{label}: {currentLabel}"); } } @@ -487,19 +489,19 @@ public partial class MtrlTab private void DrawMaterialShaders() { - if (AssociatedShpk == null) + if (_associatedShpk == null) return; using (var node = ImUtf8.TreeNode("Candidate Shaders"u8)) { if (node) - ImUtf8.Text(ShadersString.Span); + ImUtf8.Text(_shadersString.Span); } - if (ShaderComment.Length > 0) + if (_shaderComment.Length > 0) { ImGui.Dummy(new Vector2(ImGui.GetTextLineHeight() / 2)); - ImGui.TextUnformatted(ShaderComment); + ImUtf8.Text(_shaderComment); } } } diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Textures.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Textures.cs index 3181dafe..7ab2900d 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Textures.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.Textures.cs @@ -23,7 +23,7 @@ public partial class MtrlTab { Textures.Clear(); SamplerIds.Clear(); - if (AssociatedShpk == null) + if (_associatedShpk == null) { SamplerIds.UnionWith(Mtrl.ShaderPackage.Samplers.Select(sampler => sampler.SamplerId)); if (Mtrl.Table != null) @@ -34,11 +34,11 @@ public partial class MtrlTab } else { - foreach (var index in VertexShaders) - SamplerIds.UnionWith(AssociatedShpk.VertexShaders[index].Samplers.Select(sampler => sampler.Id)); - foreach (var index in PixelShaders) - SamplerIds.UnionWith(AssociatedShpk.PixelShaders[index].Samplers.Select(sampler => sampler.Id)); - if (!ShadersKnown) + foreach (var index in _vertexShaders) + SamplerIds.UnionWith(_associatedShpk.VertexShaders[index].Samplers.Select(sampler => sampler.Id)); + foreach (var index in _pixelShaders) + SamplerIds.UnionWith(_associatedShpk.PixelShaders[index].Samplers.Select(sampler => sampler.Id)); + if (!_shadersKnown) { SamplerIds.UnionWith(Mtrl.ShaderPackage.Samplers.Select(sampler => sampler.SamplerId)); if (Mtrl.Table != null) @@ -47,11 +47,11 @@ public partial class MtrlTab foreach (var samplerId in SamplerIds) { - var shpkSampler = AssociatedShpk.GetSamplerById(samplerId); + var shpkSampler = _associatedShpk.GetSamplerById(samplerId); if (shpkSampler is not { Slot: 2 }) continue; - var dkData = TryGetShpkDevkitData("Samplers", samplerId, true); + var dkData = TryGetShpkDevkitData("Samplers", samplerId, true); var hasDkLabel = !string.IsNullOrEmpty(dkData?.Label); var sampler = Mtrl.GetOrAddSampler(samplerId, dkData?.DefaultTexture ?? string.Empty, out var samplerIndex); @@ -95,9 +95,12 @@ public partial class MtrlTab private static ReadOnlySpan TextureAddressModeTooltip(TextureAddressMode addressMode) => addressMode switch { - TextureAddressMode.Wrap => "Tile the texture at every UV integer junction.\n\nFor example, for U values between 0 and 3, the texture is repeated three times."u8, - TextureAddressMode.Mirror => "Flip the texture at every UV integer junction.\n\nFor U values between 0 and 1, for example, the texture is addressed normally; between 1 and 2, the texture is mirrored; between 2 and 3, the texture is normal again; and so on."u8, - TextureAddressMode.Clamp => "Texture coordinates outside the range [0.0, 1.0] are set to the texture color at 0.0 or 1.0, respectively."u8, + TextureAddressMode.Wrap => + "Tile the texture at every UV integer junction.\n\nFor example, for U values between 0 and 3, the texture is repeated three times."u8, + TextureAddressMode.Mirror => + "Flip the texture at every UV integer junction.\n\nFor U values between 0 and 1, for example, the texture is addressed normally; between 1 and 2, the texture is mirrored; between 2 and 3, the texture is normal again; and so on."u8, + TextureAddressMode.Clamp => + "Texture coordinates outside the range [0.0, 1.0] are set to the texture color at 0.0 or 1.0, respectively."u8, TextureAddressMode.Border => "Texture coordinates outside the range [0.0, 1.0] are set to the border color (generally black)."u8, _ => ""u8, }; @@ -167,7 +170,7 @@ public partial class MtrlTab return ret; } - + private static bool ComboTextureAddressMode(ReadOnlySpan label, ref TextureAddressMode value) { using var c = ImUtf8.Combo(label, value.ToString()); @@ -202,7 +205,7 @@ public partial class MtrlTab ret = true; } - ref var samplerFlags = ref SamplerFlags.Wrap(ref sampler.Flags); + ref var samplerFlags = ref Wrap(ref sampler.Flags); ImGui.SetNextItemWidth(UiHelpers.Scale * 100.0f); var addressMode = samplerFlags.UAddressMode; diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.cs index 2d4e93f1..6e16de99 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.cs @@ -4,7 +4,6 @@ using OtterGui; using OtterGui.Raii; using OtterGui.Text; using OtterGui.Widgets; -using Penumbra.GameData; using Penumbra.GameData.Files; using Penumbra.GameData.Files.MaterialStructs; using Penumbra.GameData.Interop; @@ -21,7 +20,7 @@ public sealed partial class MtrlTab : IWritable, IDisposable private const int ShpkPrefixLength = 16; private static readonly CiByteString ShpkPrefix = CiByteString.FromSpanUnsafe("shader/sm5/shpk/"u8, true, true, true); - + private readonly IDataManager _gameData; private readonly IFramework _framework; private readonly ObjectManager _objects; @@ -40,7 +39,8 @@ public sealed partial class MtrlTab : IWritable, IDisposable private bool _updateOnNextFrame; public unsafe MtrlTab(IDataManager gameData, IFramework framework, ObjectManager objects, CharacterBaseDestructor characterBaseDestructor, - StainService stainService, ResourceTreeFactory resourceTreeFactory, FileDialogService fileDialog, MaterialTemplatePickers materialTemplatePickers, + StainService stainService, ResourceTreeFactory resourceTreeFactory, FileDialogService fileDialog, + MaterialTemplatePickers materialTemplatePickers, Configuration config, ModEditWindow edit, MtrlFile file, string filePath, bool writable) { _gameData = gameData; @@ -53,11 +53,11 @@ public sealed partial class MtrlTab : IWritable, IDisposable _materialTemplatePickers = materialTemplatePickers; _config = config; - _edit = edit; - Mtrl = file; - FilePath = filePath; - Writable = writable; - AssociatedBaseDevkit = TryLoadShpkDevkit("_base", out LoadedBaseDevkitPathName); + _edit = edit; + Mtrl = file; + FilePath = filePath; + Writable = writable; + _associatedBaseDevkit = TryLoadShpkDevkit("_base", out _loadedBaseDevkitPathName); Update(); LoadShpk(FindAssociatedShpk(out _, out _)); if (writable) @@ -118,7 +118,7 @@ public sealed partial class MtrlTab : IWritable, IDisposable using var dis = ImRaii.Disabled(disabled); var tmp = shaderFlags.EnableTransparency; - if (ImGui.Checkbox("Enable Transparency", ref tmp)) + if (ImUtf8.Checkbox("Enable Transparency"u8, ref tmp)) { shaderFlags.EnableTransparency = tmp; ret = true; @@ -127,14 +127,14 @@ public sealed partial class MtrlTab : IWritable, IDisposable ImGui.SameLine(200 * UiHelpers.Scale + ImGui.GetStyle().ItemSpacing.X + ImGui.GetStyle().WindowPadding.X); tmp = shaderFlags.HideBackfaces; - if (ImGui.Checkbox("Hide Backfaces", ref tmp)) + if (ImUtf8.Checkbox("Hide Backfaces"u8, ref tmp)) { shaderFlags.HideBackfaces = tmp; ret = true; SetShaderPackageFlags(Mtrl.ShaderPackage.Flags); } - if (ShpkLoading) + if (_shpkLoading) { ImGui.SameLine(400 * UiHelpers.Scale + 2 * ImGui.GetStyle().ItemSpacing.X + ImGui.GetStyle().WindowPadding.X); @@ -147,32 +147,32 @@ public sealed partial class MtrlTab : IWritable, IDisposable private void DrawOtherMaterialDetails(bool _) { - if (!ImGui.CollapsingHeader("Further Content")) + if (!ImUtf8.CollapsingHeader("Further Content"u8)) return; - using (var sets = ImRaii.TreeNode("UV Sets", ImGuiTreeNodeFlags.DefaultOpen)) + using (var sets = ImUtf8.TreeNode("UV Sets"u8, ImGuiTreeNodeFlags.DefaultOpen)) { if (sets) foreach (var set in Mtrl.UvSets) - ImRaii.TreeNode($"#{set.Index:D2} - {set.Name}", ImGuiTreeNodeFlags.Leaf).Dispose(); + ImUtf8.TreeNode($"#{set.Index:D2} - {set.Name}", ImGuiTreeNodeFlags.Leaf).Dispose(); } - using (var sets = ImRaii.TreeNode("Color Sets", ImGuiTreeNodeFlags.DefaultOpen)) + using (var sets = ImUtf8.TreeNode("Color Sets"u8, ImGuiTreeNodeFlags.DefaultOpen)) { if (sets) foreach (var set in Mtrl.ColorSets) - ImRaii.TreeNode($"#{set.Index:D2} - {set.Name}", ImGuiTreeNodeFlags.Leaf).Dispose(); + ImUtf8.TreeNode($"#{set.Index:D2} - {set.Name}", ImGuiTreeNodeFlags.Leaf).Dispose(); } if (Mtrl.AdditionalData.Length <= 0) return; - using var t = ImRaii.TreeNode($"Additional Data (Size: {Mtrl.AdditionalData.Length})###AdditionalData"); + using var t = ImUtf8.TreeNode($"Additional Data (Size: {Mtrl.AdditionalData.Length})###AdditionalData"); if (t) Widget.DrawHexViewer(Mtrl.AdditionalData); } - public void Update() + private void Update() { UpdateShaders(); UpdateTextures(); @@ -187,12 +187,12 @@ public sealed partial class MtrlTab : IWritable, IDisposable } public bool Valid - => ShadersKnown && Mtrl.Valid; + => _shadersKnown && Mtrl.Valid; public byte[] Write() { var output = Mtrl.Clone(); - output.GarbageCollect(AssociatedShpk, SamplerIds); + output.GarbageCollect(_associatedShpk, SamplerIds); return output.Write(); } diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTabFactory.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTabFactory.cs index af8b7db2..09db4277 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTabFactory.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTabFactory.cs @@ -8,9 +8,16 @@ using Penumbra.Services; namespace Penumbra.UI.AdvancedWindow.Materials; -public sealed class MtrlTabFactory(IDataManager gameData, IFramework framework, ObjectManager objects, - CharacterBaseDestructor characterBaseDestructor, StainService stainService, ResourceTreeFactory resourceTreeFactory, - FileDialogService fileDialog, MaterialTemplatePickers materialTemplatePickers, Configuration config) : IUiService +public sealed class MtrlTabFactory( + IDataManager gameData, + IFramework framework, + ObjectManager objects, + CharacterBaseDestructor characterBaseDestructor, + StainService stainService, + ResourceTreeFactory resourceTreeFactory, + FileDialogService fileDialog, + MaterialTemplatePickers materialTemplatePickers, + Configuration config) : IUiService { public MtrlTab Create(ModEditWindow edit, MtrlFile file, string filePath, bool writable) => new(gameData, framework, objects, characterBaseDestructor, stainService, resourceTreeFactory, fileDialog, diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs index ee883daf..59b38465 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.cs @@ -3,6 +3,7 @@ using Dalamud.Interface.Utility; using ImGuiNET; using OtterGui; using OtterGui.Raii; +using OtterGui.Text; using Penumbra.UI.AdvancedWindow.Materials; namespace Penumbra.UI.AdvancedWindow; @@ -24,7 +25,7 @@ public partial class ModEditWindow if (_editor.Files.Mdl.Count == 0) return; - using var tab = ImRaii.TabItem("Material Reassignment"); + using var tab = ImUtf8.TabItem("Material Reassignment"u8); if (!tab) return; @@ -32,45 +33,43 @@ public partial class ModEditWindow MaterialSuffix.Draw(_editor, ImGuiHelpers.ScaledVector2(175, 0)); ImGui.NewLine(); - using var child = ImRaii.Child("##mdlFiles", -Vector2.One, true); + using var child = ImUtf8.Child("##mdlFiles"u8, -Vector2.One, true); if (!child) return; - using var table = ImRaii.Table("##files", 4, ImGuiTableFlags.RowBg | ImGuiTableFlags.SizingFixedFit, -Vector2.One); + using var table = ImUtf8.Table("##files"u8, 4, ImGuiTableFlags.RowBg | ImGuiTableFlags.SizingFixedFit, -Vector2.One); if (!table) return; - var iconSize = ImGui.GetFrameHeight() * Vector2.One; foreach (var (info, idx) in _editor.MdlMaterialEditor.ModelFiles.WithIndex()) { using var id = ImRaii.PushId(idx); ImGui.TableNextColumn(); - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Save.ToIconString(), iconSize, - "Save the changed mdl file.\nUse at own risk!", !info.Changed, true)) + if (ImUtf8.IconButton(FontAwesomeIcon.Save, "Save the changed mdl file.\nUse at own risk!"u8, disabled: !info.Changed)) info.Save(_editor.Compactor); ImGui.TableNextColumn(); - if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Recycle.ToIconString(), iconSize, - "Restore current changes to default.", !info.Changed, true)) + if (ImUtf8.IconButton(FontAwesomeIcon.Recycle, "Restore current changes to default."u8, disabled: !info.Changed)) info.Restore(); ImGui.TableNextColumn(); - ImGui.TextUnformatted(info.Path.FullName[(Mod!.ModPath.FullName.Length + 1)..]); + ImUtf8.Text(info.Path.InternalName.Span[(Mod!.ModPath.FullName.Length + 1)..]); ImGui.TableNextColumn(); ImGui.SetNextItemWidth(400 * UiHelpers.Scale); var tmp = info.CurrentMaterials[0]; - if (ImGui.InputText("##0", ref tmp, 64)) + if (ImUtf8.InputText("##0"u8, ref tmp)) info.SetMaterial(tmp, 0); for (var i = 1; i < info.Count; ++i) { + using var id2 = ImUtf8.PushId(i); ImGui.TableNextColumn(); ImGui.TableNextColumn(); ImGui.TableNextColumn(); ImGui.TableNextColumn(); ImGui.SetNextItemWidth(400 * UiHelpers.Scale); tmp = info.CurrentMaterials[i]; - if (ImGui.InputText($"##{i}", ref tmp, 64)) + if (ImUtf8.InputText(""u8, ref tmp)) info.SetMaterial(tmp, i); } } diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs index 8a1c729c..41f1da26 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.ShaderPackages.cs @@ -10,7 +10,6 @@ using Penumbra.GameData.Interop; using Penumbra.String; using static Penumbra.GameData.Files.ShpkFile; using OtterGui.Widgets; -using Penumbra.GameData.Files.ShaderStructs; using OtterGui.Text; using Penumbra.GameData.Structs; @@ -56,21 +55,17 @@ public partial class ModEditWindow private static void DrawShaderPackageSummary(ShpkTab tab) { if (tab.Shpk.IsLegacy) - { ImUtf8.Text("This legacy shader package will not work in the current version of the game. Do not attempt to load it.", ImGuiUtil.HalfBlendText(0x80u)); // Half red - } - ImGui.TextUnformatted(tab.Header); + ImUtf8.Text(tab.Header); if (!tab.Shpk.Disassembled) - { ImUtf8.Text("Your system doesn't support disassembling shaders. Some functionality will be missing.", ImGuiUtil.HalfBlendText(0x80u)); // Half red - } } private static void DrawShaderExportButton(ShpkTab tab, string objectName, Shader shader, int idx) { - if (!ImGui.Button($"Export Shader Program Blob ({shader.Blob.Length} bytes)")) + if (!ImUtf8.Button($"Export Shader Program Blob ({shader.Blob.Length} bytes)")) return; var defaultName = objectName[0] switch @@ -106,7 +101,7 @@ public partial class ModEditWindow private static void DrawShaderImportButton(ShpkTab tab, string objectName, Shader[] shaders, int idx) { - if (!ImGui.Button("Replace Shader Program Blob")) + if (!ImUtf8.Button("Replace Shader Program Blob"u8)) return; tab.FileDialog.OpenFilePicker($"Replace {objectName} #{idx} Program Blob...", "Shader Program Blobs{.o,.cso,.dxbc,.dxil}", @@ -145,8 +140,8 @@ public partial class ModEditWindow private static unsafe void DrawRawDisassembly(Shader shader) { - using var t2 = ImRaii.TreeNode("Raw Program Disassembly"); - if (!t2) + using var tree = ImUtf8.TreeNode("Raw Program Disassembly"u8); + if (!tree) return; using var font = ImRaii.PushFont(UiBuilder.MonoFont); @@ -164,31 +159,34 @@ public partial class ModEditWindow { foreach (var (key, keyIdx) in shader.SystemValues!.WithIndex()) { - ImRaii.TreeNode($"Used with System Key {tab.TryResolveName(tab.Shpk.SystemKeys[keyIdx].Id)} \u2208 {{ {tab.NameSetToString(key)} }}", + ImUtf8.TreeNode( + $"Used with System Key {tab.TryResolveName(tab.Shpk.SystemKeys[keyIdx].Id)} \u2208 {{ {tab.NameSetToString(key)} }}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); } foreach (var (key, keyIdx) in shader.SceneValues!.WithIndex()) { - ImRaii.TreeNode($"Used with Scene Key {tab.TryResolveName(tab.Shpk.SceneKeys[keyIdx].Id)} \u2208 {{ {tab.NameSetToString(key)} }}", + ImUtf8.TreeNode( + $"Used with Scene Key {tab.TryResolveName(tab.Shpk.SceneKeys[keyIdx].Id)} \u2208 {{ {tab.NameSetToString(key)} }}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); } foreach (var (key, keyIdx) in shader.MaterialValues!.WithIndex()) { - ImRaii.TreeNode($"Used with Material Key {tab.TryResolveName(tab.Shpk.MaterialKeys[keyIdx].Id)} \u2208 {{ {tab.NameSetToString(key)} }}", + ImUtf8.TreeNode( + $"Used with Material Key {tab.TryResolveName(tab.Shpk.MaterialKeys[keyIdx].Id)} \u2208 {{ {tab.NameSetToString(key)} }}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); } foreach (var (key, keyIdx) in shader.SubViewValues!.WithIndex()) { - ImRaii.TreeNode($"Used with Sub-View Key #{keyIdx} \u2208 {{ {tab.NameSetToString(key)} }}", + ImUtf8.TreeNode($"Used with Sub-View Key #{keyIdx} \u2208 {{ {tab.NameSetToString(key)} }}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); } } } - ImRaii.TreeNode($"Used in Passes: {tab.NameSetToString(shader.Passes)}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + ImUtf8.TreeNode($"Used in Passes: {tab.NameSetToString(shader.Passes)}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); } private static void DrawShaderPackageFilterSection(ShpkTab tab) @@ -215,19 +213,20 @@ public partial class ModEditWindow { if (values.PossibleValues == null) { - ImRaii.TreeNode(label, ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + ImUtf8.TreeNode(label, ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); return; } - using var node = ImRaii.TreeNode(label); + using var node = ImUtf8.TreeNode(label); if (!node) return; foreach (var value in values.PossibleValues) { var contains = values.Contains(value); - if (!ImGui.Checkbox($"{tab.TryResolveName(value)}", ref contains)) + if (!ImUtf8.Checkbox($"{tab.TryResolveName(value)}", ref contains)) continue; + if (contains) { if (values.AddExisting(value)) @@ -249,7 +248,7 @@ public partial class ModEditWindow private static bool DrawShaderPackageShaderArray(ShpkTab tab, string objectName, Shader[] shaders, bool disabled) { - if (shaders.Length == 0 || !ImGui.CollapsingHeader($"{objectName}s")) + if (shaders.Length == 0 || !ImUtf8.CollapsingHeader($"{objectName}s")) return false; var ret = false; @@ -259,7 +258,7 @@ public partial class ModEditWindow if (!tab.IsFilterMatch(shader)) continue; - using var t = ImRaii.TreeNode($"{objectName} #{idx}"); + using var t = ImUtf8.TreeNode($"{objectName} #{idx}"); if (!t) continue; @@ -270,20 +269,20 @@ public partial class ModEditWindow DrawShaderImportButton(tab, objectName, shaders, idx); } - ret |= DrawShaderPackageResourceArray("Constant Buffers", "slot", true, shader.Constants, false, true); - ret |= DrawShaderPackageResourceArray("Samplers", "slot", false, shader.Samplers, false, true); + ret |= DrawShaderPackageResourceArray("Constant Buffers", "slot", true, shader.Constants, false, true); + ret |= DrawShaderPackageResourceArray("Samplers", "slot", false, shader.Samplers, false, true); if (!tab.Shpk.IsLegacy) - ret |= DrawShaderPackageResourceArray("Textures", "slot", false, shader.Textures, false, true); - ret |= DrawShaderPackageResourceArray("Unordered Access Views", "slot", true, shader.Uavs, false, true); + ret |= DrawShaderPackageResourceArray("Textures", "slot", false, shader.Textures, false, true); + ret |= DrawShaderPackageResourceArray("Unordered Access Views", "slot", true, shader.Uavs, false, true); if (shader.DeclaredInputs != 0) - ImRaii.TreeNode($"Declared Inputs: {shader.DeclaredInputs}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + ImUtf8.TreeNode($"Declared Inputs: {shader.DeclaredInputs}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); if (shader.UsedInputs != 0) - ImRaii.TreeNode($"Used Inputs: {shader.UsedInputs}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + ImUtf8.TreeNode($"Used Inputs: {shader.UsedInputs}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); if (shader.AdditionalHeader.Length > 8) { - using var t2 = ImRaii.TreeNode($"Additional Header (Size: {shader.AdditionalHeader.Length})###AdditionalHeader"); + using var t2 = ImUtf8.TreeNode($"Additional Header (Size: {shader.AdditionalHeader.Length})###AdditionalHeader"); if (t2) Widget.DrawHexViewer(shader.AdditionalHeader); } @@ -313,23 +312,28 @@ public partial class ModEditWindow var usedString = UsedComponentString(withSize, false, resource); if (usedString.Length > 0) { - ImRaii.TreeNode(hasFilter ? $"Globally Used: {usedString}" : $"Used: {usedString}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + ImUtf8.TreeNode(hasFilter ? $"Globally Used: {usedString}" : $"Used: {usedString}", + ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); if (hasFilter) { var filteredUsedString = UsedComponentString(withSize, true, resource); if (filteredUsedString.Length > 0) - ImRaii.TreeNode($"Used within Filters: {filteredUsedString}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + ImUtf8.TreeNode($"Used within Filters: {filteredUsedString}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet) + .Dispose(); else - ImRaii.TreeNode("Unused within Filters", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + ImUtf8.TreeNode("Unused within Filters"u8, ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); } } else - ImRaii.TreeNode(hasFilter ? "Globally Unused" : "Unused", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + { + ImUtf8.TreeNode(hasFilter ? "Globally Unused"u8 : "Unused"u8, ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + } return ret; } - private static bool DrawShaderPackageResourceArray(string arrayName, string slotLabel, bool withSize, Resource[] resources, bool hasFilter, bool disabled) + private static bool DrawShaderPackageResourceArray(string arrayName, string slotLabel, bool withSize, Resource[] resources, bool hasFilter, + bool disabled) { if (resources.Length == 0) return false; @@ -345,8 +349,8 @@ public partial class ModEditWindow var name = $"#{idx}: {buf.Name} (ID: 0x{buf.Id:X8}), {slotLabel}: {buf.Slot}" + (withSize ? $", size: {buf.Size} registers###{idx}: {buf.Name} (ID: 0x{buf.Id:X8})" : string.Empty); using var font = ImRaii.PushFont(UiBuilder.MonoFont); - using var t2 = ImRaii.TreeNode(name, !disabled || buf.Used != null ? 0 : ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet); - font.Dispose(); + using var t2 = ImUtf8.TreeNode(name, !disabled || buf.Used != null ? 0 : ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet); + font.Pop(); if (t2) ret |= DrawShaderPackageResource(slotLabel, withSize, ref buf, hasFilter, disabled); } @@ -361,7 +365,7 @@ public partial class ModEditWindow + new Vector2(ImGui.CalcTextSize(label).X + 3 * ImGui.GetStyle().ItemInnerSpacing.X + ImGui.GetFrameHeight(), ImGui.GetStyle().FramePadding.Y); - var ret = ImGui.CollapsingHeader(label); + var ret = ImUtf8.CollapsingHeader(label); ImGui.GetWindowDrawList() .AddText(UiBuilder.DefaultFont, UiBuilder.DefaultFont.FontSize, pos, ImGui.GetColorU32(ImGuiCol.Text), "Layout"); return ret; @@ -374,7 +378,7 @@ public partial class ModEditWindow if (isSizeWellDefined) return true; - ImGui.TextUnformatted(materialParams.HasValue + ImUtf8.Text(materialParams.HasValue ? $"Buffer size mismatch: {file.MaterialParamsSize} bytes ≠ {materialParams.Value.Size} registers ({materialParams.Value.Size << 4} bytes)" : $"Buffer size mismatch: {file.MaterialParamsSize} bytes, not a multiple of 16"); return false; @@ -382,7 +386,7 @@ public partial class ModEditWindow private static bool DrawShaderPackageMaterialMatrix(ShpkTab tab, bool disabled) { - ImGui.TextUnformatted(tab.Shpk.Disassembled + ImUtf8.Text(tab.Shpk.Disassembled ? "Parameter positions (continuations are grayed out, globally unused values are red, unused values within filters are yellow):" : "Parameter positions (continuations are grayed out):"); @@ -398,10 +402,7 @@ public partial class ModEditWindow ImGui.TableSetupColumn("w", ImGuiTableColumnFlags.WidthFixed, 250 * UiHelpers.Scale); ImGui.TableHeadersRow(); - var textColorStart = ImGui.GetColorU32(ImGuiCol.Text); - var textColorCont = ImGuiUtil.HalfTransparent(textColorStart); // Half opacity - var textColorUnusedStart = ImGuiUtil.HalfBlend(textColorStart, 0x80u); // Half red - var textColorUnusedCont = ImGuiUtil.HalfTransparent(textColorUnusedStart); + var textColorStart = ImGui.GetColorU32(ImGuiCol.Text); var ret = false; for (var i = 0; i < tab.Matrix.GetLength(0); ++i) @@ -420,12 +421,12 @@ public partial class ModEditWindow color = ImGuiUtil.HalfTransparent(color); // Half opacity using var _ = ImRaii.PushId(i * 4 + j); var deletable = !disabled && idx >= 0; - using (var font = ImRaii.PushFont(UiBuilder.MonoFont, tooltip.Length > 0)) + using (ImRaii.PushFont(UiBuilder.MonoFont, tooltip.Length > 0)) { - using (var c = ImRaii.PushColor(ImGuiCol.Text, color)) + using (ImRaii.PushColor(ImGuiCol.Text, color)) { ImGui.TableNextColumn(); - ImGui.Selectable(name); + ImUtf8.Selectable(name); if (deletable && ImGui.IsItemClicked(ImGuiMouseButton.Right) && ImGui.GetIO().KeyCtrl) { tab.Shpk.MaterialParams = tab.Shpk.MaterialParams.RemoveItems(idx); @@ -434,11 +435,11 @@ public partial class ModEditWindow } } - ImGuiUtil.HoverTooltip(tooltip); + ImUtf8.HoverTooltip(tooltip); } if (deletable) - ImGuiUtil.HoverTooltip("\nControl + Right-Click to remove."); + ImUtf8.HoverTooltip("\nControl + Right-Click to remove."u8); } } @@ -450,7 +451,9 @@ public partial class ModEditWindow if (!ImUtf8.Button("Export globally unused parameters as material dev-kit file"u8)) return; - tab.FileDialog.OpenSavePicker("Export material dev-kit file", ".json", $"{Path.GetFileNameWithoutExtension(tab.FilePath)}.json", ".json", DoSave, null, false); + tab.FileDialog.OpenSavePicker("Export material dev-kit file", ".json", $"{Path.GetFileNameWithoutExtension(tab.FilePath)}.json", + ".json", DoSave, null, false); + return; void DoSave(bool success, string path) { @@ -476,22 +479,22 @@ public partial class ModEditWindow private static void DrawShaderPackageMisalignedParameters(ShpkTab tab) { - using var t = ImRaii.TreeNode("Misaligned / Overflowing Parameters"); + using var t = ImUtf8.TreeNode("Misaligned / Overflowing Parameters"u8); if (!t) return; using var _ = ImRaii.PushFont(UiBuilder.MonoFont); foreach (var name in tab.MalformedParameters) - ImRaii.TreeNode(name, ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + ImUtf8.TreeNode(name, ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); } private static void DrawShaderPackageStartCombo(ShpkTab tab) { using var s = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, ImGui.GetStyle().ItemInnerSpacing); - using (var _ = ImRaii.PushFont(UiBuilder.MonoFont)) + using (ImRaii.PushFont(UiBuilder.MonoFont)) { ImGui.SetNextItemWidth(UiHelpers.Scale * 400); - using var c = ImRaii.Combo("##Start", tab.Orphans[tab.NewMaterialParamStart].Name); + using var c = ImUtf8.Combo("##Start", tab.Orphans[tab.NewMaterialParamStart].Name); if (c) foreach (var (start, idx) in tab.Orphans.WithIndex()) { @@ -501,7 +504,7 @@ public partial class ModEditWindow } ImGui.SameLine(); - ImGui.TextUnformatted("Start"); + ImUtf8.Text("Start"u8); } private static void DrawShaderPackageEndCombo(ShpkTab tab) @@ -510,7 +513,7 @@ public partial class ModEditWindow using (var _ = ImRaii.PushFont(UiBuilder.MonoFont)) { ImGui.SetNextItemWidth(UiHelpers.Scale * 400); - using var c = ImRaii.Combo("##End", tab.Orphans[tab.NewMaterialParamEnd].Name); + using var c = ImUtf8.Combo("##End", tab.Orphans[tab.NewMaterialParamEnd].Name); if (c) { var current = tab.Orphans[tab.NewMaterialParamStart].Index; @@ -527,7 +530,7 @@ public partial class ModEditWindow } ImGui.SameLine(); - ImGui.TextUnformatted("End"); + ImUtf8.Text("End"u8); } private static bool DrawShaderPackageNewParameter(ShpkTab tab) @@ -540,15 +543,14 @@ public partial class ModEditWindow ImGui.SetNextItemWidth(UiHelpers.Scale * 400); var newName = tab.NewMaterialParamName.Value!; - if (ImGui.InputText("Name", ref newName, 63)) + if (ImUtf8.InputText("Name", ref newName)) tab.NewMaterialParamName = newName; var tooltip = tab.UsedIds.Contains(tab.NewMaterialParamName.Crc32) - ? "The ID is already in use. Please choose a different name." - : string.Empty; - if (!ImGuiUtil.DrawDisabledButton($"Add {tab.NewMaterialParamName} (0x{tab.NewMaterialParamName.Crc32:X8})", new Vector2(400 * UiHelpers.Scale, ImGui.GetFrameHeight()), - tooltip, - tooltip.Length > 0)) + ? "The ID is already in use. Please choose a different name."u8 + : ""u8; + if (!ImUtf8.ButtonEx($"Add {tab.NewMaterialParamName} (0x{tab.NewMaterialParamName.Crc32:X8})", tooltip, + new Vector2(400 * UiHelpers.Scale, ImGui.GetFrameHeight()), tooltip.Length > 0)) return false; tab.Shpk.MaterialParams = tab.Shpk.MaterialParams.AddItem(new MaterialParam @@ -589,15 +591,15 @@ public partial class ModEditWindow { var ret = false; - if (!ImGui.CollapsingHeader("Shader Resources")) + if (!ImUtf8.CollapsingHeader("Shader Resources"u8)) return false; var hasFilters = tab.FilterPopCount != tab.FilterMaximumPopCount; - ret |= DrawShaderPackageResourceArray("Constant Buffers", "type", true, tab.Shpk.Constants, hasFilters, disabled); - ret |= DrawShaderPackageResourceArray("Samplers", "type", false, tab.Shpk.Samplers, hasFilters, disabled); + ret |= DrawShaderPackageResourceArray("Constant Buffers", "type", true, tab.Shpk.Constants, hasFilters, disabled); + ret |= DrawShaderPackageResourceArray("Samplers", "type", false, tab.Shpk.Samplers, hasFilters, disabled); if (!tab.Shpk.IsLegacy) - ret |= DrawShaderPackageResourceArray("Textures", "type", false, tab.Shpk.Textures, hasFilters, disabled); - ret |= DrawShaderPackageResourceArray("Unordered Access Views", "type", false, tab.Shpk.Uavs, hasFilters, disabled); + ret |= DrawShaderPackageResourceArray("Textures", "type", false, tab.Shpk.Textures, hasFilters, disabled); + ret |= DrawShaderPackageResourceArray("Unordered Access Views", "type", false, tab.Shpk.Uavs, hasFilters, disabled); return ret; } @@ -607,18 +609,20 @@ public partial class ModEditWindow if (keys.Count == 0) return; - using var t = ImRaii.TreeNode(arrayName); + using var t = ImUtf8.TreeNode(arrayName); if (!t) return; using var font = ImRaii.PushFont(UiBuilder.MonoFont); foreach (var (key, idx) in keys.WithIndex()) { - using var t2 = ImRaii.TreeNode(withId ? $"#{idx}: {tab.TryResolveName(key.Id)} (0x{key.Id:X8})" : $"#{idx}"); + using var t2 = ImUtf8.TreeNode(withId ? $"#{idx}: {tab.TryResolveName(key.Id)} (0x{key.Id:X8})" : $"#{idx}"); if (t2) { - ImRaii.TreeNode($"Default Value: {tab.TryResolveName(key.DefaultValue)} (0x{key.DefaultValue:X8})", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); - ImRaii.TreeNode($"Known Values: {tab.NameSetToString(key.Values, true)}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + ImUtf8.TreeNode($"Default Value: {tab.TryResolveName(key.DefaultValue)} (0x{key.DefaultValue:X8})", + ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + ImUtf8.TreeNode($"Known Values: {tab.NameSetToString(key.Values, true)}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet) + .Dispose(); } } } @@ -628,7 +632,7 @@ public partial class ModEditWindow if (tab.Shpk.Nodes.Length <= 0) return; - using var t = ImRaii.TreeNode($"Nodes ({tab.Shpk.Nodes.Length})###Nodes"); + using var t = ImUtf8.TreeNode($"Nodes ({tab.Shpk.Nodes.Length})###Nodes"); if (!t) return; @@ -639,39 +643,44 @@ public partial class ModEditWindow if (!tab.IsFilterMatch(node)) continue; - using var t2 = ImRaii.TreeNode($"#{idx:D4}: Selector: 0x{node.Selector:X8}"); + using var t2 = ImUtf8.TreeNode($"#{idx:D4}: Selector: 0x{node.Selector:X8}"); if (!t2) continue; foreach (var (key, keyIdx) in node.SystemKeys.WithIndex()) { - ImRaii.TreeNode($"System Key {tab.TryResolveName(tab.Shpk.SystemKeys[keyIdx].Id)} = {tab.TryResolveName(key)} / \u2208 {{ {tab.NameSetToString(node.SystemValues![keyIdx])} }}", + ImUtf8.TreeNode( + $"System Key {tab.TryResolveName(tab.Shpk.SystemKeys[keyIdx].Id)} = {tab.TryResolveName(key)} / \u2208 {{ {tab.NameSetToString(node.SystemValues![keyIdx])} }}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); } foreach (var (key, keyIdx) in node.SceneKeys.WithIndex()) { - ImRaii.TreeNode($"Scene Key {tab.TryResolveName(tab.Shpk.SceneKeys[keyIdx].Id)} = {tab.TryResolveName(key)} / \u2208 {{ {tab.NameSetToString(node.SceneValues![keyIdx])} }}", + ImUtf8.TreeNode( + $"Scene Key {tab.TryResolveName(tab.Shpk.SceneKeys[keyIdx].Id)} = {tab.TryResolveName(key)} / \u2208 {{ {tab.NameSetToString(node.SceneValues![keyIdx])} }}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); } foreach (var (key, keyIdx) in node.MaterialKeys.WithIndex()) { - ImRaii.TreeNode($"Material Key {tab.TryResolveName(tab.Shpk.MaterialKeys[keyIdx].Id)} = {tab.TryResolveName(key)} / \u2208 {{ {tab.NameSetToString(node.MaterialValues![keyIdx])} }}", + ImUtf8.TreeNode( + $"Material Key {tab.TryResolveName(tab.Shpk.MaterialKeys[keyIdx].Id)} = {tab.TryResolveName(key)} / \u2208 {{ {tab.NameSetToString(node.MaterialValues![keyIdx])} }}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); } foreach (var (key, keyIdx) in node.SubViewKeys.WithIndex()) { - ImRaii.TreeNode($"Sub-View Key #{keyIdx} = {tab.TryResolveName(key)} / \u2208 {{ {tab.NameSetToString(node.SubViewValues![keyIdx])} }}", + ImUtf8.TreeNode( + $"Sub-View Key #{keyIdx} = {tab.TryResolveName(key)} / \u2208 {{ {tab.NameSetToString(node.SubViewValues![keyIdx])} }}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); } - ImRaii.TreeNode($"Pass Indices: {string.Join(' ', node.PassIndices.Select(c => $"{c:X2}"))}", + ImUtf8.TreeNode($"Pass Indices: {string.Join(' ', node.PassIndices.Select(c => $"{c:X2}"))}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); foreach (var (pass, passIdx) in node.Passes.WithIndex()) { - ImRaii.TreeNode($"Pass #{passIdx}: ID: {tab.TryResolveName(pass.Id)}, Vertex Shader #{pass.VertexShader}, Pixel Shader #{pass.PixelShader}", + ImUtf8.TreeNode( + $"Pass #{passIdx}: ID: {tab.TryResolveName(pass.Id)}, Vertex Shader #{pass.VertexShader}, Pixel Shader #{pass.PixelShader}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet) .Dispose(); } @@ -680,7 +689,7 @@ public partial class ModEditWindow private static void DrawShaderPackageSelection(ShpkTab tab) { - if (!ImGui.CollapsingHeader("Shader Selection")) + if (!ImUtf8.CollapsingHeader("Shader Selection"u8)) return; DrawKeyArray(tab, "System Keys", true, tab.Shpk.SystemKeys); @@ -689,13 +698,13 @@ public partial class ModEditWindow DrawKeyArray(tab, "Sub-View Keys", false, tab.Shpk.SubViewKeys); DrawShaderPackageNodes(tab); - using var t = ImRaii.TreeNode($"Node Selectors ({tab.Shpk.NodeSelectors.Count})###NodeSelectors"); + using var t = ImUtf8.TreeNode($"Node Selectors ({tab.Shpk.NodeSelectors.Count})###NodeSelectors"); if (t) { using var font = ImRaii.PushFont(UiBuilder.MonoFont); foreach (var selector in tab.Shpk.NodeSelectors) { - ImRaii.TreeNode($"#{selector.Value:D4}: Selector: 0x{selector.Key:X8}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet) + ImUtf8.TreeNode($"#{selector.Value:D4}: Selector: 0x{selector.Key:X8}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet) .Dispose(); } } @@ -703,14 +712,14 @@ public partial class ModEditWindow private static void DrawOtherShaderPackageDetails(ShpkTab tab) { - if (!ImGui.CollapsingHeader("Further Content")) + if (!ImUtf8.CollapsingHeader("Further Content"u8)) return; - ImRaii.TreeNode($"Version: 0x{tab.Shpk.Version:X8}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); + ImUtf8.TreeNode($"Version: 0x{tab.Shpk.Version:X8}", ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.Bullet).Dispose(); if (tab.Shpk.AdditionalData.Length > 0) { - using var t = ImRaii.TreeNode($"Additional Data (Size: {tab.Shpk.AdditionalData.Length})###AdditionalData"); + using var t = ImUtf8.TreeNode($"Additional Data (Size: {tab.Shpk.AdditionalData.Length})###AdditionalData"); if (t) Widget.DrawHexViewer(tab.Shpk.AdditionalData); } @@ -718,9 +727,9 @@ public partial class ModEditWindow private static string UsedComponentString(bool withSize, bool filtered, in Resource resource) { - var used = filtered ? resource.FilteredUsed : resource.Used; + var used = filtered ? resource.FilteredUsed : resource.Used; var usedDynamically = filtered ? resource.FilteredUsedDynamically : resource.UsedDynamically; - var sb = new StringBuilder(256); + var sb = new StringBuilder(256); if (withSize) { foreach (var (components, i) in (used ?? Array.Empty()).WithIndex()) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.ShpkTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.ShpkTab.cs index de20aa9f..b5b39e90 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.ShpkTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.ShpkTab.cs @@ -21,11 +21,11 @@ public partial class ModEditWindow public short NewMaterialParamStart; public short NewMaterialParamEnd; - public SharedSet[] FilterSystemValues; - public SharedSet[] FilterSceneValues; - public SharedSet[] FilterMaterialValues; - public SharedSet[] FilterSubViewValues; - public SharedSet FilterPasses; + public readonly SharedSet[] FilterSystemValues; + public readonly SharedSet[] FilterSceneValues; + public readonly SharedSet[] FilterMaterialValues; + public readonly SharedSet[] FilterSubViewValues; + public SharedSet FilterPasses; public readonly int FilterMaximumPopCount; public int FilterPopCount; @@ -46,6 +46,7 @@ public partial class ModEditWindow { Shpk = new ShpkFile(bytes, false); } + FilePath = filePath; Header = $"Shader Package for DirectX {(int)Shpk.DirectXVersion}"; @@ -105,13 +106,21 @@ public partial class ModEditWindow _nameSetWithIdsCache.Clear(); } - public void UpdateNameCache() + private void UpdateNameCache() { - static void CollectResourceNames(Dictionary nameCache, ShpkFile.Resource[] resources) - { - foreach (var resource in resources) - nameCache.TryAdd(resource.Id, resource.Name); - } + CollectResourceNames(_nameCache, Shpk.Constants); + CollectResourceNames(_nameCache, Shpk.Samplers); + CollectResourceNames(_nameCache, Shpk.Textures); + CollectResourceNames(_nameCache, Shpk.Uavs); + + CollectKeyNames(_nameCache, Shpk.SystemKeys); + CollectKeyNames(_nameCache, Shpk.SceneKeys); + CollectKeyNames(_nameCache, Shpk.MaterialKeys); + CollectKeyNames(_nameCache, Shpk.SubViewKeys); + + _nameSetCache.Clear(); + _nameSetWithIdsCache.Clear(); + return; static void CollectKeyNames(Dictionary nameCache, ShpkFile.Key[] keys) { @@ -128,18 +137,11 @@ public partial class ModEditWindow } } - CollectResourceNames(_nameCache, Shpk.Constants); - CollectResourceNames(_nameCache, Shpk.Samplers); - CollectResourceNames(_nameCache, Shpk.Textures); - CollectResourceNames(_nameCache, Shpk.Uavs); - - CollectKeyNames(_nameCache, Shpk.SystemKeys); - CollectKeyNames(_nameCache, Shpk.SceneKeys); - CollectKeyNames(_nameCache, Shpk.MaterialKeys); - CollectKeyNames(_nameCache, Shpk.SubViewKeys); - - _nameSetCache.Clear(); - _nameSetWithIdsCache.Clear(); + static void CollectResourceNames(Dictionary nameCache, ShpkFile.Resource[] resources) + { + foreach (var resource in resources) + nameCache.TryAdd(resource.Id, resource.Name); + } } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -151,6 +153,7 @@ public partial class ModEditWindow var cache = withIds ? _nameSetWithIdsCache : _nameSetCache; if (cache.TryGetValue(nameSet, out var nameSetStr)) return nameSetStr; + if (withIds) nameSetStr = string.Join(", ", nameSet.Select(id => $"{TryResolveName(id)} (0x{id:X8})")); else @@ -186,7 +189,8 @@ public partial class ModEditWindow var jEnd = ((param.ByteOffset + param.ByteSize - 1) >> 2) & 3; if ((param.ByteOffset & 0x3) != 0 || (param.ByteSize & 0x3) != 0) { - MalformedParameters.Add($"ID: {TryResolveName(param.Id)} (0x{param.Id:X8}), offset: 0x{param.ByteOffset:X4}, size: 0x{param.ByteSize:X4}"); + MalformedParameters.Add( + $"ID: {TryResolveName(param.Id)} (0x{param.Id:X8}), offset: 0x{param.ByteOffset:X4}, size: 0x{param.ByteSize:X4}"); continue; } @@ -206,7 +210,8 @@ public partial class ModEditWindow var tt = $"{MtrlTab.MaterialParamRangeName(materialParams?.Name ?? string.Empty, param.ByteOffset >> 2, param.ByteSize >> 2).Item1} ({TryResolveName(param.Id)}, 0x{param.Id:X8})"; if (component < defaultFloats.Length) - tt += $"\n\nDefault value: {defaultFloats[component]} ({defaults[component << 2]:X2} {defaults[(component << 2) | 1]:X2} {defaults[(component << 2) | 2]:X2} {defaults[(component << 2) | 3]:X2})"; + tt += + $"\n\nDefault value: {defaultFloats[component]} ({defaults[component << 2]:X2} {defaults[(component << 2) | 1]:X2} {defaults[(component << 2) | 2]:X2} {defaults[(component << 2) | 3]:X2})"; Matrix[i, j] = (TryResolveName(param.Id).ToString(), tt, (short)idx, 0); } } @@ -265,7 +270,8 @@ public partial class ModEditWindow if (oldStart == linear) newMaterialParamStart = (short)Orphans.Count; - Orphans.Add(($"{materialParams?.Name ?? ShpkFile.MaterialParamsConstantName}{MtrlTab.MaterialParamName(false, linear)}", linear)); + Orphans.Add(($"{materialParams?.Name ?? ShpkFile.MaterialParamsConstantName}{MtrlTab.MaterialParamName(false, linear)}", + linear)); } } @@ -407,7 +413,6 @@ public partial class ModEditWindow var unusedSlices = new JArray(); if (materialParameterUsage.Indices(start, length).Any()) - { foreach (var (rgStart, rgEnd) in materialParameterUsage.Ranges(start, length, true)) { unusedSlices.Add(new JObject @@ -417,14 +422,11 @@ public partial class ModEditWindow ["Length"] = rgEnd - rgStart, }); } - } else - { unusedSlices.Add(new JObject { ["Type"] = "Hidden", }); - } dkConstants[param.Id.ToString()] = unusedSlices; } From 6d42673aa4a3d9e289cc7d1ed1a8993ce1980a9e Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 4 Aug 2024 22:52:53 +0200 Subject: [PATCH 395/433] Update GameData. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index f2734d54..8ee82929 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit f2734d543d9b2debecb8feb6d6fa928801eb2bcb +Subproject commit 8ee82929fa6c725b8f556904ba022fb418991b5c From e91e0b23f8ffd9e01e9e139786ff0f7167a9e78a Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 4 Aug 2024 23:18:18 +0200 Subject: [PATCH 396/433] Unused usings. --- Penumbra/Penumbra.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index 557e011c..438cdc49 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -17,10 +17,8 @@ using Penumbra.UI.Tabs; using ChangedItemClick = Penumbra.Communication.ChangedItemClick; using ChangedItemHover = Penumbra.Communication.ChangedItemHover; using OtterGui.Tasks; -using Penumbra.GameData.Enums; using Penumbra.UI; using ResidentResourceManager = Penumbra.Interop.Services.ResidentResourceManager; -using System.Xml.Linq; using Dalamud.Plugin.Services; using Penumbra.GameData.Data; using Penumbra.Interop.Hooks; From 0064c4c96e04f29a90018e84e3a1fd3747eef0b7 Mon Sep 17 00:00:00 2001 From: Actions User Date: Sun, 4 Aug 2024 21:23:13 +0000 Subject: [PATCH 397/433] [CI] Updating repo.json for testing_1.2.0.20 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index cbe2b121..ad1cbef0 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.19", + "TestingAssemblyVersion": "1.2.0.20", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.19/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.20/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From a5859761906db84e24ae1fcf8648530f9940c5aa Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 5 Aug 2024 00:41:39 +0200 Subject: [PATCH 398/433] Make ImcChecker threadsafe. --- Penumbra/Meta/ImcChecker.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Penumbra/Meta/ImcChecker.cs b/Penumbra/Meta/ImcChecker.cs index 751113a0..4e3ff11b 100644 --- a/Penumbra/Meta/ImcChecker.cs +++ b/Penumbra/Meta/ImcChecker.cs @@ -12,12 +12,16 @@ public class ImcChecker public static int GetVariantCount(ImcIdentifier identifier) { - if (VariantCounts.TryGetValue(identifier, out var count)) - return count; + lock (VariantCounts) + { + if (VariantCounts.TryGetValue(identifier, out var count)) + return count; - count = GetFile(identifier)?.Count ?? 0; - VariantCounts[identifier] = count; - return count; + count = GetFile(identifier)?.Count ?? 0; + VariantCounts[identifier] = count; + + return count; + } } public readonly record struct CachedEntry(ImcEntry Entry, bool FileExists, bool VariantExists); From 2534f119e9c6a35c5280309f64cb3bdd9289d8c7 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 5 Aug 2024 00:41:55 +0200 Subject: [PATCH 399/433] Make StainService deal with early-loading. --- Penumbra/Services/StainService.cs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/Penumbra/Services/StainService.cs b/Penumbra/Services/StainService.cs index ba5c3e63..0a437da0 100644 --- a/Penumbra/Services/StainService.cs +++ b/Penumbra/Services/StainService.cs @@ -16,7 +16,8 @@ namespace Penumbra.Services; public class StainService : IService { public sealed class StainTemplateCombo(FilterComboColors[] stainCombos, StmFile stmFile) - : FilterComboCache(stmFile.Entries.Keys.Prepend((ushort)0), MouseWheelType.None, Penumbra.Log) where TDyePack : unmanaged, IDyePack + : FilterComboCache(stmFile.Entries.Keys.Prepend((ushort)0), MouseWheelType.None, Penumbra.Log) + where TDyePack : unmanaged, IDyePack { // FIXME There might be a better way to handle that. public int CurrentDyeChannel = 0; @@ -80,11 +81,21 @@ public class StainService : IService public unsafe StainService(IDataManager dataManager, CharacterUtility characterUtility, DictStain stainData) { - StainData = stainData; - StainCombo1 = CreateStainCombo(); - StainCombo2 = CreateStainCombo(); - LegacyStmFile = LoadStmFile(characterUtility.Address->LegacyStmResource, dataManager); - GudStmFile = LoadStmFile(characterUtility.Address->GudStmResource, dataManager); + StainData = stainData; + StainCombo1 = CreateStainCombo(); + StainCombo2 = CreateStainCombo(); + + if (characterUtility.Address == null) + { + LegacyStmFile = LoadStmFile(null, dataManager); + GudStmFile = LoadStmFile(null, dataManager); + } + else + { + LegacyStmFile = LoadStmFile(characterUtility.Address->LegacyStmResource, dataManager); + GudStmFile = LoadStmFile(characterUtility.Address->GudStmResource, dataManager); + } + FilterComboColors[] stainCombos = [StainCombo1, StainCombo2]; @@ -98,11 +109,13 @@ public class StainService : IService { 0 => StainCombo1, 1 => StainCombo2, - _ => throw new ArgumentOutOfRangeException(nameof(channel), channel, $"Unsupported dye channel {channel} (supported values are 0 and 1)") + _ => throw new ArgumentOutOfRangeException(nameof(channel), channel, + $"Unsupported dye channel {channel} (supported values are 0 and 1)"), }; /// Loads a STM file. Opportunistically attempts to re-use the file already read by the game, with Lumina fallback. - private static unsafe StmFile LoadStmFile(ResourceHandle* stmResourceHandle, IDataManager dataManager) where TDyePack : unmanaged, IDyePack + private static unsafe StmFile LoadStmFile(ResourceHandle* stmResourceHandle, IDataManager dataManager) + where TDyePack : unmanaged, IDyePack { if (stmResourceHandle != null) { From 1b5553284c102449bbd986be10e57259f2cb4bf4 Mon Sep 17 00:00:00 2001 From: Actions User Date: Sun, 4 Aug 2024 22:44:50 +0000 Subject: [PATCH 400/433] [CI] Updating repo.json for testing_1.2.0.21 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index ad1cbef0..10a08fa1 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.20", + "TestingAssemblyVersion": "1.2.0.21", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.20/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.21/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From 700fef4f04c283aada11ac2379bde2d5de7fb98a Mon Sep 17 00:00:00 2001 From: Exter-N Date: Mon, 5 Aug 2024 03:41:35 +0200 Subject: [PATCH 401/433] Move hook to MaterialResourceHandle.Load (inlining my beloathed) --- Penumbra.GameData | 2 +- .../{MtrlShpkLoaded.cs => MtrlLoaded.cs} | 4 ++-- Penumbra/Interop/Hooks/HookSettings.cs | 2 +- .../Hooks/PostProcessing/ShaderReplacementFixer.cs | 6 +++--- .../Resources/{LoadMtrlShpk.cs => LoadMtrl.cs} | 14 +++++++------- Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs | 1 + Penumbra/Services/CommunicatorService.cs | 6 +++--- 7 files changed, 18 insertions(+), 17 deletions(-) rename Penumbra/Communication/{MtrlShpkLoaded.cs => MtrlLoaded.cs} (73%) rename Penumbra/Interop/Hooks/Resources/{LoadMtrlShpk.cs => LoadMtrl.cs} (55%) diff --git a/Penumbra.GameData b/Penumbra.GameData index 8ee82929..ac9d9c78 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 8ee82929fa6c725b8f556904ba022fb418991b5c +Subproject commit ac9d9c78ae0025489b80ce2e798cdaacb0b43947 diff --git a/Penumbra/Communication/MtrlShpkLoaded.cs b/Penumbra/Communication/MtrlLoaded.cs similarity index 73% rename from Penumbra/Communication/MtrlShpkLoaded.cs rename to Penumbra/Communication/MtrlLoaded.cs index 9d3597a8..78498844 100644 --- a/Penumbra/Communication/MtrlShpkLoaded.cs +++ b/Penumbra/Communication/MtrlLoaded.cs @@ -6,11 +6,11 @@ namespace Penumbra.Communication; /// Parameter is the material resource handle for which the shader package has been loaded. /// Parameter is the associated game object. /// -public sealed class MtrlShpkLoaded() : EventWrapper(nameof(MtrlShpkLoaded)) +public sealed class MtrlLoaded() : EventWrapper(nameof(MtrlLoaded)) { public enum Priority { - /// + /// ShaderReplacementFixer = 0, } } diff --git a/Penumbra/Interop/Hooks/HookSettings.cs b/Penumbra/Interop/Hooks/HookSettings.cs index 0c0a4020..0bc55dc5 100644 --- a/Penumbra/Interop/Hooks/HookSettings.cs +++ b/Penumbra/Interop/Hooks/HookSettings.cs @@ -99,7 +99,7 @@ public class HookOverrides public struct ResourceHooks { public bool ApricotResourceLoad; - public bool LoadMtrlShpk; + public bool LoadMtrl; public bool LoadMtrlTex; public bool ResolvePathHooks; public bool ResourceHandleDestructor; diff --git a/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs b/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs index 53b69741..d02e18bb 100644 --- a/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs +++ b/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs @@ -110,7 +110,7 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic _modelRendererOnRenderMaterialHook = hooks.CreateHook("ModelRenderer.OnRenderMaterial", Sigs.ModelRendererOnRenderMaterial, ModelRendererOnRenderMaterialDetour, !HookOverrides.Instance.PostProcessing.ModelRendererOnRenderMaterial).Result; - _communicator.MtrlShpkLoaded.Subscribe(OnMtrlShpkLoaded, MtrlShpkLoaded.Priority.ShaderReplacementFixer); + _communicator.MtrlLoaded.Subscribe(OnMtrlLoaded, MtrlLoaded.Priority.ShaderReplacementFixer); _resourceHandleDestructor.Subscribe(OnResourceHandleDestructor, ResourceHandleDestructor.Priority.ShaderReplacementFixer); } @@ -118,7 +118,7 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic { _modelRendererOnRenderMaterialHook.Dispose(); _humanOnRenderMaterialHook.Dispose(); - _communicator.MtrlShpkLoaded.Unsubscribe(OnMtrlShpkLoaded); + _communicator.MtrlLoaded.Unsubscribe(OnMtrlLoaded); _resourceHandleDestructor.Unsubscribe(OnResourceHandleDestructor); _hairMaskState.ClearMaterials(); _characterOcclusionState.ClearMaterials(); @@ -147,7 +147,7 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic return shpkName.SequenceEqual(mtrlResource->ShpkNameSpan); } - private void OnMtrlShpkLoaded(nint mtrlResourceHandle, nint gameObject) + private void OnMtrlLoaded(nint mtrlResourceHandle, nint gameObject) { var mtrl = (MaterialResourceHandle*)mtrlResourceHandle; var shpk = mtrl->ShaderPackageResourceHandle; diff --git a/Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs b/Penumbra/Interop/Hooks/Resources/LoadMtrl.cs similarity index 55% rename from Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs rename to Penumbra/Interop/Hooks/Resources/LoadMtrl.cs index 8c410ad8..f56177e4 100644 --- a/Penumbra/Interop/Hooks/Resources/LoadMtrlShpk.cs +++ b/Penumbra/Interop/Hooks/Resources/LoadMtrl.cs @@ -5,28 +5,28 @@ using Penumbra.Services; namespace Penumbra.Interop.Hooks.Resources; -public sealed unsafe class LoadMtrlShpk : FastHook +public sealed unsafe class LoadMtrl : FastHook { private readonly GameState _gameState; private readonly CommunicatorService _communicator; - public LoadMtrlShpk(HookManager hooks, GameState gameState, CommunicatorService communicator) + public LoadMtrl(HookManager hooks, GameState gameState, CommunicatorService communicator) { _gameState = gameState; _communicator = communicator; - Task = hooks.CreateHook("Load Material Shaders", Sigs.LoadMtrlShpk, Detour, !HookOverrides.Instance.Resources.LoadMtrlShpk); + Task = hooks.CreateHook("Load Material", Sigs.LoadMtrl, Detour, !HookOverrides.Instance.Resources.LoadMtrl); } - public delegate byte Delegate(MaterialResourceHandle* mtrlResourceHandle); + public delegate byte Delegate(MaterialResourceHandle* mtrlResourceHandle, void* unk1, byte unk2); - private byte Detour(MaterialResourceHandle* handle) + private byte Detour(MaterialResourceHandle* handle, void* unk1, byte unk2) { var last = _gameState.MtrlData.Value; var mtrlData = _gameState.LoadSubFileHelper((nint)handle); _gameState.MtrlData.Value = mtrlData; - var ret = Task.Result.Original(handle); + var ret = Task.Result.Original(handle, unk1, unk2); _gameState.MtrlData.Value = last; - _communicator.MtrlShpkLoaded.Invoke((nint)handle, mtrlData.AssociatedGameObject); + _communicator.MtrlLoaded.Invoke((nint)handle, mtrlData.AssociatedGameObject); return ret; } } diff --git a/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs b/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs index 0759d9b1..1866e859 100644 --- a/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs +++ b/Penumbra/Interop/Hooks/Resources/LoadMtrlTex.cs @@ -4,6 +4,7 @@ using Penumbra.GameData; namespace Penumbra.Interop.Hooks.Resources; +// TODO check if this is still needed, as our hooked function is called by LoadMtrl's hooked function public sealed unsafe class LoadMtrlTex : FastHook { private readonly GameState _gameState; diff --git a/Penumbra/Services/CommunicatorService.cs b/Penumbra/Services/CommunicatorService.cs index cacbe689..5d745419 100644 --- a/Penumbra/Services/CommunicatorService.cs +++ b/Penumbra/Services/CommunicatorService.cs @@ -24,8 +24,8 @@ public class CommunicatorService : IDisposable, IService /// public readonly CreatedCharacterBase CreatedCharacterBase = new(); - /// - public readonly MtrlShpkLoaded MtrlShpkLoaded = new(); + /// + public readonly MtrlLoaded MtrlLoaded = new(); /// public readonly ModDataChanged ModDataChanged = new(); @@ -87,7 +87,7 @@ public class CommunicatorService : IDisposable, IService TemporaryGlobalModChange.Dispose(); CreatingCharacterBase.Dispose(); CreatedCharacterBase.Dispose(); - MtrlShpkLoaded.Dispose(); + MtrlLoaded.Dispose(); ModDataChanged.Dispose(); ModOptionChanged.Dispose(); ModDiscoveryStarted.Dispose(); From dba85f5da3774706f6005ddd56859bc78362afef Mon Sep 17 00:00:00 2001 From: Exter-N Date: Mon, 5 Aug 2024 03:43:18 +0200 Subject: [PATCH 402/433] Sanity check ShPk mods, ban incompatible ones --- .../Processing/ShpkPathPreProcessor.cs | 85 +++++++++++++++++++ Penumbra/Mods/Manager/ModManager.cs | 18 ++++ Penumbra/UI/ChatWarningService.cs | 56 ++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 Penumbra/Interop/Processing/ShpkPathPreProcessor.cs create mode 100644 Penumbra/UI/ChatWarningService.cs diff --git a/Penumbra/Interop/Processing/ShpkPathPreProcessor.cs b/Penumbra/Interop/Processing/ShpkPathPreProcessor.cs new file mode 100644 index 00000000..2c6f6901 --- /dev/null +++ b/Penumbra/Interop/Processing/ShpkPathPreProcessor.cs @@ -0,0 +1,85 @@ +using FFXIVClientStructs.FFXIV.Client.System.Resource; +using Penumbra.Api.Enums; +using Penumbra.Collections; +using Penumbra.GameData.Files; +using Penumbra.GameData.Files.Utility; +using Penumbra.Interop.Hooks.ResourceLoading; +using Penumbra.String; +using Penumbra.String.Classes; +using Penumbra.UI; + +namespace Penumbra.Interop.Processing; + +/// +/// Path pre-processor for shader packages that reverts redirects to known invalid files, as bad ShPks can crash the game. +/// +public sealed class ShpkPathPreProcessor(ResourceManagerService resourceManager, ChatWarningService chatWarningService) : IPathPreProcessor +{ + public ResourceType Type + => ResourceType.Shpk; + + public unsafe FullPath? PreProcess(ResolveData resolveData, CiByteString path, Utf8GamePath originalGamePath, bool nonDefault, FullPath? resolved) + { + chatWarningService.CleanLastFileWarnings(false); + + if (!resolved.HasValue) + return null; + + // Skip the sanity check for game files. We are not considering the case where the user has modified game file: it's at their own risk. + var resolvedPath = resolved.Value; + if (!resolvedPath.IsRooted) + return resolvedPath; + + // If the ShPk is already loaded, it means that it already passed the sanity check. + var existingResource = resourceManager.FindResource(ResourceCategory.Shader, ResourceType.Shpk, unchecked((uint)resolvedPath.InternalName.Crc32)); + if (existingResource != null) + return resolvedPath; + + var checkResult = SanityCheck(resolvedPath.FullName); + if (checkResult == SanityCheckResult.Success) + return resolvedPath; + + Penumbra.Log.Warning($"Refusing to honor file redirection because of failed sanity check (result: {checkResult}). Original path: {originalGamePath} Redirected path: {resolvedPath}"); + chatWarningService.PrintFileWarning(resolvedPath.FullName, originalGamePath, WarningMessageComplement(checkResult)); + + return null; + } + + private static SanityCheckResult SanityCheck(string path) + { + try + { + using var file = MmioMemoryManager.CreateFromFile(path); + var bytes = file.GetSpan(); + + return ShpkFile.FastIsLegacy(bytes) + ? SanityCheckResult.Legacy + : SanityCheckResult.Success; + } + catch (FileNotFoundException) + { + return SanityCheckResult.NotFound; + } + catch (IOException) + { + return SanityCheckResult.IoError; + } + } + + private static string WarningMessageComplement(SanityCheckResult result) + => result switch + { + SanityCheckResult.IoError => "cannot read the modded file.", + SanityCheckResult.NotFound => "the modded file does not exist.", + SanityCheckResult.Legacy => "this mod is not compatible with Dawntrail. Get an updated version, if possible, or disable it.", + _ => string.Empty, + }; + + private enum SanityCheckResult + { + Success, + IoError, + NotFound, + Legacy, + } +} diff --git a/Penumbra/Mods/Manager/ModManager.cs b/Penumbra/Mods/Manager/ModManager.cs index f170a31b..59f8906e 100644 --- a/Penumbra/Mods/Manager/ModManager.cs +++ b/Penumbra/Mods/Manager/ModManager.cs @@ -350,4 +350,22 @@ public sealed class ModManager : ModStorage, IDisposable, IService Penumbra.Log.Error($"Could not scan for mods:\n{ex}"); } } + + public bool TryIdentifyPath(string path, [NotNullWhen(true)] out Mod? mod, [NotNullWhen(true)] out string? relativePath) + { + var relPath = Path.GetRelativePath(BasePath.FullName, path); + if (relPath != "." && (relPath.StartsWith('.') || Path.IsPathRooted(relPath))) + { + mod = null; + relativePath = null; + return false; + } + + var modDirectorySeparator = relPath.IndexOfAny([Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar]); + + var modDirectory = modDirectorySeparator < 0 ? relPath : relPath[..modDirectorySeparator]; + relativePath = modDirectorySeparator < 0 ? string.Empty : relPath[(modDirectorySeparator + 1)..]; + + return TryGetMod(modDirectory, "\0", out mod); + } } diff --git a/Penumbra/UI/ChatWarningService.cs b/Penumbra/UI/ChatWarningService.cs new file mode 100644 index 00000000..84ede2fb --- /dev/null +++ b/Penumbra/UI/ChatWarningService.cs @@ -0,0 +1,56 @@ +using Dalamud.Plugin.Services; +using OtterGui.Services; +using Penumbra.Mods.Manager; +using Penumbra.String.Classes; + +namespace Penumbra.UI; + +public sealed class ChatWarningService(IChatGui chatGui, IClientState clientState, ModManager modManager) : IUiService +{ + private readonly Dictionary _lastFileWarnings = []; + private int _lastFileWarningsCleanCounter; + + private const int LastFileWarningsCleanCycle = 100; + private static readonly TimeSpan LastFileWarningsMaxAge = new(1, 0, 0); + + public void CleanLastFileWarnings(bool force) + { + if (!force) + { + _lastFileWarningsCleanCounter = (_lastFileWarningsCleanCounter + 1) % LastFileWarningsCleanCycle; + if (_lastFileWarningsCleanCounter != 0) + return; + } + + var expiredDate = DateTime.Now - LastFileWarningsMaxAge; + var toRemove = new HashSet(); + foreach (var (key, value) in _lastFileWarnings) + { + if (value.Item1 <= expiredDate) + toRemove.Add(key); + } + foreach (var key in toRemove) + _lastFileWarnings.Remove(key); + } + + public void PrintFileWarning(string fullPath, Utf8GamePath originalGamePath, string messageComplement) + { + CleanLastFileWarnings(true); + + // Don't warn twice for the same file within a certain time interval unless the reason changed. + if (_lastFileWarnings.TryGetValue(fullPath, out var lastWarning) && lastWarning.Item2 == messageComplement) + return; + + // Don't warn for files managed by other plugins, or files we aren't sure about. + if (!modManager.TryIdentifyPath(fullPath, out var mod, out _)) + return; + + // Don't warn if there's no local player (as an approximation of no chat), so as not to trigger the cooldown. + if (clientState.LocalPlayer == null) + return; + + // The wording is an allusion to tar's "Cowardly refusing to create an empty archive" + chatGui.PrintError($"Cowardly refusing to load replacement for {originalGamePath.Filename().ToString().ToLowerInvariant()} by {mod.Name}{(messageComplement.Length > 0 ? ": " : ".")}{messageComplement}", "Penumbra"); + _lastFileWarnings[fullPath] = (DateTime.Now, messageComplement); + } +} From a36f9ccec7f4a1adf6e95c484da5a5a9ae9c2d3b Mon Sep 17 00:00:00 2001 From: Exter-N Date: Mon, 5 Aug 2024 03:45:02 +0200 Subject: [PATCH 403/433] Improve ResourceTree display with new function --- Penumbra/Interop/ResourceTree/ResourceNode.cs | 4 ++++ .../ResourceTree/ResourceTreeFactory.cs | 19 ++++++++++++++++++- .../UI/AdvancedWindow/ResourceTreeViewer.cs | 5 ++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Penumbra/Interop/ResourceTree/ResourceNode.cs b/Penumbra/Interop/ResourceTree/ResourceNode.cs index 6ab48325..85d12ce7 100644 --- a/Penumbra/Interop/ResourceTree/ResourceNode.cs +++ b/Penumbra/Interop/ResourceTree/ResourceNode.cs @@ -15,6 +15,8 @@ public class ResourceNode : ICloneable public readonly nint ResourceHandle; public Utf8GamePath[] PossibleGamePaths; public FullPath FullPath; + public string? ModName; + public string? ModRelativePath; public CiByteString AdditionalData; public readonly ulong Length; public readonly List Children; @@ -57,6 +59,8 @@ public class ResourceNode : ICloneable ResourceHandle = other.ResourceHandle; PossibleGamePaths = other.PossibleGamePaths; FullPath = other.FullPath; + ModName = other.ModName; + ModRelativePath = other.ModRelativePath; AdditionalData = other.AdditionalData; Length = other.Length; Children = other.Children; diff --git a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs index 65fac68f..9738148f 100644 --- a/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs +++ b/Penumbra/Interop/ResourceTree/ResourceTreeFactory.cs @@ -9,6 +9,7 @@ using Penumbra.GameData.Enums; using Penumbra.GameData.Interop; using Penumbra.Interop.PathResolving; using Penumbra.Meta; +using Penumbra.Mods.Manager; using Penumbra.String.Classes; namespace Penumbra.Interop.ResourceTree; @@ -21,7 +22,8 @@ public class ResourceTreeFactory( ObjectIdentification objectIdentifier, Configuration config, ActorManager actors, - PathState pathState) : IService + PathState pathState, + ModManager modManager) : IService { private TreeBuildCache CreateTreeBuildCache() => new(objects, gameData, actors); @@ -93,7 +95,10 @@ public class ResourceTreeFactory( // This is currently unneeded as we can resolve all paths by querying the draw object: // ResolveGamePaths(tree, collectionResolveData.ModCollection); if (globalContext.WithUiData) + { ResolveUiData(tree); + ResolveModData(tree); + } FilterFullPaths(tree, (flags & Flags.RedactExternalPaths) != 0 ? config.ModDirectory : null); Cleanup(tree); @@ -123,6 +128,18 @@ public class ResourceTreeFactory( }); } + private void ResolveModData(ResourceTree tree) + { + foreach (var node in tree.FlatNodes) + { + if (node.FullPath.IsRooted && modManager.TryIdentifyPath(node.FullPath.FullName, out var mod, out var relativePath)) + { + node.ModName = mod.Name; + node.ModRelativePath = relativePath; + } + } + } + private static void FilterFullPaths(ResourceTree tree, string? onlyWithinPath) { foreach (var node in tree.FlatNodes) diff --git a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs index a991c948..361094c4 100644 --- a/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs +++ b/Penumbra/UI/AdvancedWindow/ResourceTreeViewer.cs @@ -316,7 +316,10 @@ public class ResourceTreeViewer ImGui.TableNextColumn(); if (resourceNode.FullPath.FullName.Length > 0) { - ImGui.Selectable(resourceNode.FullPath.ToPath(), false, 0, new Vector2(ImGui.GetContentRegionAvail().X, cellHeight)); + var uiFullPathStr = resourceNode.ModName != null && resourceNode.ModRelativePath != null + ? $"[{resourceNode.ModName}] {resourceNode.ModRelativePath}" + : resourceNode.FullPath.ToPath(); + ImGui.Selectable(uiFullPathStr, false, 0, new Vector2(ImGui.GetContentRegionAvail().X, cellHeight)); if (ImGui.IsItemClicked()) ImGui.SetClipboardText(resourceNode.FullPath.ToPath()); ImGuiUtil.HoverTooltip( From f68e919421f46ec24e9acf21bff5416f39d73a66 Mon Sep 17 00:00:00 2001 From: "N. Lo." Date: Mon, 5 Aug 2024 04:08:24 +0200 Subject: [PATCH 404/433] Fix LiveCTPreviewer instantiation --- Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs b/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs index bbd3b16c..61ccc95c 100644 --- a/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs +++ b/Penumbra/Interop/MaterialPreview/LiveColorTablePreviewer.cs @@ -37,7 +37,7 @@ public sealed unsafe class LiveColorTablePreviewer : LiveMaterialPreviewerBase _originalColorTableTexture = new SafeTextureHandle(*_colorTableTexture, true); - if (_originalColorTableTexture == null) + if (_originalColorTableTexture.Texture == null) throw new InvalidOperationException("Material doesn't have a color table"); Width = (int)_originalColorTableTexture.Texture->Width; From 0d1ed6a926ccb593bffa95d78a96b48bd222ecf7 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Mon, 5 Aug 2024 09:18:57 +0200 Subject: [PATCH 405/433] No, ImGui, these buttons aren't the same. --- .../Materials/MtrlTab.ColorTable.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs index 352681bb..df8485c9 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs @@ -91,15 +91,21 @@ public partial class MtrlTab var dyePackB = _stainService.GudStmFile.GetValueOrNull(dyeB.Template, previewDyeB); using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) { - ColorTableCopyClipboardButton(_colorTableSelectedPair << 1); - ImUtf8.SameLineInner(); - retA |= ColorTablePasteFromClipboardButton(_colorTableSelectedPair << 1, disabled); + using (ImUtf8.PushId("ClipboardA"u8)) + { + ColorTableCopyClipboardButton(_colorTableSelectedPair << 1); + ImUtf8.SameLineInner(); + retA |= ColorTablePasteFromClipboardButton(_colorTableSelectedPair << 1, disabled); + } ImGui.SameLine(); CenteredTextInRest($"Row {_colorTableSelectedPair + 1}A"); columns.Next(); - ColorTableCopyClipboardButton((_colorTableSelectedPair << 1) | 1); - ImUtf8.SameLineInner(); - retB |= ColorTablePasteFromClipboardButton((_colorTableSelectedPair << 1) | 1, disabled); + using (ImUtf8.PushId("ClipboardB"u8)) + { + ColorTableCopyClipboardButton((_colorTableSelectedPair << 1) | 1); + ImUtf8.SameLineInner(); + retB |= ColorTablePasteFromClipboardButton((_colorTableSelectedPair << 1) | 1, disabled); + } ImGui.SameLine(); CenteredTextInRest($"Row {_colorTableSelectedPair + 1}B"); } From 1187efa243fe4e645770ca8dc1b80a6159ce0932 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Mon, 5 Aug 2024 23:02:34 +0200 Subject: [PATCH 406/433] Reinstate single-row CT highlight --- .../Materials/MtrlTab.ColorTable.cs | 73 +++++++++++-------- .../Materials/MtrlTab.CommonColorTable.cs | 14 +++- .../Materials/MtrlTab.LegacyColorTable.cs | 14 +--- .../Materials/MtrlTab.LivePreview.cs | 28 ++++++- 4 files changed, 86 insertions(+), 43 deletions(-) diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs index df8485c9..0fa38a5d 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs @@ -30,11 +30,14 @@ public partial class MtrlTab var buttonWidth = (ImGui.GetContentRegionAvail().X - itemSpacing * 7.0f) * 0.125f; var frameHeight = ImGui.GetFrameHeight(); var highlighterSize = ImUtf8.CalcIconSize(FontAwesomeIcon.Crosshairs) + framePadding * 2.0f; - var spaceWidth = ImUtf8.CalcTextSize(" "u8).X; - var spacePadding = (int)MathF.Ceiling((highlighterSize.X + framePadding.X + itemInnerSpacing) / spaceWidth); using var font = ImRaii.PushFont(UiBuilder.MonoFont); using var alignment = ImRaii.PushStyle(ImGuiStyleVar.ButtonTextAlign, new Vector2(0, 0.5f)); + + // This depends on the font being pushed for "proper" alignment of the pair indices in the buttons. + var spaceWidth = ImUtf8.CalcTextSize(" "u8).X; + var spacePadding = (int)MathF.Ceiling((highlighterSize.X + framePadding.X + itemInnerSpacing) / spaceWidth); + for (var i = 0; i < ColorTable.NumRows >> 1; i += 8) { for (var j = 0; j < 8; ++j) @@ -72,7 +75,7 @@ public partial class MtrlTab var cursor = ImGui.GetCursorScreenPos(); ImGui.SetCursorScreenPos(rcMin with { Y = float.Lerp(rcMin.Y, rcMax.Y, 0.5f) - highlighterSize.Y * 0.5f }); font.Pop(); - ColorTableHighlightButton(pairIndex, disabled); + ColorTablePairHighlightButton(pairIndex, disabled); font.Push(UiBuilder.MonoFont); ImGui.SetCursorScreenPos(cursor); } @@ -83,6 +86,8 @@ public partial class MtrlTab { var retA = false; var retB = false; + var rowAIdx = _colorTableSelectedPair << 1; + var rowBIdx = rowAIdx | 1; var dyeA = dyeTable?[_colorTableSelectedPair << 1] ?? default; var dyeB = dyeTable?[(_colorTableSelectedPair << 1) | 1] ?? default; var previewDyeA = _stainService.GetStainCombo(dyeA.Channel).CurrentSelection.Key; @@ -91,23 +96,15 @@ public partial class MtrlTab var dyePackB = _stainService.GudStmFile.GetValueOrNull(dyeB.Template, previewDyeB); using (var columns = ImUtf8.Columns(2, "ColorTable"u8)) { - using (ImUtf8.PushId("ClipboardA"u8)) + using (ImUtf8.PushId("RowHeaderA"u8)) { - ColorTableCopyClipboardButton(_colorTableSelectedPair << 1); - ImUtf8.SameLineInner(); - retA |= ColorTablePasteFromClipboardButton(_colorTableSelectedPair << 1, disabled); + retA |= DrawRowHeader(rowAIdx, disabled); } - ImGui.SameLine(); - CenteredTextInRest($"Row {_colorTableSelectedPair + 1}A"); columns.Next(); - using (ImUtf8.PushId("ClipboardB"u8)) + using (ImUtf8.PushId("RowHeaderB"u8)) { - ColorTableCopyClipboardButton((_colorTableSelectedPair << 1) | 1); - ImUtf8.SameLineInner(); - retB |= ColorTablePasteFromClipboardButton((_colorTableSelectedPair << 1) | 1, disabled); + retB |= DrawRowHeader(rowBIdx, disabled); } - ImGui.SameLine(); - CenteredTextInRest($"Row {_colorTableSelectedPair + 1}B"); } DrawHeader(" Colors"u8); @@ -116,13 +113,13 @@ public partial class MtrlTab using var dis = ImRaii.Disabled(disabled); using (ImUtf8.PushId("ColorsA"u8)) { - retA |= DrawColors(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + retA |= DrawColors(table, dyeTable, dyePackA, rowAIdx); } columns.Next(); using (ImUtf8.PushId("ColorsB"u8)) { - retB |= DrawColors(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + retB |= DrawColors(table, dyeTable, dyePackB, rowBIdx); } } @@ -132,13 +129,13 @@ public partial class MtrlTab using var dis = ImRaii.Disabled(disabled); using (ImUtf8.PushId("PbrA"u8)) { - retA |= DrawPbr(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + retA |= DrawPbr(table, dyeTable, dyePackA, rowAIdx); } columns.Next(); using (ImUtf8.PushId("PbrB"u8)) { - retB |= DrawPbr(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + retB |= DrawPbr(table, dyeTable, dyePackB, rowBIdx); } } @@ -148,13 +145,13 @@ public partial class MtrlTab using var dis = ImRaii.Disabled(disabled); using (ImUtf8.PushId("SheenA"u8)) { - retA |= DrawSheen(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + retA |= DrawSheen(table, dyeTable, dyePackA, rowAIdx); } columns.Next(); using (ImUtf8.PushId("SheenB"u8)) { - retB |= DrawSheen(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + retB |= DrawSheen(table, dyeTable, dyePackB, rowBIdx); } } @@ -164,13 +161,13 @@ public partial class MtrlTab using var dis = ImRaii.Disabled(disabled); using (ImUtf8.PushId("BlendingA"u8)) { - retA |= DrawBlending(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + retA |= DrawBlending(table, dyeTable, dyePackA, rowAIdx); } columns.Next(); using (ImUtf8.PushId("BlendingB"u8)) { - retB |= DrawBlending(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + retB |= DrawBlending(table, dyeTable, dyePackB, rowBIdx); } } @@ -180,13 +177,13 @@ public partial class MtrlTab using var dis = ImRaii.Disabled(disabled); using (ImUtf8.PushId("TemplateA"u8)) { - retA |= DrawTemplate(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + retA |= DrawTemplate(table, dyeTable, dyePackA, rowAIdx); } columns.Next(); using (ImUtf8.PushId("TemplateB"u8)) { - retB |= DrawTemplate(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + retB |= DrawTemplate(table, dyeTable, dyePackB, rowBIdx); } } @@ -197,13 +194,13 @@ public partial class MtrlTab using var dis = ImRaii.Disabled(disabled); using (ImUtf8.PushId("DyeA"u8)) { - retA |= DrawDye(dyeTable, dyePackA, _colorTableSelectedPair << 1); + retA |= DrawDye(dyeTable, dyePackA, rowAIdx); } columns.Next(); using (ImUtf8.PushId("DyeB"u8)) { - retB |= DrawDye(dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + retB |= DrawDye(dyeTable, dyePackB, rowBIdx); } } @@ -213,20 +210,20 @@ public partial class MtrlTab using var dis = ImRaii.Disabled(disabled); using (ImUtf8.PushId("FurtherA"u8)) { - retA |= DrawFurther(table, dyeTable, dyePackA, _colorTableSelectedPair << 1); + retA |= DrawFurther(table, dyeTable, dyePackA, rowAIdx); } columns.Next(); using (ImUtf8.PushId("FurtherB"u8)) { - retB |= DrawFurther(table, dyeTable, dyePackB, (_colorTableSelectedPair << 1) | 1); + retB |= DrawFurther(table, dyeTable, dyePackB, rowBIdx); } } if (retA) - UpdateColorTableRowPreview(_colorTableSelectedPair << 1); + UpdateColorTableRowPreview(rowAIdx); if (retB) - UpdateColorTableRowPreview((_colorTableSelectedPair << 1) | 1); + UpdateColorTableRowPreview(rowBIdx); return retA | retB; } @@ -239,6 +236,20 @@ public partial class MtrlTab ImUtf8.CollapsingHeader(label, ImGuiTreeNodeFlags.Leaf); } + private bool DrawRowHeader(int rowIdx, bool disabled) + { + ColorTableCopyClipboardButton(rowIdx); + ImUtf8.SameLineInner(); + var ret = ColorTablePasteFromClipboardButton(rowIdx, disabled); + ImUtf8.SameLineInner(); + ColorTableRowHighlightButton(rowIdx, disabled); + + ImGui.SameLine(); + CenteredTextInRest($"Row {(rowIdx >> 1) + 1}{"AB"[rowIdx & 1]}"); + + return ret; + } + private static bool DrawColors(ColorTable table, ColorDyeTable? dyeTable, DyePack? dyePack, int rowIdx) { var dyeOffset = ImGui.GetContentRegionAvail().X diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs index 09c8ea61..38f02100 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.CommonColorTable.cs @@ -228,7 +228,7 @@ public partial class MtrlTab } } - private void ColorTableHighlightButton(int pairIdx, bool disabled) + private void ColorTablePairHighlightButton(int pairIdx, bool disabled) { ImUtf8.IconButton(FontAwesomeIcon.Crosshairs, "Highlight this pair of rows on your character, if possible.\n\nHighlight colors can be configured in Penumbra's settings."u8, @@ -240,6 +240,18 @@ public partial class MtrlTab CancelColorTableHighlight(); } + private void ColorTableRowHighlightButton(int rowIdx, bool disabled) + { + ImUtf8.IconButton(FontAwesomeIcon.Crosshairs, + "Highlight this row on your character, if possible.\n\nHighlight colors can be configured in Penumbra's settings."u8, + ImGui.GetFrameHeight() * Vector2.One, disabled || _colorTablePreviewers.Count == 0); + + if (ImGui.IsItemHovered()) + HighlightColorTableRow(rowIdx); + else if (_highlightedColorTableRow == rowIdx) + CancelColorTableHighlight(); + } + private static void CtBlendRect(Vector2 rcMin, Vector2 rcMax, uint topColor, uint bottomColor) { var style = ImGui.GetStyle(); diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs index 0ff2b01f..f21d86a9 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LegacyColorTable.cs @@ -103,11 +103,8 @@ public partial class MtrlTab ColorTableCopyClipboardButton(rowIdx); ImUtf8.SameLineInner(); var ret = ColorTablePasteFromClipboardButton(rowIdx, disabled); - if ((rowIdx & 1) == 0) - { - ImUtf8.SameLineInner(); - ColorTableHighlightButton(rowIdx >> 1, disabled); - } + ImUtf8.SameLineInner(); + ColorTableRowHighlightButton(rowIdx, disabled); ImGui.TableNextColumn(); using (ImRaii.PushFont(UiBuilder.MonoFont)) @@ -213,11 +210,8 @@ public partial class MtrlTab ColorTableCopyClipboardButton(rowIdx); ImUtf8.SameLineInner(); var ret = ColorTablePasteFromClipboardButton(rowIdx, disabled); - if ((rowIdx & 1) == 0) - { - ImUtf8.SameLineInner(); - ColorTableHighlightButton(rowIdx >> 1, disabled); - } + ImUtf8.SameLineInner(); + ColorTableRowHighlightButton(rowIdx, disabled); ImGui.TableNextColumn(); using (ImRaii.PushFont(UiBuilder.MonoFont)) diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs index 6089f2d5..01a40980 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.LivePreview.cs @@ -14,6 +14,7 @@ public partial class MtrlTab { private readonly List _materialPreviewers = new(4); private readonly List _colorTablePreviewers = new(4); + private int _highlightedColorTableRow = -1; private int _highlightedColorTablePair = -1; private readonly Stopwatch _highlightTime = new(); @@ -168,13 +169,35 @@ public partial class MtrlTab } } + private void HighlightColorTableRow(int rowIdx) + { + var oldRowIdx = _highlightedColorTableRow; + + if (_highlightedColorTableRow != rowIdx) + { + _highlightedColorTableRow = rowIdx; + _highlightTime.Restart(); + } + + if (oldRowIdx >= 0) + UpdateColorTableRowPreview(oldRowIdx); + + if (rowIdx >= 0) + UpdateColorTableRowPreview(rowIdx); + } + private void CancelColorTableHighlight() { + var rowIdx = _highlightedColorTableRow; var pairIdx = _highlightedColorTablePair; + _highlightedColorTableRow = -1; _highlightedColorTablePair = -1; _highlightTime.Reset(); + if (rowIdx >= 0) + UpdateColorTableRowPreview(rowIdx); + if (pairIdx >= 0) { UpdateColorTableRowPreview(pairIdx << 1); @@ -214,7 +237,7 @@ public partial class MtrlTab } } - if (_highlightedColorTablePair << 1 == rowIdx) + if (_highlightedColorTablePair << 1 == rowIdx || _highlightedColorTableRow == rowIdx) ApplyHighlight(ref row, ColorId.InGameHighlight, (float)_highlightTime.Elapsed.TotalSeconds); else if (((_highlightedColorTablePair << 1) | 1) == rowIdx) ApplyHighlight(ref row, ColorId.InGameHighlight2, (float)_highlightTime.Elapsed.TotalSeconds); @@ -247,6 +270,9 @@ public partial class MtrlTab rows.ApplyDye(_stainService.GudStmFile, stainIds, dyeRows); } + if (_highlightedColorTableRow >= 0) + ApplyHighlight(ref rows[_highlightedColorTableRow], ColorId.InGameHighlight, (float)_highlightTime.Elapsed.TotalSeconds); + if (_highlightedColorTablePair >= 0) { ApplyHighlight(ref rows[_highlightedColorTablePair << 1], ColorId.InGameHighlight, (float)_highlightTime.Elapsed.TotalSeconds); From 2bf08c8c899dcb2111098affc6902b60ffcca9c6 Mon Sep 17 00:00:00 2001 From: "N. Lo." Date: Tue, 6 Aug 2024 13:05:21 +0200 Subject: [PATCH 407/433] Fix dye template combo (aka "git gud") --- Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs index df8485c9..13a36c71 100644 --- a/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs +++ b/Penumbra/UI/AdvancedWindow/Materials/MtrlTab.ColorTable.cs @@ -595,7 +595,7 @@ public partial class MtrlTab if (_stainService.GudTemplateCombo.Draw("##dyeTemplate", dye.Template.ToString(), string.Empty, scalarSize + ImGui.GetStyle().ScrollbarSize / 2, ImGui.GetTextLineHeightWithSpacing(), ImGuiComboFlags.NoArrowButton)) { - dye.Template = _stainService.LegacyTemplateCombo.CurrentSelection; + dye.Template = _stainService.GudTemplateCombo.CurrentSelection; ret = true; } From df58ac7e9248fdf3fcf465c1a6c2880577331d79 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 7 Aug 2024 15:44:21 +0200 Subject: [PATCH 408/433] Fix ref. --- Penumbra/Communication/MtrlShpkLoaded.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Communication/MtrlShpkLoaded.cs b/Penumbra/Communication/MtrlShpkLoaded.cs index 9d3597a8..2b286bb9 100644 --- a/Penumbra/Communication/MtrlShpkLoaded.cs +++ b/Penumbra/Communication/MtrlShpkLoaded.cs @@ -10,7 +10,7 @@ public sealed class MtrlShpkLoaded() : EventWrapper + /// ShaderReplacementFixer = 0, } } From fe4a046cc99b7ede7777c233df4089992931e914 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 7 Aug 2024 16:37:58 +0200 Subject: [PATCH 409/433] Make ChatWarningService part of the MessageService. --- OtterGui | 2 +- .../Processing/ShpkPathPreProcessor.cs | 27 +++++---- Penumbra/Services/MessageService.cs | 16 ++++++ Penumbra/UI/ChatWarningService.cs | 56 ------------------- 4 files changed, 32 insertions(+), 69 deletions(-) delete mode 100644 Penumbra/UI/ChatWarningService.cs diff --git a/OtterGui b/OtterGui index c53955cb..d9486ae5 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit c53955cb6199dd418c5a9538d3251ac5942e7067 +Subproject commit d9486ae54b5a4b61cf74f79ed27daa659eb1ce5b diff --git a/Penumbra/Interop/Processing/ShpkPathPreProcessor.cs b/Penumbra/Interop/Processing/ShpkPathPreProcessor.cs index 2c6f6901..96d9daff 100644 --- a/Penumbra/Interop/Processing/ShpkPathPreProcessor.cs +++ b/Penumbra/Interop/Processing/ShpkPathPreProcessor.cs @@ -4,23 +4,26 @@ using Penumbra.Collections; using Penumbra.GameData.Files; using Penumbra.GameData.Files.Utility; using Penumbra.Interop.Hooks.ResourceLoading; +using Penumbra.Mods.Manager; +using Penumbra.Services; using Penumbra.String; using Penumbra.String.Classes; -using Penumbra.UI; namespace Penumbra.Interop.Processing; /// /// Path pre-processor for shader packages that reverts redirects to known invalid files, as bad ShPks can crash the game. /// -public sealed class ShpkPathPreProcessor(ResourceManagerService resourceManager, ChatWarningService chatWarningService) : IPathPreProcessor +public sealed class ShpkPathPreProcessor(ResourceManagerService resourceManager, MessageService messager, ModManager modManager) + : IPathPreProcessor { public ResourceType Type => ResourceType.Shpk; - public unsafe FullPath? PreProcess(ResolveData resolveData, CiByteString path, Utf8GamePath originalGamePath, bool nonDefault, FullPath? resolved) + public unsafe FullPath? PreProcess(ResolveData resolveData, CiByteString path, Utf8GamePath originalGamePath, bool nonDefault, + FullPath? resolved) { - chatWarningService.CleanLastFileWarnings(false); + messager.CleanTaggedMessages(false); if (!resolved.HasValue) return null; @@ -31,7 +34,8 @@ public sealed class ShpkPathPreProcessor(ResourceManagerService resourceManager, return resolvedPath; // If the ShPk is already loaded, it means that it already passed the sanity check. - var existingResource = resourceManager.FindResource(ResourceCategory.Shader, ResourceType.Shpk, unchecked((uint)resolvedPath.InternalName.Crc32)); + var existingResource = + resourceManager.FindResource(ResourceCategory.Shader, ResourceType.Shpk, unchecked((uint)resolvedPath.InternalName.Crc32)); if (existingResource != null) return resolvedPath; @@ -39,8 +43,7 @@ public sealed class ShpkPathPreProcessor(ResourceManagerService resourceManager, if (checkResult == SanityCheckResult.Success) return resolvedPath; - Penumbra.Log.Warning($"Refusing to honor file redirection because of failed sanity check (result: {checkResult}). Original path: {originalGamePath} Redirected path: {resolvedPath}"); - chatWarningService.PrintFileWarning(resolvedPath.FullName, originalGamePath, WarningMessageComplement(checkResult)); + messager.PrintFileWarning(modManager, resolvedPath.FullName, originalGamePath, WarningMessageComplement(checkResult)); return null; } @@ -49,8 +52,8 @@ public sealed class ShpkPathPreProcessor(ResourceManagerService resourceManager, { try { - using var file = MmioMemoryManager.CreateFromFile(path); - var bytes = file.GetSpan(); + using var file = MmioMemoryManager.CreateFromFile(path); + var bytes = file.GetSpan(); return ShpkFile.FastIsLegacy(bytes) ? SanityCheckResult.Legacy @@ -69,9 +72,9 @@ public sealed class ShpkPathPreProcessor(ResourceManagerService resourceManager, private static string WarningMessageComplement(SanityCheckResult result) => result switch { - SanityCheckResult.IoError => "cannot read the modded file.", - SanityCheckResult.NotFound => "the modded file does not exist.", - SanityCheckResult.Legacy => "this mod is not compatible with Dawntrail. Get an updated version, if possible, or disable it.", + SanityCheckResult.IoError => "Cannot read the modded file.", + SanityCheckResult.NotFound => "The modded file does not exist.", + SanityCheckResult.Legacy => "This mod is not compatible with Dawntrail. Get an updated version, if possible, or disable it.", _ => string.Empty, }; diff --git a/Penumbra/Services/MessageService.cs b/Penumbra/Services/MessageService.cs index 08118483..a35a67f1 100644 --- a/Penumbra/Services/MessageService.cs +++ b/Penumbra/Services/MessageService.cs @@ -2,10 +2,14 @@ using Dalamud.Game.Text; using Dalamud.Game.Text.SeStringHandling; using Dalamud.Game.Text.SeStringHandling.Payloads; using Dalamud.Interface; +using Dalamud.Interface.ImGuiNotification; using Dalamud.Plugin.Services; using Lumina.Excel.GeneratedSheets; using OtterGui.Log; using OtterGui.Services; +using Penumbra.Mods.Manager; +using Penumbra.String.Classes; +using Notification = OtterGui.Classes.Notification; namespace Penumbra.Services; @@ -38,4 +42,16 @@ public class MessageService(Logger log, IUiBuilder builder, IChatGui chat, INoti Message = payload, }); } + + public void PrintFileWarning(ModManager modManager, string fullPath, Utf8GamePath originalGamePath, string messageComplement) + { + // Don't warn for files managed by other plugins, or files we aren't sure about. + if (!modManager.TryIdentifyPath(fullPath, out var mod, out _)) + return; + + AddTaggedMessage($"{fullPath}.{messageComplement}", + new Notification( + $"Cowardly refusing to load replacement for {originalGamePath.Filename().ToString().ToLowerInvariant()} by {mod.Name}{(messageComplement.Length > 0 ? ":\n" : ".")}{messageComplement}", + NotificationType.Warning, 10000)); + } } diff --git a/Penumbra/UI/ChatWarningService.cs b/Penumbra/UI/ChatWarningService.cs deleted file mode 100644 index 84ede2fb..00000000 --- a/Penumbra/UI/ChatWarningService.cs +++ /dev/null @@ -1,56 +0,0 @@ -using Dalamud.Plugin.Services; -using OtterGui.Services; -using Penumbra.Mods.Manager; -using Penumbra.String.Classes; - -namespace Penumbra.UI; - -public sealed class ChatWarningService(IChatGui chatGui, IClientState clientState, ModManager modManager) : IUiService -{ - private readonly Dictionary _lastFileWarnings = []; - private int _lastFileWarningsCleanCounter; - - private const int LastFileWarningsCleanCycle = 100; - private static readonly TimeSpan LastFileWarningsMaxAge = new(1, 0, 0); - - public void CleanLastFileWarnings(bool force) - { - if (!force) - { - _lastFileWarningsCleanCounter = (_lastFileWarningsCleanCounter + 1) % LastFileWarningsCleanCycle; - if (_lastFileWarningsCleanCounter != 0) - return; - } - - var expiredDate = DateTime.Now - LastFileWarningsMaxAge; - var toRemove = new HashSet(); - foreach (var (key, value) in _lastFileWarnings) - { - if (value.Item1 <= expiredDate) - toRemove.Add(key); - } - foreach (var key in toRemove) - _lastFileWarnings.Remove(key); - } - - public void PrintFileWarning(string fullPath, Utf8GamePath originalGamePath, string messageComplement) - { - CleanLastFileWarnings(true); - - // Don't warn twice for the same file within a certain time interval unless the reason changed. - if (_lastFileWarnings.TryGetValue(fullPath, out var lastWarning) && lastWarning.Item2 == messageComplement) - return; - - // Don't warn for files managed by other plugins, or files we aren't sure about. - if (!modManager.TryIdentifyPath(fullPath, out var mod, out _)) - return; - - // Don't warn if there's no local player (as an approximation of no chat), so as not to trigger the cooldown. - if (clientState.LocalPlayer == null) - return; - - // The wording is an allusion to tar's "Cowardly refusing to create an empty archive" - chatGui.PrintError($"Cowardly refusing to load replacement for {originalGamePath.Filename().ToString().ToLowerInvariant()} by {mod.Name}{(messageComplement.Length > 0 ? ": " : ".")}{messageComplement}", "Penumbra"); - _lastFileWarnings[fullPath] = (DateTime.Now, messageComplement); - } -} From d630a3dff42295b972a0c1d864468d4d384edb9b Mon Sep 17 00:00:00 2001 From: Actions User Date: Wed, 7 Aug 2024 14:47:58 +0000 Subject: [PATCH 410/433] [CI] Updating repo.json for testing_1.2.0.22 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 10a08fa1..3de0c5c8 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.21", + "TestingAssemblyVersion": "1.2.0.22", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.21/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.22/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From fb58a9c27194d2107cd926d3a31f5a8d4600a1d4 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Thu, 8 Aug 2024 23:19:18 +0200 Subject: [PATCH 411/433] Add/improve ShaderReplacementFixer hooks --- Penumbra.GameData | 2 +- Penumbra/Interop/Hooks/HookSettings.cs | 2 + .../PostProcessing/HumanSetupScalingHook.cs | 55 ++++ .../PostProcessing/PreBoneDeformerReplacer.cs | 49 +-- .../PostProcessing/ShaderReplacementFixer.cs | 284 ++++++++++++++++-- Penumbra/Interop/Services/CharacterUtility.cs | 32 +- Penumbra/Interop/Services/ModelRenderer.cs | 44 +-- .../Interop/Structs/CharacterUtilityData.cs | 26 +- .../Interop/Structs/ModelRendererStructs.cs | 35 +++ Penumbra/UI/Tabs/Debug/DebugTab.cs | 62 ++-- 10 files changed, 478 insertions(+), 113 deletions(-) create mode 100644 Penumbra/Interop/Hooks/PostProcessing/HumanSetupScalingHook.cs create mode 100644 Penumbra/Interop/Structs/ModelRendererStructs.cs diff --git a/Penumbra.GameData b/Penumbra.GameData index ac9d9c78..2fd5aa44 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit ac9d9c78ae0025489b80ce2e798cdaacb0b43947 +Subproject commit 2fd5aa44056a906df90c9a826d1d17f6fdafebff diff --git a/Penumbra/Interop/Hooks/HookSettings.cs b/Penumbra/Interop/Hooks/HookSettings.cs index 0bc55dc5..a1dd374f 100644 --- a/Penumbra/Interop/Hooks/HookSettings.cs +++ b/Penumbra/Interop/Hooks/HookSettings.cs @@ -80,6 +80,8 @@ public class HookOverrides public bool HumanCreateDeformer; public bool HumanOnRenderMaterial; public bool ModelRendererOnRenderMaterial; + public bool ModelRendererUnkFunc; + public bool PrepareColorTable; } public struct ResourceLoadingHooks diff --git a/Penumbra/Interop/Hooks/PostProcessing/HumanSetupScalingHook.cs b/Penumbra/Interop/Hooks/PostProcessing/HumanSetupScalingHook.cs new file mode 100644 index 00000000..5783c099 --- /dev/null +++ b/Penumbra/Interop/Hooks/PostProcessing/HumanSetupScalingHook.cs @@ -0,0 +1,55 @@ +using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; +using OtterGui.Services; + +namespace Penumbra.Interop.Hooks.PostProcessing; + +// TODO: "SetupScaling" does not seem to only set up scaling -> find a better name? +public unsafe class HumanSetupScalingHook : FastHook +{ + private const int ReplacementCapacity = 2; + + public event EventDelegate? SetupReplacements; + + public HumanSetupScalingHook(HookManager hooks, CharacterBaseVTables vTables) + { + Task = hooks.CreateHook("Human.SetupScaling", vTables.HumanVTable[58], Detour, + !HookOverrides.Instance.PostProcessing.HumanSetupScaling); + } + + private void Detour(CharacterBase* drawObject, uint slotIndex) + { + Span replacements = stackalloc Replacement[ReplacementCapacity]; + var numReplacements = 0; + IDisposable? pbdDisposable = null; + object? shpkLock = null; + var releaseLock = false; + + try + { + SetupReplacements?.Invoke(drawObject, slotIndex, replacements, ref numReplacements, ref pbdDisposable, ref shpkLock); + if (shpkLock != null) + { + Monitor.Enter(shpkLock); + releaseLock = true; + } + for (var i = 0; i < numReplacements; ++i) + *(nint*)replacements[i].AddressToReplace = replacements[i].ValueToSet; + Task.Result.Original(drawObject, slotIndex); + } + finally + { + for (var i = numReplacements; i-- > 0;) + *(nint*)replacements[i].AddressToReplace = replacements[i].ValueToRestore; + if (releaseLock) + Monitor.Exit(shpkLock!); + pbdDisposable?.Dispose(); + } + } + + public delegate void Delegate(CharacterBase* drawObject, uint slotIndex); + + public delegate void EventDelegate(CharacterBase* drawObject, uint slotIndex, Span replacements, ref int numReplacements, + ref IDisposable? pbdDisposable, ref object? shpkLock); + + public readonly record struct Replacement(nint AddressToReplace, nint ValueToSet, nint ValueToRestore); +} diff --git a/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs b/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs index 1aa09d7f..9273a2cb 100644 --- a/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs +++ b/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs @@ -18,27 +18,26 @@ public sealed unsafe class PreBoneDeformerReplacer : IDisposable, IRequiredServi public static readonly Utf8GamePath PreBoneDeformerPath = Utf8GamePath.FromSpan("chara/xls/boneDeformer/human.pbd"u8, MetaDataComputation.All, out var p) ? p : Utf8GamePath.Empty; - // Approximate name guesses. - private delegate void CharacterBaseSetupScalingDelegate(CharacterBase* drawObject, uint slotIndex); - private delegate void* CharacterBaseCreateDeformerDelegate(CharacterBase* drawObject, uint slotIndex); + // Approximate name guess. + private delegate void* CharacterBaseCreateDeformerDelegate(CharacterBase* drawObject, uint slotIndex); - private readonly Hook _humanSetupScalingHook; private readonly Hook _humanCreateDeformerHook; - private readonly CharacterUtility _utility; - private readonly CollectionResolver _collectionResolver; - private readonly ResourceLoader _resourceLoader; - private readonly IFramework _framework; + private readonly CharacterUtility _utility; + private readonly CollectionResolver _collectionResolver; + private readonly ResourceLoader _resourceLoader; + private readonly IFramework _framework; + private readonly HumanSetupScalingHook _humanSetupScalingHook; public PreBoneDeformerReplacer(CharacterUtility utility, CollectionResolver collectionResolver, ResourceLoader resourceLoader, - HookManager hooks, IFramework framework, CharacterBaseVTables vTables) + HookManager hooks, IFramework framework, CharacterBaseVTables vTables, HumanSetupScalingHook humanSetupScalingHook) { - _utility = utility; - _collectionResolver = collectionResolver; - _resourceLoader = resourceLoader; - _framework = framework; - _humanSetupScalingHook = hooks.CreateHook("HumanSetupScaling", vTables.HumanVTable[58], SetupScaling, - !HookOverrides.Instance.PostProcessing.HumanSetupScaling).Result; + _utility = utility; + _collectionResolver = collectionResolver; + _resourceLoader = resourceLoader; + _framework = framework; + _humanSetupScalingHook = humanSetupScalingHook; + _humanSetupScalingHook.SetupReplacements += SetupHSSReplacements; _humanCreateDeformerHook = hooks.CreateHook("HumanCreateDeformer", vTables.HumanVTable[101], CreateDeformer, !HookOverrides.Instance.PostProcessing.HumanCreateDeformer).Result; } @@ -46,7 +45,7 @@ public sealed unsafe class PreBoneDeformerReplacer : IDisposable, IRequiredServi public void Dispose() { _humanCreateDeformerHook.Dispose(); - _humanSetupScalingHook.Dispose(); + _humanSetupScalingHook.SetupReplacements -= SetupHSSReplacements; } private SafeResourceHandle GetPreBoneDeformerForCharacter(CharacterBase* drawObject) @@ -58,22 +57,24 @@ public sealed unsafe class PreBoneDeformerReplacer : IDisposable, IRequiredServi return cache.CustomResources.Get(ResourceCategory.Chara, ResourceType.Pbd, PreBoneDeformerPath, resolveData); } - private void SetupScaling(CharacterBase* drawObject, uint slotIndex) + private void SetupHSSReplacements(CharacterBase* drawObject, uint slotIndex, Span replacements, + ref int numReplacements, ref IDisposable? pbdDisposable, ref object? shpkLock) { if (!_framework.IsInFrameworkUpdateThread) Penumbra.Log.Warning( - $"{nameof(PreBoneDeformerReplacer)}.{nameof(SetupScaling)}(0x{(nint)drawObject:X}, {slotIndex}) called out of framework thread"); + $"{nameof(PreBoneDeformerReplacer)}.{nameof(SetupHSSReplacements)}(0x{(nint)drawObject:X}, {slotIndex}) called out of framework thread"); - using var preBoneDeformer = GetPreBoneDeformerForCharacter(drawObject); + var preBoneDeformer = GetPreBoneDeformerForCharacter(drawObject); try { - if (!preBoneDeformer.IsInvalid) - _utility.Address->HumanPbdResource = (Structs.ResourceHandle*)preBoneDeformer.ResourceHandle; - _humanSetupScalingHook.Original(drawObject, slotIndex); + pbdDisposable = preBoneDeformer; + replacements[numReplacements++] = new((nint)(&_utility.Address->HumanPbdResource), (nint)preBoneDeformer.ResourceHandle, + _utility.DefaultHumanPbdResource); } - finally + catch { - _utility.Address->HumanPbdResource = (Structs.ResourceHandle*)_utility.DefaultHumanPbdResource; + preBoneDeformer.Dispose(); + throw; } } diff --git a/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs b/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs index d02e18bb..20db7e25 100644 --- a/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs +++ b/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs @@ -1,4 +1,5 @@ using Dalamud.Hooking; +using FFXIVClientStructs.FFXIV.Client.Graphics.Kernel; using FFXIVClientStructs.FFXIV.Client.Graphics.Render; using FFXIVClientStructs.FFXIV.Client.Graphics.Scene; using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; @@ -7,6 +8,8 @@ using OtterGui.Services; using Penumbra.Communication; using Penumbra.GameData; using Penumbra.Interop.Hooks.Resources; +using Penumbra.Interop.Services; +using Penumbra.Interop.Structs; using Penumbra.Services; using CharacterUtility = Penumbra.Interop.Services.CharacterUtility; using CSModelRenderer = FFXIVClientStructs.FFXIV.Client.Graphics.Render.ModelRenderer; @@ -19,6 +22,12 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic public static ReadOnlySpan SkinShpkName => "skin.shpk"u8; + public static ReadOnlySpan CharacterStockingsShpkName + => "characterstockings.shpk"u8; + + public static ReadOnlySpan CharacterLegacyShpkName + => "characterlegacy.shpk"u8; + public static ReadOnlySpan IrisShpkName => "iris.shpk"u8; @@ -42,16 +51,26 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic private delegate nint ModelRendererOnRenderMaterialDelegate(CSModelRenderer* modelRenderer, ushort* outFlags, CSModelRenderer.OnRenderModelParams* param, Material* material, uint materialIndex); + private delegate void ModelRendererUnkFuncDelegate(CSModelRenderer* modelRenderer, ModelRendererStructs.UnkPayload* unkPayload, uint unk2, + uint unk3, uint unk4, uint unk5); + private readonly Hook _humanOnRenderMaterialHook; private readonly Hook _modelRendererOnRenderMaterialHook; + private readonly Hook _modelRendererUnkFuncHook; + + private readonly Hook _prepareColorTableHook; + private readonly ResourceHandleDestructor _resourceHandleDestructor; private readonly CommunicatorService _communicator; private readonly CharacterUtility _utility; private readonly ModelRenderer _modelRenderer; + private readonly HumanSetupScalingHook _humanSetupScalingHook; private readonly ModdedShaderPackageState _skinState; + private readonly ModdedShaderPackageState _characterStockingsState; + private readonly ModdedShaderPackageState _characterLegacyState; private readonly ModdedShaderPackageState _irisState; private readonly ModdedShaderPackageState _characterGlassState; private readonly ModdedShaderPackageState _characterTransparencyState; @@ -64,6 +83,12 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic public uint ModdedSkinShpkCount => _skinState.MaterialCount; + public uint ModdedCharacterStockingsShpkCount + => _characterStockingsState.MaterialCount; + + public uint ModdedCharacterLegacyShpkCount + => _characterLegacyState.MaterialCount; + public uint ModdedIrisShpkCount => _irisState.MaterialCount; @@ -83,16 +108,23 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic => _hairMaskState.MaterialCount; public ShaderReplacementFixer(ResourceHandleDestructor resourceHandleDestructor, CharacterUtility utility, ModelRenderer modelRenderer, - CommunicatorService communicator, HookManager hooks, CharacterBaseVTables vTables) + CommunicatorService communicator, HookManager hooks, CharacterBaseVTables vTables, HumanSetupScalingHook humanSetupScalingHook) { _resourceHandleDestructor = resourceHandleDestructor; _utility = utility; _modelRenderer = modelRenderer; _communicator = communicator; + _humanSetupScalingHook = humanSetupScalingHook; _skinState = new ModdedShaderPackageState( () => (ShaderPackageResourceHandle**)&_utility.Address->SkinShpkResource, () => (ShaderPackageResourceHandle*)_utility.DefaultSkinShpkResource); + _characterStockingsState = new ModdedShaderPackageState( + () => (ShaderPackageResourceHandle**)&_utility.Address->CharacterStockingsShpkResource, + () => (ShaderPackageResourceHandle*)_utility.DefaultCharacterStockingsShpkResource); + _characterLegacyState = new ModdedShaderPackageState( + () => (ShaderPackageResourceHandle**)&_utility.Address->CharacterLegacyShpkResource, + () => (ShaderPackageResourceHandle*)_utility.DefaultCharacterLegacyShpkResource); _irisState = new ModdedShaderPackageState(() => _modelRenderer.IrisShaderPackage, () => _modelRenderer.DefaultIrisShaderPackage); _characterGlassState = new ModdedShaderPackageState(() => _modelRenderer.CharacterGlassShaderPackage, () => _modelRenderer.DefaultCharacterGlassShaderPackage); @@ -105,33 +137,50 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic _hairMaskState = new ModdedShaderPackageState(() => _modelRenderer.HairMaskShaderPackage, () => _modelRenderer.DefaultHairMaskShaderPackage); + _humanSetupScalingHook.SetupReplacements += SetupHSSReplacements; _humanOnRenderMaterialHook = hooks.CreateHook("Human.OnRenderMaterial", vTables.HumanVTable[64], OnRenderHumanMaterial, !HookOverrides.Instance.PostProcessing.HumanOnRenderMaterial).Result; _modelRendererOnRenderMaterialHook = hooks.CreateHook("ModelRenderer.OnRenderMaterial", Sigs.ModelRendererOnRenderMaterial, ModelRendererOnRenderMaterialDetour, !HookOverrides.Instance.PostProcessing.ModelRendererOnRenderMaterial).Result; + _modelRendererUnkFuncHook = hooks.CreateHook("ModelRenderer.UnkFunc", + Sigs.ModelRendererUnkFunc, ModelRendererUnkFuncDetour, + !HookOverrides.Instance.PostProcessing.ModelRendererUnkFunc).Result; + _prepareColorTableHook = hooks.CreateHook("MaterialResourceHandle.PrepareColorTable", + Sigs.PrepareColorSet, PrepareColorTableDetour, + !HookOverrides.Instance.PostProcessing.PrepareColorTable).Result; + _communicator.MtrlLoaded.Subscribe(OnMtrlLoaded, MtrlLoaded.Priority.ShaderReplacementFixer); _resourceHandleDestructor.Subscribe(OnResourceHandleDestructor, ResourceHandleDestructor.Priority.ShaderReplacementFixer); } public void Dispose() { + _prepareColorTableHook.Dispose(); + _modelRendererUnkFuncHook.Dispose(); _modelRendererOnRenderMaterialHook.Dispose(); _humanOnRenderMaterialHook.Dispose(); + _humanSetupScalingHook.SetupReplacements -= SetupHSSReplacements; + _communicator.MtrlLoaded.Unsubscribe(OnMtrlLoaded); _resourceHandleDestructor.Unsubscribe(OnResourceHandleDestructor); + _hairMaskState.ClearMaterials(); _characterOcclusionState.ClearMaterials(); _characterTattooState.ClearMaterials(); _characterTransparencyState.ClearMaterials(); _characterGlassState.ClearMaterials(); _irisState.ClearMaterials(); + _characterLegacyState.ClearMaterials(); + _characterStockingsState.ClearMaterials(); _skinState.ClearMaterials(); } - public (ulong Skin, ulong Iris, ulong CharacterGlass, ulong CharacterTransparency, ulong CharacterTattoo, ulong CharacterOcclusion, ulong - HairMask) GetAndResetSlowPathCallDeltas() + public (ulong Skin, ulong CharacterStockings, ulong CharacterLegacy, ulong Iris, ulong CharacterGlass, ulong CharacterTransparency, ulong + CharacterTattoo, ulong CharacterOcclusion, ulong HairMask) GetAndResetSlowPathCallDeltas() => (_skinState.GetAndResetSlowPathCallDelta(), + _characterStockingsState.GetAndResetSlowPathCallDelta(), + _characterLegacyState.GetAndResetSlowPathCallDelta(), _irisState.GetAndResetSlowPathCallDelta(), _characterGlassState.GetAndResetSlowPathCallDelta(), _characterTransparencyState.GetAndResetSlowPathCallDelta(), @@ -155,7 +204,8 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic return; var shpkName = mtrl->ShpkNameSpan; - var shpkState = GetStateForHuman(shpkName) ?? GetStateForModelRenderer(shpkName); + var shpkState = GetStateForHumanSetup(shpkName) ?? GetStateForHumanRender(shpkName) ?? GetStateForModelRendererRender(shpkName) + ?? GetStateForModelRendererUnk(shpkName) ?? GetStateForColorTable(shpkName); if (shpkState != null && shpk != shpkState.DefaultShaderPackage) shpkState.TryAddMaterial(mtrlResourceHandle); @@ -164,6 +214,8 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic private void OnResourceHandleDestructor(Structs.ResourceHandle* handle) { _skinState.TryRemoveMaterial(handle); + _characterStockingsState.TryRemoveMaterial(handle); + _characterLegacyState.TryRemoveMaterial(handle); _irisState.TryRemoveMaterial(handle); _characterGlassState.TryRemoveMaterial(handle); _characterTransparencyState.TryRemoveMaterial(handle); @@ -172,10 +224,25 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic _hairMaskState.TryRemoveMaterial(handle); } - private ModdedShaderPackageState? GetStateForHuman(MaterialResourceHandle* mtrlResource) - => mtrlResource == null ? null : GetStateForHuman(mtrlResource->ShpkNameSpan); + private ModdedShaderPackageState? GetStateForHumanSetup(MaterialResourceHandle* mtrlResource) + => mtrlResource == null ? null : GetStateForHumanSetup(mtrlResource->ShpkNameSpan); - private ModdedShaderPackageState? GetStateForHuman(ReadOnlySpan shpkName) + private ModdedShaderPackageState? GetStateForHumanSetup(ReadOnlySpan shpkName) + { + if (CharacterStockingsShpkName.SequenceEqual(shpkName)) + return _characterStockingsState; + + return null; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + private uint GetTotalMaterialCountForHumanSetup() + => _characterStockingsState.MaterialCount; + + private ModdedShaderPackageState? GetStateForHumanRender(MaterialResourceHandle* mtrlResource) + => mtrlResource == null ? null : GetStateForHumanRender(mtrlResource->ShpkNameSpan); + + private ModdedShaderPackageState? GetStateForHumanRender(ReadOnlySpan shpkName) { if (SkinShpkName.SequenceEqual(shpkName)) return _skinState; @@ -184,17 +251,14 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic } [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private uint GetTotalMaterialCountForHuman() + private uint GetTotalMaterialCountForHumanRender() => _skinState.MaterialCount; - private ModdedShaderPackageState? GetStateForModelRenderer(MaterialResourceHandle* mtrlResource) - => mtrlResource == null ? null : GetStateForModelRenderer(mtrlResource->ShpkNameSpan); + private ModdedShaderPackageState? GetStateForModelRendererRender(MaterialResourceHandle* mtrlResource) + => mtrlResource == null ? null : GetStateForModelRendererRender(mtrlResource->ShpkNameSpan); - private ModdedShaderPackageState? GetStateForModelRenderer(ReadOnlySpan shpkName) + private ModdedShaderPackageState? GetStateForModelRendererRender(ReadOnlySpan shpkName) { - if (IrisShpkName.SequenceEqual(shpkName)) - return _irisState; - if (CharacterGlassShpkName.SequenceEqual(shpkName)) return _characterGlassState; @@ -204,9 +268,6 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic if (CharacterTattooShpkName.SequenceEqual(shpkName)) return _characterTattooState; - if (CharacterOcclusionShpkName.SequenceEqual(shpkName)) - return _characterOcclusionState; - if (HairMaskShpkName.SequenceEqual(shpkName)) return _hairMaskState; @@ -214,24 +275,93 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic } [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - private uint GetTotalMaterialCountForModelRenderer() - => _irisState.MaterialCount - + _characterGlassState.MaterialCount + private uint GetTotalMaterialCountForModelRendererRender() + => _characterGlassState.MaterialCount + _characterTransparencyState.MaterialCount + _characterTattooState.MaterialCount - + _characterOcclusionState.MaterialCount + _hairMaskState.MaterialCount; + private ModdedShaderPackageState? GetStateForModelRendererUnk(MaterialResourceHandle* mtrlResource) + => mtrlResource == null ? null : GetStateForModelRendererUnk(mtrlResource->ShpkNameSpan); + + private ModdedShaderPackageState? GetStateForModelRendererUnk(ReadOnlySpan shpkName) + { + if (IrisShpkName.SequenceEqual(shpkName)) + return _irisState; + + if (CharacterOcclusionShpkName.SequenceEqual(shpkName)) + return _characterOcclusionState; + + if (CharacterStockingsShpkName.SequenceEqual(shpkName)) + return _characterStockingsState; + + return null; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + private uint GetTotalMaterialCountForModelRendererUnk() + => _irisState.MaterialCount + + _characterOcclusionState.MaterialCount + + _characterStockingsState.MaterialCount; + + private ModdedShaderPackageState? GetStateForColorTable(ReadOnlySpan shpkName) + { + if (CharacterLegacyShpkName.SequenceEqual(shpkName)) + return _characterLegacyState; + + return null; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + private uint GetTotalMaterialCountForColorTable() + => _characterLegacyState.MaterialCount; + + private void SetupHSSReplacements(CharacterBase* drawObject, uint slotIndex, Span replacements, + ref int numReplacements, ref IDisposable? pbdDisposable, ref object? shpkLock) + { + // If we don't have any on-screen instances of modded characterstockings.shpk, we don't need the slow path at all. + if (!Enabled || GetTotalMaterialCountForHumanSetup() == 0) + return; + + var model = drawObject->Models[slotIndex]; + if (model == null) + return; + MaterialResourceHandle* mtrlResource = null; + ModdedShaderPackageState? shpkState = null; + foreach (var material in model->MaterialsSpan) + { + if (material.Value == null) + continue; + + mtrlResource = material.Value->MaterialResourceHandle; + shpkState = GetStateForHumanSetup(mtrlResource); + // Despite this function being called with what designates a model (and therefore potentially many materials), + // we currently don't need to handle more than one modded ShPk. + if (shpkState != null) + break; + } + if (shpkState == null || shpkState.MaterialCount == 0) + return; + + shpkState.IncrementSlowPathCallDelta(); + + // This is less performance-critical than the others, as this is called by the game only on draw object creation and slot update. + // There are still thread safety concerns as it might be called in other threads by plugins. + shpkLock = shpkState; + replacements[numReplacements++] = new((nint)shpkState.ShaderPackageReference, (nint)mtrlResource->ShaderPackageResourceHandle, + (nint)shpkState.DefaultShaderPackage); + } + private nint OnRenderHumanMaterial(CharacterBase* human, CSModelRenderer.OnRenderMaterialParams* param) { // If we don't have any on-screen instances of modded skin.shpk, we don't need the slow path at all. - if (!Enabled || GetTotalMaterialCountForHuman() == 0) + if (!Enabled || GetTotalMaterialCountForHumanRender() == 0) return _humanOnRenderMaterialHook.Original(human, param); var material = param->Model->Materials[param->MaterialIndex]; var mtrlResource = material->MaterialResourceHandle; - var shpkState = GetStateForHuman(mtrlResource); - if (shpkState == null) + var shpkState = GetStateForHumanRender(mtrlResource); + if (shpkState == null || shpkState.MaterialCount == 0) return _humanOnRenderMaterialHook.Original(human, param); shpkState.IncrementSlowPathCallDelta(); @@ -259,18 +389,18 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic private nint ModelRendererOnRenderMaterialDetour(CSModelRenderer* modelRenderer, ushort* outFlags, CSModelRenderer.OnRenderModelParams* param, Material* material, uint materialIndex) { - // If we don't have any on-screen instances of modded characterglass.shpk, we don't need the slow path at all. - if (!Enabled || GetTotalMaterialCountForModelRenderer() == 0) + // If we don't have any on-screen instances of modded characterglass.shpk or others, we don't need the slow path at all. + if (!Enabled || GetTotalMaterialCountForModelRendererRender() == 0) return _modelRendererOnRenderMaterialHook.Original(modelRenderer, outFlags, param, material, materialIndex); var mtrlResource = material->MaterialResourceHandle; - var shpkState = GetStateForModelRenderer(mtrlResource); - if (shpkState == null) + var shpkState = GetStateForModelRendererRender(mtrlResource); + if (shpkState == null || shpkState.MaterialCount == 0) return _modelRendererOnRenderMaterialHook.Original(modelRenderer, outFlags, param, material, materialIndex); shpkState.IncrementSlowPathCallDelta(); - // Same performance considerations as above. + // Same performance considerations as OnRenderHumanMaterial. lock (shpkState) { var shpkReference = shpkState.ShaderPackageReference; @@ -286,6 +416,102 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic } } + private void ModelRendererUnkFuncDetour(CSModelRenderer* modelRenderer, ModelRendererStructs.UnkPayload* unkPayload, uint unk2, uint unk3, + uint unk4, uint unk5) + { + // If we don't have any on-screen instances of modded iris.shpk or others, we don't need the slow path at all. + if (!Enabled || GetTotalMaterialCountForModelRendererUnk() == 0) + { + _modelRendererUnkFuncHook.Original(modelRenderer, unkPayload, unk2, unk3, unk4, unk5); + return; + } + + var mtrlResource = GetMaterialResourceHandle(unkPayload); + var shpkState = GetStateForModelRendererUnk(mtrlResource); + if (shpkState == null || shpkState.MaterialCount == 0) + { + _modelRendererUnkFuncHook.Original(modelRenderer, unkPayload, unk2, unk3, unk4, unk5); + return; + } + + shpkState.IncrementSlowPathCallDelta(); + + // Same performance considerations as OnRenderHumanMaterial. + lock (shpkState) + { + var shpkReference = shpkState.ShaderPackageReference; + try + { + *shpkReference = mtrlResource->ShaderPackageResourceHandle; + _modelRendererUnkFuncHook.Original(modelRenderer, unkPayload, unk2, unk3, unk4, unk5); + } + finally + { + *shpkReference = shpkState.DefaultShaderPackage; + } + } + } + + private MaterialResourceHandle* GetMaterialResourceHandle(ModelRendererStructs.UnkPayload* unkPayload) + { + // TODO ClientStructs-ify + var unkPointer = *(nint*)((nint)unkPayload->ModelResourceHandle + 0xE8) + unkPayload->UnkIndex * 0x24; + var materialIndex = *(ushort*)(unkPointer + 8); + var material = unkPayload->Params->Model->Materials[materialIndex]; + if (material == null) + return null; + + var mtrlResource = material->MaterialResourceHandle; + if (mtrlResource == null) + return null; + + if (mtrlResource->ShaderPackageResourceHandle == null) + { + Penumbra.Log.Warning($"ShaderReplacementFixer found a MaterialResourceHandle with no shader package"); + return null; + } + + if (mtrlResource->ShaderPackageResourceHandle->ShaderPackage != unkPayload->ShaderWrapper->ShaderPackage) + { + Penumbra.Log.Warning($"ShaderReplacementFixer found a MaterialResourceHandle (0x{(nint)mtrlResource:X}) with an inconsistent shader package (got 0x{(nint)mtrlResource->ShaderPackageResourceHandle->ShaderPackage:X}, expected 0x{(nint)unkPayload->ShaderWrapper->ShaderPackage:X})"); + return null; + } + + return mtrlResource; + } + + private Texture* PrepareColorTableDetour(MaterialResourceHandle* thisPtr, byte stain0Id, byte stain1Id) + { + // If we don't have any on-screen instances of modded characterlegacy.shpk, we don't need the slow path at all. + if (!Enabled || GetTotalMaterialCountForColorTable() == 0) + return _prepareColorTableHook.Original(thisPtr, stain0Id, stain1Id); + + var material = thisPtr->Material; + if (material == null) + return _prepareColorTableHook.Original(thisPtr, stain0Id, stain1Id); + + var shpkState = GetStateForColorTable(thisPtr->ShpkNameSpan); + if (shpkState == null || shpkState.MaterialCount == 0) + return _prepareColorTableHook.Original(thisPtr, stain0Id, stain1Id); + + shpkState.IncrementSlowPathCallDelta(); + + // Same performance considerations as HumanSetupScalingDetour. + lock (shpkState) + { + var shpkReference = shpkState.ShaderPackageReference; + try + { + *shpkReference = thisPtr->ShaderPackageResourceHandle; + return _prepareColorTableHook.Original(thisPtr, stain0Id, stain1Id); + } + finally + { + *shpkReference = shpkState.DefaultShaderPackage; + } + } + } + private sealed class ModdedShaderPackageState(ShaderPackageReferenceGetter referenceGetter, DefaultShaderPackageGetter defaultGetter) { // MaterialResourceHandle set diff --git a/Penumbra/Interop/Services/CharacterUtility.cs b/Penumbra/Interop/Services/CharacterUtility.cs index 532dc823..4ab156a9 100644 --- a/Penumbra/Interop/Services/CharacterUtility.cs +++ b/Penumbra/Interop/Services/CharacterUtility.cs @@ -28,10 +28,12 @@ public unsafe class CharacterUtility : IDisposable, IRequiredService public bool Ready { get; private set; } public event Action LoadingFinished; - public nint DefaultHumanPbdResource { get; private set; } - public nint DefaultTransparentResource { get; private set; } - public nint DefaultDecalResource { get; private set; } - public nint DefaultSkinShpkResource { get; private set; } + public nint DefaultHumanPbdResource { get; private set; } + public nint DefaultTransparentResource { get; private set; } + public nint DefaultDecalResource { get; private set; } + public nint DefaultSkinShpkResource { get; private set; } + public nint DefaultCharacterStockingsShpkResource { get; private set; } + public nint DefaultCharacterLegacyShpkResource { get; private set; } /// /// The relevant indices depend on which meta manipulations we allow for. @@ -108,6 +110,18 @@ public unsafe class CharacterUtility : IDisposable, IRequiredService anyMissing |= DefaultSkinShpkResource == nint.Zero; } + if (DefaultCharacterStockingsShpkResource == nint.Zero) + { + DefaultCharacterStockingsShpkResource = (nint)Address->CharacterStockingsShpkResource; + anyMissing |= DefaultCharacterStockingsShpkResource == nint.Zero; + } + + if (DefaultCharacterLegacyShpkResource == nint.Zero) + { + DefaultCharacterLegacyShpkResource = (nint)Address->CharacterLegacyShpkResource; + anyMissing |= DefaultCharacterLegacyShpkResource == nint.Zero; + } + if (anyMissing) return; @@ -122,10 +136,12 @@ public unsafe class CharacterUtility : IDisposable, IRequiredService if (!Ready) return; - Address->HumanPbdResource = (ResourceHandle*)DefaultHumanPbdResource; - Address->TransparentTexResource = (TextureResourceHandle*)DefaultTransparentResource; - Address->DecalTexResource = (TextureResourceHandle*)DefaultDecalResource; - Address->SkinShpkResource = (ResourceHandle*)DefaultSkinShpkResource; + Address->HumanPbdResource = (ResourceHandle*)DefaultHumanPbdResource; + Address->TransparentTexResource = (TextureResourceHandle*)DefaultTransparentResource; + Address->DecalTexResource = (TextureResourceHandle*)DefaultDecalResource; + Address->SkinShpkResource = (ResourceHandle*)DefaultSkinShpkResource; + Address->CharacterStockingsShpkResource = (ResourceHandle*)DefaultCharacterStockingsShpkResource; + Address->CharacterLegacyShpkResource = (ResourceHandle*)DefaultCharacterLegacyShpkResource; } public void Dispose() diff --git a/Penumbra/Interop/Services/ModelRenderer.cs b/Penumbra/Interop/Services/ModelRenderer.cs index 10f3977f..5e2cd1fb 100644 --- a/Penumbra/Interop/Services/ModelRenderer.cs +++ b/Penumbra/Interop/Services/ModelRenderer.cs @@ -2,6 +2,7 @@ using Dalamud.Plugin.Services; using FFXIVClientStructs.FFXIV.Client.Graphics.Render; using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; using OtterGui.Services; +using ModelRendererData = FFXIVClientStructs.FFXIV.Client.Graphics.Render.ModelRenderer; namespace Penumbra.Interop.Services; @@ -9,46 +10,53 @@ public unsafe class ModelRenderer : IDisposable, IRequiredService { public bool Ready { get; private set; } - public ShaderPackageResourceHandle** IrisShaderPackage + public ModelRendererData* Address => Manager.Instance() switch { null => null, - var renderManager => &renderManager->ModelRenderer.IrisShaderPackage, + var renderManager => &renderManager->ModelRenderer, + }; + + public ShaderPackageResourceHandle** IrisShaderPackage + => Address switch + { + null => null, + var data => &data->IrisShaderPackage, }; public ShaderPackageResourceHandle** CharacterGlassShaderPackage - => Manager.Instance() switch + => Address switch { - null => null, - var renderManager => &renderManager->ModelRenderer.CharacterGlassShaderPackage, + null => null, + var data => &data->CharacterGlassShaderPackage, }; public ShaderPackageResourceHandle** CharacterTransparencyShaderPackage - => Manager.Instance() switch + => Address switch { - null => null, - var renderManager => &renderManager->ModelRenderer.CharacterTransparencyShaderPackage, + null => null, + var data => &data->CharacterTransparencyShaderPackage, }; public ShaderPackageResourceHandle** CharacterTattooShaderPackage - => Manager.Instance() switch + => Address switch { - null => null, - var renderManager => &renderManager->ModelRenderer.CharacterTattooShaderPackage, + null => null, + var data => &data->CharacterTattooShaderPackage, }; public ShaderPackageResourceHandle** CharacterOcclusionShaderPackage - => Manager.Instance() switch + => Address switch { - null => null, - var renderManager => &renderManager->ModelRenderer.CharacterOcclusionShaderPackage, + null => null, + var data => &data->CharacterOcclusionShaderPackage, }; public ShaderPackageResourceHandle** HairMaskShaderPackage - => Manager.Instance() switch + => Address switch { - null => null, - var renderManager => &renderManager->ModelRenderer.HairMaskShaderPackage, + null => null, + var data => &data->HairMaskShaderPackage, }; public ShaderPackageResourceHandle* DefaultIrisShaderPackage { get; private set; } @@ -96,7 +104,7 @@ public unsafe class ModelRenderer : IDisposable, IRequiredService if (DefaultCharacterTransparencyShaderPackage == null) { DefaultCharacterTransparencyShaderPackage = *CharacterTransparencyShaderPackage; - anyMissing |= DefaultCharacterTransparencyShaderPackage == null; + anyMissing |= DefaultCharacterTransparencyShaderPackage == null; } if (DefaultCharacterTattooShaderPackage == null) diff --git a/Penumbra/Interop/Structs/CharacterUtilityData.cs b/Penumbra/Interop/Structs/CharacterUtilityData.cs index 7595353f..8543466d 100644 --- a/Penumbra/Interop/Structs/CharacterUtilityData.cs +++ b/Penumbra/Interop/Structs/CharacterUtilityData.cs @@ -5,15 +5,17 @@ namespace Penumbra.Interop.Structs; [StructLayout(LayoutKind.Explicit)] public unsafe struct CharacterUtilityData { - public const int IndexHumanPbd = 63; - public const int IndexTransparentTex = 79; - public const int IndexDecalTex = 80; - public const int IndexTileOrbArrayTex = 81; - public const int IndexTileNormArrayTex = 82; - public const int IndexSkinShpk = 83; - public const int IndexGudStm = 94; - public const int IndexLegacyStm = 95; - public const int IndexSphereDArrayTex = 96; + public const int IndexHumanPbd = 63; + public const int IndexTransparentTex = 79; + public const int IndexDecalTex = 80; + public const int IndexTileOrbArrayTex = 81; + public const int IndexTileNormArrayTex = 82; + public const int IndexSkinShpk = 83; + public const int IndexCharacterStockingsShpk = 84; + public const int IndexCharacterLegacyShpk = 85; + public const int IndexGudStm = 94; + public const int IndexLegacyStm = 95; + public const int IndexSphereDArrayTex = 96; public static readonly MetaIndex[] EqdpIndices = Enum.GetNames() .Zip(Enum.GetValues()) @@ -111,6 +113,12 @@ public unsafe struct CharacterUtilityData [FieldOffset(8 + IndexSkinShpk * 8)] public ResourceHandle* SkinShpkResource; + [FieldOffset(8 + IndexCharacterStockingsShpk * 8)] + public ResourceHandle* CharacterStockingsShpkResource; + + [FieldOffset(8 + IndexCharacterLegacyShpk * 8)] + public ResourceHandle* CharacterLegacyShpkResource; + [FieldOffset(8 + IndexGudStm * 8)] public ResourceHandle* GudStmResource; diff --git a/Penumbra/Interop/Structs/ModelRendererStructs.cs b/Penumbra/Interop/Structs/ModelRendererStructs.cs new file mode 100644 index 00000000..551a32e3 --- /dev/null +++ b/Penumbra/Interop/Structs/ModelRendererStructs.cs @@ -0,0 +1,35 @@ +using FFXIVClientStructs.FFXIV.Client.Graphics.Kernel; +using FFXIVClientStructs.FFXIV.Client.Graphics.Render; +using FFXIVClientStructs.FFXIV.Client.System.Resource.Handle; + +namespace Penumbra.Interop.Structs; + +public static unsafe class ModelRendererStructs +{ + [StructLayout(LayoutKind.Explicit, Size = 0x28)] + public struct UnkShaderWrapper + { + [FieldOffset(0)] + public void* Vtbl; + + [FieldOffset(8)] + public ShaderPackage* ShaderPackage; + } + + // Unknown size, this is allocated on FUN_1404446c0's stack (E8 ?? ?? ?? ?? FF C3 41 3B DE 72 ?? 48 C7 85) + [StructLayout(LayoutKind.Explicit)] + public struct UnkPayload + { + [FieldOffset(0)] + public ModelRenderer.OnRenderModelParams* Params; + + [FieldOffset(8)] + public ModelResourceHandle* ModelResourceHandle; + + [FieldOffset(0x10)] + public UnkShaderWrapper* ShaderWrapper; + + [FieldOffset(0x1C)] + public ushort UnkIndex; + } +} diff --git a/Penumbra/UI/Tabs/Debug/DebugTab.cs b/Penumbra/UI/Tabs/Debug/DebugTab.cs index 7dae19c8..5b82a523 100644 --- a/Penumbra/UI/Tabs/Debug/DebugTab.cs +++ b/Penumbra/UI/Tabs/Debug/DebugTab.cs @@ -833,20 +833,6 @@ public class DebugTab : Window, ITab, IUiService ImGui.TableSetupColumn("\u0394 Slow-Path Calls", ImGuiTableColumnFlags.WidthStretch, 0.2f); ImGui.TableHeadersRow(); - ImGui.TableNextColumn(); - ImGui.TextUnformatted("skin.shpk"); - ImGui.TableNextColumn(); - ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedSkinShpkCount}"); - ImGui.TableNextColumn(); - ImGui.TextUnformatted($"{slowPathCallDeltas.Skin}"); - - ImGui.TableNextColumn(); - ImGui.TextUnformatted("iris.shpk"); - ImGui.TableNextColumn(); - ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedIrisShpkCount}"); - ImGui.TableNextColumn(); - ImGui.TextUnformatted($"{slowPathCallDeltas.Iris}"); - ImGui.TableNextColumn(); ImGui.TextUnformatted("characterglass.shpk"); ImGui.TableNextColumn(); @@ -855,18 +841,11 @@ public class DebugTab : Window, ITab, IUiService ImGui.TextUnformatted($"{slowPathCallDeltas.CharacterGlass}"); ImGui.TableNextColumn(); - ImGui.TextUnformatted("charactertransparency.shpk"); + ImGui.TextUnformatted("characterlegacy.shpk"); ImGui.TableNextColumn(); - ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedCharacterTransparencyShpkCount}"); + ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedCharacterLegacyShpkCount}"); ImGui.TableNextColumn(); - ImGui.TextUnformatted($"{slowPathCallDeltas.CharacterTransparency}"); - - ImGui.TableNextColumn(); - ImGui.TextUnformatted("charactertattoo.shpk"); - ImGui.TableNextColumn(); - ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedCharacterTattooShpkCount}"); - ImGui.TableNextColumn(); - ImGui.TextUnformatted($"{slowPathCallDeltas.CharacterTattoo}"); + ImGui.TextUnformatted($"{slowPathCallDeltas.CharacterLegacy}"); ImGui.TableNextColumn(); ImGui.TextUnformatted("characterocclusion.shpk"); @@ -875,12 +854,47 @@ public class DebugTab : Window, ITab, IUiService ImGui.TableNextColumn(); ImGui.TextUnformatted($"{slowPathCallDeltas.CharacterOcclusion}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted("characterstockings.shpk"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedCharacterStockingsShpkCount}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{slowPathCallDeltas.CharacterStockings}"); + + ImGui.TableNextColumn(); + ImGui.TextUnformatted("charactertattoo.shpk"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedCharacterTattooShpkCount}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{slowPathCallDeltas.CharacterTattoo}"); + + ImGui.TableNextColumn(); + ImGui.TextUnformatted("charactertransparency.shpk"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedCharacterTransparencyShpkCount}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{slowPathCallDeltas.CharacterTransparency}"); + ImGui.TableNextColumn(); ImGui.TextUnformatted("hairmask.shpk"); ImGui.TableNextColumn(); ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedHairMaskShpkCount}"); ImGui.TableNextColumn(); ImGui.TextUnformatted($"{slowPathCallDeltas.HairMask}"); + + ImGui.TableNextColumn(); + ImGui.TextUnformatted("iris.shpk"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedIrisShpkCount}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{slowPathCallDeltas.Iris}"); + + ImGui.TableNextColumn(); + ImGui.TextUnformatted("skin.shpk"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{_shaderReplacementFixer.ModdedSkinShpkCount}"); + ImGui.TableNextColumn(); + ImGui.TextUnformatted($"{slowPathCallDeltas.Skin}"); } /// Draw information about the resident resource files. From 03e9dc55dfebc6de1fb3290fdd6b041f547a08e2 Mon Sep 17 00:00:00 2001 From: Exter-N Date: Thu, 8 Aug 2024 23:19:44 +0200 Subject: [PATCH 412/433] Use read-only MMIO for legacy ShPk ban --- Penumbra/Interop/Processing/ShpkPathPreProcessor.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Penumbra/Interop/Processing/ShpkPathPreProcessor.cs b/Penumbra/Interop/Processing/ShpkPathPreProcessor.cs index 96d9daff..2fb35ae0 100644 --- a/Penumbra/Interop/Processing/ShpkPathPreProcessor.cs +++ b/Penumbra/Interop/Processing/ShpkPathPreProcessor.cs @@ -1,3 +1,4 @@ +using System.IO.MemoryMappedFiles; using FFXIVClientStructs.FFXIV.Client.System.Resource; using Penumbra.Api.Enums; using Penumbra.Collections; @@ -52,7 +53,7 @@ public sealed class ShpkPathPreProcessor(ResourceManagerService resourceManager, { try { - using var file = MmioMemoryManager.CreateFromFile(path); + using var file = MmioMemoryManager.CreateFromFile(path, access: MemoryMappedFileAccess.Read); var bytes = file.GetSpan(); return ShpkFile.FastIsLegacy(bytes) From c265b917b4e56ea359a1337a06cb9cf5da155cce Mon Sep 17 00:00:00 2001 From: Exter-N Date: Fri, 9 Aug 2024 00:02:36 +0200 Subject: [PATCH 413/433] "This is how you end up on a list." --- Penumbra/Penumbra.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index 438cdc49..d99e3fcd 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -186,6 +186,7 @@ public class Penumbra : IDalamudPlugin ReadOnlySpan relevantPlugins = [ "Glamourer", "MareSynchronos", "CustomizePlus", "SimpleHeels", "VfxEditor", "heliosphere-plugin", "Ktisis", "Brio", "DynamicBridge", + "IllusioVitae", ]; var plugins = _services.GetService().InstalledPlugins .GroupBy(p => p.InternalName) From a52a43bd86cfb193204ac3bcf8d94dd8e2bf3fc6 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 9 Aug 2024 01:15:05 +0200 Subject: [PATCH 414/433] Minor cleanup. --- .../PostProcessing/HumanSetupScalingHook.cs | 7 +-- .../PostProcessing/PreBoneDeformerReplacer.cs | 23 +++---- .../PostProcessing/ShaderReplacementFixer.cs | 61 +++++++------------ Penumbra/Interop/Services/CharacterUtility.cs | 2 +- 4 files changed, 38 insertions(+), 55 deletions(-) diff --git a/Penumbra/Interop/Hooks/PostProcessing/HumanSetupScalingHook.cs b/Penumbra/Interop/Hooks/PostProcessing/HumanSetupScalingHook.cs index 5783c099..870229d6 100644 --- a/Penumbra/Interop/Hooks/PostProcessing/HumanSetupScalingHook.cs +++ b/Penumbra/Interop/Hooks/PostProcessing/HumanSetupScalingHook.cs @@ -11,10 +11,8 @@ public unsafe class HumanSetupScalingHook : FastHook("Human.SetupScaling", vTables.HumanVTable[58], Detour, + => Task = hooks.CreateHook("Human.SetupScaling", vTables.HumanVTable[58], Detour, !HookOverrides.Instance.PostProcessing.HumanSetupScaling); - } private void Detour(CharacterBase* drawObject, uint slotIndex) { @@ -32,6 +30,7 @@ public unsafe class HumanSetupScalingHook : FastHook replacements, ref int numReplacements, ref IDisposable? pbdDisposable, ref object? shpkLock); - + public readonly record struct Replacement(nint AddressToReplace, nint ValueToSet, nint ValueToRestore); } diff --git a/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs b/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs index 9273a2cb..30e643c7 100644 --- a/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs +++ b/Penumbra/Interop/Hooks/PostProcessing/PreBoneDeformerReplacer.cs @@ -19,7 +19,7 @@ public sealed unsafe class PreBoneDeformerReplacer : IDisposable, IRequiredServi Utf8GamePath.FromSpan("chara/xls/boneDeformer/human.pbd"u8, MetaDataComputation.All, out var p) ? p : Utf8GamePath.Empty; // Approximate name guess. - private delegate void* CharacterBaseCreateDeformerDelegate(CharacterBase* drawObject, uint slotIndex); + private delegate void* CharacterBaseCreateDeformerDelegate(CharacterBase* drawObject, uint slotIndex); private readonly Hook _humanCreateDeformerHook; @@ -32,12 +32,12 @@ public sealed unsafe class PreBoneDeformerReplacer : IDisposable, IRequiredServi public PreBoneDeformerReplacer(CharacterUtility utility, CollectionResolver collectionResolver, ResourceLoader resourceLoader, HookManager hooks, IFramework framework, CharacterBaseVTables vTables, HumanSetupScalingHook humanSetupScalingHook) { - _utility = utility; - _collectionResolver = collectionResolver; - _resourceLoader = resourceLoader; - _framework = framework; - _humanSetupScalingHook = humanSetupScalingHook; - _humanSetupScalingHook.SetupReplacements += SetupHSSReplacements; + _utility = utility; + _collectionResolver = collectionResolver; + _resourceLoader = resourceLoader; + _framework = framework; + _humanSetupScalingHook = humanSetupScalingHook; + _humanSetupScalingHook.SetupReplacements += SetupHssReplacements; _humanCreateDeformerHook = hooks.CreateHook("HumanCreateDeformer", vTables.HumanVTable[101], CreateDeformer, !HookOverrides.Instance.PostProcessing.HumanCreateDeformer).Result; } @@ -45,7 +45,7 @@ public sealed unsafe class PreBoneDeformerReplacer : IDisposable, IRequiredServi public void Dispose() { _humanCreateDeformerHook.Dispose(); - _humanSetupScalingHook.SetupReplacements -= SetupHSSReplacements; + _humanSetupScalingHook.SetupReplacements -= SetupHssReplacements; } private SafeResourceHandle GetPreBoneDeformerForCharacter(CharacterBase* drawObject) @@ -57,18 +57,19 @@ public sealed unsafe class PreBoneDeformerReplacer : IDisposable, IRequiredServi return cache.CustomResources.Get(ResourceCategory.Chara, ResourceType.Pbd, PreBoneDeformerPath, resolveData); } - private void SetupHSSReplacements(CharacterBase* drawObject, uint slotIndex, Span replacements, + private void SetupHssReplacements(CharacterBase* drawObject, uint slotIndex, Span replacements, ref int numReplacements, ref IDisposable? pbdDisposable, ref object? shpkLock) { if (!_framework.IsInFrameworkUpdateThread) Penumbra.Log.Warning( - $"{nameof(PreBoneDeformerReplacer)}.{nameof(SetupHSSReplacements)}(0x{(nint)drawObject:X}, {slotIndex}) called out of framework thread"); + $"{nameof(PreBoneDeformerReplacer)}.{nameof(SetupHssReplacements)}(0x{(nint)drawObject:X}, {slotIndex}) called out of framework thread"); var preBoneDeformer = GetPreBoneDeformerForCharacter(drawObject); try { pbdDisposable = preBoneDeformer; - replacements[numReplacements++] = new((nint)(&_utility.Address->HumanPbdResource), (nint)preBoneDeformer.ResourceHandle, + replacements[numReplacements++] = new HumanSetupScalingHook.Replacement((nint)(&_utility.Address->HumanPbdResource), + (nint)preBoneDeformer.ResourceHandle, _utility.DefaultHumanPbdResource); } catch diff --git a/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs b/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs index 20db7e25..80892b0f 100644 --- a/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs +++ b/Penumbra/Interop/Hooks/PostProcessing/ShaderReplacementFixer.cs @@ -8,7 +8,6 @@ using OtterGui.Services; using Penumbra.Communication; using Penumbra.GameData; using Penumbra.Interop.Hooks.Resources; -using Penumbra.Interop.Services; using Penumbra.Interop.Structs; using Penumbra.Services; using CharacterUtility = Penumbra.Interop.Services.CharacterUtility; @@ -137,7 +136,7 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic _hairMaskState = new ModdedShaderPackageState(() => _modelRenderer.HairMaskShaderPackage, () => _modelRenderer.DefaultHairMaskShaderPackage); - _humanSetupScalingHook.SetupReplacements += SetupHSSReplacements; + _humanSetupScalingHook.SetupReplacements += SetupHssReplacements; _humanOnRenderMaterialHook = hooks.CreateHook("Human.OnRenderMaterial", vTables.HumanVTable[64], OnRenderHumanMaterial, !HookOverrides.Instance.PostProcessing.HumanOnRenderMaterial).Result; _modelRendererOnRenderMaterialHook = hooks.CreateHook("ModelRenderer.OnRenderMaterial", @@ -146,7 +145,8 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic _modelRendererUnkFuncHook = hooks.CreateHook("ModelRenderer.UnkFunc", Sigs.ModelRendererUnkFunc, ModelRendererUnkFuncDetour, !HookOverrides.Instance.PostProcessing.ModelRendererUnkFunc).Result; - _prepareColorTableHook = hooks.CreateHook("MaterialResourceHandle.PrepareColorTable", + _prepareColorTableHook = hooks.CreateHook( + "MaterialResourceHandle.PrepareColorTable", Sigs.PrepareColorSet, PrepareColorTableDetour, !HookOverrides.Instance.PostProcessing.PrepareColorTable).Result; @@ -160,7 +160,7 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic _modelRendererUnkFuncHook.Dispose(); _modelRendererOnRenderMaterialHook.Dispose(); _humanOnRenderMaterialHook.Dispose(); - _humanSetupScalingHook.SetupReplacements -= SetupHSSReplacements; + _humanSetupScalingHook.SetupReplacements -= SetupHssReplacements; _communicator.MtrlLoaded.Unsubscribe(OnMtrlLoaded); _resourceHandleDestructor.Unsubscribe(OnResourceHandleDestructor); @@ -188,14 +188,6 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic _characterOcclusionState.GetAndResetSlowPathCallDelta(), _hairMaskState.GetAndResetSlowPathCallDelta()); - private static bool IsMaterialWithShpk(MaterialResourceHandle* mtrlResource, ReadOnlySpan shpkName) - { - if (mtrlResource == null) - return false; - - return shpkName.SequenceEqual(mtrlResource->ShpkNameSpan); - } - private void OnMtrlLoaded(nint mtrlResourceHandle, nint gameObject) { var mtrl = (MaterialResourceHandle*)mtrlResourceHandle; @@ -203,9 +195,11 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic if (shpk == null) return; - var shpkName = mtrl->ShpkNameSpan; - var shpkState = GetStateForHumanSetup(shpkName) ?? GetStateForHumanRender(shpkName) ?? GetStateForModelRendererRender(shpkName) - ?? GetStateForModelRendererUnk(shpkName) ?? GetStateForColorTable(shpkName); + var shpkName = mtrl->ShpkNameSpan; + var shpkState = GetStateForHumanSetup(shpkName) + ?? GetStateForHumanRender(shpkName) + ?? GetStateForModelRendererRender(shpkName) + ?? GetStateForModelRendererUnk(shpkName) ?? GetStateForColorTable(shpkName); if (shpkState != null && shpk != shpkState.DefaultShaderPackage) shpkState.TryAddMaterial(mtrlResourceHandle); @@ -228,12 +222,7 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic => mtrlResource == null ? null : GetStateForHumanSetup(mtrlResource->ShpkNameSpan); private ModdedShaderPackageState? GetStateForHumanSetup(ReadOnlySpan shpkName) - { - if (CharacterStockingsShpkName.SequenceEqual(shpkName)) - return _characterStockingsState; - - return null; - } + => CharacterStockingsShpkName.SequenceEqual(shpkName) ? _characterStockingsState : null; [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private uint GetTotalMaterialCountForHumanSetup() @@ -243,12 +232,7 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic => mtrlResource == null ? null : GetStateForHumanRender(mtrlResource->ShpkNameSpan); private ModdedShaderPackageState? GetStateForHumanRender(ReadOnlySpan shpkName) - { - if (SkinShpkName.SequenceEqual(shpkName)) - return _skinState; - - return null; - } + => SkinShpkName.SequenceEqual(shpkName) ? _skinState : null; [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private uint GetTotalMaterialCountForHumanRender() @@ -305,18 +289,13 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic + _characterStockingsState.MaterialCount; private ModdedShaderPackageState? GetStateForColorTable(ReadOnlySpan shpkName) - { - if (CharacterLegacyShpkName.SequenceEqual(shpkName)) - return _characterLegacyState; - - return null; - } + => CharacterLegacyShpkName.SequenceEqual(shpkName) ? _characterLegacyState : null; [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private uint GetTotalMaterialCountForColorTable() => _characterLegacyState.MaterialCount; - private void SetupHSSReplacements(CharacterBase* drawObject, uint slotIndex, Span replacements, + private void SetupHssReplacements(CharacterBase* drawObject, uint slotIndex, Span replacements, ref int numReplacements, ref IDisposable? pbdDisposable, ref object? shpkLock) { // If we don't have any on-screen instances of modded characterstockings.shpk, we don't need the slow path at all. @@ -326,6 +305,7 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic var model = drawObject->Models[slotIndex]; if (model == null) return; + MaterialResourceHandle* mtrlResource = null; ModdedShaderPackageState? shpkState = null; foreach (var material in model->MaterialsSpan) @@ -340,6 +320,7 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic if (shpkState != null) break; } + if (shpkState == null || shpkState.MaterialCount == 0) return; @@ -348,7 +329,8 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic // This is less performance-critical than the others, as this is called by the game only on draw object creation and slot update. // There are still thread safety concerns as it might be called in other threads by plugins. shpkLock = shpkState; - replacements[numReplacements++] = new((nint)shpkState.ShaderPackageReference, (nint)mtrlResource->ShaderPackageResourceHandle, + replacements[numReplacements++] = new HumanSetupScalingHook.Replacement((nint)shpkState.ShaderPackageReference, + (nint)mtrlResource->ShaderPackageResourceHandle, (nint)shpkState.DefaultShaderPackage); } @@ -439,7 +421,7 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic // Same performance considerations as OnRenderHumanMaterial. lock (shpkState) { - var shpkReference = shpkState.ShaderPackageReference; + var shpkReference = shpkState.ShaderPackageReference; try { *shpkReference = mtrlResource->ShaderPackageResourceHandle; @@ -452,7 +434,7 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic } } - private MaterialResourceHandle* GetMaterialResourceHandle(ModelRendererStructs.UnkPayload* unkPayload) + private static MaterialResourceHandle* GetMaterialResourceHandle(ModelRendererStructs.UnkPayload* unkPayload) { // TODO ClientStructs-ify var unkPointer = *(nint*)((nint)unkPayload->ModelResourceHandle + 0xE8) + unkPayload->UnkIndex * 0x24; @@ -467,13 +449,14 @@ public sealed unsafe class ShaderReplacementFixer : IDisposable, IRequiredServic if (mtrlResource->ShaderPackageResourceHandle == null) { - Penumbra.Log.Warning($"ShaderReplacementFixer found a MaterialResourceHandle with no shader package"); + Penumbra.Log.Warning("ShaderReplacementFixer found a MaterialResourceHandle with no shader package"); return null; } if (mtrlResource->ShaderPackageResourceHandle->ShaderPackage != unkPayload->ShaderWrapper->ShaderPackage) { - Penumbra.Log.Warning($"ShaderReplacementFixer found a MaterialResourceHandle (0x{(nint)mtrlResource:X}) with an inconsistent shader package (got 0x{(nint)mtrlResource->ShaderPackageResourceHandle->ShaderPackage:X}, expected 0x{(nint)unkPayload->ShaderWrapper->ShaderPackage:X})"); + Penumbra.Log.Warning( + $"ShaderReplacementFixer found a MaterialResourceHandle (0x{(nint)mtrlResource:X}) with an inconsistent shader package (got 0x{(nint)mtrlResource->ShaderPackageResourceHandle->ShaderPackage:X}, expected 0x{(nint)unkPayload->ShaderWrapper->ShaderPackage:X})"); return null; } diff --git a/Penumbra/Interop/Services/CharacterUtility.cs b/Penumbra/Interop/Services/CharacterUtility.cs index 4ab156a9..1641e42d 100644 --- a/Penumbra/Interop/Services/CharacterUtility.cs +++ b/Penumbra/Interop/Services/CharacterUtility.cs @@ -67,7 +67,7 @@ public unsafe class CharacterUtility : IDisposable, IRequiredService _framework.Update += LoadDefaultResources; } - /// We store the default data of the resources so we can always restore them. + /// We store the default data of the resources, so we can always restore them. private void LoadDefaultResources(object _) { if (Address == null) From 465e65e8fec7a3a3c1a51174ee99955390c15506 Mon Sep 17 00:00:00 2001 From: Actions User Date: Thu, 8 Aug 2024 23:17:52 +0000 Subject: [PATCH 415/433] [CI] Updating repo.json for testing_1.2.0.23 --- repo.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo.json b/repo.json index 3de0c5c8..efb71542 100644 --- a/repo.json +++ b/repo.json @@ -6,7 +6,7 @@ "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.22", + "TestingAssemblyVersion": "1.2.0.23", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -19,7 +19,7 @@ "LoadRequiredState": 2, "LoadSync": true, "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.22/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.23/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } From e44b450548f42bac821d3c3746a868e976aec420 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 9 Aug 2024 22:17:23 +0200 Subject: [PATCH 416/433] Disable model import/export for now. --- Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs index de088736..490fa147 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.cs @@ -97,7 +97,9 @@ public partial class ModEditWindow private void DrawImportExport(MdlTab tab, bool disabled) { - if (!ImGui.CollapsingHeader("Import / Export")) + // TODO: Enable when functional. + using var dawntrailDisabled = ImRaii.Disabled(); + if (!ImGui.CollapsingHeader("Import / Export (currently disabled due to Dawntrail format changes)") || true) return; var childSize = new Vector2((ImGui.GetContentRegionAvail().X - ImGui.GetStyle().ItemSpacing.X) / 2, 0); From b5f7f03e11cc9dc26555667f3f90448413d20d3c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 9 Aug 2024 22:46:34 +0200 Subject: [PATCH 417/433] Update BNPC Names. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 2fd5aa44..bf020ebf 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 2fd5aa44056a906df90c9a826d1d17f6fdafebff +Subproject commit bf020ebf5e4980f1814b336aabbaba5e2e00c362 From 1b671b95ab2c95b4af554430746f18d82d50e806 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 9 Aug 2024 23:04:07 +0200 Subject: [PATCH 418/433] 1.2.1.0 --- Penumbra/UI/Changelog.cs | 63 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/Penumbra/UI/Changelog.cs b/Penumbra/UI/Changelog.cs index f4cedf7d..55ce70e4 100644 --- a/Penumbra/UI/Changelog.cs +++ b/Penumbra/UI/Changelog.cs @@ -52,18 +52,68 @@ public class PenumbraChangelog : IUiService AddDummy(Changelog); Add1_1_0_0(Changelog); Add1_1_1_0(Changelog); - } - + Add1_2_1_0(Changelog); + } + #region Changelogs + private static void Add1_2_1_0(Changelog log) + => log.NextVersion("Version 1.2.1.0") + .RegisterHighlight("Penumbra is now released for Dawntrail.") + .RegisterEntry("Mods themselves may have to be updated. TexTools provides options for this.", 1) + .RegisterEntry("For model files, Penumbra provides a rudimentary update function, but prefer using TexTools if possible.", 1) + .RegisterEntry("Other files, like materials and textures, will have to go through TexTools for the moment.", 1) + .RegisterImportant("I am sorry that it took this long, but there was an immense amount of work to be done from the start.") + .RegisterImportant( + "Since Penumbra has been in Testing for quite a while, multitudes of bugs and issues cropped up that needed to be dealt with.", + 1) + .RegisterEntry("There very well may still be a lot of issues, so please report any you find.", 1) + .RegisterImportant("BUT, please make sure that those issues are not caused by outdated mods before reporting them.", 1) + .RegisterEntry( + "This changelog may seem rather short for the timespan, but I omitted hundreds of smaller fixes and the details of getting Penumbra to work in Dawntrail.", + 1) + .RegisterHighlight("The Material Editing tab in the Advanced Editing Window has been heavily improved (by Ny).") + .RegisterEntry( + "Especially for Dawntrail materials using the new shaders, the window provides much more in-depth and user-friendly editing options.", + 1) + .RegisterHighlight("Many advancements regarding modded shaders, and modding bone deformers have been made.") + .RegisterHighlight("IMC groups now allow their options to toggle attributes off that are on in the default entry.") + .RegisterImportant( + "The 'Update Bibo' button was removed. The functionality is redundant since any mods that old need to be updated anyway.") + .RegisterEntry("Clicking the button on modern mods generally caused more harm than benefit.", 1) + .RegisterImportant("Model Import/Export is temporarily disabled until Dawntrail-related changes can be made.") + .RegisterEntry( + "If you somehow still need to mass-migrate materials in your models, the Material Reassignment tab in Advanced Editing is still available for this.", + 1) + .RegisterHighlight("You can now change a mods state in any collection from its Collections tab via right-clicking the state.") + .RegisterHighlight("Items changed in a mod now sort before other items in the Item Swap tab, and are highlighted.") + .RegisterEntry("Path handling was improved in regards to case-sensitivity.") + .RegisterEntry("Fixed an issue with negative search matching on folders with no matches") + .RegisterEntry("Mod option groups on the same priority are now applied in reverse index order. (1.2.0.12)") + .RegisterEntry("Fixed the display of missing files in the Advanced Editing Window's header. (1.2.0.8)") + .RegisterEntry( + "Fixed some, but not all soft-locks that occur when your character gets redrawn while fishing. Just do not do that. (1.2.0.7)") + .RegisterEntry("Improved handling of invalid Offhand IMC files for certain jobs. (1.2.0.6)") + .RegisterEntry("Added automatic reduplication for files in the UI category, as they cause crashes when not unique. (1.2.0.5)") + .RegisterEntry("The mod import popup can now be closed by clicking outside of it, if it is finished. (1.2.0.5)") + .RegisterEntry("Fixed an issue with Mod Normalization skipping the default option. (1.2.0.5)") + .RegisterEntry("Improved the Support Info output. (1.1.1.5)") + .RegisterEntry("Reworked the handling of Meta Manipulations entirely. (1.1.1.3)") + .RegisterEntry("Added a configuration option to disable showing mods in the character lobby and at the aesthetician. (1.1.1.1)") + .RegisterEntry("Fixed an issue with the AddMods API and the root directory. (1.1.1.2)") + .RegisterEntry("Fixed an issue with the Mod Merger file lookup and casing. (1.1.1.2)") + .RegisterEntry("Fixed an issue with file saving not happening when merging mods or swapping items in some cases. (1.1.1.2)"); + private static void Add1_1_1_0(Changelog log) => log.NextVersion("Version 1.1.1.0") .RegisterHighlight("Filtering for mods is now tokenized and can filter for multiple things at once, or exclude specific things.") .RegisterEntry("Hover over the filter to see the new available options in the tooltip.", 1) - .RegisterEntry("Be aware that the tokenization changed the prior behavior slightly.", 1) - .RegisterEntry("This is open to improvements, if you have any ideas, let me know!", 1) + .RegisterEntry("Be aware that the tokenization changed the prior behavior slightly.", 1) + .RegisterEntry("This is open to improvements, if you have any ideas, let me know!", 1) .RegisterHighlight("Added initial identification of characters in the login-screen by name.") - .RegisterEntry("Those characters can not be redrawn and re-use some things, so this may not always behave as expected, but should work in general. Let me know if you encounter edge cases!", 1) + .RegisterEntry( + "Those characters can not be redrawn and re-use some things, so this may not always behave as expected, but should work in general. Let me know if you encounter edge cases!", + 1) .RegisterEntry("Added functionality for IMC groups to apply to all variants for a model instead of a specific one.") .RegisterEntry("Improved the resource tree view with filters and incognito mode. (by Ny)") .RegisterEntry("Added a tooltip to the global EQP condition.") @@ -131,7 +181,8 @@ public class PenumbraChangelog : IUiService .RegisterEntry( "Made some improvements to the Advanced Editing window, for example a much better and more performant Hex Viewer for unstructured data was added.") .RegisterEntry("Various improvements to model import/export by ackwell (throughout all patches).") - .RegisterEntry("Hovering over meta manipulations in other options in the advanced editing window now shows a list of those options.") + .RegisterEntry( + "Hovering over meta manipulations in other options in the advanced editing window now shows a list of those options.") .RegisterEntry("Reworked the API and IPC structure heavily.") .RegisterImportant("This means some plugins interacting with Penumbra may not work correctly until they update.", 1) .RegisterEntry("Worked around the UI IPC possibly displacing all settings when the drawn additions became too big.") From 741141f22769fe8af8991545867d44082a65a466 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 9 Aug 2024 23:52:12 +0200 Subject: [PATCH 419/433] Woops. --- Penumbra/UI/Changelog.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Penumbra/UI/Changelog.cs b/Penumbra/UI/Changelog.cs index 55ce70e4..5bf4f59d 100644 --- a/Penumbra/UI/Changelog.cs +++ b/Penumbra/UI/Changelog.cs @@ -59,7 +59,7 @@ public class PenumbraChangelog : IUiService private static void Add1_2_1_0(Changelog log) => log.NextVersion("Version 1.2.1.0") - .RegisterHighlight("Penumbra is now released for Dawntrail.") + .RegisterHighlight("Penumbra is now released for Dawntrail!") .RegisterEntry("Mods themselves may have to be updated. TexTools provides options for this.", 1) .RegisterEntry("For model files, Penumbra provides a rudimentary update function, but prefer using TexTools if possible.", 1) .RegisterEntry("Other files, like materials and textures, will have to go through TexTools for the moment.", 1) @@ -81,10 +81,10 @@ public class PenumbraChangelog : IUiService .RegisterImportant( "The 'Update Bibo' button was removed. The functionality is redundant since any mods that old need to be updated anyway.") .RegisterEntry("Clicking the button on modern mods generally caused more harm than benefit.", 1) - .RegisterImportant("Model Import/Export is temporarily disabled until Dawntrail-related changes can be made.") .RegisterEntry( "If you somehow still need to mass-migrate materials in your models, the Material Reassignment tab in Advanced Editing is still available for this.", 1) + .RegisterImportant("Model Import/Export is temporarily disabled until Dawntrail-related changes can be made.") .RegisterHighlight("You can now change a mods state in any collection from its Collections tab via right-clicking the state.") .RegisterHighlight("Items changed in a mod now sort before other items in the Item Swap tab, and are highlighted.") .RegisterEntry("Path handling was improved in regards to case-sensitivity.") From 421fde70b08db075c359bd699a079bf64e8297e9 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 9 Aug 2024 23:57:42 +0200 Subject: [PATCH 420/433] Addendum --- Penumbra/UI/Changelog.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Penumbra/UI/Changelog.cs b/Penumbra/UI/Changelog.cs index 5bf4f59d..41920d1c 100644 --- a/Penumbra/UI/Changelog.cs +++ b/Penumbra/UI/Changelog.cs @@ -63,6 +63,8 @@ public class PenumbraChangelog : IUiService .RegisterEntry("Mods themselves may have to be updated. TexTools provides options for this.", 1) .RegisterEntry("For model files, Penumbra provides a rudimentary update function, but prefer using TexTools if possible.", 1) .RegisterEntry("Other files, like materials and textures, will have to go through TexTools for the moment.", 1) + .RegisterEntry( + "Some outdated mods can be identified by Penumbra and are prevented from loading entirely (specifically shaders, by Ny).", 1) .RegisterImportant("I am sorry that it took this long, but there was an immense amount of work to be done from the start.") .RegisterImportant( "Since Penumbra has been in Testing for quite a while, multitudes of bugs and issues cropped up that needed to be dealt with.", @@ -84,6 +86,7 @@ public class PenumbraChangelog : IUiService .RegisterEntry( "If you somehow still need to mass-migrate materials in your models, the Material Reassignment tab in Advanced Editing is still available for this.", 1) + .RegisterEntry("The On-Screen tab was updated and improved and can now display modded actual paths in more useful form.") .RegisterImportant("Model Import/Export is temporarily disabled until Dawntrail-related changes can be made.") .RegisterHighlight("You can now change a mods state in any collection from its Collections tab via right-clicking the state.") .RegisterHighlight("Items changed in a mod now sort before other items in the Item Swap tab, and are highlighted.") From 7ba7a6e31915ba84c05fd41ffc46b486b67e9caa Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 10 Aug 2024 11:55:30 +0200 Subject: [PATCH 421/433] API 5.3 --- Penumbra.Api | 2 +- Penumbra/Api/Api/PenumbraApi.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Penumbra.Api b/Penumbra.Api index 759a8e9d..552246e5 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit 759a8e9dc50b3453cdb7c3cba76de7174c94aba0 +Subproject commit 552246e595ffab2aaba2c75f578d564f8938fc9a diff --git a/Penumbra/Api/Api/PenumbraApi.cs b/Penumbra/Api/Api/PenumbraApi.cs index 0400c694..eaaf9f38 100644 --- a/Penumbra/Api/Api/PenumbraApi.cs +++ b/Penumbra/Api/Api/PenumbraApi.cs @@ -22,7 +22,7 @@ public class PenumbraApi( } public (int Breaking, int Feature) ApiVersion - => (5, 1); + => (5, 3); public bool Valid { get; private set; } = true; public IPenumbraApiCollection Collection { get; } = collection; From ccce087b8706b95997f2c97e8df7c8ee3d9ff44c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 10 Aug 2024 11:59:18 +0200 Subject: [PATCH 422/433] Update GameData. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index bf020ebf..3a65ed1c 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit bf020ebf5e4980f1814b336aabbaba5e2e00c362 +Subproject commit 3a65ed1c86a2d5fd5794ff5c0559b02fc25d7224 From 5c9e158da36bbae9fb1299da16ef918b1aef1417 Mon Sep 17 00:00:00 2001 From: Actions User Date: Sat, 10 Aug 2024 10:01:17 +0000 Subject: [PATCH 423/433] [CI] Updating repo.json for 1.2.1.0 --- repo.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/repo.json b/repo.json index efb71542..0e5c7799 100644 --- a/repo.json +++ b/repo.json @@ -5,8 +5,8 @@ "Punchline": "Runtime mod loader and manager.", "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", - "AssemblyVersion": "1.1.1.2", - "TestingAssemblyVersion": "1.2.0.23", + "AssemblyVersion": "1.2.1.0", + "TestingAssemblyVersion": "1.2.1.0", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 9, @@ -18,9 +18,9 @@ "LoadPriority": 69420, "LoadRequiredState": 2, "LoadSync": true, - "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/testing_1.2.0.23/Penumbra.zip", - "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.1.1.2/Penumbra.zip", + "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.2.1.0/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.2.1.0/Penumbra.zip", + "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.2.1.0/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } ] From a27ce6b0a78243708c1134fcc945a09f05796351 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 10 Aug 2024 12:23:17 +0200 Subject: [PATCH 424/433] Update non-testing DalamudApiLevel. --- Penumbra.sln | 3 ++- repo.json | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Penumbra.sln b/Penumbra.sln index 78fa1543..46609f85 100644 --- a/Penumbra.sln +++ b/Penumbra.sln @@ -8,6 +8,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F89C9EAE-25C8-43BE-8108-5921E5A93502}" ProjectSection(SolutionItems) = preProject .editorconfig = .editorconfig + repo.json = repo.json EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Penumbra.GameData", "Penumbra.GameData\Penumbra.GameData.csproj", "{EE551E87-FDB3-4612-B500-DC870C07C605}" @@ -18,7 +19,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Penumbra.Api", "Penumbra.Ap EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Penumbra.String", "Penumbra.String\Penumbra.String.csproj", "{5549BAFD-6357-4B1A-800C-75AC36E5B76D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Penumbra.CrashHandler", "Penumbra.CrashHandler\Penumbra.CrashHandler.csproj", "{EE834491-A98F-4395-BE0D-6861AE5AD953}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Penumbra.CrashHandler", "Penumbra.CrashHandler\Penumbra.CrashHandler.csproj", "{EE834491-A98F-4395-BE0D-6861AE5AD953}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/repo.json b/repo.json index 0e5c7799..73ae9eff 100644 --- a/repo.json +++ b/repo.json @@ -9,7 +9,7 @@ "TestingAssemblyVersion": "1.2.1.0", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", - "DalamudApiLevel": 9, + "DalamudApiLevel": 10, "TestingDalamudApiLevel": 10, "IsHide": "False", "IsTestingExclusive": "False", From 7710d9249675e6550f9db2eaaf94e1c570929c23 Mon Sep 17 00:00:00 2001 From: "N. Lo." Date: Sat, 10 Aug 2024 18:59:02 +0200 Subject: [PATCH 425/433] Add another plugin to Copy Support Info --- Penumbra/Penumbra.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index d99e3fcd..6f0b63ce 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -186,7 +186,7 @@ public class Penumbra : IDalamudPlugin ReadOnlySpan relevantPlugins = [ "Glamourer", "MareSynchronos", "CustomizePlus", "SimpleHeels", "VfxEditor", "heliosphere-plugin", "Ktisis", "Brio", "DynamicBridge", - "IllusioVitae", + "IllusioVitae", "Aetherment", ]; var plugins = _services.GetService().InstalledPlugins .GroupBy(p => p.InternalName) From 6b0b1629bd2e828d6f237fe64f84c84d6066534c Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 11 Aug 2024 23:22:05 +0200 Subject: [PATCH 426/433] Move GuidExtensions to OtterGui (unused atm) --- OtterGui | 2 +- Penumbra/GuidExtensions.cs | 254 ------------------------------------- 2 files changed, 1 insertion(+), 255 deletions(-) delete mode 100644 Penumbra/GuidExtensions.cs diff --git a/OtterGui b/OtterGui index d9486ae5..07a00913 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit d9486ae54b5a4b61cf74f79ed27daa659eb1ce5b +Subproject commit 07a009134bf5eb7da9a54ba40e82c88fc613544a diff --git a/Penumbra/GuidExtensions.cs b/Penumbra/GuidExtensions.cs deleted file mode 100644 index fcbc8a3b..00000000 --- a/Penumbra/GuidExtensions.cs +++ /dev/null @@ -1,254 +0,0 @@ -using System.Collections.Frozen; -using OtterGui; - -namespace Penumbra; - -public static class GuidExtensions -{ - private const string Chars = - "0123456789" - + "abcdefghij" - + "klmnopqrst" - + "uv"; - - private static ReadOnlySpan Bytes - => "0123456789abcdefghijklmnopqrstuv"u8; - - private static readonly FrozenDictionary - ReverseChars = Chars.WithIndex().ToFrozenDictionary(t => t.Value, t => (byte)t.Index); - - private static readonly FrozenDictionary ReverseBytes = - ReverseChars.ToFrozenDictionary(kvp => (byte)kvp.Key, kvp => kvp.Value); - - public static unsafe string OptimizedString(this Guid guid) - { - var bytes = stackalloc ulong[2]; - if (!guid.TryWriteBytes(new Span(bytes, 16))) - return guid.ToString("N"); - - var u1 = bytes[0]; - var u2 = bytes[1]; - Span text = - [ - Chars[(int)(u1 & 0x1F)], - Chars[(int)((u1 >> 5) & 0x1F)], - Chars[(int)((u1 >> 10) & 0x1F)], - Chars[(int)((u1 >> 15) & 0x1F)], - Chars[(int)((u1 >> 20) & 0x1F)], - Chars[(int)((u1 >> 25) & 0x1F)], - Chars[(int)((u1 >> 30) & 0x1F)], - Chars[(int)((u1 >> 35) & 0x1F)], - Chars[(int)((u1 >> 40) & 0x1F)], - Chars[(int)((u1 >> 45) & 0x1F)], - Chars[(int)((u1 >> 50) & 0x1F)], - Chars[(int)((u1 >> 55) & 0x1F)], - Chars[(int)((u1 >> 60) | ((u2 & 0x01) << 4))], - Chars[(int)((u2 >> 1) & 0x1F)], - Chars[(int)((u2 >> 6) & 0x1F)], - Chars[(int)((u2 >> 11) & 0x1F)], - Chars[(int)((u2 >> 16) & 0x1F)], - Chars[(int)((u2 >> 21) & 0x1F)], - Chars[(int)((u2 >> 26) & 0x1F)], - Chars[(int)((u2 >> 31) & 0x1F)], - Chars[(int)((u2 >> 36) & 0x1F)], - Chars[(int)((u2 >> 41) & 0x1F)], - Chars[(int)((u2 >> 46) & 0x1F)], - Chars[(int)((u2 >> 51) & 0x1F)], - Chars[(int)((u2 >> 56) & 0x1F)], - Chars[(int)((u2 >> 61) & 0x1F)], - ]; - return new string(text); - } - - public static unsafe bool FromOptimizedString(ReadOnlySpan text, out Guid guid) - { - if (text.Length != 26) - return Return(out guid); - - var bytes = stackalloc ulong[2]; - if (!ReverseChars.TryGetValue(text[0], out var b0)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[1], out var b1)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[2], out var b2)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[3], out var b3)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[4], out var b4)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[5], out var b5)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[6], out var b6)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[7], out var b7)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[8], out var b8)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[9], out var b9)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[10], out var b10)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[11], out var b11)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[12], out var b12)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[13], out var b13)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[14], out var b14)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[15], out var b15)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[16], out var b16)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[17], out var b17)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[18], out var b18)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[19], out var b19)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[20], out var b20)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[21], out var b21)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[22], out var b22)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[23], out var b23)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[24], out var b24)) - return Return(out guid); - if (!ReverseChars.TryGetValue(text[25], out var b25)) - return Return(out guid); - - bytes[0] = b0 - | ((ulong)b1 << 5) - | ((ulong)b2 << 10) - | ((ulong)b3 << 15) - | ((ulong)b4 << 20) - | ((ulong)b5 << 25) - | ((ulong)b6 << 30) - | ((ulong)b7 << 35) - | ((ulong)b8 << 40) - | ((ulong)b9 << 45) - | ((ulong)b10 << 50) - | ((ulong)b11 << 55) - | ((ulong)b12 << 60); - bytes[1] = ((ulong)b12 >> 4) - | ((ulong)b13 << 1) - | ((ulong)b14 << 6) - | ((ulong)b15 << 11) - | ((ulong)b16 << 16) - | ((ulong)b17 << 21) - | ((ulong)b18 << 26) - | ((ulong)b19 << 31) - | ((ulong)b20 << 36) - | ((ulong)b21 << 41) - | ((ulong)b22 << 46) - | ((ulong)b23 << 51) - | ((ulong)b24 << 56) - | ((ulong)b25 << 61); - guid = new Guid(new Span(bytes, 16)); - return true; - - static bool Return(out Guid guid) - { - guid = Guid.Empty; - return false; - } - } - - public static unsafe bool FromOptimizedString(ReadOnlySpan text, out Guid guid) - { - if (text.Length != 26) - return Return(out guid); - - var bytes = stackalloc ulong[2]; - if (!ReverseBytes.TryGetValue(text[0], out var b0)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[1], out var b1)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[2], out var b2)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[3], out var b3)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[4], out var b4)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[5], out var b5)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[6], out var b6)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[7], out var b7)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[8], out var b8)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[9], out var b9)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[10], out var b10)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[11], out var b11)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[12], out var b12)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[13], out var b13)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[14], out var b14)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[15], out var b15)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[16], out var b16)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[17], out var b17)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[18], out var b18)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[19], out var b19)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[20], out var b20)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[21], out var b21)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[22], out var b22)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[23], out var b23)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[24], out var b24)) - return Return(out guid); - if (!ReverseBytes.TryGetValue(text[25], out var b25)) - return Return(out guid); - - bytes[0] = b0 - | ((ulong)b1 << 5) - | ((ulong)b2 << 10) - | ((ulong)b3 << 15) - | ((ulong)b4 << 20) - | ((ulong)b5 << 25) - | ((ulong)b6 << 30) - | ((ulong)b7 << 35) - | ((ulong)b8 << 40) - | ((ulong)b9 << 45) - | ((ulong)b10 << 50) - | ((ulong)b11 << 55) - | ((ulong)b12 << 60); - bytes[1] = ((ulong)b12 >> 4) - | ((ulong)b13 << 1) - | ((ulong)b14 << 6) - | ((ulong)b15 << 11) - | ((ulong)b16 << 16) - | ((ulong)b17 << 21) - | ((ulong)b18 << 26) - | ((ulong)b19 << 31) - | ((ulong)b20 << 36) - | ((ulong)b21 << 41) - | ((ulong)b22 << 46) - | ((ulong)b23 << 51) - | ((ulong)b24 << 56) - | ((ulong)b25 << 61); - guid = new Guid(new Span(bytes, 16)); - return true; - - static bool Return(out Guid guid) - { - guid = Guid.Empty; - return false; - } - } -} From 47268ab377db99a91cfc14e7560e459bbb095171 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 11 Aug 2024 23:22:42 +0200 Subject: [PATCH 427/433] Prevent loading crashy shpks. --- Penumbra/Interop/PathResolving/PathResolver.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Penumbra/Interop/PathResolving/PathResolver.cs b/Penumbra/Interop/PathResolving/PathResolver.cs index 67ec4fc3..63bbc8d8 100644 --- a/Penumbra/Interop/PathResolving/PathResolver.cs +++ b/Penumbra/Interop/PathResolving/PathResolver.cs @@ -61,7 +61,7 @@ public class PathResolver : IDisposable, IService ResourceCategory.GameScript => (null, ResolveData.Invalid), // Use actual resolving. ResourceCategory.Chara => Resolve(path, resourceType), - ResourceCategory.Shader => Resolve(path, resourceType), + ResourceCategory.Shader => ResolveShader(path, resourceType), ResourceCategory.Vfx => Resolve(path, resourceType), ResourceCategory.Sound => Resolve(path, resourceType), // EXD Modding in general should probably be prohibited but is currently used for fan translations. @@ -83,6 +83,19 @@ public class PathResolver : IDisposable, IService }; } + /// Replacing the characterstockings.shpk or the characterocclusion.shpk files currently causes crashes, so we just entirely prevent that. + private (FullPath?, ResolveData) ResolveShader(Utf8GamePath gamePath, ResourceType type) + { + if (type is not ResourceType.Shpk) + return Resolve(gamePath, type); + + if (gamePath.Path.EndsWith("occlusion.shpk"u8) + || gamePath.Path.EndsWith("stockings.shpk"u8)) + return (null, ResolveData.Invalid); + + return Resolve(gamePath, type); + } + public (FullPath?, ResolveData) Resolve(Utf8GamePath gamePath, ResourceType type) { using var performance = _performance.Measure(PerformanceType.CharacterResolver); From 8ee326853d3e15c8b4e949dfdacfb0e5f5c1c817 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 11 Aug 2024 23:24:18 +0200 Subject: [PATCH 428/433] Update GameData. --- Penumbra.GameData | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Penumbra.GameData b/Penumbra.GameData index 3a65ed1c..b7fdfe9d 160000 --- a/Penumbra.GameData +++ b/Penumbra.GameData @@ -1 +1 @@ -Subproject commit 3a65ed1c86a2d5fd5794ff5c0559b02fc25d7224 +Subproject commit b7fdfe9d19f7e3229834480db446478b0bf6acee From 6e351aa68b5903ff0b691eed43ed1c1d72ae65ff Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 12 Aug 2024 20:57:06 +0200 Subject: [PATCH 429/433] Make mods added via API migrate models if enabled. --- Penumbra/Api/Api/ModsApi.cs | 11 ++++++++++- Penumbra/Services/MigrationManager.cs | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Penumbra/Api/Api/ModsApi.cs b/Penumbra/Api/Api/ModsApi.cs index 790121d5..2acdf031 100644 --- a/Penumbra/Api/Api/ModsApi.cs +++ b/Penumbra/Api/Api/ModsApi.cs @@ -15,15 +15,17 @@ public class ModsApi : IPenumbraApiMods, IApiService, IDisposable private readonly ModImportManager _modImportManager; private readonly Configuration _config; private readonly ModFileSystem _modFileSystem; + private readonly MigrationManager _migrationManager; public ModsApi(ModManager modManager, ModImportManager modImportManager, Configuration config, ModFileSystem modFileSystem, - CommunicatorService communicator) + CommunicatorService communicator, MigrationManager migrationManager) { _modManager = modManager; _modImportManager = modImportManager; _config = config; _modFileSystem = modFileSystem; _communicator = communicator; + _migrationManager = migrationManager; _communicator.ModPathChanged.Subscribe(OnModPathChanged, ModPathChanged.Priority.ApiMods); } @@ -81,9 +83,16 @@ public class ModsApi : IPenumbraApiMods, IApiService, IDisposable return ApiHelpers.Return(PenumbraApiEc.InvalidArgument, args); _modManager.AddMod(dir); + if (_config.MigrateImportedModelsToV6) + { + _migrationManager.MigrateMdlDirectory(dir.FullName, false); + _migrationManager.Await(); + } + if (_config.UseFileSystemCompression) new FileCompactor(Penumbra.Log).StartMassCompact(dir.EnumerateFiles("*.*", SearchOption.AllDirectories), CompressionAlgorithm.Xpress8K); + return ApiHelpers.Return(PenumbraApiEc.Success, args); } diff --git a/Penumbra/Services/MigrationManager.cs b/Penumbra/Services/MigrationManager.cs index 7726f6fd..9041fbd0 100644 --- a/Penumbra/Services/MigrationManager.cs +++ b/Penumbra/Services/MigrationManager.cs @@ -63,6 +63,9 @@ public class MigrationManager(Configuration config) : IService public void CleanMtrlBackups(string path) => CleanBackups(path, "*.mtrl.bak", "material", MtrlCleanup, TaskType.MtrlCleanup); + public void Await() + => _currentTask?.Wait(); + private void CleanBackups(string path, string extension, string fileType, MigrationData data, TaskType type) { if (IsRunning) From 3135d5e7e656cc37b6a60744f3cb63d50ca79b08 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 12 Aug 2024 20:57:38 +0200 Subject: [PATCH 430/433] Make collection combo mousewheel-scrollable with ctrl. --- Penumbra/UI/CollectionTab/CollectionCombo.cs | 23 +++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Penumbra/UI/CollectionTab/CollectionCombo.cs b/Penumbra/UI/CollectionTab/CollectionCombo.cs index 9d195eed..1670be5e 100644 --- a/Penumbra/UI/CollectionTab/CollectionCombo.cs +++ b/Penumbra/UI/CollectionTab/CollectionCombo.cs @@ -1,14 +1,15 @@ using ImGuiNET; +using OtterGui; using OtterGui.Raii; +using OtterGui.Text; using OtterGui.Widgets; using Penumbra.Collections; using Penumbra.Collections.Manager; -using Penumbra.GameData.Actors; namespace Penumbra.UI.CollectionTab; public sealed class CollectionCombo(CollectionManager manager, Func> items) - : FilterComboCache(items, MouseWheelType.None, Penumbra.Log) + : FilterComboCache(items, MouseWheelType.Control, Penumbra.Log) { private readonly ImRaii.Color _color = new(); @@ -20,14 +21,26 @@ public sealed class CollectionCombo(CollectionManager manager, Func obj.Name; + + protected override void DrawCombo(string label, string preview, string tooltip, int currentSelected, float previewWidth, float itemHeight, + ImGuiComboFlags flags) + { + base.DrawCombo(label, preview, tooltip, currentSelected, previewWidth, itemHeight, flags); + ImUtf8.HoverTooltip("Control and mouse wheel to scroll."u8); + } } From bb9dd184a369513bd872cdb2e5c866f0d1b96b33 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 12 Aug 2024 20:57:52 +0200 Subject: [PATCH 431/433] Fix order of gender and model in EQDP drawer. --- Penumbra/UI/AdvancedWindow/Meta/EqdpMetaDrawer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Penumbra/UI/AdvancedWindow/Meta/EqdpMetaDrawer.cs b/Penumbra/UI/AdvancedWindow/Meta/EqdpMetaDrawer.cs index 970b70cb..5206ece8 100644 --- a/Penumbra/UI/AdvancedWindow/Meta/EqdpMetaDrawer.cs +++ b/Penumbra/UI/AdvancedWindow/Meta/EqdpMetaDrawer.cs @@ -86,11 +86,11 @@ public sealed class EqdpMetaDrawer(ModMetaEditor editor, MetaFileManager metaFil ImUtf8.HoverTooltip("Model Set ID"u8); ImGui.TableNextColumn(); - ImUtf8.TextFramed(identifier.Gender.ToName(), FrameColor); + ImUtf8.TextFramed(identifier.Race.ToName(), FrameColor); ImUtf8.HoverTooltip("Model Race"u8); ImGui.TableNextColumn(); - ImUtf8.TextFramed(identifier.Race.ToName(), FrameColor); + ImUtf8.TextFramed(identifier.Gender.ToName(), FrameColor); ImUtf8.HoverTooltip("Gender"u8); ImGui.TableNextColumn(); From bedf5dab794b1ddbe4a2c5edcd60074d239963f4 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 12 Aug 2024 20:58:17 +0200 Subject: [PATCH 432/433] Make collection resolver not cache early resolved actors that aren't characters. --- Penumbra/Interop/PathResolving/CollectionResolver.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Penumbra/Interop/PathResolving/CollectionResolver.cs b/Penumbra/Interop/PathResolving/CollectionResolver.cs index 136da0f5..313c4f8b 100644 --- a/Penumbra/Interop/PathResolving/CollectionResolver.cs +++ b/Penumbra/Interop/PathResolving/CollectionResolver.cs @@ -216,10 +216,13 @@ public sealed unsafe class CollectionResolver( private ModCollection? CollectionByAttributes(Actor actor, ref bool notYetReady) { if (!actor.IsCharacter) + { + Penumbra.Log.Excessive($"Actor to be identified was not yet a Character."); + notYetReady = true; return null; + } // Only handle human models. - if (!IsModelHuman((uint)actor.AsCharacter->CharacterData.ModelCharaId)) return null; From 1da095be9946e9fa2879ef8b61d201f25b3086d6 Mon Sep 17 00:00:00 2001 From: Actions User Date: Mon, 12 Aug 2024 19:00:43 +0000 Subject: [PATCH 433/433] [CI] Updating repo.json for 1.2.1.1 --- repo.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/repo.json b/repo.json index 73ae9eff..5a274d73 100644 --- a/repo.json +++ b/repo.json @@ -5,8 +5,8 @@ "Punchline": "Runtime mod loader and manager.", "Description": "Runtime mod loader and manager.", "InternalName": "Penumbra", - "AssemblyVersion": "1.2.1.0", - "TestingAssemblyVersion": "1.2.1.0", + "AssemblyVersion": "1.2.1.1", + "TestingAssemblyVersion": "1.2.1.1", "RepoUrl": "https://github.com/xivdev/Penumbra", "ApplicableVersion": "any", "DalamudApiLevel": 10, @@ -18,9 +18,9 @@ "LoadPriority": 69420, "LoadRequiredState": 2, "LoadSync": true, - "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.2.1.0/Penumbra.zip", - "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.2.1.0/Penumbra.zip", - "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.2.1.0/Penumbra.zip", + "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.2.1.1/Penumbra.zip", + "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.2.1.1/Penumbra.zip", + "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.2.1.1/Penumbra.zip", "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" } ]