Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SafeRange bug restricts certain ranges #5979

Closed
ksritharan opened this issue Jan 24, 2022 · 1 comment · Fixed by #5980
Closed

SafeRange bug restricts certain ranges #5979

ksritharan opened this issue Jan 24, 2022 · 1 comment · Fixed by #5980

Comments

@ksritharan
Copy link
Contributor

Version

  • Phaser Version: 3.55.2
  • Operating system:
  • Browser:

Description

The condition startIndex + endIndex > len, in SafeRange.js is not necessary, and it restricts valid ranges.

/* src/utils/arrays/SafeRange.js */
var SafeRange = function (array, startIndex, endIndex, throwError)
{
    var len = array.length;

    if (startIndex < 0 ||
        startIndex > len ||
        startIndex >= endIndex ||
        endIndex > len ||
        startIndex + endIndex > len)
    {

The purpose of this function is to validate ranges in the form of [startIndex, endIndex). Where [ includes startIndex and ) excludes endIndex.
The fix will allow the following ranges to be validated:

[n, Array.length), n = {1, ..., Array.length-1}, Array.length > 1
[n, m), n = {1, ..., Array.length - 1}, m = {n+1, ..., Array.length}, Array.length > 1

For example,

var arr = [0, 1, 2, 3, 4];
console.log(Phaser.Utils.Array.SafeRange(arr, 0, arr.length)); // true, since [0, 5) is a valid range for arr that gives [0, 1, 2, 3, 4]
console.log(Phaser.Utils.Array.SafeRange(arr, 1, arr.length)); // false, but should be true since [1, 5) is a valid range for arr that gives [1, 2, 3, 4]
console.log(Phaser.Utils.Array.SafeRange(arr, 2, 4)); // false, but should be true since [2, 4) is a valid range for arr that gives [2, 3]

It restricts the following static methods in Phaser.Utils.Arrays: CountAllMatching, EachInRange, GetAll, GetFirst, RemoveBetween, SetAll.
The proposed fix will allow these methods to be able to operate on the aforementioned ranges.
For example, this code snippet demonstrates the change for GetAll (Phaser.GameObjects.Group::getMatching)
https://jsfiddle.net/z7w96k53/1/

  create() {
    var gameObjects = [];
    for (let i = 0; i < 5; i++) {
      var gameObject = new Phaser.GameObjects.Container(this);
      gameObject.setData('num', i);
      gameObjects.push(gameObject);
    }
    var group = new Phaser.GameObjects.Group(this, gameObjects);
    var newGroup = new NewGroup(this, gameObjects)
    console.log("Before");
    console.log(group.getMatching('active', true, 0).map(element => element.getData('num'))); // [0, 1, 2, 3, 4]
    console.log(group.getMatching('active', true, 1).map(element => element.getData('num'))); // []
    console.log(group.getMatching('active', true, 2, 4).map(element => element.getData('num'))); // []
    console.log("After");
    console.log(newGroup.getMatching('active', true, 0).map(element => element.getData('num')));  // [0, 1, 2, 3, 4]
    console.log(newGroup.getMatching('active', true, 1).map(element => element.getData('num'))); // [1, 2, 3, 4]
    console.log(newGroup.getMatching('active', true, 2, 4).map(element => element.getData('num'))); // [2, 3]
  }

Example Test Code

This snippet contains the unit tests: https://jsfiddle.net/7x5Lpse1/16/

@photonstorm
Copy link
Collaborator

Thanks for opening this issue, and for submitting a PR to fix it. We have merged your PR into the master branch and attributed the work to you in the Change Log. If you need to tweak the code for whatever reason please submit a new PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants