From bd3d94c79cc51ed597866a1681b8a9239ff36069 Mon Sep 17 00:00:00 2001 From: urkerab Date: Sun, 13 Oct 2024 19:23:15 +0100 Subject: [PATCH] Simplify reuse of parent data cache when child gen has no override file (#10614) * Don't inherit init function from parent mod * Simplify reuse of parent data cache when child gen has no override file --- sim/dex.ts | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/sim/dex.ts b/sim/dex.ts index d15006e0854e..6ba35db70ac4 100644 --- a/sim/dex.ts +++ b/sim/dex.ts @@ -420,7 +420,7 @@ export class ModdedDex { return searchResults; } - loadDataFile(basePath: string, dataType: DataType | 'Aliases'): AnyObject { + loadDataFile(basePath: string, dataType: DataType | 'Aliases'): AnyObject | void { try { const filePath = basePath + DATA_FILES[dataType]; const dataObject = require(filePath); @@ -436,7 +436,6 @@ export class ModdedDex { throw e; } } - return {}; } loadTextFile( @@ -488,7 +487,9 @@ export class ModdedDex { const basePath = this.dataDir + '/'; - const Scripts = this.loadDataFile(basePath, 'Scripts'); + const Scripts = this.loadDataFile(basePath, 'Scripts') || {}; + // We want to inherit most of Scripts but not this. + const init = Scripts.init; this.parentMod = this.isBase ? '' : (Scripts.inherit || 'base'); let parentDex; @@ -506,28 +507,21 @@ export class ModdedDex { this.includeFormats(); } for (const dataType of DATA_TYPES.concat('Aliases')) { - const BattleData = this.loadDataFile(basePath, dataType); - if (BattleData !== dataCache[dataType]) dataCache[dataType] = Object.assign(BattleData, dataCache[dataType]); + dataCache[dataType] = this.loadDataFile(basePath, dataType); if (dataType === 'Rulesets' && !parentDex) { for (const format of this.formats.all()) { - BattleData[format.id] = {...format, ruleTable: null}; + dataCache.Rulesets[format.id] = {...format, ruleTable: null}; } } } if (parentDex) { for (const dataType of DATA_TYPES) { const parentTypedData: DexTable = parentDex.data[dataType]; - const childTypedData: DexTable = dataCache[dataType] || (dataCache[dataType] = {}); - // if child is empty and there's no Scripts.init, there's no need to copy, just re-use. - let childIsEmpty = true; - for (const k in childTypedData) { // eslint-disable-line @typescript-eslint/no-unused-vars - childIsEmpty = false; - break; - } - if (dataType !== 'Pokedex' && childIsEmpty && !Scripts.init) { + if (!dataCache[dataType] && !init) { dataCache[dataType] = parentTypedData; continue; } + const childTypedData: DexTable = dataCache[dataType] || (dataCache[dataType] = {}); for (const entryId in parentTypedData) { if (childTypedData[entryId] === null) { // null means don't inherit @@ -557,7 +551,7 @@ export class ModdedDex { this.dataCache = dataCache as DexTableData; // Execute initialization script. - if (Scripts.init) Scripts.init.call(this); + if (init) init.call(this); return this.dataCache; }