X-Git-Url: https://plomlompom.com/repos/feed.xml?a=blobdiff_plain;ds=inline;f=src%2Fserver%2Fio.c;h=317c088b1148bb0475e71b32b1599ef0d128f90b;hb=891ba8fbca53d920f6b3704827fa6b8aee737de4;hp=b2d8005a7a75b22788081e9cf9639ffeed8ceba1;hpb=1452d43c6d7c89219cda91362da53ac8e4acb887;p=plomrogue diff --git a/src/server/io.c b/src/server/io.c index b2d8005..317c088 100644 --- a/src/server/io.c +++ b/src/server/io.c @@ -6,12 +6,12 @@ #include /* PIPE_BUF */ #include /* size_t, NULL */ #include /* uint8_t, uint16_t, uint32_t */ -#include /* defines EOF, FILE, sprintf() */ +#include /* defines EOF, FILE, sprintf(), fprintf() */ #include /* free() */ -#include /* strlen(), memcpy(), memset() */ +#include /* strlen(), memcpy(), memset(), strchr() */ #include /* time_t */ #include /* time(), nanosleep() */ -#include "../common/readwrite.h" /* try_fopen(), try_fclose_unlink_rename(), +#include "../common/readwrite.h" /* atomic_write_start(), atomic_write_finish(), * try_fwrite(), try_fputc(), try_fgetc() */ #include "../common/rexit.h" /* exit_trouble() */ @@ -20,13 +20,16 @@ #include "field_of_view.h" /* VISIBLE */ #include "hardcoded_strings.h" /* s */ #include "map.h" /* yx_to_map_pos() */ -#include "things.h" /* Thing, ThingType, get_thing_type(), get_player() */ +#include "things.h" /* Thing, ThingType, ThingAction, get_thing_type(), + * get_player() + */ #include "world.h" /* global world */ /* Write to "file" god commands (one per line) to recreate thing "t". */ static void write_key_value(FILE * file, char * key, uint32_t value); +static void write_key_string(FILE * file, char * key, char * string); /* Write to "file" \n-delimited line of "key" + space + "value" as string. */ static void write_thing(FILE * file, struct Thing * t); @@ -72,7 +75,7 @@ static void write_key_value(FILE * file, char * key, uint32_t value) try_fwrite(key, strlen(key), 1, file, f_name); try_fputc(' ', file, f_name); char * line = try_malloc(11, f_name); - exit_trouble(-1 == sprintf(line, "%u", value), f_name, "sprintf()"); + exit_trouble(-1 == sprintf(line, "%u", value), f_name, s[S_FCN_SPRINTF]); try_fwrite(line, strlen(line), 1, file, f_name); free(line); try_fputc('\n', file, f_name); @@ -80,6 +83,26 @@ static void write_key_value(FILE * file, char * key, uint32_t value) +static void write_key_string(FILE * file, char * key, char * string) +{ + char * f_name = "write_key_string()"; + try_fwrite(key, strlen(key), 1, file, f_name); + try_fputc(' ', file, f_name); + uint8_t contains_space = NULL != strchr(string, ' '); + if (contains_space) + { + try_fputc('\'', file, f_name); + } + try_fwrite(string, strlen(string), 1, file, f_name); + if (contains_space) + { + try_fputc('\'', file, f_name); + } + try_fputc('\n', file, f_name); +} + + + static void write_thing(FILE * file, struct Thing * t) { char * f_name = "write_thing()"; @@ -88,17 +111,17 @@ static void write_thing(FILE * file, struct Thing * t) { write_thing(file, o); } - write_key_value(file, s[CMD_THING], t->id); - write_key_value(file, s[CMD_TYPE], t->type); - write_key_value(file, s[CMD_POS_Y], t->pos.y); - write_key_value(file, s[CMD_POS_X], t->pos.x); - write_key_value(file, s[CMD_COMMAND], t->command); - write_key_value(file, s[CMD_ARGUMENT], t->arg); - write_key_value(file, s[CMD_PROGRESS], t->progress); - write_key_value(file, s[CMD_LIFEPOINTS], t->lifepoints); + write_key_value(file, s[S_CMD_THING], t->id); + write_key_value(file, s[S_CMD_T_TYPE], t->type); + write_key_value(file, s[S_CMD_T_POSY], t->pos.y); + write_key_value(file, s[S_CMD_T_POSX], t->pos.x); + write_key_value(file, s[S_CMD_T_COMMAND], t->command); + write_key_value(file, s[S_CMD_T_ARGUMENT], t->arg); + write_key_value(file, s[S_CMD_T_PROGRESS], t->progress); + write_key_value(file, s[S_CMD_T_HP], t->lifepoints); for (o = t->owns; o; o = o->next) { - write_key_value(file, s[CMD_CARRIES], o->id); + write_key_value(file, s[S_CMD_T_CARRIES], o->id); } try_fputc('\n', file, f_name); } @@ -180,9 +203,8 @@ static void read_file_into_queue() static void update_worldstate_file() { char * f_name = "update_worldstate_file()"; - char path_tmp[strlen(s[PATH_WORLDSTATE]) + strlen(s[PATH_SUFFIX_TMP]) + 1]; - sprintf(path_tmp, "%s%s", s[PATH_WORLDSTATE], s[PATH_SUFFIX_TMP]); - FILE * file = try_fopen(path_tmp, "w", f_name); + char * path_tmp; + FILE * file = atomic_write_start(s[S_PATH_WORLDSTATE], &path_tmp); struct Thing * player = get_player(); write_value_as_line(world.turn, file); write_value_as_line(player->lifepoints, file); @@ -195,9 +217,9 @@ static void update_worldstate_file() { try_fwrite(world.log, strlen(world.log), 1, file, f_name); } - try_fclose_unlink_rename(file, path_tmp, s[PATH_WORLDSTATE], f_name); + atomic_write_finish(file, s[S_PATH_WORLDSTATE], path_tmp); set_cleanup_flag(CLEANUP_WORLDSTATE); - char * dot = ".\n";; + char * dot = ".\n"; try_fwrite(dot, strlen(dot), 1, world.file_out, f_name); fflush(world.file_out); } @@ -208,7 +230,7 @@ static void write_value_as_line(uint32_t value, FILE * file) { char * f_name = "write_value_as_line()"; char write_buf[12]; /* Holds 10 digits of uint32_t maximum + \n + \0. */ - sprintf(write_buf, "%u\n", value); + exit_trouble(sprintf(write_buf, "%u\n",value) < 0, f_name,s[S_FCN_SPRINTF]); try_fwrite(write_buf, strlen(write_buf), 1, file, f_name); } @@ -248,7 +270,7 @@ static char * build_visible_map(struct Thing * player) memset(visible_map, ' ', map_size); if (player->fov_map) /* May fail if player thing was created / positioned */ { /* by god command after turning off FOV building. */ - uint16_t pos_i; + uint32_t pos_i; for (pos_i = 0; pos_i < map_size; pos_i++) { if (player->fov_map[pos_i] & VISIBLE) @@ -305,10 +327,10 @@ extern char * io_round() { return get_message_from_queue(); } - if (world.turn != world.last_update_turn) + if (world.do_update) { update_worldstate_file(); - world.last_update_turn = world.turn; + world.do_update = 0; } read_file_into_queue(); if (world.queue_size && '\0' != world.queue[world.queue_size - 1]) @@ -328,19 +350,46 @@ extern char * io_round() extern void save_world() { char * f_name = "save_world()"; - char * path = s[PATH_SAVE]; - FILE * file = try_fopen(path, "w", f_name); - write_key_value(file, s[CMD_DO_FOV], 0); + char * path_tmp; + FILE * file = atomic_write_start(s[S_PATH_SAVE], &path_tmp); + write_key_value(file, s[S_CMD_MAPLENGTH], world.map.length); + write_key_value(file, s[S_CMD_PLAYTYPE], world.player_type); + try_fputc('\n', file, f_name); + struct ThingAction * ta; + for (ta = world.thing_actions; ta; ta = ta->next) + { + write_key_value(file, s[S_CMD_THINGACTION], ta->id); + write_key_value(file, s[S_CMD_TA_EFFORT], ta->effort); + write_key_string(file, s[S_CMD_TA_NAME], ta->name); + try_fputc('\n', file, f_name); + } + struct ThingType * tt; + for (tt = world.thing_types; tt; tt = tt->next) + { + write_key_value(file, s[S_CMD_THINGTYPE], tt->id); + write_key_value(file, s[S_CMD_TT_STARTN], tt->start_n); + write_key_value(file, s[S_CMD_TT_HP], tt->lifepoints); + int test = fprintf(file, "%s %c\n", s[S_CMD_TT_SYMB], tt->char_on_map); + exit_trouble(test < 0, f_name, "fprintf()"); + write_key_string(file, s[S_CMD_TT_NAME], tt->name); + write_key_value(file, s[S_CMD_TT_CONSUM], tt->consumable); + try_fputc('\n', file, f_name); + } + for (tt = world.thing_types; tt; tt = tt->next) + { + write_key_value(file, s[S_CMD_THINGTYPE], tt->id); + write_key_value(file, s[S_CMD_TT_CORPS], tt->corpse_id); + } try_fputc('\n', file, f_name); - write_key_value(file, s[CMD_SEED_MAP], world.seed_map); - write_key_value(file, s[CMD_SEED_RAND], world.seed); - write_key_value(file, s[CMD_TURN], world.turn); + write_key_value(file, s[S_CMD_SEED_MAP], world.seed_map); + write_key_value(file, s[S_CMD_SEED_RAND], world.seed); + write_key_value(file, s[S_CMD_TURN], world.turn); try_fputc('\n', file, f_name); struct Thing * t; for (t = world.things; t; t = t->next) { write_thing(file, t); } - write_key_value(file, s[CMD_DO_FOV], 1); - try_fclose(file, f_name); + write_key_value(file, s[S_CMD_WORLD_ACTIVE], 1); + atomic_write_finish(file, s[S_PATH_SAVE], path_tmp); }