1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-12-21 08:00:25 +00:00

mapstreams:: now work with sstream too

This commit is contained in:
Zeno Rogue 2022-06-16 23:06:49 +02:00
parent 45e80ca5b5
commit 86d1f109bc
2 changed files with 22 additions and 15 deletions

View File

@ -76,13 +76,16 @@ EX string get_stamp() {
inline string ONOFF(bool b) { return b ? XLAT("ON") : XLAT("OFF"); } inline string ONOFF(bool b) { return b ? XLAT("ON") : XLAT("OFF"); }
struct hstream { struct hstream {
color_t vernum;
virtual void write_char(char c) = 0; virtual void write_char(char c) = 0;
virtual void write_chars(const char* c, size_t q) { while(q--) write_char(*(c++)); } virtual void write_chars(const char* c, size_t q) { while(q--) write_char(*(c++)); }
virtual char read_char() = 0; virtual char read_char() = 0;
virtual void read_chars(char* c, size_t q) { while(q--) *(c++) = read_char(); } 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() {} virtual void flush() {}
hstream() { vernum = VERNUM_HEX; }
template<class T> void write(const T& t) { hwrite(*this, t); } template<class T> void write(const T& t) { hwrite(*this, t); }
template<class T> void read(T& t) { hread(*this, t); } template<class T> void read(T& t) { hread(*this, t); }
template<class T> T get() { T t; hread(*this, t); return t; } template<class T> T get() { T t; hread(*this, t); return t; }
@ -139,12 +142,10 @@ template<class C, class C1, class... CS> void hread(hstream& hs, C& c, C1& c1, C
struct hstream_exception : hr_exception { hstream_exception() {} }; struct hstream_exception : hr_exception { hstream_exception() {} };
struct fhstream : hstream { struct fhstream : hstream {
color_t vernum;
FILE *f; 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; } explicit fhstream(const string pathname, const char *mode) { f = fopen(pathname.c_str(), mode); vernum = VERNUM_HEX; }
~fhstream() { if(f) fclose(f); } ~fhstream() { if(f) fclose(f); }
color_t get_vernum() override { return vernum; }
void write_char(char c) override { write_chars(&c, 1); } 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 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(); } 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 { struct shstream : hstream {
color_t vernum;
string s; string s;
int pos; int pos;
explicit shstream(const string& t = "") : s(t) { pos = 0; vernum = VERNUM_HEX; } 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; } void write_char(char c) override { s += c; }
char read_char() override { if(pos == isize(s)) throw hstream_exception(); return s[pos++]; } char read_char() override { if(pos == isize(s)) throw hstream_exception(); return s[pos++]; }
}; };

View File

@ -395,7 +395,7 @@ EX namespace mapstream {
EX vector<cell*> cellbyid; EX vector<cell*> cellbyid;
EX vector<char> relspin; EX vector<char> relspin;
void load_drawing_tool(fhstream& hs) { void load_drawing_tool(hstream& hs) {
using namespace mapeditor; using namespace mapeditor;
if(hs.vernum < 0xA82A) return; if(hs.vernum < 0xA82A) return;
int i = hs.get<int>(); int i = hs.get<int>();
@ -680,15 +680,15 @@ EX namespace mapstream {
geometry_settings(was_default); geometry_settings(was_default);
} }
EX hookset<void(fhstream&)> hooks_savemap, hooks_loadmap_old; EX hookset<void(hstream&)> hooks_savemap, hooks_loadmap_old;
EX hookset<void(fhstream&, int)> hooks_loadmap; EX hookset<void(hstream&, int)> hooks_loadmap;
EX cell *save_start() { EX cell *save_start() {
return (closed_manifold || euclid || prod || arcm::in() || sol || INVERSE) ? currentmap->gamestart() : cwt.at->master->c7; return (closed_manifold || euclid || prod || arcm::in() || sol || INVERSE) ? currentmap->gamestart() : cwt.at->master->c7;
} }
#if CAP_EDIT #if CAP_EDIT
void save_only_map(fhstream& f) { void save_only_map(hstream& f) {
f.write(patterns::whichPattern); f.write(patterns::whichPattern);
save_geometry(f); save_geometry(f);
@ -824,7 +824,7 @@ EX namespace mapstream {
cellbyid.clear(); cellbyid.clear();
} }
void load_usershapes(fhstream& f) { void load_usershapes(hstream& f) {
if(f.vernum >= 7400) while(true) { if(f.vernum >= 7400) while(true) {
int i = f.get<int>(); int i = f.get<int>();
if(i == -1) break; 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(); stop_game();
if(f.vernum >= 10420 && f.vernum < 10503) { if(f.vernum >= 10420 && f.vernum < 10503) {
int i; int i;
@ -1073,7 +1073,7 @@ EX namespace mapstream {
game_active = true; game_active = true;
} }
void save_usershapes(fhstream& f) { void save_usershapes(hstream& f) {
int32_t n; int32_t n;
#if CAP_POLY #if CAP_POLY
for(int i=0; i<mapeditor::USERSHAPEGROUPS; i++) for(auto usp: usershapes[i]) { for(int i=0; i<mapeditor::USERSHAPEGROUPS; i++) for(auto usp: usershapes[i]) {
@ -1098,7 +1098,12 @@ EX namespace mapstream {
EX bool saveMap(const char *fname) { EX bool saveMap(const char *fname) {
fhstream f(fname, "wb"); fhstream f(fname, "wb");
if(!f.f) return false; if(!f.f) return false;
f.write(f.vernum); saveMap(f);
return true;
}
EX void saveMap(hstream& f) {
f.write(f.get_vernum());
f.write(dual::state); f.write(dual::state);
#if MAXMDIM >= 4 && CAP_RAY #if MAXMDIM >= 4 && CAP_RAY
int q = intra::in ? isize(intra::data) : 0; int q = intra::in ? isize(intra::data) : 0;
@ -1125,12 +1130,15 @@ EX namespace mapstream {
dual::split_or_do([&] { save_only_map(f); }); dual::split_or_do([&] { save_only_map(f); });
} }
save_usershapes(f); save_usershapes(f);
return true;
} }
EX bool loadMap(const string& fname) { EX bool loadMap(const string& fname) {
fhstream f(fname, "rb"); fhstream f(fname, "rb");
if(!f.f) return false; if(!f.f) return false;
return loadMap(f);
}
EX bool loadMap(hstream& f) {
f.read(f.vernum); f.read(f.vernum);
if(f.vernum > 10505 && f.vernum < 11000) if(f.vernum > 10505 && f.vernum < 11000)
f.vernum = 11005; f.vernum = 11005;