Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 29 additions & 56 deletions jukebox/jukebox/compat/v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
#include <string_view>
#include <unordered_map>

#include <matjson.hpp>
#include <Geode/Result.hpp>
#include <Geode/loader/Log.hpp>
#include <Geode/loader/Mod.hpp>
#include <Geode/utils/file.hpp>
#include <Geode/utils/general.hpp>
#include <matjson.hpp>

#include <jukebox/compat/compat.hpp>
#include <jukebox/nong/nong.hpp>
Expand All @@ -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)) {
Expand Down Expand Up @@ -73,21 +70,15 @@ Result<LocalSong> 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<LocalSong> getDefault(int id, const std::filesystem::path& defaultPath,
const matjson::Value& songs) {
Result<LocalSong> getDefault(int id, const std::filesystem::path& defaultPath, const matjson::Value& songs) {
for (const matjson::Value& i : songs) {
if (!isSongValid(i)) {
continue;
Expand All @@ -96,19 +87,16 @@ Result<LocalSong> 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));
}
}

return Err("Default song not found");
}

Result<LocalSong> getActive(int id, const std::filesystem::path& activePath,
const matjson::Value& songs) {
Result<LocalSong> getActive(int id, const std::filesystem::path& activePath, const matjson::Value& songs) {
for (const matjson::Value& i : songs) {
if (!isSongValid(i)) {
continue;
Expand All @@ -117,10 +105,8 @@ Result<LocalSong> 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));
}
}
Expand All @@ -135,18 +121,9 @@ Result<std::unordered_map<int, CompatManifest>> 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");
Expand All @@ -166,15 +143,13 @@ Result<std::unordered_map<int, CompatManifest>> parseManifest() {
for (const auto& [key, data] : json["nongs"]) {
GEODE_UNWRAP_INTO(int id, geode::utils::numFromString<int>(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();

Expand Down Expand Up @@ -212,16 +187,14 @@ Result<std::unordered_map<int, CompatManifest>> 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);
Expand Down
3 changes: 1 addition & 2 deletions jukebox/jukebox/download/youtube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ Future<Result<ByteVector>> startYoutubeDownload(const std::string& id) {

Result<std::string> 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<matjson::Value> jsonRes = r.json();
Expand Down
7 changes: 2 additions & 5 deletions jukebox/jukebox/hooks/gj_game_level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
};
7 changes: 2 additions & 5 deletions jukebox/jukebox/hooks/music_download_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 2 additions & 5 deletions jukebox/jukebox/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
15 changes: 4 additions & 11 deletions jukebox/jukebox/managers/index_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <jukebox/managers/index_manager.hpp>

#include <filesystem>
#include <fstream>

#include <functional>
#include <ios>
#include <memory>
Expand All @@ -17,6 +17,7 @@
#include <Geode/loader/Event.hpp>
#include <Geode/loader/Log.hpp>
#include <Geode/loader/Mod.hpp>
#include <Geode/utils/file.hpp>
#include <Geode/utils/general.hpp>
#include <Geode/utils/web.hpp>
#include <arc/future/Future.hpp>
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -230,9 +225,7 @@ Future<Result<>> IndexManager::fetchIndexes() {
}

Future<Result<matjson::Value>> 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));
Expand Down
24 changes: 9 additions & 15 deletions jukebox/jukebox/managers/nong_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#include <unordered_map>
#include <utility>

#include <asp/iter.hpp>
#include <fmt/format.h>
#include <Geode/Result.hpp>
#include <Geode/binding/LevelTools.hpp>
#include <Geode/binding/MusicDownloadManager.hpp>
#include <Geode/binding/SongInfoObject.hpp>
#include <Geode/loader/Log.hpp>
#include <Geode/utils/general.hpp>
#include <Geode/utils/file.hpp>
#include <asp/iter.hpp>
#include <matjson.hpp>

#include <jukebox/compat/compat.hpp>
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -289,19 +289,14 @@ Result<> NongManager::saveNongs(std::optional<int> saveID) {
}

Result<std::unique_ptr<Nongs>> 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<int>(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);
}));

Expand Down Expand Up @@ -345,9 +340,8 @@ arc::Future<std::string> NongManager::getMultiAssetSizes(std::string songs, std:
}
auto nongs = result.value();
if (nongs->isDefaultActive()) {
auto songObj = co_await async::waitForMainThread<SongInfoObject*>([id]() {
return MusicDownloadManager::sharedState()->getSongInfoObject(id);
});
auto songObj = co_await async::waitForMainThread<SongInfoObject*>(
[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<uintmax_t>(filesizeMB * 1000000.f);
Expand All @@ -356,7 +350,7 @@ arc::Future<std::string> 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)) {
Expand Down
8 changes: 4 additions & 4 deletions jukebox/jukebox/nong/nong.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -554,7 +554,7 @@ class Nongs::Impl {
const bool isActive = m_active->metadata()->uniqueID == id;
const std::optional<Song*> 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<std::filesystem::path> prevPath = prevNong->path();
Expand All @@ -578,7 +578,7 @@ class Nongs::Impl {
const bool isActive = m_active->metadata()->uniqueID == id;
const std::optional<Song*> 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<std::filesystem::path> prevPath = prevNong->path();
Expand All @@ -604,7 +604,7 @@ class Nongs::Impl {
const bool isActive = m_active->metadata()->uniqueID == id;
const std::optional<Song*> 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<std::filesystem::path> prevPath = prevNong->path();
Expand Down
Loading
Loading