75 lines
2.2 KiB
C#

using System.Formats.Tar;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using CmlLib.Core;
using CmlLib.Core.Auth;
using CmlLib.Core.Auth.Microsoft;
using CmlLib.Core.ProcessBuilder;
using XboxAuthNet.Game.Msal;
namespace Plombir;
/*
Basically a class to manage everything launcher related.
TODO - minecraft downloading from webserver
*/
public class Launcher
{
private readonly string _version;
private readonly string _nickname;
static private MinecraftPath? _mcPath;
static private MinecraftLauncher? _mcLauncher;
public int DownloadProgress;
public string? DownloadStatus;
public Launcher(string userName, string version, string? location = null)
{
_version = version;
_nickname = userName;
_mcPath = new($"{location}/{version}/minecraft");
_mcLauncher = new(_mcPath);
}
async public Task BuildLauncher(CancellationToken token)
{
if (_mcLauncher is null)
throw new NullReferenceException("Failed to build! Launcher is null!");
_mcLauncher.ByteProgressChanged += (_, args) =>
{
Console.WriteLine($"{(int)(args.ProgressedBytes * 0.000001)} MBytes / {(int)(args.TotalBytes * 0.000001)} MBytes");
DownloadProgress = (int)(args.ProgressedBytes * 100 / args.TotalBytes);
};
DownloadStatus = "Getting the assets from Mojang...";
Console.WriteLine(DownloadStatus);
await _mcLauncher.InstallAsync(_version);
DownloadStatus = "Finished!";
Console.WriteLine(DownloadStatus);
return;
}
async public Task RunLauncher()
{
if (_mcLauncher is null)
throw new NullReferenceException("Failed to run launcher! Launcher is null!");
var process = await _mcLauncher.BuildProcessAsync(_version, new MLaunchOption
{
Session = MSession.CreateOfflineSession(_nickname),
MaximumRamMb = 4096,
}) ?? throw new Exception("Failed to start minecraft process!");
var processUtil = new ProcessWrapper(process);
processUtil.OutputReceived += (s, e) => Console.WriteLine(e);
processUtil.StartWithEvents();
await processUtil.WaitForExitTaskAsync();
}
}