rokojori-godot/rokojori-cpp-generator/builds/cpp-creation/CppCreator.js

149 lines
7.8 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CppCreator = void 0;
const Header_1 = require("./Header");
const Implementation_1 = require("./Implementation");
const Files_1 = require("../library/Files");
const Texts_1 = require("../library/Texts");
const MemberType_1 = require("./MemberType");
const CppDefinition_1 = require("./CppDefinition");
const Variations_1 = require("./Variations");
const VariationTemplates_1 = require("./VariationTemplates");
const MemberFactory_1 = require("./factories/MemberFactory");
class CppCreator {
static createIncludes(className) {
// RJLog.log( "Create include", className );
let godotClasses = {
"Node": "scene/main/node.h",
"Node2D": "scene/2d/node_2d.h",
"Node3D": "scene/3d/node_3d.h",
"Resource": "core/io/resource.h"
};
if (godotClasses[className]) {
return `#include "${godotClasses[className]}"`;
}
return `#include "./${className}.h"`;
}
static getHeaderDefine(className) {
className = className.replace(/^RJ/, "");
let output = [className[0]];
for (let i = 1; i < className.length; i++) {
let character = className[i];
let upperCaseCharacter = character.toUpperCase();
if (character === upperCaseCharacter) {
output.push("_");
}
output.push(upperCaseCharacter);
}
className = output.join("");
return "ROKOJORI__" + className + "_H";
}
static createCppFiles(definitionPath) {
return __awaiter(this, void 0, void 0, function* () {
let jsonPath = definitionPath;
// path.join( definitionsPath, definitionPath );
let isDir = yield Files_1.Files.isDirectory(jsonPath);
if (isDir) {
let files = yield Files_1.Files.getFiles(jsonPath);
for (let file of files) {
if (file.startsWith("_")) {
continue;
}
yield CppCreator.createCppFiles(Texts_1.Texts.joinPaths(jsonPath, file));
}
return Promise.resolve();
}
return this.createCppFile(jsonPath);
});
}
static createCppFile(jsonPath) {
return __awaiter(this, void 0, void 0, function* () {
let data = yield Files_1.Files.loadJSON(jsonPath);
if (data.variations) {
let variationsData = data.variations;
if (typeof variationsData == "string") {
variationsData = VariationTemplates_1.VariationTemplates[variationsData];
}
let variations = new Variations_1.Variations(variationsData);
console.log(variations.items, variations.counter, variations.hasItems);
while (variations.hasItems) {
let newData = CppDefinition_1.CppDefinition.createVariation(data, variations.currentMap);
yield this.createCppFileWith(newData);
variations.nextItem();
}
}
else {
yield CppCreator.createCppFileWith(data);
}
return Promise.resolve();
});
}
static createCppFileWith(data) {
return __awaiter(this, void 0, void 0, function* () {
let classNameResult = /(\w+)(?:\:(\w+))?/.exec(data.class);
let className = classNameResult[1];
let extendingClass = classNameResult[2];
let headerDefine = CppCreator.getHeaderDefine(className);
let fromProtectedMembers = MemberFactory_1.MemberFactory.grabMembers(data.protected, "protected");
let fromPublicMembers = MemberFactory_1.MemberFactory.grabMembers(data.public, "public");
let protectedMembers = [];
let publicMembers = [];
let insertMember = (m) => {
let container = m.accessModifier === "public" ? publicMembers : protectedMembers;
container.push(m);
};
fromProtectedMembers.forEach(m => insertMember(m));
fromPublicMembers.forEach(m => insertMember(m));
let allMembers = [].concat(protectedMembers).concat(publicMembers);
let protectedHeader = protectedMembers.map(m => m.getHeaderDefinition()).join("\n ");
let publicHeader = publicMembers.map(m => m.getHeaderDefinition()).join("\n ");
let fields = allMembers.filter(m => m.memberType === MemberType_1.MemberType.Field);
let methods = allMembers.filter(m => m.memberType === MemberType_1.MemberType.Method);
let fieldDeclarations = fields.map(f => f.getFieldDeclaration()).join("\n ");
protectedHeader += "\n\n " + fieldDeclarations;
let methodBindings = allMembers.map(m => m.getBindings(className)).join("\n ");
let initializers = fields.filter(m => m.hasInitializer).map(m => m.getInitializer()).join("\n ");
let includes = `#include "./RJGodotHeaders.h"`;
if (data.includes) {
let mappedIncludes = data.includes.map((inc) => `#include "${inc}"`);
includes += "\n" + mappedIncludes.join("\n");
}
let extendingClassInclude = CppCreator.createIncludes(extendingClass);
includes += "\n" + extendingClassInclude;
let forwards = "";
if (data.forwards) {
let forwardDeclarations = data.forwards;
forwards = forwardDeclarations.join("\n");
}
let header = Header_1.Header.create(className, extendingClass, headerDefine, protectedHeader, publicHeader, includes, forwards);
let constructorExpressions = initializers;
let destructorExpressions = "";
let implementations = "";
let fieldImplementations = fields.map(f => f.getFieldImplementation(className));
implementations += fieldImplementations.join("\n") + "\n";
let methodsWithImplementation = methods.filter(m => m.hasMethodImplementation);
let methodImplementations = methodsWithImplementation.map(m => m.getMethodImplementation(className));
implementations += methodImplementations.join("\n");
let implementation = Implementation_1.Implementation.craete(className, methodBindings, constructorExpressions, destructorExpressions, implementations);
let rawFilePath = Texts_1.Texts.joinPaths(CppCreator.outputPath, className);
yield Files_1.Files.saveUTF8(rawFilePath + ".h", header);
yield Files_1.Files.saveUTF8(rawFilePath + ".cpp", implementation);
});
}
}
exports.CppCreator = CppCreator;
CppCreator.definitionsPath = __dirname + "/../../../rokojori-action-library-definitions";
CppCreator.outputPath = __dirname + "/../../../rokojori-action-library";
CppCreator.rawPath = CppCreator.definitionsPath + "/_raw";
CppCreator.registerTypesPath = CppCreator.rawPath + "/register_types.cpp";
CppCreator.registerTypesPathOutput = CppCreator.outputPath + "/register_types.cpp";
//# sourceMappingURL=CppCreator.js.map