Skip to content

Instantly share code, notes, and snippets.

@UserUnknownFactor
Last active April 3, 2024 12:26
Show Gist options
  • Save UserUnknownFactor/09718b5ff95854d0933862961430eb45 to your computer and use it in GitHub Desktop.
Save UserUnknownFactor/09718b5ff95854d0933862961430eb45 to your computer and use it in GitHub Desktop.
Plugin to better understand JSON loading errors source in RPG Maker MV/MZ games
@echo off
powershell.exe -ExecutionPolicy Bypass -Command "$_=((Get-Content \"%~f0\") -join \"`n\");iex $_.Substring($_.IndexOf(\"goto :\"+\"EOF\")+9)"
del add_json_debug.cmd
@goto :EOF
$pliginsName = ".\www\js\plugins.js"
$pluginsStr =(Get-Content $pliginsName)
if($pluginsStr -like '*DebugJSONLoad*') {
Write-Host "Already patched!"
} else {
$pluginsStr.Replace("];",",{`"name`":`"DebugJSONLoad`",`"status`":true,`"description`":`"Shows better info for JSON load errors`",`"parameters`":{}}`n];") | Set-Content $pliginsName
Write-Host "Patch successful."
}
//=============================================================================
// DebugJSONLoad.js (for RPG Maker MV/MZ)
// License: WTFPL
// Description: Shows exactly where and what JSON file causes a load error
// Usage: Add this to plugins.json before the last ]; to enable the plugin:
// ,{"name":"DebugJSONLoad","status":true,"description":"Shows better info for JSON load errors","parameters":{}}
//=============================================================================
if(Utils.RPGMAKER_NAME === "MV") {
DataManager.loadDataFile = function(name, src) {
var xhr = new XMLHttpRequest();
var url = 'data/' + src;
xhr.open('GET', url);
xhr.overrideMimeType('application/json');
xhr.onload = function() {
if (xhr.status < 400) {
try {
window[name] = JSON.parse(xhr.responseText);
} catch (e) {
var pos = parseInt(e.message.match(/position (\d+)/)[1]);
var err = xhr.responseText.slice(pos - 20, pos + 20);
throw e.message + " in " + src + " Text:<br>" + err + " ";
}
DataManager.onLoad(window[name]);
}
};
xhr.onerror = this._mapLoader || function() {
DataManager._errorUrl = DataManager._errorUrl || url;
};
window[name] = null;
xhr.send();
};
} else {
DataManager.onXhrLoad = function(xhr, name, src, url) {
if (xhr.status < 400) {
try {
window[name] = JSON.parse(xhr.responseText);
} catch (e) {
var pos = parseInt(e.message.match(/position (\d+)/)[1]);
var err = xhr.responseText.slice(pos - 20, pos + 20);
throw e.message + " in " + src + " Text:<br>" + err + " ";
}
this.onLoad(window[name]);
} else {
this.onXhrError(name, src, url);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment