diff options
author | pauldubois <pauldubois@c06c8d41-db1a-0410-9941-cceddc491573> | 2008-03-23 09:42:58 +0000 |
---|---|---|
committer | pauldubois <pauldubois@c06c8d41-db1a-0410-9941-cceddc491573> | 2008-03-23 09:42:58 +0000 |
commit | 7a5387cbd6aa4682c7f2845bde065a9e0b828e23 (patch) | |
tree | 995b883e6d67e8c9011bb02133b83ce65dcfa47a /crawl-ref/source | |
parent | 922cba628d3d6377574a90c7a817c10d84ed7f85 (diff) | |
download | crawl-ref-7a5387cbd6aa4682c7f2845bde065a9e0b828e23.tar.gz crawl-ref-7a5387cbd6aa4682c7f2845bde065a9e0b828e23.zip |
This was originally going to be a small refactor of stash.cc before
getting into stash/item finding, but it ended up big. Removed the
read/writeThing API in favor of the marshall/unmarshallThing API. It
was slightly awkward in a couple spots where the format of writeThing
and marshallThing differed slightly (strings, level_id, level_pos).
Doesn't affect savegames.
When it's is okay to break savegames (maybe just before releasing
0.4?) it would be nice to remove the few remaining redundancies listed
above.
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@3828 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'crawl-ref/source')
-rw-r--r-- | crawl-ref/source/Kills.cc | 94 | ||||
-rw-r--r-- | crawl-ref/source/Kills.h | 22 | ||||
-rw-r--r-- | crawl-ref/source/files.cc | 126 | ||||
-rw-r--r-- | crawl-ref/source/files.h | 14 | ||||
-rw-r--r-- | crawl-ref/source/luadgn.cc | 28 | ||||
-rw-r--r-- | crawl-ref/source/luadgn.h | 7 | ||||
-rw-r--r-- | crawl-ref/source/mapdef.cc | 87 | ||||
-rw-r--r-- | crawl-ref/source/mapdef.h | 16 | ||||
-rw-r--r-- | crawl-ref/source/maps.cc | 61 | ||||
-rw-r--r-- | crawl-ref/source/notes.cc | 50 | ||||
-rw-r--r-- | crawl-ref/source/notes.h | 18 | ||||
-rw-r--r-- | crawl-ref/source/stash.cc | 126 | ||||
-rw-r--r-- | crawl-ref/source/stash.h | 19 | ||||
-rw-r--r-- | crawl-ref/source/tags.cc | 23 | ||||
-rw-r--r-- | crawl-ref/source/tags.h | 3 | ||||
-rw-r--r-- | crawl-ref/source/travel.cc | 113 | ||||
-rw-r--r-- | crawl-ref/source/travel.h | 35 | ||||
-rw-r--r-- | crawl-ref/source/tutorial.cc | 18 | ||||
-rw-r--r-- | crawl-ref/source/tutorial.h | 6 |
19 files changed, 414 insertions, 452 deletions
diff --git a/crawl-ref/source/Kills.cc b/crawl-ref/source/Kills.cc index 3e34e92d4c..6402e82f62 100644 --- a/crawl-ref/source/Kills.cc +++ b/crawl-ref/source/Kills.cc @@ -68,27 +68,29 @@ bool KillMaster::empty() const return (true); } -void KillMaster::save(FILE *file) const +void KillMaster::save(writer& outf) const { // Write the version of the kills file - writeByte(file, KILLS_MAJOR_VERSION); - writeByte(file, KILLS_MINOR_VERSION); + marshallByte(outf, KILLS_MAJOR_VERSION); + marshallByte(outf, KILLS_MINOR_VERSION); for (int i = 0; i < KC_NCATEGORIES; ++i) - categorized_kills[i].save(file); + categorized_kills[i].save(outf); } -void KillMaster::load(FILE *file) +void KillMaster::load(reader& inf) { - unsigned char major = readByte(file), - minor = readByte(file); + unsigned char major = unmarshallByte(inf), + minor = unmarshallByte(inf); if (major != KILLS_MAJOR_VERSION || - (minor != KILLS_MINOR_VERSION && minor > 0)) - return ; + (minor != KILLS_MINOR_VERSION && minor > 0)) + { + return; + } for (int i = 0; i < KC_NCATEGORIES; ++i) { - categorized_kills[i].load(file); + categorized_kills[i].load(inf); if (!minor) break; } @@ -291,45 +293,45 @@ long Kills::get_kills(std::vector<kill_exp> &all_kills) const return (count); } -void Kills::save(FILE *file) const +void Kills::save(writer& outf) const { // How many kill records do we have? - writeLong(file, kills.size()); + marshallLong(outf, kills.size()); for ( kill_map::const_iterator iter = kills.begin(); iter != kills.end(); ++iter) { - iter->first.save(file); - iter->second.save(file); + iter->first.save(outf); + iter->second.save(outf); } // How many ghosts do we have? - writeShort(file, ghosts.size()); + marshallShort(outf, ghosts.size()); for (ghost_vec::const_iterator iter = ghosts.begin(); iter != ghosts.end(); ++iter) { - iter->save(file); + iter->save(outf); } } -void Kills::load(FILE *file) +void Kills::load(reader& inf) { // How many kill records? - long kill_count = readLong(file); + long kill_count = unmarshallLong(inf); kills.clear(); for (long i = 0; i < kill_count; ++i) { kill_monster_desc md; - md.load(file); - kills[md].load(file); + md.load(inf); + kills[md].load(inf); } - short ghost_count = readShort(file); + short ghost_count = unmarshallShort(inf); ghosts.clear(); for (short i = 0; i < ghost_count; ++i) { kill_ghost kg; - kg.load(file); + kg.load(inf); ghosts.push_back(kg); } } @@ -531,29 +533,29 @@ std::string kill_def::append_places(const kill_monster_desc &md, return name; } -void kill_def::save(FILE *file) const +void kill_def::save(writer& outf) const { - writeShort(file, kills); - writeShort(file, exp); + marshallShort(outf, kills); + marshallShort(outf, exp); - writeShort(file, places.size()); + marshallShort(outf, places.size()); for (std::vector<unsigned short>::const_iterator iter = places.begin(); iter != places.end(); ++iter) { - writeShort(file, *iter); + marshallShort(outf, *iter); } } -void kill_def::load(FILE *file) +void kill_def::load(reader& inf) { - kills = (unsigned short) readShort(file); - exp = readShort(file); + kills = (unsigned short) unmarshallShort(inf); + exp = unmarshallShort(inf); places.clear(); - short place_count = readShort(file); + short place_count = unmarshallShort(inf); for (short i = 0; i < place_count; ++i) { - places.push_back((unsigned short) readShort(file)); + places.push_back((unsigned short) unmarshallShort(inf)); } } @@ -576,18 +578,18 @@ std::string kill_ghost::info() const " (" + short_place_name(place) + ")" : std::string("")); } -void kill_ghost::save(FILE *file) const +void kill_ghost::save(writer& outf) const { - writeString(file, ghost_name); - writeShort(file, (unsigned short) exp); - writeShort(file, place); + marshallString4(outf, ghost_name); + marshallShort(outf, (unsigned short) exp); + marshallShort(outf, place); } -void kill_ghost::load(FILE *file) +void kill_ghost::load(reader& inf) { - ghost_name = readString(file); - exp = readShort(file); - place = (unsigned short) readShort(file); + unmarshallString4(inf, ghost_name); + exp = unmarshallShort(inf); + place = (unsigned short) unmarshallShort(inf); } kill_monster_desc::kill_monster_desc(const monsters *mon) @@ -621,16 +623,16 @@ kill_monster_desc::kill_monster_desc(const monsters *mon) monnum = MONS_WEAPON_MIMIC; } -void kill_monster_desc::save(FILE *file) const +void kill_monster_desc::save(writer& outf) const { - writeShort(file, (short) monnum); - writeShort(file, (short) modifier); + marshallShort(outf, (short) monnum); + marshallShort(outf, (short) modifier); } -void kill_monster_desc::load(FILE *file) +void kill_monster_desc::load(reader& inf) { - monnum = (int) readShort(file); - modifier = (name_modifier) readShort(file); + monnum = (int) unmarshallShort(inf); + modifier = (name_modifier) unmarshallShort(inf); } /////////////////////////////////////////////////////////////////////////// diff --git a/crawl-ref/source/Kills.h b/crawl-ref/source/Kills.h index 9f7b002ea5..897725df76 100644 --- a/crawl-ref/source/Kills.h +++ b/crawl-ref/source/Kills.h @@ -18,6 +18,8 @@ std::string apostrophise(const std::string &name); class monsters; +class reader; +class writer; // Not intended for external use! struct kill_monster_desc @@ -25,8 +27,8 @@ struct kill_monster_desc kill_monster_desc(const monsters *); kill_monster_desc() { } - void save(FILE*) const; - void load(FILE*); + void save(writer&) const; + void load(reader&); enum name_modifier { @@ -58,8 +60,8 @@ public: // This object just says to the world that it's uninitialized } - void save(FILE*) const; - void load(FILE*); + void save(writer&) const; + void load(reader&); void add_kill(const monsters *mon, unsigned short place); void add_place(unsigned short place, bool force = false); @@ -86,8 +88,8 @@ public: kill_ghost(const monsters *mon); kill_ghost() { } - void save(FILE*) const; - void load(FILE*); + void save(writer&) const; + void load(reader&); std::string info() const; @@ -138,8 +140,8 @@ public: void merge(const Kills &k); bool empty() const; - void save(FILE*) const; - void load(FILE*); + void save(writer&) const; + void load(reader&); long get_kills(std::vector<kill_exp> &v) const; private: @@ -164,8 +166,8 @@ public: void record_kill(const monsters *mon, int killer, bool ispet); bool empty() const; - void save(FILE*) const; - void load(FILE*); + void save(writer&) const; + void load(reader&); std::string kill_info() const; private: diff --git a/crawl-ref/source/files.cc b/crawl-ref/source/files.cc index e473a1a23a..6b606f8923 100644 --- a/crawl-ref/source/files.cc +++ b/crawl-ref/source/files.cc @@ -1220,7 +1220,8 @@ void save_game(bool leave_game, const char *farewellmsg) FILE *stashf = fopen(stashFile.c_str(), "wb"); if (stashf) { - StashTrack.save(stashf); + writer outf(stashf); + StashTrack.save(outf); fclose(stashf); DO_CHMOD_PRIVATE(stashFile.c_str()); } @@ -1238,7 +1239,8 @@ void save_game(bool leave_game, const char *farewellmsg) FILE *killf = fopen(killFile.c_str(), "wb"); if (killf) { - you.kills->save(killf); + writer outf(killf); + you.kills->save(outf); fclose(killf); DO_CHMOD_PRIVATE(killFile.c_str()); } @@ -1248,7 +1250,8 @@ void save_game(bool leave_game, const char *farewellmsg) FILE *travelf = fopen(travelCacheFile.c_str(), "wb"); if (travelf) { - travel_cache.save(travelf); + writer outf(travelf); + travel_cache.save(outf); fclose(travelf); DO_CHMOD_PRIVATE(travelCacheFile.c_str()); } @@ -1258,7 +1261,8 @@ void save_game(bool leave_game, const char *farewellmsg) FILE *notesf = fopen(notesFile.c_str(), "wb"); if (notesf) { - save_notes(notesf); + writer outf(notesf); + save_notes(outf); fclose(notesf); DO_CHMOD_PRIVATE(notesFile.c_str()); } @@ -1269,7 +1273,8 @@ void save_game(bool leave_game, const char *farewellmsg) FILE *tutorf = fopen(tutorFile.c_str(), "wb"); if (tutorf) { - save_tutorial(tutorf); + writer outf(tutorf); + save_tutorial(outf); fclose(tutorf); DO_CHMOD_PRIVATE(tutorFile.c_str()); } @@ -1421,7 +1426,8 @@ void restore_game(void) FILE *stashf = fopen(stashFile.c_str(), "rb"); if (stashf) { - StashTrack.load(stashf); + reader inf(stashf); + StashTrack.load(inf); fclose(stashf); } @@ -1434,7 +1440,8 @@ void restore_game(void) FILE *killf = fopen(killFile.c_str(), "rb"); if (killf) { - you.kills->load(killf); + reader inf(killf); + you.kills->load(inf); fclose(killf); } @@ -1442,7 +1449,8 @@ void restore_game(void) FILE *travelf = fopen(travelCacheFile.c_str(), "rb"); if (travelf) { - travel_cache.load(travelf); + reader inf(travelf); + travel_cache.load(inf); fclose(travelf); } @@ -1450,7 +1458,8 @@ void restore_game(void) FILE *notesf = fopen(notesFile.c_str(), "rb"); if (notesf) { - load_notes(notesf); + reader inf(notesf); + load_notes(inf); fclose(notesf); } @@ -1460,7 +1469,8 @@ void restore_game(void) FILE *tutorf = fopen(tutorFile.c_str(), "rb"); if (tutorf) { - load_tutorial(tutorf); + reader inf(tutorf); + load_tutorial(inf); fclose(tutorf); } } @@ -1641,17 +1651,16 @@ static bool determine_ghost_version( FILE *ghostFile, majorVersion = buf[0]; minorVersion = buf[1]; + reader inf(ghostFile); // check for the DCSS ghost signature. - if (readShort(ghostFile) != GHOST_SIGNATURE) + if (unmarshallShort(inf) != GHOST_SIGNATURE) return (false); if (majorVersion == SAVE_MAJOR_VERSION && minorVersion <= GHOST_MINOR_VERSION) { // Discard three more 32-bit words of padding. - for (int i = 0; i < 3; ++i) - readLong(ghostFile); - + inf.read(NULL, 3*4); return !feof(ghostFile); } @@ -1733,95 +1742,6 @@ void generate_random_demon() menv[rdem].pandemon_init(); } // end generate_random_demon() -void writeShort(FILE *file, short s) -{ - char data[2]; - // High byte first - network order - data[0] = static_cast<char>((s >> 8) & 0xFF); - data[1] = static_cast<char>(s & 0xFF); - - write2(file, data, sizeof(data)); -} - -short readShort(FILE *file) -{ - unsigned char data[2]; - read2(file, (char *) data, 2); - - // High byte first - return (((short) data[0]) << 8) | (short) data[1]; -} - -void writeByte(FILE *file, unsigned char byte) -{ - write2(file, (char *) &byte, sizeof byte); -} - -unsigned char readByte(FILE *file) -{ - unsigned char byte; - read2(file, (char *) &byte, sizeof byte); - return byte; -} - -void writeString(FILE* file, const std::string &s, int cap) -{ - int length = s.length(); - if (length > cap) - length = cap; - writeLong(file, length); - if (length) - write2(file, s.c_str(), length); -} - -std::string readString(FILE *file, int cap) -{ - const int length = readLong(file); - if (length > 0) - { - if (length <= cap) - { - char *buf = new char[length]; - read2(file, buf, length); - const std::string s(buf, length); - delete [] buf; - return (s); - } - - end(1, false, "String too long: %d bytes\n", length); - } - - return (""); -} - -void writeLong(FILE* file, long num) -{ - // High word first, network order - writeShort(file, (short) ((num >> 16) & 0xFFFFL)); - writeShort(file, (short) (num & 0xFFFFL)); -} - -long readLong(FILE *file) -{ - // We need the unsigned short cast even for the high word because we - // might be on a system where long is more than 4 bytes, and we don't want - // to sign extend the high short. - return ((long) (unsigned short) readShort(file)) << 16 | - (long) (unsigned short) readShort(file); -} - -void writeCoord(FILE *file, const coord_def &pos) -{ - writeShort(file, pos.x); - writeShort(file, pos.y); -} - -void readCoord(FILE *file, coord_def &pos) -{ - pos.x = readShort(file); - pos.y = readShort(file); -} - //////////////////////////////////////////////////////////////////////////// // Locking for multiuser systems diff --git a/crawl-ref/source/files.h b/crawl-ref/source/files.h index 9fdbdcc3cf..422925343e 100644 --- a/crawl-ref/source/files.h +++ b/crawl-ref/source/files.h @@ -96,20 +96,6 @@ void save_ghost( bool force = false ); std::string make_filename( const char *prefix, int level, branch_type branch, level_area_type lt, bool isGhost ); -// Default cap on strings marshalled. -#define STR_CAP 1000 - -void writeShort(FILE *file, short s); -short readShort(FILE *file); -void writeByte(FILE *file, unsigned char byte); -unsigned char readByte(FILE *file); -void writeString(FILE* file, const std::string &s, int cap = STR_CAP); -std::string readString(FILE *file, int cap = STR_CAP); -void writeLong(FILE* file, long num); -long readLong(FILE *file); -void writeCoord(FILE *file, const coord_def &pos); -void readCoord(FILE *file, coord_def &pos); - FILE *lk_open(const char *mode, const std::string &file); void lk_close(FILE *handle, const char *mode, const std::string &file); diff --git a/crawl-ref/source/luadgn.cc b/crawl-ref/source/luadgn.cc index ee432892be..c1abc08968 100644 --- a/crawl-ref/source/luadgn.cc +++ b/crawl-ref/source/luadgn.cc @@ -94,46 +94,46 @@ dlua_chunk dlua_chunk::precompiled(const std::string &chunk) return (dchunk); } -void dlua_chunk::write(FILE *outf) const +void dlua_chunk::write(writer& outf) const { if (empty()) { - writeByte(outf, CT_EMPTY); + marshallByte(outf, CT_EMPTY); return; } if (!compiled.empty()) { - writeByte(outf, CT_COMPILED); - writeString(outf, compiled, LUA_CHUNK_MAX_SIZE); + marshallByte(outf, CT_COMPILED); + marshallString4(outf, compiled); } else { - writeByte(outf, CT_SOURCE); - writeString(outf, chunk, LUA_CHUNK_MAX_SIZE); + marshallByte(outf, CT_SOURCE); + marshallString4(outf, chunk); } - writeString(outf, file); - writeLong(outf, first); + marshallString4(outf, file); + marshallLong(outf, first); } -void dlua_chunk::read(FILE *inf) +void dlua_chunk::read(reader& inf) { clear(); - chunk_t type = static_cast<chunk_t>(readByte(inf)); + chunk_t type = static_cast<chunk_t>(unmarshallByte(inf)); switch (type) { case CT_EMPTY: return; case CT_SOURCE: - chunk = readString(inf, LUA_CHUNK_MAX_SIZE); + unmarshallString4(inf, chunk); break; case CT_COMPILED: - compiled = readString(inf, LUA_CHUNK_MAX_SIZE); + unmarshallString4(inf, compiled); break; } - file = readString(inf); - first = readLong(inf); + unmarshallString4(inf, file); + first = unmarshallLong(inf); } void dlua_chunk::clear() diff --git a/crawl-ref/source/luadgn.h b/crawl-ref/source/luadgn.h index 87737d61fa..8dd447c175 100644 --- a/crawl-ref/source/luadgn.h +++ b/crawl-ref/source/luadgn.h @@ -18,6 +18,9 @@ extern CLua dlua; // Lua chunks cannot exceed 512K. Which is plenty! const int LUA_CHUNK_MAX_SIZE = 512 * 1024; +class reader; +class writer; + class dlua_chunk { private: @@ -65,8 +68,8 @@ public: const std::string &compiled_chunk() const { return compiled; } - void write(FILE *) const; - void read(FILE *); + void write(writer&) const; + void read(reader&); }; void init_dungeon_lua(); diff --git a/crawl-ref/source/mapdef.cc b/crawl-ref/source/mapdef.cc index 5f60c44453..594ce07464 100644 --- a/crawl-ref/source/mapdef.cc +++ b/crawl-ref/source/mapdef.cc @@ -16,6 +16,7 @@ #include "branch.h" #include "describe.h" #include "direct.h" +#include "dungeon.h" #include "files.h" #include "initfile.h" #include "invent.h" @@ -30,7 +31,7 @@ #include "mon-util.h" #include "place.h" #include "stuff.h" -#include "dungeon.h" +#include "tags.h" static const char *map_section_names[] = { "", @@ -110,20 +111,20 @@ level_range::level_range(const raw_range &r) { } -void level_range::write(FILE *outf) const +void level_range::write(writer& outf) const { - writeShort(outf, branch); - writeShort(outf, shallowest); - writeShort(outf, deepest); - writeByte(outf, deny); + marshallShort(outf, branch); + marshallShort(outf, shallowest); + marshallShort(outf, deepest); + marshallByte(outf, deny); } -void level_range::read(FILE *inf) +void level_range::read(reader& inf) { - branch = static_cast<branch_type>( readShort(inf) ); - shallowest = readShort(inf); - deepest = readShort(inf); - deny = readByte(inf); + branch = static_cast<branch_type>( unmarshallShort(inf) ); + shallowest = unmarshallShort(inf); + deepest = unmarshallShort(inf); + deny = unmarshallByte(inf); } std::string level_range::str_depth_range() const @@ -1209,18 +1210,18 @@ int map_def::glyph_at(const coord_def &c) const return map(c); } -void map_def::write_full(FILE *outf) +void map_def::write_full(writer& outf) { - cache_offset = ftell(outf); - writeShort(outf, MAP_CACHE_VERSION); // Level indicator. - writeString(outf, name); + cache_offset = outf.tell(); + marshallShort(outf, MAP_CACHE_VERSION); // Level indicator. + marshallString4(outf, name); prelude.write(outf); main.write(outf); validate.write(outf); veto.write(outf); } -void map_def::read_full(FILE *inf) +void map_def::read_full(reader& inf) { // There's a potential race-condition here: // - If someone modifies a .des file while there are games in progress, @@ -1230,11 +1231,16 @@ void map_def::read_full(FILE *inf) // reloading the index), but it's easier to save the game at this // point and let the player reload. - if (readShort(inf) != MAP_CACHE_VERSION || readString(inf) != name) + const short fp_version = unmarshallShort(inf); + std::string fp_name; + unmarshallString4(inf, fp_name); + if (fp_version != MAP_CACHE_VERSION || fp_name != name) + { save_game(true, make_stringf("Level file cache for %s is out-of-sync! " "Please reload your game.", file.c_str()).c_str()); + } prelude.read(inf); main.read(inf); @@ -1250,10 +1256,11 @@ void map_def::load() const std::string descache_base = get_descache_path(file, ""); file_lock deslock(descache_base + ".lk", "rb", false); const std::string loadfile = descache_base + ".dsc"; - FILE *inf = fopen(loadfile.c_str(), "rb"); - fseek(inf, cache_offset, SEEK_SET); + FILE *fp = fopen(loadfile.c_str(), "rb"); + fseek(fp, cache_offset, SEEK_SET); + reader inf(fp); read_full(inf); - fclose(inf); + fclose(fp); index_only = false; } @@ -1273,49 +1280,49 @@ coord_def map_def::find_first_glyph(const std::string &s) const return map.find_first_glyph(s); } -void map_def::write_index(FILE *outf) const +void map_def::write_index(writer& outf) const { if (!cache_offset) end(1, false, "Map %s: can't write index - cache offset not set!", name.c_str()); - writeString(outf, name); - writeString(outf, place_loaded_from.filename); - writeLong(outf, place_loaded_from.lineno); - writeShort(outf, orient); - writeLong(outf, chance); - writeLong(outf, cache_offset); - writeString(outf, tags); + marshallString4(outf, name); + marshallString4(outf, place_loaded_from.filename); + marshallLong(outf, place_loaded_from.lineno); + marshallShort(outf, orient); + marshallLong(outf, chance); + marshallLong(outf, cache_offset); + marshallString4(outf, tags); place.save(outf); write_depth_ranges(outf); prelude.write(outf); } -void map_def::read_index(FILE *inf) +void map_def::read_index(reader& inf) { - name = readString(inf); - place_loaded_from.filename = readString(inf); - place_loaded_from.lineno = readLong(inf); - orient = static_cast<map_section_type>( readShort(inf) ); - chance = readLong(inf); - cache_offset = readLong(inf); - tags = readString(inf); + unmarshallString4(inf, name); + unmarshallString4(inf, place_loaded_from.filename); + place_loaded_from.lineno = unmarshallLong(inf); + orient = static_cast<map_section_type>( unmarshallShort(inf) ); + chance = unmarshallLong(inf); + cache_offset = unmarshallLong(inf); + unmarshallString4(inf, tags); place.load(inf); read_depth_ranges(inf); prelude.read(inf); index_only = true; } -void map_def::write_depth_ranges(FILE *outf) const +void map_def::write_depth_ranges(writer& outf) const { - writeShort(outf, depths.size()); + marshallShort(outf, depths.size()); for (int i = 0, sz = depths.size(); i < sz; ++i) depths[i].write(outf); } -void map_def::read_depth_ranges(FILE *inf) +void map_def::read_depth_ranges(reader& inf) { depths.clear(); - const int nranges = readShort(inf); + const int nranges = unmarshallShort(inf); for (int i = 0; i < nranges; ++i) { level_range lr; diff --git a/crawl-ref/source/mapdef.h b/crawl-ref/source/mapdef.h index 154f2ffa90..822b811b5c 100644 --- a/crawl-ref/source/mapdef.h +++ b/crawl-ref/source/mapdef.h @@ -70,8 +70,8 @@ public: bool matches(const level_id &) const; bool matches(int depth) const; - void write(FILE *) const; - void read(FILE *); + void write(writer&) const; + void read(reader&); bool valid() const; int span() const; @@ -642,11 +642,11 @@ public: coord_def find_first_glyph(int glyph) const; coord_def find_first_glyph(const std::string &glyphs) const; - void write_index(FILE *) const; - void write_full(FILE *); + void write_index(writer&) const; + void write_full(writer&); - void read_index(FILE *); - void read_full(FILE *); + void read_index(reader&); + void read_full(reader&); void set_file(const std::string &s); std::string run_lua(bool skip_main); @@ -725,8 +725,8 @@ public: }; private: - void write_depth_ranges(FILE *) const; - void read_depth_ranges(FILE *); + void write_depth_ranges(writer&) const; + void read_depth_ranges(reader&); bool test_lua_boolchunk(dlua_chunk &, bool def = false, bool croak = false); std::string rewrite_chunk_errors(const std::string &s) const; diff --git a/crawl-ref/source/maps.cc b/crawl-ref/source/maps.cc index 05558cecae..7e4b2a95ea 100644 --- a/crawl-ref/source/maps.cc +++ b/crawl-ref/source/maps.cc @@ -14,6 +14,7 @@ #include "AppHdr.h" #include "maps.h" +#include "tags.h" #include <cstring> #include <cstdlib> @@ -511,12 +512,12 @@ std::string get_descache_path(const std::string &file, static bool verify_file_version(const std::string &file) { - FILE *inf = fopen(file.c_str(), "rb"); - if (!inf) + FILE *fp = fopen(file.c_str(), "rb"); + if (!fp) return (false); - - const long ver = readLong(inf); - fclose(inf); + reader inf(fp); + const long ver = unmarshallLong(inf); + fclose(fp); return (ver == MAP_CACHE_VERSION); } @@ -534,21 +535,26 @@ static bool verify_map_full(const std::string &base) static bool load_map_index(const std::string &base) { // If there's a global prelude, load that first. - FILE *inf = fopen((base + ".lux").c_str(), "rb"); - if (inf) { - lc_global_prelude.read(inf); - fclose(inf); + FILE *fp = fopen((base + ".lux").c_str(), "rb"); + if (fp) + { + reader inf(fp); + lc_global_prelude.read(inf); + fclose(fp); - global_preludes.push_back( lc_global_prelude ); + global_preludes.push_back( lc_global_prelude ); + } } - inf = fopen((base + ".idx").c_str(), "rb"); - if (!inf) + FILE* fp = fopen((base + ".idx").c_str(), "rb"); + if (!fp) end(1, true, "Unable to read %s", (base + ".idx").c_str()); + reader inf(fp); + // Discard version (it's been checked by verify_map_index). - readLong(inf); - const int nmaps = readShort(inf); + (void) unmarshallLong(inf); + const int nmaps = unmarshallShort(inf); const int nexist = vdefs.size(); vdefs.resize( nexist + nmaps, map_def() ); for (int i = 0; i < nmaps; ++i) @@ -559,7 +565,7 @@ static bool load_map_index(const std::string &base) lc_loaded_maps[vdef.name] = vdef.place_loaded_from; vdef.place_loaded_from.clear(); } - fclose(inf); + fclose(fp); return (true); } @@ -590,36 +596,39 @@ static void write_map_prelude(const std::string &filebase) return; } - FILE *outf = fopen(luafile.c_str(), "wb"); + FILE *fp = fopen(luafile.c_str(), "wb"); + writer outf(fp); lc_global_prelude.write(outf); - fclose(outf); + fclose(fp); } static void write_map_full(const std::string &filebase, size_t vs, size_t ve) { const std::string cfile = filebase + ".dsc"; - FILE *outf = fopen(cfile.c_str(), "wb"); - if (!outf) + FILE *fp = fopen(cfile.c_str(), "wb"); + if (!fp) end(1, true, "Unable to open %s for writing", cfile.c_str()); - writeLong(outf, MAP_CACHE_VERSION); + writer outf(fp); + marshallLong(outf, MAP_CACHE_VERSION); for (size_t i = vs; i < ve; ++i) vdefs[i].write_full(outf); - fclose(outf); + fclose(fp); } static void write_map_index(const std::string &filebase, size_t vs, size_t ve) { const std::string cfile = filebase + ".idx"; - FILE *outf = fopen(cfile.c_str(), "wb"); - if (!outf) + FILE *fp = fopen(cfile.c_str(), "wb"); + if (!fp) end(1, true, "Unable to open %s for writing", cfile.c_str()); - writeLong(outf, MAP_CACHE_VERSION); - writeShort(outf, ve > vs? ve - vs : 0); + writer outf(fp); + marshallLong(outf, MAP_CACHE_VERSION); + marshallShort(outf, ve > vs? ve - vs : 0); for (size_t i = vs; i < ve; ++i) vdefs[i].write_index(outf); - fclose(outf); + fclose(fp); } static void write_map_cache(const std::string &filename, size_t vs, size_t ve) diff --git a/crawl-ref/source/notes.cc b/crawl-ref/source/notes.cc index 8712dd1280..901fe8ea0b 100644 --- a/crawl-ref/source/notes.cc +++ b/crawl-ref/source/notes.cc @@ -26,6 +26,7 @@ #include "religion.h" #include "skills2.h" #include "spl-util.h" +#include "tags.h" #define NOTES_VERSION_NUMBER 1001 @@ -385,25 +386,26 @@ void Note::check_milestone() const #endif } -void Note::save( FILE* fp ) const { - writeLong( fp, type ); - writeLong( fp, turn ); - writeShort( fp, packed_place ); - writeLong( fp, first ); - writeLong( fp, second ); - writeString( fp, name ); - writeString( fp, desc ); +void Note::save(writer& outf) const +{ + marshallLong( outf, type ); + marshallLong( outf, turn ); + marshallShort( outf, packed_place ); + marshallLong( outf, first ); + marshallLong( outf, second ); + marshallString4( outf, name ); + marshallString4( outf, desc ); } -void Note::load( FILE* fp ) +void Note::load(reader& inf) { - type = static_cast<NOTE_TYPES>(readLong( fp )); - turn = readLong( fp ); - packed_place = readShort( fp ); - first = readLong( fp ); - second = readLong( fp ); - name = readString( fp ); - desc = readString( fp ); + type = static_cast<NOTE_TYPES>(unmarshallLong( inf )); + turn = unmarshallLong( inf ); + packed_place = unmarshallShort( inf ); + first = unmarshallLong( inf ); + second = unmarshallLong( inf ); + unmarshallString4( inf, name ); + unmarshallString4( inf, desc ); } bool notes_active = false; @@ -427,24 +429,24 @@ void activate_notes( bool active ) notes_active = active; } -void save_notes( FILE* fp ) +void save_notes(writer& outf) { - writeLong( fp, NOTES_VERSION_NUMBER ); - writeLong( fp, note_list.size() ); + marshallLong( outf, NOTES_VERSION_NUMBER ); + marshallLong( outf, note_list.size() ); for ( unsigned i = 0; i < note_list.size(); ++i ) - note_list[i].save(fp); + note_list[i].save(outf); } -void load_notes( FILE* fp ) +void load_notes(reader& inf) { - if ( readLong(fp) != NOTES_VERSION_NUMBER ) + if ( unmarshallLong(inf) != NOTES_VERSION_NUMBER ) return; - const long num_notes = readLong(fp); + const long num_notes = unmarshallLong(inf); for ( long i = 0; i < num_notes; ++i ) { Note new_note; - new_note.load(fp); + new_note.load(inf); note_list.push_back(new_note); } } diff --git a/crawl-ref/source/notes.h b/crawl-ref/source/notes.h index 8ba835861b..7e8366bb3a 100644 --- a/crawl-ref/source/notes.h +++ b/crawl-ref/source/notes.h @@ -17,6 +17,9 @@ #include <vector> #include <stdio.h> +class reader; +class writer; + enum NOTE_TYPES { NOTE_HP_CHANGE = 0, /* needs: new hp, max hp */ @@ -51,25 +54,26 @@ struct Note Note(); Note( NOTE_TYPES t, int f = 0, int s = 0, const char* n = 0, const char* d = 0); + void save(writer& outf) const; + void load(reader& inf); + std::string describe( bool when = true, bool where = true, + bool what = true ) const; + void check_milestone() const; + NOTE_TYPES type; int first, second; long turn; unsigned short packed_place; std::string name; std::string desc; - void load( FILE* fp ); - void save( FILE* fp ) const; - std::string describe( bool when = true, bool where = true, - bool what = true ) const; - void check_milestone() const; }; extern std::vector<Note> note_list; void activate_notes( bool active ); bool notes_are_active(); void take_note( const Note& note, bool force = false ); -void save_notes( FILE* fp ); -void load_notes( FILE* fp ); +void save_notes(writer&); +void load_notes(reader&); void make_user_note(); #endif diff --git a/crawl-ref/source/stash.cc b/crawl-ref/source/stash.cc index 9d561e79ae..c1007cc320 100644 --- a/crawl-ref/source/stash.cc +++ b/crawl-ref/source/stash.cc @@ -137,17 +137,9 @@ static void fully_identify_item(item_def *item) ID_KNOWN_TYPE ); } -static void save_item(FILE *file, const item_def &item) -{ - writer outf(file); - marshallItem(outf, item); -} - -static void load_item(FILE *file, item_def &item) -{ - reader inf(file); - unmarshallItem(inf, item); -} +// ---------------------------------------------------------------------- +// Stash +// ---------------------------------------------------------------------- bool Stash::aggressive_verify = true; std::vector<item_def> Stash::filters; @@ -613,44 +605,44 @@ void Stash::write(std::ostream &os, activate_notes(true); } -void Stash::save(FILE *file) const +void Stash::save(writer& outf) const { // How many items on this square? - writeShort(file, (short) items.size()); + marshallShort(outf, (short) items.size()); - writeByte(file, x); - writeByte(file, y); + marshallByte(outf, x); + marshallByte(outf, y); - writeByte(file, feat); - writeByte(file, trap); + marshallByte(outf, feat); + marshallByte(outf, trap); // Note: Enabled save value is inverted logic, so that it defaults to true - writeByte(file, + marshallByte(outf, (unsigned char) ((verified? 1 : 0) | (!enabled? 2 : 0)) ); // And dump the items individually. We don't bother saving fields we're // not interested in (and don't anticipate being interested in). for (unsigned i = 0; i < items.size(); ++i) - save_item(file, items[i]); + marshallItem(outf, items[i]); } -void Stash::load(FILE *file) +void Stash::load(reader& inf) { // How many items? - int count = readShort(file); + int count = unmarshallShort(inf); - x = readByte(file); - y = readByte(file); + x = unmarshallByte(inf); + y = unmarshallByte(inf); feat = static_cast<dungeon_feature_type>( - static_cast<unsigned char>( readByte(file) )); + static_cast<unsigned char>( unmarshallByte(inf) )); trap = static_cast<trap_type>( - static_cast<unsigned char>( readByte(file) )); + static_cast<unsigned char>( unmarshallByte(inf) )); - unsigned char flags = readByte(file); + unsigned char flags = unmarshallByte(inf); verified = (flags & 1) != 0; // Note: Enabled save value is inverted so it defaults to true. @@ -664,7 +656,7 @@ void Stash::load(FILE *file) for (int i = 0; i < count; ++i) { item_def item; - load_item(file, item); + unmarshallItem(inf, item); items.push_back(item); } @@ -890,45 +882,45 @@ void ShopInfo::write(std::ostream &os, bool identify) const activate_notes(true); } -void ShopInfo::save(FILE *file) const +void ShopInfo::save(writer& outf) const { - writeShort(file, shoptype); + marshallShort(outf, shoptype); int mangledx = (short) x; if (!visited) mangledx |= 1024; - writeShort(file, mangledx); - writeShort(file, (short) y); + marshallShort(outf, mangledx); + marshallShort(outf, (short) y); - writeShort(file, (short) items.size()); + marshallShort(outf, (short) items.size()); - writeString(file, name); + marshallString4(outf, name); for (unsigned i = 0; i < items.size(); ++i) { - save_item(file, items[i].item); - writeShort(file, (short) items[i].price ); + marshallItem(outf, items[i].item); + marshallShort(outf, (short) items[i].price ); } } -void ShopInfo::load(FILE *file) +void ShopInfo::load(reader& inf) { - shoptype = readShort(file); + shoptype = unmarshallShort(inf); - x = readShort(file); + x = unmarshallShort(inf); visited = !(x & 1024); x &= 0xFF; - y = readShort(file); + y = unmarshallShort(inf); - int itemcount = readShort(file); + int itemcount = unmarshallShort(inf); - name = readString(file); + unmarshallString4(inf, name); for (int i = 0; i < itemcount; ++i) { shop_item item; - load_item(file, item.item); - item.price = (unsigned) readShort(file); + unmarshallItem(inf, item.item); + item.price = (unsigned) unmarshallShort(inf); items.push_back(item); } } @@ -1153,44 +1145,46 @@ void LevelStashes::write(std::ostream &os, bool identify) const os << std::endl; } -void LevelStashes::save(FILE *file) const +void LevelStashes::save(writer& outf) const { // How many stashes on this level? - writeShort(file, (short) m_stashes.size()); + marshallShort(outf, (short) m_stashes.size()); - m_place.save(file); + m_place.save(outf); // And write the individual stashes for (stashes_t::const_iterator iter = m_stashes.begin(); - iter != m_stashes.end(); iter++) - iter->second.save(file); + iter != m_stashes.end(); iter++) + { + iter->second.save(outf); + } - writeShort(file, (short) m_shops.size()); + marshallShort(outf, (short) m_shops.size()); for (unsigned i = 0; i < m_shops.size(); ++i) - m_shops[i].save(file); + m_shops[i].save(outf); } -void LevelStashes::load(FILE *file) +void LevelStashes::load(reader& inf) { - int size = readShort(file); + int size = unmarshallShort(inf); - m_place.load(file); + m_place.load(inf); m_stashes.clear(); for (int i = 0; i < size; ++i) { Stash s; - s.load(file); + s.load(inf); if (!s.empty()) m_stashes[ s.abs_pos() ] = s; } m_shops.clear(); - int shopc = readShort(file); + int shopc = unmarshallShort(inf); for (int i = 0; i < shopc; ++i) { ShopInfo si(0, 0); - si.load(file); + si.load(inf); m_shops.push_back(si); } } @@ -1292,36 +1286,36 @@ void StashTracker::write(std::ostream &os, bool identify) const } } -void StashTracker::save(FILE *file) const +void StashTracker::save(writer& outf) const { // Write version info first - major + minor - writeByte(file, ST_MAJOR_VER); - writeByte(file, ST_MINOR_VER); + marshallByte(outf, ST_MAJOR_VER); + marshallByte(outf, ST_MINOR_VER); // How many levels have we? - writeShort(file, (short) levels.size()); + marshallShort(outf, (short) levels.size()); // And ask each level to write itself to the tag stash_levels_t::const_iterator iter = levels.begin(); for ( ; iter != levels.end(); iter++) - iter->second.save(file); + iter->second.save(outf); } -void StashTracker::load(FILE *file) +void StashTracker::load(reader& inf) { // Check version. Compatibility isn't important, since stash-tracking // is non-critical. - unsigned char major = readByte(file), - minor = readByte(file); + unsigned char major = unmarshallByte(inf), + minor = unmarshallByte(inf); if (major != ST_MAJOR_VER || minor != ST_MINOR_VER) return ; - int count = readShort(file); + int count = unmarshallShort(inf); levels.clear(); for (int i = 0; i < count; ++i) { LevelStashes st; - st.load(file); + st.load(inf); if (st.stash_count()) levels[st.where()] = st; } diff --git a/crawl-ref/source/stash.h b/crawl-ref/source/stash.h index 6102f013fd..8ca50d0ca3 100644 --- a/crawl-ref/source/stash.h +++ b/crawl-ref/source/stash.h @@ -21,6 +21,8 @@ #include "travel.h" class input_history; +class reader; +class writer; // Stash definitions void stash_init_new_level(); @@ -46,8 +48,8 @@ public: static std::string stash_item_name(const item_def &item); void update(); - void save(FILE*) const; - void load(FILE*); + void save(writer&) const; + void load(reader&); std::string description() const; std::string feature_description() const; @@ -121,9 +123,8 @@ public: std::string description() const; - // Note that we aren't bothering to use virtual functions here. - void save(FILE*) const; - void load(FILE*); + void save(writer&) const; + void load(reader&); bool show_menu(const std::string &place, bool can_travel) const; bool is_visited() const { return items.size() || visited; } @@ -232,8 +233,8 @@ public: void kill_stash(const Stash &s); - void save(FILE *) const; - void load(FILE *); + void save(writer&) const; + void load(reader&); void write(std::ostream &os, bool identify = false) const; std::string level_name() const; @@ -308,8 +309,8 @@ public: // used if no parameters are supplied. void no_stash(int x = -1, int y = -1); - void save(FILE*) const; - void load(FILE*); + void save(writer&) const; + void load(reader&); void write(std::ostream &os, bool identify = false) const; diff --git a/crawl-ref/source/tags.cc b/crawl-ref/source/tags.cc index 35736247de..846b49476e 100644 --- a/crawl-ref/source/tags.cc +++ b/crawl-ref/source/tags.cc @@ -145,6 +145,12 @@ void writer::write(const void *data, size_t size) } } +long writer::tell() +{ + ASSERT(_file); + return ftell(_file); +} + // static helpers static void tag_construct_you(writer &th); @@ -329,6 +335,7 @@ static void unmarshall_container(reader &th, T_container &container, (container.*inserter)(unmarshal(th)); } +// XXX: redundant with level_id.save()/load() void marshall_level_id( writer& th, const level_id& id ) { marshallByte(th, id.branch ); @@ -336,6 +343,7 @@ void marshall_level_id( writer& th, const level_id& id ) marshallByte(th, id.level_type); } +// XXX: redundant with level_pos.save()/load() void marshall_level_pos( writer& th, const level_pos& lpos ) { marshallLong(th, lpos.pos.x); @@ -468,7 +476,7 @@ float unmarshallFloat(reader &th) return k.f_num; } -// string -- marshall length & string data +// string -- 2 byte length, string data void marshallString(writer &th, const std::string &data, int maxSize) { // allow for very long strings (well, up to 32K). @@ -526,6 +534,19 @@ static std::string unmarshallStringNoMax(reader &th) return unmarshallString(th); } +// string -- 4 byte length, non-terminated string data +void marshallString4(writer &th, const std::string &data) +{ + marshallLong(th, data.length()); + th.write(data.c_str(), data.length()); +} +void unmarshallString4(reader &th, std::string& s) +{ + const int len = unmarshallLong(th); + s.resize(len); + if (len) th.read(&s.at(0), len); +} + // boolean (to avoid system-dependant bool implementations) void marshallBoolean(writer &th, bool data) { diff --git a/crawl-ref/source/tags.h b/crawl-ref/source/tags.h index fc2c176b39..385f25cfa2 100644 --- a/crawl-ref/source/tags.h +++ b/crawl-ref/source/tags.h @@ -55,6 +55,7 @@ public: void writeByte(unsigned char byte); void write(const void *data, size_t size); + long tell(); private: FILE* _file; @@ -67,6 +68,7 @@ void marshallLong (writer &, long ); void marshallFloat (writer &, float ); void marshallBoolean (writer &, bool ); void marshallString (writer &, const std::string &, int maxSize = 0); +void marshallString4 (writer &, const std::string &); void marshallCoord (writer &, const coord_def &); void marshallItem (writer &, const item_def &); @@ -98,6 +100,7 @@ float unmarshallFloat (reader &); bool unmarshallBoolean (reader &); int unmarshallCString (reader &, char *data, int maxSize); std::string unmarshallString (reader &, int maxSize = 1000); +void unmarshallString4 (reader &, std::string&); void unmarshallCoord (reader &, coord_def &c); void unmarshallItem (reader &, item_def &item); diff --git a/crawl-ref/source/travel.cc b/crawl-ref/source/travel.cc index 68470edd73..ae779e45d9 100644 --- a/crawl-ref/source/travel.cc +++ b/crawl-ref/source/travel.cc @@ -35,6 +35,7 @@ #include "player.h" #include "stash.h" #include "stuff.h" +#include "tags.h" #include "terrain.h" #ifdef USE_TILE #include "tiles.h" @@ -3004,48 +3005,50 @@ level_id level_id::parse_level_id(const std::string &s) throw (std::string) return level_id(br, dep); } -void level_id::save(FILE *file) const +// NOTE: see also marshall_level_id +void level_id::save(writer& outf) const { - writeShort(file, branch); - writeShort(file, depth); - writeShort(file, level_type); + marshallShort(outf, branch); + marshallShort(outf, depth); + marshallShort(outf, level_type); } -void level_id::load(FILE *file) +void level_id::load(reader& inf) { - branch = static_cast<branch_type>(readShort(file)); - depth = readShort(file); - level_type = static_cast<level_area_type>(readShort(file)); + branch = static_cast<branch_type>(unmarshallShort(inf)); + depth = unmarshallShort(inf); + level_type = static_cast<level_area_type>(unmarshallShort(inf)); } -void level_pos::save(FILE *file) const +// NOTE: see also marshall_level_pos +void level_pos::save(writer& outf) const { - id.save(file); - writeCoord(file, pos); + id.save(outf); + marshallCoord(outf, pos); } -void level_pos::load(FILE *file) +void level_pos::load(reader& inf) { - id.load(file); - readCoord(file, pos); + id.load(inf); + unmarshallCoord(inf, pos); } -void stair_info::save(FILE *file) const +void stair_info::save(writer& outf) const { - writeCoord(file, position); - writeShort(file, grid); - destination.save(file); - writeByte(file, guessed_pos? 1 : 0); - writeByte(file, type); + marshallCoord(outf, position); + marshallShort(outf, grid); + destination.save(outf); + marshallByte(outf, guessed_pos? 1 : 0); + marshallByte(outf, type); } -void stair_info::load(FILE *file) +void stair_info::load(reader& inf) { - readCoord(file, position); - grid = static_cast<dungeon_feature_type>(readShort(file)); - destination.load(file); - guessed_pos = readByte(file) != 0; - type = static_cast<stair_type>(readByte(file)); + unmarshallCoord(inf, position); + grid = static_cast<dungeon_feature_type>(unmarshallShort(inf)); + destination.load(inf); + guessed_pos = unmarshallByte(inf) != 0; + type = static_cast<stair_type>(unmarshallByte(inf)); } std::string stair_info::describe() const @@ -3369,13 +3372,13 @@ bool LevelInfo::is_known_branch(unsigned char branch) const return false; } -void LevelInfo::save(FILE *file) const +void LevelInfo::save(writer& outf) const { int stair_count = stairs.size(); // How many stairs do we know of? - writeShort(file, stair_count); + marshallShort(outf, stair_count); for (int i = 0; i < stair_count; ++i) - stairs[i].save(file); + stairs[i].save(outf); if (stair_count) { @@ -3384,31 +3387,31 @@ void LevelInfo::save(FILE *file) const for (int i = 0, n = stair_count * stair_count; i < n; ++i) { if (i >= sz) - writeShort(file, -1); + marshallShort(outf, -1); else - writeShort(file, stair_distances[i]); + marshallShort(outf, stair_distances[i]); } } - writeShort(file, excludes.size()); + marshallShort(outf, excludes.size()); if (excludes.size()) { for (int i = 0, count = excludes.size(); i < count; ++i) { - writeCoord(file, excludes[i].pos); - writeShort(file, excludes[i].radius); + marshallCoord(outf, excludes[i].pos); + marshallShort(outf, excludes[i].radius); } } } -void LevelInfo::load(FILE *file) +void LevelInfo::load(reader& inf) { stairs.clear(); - int stair_count = readShort(file); + int stair_count = unmarshallShort(inf); for (int i = 0; i < stair_count; ++i) { stair_info si; - si.load(file); + si.load(inf); stairs.push_back(si); if (id.branch == BRANCH_MAIN_DUNGEON && @@ -3423,18 +3426,18 @@ void LevelInfo::load(FILE *file) { stair_distances.reserve(stair_count * stair_count); for (int i = stair_count * stair_count - 1; i >= 0; --i) - stair_distances.push_back( readShort(file) ); + stair_distances.push_back( unmarshallShort(inf) ); } excludes.clear(); - int nexcludes = readShort(file); + int nexcludes = unmarshallShort(inf); if (nexcludes) { for (int i = 0; i < nexcludes; ++i) { coord_def c; - readCoord(file, c); - const int radius = readShort(file); + unmarshallCoord(inf, c); + const int radius = unmarshallShort(inf); excludes.push_back(travel_exclude(c, radius)); } } @@ -3633,11 +3636,11 @@ bool TravelCache::is_known_branch(unsigned char branch) const return false; } -void TravelCache::save(FILE *file) const +void TravelCache::save(writer& outf) const { // Travel cache version information - writeByte(file, TC_MAJOR_VERSION); - writeByte(file, TC_MINOR_VERSION); + marshallByte(outf, TC_MAJOR_VERSION); + marshallByte(outf, TC_MINOR_VERSION); int level_count = 0; for (travel_levels_map::const_iterator i = levels.begin(); @@ -3648,7 +3651,7 @@ void TravelCache::save(FILE *file) const } // Write level count: - writeShort(file, level_count); + marshallShort(outf, level_count); // Save all the LEVEL_DUNGEON levels we have std::map<level_id, LevelInfo>::const_iterator i = @@ -3661,39 +3664,39 @@ void TravelCache::save(FILE *file) const if (i->first.level_type != LEVEL_DUNGEON) continue; - i->first.save(file); - i->second.save(file); + i->first.save(outf); + i->second.save(outf); } for (int wp = 0; wp < TRAVEL_WAYPOINT_COUNT; ++wp) - waypoints[wp].save(file); + waypoints[wp].save(outf); } -void TravelCache::load(FILE *file) +void TravelCache::load(reader& inf) { levels.clear(); // Check version. If not compatible, we just ignore the file altogether. - unsigned char major = readByte(file), - minor = readByte(file); + unsigned char major = unmarshallByte(inf), + minor = unmarshallByte(inf); if (major != TC_MAJOR_VERSION || minor != TC_MINOR_VERSION) return ; - int level_count = readShort(file); + int level_count = unmarshallShort(inf); for (int i = 0; i < level_count; ++i) { level_id id; - id.load(file); + id.load(inf); LevelInfo linfo; // Must set id before load, or travel_hell_entry will not be // correctly set. linfo.id = id; - linfo.load(file); + linfo.load(inf); levels[id] = linfo; } for (int wp = 0; wp < TRAVEL_WAYPOINT_COUNT; ++wp) - waypoints[wp].load(file); + waypoints[wp].load(inf); fixup_levels(); } diff --git a/crawl-ref/source/travel.h b/crawl-ref/source/travel.h index c0ce805db7..f9f4b616d1 100644 --- a/crawl-ref/source/travel.h +++ b/crawl-ref/source/travel.h @@ -10,13 +10,16 @@ * <1> -/--/-- SD Created */ #ifndef TRAVEL_H -# define TRAVEL_H +#define TRAVEL_H -# include "externs.h" -# include <stdio.h> -# include <string> -# include <vector> -# include <map> +#include "externs.h" +#include <stdio.h> +#include <string> +#include <vector> +#include <map> + +class reader; +class writer; enum run_check_type { @@ -257,8 +260,8 @@ public: return (branch < id.branch) || (branch==id.branch && depth < id.depth); } - void save(FILE *) const; - void load(FILE *); + void save(writer&) const; + void load(reader&); }; // A position on a particular level. @@ -309,8 +312,8 @@ struct level_pos pos = coord_def(-1, -1); } - void save(FILE *) const; - void load(FILE *); + void save(writer&) const; + void load(reader&); }; struct travel_target @@ -409,8 +412,8 @@ public: void clear_distance() { distance = -1; } - void save(FILE *) const; - void load(FILE *); + void save(writer&) const; + void load(reader&); std::string describe() const; @@ -440,8 +443,8 @@ struct LevelInfo { } - void save(FILE *) const; - void load(FILE *); + void save(writer&) const; + void load(reader&); std::vector<stair_info> &get_stairs() { @@ -548,8 +551,8 @@ public: void update(); - void save(FILE *) const; - void load(FILE *); + void save(writer&) const; + void load(reader&); bool is_known_branch(unsigned char branch) const; diff --git a/crawl-ref/source/tutorial.cc b/crawl-ref/source/tutorial.cc index b43aef4b8d..8d0e910f1e 100644 --- a/crawl-ref/source/tutorial.cc +++ b/crawl-ref/source/tutorial.cc @@ -16,7 +16,6 @@ #include "tutorial.h" #include "cio.h" #include "command.h" -#include "files.h" #include "food.h" #include "format.h" #include "initfile.h" @@ -37,6 +36,7 @@ #include "spl-book.h" #include "spl-util.h" #include "stuff.h" +#include "tags.h" #include "terrain.h" #ifdef USE_TILE #include "tiles.h" @@ -60,26 +60,26 @@ static int get_tutorial_cols() #endif } -void save_tutorial( FILE* fp ) +void save_tutorial(writer& outf) { - writeLong( fp, TUTORIAL_VERSION); - writeShort( fp, Options.tutorial_type); + marshallLong( outf, TUTORIAL_VERSION); + marshallShort( outf, Options.tutorial_type); for ( long i = 0; i < TUT_EVENTS_NUM; ++i ) - writeShort( fp, Options.tutorial_events[i] ); + marshallShort( outf, Options.tutorial_events[i] ); } -void load_tutorial( FILE* fp ) +void load_tutorial(reader& inf) { Options.tutorial_left = 0; - int version = readLong(fp); + int version = unmarshallLong(inf); if (version != TUTORIAL_VERSION) return; - Options.tutorial_type = readShort(fp); + Options.tutorial_type = unmarshallShort(inf); for ( long i = 0; i < TUT_EVENTS_NUM; ++i ) { - Options.tutorial_events[i] = readShort(fp); + Options.tutorial_events[i] = unmarshallShort(inf); Options.tutorial_left += Options.tutorial_events[i]; } } diff --git a/crawl-ref/source/tutorial.h b/crawl-ref/source/tutorial.h index 4f4bc7aeb5..ef3f8bbb32 100644 --- a/crawl-ref/source/tutorial.h +++ b/crawl-ref/source/tutorial.h @@ -20,9 +20,11 @@ #include "externs.h" class formatted_string; +class writer; +class reader; -void save_tutorial( FILE* fp ); -void load_tutorial( FILE* fp ); +void save_tutorial(writer& outf); +void load_tutorial(reader& inf); void init_tutorial_options(void); bool pick_tutorial(void); |