From a794823db019fab3e67786a3505cff71ee083c02 Mon Sep 17 00:00:00 2001 From: Skillbert Date: Mon, 11 Aug 2025 14:03:56 +0200 Subject: [PATCH] support cs2 dead code decompilation (but still commented out) --- src/clientscript/ast.ts | 20 ++++++++++++++++++++ src/clientscript/codewriter.ts | 3 +++ 2 files changed, 23 insertions(+) diff --git a/src/clientscript/ast.ts b/src/clientscript/ast.ts index 2556bb5..d6ccd23 100644 --- a/src/clientscript/ast.ts +++ b/src/clientscript/ast.ts @@ -124,6 +124,7 @@ export class CodeBlockNode extends AstNode { firstPointer: CodeBlockNode | null = null; lastPointer: CodeBlockNode | null = null; branchEndNode: CodeBlockNode | null = null; + deadcodeSuccessor: CodeBlockNode | null = null; maxEndIndex = -1; knownStackDiff = new StackInOut(new StackList(), new StackList()); @@ -157,6 +158,7 @@ export class CodeBlockNode extends AstNode { } this.possibleSuccessors = block.possibleSuccessors; this.branchEndNode = block.branchEndNode; + this.deadcodeSuccessor = block.deadcodeSuccessor; } findNext() { if (!this.branchEndNode) { @@ -1339,8 +1341,26 @@ export function generateAst(calli: ClientscriptObfuscation, script: clientscript return { sections, rootfunc, subfuncs }; } +function reattachDeadcode(sections: CodeBlockNode[]) { + let reached = new Set([sections[0]]); + for (let item of reached) { + for (let suc of item.possibleSuccessors) { + if (!reached.has(suc)) { + reached.add(suc); + } + } + } + for (let i = 1; i < sections.length; i++) { + let sec = sections[i]; + if (!reached.has(sec)) { + sections[i - 1].deadcodeSuccessor = sec; + } + } +} + export function parseClientScriptIm(calli: ClientscriptObfuscation, script: clientscript, fileid = -1) { let { sections, rootfunc } = generateAst(calli, script, script.opcodedata, fileid); + // reattachDeadcode(sections); let typectx = new ClientScriptSubtypeSolver(); typectx.parseSections(sections); typectx.addKnownFromCalli(calli); diff --git a/src/clientscript/codewriter.ts b/src/clientscript/codewriter.ts index bc53330..a6475e2 100644 --- a/src/clientscript/codewriter.ts +++ b/src/clientscript/codewriter.ts @@ -275,6 +275,9 @@ addWriter(CodeBlockNode, (node, ctx) => { if (node.parent instanceof SwitchStatementNode && node.branchEndNode != null) { code += `${ctx.codeIndent()}break;\n`; } + if (node.deadcodeSuccessor) { + code += ctx.getCode(node.deadcodeSuccessor); + } ctx.popIndent(); code += `${ctx.codeIndent()}}`; }