Skip to content

Commit 9847d84

Browse files
committedJan 9, 2021
Remove all use of lcf::Data globals
1 parent 8bdca2e commit 9847d84

12 files changed

+204
-199
lines changed
 

‎bench/readldb.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ int main(int argc, char** argv) {
99
return 1;
1010
}
1111
const auto& infile = argv[1];
12-
auto rc = LDB_Reader::Load(infile, "");
13-
if (!rc) {
12+
auto db = LDB_Reader::Load(infile, "");
13+
if (db == nullptr) {
1414
std::cerr << "Failed to load file : " << infile << std::endl;
1515
return 1;
1616
}

‎src/lcf/ldb/reader.h

+9-28
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,7 @@
1212

1313
#include <string>
1414
#include <vector>
15-
#include "lcf/rpg/actor.h"
16-
#include "lcf/rpg/skill.h"
17-
#include "lcf/rpg/item.h"
18-
#include "lcf/rpg/enemy.h"
19-
#include "lcf/rpg/troop.h"
20-
#include "lcf/rpg/attribute.h"
21-
#include "lcf/rpg/state.h"
22-
#include "lcf/rpg/terrain.h"
23-
#include "lcf/rpg/chipset.h"
24-
#include "lcf/rpg/animation.h"
25-
#include "lcf/rpg/terms.h"
26-
#include "lcf/rpg/system.h"
27-
#include "lcf/rpg/commonevent.h"
28-
#include "lcf/rpg/music.h"
29-
#include "lcf/rpg/sound.h"
30-
#include "lcf/rpg/class.h"
31-
#include "lcf/rpg/battlecommand.h"
32-
#include "lcf/rpg/battleranimation.h"
33-
#include "lcf/rpg/parameters.h"
34-
#include "lcf/rpg/equipment.h"
15+
#include <memory>
3516
#include "lcf/rpg/database.h"
3617
#include "lcf/saveopt.h"
3718

@@ -49,42 +30,42 @@ namespace LDB_Reader {
4930
/**
5031
* Loads Database.
5132
*/
52-
bool Load(const std::string& filename, const std::string& encoding);
33+
std::unique_ptr<lcf::rpg::Database> Load(const std::string& filename, const std::string& encoding);
5334

5435
/**
5536
* Saves Database.
5637
*/
57-
bool Save(const std::string& filename, const std::string& encoding, SaveOpt opt = SaveOpt::eNone);
38+
bool Save(const std::string& filename, const lcf::rpg::Database& db, const std::string& encoding, SaveOpt opt = SaveOpt::eNone);
5839

5940
/**
6041
* Saves Database as XML.
6142
*/
62-
bool SaveXml(const std::string& filename);
43+
bool SaveXml(const std::string& filename, const lcf::rpg::Database& db);
6344

6445
/**
6546
* Load Database as XML.
6647
*/
67-
bool LoadXml(const std::string& filename);
48+
std::unique_ptr<lcf::rpg::Database> LoadXml(const std::string& filename);
6849

6950
/**
7051
* Loads Database.
7152
*/
72-
bool Load(std::istream& filestream, const std::string& encoding);
53+
std::unique_ptr<lcf::rpg::Database> Load(std::istream& filestream, const std::string& encoding);
7354

7455
/**
7556
* Saves Database.
7657
*/
77-
bool Save(std::ostream& filestream, const std::string& encoding, SaveOpt opt = SaveOpt::eNone);
58+
bool Save(std::ostream& filestream, const lcf::rpg::Database& db, const std::string& encoding, SaveOpt opt = SaveOpt::eNone);
7859

7960
/**
8061
* Saves Database as XML.
8162
*/
82-
bool SaveXml(std::ostream& filestream);
63+
bool SaveXml(std::ostream& filestream, const lcf::rpg::Database& db);
8364

8465
/**
8566
* Load Database as XML.
8667
*/
87-
bool LoadXml(std::istream& filestream);
68+
std::unique_ptr<lcf::rpg::Database> LoadXml(std::istream& filestream);
8869
}
8970

9071
} // namespace lcf

‎src/lcf/lmt/reader.h

+9-11
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
#define LCF_LMT_READER_H
1212

1313
#include <string>
14-
#include "lcf/reader_lcf.h"
15-
#include "lcf/writer_lcf.h"
16-
#include "lcf/writer_xml.h"
14+
#include <memory>
1715
#include "lcf/rpg/treemap.h"
1816
#include "lcf/saveopt.h"
1917

