diff --git a/jukebox/jukebox/compat/v2.cpp b/jukebox/jukebox/compat/v2.cpp index e8f74aa..9d86e11 100644 --- a/jukebox/jukebox/compat/v2.cpp +++ b/jukebox/jukebox/compat/v2.cpp @@ -6,11 +6,12 @@ #include #include -#include #include #include #include +#include #include +#include #include #include @@ -25,24 +26,20 @@ namespace compat { namespace v2 { bool isSongValid(const matjson::Value& s) { - return s.contains("songName") && s["songName"].isString() && - s.contains("authorName") && s["authorName"].isString() && - s.contains("path") && s["path"].isString(); + return s.contains("songName") && s["songName"].isString() && s.contains("authorName") && + s["authorName"].isString() && s.contains("path") && s["path"].isString(); } bool manifestExists() { return std::filesystem::exists(manifestPath()); } -std::filesystem::path manifestPath() { - return Mod::get()->getSaveDir() / "nong_data.json"; -} +std::filesystem::path manifestPath() { return Mod::get()->getSaveDir() / "nong_data.json"; } void backupManifest(bool deleteOrig) { if (!manifestExists()) { return; } - const std::filesystem::path backupDir = - Mod::get()->getSaveDir() / ".v2-compat-backup"; + const std::filesystem::path backupDir = Mod::get()->getSaveDir() / ".v2-compat-backup"; bool exists = std::filesystem::exists(backupDir); if (exists && !std::filesystem::is_directory(backupDir)) { @@ -73,21 +70,15 @@ Result parseSong(const matjson::Value& i, int id) { } std::filesystem::path path = - i["path"] - .asString() - .map([](std::string v) { return std::filesystem::path(v); }) - .unwrap(); - - return Ok(LocalSong( - SongMetadata(id, jukebox::random_string(16), - i["songName"].asString().unwrap(), - i["authorName"].asString().unwrap(), std::nullopt, - i["startOffset"].asInt().unwrapOr(0)), - path)); + i["path"].asString().map([](std::string v) { return std::filesystem::path(v); }).unwrap(); + + return Ok( + LocalSong(SongMetadata(id, jukebox::random_string(16), i["songName"].asString().unwrap(), + i["authorName"].asString().unwrap(), std::nullopt, i["startOffset"].asInt().unwrapOr(0)), + path)); } -Result getDefault(int id, const std::filesystem::path& defaultPath, - const matjson::Value& songs) { +Result getDefault(int id, const std::filesystem::path& defaultPath, const matjson::Value& songs) { for (const matjson::Value& i : songs) { if (!isSongValid(i)) { continue; @@ -96,10 +87,8 @@ Result getDefault(int id, const std::filesystem::path& defaultPath, std::filesystem::path path = i["path"].asString().unwrap(); if (path == defaultPath) { return Ok(LocalSong( - SongMetadata(id, jukebox::random_string(16), - i["songName"].asString().unwrap(), - i["authorName"].asString().unwrap(), std::nullopt, - i["startOffset"].asInt().unwrapOr(0)), + SongMetadata(id, jukebox::random_string(16), i["songName"].asString().unwrap(), + i["authorName"].asString().unwrap(), std::nullopt, i["startOffset"].asInt().unwrapOr(0)), path)); } } @@ -107,8 +96,7 @@ Result getDefault(int id, const std::filesystem::path& defaultPath, return Err("Default song not found"); } -Result getActive(int id, const std::filesystem::path& activePath, - const matjson::Value& songs) { +Result getActive(int id, const std::filesystem::path& activePath, const matjson::Value& songs) { for (const matjson::Value& i : songs) { if (!isSongValid(i)) { continue; @@ -117,10 +105,8 @@ Result getActive(int id, const std::filesystem::path& activePath, std::filesystem::path path = i["path"].asString().unwrap(); if (path == activePath) { return Ok(LocalSong( - SongMetadata(id, jukebox::random_string(16), - i["songName"].asString().unwrap(), - i["authorName"].asString().unwrap(), std::nullopt, - i["startOffset"].asInt().unwrapOr(0)), + SongMetadata(id, jukebox::random_string(16), i["songName"].asString().unwrap(), + i["authorName"].asString().unwrap(), std::nullopt, i["startOffset"].asInt().unwrapOr(0)), path)); } } @@ -135,18 +121,9 @@ Result> parseManifest() { std::filesystem::path path = manifestPath(); - std::ifstream input(path); - if (!input.is_open()) { - return Err( - fmt::format("Couldn't open file: {}", path.filename().string())); - } - - GEODE_UNWRAP_INTO(matjson::Value json, - matjson::parse(input) - .mapErr([](matjson::ParseError err) { - return fmt::format( - "Couldn't parse JSON from file: {}", err); - })); + GEODE_UNWRAP_INTO(matjson::Value json, geode::utils::file::readJson(path).mapErr([](std::string err) { + return fmt::format("Couldn't parse JSON from file: {}", err); + })); if (!json.contains("version") || !json["version"].isNumber()) { return Err("Invalid JSON"); @@ -166,15 +143,13 @@ Result> parseManifest() { for (const auto& [key, data] : json["nongs"]) { GEODE_UNWRAP_INTO(int id, geode::utils::numFromString(key)); - if (!data.contains("defaultPath") || !data["defaultPath"].isString() || - !data.contains("active") || !data["active"].isString() || - !data.contains("songs") || !data["songs"].isArray()) { + if (!data.contains("defaultPath") || !data["defaultPath"].isString() || !data.contains("active") || + !data["active"].isString() || !data.contains("songs") || !data["songs"].isArray()) { log::warn("Skipping id {}, invalid data", id); continue; } - std::filesystem::path defaultPath = - data["defaultPath"].asString().unwrap(); + std::filesystem::path defaultPath = data["defaultPath"].asString().unwrap(); std::filesystem::path activePath = data["active"].asString().unwrap(); matjson::Value songs = data["songs"].asArray().unwrap(); @@ -212,16 +187,14 @@ Result> parseManifest() { } manifestSongs.push_back(LocalSong( - SongMetadata(id, unique, i["songName"].asString().unwrap(), - i["authorName"].asString().unwrap(), std::nullopt, - i["startOffset"].asInt().unwrapOr(0)), + SongMetadata(id, unique, i["songName"].asString().unwrap(), i["authorName"].asString().unwrap(), + std::nullopt, i["startOffset"].asInt().unwrapOr(0)), path)); } - ret.insert({id, CompatManifest{.id = id, - .defaultSong = defaultSong, - .active = activeSong, - .songs = std::move(manifestSongs)}}); + ret.insert( + {id, CompatManifest{ + .id = id, .defaultSong = defaultSong, .active = activeSong, .songs = std::move(manifestSongs)}}); } return Ok(ret); diff --git a/jukebox/jukebox/download/youtube.cpp b/jukebox/jukebox/download/youtube.cpp index 34f970d..2e4c9e6 100644 --- a/jukebox/jukebox/download/youtube.cpp +++ b/jukebox/jukebox/download/youtube.cpp @@ -31,8 +31,7 @@ Future> startYoutubeDownload(const std::string& id) { Result getUrlFromMetadataPayload(web::WebResponse r) { if (!r.ok()) { - return Err(fmt::format( - "cobalt metadata query failed with status code {}", r.code())); + return Err("cobalt metadata query failed with status code {}", r.code()); } Result jsonRes = r.json(); diff --git a/jukebox/jukebox/hooks/gj_game_level.cpp b/jukebox/jukebox/hooks/gj_game_level.cpp index 7986fdf..307be85 100644 --- a/jukebox/jukebox/hooks/gj_game_level.cpp +++ b/jukebox/jukebox/hooks/gj_game_level.cpp @@ -28,10 +28,7 @@ class $modify(GJGameLevel) { return GJGameLevel::getAudioFileName(); } jukebox::NongManager::get().m_currentlyPreparingNong = res.value(); -#ifdef GEODE_IS_WINDOWS - return geode::utils::string::wideToUtf8(active->path().value().c_str()); -#else - return active->path().value().string(); -#endif + + return geode::utils::string::pathToString(active->path().value()); } }; diff --git a/jukebox/jukebox/hooks/music_download_manager.cpp b/jukebox/jukebox/hooks/music_download_manager.cpp index 635bb38..b481c90 100644 --- a/jukebox/jukebox/hooks/music_download_manager.cpp +++ b/jukebox/jukebox/hooks/music_download_manager.cpp @@ -27,11 +27,8 @@ gd::string JBMusicDownloadManager::pathForSong(int id) { return MusicDownloadManager::pathForSong(id); } NongManager::get().m_currentlyPreparingNong = value; -#ifdef GEODE_IS_WINDOWS - return geode::utils::string::wideToUtf8(active->path().value().c_str()); -#else - return active->path().value().string(); -#endif + + return geode::utils::string::pathToString(active->path().value()); } void JBMusicDownloadManager::onGetSongInfoCompleted(gd::string p1, gd::string p2) { diff --git a/jukebox/jukebox/main.cpp b/jukebox/jukebox/main.cpp index 580f78b..6d5b867 100644 --- a/jukebox/jukebox/main.cpp +++ b/jukebox/jukebox/main.cpp @@ -9,12 +9,9 @@ using namespace geode::prelude; -$execute { - (void)Mod::get()->registerCustomSettingType("indexes", - &jukebox::IndexSetting::parse); -} +$execute { (void)Mod::get()->registerCustomSettingType("indexes", &jukebox::IndexSetting::parse); } $on_mod(Loaded) { - jukebox::NongManager::get().init(); jukebox::IndexManager::get().init(); + jukebox::NongManager::get().init(); }; diff --git a/jukebox/jukebox/managers/index_manager.cpp b/jukebox/jukebox/managers/index_manager.cpp index f3e2654..e0abcfe 100644 --- a/jukebox/jukebox/managers/index_manager.cpp +++ b/jukebox/jukebox/managers/index_manager.cpp @@ -1,7 +1,7 @@ #include #include -#include + #include #include #include @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -49,7 +50,6 @@ bool IndexManager::init() { if (const std::filesystem::path path = this->baseIndexesPath(); !std::filesystem::exists(path)) { std::filesystem::create_directory(path); - return true; } async::spawn(this->fetchIndexes(), [](Result<> result) { @@ -99,12 +99,7 @@ Result<> IndexManager::loadIndex(std::filesystem::path path) { return Err("Index file does not exist"); } - std::ifstream input(path); - if (!input.is_open()) { - return Err(fmt::format("Couldn't open file: {}", path.filename().string())); - } - - GEODE_UNWRAP_INTO(matjson::Value jsonObj, matjson::parse(input)); + GEODE_UNWRAP_INTO(matjson::Value jsonObj, geode::utils::file::readJson(path)); return this->loadIndex(std::move(jsonObj)); } @@ -230,9 +225,7 @@ Future> IndexManager::fetchIndexes() { } Future> IndexManager::fetchIndex(const IndexSource& index) { - const web::WebResponse response = co_await web::WebRequest() - .timeout(std::chrono::seconds(30)) - .get(index.m_url); + const web::WebResponse response = co_await web::WebRequest().timeout(std::chrono::seconds(30)).get(index.m_url); if (!response.ok()) { co_return Err(utils::web::getErrorFromResponse(response)); diff --git a/jukebox/jukebox/managers/nong_manager.cpp b/jukebox/jukebox/managers/nong_manager.cpp index 1145d8d..891cc99 100644 --- a/jukebox/jukebox/managers/nong_manager.cpp +++ b/jukebox/jukebox/managers/nong_manager.cpp @@ -9,14 +9,14 @@ #include #include -#include #include #include #include #include #include #include -#include +#include +#include #include #include @@ -190,7 +190,7 @@ bool NongManager::init() { auto res = this->loadNongsFromPath(entry.path()); if (res.isErr()) { log::error("Failed to read file {}: {}", entry.path().filename(), res.unwrapErr()); - std::filesystem::rename(entry.path(), path / fmt::format("{}.bak", entry.path().filename().string())); + std::filesystem::rename(entry.path(), path / fmt::format("{}.bak", entry.path().filename())); continue; } @@ -289,19 +289,14 @@ Result<> NongManager::saveNongs(std::optional saveID) { } Result> NongManager::loadNongsFromPath(const std::filesystem::path& path) { - auto stem = path.stem().string(); + auto stem = string::pathToString(path.stem()); GEODE_UNWRAP_INTO(int id, geode::utils::numFromString(stem)); if (id == 0) { - return Err(fmt::format("Invalid filename {}", path.filename().string())); - } - - std::ifstream input(path); - if (!input.is_open()) { - return Err(fmt::format("Couldn't open file: {}", path.filename().string())); + return Err("Invalid filename {}", path.filename()); } - GEODE_UNWRAP_INTO(matjson::Value json, matjson::parse(input).mapErr([id](std::string err) { + GEODE_UNWRAP_INTO(matjson::Value json, geode::utils::file::readJson(path).mapErr([id](std::string err) { return fmt::format("Couldn't parse JSON from file: {}", err); })); @@ -345,9 +340,8 @@ arc::Future NongManager::getMultiAssetSizes(std::string songs, std: } auto nongs = result.value(); if (nongs->isDefaultActive()) { - auto songObj = co_await async::waitForMainThread([id]() { - return MusicDownloadManager::sharedState()->getSongInfoObject(id); - }); + auto songObj = co_await async::waitForMainThread( + [id]() { return MusicDownloadManager::sharedState()->getSongInfoObject(id); }); if (songObj && songObj.value() && songObj.value()->m_fileSize > 0.f) { double filesizeMB = songObj.value()->m_fileSize; sum += static_cast(filesizeMB * 1000000.f); @@ -356,7 +350,7 @@ arc::Future NongManager::getMultiAssetSizes(std::string songs, std: } auto path = nongs->active()->path().value(); - if (path.string().starts_with("songs/")) { + if (string::pathToString(path).starts_with("songs/")) { path = resourcesDir / path; } if (std::filesystem::exists(path, _ec)) { diff --git a/jukebox/jukebox/nong/nong.cpp b/jukebox/jukebox/nong/nong.cpp index 228f353..8c3a494 100644 --- a/jukebox/jukebox/nong/nong.cpp +++ b/jukebox/jukebox/nong/nong.cpp @@ -288,7 +288,7 @@ class Nongs::Impl { std::ofstream output(path); if (!output.is_open()) { - return Err(fmt::format("Couldn't open file: {}", path)); + return Err("Couldn't open file: {}", path); } output << json.dump(matjson::NO_INDENTATION); @@ -554,7 +554,7 @@ class Nongs::Impl { const bool isActive = m_active->metadata()->uniqueID == id; const std::optional opt = this->findSong(id); if (!opt) { - return Err(fmt::format("NONG ID {} not found", id)); + return Err("NONG ID {} not found", id); } const Song* prevNong = opt.value(); const std::optional prevPath = prevNong->path(); @@ -578,7 +578,7 @@ class Nongs::Impl { const bool isActive = m_active->metadata()->uniqueID == id; const std::optional opt = this->findSong(id); if (!opt) { - return Err(fmt::format("NONG ID {} not found", id)); + return Err("NONG ID {} not found", id); } const Song* prevNong = opt.value(); const std::optional prevPath = prevNong->path(); @@ -604,7 +604,7 @@ class Nongs::Impl { const bool isActive = m_active->metadata()->uniqueID == id; const std::optional opt = this->findSong(id); if (!opt) { - return Err(fmt::format("NONG ID {} not found", id)); + return Err("NONG ID {} not found", id); } const Song* prevNong = opt.value(); const std::optional prevPath = prevNong->path(); diff --git a/jukebox/jukebox/utils/random_string.cpp b/jukebox/jukebox/utils/random_string.cpp index 0aa1ab5..32994d6 100644 --- a/jukebox/jukebox/utils/random_string.cpp +++ b/jukebox/jukebox/utils/random_string.cpp @@ -1,24 +1,13 @@ #include - -#include -#include -#include -#include +#include namespace jukebox { std::string random_string(size_t length) { - static std::string_view const charset = + static constexpr std::string_view charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - const size_t max_index = (charset.size() - 1); - static std::random_device rd; - static std::mt19937_64 gen(rd()); - std::uniform_int_distribution<> dis(0, max_index); - std::string str(length, 0); - for (size_t i = 0; i < length; i++) { - str[i] = charset[dis(gen)]; - } - return str; + + return geode::utils::random::generateString(length, charset); } -} // namespace jukebox +} // namespace jukebox \ No newline at end of file diff --git a/jukebox/jukebox/utils/random_string.hpp b/jukebox/jukebox/utils/random_string.hpp index 88d9071..54db22d 100644 --- a/jukebox/jukebox/utils/random_string.hpp +++ b/jukebox/jukebox/utils/random_string.hpp @@ -6,8 +6,7 @@ namespace jukebox { /** - * Gracefully stolen from - * https://stackoverflow.com/questions/440133/how-do-i-create-a-random-alpha-numeric-string-in-c + * Generates a random string, was formerly gracefully stolen from StackOverflow */ std::string random_string(size_t length);