Skip to content

Commit 32674cb

Browse files
committed
feat: add assistant to import sound files from Minecraft installation
1 parent e6aaa2c commit 32674cb

10 files changed

Lines changed: 251 additions & 6 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/control_create/control_create.gml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,14 @@ function control_create() {
484484

485485
// Minecraft
486486
selected_tab_mc = 0
487+
488+
// Import sounds
489+
mc_default_path = string_copy(game_save_id, 0, string_last_pos("\\", string_copy(game_save_id, 1, string_length(game_save_id) - 1))) + ".minecraft\\";
490+
mc_install_path = mc_default_path;
491+
sound_import_asset_index_select = 0;
492+
sound_import_asset_index_count = -1;
493+
sound_import_asset_indexes = [];
494+
sound_import_menu_str = "";
487495

488496
// Schematic
489497
reset_schematic_export(0)

scripts/control_draw/control_draw.gml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1584,7 +1584,7 @@ function control_draw() {
15841584
}
15851585
}
15861586
if (!isplayer) show_menu_ext("settings", 59, 19, "Instrument|\\|" + str + condstr(customstr != "", "-|") + customstr + string_repeat("/|", insmenu) +
1587-
icon(icons.INSTRUMENTS)+"Instrument settings...|/|-|" + icon(icons.INFORMATION) + "Song info...|" + icon(icons.PROPERTIES) + "Song properties...|Song stats...|-|" + icon(icons.MIDI_INPUT) + "MIDI device manager|Ctrl+P$Preferences...")
1587+
icon(icons.INSTRUMENTS)+"Instrument settings...|Import sounds from Minecraft...|/|-|" + icon(icons.INFORMATION) + "Song info...|" + icon(icons.PROPERTIES) + "Song properties...|Song stats...|-|" + icon(icons.MIDI_INPUT) + "MIDI device manager|Ctrl+P$Preferences...")
15881588
else show_menu_ext("settingsp", 29, 19, icon(icons.INFORMATION) + "Song info...|" + "Song stats...|-|" + "Ctrl+P$Preferences...")
15891589
}
15901590
if (draw_tab("Help")) {
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
function draw_window_sound_import() {
2+
3+
// Boilerplate
4+
windowanim = 1;
5+
if (theme = 3) draw_set_alpha(windowalpha);
6+
var x1, y1, a, n;
7+
8+
// Window frame
9+
var width = 400;
10+
var height = 300;
11+
var startx = floor((rw - width) / 2);
12+
var starty = floor((rh - height) / 2) + windowoffset;
13+
var x1 = startx;
14+
var y1 = starty;
15+
draw_window(x1, y1, x1 + width, y1 + height);
16+
startx += 8;
17+
x1 = startx;
18+
y1 += 8;
19+
20+
// Title
21+
draw_theme_font(font_main_bold)
22+
draw_text_dynamic(x1, y1, ((language == 0) ? "Import sounds" : "TRANSLATE"));
23+
draw_theme_font(font_main)
24+
25+
// Info text
26+
x1 = startx;
27+
y1 += 20;
28+
draw_text_dynamic(
29+
x1, y1,
30+
"This assistant will help you extract sound files from your Minecraft:" + "\n" +
31+
"Java Edition installation." + "\n" +
32+
"Before continuing, ensure you have launched the version you want from " + "\n" +
33+
"the Minecraft Launcher!" + "\n" +
34+
"Click 'Find asset lists' to look for the available asset lists, select one" + "\n" +
35+
"from the list, and then click 'Import' to look for sounds!"
36+
)
37+
38+
// Minecraft installation path
39+
x1 = startx;
40+
y1 += 120;
41+
draw_text_dynamic(x1, y1, "1. Set your Minecraft installation path:");
42+
y1 += 20;
43+
draw_text_dynamic(x1, y1, mc_install_path);
44+
x1 += 300;
45+
46+
// Change button
47+
y1 -= 4
48+
if (draw_button2(x1, y1, 72, "Change", false, true)) {
49+
var fn = string(get_save_filename_ext("", "Select Minecraft installation directory", mc_install_path, "Minecraft installation directory"));
50+
if (fn != "") mc_install_path = filename_dir(fn);
51+
}
52+
x1 += 76;
53+
54+
// Use default button
55+
if (draw_button2(x1, y1, 72, "Use default", false, true)) {
56+
mc_install_path = mc_default_path
57+
}
58+
y1 += 4
59+
60+
// Import sounds button
61+
x1 = startx;
62+
y1 += 30;
63+
draw_text_dynamic(x1, y1, "2. Search for available asset lists:");
64+
y1 += 20;
65+
if (draw_button2(x1, y1, 112, "Find asset lists", false, true)) {
66+
sound_import_asset_indexes = find_asset_indexes();
67+
show_debug_message(sound_import_asset_indexes);
68+
69+
// Create menu
70+
for (var i = 0; i < array_length(sound_import_asset_indexes); i++) {
71+
sound_import_menu_str += check(sound_import_asset_index_select = i);
72+
sound_import_menu_str += sound_import_asset_indexes[i] + "|";
73+
}
74+
sound_import_menu_str = string_delete(sound_import_menu_str, string_length(sound_import_menu_str), 1)
75+
76+
}
77+
78+
// Asset index select menu
79+
x1 = startx;
80+
y1 += 30;
81+
draw_text_dynamic(x1, y1, "3. Select the asset list to copy sounds from:");
82+
y1 += 20;
83+
draw_area(x1, y1, x1 + 140, y1 + 16);
84+
if (draw_abutton(x1, y1)) {
85+
show_debug_message(sound_import_menu_str);
86+
menu = show_menu_ext("sound_import_asset_index", x1, y1, sound_import_menu_str);
87+
}
88+
x1 += 144
89+
90+
// Located sounds count
91+
if (sound_import_asset_index_count > -1) {
92+
draw_text_dynamic(x1, y1, sound_import_asset_index_count + " sounds located!")
93+
}
94+
95+
// Copy sounds button
96+
if (draw_button2(x1, y1, 112, "Get sounds", false, true)) {
97+
asset_indexes = load_asset_index(sound_import_asset_index_select, copy = true)
98+
}
99+
100+
x1 = startx;
101+
y1 += 30;
102+
103+
104+
105+
106+
}

scripts/draw_window_sound_import/draw_window_sound_import.yy

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

scripts/draw_windows/draw_windows.gml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function draw_windows() {
5050
case w_tempotapper: draw_window_tempo_tapper() break
5151
case w_setaccent: draw_window_set_accent() break
5252
case w_track_export: draw_window_track_export() break
53+
case w_sound_import: draw_window_sound_import() break
5354
}
5455
draw_set_alpha(1)
5556
}