@@ -27,42 +25,42 @@ namespace LMT_Reader {
2725
/**
2826
* Loads Map Tree.
2927
*/
30-
bool Load(const std::string& filename, const std::string &encoding);
28+
std::unique_ptr<lcf::rpg::TreeMap> Load(const std::string& filename, const std::string &encoding);
3129

3230
/**
3331
* Saves Map Tree.
3432
*/
35-
bool Save(const std::string& filename, const std::string &encoding, SaveOpt opt = SaveOpt::eNone);
33+
bool Save(const std::string& filename, const lcf::rpg::TreeMap& tmap, bool is2k3, const std::string &encoding, SaveOpt opt = SaveOpt::eNone);
3634

3735
/**
3836
* Saves Map Tree as XML.
3937
*/
40-
bool SaveXml(const std::string& filename);
38+
bool SaveXml(const std::string& filename, const lcf::rpg::TreeMap& tmap, bool is2k3);
4139

4240
/**
4341
* Loads Map Tree as XML.
4442
*/
45-
bool LoadXml(const std::string& filename);
43+
std::unique_ptr<lcf::rpg::TreeMap> LoadXml(const std::string& filename);
4644

4745
/**
4846
* Loads Map Tree.
4947
*/
50-
bool Load(std::istream& filestream, const std::string &encoding);
48+
std::unique_ptr<lcf::rpg::TreeMap> Load(std::istream& filestream, const std::string &encoding);
5149

5250
/**
5351
* Saves Map Tree.
5452
*/
55-
bool Save(std::ostream& filestream, const std::string &encoding, SaveOpt opt = SaveOpt::eNone);
53+
bool Save(std::ostream& filestream, const lcf::rpg::TreeMap& tmap, bool is2k3, const std::string &encoding, SaveOpt opt = SaveOpt::eNone);
5654

5755
/**
5856
* Saves Map Tree as XML.
5957
*/
60-
bool SaveXml(std::ostream& filestream);
58+
bool SaveXml(std::ostream& filestream, const lcf::rpg::TreeMap& tmap, bool is2k3);
6159

6260
/**
6361
* Loads Map Tree as XML.
6462
*/
65-
bool LoadXml(std::istream& filestream);
63+
std::unique_ptr<lcf::rpg::TreeMap> LoadXml(std::istream& filestream);
6664
}
6765

6866
} //namespace lcf

