7
7
#include < iostream>
8
8
#include < sstream>
9
9
#include < random>
10
+ #include < ranges>
10
11
11
12
AUTOHOOK_INIT ()
12
13
@@ -28,7 +29,7 @@ unsigned char EMPTY_WAVE[45] = {0x52, 0x49, 0x46, 0x46, 0x25, 0x00, 0x00, 0x00,
28
29
0x20 , 0x10 , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x02 , 0x00 , 0x44 , 0xAC , 0x00 , 0x00 , 0x88 , 0x58 ,
29
30
0x01 , 0x00 , 0x02 , 0x00 , 0x10 , 0x00 , 0x64 , 0x61 , 0x74 , 0x61 , 0x74 , 0x00 , 0x00 , 0x00 , 0x00 };
30
31
31
- EventOverrideData::EventOverrideData (const std::string& data, const fs::path& path)
32
+ EventOverrideData::EventOverrideData (const std::string& data, const fs::path& path, const std::vector<std::string>& registeredEvents )
32
33
{
33
34
if (data.length () <= 0 )
34
35
{
@@ -191,6 +192,14 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa
191
192
{
192
193
std::string pathString = file.path ().string ();
193
194
195
+ // Retrieve event id from path (standard?)
196
+ std::string eventId = file.path ().parent_path ().filename ().string ();
197
+ if (std::find (registeredEvents.begin (), registeredEvents.end (), eventId) != registeredEvents.end ())
198
+ {
199
+ spdlog::warn (" {} couldn't be loaded because {} event has already been overrided, skipping." , pathString, eventId);
200
+ continue ;
201
+ }
202
+
194
203
// Open the file.
195
204
std::ifstream wavStream (pathString, std::ios::binary);
196
205
@@ -259,7 +268,7 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa
259
268
LoadedSuccessfully = true ;
260
269
}
261
270
262
- bool CustomAudioManager::TryLoadAudioOverride (const fs::path& defPath)
271
+ bool CustomAudioManager::TryLoadAudioOverride (const fs::path& defPath, std::string modName )
263
272
{
264
273
if (IsDedicatedServer ())
265
274
return true ; // silently fail
@@ -279,19 +288,35 @@ bool CustomAudioManager::TryLoadAudioOverride(const fs::path& defPath)
279
288
280
289
jsonStream.close ();
281
290
282
- std::shared_ptr<EventOverrideData> data = std::make_shared<EventOverrideData>(jsonStringStream.str (), defPath);
291
+ // Pass the list of overriden events to avoid multiple event registrations crash
292
+ auto kv = std::views::keys (m_loadedAudioOverrides);
293
+ std::vector<std::string> keys {kv.begin (), kv.end ()};
294
+ std::shared_ptr<EventOverrideData> data = std::make_shared<EventOverrideData>(jsonStringStream.str (), defPath, keys);
283
295
284
296
if (!data->LoadedSuccessfully )
285
297
return false ; // no logging, the constructor has probably already logged
286
298
287
299
for (const std::string& eventId : data->EventIds )
288
300
{
301
+ if (m_loadedAudioOverrides.contains (eventId))
302
+ {
303
+ spdlog::warn (" \" {}\" mod tried to override sound event \" {}\" but it is already overriden, skipping." , modName, eventId);
304
+ continue ;
305
+ }
289
306
spdlog::info (" Registering sound event {}" , eventId);
290
307
m_loadedAudioOverrides.insert ({eventId, data});
291
308
}
292
309
293
310
for (const auto & eventIdRegexData : data->EventIdsRegex )
294
311
{
312
+ if (m_loadedAudioOverridesRegex.contains (eventIdRegexData.first ))
313
+ {
314
+ spdlog::warn (
315
+ " \" {}\" mod tried to override sound event regex \" {}\" but it is already overriden, skipping." ,
316
+ modName,
317
+ eventIdRegexData.first );
318
+ continue ;
319
+ }
295
320
spdlog::info (" Registering sound event regex {}" , eventIdRegexData.first );
296
321
m_loadedAudioOverridesRegex.insert ({eventIdRegexData.first , data});
297
322
}
0 commit comments