File: playlist.cpp

package info (click to toggle)
warzone2100 4.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,320 kB
  • sloc: cpp: 676,209; ansic: 391,201; javascript: 78,238; python: 16,632; php: 4,294; sh: 4,094; makefile: 2,629; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (647 lines) | stat: -rw-r--r-- 18,618 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*
	This file is part of Warzone 2100.
	Copyright (C) 1999-2004  Eidos Interactive
	Copyright (C) 2005-2020  Warzone 2100 Project

	Warzone 2100 is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Warzone 2100 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <nlohmann/json.hpp> // Must come before WZ includes

#include "lib/framework/frame.h"
#include "lib/framework/file.h"
#include "lib/framework/string_ext.h"
#include "lib/framework/stdio_ext.h"
#include "lib/framework/physfs_ext.h"

#include "playlist.h"
#include "cdaudio.h"

#include <algorithm>
#include <unordered_map>
#include <sstream>
#include <limits>

#include <nonstd/optional.hpp>
using nonstd::optional;
using nonstd::nullopt;

// MARK: - WZ_TRACK_SETTINGS

struct WZ_TRACK_SETTINGS
{
	bool music_modes[NUM_MUSICGAMEMODES] = {false};

public:
	void setMusicMode(MusicGameMode mode, bool enabled)
	{
		music_modes[static_cast<std::underlying_type<MusicGameMode>::type>(mode)] = enabled;
	}
};

inline void to_json(nlohmann::json& j, const WZ_TRACK_SETTINGS& p)
{
	j = nlohmann::json::object();
	nlohmann::json music_modes = nlohmann::json::array();
	for (size_t i = 0; i < NUM_MUSICGAMEMODES; i++)
	{
		music_modes[i] = p.music_modes[i];
	}
	j["music_modes"] = music_modes;
}

inline void from_json(const nlohmann::json& j, WZ_TRACK_SETTINGS& p)
{
	if (!j.is_object())
	{
		throw nlohmann::json::type_error::create(302, "type must be an object, but is " + std::string(j.type_name()), &j);
	}
	auto it = j.find("music_modes");
	if (it != j.end())
	{
		if (!it.value().is_array())
		{
			throw nlohmann::json::type_error::create(302, "music_modes type must be an array, but is " + std::string(j.type_name()), &j);
		}
		size_t i = 0;
		for (const auto& v : it.value())
		{
			if (i >= NUM_MUSICGAMEMODES)
			{
				break;
			}
			p.music_modes[i] = v.get<bool>();
			i++;
		}
	}
}

struct WZ_TRACK_SETTINGS_PAIR
{
	WZ_TRACK_SETTINGS default_settings;
	WZ_TRACK_SETTINGS user_settings;
};

// MARK: - WZ_Playlist_Preferences

class WZ_Playlist_Preferences
{
public:
	WZ_Playlist_Preferences(const std::string &fileName);

	optional<WZ_TRACK_SETTINGS> getTrackSettings(const std::shared_ptr<const WZ_TRACK>& track);
	void setTrackSettings(const std::shared_ptr<const WZ_TRACK>& track, const WZ_TRACK_SETTINGS& settings);

	void clearAllPreferences();
	bool savePreferences();

private:
	std::string keyForTrack(const std::shared_ptr<const WZ_TRACK>& track) const;
	void storeValueForTrack(const std::shared_ptr<const WZ_TRACK>& track, const std::string& key, const nlohmann::json& value);
	optional<nlohmann::json> getJSONValueForTrack(const std::shared_ptr<const WZ_TRACK>& track, const std::string& key) const;

	template<typename T>
	optional<T> getValueForTrack(const std::shared_ptr<const WZ_TRACK>& track, const std::string& key) const
	{
		optional<T> result = nullopt;
		try {
			auto jsonResult = getJSONValueForTrack(track, key);
			if (!jsonResult.has_value())
			{
				return nullopt;
			}
			result = jsonResult.value().get<T>();
		}
		catch (const std::exception &e) {
			debug(LOG_WARNING, "Failed to convert json_variant to %s because of error: %s", typeid(T).name(), e.what());
			return nullopt;
		}
		catch (...) {
			debug(LOG_FATAL, "Unexpected exception encountered: json_variant::toType<%s>", typeid(T).name());
		}
		return result;
	}

private:
	nlohmann::json mRoot;
	std::string mFilename;
};

