|
| 1 | +import * as vscode from "vscode" |
| 2 | +import {AssemblyWriter, disassembleRoot, debugSymbols, Cell} from "@tact-lang/opcode" |
| 3 | +import {readFileSync} from "node:fs" |
| 4 | + |
| 5 | +export class BocDecompilerProvider implements vscode.TextDocumentContentProvider { |
| 6 | + private readonly _onDidChange: vscode.EventEmitter<vscode.Uri> = new vscode.EventEmitter() |
| 7 | + public readonly onDidChange: vscode.Event<vscode.Uri> = this._onDidChange.event |
| 8 | + |
| 9 | + public static scheme: string = "boc-decompiled" |
| 10 | + |
| 11 | + private readonly decompileCache: Map<string, string> = new Map() |
| 12 | + |
| 13 | + public provideTextDocumentContent(uri: vscode.Uri): string { |
| 14 | + const bocPath = this.getBocPath(uri) |
| 15 | + |
| 16 | + try { |
| 17 | + const cached = this.decompileCache.get(bocPath) |
| 18 | + if (cached) { |
| 19 | + return cached |
| 20 | + } |
| 21 | + |
| 22 | + const decompiled = this.decompileBoc(bocPath) |
| 23 | + this.decompileCache.set(bocPath, decompiled) |
| 24 | + return decompiled |
| 25 | + } catch (error) { |
| 26 | + const errorMessage = error instanceof Error ? error.message : String(error) |
| 27 | + return this.formatError(errorMessage) |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private getBocPath(uri: vscode.Uri): string { |
| 32 | + console.log("Original URI:", uri.toString()) |
| 33 | + const bocPath = uri.fsPath.replace(".decompiled.fif", "") |
| 34 | + console.log("BOC path:", bocPath) |
| 35 | + return bocPath |
| 36 | + } |
| 37 | + |
| 38 | + private decompileBoc(bocPath: string): string { |
| 39 | + try { |
| 40 | + const content = readFileSync(bocPath).toString("base64") |
| 41 | + const cell = Cell.fromBase64(content) |
| 42 | + const program = disassembleRoot(cell, { |
| 43 | + computeRefs: true, |
| 44 | + }) |
| 45 | + |
| 46 | + const output = AssemblyWriter.write(program, { |
| 47 | + useAliases: true, |
| 48 | + debugSymbols: debugSymbols, |
| 49 | + }) |
| 50 | + |
| 51 | + return this.formatDecompiledOutput(output) |
| 52 | + } catch (error: unknown) { |
| 53 | + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
| 54 | + throw new Error(`Decompilation failed: ${error}`) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private formatDecompiledOutput(output: string): string { |
| 59 | + const header = [ |
| 60 | + "// Decompiled BOC file", |
| 61 | + "// Note: This is auto-generated code", |
| 62 | + "// Time: " + new Date().toISOString(), |
| 63 | + "", |
| 64 | + "", |
| 65 | + ].join("\n") |
| 66 | + |
| 67 | + return header + output |
| 68 | + } |
| 69 | + |
| 70 | + private formatError(error: string): string { |
| 71 | + return [ |
| 72 | + "// Failed to decompile BOC file", |
| 73 | + "// Error: " + error, |
| 74 | + "// Time: " + new Date().toISOString(), |
| 75 | + ].join("\n") |
| 76 | + } |
| 77 | + |
| 78 | + // Метод для обновления содержимого |
| 79 | + public update(uri: vscode.Uri): void { |
| 80 | + this._onDidChange.fire(uri) |
| 81 | + this.decompileCache.delete(this.getBocPath(uri)) |
| 82 | + } |
| 83 | +} |
0 commit comments