-
Notifications
You must be signed in to change notification settings - Fork 16
feat: mangler script (experimental) #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import argparse | ||
| import os | ||
| import sys | ||
| import subprocess | ||
| from anytree import Node, RenderTree, Resolver | ||
|
|
||
| # @author Dexrn ZacAttack | ||
| # my least favorite language | ||
|
|
||
| # https://www.geeksforgeeks.org/python/command-line-arguments-in-python/#using-argparse-module | ||
| parser = argparse.ArgumentParser(description = "Allows you to mangle symbols with our modified toolchain") | ||
| parser.add_argument("-d", "--decl", help = "Mangle a function declaration") | ||
| parser.add_argument("-g", "--gvar", help = "Mangle a global variable declaration") # bruh global is reserved in python | ||
| parser.add_argument("-i", "--imports", nargs='+', type=str, help = "Import a file") | ||
| parser.add_argument("-fwd", "--forwards", nargs='+', type=str, help = "Forward classes") | ||
| parser.add_argument("-s", "--static", type=bool, help = "Mark a declaration as static") | ||
| args = parser.parse_args() | ||
|
|
||
| if len(sys.argv) == 1: | ||
| parser.print_help(sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| sd = os.path.dirname(os.path.abspath(__file__)) | ||
| d = 'mangler' | ||
| f = os.path.join(sd, d, 'temp.cpp') | ||
| o = os.path.join(sd, d, 'a.out') | ||
| dt = "int" | ||
| dv = "0" | ||
| res = Resolver('name', relax=True) | ||
| clist = Node("Classes") | ||
|
|
||
| os.makedirs(d, exist_ok=True) | ||
|
|
||
|
|
||
| with open(f, 'w') as t: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move each step of the process into its own function |
||
| t.write("// Autogenerated by mangle.py\n") | ||
| t.write("#include <string>\n\n") | ||
| if args.imports: | ||
| for i in args.imports: | ||
| if i == "string": | ||
| continue; | ||
|
|
||
| t.write(f"#include <{i}>\n") | ||
|
|
||
| t.write(f"#define DEFAULT_TYPE {dt}\n") | ||
|
|
||
| if args.forwards: | ||
| for fwd in args.forwards: | ||
| p = fwd.split("::") | ||
| parent = clist | ||
| for cl in p: | ||
| t.write(f"class {cl} {{\n") | ||
| t.write("public:\n") | ||
| ex = next((c for c in parent.children if c.name == cl), None) | ||
| if ex: | ||
| node = ex | ||
| else: | ||
| node = Node(cl, line=t.tell(), parent=parent) | ||
| parent = node | ||
|
|
||
| for cl in p: | ||
| t.write("\n};") | ||
|
|
||
| if args.decl: | ||
| with open(f, 'a') as t: | ||
| p = args.decl[:args.decl.find("(")].split("::") | ||
| n = p[-1] | ||
| cls = p[:-1] | ||
| parent = clist | ||
| found = [] | ||
| for cl in cls: | ||
| ex = next((c for c in parent.children if c.name == cl), None) | ||
| if ex: | ||
| found.append(ex) | ||
| parent = ex | ||
| else: | ||
| break | ||
|
|
||
| parent = clist | ||
|
|
||
| for cl in cls: | ||
| if cl in [f.name for f in found]: | ||
| t.seek(found[[f.name for f in found].index(cl)].line) | ||
|
|
||
|
|
||
| if cl not in [f.name for f in found]: | ||
| t.write(f"class {cl} {{\n") | ||
| ex = next((c for c in parent.children if c.name == cl), None) | ||
| if ex: | ||
| node = ex | ||
| else: | ||
| node = Node(cl, line=t.tell(), parent=parent) | ||
| parent = node | ||
|
|
||
| t.write("__attribute__((used))\n") | ||
| if args.static: | ||
| t.write(f" {"static DEFAULT_TYPE " if p[-2] != n else ""}{n}{args.decl[args.decl.find('('):]} {{}}") | ||
| else: | ||
| t.write(f" {"DEFAULT_TYPE " if p[-2] != n else ""}{n}{args.decl[args.decl.find('('):]} {{}}") | ||
|
|
||
|
|
||
| for cl in cls: | ||
| if cl not in [f.name for f in found]: | ||
| t.write("\n};") | ||
| elif args.gvar: | ||
| with open(f, 'a') as t: | ||
| p = args.gvar.split("::") | ||
| n = p[-1] | ||
| cls = p[:-1] | ||
| parent = clist | ||
| found = [] | ||
| for cl in cls: | ||
| ex = next((c for c in parent.children if c.name == cl), None) | ||
| if ex: | ||
| found.append(ex) | ||
| parent = ex | ||
| else: | ||
| break | ||
|
|
||
| parent = clist | ||
|
|
||
| for cl in cls: | ||
| if cl in [f.name for f in found]: | ||
| t.seek(found[[f.name for f in found].index(cl)].line) | ||
|
|
||
|
|
||
| if cl not in [f.name for f in found]: | ||
| t.write(f"class {cl} {{\n") | ||
| ex = next((c for c in parent.children if c.name == cl), None) | ||
| if ex: | ||
| node = ex | ||
| else: | ||
| node = Node(cl, line=t.tell(), parent=parent) | ||
| parent = node | ||
|
|
||
| t.write(f"static DEFAULT_TYPE {n};") | ||
|
|
||
| for cl in cls: | ||
| if cl not in [f.name for f in found]: | ||
| t.write("\n};") | ||
|
|
||
| t.write("\n__attribute__((used))\n") | ||
| t.write(f"DEFAULT_TYPE {args.gvar} = {dv};") | ||
|
|
||
| c = subprocess.run([os.path.join(sd, "../toolchain/clang-4.0.1/bin/clang++"), f"-I{os.path.join(sd, '../src')}", f"-I{os.path.join(sd, '../src/Minecraft.World')}", f"-I{os.path.join(sd, '../src/4JLibraries_Source')}", f"-I{os.path.join(sd, '../src/Minecraft.Client')}", f"-I{os.path.join(sd, '../src/PlatformLibraries_Source')}", f"-I{os.path.join(sd, '../lib/nnheaders/include')}", "-stdlib=libc++", "-O3", "-g2", "-std=c++1z", "-fno-rtti", "-fno-exceptions", "-fno-strict-aliasing", "-c", "-Wno-return-type", f, "-o", o], | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please write some small infrastructure around testing this script that can be used in a
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll look at doing this when available |
||
| capture_output=True, text=True) | ||
|
|
||
| if (c.stderr): | ||
| print(c.stderr) | ||
|
|
||
| n = subprocess.run(["nm", o], | ||
| capture_output=True, text=True) | ||
|
|
||
| print(n.stdout) | ||
| if (n.stderr): | ||
| print(n.stderr) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move argument parsing into a
mainfunction and call it in anif __name__ == "__main__".