Compare commits

..

2 Commits

12 changed files with 147 additions and 230 deletions

View File

@ -66,7 +66,14 @@ export function toHex(x: number) {
return '0x' + x.toString(16);
}
export function assertSize(name: string, size: number) {
return `static_assert(sizeof(${name}) == ${toHex(size)}, "Invalid Size");\n`;
let out = '';
// Define Size Macro
const macro = toUpperSnakeCase(name) + '_SIZE';
out += `#define ${macro} ${toHex(size)}\n`;
// Check Size
out += `static_assert(sizeof(${name}) == ${macro}, "Invalid Size");\n`;
// Return
return out;
}
export function safeParseInt(str: string) {
const x = parseInt(str);
@ -113,6 +120,4 @@ export function removeFirstArg(args: string) {
index++;
}
return '(' + args.substring(index).trim();
}
export const ORIGINAL_SUFFIX = '_backup_do_not_use';
export const VTABLE_ADDR_SUFFIX = `_vtable_addr`;
}

View File

@ -1,5 +1,5 @@
import * as fs from 'node:fs';
import { STRUCTURE_FILES, EXTENSION } from './common';
import { STRUCTURE_FILES, EXTENSION, INDENT } from './common';
import { getStructure } from './map';
import { Struct } from './struct';
@ -130,7 +130,11 @@ function makeHeaderPart() {
}
// Return
return structures;
let result = '';
result += '// Init\n';
result += 'void init_symbols();\n\n';
result += structures;
return result;
}
// Create Main Header
@ -176,6 +180,7 @@ function makeCompiledCode(output: string) {
// Generate
let declarations = '';
let init = '';
let isFirst = true;
for (const structure of structureObjects) {
const name = structure.getName();
@ -183,11 +188,14 @@ function makeCompiledCode(output: string) {
isFirst = false;
} else {
declarations += '\n';
init += '\n';
}
declarations += `// ${name}\n`;
init += `${INDENT}// ${name}\n`;
try {
const code = structure.generateCode();
declarations += code;
declarations += code.functions;
init += code.init;
} catch (e) {
console.log(`Error Generating Code: ${name}: ${e instanceof Error ? e.stack : e}`);
process.exit(1);
@ -197,7 +205,11 @@ function makeCompiledCode(output: string) {
// Write
let result = '';
result += `#include "${fs.realpathSync(headerOutput)}"\n`;
result += '\n#include <cstring>\n\n';
result += '\n#include <cstring>\n';
result += '\n// Init\n';
result += 'void init_symbols() {\n';
result += init;
result += '}\n\n';
result += declarations;
fs.writeFileSync(output, result);
}

View File

@ -1,4 +1,4 @@
import { INDENT, formatType, getArgNames, prependArg } from './common';
import { INDENT, formatType, getArgNames } from './common';
export class Method {
readonly self: string;
@ -43,21 +43,11 @@ export class Method {
// Fancy Overwrite Does Not Support Varargs
if (!this.hasVarargs) {
// Add Overwrite Typedef
const type = this.getType();
const overwriteType = `__overwrite_${type}`;
out += `typedef ${returnType}(*${overwriteType})${prependArg(this.args, type + ' original')};\n`;
// Overwrite Helper
const helperName = '__create_overwrite_helper_for_' + this.getName();
out += `template <int>\n`;
out += `static ${type} ${helperName}(${overwriteType} method, ${type} original) {\n`;
out += `${INDENT}static ${overwriteType} stored_method = method;\n`;
out += `${INDENT}static ${type} stored_original = original;\n`;
out += `${INDENT}return []${this.args} { \\\n`;
out += `${INDENT}${INDENT}${returnType.trim() === 'void' ? '' : 'return '}stored_method(${['stored_original'].concat(getArgNames(this.args)).join(', ')}); \\\n`;
out += `${INDENT}};\n`;
out += `}\n`;
out += `#define ${helperName} ${helperName}<__COUNTER__>\n`;
out += `#define __overwrite_helper_for_${this.getName()}(method, original) \\\n`;
out += `${INDENT}[]${this.args} { \\\n`;
out += `${INDENT}${INDENT}${returnType.trim() === 'void' ? '' : 'return '}method(${['original'].concat(getArgNames(this.args)).join(', ')}); \\\n`;
out += `${INDENT}}\n`;
}
// Return
@ -66,6 +56,18 @@ export class Method {
// Generate Variable Definition
generateDefinition(nameSuffix?: string) {
return `${this.getType()} ${this.getName()}${nameSuffix !== undefined ? nameSuffix : ''}`;
return `${this.getType()} ${this.getName()}${nameSuffix !== undefined ? nameSuffix : ''};\n`;
}
// Generate "New Method" Test
generateNewMethodTest(parent: string | null, prefix: string, suffix: string) {
let out = `#define __is_new_method_${this.getName()}() (`;
if (!this.isInherited) {
out += 'true';
} else {
out += `((void *) ${prefix}${this.getName()}${suffix}) != ((void *) ${prefix}${this.getNameWithCustomSelf(parent!)}${suffix})`;
}
out += ')\n';
return out;
}
}

View File

@ -34,14 +34,8 @@ export class Property {
}
return name;
}
#fullName(separator: string) {
return this.#self + separator + this.name();
}
fullName() {
return this.#fullName('_');
}
prettyName() {
return this.#fullName('::');
return `${this.#self}_${this.name()}`;
}
rawType() {
return this.#type;
@ -54,8 +48,13 @@ export class StaticProperty extends Property {
super(address, type, name, self);
}
// Reference
referenceDefinition(addSelf: boolean) {
return `${this.type()} &${addSelf ? this.prettyName() : this.name()}`;
// Generate Variable Definition
globalPointerDefinition() {
return `${this.type()} *${this.fullName()}_pointer;\n`;
}
// Generate Macro
macro() {
return `#define ${this.fullName()} (*${this.fullName()}_pointer)\n`;
}
}

View File

@ -1,4 +1,4 @@
import { INDENT, STRUCTURE_FILES, toHex, assertSize, formatType, getArgNames, removeFirstArg, ORIGINAL_SUFFIX } from './common';
import { INDENT, STRUCTURE_FILES, toHex, assertSize, formatType, getArgNames, removeFirstArg } from './common';
import { Method } from './method';
import { Property, StaticProperty } from './property';
import { VTable } from './vtable';
@ -163,7 +163,7 @@ export class Struct {
const shortName = method.shortName;
const fullName = method.getName();
const args = getArgsOuter(method);
out += `${INDENT}${method.isStatic ? 'static ' : ''}inline ${formatType(returnType)}${shortName}${args} { \\\n`;
out += `${INDENT}inline ${formatType(returnType)}${shortName}${args} { \\\n`;
out += `${INDENT}${INDENT}${returnType.trim() === 'void' ? '' : 'return '}${fullName}(${getArgsInner(method)});\n`;
out += `${INDENT}}\n`;
}
@ -193,20 +193,23 @@ export class Struct {
// Static Properties
for (const property of this.#staticProperties) {
out += property.typedef();
out += `extern ${property.globalPointerDefinition()}`;
out += property.macro();
}
// Methods
for (const method of this.#methods) {
if (!method.isInherited) {
out += method.generateTypedef();
out += `extern ${method.generateDefinition()};\n`;
out += `extern ${method.generateDefinition(ORIGINAL_SUFFIX)};\n`;
out += `extern ${method.generateDefinition()}`;
out += `extern ${method.generateDefinition('_unedited')}`;
out += method.generateNewMethodTest(this.#directParent, '', '');
}
}
// VTable
if (this.#vtable !== null) {
out += this.#vtable.generate();
out += this.#vtable.generate(this.#directParent);
}
// Property Typedefs
@ -218,16 +221,6 @@ export class Struct {
out += `struct ${this.#name} {\n`;
out += this.#generateProperties();
out += this.#generateMethods();
for (const property of this.#staticProperties) {
// Static Property References
out += `${INDENT}static ${property.referenceDefinition(false)};\n`;
}
if (this.#size === null) {
// Prevent Manually Copying/Allocating Structure With Undefined
out += `${INDENT}${this.#name}() = delete;\n`;
out += `${INDENT}${this.#name}(const ${this.#name} &) = delete;\n`;
out += `${INDENT}${this.#name} &operator=(const ${this.#name} &) = delete;\n`;
}
out += `};\n`;
// Sanity Check Offsets
@ -243,35 +236,52 @@ export class Struct {
out += assertSize(this.#name, this.#size);
}
// Allocation Function
if (this.#size !== null) {
out += `${this.#name} *alloc_${this.#name}();\n`;
}
// Return
return out;
}
// Generate Compiled Code
generateCode() {
let out = '';
let declarations = '';
let init = '';
// Static Properties
for (const property of this.#staticProperties) {
out += `${property.referenceDefinition(true)} = *(${property.type()} *) ${toHex(property.offset)};\n`;
init += `${INDENT}${property.fullName()}_pointer = (${property.type()} *) ${toHex(property.offset)};\n`;
declarations += property.globalPointerDefinition();
}
// Methods
for (const method of this.#methods) {
if (!method.isInherited) {
out += `${method.generateDefinition()} = (${method.getType()}) ${toHex(method.address)};\n`;
out += `${method.generateDefinition(ORIGINAL_SUFFIX)} = ${method.getName()};\n`;
init += `${INDENT}${method.getName()} = (${method.getType()}) ${toHex(method.address)};\n`;
declarations += method.generateDefinition();
init += `${INDENT}${method.getName()}_unedited = (${method.getType()}) ${toHex(method.address)};\n`;
declarations += method.generateDefinition('_unedited');
}
}
// VTable
if (this.#vtable !== null) {
const vtable = this.#vtable.generateCode(this.#directParent);
out += vtable;
const vtable = this.#vtable.generateCode();
declarations += vtable.declarations;
init += vtable.init;
}
// Allocation Function
if (this.#size !== null) {
declarations += `${this.#name} *alloc_${this.#name}() {\n`;
declarations += `${INDENT}return new ${this.#name};\n`;
declarations += '}\n';
}
// Return
return out;
return {functions: declarations, init};
}
// Set Direct Parent (Used For "New Method" Testing)

View File

@ -1,8 +1,7 @@
import { INDENT, ORIGINAL_SUFFIX, POINTER_SIZE, RTTI_SIZE, VTABLE_ADDR_SUFFIX, assertSize, getSelfArg, toHex } from './common';
import { INDENT, POINTER_SIZE, RTTI_SIZE, assertSize, getSelfArg, toHex } from './common';
import { Method } from './method';
import { Property } from './property';
const structuresWithVTableAddress: string[] = [];
export class VTable {
readonly #self: string;
#address: number | null;
@ -25,7 +24,6 @@ export class VTable {
// Setters
setAddress(address: number) {
this.#address = address;
structuresWithVTableAddress.push(this.#self);
}
setSize(size: number) {
this.#size = size;
@ -86,36 +84,8 @@ export class VTable {
return out;
}
// Overwritable Test (Only For Virtual Methods)
generateOverwritableTestDefinition(method: Method) {
return `bool __is_overwritable_${method.getName()}()`;
}
generateOverwritableTest(method: Method, parent: string | null) {
// Test If Overwriting Method Would Also Modify Parent
let out = '';
out += this.generateOverwritableTestDefinition(method) + ' {\n';
let ret: string;
if (method.isInherited) {
if (structuresWithVTableAddress.includes(parent!)) {
// Check If Method Differs From Parent
out += `${INDENT}void *parent = (void *) *${method.getNameWithCustomSelf(parent!)}${VTABLE_ADDR_SUFFIX};\n`;
out += `${INDENT}void *child = (void *) *${method.getName()}${VTABLE_ADDR_SUFFIX};\n`;
ret = 'parent != child';
} else {
// Unable To Determine
ret = 'false';
}
} else {
// No Parent
ret = 'true';
}
out += `${INDENT}return ${ret};\n`;
out += '}\n';
return out;
}
// Generate Header Code
generate() {
generate(directParent: string | null) {
let out = '';
// Check
@ -158,12 +128,10 @@ export class VTable {
for (let i = 0; i < methods.length; i++) {
const info = methods[i];
if (info) {
const name = info.getName();
const type = info.getType();
const pointerType = `${type} *`;
out += `extern ${pointerType}${name}${VTABLE_ADDR_SUFFIX};\n`;
out += this.generateOverwritableTestDefinition(info) + ';\n';
out += `extern ${info.generateDefinition(ORIGINAL_SUFFIX)};\n`;
const type = `${info.getType()} *`;
out += `extern ${type}${info.getName()}_vtable_addr;\n`;
out += `extern ${info.generateDefinition('_non_virtual')}`;
out += info.generateNewMethodTest(directParent, '*', '_vtable_addr');
}
}
}
@ -178,8 +146,9 @@ export class VTable {
}
// Generate Compiled Code
generateCode(directParent: string | null) {
let out = '';
generateCode() {
let declarations = '';
let init = '';
// Check
this.#check();
@ -187,40 +156,40 @@ export class VTable {
// Pointers
if (this.#address !== null) {
// Base
out += `${this.#getName()} *${this.#getName()}_base = (${this.#getName()} *) ${toHex(this.#address)};\n`;
init += `${INDENT}${this.#getName()}_base = (${this.#getName()} *) ${toHex(this.#address)};\n`;
declarations += `${this.#getName()} *${this.#getName()}_base;\n`;
// Methods
const methods = this.getMethods();
for (let i = 0; i < methods.length; i++) {
const info = methods[i];
if (info) {
const vtableAddress = this.#address + (i * POINTER_SIZE);
const name = info.getName();
const type = info.getType();
const pointerType = `${type} *`;
out += `${pointerType}${name}${VTABLE_ADDR_SUFFIX} = (${pointerType}) ${toHex(vtableAddress)};\n`;
out += this.generateOverwritableTest(info, directParent);
out += `${info.generateDefinition(ORIGINAL_SUFFIX)} = *${name}${VTABLE_ADDR_SUFFIX};\n`;
const type = `${info.getType()} *`;
init += `${INDENT}${info.getName()}_vtable_addr = (${type}) ${toHex(vtableAddress)};\n`;
declarations += `${type}${info.getName()}_vtable_addr;\n`;
init += `${INDENT}${info.getName()}_non_virtual = *${info.getName()}_vtable_addr;\n`;
declarations += info.generateDefinition('_non_virtual');
}
}
}
// Duplication Method
if (this.#size !== null) {
out += `${this.#getName()} *dup_${this.#getName()}(${this.#getName()} *vtable) {\n`;
out += `${INDENT}uchar *real_vtable = (uchar *) vtable;\n`;
out += `${INDENT}real_vtable -= ${RTTI_SIZE};\n`;
out += `${INDENT}size_t real_vtable_size = sizeof(${this.#getName()}) + ${RTTI_SIZE};\n`;
out += `${INDENT}uchar *new_vtable = (uchar *) ::operator new(real_vtable_size);\n`;
out += `${INDENT}if (new_vtable == NULL) {\n`;
out += `${INDENT}${INDENT}return NULL;\n`;
out += `${INDENT}}\n`;
out += `${INDENT}memcpy((void *) new_vtable, (void *) real_vtable, real_vtable_size);\n`;
out += `${INDENT}new_vtable += ${RTTI_SIZE};\n`;
out += `${INDENT}return (${this.#getName()} *) new_vtable;\n`;
out += '}\n';
declarations += `${this.#getName()} *dup_${this.#getName()}(${this.#getName()} *vtable) {\n`;
declarations += `${INDENT}uchar *real_vtable = (uchar *) vtable;\n`;
declarations += `${INDENT}real_vtable -= ${RTTI_SIZE};\n`;
declarations += `${INDENT}size_t real_vtable_size = sizeof(${this.#getName()}) + ${RTTI_SIZE};\n`;
declarations += `${INDENT}uchar *new_vtable = (uchar *) ::operator new(real_vtable_size);\n`;
declarations += `${INDENT}if (new_vtable == NULL) {\n`;
declarations += `${INDENT}${INDENT}return NULL;\n`;
declarations += `${INDENT}}\n`;
declarations += `${INDENT}memcpy((void *) new_vtable, (void *) real_vtable, real_vtable_size);\n`;
declarations += `${INDENT}new_vtable += ${RTTI_SIZE};\n`;
declarations += `${INDENT}return (${this.#getName()} *) new_vtable;\n`;
declarations += '}\n';
}
// Return
return out;
return {declarations, init};
}
}

View File

@ -0,0 +1,34 @@
syntax def "\.def$"
comment "//"
# Mistakes
# Missing semicolon
color red "[^;]$"
# Missing type
color red "^(((static|virtual)-)?method|property|static-property(-array)?) [a-zA-Z_][a-zA-Z0-9_]* ?(\(|=)"
# Missing prefix
color red "^[^ ]+"
# Missing vtable
color red "^(vtable(-size|-destructor-offset))? .+$"
# Reset
color normal "(\(|\))"
# Commands
color magenta "^(extends|size|vtable(-size|-destructor-offset)?|property|static-property|((static|virtual)-)?method|constructor)\>"
# Types
color green "\<((u?(char|short|int))|float|bool|void|std::(string|vector|map))\>"
# Numbers
color yellow "0x[a-f0-9]+"
# Non-hex numbers
color red " [0-9][a-f0-9]+;"
# Comments
color brightblue "//.*"
# Whitespace.
color normal "[[:space:]]+"
# Trailing whitespace.
color ,green "[[:space:]]+$"

View File

@ -1,19 +0,0 @@
syntax def "\.def$"
comment "//"
# Commands
color magenta "\<(extends|size|vtable(-size|-destructor-offset)?|property|static-property|((static|virtual)-)?method|constructor)\>"
# Types
color green "\<(char|uchar|short|ushort|int|uint|float|bool|void|std::(string|vector|map))\>"
# Numbers
color yellow "0x[a-f0-9]+"
# Comments
color brightblue "//.*"
# Whitespace.
color normal "[[:space:]]+"
# Trailing whitespace.
color ,green "[[:space:]]+$"

View File

@ -1,24 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>Comments</string>
<key>scope</key>
<string>source.toml</string>
<key>settings</key>
<dict>
<key>shellVariables</key>
<array>
<dict>
<key>name</key>
<string>TM_COMMENT_START</string>
<key>value</key>
<string>//</string>
</dict>
</array>
</dict>
<key>uuid</key>
<string>C5C885D7-2733-4632-B709-B5B9DD518F90</string>
</dict>
</plist>

View File

@ -1,55 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>fileTypes</key>
<array>
<string>def</string>
</array>
<key>scopeName</key>
<string>source.symbol-processor</string>
<key>name</key>
<string>Symbol Processor Definition</string>
<key>patterns</key>
<array>
<dict>
<key>name</key>
<string>comment.line.double-slash.symbol-processor</string>
<key>match</key>
<string>//.*$</string>
</dict>
<dict>
<key>name</key>
<string>constant.numeric.symbol-processor.hex</string>
<key>match</key>
<string>\b0[xX][0-9a-fA-F]+</string>
</dict>
<dict>
<key>name</key>
<string>constant.numeric.symbol-processor.decimal</string>
<key>match</key>
<string>\b[0-9]+</string>
</dict>
<dict>
<key>name</key>
<string>keyword.control.symbol-processor</string>
<key>match</key>
<string>\b(extends|size|vtable-size|vtable|property|static-property|method|virtual-method|static-method|constructor|vtable-destructor-offset)\b</string>
</dict>
<dict>
<key>name</key>
<string>storage.type.symbol-processor</string>
<key>match</key>
<string>\b(char|uchar|short|ushort|int|uint|float|bool|void|std::string|std::vector|std::map)\b</string>
</dict>
<dict>
<key>name</key>
<string>keyword.operator.symbol-processor</string>
<key>match</key>
<string>(=|;)</string>
</dict>
</array>
<key>uuid</key>
<string>D44198D4-5AEB-40E5-B4E4-0E11C69FFA42</string>
</dict>
</plist>

View File

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>contactEmailRot13</key>
<string>connor24nolan@live.com</string>
<key>contactName</key>
<string>TheBrokenRail</string>
<key>description</key>
<string>Symbol Processor Definition File</string>
<key>name</key>
<string>Symbol Processor</string>
<key>uuid</key>
<string>8209EEB8-4193-4E63-BDBB-0407E47ADF50</string>
</dict>
</plist>