13
13
14
14
#include " lcf/ldb/reader.h"
15
15
#include " lcf/ldb/chunks.h"
16
- #include " lcf/data.h"
17
16
#include " lcf/reader_util.h"
18
17
#include " reader_struct.h"
19
18
@@ -23,108 +22,117 @@ void LDB_Reader::PrepareSave(rpg::Database& db) {
23
22
++db.system .save_count ;
24
23
}
25
24
26
- bool LDB_Reader::Load (const std::string& filename, const std::string& encoding) {
25
+ std::unique_ptr<lcf::rpg::Database> LDB_Reader::Load (const std::string& filename, const std::string& encoding) {
27
26
std::ifstream stream (filename.c_str (), std::ios::binary);
28
27
if (!stream.is_open ()) {
29
28
fprintf (stderr, " Failed to open LDB file `%s' for reading : %s\n " , filename.c_str (), strerror (errno));
30
- return false ;
29
+ return nullptr ;
31
30
}
32
31
return LDB_Reader::Load (stream, encoding);
33
32
}
34
33
35
- bool LDB_Reader::Save (const std::string& filename, const std::string& encoding, SaveOpt opt) {
34
+ bool LDB_Reader::Save (const std::string& filename, const lcf::rpg::Database& db, const std::string& encoding, SaveOpt opt) {
36
35
std::ofstream stream (filename.c_str (), std::ios::binary);
37
36
if (!stream.is_open ()) {
38
37
fprintf (stderr, " Failed to open LDB file `%s' for writing : %s\n " , filename.c_str (), strerror (errno));
39
38
return false ;
40
39
}
41
- return LDB_Reader::Save (stream, encoding, opt);
40
+ return LDB_Reader::Save (stream, db, encoding, opt);
42
41
}
43
42
44
- bool LDB_Reader::SaveXml (const std::string& filename) {
43
+ bool LDB_Reader::SaveXml (const std::string& filename, const lcf::rpg::Database& db ) {
45
44
std::ofstream stream (filename.c_str (), std::ios::binary);
46
45
if (!stream.is_open ()) {
47
46
fprintf (stderr, " Failed to open LDB XML file `%s' for writing : %s\n " , filename.c_str (), strerror (errno));
48
47
return false ;
49
48
}
50
- return LDB_Reader::SaveXml (stream);
49
+ return LDB_Reader::SaveXml (stream, db );
51
50
}
52
51
53
- bool LDB_Reader::LoadXml (const std::string& filename) {
52
+ std::unique_ptr<lcf::rpg::Database> LDB_Reader::LoadXml (const std::string& filename) {
54
53
std::ifstream stream (filename.c_str (), std::ios::binary);
55
54
if (!stream.is_open ()) {
56
55
fprintf (stderr, " Failed to open LDB XML file `%s' for reading : %s\n " , filename.c_str (), strerror (errno));
57
- return false ;
56
+ return nullptr ;
58
57
}
59
58
return LDB_Reader::LoadXml (stream);
60
59
}
61
60
62
- bool LDB_Reader::Load (std::istream& filestream, const std::string& encoding) {
61
+ std::unique_ptr<lcf::rpg::Database> LDB_Reader::Load (std::istream& filestream, const std::string& encoding) {
63
62
LcfReader reader (filestream, encoding);
64
63
if (!reader.IsOk ()) {
65
64
LcfReader::SetError (" Couldn't parse database file.\n " );
66
- return false ;
65
+ return nullptr ;
67
66
}
68
67
std::string header;
69
68
reader.ReadString (header, reader.ReadInt ());
70
69
if (header.length () != 11 ) {
71
70
LcfReader::SetError (" This is not a valid RPG2000 database.\n " );
72
- return false ;
71
+ return nullptr ;
73
72
}
74
73
if (header != " LcfDataBase" ) {
75
74
fprintf (stderr, " Warning: This header is not LcfDataBase and might not be a valid RPG2000 database.\n " );
76
75
}
77
- Data::data.ldb_header = header;
78
- TypeReader<rpg::Database>::ReadLcf (Data::data, reader, 0 );
76
+ auto db = std::make_unique<lcf::rpg::Database>();
77
+ db->ldb_header = header;
78
+ TypeReader<rpg::Database>::ReadLcf (*db, reader, 0 );
79
79
80
80
// Delayed initialization of some actor fields because they are engine
81
81
// dependent
82
- for (auto & actor: Data:: actors) {
83
- actor.Setup (Data:: system .ldb_id == 2003 );
82
+ for (auto & actor: db-> actors ) {
83
+ actor.Setup (db-> system .ldb_id == 2003 );
84
84
}
85
85
86
- return true ;
86
+ return db ;
87
87
}
88
88
89
- bool LDB_Reader::Save (std::ostream& filestream, const std::string& encoding, SaveOpt opt) {
90
- LcfWriter writer (filestream, Data:: system .ldb_id == 2003 , encoding);
89
+ bool LDB_Reader::Save (std::ostream& filestream, const lcf::rpg::Database& db, const std::string& encoding, SaveOpt opt) {
90
+ LcfWriter writer (filestream, db. system .ldb_id == 2003 , encoding);
91
91
if (!writer.IsOk ()) {
92
92
LcfReader::SetError (" Couldn't parse database file.\n " );
93
93
return false ;
94
94
}
95
95
std::string header;
96
- if ( Data::data .ldb_header .empty () || !bool (opt & SaveOpt::ePreserveHeader)) {
96
+ if ( db .ldb_header .empty () || !bool (opt & SaveOpt::ePreserveHeader)) {
97
97
header = " LcfDataBase" ;
98
98
} else {
99
- header= Data::data .ldb_header ;
99
+ header= db .ldb_header ;
100
100
}
101
101
writer.WriteInt (header.size ());
102
102
writer.Write (header);
103
- TypeReader<rpg::Database>::WriteLcf (Data::data , writer);
103
+ TypeReader<rpg::Database>::WriteLcf (db , writer);
104
104
return true ;
105
105
}
106
106
107
- bool LDB_Reader::SaveXml (std::ostream& filestream) {
108
- XmlWriter writer (filestream, Data:: system .ldb_id == 2003 );
107
+ bool LDB_Reader::SaveXml (std::ostream& filestream, const lcf::rpg::Database& db ) {
108
+ XmlWriter writer (filestream, db. system .ldb_id == 2003 );
109
109
if (!writer.IsOk ()) {
110
110
LcfReader::SetError (" Couldn't parse database file.\n " );
111
111
return false ;
112
112
}
113
113
writer.BeginElement (" LDB" );
114
- TypeReader<rpg::Database>::WriteXml (Data::data , writer);
114
+ TypeReader<rpg::Database>::WriteXml (db , writer);
115
115
writer.EndElement (" LDB" );
116
116
return true ;
117
117
}
118
118
119
- bool LDB_Reader::LoadXml (std::istream& filestream) {
119
+ std::unique_ptr<lcf::rpg::Database> LDB_Reader::LoadXml (std::istream& filestream) {
120
120
XmlReader reader (filestream);
121
121
if (!reader.IsOk ()) {
122
122
LcfReader::SetError (" Couldn't parse database file.\n " );
123
- return false ;
123
+ return nullptr ; ;
124
124
}
125
- reader.SetHandler (new RootXmlHandler<rpg::Database>(Data::data, " LDB" ));
125
+ auto db = std::make_unique<lcf::rpg::Database>();
126
+ reader.SetHandler (new RootXmlHandler<rpg::Database>(*db, " LDB" ));
126
127
reader.Parse ();
127
- return true ;
128
+
129
+ // Delayed initialization of some actor fields because they are engine
130
+ // dependent
131
+ for (auto & actor: db->actors ) {
132
+ actor.Setup (db->system .ldb_id == 2003 );
133
+ }
134
+
135
+ return db;
128
136
}
129
137
130
138
} // namespace lcf
0 commit comments