Skip to content

Commit

Permalink
update CmlLibCoreSample
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaBs committed May 25, 2021
1 parent b7c4059 commit b3b9bef
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 65 deletions.
99 changes: 34 additions & 65 deletions CmlLibCoreSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace CmlLibCoreSample
{
class Program
{

static void Main()
{
Console.WriteLine(CmlLib._Test.tstr);
Expand All @@ -26,13 +25,13 @@ static void Main()
// Choose one which you want.
//session = p.PremiumLogin(); // Login by mojang email and password
session = p.OfflineLogin(); // Login by username
//session = p.XboxLogin(); // Check XboxLoginTest project

// log login session information
Console.WriteLine("Success to login : {0} / {1} / {2}", session.Username, session.UUID, session.AccessToken);

// Launch
p.Start(session).GetAwaiter().GetResult();
//p.Start(session);
p.StartAsync(session).GetAwaiter().GetResult();
}

MSession PremiumLogin()
Expand Down Expand Up @@ -75,7 +74,7 @@ MSession OfflineLogin()
return MSession.GetOfflineSession("tester123");
}

async Task Start(MSession session)
void Start(MSession session)
{
// Initializing Launcher

Expand All @@ -100,7 +99,7 @@ async Task Start(MSession session)
Console.WriteLine($"Initialized in {launcher.MinecraftPath.BasePath}");

// Get all installed profiles and load all profiles from mojang server
var versions = await launcher.GetAllVersionsAsync();
var versions = launcher.GetAllVersions();

foreach (var item in versions) // Display all profiles
{
Expand Down Expand Up @@ -132,7 +131,7 @@ async Task Start(MSession session)
// var process = await launcher.CreateProcessAsync("fabric-loader-0.11.3-1.16.5") // fabric-loader

Console.WriteLine("input version (example: 1.12.2) : ");
var process = await launcher.CreateProcessAsync(Console.ReadLine(), launchOption);
var process = launcher.CreateProcess(Console.ReadLine(), launchOption);

//var process = launcher.CreateProcess("1.16.2", "33.0.5", launchOption);
Console.WriteLine(process.StartInfo.Arguments);
Expand All @@ -150,6 +149,34 @@ async Task Start(MSession session)
return;
}

async Task StartAsync(MSession session) // async version
{
var path = new MinecraftPath();
var launcher = new CMLauncher(path);

System.Net.ServicePointManager.DefaultConnectionLimit = 256;

var versions = await launcher.GetAllVersionsAsync();
foreach (var item in versions)
{
Console.WriteLine(item.Type + " " + item.Name);
}

launcher.FileChanged += Downloader_ChangeFile;
launcher.ProgressChanged += Downloader_ChangeProgress;

Console.WriteLine("input version (example: 1.12.2) : ");
var versionName = Console.ReadLine();
var process = await launcher.CreateProcessAsync(versionName, new MLaunchOption
{
Session = session,
MaximumRamMb = 1024
});

Console.WriteLine(process.StartInfo.Arguments);
process.Start();
}

#region QuickStart

// this code is from README.md
Expand Down Expand Up @@ -193,68 +220,10 @@ async Task QuickStart()

#endregion

#region TestCode

async Task TestAll(MSession session)
{
var path = MinecraftPath.GetOSDefaultPath();
var game = new MinecraftPath(path);

var launcher = new CMLauncher(game);

System.Net.ServicePointManager.DefaultConnectionLimit = 256;
launcher.FileDownloader = new AsyncParallelDownloader();

launcher.ProgressChanged += Downloader_ChangeProgress;
launcher.FileChanged += Downloader_ChangeFile;

Console.WriteLine($"Initialized in {launcher.MinecraftPath.BasePath}");

var launchOption = new MLaunchOption
{
MaximumRamMb = 1024,
Session = session,
};

var versions = await launcher.GetAllVersionsAsync();
foreach (var item in versions)
{
Console.WriteLine(item.Type + " " + item.Name);

if (!item.IsLocalVersion)
continue;

var process = launcher.CreateProcess(item.Name, launchOption);

//var process = launcher.CreateProcess("1.16.2", "33.0.5", launchOption);
Console.WriteLine(process.StartInfo.Arguments);

// Below codes are print game logs in Console.
var processUtil = new CmlLib.Utils.ProcessUtil(process);
processUtil.OutputReceived += (s, e) => Console.WriteLine(e);
processUtil.StartWithEvents();

Thread.Sleep(1000 * 15);

if (process.HasExited)
{
Console.WriteLine("FAILED!!!!!!!!!");
Console.ReadLine();
}

process.Kill();
process.WaitForExit();
}

return;
}

#endregion

// Event Handling

// The code below has some tricks to display logs prettier.
// You can use a simpler event handler
// You can also use a simpler event handler

#region Pretty event handler

Expand Down
113 changes: 113 additions & 0 deletions CmlLibCoreSample/Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using CmlLib.Core;
using CmlLib.Core.Auth;
using CmlLib.Core.Downloader;

namespace CmlLibCoreSample
{
public class Test
{
async Task TestAll(MSession session)
{
var path = MinecraftPath.GetOSDefaultPath();
var game = new MinecraftPath(path);

var launcher = new CMLauncher(game);

System.Net.ServicePointManager.DefaultConnectionLimit = 256;
launcher.FileDownloader = new AsyncParallelDownloader();

launcher.ProgressChanged += Downloader_ChangeProgress;
launcher.FileChanged += Downloader_ChangeFile;

Console.WriteLine($"Initialized in {launcher.MinecraftPath.BasePath}");

var launchOption = new MLaunchOption
{
MaximumRamMb = 1024,
Session = session,
};

var versions = await launcher.GetAllVersionsAsync();
foreach (var item in versions)
{
Console.WriteLine(item.Type + " " + item.Name);

if (!item.IsLocalVersion)
continue;

var process = launcher.CreateProcess(item.Name, launchOption);

//var process = launcher.CreateProcess("1.16.2", "33.0.5", launchOption);
Console.WriteLine(process.StartInfo.Arguments);

// Below codes are print game logs in Console.
var processUtil = new CmlLib.Utils.ProcessUtil(process);
processUtil.OutputReceived += (s, e) => Console.WriteLine(e);
processUtil.StartWithEvents();

Thread.Sleep(1000 * 15);

if (process.HasExited)
{
Console.WriteLine("FAILED!!!!!!!!!");
Console.ReadLine();
}

process.Kill();
process.WaitForExit();
}

return;
}

void TestStartSync(MSession session)
{
var path = new MinecraftPath();
var launcher = new CMLauncher(path);

launcher.FileChanged += Downloader_ChangeFile;
launcher.ProgressChanged += Downloader_ChangeProgress;

var versions = launcher.GetAllVersions();
foreach (var item in versions)
{
Console.WriteLine(item.Name);
}

var process = launcher.CreateProcess("1.5.2", new MLaunchOption
{
Session = session
});

var processUtil = new CmlLib.Utils.ProcessUtil(process);
processUtil.OutputReceived += (s, e) => Console.WriteLine(e);
processUtil.StartWithEvents();
}

int endTop = -1;

private void Downloader_ChangeProgress(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
Console.SetCursorPosition(0, endTop);

// e.ProgressPercentage: 0~100
Console.Write("{0}% ", e.ProgressPercentage);

Console.SetCursorPosition(0, endTop);
}

private void Downloader_ChangeFile(DownloadFileChangedEventArgs e)
{
// More information about DownloadFileChangedEventArgs
// https://github.com/AlphaBs/CmlLib.Core/wiki/Handling-Events#downloadfilechangedeventargs

Console.WriteLine("[{0}] ({2}/{3}) {1} ", e.FileKind.ToString(), e.FileName, e.ProgressedFileCount,
e.TotalFileCount);

endTop = Console.CursorTop;
}
}
}

0 comments on commit b3b9bef

Please sign in to comment.