Skip to content

Commit

Permalink
real
Browse files Browse the repository at this point in the history
  • Loading branch information
whoeevee committed Aug 9, 2024
1 parent 6bc5801 commit 5ba2b4a
Show file tree
Hide file tree
Showing 26 changed files with 323 additions and 404 deletions.
75 changes: 38 additions & 37 deletions Sources/EeveeSpotify/DataLoaderServiceHooks.x.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Foundation
import Orion

class SPTDataLoaderServiceHook: ClassHook<NSObject> {

class SPTDataLoaderServiceHook: ClassHook<NSObject>, SpotifySessionDelegate {
static let targetName = "SPTDataLoaderService"

// orion:new
Expand All @@ -23,48 +22,46 @@ class SPTDataLoaderServiceHook: ClassHook<NSObject> {
return
}

if error == nil && shouldModify(url) {

if let buffer = URLSessionHelper.shared.obtainData(for: url) {

if url.isLyrics {

do {
orig.URLSession(
session,
dataTask: task,
didReceiveData: try getLyricsForCurrentTrack(
originalLyrics: try? Lyrics(serializedData: buffer)
)
)

orig.URLSession(session, task: task, didCompleteWithError: nil)
}
catch {
orig.URLSession(session, task: task, didCompleteWithError: error)
}

return
}

if error == nil,
shouldModify(url),
let buffer = URLSessionHelper.shared.obtainData(for: url)
{
if url.isLyrics {
do {
var customizeMessage = try CustomizeMessage(serializedData: buffer)
modifyRemoteConfiguration(&customizeMessage.response)

orig.URLSession(
session,
dataTask: task,
didReceiveData: try customizeMessage.serializedData()
didReceiveData: try getLyricsForCurrentTrack(
originalLyrics: try? Lyrics(serializedData: buffer)
)
)

orig.URLSession(session, task: task, didCompleteWithError: nil)

NSLog("[EeveeSpotify] Modified customize data")
return
}
catch {
NSLog("[EeveeSpotify] Unable to modify customize data: \(error)")
orig.URLSession(session, task: task, didCompleteWithError: error)
}

return
}

do {
var customizeMessage = try CustomizeMessage(serializedData: buffer)
modifyRemoteConfiguration(&customizeMessage.response)

orig.URLSession(
session,
dataTask: task,
didReceiveData: try customizeMessage.serializedData()
)

orig.URLSession(session, task: task, didCompleteWithError: nil)

NSLog("[EeveeSpotify] Modified customize data")
return
}
catch {
NSLog("[EeveeSpotify] Unable to modify customize data: \(error)")
}
}

Expand All @@ -76,12 +73,16 @@ class SPTDataLoaderServiceHook: ClassHook<NSObject> {
_ session: URLSession,
dataTask task: URLSessionDataTask,
didReceiveResponse response: HTTPURLResponse,
completionHandler handler: Any
completionHandler handler: @escaping (URLSession.ResponseDisposition) -> Void
) {
let url = response.url!
guard
let request = task.currentRequest,
let url = request.url
else {
return
}

if url.isLyrics, response.statusCode != 200 {

let okResponse = HTTPURLResponse(
url: url,
statusCode: 200,
Expand Down
10 changes: 4 additions & 6 deletions Sources/EeveeSpotify/Lyrics/CustomLyrics.x.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class LyricsOnlyViewControllerHook: ClassHook<UIViewController> {
text.append(
Dynamic.SPTEncoreAttributedString.alloc(interface: SPTEncoreAttributedString.self)
.initWithString(
"\nFallback: \(description)",
"\n\("fallback_attribute".localized): \(description)",
typeStyle: typeStyle,
attributes: attributes
)
Expand All @@ -124,7 +124,7 @@ class LyricsOnlyViewControllerHook: ClassHook<UIViewController> {
text.append(
Dynamic.SPTEncoreAttributedString.alloc(interface: SPTEncoreAttributedString.self)
.initWithString(
"\nRomanized",
"\n\("romanized_attribute".localized)",
typeStyle: typeStyle,
attributes: attributes
)
Expand Down Expand Up @@ -182,10 +182,9 @@ private func loadLyricsForCurrentTrack() throws {
case .InvalidMusixmatchToken:

if !hasShownUnauthorizedPopUp {

PopUpHelper.showPopUp(
delayed: false,
message: "The tweak is unable to load lyrics from Musixmatch due to Unauthorized error. Please check or update your Musixmatch token. If you use an iPad, you should get the token from the Musixmatch app for iPad.",
message: "musixmatch_unauthorized_popup".localized,
buttonText: "OK"
)

Expand All @@ -195,10 +194,9 @@ private func loadLyricsForCurrentTrack() throws {
case .MusixmatchRestricted:

if !hasShownRestrictedPopUp {

PopUpHelper.showPopUp(
delayed: false,
message: "The tweak is unable to load lyrics from Musixmatch because they are restricted. It's likely a copyright issue due to the US IP address, so you should change it if you're in the US or use a VPN.",
message: "musixmatch_restricted_popup".localized,
buttonText: "OK"
)

Expand Down
4 changes: 2 additions & 2 deletions Sources/EeveeSpotify/Lyrics/Models/LyricsDto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ struct LyricsDto {
if lines.isEmpty {
lyricsData.lines = [
LyricsLine.with {
$0.content = "This song is instrumental."
$0.content = "song_is_instrumental".localized
},
LyricsLine.with {
$0.content = "Let the music play..."
$0.content = "let_the_music_play".localized
},
LyricsLine.with {
$0.content = ""
Expand Down
12 changes: 6 additions & 6 deletions Sources/EeveeSpotify/Lyrics/Models/LyricsError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ enum LyricsError: Error, CustomStringConvertible {

var description: String {
switch self {
case .NoSuchSong: "No Song Found"
case .MusixmatchRestricted: "Restricted"
case .InvalidMusixmatchToken: "Unauthorized"
case .DecodingError: "Decoding Error"
case .NoCurrentTrack: "No Track Instance"
case .UnknownError: "Unknown Error"
case .NoSuchSong: "no_such_song".localized
case .MusixmatchRestricted: "musixmatch_restricted".localized
case .InvalidMusixmatchToken: "invalid_musixmatch_token".localized
case .DecodingError: "decoding_error".localized
case .NoCurrentTrack: "no_current_track".localized
case .UnknownError: "unknown_error".localized
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ class MusixmatchLyricsRepository: LyricsRepository {
let subtitleTranslatedBody = subtitleTranslated["subtitle_body"] as? String,
let subtitlesTranslated = try? JSONDecoder().decode(
[MusixmatchSubtitle].self, from: subtitleTranslatedBody.data(using: .utf8)!
) {
)
{
if selectedLanguage == romanizationLanguage {
romanized = true

Expand All @@ -206,12 +206,7 @@ class MusixmatchLyricsRepository: LyricsRepository {
}
}

if options.musixmatchLanguage.isEmpty
&& options.romanization
&& selectedLanguage != romanizationLanguage {

selectedLanguage = romanizationLanguage

if options.romanization && selectedLanguage != romanizationLanguage {
if let translations = try? getTranslations(
query.spotifyTrackId,
selectedLanguage: romanizationLanguage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ import Foundation
import NaturalLanguage

extension String {

static func ~= (lhs: String, rhs: String) -> Bool {
lhs.firstMatch(rhs) != nil
}

var localized: String {
BundleHelper.shared.localizedString(self)
}

func localizeWithFormat(_ arguments: CVarArg...) -> String{
String(format: self.localized, arguments: arguments)
}

var range: NSRange {
NSRange(self.startIndex..., in: self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extension UserDefaults {
static var patchType: PatchType {
get {
if let rawValue = defaults.object(forKey: patchTypeKey) as? Int {
return PatchType(rawValue: rawValue)!
return PatchType(rawValue: rawValue) ?? .requests
}

return .notSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,46 @@ import Orion
private func showHavePremiumPopUp() {
PopUpHelper.showPopUp(
delayed: true,
message: "It looks like you have an active Premium subscription, so the tweak won't patch the data or restrict the use of Premium server-sided features. You can manage this in the EeveeSpotify settings.",
buttonText: "OK"
message: "have_premium_popup".localized,
buttonText: "ok".localized
)
}

class SPTCoreURLSessionDataDelegateHook: ClassHook<NSObject> {
class SpotifySessionDelegateBootstrapHook: ClassHook<NSObject>, SpotifySessionDelegate {
static var targetName: String {
EeveeSpotify.isOldSpotifyVersion
? "SPTCoreURLSessionDataDelegate"
: "SPTDataLoaderService"
}

func URLSession(
_ session: URLSession,
dataTask task: URLSessionDataTask,
didReceiveResponse response: HTTPURLResponse,
completionHandler handler: @escaping (URLSession.ResponseDisposition) -> Void
) {
orig.URLSession(session, dataTask: task, didReceiveResponse: response, completionHandler: handler)
}

static let targetName = "SPTCoreURLSessionDataDelegate"
func URLSession(
_ session: URLSession,
dataTask task: URLSessionDataTask,
didReceiveData data: Data
) {
guard
let request = task.currentRequest,
let url = request.url
else {
return
}

if url.isBootstrap {
URLSessionHelper.shared.setOrAppend(data, for: url)
return
}

orig.URLSession(session, dataTask: task, didReceiveData: data)
}

func URLSession(
_ session: URLSession,
Expand All @@ -23,16 +55,14 @@ class SPTCoreURLSessionDataDelegateHook: ClassHook<NSObject> {
else {
return
}

if error == nil && url.isBootstrap {

let buffer = URLSessionHelper.shared.obtainData(for: url)!

do {
var bootstrapMessage = try BootstrapMessage(serializedData: buffer)

if UserDefaults.patchType == .notSet {

if bootstrapMessage.attributes["type"]?.stringValue == "premium" {
UserDefaults.patchType = .disabled
showHavePremiumPopUp()
Expand All @@ -46,7 +76,6 @@ class SPTCoreURLSessionDataDelegateHook: ClassHook<NSObject> {
}

if UserDefaults.patchType == .requests {

modifyRemoteConfiguration(&bootstrapMessage.ucsResponse)

orig.URLSession(
Expand All @@ -71,24 +100,4 @@ class SPTCoreURLSessionDataDelegateHook: ClassHook<NSObject> {

orig.URLSession(session, task: task, didCompleteWithError: error)
}

func URLSession(
_ session: URLSession,
dataTask task: URLSessionDataTask,
didReceiveData data: Data
) {
guard
let request = task.currentRequest,
let url = request.url
else {
return
}

if url.isBootstrap {
URLSessionHelper.shared.setOrAppend(data, for: url)
return
}

orig.URLSession(session, dataTask: task, didReceiveData: data)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Foundation

func modifyRemoteConfiguration(_ configuration: inout UcsResponse) {

if UserDefaults.overwriteConfiguration {
configuration.resolve.configuration = try! BundleHelper.shared.resolveConfiguration()
}
Expand All @@ -10,7 +9,6 @@ func modifyRemoteConfiguration(_ configuration: inout UcsResponse) {
}

func modifyAttributes(_ attributes: inout [String: AccountAttribute]) {

attributes["type"] = AccountAttribute.with {
$0.stringValue = "premium"
}
Expand Down
Loading

0 comments on commit 5ba2b4a

Please sign in to comment.