Skip to content

Commit 14d54a7

Browse files
committed
Add a global auto-save preference/Implement auto-recovery system
- Song auto-save and auto-save time were not removed; rather, they're saved as the current preference set in the program (this way, opening songs in an older version will behave as the preference was set when the file was saved) - Auto-recovery works by saving a 'backup.nbs' file every minute, that gets deleted when a new song is created/loaded, and when the program is closed. If this file is present on startup (i.e. the program quit unexpectedly and the file wasn't deleted), a prompt asks the user if they want to recover the song.
1 parent 15cf718 commit 14d54a7

File tree

14 files changed

+119
-38
lines changed

14 files changed

+119
-38
lines changed

Minecraft Note Block Studio.yyp

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// backup_clear()
2+
// Deletes the backup file stored temporarily for auto-recovery
3+
4+
if (file_exists_lib(backup_file)) {
5+
files_delete_lib(backup_file)
6+
}

scripts/backup_clear/backup_clear.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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ icons_init()
3838
refreshrate = 0 //0 = 30fps, 1 = 60fps
3939
fade = 0
4040
rhval = 270
41+
autosave = 0
42+
autosavemins = 10
43+
tonextsave = 0
44+
backupmins = 1
45+
tonextbackup = 0
4146

4247
// File
4348
filename = ""
4449
changed = 0
4550
midifile = ""
46-
autosave = 0
47-
autosavemins = 10
48-
tonextsave = 0
4951
for (a = 0; a < 11; a += 1) {
5052
mididevice_instrument[a] = -1
5153
recent_song[a] = ""
@@ -342,6 +344,13 @@ if (file_exists_lib(data_directory + "settings.onbs") && vers != version) {
342344
}
343345
log("Startup OK")
344346

347+
// Auto-recovery
348+
if (file_exists_lib(backup_file)) {
349+
if (question("Minecraft Note Block Studio quit unexpectedly while you were working on a song. Do you want to recover your work?", "Auto-recovery")) {
350+
load_song(backup_file, true)
351+
}
352+
}
353+
345354
// Open song
346355
if (parameter_count() > 0) {
347356
filename = parameter_string(1)

scripts/control_draw/control_draw.gml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,21 @@ if (key_edit > -1) {
4949
}
5050
}
5151

52+
// Autosave
5253
if (autosave && filename_ext(filename) = ".nbs") {
5354
tonextsave -= 1 / room_speed / 60
5455
if (tonextsave <= 0) save_song(filename)
5556
}
5657

58+
// Auto-recovery
59+
if (totalblocks > 0) {
60+
tonextbackup -= 1 / room_speed / 60
61+
if (tonextbackup <= 0) {
62+
save_song(backup_file, true)
63+
tonextbackup = backupmins
64+
}
65+
}
66+
5767
if (theme = 0) window_background = 15790320
5868
if (theme = 1) window_background = 13160660
5969
if (theme = 2) window_background = c_dark

scripts/control_end/control_end.gml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33

44
confirm(1)
55
save_settings()
6+
backup_clear()

scripts/load_settings/load_settings.gml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ for (a = 0; a < 11; a += 1) {
1313
vers = ini_read_string("preferences", "last_version", version)
1414
check_update = ini_read_real( "preferences", "check_update", check_update)
1515
show_welcome = ini_read_real( "preferences", "show_welcome", show_welcome)
16+
autosave = ini_read_real( "preferences", "autosave", autosave)
17+
autosavemins = ini_read_real( "preferences", "autosavemins", autosavemins)
1618
theme = ini_read_real( "preferences", "theme", theme)
1719
refreshrate = ini_read_real( "preferences", "refresh_rate", refreshrate)
1820
songfolder = ini_read_string("preferences", "song_folder", songfolder)

scripts/load_song/load_song.gml

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
// load_song(fn)
2-
var fn, file_ext, f, str, stats, tstr, ca, cb, bstr, a, b, c, d, w, hei, byte1, byte2, song_first_custom_index, custom_index_diff
3-
fn = argument0
1+
// load_song(fn [, backup])
2+
var fn, backup, file_ext, f, str, stats, tstr, ca, cb, bstr, a, b, c, d, w, hei, byte1, byte2, song_first_custom_index, custom_index_diff
3+
fn = argument[0]
4+
backup = false
5+
if (argument_count > 1) {
6+
backup = argument[1]
7+
}
48
if (confirm() < 0) return 0
5-
if (fn = "") {
9+
if (!backup && fn = "") {
610
if (!directory_exists(songfolder)) songfolder = songs_directory
711
fn = string(get_open_filename_ext("Note Block Songs (*.nbs)|*.nbs|MIDI Sequences (*.mid)|*.mid;*.midi|Minecraft Schematics (*.schematic)|*.schematic", "", songfolder, "Load song"))
812
}
913
if (fn = "" || !file_exists_lib(fn)) return 0
14+
15+
// When not opening from auto-recovery, delete the backup file
16+
if (!backup) {
17+
backup_clear()
18+
}
1019
reset()
1120
file_ext = filename_ext(fn)
1221
if (file_ext = ".mid" || file_ext = ".midi") {
@@ -58,13 +67,10 @@ if (file_ext = ".nbs") {
5867
song_desc = buffer_read_string_int()
5968
// TEMPO
6069
tempo = median(0.25, floor((buffer_read_short() / 100) * 4) / 4, 30)
61-
// AUTOSAVE
62-
a = buffer_read_byte()
63-
autosave = median(0, a, 1)
64-
// AUTOSAVE MINUTES
65-
a = buffer_read_byte()
66-
autosavemins = median(0, a, 60)
67-
tonextsave = autosavemins
70+
// AUTOSAVE (deprecated)
71+
buffer_read_byte()
72+
// AUTOSAVE MINUTES (deprecated)
73+
buffer_read_byte()
6874
// TIME SIGNATURE
6975
a = buffer_read_byte()
7076
timesignature = median(2, a, 8)
@@ -178,9 +184,15 @@ if (file_ext = ".nbs") {
178184
if (question("This song uses custom instruments. However, some sounds could not be loaded:\n\n" + str+"\nMake sure that you have put the sounds in the \"Sounds\" folder. Open Instrument settings?", "Error")) window = w_instruments
179185
buffer_delete(buffer)
180186
}
181-
add_to_recent(fn)
182-
if (window != w_instruments) window = w_songinfo
183-
filename = fn
184-
changed = 0
187+
if (!backup) {
188+
add_to_recent(fn)
189+
if (window != w_instruments) window = w_songinfo
190+
filename = fn
191+
changed = 0
192+
}
193+
else {
194+
changed = 1
195+
}
196+
backup_clear()
185197
blocks_set_instruments()
186198
io_clear()

scripts/macros/macros.gml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#macro pattern_directory data_directory + "Patterns\\"
1515
#macro log_file file_directory + "log.txt"
1616
#macro temp_file file_directory + "tmp.file"
17+
#macro backup_file file_directory + "backup.nbs"
1718

1819
#macro h_stereoize 12
1920
#macro h_swaplayer 11

scripts/new_song/new_song.gml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
// new_song()
22
song_midi = ""
3-
if (confirm() > -1) reset()
3+
if (confirm() > -1) {
4+
reset()
5+
backup_clear()
6+
}

0 commit comments

Comments
 (0)