From 22f951332c58c5d37b97d4bc2f9c866bd5803ea3 Mon Sep 17 00:00:00 2001 From: Todor Imreorov Date: Thu, 6 Feb 2020 18:58:58 +0000 Subject: [PATCH 1/5] add more protection against people crashing their game with improper use --- Extensions/DialogueTree/dialoguetools.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Extensions/DialogueTree/dialoguetools.js b/Extensions/DialogueTree/dialoguetools.js index b15d19be8b4..a3a86ce84d1 100644 --- a/Extensions/DialogueTree/dialoguetools.js +++ b/Extensions/DialogueTree/dialoguetools.js @@ -120,6 +120,7 @@ gdjs.dialogueTree.completeClippedTextScrolling = function() { * Useful to prevent the user from skipping to next line before the current one has been printed fully. */ gdjs.dialogueTree.hasClippedScrollingCompleted = function() { + if (!this.dialogueIsRunning) return false; if (this.dialogueData && this.dialogueText.length) { return this.clipTextEnd >= this.dialogueText.length; } @@ -131,7 +132,7 @@ gdjs.dialogueTree.hasClippedScrollingCompleted = function() { * Used with the scrollClippedText to achieve a classic scrolling text, as well as any <> effects to pause scrolling. */ gdjs.dialogueTree.getClippedLineText = function() { - return this.dialogueText.length + return this.dialogueIsRunning && this.dialogueText.length ? this.dialogueText.substring(0, this.clipTextEnd) : ''; }; @@ -142,7 +143,9 @@ gdjs.dialogueTree.getClippedLineText = function() { */ gdjs.dialogueTree.getLineText = function() { this.completeClippedTextScrolling(); - return this.dialogueText.length ? this.dialogueText : ''; + return this.dialogueIsRunning && this.dialogueText.length + ? this.dialogueText + : ''; }; /** @@ -177,6 +180,8 @@ gdjs.dialogueTree.getCommandParameter = function(paramIndex) { * @param {string} command The command you want to check for being called. Write it without the `<<>>`. */ gdjs.dialogueTree.isCommandCalled = function(command) { + if (!this.dialogueIsRunning) return false; + var commandCalls = gdjs.dialogueTree.commandCalls; var clipTextEnd = gdjs.dialogueTree.clipTextEnd; var dialogueText = gdjs.dialogueTree.dialogueText; @@ -225,7 +230,7 @@ gdjs.dialogueTree._cycledOptionIndex = function(optionIndex) { * @param {number} optionIndex The index of the option you want to get */ gdjs.dialogueTree.getLineOption = function(optionIndex) { - if (!this.options.length) return []; + if (!this.dialogueIsRunning || !this.options.length) return []; optionIndex = gdjs.dialogueTree._normalizedOptionIndex(optionIndex); return this.options[optionIndex]; }; @@ -239,7 +244,7 @@ gdjs.dialogueTree.getLineOptionsText = function( optionSelectionCursor, addNewLine ) { - if (!this.options.length) return ''; + if (!this.dialogueIsRunning || !this.options.length) return ''; var textResult = ''; this.options.forEach(function(optionText, index) { if (index === gdjs.dialogueTree.selectedOption) { @@ -266,7 +271,7 @@ gdjs.dialogueTree.getLineOptionsTextVertical = function(optionSelectionCursor) { * @returns {number} The number of options */ gdjs.dialogueTree.getLineOptionsCount = function() { - if (this.options.length) { + if (!this.dialogueIsRunning || this.options.length) { return this.optionsCount; } return 0; @@ -279,6 +284,7 @@ gdjs.dialogueTree.getLineOptionsCount = function() { */ gdjs.dialogueTree.confirmSelectOption = function() { if ( + this.dialogueIsRunning && this.dialogueData.select && !this.selectedOptionUpdated && this.selectedOption !== -1 @@ -301,6 +307,7 @@ gdjs.dialogueTree.confirmSelectOption = function() { * Select next option during Options type line parsing. Hook this to your game input. */ gdjs.dialogueTree.selectNextOption = function() { + if (!this.dialogueIsRunning) return; if (this.dialogueData.select) { this.selectedOption += 1; this.selectedOption = gdjs.dialogueTree._cycledOptionIndex( @@ -314,6 +321,7 @@ gdjs.dialogueTree.selectNextOption = function() { * Select previous option during Options type line parsing. Hook this to your game input. */ gdjs.dialogueTree.selectPreviousOption = function() { + if (!this.dialogueIsRunning) return; if (this.dialogueData.select) { this.selectedOption -= 1; this.selectedOption = gdjs.dialogueTree._cycledOptionIndex( @@ -328,6 +336,7 @@ gdjs.dialogueTree.selectPreviousOption = function() { * @param {number} optionIndex The index of the option to select */ gdjs.dialogueTree.selectOption = function(optionIndex) { + if (!this.dialogueIsRunning) return; if (this.dialogueData.select) { this.selectedOption = gdjs.dialogueTree._normalizedOptionIndex( this.selectedOption @@ -341,6 +350,7 @@ gdjs.dialogueTree.selectOption = function(optionIndex) { * @returns {number} The index of the currently selected option */ gdjs.dialogueTree.getSelectedOption = function() { + if (!this.dialogueIsRunning) return; if (this.dialogueData.select) { return this.selectedOption; } @@ -355,6 +365,7 @@ gdjs.dialogueTree.getSelectedOption = function() { * @returns {boolean} true if the selected option was updated since the last call to this function */ gdjs.dialogueTree.hasSelectedOptionChanged = function() { + if (!this.dialogueIsRunning) return; if (this.selectedOptionUpdated) { this.selectedOptionUpdated = false; if (this.selectedOption === -1) this.selectedOption = 0; @@ -391,6 +402,7 @@ gdjs.dialogueTree.isDialogueLineType = function(type) { * @param {string} branchName The Dialogue Branch name you want to check. */ gdjs.dialogueTree.hasDialogueBranch = function(branchName) { + if (!this.dialogueIsRunning) return false; return ( this.runner && this.runner.yarnNodes && From 68da3e2a6361df734d907eb632bb310f774682ce Mon Sep 17 00:00:00 2001 From: Todor Imreorov Date: Thu, 6 Feb 2020 19:29:20 +0000 Subject: [PATCH 2/5] fix regression --- Extensions/DialogueTree/dialoguetools.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Extensions/DialogueTree/dialoguetools.js b/Extensions/DialogueTree/dialoguetools.js index a3a86ce84d1..ee172d95855 100644 --- a/Extensions/DialogueTree/dialoguetools.js +++ b/Extensions/DialogueTree/dialoguetools.js @@ -284,10 +284,10 @@ gdjs.dialogueTree.getLineOptionsCount = function() { */ gdjs.dialogueTree.confirmSelectOption = function() { if ( - this.dialogueIsRunning && - this.dialogueData.select && - !this.selectedOptionUpdated && - this.selectedOption !== -1 + !this.dialogueIsRunning || + (this.dialogueData.select && + !this.selectedOptionUpdated && + this.selectedOption !== -1) ) { this.commandCalls = []; try { @@ -307,7 +307,6 @@ gdjs.dialogueTree.confirmSelectOption = function() { * Select next option during Options type line parsing. Hook this to your game input. */ gdjs.dialogueTree.selectNextOption = function() { - if (!this.dialogueIsRunning) return; if (this.dialogueData.select) { this.selectedOption += 1; this.selectedOption = gdjs.dialogueTree._cycledOptionIndex( @@ -321,7 +320,6 @@ gdjs.dialogueTree.selectNextOption = function() { * Select previous option during Options type line parsing. Hook this to your game input. */ gdjs.dialogueTree.selectPreviousOption = function() { - if (!this.dialogueIsRunning) return; if (this.dialogueData.select) { this.selectedOption -= 1; this.selectedOption = gdjs.dialogueTree._cycledOptionIndex( @@ -336,7 +334,6 @@ gdjs.dialogueTree.selectPreviousOption = function() { * @param {number} optionIndex The index of the option to select */ gdjs.dialogueTree.selectOption = function(optionIndex) { - if (!this.dialogueIsRunning) return; if (this.dialogueData.select) { this.selectedOption = gdjs.dialogueTree._normalizedOptionIndex( this.selectedOption @@ -350,7 +347,6 @@ gdjs.dialogueTree.selectOption = function(optionIndex) { * @returns {number} The index of the currently selected option */ gdjs.dialogueTree.getSelectedOption = function() { - if (!this.dialogueIsRunning) return; if (this.dialogueData.select) { return this.selectedOption; } @@ -365,7 +361,6 @@ gdjs.dialogueTree.getSelectedOption = function() { * @returns {boolean} true if the selected option was updated since the last call to this function */ gdjs.dialogueTree.hasSelectedOptionChanged = function() { - if (!this.dialogueIsRunning) return; if (this.selectedOptionUpdated) { this.selectedOptionUpdated = false; if (this.selectedOption === -1) this.selectedOption = 0; @@ -402,7 +397,6 @@ gdjs.dialogueTree.isDialogueLineType = function(type) { * @param {string} branchName The Dialogue Branch name you want to check. */ gdjs.dialogueTree.hasDialogueBranch = function(branchName) { - if (!this.dialogueIsRunning) return false; return ( this.runner && this.runner.yarnNodes && From 6b17827fd66792b11accf185faa9b4dc49558001 Mon Sep 17 00:00:00 2001 From: Todor Imreorov Date: Fri, 7 Feb 2020 20:27:54 +0000 Subject: [PATCH 3/5] fix json loading resource reading at the beginning of scene (thanks @4ian) --- GDJS/Runtime/jsonmanager.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/GDJS/Runtime/jsonmanager.js b/GDJS/Runtime/jsonmanager.js index 81ebff9da04..f5f51b0caf6 100644 --- a/GDJS/Runtime/jsonmanager.js +++ b/GDJS/Runtime/jsonmanager.js @@ -56,7 +56,7 @@ gdjs.JsonManager.prototype.preloadJsons = function(onProgress, onComplete) { /** @type JsonManagerRequestCallback */ var onLoad = function(error, jsonContent) { if (error) { - console.error("Error while preloading a json resource:" + error); + console.error('Error while preloading a json resource:' + error); } loaded++; @@ -104,6 +104,12 @@ gdjs.JsonManager.prototype.loadJson = function(resourceName, callback) { return; } + // Don't fetch again an object that is already in memory + if (this._loadedJsons[resourceName]) { + callback(null, this._loadedJsons[resourceName]); + return; + } + var that = this; var xhr = new XMLHttpRequest(); xhr.responseType = 'json'; From 5067de1500aee2295b924c85937cfa4d938bc3e3 Mon Sep 17 00:00:00 2001 From: Todor Imreorov Date: Fri, 7 Feb 2020 20:34:35 +0000 Subject: [PATCH 4/5] fix getClippedLineText sometimes clips the last letter --- Extensions/DialogueTree/dialoguetools.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Extensions/DialogueTree/dialoguetools.js b/Extensions/DialogueTree/dialoguetools.js index ee172d95855..cde43454bcc 100644 --- a/Extensions/DialogueTree/dialoguetools.js +++ b/Extensions/DialogueTree/dialoguetools.js @@ -133,7 +133,7 @@ gdjs.dialogueTree.hasClippedScrollingCompleted = function() { */ gdjs.dialogueTree.getClippedLineText = function() { return this.dialogueIsRunning && this.dialogueText.length - ? this.dialogueText.substring(0, this.clipTextEnd) + ? this.dialogueText.substring(0, this.clipTextEnd + 1) : ''; }; From 80457100233e21bb139402744aeee54e546cf4ea Mon Sep 17 00:00:00 2001 From: Todor Imreorov Date: Sat, 8 Feb 2020 13:19:12 +0000 Subject: [PATCH 5/5] fix silly code, add even more protection --- Extensions/DialogueTree/dialoguetools.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Extensions/DialogueTree/dialoguetools.js b/Extensions/DialogueTree/dialoguetools.js index cde43454bcc..450438ee5f3 100644 --- a/Extensions/DialogueTree/dialoguetools.js +++ b/Extensions/DialogueTree/dialoguetools.js @@ -271,7 +271,7 @@ gdjs.dialogueTree.getLineOptionsTextVertical = function(optionSelectionCursor) { * @returns {number} The number of options */ gdjs.dialogueTree.getLineOptionsCount = function() { - if (!this.dialogueIsRunning || this.options.length) { + if (this.dialogueIsRunning && this.options.length) { return this.optionsCount; } return 0; @@ -283,11 +283,11 @@ gdjs.dialogueTree.getLineOptionsCount = function() { * This will advance the dialogue tree to the dialogue branch was selected by the player. */ gdjs.dialogueTree.confirmSelectOption = function() { + if (!this.dialogueIsRunning) return; if ( - !this.dialogueIsRunning || - (this.dialogueData.select && - !this.selectedOptionUpdated && - this.selectedOption !== -1) + this.dialogueData.select && + !this.selectedOptionUpdated && + this.selectedOption !== -1 ) { this.commandCalls = []; try { @@ -307,6 +307,7 @@ gdjs.dialogueTree.confirmSelectOption = function() { * Select next option during Options type line parsing. Hook this to your game input. */ gdjs.dialogueTree.selectNextOption = function() { + if (!this.dialogueIsRunning) return; if (this.dialogueData.select) { this.selectedOption += 1; this.selectedOption = gdjs.dialogueTree._cycledOptionIndex( @@ -320,6 +321,7 @@ gdjs.dialogueTree.selectNextOption = function() { * Select previous option during Options type line parsing. Hook this to your game input. */ gdjs.dialogueTree.selectPreviousOption = function() { + if (!this.dialogueIsRunning) return; if (this.dialogueData.select) { this.selectedOption -= 1; this.selectedOption = gdjs.dialogueTree._cycledOptionIndex( @@ -334,6 +336,7 @@ gdjs.dialogueTree.selectPreviousOption = function() { * @param {number} optionIndex The index of the option to select */ gdjs.dialogueTree.selectOption = function(optionIndex) { + if (!this.dialogueIsRunning) return; if (this.dialogueData.select) { this.selectedOption = gdjs.dialogueTree._normalizedOptionIndex( this.selectedOption @@ -347,6 +350,7 @@ gdjs.dialogueTree.selectOption = function(optionIndex) { * @returns {number} The index of the currently selected option */ gdjs.dialogueTree.getSelectedOption = function() { + if (!this.dialogueIsRunning) return; if (this.dialogueData.select) { return this.selectedOption; }