LANCommander/LANCommander.Playnite.Exten.../InstallController.cs

96 lines
3.2 KiB
C#
Raw Normal View History

using LANCommander.SDK;
using LANCommander.SDK.Helpers;
2023-04-08 00:09:00 +00:00
using LANCommander.SDK.Models;
using Playnite.SDK;
using Playnite.SDK.Models;
using Playnite.SDK.Plugins;
2023-01-07 04:12:03 +00:00
using System;
namespace LANCommander.PlaynitePlugin
2023-01-07 04:12:03 +00:00
{
public class LANCommanderInstallController : InstallController
{
2023-08-21 23:44:20 +00:00
public static readonly ILogger Logger = LogManager.GetLogger();
private LANCommanderLibraryPlugin Plugin;
2023-01-07 04:12:03 +00:00
public LANCommanderInstallController(LANCommanderLibraryPlugin plugin, Playnite.SDK.Models.Game game) : base(game)
2023-01-07 04:12:03 +00:00
{
Name = "Install using LANCommander";
Plugin = plugin;
}
public override void Install(InstallActionArgs args)
{
2023-08-21 23:44:20 +00:00
Logger.Trace("Game install triggered, checking connection...");
while (!Plugin.ValidateConnection())
{
2023-08-21 23:44:20 +00:00
Logger.Trace("User not authenticated. Opening auth window...");
Plugin.ShowAuthenticationWindow();
}
2023-01-07 04:12:03 +00:00
var gameId = Guid.Parse(Game.GameId);
string installDirectory = null;
2023-08-21 23:44:20 +00:00
var result = Plugin.PlayniteApi.Dialogs.ActivateGlobalProgress(progress =>
2023-04-08 01:08:03 +00:00
{
var gameManager = new GameManager(Plugin.LANCommanderClient, Plugin.Settings.InstallDirectory);
gameManager.OnArchiveExtractionProgress += (long pos, long len) =>
{
2023-11-12 07:26:51 +00:00
var percent = Math.Ceiling((pos / (decimal)len) * 100);
progress.ProgressMaxValue = len;
progress.CurrentProgressValue = pos;
2023-11-12 07:26:51 +00:00
progress.Text = $"Downloading {Game.Name} ({percent}%)";
};
gameManager.OnArchiveEntryExtractionProgress += (object sender, ArchiveEntryExtractionProgressArgs e) =>
{
if (progress.CancelToken != null && progress.CancelToken.IsCancellationRequested)
{
gameManager.CancelInstall();
progress.IsIndeterminate = true;
}
};
installDirectory = gameManager.Install(gameId);
},
2023-11-12 07:26:51 +00:00
new GlobalProgressOptions($"Preparing to download {Game.Name}")
{
2023-11-11 03:44:48 +00:00
IsIndeterminate = false,
Cancelable = true,
});
if (!result.Canceled && result.Error == null && !String.IsNullOrWhiteSpace(installDirectory))
{
var manifest = ManifestHelper.Read(installDirectory);
2023-01-07 04:12:03 +00:00
Plugin.UpdateGame(manifest);
2023-01-07 04:12:03 +00:00
var installInfo = new GameInstallationData
2023-01-07 04:12:03 +00:00
{
InstallDirectory = installDirectory,
};
2023-01-07 04:12:03 +00:00
InvokeOnInstalled(new GameInstalledEventArgs(installInfo));
2023-01-07 04:12:03 +00:00
}
else if (result.Canceled)
{
var game = Plugin.PlayniteApi.Database.Games.Get(Game.Id);
game.IsInstalling = false;
game.IsInstalled = false;
Plugin.PlayniteApi.Database.Games.Update(game);
}
else if (result.Error != null)
throw result.Error;
}
2023-01-07 04:12:03 +00:00
}
}