Skip to content

Commit efbdd0c

Browse files
committed
Merge branch 'vfs_normalized_path_26' into 'master'
Use normalized path in SoundManager (#8138) See merge request OpenMW/openmw!5048
2 parents 48b0d48 + 0859d18 commit efbdd0c

File tree

7 files changed

+39
-62
lines changed

7 files changed

+39
-62
lines changed

apps/openmw/mwbase/soundmanager.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ namespace MWBase
166166
///< Play a sound, independently of 3D-position
167167
///< @param offset Number of seconds into the sound to start playback.
168168

169-
virtual Sound* playSound(std::string_view fileName, float volume, float pitch, Type type = Type::Sfx,
169+
virtual Sound* playSound(VFS::Path::NormalizedView fileName, float volume, float pitch, Type type = Type::Sfx,
170170
PlayMode mode = PlayMode::Normal, float offset = 0)
171171
= 0;
172172
///< Play a sound, independently of 3D-position
@@ -179,7 +179,7 @@ namespace MWBase
179179
///< Play_NoTrack is specified.
180180
///< @param offset Number of seconds into the sound to start playback.
181181

182-
virtual Sound* playSound3D(const MWWorld::ConstPtr& reference, std::string_view fileName, float volume,
182+
virtual Sound* playSound3D(const MWWorld::ConstPtr& reference, VFS::Path::NormalizedView fileName, float volume,
183183
float pitch, Type type = Type::Sfx, PlayMode mode = PlayMode::Normal, float offset = 0)
184184
= 0;
185185
///< Play a 3D sound attached to an MWWorld::Ptr. Will be updated automatically with the Ptr's position, unless
@@ -198,7 +198,7 @@ namespace MWBase
198198
virtual void stopSound3D(const MWWorld::ConstPtr& reference, const ESM::RefId& soundId) = 0;
199199
///< Stop the given object from playing the given sound.
200200

201-
virtual void stopSound3D(const MWWorld::ConstPtr& reference, std::string_view fileName) = 0;
201+
virtual void stopSound3D(const MWWorld::ConstPtr& reference, VFS::Path::NormalizedView fileName) = 0;
202202
///< Stop the given object from playing the given sound.
203203

204204
virtual void stopSound3D(const MWWorld::ConstPtr& reference) = 0;
@@ -217,7 +217,7 @@ namespace MWBase
217217
///< Is the given sound currently playing on the given object?
218218
/// If you want to check if sound played with playSound is playing, use empty Ptr
219219

220-
virtual bool getSoundPlaying(const MWWorld::ConstPtr& reference, std::string_view fileName) const = 0;
220+
virtual bool getSoundPlaying(const MWWorld::ConstPtr& reference, VFS::Path::NormalizedView fileName) const = 0;
221221
///< Is the given sound currently playing on the given object?
222222
/// If you want to check if sound played with playSound is playing, use empty Ptr
223223

apps/openmw/mwlua/soundbindings.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,24 +118,25 @@ namespace MWLua
118118
auto args = getPlaySoundArgs(options);
119119
auto playMode = getPlayMode(args, false);
120120

121-
MWBase::Environment::get().getSoundManager()->playSound(
122-
fileName, args.mVolume, args.mPitch, MWSound::Type::Sfx, playMode, args.mTimeOffset);
121+
MWBase::Environment::get().getSoundManager()->playSound(VFS::Path::Normalized(fileName), args.mVolume,
122+
args.mPitch, MWSound::Type::Sfx, playMode, args.mTimeOffset);
123123
};
124124

125125
api["stopSound"] = [](std::string_view soundId) {
126126
ESM::RefId sound = ESM::RefId::deserializeText(soundId);
127127
MWBase::Environment::get().getSoundManager()->stopSound3D(MWWorld::Ptr(), sound);
128128
};
129129
api["stopSoundFile"] = [](std::string_view fileName) {
130-
MWBase::Environment::get().getSoundManager()->stopSound3D(MWWorld::Ptr(), fileName);
130+
MWBase::Environment::get().getSoundManager()->stopSound3D(MWWorld::Ptr(), VFS::Path::Normalized(fileName));
131131
};
132132

133133
api["isSoundPlaying"] = [](std::string_view soundId) {
134134
ESM::RefId sound = ESM::RefId::deserializeText(soundId);
135135
return MWBase::Environment::get().getSoundManager()->getSoundPlaying(MWWorld::Ptr(), sound);
136136
};
137137
api["isSoundFilePlaying"] = [](std::string_view fileName) {
138-
return MWBase::Environment::get().getSoundManager()->getSoundPlaying(MWWorld::Ptr(), fileName);
138+
return MWBase::Environment::get().getSoundManager()->getSoundPlaying(
139+
MWWorld::Ptr(), VFS::Path::Normalized(fileName));
139140
};
140141

141142
api["streamMusic"] = [](std::string_view fileName, const sol::optional<sol::table>& options) {
@@ -193,8 +194,8 @@ namespace MWLua
193194
auto playMode = getPlayMode(args, true);
194195
MWWorld::Ptr ptr = getMutablePtrOrThrow(ObjectVariant(object));
195196

196-
MWBase::Environment::get().getSoundManager()->playSound3D(
197-
ptr, fileName, args.mVolume, args.mPitch, MWSound::Type::Sfx, playMode, args.mTimeOffset);
197+
MWBase::Environment::get().getSoundManager()->playSound3D(ptr, VFS::Path::Normalized(fileName),
198+
args.mVolume, args.mPitch, MWSound::Type::Sfx, playMode, args.mTimeOffset);
198199
};
199200

200201
api["stopSound3d"] = [](std::string_view soundId, const sol::object& object) {
@@ -204,7 +205,7 @@ namespace MWLua
204205
};
205206
api["stopSoundFile3d"] = [](std::string_view fileName, const sol::object& object) {
206207
MWWorld::Ptr ptr = getMutablePtrOrThrow(ObjectVariant(object));
207-
MWBase::Environment::get().getSoundManager()->stopSound3D(ptr, fileName);
208+
MWBase::Environment::get().getSoundManager()->stopSound3D(ptr, VFS::Path::Normalized(fileName));
208209
};
209210

210211
api["isSoundPlaying"] = [](std::string_view soundId, const sol::object& object) {
@@ -214,7 +215,7 @@ namespace MWLua
214215
};
215216
api["isSoundFilePlaying"] = [](std::string_view fileName, const sol::object& object) {
216217
const MWWorld::Ptr& ptr = getPtrOrThrow(ObjectVariant(object));
217-
return MWBase::Environment::get().getSoundManager()->getSoundPlaying(ptr, fileName);
218+
return MWBase::Environment::get().getSoundManager()->getSoundPlaying(ptr, VFS::Path::Normalized(fileName));
218219
};
219220

220221
api["say"] = [luaManager = context.mLuaManager](

apps/openmw/mwsound/soundbuffer.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ namespace MWSound
6666
return nullptr;
6767
}
6868

69-
SoundBuffer* SoundBufferPool::lookup(std::string_view fileName) const
69+
SoundBuffer* SoundBufferPool::lookup(VFS::Path::NormalizedView fileName) const
7070
{
71-
const auto it = mBufferFileNameMap.find(std::string(fileName));
71+
const auto it = mBufferFileNameMap.find(fileName);
7272
if (it != mBufferFileNameMap.end())
7373
{
7474
SoundBuffer* sfx = it->second;
@@ -129,16 +129,14 @@ namespace MWSound
129129
return loadSfx(sfx);
130130
}
131131

132-
SoundBuffer* SoundBufferPool::load(std::string_view fileName)
132+
SoundBuffer* SoundBufferPool::load(VFS::Path::NormalizedView fileName)
133133
{
134134
SoundBuffer* sfx;
135-
const auto it = mBufferFileNameMap.find(std::string(fileName));
135+
const auto it = mBufferFileNameMap.find(fileName);
136136
if (it != mBufferFileNameMap.end())
137137
sfx = it->second;
138138
else
139-
{
140139
sfx = insertSound(fileName);
141-
}
142140

143141
return loadSfx(sfx);
144142
}
@@ -157,7 +155,7 @@ namespace MWSound
157155
mUnusedBuffers.clear();
158156
}
159157

160-
SoundBuffer* SoundBufferPool::insertSound(std::string_view fileName)
158+
SoundBuffer* SoundBufferPool::insertSound(VFS::Path::NormalizedView fileName)
161159
{
162160
static const AudioParams audioParams
163161
= makeAudioParams(MWBase::Environment::get().getESMStore()->get<ESM::GameSetting>());

apps/openmw/mwsound/soundbuffer.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
#include <algorithm>
55
#include <deque>
6-
#include <string>
76
#include <unordered_map>
87

98
#include <components/esm/refid.hpp>
9+
#include <components/vfs/pathutil.hpp>
1010

1111
#include "soundoutput.hpp"
1212

@@ -78,14 +78,14 @@ namespace MWSound
7878

7979
/// Lookup a sound by file name for its sound data (resource name, local volume,
8080
/// minRange, and maxRange)
81-
SoundBuffer* lookup(std::string_view fileName) const;
81+
SoundBuffer* lookup(VFS::Path::NormalizedView fileName) const;
8282

8383
/// Lookup a soundId for its sound data (resource name, local volume,
8484
/// minRange, and maxRange), and ensure it's ready for use.
8585
SoundBuffer* load(const ESM::RefId& soundId);
8686

8787
// Lookup for a sound by file name, and ensure it's ready for use.
88-
SoundBuffer* load(std::string_view fileName);
88+
SoundBuffer* load(VFS::Path::NormalizedView fileName);
8989

9090
void use(SoundBuffer& sfx)
9191
{
@@ -111,7 +111,7 @@ namespace MWSound
111111
SoundOutput* mOutput;
112112
std::deque<SoundBuffer> mSoundBuffers;
113113
std::unordered_map<ESM::RefId, SoundBuffer*> mBufferNameMap;
114-
std::unordered_map<std::string, SoundBuffer*> mBufferFileNameMap;
114+
std::unordered_map<VFS::Path::Normalized, SoundBuffer*, VFS::Path::Hash, std::equal_to<>> mBufferFileNameMap;
115115
std::size_t mBufferCacheMax;
116116
std::size_t mBufferCacheMin;
117117
std::size_t mBufferCacheSize = 0;
@@ -121,7 +121,7 @@ namespace MWSound
121121
SoundBuffer* insertSound(const ESM::RefId& soundId, const ESM::Sound& sound);
122122
SoundBuffer* insertSound(const ESM::RefId& soundId, const ESM4::Sound& sound);
123123
SoundBuffer* insertSound(const ESM::RefId& soundId, const ESM4::SoundReference& sound);
124-
SoundBuffer* insertSound(std::string_view fileName);
124+
SoundBuffer* insertSound(VFS::Path::NormalizedView fileName);
125125

126126
inline void unloadUnused();
127127
};

apps/openmw/mwsound/soundmanagerimp.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -487,16 +487,15 @@ namespace MWSound
487487
}
488488

489489
Sound* SoundManager::playSound(
490-
std::string_view fileName, float volume, float pitch, Type type, PlayMode mode, float offset)
490+
VFS::Path::NormalizedView fileName, float volume, float pitch, Type type, PlayMode mode, float offset)
491491
{
492492
if (!mOutput->isInitialized())
493493
return nullptr;
494494

495-
std::string normalizedName = VFS::Path::normalizeFilename(fileName);
496-
if (!mVFS->exists(normalizedName))
495+
if (!mVFS->exists(fileName))
497496
return nullptr;
498497

499-
SoundBuffer* sfx = mSoundBuffers.load(normalizedName);
498+
SoundBuffer* const sfx = mSoundBuffers.load(fileName);
500499
if (!sfx)
501500
return nullptr;
502501

@@ -584,18 +583,17 @@ namespace MWSound
584583
return playSound3D(ptr, sfx, volume, pitch, type, mode, offset);
585584
}
586585

587-
Sound* SoundManager::playSound3D(const MWWorld::ConstPtr& ptr, std::string_view fileName, float volume, float pitch,
588-
Type type, PlayMode mode, float offset)
586+
Sound* SoundManager::playSound3D(const MWWorld::ConstPtr& ptr, VFS::Path::NormalizedView fileName, float volume,
587+
float pitch, Type type, PlayMode mode, float offset)
589588
{
590589
if (remove3DSoundAtDistance(mode, ptr))
591590
return nullptr;
592591

593592
// Look up the sound
594-
std::string normalizedName = VFS::Path::normalizeFilename(fileName);
595-
if (!mVFS->exists(normalizedName))
593+
if (!mVFS->exists(fileName))
596594
return nullptr;
597595

598-
SoundBuffer* sfx = mSoundBuffers.load(normalizedName);
596+
SoundBuffer* const sfx = mSoundBuffers.load(fileName);
599597
if (!sfx)
600598
return nullptr;
601599

@@ -668,13 +666,12 @@ namespace MWSound
668666
stopSound(sfx, ptr);
669667
}
670668

671-
void SoundManager::stopSound3D(const MWWorld::ConstPtr& ptr, std::string_view fileName)
669+
void SoundManager::stopSound3D(const MWWorld::ConstPtr& ptr, VFS::Path::NormalizedView fileName)
672670
{
673671
if (!mOutput->isInitialized())
674672
return;
675673

676-
std::string normalizedName = VFS::Path::normalizeFilename(fileName);
677-
SoundBuffer* sfx = mSoundBuffers.lookup(normalizedName);
674+
SoundBuffer* const sfx = mSoundBuffers.lookup(fileName);
678675
if (!sfx)
679676
return;
680677

@@ -737,14 +734,12 @@ namespace MWSound
737734
}
738735
}
739736

