diff --git a/command.c b/command.c index b3f482472b..4a63b47616 100644 --- a/command.c +++ b/command.c @@ -1471,9 +1471,10 @@ static void command_event_save_state(const char *path, settings_t *settings = config_get_ptr(); char buf[PATH_MAX_LENGTH] = {0}; + /* if a save state already exists rename it to .last before saving + * so it can be recovered */ if (path_file_exists(path)) { - /* TODO: Fence with a setting */ strlcpy(buf, path, sizeof(buf)); snprintf(buf, sizeof(buf), "%s", path); path_remove_extension(buf); @@ -1482,8 +1483,8 @@ static void command_event_save_state(const char *path, if (!content_rename_state(path, buf)) { snprintf(s, len, "%s \"%s\".", - "Failed to save undo information\n", - buf); + msg_hash_to_str(MSG_FAILED_TO_SAVE_UNDO), + path); return; } } @@ -1517,9 +1518,11 @@ static void command_event_load_state(const char *path, char *s, size_t len, bool settings_t *settings = config_get_ptr(); char buf[PATH_MAX_LENGTH] = {0}; + /* save a state before loading (unless it's an undo operation already) + * so the state can be recovered + */ if (!undo) { - /* TODO: Fence with a setting */ strlcpy(buf, path, sizeof(buf)); snprintf(buf, sizeof(buf), "%s", path); path_remove_extension(buf); @@ -1528,8 +1531,8 @@ static void command_event_load_state(const char *path, char *s, size_t len, bool if (!content_save_state(buf)) { snprintf(s, len, "%s \"%s\".", - "Failed to save undo information\n", - buf); + msg_hash_to_str(MSG_FAILED_TO_SAVE_UNDO), + path); return; } } @@ -1586,13 +1589,27 @@ static void command_event_main_state(unsigned cmd) strlcpy(buf, path, sizeof(buf)); path_remove_extension(buf); snprintf(buf, sizeof(buf), "%s.undo", buf); - command_event_load_state(buf, msg, sizeof(msg), true); + + if (path_file_exists(buf)) + command_event_load_state(buf, msg, sizeof(msg), true); + else + { + snprintf(msg, sizeof(msg), "%s.", + msg_hash_to_str(MSG_FAILED_TO_LOAD_UNDO)); + } break; case CMD_EVENT_UNDO_SAVE_STATE: strlcpy(buf, path, sizeof(buf)); path_remove_extension(buf); snprintf(buf, sizeof(buf), "%s.last", buf); - command_event_load_state(buf, msg, sizeof(msg), true); + + if (path_file_exists(buf)) + command_event_load_state(buf, msg, sizeof(msg), true); + else + { + snprintf(msg, sizeof(msg), "%s.", + msg_hash_to_str(MSG_FAILED_TO_LOAD_UNDO)); + } break; } } diff --git a/intl/msg_hash_us.c b/intl/msg_hash_us.c index 2a270639f2..5c331af928 100644 --- a/intl/msg_hash_us.c +++ b/intl/msg_hash_us.c @@ -115,6 +115,10 @@ const char *msg_hash_to_str_us(uint32_t hash) return "Failed to load state from"; case MSG_FAILED_TO_SAVE_STATE_TO: return "Failed to save state to"; + case MSG_FAILED_TO_LOAD_UNDO: + return "No undo state found"; + case MSG_FAILED_TO_SAVE_UNDO: + return "Failed to save undo information"; case MSG_FAILED_TO_SAVE_SRAM: return "Failed to save SRAM"; case MSG_STATE_SIZE: diff --git a/msg_hash.h b/msg_hash.h index 5d5d7fe692..03a11f6b4d 100644 --- a/msg_hash.h +++ b/msg_hash.h @@ -125,6 +125,9 @@ #define MSG_CORE_DOES_NOT_SUPPORT_SAVESTATES 0xd50adf46U #define MSG_FAILED_TO_LOAD_STATE 0x91f348ebU +#define MSG_FAILED_TO_LOAD_UNDO 0xb6e2fc55U +#define MSG_FAILED_TO_SAVE_UNDO 0xf2e29478U + #define MSG_RESET 0x10474288U #define MSG_AUDIO_MUTED 0xfa0c3bd5U