UNPKG

@bscotch/stitch

Version:

Stitch: The GameMaker Studio 2 Asset Pipeline Development Kit.

61 lines 2 kB
import paths from '../../../utility/paths.js'; import { findOuterFunctions, findTokenReferences, } from '../../parser/codeParser.js'; import { Gms2ResourceBase, } from './Gms2ResourceBase.js'; export class Gms2Script extends Gms2ResourceBase { codeCache; globalFunctionCache; constructor(...setup) { super('scripts', ...setup); } get codeFilePathAbsolute() { return paths.join(this.yyDirAbsolute, `${this.name}.gml`); } set code(code) { this.purgeCaches(); this.codeCache = code; this.storage.writeBlobSync(this.codeFilePathAbsolute, this.codeCache, '\r\n'); } get code() { this.codeCache ??= this.storage .readBlobSync(this.codeFilePathAbsolute) .toString(); return this.codeCache; } /** * Get all functions defined in this script that will be globally available. * (Only returns outer-scope named functions.) */ get globalFunctions() { try { this.globalFunctionCache ||= findOuterFunctions(this.code, this); return this.globalFunctionCache; } catch (err) { console.log(`Failed to lint the gml code in this script resource: ${this.name}`); throw err; } } /** * Find all references to a token. ⚠ WARNING: does not consider scope or type! */ findTokenReferences(token, options) { return findTokenReferences(this.code, token, { resource: this, suffixPattern: options?.suffix, includeSelf: options?.includeSelf, }); } purgeCaches() { this.codeCache = undefined; this.globalFunctionCache = undefined; } static async create(name, code, comms) { const script = new Gms2Script(name, comms); await script.replaceYyFile({ name: script.name, }); script.code = code; return script; } } //# sourceMappingURL=Gms2Script.js.map