-
Notifications
You must be signed in to change notification settings - Fork 51
/
lsd_reader.cpp
111 lines (95 loc) · 3.44 KB
/
lsd_reader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* This file is part of liblcf. Copyright (c) 2017 liblcf authors.
* https://github.com/EasyRPG/liblcf - https://easyrpg.org
*
* liblcf is Free/Libre Open Source Software, released under the MIT License.
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code.
*/
#include <cmath>
#include <fstream>
#include "lsd_reader.h"
#include "lsd_chunks.h"
#include "ldb_reader.h"
#include "rpg_save.h"
#include "reader_util.h"
#include "reader_struct.h"
double LSD_Reader::ToTDateTime(std::time_t const t) {
// 25569 is UnixDateDelta: number of days between 1970-01-01 and 1900-01-01
return(t / 86400.0 + 25569.0);
}
std::time_t LSD_Reader::ToUnixTimestamp(double const ms) {
return(std::time_t(ms * 86400.0 - 25569.0 * 86400.0 + 0.5));
}
double LSD_Reader::GenerateTimestamp(std::time_t const t) {
return ToTDateTime(t);
}
std::unique_ptr<RPG::Save> LSD_Reader::Load(const std::string& filename, const std::string& encoding) {
std::ifstream stream(filename.c_str(), std::ios::binary);
return LSD_Reader::Load(stream, encoding);
}
bool LSD_Reader::Save(const std::string& filename, const RPG::Save& save, const std::string& encoding) {
std::ofstream stream(filename.c_str(), std::ios::binary);
return LSD_Reader::Save(stream, save, encoding);
}
bool LSD_Reader::SaveXml(const std::string& filename, const RPG::Save& save) {
std::ofstream stream(filename.c_str(), std::ios::binary);
return LSD_Reader::SaveXml(stream, save);
}
std::unique_ptr<RPG::Save> LSD_Reader::LoadXml(const std::string& filename) {
std::ifstream stream(filename.c_str(), std::ios::binary);
return LSD_Reader::LoadXml(stream);
}
std::unique_ptr<RPG::Save> LSD_Reader::Load(std::istream& filestream, const std::string &encoding) {
LcfReader reader(filestream, encoding);
if (!reader.IsOk()) {
LcfReader::SetError("Couldn't parse save file.\n");
return std::unique_ptr<RPG::Save>();
}
std::string header;
reader.ReadString(header, reader.ReadInt());
if (header.length() != 11) {
LcfReader::SetError("This is not a valid RPG2000 save.\n");
return std::unique_ptr<RPG::Save>();
}
if (header != "LcfSaveData") {
fprintf(stderr, "Warning: This header is not LcfSaveData and might not be a valid RPG2000 save.\n");
}
RPG::Save* save = new RPG::Save();
Struct<RPG::Save>::ReadLcf(*save, reader);
return std::unique_ptr<RPG::Save>(save);
}
bool LSD_Reader::Save(std::ostream& filestream, const RPG::Save& save, const std::string &encoding) {
LcfWriter writer(filestream, encoding);
if (!writer.IsOk()) {
LcfReader::SetError("Couldn't parse save file.\n");
return false;
}
const std::string header("LcfSaveData");
writer.WriteInt(header.size());
writer.Write(header);
Struct<RPG::Save>::WriteLcf(save, writer);
return true;
}
bool LSD_Reader::SaveXml(std::ostream& filestream, const RPG::Save& save) {
XmlWriter writer(filestream);
if (!writer.IsOk()) {
LcfReader::SetError("Couldn't parse save file.\n");
return false;
}
writer.BeginElement("LSD");
Struct<RPG::Save>::WriteXml(save, writer);
writer.EndElement("LSD");
return true;
}
std::unique_ptr<RPG::Save> LSD_Reader::LoadXml(std::istream& filestream) {
XmlReader reader(filestream);
if (!reader.IsOk()) {
LcfReader::SetError("Couldn't parse save file.\n");
return std::unique_ptr<RPG::Save>();
}
RPG::Save* save = new RPG::Save();
reader.SetHandler(new RootXmlHandler<RPG::Save>(*save, "LSD"));
reader.Parse();
return std::unique_ptr<RPG::Save>(save);
}