Skip to content

Commit

Permalink
Adds Nintendo auth & purchased games list retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
XenorPLxx committed Oct 6, 2021
1 parent f8654fe commit a00e6d2
Show file tree
Hide file tree
Showing 23 changed files with 494 additions and 1,041 deletions.
File renamed without changes.
35 changes: 35 additions & 0 deletions source/Libraries/NintendoLibrary/Models/NintendoModels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NintendoLibrary.Models
{
public class AuthError
{
public class Error
{
public string code { get; set; }
public string message { get; set; }
}
public Error error;
}

public class PurchasedList
{
public int length { get; set; }
public int offset { get; set; }
public int total { get; set; }
public class Transaction
{
public string content_type { get; set; }
public string title { get; set; }
public string device_type { get; set; }
public DateTime date { get; set; }
public ulong transaction_id { get; set; }
}

public List<Transaction> transactions { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
using System.Text;
using System.Threading.Tasks;

namespace PSNLibrary
namespace NintendoLibrary
{
}
146 changes: 146 additions & 0 deletions source/Libraries/NintendoLibrary/NintendoLibrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
using Playnite.SDK;
using Playnite.SDK.Models;
using Playnite.SDK.Plugins;
using NintendoLibrary.Models;
using NintendoLibrary.Services;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace NintendoLibrary
{
[LoadPlugin]
public class NintendoLibrary : LibraryPluginBase<NintendoLibrarySettingsViewModel>
{
public NintendoLibrary(IPlayniteAPI api) : base(
"Nintendo",
Guid.Parse("e4ac81cb-1b1a-4ec9-8639-9a9633989a72"),
new LibraryPluginProperties { CanShutdownClient = false, HasCustomizedGameImport = true, HasSettings = true },
null,
Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"icon.png"),
(_) => new NintendoLibrarySettingsView(),
api)
{
SettingsViewModel = new NintendoLibrarySettingsViewModel(this, api);
}

private string ParsePlatform(string platformId)
{
string platform = null;

switch (platformId)
{
case "HAC":
platform = "nintendo_switch";
break;

case "CTR":
platform = "nintendo_3ds";
break;

default:
break;
}
return platform;
}

private string FixGameName(string name)
{
var gameName = name.
RemoveTrademarks(" ").
NormalizeGameName().
Replace("full game", "", StringComparison.OrdinalIgnoreCase).
Trim();
return Regex.Replace(gameName, @"\s+", " ");
}

private List<GameMetadata> parseGames(List<PurchasedList.Transaction> gamesToParse)
{
var parsedGames = new List<GameMetadata>();
foreach (var title in gamesToParse)
{
var gameName = FixGameName(title.title);

string platform = ParsePlatform(title.device_type);

parsedGames.Add(new GameMetadata
{
GameId = title.transaction_id.ToString(),
Name = gameName,
Platforms = new HashSet<MetadataProperty> { new MetadataSpecProperty(platform) }
});
}

return parsedGames;
}

private List<GameMetadata> ParsePlayedList(NintendoAccountClient clientApi)
{
var gamesToParse = clientApi.GetPurchasedList().GetAwaiter().GetResult();
return parseGames(gamesToParse);
}

public override IEnumerable<Game> ImportGames(LibraryImportGamesArgs args)
{
var importedGames = new List<Game>();

Exception importError = null;
if (!SettingsViewModel.Settings.ConnectAccount)
{
return importedGames;
}

try
{
var clientApi = new NintendoAccountClient(this, PlayniteApi);
var allGames = new List<GameMetadata>();
allGames.AddRange(ParsePlayedList(clientApi));

// This need to happen to merge games from different APIs
foreach (var group in allGames.GroupBy(a => a.GameId))
{
var game = group.First();
if (PlayniteApi.ApplicationSettings.GetGameExcludedFromImport(game.GameId, Id))
{
continue;
}

var alreadyImported = PlayniteApi.Database.Games.FirstOrDefault(a => a.GameId == game.GameId && a.PluginId == Id);
if (alreadyImported == null)
{
game.Source = new MetadataNameProperty("Nintendo");
importedGames.Add(PlayniteApi.Database.ImportGame(game, this));
}
}
}
catch (Exception e) when (!Debugger.IsAttached)
{
Logger.Error(e, "Failed to import Nintendo games.");
importError = e;
}

if (importError != null)
{
PlayniteApi.Notifications.Add(new NotificationMessage(
ImportErrorMessageId,
string.Format(PlayniteApi.Resources.GetString("LOCLibraryImportError"), Name) +
System.Environment.NewLine + importError.Message,
NotificationType.Error,
() => OpenSettingsView()));
}
else
{
PlayniteApi.Notifications.Remove(ImportErrorMessageId);
}

return importedGames;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PSNLibrary</RootNamespace>
<AssemblyName>PSNLibrary</AssemblyName>
<RootNamespace>NintendoLibrary</RootNamespace>
<AssemblyName>NintendoLibrary</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
Expand Down Expand Up @@ -151,16 +151,16 @@
<Compile Include="..\..\Generic\PlayniteExtensions.Common\PluginSettingsViewModel.cs">
<Link>Shared\PluginSettingsViewModel.cs</Link>
</Compile>
<Compile Include="Models\PSNModels.cs" />
<Compile Include="PSNGameController.cs" />
<Compile Include="PSNLibraryClient.cs" />
<Compile Include="PSNLibrary.cs" />
<Compile Include="PSNLibrarySettingsViewModel.cs" />
<Compile Include="PSNLibrarySettingsView.xaml.cs">
<DependentUpon>PSNLibrarySettingsView.xaml</DependentUpon>
<Compile Include="Models\NintendoModels.cs" />
<Compile Include="NintendoGameController.cs" />
<Compile Include="NintendoLibraryClient.cs" />
<Compile Include="NintendoLibrary.cs" />
<Compile Include="NintendoLibrarySettingsViewModel.cs" />
<Compile Include="NintendoLibrarySettingsView.xaml.cs">
<DependentUpon>NintendoLibrarySettingsView.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\PSNAccountClient.cs" />
<Compile Include="Services\NintendoAccountClient.cs" />
</ItemGroup>
<ItemGroup>
<None Include="addon.yaml" />
Expand All @@ -185,7 +185,7 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="PSNLibrarySettingsView.xaml">
<Page Include="NintendoLibrarySettingsView.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
using System.Text;
using System.Threading.Tasks;

namespace PSNLibrary
namespace NintendoLibrary
{
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<UserControl x:Class="PSNLibrary.PSNLibrarySettingsView"
<UserControl x:Class="NintendoLibrary.NintendoLibrarySettingsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Expand All @@ -13,25 +13,15 @@
</UserControl.Resources>

<!--<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center"
Text="PSN integration is currently broken and not supported." />-->
Text="Nintendo integration is currently broken and not supported." />-->

<StackPanel Margin="20">
<CheckBox Name="CheckPSNConnectAccount"
<CheckBox Name="CheckNintendoConnectAccount"
IsChecked="{Binding Settings.ConnectAccount}"
Content="{DynamicResource LOCSettingsConnectAccount}"/>

<CheckBox Name="CheckPSNDownloadImageMetadata"
IsChecked="{Binding Settings.DownloadImageMetadata}"
Content="{DynamicResource LOCSettingsDonwloadImageMetadata}"
Margin="0,4,0,0"/>

<CheckBox Name="CheckPSNMigration"
IsChecked="{Binding Settings.Migration}"
Content="{DynamicResource LOCSettingsMigration}"
Margin="0,4,0,0"/>

<StackPanel DockPanel.Dock="Top" Margin="40,5,0,0"
IsEnabled="{Binding IsChecked, ElementName=CheckPSNConnectAccount}">
IsEnabled="{Binding IsChecked, ElementName=CheckNintendoConnectAccount}">

<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<Button Content="{DynamicResource LOCAuthenticateLabel}" HorizontalAlignment="Left"
Expand Down Expand Up @@ -61,32 +51,5 @@
</TextBlock>
</StackPanel>
</StackPanel>

<StackPanel Margin="0,16,0,0">
<CheckBox Name="CheckUpdateLastPlayed"
IsChecked="{Binding Settings.LastPlayed}"
Content="{DynamicResource LOCSettingsUpdateLastPlayed}"
Margin="0,4,0,0"/>
<CheckBox Name="CheckUpdatePlaytime"
IsChecked="{Binding Settings.Playtime}"
Content="{DynamicResource LOCSettingsUpdatePlaytime}"
Margin="0,4,0,0"/>
</StackPanel>

<StackPanel Margin="0,20,0,0">
<Label Content="{DynamicResource LOCSettingsLegacyGames}"></Label>
<CheckBox Name="CheckPSNDownloadPS3"
IsChecked="{Binding Settings.PS3}"
Content="{DynamicResource LOCSettingsDownloadPS3}"
Margin="0,4,0,0"/>
<CheckBox Name="CheckPSNDownloadPSP"
IsChecked="{Binding Settings.PSP}"
Content="{DynamicResource LOCSettingsDownloadPSP}"
Margin="0,4,0,0"/>
<CheckBox Name="CheckPSNDownloadPSVITA"
IsChecked="{Binding Settings.PSVITA}"
Content="{DynamicResource LOCSettingsDownloadPSVITA}"
Margin="0,4,0,0"/>
</StackPanel>
</StackPanel>
</UserControl>
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PSNLibrary
namespace NintendoLibrary
{
public partial class PSNLibrarySettingsView : UserControl
public partial class NintendoLibrarySettingsView : UserControl
{
public PSNLibrarySettingsView()
public NintendoLibrarySettingsView()
{
InitializeComponent();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
using Playnite.SDK;
using PSNLibrary.Services;
using NintendoLibrary.Services;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PSNLibrary
namespace NintendoLibrary
{
public class PSNLibrarySettings
public class NintendoLibrarySettings
{
public bool ConnectAccount { get; set; } = true;
public bool DownloadImageMetadata { get; set; } = true;
public bool LastPlayed{ get; set; } = false;
public bool Playtime { get; set; } = false;
public bool PS3 { get; set; } = true;
public bool PSP { get; set; } = true;
public bool PSVITA { get; set; } = true;
public bool Migration { get; set; } = true;
}

public class PSNLibrarySettingsViewModel : PluginSettingsViewModel<PSNLibrarySettings, PSNLibrary>
public class NintendoLibrarySettingsViewModel : PluginSettingsViewModel<NintendoLibrarySettings, NintendoLibrary>
{
private PSNAccountClient clientApi;
private NintendoAccountClient clientApi;

public bool IsUserLoggedIn
{
Expand All @@ -49,17 +42,17 @@ public RelayCommand<object> LoginCommand
});
}

public PSNLibrarySettingsViewModel(PSNLibrary plugin, IPlayniteAPI api) : base(plugin, api)
public NintendoLibrarySettingsViewModel(NintendoLibrary plugin, IPlayniteAPI api) : base(plugin, api)
{
clientApi = new PSNAccountClient(plugin, api);
clientApi = new NintendoAccountClient(plugin, api);
var savedSettings = LoadSavedSettings();
if (savedSettings != null)
{
Settings = savedSettings;
}
else
{
Settings = new PSNLibrarySettings();
Settings = new NintendoLibrarySettings();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("PSNLibrary")]
[assembly: AssemblyTitle("NintendoLibrary")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PSNLibrary")]
[assembly: AssemblyProduct("NintendoLibrary")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
Expand Down
Loading

0 comments on commit a00e6d2

Please sign in to comment.