xoreos  0.0.5
util.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 <cstdlib>
26 
27 #include "src/common/scopedptr.h"
28 #include "src/common/error.h"
29 #include "src/common/readstream.h"
30 #include "src/common/writefile.h"
31 #include "src/common/configman.h"
32 #include "src/common/debug.h"
33 
34 #include "src/aurora/util.h"
35 #include "src/aurora/resman.h"
36 #include "src/aurora/gff3file.h"
37 #include "src/aurora/gff4file.h"
38 #include "src/aurora/2dafile.h"
39 
41 
43 
44 #include "src/sound/sound.h"
45 
47 
48 #include "src/events/events.h"
49 
51 
52 namespace Engines {
53 
54 void playVideo(const Common::UString &video) {
55  if (ConfigMan.getBool("skipvideos", false)) {
56  debugC(Common::kDebugEngineVideo, 1, "Skipping video \"%s\"", video.c_str());
57  return;
58  }
59 
60  // Mute other sound sources
61  SoundMan.setTypeGain(Sound::kSoundTypeMusic, 0.0f);
62  SoundMan.setTypeGain(Sound::kSoundTypeSFX , 0.0f);
63  SoundMan.setTypeGain(Sound::kSoundTypeVoice, 0.0f);
64 
65  try {
66  Video::Aurora::VideoPlayer videoPlayer(video);
67 
68  debugC(Common::kDebugEngineVideo, 1, "Playing video \"%s\"", video.c_str());
69 
70  videoPlayer.play();
71  } catch (...) {
73  }
74 
75  // Restore volumes
76  SoundMan.setTypeGain(Sound::kSoundTypeMusic, ConfigMan.getDouble("volume_music", 1.0));
77  SoundMan.setTypeGain(Sound::kSoundTypeSFX , ConfigMan.getDouble("volume_sfx" , 1.0));
78  SoundMan.setTypeGain(Sound::kSoundTypeVoice, ConfigMan.getDouble("volume_voice", 1.0));
79 }
80 
82  bool loop, float volume, bool pitchVariance) {
83 
84  Aurora::ResourceType resType =
86 
87  Sound::ChannelHandle channel;
88 
89  try {
90  Common::SeekableReadStream *soundStream = ResMan.getResource(resType, sound);
91  if (!soundStream)
92  return channel;
93 
94  channel = SoundMan.playSoundFile(soundStream, soundType, loop);
95 
96  debugC(Common::kDebugEngineSound, 1, "Playing sound \"%s\" in %s",
97  sound.c_str(), SoundMan.formatChannel(channel).c_str());
98 
99  SoundMan.setChannelGain(channel, volume);
100 
101  if (pitchVariance) {
102  const float pitch = 1.0f + ((((std::rand() % 1001) / 1000.0f) / 5.0f) - 0.1f);
103 
104  SoundMan.setChannelPitch(channel, pitch);
105  }
106 
107  SoundMan.startChannel(channel);
108 
109  } catch (...) {
111  }
112 
113  return channel;
114 }
115 
116 void checkConfigInt(const Common::UString &key, int min, int max) {
117  const int def = ConfigMan.getDefaultInt(key);
118 
119  const int value = ConfigMan.getInt(key, def);
120  if ((value >= min) && (value <= max))
121  return;
122 
123  warning("Config \"%s\" has invalid value (%d), resetting to default (%d)", key.c_str(), value, def);
124  ConfigMan.setInt(key, def);
125 }
126 
127 void checkConfigDouble(const Common::UString &key, double min, double max) {
128  const double def = ConfigMan.getDefaultDouble(key);
129 
130  const double value = ConfigMan.getDouble(key, def);
131  if ((value >= min) && (value <= max))
132  return;
133 
134  warning("Config \"%s\" has invalid value (%lf), resetting to default (%lf)", key.c_str(), value, def);
135  ConfigMan.setDouble(key, def);
136 }
137 
138 bool longDelay(uint32 ms) {
139  while ((ms > 0) && !EventMan.quitRequested()) {
140  uint32 delay = MIN<uint32>(ms, 10);
141 
142  EventMan.delay(delay);
143 
144  ms -= delay;
145  }
146 
147  return EventMan.quitRequested();
148 }
149 
151  uint32 id, bool repairNWNPremium) {
152 
153  try {
154  return new Aurora::GFF3File(gff3, type, id, repairNWNPremium);
155  } catch (...) {
156  }
157 
158  return 0;
159 }
160 
162  Aurora::FileType fileType, uint32 type) {
163 
164  try {
165  return new Aurora::GFF4File(gff4, fileType, type);
166  } catch (...) {
167  }
168 
169  return 0;
170 }
171 
172 bool dumpResList(const Common::UString &name) {
173  try {
174 
175  ResMan.dumpResourcesList(name);
176  return true;
177 
178  } catch (...) {
179  }
180 
181  return false;
182 }
183 
184 bool dumpStream(Common::SeekableReadStream &stream, const Common::UString &fileName) {
185  Common::WriteFile file;
186  if (!file.open(fileName))
187  return false;
188 
189  const size_t pos = stream.pos();
190  try {
191  stream.seek(0);
192 
193  file.writeStream(stream);
194  stream.seek(pos);
195 
196  file.flush();
197 
198  } catch (...) {
199  stream.seek(pos);
200  return false;
201  }
202 
203  file.close();
204  return true;
205 }
206 
207 bool dumpResource(const Common::UString &name, Aurora::FileType type, const Common::UString &file) {
208  Common::ScopedPtr<Common::SeekableReadStream> res(ResMan.getResource(name, type));
209  if (!res)
210  return false;
211 
212  return dumpStream(*res, file.empty() ? TypeMan.setFileType(name, type) : file);
213 }
214 
215 bool dumpResource(const Common::UString &name, const Common::UString &file) {
216  Aurora::FileType type = TypeMan.getFileType(name);
217 
218  return dumpResource(TypeMan.setFileType(name, Aurora::kFileTypeNone), type, file);
219 }
220 
221 bool dumpTGA(const Common::UString &name, const Common::UString &file) {
222  try {
224 
225  return image->dumpTGA(file.empty() ? (name + ".tga") : file);
226  } catch (...) {
227  }
228 
229  return false;
230 }
231 
232 bool dump2DA(const Common::UString &name, const Common::UString &file) {
234  if (!twoDAFile)
235  return false;
236 
237  try {
238  Aurora::TwoDAFile twoda(*twoDAFile);
239 
240  return twoda.writeASCII(file.empty() ? (name + ".2da") : file);
241  } catch (...) {
242  }
243 
244  return false;
245 }
246 
247 } // End of namespace Engines
A video player.
Class to hold the two-dimensional array of a 2DA file.
Definition: 2dafile.h:124
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
ResourceType
Definition: types.h:407
#define ResMan
Shortcut for accessing the sound manager.
Definition: resman.h:557
Table data, 2-dimensional text array.
Definition: types.h:86
Generic image decoder interface.
void playVideo(const Common::UString &video)
Play this video resource.
Definition: util.cpp:54
#define TypeMan
Shortcut for accessing the file type manager.
Definition: util.h:85
void debugC(Common::DebugChannel channel, uint32 level, const char *s,...)
Definition: debug.cpp:34
A class holding an UTF-8 string.
Definition: ustring.h:48
virtual size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)=0
Sets the stream position indicator for the stream.
Utility functions for debug output.
The global config manager.
bool dumpResList(const Common::UString &name)
Debug method to quickly dump the current list of resource to disk.
Definition: util.cpp:172
A video player.
Definition: videoplayer.h:41
Aurora::GFF4File * loadOptionalGFF4(const Common::UString &gff4, Aurora::FileType fileType, uint32 type)
Load a GFF4, but return 0 instead of throwing on error.
Definition: util.cpp:161
Utility functions to handle files used in BioWare&#39;s Aurora engine.
bool dumpStream(Common::SeekableReadStream &stream, const Common::UString &fileName)
Debug method to quickly dump a stream to disk.
Definition: util.cpp:184
bool dumpResource(const Common::UString &name, Aurora::FileType type, const Common::UString &file)
Debug method to quickly dump a resource to disk.
Definition: util.cpp:207
A GFF (generic file format) V4.0/V4.1 file, found in Dragon Age: Origins, Dragon Age 2 and Sonic Chro...
Definition: gff4file.h:93
Aurora::GFF3File * loadOptionalGFF3(const Common::UString &gff3, Aurora::FileType type, uint32 id, bool repairNWNPremium)
Load a GFF3, but return 0 instead of throwing on error.
Definition: util.cpp:150
bool longDelay(uint32 ms)
Wait for a "long" amount of time, returning prematurely with true in the case of a requested quit...
Definition: util.cpp:138
A simple scoped smart pointer template.
void exceptionDispatcherWarning(const char *s,...)
Exception dispatcher that prints the exception as a warning, and adds another reason on top...
Definition: error.cpp:158
"EVideo", engine video.
Definition: debugman.h:57
Handling version V4.0/V4.1 of BioWare&#39;s GFFs (generic file format).
Basic exceptions to throw.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
bool open(const UString &fileName)
Try to open the file with the given fileName.
Definition: writefile.cpp:50
#define ConfigMan
Shortcut for accessing the config manager.
Definition: configman.h:176
static ImageDecoder * loadImage(const Common::UString &name, bool deswizzle=false)
Load an image in any of the common texture formats.
Definition: texture.cpp:401
bool dumpTGA(const Common::UString &name, const Common::UString &file)
Debug method to quickly dump an image resource to disk.
Definition: util.cpp:221
Handling BioWare&#39;s 2DAs (two-dimensional array).
The global events manager.
A GFF (generic file format) V3.2/V3.3 file, found in all Aurora games except Sonic Chronicles: The Da...
Definition: gff3file.h:85
void writeASCII(Common::WriteStream &out) const
Write the 2DA data into an V2.0 ASCII 2DA.
Definition: 2dafile.cpp:443
The global sound manager, handling all sound output.
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
#define SoundMan
Shortcut for accessing the sound manager.
Definition: sound.h:293
bool dump2DA(const Common::UString &name, const Common::UString &file)
Debug method to quickly dump a 2DA to disk.
Definition: util.cpp:232
void warning(const char *s,...)
Definition: util.cpp:33
Basic reading stream interfaces.
virtual size_t pos() const =0
Obtains the current value of the stream position indicator of the stream.
Voice/Speech.
Definition: types.h:46
#define EventMan
Shortcut for accessing the events manager.
Definition: events.h:210
void flush()
Commit any buffered data to the underlying channel or storage medium; unbuffered streams can use the ...
Definition: writefile.cpp:83
Sound::ChannelHandle playSound(const Common::UString &sound, Sound::SoundType soundType, bool loop, float volume, bool pitchVariance)
Play this sound resource.
Definition: util.cpp:81
A sound resource.
Definition: types.h:410
void close()
Close the file, if open.
Definition: writefile.cpp:69
A texture as used in the Aurora engines.
A handle to a sound channel.
Definition: types.h:35
SoundType
The type of a sound.
Definition: types.h:43
uint32_t uint32
Definition: types.h:204
size_t writeStream(ReadStream &stream, size_t n)
Copy n bytes of the given stream into the stream.
Definition: writestream.cpp:72
FileType
Various file types used by the Aurora engine and found in archives.
Definition: types.h:56
void checkConfigInt(const Common::UString &key, int min, int max)
Make sure that an int config value is in the right range.
Definition: util.cpp:116
Implementing the stream writing interfaces for files.
A music resource.
Definition: types.h:411
Sound effect.
Definition: types.h:45
Generic Aurora engines utility functions.
A simple streaming file writing class.
Definition: writefile.h:40
void checkConfigDouble(const Common::UString &key, double min, double max)
Make sure that a double config value is in the right range.
Definition: util.cpp:127
Interface for a seekable & readable data stream.
Definition: readstream.h:265
The global resource manager for Aurora resources.
"ESound", engine sound.
Definition: debugman.h:56