MV [solved but still open for ideas] Safely load Ressource that might not exist

Hey all,

for my plugin, I want it to load some PNG files, whereas some files might not be available (in this case, dummy graphics are shown), and this is intentional! However, RPG Maker will just abort the game.

I want to avoid doing "does this file exist?"-checks as they work fine on Node but not with a browser game, and I also think that checking the existence first and then load a resource asynchronously is no good design.

Whereas I had almost nothing to do to make it work in MZ, I'm still struggling to find a nice way to make something like this in MV.
So I've recently tried to do this in order to remove errors after missing files (to help in this thread), and decided that it's too much of a pain and instead went with checking if a file exists.

If you want to do this with a browser version, the main issue is that you don't have an easy way to tell if a file failed to load because of a connection issue or because it doesn't exist. You'd need to either do some low-level modifications to look at the server responses and check which one it is, or accept that your game will break if the player ever loses connection for more than a few seconds.

As for handling errors, a good point to start is ResourceHandler.createLoader - is creates a function that's added as a listener for errors with loading files, and handles retries and failures, so that's where you'd probably want to start implementing a custom failure behaviour.
So, I ended up with the following:

  • When using MZ, everything works fine
  • When using MV, the code will do a "does file exist?"-check first
  • As already explained by @Mac15001900 , such a check is hard to program when you want to support browser games. Therefore, a browser game made with MV will NOT check if a requested file exists and crash if it doesn't.

Here's my code:

Code:
function MK_ImageManager() {
    throw new Error("This is a static class");
}
MK_ImageManager._imageCache = { };
// may return null
MK_ImageManager.safelyLoadBitmap = function(folder, name) {
    const key = this.makeKey(folder, name);
    if (this._imageCache[key]) {
        return this._imageCache[key];
    }
    if (this.shouldLoadBitmap(folder, name)) {
        const url = this.makeUrl(folder, name);
        const bitmap = Bitmap.load(url);
        this._imageCache[key] = bitmap;
        return bitmap;
    } else {
        return null;
    }
}
MK_ImageManager.shouldLoadBitmap = function(folder, name) {
    return (
        'MZ' == Utils.RPGMAKER_NAME ||
        this.isWeb() ||
        this.existsFileSync(folder, name)
    );
}
MK_ImageManager.isWeb = function() {
    return !StorageManager.isLocalMode();
}

// MV only
MK_ImageManager.existsFileSync = function(folder, name) {
    if (!Utils.isNwjs()) {
        return false; // escape route
    }
    var fs = require('fs');
    
    const filepath = this.makeFilepath(folder, name);
    return fs.existsSync(filepath);
}

// MV only
MK_ImageManager.makeFilepath = function(folder, name) {
    var path = require('path');
    return path.dirname(process.mainModule.filename) + '/' + folder + name + '.png';
}
MK_ImageManager.isReady = function() {
    return Object
        .values(this._imageCache)
        .filter(Boolean)
        .every(bitmap => bitmap.isReady() || bitmap.isError());
}

// may return null
MK_ImageManager.getBitmap = function(folder, name) {
    const key = this.makeKey(folder, name);
    return this._imageCache[key];
}
MK_ImageManager.addBitmapToCache = function(folder, name, bitmap) {
    const key = this.makeKey(folder, name);
    this._imageCache[key] = bitmap;
}
MK_ImageManager.makeKey = function(folder, name) {
    return folder + name;
}
MK_ImageManager.makeUrl = function(folder, name) {
    return folder + this.encode(name) + ".png";
}
MK_ImageManager.encode = function(name) {
    return 'MZ' == Utils.RPGMAKER_NAME
        ? Utils.encodeURI(name)
        : encodeURIComponent(name);
}

Usage:
Code:
const bitmap = MK_ImageManager.safelyLoadBitmap(folder, name)

Original thread: https://forums.rpgmakerweb.com/threads/solved-but-still-open-for-ideas-safely-load-ressource-that-might-not-exist.166624/