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; static private MinecraftPath? _mcPath; static private MinecraftLauncher? _mcLauncher; public int DownloadProgress; public string? DownloadStatus; public Launcher(string version, string? location = null) { _version = version; _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; } public MSession CreateOfflineSession(string nickname) { var s = MSession.CreateOfflineSession(nickname); if (s is null) throw new NullReferenceException("Tried creating offline session, but it's resulted in null!"); return s; } public MSession CreateLicensedSession(string uuid, string accsessToken, string nickname) { var s = new MSession(nickname, accsessToken, uuid); if (s is null) throw new NullReferenceException("Tried creating licensed session, but it's resulted in null!"); return s; } async public Task RunLauncher(MSession session) { if (_mcLauncher is null) throw new NullReferenceException("Failed to run launcher! Launcher is null!"); var process = await _mcLauncher.BuildProcessAsync(_version, new MLaunchOption { // Passing session, to support offline and licensed login methods. Session = session, 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(); } }