Skip to content

Commit d8af355

Browse files
authored
Merge pull request #117 from HielkeMinecraft/auto-updater
Auto-updater/Download new versions automatically
2 parents 062a262 + 279132a commit d8af355

16 files changed

+207
-33
lines changed

Minecraft Note Block Studio.yyp

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// check_updates()
2+
// Handles the update checking
3+
// update values:
4+
// -1: unable to check for update
5+
// 1: update found
6+
// 2: up to date
7+
8+
if (async_load[? "id"] = update_http) {
9+
update_http = -1
10+
if (async_load[? "http_status"] = 200) {
11+
var res = async_load[? "result"];
12+
if (is_string(res)) {
13+
res = json_decode(res)
14+
if(res[?"tag_name"] != undefined){
15+
var newVersion = string_replace(res[?"tag_name"],"v","")
16+
if (string_count(".", newVersion) = 2) {
17+
if (newVersion = version) {
18+
update = 2
19+
} else {
20+
if (question("Version " + newVersion + " is available! Do you want to download it?", "Update available!")) {
21+
update_download = http_get_file("https://github.com/HielkeMinecraft/OpenNoteBlockStudio/releases/latest/download/Minecraft.Note.Block.Studio.exe", update_file)
22+
update = 4
23+
} else {
24+
update = 1
25+
}
26+
}
27+
}
28+
}else
29+
update = -1
30+
}else
31+
update = -1
32+
} else
33+
update = -1
34+
}

scripts/check_updates/check_updates.yy

Lines changed: 8 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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,24 @@ save_version = nbs_version
333333
load_settings()
334334
change_theme()
335335
if (show_welcome) window = w_greeting
336+
337+
// Updates
336338
if (check_update)
337339
update_http = http_get("https://api.github.com/repos/HielkeMinecraft/OpenNoteBlockStudio/releases/latest")
338340
else
339341
update_http = -1
342+
update_download = -1
343+
downloaded_size = 0
344+
total_size = -1
340345
if (file_exists_lib(settings_file) && vers != version) {
341346
window = w_update
342347
update = 3
343348
}
344-
log("Startup OK")
349+
350+
// Delete old installer
351+
if (file_exists_lib(update_file)) {
352+
files_delete_lib(update_file)
353+
}
345354

346355
// Auto-recovery
347356
if (file_exists_lib(backup_file)) {
@@ -354,4 +363,6 @@ if (file_exists_lib(backup_file)) {
354363
if (parameter_count() > 0) {
355364
filename = parameter_string(1)
356365
if (filename != "") load_song(filename)
357-
}
366+
}
367+
368+
log("Startup OK")

scripts/control_draw/control_draw.gml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,13 @@ if (mouse_check_button_released(mb_left)) {
13231323
}
13241324
if (window = w_releasemouse && !mouse_check_button(mb_left)) window = 0
13251325
draw_windows()
1326+
1327+
// Draw update progress bar
1328+
if (update == 4) {
1329+
window = -1
1330+
draw_downloadprogress("Update", "Downloading update...", downloaded_size, total_size)
1331+
}
1332+
13261333
window_set_cursor(curs)
13271334
mouse_xprev = mouse_x
13281335
mouse_yprev = mouse_y
Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,5 @@
11
// control_http()
2-
// Handles the update checking
3-
// update values:
4-
// -1: unable to check for update
5-
// 1: update found
6-
// 2: up to date
2+
// Handles the check for updates, then attempts to download it if one is available
73

8-
if (async_load[? "id"] = update_http) {
9-
update_http = -1
10-
if (async_load[? "http_status"] = 200) {
11-
var res = async_load[? "result"];
12-
if (is_string(res)) {
13-
res = json_decode(res)
14-
if(res[?"tag_name"] != undefined){
15-
var newVersion = string_replace(res[?"tag_name"],"v","")
16-
if (string_count(".", newVersion) = 2) {
17-
if (newVersion = version) {
18-
update = 2
19-
} else {
20-
if (question("Version " + newVersion + " is available! Do you want to download it?", "Update available!")) open_url(link_download)
21-
update = 1
22-
}
23-
}
24-
}else
25-
update = -1
26-
}else
27-
update = -1
28-
} else
29-
update = -1
30-
}
4+
check_updates()
5+
get_update()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// draw_downloadprogress(caption, desc, downloaded_size, total_size)
2+
var caption, desc, done, total, done_mb, total_mb, done_text, total_text, text, percent, x1, y1;
3+
caption = argument0
4+
desc = argument1
5+
done = argument2
6+
total = argument3
7+
percent = done/total
8+
done_mb = done/power(1024, 2)
9+
total_mb = total/power(1024, 2)
10+
x1 = floor(window_width / 2 - 150)
11+
y1 = floor(window_height / 2 - 50)
12+
draw_theme_color()
13+
draw_window(x1, y1, x1 + 300, y1 + 100)
14+
draw_set_font(fnt_mainbold)
15+
draw_text(x1 + 16, y1 + 16, caption)
16+
draw_set_font(fnt_main)
17+
draw_set_halign(fa_center)
18+
draw_text(floor(window_width / 2), y1 + 40, desc)
19+
draw_set_color(10512464)
20+
draw_rectangle(x1 + 30, y1 + 60, x1 + 30 + percent * 240, y1 + 80, 0)
21+
draw_theme_color()
22+
draw_rectangle(x1 + 30, y1 + 60, x1 + 270, y1 + 80, 1)
23+
if (percent > 0.5) draw_set_color(c_white)
24+
if (total <= 0) {
25+
done_text = "-.--"
26+
total_text = "-.--"
27+
} else {
28+
done_text = string_format(done_mb, 0, 2)
29+
total_text = string_format(total_mb, 0, 2)
30+
}
31+
text = done_text + "/" + total_text + " MB (" + string(round(percent * 100)) + "%)"
32+
draw_text(floor(window_width / 2), y1 + 65, text)
33+
draw_set_halign(fa_left)

scripts/draw_downloadprogress/draw_downloadprogress.yy

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// file_get_size(path)
2+
// Returns the size of a file, in bytes.
3+
4+
var path, file, size
5+
path = argument0
6+
if (file_exists(path)) {
7+
file = file_bin_open(path, 0)
8+
size = file_bin_size(file)
9+
file_bin_close(file)
10+
return size
11+
}
12+
return 0

scripts/file_get_size/file_get_size.yy

Lines changed: 8 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)