Whitespace causing regex to pick up wrong array content (Irina VisualNovelBusts)

I've got a small but still annoying bug I want to fix in the Irina VisualNovelBusts plugin. (https://atelieririna.itch.io/visual-novel-busts)

The Situation:
If I use the escape code \bustMsgExp[x, y] in a Show text event it switches the face used in bustslot x to y.
So if I make a showtext event like this:
Code:
\bust[1]This is a simple message.\BustMsgExp[1, 5]
It sets the current face picture to show as bust in Bust slot 1, prints "This is a simple message" and afterwards changes the face to expression 5.
That's working fine.

The Problem:
However if I write this code...
Code:
\bust[1]This is a simple message. \BustMsgExp[1, 5]
The game crashes and I get an error message.

My suspicion: the regular expression that's supposed to pick up \BustMsgExp doesn't get picked up correctly due to the white space confusing the process.

The error message itself refers as the problem happening in this line:
Code:
var expression=array[1].toUpperCase().trim();
Regardless of what comes first, toUpperCase or trim, the code immediately crashes.

I've set up a couple alert messages to get the contents of the array before the crash:
If it's working, the array returns:
array[0] = 1
array[1] = 5
array[2] = undefined (this array is not made use of, no issues)
The array[0] is the x in \BustMsgExp[x, y], the array[1] is the y.

But if I insert a whitespace:
array[0] =
(there is nothing in the array, not even undefined or 0. Just nothing)
array[1] = undefined
array[2] = undefined (as it should be)

