This commit is contained in:
TheBrokenRail 2024-01-05 15:47:53 -05:00
parent ddbbbcb8e3
commit 92bb2b8191
5 changed files with 1726 additions and 22 deletions

3
.gitignore vendored
View File

@ -1,5 +1,2 @@
/build
/package-lock.json
/node_modules
/out
/symbols

1684
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -10,13 +10,12 @@
"author": "TheBrokenRail",
"license": "MIT",
"devDependencies": {
"@tsconfig/node-lts": "^18.12.4",
"@tsconfig/strictest": "^2.0.1",
"@types/node": "^20.5.6",
"@typescript-eslint/eslint-plugin": "^6.4.1",
"@typescript-eslint/parser": "^6.4.1",
"eslint": "^8.48.0",
"openapi-types": "^12.1.3",
"typescript": "^5.2.2"
"@tsconfig/node-lts": "^18.12.5",
"@tsconfig/strictest": "^2.0.2",
"@types/node": "^20.10.6",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"eslint": "^8.56.0",
"typescript": "^5.3.3"
}
}

View File

@ -1,10 +1,11 @@
import * as fs from 'node:fs';
import { STRUCTURE_FILES, EXTENSION, INDENT } from './common';
import { getStructure, setCppAllowed } from './map';
import { Struct } from './struct';
// Arguments
if (process.argv.length < 5) {
console.log('USAGE: npm run <Source Output File> <Header Output File> <Input Files...>');
console.log('USAGE: npm start -- <Source Output File> <Header Output File> <Input Files...>');
process.exit(1);
}
process.argv.shift();
@ -60,16 +61,15 @@ function loadSymbols() {
return structureObjects;
}
// Generate Part Of Header
function makeHeaderPart(allowCpp: boolean) {
// Set Mode
setCppAllowed(allowCpp);
// Load Symbols
const structureObjects = loadSymbols();
// Sort Structures By Dependency
// Sort Structures By Dependency
function dependencySort(structureObjects: Struct[]) {
let loops = 0;
const MAX_LOOPS = 100;
while (true) {
if (loops > MAX_LOOPS) {
throw new Error('Unable To Sort Dependencies');
}
loops++;
let valid = true;
// Loop Through Structures
for (const structure of structureObjects) {
@ -97,6 +97,18 @@ function makeHeaderPart(allowCpp: boolean) {
break;
}
}
}
// Generate Part Of Header
function makeHeaderPart(allowCpp: boolean) {
// Set Mode
setCppAllowed(allowCpp);
// Load Symbols
const structureObjects = loadSymbols();
// Sort Structures By Dependency
dependencySort(structureObjects);
// Generate Code
let structures = '';
@ -125,6 +137,10 @@ function makeHeaderPart(allowCpp: boolean) {
function makeMainHeader(output: string) {
let result = '';
result += '#pragma once\n';
result += '\n// Check Architecture\n';
result += '#ifndef __arm__\n';
result += '#error "Symbols Are ARM-Only"\n';
result += '#endif\n';
result += '\n// Suppress Warnings\n';
result += '#pragma GCC diagnostic push\n';
result += '#pragma GCC diagnostic ignored "-Wunused-variable"\n';
@ -137,13 +153,15 @@ function makeMainHeader(output: string) {
result += '\n// Forward Declarations\n';
for (const name in STRUCTURE_FILES) {
result += `typedef struct ${name} ${name};\n`;
}result += '\n// Extra Headers\n';
}
result += '\n// Extra Headers\n';
for (const file of extraHeaders) {
result += fs.readFileSync(file, {encoding: 'utf8'});
}
result += '\n// Switch Mode\n';
result += '#ifndef __cplusplus\n';
result += '\n// C Mode\n';
result += '#include <stddef.h>\n';
result += 'typedef uchar bool;\n\n';
result += makeHeaderPart(false);
result += '\n// End C Mode\n';
@ -151,6 +169,7 @@ function makeMainHeader(output: string) {
result += '\n// C++ Mode\n';
result += '#include <string>\n';
result += '#include <vector>\n';
result += '#include <cstddef>\n';
result += 'extern "C" {\n\n';
result += makeHeaderPart(true);
result += '\n// End C++ Mode\n';

View File

@ -157,6 +157,11 @@ export class Struct {
out += `extern ${method.generateDefinition()}`;
}
// Early Exit For Undefined Structures
if (this.#properties.length === 0 && this.#size === null) {
return out;
}
// VTable
if (this.#vtable !== null) {
out += this.#vtable.generate();