2023-01-07 04:12:03 +00:00
|
|
|
|
using Playnite.SDK;
|
|
|
|
|
using Playnite.SDK.Models;
|
|
|
|
|
using Playnite.SDK.Plugins;
|
|
|
|
|
using LANCommander.SDK.Extensions;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using ICSharpCode.SharpZipLib.Zip;
|
|
|
|
|
using ICSharpCode.SharpZipLib.Core;
|
|
|
|
|
|
|
|
|
|
namespace LANCommander.Playnite.Extension
|
|
|
|
|
{
|
|
|
|
|
public class LANCommanderInstallController : InstallController
|
|
|
|
|
{
|
|
|
|
|
private PlayniteLibraryPlugin Plugin;
|
|
|
|
|
|
|
|
|
|
public LANCommanderInstallController(PlayniteLibraryPlugin plugin, Game game) : base(game)
|
|
|
|
|
{
|
|
|
|
|
Name = "Install using LANCommander";
|
|
|
|
|
Plugin = plugin;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Install(InstallActionArgs args)
|
|
|
|
|
{
|
|
|
|
|
var tempPath = System.IO.Path.GetTempFileName();
|
|
|
|
|
var gameId = Guid.Parse(Game.GameId);
|
|
|
|
|
|
|
|
|
|
var game = Plugin.LANCommander.GetGame(gameId);
|
|
|
|
|
|
|
|
|
|
var tempFile = Download(game);
|
|
|
|
|
|
2023-01-07 04:39:56 +00:00
|
|
|
|
var installDirectory = Extract(game, tempFile);
|
|
|
|
|
var installInfo = new GameInstallationData()
|
|
|
|
|
{
|
|
|
|
|
InstallDirectory = installDirectory
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
InvokeOnInstalled(new GameInstalledEventArgs(installInfo));
|
2023-01-07 20:32:52 +00:00
|
|
|
|
|
|
|
|
|
Plugin.UpdateGamesFromManifest();
|
2023-01-07 04:12:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string Download(LANCommander.SDK.Models.Game game)
|
|
|
|
|
{
|
|
|
|
|
string tempFile = String.Empty;
|
|
|
|
|
|
|
|
|
|
var archive = game.Archives.OrderByDescending(a => a.CreatedOn).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (archive != null)
|
|
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
|
|
var destination = Plugin.LANCommander.DownloadArchive(archive.Id, (changed) =>
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
throw new Exception("Game failed to download");
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
var destination = $"C:\\Games\\{game.Title.SanitizeFilename()}";
|
|
|
|
|
|
2023-01-07 04:39:56 +00:00
|
|
|
|
Plugin.PlayniteApi.Dialogs.ActivateGlobalProgress(progress =>
|
2023-01-07 04:12:03 +00:00
|
|
|
|
{
|
2023-01-07 04:39:56 +00:00
|
|
|
|
ZipFile file = null;
|
2023-01-07 04:12:03 +00:00
|
|
|
|
|
2023-01-07 04:39:56 +00:00
|
|
|
|
try
|
2023-01-07 04:12:03 +00:00
|
|
|
|
{
|
2023-01-07 04:39:56 +00:00
|
|
|
|
FileStream fs = File.OpenRead(archivePath);
|
2023-01-07 04:12:03 +00:00
|
|
|
|
|
2023-01-07 04:39:56 +00:00
|
|
|
|
file = new ZipFile(fs);
|
2023-01-07 04:12:03 +00:00
|
|
|
|
|
2023-01-07 04:39:56 +00:00
|
|
|
|
progress.ProgressMaxValue = file.Count;
|
2023-01-07 04:12:03 +00:00
|
|
|
|
|
2023-01-07 04:39:56 +00:00
|
|
|
|
foreach (ZipEntry entry in file)
|
2023-01-07 04:12:03 +00:00
|
|
|
|
{
|
2023-01-07 04:39:56 +00:00
|
|
|
|
if (!entry.IsFile)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
byte[] buffer = new byte[4096];
|
|
|
|
|
var zipStream = file.GetInputStream(entry);
|
|
|
|
|
|
|
|
|
|
var entryDestination = Path.Combine(destination, entry.Name);
|
|
|
|
|
var entryDirectory = Path.GetDirectoryName(entryDestination);
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrWhiteSpace(entryDirectory))
|
|
|
|
|
Directory.CreateDirectory(entryDirectory);
|
|
|
|
|
|
|
|
|
|
using (FileStream streamWriter = File.Create(entryDestination))
|
|
|
|
|
{
|
|
|
|
|
StreamUtils.Copy(zipStream, streamWriter, buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
progress.CurrentProgressValue = entry.ZipFileIndex;
|
2023-01-07 04:12:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-07 04:39:56 +00:00
|
|
|
|
finally
|
2023-01-07 04:12:03 +00:00
|
|
|
|
{
|
2023-01-07 04:39:56 +00:00
|
|
|
|
if (file != null)
|
|
|
|
|
{
|
|
|
|
|
file.IsStreamOwner = true;
|
|
|
|
|
file.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.Delete(archivePath);
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|