From 36966fab3b879ec9750dff929c17767b9a31d93f Mon Sep 17 00:00:00 2001 From: Slayer95 Date: Wed, 30 Oct 2024 23:27:31 -0500 Subject: [PATCH] Preload data when using Dex.mod (#10641) * Preload data when using Dex.mod With the initial introduction fo TypeScript at 3716f36, ``Dex.mod``, and ``Dex.format`` (nowadays ``Dex.forFormat``) no longer ensured that the data cache has been filled. Newer lazy-loading getter APIs were introduced, which were intended to be used as a replacement for direct access to the loaded data (``dataCache``). However, these APIs were either not effective or not properly used., as reflected by 43ef4d9, which reverted ``Dex.forFormat`` to its full capabilities. Nevertheless, ``Dex.mod`` was left untouched, which results in a footgun where ``Dex.forFormat('gen1randbats').gen == 1``, but ``Dex.mod('gen1').gen == 9``. This commit brings back ``Dex.mod`` to its original behavior, so that gen information is always there when it's needed. * Add test for dex.mod(mod).gen * Swap around test instructions This is more reliable given the load order. --- sim/dex.ts | 2 +- test/sim/dex.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/sim/dex.ts b/sim/dex.ts index 2d468d7bdc..7bc0590029 100644 --- a/sim/dex.ts +++ b/sim/dex.ts @@ -165,7 +165,7 @@ export class ModdedDex { mod(mod: string | undefined): ModdedDex { if (!dexes['base'].modsLoaded) dexes['base'].includeMods(); - return dexes[mod || 'base']; + return dexes[mod || 'base'].includeData(); } forGen(gen: number) { diff --git a/test/sim/dex.js b/test/sim/dex.js index e8c0f3aaef..b15e77b64b 100644 --- a/test/sim/dex.js +++ b/test/sim/dex.js @@ -3,6 +3,14 @@ const assert = require('./../assert'); describe('Mod loader', function () { + it('should always provide accurate gen information', function () { + { + const Dex = require('./../../dist/sim/dex').Dex; + assert.equal(Dex.mod('gen2').gen, 2); + assert.equal(Dex.forFormat('gen1randombattle').gen, 1); + } + }); + it('should work fine in any order', function () { { const Dex = require('./../../dist/sim/dex').Dex;