| 1 | /* |
| 2 | * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 | * It is copyright by its individual contributors, as recorded in the |
| 4 | * project's Git history. See COPYING.txt at the top level for license |
| 5 | * terms and a link to the Git history. |
| 6 | */ |
| 7 | /* |
| 8 | * DXX Rebirth "jukebox" code |
| 9 | * MD 2211 <[email protected]>, 2007 |
| 10 | */ |
| 11 | |
| 12 | #include <stdlib.h> |
| 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | #include <ranges> |
| 16 | |
| 17 | #include "hudmsg.h" |
| 18 | #include "songs.h" |
| 19 | #include "jukebox.h" |
| 20 | #include "dxxerror.h" |
| 21 | #include "console.h" |
| 22 | #include "config.h" |
| 23 | #include "strutil.h" |
| 24 | #include "u_mem.h" |
| 25 | #include "physfs_list.h" |
| 26 | |
| 27 | #include "partial_range.h" |
| 28 | #include <memory> |
| 29 | |
| 30 | namespace dcx { |
| 31 | |
| 32 | #define JUKEBOX_HUDMSG_PLAYING "Now playing:" |
| 33 | #define JUKEBOX_HUDMSG_STOPPED "Jukebox stopped" |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | struct m3u_bytes |
| 38 | { |
| 39 | std::ranges::subrange<char *> range; |
| 40 | std::ranges::subrange<char **> ptr_range; |
| 41 | std::unique_ptr<char *[]> alloc; |
| 42 | }; |
| 43 | |
| 44 | class FILE_deleter |
| 45 | { |
| 46 | public: |
| 47 | void operator()(FILE *const p) const |
| 48 | { |
| 49 | fclose(p); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | class list_deleter : PHYSFS_list_deleter |
| 54 | { |
| 55 | public: |
| 56 | /* When `list_pointers` is a PHYSFS allocation, `buf` is nullptr. |
| 57 | * When `list_pointers` is a new[char *[]] allocation, `buf` |
| 58 | * points to the same location as `list_pointers`. |
| 59 | */ |
| 60 | std::unique_ptr<char *[]> buf; |
| 61 | void operator()(char **list) |
| 62 | { |
| 63 | if (buf) |
| 64 | { |
| 65 | assert(buf.get() == list); |
| 66 | buf.reset(); |
| 67 | } |
| 68 | else |
| 69 | this->PHYSFS_list_deleter::operator()(list); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | class list_pointers : public PHYSFSX_uncounted_list_template<list_deleter> |
| 74 | { |
| 75 | using base_type = PHYSFSX_uncounted_list_template<list_deleter>; |
| 76 | public: |
| 77 | using base_type::reset; |
| 78 | void set_combined(std::unique_ptr<char *[]> &&buf) |
| 79 | noexcept( |
| 80 | noexcept(std::declval<base_type>() = base_type{buf.get()}) && |
| 81 | noexcept(std::declval<list_deleter>().buf = std::move(buf)) |
| 82 | ) |
| 83 | { |
| 84 | static_cast<base_type &>(*this) = base_type{buf.get()}; |
| 85 | get_deleter().buf = std::move(buf); |
| 86 | } |
| 87 | void reset(PHYSFSX_uncounted_list list) |
| 88 | noexcept(noexcept(std::declval<base_type>() = base_type{list.release()})) |
| 89 | { |
| 90 | static_cast<base_type &>(*this) = base_type{list.release()}; |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | class jukebox_songs |
| 95 | { |
| 96 | public: |
| 97 | void unload(); |
| 98 | list_pointers list; // the actual list |
| 99 | static const std::size_t max_songs = 1024; // maximum number of pointers that 'list' can hold, i.e. size of list / size of one pointer |
| 100 | }; |
| 101 | |
| 102 | } |
| 103 | |
| 104 | static jukebox_songs JukeboxSongs; |
| 105 | |
| 106 | void jukebox_songs::unload() |
| 107 | { |
| 108 | list.reset(); |
| 109 | } |
| 110 | |
| 111 | void jukebox_unload() |
| 112 | { |
| 113 | JukeboxSongs.unload(); |
| 114 | } |
| 115 | |
| 116 | const std::array<file_extension_t, 5> jukebox_exts{{ |
| 117 | SONG_EXT_HMP, |
| 118 | SONG_EXT_MID, |
| 119 | SONG_EXT_OGG, |
| 120 | SONG_EXT_FLAC, |
| 121 | SONG_EXT_MP3 |
| 122 | }}; |
| 123 | |
| 124 | /* Open an m3u using fopen, not PHYSFS. If the path seems to be under |
| 125 | * PHYSFS, that will be preferred over a raw filesystem path. |
| 126 | */ |
| 127 | static std::unique_ptr<FILE, FILE_deleter> open_m3u_from_disk(const char *const cfgpath) |
| 128 | { |
| 129 | std::array<char, PATH_MAX> absbuf; |
| 130 | return std::unique_ptr<FILE, FILE_deleter>(fopen( |
| 131 | // it's a child of Sharepath, build full path |
| 132 | (PHYSFS_exists(cfgpath) && PHYSFSX_getRealPath(cfgpath, absbuf) |
| 133 | ? absbuf.data() |
| 134 | : cfgpath), "rb") |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | static m3u_bytes read_m3u_bytes_from_disk(const char *const cfgpath) |
| 139 | { |
| 140 | const auto &&f = open_m3u_from_disk(cfgpath); |
| 141 | if (!f) |
| 142 | return {}; |
| 143 | const auto fp = f.get(); |
| 144 | fseek(fp, -1, SEEK_END); |
| 145 | const std::size_t length = ftell(fp) + 1; |
| 146 | const auto juke_max_songs = JukeboxSongs.max_songs; |
| 147 | if (length >= PATH_MAX * juke_max_songs) |
| 148 | return {}; |
| 149 | fseek(fp, 0, SEEK_SET); |
| 150 | /* A file consisting only of single character records and newline |
| 151 | * separators, with no junk newlines, comments, or final terminator, |
| 152 | * will need one pointer per two bytes of file, rounded up. Any |
| 153 | * file that uses longer records, which most will use, will need |
| 154 | * fewer pointers. This expression usually overestimates, sometimes |
| 155 | * substantially. However, it is still more conservative than the |
| 156 | * previous expression, which was to allocate exactly |
| 157 | * `JukeboxSongs.max_songs` pointers without regard to the file size |
| 158 | * or contents. |
| 159 | */ |
| 160 | const auto required_alloc_size = 1 + (length / 2); |
| 161 | const auto max_songs = std::min(required_alloc_size, juke_max_songs); |
| 162 | /* Use T=`char*[]` to ensure alignment. Place pointers before file |
| 163 | * contents to keep the pointer array aligned. |
| 164 | */ |
| 165 | auto &&list_buf = std::make_unique<char*[]>(max_songs + 1 + (length / sizeof(char *))); |
| 166 | const auto p = reinterpret_cast<char *>(list_buf.get() + max_songs); |
| 167 | p[length] = '\0'; // make sure the last string is terminated |
| 168 | return fread(p, length, 1, fp) |
| 169 | ? m3u_bytes{ |
| 170 | unchecked_partial_range(p, length), |
| 171 | unchecked_partial_range(list_buf.get(), max_songs), |
| 172 | std::move(list_buf) |
| 173 | } |
| 174 | : m3u_bytes{}; |
| 175 | } |
| 176 | |
| 177 | [[nodiscard]] |
| 178 | static std::size_t read_m3u(void) |
| 179 | { |
| 180 | auto &&m3u = read_m3u_bytes_from_disk(CGameCfg.CMLevelMusicPath.data()); |
| 181 | auto &list_buf = m3u.alloc; |
| 182 | if (!list_buf) |
| 183 | return 0; |
| 184 | |
| 185 | // The growing string list is allocated last, hopefully reducing memory fragmentation when it grows |
| 186 | const auto eol = [](char c) { |
| 187 | return c == '\n' || c == '\r' || !c; |
| 188 | }; |
| 189 | JukeboxSongs.list.set_combined(std::move(list_buf)); |
| 190 | const auto &range = m3u.range; |
| 191 | const auto ib = m3u.ptr_range.begin(); |
| 192 | auto pp = ib; |
| 193 | for (auto buf = range.begin(); buf != range.end(); ++buf) |
| 194 | { |
| 195 | for (; buf != range.end() && eol(*buf);) // find new line - support DOS, Unix and Mac line endings |
| 196 | buf++; |
| 197 | if (buf == range.end()) |
| 198 | break; |
| 199 | if (*buf != '#') // ignore comments / extra info |
| 200 | { |
| 201 | *pp++ = buf; |
| 202 | if (pp == m3u.ptr_range.end()) |
| 203 | break; |
| 204 | } |
| 205 | for (; buf != range.end(); ++buf) // find end of line |
| 206 | if (eol(*buf)) |
| 207 | { |
| 208 | *buf = 0; |
| 209 | break; |
| 210 | } |
| 211 | if (buf == range.end()) |
| 212 | break; |
| 213 | } |
| 214 | return std::distance(ib, pp); |
| 215 | } |
| 216 | |
| 217 | /* Loads music file names from a given directory or M3U playlist */ |
| 218 | void jukebox_load() |
| 219 | { |
| 220 | jukebox_unload(); |
| 221 | |
| 222 | // Check if it's an M3U file |
| 223 | auto &cfgpath = CGameCfg.CMLevelMusicPath; |
| 224 | size_t musiclen = strlen(cfgpath.data()); |
| 225 | std::size_t num_songs; |
| 226 | if (musiclen > 4 && !d_stricmp(&cfgpath[musiclen - 4], ".m3u")) |
| 227 | num_songs = read_m3u(); |
| 228 | else // a directory |
| 229 | { |
| 230 | const char *sep = PHYSFS_getDirSeparator(); |
| 231 | size_t seplen = strlen(sep); |
| 232 | |
| 233 | // stick a separator on the end if necessary. |
| 234 | if (musiclen >= seplen) |
| 235 | { |
| 236 | auto p = &cfgpath[musiclen - seplen]; |
| 237 | if (strcmp(p, sep)) |
| 238 | cfgpath.copy_if(musiclen, sep, seplen); |
| 239 | } |
| 240 | |
| 241 | const auto p = cfgpath.data(); |
| 242 | RAIIPHYSFS_LiteralMount new_path; |
| 243 | // Read directory using PhysicsFS |
| 244 | if (PHYSFS_isDirectory(p)) // find files in relative directory |
| 245 | JukeboxSongs.list.reset(PHYSFSX_findFiles(p, jukebox_exts)); |
| 246 | else |
| 247 | { |
| 248 | if (!PHYSFS_getMountPoint(p)) |
| 249 | new_path.reset(p); |
| 250 | PHYSFS_mount(p, nullptr, 0); |
| 251 | |
| 252 | // as mountpoints are no option (yet), make sure only files originating from GameCfg.CMLevelMusicPath are aded to the list. |
| 253 | JukeboxSongs.list.reset(PHYSFSX_findabsoluteFiles("", p, jukebox_exts)); |
| 254 | } |
| 255 | |
| 256 | if (!JukeboxSongs.list) |
| 257 | { |
| 258 | return; |
| 259 | } |
| 260 | std::size_t count = 0; |
| 261 | auto ii = JukeboxSongs.list.begin(); |
| 262 | auto ie = JukeboxSongs.list.end(); |
| 263 | for (; ii != ie; ++ii) |
| 264 | ++ count; |
| 265 | num_songs = count; |
| 266 | } |
| 267 | |
| 268 | if (num_songs) |
| 269 | { |
| 270 | con_printf(CON_DEBUG,"Jukebox: %" DXX_PRI_size_type " music file(s) found in %s", num_songs, cfgpath.data()); |
| 271 | if (CGameCfg.CMLevelMusicTrack[1] != num_songs) |
| 272 | { |
| 273 | CGameCfg.CMLevelMusicTrack[1] = num_songs; |
| 274 | CGameCfg.CMLevelMusicTrack[0] = 0; // number of songs changed so start from beginning. |
| 275 | } |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | CGameCfg.CMLevelMusicTrack[0] = -1; |
| 280 | CGameCfg.CMLevelMusicTrack[1] = -1; |
| 281 | con_puts(CON_DEBUG,"Jukebox music could not be found!"); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // To proceed tru our playlist. Usually used for continous play, but can loop as well. |
| 286 | static void jukebox_hook_next() |
| 287 | { |
| 288 | if (!JukeboxSongs.list || CGameCfg.CMLevelMusicTrack[0] == -1) |
| 289 | return; |
| 290 | |
| 291 | if (CGameCfg.CMLevelMusicPlayOrder == LevelMusicPlayOrder::Random) |
| 292 | CGameCfg.CMLevelMusicTrack[0] = d_rand() % CGameCfg.CMLevelMusicTrack[1]; // simply a random selection - no check if this song has already been played. But that's how I roll! |
| 293 | else |
| 294 | CGameCfg.CMLevelMusicTrack[0]++; |
| 295 | if (CGameCfg.CMLevelMusicTrack[0] + 1 > CGameCfg.CMLevelMusicTrack[1]) |
| 296 | CGameCfg.CMLevelMusicTrack[0] = 0; |
| 297 | |
| 298 | jukebox_play(); |
| 299 | } |
| 300 | |
| 301 | // Play tracks from Jukebox directory. Play track specified in GameCfg.CMLevelMusicTrack[0] and loop depending on CGameCfg.CMLevelMusicPlayOrder |
| 302 | int jukebox_play() |
| 303 | { |
| 304 | |
| 305 | if (!JukeboxSongs.list) |
| 306 | return 0; |
| 307 | |
| 308 | if (CGameCfg.CMLevelMusicTrack[0] < 0 || |
| 309 | CGameCfg.CMLevelMusicTrack[0] + 1 > CGameCfg.CMLevelMusicTrack[1]) |
| 310 | return 0; |
| 311 | |
| 312 | auto music_filename{JukeboxSongs.list[CGameCfg.CMLevelMusicTrack[0]]}; |
| 313 | if (!music_filename) |
| 314 | return 0; |
| 315 | |
| 316 | const size_t size_music_filename = strlen(music_filename); |
| 317 | auto &cfgpath = CGameCfg.CMLevelMusicPath; |
| 318 | const size_t musiclen = strlen(cfgpath.data()); |
| 319 | { |
| 320 | char *LevelMusicPath; |
| 321 | std::unique_ptr<char[]> full_filename; |
| 322 | if (musiclen > 4 && !d_stricmp(&cfgpath[musiclen - 4], ".m3u")) // if it's from an M3U playlist |
| 323 | LevelMusicPath = music_filename; |
| 324 | else // if it's from a specified path |
| 325 | { |
| 326 | const std::size_t size_full_filename = musiclen + size_music_filename + 1; |
| 327 | full_filename = std::make_unique<char[]>(size_full_filename); |
| 328 | LevelMusicPath = full_filename.get(); |
| 329 | snprintf(full_filename.get(), size_full_filename, "%s%s", cfgpath.data(), music_filename); |
| 330 | } |
| 331 | |
| 332 | if (!songs_play_file(LevelMusicPath, (CGameCfg.CMLevelMusicPlayOrder == LevelMusicPlayOrder::Level ? 1 : 0), (CGameCfg.CMLevelMusicPlayOrder == LevelMusicPlayOrder::Level ? nullptr : jukebox_hook_next))) |
| 333 | { |
| 334 | return 0; // whoops, got an error |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // Formatting a pretty message |
| 339 | const char *prefix = "..."; |
| 340 | constexpr std::size_t MUSIC_HUDMSG_MAXLEN{40}; |
| 341 | if (size_music_filename >= MUSIC_HUDMSG_MAXLEN) { |
| 342 | music_filename += size_music_filename - MUSIC_HUDMSG_MAXLEN; |
| 343 | } else { |
| 344 | prefix += 3; |
| 345 | } |
| 346 | |
| 347 | HUD_init_message(HM_DEFAULT, "%s %s%s", JUKEBOX_HUDMSG_PLAYING, prefix, music_filename); |
| 348 | return 1; |
| 349 | } |
| 350 | |
| 351 | } |