Skip to content

Commit 97335af

Browse files
committed
bugfix: Fix a stack overflow when pasting large selection to a existing area
- This also improve the speed when pasting large selection to a existing area; - This doesn't happen when the copy-on-write option is turned off, but it will affect all arrays so I keep it on; - Move the code to check and extend the `selection_exists` array to separate functions.
1 parent 32fa0a1 commit 97335af

15 files changed

Lines changed: 102 additions & 167 deletions

File tree

Minecraft Note Block Studio.yyp

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/add_block_select/add_block_select.gml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ function add_block_select(argument0, argument1, argument2, argument3, argument4,
3333

3434
// Add block
3535
song_exists[xx, yy] = 1
36-
song_ins[xx, yy] = ins
37-
song_key[xx, yy] = key
38-
song_vel[xx, yy] = vel
39-
song_pan[xx, yy] = pan
40-
song_pit[xx, yy] = pit
41-
song_played[xx, yy] = 0
42-
song_added[xx, yy] = 0
43-
colamount[xx] += 1
44-
rowamount[yy] += 1
36+
song_ins[@ xx, yy] = ins
37+
song_key[@ xx, yy] = key
38+
song_vel[@ xx, yy] = vel
39+
song_pan[@ xx, yy] = pan
40+
song_pit[@ xx, yy] = pit
41+
song_played[@ xx, yy] = 0
42+
song_added[@ xx, yy] = 0
43+
colamount[@ xx] += 1
44+
rowamount[@ yy] += 1
4545
ins.num_blocks++
4646
if (key < 33 || key > 57) block_outside += 1
4747
if (ins.user) block_custom += 1
4848
if (pit != 0) block_pitched += 1
4949
totalblocks += 1
5050

51-
if (colfirst[xx] = -1 || yy < colfirst[xx]) colfirst[xx] = yy
52-
if (yy > collast[xx]) collast[xx] = yy
51+
if (colfirst[xx] = -1 || yy < colfirst[xx]) colfirst[@ xx] = yy
52+
if (yy > collast[xx]) collast[@ xx] = yy
5353

5454
if (xx >= enda) enda = xx
5555
if (yy >= endb) endb = yy

scripts/pattern_import/pattern_import.gml

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,8 @@ function pattern_import() {
4141
if check_custom_instrument(temp_str) = 0 {
4242
// show_debug_message("selection_code " + string(selection_copied))
4343
selection_copied = temp_str
44-
if (pat_length > selection_arraylength) { // New length
45-
for (a = selection_arraylength + 1; a <= pat_length; a += 1) {
46-
selection_colfirst[a] = -1
47-
selection_collast[a] = -1
48-
for (b = 0; b <= selection_arrayheight; b += 1) {
49-
selection_exists[a, b] = 0
50-
}
51-
}
52-
selection_arraylength = pat_length
53-
}
54-
55-
if (pat_height > selection_arrayheight) { // New height
56-
for (a = 0; a <= selection_arraylength; a += 1) {
57-
for (b = selection_arrayheight + 1; b <= pat_height; b += 1) {
58-
selection_exists[a, b] = 0
59-
}
60-
}
61-
selection_arrayheight = pat_height
62-
}
44+
selection_extend_length(pat_length)
45+
selection_extend_height(pat_height)
6346

6447
for (a = 0; a < selection_l; a += 1) {
6548
temp_colfirst[a] = buffer_read_byte_signed()

scripts/remove_block_select/remove_block_select.gml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ function remove_block_select(argument0, argument1) {
55
yy = argument1
66

77
if (xx >= arraylength || yy >= arrayheight || song_exists[xx, yy] == 0) return 0
8-
song_exists[xx, yy] = 0
8+
song_exists[@ xx, yy] = 0
99
song_ins[xx, yy].num_blocks--
1010
if (song_key[xx, yy] < 33 || song_key[xx, yy] > 57) block_outside -= 1
1111
if (song_ins[xx, yy].user) block_custom -= 1
1212
if (song_pit[xx, yy] != 0) block_pitched -= 1
1313

14-
song_ins[xx, yy] = 0
15-
song_key[xx, yy] = 0
16-
song_vel[xx, yy] = 0
17-
song_pan[xx, yy] = 0
18-
song_pit[xx, yy] = 0
19-
song_played[xx, yy] = 0
20-
song_added[xx, yy] = 0
21-
colamount[xx] -= 1
22-
rowamount[yy] -= 1
14+
song_ins[@ xx, yy] = 0
15+
song_key[@ xx, yy] = 0
16+
song_vel[@ xx, yy] = 0
17+
song_pan[@ xx, yy] = 0
18+
song_pit[@ xx, yy] = 0
19+
song_played[@ xx, yy] = 0
20+
song_added[@ xx, yy] = 0
21+
colamount[@ xx] -= 1
22+
rowamount[@ yy] -= 1
2323
totalblocks -= 1
2424

2525

scripts/select_all/select_all.gml

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,10 @@ function select_all(argument0, argument1) {
1111
if (totalblocks = 0) return 0
1212
if (inst > -1 && !n && inst.num_blocks = 0)
1313
return 0
14-
if (enda > selection_arraylength) { // New length
15-
for (a = selection_arraylength + 1; a <= enda; a += 1) {
16-
selection_colfirst[a] = -1
17-
selection_collast[a] = -1
18-
for (b = 0; b <= selection_arrayheight; b += 1) {
19-
selection_exists[a, b] = 0
20-
}
21-
}
22-
selection_arraylength = enda
23-
}
24-
if (endb > selection_arrayheight) { // New height
25-
for (a = 0; a <= selection_arraylength; a += 1) {
26-
for (b = selection_arrayheight + 1; b <= endb; b += 1) {
27-
selection_exists[a, b] = 0
28-
}
29-
}
30-
selection_arrayheight = endb
31-
}
14+
15+
selection_extend_length()
16+
selection_extend_height()
17+
3218
for (a = 0; a <= enda; a += 1) {
3319
if (colamount[a] > 0) {
3420
for (b = colfirst[a]; b <= collast[a]; b += 1) {
@@ -55,6 +41,7 @@ function select_all(argument0, argument1) {
5541
}
5642
}
5743
}
44+
5845
selection_x = 0
5946
selection_y = 0
6047
selection_l = enda + 1

scripts/select_custom/select_custom.gml

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,8 @@ function select_custom() {
44
var a, b, str, ins, key, vel, pan, pit;
55
str = selection_code
66
if (selected > 0) selection_place(1)
7-
if (enda > selection_arraylength) { // New length
8-
for (a = selection_arraylength + 1; a <= enda; a += 1) {
9-
selection_colfirst[a] = -1
10-
selection_collast[a] = -1
11-
for (b = 0; b <= selection_arrayheight; b += 1) {
12-
selection_exists[a, b] = 0
13-
}
14-
}
15-
selection_arraylength = enda
16-
}
17-
if (endb > selection_arrayheight) { // New height
18-
for (a = 0 ;a <= selection_arraylength; a += 1) {
19-
for (b = selection_arrayheight + 1; b <= endb; b += 1) {
20-
selection_exists[a, b] = 0
21-
}
22-
}
23-
selection_arrayheight = endb
24-
}
7+
selection_extend_length()
8+
selection_extend_height()
259
for (a = 0; a <= enda; a += 1) {
2610
if (colamount[a] > 0) {
2711
for (b = colfirst[a]; b <= collast[a]; b += 1) {

scripts/select_outside/select_outside.gml

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,8 @@ function select_outside(argument0, argument1) {
44
var a, b, str, ins, key, vel, pan, pit;
55
str = selection_code
66
if (selected > 0) selection_place(1)
7-
if (enda > selection_arraylength) { // New length
8-
for (a = selection_arraylength + 1; a <= enda; a += 1) {
9-
selection_colfirst[a] = -1
10-
selection_collast[a] = -1
11-
for (b = 0; b <= selection_arrayheight; b += 1) {
12-
selection_exists[a, b] = 0
13-
}
14-
}
15-
selection_arraylength = enda
16-
}
17-
if (endb > selection_arrayheight) { // New height
18-
for (a = 0; a <= selection_arraylength; a += 1) {
19-
for (b = selection_arrayheight + 1; b <= endb; b += 1) {
20-
selection_exists[a, b] = 0
21-
}
22-
}
23-
selection_arrayheight = endb
24-
}
7+
selection_extend_length()
8+
selection_extend_height()
259
for (a = 0; a <= enda; a += 1) {
2610
if (colamount[a] > 0) {
2711
for (b = colfirst[a]; b <= collast[a]; b += 1) {

scripts/selection_add/selection_add.gml

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -60,45 +60,13 @@ function selection_add() {
6060
selection_y = y1
6161
selection_l = al
6262
selection_h = ah
63-
if (al > selection_arraylength) { // New length
64-
for (a = selection_arraylength + 1; a <= al; a += 1) {
65-
selection_colfirst[a] = -1
66-
selection_collast[a] = -1
67-
for (b = 0; b <= selection_arrayheight; b += 1) {
68-
selection_exists[a, b] = 0
69-
}
70-
}
71-
selection_arraylength = al
72-
}
73-
if (ah > selection_arrayheight) { // New height
74-
for (a = 0; a <= selection_arraylength; a += 1) {
75-
for (b = selection_arrayheight + 1; b <= ah; b += 1) {
76-
selection_exists[a, b] = 0
77-
}
78-
}
79-
selection_arrayheight = ah
80-
}
63+
selection_extend_length(al)
64+
selection_extend_height(ah)
8165
} else { // Add to existing
8266
al = selection_l + max(0, selection_x - x1) + max(0, x2 - (selection_x + selection_l))
8367
ah = selection_h + max(0, selection_y - y1) + max(0, y2 - (selection_y + selection_h))
84-
if (al > selection_arraylength) { // New length
85-
for (a = selection_arraylength + 1; a <= al; a += 1) {
86-
selection_colfirst[a] = -1
87-
selection_collast[a] = -1
88-
for (b = 0; b <= selection_arrayheight; b += 1) {
89-
selection_exists[a, b] = 0
90-
}
91-
}
92-
selection_arraylength = al
93-
}
94-
if (ah > selection_arrayheight) { // New height
95-
for (a = 0; a <= selection_arraylength; a += 1) {
96-
for (b = selection_arrayheight + 1; b <= ah; b += 1) {
97-
selection_exists[a, b] = 0
98-
}
99-
}
100-
selection_arrayheight = ah
101-
}
68+
selection_extend_length(al)
69+
selection_extend_height(ah)
10270
if (x1 < selection_x || y1 < selection_y) { // Move blocks
10371
var temp_colfirst, temp_collast, temp_exists, temp_ins, temp_key, temp_vel, temp_pan, temp_pit, temp_played;
10472
for (a = 0; a < selection_l; a += 1) {

scripts/selection_expand/selection_expand.gml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,7 @@ function selection_expand() {
44
str = selection_code
55
if (selected = 0) return 0
66
nw = selection_l * 2
7-
if (nw > selection_arraylength) { // New length
8-
for (a = selection_arraylength + 1; a <= nw; a += 1) {
9-
selection_colfirst[a] = -1
10-
selection_collast[a] = -1
11-
for (b = 0; b <= selection_arrayheight; b += 1) {
12-
selection_exists[a, b] = 0
13-
}
14-
}
15-
selection_arraylength = nw
16-
}
7+
selection_extend_length(nw)
178
for (a = 0; a < selection_l; a += 1) {
189
if (selection_colfirst[a] > -1) {
1910
for (b = selection_colfirst[a]; b <= selection_collast[a]; b += 1) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function selection_extend_height(new_height = undefined){
2+
if (is_undefined(new_height)) {
3+
new_height = endb
4+
}
5+
if (new_height > selection_arrayheight) { // New height
6+
for (var a = selection_arraylength; a >= 0; --a) {
7+
for (var b = new_height; b >= selection_arrayheight + 1; --b) {
8+
selection_exists[@ a, b] = 0
9+
}
10+
}
11+
selection_arrayheight = new_height
12+
}
13+
}

0 commit comments

Comments
 (0)