2023-04-08 00:09:00 +00:00
|
|
|
|
using LANCommander.PlaynitePlugin.Helpers;
|
2023-01-15 10:29:47 +00:00
|
|
|
|
using LANCommander.SDK.Enums;
|
2023-01-07 04:12:03 +00:00
|
|
|
|
using LANCommander.SDK.Extensions;
|
2023-04-08 00:09:00 +00:00
|
|
|
|
using LANCommander.SDK.Models;
|
|
|
|
|
using Playnite.SDK;
|
|
|
|
|
using Playnite.SDK.Models;
|
|
|
|
|
using Playnite.SDK.Plugins;
|
|
|
|
|
using SharpCompress.Common;
|
|
|
|
|
using SharpCompress.Readers;
|
2023-01-07 04:12:03 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2023-01-14 21:09:45 +00:00
|
|
|
|
using YamlDotNet.Serialization;
|
|
|
|
|
using YamlDotNet.Serialization.NamingConventions;
|
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-01-14 21:15:31 +00:00
|
|
|
|
private LANCommanderLibraryPlugin Plugin;
|
2023-01-15 07:10:36 +00:00
|
|
|
|
private PowerShellRuntime PowerShellRuntime;
|
2023-01-15 10:29:47 +00:00
|
|
|
|
private Playnite.SDK.Models.Game PlayniteGame;
|
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;
|
2023-01-15 07:10:36 +00:00
|
|
|
|
PlayniteGame = game;
|
|
|
|
|
PowerShellRuntime = new PowerShellRuntime();
|
2023-01-07 04:12:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Install(InstallActionArgs args)
|
|
|
|
|
{
|
2023-03-17 07:07:25 +00:00
|
|
|
|
while (!Plugin.ValidateConnection())
|
|
|
|
|
{
|
|
|
|
|
Plugin.ShowAuthenticationWindow();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 04:12:03 +00:00
|
|
|
|
var gameId = Guid.Parse(Game.GameId);
|
|
|
|
|
var game = Plugin.LANCommander.GetGame(gameId);
|
|
|
|
|
|
2023-03-24 00:16:52 +00:00
|
|
|
|
var installDirectory = RetryHelper.RetryOnException(10, TimeSpan.FromMilliseconds(500), "", () =>
|
|
|
|
|
{
|
2023-04-08 01:08:03 +00:00
|
|
|
|
return DownloadAndExtract(game);
|
2023-03-24 00:16:52 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (installDirectory == "")
|
|
|
|
|
throw new Exception("Could not extract the install archive. Retry the install or check your connection.");
|
2023-01-15 07:10:36 +00:00
|
|
|
|
|
2023-01-07 04:39:56 +00:00
|
|
|
|
var installInfo = new GameInstallationData()
|
|
|
|
|
{
|
|
|
|
|
InstallDirectory = installDirectory
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-15 07:10:36 +00:00
|
|
|
|
PlayniteGame.InstallDirectory = installDirectory;
|
|
|
|
|
|
2023-03-20 23:37:12 +00:00
|
|
|
|
SDK.GameManifest manifest = null;
|
|
|
|
|
|
|
|
|
|
var writeManifestSuccess = RetryHelper.RetryOnException(10, TimeSpan.FromSeconds(1), false, () =>
|
|
|
|
|
{
|
|
|
|
|
manifest = Plugin.LANCommander.GetGameManifest(gameId);
|
|
|
|
|
|
|
|
|
|
WriteManifest(manifest, installDirectory);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!writeManifestSuccess)
|
|
|
|
|
throw new Exception("Could not get or write the manifest file. Retry the install or check your connection.");
|
2023-01-14 21:09:45 +00:00
|
|
|
|
|
2023-01-15 10:29:47 +00:00
|
|
|
|
SaveScript(game, installDirectory, ScriptType.Install);
|
|
|
|
|
SaveScript(game, installDirectory, ScriptType.Uninstall);
|
|
|
|
|
SaveScript(game, installDirectory, ScriptType.NameChange);
|
|
|
|
|
SaveScript(game, installDirectory, ScriptType.KeyChange);
|
|
|
|
|
|
2023-01-15 07:19:03 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-01-15 10:29:47 +00:00
|
|
|
|
PowerShellRuntime.RunScript(PlayniteGame, ScriptType.Install);
|
2023-01-20 02:03:16 +00:00
|
|
|
|
PowerShellRuntime.RunScript(PlayniteGame, ScriptType.NameChange, Plugin.Settings.PlayerName);
|
2023-01-28 23:18:41 +00:00
|
|
|
|
|
|
|
|
|
var key = Plugin.LANCommander.GetAllocatedKey(game.Id);
|
|
|
|
|
|
|
|
|
|
PowerShellRuntime.RunScript(PlayniteGame, ScriptType.KeyChange, $"\"{key}\"");
|
2023-01-15 07:19:03 +00:00
|
|
|
|
}
|
|
|
|
|
catch { }
|
2023-01-15 07:10:36 +00:00
|
|
|
|
|
2023-01-17 05:45:46 +00:00
|
|
|
|
Plugin.UpdateGame(manifest, gameId);
|
2023-01-07 20:32:52 +00:00
|
|
|
|
|
2023-03-20 23:37:12 +00:00
|
|
|
|
Plugin.DownloadCache.Remove(gameId);
|
|
|
|
|
|
2023-01-17 05:45:46 +00:00
|
|
|
|
InvokeOnInstalled(new GameInstalledEventArgs(installInfo));
|
2023-01-07 04:12:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 01:08:03 +00:00
|
|
|
|
private string DownloadAndExtract(LANCommander.SDK.Models.Game game)
|
|
|
|
|
{
|
|
|
|
|
if (game == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Game failed to download!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var destination = Path.Combine(Plugin.Settings.InstallDirectory, game.Title.SanitizeFilename());
|
|
|
|
|
|
|
|
|
|
Plugin.PlayniteApi.Dialogs.ActivateGlobalProgress(progress =>
|
|
|
|
|
{
|
2023-04-08 20:47:01 +00:00
|
|
|
|
try
|
2023-04-08 01:08:03 +00:00
|
|
|
|
{
|
2023-04-08 20:47:01 +00:00
|
|
|
|
Directory.CreateDirectory(destination);
|
|
|
|
|
progress.ProgressMaxValue = 100;
|
|
|
|
|
progress.CurrentProgressValue = 0;
|
2023-04-08 01:08:03 +00:00
|
|
|
|
|
2023-04-08 20:47:01 +00:00
|
|
|
|
using (var gameStream = Plugin.LANCommander.StreamGame(game.Id))
|
|
|
|
|
using (var reader = ReaderFactory.Open(gameStream))
|
2023-04-08 01:08:03 +00:00
|
|
|
|
{
|
2023-04-08 20:47:01 +00:00
|
|
|
|
progress.ProgressMaxValue = gameStream.Length;
|
|
|
|
|
|
|
|
|
|
gameStream.OnProgress += (pos, len) =>
|
|
|
|
|
{
|
|
|
|
|
progress.CurrentProgressValue = pos;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
reader.WriteAllToDirectory(destination, new ExtractionOptions()
|
|
|
|
|
{
|
|
|
|
|
ExtractFullPath = true,
|
|
|
|
|
Overwrite = true
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if (Directory.Exists(destination))
|
2023-04-08 01:08:03 +00:00
|
|
|
|
{
|
2023-04-08 20:47:01 +00:00
|
|
|
|
Directory.Delete(destination, true);
|
|
|
|
|
}
|
2023-04-08 01:08:03 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
new GlobalProgressOptions($"Downloading {game.Title}...")
|
|
|
|
|
{
|
|
|
|
|
IsIndeterminate = false,
|
|
|
|
|
Cancelable = false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return destination;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 04:12:03 +00:00
|
|
|
|
private string Download(LANCommander.SDK.Models.Game game)
|
|
|
|
|
{
|
|
|
|
|
string tempFile = String.Empty;
|
|
|
|
|
|
2023-01-14 21:09:45 +00:00
|
|
|
|
if (game != null)
|
2023-01-07 04:12:03 +00:00
|
|
|
|
{
|
2023-01-07 04:39:56 +00:00
|
|
|
|
Plugin.PlayniteApi.Dialogs.ActivateGlobalProgress(progress =>
|
2023-01-07 04:12:03 +00:00
|
|
|
|
{
|
|
|
|
|
progress.ProgressMaxValue = 100;
|
|
|
|
|
progress.CurrentProgressValue = 0;
|
|
|
|
|
|
2023-01-14 21:09:45 +00:00
|
|
|
|
var destination = Plugin.LANCommander.DownloadGame(game.Id, (changed) =>
|
2023-01-07 04:12:03 +00:00
|
|
|
|
{
|
|
|
|
|
progress.CurrentProgressValue = changed.ProgressPercentage;
|
|
|
|
|
}, (complete) =>
|
|
|
|
|
{
|
|
|
|
|
progress.CurrentProgressValue = 100;
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-07 04:39:56 +00:00
|
|
|
|
// Lock the thread until download is done
|
2023-01-07 04:12:03 +00:00
|
|
|
|
while (progress.CurrentProgressValue != 100)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tempFile = destination;
|
|
|
|
|
},
|
2023-01-07 04:39:56 +00:00
|
|
|
|
new GlobalProgressOptions($"Downloading {game.Title}...")
|
2023-01-07 04:12:03 +00:00
|
|
|
|
{
|
|
|
|
|
IsIndeterminate = false,
|
|
|
|
|
Cancelable = false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return tempFile;
|
|
|
|
|
}
|
|
|
|
|
else
|
2023-01-14 21:09:45 +00:00
|
|
|
|
throw new Exception("Game failed to download!");
|
2023-01-07 04:12:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 04:39:56 +00:00
|
|
|
|
private string Extract(LANCommander.SDK.Models.Game game, string archivePath)
|
2023-01-07 04:12:03 +00:00
|
|
|
|
{
|
2023-01-12 02:48:39 +00:00
|
|
|
|
var destination = Path.Combine(Plugin.Settings.InstallDirectory, game.Title.SanitizeFilename());
|
2023-01-07 04:12:03 +00:00
|
|
|
|
|
2023-01-07 04:39:56 +00:00
|
|
|
|
Plugin.PlayniteApi.Dialogs.ActivateGlobalProgress(progress =>
|
2023-01-07 04:12:03 +00:00
|
|
|
|
{
|
2023-04-08 00:09:00 +00:00
|
|
|
|
Directory.CreateDirectory(destination);
|
2023-01-07 04:12:03 +00:00
|
|
|
|
|
2023-04-08 00:09:00 +00:00
|
|
|
|
using (var fs = File.OpenRead(archivePath))
|
|
|
|
|
using (var ts = new TrackableStream(fs))
|
|
|
|
|
using (var reader = ReaderFactory.Open(ts))
|
2023-01-07 04:12:03 +00:00
|
|
|
|
{
|
2023-04-08 00:09:00 +00:00
|
|
|
|
progress.ProgressMaxValue = ts.Length;
|
|
|
|
|
ts.OnProgress += (pos, len) =>
|
2023-01-07 04:12:03 +00:00
|
|
|
|
{
|
2023-04-08 00:09:00 +00:00
|
|
|
|
progress.CurrentProgressValue = pos;
|
|
|
|
|
};
|
2023-01-07 04:39:56 +00:00
|
|
|
|
|
2023-04-08 00:09:00 +00:00
|
|
|
|
reader.WriteAllToDirectory(destination, new ExtractionOptions()
|
2023-01-07 04:39:56 +00:00
|
|
|
|
{
|
2023-04-08 00:09:00 +00:00
|
|
|
|
ExtractFullPath = true,
|
|
|
|
|
Overwrite = true
|
|
|
|
|
});
|
2023-01-07 04:12:03 +00:00
|
|
|
|
}
|
2023-01-07 04:39:56 +00:00
|
|
|
|
},
|
|
|
|
|
new GlobalProgressOptions($"Extracting {game.Title}...")
|
|
|
|
|
{
|
|
|
|
|
IsIndeterminate = false,
|
|
|
|
|
Cancelable = false,
|
|
|
|
|
});
|
2023-01-07 04:12:03 +00:00
|
|
|
|
|
2023-01-07 04:39:56 +00:00
|
|
|
|
return destination;
|
2023-01-07 04:12:03 +00:00
|
|
|
|
}
|
2023-01-14 21:09:45 +00:00
|
|
|
|
|
2023-01-17 05:45:46 +00:00
|
|
|
|
private void WriteManifest(SDK.GameManifest manifest, string installDirectory)
|
2023-01-14 21:09:45 +00:00
|
|
|
|
{
|
|
|
|
|
var serializer = new SerializerBuilder()
|
2023-03-22 22:30:25 +00:00
|
|
|
|
.WithNamingConvention(new PascalCaseNamingConvention())
|
2023-01-14 21:09:45 +00:00
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
var yaml = serializer.Serialize(manifest);
|
|
|
|
|
|
2023-01-17 05:45:46 +00:00
|
|
|
|
File.WriteAllText(Path.Combine(installDirectory, "_manifest.yml"), yaml);
|
2023-01-14 21:09:45 +00:00
|
|
|
|
}
|
2023-01-15 10:29:47 +00:00
|
|
|
|
|
|
|
|
|
private void SaveScript(LANCommander.SDK.Models.Game game, string installationDirectory, ScriptType type)
|
|
|
|
|
{
|
|
|
|
|
var script = game.Scripts.FirstOrDefault(s => s.Type == type);
|
|
|
|
|
|
|
|
|
|
if (script == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (script.RequiresAdmin)
|
|
|
|
|
script.Contents = "# Requires Admin" + "\r\n\r\n" + script.Contents;
|
|
|
|
|
|
2023-01-15 10:56:56 +00:00
|
|
|
|
var filename = PowerShellRuntime.GetScriptFilePath(PlayniteGame, type);
|
2023-01-15 10:29:47 +00:00
|
|
|
|
|
|
|
|
|
if (File.Exists(filename))
|
|
|
|
|
File.Delete(filename);
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(filename, script.Contents);
|
|
|
|
|
}
|
2023-01-07 04:12:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|