MCPcopy Create free account
hub / github.com/MaxBittker/rs-sdk / parse

Method parse

server/engine/src/engine/script/ScriptProvider.ts:41–89  ·  view source on GitHub ↗
(dat: Packet, idx: Packet)

Source from the content-addressed store, hash-verified

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`.

Callers 1

loadMethod · 0.95

Calls 6

printFatalErrorFunction · 0.85
printWarningFunction · 0.85
g4sMethod · 0.80
gdataMethod · 0.45
decodeMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected