]> jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/libgameservgldl/src/DataLayer/File/FileMasterDAO.cpp
Consolidated all the individual level files into a single levels.dat file.
[irc/gameservirc.git] / gameserv-2.0 / libgameservgldl / src / DataLayer / File / FileMasterDAO.cpp
1 #include <GameServ/DataLayer/File/FileMasterDAO.h>
2 using GameServ::DataLayer::File::FileMasterDAO;
3
4 #include <GameServ/GameLayer/GameObjects/MasterGO.h>
5 using GameServ::GameLayer::GameObjects::MasterGO;
6
7 #include <GameServ/DataLayer/DataLayerExceptions.h>
8 using GameServ::DataLayer::Exceptions::ResourceException;
9 using GameServ::DataLayer::Exceptions::DataLayerException;
10
11 #include <GameServ/DataLayer/File/FileId.h>
12 using GameServ::DataLayer::File::FileId;
13
14 #include <boost/smart_ptr/shared_ptr.hpp>
15 #include <boost/format.hpp>
16 #include <boost/algorithm/string.hpp>
17 #include <boost/lexical_cast.hpp>
18 using boost::shared_ptr;
19 using boost::shared_static_cast;
20 using boost::format;
21 using boost::str;
22 using boost::lexical_cast;
23 using boost::bad_lexical_cast;
24 #include <boost/tokenizer.hpp>
25 typedef boost::tokenizer<boost::char_separator<char> >
26 tokenizer;
27
28 #include <string>
29 using std::string;
30
31 #include <fstream>
32 using std::ifstream;
33 using std::getline;
34 using std::ofstream;
35 using std::endl;
36
37 #include <map>
38 using std::map;
39
40 FileMasterDAO::FileMasterDAO() : mFilename("data\\masters.dat")
41 {
42 Initialize(GetMasterFilePath());
43 }
44
45 FileMasterDAO::FileMasterDAO(const string &filename)
46 {
47 Initialize(filename);
48 }
49
50 FileMasterDAO::~FileMasterDAO()
51 {
52 }
53
54 void FileMasterDAO::Initialize(const string &filename)
55 {
56 mFilename = filename;
57 LoadMasterCache();
58 }
59
60 void FileMasterDAO::LoadMasterCache(void)
61 {
62 assert(!mFilename.empty());
63 spMasterCache.clear();
64 ifstream infile;
65 infile.open(mFilename.c_str());
66 if (infile.fail())
67 {
68 throw DataLayerException(str(format("Unable to open %1% for loading Masters") % mFilename),
69 __FILE__, __LINE__);
70 }
71 while (!infile.eof())
72 {
73 string line;
74 shared_ptr<MasterGO> spMaster;
75 getline(infile, line);
76 if (line.empty())
77 {
78 continue;
79 }
80 else if (line[0] == '#')
81 {
82 continue;
83 }
84 spMaster = CreateMasterFromLine(line);
85 spMasterCache[spMaster->Id()] = spMaster;
86 }
87 infile.close();
88 }
89
90 string FileMasterDAO::GetMasterFilePath() const
91 {
92 return mFilename;
93 }
94
95 shared_ptr<MasterGO> FileMasterDAO::GetById(const string &Id) const
96 {
97 map<string, shared_ptr<MasterGO> >::const_iterator iter;
98 iter = spMasterCache.find(Id);
99 if (iter != spMasterCache.end())
100 {
101 return shared_ptr<MasterGO>(iter->second);
102 }
103 return shared_ptr<MasterGO>();
104 }
105
106 shared_ptr<MasterGO> FileMasterDAO::CreateMasterFromLine(const string &line) const
107 {
108 assert(!line.empty());
109 shared_ptr<MasterGO> spMaster;
110 spMaster = shared_ptr<MasterGO>(new MasterGO());
111 boost::char_separator<char> sep("~", 0);
112 tokenizer tokens(line, sep);
113 tokenizer::iterator tok_iter = tokens.begin();
114
115 // Level
116 try
117 {
118 spMaster->Level(lexical_cast<unsigned int>((*tok_iter)));
119 }
120 catch (bad_lexical_cast)
121 {
122 throw DataLayerException(str(format("Corrupt master line %1%. Invalid integer value %2%") %
123 line % (*tok_iter)));
124 }
125
126 // Name
127 tok_iter++;
128 if (tok_iter == tokens.end())
129 {
130 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
131 line), __FILE__, __LINE__);
132 }
133 spMaster->Name((*tok_iter));
134 spMaster->Id(FileId::CreateMasterId(spMaster->Level(), spMaster->Name()));
135
136 // Weapon
137 tok_iter++;
138 if (tok_iter == tokens.end())
139 {
140 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
141 line), __FILE__, __LINE__);
142 }
143 spMaster->WeaponName((*tok_iter));
144
145 // Strength
146 tok_iter++;
147 if (tok_iter == tokens.end())
148 {
149 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
150 line), __FILE__, __LINE__);
151 }
152 try
153 {
154 spMaster->Strength(lexical_cast<int>((*tok_iter)));
155 }
156 catch (bad_lexical_cast)
157 {
158 throw DataLayerException(str(format("Corrupt master line %1%. Invalid integer value %2%") %
159 line % (*tok_iter)));
160 }
161
162 // Defense
163 tok_iter++;
164 if (tok_iter == tokens.end())
165 {
166 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
167 line), __FILE__, __LINE__);
168 }
169 try
170 {
171 spMaster->Defense(lexical_cast<int>((*tok_iter)));
172 }
173 catch (bad_lexical_cast)
174 {
175 throw DataLayerException(str(format("Corrupt master line %1%. Invalid integer value %2%") %
176 line % (*tok_iter)));
177 }
178
179
180 // Gold
181 tok_iter++;
182 if (tok_iter == tokens.end())
183 {
184 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
185 line), __FILE__, __LINE__);
186 }
187 try
188 {
189 spMaster->Gold(lexical_cast<unsigned long int>((*tok_iter)));
190 }
191 catch (bad_lexical_cast)
192 {
193 throw DataLayerException(str(format("Corrupt master line %1%. Invalid integer value %2%") %
194 line % (*tok_iter)));
195 }
196
197 // Experience
198 tok_iter++;
199 if (tok_iter == tokens.end())
200 {
201 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
202 line), __FILE__, __LINE__);
203 }
204 try
205 {
206 spMaster->Experience(lexical_cast<unsigned long int>((*tok_iter)));
207 }
208 catch (bad_lexical_cast)
209 {
210 throw DataLayerException(str(format("Corrupt master line %1%. Invalid integer value %2%") %
211 line % (*tok_iter)));
212 }
213
214 // Health and Max Health
215 tok_iter++;
216 if (tok_iter == tokens.end())
217 {
218 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
219 line), __FILE__, __LINE__);
220 }
221 try
222 {
223 spMaster->Health(lexical_cast<int>((*tok_iter)));
224 spMaster->MaxHealth(spMaster->Health());
225 }
226 catch (bad_lexical_cast)
227 {
228 throw DataLayerException(str(format("Corrupt master line %1%. Invalid integer value %2%") %
229 line % (*tok_iter)));
230 }
231
232 // Parting Words
233 tok_iter++;
234 if (tok_iter == tokens.end())
235 {
236 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
237 line), __FILE__, __LINE__);
238 }
239 spMaster->PartingWords((*tok_iter));
240 // # level~name~weapon~strength~defense~gold~exp~hp~parting words
241 return spMaster;
242 }
243
244 bool FileMasterDAO::IdExists(const string &Id) const
245 {
246 map<string, shared_ptr<MasterGO> >::const_iterator iter;
247 iter = spMasterCache.find(Id);
248 return (iter != spMasterCache.end());
249 }