2023-12-11 16:59:59 -06:00
|
|
|
"use math";
|
2024-03-14 14:10:06 -05:00
|
|
|
|
2024-08-06 14:23:21 -05:00
|
|
|
os.mem_limit.doc = "Set the memory limit of the runtime in bytes.";
|
|
|
|
os.gc_threshold.doc = "Set the threshold before a GC pass is triggered in bytes. This is set to malloc_size + malloc_size>>1 after a GC pass.";
|
|
|
|
os.max_stacksize.doc = "Set the max stack size in bytes.";
|
2024-08-05 15:26:18 -05:00
|
|
|
|
2024-07-25 17:53:53 -05:00
|
|
|
Object.defineProperty(String.prototype, 'rm', {
|
|
|
|
value: function(index, endidx = index+1) { return this.slice(0,index) + this.slice(endidx); }
|
|
|
|
});
|
|
|
|
|
2024-06-18 16:14:23 -05:00
|
|
|
Object.defineProperty(String.prototype, "tolast", {
|
|
|
|
value: function (val) {
|
2024-04-01 17:58:29 -05:00
|
|
|
var idx = this.lastIndexOf(val);
|
|
|
|
if (idx === -1) return this.slice();
|
2024-06-18 16:14:23 -05:00
|
|
|
return this.slice(0, idx);
|
|
|
|
},
|
2024-04-01 17:58:29 -05:00
|
|
|
});
|
|
|
|
|
2024-06-18 16:14:23 -05:00
|
|
|
Object.defineProperty(String.prototype, "dir", {
|
|
|
|
value: function () {
|
|
|
|
if (!this.includes("/")) return "";
|
|
|
|
return this.tolast("/");
|
|
|
|
},
|
2024-04-01 17:58:29 -05:00
|
|
|
});
|
|
|
|
|
2024-06-18 16:14:23 -05:00
|
|
|
Object.defineProperty(String.prototype, "folder", {
|
|
|
|
value: function () {
|
2024-04-16 12:36:26 -05:00
|
|
|
var dir = this.dir();
|
|
|
|
if (!dir) return "";
|
|
|
|
else return dir + "/";
|
2024-06-18 16:14:23 -05:00
|
|
|
},
|
2024-04-16 12:36:26 -05:00
|
|
|
});
|
|
|
|
|
2024-04-01 17:58:29 -05:00
|
|
|
globalThis.Resources = {};
|
|
|
|
|
2024-08-02 20:52:50 -05:00
|
|
|
Resources.rm_fn = function rm_fn(fn, text)
|
2024-07-25 16:14:37 -05:00
|
|
|
{
|
2024-07-25 17:53:53 -05:00
|
|
|
var reg = new RegExp(fn.source + "\\s*\\(");
|
|
|
|
var match;
|
|
|
|
while (match = text.match(reg)) {
|
|
|
|
var last = match.index+match[0].length;
|
|
|
|
var par = 1;
|
|
|
|
while (par !== 0) {
|
|
|
|
if (text[last] === '(') par++;
|
|
|
|
if (text[last] === ')') par--;
|
|
|
|
last++;
|
|
|
|
}
|
|
|
|
text = text.rm(match.index, last);
|
|
|
|
}
|
2024-07-25 16:14:37 -05:00
|
|
|
|
2024-07-25 17:53:53 -05:00
|
|
|
return text;
|
|
|
|
}
|
2024-07-25 16:14:37 -05:00
|
|
|
Resources.rm_fn.doc = "Remove calls to a given function from a given text script.";
|
|
|
|
|
2024-08-02 20:52:50 -05:00
|
|
|
Resources.replpath = function replpath(str, path) {
|
2024-04-01 17:58:29 -05:00
|
|
|
if (!str) return str;
|
2024-06-18 16:14:23 -05:00
|
|
|
if (str[0] === "/") return str.rm(0);
|
2024-04-01 17:58:29 -05:00
|
|
|
|
2024-06-18 16:14:23 -05:00
|
|
|
if (str[0] === "@") return os.prefpath() + "/" + str.rm(0);
|
2024-04-01 17:58:29 -05:00
|
|
|
|
|
|
|
if (!path) return str;
|
2024-06-18 16:14:23 -05:00
|
|
|
|
2024-04-01 17:58:29 -05:00
|
|
|
var stem = path.dir();
|
|
|
|
while (stem) {
|
2024-06-18 16:14:23 -05:00
|
|
|
var tr = stem + "/" + str;
|
2024-04-01 17:58:29 -05:00
|
|
|
if (io.exists(tr)) return tr;
|
|
|
|
stem = stem.updir();
|
|
|
|
}
|
2024-06-18 16:14:23 -05:00
|
|
|
|
2024-04-01 17:58:29 -05:00
|
|
|
return str;
|
2024-06-18 16:14:23 -05:00
|
|
|
};
|
2024-04-01 17:58:29 -05:00
|
|
|
|
2024-08-02 20:52:50 -05:00
|
|
|
Resources.replstrs = function replstrs(path) {
|
2024-04-01 17:58:29 -05:00
|
|
|
if (!path) return;
|
|
|
|
var script = io.slurp(path);
|
|
|
|
var regexp = /"[^"\s]*?\.[^"\s]+?"/g;
|
|
|
|
var stem = path.dir();
|
2024-07-25 16:14:37 -05:00
|
|
|
|
|
|
|
// remove console statements
|
2024-07-25 22:07:22 -05:00
|
|
|
if (!console.enabled)
|
|
|
|
script = Resources.rm_fn(/console\.(spam|info|warn|error)/, script);
|
|
|
|
|
|
|
|
if (!profile.enabled)
|
|
|
|
script = Resources.rm_fn(/profile\.(cache|frame|endcache|endframe)/, script);
|
|
|
|
|
|
|
|
if (!debug.enabled) {
|
|
|
|
script = Resources.rm_fn(/assert/, script);
|
|
|
|
script = Resources.rm_fn(/debug\.(build|fn_break)/, script);
|
|
|
|
}
|
|
|
|
|
2024-06-18 16:14:23 -05:00
|
|
|
script = script.replace(regexp, function (str) {
|
2024-04-01 17:58:29 -05:00
|
|
|
var newstr = Resources.replpath(str.trimchr('"'), path);
|
|
|
|
return `"${newstr}"`;
|
|
|
|
});
|
|
|
|
|
|
|
|
return script;
|
2024-06-18 16:14:23 -05:00
|
|
|
};
|
2024-04-01 17:58:29 -05:00
|
|
|
|
2024-03-26 07:53:36 -05:00
|
|
|
globalThis.json = {};
|
2024-06-18 16:14:23 -05:00
|
|
|
json.encode = function (value, replacer, space = 1) {
|
2024-03-26 07:53:36 -05:00
|
|
|
return JSON.stringify(value, replacer, space);
|
2024-06-18 16:14:23 -05:00
|
|
|
};
|
2024-03-26 07:53:36 -05:00
|
|
|
|
2024-06-18 16:14:23 -05:00
|
|
|
json.decode = function (text, reviver) {
|
2024-03-26 07:53:36 -05:00
|
|
|
if (!text) return undefined;
|
2024-06-18 16:14:23 -05:00
|
|
|
return JSON.parse(text, reviver);
|
|
|
|
};
|
2024-03-26 07:53:36 -05:00
|
|
|
|
2024-06-18 16:14:23 -05:00
|
|
|
json.readout = function (obj) {
|
2024-03-26 07:53:36 -05:00
|
|
|
var j = {};
|
|
|
|
for (var k in obj)
|
2024-06-18 16:14:23 -05:00
|
|
|
if (typeof obj[k] === "function") j[k] = "function " + obj[k].toString();
|
|
|
|
else j[k] = obj[k];
|
2024-03-26 07:53:36 -05:00
|
|
|
|
|
|
|
return json.encode(j);
|
2024-06-18 16:14:23 -05:00
|
|
|
};
|
2024-03-26 07:53:36 -05:00
|
|
|
|
|
|
|
json.doc = {
|
|
|
|
doc: "json implementation.",
|
|
|
|
encode: "Encode a value to json.",
|
|
|
|
decode: "Decode a json string to a value.",
|
2024-06-18 16:14:23 -05:00
|
|
|
readout: "Encode an object fully, including function definitions.",
|
2024-03-26 07:53:36 -05:00
|
|
|
};
|
|
|
|
|
2024-03-21 11:33:36 -05:00
|
|
|
Resources.scripts = ["jsoc", "jsc", "jso", "js"];
|
|
|
|
Resources.images = ["png", "gif", "jpg", "jpeg"];
|
2024-06-18 16:14:23 -05:00
|
|
|
Resources.sounds = ["wav", "flac", "mp3", "qoa"];
|
|
|
|
Resources.is_image = function (path) {
|
2024-03-21 11:33:36 -05:00
|
|
|
var ext = path.ext();
|
2024-06-18 16:14:23 -05:00
|
|
|
return Resources.images.any((x) => x === ext);
|
|
|
|
};
|
2024-03-21 11:33:36 -05:00
|
|
|
|
2024-06-18 16:14:23 -05:00
|
|
|
function find_ext(file, ext) {
|
2024-03-21 11:33:36 -05:00
|
|
|
if (io.exists(file)) return file;
|
|
|
|
for (var e of ext) {
|
|
|
|
var nf = `${file}.${e}`;
|
|
|
|
if (io.exists(nf)) return nf;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-18 16:14:23 -05:00
|
|
|
Resources.find_image = function (file) {
|
|
|
|
return find_ext(file, Resources.images);
|
|
|
|
};
|
|
|
|
Resources.find_sound = function (file) {
|
|
|
|
return find_ext(file, Resources.sounds);
|
|
|
|
};
|
|
|
|
Resources.find_script = function (file) {
|
|
|
|
return find_ext(file, Resources.scripts);
|
|
|
|
};
|
2024-03-21 11:33:36 -05:00
|
|
|
|
2024-04-03 17:17:32 -05:00
|
|
|
console.transcript = "";
|
2024-06-18 16:14:23 -05:00
|
|
|
console.say = function (msg) {
|
|
|
|
msg += "\n";
|
2024-04-03 17:17:32 -05:00
|
|
|
console.print(msg);
|
|
|
|
console.transcript += msg;
|
|
|
|
};
|
|
|
|
console.log = console.say;
|
2024-04-15 07:58:23 -05:00
|
|
|
globalThis.say = console.say;
|
|
|
|
globalThis.print = console.print;
|
2024-04-03 17:17:32 -05:00
|
|
|
|
2024-06-18 16:14:23 -05:00
|
|
|
console.pprint = function (msg, lvl = 0) {
|
|
|
|
if (typeof msg === "object") msg = JSON.stringify(msg, null, 2);
|
2024-04-03 17:17:32 -05:00
|
|
|
|
|
|
|
var file = "nofile";
|
|
|
|
var line = 0;
|
2024-06-18 16:14:23 -05:00
|
|
|
console.rec(0, msg, file, line);
|
|
|
|
|
|
|
|
var caller = new Error().stack.split("\n")[2];
|
|
|
|
if (caller) {
|
2024-04-03 17:17:32 -05:00
|
|
|
var md = caller.match(/\((.*)\:/);
|
|
|
|
var m = md ? md[1] : "SCRIPT";
|
|
|
|
if (m) file = m;
|
|
|
|
md = caller.match(/\:(\d*)\)/);
|
|
|
|
m = md ? md[1] : 0;
|
|
|
|
if (m) line = m;
|
|
|
|
}
|
2024-06-18 16:14:23 -05:00
|
|
|
|
2024-04-03 17:17:32 -05:00
|
|
|
console.rec(lvl, msg, file, line);
|
|
|
|
};
|
2024-03-14 14:10:06 -05:00
|
|
|
|
2024-06-18 16:14:23 -05:00
|
|
|
console.spam = function (msg) {
|
|
|
|
console.pprint(msg, 0);
|
|
|
|
};
|
|
|
|
console.debug = function (msg) {
|
|
|
|
console.pprint(msg, 1);
|
|
|
|
};
|
|
|
|
console.info = function (msg) {
|
|
|
|
console.pprint(msg, 2);
|
|
|
|
};
|
|
|
|
console.warn = function (msg) {
|
|
|
|
console.pprint(msg, 3);
|
|
|
|
};
|
|
|
|
console.error = function (msg) {
|
|
|
|
console.pprint(msg + "\n" + console.stackstr(2), 4);
|
|
|
|
};
|
|
|
|
console.panic = function (msg) {
|
|
|
|
console.pprint(msg + "\n" + console.stackstr(2), 5);
|
|
|
|
};
|
|
|
|
console.stackstr = function (skip = 0) {
|
2024-04-03 17:17:32 -05:00
|
|
|
var err = new Error();
|
2024-06-18 16:14:23 -05:00
|
|
|
var stack = err.stack.split("\n");
|
|
|
|
return stack.slice(skip, stack.length).join("\n");
|
2024-04-03 17:17:32 -05:00
|
|
|
};
|
|
|
|
|
2024-06-18 16:14:23 -05:00
|
|
|
console.stack = function (skip = 0) {
|
|
|
|
console.log(console.stackstr(skip + 1));
|
|
|
|
};
|
2024-03-14 14:10:06 -05:00
|
|
|
|
2024-03-23 09:56:38 -05:00
|
|
|
console.stdout_lvl = 1;
|
2024-03-26 07:53:36 -05:00
|
|
|
console.trace = console.stack;
|
2024-03-14 14:10:06 -05:00
|
|
|
|
|
|
|
console.doc = {
|
|
|
|
level: "Set level to output logging to console.",
|
|
|
|
info: "Output info level message.",
|
|
|
|
warn: "Output warn level message.",
|
|
|
|
error: "Output error level message, and print stacktrace.",
|
|
|
|
critical: "Output critical level message, and exit game immediately.",
|
|
|
|
write: "Write raw text to console.",
|
|
|
|
say: "Write raw text to console, plus a newline.",
|
|
|
|
stack: "Output a stacktrace to console.",
|
|
|
|
console: "Output directly to in game console.",
|
2024-06-18 16:14:23 -05:00
|
|
|
clear: "Clear console.",
|
2024-03-14 14:10:06 -05:00
|
|
|
};
|
|
|
|
|
2024-02-25 17:31:48 -06:00
|
|
|
globalThis.global = globalThis;
|
2023-05-24 20:45:50 -05:00
|
|
|
|
2024-07-25 17:53:53 -05:00
|
|
|
var use_cache = {};
|
2024-07-24 14:17:32 -05:00
|
|
|
|
2024-08-05 15:26:18 -05:00
|
|
|
globalThis.use = function use(file) {
|
2024-03-21 11:33:36 -05:00
|
|
|
file = Resources.find_script(file);
|
2024-07-25 17:53:53 -05:00
|
|
|
profile.cache("USE", file);
|
2024-06-18 16:14:23 -05:00
|
|
|
|
2024-07-25 17:53:53 -05:00
|
|
|
if (use_cache[file]) {
|
2024-08-05 15:26:18 -05:00
|
|
|
var ret = use_cache[file]();
|
|
|
|
profile.endcache(" [cached]");
|
|
|
|
return ret;
|
2024-03-21 11:33:36 -05:00
|
|
|
}
|
2024-08-05 15:26:18 -05:00
|
|
|
|
|
|
|
var script = Resources.replstrs(file);
|
2024-04-01 17:58:29 -05:00
|
|
|
script = `(function() { var self = this; ${script}; })`;
|
2024-06-18 16:14:23 -05:00
|
|
|
var fn = os.eval(file, script);
|
2024-07-25 17:53:53 -05:00
|
|
|
use_cache[file] = fn;
|
2024-08-05 15:26:18 -05:00
|
|
|
var ret = fn();
|
2024-07-25 17:53:53 -05:00
|
|
|
profile.endcache();
|
2024-08-05 15:26:18 -05:00
|
|
|
|
2024-03-21 11:33:36 -05:00
|
|
|
return ret;
|
2024-02-29 13:54:33 -06:00
|
|
|
}
|
|
|
|
|
2024-08-05 15:26:18 -05:00
|
|
|
function stripped_use (file, script) {
|
2024-07-25 17:53:53 -05:00
|
|
|
file = Resources.find_script(file);
|
2024-07-11 16:37:24 -05:00
|
|
|
|
2024-07-25 17:53:53 -05:00
|
|
|
if (use_cache[file]) {
|
2024-08-05 15:26:18 -05:00
|
|
|
var ret = use_cache[file]();
|
|
|
|
return ret;
|
2024-07-23 17:21:27 -05:00
|
|
|
}
|
2024-07-25 17:53:53 -05:00
|
|
|
script ??= Resources.replstrs(file);
|
2024-07-23 17:21:27 -05:00
|
|
|
|
2024-07-25 17:53:53 -05:00
|
|
|
script = `(function() { var self = this; ${script}; })`;
|
|
|
|
var fn = os.eval(file, script);
|
2024-08-05 15:26:18 -05:00
|
|
|
var ret = fn();
|
2024-07-25 17:53:53 -05:00
|
|
|
profile.endcache();
|
2024-08-02 20:52:50 -05:00
|
|
|
|
2024-07-25 17:53:53 -05:00
|
|
|
return ret;
|
2024-07-23 17:21:27 -05:00
|
|
|
}
|
|
|
|
|
2024-07-25 17:53:53 -05:00
|
|
|
function bare_use(file)
|
2024-07-23 17:21:27 -05:00
|
|
|
{
|
2024-07-25 17:53:53 -05:00
|
|
|
var script = io.slurp(file);
|
2024-07-25 22:07:22 -05:00
|
|
|
if (!script) return;
|
2024-07-25 17:53:53 -05:00
|
|
|
script = `(function() { var self = this; ${script}; })`;
|
2024-08-05 15:26:18 -05:00
|
|
|
Object.assign(globalThis, os.eval(file, script)());
|
2024-04-23 15:58:08 -05:00
|
|
|
}
|
|
|
|
|
2024-07-25 22:07:22 -05:00
|
|
|
globalThis.debug = {};
|
|
|
|
|
|
|
|
profile.enabled = true;
|
|
|
|
console.enabled = true;
|
|
|
|
debug.enabled = true;
|
2023-11-22 03:51:43 -06:00
|
|
|
|
2024-07-25 17:53:53 -05:00
|
|
|
bare_use("scripts/base.js");
|
|
|
|
bare_use("scripts/profile.js");
|
2023-09-08 12:35:06 -05:00
|
|
|
|
2024-08-02 20:52:50 -05:00
|
|
|
prosperon.release = function()
|
|
|
|
{
|
|
|
|
profile.enabled = false;
|
|
|
|
console.enabled = false;
|
|
|
|
debug.enabled = false;
|
|
|
|
}
|
|
|
|
|
2024-07-25 22:07:22 -05:00
|
|
|
bare_use("preconfig.js");
|
|
|
|
|
2024-07-25 17:53:53 -05:00
|
|
|
if (!profile.enabled)
|
|
|
|
use = stripped_use;
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2024-07-25 17:53:53 -05:00
|
|
|
Object.assign(globalThis, use("scripts/prosperon.js"));
|