xoreos  0.0.5
resources.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/ustring.h"
27 
28 #include "src/aurora/resman.h"
29 
30 #include "src/events/events.h"
31 
33 
34 namespace Engines {
35 
36 void indexMandatoryArchive(const Common::UString &file, uint32 priority, const std::vector<byte> &password,
37  Common::ChangeID *changeID) {
38 
39  if (EventMan.quitRequested())
40  return;
41 
42  try {
43  ResMan.indexArchive(file, priority, password, changeID);
44  } catch (Common::Exception &e) {
45  e.add("Failed to index mandatory archive \"%s\"", file.c_str());
46  throw;
47  }
48 }
49 
50 void indexMandatoryArchive(const Common::UString &file, uint32 priority, const std::vector<byte> &password,
51  ChangeList &changes) {
52 
53  changes.push_back(Common::ChangeID());
54  indexMandatoryArchive(file, priority, password, &changes.back());
55 }
56 
57 void indexMandatoryArchive(const Common::UString &file, uint32 priority, Common::ChangeID *changeID) {
58  std::vector<byte> password;
59 
60  indexMandatoryArchive(file, priority, password, changeID);
61 }
62 
63 void indexMandatoryArchive(const Common::UString &file, uint32 priority, ChangeList &changes) {
64  std::vector<byte> password;
65 
66  indexMandatoryArchive(file, priority, password, changes);
67 }
68 
69 bool indexOptionalArchive(const Common::UString &file, uint32 priority, const std::vector<byte> &password,
70  Common::ChangeID *changeID) {
71 
72  if (EventMan.quitRequested())
73  return false;
74 
75  if (!ResMan.hasArchive(file))
76  return false;
77 
78  try {
79  ResMan.indexArchive(file, priority, password, changeID);
80  } catch (Common::Exception &e) {
81  e.add("Found optional archive \"%s\", but failed to index it", file.c_str());
82  throw;
83  }
84 
85  return true;
86 }
87 
88 bool indexOptionalArchive(const Common::UString &file, uint32 priority, const std::vector<byte> &password,
89  ChangeList &changes) {
90 
91  changes.push_back(Common::ChangeID());
92  if (!indexOptionalArchive(file, priority, password, &changes.back())) {
93  changes.pop_back();
94  return false;
95  }
96 
97  return true;
98 }
99 
100 bool indexOptionalArchive(const Common::UString &file, uint32 priority, Common::ChangeID *changeID) {
101  std::vector<byte> password;
102 
103  return indexOptionalArchive(file, priority, password, changeID);
104 }
105 
106 bool indexOptionalArchive(const Common::UString &file, uint32 priority, ChangeList &changes) {
107  std::vector<byte> password;
108 
109  return indexOptionalArchive(file, priority, password, changes);
110 }
111 
112 void indexMandatoryDirectory(const Common::UString &dir, const char *glob, int depth,
113  uint32 priority, Common::ChangeID *changeID) {
114 
115  if (EventMan.quitRequested())
116  return;
117 
118  try {
119  ResMan.indexResourceDir(dir, glob, depth, priority, changeID);
120  } catch (Common::Exception &e) {
121  e.add("Failed to index mandatory directory \"%s\"", dir.c_str());
122  throw;
123  }
124 }
125 
126 void indexMandatoryDirectory(const Common::UString &dir, const char *glob, int depth,
127  uint32 priority, ChangeList &changes) {
128 
129  changes.push_back(Common::ChangeID());
130  indexMandatoryDirectory(dir, glob, depth, priority, &changes.back());
131 }
132 
133 bool indexOptionalDirectory(const Common::UString &dir, const char *glob, int depth,
134  uint32 priority, Common::ChangeID *changeID) {
135 
136  if (EventMan.quitRequested())
137  return false;
138 
139  if (!ResMan.hasResourceDir(dir))
140  return false;
141 
142  try {
143  ResMan.indexResourceDir(dir, glob, depth, priority, changeID);
144  } catch (Common::Exception &e) {
145  e.add("Found optional directory \"%s\", but failed to index it", dir.c_str());
146  throw;
147  }
148 
149  return true;
150 }
151 
152 bool indexOptionalDirectory(const Common::UString &dir, const char *glob, int depth,
153  uint32 priority, ChangeList &changes) {
154 
155  changes.push_back(Common::ChangeID());
156  if (!indexOptionalDirectory(dir, glob, depth, priority, &changes.back())) {
157  changes.pop_back();
158  return false;
159  }
160 
161  return true;
162 }
163 
165  ResMan.undo(changeID);
166 }
167 
168 void deindexResources(ChangeList &changes) {
169  for (ChangeList::reverse_iterator c = changes.rbegin(); c != changes.rend(); ++c)
170  deindexResources(*c);
171 
172  changes.clear();
173 }
174 
175 } // End of namespace Engines
#define ResMan
Shortcut for accessing the sound manager.
Definition: resman.h:557
Generic Aurora engines resource utility functions.
void add(const char *s,...) GCC_PRINTF(2
Definition: error.cpp:58
A class holding an UTF-8 string.
Definition: ustring.h:48
bool indexOptionalArchive(const Common::UString &file, uint32 priority, const std::vector< byte > &password, Common::ChangeID *changeID)
Definition: resources.cpp:69
bool indexOptionalDirectory(const Common::UString &dir, const char *glob, int depth, uint32 priority, Common::ChangeID *changeID)
Add a directory to the resource manager, if it exists.
Definition: resources.cpp:133
Exception that provides a stack of explanations.
Definition: error.h:36
std::list< Common::ChangeID > ChangeList
Definition: resources.h:41
void deindexResources(Common::ChangeID &changeID)
Remove previously added resources from the ResourceManager.
Definition: resources.cpp:164
Basic exceptions to throw.
void indexMandatoryArchive(const Common::UString &file, uint32 priority, const std::vector< byte > &password, Common::ChangeID *changeID)
Definition: resources.cpp:36
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
The global events manager.
#define EventMan
Shortcut for accessing the events manager.
Definition: events.h:210
Unicode string handling.
void indexMandatoryDirectory(const Common::UString &dir, const char *glob, int depth, uint32 priority, Common::ChangeID *changeID)
Add a directory to the resource manager, erroring out if it does not exist.
Definition: resources.cpp:112
A class representing an undoable change.
Definition: changeid.h:35
uint32_t uint32
Definition: types.h:204
The global resource manager for Aurora resources.