Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit ee254ae

Browse files
committed
Renamed ContentFile GetSample to GetSound to be more consistent
Cleaned up contentfile comments and errors a little Added safety checking for empty soundfiles when loading them Fixed some safety problems in AudioMan and made SoundContainer create properly abort on failure
1 parent 7d3c910 commit ee254ae

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

Entities/SoundContainer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ namespace RTE {
134134
auto readSound = [&soundData, &reader](const std::string &soundPath) {
135135
ContentFile soundFile(soundPath.c_str());
136136
soundFile.SetFormattedReaderPosition("in file " + reader.GetCurrentFilePath() + " on line " + std::to_string(reader.GetCurrentFileLine()));
137-
FMOD::Sound *soundObject = soundFile.GetAsSample();
137+
FMOD::Sound *soundObject = soundFile.GetAsSound();
138138
if (g_AudioMan.IsAudioEnabled() && !soundObject) { reader.ReportError(std::string("Failed to load the sound from the file")); }
139139

140140
soundData.SoundFile = soundFile;
@@ -227,7 +227,7 @@ namespace RTE {
227227
if (soundSetIndex < m_SoundSets.size()) { soundSet = m_SoundSets[soundSetIndex]; }
228228

229229
ContentFile soundFile(soundFilePath.c_str());
230-
FMOD::Sound *soundObject = soundFile.GetAsSample(abortGameForInvalidSound, false);
230+
FMOD::Sound *soundObject = soundFile.GetAsSound(abortGameForInvalidSound, false);
231231
if (!soundObject) {
232232
return;
233233
}

Entities/SoundContainer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace RTE {
6565
/// <param name="immobile">Whether this SoundContainer's sounds will be treated as immobile, i.e. they won't be affected by 3D sound manipulation.</param>
6666
/// <param name="affectedByGlobalPitch">Whether this SoundContainer's sounds' frequency will be affected by the global pitch.</param>
6767
/// <returns>An error return value signaing success or any particular failure. Anything below 0 is an error signal.</returns>
68-
int Create(const std::string &soundFilePath, bool immobile = false, bool affectedByGlobalPitch = true) { AddSound(soundFilePath); SetImmobile(immobile); SetAffectedByGlobalPitch(affectedByGlobalPitch); return 0; }
68+
int Create(const std::string &soundFilePath, bool immobile = false, bool affectedByGlobalPitch = true) { AddSound(soundFilePath, true); SetImmobile(immobile); SetAffectedByGlobalPitch(affectedByGlobalPitch); return 0; }
6969
#pragma endregion
7070

7171
#pragma region Destruction

Managers/AudioMan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ namespace RTE {
296296
std::string nextString = m_MusicPlayList.front();
297297
m_MusicPlayList.pop_front();
298298

299-
if (nextString.at(0) == '@') {
299+
if (!nextString.empty() && nextString.at(0) == '@') {
300300
try {
301301
int seconds = std::stoi(nextString.substr(1, nextString.size()));
302302
m_SilenceTimer.SetRealTimeLimitS((seconds > 0) ? seconds : 0);

System/ContentFile.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ namespace RTE {
184184

185185
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
186186

187-
FMOD::Sound * ContentFile::GetAsSample(bool abortGameForInvalidSound, bool asyncLoading) {
187+
FMOD::Sound * ContentFile::GetAsSound(bool abortGameForInvalidSound, bool asyncLoading) {
188188
if (m_DataPath.empty() || !g_AudioMan.IsAudioEnabled()) {
189189
return nullptr;
190190
}
@@ -194,7 +194,7 @@ namespace RTE {
194194
if (foundSound != s_LoadedSamples.end()) {
195195
returnSample = (*foundSound).second;
196196
} else {
197-
returnSample = LoadAndReleaseSample(abortGameForInvalidSound, asyncLoading); //NOTE: This takes ownership of the sample file
197+
returnSample = LoadAndReleaseSound(abortGameForInvalidSound, asyncLoading); //NOTE: This takes ownership of the sample file
198198

199199
// Insert the Sound object into the map, PASSING OVER OWNERSHIP OF THE LOADED FILE
200200
s_LoadedSamples.insert(std::pair<std::string, FMOD::Sound *>(m_DataPath, returnSample));
@@ -204,7 +204,7 @@ namespace RTE {
204204

205205
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
206206

207-
FMOD::Sound * ContentFile::LoadAndReleaseSample(bool abortGameForInvalidSound, bool asyncLoading) {
207+
FMOD::Sound * ContentFile::LoadAndReleaseSound(bool abortGameForInvalidSound, bool asyncLoading) {
208208
if (m_DataPath.empty() || !g_AudioMan.IsAudioEnabled()) {
209209
return nullptr;
210210
}
@@ -218,18 +218,24 @@ namespace RTE {
218218
if (abortGameForInvalidSound) {
219219
RTEAbort("Failed to find audio file with following path and name:\n\n" + m_DataPath + " or " + altFileExtension + "\n" + m_FormattedReaderPosition);
220220
} else {
221-
g_ConsoleMan.PrintString("Failed to find audio file with following path and name:\n\n" + m_DataPath + " or " + altFileExtension + ". The file was not loaded!");
221+
g_ConsoleMan.PrintString("Failed to find audio file with following path and name:\n" + m_DataPath + " or " + altFileExtension + ". The file was not loaded!");
222222
return nullptr;
223223
}
224224
}
225225
}
226+
if (std::filesystem::file_size(m_DataPath) == 0) {
227+
const std::string errorMessage = "Failed to create sound because because the file was empty. The path and name were: ";
228+
RTEAssert(!abortGameForInvalidSound, errorMessage + "\n\n" + m_DataPathAndReaderPosition);
229+
g_ConsoleMan.PrintString("ERROR: " + errorMessage + m_DataPath);
230+
return nullptr;
231+
}
226232
FMOD::Sound *returnSample = nullptr;
227233

228234
FMOD_MODE fmodFlags = FMOD_CREATESAMPLE | FMOD_3D | (asyncLoading ? FMOD_NONBLOCKING : FMOD_DEFAULT);
229235
FMOD_RESULT result = g_AudioMan.GetAudioSystem()->createSound(m_DataPath.c_str(), fmodFlags, nullptr, &returnSample);
230236

231237
if (result != FMOD_OK) {
232-
const std::string errorMessage = "Failed to create sound because of FMOD error: " + std::string(FMOD_ErrorString(result)) + "\nThe path and name were: ";
238+
const std::string errorMessage = "Failed to create sound because of FMOD error: " + std::string(FMOD_ErrorString(result)) + " The path and name were: ";
233239
RTEAssert(!abortGameForInvalidSound, errorMessage + "\n\n" + m_DataPathAndReaderPosition);
234240
g_ConsoleMan.PrintString("ERROR: " + errorMessage + m_DataPath);
235241
return returnSample;

System/ContentFile.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ namespace RTE {
145145
/// <param name="abortGameForInvalidSound">Whether to abort the game if the sound couldn't be added, or just show a console error. Default true.</param>
146146
/// <param name="asyncLoading">Whether to enable FMOD asynchronous loading or not. Should be disabled for loading audio files with Lua AddSound.
147147
/// <returns>Pointer to the FSOUND_SAMPLE loaded from disk.</returns>
148-
FMOD::Sound * GetAsSample(bool abortGameForInvalidSound = true, bool asyncLoading = true);
148+
FMOD::Sound * GetAsSound(bool abortGameForInvalidSound = true, bool asyncLoading = true);
149149

150150
/// <summary>
151151
/// Loads and transfers the data represented by this ContentFile object as an FMOD FSOUND_SAMPLE. Ownership of the FSOUND_SAMPLE is NOT transferred!
152152
/// </summary>
153153
/// <param name="abortGameForInvalidSound">Whether to abort the game if the sound couldn't be added, or just show a console error</param>
154154
/// <param name="asyncLoading">Whether to enable FMOD asynchronous loading or not. Should be disabled for loading audio files with Lua AddSound.
155155
/// <returns>Pointer to the FSOUND_SAMPLE loaded from disk.</returns>
156-
FMOD::Sound * LoadAndReleaseSample(bool abortGameForInvalidSound, bool asyncLoading);
156+
FMOD::Sound * LoadAndReleaseSound(bool abortGameForInvalidSound, bool asyncLoading);
157157
#pragma endregion
158158

159159
#pragma region Class Info

0 commit comments

Comments
 (0)