diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0b1135c..ddb5c9c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -21,6 +21,18 @@ In some scenarios we make use of this cache purging mechanism to make sure users of external tools that GM-CLI depends on. For instance, if we require a new version of Igor for GM-CLI to function, we may bump the major version. +### Which Igor gets used + +The above applies to the standalone Igor that GM-CLI downloads from `gms.yoyogames.com` into `.gmcache/igor`. That copy +is only used to bootstrap: fetching a licence, and listing and installing runtimes. It is versioned independently of the +runtimes and can lag a long way behind them. + +Building, running and packaging a project instead use the Igor that ships inside the installed runtime, at +`bin/igor//`, falling back to the downloaded one when the runtime has none (see `findRuntimeIgor`). +Igor and the runner are built and released together, and only that pairing is guaranteed to agree on where a build's +output belongs on disk — mixing them silently produces packages the runner cannot load. This means a fix on the Igor +side reaches users as soon as they install the runtime carrying it, with no `MAJOR` bump needed to purge the cache. + ## Publish ```sh diff --git a/src/gms2/install-runtime.ts b/src/gms2/install-runtime.ts index dc8d85d..02272b9 100644 --- a/src/gms2/install-runtime.ts +++ b/src/gms2/install-runtime.ts @@ -21,6 +21,7 @@ import type { Log } from "~/log"; import { KnownError } from "~/error"; import type { Target } from "~/target"; import { + IGOR_PLATFORM_DIRS, installRuntime, listRuntimes, ModuleSchema, @@ -304,7 +305,10 @@ export async function installRuntimeIfNeeded( } log.success("Runtime installed"); - runtimeLocation = await findRuntimeLocation(ctx, runtimeDir); + // Keep honouring the requested version: without it we'd hand back whatever + // runtime in the cache happens to be newest, which is rarely the one we just + // installed. + runtimeLocation = await findRuntimeLocation(ctx, runtimeDir, version); if (!runtimeLocation) { throw new Error("Invariant broken: no runtime found after installation"); } @@ -325,3 +329,77 @@ export async function getInstalledRuntimeModules( .filter((result) => result.success) .map((result) => result.data); } + +const MIN_RUNTIME_WITH_USABLE_IGOR: Gms2Version = [2026, 0, 0, 0]; + +/** + * Every runtime ships the Igor that was built alongside it, at + * `bin/igor///Igor`. That copy is the one that agrees with the + * runner about where files belong on disk, so it's what we build with. + * + * The standalone Igor we download to bootstrap (fetch a license, list and + * install runtimes) is versioned separately from the runtimes and lags behind + * them, which silently breaks targets where Igor does the packaging. For + * example on OperaGX, external assets such as `.yytex` texture pages moved into + * an `assets` subfolder of the package, and a runner from a runtime newer than + * the bootstrap Igor 404s on every one of them. + * See https://github.com/YoYoGames/GameMaker-Bugs/issues/15930 + * + * Returns undefined when the runtime has no usable Igor, in which case the + * caller should stick with the bootstrap one. + */ +export async function findRuntimeIgor( + ctx: Context, + runtimeLocation: string, +): Promise { + const runtimeVersion = parseRuntimeVersionFromDirName( + ctx.path.basename(runtimeLocation), + ); + // Older runtimes predate the Igor flags we build with (-projectool, -prefabs, + // -jsonErrors), so for those the bootstrap Igor really is the better choice. + if ( + runtimeVersion === undefined || + gms2VersionCompare(runtimeVersion, MIN_RUNTIME_WITH_USABLE_IGOR) < 0 + ) { + return undefined; + } + + const platform = ctx.process.platform; + const platformDir = IGOR_PLATFORM_DIRS[platform] ?? platform; + // We use x64 builds on ARM Windows, and fall back to the x64 build under + // Rosetta on Apple Silicon, matching downloadIgor(). + const archDirs = + platform === "win32" + ? ["x64"] + : platform === "darwin" + ? [ctx.process.arch, "x64"] + : [ctx.process.arch]; + + for (const archDir of archDirs) { + const igorPath = ctx.path.join( + runtimeLocation, + "bin", + "igor", + platformDir, + archDir, + platform === "win32" ? "Igor.exe" : "Igor", + ); + if (!(await exists(ctx, igorPath))) { + continue; + } + if (platform !== "win32") { + // installationFixup() already does this for runtimes we installed + // ourselves, but be safe about runtimes left behind by other tools. + try { + await ctx.fs.chmod(igorPath, 0o755); + } catch { + // Read-only cache, or someone else owns the file: if it's already + // executable the spawn below still works, and if it isn't we'd rather + // report the spawn failure than this one. + } + } + return igorPath; + } + + return undefined; +} diff --git a/src/gms2/use-gms2.ts b/src/gms2/use-gms2.ts index b07fed7..b9367fb 100644 --- a/src/gms2/use-gms2.ts +++ b/src/gms2/use-gms2.ts @@ -22,7 +22,7 @@ import { stopProcesses } from "~/kill-process"; import { getProjectName, type ProjectPath } from "~/project"; import type { Target } from "~/target"; import type { Gms2VersionPartial } from "~/toolchain"; -import { installRuntimeIfNeeded } from "./install-runtime"; +import { findRuntimeIgor, installRuntimeIfNeeded } from "./install-runtime"; import { defaultGms2ToolchainOptions, type Gms2ToolchainOptions, @@ -93,6 +93,12 @@ export async function useGms2( target: options.target, }); + // Prefer the Igor that shipped with this runtime over the standalone one we + // downloaded to bootstrap: only that pairing is guaranteed to agree on where + // the build output belongs. See findRuntimeIgor(). + const igorPath = + (await findRuntimeIgor(ctx, runtimeLocation)) ?? tools.igorPath; + const buildCacheDir = await cache.getSubDirPath( ctx, `build-gms2-${options.target}-${runtime}`, @@ -149,12 +155,11 @@ export async function useGms2( try { await spawnIgor(ctx, actionLog, { - igorPath: tools.igorPath, + igorPath, verbose: options.verbose, args: constructIgorBuildArgs( ctx, { - igorPath: tools.igorPath, licenseFile: options.licenseFile, prefabsDir: options.prefabsDir, runtimeDir: runtimeLocation, @@ -369,7 +374,6 @@ function encodeFeatureFlags(flags: string[]): string { } export interface CommonIgorBuildArgs { - igorPath: string; licenseFile: string; prefabsDir: string; runtimeDir: string; diff --git a/src/igor/download.ts b/src/igor/download.ts index cc3fcd3..23ca40e 100644 --- a/src/igor/download.ts +++ b/src/igor/download.ts @@ -38,7 +38,7 @@ const IGOR_ZIPS: Record> = { }, }; -const IGOR_PLATFORM_DIRS: Record = { +export const IGOR_PLATFORM_DIRS: Record = { win32: "windows", linux: "linux", darwin: "osx", diff --git a/src/parse-env.ts b/src/parse-env.ts index 62260db..f8af0f6 100644 --- a/src/parse-env.ts +++ b/src/parse-env.ts @@ -24,6 +24,7 @@ export interface ParsedEnv { NO_COLOR: boolean | undefined; CI: boolean | undefined; LOCALAPPDATA: string | undefined; + PROGRAMDATA: string | undefined; XDG_CACHE_HOME: string | undefined; } @@ -75,6 +76,7 @@ function parseEnv(rawEnv: NodeJS.ProcessEnv): ParsedEnv { NO_COLOR: parse("NO_COLOR", booleanEnvVar), CI: parse("CI", booleanEnvVar), LOCALAPPDATA: parse("LOCALAPPDATA", z.string()), + PROGRAMDATA: parse("PROGRAMDATA", z.string()), XDG_CACHE_HOME: parse("XDG_CACHE_HOME", z.string()), }; diff --git a/src/restore-prefabs.ts b/src/restore-prefabs.ts index 059f64c..d5bd1dd 100644 --- a/src/restore-prefabs.ts +++ b/src/restore-prefabs.ts @@ -15,7 +15,7 @@ */ import type { Cache } from "./cache"; -import type { Context } from "./context"; +import { exists, type Context } from "./context"; import type { Log } from "./log"; import type { ProjectPath } from "./project"; import { spawnProcess } from "./spawn"; @@ -39,6 +39,7 @@ export async function restorePrefabs( }, ): Promise { const prefabsDir = await cache.getSubDirPath(ctx, "prefabs"); + await seedPrefabsFromIdeStores(ctx, log, { projectPath, prefabsDir }); // TODO LATER: add options to pick registry etc. const args = [ "PREFABS", @@ -57,3 +58,145 @@ export async function restorePrefabs( }); return prefabsDir; } + +/** + * Directories the GameMaker IDE unpacks prefab packages into, one + * `-` folder per package. The IDE keeps this store per machine + * rather than per project, so a project built in the IDE looks, to us, like it + * has no prefabs at all. + * + * Each entry is a directory that may hold several `GameMakerStudio2*` installs + * (stable, Beta, Dev, LTS2026, ...), each with its own `Prefabs` folder. + */ +function idePrefabStoreRoots(ctx: Context): string[] { + switch (ctx.process.platform) { + case "win32": + return [ctx.env.PROGRAMDATA ?? "C:\\ProgramData"]; + case "darwin": + return ["/Users/Shared"]; + default: + return [ctx.path.join(ctx.os.homedir(), ".config")]; + } +} + +/** + * Copy any prefab package the project references out of the IDE's store and + * into the folder we hand to ProjectTool, so it doesn't have to fetch them. + * + * This is a workaround: fetching is PackageTool's job, but the newest published + * package-tool binds to gmpm by reflection and no longer matches the gmpm we + * install alongside it, so every fetch dies with "Parameter count mismatch". + * Seeding from the IDE store sidesteps the fetch entirely on machines that have + * the IDE. Once package-tool is republished against the current gmpm this only + * saves a download and can go away. + * + * Best effort throughout: anything we can't work out is left to ProjectTool, + * which reports missing prefabs far better than we could here. + */ +async function seedPrefabsFromIdeStores( + ctx: Context, + log: Log, + { projectPath, prefabsDir }: { projectPath: ProjectPath; prefabsDir: string }, +): Promise { + try { + const referenced = await readPrefabReferences(ctx, projectPath); + if (referenced.length === 0) { + return; + } + + // ProjectTool looks next to the project as well as in the folder we pass, + // so a package in either place is already accounted for. + const projectPrefabsDir = ctx.path.join( + ctx.path.dirname(projectPath), + "prefabs", + ); + const missing: string[] = []; + for (const name of referenced) { + if ( + !(await exists(ctx, ctx.path.join(prefabsDir, name))) && + !(await exists(ctx, ctx.path.join(projectPrefabsDir, name))) + ) { + missing.push(name); + } + } + if (missing.length === 0) { + return; + } + + const stores = await findIdePrefabStores(ctx); + if (stores.length === 0) { + return; + } + + let copied = 0; + for (const name of missing) { + for (const store of stores) { + const src = ctx.path.join(store, name); + if (!(await exists(ctx, src))) { + continue; + } + await ctx.fs.cp(src, ctx.path.join(prefabsDir, name), { + recursive: true, + }); + log.message(`Copied prefab ${name} from ${store}`); + copied++; + break; + } + } + if (copied > 0) { + log.message( + `Seeded ${String(copied)} of ${String(missing.length)} missing prefabs from the GameMaker IDE`, + ); + } + } catch (e) { + log.message(`Could not read prefabs from the GameMaker IDE: ${String(e)}`); + } +} + +/** Every `GameMakerStudio2*` install's `Prefabs` folder that exists on this machine. */ +async function findIdePrefabStores(ctx: Context): Promise { + const stores: string[] = []; + for (const root of idePrefabStoreRoots(ctx)) { + let entries: string[]; + try { + entries = await ctx.fs.readdir(root); + } catch { + // Root doesn't exist (no IDE installed, or not where we look on this OS). + continue; + } + for (const entry of entries.sort()) { + if (!entry.startsWith("GameMakerStudio2")) { + continue; + } + const store = ctx.path.join(root, entry, "Prefabs"); + if (await exists(ctx, store)) { + stores.push(store); + } + } + } + return stores; +} + +/** + * Names of the prefab packages a project pulls in, read out of the .yyp's + * `ForcedPrefabProjectReferences`. The .yyp is JSON with trailing commas, so we + * pick the names out with a regex rather than teaching the CLI to parse it. + */ +async function readPrefabReferences( + ctx: Context, + projectPath: ProjectPath, +): Promise { + const yyp = await ctx.fs.readFile(projectPath, "utf-8"); + const block = /"ForcedPrefabProjectReferences"\s*:\s*\[([\s\S]*?)\]/.exec( + yyp, + )?.[1]; + if (!block) { + return []; + } + const names = new Set(); + for (const match of block.matchAll(/"name"\s*:\s*"([^"\\/]+)"/g)) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + names.add(match[1]!); + } + return [...names]; +}