Skip to content

Commit

Permalink
savegame: add 64 bit int support
Browse files Browse the repository at this point in the history
  • Loading branch information
walkawayy committed Aug 4, 2023
1 parent e7f5afc commit 18665dd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/game/savegame/savegame_bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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 *)&timestamp;
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;
}
Expand Down
29 changes: 29 additions & 0 deletions src/json/json_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
5 changes: 5 additions & 0 deletions src/json/json_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 18665dd

Please sign in to comment.