Instance Control

Overview

The instance control system provides process management for running Minecraft instances, including PID tracking, lifecycle management, console streaming, and instance termination.

InstanceControl Trait

CRITICAL: You must import the trait to use instance management methods.

use lighty_launch::InstanceControl;  // Required!

// Now you can use instance management methods
if let Some(pid) = instance.get_pid() {
    println!("Running: {}", pid);
}

Trait Definition

pub trait InstanceControl: VersionInfo {
    /// Get the first PID for this instance
    fn get_pid(&self) -> Option<u32>;

    /// Get all PIDs for this instance (supports multiple processes)
    fn get_pids(&self) -> Vec<u32>;

    /// Close an instance by PID
    async fn close_instance(&self, pid: u32) -> InstanceResult<()>;

    /// Delete an instance completely (must not be running)
    async fn delete_instance(&self) -> InstanceResult<()>;

    /// Calculate the size of an instance
    fn size_of_instance(&self, version: &Version) -> InstanceSize;
}

Auto-implemented for any type implementing VersionInfo.

Instance Lifecycle

Instance Manager

Internal Structure

Global singleton: INSTANCE_MANAGER tracks all running instances

Registration

When a game launches:

Unregistration

When a game exits or is closed:

Process Management

Get PID

Get the first PID for an instance:

Implementation:

Get All PIDs

Get all PIDs for an instance (supports multiple processes):

Use case: Multiple instances with the same name running simultaneously

Close Instance

Terminate a running instance:

Implementation:

Platform-specific kill:

Windows

Linux/macOS

Error handling:

Delete Instance

Delete an instance completely (must not be running):

Implementation:

Deletes:

  • Game directory ({game_dir}/)

  • All saves, mods, configs

  • Libraries, assets, client JAR

Preserves:

  • Java installations (shared across instances)

Console Streaming

Console Handler

Asynchronous console streaming:

Stdout Streaming

Stderr Streaming

Console Events

Example usage:

Instance Size

Calculate the size of an instance:

InstanceSize Structure

Implementation:

Events

Instance Lifecycle Events

Listening to Events

Complete Examples

Basic Instance Management

Multiple Instance Tracking

Console Monitoring

Instance Size Calculation

Example output:

Error Handling

InstanceError Types

Error Examples

Best Practices

1. Always Import the Trait

2. Check Before Closing

3. Close Before Deleting

4. Monitor Console with Events

5. Handle Errors Gracefully

Last updated