The part of the code that parses into the array is this one:
Code:
if(code.match(/BUSTMSG(?:INDEX|EXP|EXPRESSION)/i)){var array=this.obtainVNBustMsgEscapeString(textState).split(",");
var sprite=$bust(parseInt(array[0]));
That's where array[0], the Bustslot, gets its ID assigned.

The part obtainVNBustMsgEscapeString leads back to this function that's used for all escape strings:
Code:
Window_Base.prototype.obtainVNBustMsgEscapeString=function(t){var e=/^\[(.*?)\]/.exec(t.text.slice(t.index));
if(e){t.index+=e[0].length;
return String(e[0].slice(1,e[0].length-1))}else{return""}};
Imported.Irina_VisualNovelBusts.Window_Message_initMembers=Window_Message.prototype.initMembers;

I don't know much about regular expressions, but my assumption is that somewhere in these two codes, there's something that causes a single whitespace to pick up the entirely wrong contents.

Can someone tell me where the problem is and how to fix it? I'd really like to ensure that a single whitespace won't cause crashes and that the plugin becomes more stable.
As the plugins paid I can't actually run any tests on this to confirm anything, and some of these array values you've provided look like they might be unrelated. eg. if array[1] = 5, then array[1].toUpperCase().trim() shouldn't be working.

It would be useful to see what value "code" holds and what values "array" holds immediately after it's declared and initialized.

Could you set up console logs for this and let me know the values. Something like this would work.

Code:
console.log(code);
if(code.match(/BUSTMSG(?:INDEX|EXP|EXPRESSION)/i)){var array=this.obtainVNBustMsgEscapeString(textState).split(",");
console.log(array); // I expect this won't execute.
var sprite=$bust(parseInt(array[0]));
As the plugins paid I can't actually run any tests on this to confirm anything, and some of these array values you've provided look like they might be unrelated. eg. if array[1] = 5, then array[1].toUpperCase().trim() shouldn't be working.

It would be useful to see what value "code" holds and what values "array" holds immediately after it's declared and initialized.

Could you set up console logs for this and let me know the values. Something like this would work.

Code:
console.log(code);
if(code.match(/BUSTMSG(?:INDEX|EXP|EXPRESSION)/i)){var array=this.obtainVNBustMsgEscapeString(textState).split(",");
console.log(array); // I expect this won't execute.
var sprite=$bust(parseInt(array[0]));
I did.

This is the result:
issue.png
For testing purposes, I set up the same message to play again, correct the first time, with whitespace the second time.
C, HC, C and HC are also results of the console.log, though I assume these are previous executions where not message bust had yet been set or something?
The array result for the working one is BUSTMSGEXPRESSION with (2) ["1", "5"]
while the second one for didn't capture anything because of the whitespace for some reason.
Underneath it is the crashlog when the game then crashes.
@Anyone Could you provide the second BUSTMSGEXPRESSION text code used in the message window?
@Anyone Could you provide the second BUSTMSGEXPRESSION text code used in the message window?
Sure.

First though, the messages weren't absolutely identical, so I fixed that and redid the logging, just to be sure:
issue 2.png
It's still the same, but for some reason presented slightly different? There's a slight difference in how the array is written, though the contents seem identical. I've opened them just to be sure everything can be seen, instead of just seeing Array(2).

Message number 1, the working one, in full: (directly copied)
Code:
\Bust[1] \bustSlideInFromRight[1, 90]Is this finally working?\|<br>
\bustMsgExpression[1, 5] Has my expression changed?

Message number 2, the crashing one, in full: (directly copied)
Code:
\Bust[1] \bustSlideInFromRight[1, 90]Is this finally working?\|<br>
 \bustMsgExpression[1, 5] Has my expression changed?

Oh and for reference, the crash location is in the code at the top.
@Anyone Hmm, well, I have set up some code which more or less emulates what is going on, and I cannot get any message to explode.

Can the second argument in BustMsgExpression ever be a string according to the plugin documentation?



EDIT: Try slipping a 'console.log(e)' into the 'obtainVNBustMsgEscapeString' function to get a bit more information.

Code:
Window_Base.prototype.obtainVNBustMsgEscapeString=function(t){var e=/^\[(.*?)\]/.exec(t.text.slice(t.index));
console.log(e);
if(e){t.index+=e[0].length;
return String(e[0].slice(1,e[0].length-1))}else{return""}};
@Anyone Hmm, well, I have set up some code which more or less emulates what is going on, and I cannot get any message to explode.

Can the second argument in BustMsgExpression ever be a string according to the plugin documentation?



EDIT: Try slipping a 'console.log(e)' into the 'obtainVNBustMsgEscapeString' function to get a bit more information.

Code:
Window_Base.prototype.obtainVNBustMsgEscapeString=function(t){var e=/^\[(.*?)\]/.exec(t.text.slice(t.index));
console.log(e);
if(e){t.index+=e[0].length;
return String(e[0].slice(1,e[0].length-1))}else{return""}};
issue 3.png
e seems to be most of the text though at that point, \bust[1] and the \BustSlide things seem to have already been stripped from the text.

EDIT: Okay, I feel slighty stupid now that I didn't try this earlier, but it actually seems like the bug must be caused elsewhere.
I've made a lightweight test version just with this plugin, and it didn't bug out.

I'm going to check which of the other plugins might cause those escape characters to bug out and report back then.
@Anyone Huh ... no idea why the contents of the second text code is coming up empty. Toss the textState into the console, too ... so, 'console.log(e, t)'
@Anyone Huh ... no idea why the contents of the second text code is coming up empty. Toss the textState into the console, too ... so, 'console.log(e, t)'
Sorry for the delay.
I basically had to sift through close to 300 plugins, on and off them, to finally track down the true culprint.

First the textstate logs:
issue 4.png issue 4.png issue 6.png

But it's actually not this plugin that's responsible.
The problem is actually somewhere in yanfly's message core plugin, specifically when WORDWRAP is used.
Something that wordwrap does causes the text to change in such a way it can no longer be processed correctly by Irina's plugin.
What if you swap the order of the plugins? If this one is below Yanfly's, put it above instead. And if it's already above, put it below. Might not work, but sometimes that's all that's needed.
What if you swap the order of the plugins? If this one is below Yanfly's, put it above instead. And if it's already above, put it below. Might not work, but sometimes that's all that's needed.
Doesn't work unfortunately. The yanfly plugin still seems to be doing something with wordwrap before this plugin can extract its escapes, even if I place this plugin above message core and the yanfly engine core.

I've got a test build where I've only got Yanfly's Engine Core, the Message Core, and Irina's plugin. No matter what position her plugin is in, even no 1, wordwrap makes it crash due to that whitespace. 😕
Okay the way wordwrap normally works is inserting a new line if there is not enough space left to display the word. Words are often seperated by spaces. I suppose that is how it works in Message Core too, however I do not really remember. But just try using
\bustMsgExpression[1,5] instead of \bustMsgExpression[1, 5] by removing the space it should hopefully be seen as a single word and the wordwrap shouldn't cut it short.
Okay the way wordwrap normally works is inserting a new line if there is not enough space left to display the word. Words are often seperated by spaces. I suppose that is how it works in Message Core too, however I do not really remember. But just try using
\bustMsgExpression[1,5] instead of \bustMsgExpression[1, 5] by removing the space it should hopefully be seen as a single word and the wordwrap shouldn't cut it short.
I'll be damned, that actually works almost perfectly.

It now still has a slight error, which may or may not be noticeable.
For some reason it now, instead of going through the expression once, does it twice. So since I've got alert codes in the code, I can see that it executes that command two times in a row.

I'm not sure if that's really a problem, though. I've tried it without the alert codes, and it's not visibly noticeable on any level. So yeah, that simple trick might actually do it. 😮
The issue is that the line begins with a space. Word Wrap will then attempt to find the next space which in the above text codes is between the 1 and 5, causing the Message Core plugin to believe it has found a new word which is then passed on to the Bust plugin and the malformed text code then blows up.


EDIT: Here is a hack-y work-around to keep spaces at the beginning of a new line from exploding certain text codes :

Code:
Window_Base.prototype.checkWordWrap = function(textState) {
    if (!textState) return false;
    if (!this._wordWrap) return false;
    var wIndex = /[^ ]/.exec(textState.text.substring(textState.index, textState.text.length)).index;
    if (textState.text[textState.index] === ' ' && wIndex === 0) {
      var nextSpace = textState.text.indexOf(' ', textState.index + 1);
      var nextBreak = textState.text.indexOf('\n', textState.index + 1);
      if (nextSpace < 0) nextSpace = textState.text.length + 1;
      if (nextBreak > 0) nextSpace = Math.min(nextSpace, nextBreak);
      var word = textState.text.substring(textState.index, nextSpace);
      var size = this.textWidthExCheck(word);
    }
    return (size + textState.x > this.wordwrapWidth());
};

Original thread: https://forums.rpgmakerweb.com/threads/whitespace-causing-regex-to-pick-up-wrong-array-content-irina-visualnovelbusts.115269/