Skip to content

Commit

Permalink
Merge branch 'master' into l10n_master
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrKnarz authored Nov 8, 2023
2 parents b56487f + 141b2a8 commit 50aadaf
Show file tree
Hide file tree
Showing 21 changed files with 662 additions and 68 deletions.
15 changes: 15 additions & 0 deletions Common/KNARZhelper/MiscHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Playnite.SDK;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
Expand Down Expand Up @@ -29,5 +31,18 @@ public static void AddTextIcoFontResource(string key, string text)
FontFamily = ResourceProvider.GetResource("FontIcoFont") as FontFamily
});
}

public static int RemoveAll<T>(
this ObservableCollection<T> coll, Func<T, bool> condition)
{
System.Collections.Generic.List<T> itemsToRemove = coll.Where(condition).ToList();

foreach (T itemToRemove in itemsToRemove)
{
coll.Remove(itemToRemove);
}

return itemsToRemove.Count;
}
}
}
4 changes: 3 additions & 1 deletion Generic/LinkUtilities/BaseClasses/Linker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public virtual bool AddSearchedLink(Game game, bool skipExistingLinks = false, b
game.Name,
$"{ResourceProvider.GetString("LOCLinkUtilitiesDialogSearchGame")} ({LinkName})");

return result != null && LinkHelper.AddLink(game, LinkName, ((SearchResult)result).Url, false, cleanUpAfterAdding);
return result != null && AddLinkFromSearch(game, (SearchResult)result, cleanUpAfterAdding);
}

public virtual List<GenericItemOption> GetSearchResults(string searchTerm) => new List<GenericItemOption>();
Expand Down Expand Up @@ -184,6 +184,8 @@ public virtual string GetGamePath(Game game, string gameName = null)
return string.Empty;
}

public virtual bool AddLinkFromSearch(Game game, SearchResult result, bool cleanUpAfterAdding = true) => LinkHelper.AddLink(game, LinkName, result.Url, false, cleanUpAfterAdding);

/// <summary>
/// Searches for a game by name and looks for a matching search result.
/// </summary>
Expand Down
30 changes: 19 additions & 11 deletions Generic/LinkUtilities/Interfaces/ILinkAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,49 @@ public enum ActionModifierTypes
SearchSelected,
Name,
SortOrder,
DontRename
DontRename,
AppLink,
WebLink
}

/// <summary>
/// Interface for classes, that can be used as a link action. Contains texts for the progress bar, result dialog and the action to
/// execute.
/// Interface for classes, that can be used as a link action. Contains texts for the progress bar, result dialog and
/// the action to
/// execute.
/// </summary>
internal interface ILinkAction
{
/// <summary>
/// Resource for the localized text in the progress bar
/// Resource for the localized text in the progress bar
/// </summary>
string ProgressMessage { get; }

/// <summary>
/// Resource for the localized text in the result dialog. Should contain placeholder for the number of affected games.
/// Resource for the localized text in the result dialog. Should contain placeholder for the number of affected games.
/// </summary>
string ResultMessage { get; }

/// <summary>
/// Prepares the link action before performing the execute method. Should be executed before a loop containing the Execute method.
/// Prepares the link action before performing the execute method. Should be executed before a loop containing the
/// Execute method.
/// </summary>
/// <param name="actionModifier">Optional modifier for the underlying class to do different things in the execute method</param>
/// <param name="isBulkAction">If true the action is executed for more than one game in a loop. Can be used to do things
/// differently if only one game is processed.</param>
/// <param name="isBulkAction">
/// If true the action is executed for more than one game in a loop. Can be used to do things
/// differently if only one game is processed.
/// </param>
/// <returns>true, if the action was successful</returns>
bool Prepare(ActionModifierTypes actionModifier = ActionModifierTypes.None, bool isBulkAction = true);

/// <summary>
/// Executes the action on a game.
/// Executes the action on a game.
/// </summary>
/// <param name="game">The game to be processed</param>
/// <param name="actionModifier">Optional modifier for the underlying class to do different things in the execute method</param>
/// <param name="isBulkAction">If true the action is executed for more than one game in a loop. Can be used to do things
/// differently if only one game is processed. If false, the Prepare method will also be executed here!</param>
/// <param name="isBulkAction">
/// If true the action is executed for more than one game in a loop. Can be used to do things
/// differently if only one game is processed. If false, the Prepare method will also be executed here!
/// </param>
/// <returns>true, if the action was successful</returns>
bool Execute(Game game, ActionModifierTypes actionModifier = ActionModifierTypes.None, bool isBulkAction = true);
}
Expand Down
110 changes: 110 additions & 0 deletions Generic/LinkUtilities/LinkActions/ChangeSteamLinks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using LinkUtilities.BaseClasses;
using LinkUtilities.Settings;
using Playnite.SDK;
using Playnite.SDK.Models;
using System.Linq;

