2024-02-17 07:30:29 +01:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
exports.SaveProject = void 0;
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("node:path");
|
2024-02-17 14:32:19 +01:00
|
|
|
const LoadModel_1 = require("./LoadModel");
|
2024-02-17 08:45:22 +01:00
|
|
|
const ModelComponentType_1 = require("../src/app/game-model/ModelComponentType");
|
|
|
|
const LoadedProject_1 = require("./LoadedProject");
|
2024-02-17 14:32:19 +01:00
|
|
|
const RecursiveLoadModel_1 = require("./RecursiveLoadModel");
|
2024-02-17 07:30:29 +01:00
|
|
|
class SaveProject {
|
|
|
|
static saveProject(projectDir, storageModels) {
|
2024-02-17 08:45:22 +01:00
|
|
|
if (!fs.existsSync(projectDir)) {
|
|
|
|
fs.mkdirSync(projectDir, { recursive: true });
|
2024-02-17 07:30:29 +01:00
|
|
|
}
|
2024-02-17 10:13:32 +01:00
|
|
|
console.log(storageModels);
|
2024-02-17 07:30:29 +01:00
|
|
|
storageModels.forEach(storageModel => {
|
2024-02-17 10:13:32 +01:00
|
|
|
let modelDir = path.join(projectDir, storageModel.storageRootDir);
|
|
|
|
storageModel.storagePath.forEach(pathElement => modelDir = path.join(modelDir, pathElement));
|
2024-02-17 07:30:29 +01:00
|
|
|
if (!fs.existsSync(modelDir)) {
|
|
|
|
fs.mkdirSync(modelDir, { recursive: true });
|
|
|
|
}
|
2024-02-17 08:45:22 +01:00
|
|
|
const filePath = path.join(modelDir, storageModel.fileName + ".json");
|
2024-02-17 07:30:29 +01:00
|
|
|
fs.writeFile(filePath, storageModel.jsonString, 'utf-8', (err) => {
|
|
|
|
if (err) {
|
|
|
|
console.error('Error writing JSON to file:', err);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log('JSON file saved successfully:', filePath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2024-02-17 08:45:22 +01:00
|
|
|
static loadProject(projectDir) {
|
2024-02-17 14:32:19 +01:00
|
|
|
let loadedScriptAccounts = SaveProject.loadScriptAccounts(projectDir);
|
|
|
|
loadedScriptAccounts = loadedScriptAccounts.concat(SaveProject.loadGamesystems(projectDir));
|
2024-02-17 08:45:22 +01:00
|
|
|
return new LoadedProject_1.LoadedProject(path.basename(projectDir), loadedScriptAccounts);
|
|
|
|
}
|
|
|
|
static loadScriptAccounts(projectDir) {
|
|
|
|
const scriptAccountDir = path.join(projectDir, "script-accounts");
|
|
|
|
if (!fs.existsSync(scriptAccountDir)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const loadedScriptAccounts = [];
|
|
|
|
const scriptAccountFileNames = fs.readdirSync(scriptAccountDir);
|
|
|
|
scriptAccountFileNames.forEach(scriptAccountFileName => {
|
|
|
|
const scriptAccountFile = path.join(scriptAccountDir, scriptAccountFileName);
|
|
|
|
const scriptAccountData = fs.readFileSync(scriptAccountFile, 'utf-8');
|
|
|
|
loadedScriptAccounts.push({
|
|
|
|
modelType: ModelComponentType_1.ModelComponentType.SCRIPTACCOUNT,
|
|
|
|
jsonString: scriptAccountData
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return loadedScriptAccounts;
|
|
|
|
}
|
2024-02-17 14:32:19 +01:00
|
|
|
static loadGamesystems(projectDir) {
|
|
|
|
const gamesystemDir = path.join(projectDir, "gamesystems");
|
|
|
|
const loadedGamesystems = this.loadGamesystemsRecursively(gamesystemDir);
|
|
|
|
console.log("LoadedGamesystems: ", loadedGamesystems.length);
|
|
|
|
return loadedGamesystems;
|
|
|
|
}
|
|
|
|
static loadGamesystemsRecursively(gamesystemDir) {
|
|
|
|
let loadedGamesystems = [];
|
|
|
|
const gamesystemFileNames = fs.readdirSync(gamesystemDir);
|
|
|
|
gamesystemFileNames.forEach(fileName => {
|
|
|
|
const gamesystemPath = path.join(gamesystemDir, fileName);
|
|
|
|
if (fs.lstatSync(gamesystemPath).isDirectory()) {
|
|
|
|
const childModels = SaveProject.loadGamesystemsRecursively(gamesystemPath);
|
|
|
|
loadedGamesystems = loadedGamesystems.concat(childModels);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const gamesystemData = fs.readFileSync(path.join(gamesystemDir, fileName), "utf-8");
|
|
|
|
if (path.parse(fileName).name === path.basename(gamesystemDir)) {
|
|
|
|
if ((path.basename(gamesystemDir) === path.parse(fileName).name) && path.basename(path.parse(gamesystemDir).dir) === "gamesystems") {
|
|
|
|
const loadedModel = new LoadModel_1.LoadModel(gamesystemData, ModelComponentType_1.ModelComponentType.GAMESYTEM);
|
|
|
|
loadedGamesystems.unshift(loadedModel);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const loadedModel = new RecursiveLoadModel_1.RecursiveLoadModel(gamesystemData, ModelComponentType_1.ModelComponentType.GAMESYTEM, path.basename(gamesystemDir));
|
|
|
|
loadedGamesystems.unshift(loadedModel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const secondCon = path.basename(gamesystemDir) === path.parse(fileName).name;
|
|
|
|
const thirdCon = path.basename(path.parse(gamesystemDir).dir) === "gamesystems";
|
|
|
|
if (path.basename(gamesystemDir) === "gamesystems") {
|
|
|
|
const loadedModel = new LoadModel_1.LoadModel(gamesystemData, ModelComponentType_1.ModelComponentType.GAMESYTEM);
|
|
|
|
loadedGamesystems.push(loadedModel);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const loadedModel = new RecursiveLoadModel_1.RecursiveLoadModel(gamesystemData, ModelComponentType_1.ModelComponentType.GAMESYTEM, path.basename(gamesystemDir));
|
|
|
|
loadedGamesystems.push(loadedModel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return loadedGamesystems;
|
|
|
|
}
|
2024-02-17 07:30:29 +01:00
|
|
|
}
|
|
|
|
exports.SaveProject = SaveProject;
|
|
|
|
//# sourceMappingURL=SaveProject.js.map
|