diff --git a/hprint.cpp b/hprint.cpp index fcfa59d3..e2789ac0 100644 --- a/hprint.cpp +++ b/hprint.cpp @@ -76,12 +76,15 @@ EX string get_stamp() { inline string ONOFF(bool b) { return b ? XLAT("ON") : XLAT("OFF"); } struct hstream { + color_t vernum; virtual void write_char(char c) = 0; virtual void write_chars(const char* c, size_t q) { while(q--) write_char(*(c++)); } virtual char read_char() = 0; virtual void read_chars(char* c, size_t q) { while(q--) *(c++) = read_char(); } - virtual color_t get_vernum() { return VERNUM_HEX; } + virtual color_t get_vernum() { return vernum; } virtual void flush() {} + + hstream() { vernum = VERNUM_HEX; } template void write(const T& t) { hwrite(*this, t); } template void read(T& t) { hread(*this, t); } @@ -139,12 +142,10 @@ template void hread(hstream& hs, C& c, C1& c1, C struct hstream_exception : hr_exception { hstream_exception() {} }; struct fhstream : hstream { - color_t vernum; FILE *f; - explicit fhstream() { f = NULL; vernum = VERNUM_HEX; } + explicit fhstream() { f = NULL; } explicit fhstream(const string pathname, const char *mode) { f = fopen(pathname.c_str(), mode); vernum = VERNUM_HEX; } ~fhstream() { if(f) fclose(f); } - color_t get_vernum() override { return vernum; } void write_char(char c) override { write_chars(&c, 1); } void write_chars(const char* c, size_t i) override { if(fwrite(c, i, 1, f) != 1) throw hstream_exception(); } void read_chars(char* c, size_t i) override { if(fread(c, i, 1, f) != 1) throw hstream_exception(); } @@ -153,11 +154,9 @@ struct fhstream : hstream { }; struct shstream : hstream { - color_t vernum; string s; int pos; explicit shstream(const string& t = "") : s(t) { pos = 0; vernum = VERNUM_HEX; } - color_t get_vernum() override { return vernum; } void write_char(char c) override { s += c; } char read_char() override { if(pos == isize(s)) throw hstream_exception(); return s[pos++]; } }; diff --git a/mapeditor.cpp b/mapeditor.cpp index d6320d3a..8cd02731 100644 --- a/mapeditor.cpp +++ b/mapeditor.cpp @@ -395,7 +395,7 @@ EX namespace mapstream { EX vector cellbyid; EX vector relspin; - void load_drawing_tool(fhstream& hs) { + void load_drawing_tool(hstream& hs) { using namespace mapeditor; if(hs.vernum < 0xA82A) return; int i = hs.get(); @@ -680,15 +680,15 @@ EX namespace mapstream { geometry_settings(was_default); } - EX hookset hooks_savemap, hooks_loadmap_old; - EX hookset hooks_loadmap; + EX hookset hooks_savemap, hooks_loadmap_old; + EX hookset hooks_loadmap; EX cell *save_start() { return (closed_manifold || euclid || prod || arcm::in() || sol || INVERSE) ? currentmap->gamestart() : cwt.at->master->c7; } #if CAP_EDIT - void save_only_map(fhstream& f) { + void save_only_map(hstream& f) { f.write(patterns::whichPattern); save_geometry(f); @@ -824,7 +824,7 @@ EX namespace mapstream { cellbyid.clear(); } - void load_usershapes(fhstream& f) { + void load_usershapes(hstream& f) { if(f.vernum >= 7400) while(true) { int i = f.get(); if(i == -1) break; @@ -854,7 +854,7 @@ EX namespace mapstream { } } - void load_only_map(fhstream& f) { + void load_only_map(hstream& f) { stop_game(); if(f.vernum >= 10420 && f.vernum < 10503) { int i; @@ -1073,7 +1073,7 @@ EX namespace mapstream { game_active = true; } - void save_usershapes(fhstream& f) { + void save_usershapes(hstream& f) { int32_t n; #if CAP_POLY for(int i=0; i= 4 && CAP_RAY int q = intra::in ? isize(intra::data) : 0; @@ -1125,12 +1130,15 @@ EX namespace mapstream { dual::split_or_do([&] { save_only_map(f); }); } save_usershapes(f); - return true; } EX bool loadMap(const string& fname) { fhstream f(fname, "rb"); if(!f.f) return false; + return loadMap(f); + } + + EX bool loadMap(hstream& f) { f.read(f.vernum); if(f.vernum > 10505 && f.vernum < 11000) f.vernum = 11005;