Some Changes
This commit is contained in:
parent
6098f57b03
commit
be25749aa4
15
data/out.cpp
Normal file
15
data/out.cpp
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#include "{{ headerPath }}"
|
||||||
|
|
||||||
|
// Thunks
|
||||||
|
template <typename Ret, typename... Args>
|
||||||
|
struct __Thunk<Ret(Args...)> {
|
||||||
|
template <__Function<Ret(Args...)> *func>
|
||||||
|
static Ret call(Args... args) {
|
||||||
|
return func->get_thunk_target()(std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Thunk Enabler
|
||||||
|
thunk_enabler_t thunk_enabler;
|
||||||
|
|
||||||
|
{{ main }}
|
189
data/out.h
Normal file
189
data/out.h
Normal file
@ -0,0 +1,189 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// Check Architecture
|
||||||
|
#ifndef __arm__
|
||||||
|
#error "Symbols Are ARM-Only"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Headers
|
||||||
|
#include <cstddef>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <functional>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
// Virtual Function Information
|
||||||
|
template <typename T>
|
||||||
|
class __Function;
|
||||||
|
template <typename T>
|
||||||
|
class __VirtualFunctionInfo {
|
||||||
|
__VirtualFunctionInfo(T *const addr_, void *const parent_):
|
||||||
|
addr(addr_),
|
||||||
|
parent(parent_) {}
|
||||||
|
bool can_overwrite() const {
|
||||||
|
return ((void *) *addr) != parent;
|
||||||
|
}
|
||||||
|
T *const addr;
|
||||||
|
void *const parent;
|
||||||
|
template <typename U>
|
||||||
|
friend class __Function;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Thunks
|
||||||
|
template <typename T>
|
||||||
|
struct __Thunk;
|
||||||
|
typedef void *(*thunk_enabler_t)(void *target, void *thunk);
|
||||||
|
extern thunk_enabler_t thunk_enabler;
|
||||||
|
|
||||||
|
// Function Information
|
||||||
|
template <typename Ret, typename... Args>
|
||||||
|
class __Function<Ret(Args...)> {
|
||||||
|
public:
|
||||||
|
// Types
|
||||||
|
using ptr_type = Ret (*)(Args...);
|
||||||
|
using type = std::function<Ret(Args...)>;
|
||||||
|
using overwrite_type = std::function<Ret(type, Args...)>;
|
||||||
|
|
||||||
|
// Normal Function
|
||||||
|
__Function(const char *const name_, const ptr_type func_, const ptr_type thunk_):
|
||||||
|
enabled(true),
|
||||||
|
name(name_),
|
||||||
|
is_virtual(false),
|
||||||
|
func(func_),
|
||||||
|
backup(func_),
|
||||||
|
thunk(thunk_) {}
|
||||||
|
// Virtual Function
|
||||||
|
__Function(const char *const name_, const __VirtualFunctionInfo<ptr_type> virtual_info_, const ptr_type thunk_):
|
||||||
|
enabled(virtual_info_.can_overwrite()),
|
||||||
|
name(name_),
|
||||||
|
is_virtual(true),
|
||||||
|
func(virtual_info_),
|
||||||
|
backup(*get_vtable_addr()),
|
||||||
|
thunk(thunk_) {}
|
||||||
|
__Function(const char *const name_, ptr_type *const func_, void *const parent, const ptr_type thunk_):
|
||||||
|
__Function(name_, __VirtualFunctionInfo(func_, parent), thunk_) {}
|
||||||
|
|
||||||
|
// Overwrite Function
|
||||||
|
bool overwrite(overwrite_type target) {
|
||||||
|
// Check If Enabled
|
||||||
|
if (!enabled) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Enable Thunk
|
||||||
|
enable_thunk();
|
||||||
|
// Overwrite
|
||||||
|
type original = get_thunk_target();
|
||||||
|
thunk_target = [original, target](Args... args) {
|
||||||
|
return target(original, args...);
|
||||||
|
};
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
ptr_type get_backup() const {
|
||||||
|
return backup;
|
||||||
|
}
|
||||||
|
ptr_type get() {
|
||||||
|
if (!enabled) {
|
||||||
|
return nullptr;
|
||||||
|
} else {
|
||||||
|
enable_thunk();
|
||||||
|
if (is_virtual) {
|
||||||
|
return *get_vtable_addr();
|
||||||
|
} else {
|
||||||
|
return func.normal_addr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ptr_type *get_vtable_addr() const {
|
||||||
|
if (is_virtual) {
|
||||||
|
return func.virtual_info.addr;
|
||||||
|
} else {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const char *get_name() const {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
// State
|
||||||
|
const bool enabled;
|
||||||
|
const char *const name;
|
||||||
|
|
||||||
|
// Current Function
|
||||||
|
const bool is_virtual;
|
||||||
|
union __FunctionInfo {
|
||||||
|
explicit __FunctionInfo(const ptr_type normal_addr_): normal_addr(normal_addr_) {}
|
||||||
|
explicit __FunctionInfo(const __VirtualFunctionInfo<ptr_type> virtual_info_): virtual_info(virtual_info_) {}
|
||||||
|
ptr_type normal_addr;
|
||||||
|
const __VirtualFunctionInfo<ptr_type> virtual_info;
|
||||||
|
} func;
|
||||||
|
|
||||||
|
// Backup Of Original Function Pointer
|
||||||
|
const ptr_type backup;
|
||||||
|
|
||||||
|
// Thunk
|
||||||
|
const ptr_type thunk;
|
||||||
|
bool thunk_enabled = false;
|
||||||
|
type thunk_target;
|
||||||
|
void enable_thunk() {
|
||||||
|
if (!thunk_enabled) {
|
||||||
|
ptr_type real_thunk = (ptr_type) thunk_enabler((void *) backup, (void *) thunk);
|
||||||
|
if (!is_virtual) {
|
||||||
|
func.normal_addr = real_thunk;
|
||||||
|
}
|
||||||
|
thunk_enabled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
type get_thunk_target() const {
|
||||||
|
if (thunk_target) {
|
||||||
|
return thunk_target;
|
||||||
|
} else {
|
||||||
|
return backup;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
friend struct __Thunk<Ret(Args...)>;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Shortcuts
|
||||||
|
typedef unsigned char uchar;
|
||||||
|
typedef unsigned short ushort;
|
||||||
|
typedef unsigned int uint;
|
||||||
|
|
||||||
|
// Duplicate VTable
|
||||||
|
#define RTTI_SIZE 4
|
||||||
|
template <typename T>
|
||||||
|
T *dup_vtable(T *vtable) {
|
||||||
|
// Check
|
||||||
|
static_assert(std::is_constructible<T>::value, "Unable To Construct VTable");
|
||||||
|
// Get Size
|
||||||
|
uchar *real_vtable = (uchar *) vtable;
|
||||||
|
real_vtable -= RTTI_SIZE;
|
||||||
|
size_t real_vtable_size = sizeof(T) + RTTI_SIZE;
|
||||||
|
// Allocate
|
||||||
|
uchar *new_vtable = (uchar *) ::operator new(real_vtable_size);
|
||||||
|
// Copy
|
||||||
|
memcpy((void *) new_vtable, (void *) real_vtable, real_vtable_size);
|
||||||
|
// Return
|
||||||
|
new_vtable += RTTI_SIZE;
|
||||||
|
return (T *) new_vtable;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Forward Declarations
|
||||||
|
{{ forwardDeclarations }}
|
||||||
|
|
||||||
|
// Extra Headers
|
||||||
|
{{ extraHeaders }}
|
||||||
|
|
||||||
|
// Warnings
|
||||||
|
#pragma GCC diagnostic push
|
||||||
|
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
|
||||||
|
#pragma GCC diagnostic ignored "-Wshadow"
|
||||||
|
|
||||||
|
{{ main }}
|
||||||
|
|
||||||
|
// Cleanup Warnings
|
||||||
|
#pragma GCC diagnostic pop
|
@ -1,4 +1,5 @@
|
|||||||
import * as fs from 'node:fs';
|
import * as fs from 'node:fs';
|
||||||
|
import * as path from 'node:path';
|
||||||
|
|
||||||
export const INDENT = ' ';
|
export const INDENT = ' ';
|
||||||
export const POINTER_SIZE = 4;
|
export const POINTER_SIZE = 4;
|
||||||
@ -56,7 +57,7 @@ export function toUpperSnakeCase(str: string) {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
export function formatType(type: string) {
|
export function formatType(type: string) {
|
||||||
if (!type.endsWith('*')) {
|
if (!type.endsWith('*') && !type.endsWith('&')) {
|
||||||
type += ' ';
|
type += ' ';
|
||||||
}
|
}
|
||||||
return type;
|
return type;
|
||||||
@ -78,42 +79,25 @@ export function safeParseInt(str: string) {
|
|||||||
export function getSelfArg(type: string) {
|
export function getSelfArg(type: string) {
|
||||||
return `${type} *self`;
|
return `${type} *self`;
|
||||||
}
|
}
|
||||||
export function getArgNames(args: string) {
|
|
||||||
// Remove Parentheses
|
|
||||||
args = args.substring(1, args.length - 1);
|
|
||||||
if (args.length === 0) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
// Split
|
|
||||||
const argsList = args.split(',');
|
|
||||||
// Parse
|
|
||||||
const out = [];
|
|
||||||
for (let arg of argsList) {
|
|
||||||
arg = arg.trim();
|
|
||||||
// Remove Type
|
|
||||||
const nameStart = Math.max(arg.lastIndexOf(' '), arg.lastIndexOf('*')) + 1;
|
|
||||||
arg = arg.substring(nameStart);
|
|
||||||
// Collect
|
|
||||||
out.push(arg);
|
|
||||||
}
|
|
||||||
// Return
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
export function prependArg(args: string, arg: string) {
|
export function prependArg(args: string, arg: string) {
|
||||||
if (args !== '()') {
|
if (args !== '()') {
|
||||||
arg += ', ';
|
arg += ', ';
|
||||||
}
|
}
|
||||||
return '(' + arg + args.substring(1);
|
return '(' + arg + args.substring(1);
|
||||||
}
|
}
|
||||||
export function removeFirstArg(args: string) {
|
export function formatFile(file: string, options: {[key: string]: string}) {
|
||||||
let index = args.indexOf(',');
|
file = path.join(__dirname, '..', 'data', file);
|
||||||
if (index === -1) {
|
let data = fs.readFileSync(file, {encoding: 'utf8'});
|
||||||
index = args.indexOf(')');
|
for (const key in options) {
|
||||||
} else {
|
data = data.replace(`{{ ${key} }}`, options[key]!);
|
||||||
index++;
|
|
||||||
}
|
}
|
||||||
return '(' + args.substring(index).trim();
|
return data.trim() + '\n';
|
||||||
}
|
}
|
||||||
export const ORIGINAL_SUFFIX = '_backup_do_not_use';
|
export const INTERNAL = '__';
|
||||||
export const VTABLE_ADDR_SUFFIX = `_vtable_addr`;
|
export function preventConstruction(self: string) {
|
||||||
export const OVERWRITE_TEST_SUFFIX = '__is_overwritable_';
|
let out = '';
|
||||||
|
out += `${INDENT}${self}() = delete;\n`;
|
||||||
|
out += `${INDENT}${self}(const ${self} &) = delete;\n`;
|
||||||
|
out += `${INDENT}${self} &operator=(const ${self} &) = delete;\n`;
|
||||||
|
return out;
|
||||||
|
}
|
69
src/index.ts
69
src/index.ts
@ -1,5 +1,5 @@
|
|||||||
import * as fs from 'node:fs';
|
import * as fs from 'node:fs';
|
||||||
import { STRUCTURE_FILES, EXTENSION } from './common';
|
import { STRUCTURE_FILES, EXTENSION, formatFile } from './common';
|
||||||
import { getStructure } from './map';
|
import { getStructure } from './map';
|
||||||
import { Struct } from './struct';
|
import { Struct } from './struct';
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ const headerOutput = process.argv.shift()!;
|
|||||||
if (!headerOutput.endsWith('.h')) {
|
if (!headerOutput.endsWith('.h')) {
|
||||||
invalidFileType(headerOutput);
|
invalidFileType(headerOutput);
|
||||||
}
|
}
|
||||||
const extraHeaders: string[] = [];
|
const extraHeaderFiles: string[] = [];
|
||||||
while (process.argv.length > 0) {
|
while (process.argv.length > 0) {
|
||||||
const file = process.argv.shift()!;
|
const file = process.argv.shift()!;
|
||||||
if (file.endsWith(EXTENSION)) {
|
if (file.endsWith(EXTENSION)) {
|
||||||
@ -36,7 +36,7 @@ while (process.argv.length > 0) {
|
|||||||
STRUCTURE_FILES[name] = file;
|
STRUCTURE_FILES[name] = file;
|
||||||
} else if (file.endsWith('.h')) {
|
} else if (file.endsWith('.h')) {
|
||||||
// Extra Headers
|
// Extra Headers
|
||||||
extraHeaders.push(file);
|
extraHeaderFiles.push(file);
|
||||||
} else {
|
} else {
|
||||||
// Invalid File Type
|
// Invalid File Type
|
||||||
invalidFileType(file);
|
invalidFileType(file);
|
||||||
@ -112,14 +112,8 @@ function makeHeaderPart() {
|
|||||||
|
|
||||||
// Generate Code
|
// Generate Code
|
||||||
let structures = '';
|
let structures = '';
|
||||||
let isFirst = true;
|
|
||||||
for (const structure of structureObjects) {
|
for (const structure of structureObjects) {
|
||||||
const name = structure.getName();
|
const name = structure.getName();
|
||||||
if (isFirst) {
|
|
||||||
isFirst = false;
|
|
||||||
} else {
|
|
||||||
structures += '\n';
|
|
||||||
}
|
|
||||||
structures += `// ${name}\n`;
|
structures += `// ${name}\n`;
|
||||||
try {
|
try {
|
||||||
structures += structure.generate();
|
structures += structure.generate();
|
||||||
@ -127,6 +121,7 @@ function makeHeaderPart() {
|
|||||||
console.log(`Error Generating Header: ${name}: ${e instanceof Error ? e.stack : e}`);
|
console.log(`Error Generating Header: ${name}: ${e instanceof Error ? e.stack : e}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
structures += '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return
|
// Return
|
||||||
@ -135,38 +130,22 @@ function makeHeaderPart() {
|
|||||||
|
|
||||||
// Create Main Header
|
// Create Main Header
|
||||||
function makeMainHeader(output: string) {
|
function makeMainHeader(output: string) {
|
||||||
let result = '';
|
// Forward Declarations
|
||||||
result += '#pragma once\n';
|
let forwardDeclarations = '';
|
||||||
result += '\n// Check Architecture\n';
|
|
||||||
result += '#ifndef __arm__\n';
|
|
||||||
result += '#error "Symbols Are ARM-Only"\n';
|
|
||||||
result += '#endif\n';
|
|
||||||
result += '\n// Forward Declarations\n';
|
|
||||||
for (const name in STRUCTURE_FILES) {
|
for (const name in STRUCTURE_FILES) {
|
||||||
result += `typedef struct ${name} ${name};\n`;
|
forwardDeclarations += `typedef struct ${name} ${name};\n`;
|
||||||
}
|
}
|
||||||
result += '\n// Extra Headers\n';
|
forwardDeclarations = forwardDeclarations.trim();
|
||||||
for (const file of extraHeaders) {
|
// Extra Headers
|
||||||
result += fs.readFileSync(file, {encoding: 'utf8'});
|
let extraHeaders = '';
|
||||||
|
for (const file of extraHeaderFiles) {
|
||||||
|
extraHeaders += fs.readFileSync(file, {encoding: 'utf8'});
|
||||||
}
|
}
|
||||||
result += '\n// Headers\n';
|
extraHeaders = extraHeaders.trim();
|
||||||
result += '#include <cstddef>\n';
|
// Main
|
||||||
result += '#include <string>\n';
|
const main = makeHeaderPart().trim();
|
||||||
result += '#include <vector>\n';
|
// Write
|
||||||
result += '#include <map>\n';
|
const result = formatFile('out.h', {forwardDeclarations, extraHeaders, main});
|
||||||
result += `#include <type_traits>\n`;
|
|
||||||
result += `#include <functional>\n`;
|
|
||||||
result += '\n// Shortcuts\n';
|
|
||||||
result += 'typedef unsigned char uchar;\n';
|
|
||||||
result += 'typedef unsigned short ushort;\n';
|
|
||||||
result += 'typedef unsigned int uint;\n';
|
|
||||||
result += '\n// Warnings\n';
|
|
||||||
result += '#pragma GCC diagnostic push\n';
|
|
||||||
result += '#pragma GCC diagnostic ignored "-Winvalid-offsetof"\n';
|
|
||||||
result += '#pragma GCC diagnostic ignored "-Wshadow"\n\n';
|
|
||||||
result += makeHeaderPart();
|
|
||||||
result += '\n// Cleanup Warnings\n';
|
|
||||||
result += '#pragma GCC diagnostic pop\n';
|
|
||||||
fs.writeFileSync(output, result);
|
fs.writeFileSync(output, result);
|
||||||
}
|
}
|
||||||
makeMainHeader(headerOutput);
|
makeMainHeader(headerOutput);
|
||||||
@ -178,14 +157,8 @@ function makeCompiledCode(output: string) {
|
|||||||
|
|
||||||
// Generate
|
// Generate
|
||||||
let declarations = '';
|
let declarations = '';
|
||||||
let isFirst = true;
|
|
||||||
for (const structure of structureObjects) {
|
for (const structure of structureObjects) {
|
||||||
const name = structure.getName();
|
const name = structure.getName();
|
||||||
if (isFirst) {
|
|
||||||
isFirst = false;
|
|
||||||
} else {
|
|
||||||
declarations += '\n';
|
|
||||||
}
|
|
||||||
declarations += `// ${name}\n`;
|
declarations += `// ${name}\n`;
|
||||||
try {
|
try {
|
||||||
const code = structure.generateCode();
|
const code = structure.generateCode();
|
||||||
@ -194,13 +167,13 @@ function makeCompiledCode(output: string) {
|
|||||||
console.log(`Error Generating Code: ${name}: ${e instanceof Error ? e.stack : e}`);
|
console.log(`Error Generating Code: ${name}: ${e instanceof Error ? e.stack : e}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
declarations += '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write
|
// Write
|
||||||
let result = '';
|
const headerPath = fs.realpathSync(headerOutput);
|
||||||
result += `#include "${fs.realpathSync(headerOutput)}"\n`;
|
const main = declarations.trim();
|
||||||
result += '\n#include <cstring>\n\n';
|
const result = formatFile('out.cpp', {headerPath, main});
|
||||||
result += declarations;
|
|
||||||
fs.writeFileSync(output, result);
|
fs.writeFileSync(output, result);
|
||||||
}
|
}
|
||||||
makeCompiledCode(sourceOutput);
|
makeCompiledCode(sourceOutput);
|
||||||
|
114
src/method.ts
114
src/method.ts
@ -1,4 +1,4 @@
|
|||||||
import { INDENT, OVERWRITE_TEST_SUFFIX, VTABLE_ADDR_SUFFIX, formatType, getArgNames, prependArg } from './common';
|
import { INDENT, INTERNAL, toHex } from './common';
|
||||||
|
|
||||||
export class Method {
|
export class Method {
|
||||||
readonly self: string;
|
readonly self: string;
|
||||||
@ -7,7 +7,6 @@ export class Method {
|
|||||||
readonly args: string;
|
readonly args: string;
|
||||||
readonly address: number;
|
readonly address: number;
|
||||||
readonly isInherited: boolean;
|
readonly isInherited: boolean;
|
||||||
readonly hasVarargs: boolean;
|
|
||||||
readonly isStatic: boolean;
|
readonly isStatic: boolean;
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
@ -18,102 +17,49 @@ export class Method {
|
|||||||
this.args = args;
|
this.args = args;
|
||||||
this.address = address;
|
this.address = address;
|
||||||
this.isInherited = isInherited;
|
this.isInherited = isInherited;
|
||||||
this.hasVarargs = this.args.includes('...');
|
|
||||||
this.isStatic = isStatic;
|
this.isStatic = isStatic;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get Type
|
// Getters
|
||||||
getNameWithCustomSelf(self: string) {
|
getName(separator: string = '_') {
|
||||||
return `${self}_${this.shortName}`;
|
return this.self + separator + this.shortName;
|
||||||
}
|
|
||||||
getName() {
|
|
||||||
return this.getNameWithCustomSelf(this.self);
|
|
||||||
}
|
|
||||||
#getBaseType() {
|
|
||||||
return `${this.getName()}_t`;
|
|
||||||
}
|
}
|
||||||
getType() {
|
getType() {
|
||||||
return `__raw_${this.#getBaseType()}`;
|
return this.getName() + '_t';
|
||||||
}
|
}
|
||||||
|
getProperty() {
|
||||||
// Generate Type Definition
|
return `${INDENT}${this.getWrapperType()}::ptr_type ${this.shortName};\n`;
|
||||||
generateTypedef() {
|
}
|
||||||
let out = '';
|
getWrapperType() {
|
||||||
|
return `decltype(${this.getName()})`;
|
||||||
// Normal Definition
|
|
||||||
out += `typedef ${formatType(this.returnType)}(*${this.getType()})${this.args};\n`;
|
|
||||||
|
|
||||||
// Return
|
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overwrite Helper
|
// Overwrite Helper
|
||||||
#hasOverwriteHelper() {
|
#getVirtualCall(self: string = this.self) {
|
||||||
// Fancy Overwrite Does Not Support Varargs
|
return `${self}_vtable_base->${this.shortName}`;
|
||||||
return !this.hasVarargs;
|
|
||||||
}
|
}
|
||||||
generateOverwriteHelper(code: boolean, isVirtual: boolean) {
|
generate(code: boolean, isVirtual: boolean, parentSelf?: string) {
|
||||||
let out = '';
|
let out = '';
|
||||||
if (this.#hasOverwriteHelper()) {
|
if (!code) {
|
||||||
// Add Additional Typedefs
|
out += 'extern ';
|
||||||
const baseType = this.#getBaseType();
|
}
|
||||||
const overwriteType = `__overwrite_${baseType}`;
|
const signature = this.returnType.trim() + this.args.trim();
|
||||||
const functionType = baseType;
|
const type = `${INTERNAL}Function<${signature}>`;
|
||||||
const rawType = this.getType();
|
out += `${type} ${this.getName()}`;
|
||||||
if (!code) {
|
if (code) {
|
||||||
out += `typedef std::function<std::remove_pointer<${rawType}>::type> ${functionType};\n`;
|
out += `(${JSON.stringify(this.getName('::'))}, `;
|
||||||
out += `typedef std::function<${this.returnType}${prependArg(this.args, functionType + ' original')}> ${overwriteType};\n`;
|
if (isVirtual) {
|
||||||
}
|
const parentMethod = parentSelf ? `(void *) ${this.#getVirtualCall(parentSelf)}` : 'nullptr';
|
||||||
// Thunk
|
out += `&${this.#getVirtualCall()}, ${parentMethod}`;
|
||||||
const name = this.getName();
|
|
||||||
const currentOverwriteName = '__current_overwrite_for_' + name;
|
|
||||||
const thunkName = '__overwrite_thunk_for_' + name;
|
|
||||||
if (code) {
|
|
||||||
out += `static ${functionType} &${currentOverwriteName} = *new ${functionType};\n`;
|
|
||||||
out += `static ${formatType(this.returnType)}${thunkName}${this.args} {\n`;
|
|
||||||
out += `${INDENT}${this.returnType.trim() === 'void' ? '' : 'return '}${currentOverwriteName}(${getArgNames(this.args).join(', ')});\n`;
|
|
||||||
out += `}\n`;
|
|
||||||
}
|
|
||||||
// Overwrite Helper
|
|
||||||
out += `std::string __set_overwrite_for_${name}(const ${overwriteType} &method, const std::function<void *(void *, void *)> &overwriter)`;
|
|
||||||
if (code) {
|
|
||||||
out += ' {\n';
|
|
||||||
if (isVirtual) {
|
|
||||||
// Check If Method Can Be Overwritten
|
|
||||||
out += `${INDENT}if (!${OVERWRITE_TEST_SUFFIX}${name}()) {\n`;
|
|
||||||
out += `${INDENT}${INDENT}return "Unable To Overwrite Virtual Method";\n`;
|
|
||||||
out += `${INDENT}}\n`;
|
|
||||||
}
|
|
||||||
// Redirect Method Calls To Thunk
|
|
||||||
out += `${INDENT}static bool enabled = false;\n`;
|
|
||||||
out += `${INDENT}if (!enabled) {\n`;
|
|
||||||
if (isVirtual) {
|
|
||||||
// Get Address Of Virtual Function
|
|
||||||
out += `${INDENT}${INDENT}${rawType} ${name} = *${name}${VTABLE_ADDR_SUFFIX};\n`;
|
|
||||||
}
|
|
||||||
out += `${INDENT}${INDENT}${rawType} &original = ${name};\n`;
|
|
||||||
out += `${INDENT}${INDENT}${currentOverwriteName} = original;\n`;
|
|
||||||
out += `${INDENT}${INDENT}original = (${rawType}) overwriter((void *) original, (void *) ${thunkName});\n`;
|
|
||||||
out += `${INDENT}${INDENT}enabled = true;\n`;
|
|
||||||
out += `${INDENT}}\n`;
|
|
||||||
// Bind Original Parameter
|
|
||||||
out += `${INDENT}${functionType} original = ${currentOverwriteName};\n`;
|
|
||||||
out += `${INDENT}${currentOverwriteName} = [method, original]${this.args} {\n`;
|
|
||||||
out += `${INDENT}${INDENT}${this.returnType.trim() === 'void' ? '' : 'return '}method(${['original'].concat(getArgNames(this.args)).join(', ')});\n`;
|
|
||||||
out += `${INDENT}};\n`;
|
|
||||||
// Return Success
|
|
||||||
out += `${INDENT}return "";\n`;
|
|
||||||
out += `}`;
|
|
||||||
} else {
|
} else {
|
||||||
out += ';';
|
out += `${this.getWrapperType()}::ptr_type(${toHex(this.address)})`;
|
||||||
}
|
}
|
||||||
out += '\n';
|
out += `, ${INTERNAL}Thunk<${signature}>::call<&${this.getName()}>)`;
|
||||||
|
}
|
||||||
|
out += ';\n';
|
||||||
|
if (!code) {
|
||||||
|
out += `typedef ${this.getWrapperType()}::type ${this.getType()};\n`;
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate Variable Definition
|
|
||||||
generateDefinition(nameSuffix?: string) {
|
|
||||||
return `${this.getType()} ${this.getName()}${nameSuffix !== undefined ? nameSuffix : ''}`;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
import { INDENT, STRUCTURE_FILES, toHex, assertSize, formatType, getArgNames, removeFirstArg, ORIGINAL_SUFFIX } from './common';
|
import { INDENT, STRUCTURE_FILES, toHex, assertSize, INTERNAL, preventConstruction } from './common';
|
||||||
import { Method } from './method';
|
import { Method } from './method';
|
||||||
import { Property, StaticProperty } from './property';
|
import { Property, StaticProperty } from './property';
|
||||||
import { VTable } from './vtable';
|
import { VTable } from './vtable';
|
||||||
@ -127,7 +127,7 @@ export class Struct {
|
|||||||
if (lastProperty) {
|
if (lastProperty) {
|
||||||
paddingSize += ` - sizeof(${lastProperty.type()})`;
|
paddingSize += ` - sizeof(${lastProperty.type()})`;
|
||||||
}
|
}
|
||||||
out += `${INDENT}uchar __padding${i}[${paddingSize}];\n`;
|
out += `${INDENT}uchar ${INTERNAL}padding${i}[${paddingSize}];\n`;
|
||||||
|
|
||||||
// The Actual Property
|
// The Actual Property
|
||||||
if (property !== sizeProperty) {
|
if (property !== sizeProperty) {
|
||||||
@ -138,47 +138,42 @@ export class Struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate C++ Method Shortcuts
|
// Generate C++ Method Shortcuts
|
||||||
|
#generateMethod(method: Method, isVirtual: boolean) {
|
||||||
|
// Generate Method Call
|
||||||
|
let call = '';
|
||||||
|
if (isVirtual) {
|
||||||
|
call += `this->vtable->${method.shortName}`;
|
||||||
|
} else {
|
||||||
|
call += `${method.getName()}.get()`;
|
||||||
|
}
|
||||||
|
call += '(';
|
||||||
|
if (!method.isStatic) {
|
||||||
|
call += `(${method.self} *) this, `;
|
||||||
|
}
|
||||||
|
call += 'std::forward<Args>(args)...)';
|
||||||
|
// Generate Shortcut
|
||||||
|
let out = '';
|
||||||
|
out += `${INDENT}template <typename... Args>\n`;
|
||||||
|
if (method.isStatic) {
|
||||||
|
out += 'static ';
|
||||||
|
}
|
||||||
|
out += `${INDENT}auto ${method.shortName}(Args&&... args) -> decltype(${call}) {\n`;
|
||||||
|
out += `${INDENT}${INDENT}return ${call};\n`;
|
||||||
|
out += `${INDENT}}\n`;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
#generateMethods() {
|
#generateMethods() {
|
||||||
let out = '';
|
let out = '';
|
||||||
// Normal Methods
|
// Normal Methods
|
||||||
const getArgsOuter = (method: Method) => {
|
|
||||||
let out = method.args;
|
|
||||||
if (!method.isStatic) {
|
|
||||||
// Remove "self"
|
|
||||||
out = removeFirstArg(out);
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
};
|
|
||||||
const getArgsInner = (method: Method) => {
|
|
||||||
const list = getArgNames(method.args);
|
|
||||||
if (!method.isStatic) {
|
|
||||||
// Replace "self" With "this"
|
|
||||||
list[0] = `(${method.self} *) this`;
|
|
||||||
}
|
|
||||||
return list.join(', ');
|
|
||||||
};
|
|
||||||
for (const method of this.#methods) {
|
for (const method of this.#methods) {
|
||||||
if (!method.hasVarargs) {
|
out += this.#generateMethod(method, false);
|
||||||
const returnType = method.returnType;
|
|
||||||
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}${INDENT}${returnType.trim() === 'void' ? '' : 'return '}${fullName}(${getArgsInner(method)});\n`;
|
|
||||||
out += `${INDENT}}\n`;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Virtual Methods
|
// Virtual Methods
|
||||||
if (this.#vtable !== null) {
|
if (this.#vtable !== null) {
|
||||||
const virtualMethods = this.#vtable.getMethods();
|
const virtualMethods = this.#vtable.getMethods();
|
||||||
for (const method of virtualMethods) {
|
for (const method of virtualMethods) {
|
||||||
if (method && !method.hasVarargs) {
|
if (method) {
|
||||||
const returnType = method.returnType;
|
out += this.#generateMethod(method, true);
|
||||||
const shortName = method.shortName;
|
|
||||||
const args = getArgsOuter(method);
|
|
||||||
out += `${INDENT}inline ${formatType(returnType)}${shortName}${args} { \\\n`;
|
|
||||||
out += `${INDENT}${INDENT}${returnType.trim() === 'void' ? '' : 'return '}this->vtable->${shortName}(${getArgsInner(method)});\n`;
|
|
||||||
out += `${INDENT}}\n`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -190,31 +185,28 @@ export class Struct {
|
|||||||
generate() {
|
generate() {
|
||||||
let out = '';
|
let out = '';
|
||||||
|
|
||||||
// Static Properties
|
|
||||||
for (const property of this.#staticProperties) {
|
|
||||||
out += property.typedef();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 += method.generateOverwriteHelper(false, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// VTable
|
// VTable
|
||||||
if (this.#vtable !== null) {
|
if (this.#vtable !== null) {
|
||||||
out += this.#vtable.generate();
|
out += this.#vtable.generate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Static Properties
|
||||||
|
for (const property of this.#staticProperties) {
|
||||||
|
out += property.typedef();
|
||||||
|
}
|
||||||
|
|
||||||
// Property Typedefs
|
// Property Typedefs
|
||||||
for (const property of this.#properties) {
|
for (const property of this.#properties) {
|
||||||
out += property.typedef();
|
out += property.typedef();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Method Wrappers
|
||||||
|
for (const method of this.#methods) {
|
||||||
|
if (!method.isInherited) {
|
||||||
|
out += method.generate(false, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Structure
|
// Structure
|
||||||
out += `struct ${this.#name} {\n`;
|
out += `struct ${this.#name} {\n`;
|
||||||
out += this.#generateProperties();
|
out += this.#generateProperties();
|
||||||
@ -225,9 +217,7 @@ export class Struct {
|
|||||||
}
|
}
|
||||||
if (this.#size === null) {
|
if (this.#size === null) {
|
||||||
// Prevent Manually Copying/Allocating Structure With Undefined
|
// Prevent Manually Copying/Allocating Structure With Undefined
|
||||||
out += `${INDENT}${this.#name}() = delete;\n`;
|
out += preventConstruction(this.#name);
|
||||||
out += `${INDENT}${this.#name}(const ${this.#name} &) = delete;\n`;
|
|
||||||
out += `${INDENT}${this.#name} &operator=(const ${this.#name} &) = delete;\n`;
|
|
||||||
}
|
}
|
||||||
out += `};\n`;
|
out += `};\n`;
|
||||||
|
|
||||||
@ -260,9 +250,7 @@ export class Struct {
|
|||||||
// Methods
|
// Methods
|
||||||
for (const method of this.#methods) {
|
for (const method of this.#methods) {
|
||||||
if (!method.isInherited) {
|
if (!method.isInherited) {
|
||||||
out += `${method.generateDefinition()} = (${method.getType()}) ${toHex(method.address)};\n`;
|
out += method.generate(true, false);
|
||||||
out += `${method.generateDefinition(ORIGINAL_SUFFIX)} = ${method.getName()};\n`;
|
|
||||||
out += method.generateOverwriteHelper(true, false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { INDENT, ORIGINAL_SUFFIX, OVERWRITE_TEST_SUFFIX, POINTER_SIZE, RTTI_SIZE, VTABLE_ADDR_SUFFIX, assertSize, getSelfArg, toHex } from './common';
|
import { INDENT, POINTER_SIZE, assertSize, getSelfArg, preventConstruction, toHex } from './common';
|
||||||
import { Method } from './method';
|
import { Method } from './method';
|
||||||
import { Property } from './property';
|
import { Property } from './property';
|
||||||
|
|
||||||
@ -86,29 +86,22 @@ export class VTable {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overwritable Test (Only For Virtual Methods)
|
// Get Parent Method
|
||||||
generateOverwritableTest(method: Method, parent: string | null) {
|
#getParentSelf(method: Method, parent: string | null) {
|
||||||
// Test If Overwriting Method Would Also Modify Parent
|
|
||||||
let out = '';
|
|
||||||
out += `static inline bool ${OVERWRITE_TEST_SUFFIX}${method.getName()}() {\n`;
|
|
||||||
let ret: string;
|
|
||||||
if (method.isInherited) {
|
if (method.isInherited) {
|
||||||
|
// Parent Exists
|
||||||
|
let out: string;
|
||||||
if (structuresWithVTableAddress.includes(parent!)) {
|
if (structuresWithVTableAddress.includes(parent!)) {
|
||||||
// Check If Method Differs From Parent
|
out = 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 {
|
} else {
|
||||||
// Unable To Determine
|
// Unable To Determine
|
||||||
ret = 'false';
|
out = this.#self;
|
||||||
}
|
}
|
||||||
|
return out;
|
||||||
} else {
|
} else {
|
||||||
// No Parent
|
// Method Has No Parent
|
||||||
ret = 'true';
|
return undefined;
|
||||||
}
|
}
|
||||||
out += `${INDENT}return ${ret};\n`;
|
|
||||||
out += '}\n';
|
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate Header Code
|
// Generate Header Code
|
||||||
@ -118,12 +111,11 @@ export class VTable {
|
|||||||
// Check
|
// Check
|
||||||
this.#check();
|
this.#check();
|
||||||
|
|
||||||
// Method Prototypes
|
// Wrappers
|
||||||
const methods = this.getMethods();
|
const methods = this.getMethods();
|
||||||
for (let i = 0; i < methods.length; i++) {
|
for (const info of methods) {
|
||||||
const info = methods[i];
|
|
||||||
if (info) {
|
if (info) {
|
||||||
out += info.generateTypedef();
|
out += info.generate(false, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,14 +123,16 @@ export class VTable {
|
|||||||
out += `typedef struct ${this.#getName()} ${this.#getName()};\n`;
|
out += `typedef struct ${this.#getName()} ${this.#getName()};\n`;
|
||||||
out += `struct ${this.#getName()} {\n`;
|
out += `struct ${this.#getName()} {\n`;
|
||||||
for (let i = 0; i < methods.length; i++) {
|
for (let i = 0; i < methods.length; i++) {
|
||||||
let name = `unknown${i}`;
|
|
||||||
let type = 'void *';
|
|
||||||
const info = methods[i];
|
const info = methods[i];
|
||||||
if (info) {
|
if (info) {
|
||||||
name = info.shortName;
|
out += info.getProperty();
|
||||||
type = info.getType() + ' ';
|
} else {
|
||||||
|
out += `${INDENT}void *unknown${i};\n`;
|
||||||
}
|
}
|
||||||
out += `${INDENT}${type}${name};\n`;
|
}
|
||||||
|
if (this.#size === null) {
|
||||||
|
// Prevent Construction
|
||||||
|
out += preventConstruction(this.#getName());
|
||||||
}
|
}
|
||||||
out += `};\n`;
|
out += `};\n`;
|
||||||
|
|
||||||
@ -151,23 +145,6 @@ export class VTable {
|
|||||||
if (this.#address !== null) {
|
if (this.#address !== null) {
|
||||||
// Base
|
// Base
|
||||||
out += `extern ${this.#getName()} *${this.#getName()}_base;\n`;
|
out += `extern ${this.#getName()} *${this.#getName()}_base;\n`;
|
||||||
// Methods
|
|
||||||
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 += `extern ${info.generateDefinition(ORIGINAL_SUFFIX)};\n`;
|
|
||||||
out += info.generateOverwriteHelper(false, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Duplication Method
|
|
||||||
if (this.#size !== null) {
|
|
||||||
out += `${this.#getName()} *dup_${this.#getName()}(${this.#getName()} *vtable);\n`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return
|
// Return
|
||||||
@ -187,37 +164,13 @@ export class VTable {
|
|||||||
out += `${this.#getName()} *${this.#getName()}_base = (${this.#getName()} *) ${toHex(this.#address)};\n`;
|
out += `${this.#getName()} *${this.#getName()}_base = (${this.#getName()} *) ${toHex(this.#address)};\n`;
|
||||||
// Methods
|
// Methods
|
||||||
const methods = this.getMethods();
|
const methods = this.getMethods();
|
||||||
for (let i = 0; i < methods.length; i++) {
|
for (const info of methods) {
|
||||||
const info = methods[i];
|
|
||||||
if (info) {
|
if (info) {
|
||||||
const vtableAddress = this.#address + (i * POINTER_SIZE);
|
out += info.generate(true, true, this.#getParentSelf(info, directParent));
|
||||||
const name = info.getName();
|
|
||||||
const type = info.getType();
|
|
||||||
const pointerType = `${type} *`;
|
|
||||||
out += `${pointerType}${name}${VTABLE_ADDR_SUFFIX} = (${pointerType}) ${toHex(vtableAddress)};\n`;
|
|
||||||
out += `${info.generateDefinition(ORIGINAL_SUFFIX)} = *${name}${VTABLE_ADDR_SUFFIX};\n`;
|
|
||||||
out += this.generateOverwritableTest(info, directParent);
|
|
||||||
out += info.generateOverwriteHelper(true, true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return
|
// Return
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
<item>vtable-destructor-offset</item>
|
<item>vtable-destructor-offset</item>
|
||||||
</list>
|
</list>
|
||||||
<list name="types">
|
<list name="types">
|
||||||
|
<item>const</item>
|
||||||
<item>char</item>
|
<item>char</item>
|
||||||
<item>uchar</item>
|
<item>uchar</item>
|
||||||
<item>short</item>
|
<item>short</item>
|
||||||
|
@ -5,10 +5,12 @@ comment "//"
|
|||||||
color magenta "\<(extends|size|vtable(-size|-destructor-offset)?|property|static-property|((static|virtual)-)?method|constructor)\>"
|
color magenta "\<(extends|size|vtable(-size|-destructor-offset)?|property|static-property|((static|virtual)-)?method|constructor)\>"
|
||||||
|
|
||||||
# Types
|
# Types
|
||||||
color green "\<(char|uchar|short|ushort|int|uint|float|bool|void|std::(string|vector|map))\>"
|
color green "\<((u?(char|short|int))|float|bool|void|std::(string|vector|map))\>"
|
||||||
|
|
||||||
# Numbers
|
# Numbers
|
||||||
color yellow "0x[a-f0-9]+"
|
color yellow "0x[a-f0-9]+"
|
||||||
|
# Non-hex numbers
|
||||||
|
color red " [0-9][a-f0-9]+;"
|
||||||
|
|
||||||
# Comments
|
# Comments
|
||||||
color brightblue "//.*"
|
color brightblue "//.*"
|
||||||
|
@ -40,7 +40,7 @@
|
|||||||
<key>name</key>
|
<key>name</key>
|
||||||
<string>storage.type.symbol-processor</string>
|
<string>storage.type.symbol-processor</string>
|
||||||
<key>match</key>
|
<key>match</key>
|
||||||
<string>\b(char|uchar|short|ushort|int|uint|float|bool|void|std::string|std::vector|std::map)\b</string>
|
<string>\b(const|char|uchar|short|ushort|int|uint|float|bool|void|std::string|std::vector|std::map)\b</string>
|
||||||
</dict>
|
</dict>
|
||||||
<dict>
|
<dict>
|
||||||
<key>name</key>
|
<key>name</key>
|
||||||
|
Loading…
Reference in New Issue
Block a user