| 56 | |
| 57 | // decodes the same binary format as clientscript2 |
| 58 | static decode(id: number, stream: Packet): ScriptFile { |
| 59 | const length: number = stream.data.length; |
| 60 | if (length < 16) { |
| 61 | throw new Error('Invalid script file (minimum length)'); |
| 62 | } |
| 63 | |
| 64 | stream.pos = length - 2; |
| 65 | |
| 66 | const trailerLen = stream.g2(); |
| 67 | const trailerPos = length - trailerLen - 12 - 2; |
| 68 | |
| 69 | if (trailerPos < 0 || trailerPos >= length) { |
| 70 | throw new Error('Invalid script file (bad trailer pos)'); |
| 71 | } |
| 72 | |
| 73 | stream.pos = trailerPos; |
| 74 | |
| 75 | const script = new ScriptFile(id); |
| 76 | const _instructions = stream.g4s(); // we don't need to preallocate anything in JS, but still need to read it |
| 77 | script.intLocalCount = stream.g2(); |
| 78 | script.stringLocalCount = stream.g2(); |
| 79 | script.intArgCount = stream.g2(); |
| 80 | script.stringArgCount = stream.g2(); |
| 81 | |
| 82 | const switches = stream.g1(); |
| 83 | for (let i = 0; i < switches; i++) { |
| 84 | const count = stream.g2(); |
| 85 | const table: SwitchTable = []; |
| 86 | |
| 87 | for (let j = 0; j < count; j++) { |
| 88 | const key = stream.g4s(); |
| 89 | const offset = stream.g4s(); |
| 90 | table[key] = offset; |
| 91 | } |
| 92 | |
| 93 | script.switchTables[i] = table; |
| 94 | } |
| 95 | |
| 96 | stream.pos = 0; |
| 97 | script.info.scriptName = stream.gjstr(0); |
| 98 | script.info.sourceFilePath = stream.gjstr(0); |
| 99 | script.info.lookupKey = stream.g4s(); |
| 100 | const parameterTypeCount = stream.g1(); |
| 101 | for (let i = 0; i < parameterTypeCount; i++) { |
| 102 | script.info.parameterTypes.push(stream.g1()); |
| 103 | } |
| 104 | |
| 105 | const lineNumberTableLength = stream.g2(); |
| 106 | for (let i = 0; i < lineNumberTableLength; i++) { |
| 107 | script.info.pcs.push(stream.g4s()); |
| 108 | script.info.lines.push(stream.g4s()); |
| 109 | } |
| 110 | |
| 111 | let instr = 0; |
| 112 | while (trailerPos > stream.pos) { |
| 113 | const opcode = stream.g2(); |
| 114 | |
| 115 | if (opcode === ScriptOpcode.PUSH_CONSTANT_STRING) { |