dmitmel / ccloader3

MIT License
2 stars 4 forks source link

Add support to override game.compiled.js without code hacks #2

Open ghost opened 4 years ago

ghost commented 4 years ago

It's for my evil game source patching plan.

ghost commented 4 years ago

Preferably available during preload.

dmitmel commented 4 years ago

As I don't know what degree of customization is needed, here's a temporary solution which simply creates a global variable GAME_SCRIPT_INPUT_URL which you can read and if GAME_SCRIPT_PATCHED_URL is set by the time game.loadMainScript() is reached, uses its value instead.

diff --git a/src/game-types.d.ts b/src/game-types.d.ts
index be5f412..e719691 100644
--- a/src/game-types.d.ts
+++ b/src/game-types.d.ts
@@ -23,4 +23,7 @@ declare global {
   }

   function startCrossCode(): void;
+
+  var GAME_SCRIPT_INPUT_URL: string;
+  var GAME_SCRIPT_PATCHED_URL: string;
 }
diff --git a/src/game.ts b/src/game.ts
index dbf6c69..e287e11 100644
--- a/src/game.ts
+++ b/src/game.ts
@@ -72,7 +72,7 @@ export async function buildNecessaryDOM(config: Config): Promise<void> {
 }

 export async function loadMainScript(
-  config: Config,
+  url: string,
   eventReceiver: { onImpactInit(): void },
 ): Promise<() => void> {
   let domReadyCallback: () => void = null!;
@@ -84,7 +84,7 @@ export async function loadMainScript(

   // async is turned off so that the main script blocks the UI thread while it
   // is being executed
-  await loadScript(config.gameScriptURL, { async: false });
+  await loadScript(url, { async: false });

   if (domReadyCallback == null) {
     throw new Error('domReadyCallback');
diff --git a/src/modloader.ts b/src/modloader.ts
index 0581b5b..70bd712 100644
--- a/src/modloader.ts
+++ b/src/modloader.ts
@@ -88,11 +88,13 @@ export async function boot(): Promise<void> {

   await game.buildNecessaryDOM(config);

+  window.GAME_SCRIPT_INPUT_URL = config.gameScriptURL;
+
   await initModClasses(loadedMods);

   await executeStage(loadedMods, 'preload');
   let domReadyCallback = await game.loadMainScript(
-    config,
+    window.GAME_SCRIPT_PATCHED_URL ?? config.gameScriptURL,
     runtimeMod.classInstance! as import('../runtime/src/main').default,
   );
   await executeStage(loadedMods, 'postload');

Not that I like this solution, but it should be sufficient for you to experiment with.