‎src/lcf/lmu/reader.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ namespace LMU_Reader {
3535
/**
3636
* Saves map.
3737
*/
38-
bool Save(const std::string& filename, const rpg::Map& map, const std::string& encoding, SaveOpt opt = SaveOpt::eNone);
38+
bool Save(const std::string& filename, const rpg::Map& map, bool is2k3, const std::string& encoding, SaveOpt opt = SaveOpt::eNone);
3939

4040
/**
4141
* Saves map as XML.
4242
*/
43-
bool SaveXml(const std::string& filename, const rpg::Map& map);
43+
bool SaveXml(const std::string& filename, const rpg::Map& map, bool is2k3);
4444

4545
/**
4646
* Loads map as XML.
@@ -55,12 +55,12 @@ namespace LMU_Reader {
5555
/**
5656
* Saves map.
5757
*/
58-
bool Save(std::ostream& filestream, const rpg::Map& map, const std::string& encoding, SaveOpt opt = SaveOpt::eNone);
58+
bool Save(std::ostream& filestream, const rpg::Map& map, bool is2k3, const std::string& encoding, SaveOpt opt = SaveOpt::eNone);
5959

6060
/**
6161
* Saves map as XML.
6262
*/
63-
bool SaveXml(std::ostream& filestream, const rpg::Map& map);
63+
bool SaveXml(std::ostream& filestream, const rpg::Map& map, bool is2k3);
6464

6565
/**
6666
* Loads map as XML.

‎src/lcf/lsd/reader.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ namespace LSD_Reader {
5151
/**
5252
* Saves Savegame.
5353
*/
54-
bool Save(const std::string& filename, const rpg::Save& save, const std::string &encoding);
54+
bool Save(const std::string& filename, const rpg::Save& save, bool is2k3, const std::string &encoding);
5555

5656
/*
5757
* Saves Savegame as XML.
5858
*/
59-
bool SaveXml(const std::string& filename, const rpg::Save& save);
59+
bool SaveXml(const std::string& filename, const rpg::Save& save, bool is2k3);
6060

6161
/**
6262
* Loads Savegame as XML.
@@ -71,12 +71,12 @@ namespace LSD_Reader {
7171
/**
7272
* Saves Savegame.
7373
*/
74-
bool Save(std::ostream& filestream, const rpg::Save& save, const std::string &encoding);
74+
bool Save(std::ostream& filestream, const rpg::Save& save, bool is2k3, const std::string &encoding);
7575

7676
/*
7777
* Saves Savegame as XML.
7878
*/
79-
bool SaveXml(std::ostream& filestream, const rpg::Save& save);
79+
bool SaveXml(std::ostream& filestream, const rpg::Save& save, bool is2k3);
8080

8181
/**
8282
* Loads Savegame as XML.

‎src/ldb_reader.cpp

+37-29
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#include "lcf/ldb/reader.h"
1515
#include "lcf/ldb/chunks.h"
16-
#include "lcf/data.h"
1716
#include "lcf/reader_util.h"
1817
#include "reader_struct.h"
1918

@@ -23,108 +22,117 @@ void LDB_Reader::PrepareSave(rpg::Database& db) {
2322
++db.system.save_count;
2423
}
2524

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) {
2726
std::ifstream stream(filename.c_str(), std::ios::binary);
2827
if (!stream.is_open()) {
2928
fprintf(stderr, "Failed to open LDB file `%s' for reading : %s\n", filename.c_str(), strerror(errno));
30-
return false;
29+
return nullptr;
3130
}
3231
return LDB_Reader::Load(stream, encoding);
3332
}
3433

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) {
3635
std::ofstream stream(filename.c_str(), std::ios::binary);
3736
if (!stream.is_open()) {
3837
fprintf(stderr, "Failed to open LDB file `%s' for writing : %s\n", filename.c_str(), strerror(errno));
3938
return false;
4039
}
41-
return LDB_Reader::Save(stream, encoding, opt);
40+
return LDB_Reader::Save(stream, db, encoding, opt);
4241
}
4342

44-
bool LDB_Reader::SaveXml(const std::string& filename) {
43+
bool LDB_Reader::SaveXml(const std::string& filename, const lcf::rpg::Database& db) {
4544
std::ofstream stream(filename.c_str(), std::ios::binary);
4645
if (!stream.is_open()) {
4746
fprintf(stderr, "Failed to open LDB XML file `%s' for writing : %s\n", filename.c_str(), strerror(errno));
4847
return false;
4948
}
50-
return LDB_Reader::SaveXml(stream);
49+
return LDB_Reader::SaveXml(stream, db);
5150
}
5251

53-
bool LDB_Reader::LoadXml(const std::string& filename) {
52+
std::unique_ptr<lcf::rpg::Database> LDB_Reader::LoadXml(const std::string& filename) {
5453
std::ifstream stream(filename.c_str(), std::ios::binary);
5554
if (!stream.is_open()) {
5655
fprintf(stderr, "Failed to open LDB XML file `%s' for reading : %s\n", filename.c_str(), strerror(errno));
57-
return false;
56+
return nullptr;
5857
}
5958
return LDB_Reader::LoadXml(stream);
6059
}
6160

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) {
6362
LcfReader reader(filestream, encoding);
6463
if (!reader.IsOk()) {
6564
LcfReader::SetError("Couldn't parse database file.\n");
66-
return false;
65+
return nullptr;
6766
}
6867
std::string header;
6968
reader.ReadString(header, reader.ReadInt());
7069
if (header.length() != 11) {
7170
LcfReader::SetError("This is not a valid RPG2000 database.\n");
72-
return false;
71+
return nullptr;
7372
}
7473
if (header != "LcfDataBase") {
7574
fprintf(stderr, "Warning: This header is not LcfDataBase and might not be a valid RPG2000 database.\n");
7675
}
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);
7979

8080
// Delayed initialization of some actor fields because they are engine
8181
// 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);
8484
}
8585

86-
return true;
86+
return db;
8787
}
8888

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);
9191
if (!writer.IsOk()) {
9292
LcfReader::SetError("Couldn't parse database file.\n");
9393
return false;
9494
}
9595
std::string header;
96-
if ( Data::data.ldb_header.empty() || !bool(opt & SaveOpt::ePreserveHeader)) {
96+
if ( db.ldb_header.empty() || !bool(opt & SaveOpt::ePreserveHeader)) {
9797
header = "LcfDataBase";
9898
} else {
99-
header= Data::data.ldb_header;
99+
header= db.ldb_header;
100100
}
101101
writer.WriteInt(header.size());
102102
writer.Write(header);
103-
TypeReader<rpg::Database>::WriteLcf(Data::data, writer);
103+
TypeReader<rpg::Database>::WriteLcf(db, writer);
104104
return true;
105105
}
106106

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);
109109
if (!writer.IsOk()) {
110110
LcfReader::SetError("Couldn't parse database file.\n");
111111
return false;
112112
}
113113
writer.BeginElement("LDB");
114-
TypeReader<rpg::Database>::WriteXml(Data::data, writer);
114+
TypeReader<rpg::Database>::WriteXml(db, writer);
115115
writer.EndElement("LDB");
116116
return true;
117117
}
118118

119-
bool LDB_Reader::LoadXml(std::istream& filestream) {
119+
std::unique_ptr<lcf::rpg::Database> LDB_Reader::LoadXml(std::istream& filestream) {
120120
XmlReader reader(filestream);
121121
if (!reader.IsOk()) {
122122
LcfReader::SetError("Couldn't parse database file.\n");
123-
return false;
123+
return nullptr;;
124124
}
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"));
126127
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;
128136
}
129137

130138
} // namespace lcf

0 commit comments

Comments
 (0)
Please sign in to comment.