From 03af18fe739ec3bd41eb22b618e8529e25a95036 Mon Sep 17 00:00:00 2001 From: Chris Maltby Date: Mon, 18 Mar 2024 16:27:27 +0000 Subject: [PATCH] Add step to validate build files before compiling with GBDK, currently just confirms that MAX_GLOBAL_VARS is not larger than VM_HEAP_SIZE --- src/bin/gb-studio-cli.ts | 7 ++++ src/lib/compiler/buildProject.ts | 6 +++ .../compiler/validate/validateEjectedBuild.ts | 39 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 src/lib/compiler/validate/validateEjectedBuild.ts diff --git a/src/bin/gb-studio-cli.ts b/src/bin/gb-studio-cli.ts index 316fe9862..1b67f7e42 100644 --- a/src/bin/gb-studio-cli.ts +++ b/src/bin/gb-studio-cli.ts @@ -11,6 +11,7 @@ import makeBuild from "lib/compiler/makeBuild"; import initElectronL10N from "lib/lang/initElectronL10N"; import { loadEngineFields } from "lib/project/engineFields"; import loadAllScriptEventHandlers from "lib/project/loadScriptEventHandlers"; +import { validateEjectedBuild } from "lib/compiler/validate/validateEjectedBuild"; const rmdir = promisify(rimraf); @@ -74,6 +75,12 @@ const main = async ( warnings, }); + await validateEjectedBuild({ + buildRoot: tmpBuildDir, + progress, + warnings, + }); + if (command === "export") { if (program.onlyData) { // Export src/data and include/data to destination diff --git a/src/lib/compiler/buildProject.ts b/src/lib/compiler/buildProject.ts index cb4b9a320..6a36c8972 100755 --- a/src/lib/compiler/buildProject.ts +++ b/src/lib/compiler/buildProject.ts @@ -7,6 +7,7 @@ import copy from "lib/helpers/fsCopy"; import type { ProjectData } from "store/features/project/projectActions"; import type { EngineFieldSchema } from "store/features/engine/engineState"; import { ScriptEventHandlers } from "lib/project/loadScriptEventHandlers"; +import { validateEjectedBuild } from "lib/compiler/validate/validateEjectedBuild"; type BuildOptions = { buildType: "rom" | "web" | "pocket"; @@ -53,6 +54,11 @@ const buildProject = async ( progress, warnings, }); + await validateEjectedBuild({ + buildRoot: outputRoot, + progress, + warnings, + }); await makeBuild({ buildRoot: outputRoot, tmpPath, diff --git a/src/lib/compiler/validate/validateEjectedBuild.ts b/src/lib/compiler/validate/validateEjectedBuild.ts new file mode 100644 index 000000000..e47cbf896 --- /dev/null +++ b/src/lib/compiler/validate/validateEjectedBuild.ts @@ -0,0 +1,39 @@ +import { readFile } from "fs-extra"; +import Path from "path"; + +type ValidateOptions = { + buildRoot: string; + progress: (msg: string) => void; + warnings: (msg: string) => void; +}; + +export const validateEjectedBuild = async ({ + buildRoot, + progress = (_msg) => {}, + warnings = (_msg) => {}, +}: ValidateOptions) => { + const vmIncludePath = Path.join(buildRoot, "include/vm.h"); + const gameGlobalsPath = Path.join(buildRoot, "include/data/game_globals.i"); + const vmInclude = await readFile(vmIncludePath, "utf8"); + const gameGlobals = await readFile(gameGlobalsPath, "utf8"); + + const vmHeapSizeStr = vmInclude.match(/#define VM_HEAP_SIZE (\d+)/m)?.[1]; + const maxGlobalVarsStr = gameGlobals.match(/MAX_GLOBAL_VARS = (\d+)/m)?.[1]; + + const vmHeapSize = parseInt(vmHeapSizeStr ?? "", 10); + const maxGlobalVars = parseInt(maxGlobalVarsStr ?? "", 10); + + progress(`Validating build files...`); + + if (isNaN(vmHeapSize) || isNaN(maxGlobalVars)) { + warnings( + "Unable to read VM_HEAP_SIZE and MAX_GLOBAL_VARS to determine if project contains too many unique variables" + ); + } + + if (maxGlobalVars > vmHeapSize) { + warnings( + `Your project contains too many unique variables and will not work as expected. VM_HEAP_SIZE defines the maximum amount of variables allowed ${vmHeapSize} but your project contained ${maxGlobalVars} unique variables.` + ); + } +};