2023-01-07 18:34:12 +00:00
|
|
|
|
using LANCommander.Models;
|
|
|
|
|
using Playnite.SDK;
|
2023-01-05 01:23:32 +00:00
|
|
|
|
using Playnite.SDK.Models;
|
|
|
|
|
using Playnite.SDK.Plugins;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-01-07 18:34:12 +00:00
|
|
|
|
using System.IO;
|
2023-01-05 01:23:32 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Controls;
|
2023-01-07 18:34:12 +00:00
|
|
|
|
using YamlDotNet.Serialization;
|
|
|
|
|
using YamlDotNet.Serialization.NamingConventions;
|
|
|
|
|
using PN = Playnite;
|
2023-01-05 01:23:32 +00:00
|
|
|
|
|
|
|
|
|
namespace LANCommander.Playnite.Extension
|
|
|
|
|
{
|
|
|
|
|
public class PlayniteLibraryPlugin : LibraryPlugin
|
|
|
|
|
{
|
|
|
|
|
public static readonly ILogger Logger = LogManager.GetLogger();
|
2023-01-07 18:34:12 +00:00
|
|
|
|
internal PlayniteSettingsViewModel Settings { get; set; }
|
2023-01-05 07:07:17 +00:00
|
|
|
|
internal LANCommanderClient LANCommander { get; set; }
|
2023-01-05 01:23:32 +00:00
|
|
|
|
|
|
|
|
|
public override Guid Id { get; } = Guid.Parse("48e1bac7-e0a0-45d7-ba83-36f5e9e959fc");
|
|
|
|
|
public override string Name => "LANCommander";
|
|
|
|
|
public override LibraryClient Client { get; } = new PlayniteClient();
|
|
|
|
|
|
|
|
|
|
public PlayniteLibraryPlugin(IPlayniteAPI api) : base(api)
|
|
|
|
|
{
|
|
|
|
|
Properties = new LibraryPluginProperties
|
|
|
|
|
{
|
|
|
|
|
HasSettings = true,
|
|
|
|
|
};
|
2023-01-07 18:34:12 +00:00
|
|
|
|
|
|
|
|
|
LoadSettings();
|
2023-01-05 01:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<GameMetadata> GetGames(LibraryGetGamesArgs args)
|
|
|
|
|
{
|
2023-01-05 07:07:17 +00:00
|
|
|
|
try
|
2023-01-05 02:31:02 +00:00
|
|
|
|
{
|
2023-01-07 18:34:12 +00:00
|
|
|
|
var syncedGames = PlayniteApi.Database.Games;
|
2023-01-06 08:39:00 +00:00
|
|
|
|
|
|
|
|
|
var games = LANCommander
|
|
|
|
|
.GetGames()
|
|
|
|
|
.Where(g => g.Archives != null && g.Archives.Count() > 0)
|
|
|
|
|
.Select(g =>
|
|
|
|
|
{
|
|
|
|
|
return new GameMetadata()
|
|
|
|
|
{
|
|
|
|
|
IsInstalled = false,
|
|
|
|
|
Name = g.Title,
|
|
|
|
|
SortingName = g.SortTitle,
|
|
|
|
|
Description = g.Description,
|
|
|
|
|
GameId = g.Id.ToString(),
|
|
|
|
|
ReleaseDate = new ReleaseDate(g.ReleasedOn),
|
|
|
|
|
Version = g.Archives.OrderByDescending(a => a.CreatedOn).FirstOrDefault().Version,
|
|
|
|
|
};
|
|
|
|
|
});
|
2023-01-05 02:31:02 +00:00
|
|
|
|
|
2023-01-05 07:07:17 +00:00
|
|
|
|
return games;
|
|
|
|
|
}
|
2023-01-07 04:12:03 +00:00
|
|
|
|
catch (Exception ex)
|
2023-01-05 07:07:17 +00:00
|
|
|
|
{
|
|
|
|
|
return new List<GameMetadata>();
|
|
|
|
|
}
|
2023-01-05 01:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 04:12:03 +00:00
|
|
|
|
public override IEnumerable<InstallController> GetInstallActions(GetInstallActionsArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Game.PluginId != Id)
|
|
|
|
|
yield break;
|
|
|
|
|
|
|
|
|
|
yield return new LANCommanderInstallController(this, args.Game);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-05 01:23:32 +00:00
|
|
|
|
public override ISettings GetSettings(bool firstRunSettings)
|
|
|
|
|
{
|
2023-01-05 07:07:17 +00:00
|
|
|
|
return Settings;
|
2023-01-05 01:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override UserControl GetSettingsView(bool firstRunView)
|
|
|
|
|
{
|
2023-01-05 07:07:17 +00:00
|
|
|
|
return new PlayniteSettingsView(this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 18:34:12 +00:00
|
|
|
|
public void LoadSettings()
|
|
|
|
|
{
|
|
|
|
|
Settings = LoadPluginSettings<PlayniteSettingsViewModel>();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (LANCommander == null)
|
|
|
|
|
LANCommander = new LANCommanderClient(Settings.ServerAddress);
|
|
|
|
|
|
|
|
|
|
LANCommander.Client.BaseUrl = new Uri(Settings.ServerAddress);
|
|
|
|
|
|
|
|
|
|
var token = new SDK.Models.AuthToken()
|
|
|
|
|
{
|
|
|
|
|
AccessToken = Settings.AccessToken,
|
|
|
|
|
RefreshToken = Settings.RefreshToken,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LANCommander.Token = token;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SaveSettings()
|
|
|
|
|
{
|
|
|
|
|
SavePluginSettings(Settings);
|
|
|
|
|
|
|
|
|
|
if (LANCommander == null)
|
|
|
|
|
LANCommander = new LANCommanderClient(Settings.ServerAddress);
|
|
|
|
|
|
|
|
|
|
if (Settings.ServerAddress != LANCommander.Client.BaseUrl.ToString())
|
|
|
|
|
{
|
|
|
|
|
LANCommander.Client.BaseUrl = new Uri(Settings.ServerAddress);
|
|
|
|
|
|
|
|
|
|
var token = new SDK.Models.AuthToken()
|
|
|
|
|
{
|
|
|
|
|
AccessToken = Settings.AccessToken,
|
|
|
|
|
RefreshToken = Settings.RefreshToken,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LANCommander.Token = token;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 08:39:00 +00:00
|
|
|
|
public System.Windows.Window ShowAuthenticationWindow()
|
2023-01-05 07:07:17 +00:00
|
|
|
|
{
|
|
|
|
|
var window = PlayniteApi.Dialogs.CreateWindow(new WindowCreationOptions()
|
|
|
|
|
{
|
|
|
|
|
ShowMinimizeButton = false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
window.Title = "Authenticate to LANCommander";
|
|
|
|
|
|
2023-01-06 00:37:13 +00:00
|
|
|
|
window.Content = new Views.Authentication(this);
|
|
|
|
|
window.DataContext = new ViewModels.Authentication();
|
2023-01-05 07:07:17 +00:00
|
|
|
|
|
|
|
|
|
window.Owner = PlayniteApi.Dialogs.GetCurrentAppWindow();
|
|
|
|
|
window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
|
|
|
|
|
window.ShowDialog();
|
2023-01-06 08:39:00 +00:00
|
|
|
|
|
|
|
|
|
return window;
|
2023-01-05 01:23:32 +00:00
|
|
|
|
}
|
2023-01-07 18:34:12 +00:00
|
|
|
|
|
|
|
|
|
private GameMetadata ParseManifest(string installDirectory)
|
|
|
|
|
{
|
|
|
|
|
var manifestContents = File.ReadAllText(Path.Combine(installDirectory, "_manifest.yml"));
|
|
|
|
|
var deserializer = new DeserializerBuilder()
|
|
|
|
|
.WithNamingConvention(PascalCaseNamingConvention.Instance)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var manifest = deserializer.Deserialize<GameManifest>(manifestContents);
|
|
|
|
|
|
|
|
|
|
var metadata = new GameMetadata()
|
|
|
|
|
{
|
|
|
|
|
Name = manifest.Title,
|
|
|
|
|
SortingName = manifest.SortTitle,
|
|
|
|
|
Description = manifest.Description,
|
|
|
|
|
ReleaseDate = new ReleaseDate(manifest.ReleasedOn),
|
|
|
|
|
Version = manifest.Version,
|
|
|
|
|
GameActions = manifest.Actions.Select(a =>
|
|
|
|
|
{
|
|
|
|
|
return new PN.SDK.Models.GameAction()
|
|
|
|
|
{
|
|
|
|
|
Name = a.Name,
|
|
|
|
|
Arguments = a.Arguments,
|
|
|
|
|
Path = a.Path,
|
|
|
|
|
WorkingDir = a.WorkingDirectory,
|
|
|
|
|
IsPlayAction = a.IsPrimaryAction
|
|
|
|
|
};
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return metadata;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
throw new FileNotFoundException("The manifest file is invalid or corrupt.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-05 01:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|