Skip to content

Commit dc5c22c

Browse files
committed
port to Geode v5
1 parent d580c03 commit dc5c22c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1600
-2346
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
BasedOnStyle: Google
22
AccessModifierOffset: -4
3+
ColumnLimit: 120
34
IndentWidth: 4
45
IncludeBlocks: Preserve
56
InsertBraces: true

CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.21.0)
2-
set(CMAKE_CXX_STANDARD 20)
2+
set(CMAKE_CXX_STANDARD 23)
33
set(CMAKE_CXX_STANDARD_REQUIRED ON)
44
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "iOS" OR IOS)
55
set(CMAKE_OSX_ARCHITECTURES "arm64")
@@ -10,6 +10,8 @@ set(CMAKE_CXX_VISIBILITY_PRESET hidden)
1010

1111
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
1212

13+
set(GEODE_LINK_NIGHTLY 1)
14+
1315
project(jukebox VERSION 3.4.0)
1416

1517
file(GLOB SOURCES
@@ -25,9 +27,11 @@ file(GLOB SOURCES
2527
jukebox/jukebox/*.cpp
2628
)
2729

28-
add_library(${PROJECT_NAME} SHARED ${SOURCES})
30+
add_library(${PROJECT_NAME} SHARED ${SOURCES}
31+
jukebox/jukebox/events/file_download_progress.hpp)
2932
target_include_directories(${PROJECT_NAME} PUBLIC jukebox)
3033

34+
3135
set_property(TARGET ${PROJECT_NAME} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
3236

3337
if (NOT DEFINED ENV{GEODE_SDK})
@@ -39,4 +43,4 @@ endif()
3943
add_subdirectory($ENV{GEODE_SDK} $ENV{GEODE_SDK}/build)
4044

4145
target_link_libraries(${PROJECT_NAME} geode-sdk)
42-
create_geode_file(${PROJECT_NAME})
46+
setup_geode_mod(${PROJECT_NAME})

jukebox/jukebox/download/download.hpp

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
11
#include <jukebox/download/hosted.hpp>
22

3+
#include <string_view>
4+
35
#include <fmt/core.h>
4-
#include <Geode/loader/Mod.hpp>
56
#include <Geode/Result.hpp>
7+
#include <Geode/loader/Mod.hpp>
68
#include <Geode/utils/web.hpp>
79

8-
#include <jukebox/download/download.hpp>
10+
#include <jukebox/events/file_download_progress.hpp>
911

1012
using namespace geode::prelude;
1113

12-
namespace jukebox {
13-
14-
namespace download {
14+
namespace jukebox::download {
1515

16-
DownloadTask startHostedDownload(const std::string& url) {
16+
arc::Future<Result<ByteVector>> startHostedDownload(const std::string_view url) {
1717
int timeout = Mod::get()->getSettingValue<int>("download-timeout");
1818

1919
if (timeout < 30) {
2020
timeout = 30;
2121
}
2222

23-
return web::WebRequest()
24-
.timeout(std::chrono::seconds(timeout))
25-
.get(url)
26-
.map(
27-
[](web::WebResponse* response) -> DownloadTask::Value {
28-
if (!response->ok()) {
29-
if (response->code() == 502) {
30-
return Err(
31-
"Web request failed. Service is currently "
32-
"unavailable or under maintenance. Please try "
33-
"again later.");
34-
} else {
35-
return Err(fmt::format("Web request failed. Status {}",
36-
response->code()));
37-
}
38-
}
39-
return Ok(std::move(response->data()));
40-
},
41-
[](web::WebProgress* progress) {
42-
return progress->downloadProgress().value_or(0);
43-
});
44-
}
23+
const web::WebResponse response =
24+
co_await web::WebRequest()
25+
.timeout(std::chrono::seconds(timeout))
26+
.onProgress([url = std::string(url)](const web::WebProgress& progress) {
27+
const float actual = progress.downloadProgress().value_or(0.0f);
28+
29+
events::FileDownloadProgress(url).send(events::FileDownloadProgressData{url, actual});
30+
})
31+
.get(std::string(url));
32+
33+
if (!response.ok()) {
34+
if (response.code() == 502) {
35+
co_return Err(
36+
"Web request failed. Service is currently "
37+
"unavailable or under maintenance. Please try "
38+
"again later.");
39+
}
40+
41+
co_return Err(fmt::format("Web request failed. Status {}", response.code()));
42+
}
4543

46-
} // namespace download
44+
co_return Ok(std::move(response).data());
45+
}
4746

48-
} // namespace jukebox
47+
} // namespace jukebox::download
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
#pragma once
22

3-
#include <string>
3+
#include <string_view>
44

5-
#include <jukebox/download/download.hpp>
5+
#include <Geode/Result.hpp>
6+
#include <Geode/utils/general.hpp>
7+
#include <arc/future/Future.hpp>
68

7-
namespace jukebox {
9+
namespace jukebox::download {
810

9-
namespace download {
11+
arc::Future<geode::Result<geode::ByteVector>> startHostedDownload(std::string_view url);
1012

11-
DownloadTask startHostedDownload(const std::string& url);
12-
13-
}
14-
15-
} // namespace jukebox
13+
} // namespace jukebox::download

jukebox/jukebox/download/youtube.cpp

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,35 @@
77
#include <Geode/Result.hpp>
88
#include <Geode/utils/web.hpp>
99

10-
#include <jukebox/download/download.hpp>
1110
#include <jukebox/download/hosted.hpp>
1211

1312
using namespace geode::prelude;
13+
using namespace arc;
1414

15-
web::WebTask getMetadata(const std::string& id);
16-
Result<std::string> getUrlFromMetadataPayload(web::WebResponse* resp);
17-
jukebox::download::DownloadTask onMetadata(web::WebResponse*);
15+
web::WebFuture getMetadata(const std::string& id);
16+
Result<std::string> getUrlFromMetadataPayload(web::WebResponse r);
17+
Future<Result<ByteVector>> onMetadata(web::WebResponse result);
1818

19-
namespace jukebox {
19+
namespace jukebox::download {
2020

21-
namespace download {
22-
23-
DownloadTask startYoutubeDownload(const std::string& id) {
21+
Future<Result<ByteVector>> startYoutubeDownload(const std::string& id) {
2422
if (id.length() != 11) {
25-
return DownloadTask::immediate(Err("Invalid YouTube ID"));
23+
co_return Err("Invalid YouTube ID");
2624
}
2725

28-
return getMetadata(id).chain(
29-
[](web::WebResponse* r) { return onMetadata(r); });
26+
auto metadata = co_await getMetadata(id);
27+
co_return co_await onMetadata(std::move(metadata));
3028
}
3129

32-
} // namespace download
33-
34-
} // namespace jukebox
30+
} // namespace jukebox::download
3531

36-
Result<std::string> getUrlFromMetadataPayload(web::WebResponse* r) {
37-
if (!r->ok()) {
32+
Result<std::string> getUrlFromMetadataPayload(web::WebResponse r) {
33+
if (!r.ok()) {
3834
return Err(fmt::format(
39-
"cobalt metadata query failed with status code {}", r->code()));
35+
"cobalt metadata query failed with status code {}", r.code()));
4036
}
4137

42-
Result<matjson::Value> jsonRes = r->json();
38+
Result<matjson::Value> jsonRes = r.json();
4339
if (jsonRes.isErr()) {
4440
return Err("cobalt metadata query returned invalid JSON");
4541
}
@@ -57,15 +53,15 @@ Result<std::string> getUrlFromMetadataPayload(web::WebResponse* r) {
5753
return Ok(payload["url"].asString().unwrap());
5854
}
5955

60-
web::WebTask getMetadata(const std::string& id) {
56+
web::WebFuture getMetadata(const std::string& id) {
6157
int timeout = Mod::get()->getSettingValue<int>("download-timeout");
6258

6359
if (timeout < 30) {
6460
timeout = 30;
6561
}
6662

6763
return web::WebRequest()
68-
.timeout(std::chrono::seconds(30))
64+
.timeout(std::chrono::seconds(timeout))
6965
.bodyJSON(matjson::makeObject(
7066
{{"url", fmt::format("https://www.youtube.com/watch?v={}", id)},
7167
{"aFormat", "mp3"},
@@ -75,11 +71,11 @@ web::WebTask getMetadata(const std::string& id) {
7571
.post("https://api.cobalt.tools/api/json");
7672
}
7773

78-
jukebox::download::DownloadTask onMetadata(web::WebResponse* result) {
79-
Result<std::string> res = getUrlFromMetadataPayload(result);
74+
Future<Result<ByteVector>> onMetadata(web::WebResponse result) {
75+
Result<std::string> res = getUrlFromMetadataPayload(std::move(result));
8076
if (res.isErr()) {
81-
return jukebox::download::DownloadTask::immediate(Err(res.unwrapErr()));
77+
co_return Err(res.unwrapErr());
8278
}
8379

84-
return jukebox::download::startHostedDownload(res.unwrap());
80+
co_return co_await jukebox::download::startHostedDownload(res.unwrap());
8581
}

jukebox/jukebox/download/youtube.hpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
#include <string>
44

5-
#include <jukebox/download/download.hpp>
5+
#include <Geode/utils/general.hpp>
6+
#include <arc/future/Future.hpp>
67

7-
namespace jukebox {
8+
namespace jukebox::download {
89

9-
namespace download {
10+
arc::Future<geode::Result<geode::ByteVector>> startYoutubeDownload(const std::string& id);
1011

11-
DownloadTask startYoutubeDownload(const std::string& id);
12-
13-
}
14-
15-
}
12+
} // namespace jukebox::download
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <string_view>
5+
6+
#include <Geode/loader/Event.hpp>
7+
8+
namespace jukebox::events {
9+
10+
struct FileDownloadProgressData final {
11+
private:
12+
float m_progress;
13+
std::string m_url;
14+
15+
public:
16+
FileDownloadProgressData(std::string url, const float progress) noexcept
17+
: m_progress{progress}, m_url(std::move(url)) {}
18+
19+
[[nodiscard]] float progress() const noexcept { return m_progress; }
20+
[[nodiscard]] std::string_view url() const noexcept { return m_url; }
21+
};
22+
23+
struct FileDownloadProgress
24+
: geode::GlobalEvent<FileDownloadProgress, bool(const FileDownloadProgressData&), std::string> {
25+
using GlobalEvent::GlobalEvent;
26+
};
27+
28+
} // namespace jukebox::events

jukebox/jukebox/events/get_song_info.cpp

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
#pragma once
22

33
#include <string>
4+
#include <string_view>
45

56
#include <Geode/loader/Event.hpp>
67

7-
#include <jukebox/hooks/music_download_manager.hpp>
8+
namespace jukebox::event {
89

9-
namespace jukebox {
10-
11-
namespace event {
12-
13-
class GetSongInfo : public geode::Event {
10+
struct GetSongInfoData final {
1411
private:
12+
int m_gdId;
1513
std::string m_songName;
1614
std::string m_artistName;
17-
int m_gdSongID;
18-
19-
protected:
20-
friend class ::JBMusicDownloadManager;
21-
22-
GetSongInfo(std::string songName, std::string artistName, int gdSongID);
2315

2416
public:
25-
std::string songName();
26-
std::string artistName();
27-
int gdSongID();
17+
GetSongInfoData(const int gdId, std::string songName, std::string artistName) noexcept
18+
: m_gdId(gdId), m_songName(std::move(songName)), m_artistName(std::move(artistName)) {}
19+
20+
[[nodiscard]] int gdId() const noexcept { return m_gdId; }
21+
[[nodiscard]] std::string_view songName() const noexcept { return m_songName; }
22+
[[nodiscard]] std::string_view artistName() const noexcept { return m_artistName; }
2823
};
2924

30-
} // namespace event
25+
struct GetSongInfo final : geode::GlobalEvent<GetSongInfo, bool(const GetSongInfoData&), int> {
26+
using GlobalEvent::GlobalEvent;
27+
};
3128

32-
} // namespace jukebox
29+
} // namespace jukebox::event

0 commit comments

Comments
 (0)