From 3eb3be05e7e52ea5462bf969fc88906541e1789b Mon Sep 17 00:00:00 2001 From: Alexander Heidn <40741234+HerrKnarz@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:01:45 +0200 Subject: [PATCH] Fixed bug that caused merge rules not being executed on metadata update, when the list of unwanted items is empty. Since unwanted are removed before the merge rules and an empty list resulted in false, the merge rules never triggered. --- .../Actions/AfterMetadataUpdateAction.cs | 29 +++++++---- .../ExecuteConditionalActionsAction.cs | 3 ++ .../MetadataUtilities/Actions/MergeAction.cs | 48 ++++++++++++------- .../MetadataUtilities/MetadataFunctions.cs | 19 ++++---- .../Properties/AssemblyInfo.cs | 4 +- Generic/MetadataUtilities/extension.yaml | 2 +- 6 files changed, 67 insertions(+), 38 deletions(-) diff --git a/Generic/MetadataUtilities/Actions/AfterMetadataUpdateAction.cs b/Generic/MetadataUtilities/Actions/AfterMetadataUpdateAction.cs index 7e9d0ee9f..7c488072e 100644 --- a/Generic/MetadataUtilities/Actions/AfterMetadataUpdateAction.cs +++ b/Generic/MetadataUtilities/Actions/AfterMetadataUpdateAction.cs @@ -8,6 +8,9 @@ namespace MetadataUtilities.Actions public class AfterMetadataUpdateAction : BaseAction { private static AfterMetadataUpdateAction _instance; + private bool _haveToExecActions; + private bool _haveToMerge; + private bool _haveToRemove; private AfterMetadataUpdateAction(Settings settings) : base(settings) { @@ -36,17 +39,17 @@ public override bool Execute(MyGame game, ActionModifierType actionModifier = Ac var result = false; - if (Settings.RemoveUnwantedOnMetadataUpdate && game.ExecuteRemoveUnwanted) + if (Settings.RemoveUnwantedOnMetadataUpdate && game.ExecuteRemoveUnwanted && _haveToRemove) { result |= RemoveUnwantedAction.Instance(Settings).Execute(game, ActionModifierType.IsCombi, item, isBulkAction); } - if (Settings.MergeMetadataOnMetadataUpdate && game.ExecuteMergeRules) + if (Settings.MergeMetadataOnMetadataUpdate && game.ExecuteMergeRules && _haveToMerge) { result |= MergeAction.Instance(Settings).Execute(game, ActionModifierType.IsCombi, item, isBulkAction); } - if (game.ExecuteConditionalActions) + if (game.ExecuteConditionalActions && _haveToExecActions) { result |= ExecuteConditionalActionsAction.Instance(Settings).Execute(game, ActionModifierType.IsCombi, item, isBulkAction); } @@ -61,17 +64,20 @@ public override bool Execute(MyGame game, ActionModifierType actionModifier = Ac public override void FollowUp(ActionModifierType actionModifier = ActionModifierType.None, object item = null, bool isBulkAction = true) { - if (Settings.RemoveUnwantedOnMetadataUpdate) + if (Settings.RemoveUnwantedOnMetadataUpdate && _haveToRemove) { RemoveUnwantedAction.Instance(Settings).FollowUp(ActionModifierType.IsCombi, item, isBulkAction); } - if (Settings.MergeMetadataOnMetadataUpdate) + if (Settings.MergeMetadataOnMetadataUpdate && _haveToMerge) { MergeAction.Instance(Settings).FollowUp(ActionModifierType.IsCombi, item, isBulkAction); } - ExecuteConditionalActionsAction.Instance(Settings).FollowUp(ActionModifierType.IsCombi, item, isBulkAction); + if (_haveToExecActions) + { + ExecuteConditionalActionsAction.Instance(Settings).FollowUp(ActionModifierType.IsCombi, item, isBulkAction); + } base.FollowUp(ActionModifierType.None, item, isBulkAction); } @@ -90,19 +96,22 @@ public override bool Prepare(ActionModifierType actionModifier = ActionModifierT return false; } - var result = true; + var result = false; if (Settings.RemoveUnwantedOnMetadataUpdate) { - result &= RemoveUnwantedAction.Instance(Settings).Prepare(ActionModifierType.IsCombi, item, isBulkAction); + _haveToRemove = RemoveUnwantedAction.Instance(Settings).Prepare(ActionModifierType.IsCombi, item, isBulkAction); + result |= _haveToRemove; } if (Settings.MergeMetadataOnMetadataUpdate) { - result &= MergeAction.Instance(Settings).Prepare(ActionModifierType.IsCombi, item, isBulkAction); + _haveToMerge = MergeAction.Instance(Settings).Prepare(ActionModifierType.IsCombi, item, isBulkAction); + result |= _haveToMerge; } - result &= ExecuteConditionalActionsAction.Instance(Settings).Prepare(ActionModifierType.IsCombi, item, isBulkAction); + _haveToExecActions = ExecuteConditionalActionsAction.Instance(Settings).Prepare(ActionModifierType.IsCombi, item, isBulkAction); + result |= _haveToExecActions; return result; } diff --git a/Generic/MetadataUtilities/Actions/ExecuteConditionalActionsAction.cs b/Generic/MetadataUtilities/Actions/ExecuteConditionalActionsAction.cs index 8743419e7..35052a726 100644 --- a/Generic/MetadataUtilities/Actions/ExecuteConditionalActionsAction.cs +++ b/Generic/MetadataUtilities/Actions/ExecuteConditionalActionsAction.cs @@ -40,5 +40,8 @@ public override bool Execute(MyGame game, ActionModifierType actionModifier = Ac return mustUpdate; } + + public override bool Prepare(ActionModifierType actionModifier = ActionModifierType.None, object item = null, + bool isBulkAction = true) => base.Prepare(actionModifier, item, isBulkAction) && Settings.ConditionalActions.Count > 0; } } \ No newline at end of file diff --git a/Generic/MetadataUtilities/Actions/MergeAction.cs b/Generic/MetadataUtilities/Actions/MergeAction.cs index b9e462b3f..92a082a40 100644 --- a/Generic/MetadataUtilities/Actions/MergeAction.cs +++ b/Generic/MetadataUtilities/Actions/MergeAction.cs @@ -2,6 +2,7 @@ using MetadataUtilities.Enums; using MetadataUtilities.Models; using Playnite.SDK; +using System; using System.Collections.Generic; using System.Linq; @@ -93,30 +94,43 @@ public override void FollowUp(ActionModifierType actionModifier = ActionModifier public override bool Prepare(ActionModifierType actionModifier = ActionModifierType.None, object item = null, bool isBulkAction = true) { - if (!base.Prepare(actionModifier, item, isBulkAction)) + try { - return false; - } + if (!base.Prepare(actionModifier, item, isBulkAction)) + { + return false; + } - _rules.Clear(); + _rules.Clear(); - if (item is MergeRule singleRule) - { - _rules.Add(singleRule); - } - else - { - _rules.AddRange(Settings.MergeRules.DeepClone().ToList()); - } + if (item is MergeRule singleRule) + { + _rules.Add(singleRule); + } + else + { + _rules.AddRange(Settings.MergeRules.DeepClone().ToList()); + } - foreach (var rule in _rules) - { - foreach (var sourceItem in rule.SourceObjects) + if (_rules.Count == 0) { - sourceItem.RefreshId(); + return false; } - rule.AddToDb(); + foreach (var rule in _rules) + { + foreach (var sourceItem in rule.SourceObjects) + { + sourceItem.RefreshId(); + } + + rule.AddToDb(); + } + } + catch (Exception e) + { + Log.Error(e); + return false; } return true; diff --git a/Generic/MetadataUtilities/MetadataFunctions.cs b/Generic/MetadataUtilities/MetadataFunctions.cs index ee1016483..074892c36 100644 --- a/Generic/MetadataUtilities/MetadataFunctions.cs +++ b/Generic/MetadataUtilities/MetadataFunctions.cs @@ -22,6 +22,7 @@ public static void DoForAll(MetadataUtilities plugin, List games, IBaseA bool showDialog = false, ActionModifierType actionModifier = ActionModifierType.None, object item = null) { plugin.IsUpdating = true; + var gamesAffected = 0; if (plugin.Settings.Settings.WriteDebugLog) { @@ -33,15 +34,17 @@ public static void DoForAll(MetadataUtilities plugin, List games, IBaseA { if (games.Count == 1) { - action.Execute(games.First(), actionModifier, item, false); + if (action.Execute(games.First(), actionModifier, item, false)) + { + gamesAffected++; + } + action.FollowUp(actionModifier, item, false); } // if we have more than one game in the list, we want to start buffered mode and // show a progress bar. else if (games.Count > 1) { - var gamesAffected = 0; - using (plugin.PlayniteApi.Database.BufferedUpdate()) { if (!action.Prepare(actionModifier, item)) @@ -90,11 +93,6 @@ public static void DoForAll(MetadataUtilities plugin, List games, IBaseA }, globalProgressOptions); } - if (plugin.Settings.Settings.WriteDebugLog) - { - Log.Debug($"===> Finished {action.GetType()} with {gamesAffected} games affected. ======================="); - } - // Shows a dialog with the number of games actually affected. if (!showDialog) { @@ -107,6 +105,11 @@ public static void DoForAll(MetadataUtilities plugin, List games, IBaseA } finally { + if (plugin.Settings.Settings.WriteDebugLog) + { + Log.Debug($"===> Finished {action.GetType()} with {gamesAffected} games affected. ======================="); + } + plugin.IsUpdating = false; Cursor.Current = Cursors.Default; } diff --git a/Generic/MetadataUtilities/Properties/AssemblyInfo.cs b/Generic/MetadataUtilities/Properties/AssemblyInfo.cs index a0bd03408..26b65f481 100644 --- a/Generic/MetadataUtilities/Properties/AssemblyInfo.cs +++ b/Generic/MetadataUtilities/Properties/AssemblyInfo.cs @@ -25,5 +25,5 @@ // // You can specify all the values or you can default the Build and Revision Numbers by using the '*' // as shown below: [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.1.0")] -[assembly: AssemblyFileVersion("1.2.1.0")] \ No newline at end of file +[assembly: AssemblyVersion("1.2.2.0")] +[assembly: AssemblyFileVersion("1.2.2.0")] \ No newline at end of file diff --git a/Generic/MetadataUtilities/extension.yaml b/Generic/MetadataUtilities/extension.yaml index e779c5e8b..50170bbb2 100644 --- a/Generic/MetadataUtilities/extension.yaml +++ b/Generic/MetadataUtilities/extension.yaml @@ -1,7 +1,7 @@ Id: MetadataUtilities_485ab5f0-bfb1-4c17-93cc-20d8338673be Name: Metadata Utilities Author: HerrKnarz -Version: 1.2.1 +Version: 1.2.2 Module: MetadataUtilities.dll Type: GenericPlugin Icon: icon.png