add easy hashify function

This commit is contained in:
John Alanbrook 2024-08-27 13:57:38 -05:00
parent 2ad2d18af7
commit 31a47b5b7d
6 changed files with 65 additions and 34 deletions

View file

@ -25,6 +25,7 @@ globalThis.class_use = function(script, config, base, callback)
var padawan = Object.create(actor_urs[file]); var padawan = Object.create(actor_urs[file]);
actor_spawns[file].push(padawan); actor_spawns[file].push(padawan);
padawan._file = file; padawan._file = file;
padawan._root = file.dir();
if (callback) callback(padawan); if (callback) callback(padawan);

View file

@ -1083,15 +1083,6 @@ Object.defineProperty(Array.prototype, 'flat', {
} }
}); });
Object.defineProperty(Array.prototype, 'anyjs', {
value: function(fn) {
var ev = this.every(function(x) {
return !fn(x);
});
return !ev;
}
});
/* Return true if array contains x */ /* Return true if array contains x */
Object.defineProperty(Array.prototype, 'empty', { Object.defineProperty(Array.prototype, 'empty', {
get: function() { return this.length === 0; }, get: function() { return this.length === 0; },

View file

@ -83,7 +83,7 @@ var sprite = {
this.del_anim?.(); this.del_anim?.();
}, },
set path(p) { set path(p) {
p = Resources.find_image(p); p = Resources.find_image(p, this.gameobject._root);
if (!p) { if (!p) {
console.warn(`Could not find image ${p}.`); console.warn(`Could not find image ${p}.`);
return; return;

View file

@ -101,11 +101,6 @@ Resources.is_sound = function(path) {
return Resources.sounds.any(x => x === ext); return Resources.sounds.any(x => x === ext);
} }
Resources.is_image = function(path) {
var ext = path.ext();
return Resources.images.any(x => x === ext);
}
Resources.is_animation = function(path) Resources.is_animation = function(path)
{ {
if (path.ext() === 'gif' && Resources.gif.frames(path) > 1) return true; if (path.ext() === 'gif' && Resources.gif.frames(path) > 1) return true;
@ -154,26 +149,58 @@ Resources.is_image = function (path) {
return Resources.images.some(x => x === ext); return Resources.images.some(x => x === ext);
}; };
function find_ext(file, ext) { function find_ext(file, ext, root = "") {
if (!file) return; if (!file) return;
var file_ext = file.ext(); var file_ext = file.ext();
if (ext.some(x => x === file_ext)) return file; if (ext.some(x => x === file_ext)) return file;
for (var e of ext) { for (var e of ext) {
var nf = `${file}.${e}`; var nf = `${file}.${e}`;
if (io.exists(nf)) return nf; if (io.exists(nf))
} return nf;
return;
} }
Resources.find_image = function (file) { var all_files = io.glob(`**${file}.*`);
return find_ext(file, Resources.images); var find = undefined;
}; for (var e of ext) {
Resources.find_sound = function (file) { var finds = all_files.filter(x => x.ext() === e);
return find_ext(file, Resources.sounds); if (finds.length === 1) {
}; find = finds[0];
Resources.find_script = function (file) { break;
return find_ext(file, Resources.scripts); }
}; }
return find;
}
var hashhit = 0;
var hashmiss = 0;
Object.defineProperty(Function.prototype, 'hashify', {
value: function() {
var hash = new Map();
var fn = this;
function ret() {
if (!hash.has(arguments[0]))
hash.set(arguments[0], fn(...arguments));
return hash.get(arguments[0]);
}
return ret;
}
});
Resources.find_image = function (file, root = "") {
return find_ext(file, Resources.images, root);
}.hashify();
Resources.find_sound = function (file, root = "") {
return find_ext(file, Resources.sounds, root);
}.hashify();
Resources.find_script = function (file, root = "") {
return find_ext(file, Resources.scripts, root);
}.hashify();
console.transcript = ""; console.transcript = "";
console.say = function (msg) { console.say = function (msg) {

View file

@ -200,7 +200,7 @@ game.texture = function (path) {
path = Resources.find_image(path); path = Resources.find_image(path);
if (!io.exists(path)) { if (!io.exists(path)) {
console.warn(`Missing texture: ${path}`); console.error(`Missing texture: ${path}`);
game.texture.cache[path] = game.texture("icons/no_tex.gif"); game.texture.cache[path] = game.texture("icons/no_tex.gif");
game.texture.time_cache[path] = io.mod(path); game.texture.time_cache[path] = io.mod(path);
return game.texture.cache[path]; return game.texture.cache[path];

View file

@ -134,6 +134,23 @@ io.rm = function(f)
tmprm(Resources.replpath(f)); tmprm(Resources.replpath(f));
} }
io.globToRegex = function(glob) {
// Escape special regex characters
// Replace glob characters with regex equivalents
let regexStr = glob
.replace(/[\.\\]/g, '\\$&') // Escape literal backslashes and dots
.replace(/([^\*])\*/g, '$1[^/]*') // * matches any number of characters except /
.replace(/\*\*/g, '.*') // ** matches any number of characters, including none
.replace(/\[(.*?)\]/g, '[$1]') // Character sets
.replace(/\?/g, '.'); // ? matches any single character
// Ensure the regex matches the whole string
regexStr = '^' + regexStr + '$';
// Create and return the regex object
return new RegExp(regexStr);
}
io.mixin({ io.mixin({
extensions(ext) { extensions(ext) {
var paths = io.ls(); var paths = io.ls();
@ -143,12 +160,7 @@ io.mixin({
glob(pat) { glob(pat) {
var paths = io.ls('.'); var paths = io.ls('.');
pat = pat.replaceAll(/([\[\]\(\)\^\$\.\|\+])/g, "\\$1"); var regex = io.globToRegex(pat);
pat = pat.replaceAll('**', '.*');
pat = pat.replaceAll(/[^\.]\*/g, '[^\\/]*');
pat = pat.replaceAll('?', '.');
var regex = new RegExp("^"+pat+"$", "");
paths = paths.filter(str => str.match(regex)).sort(); paths = paths.filter(str => str.match(regex)).sort();
return paths; return paths;
}, },