Skip to content

Commit da7e0a8

Browse files
committed
Re-add importing instruments from another song
Updated to be compatible with all NBS versions up to date, which no longer crashes the program.
1 parent dd96a16 commit da7e0a8

File tree

2 files changed

+70
-40
lines changed

2 files changed

+70
-40
lines changed

scripts/draw_window_instruments/draw_window_instruments.gml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ draw_window(x1, y1, x1 + 550, y1 + 350)
88
draw_set_font(fnt_mainbold)
99
draw_text(x1 + 10, y1 + 8, "Instrument Settings")
1010
draw_set_font(fnt_main)
11-
// draw_text(x1 + 12, y1 + 31, "These settings only apply to this song. To import the settings from another\nsong, click \"Import\". The sound files must be located in the \"Sounds\" folder.")
11+
draw_text(x1 + 12, y1 + 31, "These settings only apply to this song. To import the settings from another\nsong, click \"Import\". The sound files must be located in the \"Sounds\" folder.")
1212
draw_set_color(c_white)
1313
if(theme = 2) draw_set_color(c_dark)
1414
draw_rectangle(x1 + 12, y1 + 66, x1 + 535, y1 + 310, 0)
@@ -32,7 +32,7 @@ draw_text(x1 + 18 + 194, y1 + 70, "Sound")
3232
draw_window(x1 + 13, y1 + 67, x1 + 14 + 194, y1 + 67 + 20)
3333
popup_set_window(x1 + 13, y1 + 67, 194, 20, "The name of this instrument.")
3434
draw_text(x1 + 18, y1 + 70, "Name")
35-
// if (draw_button2(x1 + 12, y1 + 318, 80, "Import")) load_instruments("")
35+
if (draw_button2(x1 + 12, y1 + 318, 80, "Import")) load_instruments("")
3636
c = 0
3737
if (draw_button2(x1 + 256, y1 + 318, 80, "Add", user_instruments >= 18) && wmenu = 0) {
3838
changed = true
Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,99 @@
1-
/// @description load_instruments(filename)
2-
/// @function load_instruments
3-
/// @param filename
1+
// load_instruments(filename)
2+
// Loads custom instruments from a song and adds them to the current song.
43

5-
var fn, a, b, hei2;
4+
var fn, a, b, nbsver, height;
65
fn = argument0
76
if (fn = "") {
87
if (!directory_exists_lib(songfolder)) songfolder = songs_directory
98
fn = string(get_open_filename_ext("Note Block Songs (*.nbs)|*.nbs", "", songfolder, "Load instruments from song"))
109
}
1110
if (fn = "" || !file_exists_lib(fn)) return 0
12-
1311
buffer = buffer_import(fn)
14-
buffer_read_short()
15-
hei2 = buffer_read_short()
12+
13+
if (buffer_read_short() == 0) {
14+
nbsver = buffer_read_byte()
15+
} else {
16+
nbsver = 0
17+
}
18+
19+
// Future version
20+
if (nbsver > nbs_version) {
21+
message("Warning: You are opening an NBS file created in a later version of Note Block Studio.\nPlease save the song as a version " + string(nbs_version) + " file or lower via the Save Options menu.","Error")
22+
return -1
23+
}
24+
25+
// Header
26+
if (nbsver >= 1) buffer_read_byte() // Custom instrument index
27+
if (nbsver >= 3) buffer_read_short() // Song length
28+
height = buffer_read_short()
29+
buffer_read_string_int() // Song name
30+
buffer_read_string_int() // Soung author
31+
buffer_read_string_int() // Song original author
32+
buffer_read_string_int() // Song description
33+
buffer_read_short() // Tempo
34+
buffer_read_byte() // Autosave
35+
buffer_read_byte() // Autosave minutes
36+
buffer_read_byte() // Time signature
37+
buffer_read_int() // Minutes spent
38+
buffer_read_int() // Left-clicks
39+
buffer_read_int() // Right-clicks
40+
buffer_read_int() // Blocks added
41+
buffer_read_int() // Blocks removed
42+
buffer_read_string_int() // MIDI filename
43+
if (nbsver >= 4) {
44+
buffer_read_byte() // Loop
45+
buffer_read_byte() // Loop count
46+
buffer_read_short() // Loop start tick
47+
}
1648

17-
buffer_read_string_int()
18-
buffer_read_string_int()
19-
buffer_read_string_int()
20-
buffer_read_string_int()
21-
buffer_read_short()
22-
buffer_read_byte()
23-
buffer_read_byte()
24-
buffer_read_byte()
25-
a = buffer_read_int()
26-
a = buffer_read_int()
27-
a = buffer_read_int()
28-
a = buffer_read_int()
29-
a = buffer_read_int()
30-
a = buffer_read_string_int()
3149
// Note blocks
32-
while (1) {
50+
while (true) {
3351
a = buffer_read_short()
3452
if (a = 0) break
35-
while (1) {
53+
while (true) {
3654
a = buffer_read_short()
3755
if (a = 0) break
38-
repeat (2) buffer_read_byte()
56+
buffer_read_byte() // Instrument
57+
buffer_read_byte() // Key
58+
if (nbsver >= 4) {
59+
buffer_read_byte() // Velocity
60+
buffer_read_byte() // Pan
61+
buffer_read_short() // Pitch
62+
}
3963
}
4064
}
4165
if (buffer_is_eof()) {
42-
message("This file does not contain any custom instruments.", "Error")
43-
return 0
66+
message("This file does not contain any custom instruments.", "Error")
67+
return 0
4468
}
45-
//buffer_read_short()
46-
for (b = 0; b < hei2; b += 1) {
47-
buffer_read_string_int()
48-
buffer_read_byte()
69+
70+
// Layers
71+
for (b = 0; b < height; b++) {
72+
buffer_read_string_int() // Layer name
73+
if (nbsver >= 4) buffer_read_byte() // Layer lock
74+
buffer_read_byte() // Layer volume
75+
if (nbsver >= 2) buffer_read_byte() // Layer stereo
4976
}
5077
if (buffer_is_eof()) {
51-
message("This file does not contain any custom instruments.", "Error")
52-
return 0
78+
message("This file does not contain any custom instruments.", "Error")
79+
return 0
5380
}
81+
82+
// Instruments
5483
a = buffer_read_byte()
5584
if (a = 0) {
5685
message("This file does not contain any custom instruments.", "Error")
5786
return 0
5887
}
59-
for (b = 0; b < a; b += 1) {
60-
var name = buffer_read_string_int();
61-
var filename = buffer_read_string_int();
62-
var key = buffer_read_byte();
63-
var press = buffer_read_byte();
64-
var ins = new_instrument(name, filename, true, press, key);
88+
for (b = 0; b < a; b++) {
89+
var name = buffer_read_string_int()
90+
var filename = buffer_read_string_int()
91+
var key = buffer_read_byte()
92+
var press = buffer_read_byte()
93+
var ins = new_instrument(name, filename, true, press, key)
6594
with (ins)
6695
instrument_load()
6796
ds_list_add(instrument_list, ins)
6897
}
6998
changed = true
99+

0 commit comments

Comments
 (0)