(dat: Packet, idx: Packet)
| 39 | } |
| 40 | |
| 41 | static parse(dat: Packet, idx: Packet): number { |
| 42 | if (!dat.data.length || !idx.data.length) { |
| 43 | printFatalError('No server cache found. Please build the cache first.'); |
| 44 | } |
| 45 | |
| 46 | const entries = dat.g4s(); |
| 47 | idx.pos += 4; |
| 48 | |
| 49 | const version = dat.g4s(); |
| 50 | if (version !== ScriptProvider.COMPILER_VERSION) { |
| 51 | printFatalError('\nFatal: Scripts were compiled with an incompatible script compiler. Please run `npm install`, then `npm run build`, and restart the server.'); |
| 52 | } |
| 53 | |
| 54 | const scripts = new Array<ScriptFile>(entries); |
| 55 | const scriptNames = new Map<string, number>(); |
| 56 | const scriptLookup = new Map<number, ScriptFile>(); |
| 57 | |
| 58 | let loaded = 0; |
| 59 | for (let id = 0; id < entries; id++) { |
| 60 | const size = idx.g4s(); |
| 61 | if (size === 0) { |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | try { |
| 66 | const data: Uint8Array = new Uint8Array(size); |
| 67 | dat.gdata(data, 0, data.length); |
| 68 | const script = ScriptFile.decode(id, new Packet(data)); |
| 69 | scripts[id] = script; |
| 70 | scriptNames.set(script.name, id); |
| 71 | |
| 72 | // add the script to lookup table if the value isn't -1 |
| 73 | if (script.info.lookupKey !== 0xffffffff) { |
| 74 | scriptLookup.set(script.info.lookupKey, script); |
| 75 | } |
| 76 | |
| 77 | loaded++; |
| 78 | } catch (err) { |
| 79 | console.error(err); |
| 80 | printWarning(`Warning: Failed to load script ${id}, something may have been partially written`); |
| 81 | return -1; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | ScriptProvider.scripts = scripts; |
| 86 | ScriptProvider.scriptNames = scriptNames; |
| 87 | ScriptProvider.scriptLookup = scriptLookup; |
| 88 | return loaded; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Finds a script by `id`. |
no test coverage detected