xoreos  0.0.5
2dareg.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/error.h"
26 #include "src/common/scopedptr.h"
27 #include "src/common/readstream.h"
28 
29 #include "src/aurora/2dareg.h"
30 #include "src/aurora/types.h"
31 #include "src/aurora/2dafile.h"
32 #include "src/aurora/gdafile.h"
33 #include "src/aurora/resman.h"
34 
36 
37 namespace Aurora {
38 
40 }
41 
43  clear();
44 }
45 
47  _twodas.clear();
48  _gdas.clear();
49 }
50 
52  TwoDAMap::const_iterator twoda = _twodas.find(name);
53  if (twoda != _twodas.end())
54  // Entry exists => return
55  return *twoda->second;
56 
57  // Entry doesn't exist => load and add
58 
59  TwoDAFile *newTwoDA = load2DA(name);
60 
61  std::pair<TwoDAMap::iterator, bool> result;
62  result = _twodas.insert(std::make_pair(name, newTwoDA));
63 
64  return *result.first->second;
65 }
66 
68  GDAMap::const_iterator gda = _gdas.find(name);
69  if (gda != _gdas.end())
70  // Entry exists => return
71  return *gda->second;
72 
73  // Entry doesn't exist => load and add
74 
75  GDAFile *newGDA = loadGDA(name);
76 
77  std::pair<GDAMap::iterator, bool> result;
78  result = _gdas.insert(std::make_pair(name, newGDA));
79 
80  return *result.first->second;
81 }
82 
84  GDAMap::const_iterator gda = _gdas.find(prefix);
85  if (gda != _gdas.end())
86  // Entry exists => return
87  return *gda->second;
88 
89  // Entry doesn't exist => load and add
90 
91  GDAFile *newGDA = loadMGDA(prefix);
92 
93  std::pair<GDAMap::iterator, bool> result;
94  result = _gdas.insert(std::make_pair(prefix, newGDA));
95 
96  return *result.first->second;
97 }
98 
100  TwoDAMap::iterator twoda = _twodas.find(name);
101  if (twoda != _twodas.end())
102  // Entry exists => remove first
103  _twodas.erase(twoda);
104 
105  // Load and add
106  _twodas[name] = load2DA(name);
107 }
108 
110  TwoDAMap::iterator twoda = _twodas.find(name);
111  if (twoda == _twodas.end())
112  // Doesn't exist, nothing to do
113  return;
114 
115  _twodas.erase(twoda);
116 }
117 
119  GDAMap::iterator gda = _gdas.find(name);
120  if (gda != _gdas.end())
121  // Entry exists => remove first
122  _gdas.erase(gda);
123 
124  // Load and add
125  _gdas[name] = loadGDA(name);
126 }
127 
129  GDAMap::iterator gda = _gdas.find(prefix);
130  if (gda != _gdas.end())
131  // Entry exists => remove first
132  _gdas.erase(gda);
133 
134  // Load and add
135  _gdas[prefix] = loadMGDA(prefix);
136 }
137 
139  GDAMap::iterator gda = _gdas.find(name);
140  if (gda == _gdas.end())
141  // Doesn't exist, nothing to do
142  return;
143 
144  _gdas.erase(gda);
145 }
146 
150 
151  try {
152  twodaFile.reset(ResMan.getResource(name, kFileType2DA));
153  if (!twodaFile)
154  throw Common::Exception("No such 2DA");
155 
156  twoda.reset(new TwoDAFile(*twodaFile));
157 
158  } catch (Common::Exception &e) {
159  e.add("Failed loading 2DA \"%s\"", name.c_str());
160  throw;
161  }
162 
163  return twoda.release();
164 }
165 
169 
170  try {
171  gdaFile.reset(ResMan.getResource(name, kFileTypeGDA));
172  if (!gdaFile)
173  throw Common::Exception("No such GDA");
174 
175  gda.reset(new GDAFile(gdaFile.release()));
176 
177  } catch (Common::Exception &e) {
178  e.add("Failed loading GDA \"%s\"", name.c_str());
179  throw;
180  }
181 
182  return gda.release();
183 }
184 
186  /* Load multiple GDAs with the same prefix, and merge them together into a single GDA. */
187 
188  if (prefix.empty())
189  throw Common::Exception("Trying to load MGDA \"\"");
190 
191  prefix.makeLower();
192 
193  std::list<ResourceManager::ResourceID> gdas;
194  ResMan.getAvailableResources(kFileTypeGDA, gdas);
195 
197 
198  try {
199  for (std::list<ResourceManager::ResourceID>::const_iterator g = gdas.begin(); g != gdas.end(); ++g) {
200  // Find all GDAs that match the prefix
201  if (!g->name.toLower().beginsWith(prefix))
202  continue;
203 
204  // Load the GDA
205 
207  if (!stream)
208  throw Common::Exception("No such GDA \"%s\"", g->name.c_str());
209 
210  // If this is the first GDA, plain load it. Otherwise, merge it into the first one
211  if (!gda)
212  gda.reset(new GDAFile(stream.release()));
213  else
214  gda->add(stream.release());
215  }
216 
217  if (!gda)
218  throw Common::Exception("No such GDA");
219 
220  } catch (Common::Exception &e) {
221  e.add("Failed loading multiple GDA \"%s\"", prefix.c_str());
222  throw;
223  }
224 
225  return gda.release();
226 }
227 
228 } // End of namespace Aurora
void addGDA(const Common::UString &name)
Add a certain GDA to the registry, reloading it if necessary.
Definition: 2dareg.cpp:118
Class to hold the two-dimensional array of a 2DA file.
Definition: 2dafile.h:124
#define ResMan
Shortcut for accessing the sound manager.
Definition: resman.h:557
Table data, 2-dimensional text array.
Definition: types.h:86
void add(const char *s,...) GCC_PRINTF(2
Definition: error.cpp:58
Handling BioWare&#39;s GDAs (2DAs, two-dimensional array, within V4.0 GFFs).
void erase(typename std::map< Key, T *, Compare >::iterator position)
Definition: ptrmap.h:56
A class holding an UTF-8 string.
Definition: ustring.h:48
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
PointerType release()
Returns the plain pointer value and releases ScopedPtr.
Definition: scopedptr.h:103
void removeGDA(const Common::UString &name)
Remove a certain GDA from the registry.
Definition: 2dareg.cpp:138
TwoDAFile * load2DA(const Common::UString &name)
Definition: 2dareg.cpp:147
GDAFile * loadMGDA(Common::UString prefix)
Definition: 2dareg.cpp:185
Exception that provides a stack of explanations.
Definition: error.h:36
A simple scoped smart pointer template.
GDAFile * loadGDA(const Common::UString &name)
Definition: 2dareg.cpp:166
Basic exceptions to throw.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
#define DECLARE_SINGLETON(T)
Note that you need to use this macro from the global namespace.
Definition: singleton.h:122
const GDAFile & getMGDA(const Common::UString &prefix)
Get a certain multiple GDA, loading it if necessary.
Definition: 2dareg.cpp:83
Handling BioWare&#39;s 2DAs (two-dimensional array).
void clear()
Definition: ptrmap.h:48
void remove2DA(const Common::UString &name)
Remove a certain 2DA from the registry.
Definition: 2dareg.cpp:109
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
StackException Exception
Definition: error.h:59
The global 2DA registry.
Basic reading stream interfaces.
void addMGDA(const Common::UString &prefix)
Add a certain multiple GDA to the registry, reloading it if necessary.
Definition: 2dareg.cpp:128
void add2DA(const Common::UString &name)
Add a certain 2DA to the registry, reloading it if necessary.
Definition: 2dareg.cpp:99
Basic type definitions to handle files used in BioWare&#39;s Aurora engine.
Table data, GFF&#39;d 2DA, 2-dimensional text array.
Definition: types.h:291
TwoDAMap _twodas
Definition: 2dareg.h:90
const TwoDAFile & get2DA(const Common::UString &name)
Get a certain 2DA, loading it if necessary.
Definition: 2dareg.cpp:51
The global 2DA registry, holding all current 2DAs.
Definition: 2dareg.h:58
void makeLower()
Convert the string to lowercase.
Definition: ustring.cpp:473
const GDAFile & getGDA(const Common::UString &name)
Get a certain GDA, loading it if necessary.
Definition: 2dareg.cpp:67
The global resource manager for Aurora resources.
Class to hold the GFF&#39;d two-dimensional array of a GDA file.
Definition: gdafile.h:62