namespace LinkUtilities.LinkActions
{
/// <summary>
/// Class to rename links based on patterns.
/// </summary>
internal class ChangeSteamLinks : LinkAction
{
private const string _communityPattern = "steamcommunity.com";
private const string _steamAppPrefix = "steam://openurl/";
private const string _storePattern = "steampowered.com";
private static ChangeSteamLinks _instance;
private static readonly object _mutex = new object();
private ChangeSteamLinks() { }

public override string ProgressMessage => "LOCLinkUtilitiesProgressChangeSteamLinks";

public override string ResultMessage => "LOCLinkUtilitiesDialogSteamChangedMessage";

public bool ChangeSteamLinksAfterChange { get; set; } = false;

public static ChangeSteamLinks Instance()
{
if (_instance != null)
{
return _instance;
}

lock (_mutex)
{
if (_instance == null)
{
_instance = new ChangeSteamLinks();
}
}

return _instance;
}

private static bool Change(Game game, ActionModifierTypes actionModifier, bool updateDb = true)
{
if (!game.Links?.Any() ?? true)
{
return false;
}

bool mustUpdate = false;

foreach (Link link in game.Links)
{
if (!link.Url.Contains(_communityPattern) && !link.Url.Contains(_storePattern))
{
continue;
}

string url = link.Url;

if (actionModifier == ActionModifierTypes.AppLink && link.Url.StartsWith("http"))
{
url = _steamAppPrefix + url;
}

if (actionModifier == ActionModifierTypes.WebLink && link.Url.StartsWith(_steamAppPrefix))
{
url = url.Replace(_steamAppPrefix, string.Empty);
}

if (url == link.Url)
{
continue;
}

if (GlobalSettings.Instance().OnlyATest)
{
link.Url = url;
}
else
{
API.Instance.MainView.UIDispatcher.Invoke(delegate
{
link.Url = url;
});
}

mustUpdate = true;
}

if (!mustUpdate)
{
return false;
}

if (updateDb && !GlobalSettings.Instance().OnlyATest)
{
API.Instance.Database.Games.Update(game);
}

return true;
}

public override bool Execute(Game game, ActionModifierTypes actionModifier = ActionModifierTypes.None, bool isBulkAction = true)
=> base.Execute(game, actionModifier, isBulkAction) &&
Change(game, actionModifier);
}
}
13 changes: 9 additions & 4 deletions Generic/LinkUtilities/LinkActions/DoAfterChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
namespace LinkUtilities.LinkActions
{
/// <summary>
/// Sorts the Links of a game.
/// Sorts the Links of a game.
/// </summary>
internal class DoAfterChange : LinkAction
{
private static DoAfterChange _instance;
private static readonly object _mutex = new object();
private DoAfterChange() { }

public override string ProgressMessage => "LOCLinkUtilitiesProgressSortLinks";
public override string ResultMessage => "LOCLinkUtilitiesDialogSortedMessage";

public static DoAfterChange Instance()
{
if (_instance != null)
Expand All @@ -31,9 +34,6 @@ public static DoAfterChange Instance()
return _instance;
}

public override string ProgressMessage => "LOCLinkUtilitiesProgressSortLinks";
public override string ResultMessage => "LOCLinkUtilitiesDialogSortedMessage";

public override bool Execute(Game game, ActionModifierTypes actionModifier = ActionModifierTypes.None, bool isBulkAction = true)
{
if (!base.Execute(game, actionModifier, isBulkAction))
Expand All @@ -53,6 +53,11 @@ public override bool Execute(Game game, ActionModifierTypes actionModifier = Act
result |= RemoveLinks.Instance().Execute(game, actionModifier);
}

if (ChangeSteamLinks.Instance().ChangeSteamLinksAfterChange)
{
result |= ChangeSteamLinks.Instance().Execute(game, ActionModifierTypes.AppLink);
}

if (RemoveDuplicates.Instance().RemoveDuplicatesAfterChange)
{
result |= RemoveDuplicates.Instance().Execute(game, actionModifier);
Expand Down
70 changes: 69 additions & 1 deletion Generic/LinkUtilities/LinkUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public LinkUtilities(IPlayniteAPI api) : base(api)
{ "luReviewIcon", "\xeaeb" },
{ "luDuplicateIcon", "\xedea" },
{ "luRenameIcon", "\xeded" },
{ "luTagIcon", "\xf004" }
{ "luTagIcon", "\xf004" },
{ "luSteamIcon", "\xe93e" }
};

foreach (KeyValuePair<string, string> iconResource in iconResourcesToAdd)
Expand Down Expand Up @@ -425,6 +426,55 @@ public override IEnumerable<MainMenuItem> GetMainMenuItems(GetMainMenuItemsArgs
});
}

// Adds the "Change steam links" item to the main menu.
menuItems.Add(new MainMenuItem
{
Description = ResourceProvider.GetString("LOCLinkUtilitiesMenuChangeSteamLinksToApp"),
MenuSection = $"@{menuSection}|{menuAllGames}",
Icon = "luSteamIcon",
Action = a =>
{
List<Game> games = PlayniteApi.Database.Games.Distinct().ToList();
DoForAll(games, ChangeSteamLinks.Instance(), true, ActionModifierTypes.AppLink);
}
});

menuItems.Add(new MainMenuItem
{
Description = ResourceProvider.GetString("LOCLinkUtilitiesMenuChangeSteamLinksToApp"),
MenuSection = $"@{menuSection}|{menuFilteredGames}",
Icon = "luSteamIcon",
Action = a =>
{
List<Game> games = PlayniteApi.MainView.FilteredGames.Distinct().ToList();
DoForAll(games, ChangeSteamLinks.Instance(), true, ActionModifierTypes.AppLink);
}
});

menuItems.Add(new MainMenuItem
{
Description = ResourceProvider.GetString("LOCLinkUtilitiesMenuChangeSteamLinksToWeb"),
MenuSection = $"@{menuSection}|{menuAllGames}",
Icon = "luSteamIcon",
Action = a =>
{
List<Game> games = PlayniteApi.Database.Games.Distinct().ToList();
DoForAll(games, ChangeSteamLinks.Instance(), true, ActionModifierTypes.WebLink);
}
});

menuItems.Add(new MainMenuItem
{
Description = ResourceProvider.GetString("LOCLinkUtilitiesMenuChangeSteamLinksToWeb"),
MenuSection = $"@{menuSection}|{menuFilteredGames}",
Icon = "luSteamIcon",
Action = a =>
{
List<Game> games = PlayniteApi.MainView.FilteredGames.Distinct().ToList();
DoForAll(games, ChangeSteamLinks.Instance(), true, ActionModifierTypes.WebLink);
}
});

// Adds the "Tag missing links" item to the main menu.
if (TagMissingLinks.Instance().MissingLinkPatterns?.Any() ?? false)
{
Expand Down Expand Up @@ -662,6 +712,24 @@ public override IEnumerable<GameMenuItem> GetGameMenuItems(GetGameMenuItemsArgs
});
}

