Add launchable
This commit is contained in:
56
js/serverParams.js
Normal file
56
js/serverParams.js
Normal file
@@ -0,0 +1,56 @@
|
||||
import { ServiceService } from "./instances/ServiceService.js";
|
||||
|
||||
import fs from 'fs';
|
||||
import { program } from "commander";
|
||||
import { sha256, encoders } from "./core/util.js";
|
||||
import { exit } from "process";
|
||||
|
||||
program
|
||||
.option("-p, --port <number>", "Port to listen on", "8080")
|
||||
.option("-a, --place <url>", "Place JSON file", "https://example.com/asset.php?id=1818");
|
||||
program.parse();
|
||||
|
||||
const options = program.opts();
|
||||
options.port = parseInt(options.port, 10); // optional, if you need a number
|
||||
const Services = new ServiceService(options.port);
|
||||
Services.initAll();
|
||||
|
||||
globalThis.Services = Services; // optional global shortcut
|
||||
|
||||
|
||||
// Load from place.json
|
||||
|
||||
// import FS
|
||||
|
||||
(async () => {
|
||||
const placeReq = await fetch(options.place);
|
||||
if (!placeData.ok) {
|
||||
console.error("Failed to fetch place data!");
|
||||
exit(1);
|
||||
}
|
||||
const placeData = await placeReq.json();
|
||||
// Create instances off this
|
||||
for (let obj of placeData.objects) {
|
||||
const parent = Services.GetDataModel().FindInstanceRecursively(obj.ParentId)
|
||||
if (parent) {
|
||||
import(`./instances/${obj.ClassName}.js`).then((module) => {
|
||||
const NewClass = module[obj.ClassName];
|
||||
if (!NewClass) {
|
||||
console.warn(`Init: Class ${obj.ClassName} not found for startup. Is this server up to date?`);
|
||||
return;
|
||||
}
|
||||
const newInst = new NewClass();
|
||||
newInst.InstanceId = obj.InstanceId; // Set the same InstanceId
|
||||
newInst.Name = obj.Name;
|
||||
for (let prop in obj.properties) {
|
||||
const type = prop._type
|
||||
const value = prop.value
|
||||
if (encoders[type]) obj.value = encoders[type].decode(value);
|
||||
else obj.value = value;
|
||||
};
|
||||
newInst.SetParent(parent)
|
||||
});
|
||||
}
|
||||
}
|
||||
console.log("Server initialized and listening on port " + options.port.toString() + "!");
|
||||
})();
|
||||
Reference in New Issue
Block a user