Skip to content

Commit

Permalink
Fixed bug that caused merge rules not being executed on metadata upda…
Browse files Browse the repository at this point in the history
…te, 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.
  • Loading branch information
HerrKnarz committed Oct 1, 2024
1 parent 8f31bf8 commit 3eb3be0
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 38 deletions.
29 changes: 19 additions & 10 deletions Generic/MetadataUtilities/Actions/AfterMetadataUpdateAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
48 changes: 31 additions & 17 deletions Generic/MetadataUtilities/Actions/MergeAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using MetadataUtilities.Enums;
using MetadataUtilities.Models;
using Playnite.SDK;
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -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;
Expand Down
19 changes: 11 additions & 8 deletions Generic/MetadataUtilities/MetadataFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static void DoForAll(MetadataUtilities plugin, List<MyGame> games, IBaseA
bool showDialog = false, ActionModifierType actionModifier = ActionModifierType.None, object item = null)
{
plugin.IsUpdating = true;
var gamesAffected = 0;

if (plugin.Settings.Settings.WriteDebugLog)
{
Expand All @@ -33,15 +34,17 @@ public static void DoForAll(MetadataUtilities plugin, List<MyGame> 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))
Expand Down Expand Up @@ -90,11 +93,6 @@ public static void DoForAll(MetadataUtilities plugin, List<MyGame> 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)
{
Expand All @@ -107,6 +105,11 @@ public static void DoForAll(MetadataUtilities plugin, List<MyGame> games, IBaseA
}
finally
{
if (plugin.Settings.Settings.WriteDebugLog)
{
Log.Debug($"===> Finished {action.GetType()} with {gamesAffected} games affected. =======================");
}

plugin.IsUpdating = false;
Cursor.Current = Cursors.Default;
}
Expand Down
4 changes: 2 additions & 2 deletions Generic/MetadataUtilities/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
[assembly: AssemblyVersion("1.2.2.0")]
[assembly: AssemblyFileVersion("1.2.2.0")]
2 changes: 1 addition & 1 deletion Generic/MetadataUtilities/extension.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 3eb3be0

Please sign in to comment.