Not a full Plugin, but a utility for Plugin developers that want to use JSON with MV plugin commands.
Plugin Commands are separated by spaces and do not take quotes into account, which means data structures aren't very compatible.
This snippet parses that array of space-separated command arguments and gives you something that's been sanitised to support JSON.
The way a JSON structure starts is a white-space followed by an open-brace character {
"abc{" will not work, but {{ will
There is no error detection or JSON validation, so if there is a missing closing brace the entire string will be appended on.
I have tried to cover every weird case to avoid bugs, but if you find a weird-case bug please say so.
Usage: var args = ParseCommandArgs( ["plugin", "command", "array"] );
An example command could be:
MyPlugin character { 'name' : 'xilef', 'level' : 0, 'stats' : { 'hp' : 123 } }
Everything in that JSON structure will be sanitised into a single string and all the arguments around it will be strings as usual.
Result : ["MyPlugin", "character", "{ 'name' : 'xilef', 'level' : 0, 'stats' : { 'hp' : 123 } }"]
Without this snippet : ["MyPlugin", "character", "{", "'name'", ":", "'xilef',", "'level'", ":", "0,", "'stats'", ":", "{", "'hp'", ":", "123", "}", "}"]
Here's an optimised version of the snippet:
Plugin Commands are separated by spaces and do not take quotes into account, which means data structures aren't very compatible.
This snippet parses that array of space-separated command arguments and gives you something that's been sanitised to support JSON.
The way a JSON structure starts is a white-space followed by an open-brace character {
"abc{" will not work, but {{ will
There is no error detection or JSON validation, so if there is a missing closing brace the entire string will be appended on.
I have tried to cover every weird case to avoid bugs, but if you find a weird-case bug please say so.
Usage: var args = ParseCommandArgs( ["plugin", "command", "array"] );
Code:
var ParseCommandArgs = function( inArgs ) {
var argStr = inArgs.join( ' ' ), args = [], argBuild = "", depth = 0, apo = 0, quot = 0, nonWhiteSpace = 0;
for ( var ii = 0, argStrLen = argStr.length; ii < argStrLen; ii++ ) {
switch ( argStr[ii] ) {
case '<span style="color: #000099; font-weight: bold;">\'':
if ( depth ) {
apo = 1 - apo; // String disables JSON
}
break;
case '"':
if ( depth ) {
quot = 1 - quot; // String disables JSON
}
break;
case '{':
if ( !nonWhiteSpace && !( quot + apo ) ) {
depth++; // Entered JSON
}
break;
case '}':
if ( depth > 0 && !( quot + apo ) ) {
depth--; // Left JSON
}
break;
default:
if ( !depth && <span style="color: #0066FF;">/\s/.test( argStr[ii] ) ) {
args.push( argBuild ); // Add to args
argBuild = ""; // Reset arg builder
nonWhiteSpace = 0; // We found white space so next char could be JSON {
continue;
}
break;
}
argBuild += argStr[ii]; // Concat next character
if ( !depth ) {
nonWhiteSpace++;
}
}
if ( argBuild.length > 0 ) {
args.push( argBuild ); // Add final string
}
return args;
};
An example command could be:
MyPlugin character { 'name' : 'xilef', 'level' : 0, 'stats' : { 'hp' : 123 } }
Everything in that JSON structure will be sanitised into a single string and all the arguments around it will be strings as usual.
Result : ["MyPlugin", "character", "{ 'name' : 'xilef', 'level' : 0, 'stats' : { 'hp' : 123 } }"]
Without this snippet : ["MyPlugin", "character", "{", "'name'", ":", "'xilef',", "'level'", ":", "0,", "'stats'", ":", "{", "'hp'", ":", "123", "}", "}"]
Here's an optimised version of the snippet:
Code:
var ParseCommandArgs = function( inArgs ) {
inArgs = inArgs.join( " " );
for ( var args = [], argBuild = "", depth = 0, apo = 0, quot = 0, nonWhiteSpace = 0, ii = 0, argStrLen = inArgs.length; ii < argStrLen; ii++ ) {
switch ( inArgs[ii] ) {
case "'":
depth && ( apo = 1 - apo );
break;
case '"':
depth && ( quot = 1 - quot );
break;
case "{":
nonWhiteSpace || quot + apo || depth++;
break;
case "}":
0 < depth && !( quot + apo ) && depth--;
break;
default:
if ( !depth && <span style="color: #0066FF;">/\s/.test( inArgs[ii] ) ) {
args.push( argBuild );
argBuild = "";
nonWhiteSpace = 0;
continue;
}
}
argBuild += inArgs[ii];
depth || nonWhiteSpace++;
}
0 < argBuild.length && args.push( argBuild );
return args;
};