2023-11-10 06:29:16 +00:00
|
|
|
|
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;
|
2023-11-12 07:27:15 +00:00
|
|
|
|
using System.Linq;
|
2023-01-07 04:12:03 +00:00
|
|
|
|
|
2023-01-08 18:09:57 +00:00
|
|
|
|
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();
|
|
|
|
|
|
2023-01-14 21:15:31 +00:00
|
|
|
|
private LANCommanderLibraryPlugin Plugin;
|
2023-01-07 04:12:03 +00:00
|
|
|
|
|
2023-01-15 10:29:47 +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...");
|
|
|
|
|
|
2023-03-17 07:07:25 +00:00
|
|
|
|
while (!Plugin.ValidateConnection())
|
|
|
|
|
{
|
2023-08-21 23:44:20 +00:00
|
|
|
|
Logger.Trace("User not authenticated. Opening auth window...");
|
|
|
|
|
|
2023-03-17 07:07:25 +00:00
|
|
|
|
Plugin.ShowAuthenticationWindow();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 04:12:03 +00:00
|
|
|
|
var gameId = Guid.Parse(Game.GameId);
|
|
|
|
|
|
2023-11-10 06:29:16 +00:00
|
|
|
|
string installDirectory = null;
|
2023-08-21 23:44:20 +00:00
|
|
|
|
|
2023-09-16 20:13:55 +00:00
|
|
|
|
var result = Plugin.PlayniteApi.Dialogs.ActivateGlobalProgress(progress =>
|
2023-04-08 01:08:03 +00:00
|
|
|
|
{
|
2023-11-11 03:36:35 +00:00
|
|
|
|
var gameManager = new GameManager(Plugin.LANCommanderClient, Plugin.Settings.InstallDirectory);
|
2023-10-25 00:11:50 +00:00
|
|
|
|
|
2023-11-10 06:29:16 +00:00
|
|
|
|
gameManager.OnArchiveExtractionProgress += (long pos, long len) =>
|
2023-10-25 00:11:50 +00:00
|
|
|
|
{
|
2023-11-12 07:26:51 +00:00
|
|
|
|
var percent = Math.Ceiling((pos / (decimal)len) * 100);
|
|
|
|
|
|
2023-11-10 06:29:16 +00:00
|
|
|
|
progress.ProgressMaxValue = len;
|
|
|
|
|
progress.CurrentProgressValue = pos;
|
2023-11-12 07:26:51 +00:00
|
|
|
|
progress.Text = $"Downloading {Game.Name} ({percent}%)";
|
2023-11-10 06:29:16 +00:00
|
|
|
|
};
|
2023-10-25 00:11:50 +00:00
|
|
|
|
|
2023-11-10 06:29:16 +00:00
|
|
|
|
gameManager.OnArchiveEntryExtractionProgress += (object sender, ArchiveEntryExtractionProgressArgs e) =>
|
2023-10-25 00:11:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (progress.CancelToken != null && progress.CancelToken.IsCancellationRequested)
|
|
|
|
|
{
|
2023-11-12 07:04:05 +00:00
|
|
|
|
gameManager.CancelInstall();
|
2023-10-25 00:11:50 +00:00
|
|
|
|
|
2023-11-10 06:29:16 +00:00
|
|
|
|
progress.IsIndeterminate = true;
|
2023-10-25 00:11:50 +00:00
|
|
|
|
}
|
2023-11-10 06:29:16 +00:00
|
|
|
|
};
|
2023-10-25 00:11:50 +00:00
|
|
|
|
|
2023-11-10 06:29:16 +00:00
|
|
|
|
installDirectory = gameManager.Install(gameId);
|
2023-10-25 00:11:50 +00:00
|
|
|
|
},
|
2023-11-12 07:26:51 +00:00
|
|
|
|
new GlobalProgressOptions($"Preparing to download {Game.Name}")
|
2023-10-25 00:11:50 +00:00
|
|
|
|
{
|
2023-11-11 03:44:48 +00:00
|
|
|
|
IsIndeterminate = false,
|
2023-10-25 00:11:50 +00:00
|
|
|
|
Cancelable = true,
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-12 07:27:15 +00:00
|
|
|
|
// Install any redistributables
|
|
|
|
|
var game = Plugin.LANCommanderClient.GetGame(gameId);
|
|
|
|
|
|
|
|
|
|
if (game.Redistributables != null && game.Redistributables.Count() > 0)
|
|
|
|
|
{
|
|
|
|
|
Plugin.PlayniteApi.Dialogs.ActivateGlobalProgress(progress =>
|
|
|
|
|
{
|
|
|
|
|
var redistributableManager = new RedistributableManager(Plugin.LANCommanderClient);
|
|
|
|
|
|
|
|
|
|
redistributableManager.Install(game);
|
|
|
|
|
},
|
|
|
|
|
new GlobalProgressOptions("Installing redistributables...")
|
|
|
|
|
{
|
|
|
|
|
IsIndeterminate = true,
|
|
|
|
|
Cancelable = false,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-10 06:29:16 +00:00
|
|
|
|
if (!result.Canceled && result.Error == null && !String.IsNullOrWhiteSpace(installDirectory))
|
2023-10-25 00:11:50 +00:00
|
|
|
|
{
|
2023-11-10 06:29:16 +00:00
|
|
|
|
var manifest = ManifestHelper.Read(installDirectory);
|
2023-01-07 04:12:03 +00:00
|
|
|
|
|
2023-11-10 06:29:16 +00:00
|
|
|
|
Plugin.UpdateGame(manifest);
|
2023-01-07 04:12:03 +00:00
|
|
|
|
|
2023-11-10 06:29:16 +00:00
|
|
|
|
var installInfo = new GameInstallationData
|
2023-01-07 04:12:03 +00:00
|
|
|
|
{
|
2023-11-10 06:29:16 +00:00
|
|
|
|
InstallDirectory = installDirectory,
|
|
|
|
|
};
|
2023-01-07 04:12:03 +00:00
|
|
|
|
|
2023-11-10 06:29:16 +00:00
|
|
|
|
InvokeOnInstalled(new GameInstalledEventArgs(installInfo));
|
2023-01-07 04:12:03 +00:00
|
|
|
|
}
|
2023-11-12 07:04:05 +00:00
|
|
|
|
else if (result.Canceled)
|
|
|
|
|
{
|
2023-11-12 07:27:15 +00:00
|
|
|
|
var dbGame = Plugin.PlayniteApi.Database.Games.Get(Game.Id);
|
2023-11-12 07:04:05 +00:00
|
|
|
|
|
2023-11-12 07:27:15 +00:00
|
|
|
|
dbGame.IsInstalling = false;
|
|
|
|
|
dbGame.IsInstalled = false;
|
2023-11-12 07:04:05 +00:00
|
|
|
|
|
2023-11-12 07:27:15 +00:00
|
|
|
|
Plugin.PlayniteApi.Database.Games.Update(dbGame);
|
2023-11-12 07:04:05 +00:00
|
|
|
|
}
|
|
|
|
|
else if (result.Error != null)
|
|
|
|
|
throw result.Error;
|
2023-01-15 10:29:47 +00:00
|
|
|
|
}
|
2023-01-07 04:12:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|