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]);
actor_spawns[file].push(padawan);
padawan._file = file;
padawan._root = file.dir();
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 */
Object.defineProperty(Array.prototype, 'empty', {
get: function() { return this.length === 0; },

View file

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

View file

@ -101,11 +101,6 @@ Resources.is_sound = function(path) {
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)
{
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);
};
function find_ext(file, ext) {
function find_ext(file, ext, root = "") {
if (!file) return;
var file_ext = file.ext();
if (ext.some(x => x === file_ext)) return file;
for (var e of ext) {
var nf = `${file}.${e}`;
if (io.exists(nf)) return nf;
if (io.exists(nf))
return nf;
}
return;
var all_files = io.glob(`**${file}.*`);
var find = undefined;
for (var e of ext) {
var finds = all_files.filter(x => x.ext() === e);
if (finds.length === 1) {
find = finds[0];
break;
}
}
return find;
}
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);
};
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.say = function (msg) {

View file

@ -200,7 +200,7 @@ game.texture = function (path) {
path = Resources.find_image(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.time_cache[path] = io.mod(path);
return game.texture.cache[path];

View file

@ -134,6 +134,23 @@ io.rm = function(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({
extensions(ext) {
var paths = io.ls();
@ -143,12 +160,7 @@ io.mixin({
glob(pat) {
var paths = io.ls('.');
pat = pat.replaceAll(/([\[\]\(\)\^\$\.\|\+])/g, "\\$1");
pat = pat.replaceAll('**', '.*');
pat = pat.replaceAll(/[^\.]\*/g, '[^\\/]*');
pat = pat.replaceAll('?', '.');
var regex = new RegExp("^"+pat+"$", "");
var regex = io.globToRegex(pat);
paths = paths.filter(str => str.match(regex)).sort();
return paths;
},