-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
1,768 additions
and
449 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,8 @@ linters: | |
- goconst | ||
- exportloopref | ||
- durationcheck | ||
|
||
linters-settings: | ||
goconst: | ||
min-len: 4 | ||
min-occurrences: 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Vilsol/go-mlog/decompiler" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"io/ioutil" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(decompileCmd) | ||
} | ||
|
||
var decompileCmd = &cobra.Command{ | ||
Use: "decompile [flags] <program>", | ||
Short: "Decompile MLOG to Go", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
result, err := decompiler.MLOGToGolangFile(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if output := viper.GetString("output"); output != "" { | ||
if err := ioutil.WriteFile(output, []byte(result), 0644); err != nil { | ||
return err | ||
} | ||
} else { | ||
fmt.Println(result) | ||
} | ||
|
||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/Vilsol/go-mlog/checker" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(typingsCmd) | ||
} | ||
|
||
var typingsCmd = &cobra.Command{ | ||
Use: "typings", | ||
Short: "Output typings as JSON", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
result := checker.GetSerializablePackages() | ||
marshal, _ := json.Marshal(result) | ||
fmt.Println(string(marshal)) | ||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package decompiler | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"github.com/Vilsol/go-mlog/mlog" | ||
"go/ast" | ||
"go/printer" | ||
"go/token" | ||
"io/ioutil" | ||
"strconv" | ||
) | ||
|
||
const LabelPrefix = "jumpTo" | ||
|
||
func MLOGToGolangFile(fileName string) (string, error) { | ||
file, err := ioutil.ReadFile(fileName) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return MLOGToGolangBytes(file) | ||
} | ||
|
||
func MLOGToGolangBytes(input []byte) (string, error) { | ||
return MLOGToGolang(string(input)) | ||
} | ||
|
||
func MLOGToGolang(input string) (string, error) { | ||
lines, _ := mlog.Tokenize(input) | ||
|
||
global := &Global{ | ||
Lines: lines, | ||
Labels: make(map[string]*mlog.MLOGLine), | ||
MappedLines: make(map[int]*mlog.MLOGLine), | ||
Variables: make(map[string]string), | ||
} | ||
|
||
allJumpTargets := make(map[int]bool) | ||
for _, line := range lines { | ||
// Detect "set @counter" and early exit | ||
// TODO Benchmark | ||
if len(line.Instruction) >= 2 && line.Instruction[0] == "set" && line.Instruction[1] == "@counter" { | ||
return "", errors.New("decompiler does not support programs that set @counter variable") | ||
} | ||
|
||
tempLine := line | ||
global.MappedLines[line.SourceLine] = &tempLine | ||
|
||
if line.Label != "" { | ||
global.Labels[line.Label] = &tempLine | ||
} | ||
|
||
if len(line.Instruction) > 0 { | ||
translator, ok := funcTranslations[line.Instruction[0]] | ||
if !ok { | ||
return "", errors.New("unknown statement: " + line.Instruction[0]) | ||
} | ||
|
||
if translator.Preprocess != nil { | ||
jumpTargets, err := translator.Preprocess(line.Instruction[1:]) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
for _, target := range jumpTargets { | ||
allJumpTargets[target] = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
allImports := make(map[string]bool) | ||
statements := make([]ast.Stmt, 0) | ||
for _, line := range lines { | ||
// TODO Comments | ||
if len(line.Instruction) > 0 { | ||
translator := funcTranslations[line.Instruction[0]] | ||
|
||
statement, imports, err := translator.Translate(line.Instruction[1:], global) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
for _, s := range imports { | ||
allImports[s] = true | ||
} | ||
|
||
labelName := line.Label | ||
if labelName == "" { | ||
if _, ok := allJumpTargets[line.SourceLine]; ok { | ||
labelName = LabelPrefix + strconv.Itoa(line.SourceLine) | ||
} | ||
} | ||
|
||
if labelName != "" && len(statement) > 0 { | ||
statements = append(statements, &ast.LabeledStmt{ | ||
Label: ast.NewIdent(labelName), | ||
Stmt: statement[0], | ||
}) | ||
statements = append(statements, statement[1:]...) | ||
} else { | ||
statements = append(statements, statement...) | ||
} | ||
} | ||
} | ||
|
||
importSpecs := make([]ast.Spec, 0) | ||
for s := range allImports { | ||
importSpecs = append(importSpecs, &ast.ImportSpec{ | ||
Path: &ast.BasicLit{ | ||
Value: "\"" + s + "\"", | ||
}, | ||
}) | ||
} | ||
|
||
mainFile := &ast.File{ | ||
Name: ast.NewIdent("main"), | ||
Decls: []ast.Decl{ | ||
&ast.GenDecl{ | ||
Tok: token.IMPORT, | ||
Specs: importSpecs, | ||
}, | ||
&ast.FuncDecl{ | ||
Name: ast.NewIdent("main"), | ||
Type: &ast.FuncType{}, | ||
Body: &ast.BlockStmt{ | ||
List: statements, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
var buf bytes.Buffer | ||
fileSet := token.NewFileSet() | ||
if err := printer.Fprint(&buf, fileSet, mainFile); err != nil { | ||
return "", err | ||
} | ||
|
||
return buf.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package decompiler | ||
|
||
import "go/ast" | ||
|
||
type PreprocessFunc func(args []string) ([]int, error) | ||
type TranslateFunc func(args []string, global *Global) ([]ast.Stmt, []string, error) | ||
|
||
type Translator struct { | ||
Preprocess PreprocessFunc | ||
Translate TranslateFunc | ||
} | ||
|
||
var funcTranslations = map[string]Translator{} | ||
|
||
func RegisterFuncTranslation(name string, translator Translator) { | ||
if _, ok := funcTranslations[name]; ok { | ||
panic("Function translation already exists: " + name) | ||
} | ||
|
||
funcTranslations[name] = translator | ||
} |
Oops, something went wrong.