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-07 20:32:52 +00:00
|
|
|
|
var gameMetadata = new List<GameMetadata>();
|
|
|
|
|
|
2023-01-05 07:07:17 +00:00
|
|
|
|
try
|
2023-01-05 02:31:02 +00:00
|
|
|
|
{
|
2023-01-06 08:39:00 +00:00
|
|
|
|
var games = LANCommander
|
|
|
|
|
.GetGames()
|
2023-01-07 20:32:52 +00:00
|
|
|
|
.Where(g => g.Archives != null && g.Archives.Count() > 0);
|
|
|
|
|
|
|
|
|
|
foreach (var game in games)
|
|
|
|
|
{
|
|
|
|
|
var existingGame = PlayniteApi.Database.Games.FirstOrDefault(g => g.GameId == game.Id.ToString() && g.PluginId == Id && g.IsInstalled);
|
|
|
|
|
|
|
|
|
|
var metadata = new GameMetadata()
|
2023-01-06 08:39:00 +00:00
|
|
|
|
{
|
2023-01-07 20:32:52 +00:00
|
|
|
|
IsInstalled = existingGame != null,
|
|
|
|
|
Name = game.Title,
|
|
|
|
|
SortingName = game.SortTitle,
|
|
|
|
|
Description = game.Description,
|
|
|
|
|
GameId = game.Id.ToString(),
|
|
|
|
|
ReleaseDate = new ReleaseDate(game.ReleasedOn),
|
|
|
|
|
Version = game.Archives.OrderByDescending(a => a.CreatedOn).FirstOrDefault().Version,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
gameMetadata.Add(metadata);
|
|
|
|
|
};
|
2023-01-05 07:07:17 +00:00
|
|
|
|
}
|
2023-01-07 04:12:03 +00:00
|
|
|
|
catch (Exception ex)
|
2023-01-05 07:07:17 +00:00
|
|
|
|
{
|
2023-01-07 20:32:52 +00:00
|
|
|
|
|
2023-01-05 07:07:17 +00:00
|
|
|
|
}
|
2023-01-07 20:32:52 +00:00
|
|
|
|
|
|
|
|
|
return 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-07 18:46:26 +00:00
|
|
|
|
public override IEnumerable<UninstallController> GetUninstallActions(GetUninstallActionsArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Game.PluginId != Id)
|
|
|
|
|
yield break;
|
|
|
|
|
|
|
|
|
|
yield return new LANCommanderUninstallController(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
|
|
|
|
|
2023-01-07 20:32:52 +00:00
|
|
|
|
public void UpdateGamesFromManifest()
|
2023-01-07 18:34:12 +00:00
|
|
|
|
{
|
2023-01-07 20:32:52 +00:00
|
|
|
|
var games = PlayniteApi.Database.Games;
|
2023-01-07 18:34:12 +00:00
|
|
|
|
|
2023-01-07 20:32:52 +00:00
|
|
|
|
foreach (var game in games.Where(g => g.PluginId == Id && g.IsInstalled))
|
2023-01-07 18:34:12 +00:00
|
|
|
|
{
|
2023-01-07 20:32:52 +00:00
|
|
|
|
if (!Directory.Exists(game.InstallDirectory))
|
|
|
|
|
continue;
|
2023-01-07 18:34:12 +00:00
|
|
|
|
|
2023-01-07 20:32:52 +00:00
|
|
|
|
var manifestPath = Path.Combine(game.InstallDirectory, "_manifest.yml");
|
|
|
|
|
|
|
|
|
|
if (File.Exists(manifestPath))
|
2023-01-07 18:34:12 +00:00
|
|
|
|
{
|
2023-01-07 20:32:52 +00:00
|
|
|
|
try
|
2023-01-07 18:34:12 +00:00
|
|
|
|
{
|
2023-01-07 20:32:52 +00:00
|
|
|
|
var manifestContents = File.ReadAllText(manifestPath);
|
|
|
|
|
var deserializer = new DeserializerBuilder()
|
|
|
|
|
.WithNamingConvention(PascalCaseNamingConvention.Instance)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
var manifest = deserializer.Deserialize<GameManifest>(manifestContents);
|
|
|
|
|
|
|
|
|
|
if (game.GameActions == null)
|
|
|
|
|
game.GameActions = new System.Collections.ObjectModel.ObservableCollection<PN.SDK.Models.GameAction>();
|
|
|
|
|
|
|
|
|
|
foreach (var action in manifest.Actions)
|
2023-01-07 18:34:12 +00:00
|
|
|
|
{
|
2023-01-07 20:32:52 +00:00
|
|
|
|
bool isFirstAction = !manifest.Actions.Any(a => a.IsPrimaryAction) && manifest.Actions.First().Name == action.Name;
|
|
|
|
|
|
|
|
|
|
game.GameActions.AddMissing(new PN.SDK.Models.GameAction()
|
|
|
|
|
{
|
|
|
|
|
Name = action.Name,
|
|
|
|
|
Arguments = action.Arguments,
|
|
|
|
|
Path = PlayniteApi.ExpandGameVariables(game, action.Path),
|
|
|
|
|
WorkingDir = action.WorkingDirectory ?? game.InstallDirectory,
|
|
|
|
|
IsPlayAction = action.IsPrimaryAction || isFirstAction
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PlayniteApi.Database.Games.Update(game);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-01-07 18:34:12 +00:00
|
|
|
|
|
2023-01-07 20:32:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-07 18:34:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-05 01:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|