Compare commits
No commits in common. "2a63f1ff521087f3faf23bf281503cc07a50d2df" and "10026e9a0419ff1cea1ddb80f5ccddf6a53818d3" have entirely different histories.
2a63f1ff52
...
10026e9a04
32
data/function.cpp
Normal file
32
data/function.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include "function.h"
|
||||
|
||||
// Virtual Function Information
|
||||
template <typename T>
|
||||
__VirtualFunctionInfo<T>::__VirtualFunctionInfo(T *const addr_, void *const parent_):
|
||||
addr(addr_),
|
||||
parent(parent_) {}
|
||||
template <typename T>
|
||||
bool __VirtualFunctionInfo<T>::can_overwrite() const {
|
||||
return ((void *) *addr) != parent;
|
||||
}
|
||||
|
||||
// Function Information
|
||||
template <typename Ret, typename... Args>
|
||||
__Function<Ret(Args...)>::__Function(const char *const name_, const __Function<Ret(Args...)>::ptr_type func_, const __Function<Ret(Args...)>::ptr_type thunk_):
|
||||
is_virtual(false),
|
||||
func(func_),
|
||||
enabled(true),
|
||||
name(name_),
|
||||
backup(func_),
|
||||
thunk(thunk_) {}
|
||||
template <typename Ret, typename... Args>
|
||||
__Function<Ret(Args...)>::__Function(const char *const name_, __Function<Ret(Args...)>::ptr_type *const func_, void *const parent, const __Function<Ret(Args...)>::ptr_type thunk_):
|
||||
is_virtual(true),
|
||||
func(__VirtualFunctionInfo(func_, parent)),
|
||||
enabled(std::get<__VirtualFunctionInfo<ptr_type>>(func).can_overwrite()),
|
||||
name(name_),
|
||||
backup(*get_vtable_addr()),
|
||||
thunk(thunk_) {}
|
||||
|
||||
// Thunks
|
||||
thunk_enabler_t thunk_enabler;
|
110
data/function.h
Normal file
110
data/function.h
Normal file
@ -0,0 +1,110 @@
|
||||
#pragma once
|
||||
|
||||
#include <variant>
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
// Virtual Function Information
|
||||
template <typename T>
|
||||
class __Function;
|
||||
template <typename T>
|
||||
class __VirtualFunctionInfo {
|
||||
__VirtualFunctionInfo(T *addr_, void *parent_);
|
||||
[[nodiscard]] bool can_overwrite() const;
|
||||
T *const addr;
|
||||
void *const parent;
|
||||
friend class __Function<std::remove_pointer_t<T>>;
|
||||
};
|
||||
|
||||
// Thunks
|
||||
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 *name_, ptr_type func_, ptr_type thunk_);
|
||||
// Virtual Function
|
||||
__Function(const char *name_, ptr_type *func_, void *parent, ptr_type 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 {
|
||||
if (is_virtual) {
|
||||
return std::get<__VirtualFunctionInfo<ptr_type>>(func).addr;
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
[[nodiscard]] type get_thunk_target() const {
|
||||
if (thunk_target) {
|
||||
return thunk_target;
|
||||
} else {
|
||||
return backup;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
// Current Function
|
||||
const bool is_virtual;
|
||||
std::variant<ptr_type, __VirtualFunctionInfo<ptr_type>> func;
|
||||
|
||||
public:
|
||||
// State
|
||||
const bool enabled;
|
||||
const char *const 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;
|
||||
}
|
||||
}
|
||||
};
|
17
data/out.cpp
17
data/out.cpp
@ -1,10 +1,15 @@
|
||||
#define LEAN_SYMBOLS_HEADER
|
||||
#include "{{ headerPath }}"
|
||||
#include "{{ data }}/function.cpp"
|
||||
|
||||
// Thunk Template
|
||||
template <auto *const *func>
|
||||
decltype(auto) __thunk(auto... args) {
|
||||
return (*func)->get_thunk_target()(std::forward<decltype(args)>(args)...);
|
||||
}
|
||||
// Thunks
|
||||
template <typename T>
|
||||
struct __Thunk;
|
||||
template <typename Ret, typename... Args>
|
||||
struct __Thunk<Ret(Args...)> {
|
||||
template <__Function<Ret(Args...)> *const *func>
|
||||
static Ret call(Args... args) {
|
||||
return (*func)->get_thunk_target()(std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
{{ main }}
|
120
data/out.h
120
data/out.h
@ -6,8 +6,7 @@
|
||||
#endif
|
||||
|
||||
// Headers
|
||||
#include <variant>
|
||||
#include <functional>
|
||||
#include "{{ data }}/function.h"
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@ -15,123 +14,6 @@
|
||||
#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_) {}
|
||||
[[nodiscard]] bool can_overwrite() const {
|
||||
return ((void *) *addr) != parent;
|
||||
}
|
||||
T *const addr;
|
||||
void *const parent;
|
||||
friend class __Function<std::remove_pointer_t<T>>;
|
||||
};
|
||||
|
||||
// Thunks
|
||||
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_):
|
||||
func(func_),
|
||||
enabled(true),
|
||||
name(name_),
|
||||
backup(func_),
|
||||
thunk(thunk_) {}
|
||||
// Virtual Function
|
||||
__Function(const char *const name_, ptr_type *const func_, void *const parent, const ptr_type thunk_):
|
||||
func(__VirtualFunctionInfo(func_, parent)),
|
||||
enabled(std::get<__VirtualFunctionInfo<ptr_type>>(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 std::get<__VirtualFunctionInfo<ptr_type>>(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<ptr_type>> func;
|
||||
[[nodiscard]] bool is_virtual() const {
|
||||
return func.index() == 1;
|
||||
}
|
||||
|
||||
public:
|
||||
// State
|
||||
const bool enabled;
|
||||
const char *const 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;
|
||||
|
@ -104,5 +104,4 @@ export function preventConstruction(self: string) {
|
||||
out += `${INDENT}${self}(const ${self} &) = delete;\n`;
|
||||
out += `${INDENT}${self} &operator=(const ${self} &) = delete;\n`;
|
||||
return out;
|
||||
}
|
||||
export const LEAN_HEADER_GUARD = '#ifndef LEAN_SYMBOLS_HEADER\n';
|
||||
}
|
34
src/index.ts
34
src/index.ts
@ -1,5 +1,4 @@
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import { STRUCTURE_FILES, EXTENSION, formatFile, getDataDir } from './common';
|
||||
import { getStructure } from './map';
|
||||
import { Struct } from './struct';
|
||||
@ -15,8 +14,9 @@ function invalidFileType(file: string) {
|
||||
throw new Error(`Invalid File Type: ${file}`);
|
||||
}
|
||||
const sourceOutput = process.argv.shift()!;
|
||||
fs.rmSync(sourceOutput, {force: true, recursive: true});
|
||||
fs.mkdirSync(sourceOutput, {recursive: true});
|
||||
if (!sourceOutput.endsWith('.cpp')) {
|
||||
invalidFileType(sourceOutput);
|
||||
}
|
||||
const headerOutput = process.argv.shift()!;
|
||||
if (!headerOutput.endsWith('.h')) {
|
||||
invalidFileType(headerOutput);
|
||||
@ -151,38 +151,28 @@ function makeMainHeader(output: string) {
|
||||
makeMainHeader(headerOutput);
|
||||
|
||||
// Generate Compiled Code
|
||||
function makeCompiledCode(outputDir: string) {
|
||||
function makeCompiledCode(output: string) {
|
||||
// Load Symbols
|
||||
const structureObjects = loadSymbols();
|
||||
|
||||
// Generate
|
||||
let first = true;
|
||||
let declarations = '';
|
||||
for (const structure of structureObjects) {
|
||||
// Thunks
|
||||
let declarations = '';
|
||||
if (first) {
|
||||
first = false;
|
||||
declarations += '// Thunk Enabler\n';
|
||||
declarations += 'thunk_enabler_t thunk_enabler;\n\n';
|
||||
}
|
||||
|
||||
// Structure
|
||||
const name = structure.getName();
|
||||
declarations += `// ${name}\n`;
|
||||
try {
|
||||
declarations += structure.generateCode().trim();
|
||||
declarations += structure.generateCode();
|
||||
} catch (e) {
|
||||
console.log(`Error Generating Code: ${name}: ${e instanceof Error ? e.stack : e}`);
|
||||
process.exit(1);
|
||||
}
|
||||
declarations += '\n';
|
||||
|
||||
// Write
|
||||
const headerPath = fs.realpathSync(headerOutput);
|
||||
const main = declarations.trim();
|
||||
const result = formatFile('out.cpp', {headerPath, main, data: getDataDir()});
|
||||
const output = path.join(outputDir, name + '.cpp');
|
||||
fs.writeFileSync(output, result);
|
||||
}
|
||||
|
||||
// Write
|
||||
const headerPath = fs.realpathSync(headerOutput);
|
||||
const main = declarations.trim();
|
||||
const result = formatFile('out.cpp', {headerPath, main, data: getDataDir()});
|
||||
fs.writeFileSync(output, result);
|
||||
}
|
||||
makeCompiledCode(sourceOutput);
|
||||
|
@ -27,22 +27,24 @@ export class Method {
|
||||
getType() {
|
||||
return this.getName() + '_t';
|
||||
}
|
||||
#getRawType() {
|
||||
return INTERNAL + 'raw_' + this.getType();
|
||||
getProperty(hasWrapper: boolean) {
|
||||
let out = INDENT;
|
||||
if (hasWrapper) {
|
||||
out += `${this.getWrapperType()}::ptr_type ${this.shortName}`;
|
||||
} else {
|
||||
out += `${formatType(this.returnType.trim())}(*${this.shortName})${this.args.trim()}`;
|
||||
}
|
||||
out += ';\n';
|
||||
return out;
|
||||
}
|
||||
getProperty() {
|
||||
return `${INDENT}${this.#getRawType()} *${this.shortName};\n`;
|
||||
getWrapperType() {
|
||||
return `std::remove_pointer_t<decltype(${this.getName()})>`;
|
||||
}
|
||||
#getSignature() {
|
||||
return this.returnType.trim() + this.args.trim();
|
||||
}
|
||||
#getFullType() {
|
||||
return `${INTERNAL}Function<${this.#getRawType()}>`;
|
||||
}
|
||||
|
||||
// Typedefs
|
||||
generateTypedefs() {
|
||||
let out = '';
|
||||
out += `typedef ${formatType(this.returnType.trim())}${this.#getRawType()}${this.args.trim()};\n`;
|
||||
out += `typedef std::function<${this.#getRawType()}> ${this.getType()};\n`;
|
||||
return out;
|
||||
return `${INTERNAL}Function<${this.#getSignature()}>`;
|
||||
}
|
||||
|
||||
// Overwrite Helper
|
||||
@ -51,7 +53,9 @@ export class Method {
|
||||
}
|
||||
generate(code: boolean, isVirtual: boolean, parentSelf?: string) {
|
||||
let out = '';
|
||||
out += 'extern ';
|
||||
if (!code) {
|
||||
out += 'extern ';
|
||||
}
|
||||
const type = this.#getFullType();
|
||||
out += `${type} *const ${this.getName()}`;
|
||||
if (code) {
|
||||
@ -60,11 +64,14 @@ export class Method {
|
||||
const parentMethod = parentSelf ? `(void *) ${this.#getVirtualCall(parentSelf)}` : 'nullptr';
|
||||
out += `&${this.#getVirtualCall()}, ${parentMethod}`;
|
||||
} else {
|
||||
out += `(${this.#getRawType()} *) ${toHex(this.address)}`;
|
||||
out += `${this.getWrapperType()}::ptr_type(${toHex(this.address)})`;
|
||||
}
|
||||
out += `, ${INTERNAL}thunk<&${this.getName()}>)`;
|
||||
out += `, ${INTERNAL}Thunk<${this.#getSignature()}>::call<&${this.getName()}>)`;
|
||||
}
|
||||
out += ';\n';
|
||||
if (!code) {
|
||||
out += `typedef ${this.getWrapperType()}::type ${this.getType()};\n`;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import { INDENT, STRUCTURE_FILES, toHex, assertSize, INTERNAL, preventConstruction, LEAN_HEADER_GUARD } from './common';
|
||||
import { INDENT, STRUCTURE_FILES, toHex, assertSize, INTERNAL, preventConstruction } from './common';
|
||||
import { Method } from './method';
|
||||
import { Property, StaticProperty } from './property';
|
||||
import { VTable } from './vtable';
|
||||
@ -139,29 +139,32 @@ export class Struct {
|
||||
|
||||
// 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(false)`;
|
||||
}
|
||||
call += '(';
|
||||
if (!method.isStatic) {
|
||||
call += `(${method.self} *) this, `;
|
||||
}
|
||||
call += 'std::forward<Args>(args)...)';
|
||||
// Generate Shortcut
|
||||
let out = '';
|
||||
out += `${INDENT}template <typename... Args>\n`;
|
||||
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 += `auto ${method.shortName}(Args&&... args) -> decltype(${call}) {\n`;
|
||||
out += `${INDENT}${INDENT}return ${call};\n`;
|
||||
out += `${INDENT}}\n`;
|
||||
return out;
|
||||
}
|
||||
#generateMethods() {
|
||||
let out = '';
|
||||
out += LEAN_HEADER_GUARD;
|
||||
// Normal Methods
|
||||
for (const method of this.#methods) {
|
||||
out += this.#generateMethod(method, false);
|
||||
@ -176,7 +179,6 @@ export class Struct {
|
||||
}
|
||||
}
|
||||
// Return
|
||||
out += '#endif\n';
|
||||
return out;
|
||||
}
|
||||
|
||||
@ -200,18 +202,11 @@ export class Struct {
|
||||
}
|
||||
|
||||
// Method Wrappers
|
||||
let typedefs = '';
|
||||
let methodsOut = '';
|
||||
for (const method of this.#methods) {
|
||||
if (!method.isInherited) {
|
||||
typedefs += method.generateTypedefs();
|
||||
methodsOut += method.generate(false, false);
|
||||
out += method.generate(false, false);
|
||||
}
|
||||
}
|
||||
out += typedefs;
|
||||
out += LEAN_HEADER_GUARD;
|
||||
out += methodsOut;
|
||||
out += '#endif\n';
|
||||
|
||||
// Structure
|
||||
out += `struct ${this.#name} {\n`;
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { INDENT, LEAN_HEADER_GUARD, POINTER_SIZE, assertSize, getSelfArg, preventConstruction, toHex } from './common';
|
||||
import { INDENT, POINTER_SIZE, assertSize, getSelfArg, preventConstruction, toHex } from './common';
|
||||
import { Method } from './method';
|
||||
import { Property } from './property';
|
||||
|
||||
@ -116,20 +116,13 @@ export class VTable {
|
||||
|
||||
// Wrappers
|
||||
const methods = this.getMethods();
|
||||
let typedefs = '';
|
||||
let methodsOut = '';
|
||||
for (const info of methods) {
|
||||
if (info) {
|
||||
typedefs += info.generateTypedefs();
|
||||
if (this.canGenerateWrappers()) {
|
||||
methodsOut += info.generate(false, true);
|
||||
if (this.canGenerateWrappers()) {
|
||||
for (const info of methods) {
|
||||
if (info) {
|
||||
out += info.generate(false, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
out += typedefs;
|
||||
out += LEAN_HEADER_GUARD;
|
||||
out += methodsOut;
|
||||
out += '#endif\n';
|
||||
|
||||
// Structure
|
||||
out += `typedef struct ${this.#getName()} ${this.#getName()};\n`;
|
||||
@ -137,7 +130,7 @@ export class VTable {
|
||||
for (let i = 0; i < methods.length; i++) {
|
||||
const info = methods[i];
|
||||
if (info) {
|
||||
out += info.getProperty();
|
||||
out += info.getProperty(this.canGenerateWrappers());
|
||||
} else {
|
||||
out += `${INDENT}void *unknown${i};\n`;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user