From 552f1b21c2ae1b31583510fd5ba957169049aaff Mon Sep 17 00:00:00 2001 From: NyakoFox Date: Sat, 12 Apr 2025 18:21:04 -0300 Subject: [PATCH] Add warning for `playef` command and lower sound IDs Ethan mentioned how case sensitivity could be a concern -- this commit lowers the filename to create the sound ID, and also strips spaces. Script arguments are always lowered, and the spaces get removed, so the IDs are modified to match this behavior. If someone types in the command `playef(horses REALLY COOL)`, the argument will get converted to `horsesreallycool`. Likewise, the filename "horses REALLY COOL.wav" will get converted to `horsesreallycool`. To the level creator, they can just enter the same name twice and their sound will play. This commit also adds a warning for the `playef` script command. While most creators do not open the console, some warning somewhere is better than nothing. I do think we should eventually have some sort of system where it's easier to view level warnings, but for now this should be sufficient. --- desktop_version/src/Music.cpp | 44 +++++++++++++++++++++++++++------- desktop_version/src/Music.h | 4 ++-- desktop_version/src/Script.cpp | 13 +++++++--- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/desktop_version/src/Music.cpp b/desktop_version/src/Music.cpp index 5a3a8fa8..29a06388 100644 --- a/desktop_version/src/Music.cpp +++ b/desktop_version/src/Music.cpp @@ -795,8 +795,31 @@ void musicclass::init(void) char id[256]; SDL_snprintf(asset_filename, sizeof(asset_filename), "sounds/%s", item); - // We need an ID, so chop off the extension - SDL_strlcpy(id, item, sizeof(id)); + // Create the ID + size_t current_char = 0; + size_t item_len = SDL_strlen(item); + for (size_t i = 0; i < item_len; i++) + { + // If it's a space, we don't want to include this. + if (item[i] == ' ') + { + continue; + } + // Otherwise, add it to our ID string, lowered + id[current_char] = SDL_tolower(item[i]); + + current_char++; + + if (current_char >= 255) + { + break; + } + } + + // Null-terminate the string + id[current_char] = '\0'; + + // Chop off the extension! char* dot = SDL_strrchr(id, '.'); if (dot != NULL) { @@ -1322,26 +1345,31 @@ void musicclass::changemusicarea(int x, int y) niceplay(track); } -void musicclass::playef(int t) +bool musicclass::playef(int t) { if (!INBOUNDS_VEC(t, soundTracks)) { - return; + return false; } - soundTracks[t].Play(); + if (soundTracks[t].valid) + { + soundTracks[t].Play(); + return true; + } + return false; } -void musicclass::playefid(const char* id) +bool musicclass::playefid(const char* id) { for (size_t i = 0; i < soundTracks.size(); i++) { if (SDL_strcmp(soundTracks[i].id, id) == 0) { - playef(i); - return; + return playef(i); } } vlog_error("playefid() couldn't find sound ID: %s", id); + return false; } bool musicclass::soundidexists(const char* id) diff --git a/desktop_version/src/Music.h b/desktop_version/src/Music.h index 3705a0d7..c96c3ff5 100644 --- a/desktop_version/src/Music.h +++ b/desktop_version/src/Music.h @@ -94,8 +94,8 @@ public: int currentsong; int haltedsong; - void playef(int t); - void playefid(const char* id); + bool playef(int t); + bool playefid(const char* id); bool soundidexists(const char* id); bool soundisextra(int t); const char* getsoundid(int t); diff --git a/desktop_version/src/Script.cpp b/desktop_version/src/Script.cpp index a1aff414..e14066aa 100644 --- a/desktop_version/src/Script.cpp +++ b/desktop_version/src/Script.cpp @@ -447,13 +447,20 @@ void scriptclass::run(void) } if (words[0] == "playef") { + bool played = false; + int sound_id = help.Int(words[1].c_str(), -1); + if (music.soundidexists(words[1].c_str())) { - music.playefid(words[1].c_str()); + played = music.playefid(words[1].c_str()); } - else if (!music.soundisextra(ss_toi(words[1]))) + else if (!music.soundisextra(sound_id)) { - music.playef(ss_toi(words[1])); + played = music.playef(sound_id); + } + if (!played) + { + vlog_error("playef() couldn't play sound: %s", words[1].c_str()); } } if (words[0] == "play")