scripts/macros/macros.gml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ function macros() {
9696
#macro w_tempotapper 44
9797
#macro w_setaccent 45
9898
#macro w_track_export 46
99+
#macro w_sound_import 47
99100

100101
#macro br "\r\n"
101102

scripts/menu_click/menu_click.gml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,12 @@ function menu_click(argument0) {
171171
ins -= floor((ins) / 26) // subtract the "More..." entries to get the instrument number
172172
if (sel < insoffset + 1) {instrument = instrument_list[| ins]; selected_vel = 100; selected_pan = 100; selected_pit = 0}
173173
if (sel = insoffset + 1) window = w_instruments
174-
if (sel = insoffset + 2) window = w_songinfoedit
175-
if (sel = insoffset + 3) window = w_properties
176-
if (sel = insoffset + 4) {selection_place(0) window = w_stats}
177-
if (sel = insoffset + 5) window = w_mididevices
178-
if (sel = insoffset + 6) window = w_preferences
174+
if (sel = insoffset + 2) window = w_sound_import
175+
if (sel = insoffset + 3) window = w_songinfoedit
176+
if (sel = insoffset + 4) window = w_properties
177+
if (sel = insoffset + 5) {selection_place(0) window = w_stats}
178+
if (sel = insoffset + 6) window = w_mididevices
179+
if (sel = insoffset + 7) window = w_preferences
179180
break
180181
}
181182
case "help": {
@@ -390,6 +391,11 @@ function menu_click(argument0) {
390391
}
391392
break;
392393
}
394+
case "sound_import_asset_index": {
395+
sound_import_asset_index_select = sel;
396+
sound_import_asset_index_count = load_asset_index(sel, false);
397+
break;
398+
}
393399
}
394400
mouse_clear(mb_left)
395401
io_clear()
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
function find_asset_indexes() {
2+
3+
if (!directory_exists(mc_install_path)) {
4+
show_message("No Minecraft installation was found\nat the selected location!");
5+
return;
6+
}
7+
8+
var assets_dir = mc_install_path
9+
if (string_char_at(assets_dir, string_length(assets_dir)) != "\\") {
10+
assets_dir = assets_dir + "\\";
11+
}
12+
assets_dir = assets_dir + "assets\\";
13+
14+
// Search for .json files in the assets/ folder
15+
show_debug_message("Looking for index files at " + assets_dir);
16+
var asset_indexes = [];
17+
var file_name = file_find_first(assets_dir + "indexes\\*.json", 0);
18+
while (file_name != "") {
19+
array_push(asset_indexes, string_replace(file_name, ".json", ""));
20+
show_debug_message(file_name);
21+
file_name = file_find_next();
22+
}
23+
file_find_close();
24+
25+
return asset_indexes;
26+
}
27+
28+
29+
function load_asset_index(index_name, copy = false) {
30+
31+
// Loads an asset index.
32+
33+
// Open and parse asset index
34+
var asset_index_path = assets_dir + "indexes\\" + "16.json"
35+
if (!file_exists(asset_index_path)) {
36+
show_message("The file for the specified asset index could not be found!")
37+
return;
38+
}
39+
var file_buffer = buffer_load(asset_index_path);
40+
var content = buffer_read(file_buffer, buffer_string);
41+
buffer_delete(file_buffer);
42+
43+
// Get objects in asset index
44+
var json = json_parse(content);
45+
var objects = json[$ "objects"];
46+
47+
var count = 0;
48+
var keys = variable_struct_get_names(objects);
49+
50+
var sounds_mc_subdir = sounds_directory + "minecraft";
51+
52+
if (directory_exists_lib(sounds_mc_subdir)) {
53+
if (!message_yesnocancel("An existing folder with imported Minecraft sounds has been found in your Sounds folder. Would you like to replace it?", "Warning")) {
54+
return;
55+
} else {
56+
directory_delete_lib(sounds_mc_subdir);
57+
}
58+
}
59+
60+
for (var i = array_length(keys) - 1; i >= 0; --i) {
61+
var key = keys[i];
62+
var value = objects[$ key];
63+
64+
if (string_count("minecraft/sounds/", key) == 0) {
65+
continue;
66+
}
67+
68+
// Skip music and records
69+
if (string_count("music/", key) > 0 || string_count("records/", key) > 0) {
70+
continue;
71+
}
72+
73+
var hash = value[$ "hash"];
74+
//show_debug_message(key + " " + hash);
75+
76+
if (copy) {
77+
var src = assets_dir + "objects\\" + string_copy(hash, 1, 2) + "\\" + hash;
78+
var dst = sounds_mc_subdir + string_replace(key, "minecraft/sounds/", "");
79+
80+
if (!file_exists_lib(src)) {
81+
continue;
82+
}
83+
if (!directory_exists_lib(filename_dir(dst))) {
84+
directory_create_lib(filename_dir(dst))
85+
}
86+
files_copy_lib(src, dst);
87+
}
88+
count++;
89+
90+
//show_debug_message(src);
91+
//show_debug_message(dst);
92+
}
93+
94+
return count;
95+
}
96+
97+

scripts/sound_import/sound_import.yy

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

0 commit comments

Comments
 (0)