Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
xjbeta committed Sep 25, 2022
2 parents 1306ade + 7a60abd commit bcbc718
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 68 deletions.
6 changes: 4 additions & 2 deletions IINA+.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -938,12 +938,13 @@
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
GCC_OPTIMIZATION_LEVEL = 0;
INFOPLIST_FILE = "iina+/Info.plist";
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.14;
MARKETING_VERSION = 0.6.17;
MARKETING_VERSION = 0.6.18;
PRODUCT_BUNDLE_IDENTIFIER = "com.xjbeta.iina-plus";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand All @@ -962,12 +963,13 @@
DEVELOPMENT_TEAM = "";
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = "iina+/Info.plist";
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.14;
MARKETING_VERSION = 0.6.17;
MARKETING_VERSION = 0.6.18;
PRODUCT_BUNDLE_IDENTIFIER = "com.xjbeta.iina-plus";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/krzyzanowskim/CryptoSwift",
"state" : {
"revision" : "039f56c5d7960f277087a0be51f5eb04ed0ec073",
"version" : "1.5.1"
"revision" : "19b3c3ceed117c5cc883517c4e658548315ba70b",
"version" : "1.6.0"
}
},
{
Expand Down Expand Up @@ -50,17 +50,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/mxcl/PromiseKit",
"state" : {
"revision" : "ed3192004c0b00b4b3d7baa9578ee655c66faae3",
"version" : "6.18.0"
"revision" : "43772616c46a44a9977e41924ae01d0e55f2f9ca",
"version" : "6.18.1"
}
},
{
"identity" : "sdwebimage",
"kind" : "remoteSourceControl",
"location" : "https://github.com/SDWebImage/SDWebImage",
"state" : {
"revision" : "3e48cb68d8e668d146dc59c73fb98cb628616236",
"version" : "5.13.2"
"revision" : "484bc774e1091f622c4856e576ff957b29403676",
"version" : "5.13.3"
}
},
{
Expand All @@ -77,17 +77,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/sparkle-project/Sparkle",
"state" : {
"revision" : "059ce9128775d5a636c7f4716f71a58919d3111e",
"version" : "2.2.1"
"revision" : "8e27997de02457a0c4690ffcfb8ac9c8e1066883",
"version" : "2.2.2"
}
},
{
"identity" : "swift-protobuf",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-protobuf.git",
"state" : {
"revision" : "fa0fcd43f272a260e7f734f23e6dc55e16fcae0a",
"version" : "1.19.1"
"revision" : "88c7d15e1242fdb6ecbafbc7926426a19be1e98a",
"version" : "1.20.2"
}
},
{
Expand Down
5 changes: 5 additions & 0 deletions IINA+/Core Data/DataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class DataManager: NSObject {
try? context.save()
}

func delete(_ bookmark: Bookmark) {
context.delete(bookmark)
try? context.save()
}

func moveBookmark(at oldIndex: Int, to newIndex: Int) {
let bookmarks = requestData()
let oldBookmark = bookmarks[oldIndex]
Expand Down
140 changes: 117 additions & 23 deletions IINA+/Utils/Danmaku/Danmaku.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ import Gzip
import JavaScriptCore
import CryptoSwift
import PromiseKit
import PMKAlamofire
import Marshal
import SDWebImage

protocol DanmakuDelegate {
func send(_ method: DanamkuMethod, text: String, sender: Danmaku)
func send(_ event: DanmakuEvent, sender: Danmaku)
}

protocol DanmakuSubDelegate {
func send(_ method: DanamkuMethod, text: String)
func send(_ event: DanmakuEvent)
}

class Danmaku: NSObject {
Expand All @@ -35,6 +37,7 @@ class Danmaku: NSObject {

let biliLiveServer = URL(string: "wss://broadcastlv.chat.bilibili.com/sub")
var biliLiveIDs = (rid: "", token: "")
var bililiveEmoticons = [BiliLiveEmoticon]()

struct BiliLiveDanmuMsg: Decodable {
struct ResultObj: Decodable {
Expand Down Expand Up @@ -149,10 +152,11 @@ class Danmaku: NSObject {
bililiveRid(roomID).get {
self.biliLiveIDs.rid = $0
}.then {
self.bililiveToken($0)
}.get {
self.biliLiveIDs.token = $0
}.done { _ in
when(fulfilled: self.bililiveToken($0),
self.bililiveEmoticons($0))
}.done {
self.biliLiveIDs.token = $0.0
self.bililiveEmoticons = $0.1
self.socket?.open()
}.catch {
Log("can't find bilibili ids \($0).")
Expand All @@ -172,7 +176,13 @@ class Danmaku: NSObject {
let roomData = (js + "}").data(using: .utf8) ?? Data()
let roomInfo: JSONObject = try JSONParser.JSONObjectWithData(roomData)

self.huyaAnchorUid = try roomInfo.value(for: "id")
if let id: String = try? roomInfo.value(for: "id"),
let uid = Int(id) {
self.huyaAnchorUid = uid
} else {
self.huyaAnchorUid = try roomInfo.value(for: "id")
}

self.socket = .init(url: self.huyaServer!)
self.socket?.delegate = self
self.socket?.open()
Expand All @@ -195,8 +205,8 @@ class Danmaku: NSObject {
}
}

private func sendDM(_ str: String) {
delegate?.send(.sendDM, text: str, sender: self)
private func sendDM(_ event: DanmakuEvent) {
delegate?.send(event, sender: self)
}

private func initDouYuSocket(_ roomID: String) {
Expand Down Expand Up @@ -294,6 +304,75 @@ class Danmaku: NSObject {
}
}

struct BiliLiveEmoticon: Unmarshaling {
let emoji: String
let url: String
let width: Int
let height: Int
let identity: Int
let emoticonUnique: String
let emoticonId: Int

var emoticonData: Data?

init(object: MarshaledObject) throws {
emoji = try object.value(for: "emoji")
let u: String = try object.value(for: "url")
url = u.replacingOccurrences(of: "http://", with: "https://")
width = try object.value(for: "width")
height = try object.value(for: "height")
identity = try object.value(for: "identity")
emoticonUnique = try object.value(for: "emoticon_unique")
emoticonId = try object.value(for: "emoticon_id")
}
}


func bililiveEmoticons(_ rid: String) -> Promise<([BiliLiveEmoticon])> {
return Promise { resolver in
AF.request("https://api.live.bilibili.com/xlive/web-ucenter/v2/emoticon/GetEmoticons?platform=pc&room_id=\(rid)").response {

struct BiliLiveEmoticonData: Unmarshaling {
let emoticons: [BiliLiveEmoticon]
let pkgId: Int
let pkgName: String
init(object: MarshaledObject) throws {
emoticons = try object.value(for: "emoticons")
pkgId = try object.value(for: "pkg_id")
pkgName = try object.value(for: "pkg_name")
}
}

do {
let json = try JSONParser.JSONObjectWithData($0.data ?? Data())
let emoticonData: [BiliLiveEmoticonData] = try json.value(for: "data.data")
var emoticons = emoticonData.flatMap {
$0.emoticons
}

when(fulfilled: emoticons.enumerated().map { item -> Promise<()> in
let key = "BiliLive_Emoticons_" + item.element.emoticonUnique
if let image = SDImageCache.shared.imageFromCache(forKey: key) {
emoticons[item.offset].emoticonData = image.sd_imageData()
return .value
} else {
return AF.request(item.element.url).responseData().done {
emoticons[item.offset].emoticonData = $0.data
SDImageCache.shared.store(NSImage(data: $0.data), forKey: key)
}
}
}).done {
resolver.fulfill(emoticons)
}.catch {
resolver.reject($0)
}
} catch let error {
resolver.reject(error)
}
}
}
}

/*
func testedBilibiliAPI() {
let p = ["aid": 31027408,
Expand Down Expand Up @@ -377,7 +456,7 @@ new Uint8Array(sendRegisterGroups(["live:\(id)", "chat:\(id)"]));
default:
break
}
delegate?.send(.liveDMServer, text: "error", sender: self)
delegate?.send(.init(method: .liveDMServer, text: "error"), sender: self)
}

func webSocket(_ webSocket: SRWebSocket, didReceiveMessageWith data: Data) {
Expand All @@ -391,6 +470,7 @@ new Uint8Array(sendRegisterGroups(["live:\(id)", "chat:\(id)"]));
return
} else if data.count == 26 {
Log("bililive connect success")
self.delegate?.send(.init(method: .liveDMServer, text: ""), sender: self)
return
}

Expand Down Expand Up @@ -431,14 +511,26 @@ new Uint8Array(sendRegisterGroups(["live:\(id)", "chat:\(id)"]));
d.removeAll()
}
}


datas.compactMap {
try? JSONDecoder().decode(BiliLiveDanmuMsg.self, from: $0)
}.compactMap {
$0.info.compactMap ({ $0.msg }).first
}.forEach {
sendDM($0)

datas.forEach { data in
if let s = String(data: data, encoding: .utf8)?.subString(from: "\"emoticon_unique\":\"", to: "\","),
let emoticon = self.bililiveEmoticons.first(where: { $0.emoticonUnique == s }) {

guard let base64 = emoticon.emoticonData?.base64EncodedString(),
base64.count > 0 else { return }

let ext = NSString(string: emoticon.url.lastPathComponent).pathExtension

let size = Int(emoticon.width / 2) > 125 ? 125 : Int(emoticon.width / 2)

sendDM(.init(
method: .sendDM,
text: "",
imageSrc: "data:image/\(ext);base64," + base64,
imageWidth: size))
} else if let s = (try? JSONDecoder().decode(BiliLiveDanmuMsg.self, from: data))?.info.compactMap ({ $0.msg }).first {
sendDM(.init(method: .sendDM, text: s))
}
}
case .huya:
let bytes = [UInt8](data)
Expand All @@ -450,6 +542,7 @@ new Uint8Array(sendRegisterGroups(["live:\(id)", "chat:\(id)"]));

if str == "EWebSocketCommandType.EWSCmdS2C_RegisterGroupRsp" {
Log("huya connect success")
self.delegate?.send(.init(method: .liveDMServer, text: ""), sender: self)
return
} else if str.starts(with: "EWebSocketCommandType") {
Log("huya websocket info \(str)")
Expand All @@ -471,7 +564,7 @@ new Uint8Array(sendRegisterGroups(["live:\(id)", "chat:\(id)"]));
msg.iProtocolType == 2,
!huyaBlockList.contains(where: msg.sMsg.contains) {

sendDM(msg.sMsg)
sendDM(.init(method: .sendDM, text: msg.sMsg))
}


Expand Down Expand Up @@ -590,14 +683,15 @@ new Uint8Array(sendRegisterGroups(["live:\(id)", "chat:\(id)"]));
return
}
DispatchQueue.main.async {
self.sendDM(dm)
self.sendDM(.init(method: .sendDM, text: dm))
}
} else if $0.starts(with: "type@=error") {
Log("douyu socket disconnected: \($0)")
self.delegate?.send(.liveDMServer, text: "error", sender: self)
self.delegate?.send(.init(method: .liveDMServer, text: "error"), sender: self)
socket?.close()
} else if $0.starts(with: "type@=loginres") {
Log("douyu content success")
self.delegate?.send(.init(method: .liveDMServer, text: ""), sender: self)
} else if $0 == "type@=mrkl" {
Log("Danmaku HeartBeatRsp")
heartBeatCount = 0
Expand Down Expand Up @@ -636,8 +730,8 @@ new Uint8Array(sendRegisterGroups(["live:\(id)", "chat:\(id)"]));
}

extension Danmaku: DanmakuSubDelegate {
func send(_ method: DanamkuMethod, text: String) {
delegate?.send(method, text: text, sender: self)
func send(_ event: DanmakuEvent) {
delegate?.send(event, sender: self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion IINA+/Utils/Danmaku/DouYinDM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class DouYinDM: NSObject {
// Log(msgs.map({ $0.content }))

msgs.forEach {
delegate?.send(.sendDM, text: $0.content)
delegate?.send(.init(method: .sendDM, text: $0.content))
}

requestTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { [weak self] timer in
Expand Down
Loading

0 comments on commit bcbc718

Please sign in to comment.