menuItems.Add(new GameMenuItem
{
Description = ResourceProvider.GetString("LOCLinkUtilitiesMenuChangeSteamLinksToApp"),
MenuSection = menuSection,
Icon = "luSteamIcon",
Action = a => DoForAll(games, ChangeSteamLinks.Instance(), true, ActionModifierTypes.AppLink)
});

menuItems.Add(new GameMenuItem
{
Description = ResourceProvider.GetString("LOCLinkUtilitiesMenuChangeSteamLinksToWeb"),
MenuSection = menuSection,
Icon = "luSteamIcon",
Action = a => DoForAll(games, ChangeSteamLinks.Instance(), true, ActionModifierTypes.WebLink)
});

//TODO nach einem besseren Icon suchen!

// Adds the "Tag missing links" item to the game menu.
if (TagMissingLinks.Instance().MissingLinkPatterns?.Any() ?? false)
{
Expand Down
3 changes: 3 additions & 0 deletions Generic/LinkUtilities/LinkUtilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,13 @@
<Compile Include="Helper\ParseHelper.cs" />
<Compile Include="Helper\WikipediaApiCaller.cs" />
<Compile Include="LinkActions\AddLinkFromClipboard.cs" />
<Compile Include="LinkActions\ChangeSteamLinks.cs" />
<Compile Include="LinkActions\DoAfterChange.cs" />
<Compile Include="Linker\LinkSources\LinkGrouvee.cs" />
<Compile Include="Linker\LinkSources\LinkBackloggd.cs" />
<Compile Include="Linker\LinkSources\LinkGGDeals.cs" />
<Compile Include="Linker\LinkSources\LinkModDB.cs" />
<Compile Include="Linker\LinkSources\LinkTvTropes.cs" />
<Compile Include="ViewModels\CheckLink.cs" />
<Compile Include="ViewModels\CheckLinks.cs" />
<Compile Include="ViewModels\CheckLinksViewModel.cs" />
Expand Down
Loading

0 comments on commit 50aadaf

Please sign in to comment.