Skip to content

Commit

Permalink
Refactorings as preparation to add toggles of all link sources as set…
Browse files Browse the repository at this point in the history
…tings
  • Loading branch information
HerrKnarz committed Nov 1, 2022
1 parent 13292e2 commit d8cece6
Show file tree
Hide file tree
Showing 23 changed files with 128 additions and 76 deletions.
15 changes: 6 additions & 9 deletions Source/LinkActions/AddLibraryLinks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,22 @@ namespace LinkUtilities.LinkActions
/// <summary>
/// Adds a link to the game store page of the library (e.g. steam or gog) the game is part of.
/// </summary>
public class AddLibraryLinks : ILinkAction
public class AddLibraryLinks : LinkAction
{
/// <summary>
/// contains all game libraries that have a link to a store page that can be added.
/// </summary>
private readonly Libraries libraries;

public string ProgressMessage { get; } = "LOCLinkUtilitiesProgressLibraryLink";
public string ResultMessage { get; } = "LOCLinkUtilitiesDialogAddedMessage";
public LinkUtilitiesSettings Settings { get; set; }
public override string ProgressMessage { get; } = "LOCLinkUtilitiesProgressLibraryLink";
public override string ResultMessage { get; } = "LOCLinkUtilitiesDialogAddedMessage";

public AddLibraryLinks(LinkUtilitiesSettings settings)
public AddLibraryLinks(LinkUtilities plugin) : base(plugin)
{
Settings = settings;

libraries = new Libraries(Settings);
libraries = new Libraries(Plugin);
}

public bool Execute(Game game, string actionModifier = "")
public override bool Execute(Game game, string actionModifier = "")
{
ILibraryLink library;
bool result = false;
Expand Down
15 changes: 6 additions & 9 deletions Source/LinkActions/AddWebsiteLinks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,22 @@ namespace LinkUtilities.LinkActions
/// <summary>
/// Class to add a link to all available websites in the Links list, if a definitive link was found.
/// </summary>
public class AddWebsiteLinks : ILinkAction
public class AddWebsiteLinks : LinkAction
{
/// <summary>
/// contains all website Links that can be added.
/// </summary>
public readonly Links Links;

public string ProgressMessage { get; } = "LOCLinkUtilitiesProgressWebsiteLink";
public string ResultMessage { get; } = "LOCLinkUtilitiesDialogAddedMessage";
public LinkUtilitiesSettings Settings { get; set; }
public override string ProgressMessage { get; } = "LOCLinkUtilitiesProgressWebsiteLink";
public override string ResultMessage { get; } = "LOCLinkUtilitiesDialogAddedMessage";

public AddWebsiteLinks(LinkUtilitiesSettings settings)
public AddWebsiteLinks(LinkUtilities plugin) : base(plugin)
{
Settings = settings;

Links = new Links(Settings);
Links = new Links(Plugin);
}

public bool Execute(Game game, string actionModifier = "")
public override bool Execute(Game game, string actionModifier = "")
{
bool result = false;

Expand Down
4 changes: 2 additions & 2 deletions Source/LinkActions/ILinkAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public interface ILinkAction
/// </summary>
string ResultMessage { get; }
/// <summary>
/// Settings to use for the action
/// instance of the extension to access settings etc.
/// </summary>
LinkUtilitiesSettings Settings { get; set; }
LinkUtilities Plugin { get; }

/// <summary>
/// Executes the action on a game.
Expand Down
21 changes: 21 additions & 0 deletions Source/LinkActions/LinkAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Playnite.SDK.Models;

namespace LinkUtilities.LinkActions
{
public abstract class LinkAction : ILinkAction
{
public abstract string ProgressMessage { get; }

public abstract string ResultMessage { get; }

private readonly LinkUtilities plugin;
public LinkUtilities Plugin { get { return plugin; } }

public abstract bool Execute(Game game, string actionModifier = "");

public LinkAction(LinkUtilities plugin)
{
this.plugin = plugin;
}
}
}
14 changes: 6 additions & 8 deletions Source/LinkActions/SortLinks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ namespace LinkUtilities.LinkActions
/// <summary>
/// Sorts the Links of a game.
/// </summary>
public class SortLinks : ILinkAction
public class SortLinks : LinkAction
{
public string ProgressMessage { get; } = "LOCLinkUtilitiesProgressSortLinks";
public string ResultMessage { get; } = "LOCLinkUtilitiesDialogSortedMessage";
public LinkUtilitiesSettings Settings { get; set; }

public SortLinks(LinkUtilitiesSettings settings)
public SortLinks(LinkUtilities plugin) : base(plugin)
{
Settings = settings;
}

public bool Execute(Game game, string actionModifier = "")
public override string ProgressMessage { get; } = "LOCLinkUtilitiesProgressSortLinks";
public override string ResultMessage { get; } = "LOCLinkUtilitiesDialogSortedMessage";

public override bool Execute(Game game, string actionModifier = "")
{
return LinkHelper.SortLinks(game);
}
Expand Down
10 changes: 7 additions & 3 deletions Source/LinkUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public LinkUtilities(IPlayniteAPI api) : base(api)
HasSettings = true
};

sortLinks = new SortLinks(Settings.Settings);
addLibraryLinks = new AddLibraryLinks(Settings.Settings);
addWebsiteLinks = new AddWebsiteLinks(Settings.Settings);
sortLinks = new SortLinks(this);
addLibraryLinks = new AddLibraryLinks(this);
addWebsiteLinks = new AddWebsiteLinks(this);
IsUpdating = false;

PlayniteApi.UriHandler.RegisterSource("LinkUtilities", (args) =>
Expand All @@ -53,6 +53,10 @@ public LinkUtilities(IPlayniteAPI api) : base(api)
/// Class to add a link to all available websites in the Links list, if a definitive link was found.
/// </summary>
private readonly AddWebsiteLinks addWebsiteLinks;
/// <summary>
/// Class to add a link to all available websites in the Links list, if a definitive link was found.
/// </summary>
public AddWebsiteLinks AddWebsiteLinks { get; }

/// <summary>
/// Is set to true, while the library is updated via the sortLinks function. Is used to avoid an endless loop in the function.
Expand Down
2 changes: 2 additions & 0 deletions Source/LinkUtilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<Compile Include="LinkActions\AddLibraryLinks.cs" />
<Compile Include="LinkActions\AddWebsiteLinks.cs" />
<Compile Include="LinkActions\ILinkAction.cs" />
<Compile Include="LinkActions\LinkAction.cs" />
<Compile Include="Linker\IGameLibrary.cs" />
<Compile Include="Linker\ILibraryLink.cs" />
<Compile Include="Linker\ILink.cs" />
Expand Down Expand Up @@ -84,6 +85,7 @@
<DependentUpon>LinkUtilitiesSettingsView.xaml</DependentUpon>
</Compile>
<Compile Include="Models\ItchSearchResult.cs" />
<Compile Include="Models\LinkSourceSettings.cs" />
<Compile Include="Models\MediaWikiSearchSuggestion.cs" />
<Compile Include="Models\SearchResult.cs" />
<Compile Include="Models\SteamSearchResult.cs" />
Expand Down
4 changes: 2 additions & 2 deletions Source/Linker/ILink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ interface ILink
/// </summary>
string LinkUrl { get; set; }
/// <summary>
/// Settings to be used
/// instance of the extension to access settings etc.
/// </summary>
LinkUtilitiesSettings Settings { get; set; }
LinkUtilities Plugin { get; }

/// <summary>
/// Adds a link to the specific game page of the specified website.
Expand Down
10 changes: 5 additions & 5 deletions Source/Linker/Libraries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace LinkUtilities.Linker
/// </summary>
class Libraries : List<ILibraryLink>
{
public Libraries(LinkUtilitiesSettings settings)
public Libraries(LinkUtilities plugin)
{
Add(new LibraryLinkSteam(settings));
Add(new LibraryLinkGog(settings));
if (!string.IsNullOrWhiteSpace(settings.ItchApiKey))
Add(new LibraryLinkSteam(plugin));
Add(new LibraryLinkGog(plugin));
if (!string.IsNullOrWhiteSpace(plugin.Settings.Settings.ItchApiKey))
{
Add(new LibraryLinkItch(settings));
Add(new LibraryLinkItch(plugin));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Linker/Libraries/LibraryLinkGog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override bool AddLibraryLink(Game game)

LinkUrl = $"{BaseUrl}{gogMetaData.Slug}";

return LinkHelper.AddLink(game, LinkName, LinkUrl, Settings);
return LinkHelper.AddLink(game, LinkName, LinkUrl, Plugin.Settings.Settings);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -110,7 +110,7 @@ public override List<GenericItemOption> SearchLink(string searchTerm)
return base.SearchLink(searchTerm);
}

public LibraryLinkGog(LinkUtilitiesSettings settings) : base(settings)
public LibraryLinkGog(LinkUtilities plugin) : base(plugin)
{
}
}
Expand Down
12 changes: 6 additions & 6 deletions Source/Linker/Libraries/LibraryLinkItch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class LibraryLinkItch : LinkAndLibrary
public override bool AddLibraryLink(Game game)
{
// To get the link to a game on the itch website, you need an API key and request the game data from their api.
if (!string.IsNullOrWhiteSpace(Settings.ItchApiKey) && !LinkHelper.LinkExists(game, LinkName))
if (!string.IsNullOrWhiteSpace(Plugin.Settings.Settings.ItchApiKey) && !LinkHelper.LinkExists(game, LinkName))
{
try
{
string apiUrl = string.Format(LibraryUrl, Settings.ItchApiKey, game.GameId);
string apiUrl = string.Format(LibraryUrl, Plugin.Settings.Settings.ItchApiKey, game.GameId);

WebClient client = new WebClient();

Expand All @@ -36,7 +36,7 @@ public override bool AddLibraryLink(Game game)

LinkUrl = itchMetaData.Game.Url;

return LinkHelper.AddLink(game, LinkName, LinkUrl, Settings);
return LinkHelper.AddLink(game, LinkName, LinkUrl, Plugin.Settings.Settings);
}
catch (Exception ex)
{
Expand All @@ -55,11 +55,11 @@ public override List<GenericItemOption> SearchLink(string searchTerm)
{
SearchResults.Clear();

if (!string.IsNullOrWhiteSpace(Settings.ItchApiKey))
if (!string.IsNullOrWhiteSpace(Plugin.Settings.Settings.ItchApiKey))
{
try
{
string apiUrl = string.Format(SearchUrl, Settings.ItchApiKey, searchTerm.UrlEncode());
string apiUrl = string.Format(SearchUrl, Plugin.Settings.Settings.ItchApiKey, searchTerm.UrlEncode());

WebClient client = new WebClient();

Expand Down Expand Up @@ -94,7 +94,7 @@ public override List<GenericItemOption> SearchLink(string searchTerm)
return base.SearchLink(searchTerm);
}

public LibraryLinkItch(LinkUtilitiesSettings settings) : base(settings)
public LibraryLinkItch(LinkUtilities plugin) : base(plugin)
{
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Linker/Libraries/LibraryLinkSteam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override bool AddLibraryLink(Game game)
{
// Adding a link to steam is extremely simple. You only have to add the GameId to the base URL.
LinkUrl = $"{LibraryUrl}{game.GameId}";
return LinkHelper.AddLink(game, LinkName, LinkUrl, Settings);
return LinkHelper.AddLink(game, LinkName, LinkUrl, Plugin.Settings.Settings);
}

public override List<GenericItemOption> SearchLink(string searchTerm)
Expand Down Expand Up @@ -72,7 +72,7 @@ public override List<GenericItemOption> SearchLink(string searchTerm)
return base.SearchLink(searchTerm);
}

public LibraryLinkSteam(LinkUtilitiesSettings settings) : base(settings)
public LibraryLinkSteam(LinkUtilities plugin) : base(plugin)
{
}
}
Expand Down
7 changes: 4 additions & 3 deletions Source/Linker/LibraryLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ public abstract class LibraryLink : ILibraryLink
public abstract string LinkName { get; }
public virtual string BaseUrl { get => string.Empty; }
public virtual string LinkUrl { get; set; } = string.Empty;
public LinkUtilitiesSettings Settings { get; set; }
private readonly LinkUtilities plugin;
public LinkUtilities Plugin { get { return plugin; } }

public abstract bool AddLink(Game game);

public abstract bool AddLibraryLink(Game game);

public LibraryLink(LinkUtilitiesSettings settings)
public LibraryLink(LinkUtilities plugin)
{
Settings = settings;
this.plugin = plugin;
}
}
}
12 changes: 7 additions & 5 deletions Source/Linker/Link.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public abstract class Link : ILink, ILinkAction
public virtual string LinkUrl { get; set; } = string.Empty;
public string ProgressMessage { get; } = "LOCLinkUtilitiesProgressLink";
public string ResultMessage { get; } = "LOCLinkUtilitiesDialogAddedMessage";
public LinkUtilitiesSettings Settings { get; set; }

private readonly LinkUtilities plugin;
public LinkUtilities Plugin { get { return plugin; } }

/// <summary>
/// Results of the last search for the link. Is used to get the right link after closing the search dialog, because the dialog
Expand All @@ -57,7 +59,7 @@ public virtual bool AddSearchedLink(Game game)

if (result != null)
{
return LinkHelper.AddLink(game, LinkName, SearchResults.Find(x => x.Name == result.Name).Url, Settings, false);
return LinkHelper.AddLink(game, LinkName, SearchResults.Find(x => x.Name == result.Name).Url, plugin.Settings.Settings, false);
}
else
{
Expand All @@ -84,7 +86,7 @@ public virtual bool AddLink(Game game)

if (LinkHelper.CheckUrl(LinkUrl, AllowRedirects))
{
return LinkHelper.AddLink(game, LinkName, LinkUrl, Settings);
return LinkHelper.AddLink(game, LinkName, LinkUrl, plugin.Settings.Settings);
}
else
{
Expand Down Expand Up @@ -121,9 +123,9 @@ public virtual bool Execute(Game game, string actionModifier = "")
}
}

public Link(LinkUtilitiesSettings settings)
public Link(LinkUtilities plugin)
{
Settings = settings;
this.plugin = plugin;
}
}
}
2 changes: 1 addition & 1 deletion Source/Linker/LinkAndLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace LinkUtilities.Linker
/// </summary>
public abstract class LinkAndLibrary : Link, ILibraryLink
{
protected LinkAndLibrary(LinkUtilitiesSettings settings) : base(settings)
protected LinkAndLibrary(LinkUtilities plugin) : base(plugin)
{
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Linker/LinkSources/LinkEpic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override bool AddLink(Game game)
{
string _ = client.DownloadString(url);
LinkUrl = $"{BaseUrl}{gameSlug}";
return LinkHelper.AddLink(game, LinkName, LinkUrl, Settings);
return LinkHelper.AddLink(game, LinkName, LinkUrl, Plugin.Settings.Settings);
}
catch
{
Expand Down Expand Up @@ -97,7 +97,7 @@ public override List<GenericItemOption> SearchLink(string searchTerm)
return base.SearchLink(searchTerm);
}

public LinkEpic(LinkUtilitiesSettings settings) : base(settings)
public LinkEpic(LinkUtilities plugin) : base(plugin)
{
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Linker/LinkSources/LinkHG101.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override List<GenericItemOption> SearchLink(string searchTerm)
return base.SearchLink(searchTerm);
}

public LinkHG101(LinkUtilitiesSettings settings) : base(settings)
public LinkHG101(LinkUtilities plugin) : base(plugin)
{
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Linker/LinkSources/LinkMetacritic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public override bool AddLink(Game game)

if (LinkHelper.CheckUrl(LinkUrl))
{
result = LinkHelper.AddLink(game, linkName, LinkUrl, Settings) || result;
result = LinkHelper.AddLink(game, linkName, LinkUrl, Plugin.Settings.Settings) || result;
}
}
}
Expand All @@ -100,7 +100,7 @@ public override string GetGamePath(Game game)
return game.Name.RemoveSpecialChars().CollapseWhitespaces().Replace(" ", "-").ToLower();
}

public LinkMetacritic(LinkUtilitiesSettings settings) : base(settings)
public LinkMetacritic(LinkUtilities plugin) : base(plugin)
{
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Linker/LinkSources/LinkMobyGames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override List<GenericItemOption> SearchLink(string searchTerm)
return base.SearchLink(searchTerm);
}

public LinkMobyGames(LinkUtilitiesSettings settings) : base(settings)
public LinkMobyGames(LinkUtilities plugin) : base(plugin)
{
}
}
Expand Down
Loading

0 comments on commit d8cece6

Please sign in to comment.