diff --git a/src/game/savegame/savegame_bson.c b/src/game/savegame/savegame_bson.c index 02316e88f..ddb8f8211 100644 --- a/src/game/savegame/savegame_bson.c +++ b/src/game/savegame/savegame_bson.c @@ -812,16 +812,13 @@ static bool SaveGame_BSON_LoadCurrentMusic(struct json_object_s *music_obj) } int16_t current_track = json_object_get_int(music_obj, "current_track", -1); - int32_t timestamp_arr[2]; - timestamp_arr[0] = json_object_get_int(music_obj, "timestamp1", 0); - timestamp_arr[1] = json_object_get_int(music_obj, "timestamp2", 0); - int64_t *timestamp = (int64_t *)timestamp_arr; + int64_t timestamp = json_object_get_int64(music_obj, "timestamp", -1); if (current_track) { Music_Play(current_track); - if (!Music_SeekTimestamp(current_track, *timestamp)) { + if (!Music_SeekTimestamp(current_track, timestamp)) { LOG_WARNING( "Could not load current track %d at timestamp %d.", - current_track, *timestamp); + current_track, timestamp); } } @@ -1146,9 +1143,7 @@ static struct json_object_s *SaveGame_BSON_DumpCurrentMusic(void) if (Music_CurrentTrack()) { timestamp = Music_GetTimestamp(Music_CurrentTrack()); } - int32_t *timestamp_arr = (int32_t *)×tamp; - json_object_append_int(current_music_obj, "timestamp1", timestamp_arr[0]); - json_object_append_int(current_music_obj, "timestamp2", timestamp_arr[1]); + json_object_append_int64(current_music_obj, "timestamp", timestamp); return current_music_obj; } diff --git a/src/json/json_base.c b/src/json/json_base.c index a9cc9ab9e..160ec98d7 100644 --- a/src/json/json_base.c +++ b/src/json/json_base.c @@ -68,6 +68,17 @@ struct json_number_s *json_number_new_int(int number) return elem; } +struct json_number_s *json_number_new_int64(int64_t number) +{ + size_t size = snprintf(NULL, 0, "%" PRId64, number) + 1; + char *buf = Memory_Alloc(size); + sprintf(buf, "%" PRId64, number); + struct json_number_s *elem = Memory_Alloc(sizeof(struct json_number_s)); + elem->number = buf; + elem->number_size = strlen(buf); + return elem; +} + struct json_number_s *json_number_new_double(double number) { size_t size = snprintf(NULL, 0, "%f", number) + 1; @@ -317,6 +328,13 @@ void json_object_append_int( obj, key, json_value_from_number(json_number_new_int(number))); } +void json_object_append_int64( + struct json_object_s *obj, const char *key, int64_t number) +{ + json_object_append( + obj, key, json_value_from_number(json_number_new_int64(number))); +} + void json_object_append_double( struct json_object_s *obj, const char *key, double number) { @@ -402,6 +420,17 @@ int json_object_get_int(struct json_object_s *obj, const char *key, int d) return d; } +int64_t json_object_get_int64( + struct json_object_s *obj, const char *key, int64_t d) +{ + struct json_value_s *value = json_object_get_value(obj, key); + struct json_number_s *num = json_value_as_number(value); + if (num) { + return strtoll(num->number, NULL, 10); + } + return d; +} + double json_object_get_double( struct json_object_s *obj, const char *key, double d) { diff --git a/src/json/json_base.h b/src/json/json_base.h index 7354441d8..82573ad59 100644 --- a/src/json/json_base.h +++ b/src/json/json_base.h @@ -81,6 +81,7 @@ struct json_value_ex_s { // numbers struct json_number_s *json_number_new_int(int number); +struct json_number_s *json_number_new_int64(int64_t number); struct json_number_s *json_number_new_double(double number); void json_number_free(struct json_number_s *num); @@ -126,6 +127,8 @@ void json_object_append( void json_object_append_bool(struct json_object_s *obj, const char *key, int b); void json_object_append_int( struct json_object_s *obj, const char *key, int number); +void json_object_append_int64( + struct json_object_s *obj, const char *key, int64_t number); void json_object_append_double( struct json_object_s *obj, const char *key, double number); void json_object_append_string( @@ -141,6 +144,8 @@ struct json_value_s *json_object_get_value( struct json_object_s *obj, const char *key); int json_object_get_bool(struct json_object_s *obj, const char *key, int d); int json_object_get_int(struct json_object_s *obj, const char *key, int d); +int64_t json_object_get_int64( + struct json_object_s *obj, const char *key, int64_t d); double json_object_get_double( struct json_object_s *obj, const char *key, double d); const char *json_object_get_string(