Skip to content

Commit

Permalink
chore(install): cleanup install code
Browse files Browse the repository at this point in the history
  • Loading branch information
skjsjhb committed Mar 1, 2025
1 parent 6acf56c commit b4ad542
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 161 deletions.
5 changes: 3 additions & 2 deletions src/main/api/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { containers } from "@/main/container/manage";
import type { ContainerProps } from "@/main/container/spec";
import { paths } from "@/main/fs/paths";
import { games } from "@/main/game/manage";
import type { GameCoreType, GameInstallProps, GameProfile } from "@/main/game/spec";
import type { GameCoreType, GameProfile } from "@/main/game/spec";
import type { InstallerProps } from "@/main/install/installers";
import { vanillaInstaller } from "@/main/install/vanilla";
import { ipcMain } from "@/main/ipc/typed";
import { venv } from "@/main/launch/venv";
Expand Down Expand Up @@ -37,7 +38,7 @@ export interface CreateGameInit {
name: string;
gameVersion: string;
authType: "new-vanilla" | "manual" | "reuse";
installProps: GameInstallProps;
installProps: InstallerProps;
playerName: string;
accountId: string | null;
assetsLevel: "full" | "video-only";
Expand Down
144 changes: 8 additions & 136 deletions src/main/api/install.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { containers } from "@/main/container/manage";
import { games } from "@/main/game/manage";
import { fabricInstaller } from "@/main/install/fabric";
import { forgeInstaller } from "@/main/install/forge";
import { forgeCompat } from "@/main/install/forge-compat";
import { installers } from "@/main/install/installers";
import { neoforgedInstaller } from "@/main/install/neoforged";
import { quiltInstaller } from "@/main/install/quilt";
import { smelt, type SmeltInstallInit } from "@/main/install/smelt";
import { smeltLegacy } from "@/main/install/smelt-legacy";
import { vanillaInstaller } from "@/main/install/vanilla";
import { ipcMain } from "@/main/ipc/typed";
import { jrt } from "@/main/jrt/install";
import { profileLoader } from "@/main/profile/loader";
import { reg } from "@/main/registry/registry";
import { exceptions } from "@/main/util/exception";
import type { Progress, ProgressController } from "@/main/util/progress";
import fs from "fs-extra";

export type VanillaInstallEvent =
{
Expand Down Expand Up @@ -44,7 +37,7 @@ ipcMain.on("installGame", async (e, gameId) => {
console.debug(`Starting installation of ${gameId}`);

const game = structuredClone(reg.games.get(gameId));
const c = containers.get(game.launchHint.containerId);
const container = containers.get(game.launchHint.containerId);

function send(e: VanillaInstallEvent) {
port.postMessage(e);
Expand All @@ -57,134 +50,13 @@ ipcMain.on("installGame", async (e, gameId) => {
const abortController = new AbortController();
installControllers.set(gameId, abortController);

const control: ProgressController = {
signal: abortController.signal,
onProgress
};

try {
const installType = game.installProps.type;
const { gameVersion } = game.installProps;

const control: ProgressController = {
signal: abortController.signal,
onProgress
};

const vanillaProfile = await vanillaInstaller.installProfile(gameVersion, c, control);
let p = vanillaProfile;

let forgeInstallerPath: string | null = null;
let forgeModLoaderPath: string | null = null;
let forgeInstallerInit: SmeltInstallInit | null = null;
let forgeInstallAction: ForgeInstallActionType = "none";

// Preprocess mod loaders, retrieve profile
if (installType === "fabric") {
const fid = await fabricInstaller.retrieveProfile(
gameVersion,
game.installProps.loaderVersion,
c,
control
);
p = await profileLoader.fromContainer(fid, c);
}

if (installType === "quilt") {
const qid = await quiltInstaller.retrieveProfile(
gameVersion,
game.installProps.loaderVersion,
c,
control
);
p = await profileLoader.fromContainer(qid, c);
}

if (installType === "neoforged") {
let loaderVersion = game.installProps.loaderVersion;

if (!loaderVersion) {
loaderVersion = await neoforgedInstaller.pickLoaderVersion(gameVersion, control);
}

forgeInstallerPath = await neoforgedInstaller.downloadInstaller(loaderVersion, control);
forgeInstallAction = "smelt";

forgeInstallerInit = await smelt.readInstallProfile(forgeInstallerPath);
const fid = await smelt.deployVersionProfile(forgeInstallerInit, c);
p = await profileLoader.fromContainer(fid, c);
}

if (installType === "forge") {
let loaderVersion = game.installProps.loaderVersion;

if (!loaderVersion) {
loaderVersion = await forgeInstaller.pickLoaderVersion(gameVersion, control);
}

const installerType = forgeInstaller.getInstallType(gameVersion);
forgeInstallerPath = await forgeInstaller.downloadInstaller(loaderVersion, installerType, control);

const modLoaderUrl = await forgeCompat.getModLoaderUrl(gameVersion);
forgeModLoaderPath = modLoaderUrl && await forgeCompat.downloadModLoader(modLoaderUrl);

if (installerType === "installer") {
const legacyProfileId = await smeltLegacy.dumpContent(forgeInstallerPath, c);
if (legacyProfileId) {
forgeInstallAction = "smelt-legacy";
p = await profileLoader.fromContainer(legacyProfileId, c);
} else {
forgeInstallAction = "smelt";
forgeInstallerInit = await smelt.readInstallProfile(forgeInstallerPath);
const fid = await smelt.deployVersionProfile(forgeInstallerInit, c);
p = await profileLoader.fromContainer(fid, c);
}
} else {
forgeInstallAction = "merge";
if (modLoaderUrl) {
// There exists a bug with ModLoader which makes it incompatible with the directory structure
// This cannot be fixed even with VENV
// We're using a patch named DAMT: https://www.minecraftforum.net/forums/mapping-and-modding-java-edition/minecraft-mods/mods-discussion/1291855-a-custom-tweaker-for-using-older-modloaders-in-the
await forgeCompat.patchProfile(c, gameVersion);
p = await profileLoader.fromContainer(gameVersion, c);
} else {
p = vanillaProfile;
}

}
}

// Ensure libraries
await jrt.installRuntime(p.javaVersion?.component ?? "jre-legacy", control);

await vanillaInstaller.installLibraries(p, c, new Set(), control);

// Finalize Forge
if (installType === "neoforged" || installType === "forge") {
if (forgeInstallAction === "smelt") {
await smelt.runPostInstall(forgeInstallerInit!, forgeInstallerPath!, p, c, control);
} else if (forgeInstallAction === "merge") {
await smeltLegacy.patchLegacyLibraries(
jrt.executable(p.javaVersion?.component ?? "jre-legacy"),
forgeInstallerPath!,
c
);

const clientPath = c.client(p.version || p.id);

if (forgeModLoaderPath) {
await smeltLegacy.mergeClient(forgeModLoaderPath, clientPath);
}

await smeltLegacy.mergeClient(forgeInstallerPath!, clientPath);
}

game.launchHint.venv = await forgeCompat.shouldUseVenv(gameVersion);
await fs.remove(forgeInstallerPath!);
}

// Game-level post install
await vanillaInstaller.installAssets(p, c, game.assetsLevel, control);
await vanillaInstaller.emitOptions(c);

game.launchHint.profileId = p.id;
game.installed = true;
games.add(game);
await installers.runInstall(gameId, control);

console.debug(`Completed installation of ${gameId}`);
send({ type: "finish" });
Expand Down
19 changes: 2 additions & 17 deletions src/main/game/spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { InstallerProps } from "@/main/install/installers";
import type { LaunchHint } from "@/main/launch/types";
import type { RegistryTransformer } from "@/main/registry/registry";

Expand Down Expand Up @@ -25,7 +26,7 @@ export interface GameProfile {
/**
* Props for installing the game.
*/
installProps: GameInstallProps;
installProps: InstallerProps;

/**
* Game related versions.
Expand Down Expand Up @@ -59,22 +60,6 @@ export type GameCoreType =
"neoforged" |
"unknown"

export type GameInstallProps =
{
type: "vanilla";
gameVersion: string;
} |
{
type: "fabric" | "quilt";
gameVersion: string;
loaderVersion: string;
} |
{
type: "neoforged" | "forge";
gameVersion: string;
loaderVersion: string;
}

export const GAME_REG_VERSION = 2;
export const GAME_REG_TRANS: RegistryTransformer[] = [
// v1: patch the `installerProps` key
Expand Down
Loading

0 comments on commit b4ad542

Please sign in to comment.