[MV] JSON from Commands snippet

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"] );
  1. var ParseCommandArgs = function( inArgs ) {
  2.     var argStr = inArgs.join( ' ' ), args = [], argBuild = "", depth = 0, apo = 0, quot = 0, nonWhiteSpace = 0;
  3.     for ( var ii = 0, argStrLen = argStr.length; ii < argStrLen; ii++ ) {
  4.         switch ( argStr[ii] ) {
  5.         case ''':
  6.             if ( depth ) {
  7.                 apo = 1 - apo; // String disables JSON
  8.             }
  9.             break;
  10.         case '"':
  11.             if ( depth ) {
  12.                 quot = 1 - quot; // String disables JSON
  13.             }
  14.             break;
  15.         case '{':
  16.             if ( !nonWhiteSpace && !( quot + apo ) ) {
  17.                 depth++; // Entered JSON
  18.             }
  19.             break;
  20.         case '}':
  21.             if ( depth > 0 && !( quot + apo ) ) {
  22.                 depth--; // Left JSON
  23.             }
  24.             break;
  25.         default:
  26.             if ( !depth && /\s/.test( argStr[ii] ) ) {
  27.                 args.push( argBuild ); // Add to args
  28.                 argBuild = ""; // Reset arg builder
  29.                 nonWhiteSpace = 0; // We found white space so next char could be JSON {        
  30.                 continue;
  31.             }
  32.             break;
  33.         }
  34.         argBuild += argStr[ii]; // Concat next character
  35.         if ( !depth ) {
  36.             nonWhiteSpace++;
  37.         }
  38.     }
  39.     if ( argBuild.length > 0 ) {
  40.         args.push( argBuild ); // Add final string
  41.     }
  42.     return args;
  43. };
Expand to see the code.


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:
  1. var ParseCommandArgs = function( inArgs ) {
  2.     inArgs = inArgs.join( " " );
  3.     for ( var args = [], argBuild = "", depth = 0, apo = 0, quot = 0, nonWhiteSpace = 0, ii = 0, argStrLen = inArgs.length; ii < argStrLen; ii++ ) {
  4.         switch ( inArgs[ii] ) {
  5.         case "'":
  6.             depth && ( apo = 1 - apo );
  7.             break;
  8.         case '"':
  9.             depth && ( quot = 1 - quot );
  10.             break;
  11.         case "{":
  12.             nonWhiteSpace || quot + apo || depth++;
  13.             break;
  14.         case "}":
  15.             0 < depth && !( quot + apo ) && depth--;
  16.             break;
  17.         default:
  18.             if ( !depth && /\s/.test( inArgs[ii] ) ) {
  19.                 args.push( argBuild );
  20.                 argBuild = "";
  21.                 nonWhiteSpace = 0;
  22.                 continue;
  23.             }
  24.         }
  25.         argBuild += inArgs[ii];
  26.         depth || nonWhiteSpace++;
  27.     }
  28.     0 < argBuild.length && args.push( argBuild );
  29.     return args;
  30. };
Expand to see the code.
Nice, it's always bugged me how limited plugin commands are.
Forgot to mention; this only sanitises the string. To get a Javascript object out of the JSON you need to do something like this;

  1. args = ParseCommandArgs( args );
  2.  
  3. var data = JSON.parse( args[index_of_json] );
  4. for ( var key in data ) {
  5.     my_object[key] = data[key];
  6. }
Expand to see the code.


Princess Amy wrote:
Nice, it's always bugged me how limited plugin commands are.

This is part of an attempt to make them more useful.

On one hand it solves the problem of lack of API space in previous RPG Maker versions, on the other hand it's very difficult to get a sane usage scenario figured out for it - especially when the script option is just above and can do the same + more than the plugin command system.

Adding data structure support is a small way to alleviate that, at the expense of JSON's complexity being dragged into it (ugh).

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=78923