WZ_Playlist_Preferences::WZ_Playlist_Preferences(const std::string &fileName)
: mFilename(fileName)
{
	if (PHYSFS_exists(fileName.c_str()))
	{
		UDWORD size;
		char *data;
		if (loadFile(fileName.c_str(), &data, &size))
		{
			try {
				mRoot = nlohmann::json::parse(data, data + size);
			}
			catch (const std::exception &e) {
				ASSERT(false, "JSON document from %s is invalid: %s", fileName.c_str(), e.what());
			}
			catch (...) {
				debug(LOG_ERROR, "Unexpected exception parsing JSON %s", fileName.c_str());
			}
			ASSERT(!mRoot.is_null(), "JSON document from %s is null", fileName.c_str());
			ASSERT(mRoot.is_object(), "JSON document from %s is not an object. Read: \n%s", fileName.c_str(), data);
			free(data);
		}
		else
		{
			debug(LOG_ERROR, "Could not open \"%s\"", fileName.c_str());
			// treat as if no preferences exist yet
			mRoot = nlohmann::json::object();
		}
	}
	else
	{
		// no preferences exist yet
		mRoot = nlohmann::json::object();
	}

	// always ensure there's a "notifications" dictionary in the root object
	auto notificationsObject = mRoot.find("tracks");
	if (notificationsObject == mRoot.end() || !notificationsObject->is_object())
	{
		// create a dictionary object
		mRoot["tracks"] = nlohmann::json::object();
	}
}

std::string WZ_Playlist_Preferences::keyForTrack(const std::shared_ptr<const WZ_TRACK>& track) const
{
	// remove the "music/albums/" prefix
	std::string result = track->filename;
	if (result.rfind("music/albums/", 0) == 0)
	{
		result = result.substr(13);
	}
	return result;
}

void WZ_Playlist_Preferences::storeValueForTrack(const std::shared_ptr<const WZ_TRACK>& track, const std::string& key, const nlohmann::json& value)
{
	nlohmann::json& trackSettingsObj = mRoot["tracks"];
	std::string trackKey = keyForTrack(track);
	auto trackData = trackSettingsObj.find(trackKey);
	if (trackData == trackSettingsObj.end() || !trackData->is_object())
	{
		trackSettingsObj[trackKey] = nlohmann::json::object();
		trackData = trackSettingsObj.find(trackKey);
	}
	(*trackData)[key] = value;
}

optional<nlohmann::json> WZ_Playlist_Preferences::getJSONValueForTrack(const std::shared_ptr<const WZ_TRACK>& track, const std::string& key) const
{
	try {
		return mRoot.at("tracks").at(keyForTrack(track)).at(key);
	}
	catch (nlohmann::json::out_of_range&)
	{
		// some part of the path doesn't exist yet
		return nullopt;
	}
}

optional<WZ_TRACK_SETTINGS> WZ_Playlist_Preferences::getTrackSettings(const std::shared_ptr<const WZ_TRACK>& track)
{
	return getValueForTrack<WZ_TRACK_SETTINGS>(track, "settings");
}

void WZ_Playlist_Preferences::setTrackSettings(const std::shared_ptr<const WZ_TRACK>& track, const WZ_TRACK_SETTINGS& settings)
{
	storeValueForTrack(track, "settings", settings);
}

void WZ_Playlist_Preferences::clearAllPreferences()
{
	mRoot["tracks"] = nlohmann::json::object();
}

bool WZ_Playlist_Preferences::savePreferences()
{
	std::string jsonString = mRoot.dump(4);
#if SIZE_MAX >= UDWORD_MAX
	ASSERT_OR_RETURN(false, jsonString.size() <= static_cast<size_t>(std::numeric_limits<UDWORD>::max()), "jsonString.size (%zu) exceeds UDWORD::max", jsonString.size());
#endif
	saveFile(mFilename.c_str(), jsonString.c_str(), static_cast<UDWORD>(jsonString.size()));
	return true;
}

// MARK: - Globals

static std::vector<std::shared_ptr<WZ_ALBUM>> albumList;
static std::vector<std::shared_ptr<const WZ_TRACK>> fullTrackList;
typedef std::unordered_map<std::shared_ptr<const WZ_TRACK>, WZ_TRACK_SETTINGS_PAIR> TRACK_SETTINGS_MAP;
static TRACK_SETTINGS_MAP trackToSettingsMap;
static optional<size_t> currentSong = nullopt;
static MusicGameMode lastFilteredMode = MusicGameMode::MENUS;
static std::function<bool (const std::shared_ptr<const WZ_TRACK>& track)> currentFilterFunc;
static WZ_Playlist_Preferences* playlistPrefs = nullptr;

