1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-12-18 23:10:26 +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"); }
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<class T> void write(const T& t) { hwrite(*this, t); }
template<class T> void read(T& t) { hread(*this, 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 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++]; }
};

View File

@ -395,7 +395,7 @@ EX namespace mapstream {
EX vector<cell*> cellbyid;
EX vector<char> 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<int>();
@ -680,15 +680,15 @@ EX namespace mapstream {
geometry_settings(was_default);
}
EX hookset<void(fhstream&)> hooks_savemap, hooks_loadmap_old;
EX hookset<void(fhstream&, int)> hooks_loadmap;
EX hookset<void(hstream&)> hooks_savemap, hooks_loadmap_old;
EX hookset<void(hstream&, int)> 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<int>();
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<mapeditor::USERSHAPEGROUPS; i++) for(auto usp: usershapes[i]) {
@ -1098,7 +1098,12 @@ EX namespace mapstream {
EX bool saveMap(const char *fname) {
fhstream f(fname, "wb");
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);
#if MAXMDIM >= 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;