CmlLib
English (v4)
English (v4)
  • 🧊CmlLib Projects
  • 🚀CmlLib.Core
    • Home
    • Getting Started
      • Minecraft Launcher
      • Minecraft Path
      • Versions
      • Launch Options
      • Event Handling
    • Login and Sessions
      • Microsoft Xbox Account
      • Offline Account
    • More APIs
      • MinecraftLauncherParameters
      • Rules
      • FileExtractor
      • GameInstaller
      • Java
    • Mod Loader Installers
      • Forge Installer
      • Fabric Installer
      • Quilt Installer
      • LiteLoader Installer
    • Utilities
      • Minecraft Changelogs
    • Resources
      • FAQ
      • Known Issues
      • Sample Launcher
      • License
  • 🔓Auth.Microsoft
    • Home
    • CmlLib.Core.Auth.Microsoft
      • JELoginHandler
      • JELoginHandlerBuilder
      • JEAuthenticator
      • Authentication with MSAL
    • XboxAuthNet.Game
      • OAuth
      • XboxAuth
      • XboxAuthException
      • AccountManager
      • Accounts
    • XboxAuthNet.Game.Msal
      • ClientID
      • MsalClientHelper
      • OAuth
    • CmlLib.Core.Bedrock.Auth
    • Resources
  • 🌐MojangAPI
    • Home
    • Mojang API
    • SecurityQuestion
  • ⚒️Installer.Forge
    • Home
    • Supported Versions
    • Getting Started
    • MForge
    • ForgeVersionLoader
Powered by GitBook
On this page
  • Explaination
  • More Methods
  • API References
Edit on GitHub
  1. CmlLib.Core
  2. Getting Started

Minecraft Launcher

Basic Usage

In .NET Framework, add the following code to maximize the download speed. This is not necessary in .NET Core.

System.Net.ServicePointManager.DefaultConnectionLimit = 256;
// initialize the launcher
var path = new MinecraftPath();
var launcher = new MinecraftLauncher(path);

// add event handlers
launcher.FileProgressChanged += (sender, args) =>
{
    Console.WriteLine($"Name: {args.Name}");
    Console.WriteLine($"Type: {args.EventType}");
    Console.WriteLine($"Total: {args.TotalTasks}");
    Console.WriteLine($"Progressed: {args.ProgressedTasks}");
};
launcher.ByteProgressChanged += (sender, args) =>
{
    Console.WriteLine($"{args.ProgressedBytes} bytes / {args.TotalBytes} bytes");
};

// get all versions
var versions = await launcher.GetAllVersionsAsync();
foreach (var v in versions)
{
    Console.WriteLine(v.GetVersionType());
    Console.WriteLine(v.Name);
}

// install and launch the game
await launcher.InstallAsync("1.20.6");
var process = await launcher.BuildProcessAsync("1.20.6", new MLaunchOption
{
    Session = MSession.CreateOfflineSession("Gamer123"),
    MaximumRamMb = 4096
});
process.Start();

Explaination

var path = new MinecraftPath();
var launcher = new MinecraftLauncher(path);

Create Minecraft directory structure and initialize launcher instance. You can change the path and directory structure. See Minecraft Path and MinecraftLauncherParameters

launcher.FileProgressChanged += (sender, args) =>
{
    Console.WriteLine($"Name: {args.Name}");
    Console.WriteLine($"Type: {args.EventType}");
    Console.WriteLine($"Total: {args.TotalTasks}");
    Console.WriteLine($"Progressed: {args.ProgressedTasks}");
};
launcher.ByteProgressChanged += (sender, args) =>
{
    Console.WriteLine($"{args.ProgressedBytes} bytes / {args.TotalBytes} bytes");
};

Add event handler. It prints download progress to console. See Event Handling

var versions = await launcher.GetAllVersionsAsync();
foreach (var v in versions)
{
    Console.WriteLine(v.Name);
}

Get all version and print its names. See Versions

await launcher.InstallAsync("1.20.4");
var process = await launcher.BuildProcessAsync("1.20.4", new MLaunchOption
{
    Session = MSession.CreateOfflineSession("Gamer123"),
    MaximumRamMb = 4096
});
process.Start();

Install the game and build the game process and return it. See Launch Options for more launch options.

More Methods

Get all files to launch the version

// by version name
IEnumerable<GameFile> files = await launcher.ExtractFiles("1.20.4", cancellationToken);
// by IVersion 
IVersion version = await launcher.GetVersionAsync("1.20.4", cancellationToken);
IEnumerable<GameFile> files = await launcher.ExtractFiles(version, cancellationToken);

Scan files and download any files that need to be downloaded

// report install progress to launcher.FileProgressChanged, launcher.ByteProgressChanged
await launcher.InstallAsync("1.20.4", cancellationToken); // by version name
await launcher.InstallAsync(version, cancellationToken); // by IVersion 

// report install progress to fileProgress, byteProgress
await launcher.InstallAsync("1.20.4", fileProgress, byteProgress, cancellationToken); // by version name 
await launcher.InstallAsync(version, fileProgress, byteProgress, cancellationToken); // by IVersion 

Build game process

// by version name
Process process = await launcher.BuildProcessAsync("1.20.4", new MLaunchOption(), cancellationTokene);
// by IVersion
IVersion version = await launcher.GetVersionAsync("1.20.4", cancellationToken);
Process process = launcher.BuildProcess(version, new MLaunchOption());

Get the Java path required to launch the version

IVersion version = await launcher.GetVersionAsync("1.20.4", cancellationToken);
string? javaPath = await launcher.GetJavaPath(version);

Get the path to the first installed Java

string? javaPath = await launcher.GetDefaultJavaPath();

API References

Methods

ValueTask InstallAndBuildProcessAsync(string versionName, MLaunchOption launchOption, CancellationToken cancellationToken = default)

PreviousGetting StartedNextMinecraft Path

Last updated 2 months ago

🚀