// MARK: - Core PlayList Functions

static void PlayList_ClearAll()
{
	albumList.clear();
	fullTrackList.clear();
	trackToSettingsMap.clear();
	currentFilterFunc = nullptr;
	currentSong = nullopt;
	if (playlistPrefs)
	{
		delete playlistPrefs;
		playlistPrefs = nullptr;
	}
}

void PlayList_Init()
{
	PlayList_ClearAll();
	playlistPrefs = new WZ_Playlist_Preferences("music.json");
}

void PlayList_Quit()
{
	if (playlistPrefs)
	{
		playlistPrefs->savePreferences();
	}
	PlayList_ClearAll();
}

bool PlayList_IsTrackEnabledForMusicMode(const std::shared_ptr<const WZ_TRACK>& track, MusicGameMode mode)
{
	auto it = trackToSettingsMap.find(track);
	if (it == trackToSettingsMap.end())
	{
		return false;
	}
	return it->second.user_settings.music_modes[static_cast<std::underlying_type<MusicGameMode>::type>(mode)];
}

void PlayList_SetTrackMusicMode(const std::shared_ptr<const WZ_TRACK>& track, MusicGameMode mode, bool enabled)
{
	auto it = trackToSettingsMap.find(track);
	if (it == trackToSettingsMap.end())
	{
		return;
	}
	bool oldValue = it->second.user_settings.music_modes[static_cast<std::underlying_type<MusicGameMode>::type>(mode)];
	if (oldValue != enabled)
	{
		it->second.user_settings.music_modes[static_cast<std::underlying_type<MusicGameMode>::type>(mode)] = enabled;
		if (playlistPrefs)
		{
			playlistPrefs->setTrackSettings(track, it->second.user_settings);
		}
	}
}

static void PlayList_SetTrackDefaultSettings(const std::shared_ptr<const WZ_TRACK>& track, const WZ_TRACK_SETTINGS& default_settings)
{
	auto it = trackToSettingsMap.find(track);
	if (it == trackToSettingsMap.end())
	{
		auto result = trackToSettingsMap.insert(TRACK_SETTINGS_MAP::value_type(track, WZ_TRACK_SETTINGS_PAIR()));
		it = result.first;
	}
	it->second.default_settings = default_settings;
	auto user_settings = playlistPrefs->getTrackSettings(track);
	if (user_settings.has_value())
	{
		// load the saved user settings
		it->second.user_settings = user_settings.value();
	}
	else
	{
		// initialize the user settings to the defaults
		it->second.user_settings = default_settings;
		playlistPrefs->setTrackSettings(track, it->second.user_settings);
	}
}

const std::vector<std::shared_ptr<const WZ_TRACK>>& PlayList_GetFullTrackList()
{
	return fullTrackList;
}

size_t PlayList_SetTrackFilter(const std::function<bool (const std::shared_ptr<const WZ_TRACK>& track)>& filterFunc)
{
	size_t tracksEnabled = 0;
	for (const auto& album : albumList)
	{
		for (const auto& track : album->tracks)
		{
			if (filterFunc(track))
			{
				++tracksEnabled;
			}
		}
	}
	currentFilterFunc = filterFunc;
	return tracksEnabled;
}

size_t PlayList_FilterByMusicMode(MusicGameMode currentMode)
{
	size_t result = 0;
	if (currentMode != MusicGameMode::MENUS)
	{
		result = PlayList_SetTrackFilter([currentMode](const std::shared_ptr<const WZ_TRACK>& track) -> bool {
			return PlayList_IsTrackEnabledForMusicMode(track, currentMode);
		});
	}
	else
	{
		currentFilterFunc = nullptr;
	}
	if (lastFilteredMode != currentMode)
	{
		currentSong = nullopt;
	}
	lastFilteredMode = currentMode;
	return result;
}

MusicGameMode PlayList_GetCurrentMusicMode()
{
	return lastFilteredMode;
}

