Compare commits
No commits in common. "10026e9a0419ff1cea1ddb80f5ccddf6a53818d3" and "6f792dfb167e48e2e80b8484ac51261ab976b22d" have entirely different histories.
10026e9a04
...
6f792dfb16
@ -1,32 +0,0 @@
|
||||
#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
110
data/function.h
@ -1,110 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
}
|
||||
};
|
@ -1,9 +1,6 @@
|
||||
#include "{{ headerPath }}"
|
||||
#include "{{ data }}/function.cpp"
|
||||
|
||||
// Thunks
|
||||
template <typename T>
|
||||
struct __Thunk;
|
||||
template <typename Ret, typename... Args>
|
||||
struct __Thunk<Ret(Args...)> {
|
||||
template <__Function<Ret(Args...)> *const *func>
|
||||
@ -12,4 +9,7 @@ struct __Thunk<Ret(Args...)> {
|
||||
}
|
||||
};
|
||||
|
||||
// Thunk Enabler
|
||||
thunk_enabler_t thunk_enabler;
|
||||
|
||||
{{ main }}
|
143
data/out.h
143
data/out.h
@ -6,14 +6,147 @@
|
||||
#endif
|
||||
|
||||
// Headers
|
||||
#include "{{ data }}/function.h"
|
||||
#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;
|
||||
friend class __Function<std::remove_pointer_t<T>>;
|
||||
};
|
||||
|
||||
// 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;
|
||||
@ -24,15 +157,15 @@ typedef unsigned int uint;
|
||||
template <typename T>
|
||||
T *dup_vtable(T *vtable) {
|
||||
// Check
|
||||
static_assert(std::is_constructible_v<T>, "Unable To Construct VTable");
|
||||
static_assert(std::is_constructible<T>::value, "Unable To Construct VTable");
|
||||
// Get Size
|
||||
const uchar *real_vtable = (uchar *) vtable;
|
||||
uchar *real_vtable = (uchar *) vtable;
|
||||
real_vtable -= RTTI_SIZE;
|
||||
const size_t real_vtable_size = sizeof(T) + RTTI_SIZE;
|
||||
size_t real_vtable_size = sizeof(T) + RTTI_SIZE;
|
||||
// Allocate
|
||||
uchar *new_vtable = (uchar *) ::operator new(real_vtable_size);
|
||||
// Copy
|
||||
memcpy(new_vtable, real_vtable, real_vtable_size);
|
||||
memcpy((void *) new_vtable, (void *) real_vtable, real_vtable_size);
|
||||
// Return
|
||||
new_vtable += RTTI_SIZE;
|
||||
return (T *) new_vtable;
|
||||
|
@ -86,11 +86,8 @@ export function prependArg(args: string, arg: string) {
|
||||
}
|
||||
return '(' + arg + args.substring(1);
|
||||
}
|
||||
export function getDataDir() {
|
||||
return path.join(__dirname, '..', 'data');
|
||||
}
|
||||
export function formatFile(file: string, options: {[key: string]: string}) {
|
||||
file = path.join(getDataDir(), file);
|
||||
file = path.join(__dirname, '..', 'data', file);
|
||||
let data = fs.readFileSync(file, {encoding: 'utf8'});
|
||||
for (const key in options) {
|
||||
data = data.replace(`{{ ${key} }}`, options[key]!);
|
||||
|
@ -1,5 +1,5 @@
|
||||
import * as fs from 'node:fs';
|
||||
import { STRUCTURE_FILES, EXTENSION, formatFile, getDataDir } from './common';
|
||||
import { STRUCTURE_FILES, EXTENSION, formatFile } from './common';
|
||||
import { getStructure } from './map';
|
||||
import { Struct } from './struct';
|
||||
|
||||
@ -145,7 +145,7 @@ function makeMainHeader(output: string) {
|
||||
// Main
|
||||
const main = makeHeaderPart().trim();
|
||||
// Write
|
||||
const result = formatFile('out.h', {forwardDeclarations, extraHeaders, main, data: getDataDir()});
|
||||
const result = formatFile('out.h', {forwardDeclarations, extraHeaders, main});
|
||||
fs.writeFileSync(output, result);
|
||||
}
|
||||
makeMainHeader(headerOutput);
|
||||
@ -161,7 +161,8 @@ function makeCompiledCode(output: string) {
|
||||
const name = structure.getName();
|
||||
declarations += `// ${name}\n`;
|
||||
try {
|
||||
declarations += structure.generateCode();
|
||||
const code = structure.generateCode();
|
||||
declarations += code;
|
||||
} catch (e) {
|
||||
console.log(`Error Generating Code: ${name}: ${e instanceof Error ? e.stack : e}`);
|
||||
process.exit(1);
|
||||
@ -172,7 +173,7 @@ function makeCompiledCode(output: string) {
|
||||
// Write
|
||||
const headerPath = fs.realpathSync(headerOutput);
|
||||
const main = declarations.trim();
|
||||
const result = formatFile('out.cpp', {headerPath, main, data: getDataDir()});
|
||||
const result = formatFile('out.cpp', {headerPath, main});
|
||||
fs.writeFileSync(output, result);
|
||||
}
|
||||
makeCompiledCode(sourceOutput);
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { INDENT, INTERNAL, formatType, toHex } from './common';
|
||||
import { INDENT, INTERNAL, toHex } from './common';
|
||||
|
||||
export class Method {
|
||||
readonly self: string;
|
||||
@ -27,25 +27,12 @@ export class Method {
|
||||
getType() {
|
||||
return this.getName() + '_t';
|
||||
}
|
||||
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.getWrapperType()}::ptr_type ${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.#getSignature()}>`;
|
||||
}
|
||||
|
||||
// Overwrite Helper
|
||||
#getVirtualCall(self: string = this.self) {
|
||||
@ -56,7 +43,8 @@ export class Method {
|
||||
if (!code) {
|
||||
out += 'extern ';
|
||||
}
|
||||
const type = this.#getFullType();
|
||||
const signature = this.returnType.trim() + this.args.trim();
|
||||
const type = `${INTERNAL}Function<${signature}>`;
|
||||
out += `${type} *const ${this.getName()}`;
|
||||
if (code) {
|
||||
out += ` = new ${type}(${JSON.stringify(this.getName('::'))}, `;
|
||||
@ -66,7 +54,7 @@ export class Method {
|
||||
} else {
|
||||
out += `${this.getWrapperType()}::ptr_type(${toHex(this.address)})`;
|
||||
}
|
||||
out += `, ${INTERNAL}Thunk<${this.#getSignature()}>::call<&${this.getName()}>)`;
|
||||
out += `, ${INTERNAL}Thunk<${signature}>::call<&${this.getName()}>)`;
|
||||
}
|
||||
out += ';\n';
|
||||
if (!code) {
|
||||
|
@ -144,7 +144,7 @@ export class Struct {
|
||||
if (isVirtual) {
|
||||
call += `this->vtable->${method.shortName}`;
|
||||
} else {
|
||||
call += `${method.getName()}->get(false)`;
|
||||
call += `${method.getName()}->get()`;
|
||||
}
|
||||
call += '(';
|
||||
if (!method.isStatic) {
|
||||
|
@ -105,9 +105,6 @@ export class VTable {
|
||||
}
|
||||
|
||||
// Generate Header Code
|
||||
canGenerateWrappers() {
|
||||
return this.#address !== null;
|
||||
}
|
||||
generate() {
|
||||
let out = '';
|
||||
|
||||
@ -116,13 +113,11 @@ export class VTable {
|
||||
|
||||
// Wrappers
|
||||
const methods = this.getMethods();
|
||||
if (this.canGenerateWrappers()) {
|
||||
for (const info of methods) {
|
||||
if (info) {
|
||||
out += info.generate(false, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Structure
|
||||
out += `typedef struct ${this.#getName()} ${this.#getName()};\n`;
|
||||
@ -130,7 +125,7 @@ export class VTable {
|
||||
for (let i = 0; i < methods.length; i++) {
|
||||
const info = methods[i];
|
||||
if (info) {
|
||||
out += info.getProperty(this.canGenerateWrappers());
|
||||
out += info.getProperty();
|
||||
} else {
|
||||
out += `${INDENT}void *unknown${i};\n`;
|
||||
}
|
||||
@ -167,10 +162,7 @@ export class VTable {
|
||||
if (this.#address !== null) {
|
||||
// Base
|
||||
out += `${this.#getName()} *${this.#getName()}_base = (${this.#getName()} *) ${toHex(this.#address)};\n`;
|
||||
}
|
||||
|
||||
// Method Wrappers
|
||||
if (this.canGenerateWrappers()) {
|
||||
// Methods
|
||||
const methods = this.getMethods();
|
||||
for (const info of methods) {
|
||||
if (info) {
|
||||
|
Loading…
Reference in New Issue
Block a user