OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
savegameheader.cpp
Go to the documentation of this file.
1#include "savegameheader.h"
2#include "serialize.h"
3
4const char SaveGameHeader::tag[]="OpenGothic/Save";
5
6struct tm36 {
7 int32_t tm_sec;
8 int32_t tm_min;
9 int32_t tm_hour;
10 int32_t tm_mday;
11 int32_t tm_mon;
12 int32_t tm_year;
13 int32_t tm_wday;
14 int32_t tm_yday;
15 int32_t tm_isdst;
16 };
17
18static void readTm(Serialize& fin, std::tm& i, uint16_t version) {
19 if(version<=38) {
20 // NOTE: apparently std::tm is not binary same across different C++ compillers
21 // 20 instead of 36
22 fin.readBytes(&i,sizeof(i));
23 return;
24 }
25 tm36 tx = {};
26 fin.readBytes(&tx,sizeof(tx));
27
28 i.tm_sec = tx.tm_sec;
29 i.tm_min = tx.tm_min;
30 i.tm_hour = tx.tm_hour;
31 i.tm_mday = tx.tm_mday;
32 i.tm_mon = tx.tm_mon;
33 i.tm_year = tx.tm_year;
34 i.tm_wday = tx.tm_wday;
35 i.tm_yday = tx.tm_yday;
36 i.tm_isdst = tx.tm_isdst;
37 }
38
39static void writeTm(Serialize& fout, const std::tm& i) {
40 tm36 tx = {};
41 tx.tm_sec = i.tm_sec;
42 tx.tm_min = i.tm_min;
43 tx.tm_hour = i.tm_hour;
44 tx.tm_mday = i.tm_mday;
45 tx.tm_mon = i.tm_mon;
46 tx.tm_year = i.tm_year;
47 tx.tm_wday = i.tm_wday;
48 tx.tm_yday = i.tm_yday;
49 tx.tm_isdst = i.tm_isdst;
50
51 fout.writeBytes(&tx,sizeof(tx));
52 }
53
55 char buf[sizeof(tag)]={};
56 fin.readBytes(buf,sizeof(buf));
57 if(std::memcmp(tag,buf,sizeof(buf))!=0)
58 throw std::runtime_error("invalid file format");
61 //char b[40]; fin.readBytes(b,20);
63 }
64
66 fout.writeBytes(tag,sizeof(tag));
67 fout.write(version,name,world);
68 writeTm(fout,pcTime);
70 }
std::string world
SaveGameHeader()=default
void save(Serialize &fout) const
std::string name
void write(const Arg &... a)
Definition serialize.h:76
void writeBytes(const void *v, size_t sz)
void readBytes(void *v, size_t sz)
void read(Arg &... a)
Definition serialize.h:81
static void writeTm(Serialize &fout, const std::tm &i)
static void readTm(Serialize &fin, std::tm &i, uint16_t version)
int32_t tm_sec
int32_t tm_isdst
int32_t tm_wday
int32_t tm_yday
int32_t tm_mday
int32_t tm_year
int32_t tm_min
int32_t tm_mon
int32_t tm_hour