static std::shared_ptr<WZ_ALBUM> PlayList_LoadAlbum(const nlohmann::json& json, const std::string& sourcePath, const std::string& albumDir)
{
	ASSERT(!json.is_null(), "JSON album from %s is null", sourcePath.c_str());
	ASSERT(json.is_object(), "JSON album from %s is not an object", sourcePath.c_str());

	std::shared_ptr<WZ_ALBUM> album = std::make_shared<WZ_ALBUM>();

#define GET_REQUIRED_ALBUM_KEY_STRVAL(KEY) \
	if (!json.contains(#KEY)) \
	{ \
		debug(LOG_ERROR, "%s: Missing required `" #KEY "` key", sourcePath.c_str()); \
		return nullptr; \
	} \
	try { \
		album->KEY = json[#KEY].get<std::string>(); \
	} catch (const std::exception &e) { \
		debug(LOG_ERROR, "%s: Failed to get `" #KEY "` key, error: %s", sourcePath.c_str(), e.what()); \
		return nullptr; \
	}

	GET_REQUIRED_ALBUM_KEY_STRVAL(title);
	GET_REQUIRED_ALBUM_KEY_STRVAL(author);
	GET_REQUIRED_ALBUM_KEY_STRVAL(date);
	GET_REQUIRED_ALBUM_KEY_STRVAL(description);
	GET_REQUIRED_ALBUM_KEY_STRVAL(album_cover_filename);
	if (!album->album_cover_filename.empty())
	{
		album->album_cover_filename = albumDir + "/" + album->album_cover_filename;
	}

	if (!json.contains("tracks"))
	{
		debug(LOG_ERROR, "Missing required `tracks` key");
		return nullptr;
	}
	if (!json["tracks"].is_array())
	{
		debug(LOG_ERROR, "Required key `tracks` should be an array");
		return nullptr;
	}

#define GET_REQUIRED_TRACK_KEY(KEY, TYPE) \
	if (!trackJson.contains(#KEY)) \
	{ \
		debug(LOG_ERROR, "%s: Missing required track key: `" #KEY "`", sourcePath.c_str()); \
		return nullptr; \
	} \
	try { \
		track->KEY = trackJson[#KEY].get<TYPE>(); \
	} catch (const std::exception &e) { \
		debug(LOG_ERROR, "%s: Failed to get track key `" #KEY "`, error: %s", sourcePath.c_str(), e.what()); \
		return nullptr; \
	}

	for (auto& trackJson : json["tracks"])
	{
		std::shared_ptr<WZ_TRACK> track = std::make_shared<WZ_TRACK>();
		GET_REQUIRED_TRACK_KEY(filename, std::string);
		if (track->filename.empty())
		{
			// Track must have a filename
			debug(LOG_ERROR, "%s: Empty or invalid filename for track", sourcePath.c_str());
			return nullptr;
		}
		track->filename = albumDir + "/" + track->filename;
		GET_REQUIRED_TRACK_KEY(title, std::string);
		GET_REQUIRED_TRACK_KEY(author, std::string);
		GET_REQUIRED_TRACK_KEY(base_volume, unsigned int);
		GET_REQUIRED_TRACK_KEY(bpm, unsigned int);
		track->album = std::weak_ptr<WZ_ALBUM>(album);

		WZ_TRACK_SETTINGS default_settings;
		// default_music_modes
		if (!trackJson.contains("default_music_modes"))
		{
			debug(LOG_ERROR, "%s: Missing required track key: `default_music_modes`", sourcePath.c_str());
			return nullptr;
		}
		if (!trackJson["default_music_modes"].is_array())
		{
			debug(LOG_ERROR, "%s: Required track key `default_music_modes` should be an array", sourcePath.c_str());
			return nullptr;
		}
		for (const auto& musicModeJSON : trackJson["default_music_modes"])
		{
			if (!musicModeJSON.is_string())
			{
				debug(LOG_ERROR, "%s: `default_music_modes` array item should be a string", sourcePath.c_str());
				continue;
			}
			std::string musicMode = musicModeJSON.get<std::string>();
			if (musicMode == "campaign")
			{
				default_settings.setMusicMode(MusicGameMode::CAMPAIGN, true);
			}
			else if (musicMode == "challenge")
			{
				default_settings.setMusicMode(MusicGameMode::CHALLENGE, true);
			}
			else if (musicMode == "skirmish")
			{
				default_settings.setMusicMode(MusicGameMode::SKIRMISH, true);
			}
			else if (musicMode == "multiplayer")
			{
				default_settings.setMusicMode(MusicGameMode::MULTIPLAYER, true);
			}
			else
			{
				debug(LOG_WARNING, "%s: `default_music_modes` array item is unknown value: %s", sourcePath.c_str(), musicMode.c_str());
				continue;
			}
		}
		PlayList_SetTrackDefaultSettings(track, default_settings);
		album->tracks.push_back(std::move(track));
	}

	return album;
}

bool PlayList_Read(const char *path)
{
	UDWORD size = 0;
	char *data = nullptr;
	std::string albumsPath = astringf("%s/albums", path);

	WZ_PHYSFS_enumerateFolders(albumsPath.c_str(), [&](const char *i) -> bool {
		std::string albumDir = albumsPath + "/" + i;
		std::string str = albumDir + "/album.json";
		if (!PHYSFS_exists(str.c_str()))
		{
			return true; // continue;
		}
		if (!loadFile(str.c_str(), &data, &size))
		{
			debug(LOG_ERROR, "album JSON file \"%s\" could not be opened!", str.c_str());
			return true; // continue;
		}
		nlohmann::json tmpJson;
		try {
			tmpJson = nlohmann::json::parse(data, data + size);
		}
		catch (const std::exception &e) {
			debug(LOG_ERROR, "album JSON file %s is invalid: %s", str.c_str(), e.what());
			return true; // continue;
		}
		catch (...) {
			debug(LOG_FATAL, "Unexpected exception parsing album JSON from %s", str.c_str());
		}
		ASSERT(!tmpJson.is_null(), "JSON album from %s is null", str.c_str());
		ASSERT(tmpJson.is_object(), "JSON album from %s is not an object. Read: \n%s", str.c_str(), data);
		free(data);

		// load album data
		auto album = PlayList_LoadAlbum(tmpJson, str, albumDir);
		if (!album)
		{
			debug(LOG_ERROR, "Failed to load album JSON: %s", str.c_str());
			return true; // continue;
		}
		albumList.push_back(std::move(album));
		return true; // continue
	});

	// Ensure that "Warzone 2100 OST" is always the first album in the list
	std::stable_sort(albumList.begin(), albumList.end(), [](const std::shared_ptr<WZ_ALBUM>& a, const std::shared_ptr<WZ_ALBUM>& b) -> bool {
		if (a == b) { return false; }
		if (a->title == "Warzone 2100 OST") { return true; }
		return false;
	});

	// generate full track list
	fullTrackList.clear();
	for (const auto& album : albumList)
	{
		for (const auto& track : album->tracks)
		{
			fullTrackList.push_back(track);
		}
	}
	currentSong = nullopt;

	return !albumList.empty();
}

static optional<size_t> PlayList_FindNextMatchingTrack(size_t startingSongIdx)
{
	if (!currentFilterFunc) { return nullopt; }
	if (fullTrackList.empty()) { return nullopt; }
	size_t idx = (startingSongIdx < (fullTrackList.size() - 1)) ? (startingSongIdx + 1) : 0;
	while (idx != startingSongIdx)
	{
		if (currentFilterFunc(fullTrackList[idx]))
		{
			return idx;
		}
		idx = (idx < (fullTrackList.size() - 1)) ? (idx + 1) : 0;
	}
	return nullopt;
}

std::shared_ptr<const WZ_TRACK> PlayList_CurrentSong()
{
	if (fullTrackList.empty())
	{
		return nullptr;
	}
	if (!currentSong.has_value() || (currentSong.value() >= fullTrackList.size()))
	{
		currentSong = PlayList_FindNextMatchingTrack(fullTrackList.size() - 1);
		if (!currentSong.has_value())
		{
			return nullptr;
		}
	}
	return fullTrackList[currentSong.value()];
}

std::shared_ptr<const WZ_TRACK> PlayList_NextSong()
{
	size_t currentSongIdx = currentSong.has_value() ? currentSong.value() : (fullTrackList.size() - 1);
	currentSong = PlayList_FindNextMatchingTrack(currentSongIdx);
	return PlayList_CurrentSong();
}

std::shared_ptr<const WZ_TRACK> PlayList_RandomizeCurrentSong()
{
	if (fullTrackList.empty())
	{
		return nullptr;
	}
	size_t incrementByNum = ((size_t)rand() % fullTrackList.size());
	size_t currentSongIdx = currentSong.has_value() ? currentSong.value() : (fullTrackList.size() - 1);
	for (size_t i = 0; i < incrementByNum; i++)
	{
		currentSong = PlayList_FindNextMatchingTrack(currentSongIdx);
		if (!currentSong.has_value())
		{
			break;
		}
		currentSongIdx = currentSong.value();
	}
	return PlayList_CurrentSong();
}

bool PlayList_SetCurrentSong(const std::shared_ptr<const WZ_TRACK>& track)
{
	if (!track) { return false; }
	optional<size_t> trackIdx = nullopt;
	for (size_t i = 0; i < fullTrackList.size(); i++)
	{
		if (track == fullTrackList[i])
		{
			trackIdx = i;
			break;
		}
	}
	currentSong = trackIdx;
	return trackIdx.has_value();
}