Skip to content

Commit

Permalink
ImporterForAnilist: Add Play Action functionality, providing options …
Browse files Browse the repository at this point in the history
…to open the game links.
  • Loading branch information
darklinkpower committed Oct 22, 2021
1 parent 54cebb5 commit c2aad97
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 19 deletions.
108 changes: 99 additions & 9 deletions source/Library/ImporterforAnilist/ImporterForAnilist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.IO;
using System.Text.RegularExpressions;

namespace ImporterforAnilist
{
public class ImporterForAnilist : LibraryPlugin
{
private static readonly ILogger logger = LogManager.GetLogger();
private const string dbImportMessageId = "anilistlibImportError";
private static readonly Regex mangadexIdRegex = new Regex(@"^https:\/\/mangadex\.org\/title\/([^\/]+)", RegexOptions.Compiled);
private static readonly Regex mangaseeIdRegex = new Regex(@"^https:\/\/mangasee123\.com\/manga\/([^\/]+)", RegexOptions.Compiled);

private ImporterForAnilistSettingsViewModel settings { get; set; }

Expand All @@ -31,7 +35,7 @@ public class ImporterForAnilist : LibraryPlugin

public ImporterForAnilist(IPlayniteAPI api) : base(api)
{
settings = new ImporterForAnilistSettingsViewModel(this);
settings = new ImporterForAnilistSettingsViewModel(this, PlayniteApi);
Properties = new LibraryPluginProperties
{
HasSettings = true
Expand Down Expand Up @@ -62,13 +66,6 @@ public GameMetadata EntryToGameMetadata(Entry entry, string propertiesPrefix)
GameId = entry.Media.Id.ToString(),
Name = entry.Media.Title.Romaji ?? entry.Media.Title.English ?? entry.Media.Title.Native ?? string.Empty,
IsInstalled = true,
GameActions = new List<GameAction> {
new GameAction
{
Type = GameActionType.URL,
Path = entry.Media.SiteUrl.ToString()
}
},
Platforms = new HashSet<MetadataProperty> { new MetadataNameProperty(string.Format("AniList {0}", entry.Media.Type.ToString())) },
//Description
Description = entry.Media.Description ?? string.Empty,
Expand Down Expand Up @@ -238,7 +235,6 @@ public override IEnumerable<GameMetadata> GetGames(LibraryGetGamesArgs args)
var accountApi = new AnilistAccountClient(PlayniteApi, settings.Settings.AccountAccessCode);
if (string.IsNullOrEmpty(accountApi.anilistUsername))
{

//Username could not be obtained
PlayniteApi.Notifications.Add(new NotificationMessage(
dbImportMessageId,
Expand Down Expand Up @@ -298,5 +294,99 @@ public override LibraryMetadataProvider GetMetadataDownloader()

return new AnilistMetadataProvider(this, PlayniteApi, propertiesPrefix, MalSyncRateLimiter);
}

public override IEnumerable<PlayController> GetPlayActions(GetPlayActionsArgs args)
{
var game = args.Game;
if (game.PluginId != Id)
{
yield break;
}

if (game.Links == null || game.Links.Count == 0)
{
PlayniteApi.Dialogs.ShowMessage("Game doesn't have links available. Download metadata.");
yield break;
}

var browserPath = string.Empty;
if (!string.IsNullOrEmpty(settings.Settings.BrowserPath) && File.Exists(settings.Settings.BrowserPath))
{
browserPath = settings.Settings.BrowserPath;
}

var cubariLinks = new List<Link>();
foreach (Link link in game.Links)
{
if (link.Name == string.Empty || link.Name == "AniList" || link.Name == "MyAnimeList")
{
continue;
}

if (link.Name.StartsWith("Mangadex"))
{
var match = mangadexIdRegex.Match(link.Url);
if (match.Success)
{
var actionName = string.Format("Cubari (MangaDex) {0}", link.Name.Replace("Mangadex - ", ""));
var actionUrl = string.Format(@"https://cubari.moe/read/mangadex/{0}/", match.Groups[1]);
cubariLinks.Add(new Link { Name = actionName, Url = actionUrl });
}
}
else if (link.Name.StartsWith("MangaSee"))
{
var match = mangaseeIdRegex.Match(link.Url);
if (match.Success)
{
var actionName = string.Format("Cubari (MangaSee) {0}", link.Name.Replace("MangaSee - ", ""));
var actionUrl = string.Format(@"https://cubari.moe/read/mangasee/{0}/", match.Groups[1]);
cubariLinks.Add(new Link { Name = actionName, Url = actionUrl });
}
}

yield return CreatePlayController(game, link.Name, link.Url, browserPath);
}

foreach (Link link in cubariLinks)
{
yield return CreatePlayController(game, link.Name, link.Url, browserPath);
}
}

public AutomaticPlayController CreatePlayController(Game game, string name, string url, string browserPath)
{
if (browserPath != string.Empty)
{
return CreateBrowserPlayController(game, name, url);
}
else
{
return CreateUrlPlayController(game, name, url);
}
}

public AutomaticPlayController CreateBrowserPlayController(Game game, string name, string url)
{
return new AutomaticPlayController(game)
{
Name = $"Open link \"{name}\"",
Path = settings.Settings.BrowserPath,
Type = AutomaticPlayActionType.File,
Arguments = url,
WorkingDir = Path.GetDirectoryName(settings.Settings.BrowserPath),
TrackingMode = TrackingMode.Process
};
}

public AutomaticPlayController CreateUrlPlayController(Game game, string name, string url)
{
return new AutomaticPlayController(game)
{
Name = $"Open link \"{name}\"",
Path = url,
Type = AutomaticPlayActionType.Url,
TrackingMode = TrackingMode.Default
};
}
}
}
30 changes: 23 additions & 7 deletions source/Library/ImporterforAnilist/ImporterForAnilistSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace ImporterforAnilist
{
public class ImporterForAnilistSettings
public class ImporterForAnilistSettings : ObservableObject
{
public string AccountAccessCode { get; set; } = string.Empty;
public string PropertiesPrefix { get; set; } = string.Empty;
Expand All @@ -17,11 +17,8 @@ public class ImporterForAnilistSettings
public bool UpdateUserScoreOnLibUpdate { get; set; } = true;
public bool UpdateCompletionStatusOnLibUpdate { get; set; } = true;
public bool UpdateProgressOnLibUpdate { get; set; } = false;

// Playnite serializes settings object to a JSON object and saves it as text file.
// If you want to exclude some property from being saved then use `JsonDontSerialize` ignore attribute.
[DontSerialize]
public bool OptionThatWontBeSaved { get; set; } = false;
private string browserPath = string.Empty;
public string BrowserPath { get => browserPath; set => SetValue(ref browserPath, value); }
}

public class ImporterForAnilistSettingsViewModel : ObservableObject, ISettings
Expand All @@ -40,10 +37,13 @@ public ImporterForAnilistSettings Settings
}
}

public ImporterForAnilistSettingsViewModel(ImporterForAnilist plugin)
private IPlayniteAPI playniteApi;

public ImporterForAnilistSettingsViewModel(ImporterForAnilist plugin, IPlayniteAPI playniteApi)
{
// Injecting your plugin instance is required for Save/Load method because Playnite saves data to a location based on what plugin requested the operation.
this.plugin = plugin;
this.playniteApi = playniteApi;

// Load saved settings.
var savedSettings = plugin.LoadPluginSettings<ImporterForAnilistSettings>();
Expand Down Expand Up @@ -101,5 +101,21 @@ private void Login()
string url = string.Format("https://anilist.co/api/v2/oauth/authorize?client_id={0}&response_type=token", "5706");
System.Diagnostics.Process.Start(url);
}

public RelayCommand<object> SelectBrowserExecutableCommand
{
get => new RelayCommand<object>((a) =>
{
SelectBrowserExecutable();
});
}
private void SelectBrowserExecutable()
{
var executablePath = playniteApi.Dialogs.SelectFile("Exe|*.exe");
if (!string.IsNullOrEmpty(executablePath))
{
settings.BrowserPath = executablePath;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
<!--TODO Completion Status matching-->
<!--<CheckBox Name="UpdateCompletionStatusOnLibUpdate" Margin="0,10,0,0" IsChecked="{Binding UpdateCompletionStatusOnLibUpdate}" Content="{DynamicResource LOCImporter_For_Anilist_UpdateCompletionStatusSetting}"/>-->
<CheckBox Name="UpdateProgressOnLibUpdate" Margin="0,10,0,0" IsChecked="{Binding Settings.UpdateProgressOnLibUpdate}" Content="{DynamicResource LOCImporter_For_Anilist_UpdateProgressSetting}"/>
<DockPanel Margin="0,10,0,0">
<TextBlock Text="{DynamicResource LOCImporter_For_Anilist_SettingBrowserPathLabel}" DockPanel.Dock="Left" VerticalAlignment="Center"/>
<Button Content="{DynamicResource LOCImporter_For_Anilist_SettingBrowserSelectExecutableLabel}" DockPanel.Dock="Right" VerticalAlignment="Center"
Command="{Binding SelectBrowserExecutableCommand}" Margin="10,0,0,0"/>
<TextBox Text="{Binding Settings.BrowserPath}" DockPanel.Dock="Left" VerticalAlignment="Center" IsReadOnly="True"
ToolTip="{DynamicResource LOCImporter_For_Anilist_BrowserExecutableTooltip}" Margin="10,0,0,0" />
</DockPanel>
</StackPanel>
</Grid>
</UserControl>
4 changes: 1 addition & 3 deletions source/Library/ImporterforAnilist/ImporterforAnilist.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Folder Include="Converters\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>xcopy "$(ProjectDir)Localization" "$(TargetDir)\Localization" /Y /I /E</PreBuildEvent>
Expand Down
3 changes: 3 additions & 0 deletions source/Library/ImporterforAnilist/Localization/en_US.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
<sys:String x:Key="LOCImporter_For_Anilist_UpdaterUserscoreSetting">Update user score from AniList account during library update</sys:String>
<sys:String x:Key="LOCImporter_For_Anilist_UpdateCompletionStatusSetting">Update completion status from AniList account during library update</sys:String>
<sys:String x:Key="LOCImporter_For_Anilist_UpdateProgressSetting">Update progress from AniList account during library update (&quot;Version&quot; field is used)</sys:String>
<sys:String x:Key="LOCImporter_For_Anilist_BrowserExecutableTooltip">If configured, the links opened by the Play Action will be opened in the configured browser.</sys:String>
<sys:String x:Key="LOCImporter_For_Anilist_SettingBrowserPathLabel">Browser path:</sys:String>
<sys:String x:Key="LOCImporter_For_Anilist_SettingBrowserSelectExecutableLabel">Select browser</sys:String>
</ResourceDictionary>

0 comments on commit c2aad97

Please sign in to comment.