Files
cell/scripts/cmd.js

421 lines
9.0 KiB
JavaScript

var io = use('io')
var util = use('util')
var loop = use('loop')
var dumpfolder = ".prosperon";
var Cmdline = {};
Cmdline.cmds = [];
Cmdline.orders = {};
Cmdline.register_cmd = function (flag, fn, doc) {
Cmdline.cmds.push({
flag: flag,
fn: fn,
doc: doc,
});
};
Cmdline.register_order = function (order, fn, doc, usage = "") {
Cmdline.orders[order] = fn;
fn.doc = doc;
fn.usage = `${order} ${usage}`;
};
Cmdline.register_order(
"edit",
function () {
if (!io.exists(projectfile)) {
console.print("No game to edit. Try making one with 'prosperon init'.");
return;
}
},
"Edit the project in this folder. Give it the name of an UR to edit that specific object.",
"?UR?",
);
Cmdline.register_order(
"init",
function () {
if (io.exists(projectfile)) {
console.print("Already a game here.");
return;
}
io.mkdir(dumpfolder);
var project = {};
project.version = prosperon.version;
project.revision = prosperon.revision;
io.slurpwrite(projectfile, json.encode(project));
},
"Turn the directory into a Prosperon game.",
);
Cmdline.register_order(
"debug",
function () {
Cmdline.orders.play([]);
},
"Play the game with debugging enabled.",
);
Cmdline.register_order(
"web",
function () {
Cmdline.orders.play([]);
},
"Play the game in a web browser.",
);
Cmdline.register_order(
"play",
function (argv) {
var app
if (io.exists("main.js"))
app = actor.spawn("main.js", {}, function(underling, msg) {
if (msg.message !== "created") return;
Object.defineProperty(underling, 'then', {
configurable:false,
writable:false,
value:function() {
os.exit(0);
}
});
})
else
app = actor.spawn("nogame.js");
var ren = use('render')
while(1) loop.step();
},
"Play the game present in this folder.",
);
Cmdline.register_order(
"pack",
function (str) {
var packname;
if (str.length === 0) packname = "game.zip";
else if (str.length > 1) {
console.warn("Give me a single filename for the pack.");
return;
} else packname = str[0];
console.print(`Packing into ${packname}`);
io.pack_start(packname);
files = allfiles.filter(f => !f.startsWith(".git"));
files = files.filter(f => !f.startsWith(".nova"));
files = files.filter(f => !f.includes(".DS_Store"));
files = files.filter(f => !f.startsWith(".gitignore"));
console.print(files);
for (var f of files) io.pack_add(f);
io.pack_end();
},
"Pack the game into the given name.",
"NAME",
);
Cmdline.register_order(
"cdb",
function (argv) {
var cdb = "game.zip";
if (!io.exists(cdb)) {
console.print(`No 'game.zip' present.`);
return;
}
if (argv.length === 0) {
console.print(`cdb name: ${cdb}`);
}
},
"CDB commands.",
);
Cmdline.register_order(
"qoa",
function (argv) {
var sounds = Resources.sounds.filter(x => x !== "qoa");
for (var file of argv) {
if (!sounds.includes(file.ext())) continue;
console.print(`converting ${file}`);
io.save_qoa(file);
}
},
"Convert file(s) to qoa.",
);
Cmdline.register_order(
"about",
function (argv) {
if (!argv[0]) {
console.print("About your game");
console.print(`Prosperon version ${prosperon.version}`);
console.print(`Total entities ${ur._list.length}`);
}
switch (argv[0]) {
case "entities":
for (var i of ur._list) console.print(i);
break;
}
},
"Get information about this game.",
);
Cmdline.register_order(
"ur",
function (argv) {
// game.loadurs();
for (var i of ur._list.sort()) console.print(i);
},
"Get information about the ur types in your game.",
);
Cmdline.register_order(
"env",
function (argv) {
if (argv.length > 2) return;
var gg = json.decode(io.slurp(projectfile));
if (argv.length === 0) {
console.print(json.encode(gg, null, 1));
return;
}
if (argv.length === 1) {
var v = gg[argv[0]];
if (!v) {
console.print(`Value ${argv[0]} not found.`);
return;
}
console.print(`${argv[0]}:${v}`);
} else {
gg[argv[0]] = argv[1];
console.print(`Set ${argv[0]}:${v}`);
console.print(json.encode(gg, null, 1));
io.slurpwrite(projectfile, json.encode(gg));
}
},
"Get or set game variables.",
);
Cmdline.register_order(
"unpack",
function () {
console.print("Unpacking not implemented.");
},
"Unpack this binary's contents into this folder for editing.",
);
Cmdline.register_order(
"build",
function () {
console.print("Building not implemented.");
},
"Build static assets for this project.",
);
Cmdline.register_order(
"nota",
function (argv) {
for (var file of argv) {
if (!io.exists(file)) {
console.print(`File ${file} does not exist.`);
continue;
}
var obj = json.decode(io.slurp(file));
var nn = nota.encode(obj);
io.slurpwrite(file.strip_ext() + ".nota", nn);
}
},
"Create a nota file from a json.",
);
Cmdline.register_order(
"json",
function (argv) {
for (var file of argv) {
if (!io.exists(file)) {
console.print(`File ${file} does not exist.`);
continue;
}
console.print(file.ext());
var obj = nota.decode(io.slurp(file));
var nn = json.encode(obj);
io.slurpwrite(file.strip_ext() + ".json", nn);
}
},
"Create a JSON from a nota.",
);
Cmdline.register_order(
"api",
function (obj) {
if (!obj[0]) {
Cmdline.print_order("api");
return;
}
use("editor.js");
var api = debug.api.print_doc(obj[0]);
if (!api) return;
console.print(api);
},
"Print the API for an object as markdown. Give it a file to save the output to.",
"OBJECT",
);
Cmdline.register_order(
"input",
function (pawn) {
use("editor.js");
console.print(`## Input for ${pawn}`);
eval(`console.print(input.print_md_kbm(${pawn}));`);
},
"Print input documentation for a given object as markdown. Give it a file to save the output to",
"OBJECT ?FILE?",
);
Cmdline.register_order(
"run",
function (script) {
var s = os.now()
script = script.join(" ");
if (!script) {
console.print("Need something to run.");
return;
}
console.print(eval(script));
},
"Run a given script. SCRIPT can be the script itself, or a file containing the script",
"SCRIPT",
);
Cmdline.orders.script = Cmdline.orders.run;
Cmdline.print_order = function (fn) {
if (typeof fn === "string") fn = Cmdline.orders[fn];
if (!fn) return;
console.print(`Usage: prosperon ${fn.usage}`);
console.print(fn.doc);
};
Cmdline.register_order(
"help",
function (order) {
if (!util.isEmpty(order)) {
var orfn = Cmdline.orders[order];
if (!orfn) {
console.warn(`No command named ${order}.`);
return;
}
Cmdline.print_order(orfn);
return;
}
Cmdline.print_order("help");
for (var cmd of Object.keys(Cmdline.orders).sort()) console.print(cmd);
Cmdline.orders.version();
},
"Give help with a specific command.",
"TOPIC",
);
Cmdline.register_order(
"version",
function () {
console.print(`Prosperon version ${prosperon.version} [${prosperon.revision}]`);
},
"Display Prosperon info.",
);
function cmd_args(cmds) {
var play = false;
if (cmds.length === 0) cmds[0] = "play";
else if (!Cmdline.orders[cmds[0]]) {
console.warn(`Command ${cmds[0]} not found. Playing instead.`);
cmds[0] = "play";
}
Cmdline.orders[cmds[0]](cmds.slice(1));
}
Cmdline.register_order(
"clean",
function (argv) {
console.print("Cleaning not implemented.");
},
"Clean up a given object file.",
"JSON ...",
);
Cmdline.register_order(
"test",
function (argv) {
use("test.js");
},
"Run tests.",
);
Cmdline.register_cmd(
"l",
function (n) {
console.level = n;
},
"Set log level.",
);
function convertYAMLtoJSON(yamlString) {
const lines = yamlString.split("\n");
const jsonObj = {};
let currentKey = "";
let currentValue = "";
let currentDepth = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (!line || line.startsWith("#")) {
continue;
}
const depth = (line.match(/^\s+/g) || [""])[0].length;
const keyValue = line.split(":");
const key = keyValue[0].trim();
const value = keyValue[1].trim();
if (depth > currentDepth) {
jsonObj[currentKey] = convertYAMLtoJSON(currentValue);
currentKey = key;
currentValue = value;
} else if (depth === currentDepth) {
jsonObj[currentKey] = convertYAMLtoJSON(currentValue);
currentKey = key;
currentValue = value;
} else {
jsonObj[currentKey] = convertYAMLtoJSON(currentValue);
currentKey = "";
currentValue = "";
i--; // To reprocess the current line with updated values
}
currentDepth = depth;
}
if (currentKey) {
jsonObj[currentKey] = convertYAMLtoJSON(currentValue);
}
return jsonObj;
}
return cmd_args;