Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
c803572e24 | |||
308a36b4ba | |||
f72c4f0567 | |||
5d2b146b08 | |||
2a63f1ff52 | |||
199eecda97 | |||
10026e9a04 | |||
b0814f257a | |||
6f792dfb16 | |||
46c486e56a | |||
346d4403df | |||
11342dbb78 | |||
be25749aa4 | |||
6098f57b03 | |||
8249a305df | |||
eb49f25fa4 | |||
fbb9b6d6da | |||
603010e3cc | |||
eca52455c3 | |||
9697b35de4 | |||
853481bd9a |
10
data/out.cpp
Normal file
10
data/out.cpp
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#define LEAN_SYMBOLS_HEADER
|
||||||
|
#include "{{ headerPath }}"
|
||||||
|
|
||||||
|
// Thunk Template
|
||||||
|
template <auto *const *func>
|
||||||
|
decltype(auto) __thunk(auto... args) {
|
||||||
|
return (*func)->get_thunk_target()(std::forward<decltype(args)>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
{{ main }}
|
174
data/out.h
Normal file
174
data/out.h
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// Check Architecture
|
||||||
|
#ifndef __arm__
|
||||||
|
#error "Symbols Are ARM-Only"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Headers
|
||||||
|
#include <variant>
|
||||||
|
#include <functional>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
// Internal Macros
|
||||||
|
#define __PREVENT_DESTRUCTION(self) \
|
||||||
|
~self() = delete
|
||||||
|
#define __PREVENT_CONSTRUCTION(self) \
|
||||||
|
self() = delete; \
|
||||||
|
__PREVENT_DESTRUCTION(self)
|
||||||
|
#define __PREVENT_COPY(self) \
|
||||||
|
self(const self &) = delete; \
|
||||||
|
self &operator=(const self &) = delete
|
||||||
|
|
||||||
|
// Virtual Function Information
|
||||||
|
struct __VirtualFunctionInfo {
|
||||||
|
// Constructors
|
||||||
|
template <typename Ret, typename Self, typename Super, typename... Args>
|
||||||
|
__VirtualFunctionInfo(Ret (**const addr_)(Self, Args...), Ret (*const parent_)(Super, Args...)):
|
||||||
|
addr((void **) addr_),
|
||||||
|
parent((void *) parent_) {}
|
||||||
|
template <typename T>
|
||||||
|
__VirtualFunctionInfo(T **const addr_, const std::nullptr_t parent_):
|
||||||
|
__VirtualFunctionInfo(addr_, (T *) parent_) {}
|
||||||
|
// Method
|
||||||
|
[[nodiscard]] bool can_overwrite() const {
|
||||||
|
return *addr != parent;
|
||||||
|
}
|
||||||
|
// Properties
|
||||||
|
void **const addr;
|
||||||
|
void *const parent;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Thunks
|
||||||
|
typedef void *(*thunk_enabler_t)(void *target, void *thunk);
|
||||||
|
extern thunk_enabler_t thunk_enabler;
|
||||||
|
|
||||||
|
// Function Information
|
||||||
|
template <typename T>
|
||||||
|
class __Function;
|
||||||
|
template <typename Ret, typename... Args>
|
||||||
|
class __Function<Ret(Args...)> {
|
||||||
|
// Prevent Copying
|
||||||
|
__PREVENT_COPY(__Function);
|
||||||
|
__PREVENT_DESTRUCTION(__Function);
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Types
|
||||||
|
typedef Ret (*ptr_type)(Args...);
|
||||||
|
typedef std::function<Ret(Args...)> type;
|
||||||
|
typedef std::function<Ret(const type &, Args...)> overwrite_type;
|
||||||
|
|
||||||
|
// Normal Function
|
||||||
|
__Function(const std::string name_, const ptr_type thunk_, const ptr_type func_):
|
||||||
|
func(func_),
|
||||||
|
enabled(true),
|
||||||
|
name(name_),
|
||||||
|
backup(func_),
|
||||||
|
thunk(thunk_) {}
|
||||||
|
// Virtual Function
|
||||||
|
template <typename Parent>
|
||||||
|
__Function(const std::string name_, const ptr_type thunk_, ptr_type *const func_, const Parent parent):
|
||||||
|
func(__VirtualFunctionInfo(func_, parent)),
|
||||||
|
enabled(std::get<__VirtualFunctionInfo>(func).can_overwrite()),
|
||||||
|
name(name_),
|
||||||
|
backup(*get_vtable_addr()),
|
||||||
|
thunk(thunk_) {}
|
||||||
|
|
||||||
|
// Overwrite Function
|
||||||
|
[[nodiscard]] 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, std::forward<Args>(args)...);
|
||||||
|
};
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
[[nodiscard]] ptr_type get(bool result_will_be_stored) {
|
||||||
|
if (!enabled) {
|
||||||
|
return nullptr;
|
||||||
|
} else {
|
||||||
|
if (result_will_be_stored) {
|
||||||
|
enable_thunk();
|
||||||
|
}
|
||||||
|
if (is_virtual()) {
|
||||||
|
return *get_vtable_addr();
|
||||||
|
} else {
|
||||||
|
return std::get<ptr_type>(func);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[[nodiscard]] ptr_type *get_vtable_addr() const {
|
||||||
|
return (ptr_type *) std::get<__VirtualFunctionInfo>(func).addr;
|
||||||
|
}
|
||||||
|
[[nodiscard]] type get_thunk_target() const {
|
||||||
|
if (thunk_target) {
|
||||||
|
return thunk_target;
|
||||||
|
} else {
|
||||||
|
return backup;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Current Function
|
||||||
|
std::variant<ptr_type, __VirtualFunctionInfo> func;
|
||||||
|
[[nodiscard]] bool is_virtual() const {
|
||||||
|
return func.index() == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
// State
|
||||||
|
const bool enabled;
|
||||||
|
const std::string name;
|
||||||
|
|
||||||
|
// Backup Of Original Function Pointer
|
||||||
|
const ptr_type backup;
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Thunk
|
||||||
|
const ptr_type thunk;
|
||||||
|
type thunk_target;
|
||||||
|
bool thunk_enabled = false;
|
||||||
|
void enable_thunk() {
|
||||||
|
if (!thunk_enabled && enabled) {
|
||||||
|
ptr_type real_thunk = (ptr_type) thunk_enabler((void *) backup, (void *) thunk);
|
||||||
|
if (!is_virtual()) {
|
||||||
|
func = real_thunk;
|
||||||
|
}
|
||||||
|
thunk_enabled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Shortcuts
|
||||||
|
typedef unsigned char uchar;
|
||||||
|
typedef unsigned short ushort;
|
||||||
|
typedef unsigned int uint;
|
||||||
|
|
||||||
|
// 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;
|
||||||
@ -23,12 +24,13 @@ export function parseTypeAndName(piece: string) {
|
|||||||
let name = piece.substring(index + 1)!;
|
let name = piece.substring(index + 1)!;
|
||||||
let type = piece.substring(0, index)!;
|
let type = piece.substring(0, index)!;
|
||||||
// Move Asterisks From Name To Type
|
// Move Asterisks From Name To Type
|
||||||
while (name.startsWith('*')) {
|
while (name.startsWith('*') || name.startsWith('&')) {
|
||||||
|
const x = name.substring(0, 1);
|
||||||
name = name.substring(1);
|
name = name.substring(1);
|
||||||
if (!type.endsWith('*')) {
|
if (!type.endsWith('*') && !type.endsWith('&')) {
|
||||||
type += ' ';
|
type += ' ';
|
||||||
}
|
}
|
||||||
type += '*';
|
type += x;
|
||||||
}
|
}
|
||||||
// Return
|
// Return
|
||||||
return {type, name};
|
return {type, name};
|
||||||
@ -56,7 +58,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;
|
||||||
@ -66,14 +68,7 @@ export function toHex(x: number) {
|
|||||||
return '0x' + x.toString(16);
|
return '0x' + x.toString(16);
|
||||||
}
|
}
|
||||||
export function assertSize(name: string, size: number) {
|
export function assertSize(name: string, size: number) {
|
||||||
let out = '';
|
return `static_assert(sizeof(${name}) == ${toHex(size)}, "Invalid Size");\n`;
|
||||||
// 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) {
|
export function safeParseInt(str: string) {
|
||||||
const x = parseInt(str);
|
const x = parseInt(str);
|
||||||
@ -85,39 +80,28 @@ 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 getDataDir() {
|
||||||
let index = args.indexOf(',');
|
return path.join(__dirname, '..', 'data');
|
||||||
if (index === -1) {
|
}
|
||||||
index = args.indexOf(')');
|
export function formatFile(file: string, options: {[key: string]: string}) {
|
||||||
} else {
|
file = path.join(getDataDir(), file);
|
||||||
index++;
|
let data = fs.readFileSync(file, {encoding: 'utf8'});
|
||||||
|
for (const key in options) {
|
||||||
|
data = data.replace(`{{ ${key} }}`, options[key]!);
|
||||||
}
|
}
|
||||||
return '(' + args.substring(index).trim();
|
return data.trim() + '\n';
|
||||||
}
|
}
|
||||||
|
export const INTERNAL = '__';
|
||||||
|
export function preventConstruction(self: string) {
|
||||||
|
let out = '';
|
||||||
|
out += `${INDENT}${INTERNAL}PREVENT_CONSTRUCTION(${self});\n`;
|
||||||
|
out += `${INDENT}${INTERNAL}PREVENT_COPY(${self});\n`;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
export const LEAN_HEADER_GUARD = '#ifndef LEAN_SYMBOLS_HEADER\n';
|
116
src/index.ts
116
src/index.ts
@ -1,5 +1,6 @@
|
|||||||
import * as fs from 'node:fs';
|
import * as fs from 'node:fs';
|
||||||
import { STRUCTURE_FILES, EXTENSION, INDENT } from './common';
|
import * as path from 'node:path';
|
||||||
|
import { STRUCTURE_FILES, EXTENSION, formatFile, getDataDir } from './common';
|
||||||
import { getStructure } from './map';
|
import { getStructure } from './map';
|
||||||
import { Struct } from './struct';
|
import { Struct } from './struct';
|
||||||
|
|
||||||
@ -14,14 +15,13 @@ function invalidFileType(file: string) {
|
|||||||
throw new Error(`Invalid File Type: ${file}`);
|
throw new Error(`Invalid File Type: ${file}`);
|
||||||
}
|
}
|
||||||
const sourceOutput = process.argv.shift()!;
|
const sourceOutput = process.argv.shift()!;
|
||||||
if (!sourceOutput.endsWith('.cpp')) {
|
fs.rmSync(sourceOutput, {force: true, recursive: true});
|
||||||
invalidFileType(sourceOutput);
|
fs.mkdirSync(sourceOutput, {recursive: true});
|
||||||
}
|
|
||||||
const headerOutput = process.argv.shift()!;
|
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);
|
||||||
@ -52,9 +52,9 @@ function loadSymbols() {
|
|||||||
}
|
}
|
||||||
// Sort
|
// Sort
|
||||||
structureObjects.sort((a, b) => {
|
structureObjects.sort((a, b) => {
|
||||||
if (a.getName() > b.getName()) {
|
if (a.name > b.name) {
|
||||||
return 1;
|
return 1;
|
||||||
} else if (a.getName() < b.getName()) {
|
} else if (a.name < b.name) {
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
@ -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.name;
|
||||||
if (isFirst) {
|
|
||||||
isFirst = false;
|
|
||||||
} else {
|
|
||||||
structures += '\n';
|
|
||||||
}
|
|
||||||
structures += `// ${name}\n`;
|
structures += `// ${name}\n`;
|
||||||
try {
|
try {
|
||||||
structures += structure.generate();
|
structures += structure.generate();
|
||||||
@ -127,90 +121,68 @@ 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
|
||||||
let result = '';
|
return structures;
|
||||||
result += '// Init\n';
|
|
||||||
result += 'void init_symbols();\n\n';
|
|
||||||
result += structures;
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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// Shortcuts\n';
|
|
||||||
result += 'typedef unsigned char uchar;\n';
|
|
||||||
result += 'typedef unsigned short ushort;\n';
|
|
||||||
result += 'typedef unsigned int uint;\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, data: getDataDir()});
|
||||||
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);
|
||||||
|
|
||||||
// Generate Compiled Code
|
// Generate Compiled Code
|
||||||
function makeCompiledCode(output: string) {
|
function makeCompiledCode(outputDir: string) {
|
||||||
// Load Symbols
|
// Load Symbols
|
||||||
const structureObjects = loadSymbols();
|
const structureObjects = loadSymbols();
|
||||||
|
|
||||||
// Generate
|
// Generate
|
||||||
let declarations = '';
|
let first = true;
|
||||||
let init = '';
|
|
||||||
let isFirst = true;
|
|
||||||
for (const structure of structureObjects) {
|
for (const structure of structureObjects) {
|
||||||
const name = structure.getName();
|
// Thunks
|
||||||
if (isFirst) {
|
let declarations = '';
|
||||||
isFirst = false;
|
if (first) {
|
||||||
} else {
|
first = false;
|
||||||
declarations += '\n';
|
declarations += '// Thunk Enabler\n';
|
||||||
init += '\n';
|
declarations += 'thunk_enabler_t thunk_enabler;\n\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Structure
|
||||||
|
const name = structure.name;
|
||||||
declarations += `// ${name}\n`;
|
declarations += `// ${name}\n`;
|
||||||
init += `${INDENT}// ${name}\n`;
|
|
||||||
try {
|
try {
|
||||||
const code = structure.generateCode();
|
declarations += structure.generateCode().trim();
|
||||||
declarations += code.functions;
|
|
||||||
init += code.init;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
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';
|
const result = formatFile('out.cpp', {headerPath, main, data: getDataDir()});
|
||||||
result += '\n// Init\n';
|
const output = path.join(outputDir, name + '.cpp');
|
||||||
result += 'void init_symbols() {\n';
|
fs.writeFileSync(output, result);
|
||||||
result += init;
|
}
|
||||||
result += '}\n\n';
|
|
||||||
result += declarations;
|
|
||||||
fs.writeFileSync(output, result);
|
|
||||||
}
|
}
|
||||||
makeCompiledCode(sourceOutput);
|
makeCompiledCode(sourceOutput);
|
||||||
|
@ -116,29 +116,29 @@ export function load(target: Struct, name: string, isExtended: boolean) {
|
|||||||
case 'vtable-size': {
|
case 'vtable-size': {
|
||||||
// Set VTable Size
|
// Set VTable Size
|
||||||
if (!isExtended) {
|
if (!isExtended) {
|
||||||
target.setVTableSize(safeParseInt(args));
|
target.getVTable().setSize(safeParseInt(args));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'vtable': {
|
case 'vtable': {
|
||||||
// Set VTable Address
|
// Set VTable Address
|
||||||
target.ensureVTable();
|
const vtable = target.getVTable();
|
||||||
if (!isExtended && args.length > 0) {
|
if (!isExtended && args.length > 0) {
|
||||||
target.setVTableAddress(safeParseInt(args));
|
vtable.setAddress(safeParseInt(args));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'property': {
|
case 'property': {
|
||||||
// Add Property
|
// Add Property
|
||||||
const info = parseProperty(args);
|
const info = parseProperty(args);
|
||||||
target.addProperty(new Property(info.offset, info.type, info.name, target.getName()));
|
target.addProperty(new Property(info.offset, info.type, info.name, target.name));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'static-property': {
|
case 'static-property': {
|
||||||
// Add Static Property
|
// Add Static Property
|
||||||
if (!isExtended) {
|
if (!isExtended) {
|
||||||
const info = parseProperty(args);
|
const info = parseProperty(args);
|
||||||
target.addStaticProperty(new StaticProperty(info.offset, info.type, info.name, target.getName()));
|
target.addStaticProperty(new StaticProperty(info.offset, info.type, info.name, target.name));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -150,14 +150,14 @@ export function load(target: Struct, name: string, isExtended: boolean) {
|
|||||||
}
|
}
|
||||||
case 'virtual-method': {
|
case 'virtual-method': {
|
||||||
// Add Virtual Method
|
// Add Virtual Method
|
||||||
const method = parseMethod(args, target.getName(), true, isExtended);
|
const method = parseMethod(args, target.name, true, isExtended);
|
||||||
target.addMethod(method, true);
|
target.addMethod(method, true);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'static-method': {
|
case 'static-method': {
|
||||||
// Add Static Method
|
// Add Static Method
|
||||||
if (!isExtended) {
|
if (!isExtended) {
|
||||||
const method = parseMethod(args, target.getName(), false, false);
|
const method = parseMethod(args, target.name, false, false);
|
||||||
target.addMethod(method, false);
|
target.addMethod(method, false);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -165,7 +165,7 @@ export function load(target: Struct, name: string, isExtended: boolean) {
|
|||||||
case 'constructor': {
|
case 'constructor': {
|
||||||
// Constructor
|
// Constructor
|
||||||
if (!isExtended) {
|
if (!isExtended) {
|
||||||
let data = `${target.getName()} *constructor`;
|
let data = `${target.name} *constructor`;
|
||||||
if (args.startsWith('(')) {
|
if (args.startsWith('(')) {
|
||||||
// No Custom Name
|
// No Custom Name
|
||||||
data += ' ';
|
data += ' ';
|
||||||
@ -174,14 +174,22 @@ export function load(target: Struct, name: string, isExtended: boolean) {
|
|||||||
data += '_';
|
data += '_';
|
||||||
}
|
}
|
||||||
data += args;
|
data += args;
|
||||||
const method = parseMethod(data, target.getName(), true, false);
|
const method = parseMethod(data, target.name, true, false);
|
||||||
target.addMethod(method, false);
|
target.addMethod(method, false);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'vtable-destructor-offset': {
|
case 'vtable-destructor-offset': {
|
||||||
// Set VTable Destructor Offset
|
// Set VTable Destructor Offset
|
||||||
target.setVTableDestructorOffset(safeParseInt(args));
|
target.getVTable().setDestructorOffset(safeParseInt(args));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'mark-as-simple': {
|
||||||
|
// Mark As Simple
|
||||||
|
if (isExtended) {
|
||||||
|
throw new Error('Cannot Extend Simple Structure');
|
||||||
|
}
|
||||||
|
target.markAsSimple();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { INDENT, formatType, getArgNames } from './common';
|
import { INDENT, INTERNAL, formatType, 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,56 +17,55 @@ 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);
|
|
||||||
}
|
}
|
||||||
getType() {
|
getType() {
|
||||||
return `${this.getName()}_t`;
|
return this.getName() + '_t';
|
||||||
|
}
|
||||||
|
#getRawType() {
|
||||||
|
return INTERNAL + 'raw_' + this.getType();
|
||||||
|
}
|
||||||
|
getProperty() {
|
||||||
|
return `${INDENT}${this.#getRawType()} *${this.shortName};\n`;
|
||||||
|
}
|
||||||
|
#getFullType() {
|
||||||
|
return `${INTERNAL}Function<${this.#getRawType()}>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate Type Definition
|
// Typedefs
|
||||||
generateTypedef() {
|
generateTypedefs() {
|
||||||
let out = '';
|
let out = '';
|
||||||
|
out += `typedef ${formatType(this.returnType.trim())}${this.#getRawType()}${this.args.trim()};\n`;
|
||||||
// Normal Definition
|
out += `typedef const std::function<${this.#getRawType()}> &${this.getType()};\n`;
|
||||||
const returnType = formatType(this.returnType);
|
|
||||||
out += `typedef ${returnType}(*${this.getType()})${this.args};\n`;
|
|
||||||
|
|
||||||
// Fancy Overwrite Does Not Support Varargs
|
|
||||||
if (!this.hasVarargs) {
|
|
||||||
// Overwrite Helper
|
|
||||||
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
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate Variable Definition
|
// Overwrite Helper
|
||||||
generateDefinition(nameSuffix?: string) {
|
#getVirtualCall(self: string = this.self) {
|
||||||
return `${this.getType()} ${this.getName()}${nameSuffix !== undefined ? nameSuffix : ''};\n`;
|
return `${self}_vtable::base->${this.shortName}`;
|
||||||
}
|
}
|
||||||
|
generate(code: boolean, isVirtual: boolean, parentSelf?: string) {
|
||||||
// Generate "New Method" Test
|
let out = '';
|
||||||
generateNewMethodTest(parent: string | null, prefix: string, suffix: string) {
|
out += 'extern ';
|
||||||
let out = `#define __is_new_method_${this.getName()}() (`;
|
const type = this.#getFullType();
|
||||||
if (!this.isInherited) {
|
out += `${type} *const ${this.getName()}`;
|
||||||
out += 'true';
|
if (code) {
|
||||||
} else {
|
out += ` = new ${type}(${JSON.stringify(this.getName('::'))}, `;
|
||||||
out += `((void *) ${prefix}${this.getName()}${suffix}) != ((void *) ${prefix}${this.getNameWithCustomSelf(parent!)}${suffix})`;
|
out += `${INTERNAL}thunk<&${this.getName()}>, `;
|
||||||
|
if (isVirtual) {
|
||||||
|
const parentMethod = parentSelf ? this.#getVirtualCall(parentSelf) : 'nullptr';
|
||||||
|
out += `&${this.#getVirtualCall()}, ${parentMethod}`;
|
||||||
|
} else {
|
||||||
|
out += `(${this.#getRawType()} *) ${toHex(this.address)}`;
|
||||||
|
}
|
||||||
|
out += ')';
|
||||||
}
|
}
|
||||||
out += ')\n';
|
out += ';\n';
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
import { formatType } from './common';
|
import { INTERNAL, formatType } from './common';
|
||||||
|
|
||||||
export class Property {
|
export class Property {
|
||||||
readonly offset: number;
|
readonly offset: number;
|
||||||
@ -16,7 +16,7 @@ export class Property {
|
|||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
type() {
|
type() {
|
||||||
return `__type_${this.fullName()}`;
|
return `${INTERNAL}type_${this.fullName()}`;
|
||||||
}
|
}
|
||||||
typedef() {
|
typedef() {
|
||||||
let arrayInfo = '';
|
let arrayInfo = '';
|
||||||
@ -34,8 +34,14 @@ export class Property {
|
|||||||
}
|
}
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
#fullName(separator: string) {
|
||||||
|
return this.#self + separator + this.name();
|
||||||
|
}
|
||||||
fullName() {
|
fullName() {
|
||||||
return `${this.#self}_${this.name()}`;
|
return this.#fullName('_');
|
||||||
|
}
|
||||||
|
prettyName() {
|
||||||
|
return this.#fullName('::');
|
||||||
}
|
}
|
||||||
rawType() {
|
rawType() {
|
||||||
return this.#type;
|
return this.#type;
|
||||||
@ -48,13 +54,8 @@ export class StaticProperty extends Property {
|
|||||||
super(address, type, name, self);
|
super(address, type, name, self);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate Variable Definition
|
// Reference
|
||||||
globalPointerDefinition() {
|
referenceDefinition(addSelf: boolean) {
|
||||||
return `${this.type()} *${this.fullName()}_pointer;\n`;
|
return `${this.type()} &${addSelf ? this.prettyName() : this.name()}`;
|
||||||
}
|
|
||||||
|
|
||||||
// Generate Macro
|
|
||||||
macro() {
|
|
||||||
return `#define ${this.fullName()} (*${this.fullName()}_pointer)\n`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
200
src/struct.ts
200
src/struct.ts
@ -1,10 +1,10 @@
|
|||||||
import { INDENT, STRUCTURE_FILES, toHex, assertSize, formatType, getArgNames, removeFirstArg } from './common';
|
import { INDENT, STRUCTURE_FILES, toHex, assertSize, INTERNAL, preventConstruction, LEAN_HEADER_GUARD } 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';
|
||||||
|
|
||||||
export class Struct {
|
export class Struct {
|
||||||
readonly #name: string;
|
readonly name: string;
|
||||||
#vtable: VTable | null;
|
#vtable: VTable | null;
|
||||||
readonly #methods: Method[];
|
readonly #methods: Method[];
|
||||||
readonly #properties: Property[];
|
readonly #properties: Property[];
|
||||||
@ -12,10 +12,11 @@ export class Struct {
|
|||||||
readonly #dependencies: string[];
|
readonly #dependencies: string[];
|
||||||
readonly #staticProperties: StaticProperty[];
|
readonly #staticProperties: StaticProperty[];
|
||||||
#directParent: string | null;
|
#directParent: string | null;
|
||||||
|
#isSimple: boolean;
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
constructor(name: string) {
|
constructor(name: string) {
|
||||||
this.#name = name;
|
this.name = name;
|
||||||
this.#methods = [];
|
this.#methods = [];
|
||||||
this.#properties = [];
|
this.#properties = [];
|
||||||
this.#vtable = null;
|
this.#vtable = null;
|
||||||
@ -23,6 +24,7 @@ export class Struct {
|
|||||||
this.#dependencies = [];
|
this.#dependencies = [];
|
||||||
this.#staticProperties = [];
|
this.#staticProperties = [];
|
||||||
this.#directParent = null;
|
this.#directParent = null;
|
||||||
|
this.#isSimple = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dependencies
|
// Dependencies
|
||||||
@ -34,39 +36,29 @@ export class Struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ensure VTable Exists
|
// Ensure VTable Exists
|
||||||
ensureVTable() {
|
getVTable() {
|
||||||
if (this.#vtable === null) {
|
if (this.#vtable === null) {
|
||||||
this.#vtable = new VTable(this.#name);
|
this.#vtable = new VTable(this.name);
|
||||||
this.addProperty(this.#vtable.property);
|
this.addProperty(this.#vtable.property);
|
||||||
}
|
}
|
||||||
}
|
return this.#vtable;
|
||||||
|
|
||||||
// Set VTable Destructor Offset
|
|
||||||
setVTableDestructorOffset(offset: number) {
|
|
||||||
this.ensureVTable();
|
|
||||||
this.#vtable!.setDestructorOffset(offset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setters
|
// Setters
|
||||||
setSize(size: number) {
|
setSize(size: number) {
|
||||||
this.#size = size;
|
this.#size = size;
|
||||||
}
|
}
|
||||||
// Getters
|
|
||||||
getName() {
|
|
||||||
return this.#name;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add Method
|
// Add Method
|
||||||
addMethod(method: Method, isVirtual: boolean) {
|
addMethod(method: Method, isVirtual: boolean) {
|
||||||
if (method.returnType !== this.#name && method.returnType in STRUCTURE_FILES) {
|
if (method.returnType !== this.name && method.returnType in STRUCTURE_FILES) {
|
||||||
this.#addDependency(method.returnType);
|
this.#addDependency(method.returnType);
|
||||||
}
|
}
|
||||||
if (isVirtual) {
|
if (isVirtual) {
|
||||||
if (method.self !== this.#name) {
|
if (method.self !== this.name) {
|
||||||
throw new Error();
|
throw new Error();
|
||||||
}
|
}
|
||||||
this.ensureVTable();
|
this.getVTable().add(method);
|
||||||
this.#vtable!.add(method);
|
|
||||||
} else {
|
} else {
|
||||||
if (method.isInherited) {
|
if (method.isInherited) {
|
||||||
this.#addDependency(method.self);
|
this.#addDependency(method.self);
|
||||||
@ -88,14 +80,21 @@ export class Struct {
|
|||||||
this.#staticProperties.push(property);
|
this.#staticProperties.push(property);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure VTable
|
// "Simple" Structures
|
||||||
setVTableSize(size: number) {
|
markAsSimple() {
|
||||||
this.ensureVTable();
|
this.#isSimple = true;
|
||||||
this.#vtable!.setSize(size);
|
|
||||||
}
|
}
|
||||||
setVTableAddress(address: number) {
|
#checkSimple() {
|
||||||
this.ensureVTable();
|
const checks = [
|
||||||
this.#vtable!.setAddress(address);
|
['Cannot Inherit', this.#directParent !== null],
|
||||||
|
['Must Have A Defined Size', this.#size === null],
|
||||||
|
['Cannot Have A VTable', this.#vtable !== null]
|
||||||
|
];
|
||||||
|
for (const check of checks) {
|
||||||
|
if (check[1]) {
|
||||||
|
throw new Error('Simple Structures ' + check[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate Properties
|
// Generate Properties
|
||||||
@ -127,7 +126,9 @@ export class Struct {
|
|||||||
if (lastProperty) {
|
if (lastProperty) {
|
||||||
paddingSize += ` - sizeof(${lastProperty.type()})`;
|
paddingSize += ` - sizeof(${lastProperty.type()})`;
|
||||||
}
|
}
|
||||||
out += `${INDENT}uchar __padding${i}[${paddingSize}];\n`;
|
if (!this.#isSimple) {
|
||||||
|
out += `${INDENT}uchar ${INTERNAL}padding${i}[${paddingSize}];\n`;
|
||||||
|
}
|
||||||
|
|
||||||
// The Actual Property
|
// The Actual Property
|
||||||
if (property !== sizeProperty) {
|
if (property !== sizeProperty) {
|
||||||
@ -138,51 +139,52 @@ export class Struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate C++ Method Shortcuts
|
// Generate C++ Method Shortcuts
|
||||||
|
#generateMethod(method: Method, isVirtual: boolean) {
|
||||||
|
let out = '';
|
||||||
|
out += INDENT;
|
||||||
|
if (method.isStatic) {
|
||||||
|
out += 'static ';
|
||||||
|
}
|
||||||
|
out += `decltype(auto) ${method.shortName}(auto&&... args) {\n`;
|
||||||
|
out += `${INDENT}${INDENT}return `;
|
||||||
|
if (isVirtual) {
|
||||||
|
out += `this->vtable->${method.shortName}`;
|
||||||
|
} else {
|
||||||
|
out += `${method.getName()}->get(false)`;
|
||||||
|
}
|
||||||
|
out += '(';
|
||||||
|
if (!method.isStatic) {
|
||||||
|
out += `(${method.self} *) this, `;
|
||||||
|
}
|
||||||
|
out += 'std::forward<decltype(args)>(args)...);\n';
|
||||||
|
out += `${INDENT}}\n`;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
#generateMethods() {
|
#generateMethods() {
|
||||||
let out = '';
|
let out = '';
|
||||||
|
out += LEAN_HEADER_GUARD;
|
||||||
// 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}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`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Allocation Method
|
||||||
|
if (this.#size !== null) {
|
||||||
|
// THIS DOES NOT CONSTRUCT THE OBJECT
|
||||||
|
out += `${INDENT}static ${this.name} *allocate() {\n`;
|
||||||
|
out += `${INDENT}${INDENT}return (${this.name} *) ::operator new(sizeof(${this.name}));\n`;
|
||||||
|
out += `${INDENT}}\n`;
|
||||||
|
}
|
||||||
// Return
|
// Return
|
||||||
|
out += '#endif\n';
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,25 +192,19 @@ export class Struct {
|
|||||||
generate() {
|
generate() {
|
||||||
let out = '';
|
let out = '';
|
||||||
|
|
||||||
// Static Properties
|
// Check "Simple" Status
|
||||||
for (const property of this.#staticProperties) {
|
if (this.#isSimple) {
|
||||||
out += property.typedef();
|
this.#checkSimple();
|
||||||
out += `extern ${property.globalPointerDefinition()}`;
|
|
||||||
out += property.macro();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Methods
|
|
||||||
for (const method of this.#methods) {
|
|
||||||
if (!method.isInherited) {
|
|
||||||
out += method.generateTypedef();
|
|
||||||
out += `extern ${method.generateDefinition()}`;
|
|
||||||
out += method.generateNewMethodTest(this.#directParent, '', '');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// VTable
|
// VTable
|
||||||
if (this.#vtable !== null) {
|
if (this.#vtable !== null) {
|
||||||
out += this.#vtable.generate(this.#directParent);
|
out += this.#vtable.generate();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Static Properties
|
||||||
|
for (const property of this.#staticProperties) {
|
||||||
|
out += property.typedef();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Property Typedefs
|
// Property Typedefs
|
||||||
@ -216,10 +212,32 @@ export class Struct {
|
|||||||
out += property.typedef();
|
out += property.typedef();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Method Wrappers
|
||||||
|
let typedefs = '';
|
||||||
|
let methodsOut = '';
|
||||||
|
for (const method of this.#methods) {
|
||||||
|
if (!method.isInherited) {
|
||||||
|
typedefs += method.generateTypedefs();
|
||||||
|
methodsOut += method.generate(false, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out += typedefs;
|
||||||
|
out += LEAN_HEADER_GUARD;
|
||||||
|
out += methodsOut;
|
||||||
|
out += '#endif\n';
|
||||||
|
|
||||||
// Structure
|
// Structure
|
||||||
out += `struct ${this.#name} {\n`;
|
out += `struct ${this.name} final {\n`;
|
||||||
out += this.#generateProperties();
|
out += this.#generateProperties();
|
||||||
out += this.#generateMethods();
|
out += this.#generateMethods();
|
||||||
|
for (const property of this.#staticProperties) {
|
||||||
|
// Static Property References
|
||||||
|
out += `${INDENT}static ${property.referenceDefinition(false)};\n`;
|
||||||
|
}
|
||||||
|
if (!this.#isSimple) {
|
||||||
|
// Disable Construction Of Complex Structures
|
||||||
|
out += preventConstruction(this.name);
|
||||||
|
}
|
||||||
out += `};\n`;
|
out += `};\n`;
|
||||||
|
|
||||||
// Sanity Check Offsets
|
// Sanity Check Offsets
|
||||||
@ -227,17 +245,12 @@ export class Struct {
|
|||||||
const property = this.#properties[i]!;
|
const property = this.#properties[i]!;
|
||||||
const name = property.name();
|
const name = property.name();
|
||||||
const offset = property.offset;
|
const offset = property.offset;
|
||||||
out += `static_assert(offsetof(${this.#name}, ${name}) == ${toHex(offset)}, "Invalid Offset");\n`;
|
out += `static_assert(offsetof(${this.name}, ${name}) == ${toHex(offset)}, "Invalid Offset");\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sanity Check Size
|
// Sanity Check Size
|
||||||
if (this.#size !== null) {
|
if (this.#size !== null) {
|
||||||
out += assertSize(this.#name, this.#size);
|
out += assertSize(this.name, this.#size);
|
||||||
}
|
|
||||||
|
|
||||||
// Allocation Function
|
|
||||||
if (this.#size !== null) {
|
|
||||||
out += `${this.#name} *alloc_${this.#name}();\n`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return
|
// Return
|
||||||
@ -246,39 +259,28 @@ export class Struct {
|
|||||||
|
|
||||||
// Generate Compiled Code
|
// Generate Compiled Code
|
||||||
generateCode() {
|
generateCode() {
|
||||||
let declarations = '';
|
let out = '';
|
||||||
let init = '';
|
|
||||||
|
|
||||||
// Static Properties
|
// Static Properties
|
||||||
for (const property of this.#staticProperties) {
|
for (const property of this.#staticProperties) {
|
||||||
init += `${INDENT}${property.fullName()}_pointer = (${property.type()} *) ${toHex(property.offset)};\n`;
|
out += `${property.referenceDefinition(true)} = *(${property.type()} *) ${toHex(property.offset)};\n`;
|
||||||
declarations += property.globalPointerDefinition();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
for (const method of this.#methods) {
|
for (const method of this.#methods) {
|
||||||
if (!method.isInherited) {
|
if (!method.isInherited) {
|
||||||
init += `${INDENT}${method.getName()} = (${method.getType()}) ${toHex(method.address)};\n`;
|
out += method.generate(true, false);
|
||||||
declarations += method.generateDefinition();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// VTable
|
// VTable
|
||||||
if (this.#vtable !== null) {
|
if (this.#vtable !== null) {
|
||||||
const vtable = this.#vtable.generateCode();
|
const vtable = this.#vtable.generateCode(this.#directParent);
|
||||||
declarations += vtable.declarations;
|
out += vtable;
|
||||||
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
|
||||||
return {functions: declarations, init};
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set Direct Parent (Used For "New Method" Testing)
|
// Set Direct Parent (Used For "New Method" Testing)
|
||||||
|
120
src/vtable.ts
120
src/vtable.ts
@ -1,7 +1,8 @@
|
|||||||
import { INDENT, POINTER_SIZE, RTTI_SIZE, assertSize, getSelfArg, toHex } from './common';
|
import { INDENT, LEAN_HEADER_GUARD, POINTER_SIZE, assertSize, getSelfArg, preventConstruction, toHex } from './common';
|
||||||
import { Method } from './method';
|
import { Method } from './method';
|
||||||
import { Property } from './property';
|
import { Property } from './property';
|
||||||
|
|
||||||
|
const structuresWithVTableAddress: string[] = [];
|
||||||
export class VTable {
|
export class VTable {
|
||||||
readonly #self: string;
|
readonly #self: string;
|
||||||
#address: number | null;
|
#address: number | null;
|
||||||
@ -24,6 +25,7 @@ export class VTable {
|
|||||||
// Setters
|
// Setters
|
||||||
setAddress(address: number) {
|
setAddress(address: number) {
|
||||||
this.#address = address;
|
this.#address = address;
|
||||||
|
structuresWithVTableAddress.push(this.#self);
|
||||||
}
|
}
|
||||||
setSize(size: number) {
|
setSize(size: number) {
|
||||||
this.#size = size;
|
this.#size = size;
|
||||||
@ -84,34 +86,69 @@ export class VTable {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get Parent Method
|
||||||
|
#getParentSelf(method: Method, parent: string | null) {
|
||||||
|
if (method.isInherited) {
|
||||||
|
// Parent Exists
|
||||||
|
let out: string;
|
||||||
|
if (structuresWithVTableAddress.includes(parent!)) {
|
||||||
|
out = parent!;
|
||||||
|
} else {
|
||||||
|
// Unable To Determine
|
||||||
|
out = this.#self;
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
} else {
|
||||||
|
// Method Has No Parent
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Generate Header Code
|
// Generate Header Code
|
||||||
generate(directParent: string | null) {
|
canGenerateWrappers() {
|
||||||
|
return this.#address !== null;
|
||||||
|
}
|
||||||
|
generate() {
|
||||||
let out = '';
|
let out = '';
|
||||||
|
|
||||||
// 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++) {
|
let typedefs = '';
|
||||||
const info = methods[i];
|
let methodsOut = '';
|
||||||
|
for (const info of methods) {
|
||||||
if (info) {
|
if (info) {
|
||||||
out += info.generateTypedef();
|
typedefs += info.generateTypedefs();
|
||||||
|
if (this.canGenerateWrappers()) {
|
||||||
|
methodsOut += info.generate(false, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
out += typedefs;
|
||||||
|
out += LEAN_HEADER_GUARD;
|
||||||
|
out += methodsOut;
|
||||||
|
out += '#endif\n';
|
||||||
|
|
||||||
// Structure
|
// Structure
|
||||||
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()} final {\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());
|
||||||
|
}
|
||||||
|
if (this.#address !== null) {
|
||||||
|
// Base
|
||||||
|
out += `${INDENT}static ${this.#getName()} *base;\n`;
|
||||||
}
|
}
|
||||||
out += `};\n`;
|
out += `};\n`;
|
||||||
|
|
||||||
@ -120,34 +157,13 @@ export class VTable {
|
|||||||
out += assertSize(this.#getName(), this.#size);
|
out += assertSize(this.#getName(), this.#size);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pointers
|
|
||||||
if (this.#address !== null) {
|
|
||||||
// Base
|
|
||||||
out += `extern ${this.#getName()} *${this.#getName()}_base;\n`;
|
|
||||||
// Methods
|
|
||||||
for (let i = 0; i < methods.length; i++) {
|
|
||||||
const info = methods[i];
|
|
||||||
if (info) {
|
|
||||||
const type = `${info.getType()} *`;
|
|
||||||
out += `extern ${type}${info.getName()}_vtable_addr;\n`;
|
|
||||||
out += info.generateNewMethodTest(directParent, '*', '_vtable_addr');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Duplication Method
|
|
||||||
if (this.#size !== null) {
|
|
||||||
out += `${this.#getName()} *dup_${this.#getName()}(${this.#getName()} *vtable);\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return
|
// Return
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate Compiled Code
|
// Generate Compiled Code
|
||||||
generateCode() {
|
generateCode(directParent: string | null) {
|
||||||
let declarations = '';
|
let out = '';
|
||||||
let init = '';
|
|
||||||
|
|
||||||
// Check
|
// Check
|
||||||
this.#check();
|
this.#check();
|
||||||
@ -155,38 +171,20 @@ export class VTable {
|
|||||||
// Pointers
|
// Pointers
|
||||||
if (this.#address !== null) {
|
if (this.#address !== null) {
|
||||||
// Base
|
// Base
|
||||||
init += `${INDENT}${this.#getName()}_base = (${this.#getName()} *) ${toHex(this.#address)};\n`;
|
out += `${this.#getName()} *${this.#getName()}::base = (${this.#getName()} *) ${toHex(this.#address)};\n`;
|
||||||
declarations += `${this.#getName()} *${this.#getName()}_base;\n`;
|
}
|
||||||
// Methods
|
|
||||||
|
// Method Wrappers
|
||||||
|
if (this.canGenerateWrappers()) {
|
||||||
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 type = `${info.getType()} *`;
|
|
||||||
init += `${INDENT}${info.getName()}_vtable_addr = (${type}) ${toHex(vtableAddress)};\n`;
|
|
||||||
declarations += `${type}${info.getName()}_vtable_addr;\n`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Duplication Method
|
|
||||||
if (this.#size !== null) {
|
|
||||||
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
|
||||||
return {declarations, init};
|
return out;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -13,8 +13,10 @@
|
|||||||
<item>static-method</item>
|
<item>static-method</item>
|
||||||
<item>constructor</item>
|
<item>constructor</item>
|
||||||
<item>vtable-destructor-offset</item>
|
<item>vtable-destructor-offset</item>
|
||||||
|
<item>mark-as-simple</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>
|
@ -2,16 +2,20 @@ syntax def "\.def$"
|
|||||||
comment "//"
|
comment "//"
|
||||||
|
|
||||||
# Commands
|
# Commands
|
||||||
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|mark-as-simple)\>"
|
||||||
|
|
||||||
# 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 "//.*"
|
||||||
|
|
||||||
|
# Whitespace.
|
||||||
|
color normal "[[:space:]]+"
|
||||||
# Trailing whitespace.
|
# Trailing whitespace.
|
||||||
color ,green "[[:space:]]+$"
|
color ,green "[[:space:]]+$"
|
@ -0,0 +1,24 @@
|
|||||||
|
<?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>
|
@ -0,0 +1,55 @@
|
|||||||
|
<?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-destructor-offset|vtable|property|static-property|method|virtual-method|static-method|constructor|mark-as-simple)\b</string>
|
||||||
|
</dict>
|
||||||
|
<dict>
|
||||||
|
<key>name</key>
|
||||||
|
<string>storage.type.symbol-processor</string>
|
||||||
|
<key>match</key>
|
||||||
|
<string>\b(const|char|uchar|short|ushort|int|uint|float|bool|void|std::string|std::vector|std::map|unsigned|long)\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>
|
@ -0,0 +1,16 @@
|
|||||||
|
<?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>
|
Loading…
Reference in New Issue
Block a user