diff --git a/src/clientscript/ast.ts b/src/clientscript/ast.ts index 92b854d..983e177 100644 --- a/src/clientscript/ast.ts +++ b/src/clientscript/ast.ts @@ -1017,18 +1017,21 @@ function fixControlFlow(ast: AstNode, scriptjson: clientscript) { } export class ClientScriptFunction extends AstNode { - name: string; + scriptid: number; returntype: StackList; argtype: StackList; - constructor(name: string, returntype: StackList, argtype: StackList) { + constructor(scriptid: number, returntype: StackList, argtype: StackList) { super(0); - this.name = name; + this.scriptid = scriptid; this.returntype = returntype; this.argtype = argtype; } getCode(calli: ClientscriptObfuscation, indent: number) { - let res = `${codeIndent(indent)}function ${this.name.match(/^\d/) ? `script${this.name}` : this.name}(${this.argtype.toTypeScriptVarlist()}):${this.returntype.toTypeScriptReturnType()}`; + let meta = calli.scriptargs.get(this.scriptid); + let res = ""; + res += `//${meta?.scriptname ?? "unknown name"}\n`; + res += `${codeIndent(indent)}function script${this.scriptid}(${this.argtype.toTypeScriptVarlist()}):${this.returntype.toTypeScriptReturnType()}`; res += this.children[0].getCode(calli, indent); return res; } @@ -1349,7 +1352,7 @@ export async function renderClientScript(source: CacheFileSource, buf: Buffer, f let returntype = getReturnType(calli, script.opcodedata); let argtype = getArgType(script); - let func = new ClientScriptFunction(fileid + "", returntype, new StackList([argtype])); + let func = new ClientScriptFunction(fileid, returntype, new StackList([argtype])); let res = ""; if (full) { func.push(program); diff --git a/src/clientscript/callibrator.ts b/src/clientscript/callibrator.ts index 68dcb9b..5431229 100644 --- a/src/clientscript/callibrator.ts +++ b/src/clientscript/callibrator.ts @@ -14,6 +14,7 @@ import { params } from "../../generated/params"; import { clientscriptParser } from "./codeparser"; import { ClientScriptOp, ImmediateType, StackConstants, StackDiff, StackInOut, StackList, StackType, knownClientScriptOpNames, namedClientScriptOps, variableSources } from "./definitions"; import { dbtables } from "../../generated/dbtables"; +import { reverseHashes } from "../libs/rshashnames"; globalThis.parser = clientscriptParser; @@ -70,6 +71,7 @@ export class OpcodeInfo { export type ScriptCandidate = { id: number, + scriptname: string, solutioncount: number, buf: Buffer, script: clientscriptdata, @@ -262,6 +264,7 @@ export class ClientscriptObfuscation { varmeta: Map ? T : never> }> = new Map(); parammeta = new Map(); scriptargs = new Map(buf => ({ id: entry.minor, + scriptname: reverseHashes.get(index[entry.minor].name!) ?? "", solutioncount: 0, buf, script: parse.clientscriptdata.read(buf, source), @@ -429,7 +434,7 @@ export class ClientscriptObfuscation { async runCallibrationFrom(previousCallibration: ClientscriptObfuscation) { let refscript = await previousCallibration.generateDump(); await this.runCallibration(refscript); - console.log("callibrated", this); + // console.log("callibrated", this); } setNonObbedMappings() { //originally all <0x80 were ints @@ -611,6 +616,7 @@ function parseCandidateContents(calli: ClientscriptObfuscation) { cand.returnType = getReturnType(calli, cand.scriptcontents); cand.argtype = getArgType(cand.script); calli.scriptargs.set(cand.id, { + scriptname: cand.scriptname, args: cand.argtype, returns: cand.returnType, arglist: cand.argtype.getArglist(), diff --git a/src/clientscript/codeparser.ts b/src/clientscript/codeparser.ts index 6843430..64ff801 100644 --- a/src/clientscript/codeparser.ts +++ b/src/clientscript/codeparser.ts @@ -465,7 +465,8 @@ export function clientscriptParser(deob: ClientscriptObfuscation) { yield whitespace; let ops: AstNode[] = yield statementlist; let codeblock = new CodeBlockNode(-1, -1, ops); - let res = new ClientScriptFunction(name, returntype, argtype); + let namematch = name.match(/^script(\d+)$/); + let res = new ClientScriptFunction((namematch ? +namematch[1] : -1), returntype, argtype); res.push(codeblock); return res; }