740-
bool SoundManager::getSoundPlaying(const MWWorld::ConstPtr& ptr, std::string_view fileName) const
737+
bool SoundManager::getSoundPlaying(const MWWorld::ConstPtr& ptr, VFS::Path::NormalizedView fileName) const
741738
{
742-
std::string normalizedName = VFS::Path::normalizeFilename(fileName);
743-
744739
SoundMap::const_iterator snditer = mActiveSounds.find(ptr.mRef);
745740
if (snditer != mActiveSounds.end())
746741
{
747-
SoundBuffer* sfx = mSoundBuffers.lookup(normalizedName);
742+
SoundBuffer* const sfx = mSoundBuffers.lookup(fileName);
748743
if (!sfx)
749744
return false;
750745

apps/openmw/mwsound/soundmanagerimp.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ namespace MWSound
219219
///< Play a sound, independently of 3D-position
220220
///< @param offset Number of seconds into the sound to start playback.
221221

222-
Sound* playSound(std::string_view fileName, float volume, float pitch, Type type = Type::Sfx,
222+
Sound* playSound(VFS::Path::NormalizedView fileName, float volume, float pitch, Type type = Type::Sfx,
223223
PlayMode mode = PlayMode::Normal, float offset = 0) override;
224224
///< Play a sound, independently of 3D-position
225225
///< @param offset Number of seconds into the sound to start playback.
@@ -230,8 +230,8 @@ namespace MWSound
230230
///< Play_NoTrack is specified.
231231
///< @param offset Number of seconds into the sound to start playback.
232232

233-
Sound* playSound3D(const MWWorld::ConstPtr& reference, std::string_view fileName, float volume, float pitch,
234-
Type type = Type::Sfx, PlayMode mode = PlayMode::Normal, float offset = 0) override;
233+
Sound* playSound3D(const MWWorld::ConstPtr& reference, VFS::Path::NormalizedView fileName, float volume,
234+
float pitch, Type type = Type::Sfx, PlayMode mode = PlayMode::Normal, float offset = 0) override;
235235
///< Play a 3D sound attached to an MWWorld::Ptr. Will be updated automatically with the Ptr's position, unless
236236
///< Play_NoTrack is specified.
237237
///< @param offset Number of seconds into the sound to start playback.
@@ -249,7 +249,7 @@ namespace MWSound
249249
void stopSound3D(const MWWorld::ConstPtr& reference, const ESM::RefId& soundId) override;
250250
///< Stop the given object from playing the given sound.
251251

252-
void stopSound3D(const MWWorld::ConstPtr& reference, std::string_view fileName) override;
252+
void stopSound3D(const MWWorld::ConstPtr& reference, VFS::Path::NormalizedView fileName) override;
253253
///< Stop the given object from playing the given sound.
254254

255255
void stopSound3D(const MWWorld::ConstPtr& reference) override;
@@ -267,7 +267,7 @@ namespace MWSound
267267
bool getSoundPlaying(const MWWorld::ConstPtr& reference, const ESM::RefId& soundId) const override;
268268
///< Is the given sound currently playing on the given object?
269269

270-
bool getSoundPlaying(const MWWorld::ConstPtr& reference, std::string_view fileName) const override;
270+
bool getSoundPlaying(const MWWorld::ConstPtr& reference, VFS::Path::NormalizedView fileName) const override;
271271
///< Is the given sound currently playing on the given object?
272272

273273
void pauseSounds(MWSound::BlockerType blocker, int types = int(Type::Mask)) override;

components/vfs/pathutil.hpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,6 @@ namespace VFS::Path
7777
return out;
7878
}
7979

80-
struct PathCharLess
81-
{
82-
bool operator()(char x, char y) const { return normalize(x) < normalize(y); }
83-
};
84-
85-
inline bool pathLess(std::string_view x, std::string_view y)
86-
{
87-
return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), PathCharLess());
88-
}
89-
9080
inline bool pathEqual(std::string_view x, std::string_view y)
9181
{
9282
if (std::size(x) != std::size(y))
@@ -95,13 +85,6 @@ namespace VFS::Path
9585
std::begin(x), std::end(x), std::begin(y), [](char l, char r) { return normalize(l) == normalize(r); });
9686
}
9787

98-
struct PathLess
99-
{
100-
using is_transparent = void;
101-
102-
bool operator()(std::string_view left, std::string_view right) const { return pathLess(left, right); }
103-
};
104-
10588
inline constexpr auto findSeparatorOrExtensionSeparator(auto begin, auto end)
10689
{
10790
return std::find_if(begin, end, [](char v) { return v == extensionSeparator || v == separator; });

0 commit comments

Comments
 (0)