diff --git a/data/out.cpp b/data/out.cpp index d355907..282ca35 100644 --- a/data/out.cpp +++ b/data/out.cpp @@ -1,3 +1,4 @@ +#define LEAN_SYMBOLS_HEADER #include "{{ headerPath }}" // Thunk Template diff --git a/src/common.ts b/src/common.ts index e3151e8..7f2eab7 100644 --- a/src/common.ts +++ b/src/common.ts @@ -104,4 +104,5 @@ export function preventConstruction(self: string) { out += `${INDENT}${self}(const ${self} &) = delete;\n`; out += `${INDENT}${self} &operator=(const ${self} &) = delete;\n`; return out; -} \ No newline at end of file +} +export const LEAN_HEADER_GUARD = '#ifndef LEAN_SYMBOLS_HEADER\n'; \ No newline at end of file diff --git a/src/method.ts b/src/method.ts index 1dd87df..7456f7e 100644 --- a/src/method.ts +++ b/src/method.ts @@ -27,24 +27,22 @@ 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; + #getRawType() { + return INTERNAL + 'raw_' + this.getType(); } - getWrapperType() { - return `std::remove_pointer_t`; - } - #getSignature() { - return this.returnType.trim() + this.args.trim(); + getProperty() { + return `${INDENT}${this.#getRawType()} *${this.shortName};\n`; } #getFullType() { - return `${INTERNAL}Function<${this.#getSignature()}>`; + 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; } // Overwrite Helper @@ -53,9 +51,7 @@ export class Method { } generate(code: boolean, isVirtual: boolean, parentSelf?: string) { let out = ''; - if (!code) { - out += 'extern '; - } + out += 'extern '; const type = this.#getFullType(); out += `${type} *const ${this.getName()}`; if (code) { @@ -64,14 +60,11 @@ export class Method { const parentMethod = parentSelf ? `(void *) ${this.#getVirtualCall(parentSelf)}` : 'nullptr'; out += `&${this.#getVirtualCall()}, ${parentMethod}`; } else { - out += `${this.getWrapperType()}::ptr_type(${toHex(this.address)})`; + out += `(${this.#getRawType()} *) ${toHex(this.address)}`; } out += `, ${INTERNAL}thunk<&${this.getName()}>)`; } out += ';\n'; - if (!code) { - out += `typedef ${this.getWrapperType()}::type ${this.getType()};\n`; - } return out; } } \ No newline at end of file diff --git a/src/struct.ts b/src/struct.ts index c0f354f..484fcf9 100644 --- a/src/struct.ts +++ b/src/struct.ts @@ -1,4 +1,4 @@ -import { INDENT, STRUCTURE_FILES, toHex, assertSize, INTERNAL, preventConstruction } from './common'; +import { INDENT, STRUCTURE_FILES, toHex, assertSize, INTERNAL, preventConstruction, LEAN_HEADER_GUARD } from './common'; import { Method } from './method'; import { Property, StaticProperty } from './property'; import { VTable } from './vtable'; @@ -161,6 +161,7 @@ export class Struct { } #generateMethods() { let out = ''; + out += LEAN_HEADER_GUARD; // Normal Methods for (const method of this.#methods) { out += this.#generateMethod(method, false); @@ -175,6 +176,7 @@ export class Struct { } } // Return + out += '#endif\n'; return out; } @@ -198,11 +200,18 @@ export class Struct { } // Method Wrappers + let typedefs = ''; + let methodsOut = ''; for (const method of this.#methods) { if (!method.isInherited) { - out += method.generate(false, false); + typedefs += method.generateTypedefs(); + methodsOut += method.generate(false, false); } } + out += typedefs; + out += LEAN_HEADER_GUARD; + out += methodsOut; + out += '#endif\n'; // Structure out += `struct ${this.#name} {\n`; diff --git a/src/vtable.ts b/src/vtable.ts index 702b980..2c3ecfc 100644 --- a/src/vtable.ts +++ b/src/vtable.ts @@ -1,4 +1,4 @@ -import { INDENT, POINTER_SIZE, assertSize, getSelfArg, preventConstruction, toHex } from './common'; +import { INDENT, LEAN_HEADER_GUARD, POINTER_SIZE, assertSize, getSelfArg, preventConstruction, toHex } from './common'; import { Method } from './method'; import { Property } from './property'; @@ -116,13 +116,20 @@ export class VTable { // Wrappers const methods = this.getMethods(); - if (this.canGenerateWrappers()) { - for (const info of methods) { - if (info) { - out += info.generate(false, true); + let typedefs = ''; + let methodsOut = ''; + for (const info of methods) { + if (info) { + typedefs += info.generateTypedefs(); + if (this.canGenerateWrappers()) { + methodsOut += info.generate(false, true); } } } + out += typedefs; + out += LEAN_HEADER_GUARD; + out += methodsOut; + out += '#endif\n'; // Structure out += `typedef struct ${this.#getName()} ${this.#getName()};\n`; @@ -130,7 +137,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`; }